vinyl-cache/bin/vinyld/cache/cache_shmlog.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
#include "config.h"
32
33
#include "cache_int.h"
34
35
#include <stdio.h>
36
#include <stdlib.h>
37
38
#include "vsl_priv.h"
39
#include "vmb.h"
40
41
#include "common/heritage.h"
42
#include "common/vsmw.h"
43
44
/* ------------------------------------------------------------
45
 * strands helpers - move elsewhere?
46
 */
47
48
static unsigned
49 204396
strands_len(const struct strands *s)
50
{
51 204396
        unsigned r = 0;
52
        int i;
53
54 204396
        CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC);
55 409035
        for (i = 0; i < s->n; i++) {
56 204639
                if (s->p[i] == NULL || *s->p[i] == '\0')
57 19538
                        continue;
58 185101
                r += vstrlen(s->p[i]);
59 185101
        }
60
61 204396
        return (r);
62
}
63
64
/*
65
 * like VRT_Strands(), but truncating instead of failing for end of buffer
66
 *
67
 * returns number of bytes including NUL
68
 */
69
static unsigned
70 205169
strands_cat(char *buf, unsigned bufl, const struct strands *s)
71
{
72 205169
        unsigned l = 0, ll;
73
        int i;
74
75
        /* NUL-terminated */
76 205169
        assert(bufl > 0);
77 205169
        bufl--;
78 205169
        CHECK_OBJ_NOTNULL(s, STRANDS_MAGIC);
79
80 391441
        for (i = 0; i < s->n && bufl > 0; i++) {
81 186276
                if (s->p[i] == NULL || *s->p[i] == '\0')
82 12
                        continue;
83 186264
                ll = vmin_t(unsigned, vstrlen(s->p[i]), bufl);
84 186264
                memcpy(buf, s->p[i], ll);
85 186264
                l += ll;
86 186264
                buf += ll;
87 186264
                bufl -= ll;
88 186264
        }
89 205165
        *buf = '\0';    /* NUL-terminated */
90 205165
        return (l + 1);
91
}
92
93
/* These cannot be struct lock, which depends on vsm/vsl working */
94
static pthread_mutex_t vsl_mtx;
95
static pthread_mutex_t vsc_mtx;
96
static pthread_mutex_t vsm_mtx;
97
98
static struct VSL_head          *vsl_head;
99
static const uint32_t           *vsl_end;
100
static uint32_t                 *vsl_ptr;
101
static unsigned                 vsl_segment_n;
102
static ssize_t                  vsl_segsize;
103
104
struct VSC_main *VSC_C_main;
105
106
static void
107 2031367
vsl_sanity(const struct vsl_log *vsl)
108
{
109
110 2031367
        CHECK_OBJ_NOTNULL(vsl, VSL_LOG_MAGIC);
111 2031367
        AN(vsl);
112 2031367
        AN(vsl->wlp);
113 2031367
        AN(vsl->wlb);
114 2031367
        AN(vsl->wle);
115 2031367
        assert(vsl->wlb <= vsl->wlp);
116 2031367
        assert(vsl->wlp <= vsl->wle);
117 2031367
}
118
119
/*--------------------------------------------------------------------
120
 * Check if the VSL_tag is masked by parameter bitmap
121
 */
122
123
static inline int
124 1550225
vsl_tag_is_masked(enum VSL_tag_e tag)
125
{
126 1550225
        volatile uint8_t *bm = &cache_param->vsl_mask[0];
127
        uint8_t b;
128
129 1550225
        assert(tag > SLT__Bogus);
130 1550225
        assert(tag < SLT__Reserved);
131 1550225
        bm += ((unsigned)tag >> 3);
132 1550225
        b = (0x80 >> ((unsigned)tag & 7));
133 1550225
        return (*bm & b);
134
}
135
136
int
137 5141
VSL_tag_is_masked(enum VSL_tag_e tag)
138
{
139 5141
        return (vsl_tag_is_masked(tag));
140
}
141
142
/*--------------------------------------------------------------------
143
 * Lay down a header fields, and return pointer to the next record
144
 */
