varnish-cache/bin/varnishtest/vtc_barrier.c
0
/*-
1
 * Copyright (c) 2005 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Dridi Boukelmoune <dridi@varnish-software.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <poll.h>
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <unistd.h>
37
38
#include <sys/socket.h>
39
#include <sys/time.h> /* for MUSL */
40
41
#include "vtc.h"
42
#include "vtcp.h"
43
#include "vtim.h"
44
#include "vsa.h"
45
46
enum barrier_e {
47
        BARRIER_NONE = 0,
48
        BARRIER_COND,
49
        BARRIER_SOCK,
50
};
51
52
struct barrier {
53
        unsigned                magic;
54
#define BARRIER_MAGIC           0x7b54c275
55
        char                    *name;
56
        VTAILQ_ENTRY(barrier)   list;
57
        pthread_mutex_t         mtx;
58
        pthread_cond_t          cond;
59
60
        int                     waiters;
61
        int                     expected;
62
        int                     cyclic;
63
64
        enum barrier_e          type;
65
        union {
66
                int             cond_cycle;
67
                pthread_t       sock_thread;
68
        };
69
};
70
71
static VTAILQ_HEAD(, barrier)   barriers = VTAILQ_HEAD_INITIALIZER(barriers);
72
73
static struct barrier *
74 8320
barrier_new(const char *name, struct vtclog *vl)
75
{
76
        struct barrier *b;
77
78 8320
        ALLOC_OBJ(b, BARRIER_MAGIC);
79 8320
        AN(b);
80 8320
        if (!pthread_equal(pthread_self(), vtc_thread))
81 0
                vtc_fatal(vl,
82 0
                    "Barrier %s can only be created on the top thread", name);
83 8320
        REPLACE(b->name, name);
84
85 8320
        PTOK(pthread_mutex_init(&b->mtx, NULL));
86 8320
        PTOK(pthread_cond_init(&b->cond, NULL));
87 8320
        b->waiters = 0;
88 8320
        b->expected = 0;
89 8320
        VTAILQ_INSERT_TAIL(&barriers, b, list);
90 8320
        return (b);
91
}
92
93
/**********************************************************************
94
 * Init a barrier
95
 */