145
146
static inline uint32_t *
147 1244366
vsl_hdr(enum VSL_tag_e tag, uint32_t *p, unsigned len, vxid_t vxid)
148
{
149
150 1244366
        AZ((uintptr_t)p & 0x3);
151 1244366
        assert(tag > SLT__Bogus);
152 1244366
        assert(tag < SLT__Reserved);
153 1244366
        AZ(len & ~VSL_LENMASK);
154
155 1244366
        p[2] = vxid.vxid >> 32;
156 1244366
        p[1] = vxid.vxid;
157 2488732
        p[0] = (((unsigned)tag & VSL_IDMASK) << VSL_IDSHIFT) |
158 1244366
             (VSL_VERSION_3 << VSL_VERSHIFT) |
159 1244366
             len;
160 1244366
        return (VSL_END(p, len));
161
}
162
163
/*--------------------------------------------------------------------
164
 * Space available in a VSL buffer when accounting for overhead
165
 */
166
167
static unsigned
168 302916
vsl_space(const struct vsl_log *vsl)
169
{
170
        ptrdiff_t mlen;
171
172 302916
        mlen = vsl->wle - vsl->wlp;
173 302916
        assert(mlen >= 0);
174 302916
        if (mlen < VSL_OVERHEAD + 1)
175 8
                return (0);
176 302908
        mlen -= VSL_OVERHEAD;
177 302908
        mlen *= sizeof *vsl->wlp;
178 302908
        if (mlen > cache_param->vsl_reclen)
179 302826
                mlen = cache_param->vsl_reclen;
180 302908
        return(mlen);
181 302916
}
182
183
/*--------------------------------------------------------------------
184
 * Wrap the VSL buffer
185
 */
186
187
static void
188 0
vsl_wrap(void)
189
{
190
191 0
        assert(vsl_ptr >= vsl_head->log);
192 0
        assert(vsl_ptr < vsl_end);
193 0
        vsl_segment_n += VSL_SEGMENTS - (vsl_segment_n % VSL_SEGMENTS);
194 0
        assert(vsl_segment_n % VSL_SEGMENTS == 0);
195 0
        vsl_head->offset[0] = 0;
196 0
        vsl_head->log[0] = VSL_ENDMARKER;
197 0
        VWMB();
198 0
        if (vsl_ptr != vsl_head->log) {
199 0
                *vsl_ptr = VSL_WRAPMARKER;
200 0
                vsl_ptr = vsl_head->log;
201 0
        }
202 0
        vsl_head->segment_n = vsl_segment_n;
203 0
        VSC_C_main->shm_cycles++;
204 0
}
205
206
/*--------------------------------------------------------------------
207
 * Reserve bytes for a record, wrap if necessary
208
 */
209
210
static uint32_t *
211 368018
vsl_get(unsigned len, unsigned records, unsigned flushes)
212
{
213
        uint32_t *p;
214
        int err;
215
216 368018
        err = pthread_mutex_trylock(&vsl_mtx);
217 368018
        if (err == EBUSY) {
218 2390
                PTOK(pthread_mutex_lock(&vsl_mtx));
219 2390
                VSC_C_main->shm_cont++;
220 2390
        } else {
221 365628
                AZ(err);
222
        }
223 368018
        assert(vsl_ptr < vsl_end);
224 368018
        AZ((uintptr_t)vsl_ptr & 0x3);
225
226 368018
        VSC_C_main->shm_writes++;
227 368018
        VSC_C_main->shm_flushes += flushes;
228 368018
        VSC_C_main->shm_records += records;
229 368018
        VSC_C_main->shm_bytes +=
230 368018
            VSL_BYTES(VSL_OVERHEAD + VSL_WORDS((uint64_t)len));
231
232
        /* Wrap if necessary */
233 368018
        if (VSL_END(vsl_ptr, len) >= vsl_end)
234 0
                vsl_wrap();
235
236 368018
        p = vsl_ptr;
237 368018
        vsl_ptr = VSL_END(vsl_ptr, len);
238 368018
        assert(vsl_ptr < vsl_end);
239 368018
        AZ((uintptr_t)vsl_ptr & 0x3);
240
241 368018
        *vsl_ptr = VSL_ENDMARKER;
242
243 368026
        while ((vsl_ptr - vsl_head->log) / vsl_segsize >
244 368026
            vsl_segment_n % VSL_SEGMENTS) {
245 8
                vsl_segment_n++;
246 8
                vsl_head->offset[vsl_segment_n % VSL_SEGMENTS] =
247 8
                    vsl_ptr - vsl_head->log;
248
        }
249
250 368018
        PTOK(pthread_mutex_unlock(&vsl_mtx));
251
        /* Implicit VWMB() in mutex op ensures ENDMARKER and new table
252
           values are seen before new segment number */
253 368018
        vsl_head->segment_n = vsl_segment_n;
254
255 368018
        return (p);
256
}
257
258
/*--------------------------------------------------------------------
259
 * Stick a finished record into VSL.
260
 */
261
262
static void
263 240584
vslr(enum VSL_tag_e tag, vxid_t vxid, const char *b, unsigned len)
264
{
265
        uint32_t *p;
266
        unsigned mlen;
267
268 240584
        mlen = cache_param->vsl_reclen;
269
270
        /* Truncate */
271 240584
        if (len > mlen)
272 0
                len = mlen;
273
274 240584
        p = vsl_get(len, 1, 0);
275
276 240584
        memcpy(p + VSL_OVERHEAD, b, len);
277
278
        /*
279
         * the vxid needs to be written before the barrier to
280
         * ensure it is valid when vsl_hdr() marks the record
281
         * ready by writing p[0]
282
         */
283 240584
        p[2] = vxid.vxid >> 32;
284 240584
        p[1] = vxid.vxid;
285 240584
        VWMB();
286 240584
        (void)vsl_hdr(tag, p, len, vxid);
287 240584
}
288
289
/*--------------------------------------------------------------------
290
 * Add a unbuffered record to VSL
291
 *
292
 * NB: This variant should be used sparingly and only for low volume
293
 * NB: since it significantly adds to the mutex load on the VSL.
294
 */
295
296
void
297 349194
VSLv(enum VSL_tag_e tag, vxid_t vxid, const char *fmt, va_list ap)
298
{
299 349194
        unsigned n, mlen = cache_param->vsl_reclen;
300 349194
        char buf[mlen];
301
302 349194
        AN(fmt);
303 349194
        if (vsl_tag_is_masked(tag))
304 109393
                return;
305
306 239801
        if (strchr(fmt, '%') == NULL) {
307 4688
                vslr(tag, vxid, fmt, vstrlen(fmt) + 1);
308 4688
        } else {
309 235113
                n = vsnprintf(buf, mlen, fmt, ap);
310 235113
                n = vmin(n, mlen - 1);
311 235113
                buf[n++] = '\0'; /* NUL-terminated */
312 235113
                vslr(tag, vxid, buf, n);
313
        }
314
315 349194
}
316
317
void
318 800
VSLs(enum VSL_tag_e tag, vxid_t vxid, const struct strands *s)
319
{
320 800
        unsigned n, mlen = cache_param->vsl_reclen;
321 800
        char buf[mlen];
322
323 800
        if (vsl_tag_is_masked(tag))
324 0
                return;
325
326 800
        n = strands_cat(buf, mlen, s);
327
328 800
        vslr(tag, vxid, buf, n);
329 800
}
330
331
void
332 342607
VSL(enum VSL_tag_e tag, vxid_t vxid, const char *fmt, ...)
333
{
334
        va_list ap;
335
336 342607
        va_start(ap, fmt);
337 342607
        VSLv(tag, vxid, fmt, ap);
338 342607
        va_end(ap);
339 342607
}
340
341
/*--------------------------------------------------------------------*/
342
343
void
344 160606
VSL_Flush(struct vsl_log *vsl, int overflow)
345
{
346
        uint32_t *p;
347
        unsigned l;
348
349 160606
        vsl_sanity(vsl);
350 160606
        l = pdiff(vsl->wlb, vsl->wlp);
351 160606
        if (l == 0)
352 33607
                return;
353
354 126999
        assert(l >= 8);
355
356 126999
        p = vsl_get(l, vsl->wlr, overflow);
357
358 126999
        memcpy(p + VSL_OVERHEAD, vsl->wlb, l);
359 126999
        p[1] = l;
360 126999
        VWMB();
361 126999
        p[0] = ((((unsigned)SLT__Batch & 0xff) << VSL_IDSHIFT));
362 126999
        vsl->wlp = vsl->wlb;
363 126999
        vsl->wlr = 0;
364 160606
}
365
366
/*--------------------------------------------------------------------
367
 * Buffered VSLs
368
 */