96
97
static void
98 8320
barrier_expect(struct barrier *b, const char *av, struct vtclog *vl)
99
{
100
        unsigned expected;
101
102 8320
        if (b->type != BARRIER_NONE)
103 0
                vtc_fatal(vl,
104 0
                    "Barrier(%s) use error: already initialized", b->name);
105
106 8320
        AZ(b->expected);
107 8320
        AZ(b->waiters);
108 8320
        expected = strtoul(av, NULL, 0);
109 8320
        if (expected < 2)
110 0
                vtc_fatal(vl,
111
                    "Barrier(%s) use error: wrong expectation (%u)",
112 0
                    b->name, expected);
113
114 8320
        b->expected = expected;
115 8320
}
116
117
static void
118 6440
barrier_cond(struct barrier *b, const char *av, struct vtclog *vl)
119
{
120
121 6440
        CHECK_OBJ_NOTNULL(b, BARRIER_MAGIC);
122 6440
        PTOK(pthread_mutex_lock(&b->mtx));
123 6440
        barrier_expect(b, av, vl);
124 6440
        b->type = BARRIER_COND;
125 6440
        PTOK(pthread_mutex_unlock(&b->mtx));
126 6440
}
127
128
static void *
129 1880
barrier_sock_thread(void *priv)
130
{
131
        struct barrier *b;
132
        struct vtclog *vl;
133
        const char *err;
134 1880
        char buf[vsa_suckaddr_len];
135
        const struct suckaddr *sua;
136
137
        char abuf[VTCP_ADDRBUFSIZE], pbuf[VTCP_PORTBUFSIZE];
138
        int i, sock, *conns;
139
        struct pollfd pfd[1];
140
141 1880
        CAST_OBJ_NOTNULL(b, priv, BARRIER_MAGIC);
142 1880
        assert(b->type == BARRIER_SOCK);
143
144 1880
        PTOK(pthread_mutex_lock(&b->mtx));
145
146 1880
        vl = vtc_logopen("%s", b->name);
147 1880
        pthread_cleanup_push(vtc_logclose, vl);
148
149 1880
        sock = VTCP_listen_on(default_listen_addr, NULL, b->expected, &err);
150 1880
        if (sock < 0) {
151 0
                PTOK(pthread_cond_signal(&b->cond));
152 0
                PTOK(pthread_mutex_unlock(&b->mtx));
153 0
                vtc_fatal(vl, "Barrier(%s) %s fails: %s (errno=%d)",
154 0
                    b->name, err, strerror(errno), errno);
155
        }
156 1880
        assert(sock > 0);
157 1880
        VTCP_nonblocking(sock);
158 1880
        sua = VSA_getsockname(sock, buf, sizeof buf);
159 1880
        AN(sua);
160 1880
        VTCP_name(sua, abuf, sizeof abuf, pbuf, sizeof pbuf);
161
162 1880
        macro_def(vl, b->name, "addr", "%s", abuf);
163 1880
        macro_def(vl, b->name, "port", "%s", pbuf);
164 1880
        if (VSA_Get_Proto(sua) == AF_INET)
165 1880
                macro_def(vl, b->name, "sock", "%s:%s", abuf, pbuf);
166
        else
167 0
                macro_def(vl, b->name, "sock", "[%s]:%s", abuf, pbuf);
168
169 1880
        PTOK(pthread_cond_signal(&b->cond));
170 1880
        PTOK(pthread_mutex_unlock(&b->mtx));
171
172 1880
        conns = calloc(b->expected, sizeof *conns);
173 1880
        AN(conns);
174
175 42375
        while (!vtc_stop && !vtc_error) {
176 42055
                pfd[0].fd = sock;
177 42055
                pfd[0].events = POLLIN;
178
179 42055
                i = poll(pfd, 1, 100);
180 42055
                if (i == 0)
181 35895
                        continue;
182 6160
                if (i < 0) {
183 0
                        if (errno == EINTR)
184 0
                                continue;
185 0
                        closefd(&sock);
186 0
                        vtc_fatal(vl,
187
                            "Barrier(%s) select fails: %s (errno=%d)",
188 0
                            b->name, strerror(errno), errno);
189
                }
190 6160
                assert(i == 1);
191 6160
                assert(b->waiters <= b->expected);
192 6160
                if (b->waiters == b->expected)
193 0
                        vtc_fatal(vl,
194
                            "Barrier(%s) use error: "
195
                            "more waiters than the %u expected",
196 0
                            b->name, b->expected);
197
198 6160
                i = accept(sock, NULL, NULL);
199 6160
                if (i < 0) {
200 0
                        closefd(&sock);
201 0
                        vtc_fatal(vl,
202
                            "Barrier(%s) accept fails: %s (errno=%d)",
203 0
                            b->name, strerror(errno), errno);
204
                }
205
206
                /* NB. We don't keep track of the established connections, only
207
                 *     that connections were made to the barrier's socket.
208
                 */
209 6160
                conns[b->waiters] = i;
210
211 6160
                if (++b->waiters < b->expected) {
212 7760
                        vtc_log(vl, 4, "Barrier(%s) wait %u of %u",
213 3880
                            b->name, b->waiters, b->expected);
214 3880
                        continue;
215
                }
216
217 2280
                vtc_log(vl, 4, "Barrier(%s) wake %u", b->name, b->expected);
218 8440
                for (i = 0; i < b->expected; i++)
219 6160
                        closefd(&conns[i]);
220
221 2280
                if (b->cyclic)
222 720
                        b->waiters = 0;
223
                else
224 1560
                        break;
225
        }
226
227 1880
        if (b->waiters % b->expected > 0) {
228
                /* wake up outstanding waiters */
229 0
                for (i = 0; i < b->waiters; i++)
230 0
                        closefd(&conns[i]);
231 0
                if (!vtc_error)
232 0
                        vtc_fatal(vl, "Barrier(%s) has %u outstanding waiters",
233 0
                            b->name, b->waiters);
234 0
        }
235
236 1880
        macro_undef(vl, b->name, "addr");
237 1880
        macro_undef(vl, b->name, "port");
238 1880
        macro_undef(vl, b->name, "sock");
239 1880
        closefd(&sock);
240 1880
        free(conns);
241 1880
        pthread_cleanup_pop(0);
242 1880
        vtc_logclose(vl);
243
        return (NULL);
244 1880
}
245
246
static void
247 1880
barrier_sock(struct barrier *b, const char *av, struct vtclog *vl)
248
{
249
250 1880
        CHECK_OBJ_NOTNULL(b, BARRIER_MAGIC);
251 1880
        PTOK(pthread_mutex_lock(&b->mtx));
252 1880
        barrier_expect(b, av, vl);
253 1880
        b->type = BARRIER_SOCK;
254
255
        /* NB. We can use the BARRIER_COND's pthread_cond_t to wait until the
256
         *     socket is ready for convenience.
257
         */
258 1880
        PTOK(pthread_create(&b->sock_thread, NULL, barrier_sock_thread, b));
259 1880
        PTOK(pthread_cond_wait(&b->cond, &b->mtx));
260 1880
        PTOK(pthread_mutex_unlock(&b->mtx));
261 1880
}
262
263
static void
264 720
barrier_cyclic(struct barrier *b, struct vtclog *vl)
265
{
266
        enum barrier_e t;
267
        int w;
268
269 720
        CHECK_OBJ_NOTNULL(b, BARRIER_MAGIC);
270
271 720
        PTOK(pthread_mutex_lock(&b->mtx));
272 720
        t = b->type;
273 720
        w = b->waiters;
274 720
        PTOK(pthread_mutex_unlock(&b->mtx));
275
276 720
        if (t == BARRIER_NONE)
277 0
                vtc_fatal(vl,
278 0
                    "Barrier(%s) use error: not initialized", b->name);
279
280 720
        if (w != 0)
281 0
                vtc_fatal(vl,
282 0
                    "Barrier(%s) use error: already in use", b->name);
283
284 720
        PTOK(pthread_mutex_lock(&b->mtx));
285 720
        b->cyclic = 1;
286 720
        PTOK(pthread_mutex_unlock(&b->mtx));
287 720
}
288
289
/**********************************************************************
290
 * Sync a barrier
291
 */