369
370
static char *
371 998720
vslb_get(struct vsl_log *vsl, enum VSL_tag_e tag, unsigned *length)
372
{
373 998720
        unsigned mlen = cache_param->vsl_reclen;
374
        char *retval;
375
376 998720
        vsl_sanity(vsl);
377 998720
        if (*length < mlen)
378 996028
                mlen = *length;
379
380 998720
        if (VSL_END(vsl->wlp, mlen) > vsl->wle)
381 40
                VSL_Flush(vsl, 1);
382
383 998720
        retval = VSL_DATA(vsl->wlp);
384
385
        /* If it still doesn't fit, truncate */
386 998720
        if (VSL_END(vsl->wlp, mlen) > vsl->wle)
387 8
                mlen = vsl_space(vsl);
388
389 998720
        vsl->wlp = vsl_hdr(tag, vsl->wlp, mlen, vsl->wid);
390 998720
        vsl->wlr++;
391 998720
        *length = mlen;
392 998720
        return (retval);
393
}
394
395
static void
396 491828
vslb_simple(struct vsl_log *vsl, enum VSL_tag_e tag,
397
    unsigned length, const char *str)
398
{
399
        char *p;
400
401 491828
        if (length == 0)
402 38401
                length = vstrlen(str);
403 491828
        length += 1; // NUL
404 491828
        p = vslb_get(vsl, tag, &length);
405 491828
        memcpy(p, str, length - 1);
406 491828
        p[length - 1] = '\0';
407
408 491828
        if (DO_DEBUG(DBG_SYNCVSL))
409 46639
                VSL_Flush(vsl, 0);
410 491828
}
411
412
/*--------------------------------------------------------------------
413
 * VSL-buffered-txt
414
 */
415
416
void
417 565313
VSLbt(struct vsl_log *vsl, enum VSL_tag_e tag, txt t)
418
{
419
420 565313
        Tcheck(t);
421 565313
        if (vsl_tag_is_masked(tag))
422 86092
                return;
423
424 479221
        vslb_simple(vsl, tag, Tlen(t), t.b);
425 565313
}
426
427
/*--------------------------------------------------------------------
428
 * VSL-buffered-strands
429
 */
430
void
431 241593
VSLbs(struct vsl_log *vsl, enum VSL_tag_e tag, const struct strands *s)
432
{
433
        unsigned l;
434
        char *p;
435
436 241593
        if (vsl_tag_is_masked(tag))
437 37194
                return;
438
439 204399
        l = strands_len(s) + 1;
440 204399
        p = vslb_get(vsl, tag, &l);
441
442 204399
        (void)strands_cat(p, l, s);
443
444 204399
        if (DO_DEBUG(DBG_SYNCVSL))
445 18616
                VSL_Flush(vsl, 0);
446 241593
}
447
448
/*--------------------------------------------------------------------
449
 * VSL-buffered
450
 */
451
452
void
453 377169
VSLbv(struct vsl_log *vsl, enum VSL_tag_e tag, const char *fmt, va_list ap)
454
{
455
        char *p, *p1;
456 377169
        unsigned n = 0, mlen;
457
        va_list ap2;
458
459 377169
        AN(fmt);
460 377169
        if (vsl_tag_is_masked(tag))
461 61598
                return;
462
463
        /*
464
         * If there are no printf-expansions, don't waste time expanding them
465
         */
466 315571
        if (strchr(fmt, '%') == NULL) {
467 12588
                vslb_simple(vsl, tag, 0, fmt);
468 12588
                return;
469
        }
470
471
        /*
472
         * If the format is trivial, deal with it directly
473
         */
474 302983
        if (!vstrcmp(fmt, "%s")) {
475 32
                p1 = va_arg(ap, char *);
476 32
                vslb_simple(vsl, tag, 0, p1);
477 32
                return;
478
        }
479
480 302951
        vsl_sanity(vsl);
481
482 302951
        mlen = vsl_space(vsl);
483
484
        // First attempt, only if any space at all
485 302951
        if (mlen > 0) {
486 302865
                p = VSL_DATA(vsl->wlp);
487 302865
                va_copy(ap2, ap);
488 302865
                n = vsnprintf(p, mlen, fmt, ap2);
489 302865
                va_end(ap2);
490 302865
        }
491
492
        // Second attempt, if a flush might help
493 302951
        if (mlen == 0 || (n + 1 > mlen && n + 1 <= cache_param->vsl_reclen)) {
494 292
                VSL_Flush(vsl, 1);
495 292
                mlen = vsl_space(vsl);
496 292
                p = VSL_DATA(vsl->wlp);
497 292
                n = vsnprintf(p, mlen, fmt, ap);
498 292
        }
499 302991
        if (n + 1 < mlen)
500 301947
                mlen = n + 1;
501 302991
        (void)vslb_get(vsl, tag, &mlen);
502
503 302991
        if (DO_DEBUG(DBG_SYNCVSL))
504 35543
                VSL_Flush(vsl, 0);
505 377209
}
506
507
void
508 362908
VSLb(struct vsl_log *vsl, enum VSL_tag_e tag, const char *fmt, ...)
509
{
510
        va_list ap;
511
512 362908
        vsl_sanity(vsl);
513 362908
        va_start(ap, fmt);
514 362908
        VSLbv(vsl, tag, fmt, ap);
515 362908
        va_end(ap);
516 362908
}
517
518
#define Tf6 "%ju.%06ju"
519
#define Ta6(t) (uintmax_t)floor((t)), (uintmax_t)floor((t) * 1e6) % 1000000U
520
521
void
522 136213
VSLb_ts(struct vsl_log *vsl, const char *event, vtim_real first,
523
    vtim_real *pprev, vtim_real now)