292
293
static void
294 16640
barrier_cond_sync(struct barrier *b, struct vtclog *vl)
295
{
296
        struct timespec ts;
297
        int r, w, c;
298
299 16640
        CHECK_OBJ_NOTNULL(b, BARRIER_MAGIC);
300 16640
        assert(b->type == BARRIER_COND);
301
302 16640
        PTOK(pthread_mutex_lock(&b->mtx));
303 16640
        w = b->waiters;
304 16640
        assert(w <= b->expected);
305
306 16640
        if (w == b->expected)
307 0
                w = -1;
308
        else
309 16640
                b->waiters = ++w;
310
311 16640
        c = b->cond_cycle;
312 16640
        PTOK(pthread_mutex_unlock(&b->mtx));
313
314 16640
        if (w < 0)
315 0
                vtc_fatal(vl,
316
                    "Barrier(%s) use error: more waiters than the %u expected",
317 0
                    b->name, b->expected);
318
319 16640
        PTOK(pthread_mutex_lock(&b->mtx));
320 16640
        if (w == b->expected) {
321 7120
                vtc_log(vl, 4, "Barrier(%s) wake %u", b->name, b->expected);
322 7120
                b->cond_cycle++;
323 7120
                if (b->cyclic)
324 1080
                        b->waiters = 0;
325 7120
                PTOK(pthread_cond_broadcast(&b->cond));
326 7120
        } else {
327 19040
                vtc_log(vl, 4, "Barrier(%s) wait %u of %u",
328 9520
                    b->name, b->waiters, b->expected);
329 9520
                do {
330 33138
                        ts = VTIM_timespec(VTIM_real() + .1);
331 33138
                        r = pthread_cond_timedwait(&b->cond, &b->mtx, &ts);
332 33138
                        assert(r == 0 || r == ETIMEDOUT);
333 56757
                } while (!vtc_stop && !vtc_error && r == ETIMEDOUT &&
334 23619
                    c == b->cond_cycle);
335
        }
336 16640
        PTOK(pthread_mutex_unlock(&b->mtx));
337 16640
}
338
339
static void
340 3800
barrier_sock_sync(const struct barrier *b, struct vtclog *vl)
341
{
342
        struct vsb *vsb;
343
        const char *err;
344
        char buf[32];
345
        int i, sock;
346
        ssize_t sz;
347
348 3800
        CHECK_OBJ_NOTNULL(b, BARRIER_MAGIC);
349 3800
        assert(b->type == BARRIER_SOCK);
350
351 3800
        vsb = macro_expandf(vl, "${%s_sock}", b->name);
352 3800
        vtc_log(vl, 4, "Barrier(%s) sync with socket", b->name);
353
354 3800
        sock = VTCP_open(VSB_data(vsb), NULL, 0., &err);
355 3800
        if (sock < 0)
356 0
                vtc_fatal(vl, "Barrier(%s) connection failed: %s",
357 0
                    b->name, err);
358
359 3800
        VSB_destroy(&vsb);
360
361 3800
        sz = read(sock, buf, sizeof buf); /* XXX loop with timeout? */
362 3800
        i = errno;
363 3800
        closefd(&sock);
364
365 3800
        if (sz < 0)
366 0
                vtc_fatal(vl, "Barrier(%s) read failed: %s (errno=%d)",
367 0
                    b->name, strerror(i), i);
368 3800
        if (sz > 0)
369 0
                vtc_fatal(vl, "Barrier(%s) unexpected data (%zdB)",
370 0
                    b->name, sz);
371 3800
}
372
373
static void
374 20434
barrier_sync(struct barrier *b, struct vtclog *vl)
375
{
376
377 20434
        CHECK_OBJ_NOTNULL(b, BARRIER_MAGIC);
378
379 20434
        switch (b->type) {
380
        case BARRIER_NONE:
381 0
                vtc_fatal(vl,
382 0
                    "Barrier(%s) use error: not initialized", b->name);
383
                break;
384
        case BARRIER_COND:
385 16636
                barrier_cond_sync(b, vl);
386 16636
                break;
387
        case BARRIER_SOCK:
388 3798
                barrier_sock_sync(b, vl);
389 3798
                break;
390
        default:
391 0
                WRONG("Wrong barrier type");
392 0
        }
393 20434
}
394
395
/* SECTION: barrier barrier
396
 *
397
 * NOTE: This command is available everywhere commands are given.
398
 *
399
 * Barriers allows you to synchronize different threads to make sure events
400
 * occur in the right order. It's even possible to use them in VCL.
401
 *
402
 * First, it's necessary to declare the barrier::
403
 *
404
 *         barrier bNAME TYPE NUMBER [-cyclic]
405
 *
406
 * With the arguments being:
407
 *
408
 * bNAME
409
 *         this is the name of the barrier, used to identify it when you'll
410
 *         create sync points. It must start with 'b'.
411
 *
412
 * TYPE
413
 *         it can be "cond" (mutex) or "sock" (socket) and sets internal
414
 *         behavior. If you don't need VCL synchronization, use cond.
415
 *
416
 * NUMBER
417
 *         number of sync point needed to go through the barrier.
418
 *
419
 * \-cyclic
420
 *         if present, the barrier will reset itself and be ready for another
421
 *         round once gotten through.
422
 *
423
 * Then, to add a sync point::
424
 *
425
 *         barrier bNAME sync
426
 *
427
 * This will block the parent thread until the number of sync points for bNAME
428
 * reaches the NUMBER given in the barrier declaration.
429
 *
430
 * If you wish to synchronize the VCL, you need to declare a "sock" barrier.
431
 * This will emit a macro definition named "bNAME_sock" that you can use in
432
 * VCL (after importing the vtc vmod)::
433
 *
434
 *         vtc.barrier_sync("${bNAME_sock}");
435
 *
436
 * This function returns 0 if everything went well and is the equivalent of
437
 * ``barrier bNAME sync`` at the VTC top-level.
438
 *
439
 *
440
 */