524
{
525
526
        /*
527
         * XXX: Make an option to turn off some unnecessary timestamp
528
         * logging. This must be done carefully because some functions
529
         * (e.g. V1L_Open) takes the last timestamp as its initial
530
         * value for timeout calculation.
531
         */
532 136213
        vsl_sanity(vsl);
533 136213
        AN(event);
534 136213
        AN(pprev);
535 136213
        assert(!isnan(now) && now != 0.);
536 272426
        VSLb(vsl, SLT_Timestamp, "%s: " Tf6 " " Tf6 " " Tf6,
537 136213
            event, Ta6(now), Ta6(now - first), Ta6(now - *pprev));
538 136213
        *pprev = now;
539 136213
}
540
541
void
542 71856
VSLb_ts_req(struct req *req, const char *event, vtim_real now)
543
{
544
545 71856
        if (isnan(req->t_first) || req->t_first == 0.)
546 1469
                req->t_first = req->t_prev = now;
547 71856
        VSLb_ts(req->vsl, event, req->t_first, &req->t_prev, now);
548 71856
}
549
550
void
551 64385
VSLb_ts_busyobj(struct busyobj *bo, const char *event, vtim_real now)
552
{
553
554 64385
        if (isnan(bo->t_first) || bo->t_first == 0.)
555 9547
                bo->t_first = bo->t_prev = now;
556 64385
        VSLb_ts(bo->vsl, event, bo->t_first, &bo->t_prev, now);
557 64385
}
558
559
void
560 12041
VSLb_bin(struct vsl_log *vsl, enum VSL_tag_e tag, ssize_t len, const void *ptr)
561
{
562
        unsigned mlen;
563
        char *p;
564
565 12041
        vsl_sanity(vsl);
566 12041
        AN(ptr);
567 12041
        if (vsl_tag_is_masked(tag))
568 6045
                return;
569 5996
        mlen = cache_param->vsl_reclen;
570
571
        /* Truncate */
572 5996
        len = vmin_t(ssize_t, len, mlen);
573
574 5996
        assert(vsl->wlp <= vsl->wle);
575
576
        /* Flush if necessary */
577 5996
        if (VSL_END(vsl->wlp, len) > vsl->wle)
578 36
                VSL_Flush(vsl, 1);
579 5996
        assert(VSL_END(vsl->wlp, len) <= vsl->wle);
580 5996
        p = VSL_DATA(vsl->wlp);
581 5996
        memcpy(p, ptr, len);
582 5996
        vsl->wlp = vsl_hdr(tag, vsl->wlp, len, vsl->wid);
583 5996
        assert(vsl->wlp <= vsl->wle);
584 5996
        vsl->wlr++;
585
586 5996
        if (DO_DEBUG(DBG_SYNCVSL))
587 2500
                VSL_Flush(vsl, 0);
588 12041
}
589
590
/*--------------------------------------------------------------------
591
 * Setup a VSL buffer
592
 */
593
594
void
595 33798
VSL_Init(struct vsl_log *vsl, void *ptr, size_t len)
596
{
597
598 33798
        AN(vsl);
599 33798
        AN(ptr);
600 33798
        AN(len);
601
602 33798
        INIT_OBJ(vsl, VSL_LOG_MAGIC);
603 33798
        vsl->wlp = ptr;
604 33798
        vsl->wlb = ptr;
605 33798
        vsl->wle = ptr;
606 33798
        vsl->wle += len / sizeof(*vsl->wle);
607 33798
        vsl->wlr = 0;
608 33798
        vsl->wid = NO_VXID;
609 33798
        vsl_sanity(vsl);
610 33798
}
611
612
void
613 11668
VSL_Alloc(struct vsl_log *vsl)
614
{
615 11668
        size_t len = cache_param->vsl_buffer;
616 11668
        void *ptr = malloc(len);
617 11668
        AN(ptr);
618 11668
        VSL_Init(vsl, ptr, len);
619 11668
        vsl->flags |= VSL_LOG_F_MALLOCED;
620 11668
}
621
622
void
623 7696
VSL_Free(struct vsl_log *vsl)
624
{
625
626 7696
        CHECK_OBJ_NOTNULL(vsl, VSL_LOG_MAGIC);
627 7696
        assert(vsl->flags & VSL_LOG_F_MALLOCED);
628 7696
        free(vsl->wlb);
629 7696
        memset(vsl, 0, sizeof *vsl);
630 7696
}
631
632
/*--------------------------------------------------------------------*/
633
634
void
635 405
VSL_ChgId(struct vsl_log *vsl, const char *typ, const char *why, vxid_t vxid)
636
{
637
        vxid_t ovxid;
638
639 405
        vsl_sanity(vsl);
640 405
        ovxid = vsl->wid;
641 405
        VSLb(vsl, SLT_Link, "%s %ju %s", typ, VXID(vxid), why);
642 405
        VSL_End(vsl);
643 405
        vsl->wid = vxid;
644 405
        VSLb(vsl, SLT_Begin, "%s %ju %s", typ, VXID(ovxid), why);
645 405
}
646
647
/*--------------------------------------------------------------------*/
648
649
void
650 25782
VSL_End(struct vsl_log *vsl)
651
{
652
        txt t;
653 25782
        char p[] = "";
654
655 25782
        vsl_sanity(vsl);
656 25782
        assert(!IS_NO_VXID(vsl->wid));
657 25782
        t.b = p;
658 25782
        t.e = p;
659 25782
        VSLbt(vsl, SLT_End, t);
660 25782
        VSL_Flush(vsl, 0);
661 25782
        vsl->wid = NO_VXID;
662 25782
}
663
664
static void v_matchproto_(vsm_lock_f)
665 154552
vsm_vsc_lock(void)
666
{
667 154552
        PTOK(pthread_mutex_lock(&vsc_mtx));
668 154552
}
669
670
static void v_matchproto_(vsm_lock_f)
671 154552
vsm_vsc_unlock(void)
672
{
673 154552
        PTOK(pthread_mutex_unlock(&vsc_mtx));
674 154552
}
675
676
static void v_matchproto_(vsm_lock_f)
677 197296
vsm_vsmw_lock(void)
678
{
679 197296
        PTOK(pthread_mutex_lock(&vsm_mtx));
680 197296
}
681
682
static void v_matchproto_(vsm_lock_f)
683 197296
vsm_vsmw_unlock(void)
684
{
685 197296
        PTOK(pthread_mutex_unlock(&vsm_mtx));
686 197296
}
687
688
/*--------------------------------------------------------------------*/
689
690
void
691 3892
VSM_Init(void)
692
{
693
        unsigned u;
694
695 3892
        assert(UINT_MAX % VSL_SEGMENTS == VSL_SEGMENTS - 1);
696
697 3892
        PTOK(pthread_mutex_init(&vsl_mtx, &mtxattr_errorcheck));
698 3892
        PTOK(pthread_mutex_init(&vsc_mtx, &mtxattr_errorcheck));
699 3892
        PTOK(pthread_mutex_init(&vsm_mtx, &mtxattr_errorcheck));
700
701 3892
        vsc_lock = vsm_vsc_lock;
702 3892
        vsc_unlock = vsm_vsc_unlock;
703 3892
        vsmw_lock = vsm_vsmw_lock;
704 3892
        vsmw_unlock = vsm_vsmw_unlock;
705
706 3892
        heritage.proc_vsmw = VSMW_New(heritage.vsm_fd, 0640, "_.index");
707 3892
        AN(heritage.proc_vsmw);
708
709 3892
        VSC_C_main = VSC_main_New(NULL, NULL, "");
710 3892
        AN(VSC_C_main);
711
712 3892
        AN(heritage.proc_vsmw);
713 7784
        vsl_head = VSMW_Allocf(heritage.proc_vsmw, NULL, VSL_CLASS,
714 3892
            cache_param->vsl_space, VSL_CLASS);
715 3892
        AN(vsl_head);
716 7784
        vsl_segsize = ((cache_param->vsl_space - sizeof *vsl_head) /
717 3892
            sizeof *vsl_end) / VSL_SEGMENTS;
718 3892
        vsl_end = vsl_head->log + vsl_segsize * VSL_SEGMENTS;
719
        /* Make segment_n always overflow on first log wrap to make any
720
           problems with regard to readers on that event visible */
721 3892
        vsl_segment_n = UINT_MAX - (VSL_SEGMENTS - 1);
722 3892
        AZ(vsl_segment_n % VSL_SEGMENTS);
723 3892
        vsl_ptr = vsl_head->log;
724 3892
        *vsl_ptr = VSL_ENDMARKER;
725
726 3892
        memset(vsl_head, 0, sizeof *vsl_head);
727 3892
        vsl_head->segsize = vsl_segsize;
728 3892
        vsl_head->offset[0] = 0;
729 3892
        vsl_head->segment_n = vsl_segment_n;
730 31136
        for (u = 1; u < VSL_SEGMENTS; u++)
731 27244
                vsl_head->offset[u] = -1;
732 3892
        VWMB();
733 3892
        memcpy(vsl_head->marker, VSL_HEAD_MARKER, sizeof vsl_head->marker);
734 3892
}