441
442
void
443 68879
cmd_barrier(CMD_ARGS)
444
{
445
        struct barrier *b, *b2;
446
        int r;
447
448 68879
        (void)priv;
449
450 68879
        if (av == NULL) {
451
                /* Reset and free */
452 48440
                VTAILQ_FOREACH_SAFE(b, &barriers, list, b2) {
453 8320
                        r = pthread_mutex_trylock(&b->mtx);
454 8320
                        assert(r == 0 || r == EBUSY);
455 8320
                        switch (b->type) {
456
                        case BARRIER_COND:
457 6440
                                break;
458
                        case BARRIER_SOCK:
459 1880
                                PTOK(pthread_join(b->sock_thread, NULL));
460 1880
                                break;
461
                        default:
462 0
                                WRONG("Wrong barrier type");
463 0
                        }
464 8320
                        if (r == 0)
465 8318
                                PTOK(pthread_mutex_unlock(&b->mtx));
466 8320
                }
467 40120
                return;
468
        }
469
470 28759
        AZ(strcmp(av[0], "barrier"));
471 28759
        av++;
472
473 28759
        VTC_CHECK_NAME(vl, av[0], "Barrier", 'b');
474 56354
        VTAILQ_FOREACH(b, &barriers, list)
475 48034
                if (!strcmp(b->name, av[0]))
476 20439
                        break;
477 28759
        if (b == NULL)
478 8320
                b = barrier_new(av[0], vl);
479 28759
        av++;
480
481 58236
        for (; *av != NULL; av++) {
482 29477
                if (!strcmp(*av, "cond")) {
483 6440
                        av++;
484 6440
                        AN(*av);
485 6440
                        barrier_cond(b, *av, vl);
486 6440
                        continue;
487
                }
488 23037
                if (!strcmp(*av, "sock")) {
489 1880
                        av++;
490 1880
                        AN(*av);
491 1880
                        barrier_sock(b, *av, vl);
492 1880
                        continue;
493
                }
494 21157
                if (!strcmp(*av, "sync")) {
495 20437
                        barrier_sync(b, vl);
496 20437
                        continue;
497
                }
498 720
                if (!strcmp(*av, "-cyclic")) {
499 720
                        barrier_cyclic(b, vl);
500 720
                        continue;
501
                }
502 0
                vtc_fatal(vl, "Unknown barrier argument: %s", *av);
503
        }
504 68879
}