vinyl-cache/lib/libvinylapi/vsl_dispatch.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
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
32
#include "config.h"
33
34
#include <stdarg.h>
35
#include <stdint.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
#include "vdef.h"
41
#include "vas.h"
42
#include "miniobj.h"
43
44
#include "vqueue.h"
45
#include "vre.h"
46
#include "vtim.h"
47
#include "vtree.h"
48
49
#include "vapi/vsl.h"
50
51
#include "vsl_api.h"
52
53
#define VTX_CACHE 10
54
#define VTX_BUFSIZE_MIN 64
55
#define VTX_SHMCHUNKS 3
56
57
static const char * const vsl_t_names[VSL_t__MAX] = {
58
        [VSL_t_unknown] = "unknown",
59
        [VSL_t_sess]    = "sess",
60
        [VSL_t_req]     = "req",
61
        [VSL_t_bereq]   = "bereq",
62
        [VSL_t_raw]     = "raw",
63
};
64
65
static const char * const vsl_r_names[VSL_r__MAX] = {
66
        [VSL_r_unknown] = "unknown",
67
        [VSL_r_http_1]  = "HTTP/1",
68
        [VSL_r_rxreq]   = "rxreq",
69
        [VSL_r_esi]     = "esi",
70
        [VSL_r_restart] = "restart",
71
        [VSL_r_pass]    = "pass",
72
        [VSL_r_fetch]   = "fetch",
73
        [VSL_r_bgfetch] = "bgfetch",
74
        [VSL_r_pipe]    = "pipe",
75
};
76
77
struct vtx;
78
VTAILQ_HEAD(vtxhead, vtx);
79
80
struct vslc_raw {
81
        unsigned                magic;
82
#define VSLC_RAW_MAGIC          0x247EBD44
83
84
        struct VSL_cursor       cursor;
85
86
        const uint32_t          *ptr;
87
};
88
89
struct synth {
90
        unsigned                magic;
91
#define SYNTH_MAGIC             0xC654479F
92
93
        VTAILQ_ENTRY(synth)     list;
94
        size_t                  offset;
95
        uint32_t                data[VSL_OVERHEAD + VSL_WORDS(64)];
96
};
97
VTAILQ_HEAD(synthhead, synth);
98
99
enum chunk_t {
100
        chunk_t__unassigned,
101
        chunk_t_shm,
102
        chunk_t_buf,
103
};
104
105
struct chunk {
106
        unsigned                                magic;
107
#define CHUNK_MAGIC                             0x48DC0194
108
        enum chunk_t                            type;
109
        union {
110
                struct {
111
                        struct VSLC_ptr         start;
112
                        VTAILQ_ENTRY(chunk)     shmref;
113
                } shm;
114
                struct {
115
                        uint32_t                *data;
116
                        size_t                  space;
117
                } buf;
118
        };
119
        size_t                                  len;
120
        struct vtx                              *vtx;
121
        VTAILQ_ENTRY(chunk)                     list;
122
};
123
VTAILQ_HEAD(chunkhead, chunk);
124
125
struct vslc_vtx {
126
        unsigned                magic;
127
#define VSLC_VTX_MAGIC          0x74C6523F
128
129
        struct VSL_cursor       cursor;
130
131
        struct vtx              *vtx;
132
        struct synth            *synth;
133
        struct chunk            *chunk;
134
        size_t                  chunkstart;
135
        size_t                  offset;
136
};
137
138
struct vtx_key {
139
        uint64_t                vxid;
140
        VRBT_ENTRY(vtx_key)     entry;
141
};
142
VRBT_HEAD(vtx_tree, vtx_key);
143
144
struct vtx {
145
        struct vtx_key          key;
146
        unsigned                magic;
147
#define VTX_MAGIC               0xACC21D09
148
        VTAILQ_ENTRY(vtx)       list_child;
149
        VTAILQ_ENTRY(vtx)       list_vtx;
150
151
        double                  t_start;
152
        unsigned                flags;
153
#define VTX_F_BEGIN             0x1 /* Begin record processed */
154
#define VTX_F_END               0x2 /* End record processed */
155
#define VTX_F_COMPLETE          0x4 /* Marked complete. No new children
156
                                       should be appended */
157
#define VTX_F_READY             0x8 /* This vtx and all it's children are
158
                                       complete */
159
160
        enum VSL_transaction_e  type;
161
        enum VSL_reason_e       reason;
162
163
        struct vtx              *parent;
164
        struct vtxhead          child;
165
        unsigned                n_child;
166
        unsigned                n_childready;
167
        unsigned                n_descend;
168
169
        struct synthhead        synth;
170
171
        struct chunk            shmchunks[VTX_SHMCHUNKS];
172
        struct chunkhead        shmchunks_free;
173
174
        struct chunkhead        chunks;
175
        size_t                  len;
176
177
        struct vslc_vtx         c;
178
};
179
180
struct VSLQ {
181
        unsigned                magic;
182
#define VSLQ_MAGIC              0x23A8BE97
183
184
        struct VSL_data         *vsl;
185
        struct VSL_cursor       *c;
186
        struct vslq_query       *query;
187
188
        enum VSL_grouping_e     grouping;
189
190
        /* Structured mode */
191
        struct vtx_tree         tree;
192
        struct vtxhead          ready;
193
        struct vtxhead          incomplete;
194
        int                     n_outstanding;
195
        struct chunkhead        shmrefs;
196
        struct vtxhead          cache;
197
        unsigned                n_cache;
198
199
        /* Rate limiting */
200
        double                  credits;
201
        vtim_mono               last_use;
202
203
        /* Raw mode */
204
        struct {
205
                struct vslc_raw         c;
206
                struct VSL_transaction  trans;
207
                struct VSL_transaction  *ptrans[2];
208
                struct VSLC_ptr         start;
209
                ssize_t                 len;
210
                ssize_t                 offset;
211
        } raw;
212
};
213
214
static void vtx_synth_rec(struct vtx *vtx, unsigned tag, const char *fmt, ...);
215
/*lint -esym(534, vtx_diag) */
216
static int vtx_diag(struct vtx *vtx, const char *msg);
217
/*lint -esym(534, vtx_diag_tag) */
218
static int vtx_diag_tag(struct vtx *vtx, const uint32_t *ptr,
219
    const char *reason);
220
221
static inline int
222 41570
vtx_keycmp(const struct vtx_key *a, const struct vtx_key *b)
223
{
224 41570
        if (a->vxid < b->vxid)
225 1052
                return (-1);
226 40518
        if (a->vxid > b->vxid)
227 16558
                return (1);
228 23960
        return (0);
229 41570
}
230
231 2831
VRBT_GENERATE_REMOVE_COLOR(vtx_tree, vtx_key, entry, static)
232 6806
VRBT_GENERATE_REMOVE(vtx_tree, vtx_key, entry, static)
233 3376
VRBT_GENERATE_INSERT_COLOR(vtx_tree, vtx_key, entry, static)
234 4148
VRBT_GENERATE_INSERT_FINISH(vtx_tree, vtx_key, entry, static)
235 7628
VRBT_GENERATE_INSERT(vtx_tree, vtx_key, entry, vtx_keycmp, static)
236 44333
VRBT_GENERATE_FIND(vtx_tree, vtx_key, entry, vtx_keycmp, static)
237
238
static enum vsl_status v_matchproto_(vslc_next_f)
239 86341
vslc_raw_next(const struct VSL_cursor *cursor)
240
{
241
        struct vslc_raw *c;
242
243 86341
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
244 86341
        assert(&c->cursor == cursor);
245
246 86341
        AN(c->ptr);
247 86341
        if (c->cursor.rec.ptr == NULL) {
248 43466
                c->cursor.rec.ptr = c->ptr;
249 43466
                return (vsl_more);
250
        } else {
251 42875
                c->cursor.rec.ptr = NULL;
252 42875
                return (vsl_end);
253
        }
254 86341
}
255
256
static enum vsl_status v_matchproto_(vslc_reset_f)
257 11007
vslc_raw_reset(const struct VSL_cursor *cursor)
258
{
259
        struct vslc_raw *c;
260
261 11007
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
262 11007
        assert(&c->cursor == cursor);
263
264 11007
        AN(c->ptr);
265 11007
        c->cursor.rec.ptr = NULL;
266
267 11007
        return (vsl_end);
268
}
269
270
static const struct vslc_tbl vslc_raw_tbl = {
271
        .magic  = VSLC_TBL_MAGIC,
272
        .delete = NULL,
273
        .next   = vslc_raw_next,
274
        .reset  = vslc_raw_reset,
275
        .check  = NULL,
276
};
277
278
static enum vsl_status v_matchproto_(vslc_next_f)
279 278658
vslc_vtx_next(const struct VSL_cursor *cursor)
280
{
281
        struct vslc_vtx *c;
282
        const uint32_t *ptr;
283
        unsigned overrun;
284
285 278658
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
286 278658
        assert(&c->cursor == cursor);
287 278658
        CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
288
289 278658
        do {
290 300173
                CHECK_OBJ_ORNULL(c->synth, SYNTH_MAGIC);
291 300173
                if (c->synth != NULL && c->synth->offset == c->offset) {
292
                        /* We're at the offset of the next synth record,
293
                           point to it and advance the pointer */
294 978
                        c->cursor.rec.ptr = c->synth->data;
295 978
                        c->synth = VTAILQ_NEXT(c->synth, list);
296 978
                } else {
297 299195
                        overrun = c->offset > c->vtx->len;
298 299195
                        AZ(overrun);
299 299195
                        if (c->offset == c->vtx->len)
300 26977
                                return (vsl_end);
301
302
                        /* Advance chunk pointer */
303 272218
                        if (c->chunk == NULL) {
304 7975
                                c->chunk = VTAILQ_FIRST(&c->vtx->chunks);
305 7975
                                c->chunkstart = 0;
306 7975
                        }
307 272218
                        CHECK_OBJ_NOTNULL(c->chunk, CHUNK_MAGIC);
308 279379
                        while (c->offset >= c->chunkstart + c->chunk->len) {
309 7161
                                c->chunkstart += c->chunk->len;
310 7161
                                c->chunk = VTAILQ_NEXT(c->chunk, list);
311 7161
                                CHECK_OBJ_NOTNULL(c->chunk, CHUNK_MAGIC);
312
                        }
313
314
                        /* Point to the next stored record */
315 272218
                        if (c->chunk->type == chunk_t_shm)
316 223722
                                ptr = c->chunk->shm.start.ptr;
317
                        else {
318 48496
                                assert(c->chunk->type == chunk_t_buf);
319 48496
                                ptr = c->chunk->buf.data;
320
                        }
321 272218
                        c->cursor.rec.ptr = ptr + c->offset - c->chunkstart;
322 544436
                        c->offset += VSL_NEXT(c->cursor.rec.ptr) -
323 272218
                            c->cursor.rec.ptr;
324
                }
325 273196
        } while (VSL_TAG(c->cursor.rec.ptr) == SLT__Batch);
326
327 251681
        return (vsl_more);
328 278658
}
329
330
static enum vsl_status v_matchproto_(vslc_reset_f)
331 11323
vslc_vtx_reset(const struct VSL_cursor *cursor)
332
{
333
        struct vslc_vtx *c;
334
335 11323
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
336 11323
        assert(&c->cursor == cursor);
337 11323
        CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
338 11323
        c->synth = VTAILQ_FIRST(&c->vtx->synth);
339 11323
        c->chunk = NULL;
340 11323
        c->chunkstart = 0;
341 11323
        c->offset = 0;
342 11323
        c->cursor.rec.ptr = NULL;
343
344 11323
        return (vsl_end);
345
}
346
347
static const struct vslc_tbl vslc_vtx_tbl = {
348
        .magic  = VSLC_TBL_MAGIC,
349
        .delete = NULL,
350
        .next   = vslc_vtx_next,
351
        .reset  = vslc_vtx_reset,
352
        .check  = NULL,
353
};
354
355
/* Create a buf chunk */
356
static struct chunk *
357 1392
chunk_newbuf(struct vtx *vtx, const uint32_t *ptr, size_t len)
358
{
359
        struct chunk *chunk;
360
361 1392
        ALLOC_OBJ(chunk, CHUNK_MAGIC);
362 1392
        XXXAN(chunk);
363 1392
        chunk->type = chunk_t_buf;
364 1392
        chunk->vtx = vtx;
365 1392
        chunk->buf.space = VTX_BUFSIZE_MIN;
366 1392
        while (chunk->buf.space < len)
367 0
                chunk->buf.space *= 2;
368 1392
        chunk->buf.data = malloc(sizeof (uint32_t) * chunk->buf.space);
369 1392
        AN(chunk->buf.data);
370 1392
        memcpy(chunk->buf.data, ptr, sizeof (uint32_t) * len);
371 1392
        chunk->len = len;
372 1392
        return (chunk);
373
}
374
375
/* Free a buf chunk */
376
static void
377 1391
chunk_freebuf(struct chunk **pchunk)
378
{
379
        struct chunk *chunk;
380
381 1391
        TAKE_OBJ_NOTNULL(chunk, pchunk, CHUNK_MAGIC);
382 1391
        assert(chunk->type == chunk_t_buf);
383 1391
        free(chunk->buf.data);
384 1391
        FREE_OBJ(chunk);
385 1391
}
386
387
/* Append a set of records to a chunk */
388
static void
389 18986
chunk_appendbuf(struct chunk *chunk, const uint32_t *ptr, size_t len)
390
{
391
392 18986
        CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
393 18986
        assert(chunk->type == chunk_t_buf);
394 18986
        if (chunk->buf.space < chunk->len + len) {
395 5894
                while (chunk->buf.space < chunk->len + len)
396 2947
                        chunk->buf.space *= 2;
397 5894
                chunk->buf.data = realloc(chunk->buf.data,
398 2947
                    sizeof (uint32_t) * chunk->buf.space);
399 2947
        }
400 18986
        memcpy(chunk->buf.data + chunk->len, ptr, sizeof (uint32_t) * len);
401 18986
        chunk->len += len;
402 18986
}
403
404
/* Transform a shm chunk to a buf chunk */
405
static void
406 0
chunk_shm_to_buf(struct VSLQ *vslq, struct chunk *chunk)
407
{
408
        struct vtx *vtx;
409
        struct chunk *buf;
410
411 0
        CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
412 0
        assert(chunk->type == chunk_t_shm);
413 0
        vtx = chunk->vtx;
414 0
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
415
416 0
        buf = VTAILQ_PREV(chunk, chunkhead, list);
417 0
        if (buf != NULL && buf->type == chunk_t_buf)
418
                /* Previous is a buf chunk, append to it */
419 0
                chunk_appendbuf(buf, chunk->shm.start.ptr, chunk->len);
420
        else {
421
                /* Create a new buf chunk and insert it before this */
422 0
                buf = chunk_newbuf(vtx, chunk->shm.start.ptr, chunk->len);
423 0
                AN(buf);
424 0
                VTAILQ_INSERT_BEFORE(chunk, buf, list);
425
        }
426
427
        /* Reset cursor chunk pointer, vslc_vtx_next will set it correctly */
428 0
        vtx->c.chunk = NULL;
429
430
        /* Remove from the shmref list and vtx, and put chunk back
431
           on the free list */
432 0
        VTAILQ_REMOVE(&vslq->shmrefs, chunk, shm.shmref);
433 0
        VTAILQ_REMOVE(&vtx->chunks, chunk, list);
434 0
        VTAILQ_INSERT_HEAD(&vtx->shmchunks_free, chunk, list);
435 0
}
436
437
/* Append a set of records to a vtx structure */
438
static enum vsl_status
439 27449
vtx_append(struct VSLQ *vslq, struct vtx *vtx, const struct VSLC_ptr *start,
440
    size_t len)
441
{
442
        struct chunk *chunk;
443
        enum vsl_check i;
444
445 27449
        AN(vtx);
446 27449
        AN(len);
447 27449
        AN(start);
448
449 27449
        i = VSL_Check(vslq->c, start);
450 27449
        if (i == vsl_check_e_inval)
451 0
                return (vsl_e_overrun);
452
453 27449
        if (i == vsl_check_valid && !VTAILQ_EMPTY(&vtx->shmchunks_free)) {
454
                /* Shmref it */
455 7063
                chunk = VTAILQ_FIRST(&vtx->shmchunks_free);
456 7063
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
457 7063
                assert(chunk->type == chunk_t_shm);
458 7063
                assert(chunk->vtx == vtx);
459 7063
                VTAILQ_REMOVE(&vtx->shmchunks_free, chunk, list);
460 7063
                chunk->shm.start = *start;
461 7063
                chunk->len = len;
462 7063
                VTAILQ_INSERT_TAIL(&vtx->chunks, chunk, list);
463
464
                /* Append to shmref list */
465 7063
                VTAILQ_INSERT_TAIL(&vslq->shmrefs, chunk, shm.shmref);
466 7063
        } else {
467
                /* Buffer it */
468 20386
                chunk = VTAILQ_LAST(&vtx->chunks, chunkhead);
469 20386
                CHECK_OBJ_ORNULL(chunk, CHUNK_MAGIC);
470 20386
                if (chunk != NULL && chunk->type == chunk_t_buf) {
471
                        /* Tail is a buf chunk, append to that */
472 18994
                        chunk_appendbuf(chunk, start->ptr, len);
473 18994
                } else {
474
                        /* Append new buf chunk */
475 1392
                        chunk = chunk_newbuf(vtx, start->ptr, len);
476 1392
                        AN(chunk);
477 1392
                        VTAILQ_INSERT_TAIL(&vtx->chunks, chunk, list);
478
                }
479
        }
480 27449
        vtx->len += len;
481 27449
        return (vsl_more);
482 27449
}
483
484
/* Allocate a new vtx structure */
485
static struct vtx *
486 4150
vtx_new(struct VSLQ *vslq)
487
{
488
        struct vtx *vtx;
489
        int i;
490
491 4150
        AN(vslq);
492 4150
        if (vslq->n_cache) {
493 2235
                AZ(VTAILQ_EMPTY(&vslq->cache));
494 2235
                vtx = VTAILQ_FIRST(&vslq->cache);
495 2235
                VTAILQ_REMOVE(&vslq->cache, vtx, list_child);
496 2235
                vslq->n_cache--;
497 2235
        } else {
498 1915
                ALLOC_OBJ(vtx, VTX_MAGIC);
499 1913
                AN(vtx);
500
501 1913
                VTAILQ_INIT(&vtx->child);
502 1913
                VTAILQ_INIT(&vtx->shmchunks_free);
503 7652
                for (i = 0; i < VTX_SHMCHUNKS; i++) {
504 5739
                        vtx->shmchunks[i].magic = CHUNK_MAGIC;
505 5739
                        vtx->shmchunks[i].type = chunk_t_shm;
506 5739
                        vtx->shmchunks[i].vtx = vtx;
507 5739
                        VTAILQ_INSERT_TAIL(&vtx->shmchunks_free,
508
                            &vtx->shmchunks[i], list);
509 5739
                }
510 1913
                VTAILQ_INIT(&vtx->chunks);
511 1913
                VTAILQ_INIT(&vtx->synth);
512 1913
                vtx->c.magic = VSLC_VTX_MAGIC;
513 1913
                vtx->c.vtx = vtx;
514 1913
                vtx->c.cursor.priv_tbl = &vslc_vtx_tbl;
515 1913
                vtx->c.cursor.priv_data = &vtx->c;
516
        }
517
518 4148
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
519 4148
        vtx->key.vxid = 0;
520 4148
        vtx->t_start = VTIM_mono();
521 4148
        vtx->flags = 0;
522 4148
        vtx->type = VSL_t_unknown;
523 4148
        vtx->reason = VSL_r_unknown;
524 4148
        vtx->parent = NULL;
525 4148
        vtx->n_child = 0;
526 4148
        vtx->n_childready = 0;
527 4148
        vtx->n_descend = 0;
528 4148
        vtx->len = 0;
529 4148
        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
530
531 4148
        return (vtx);
532
}
533
534
/* Disuse a vtx and all it's children, freeing any resources held. Free or
535
   cache the vtx for later use */
536
static void
537 4146
vtx_retire(struct VSLQ *vslq, struct vtx **pvtx)
538
{
539
        struct vtx *vtx;
540
        struct vtx *child;
541
        struct synth *synth;
542
        struct chunk *chunk;
543
544 4146
        AN(vslq);
545 4146
        TAKE_OBJ_NOTNULL(vtx, pvtx, VTX_MAGIC);
546
547 4146
        assert((vtx->flags & (VTX_F_COMPLETE | VTX_F_READY)) == (VTX_F_COMPLETE | VTX_F_READY));
548 4146
        AZ(vtx->parent);
549
550 4568
        while (!VTAILQ_EMPTY(&vtx->child)) {
551 422
                child = VTAILQ_FIRST(&vtx->child);
552 422
                assert(child->parent == vtx);
553 422
                AN(vtx->n_child);
554 422
                assert(vtx->n_descend >= child->n_descend + 1);
555 422
                VTAILQ_REMOVE(&vtx->child, child, list_child);
556 422
                child->parent = NULL;
557 422
                vtx->n_child--;
558 422
                vtx->n_descend -= child->n_descend + 1;
559 422
                vtx_retire(vslq, &child);
560 422
                AZ(child);
561
        }
562 4146
        AZ(vtx->n_child);
563 4146
        AZ(vtx->n_descend);
564 4146
        vtx->n_childready = 0;
565
        // remove rval is no way to check if element was present
566 4146
        (void)VRBT_REMOVE(vtx_tree, &vslq->tree, &vtx->key);
567 4146
        vtx->key.vxid = 0;
568 4146
        vtx->flags = 0;
569
570 5112
        while (!VTAILQ_EMPTY(&vtx->synth)) {
571 966
                synth = VTAILQ_FIRST(&vtx->synth);
572 966
                CHECK_OBJ_NOTNULL(synth, SYNTH_MAGIC);
573 966
                VTAILQ_REMOVE(&vtx->synth, synth, list);
574 966
                FREE_OBJ(synth);
575
        }
576
577 12598
        while (!VTAILQ_EMPTY(&vtx->chunks)) {
578 8452
                chunk = VTAILQ_FIRST(&vtx->chunks);
579 8452
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
580 8452
                VTAILQ_REMOVE(&vtx->chunks, chunk, list);
581 8452
                if (chunk->type == chunk_t_shm) {
582 7060
                        VTAILQ_REMOVE(&vslq->shmrefs, chunk, shm.shmref);
583 7060
                        VTAILQ_INSERT_HEAD(&vtx->shmchunks_free, chunk, list);
584 7060
                } else {
585 1392
                        assert(chunk->type == chunk_t_buf);
586 1392
                        chunk_freebuf(&chunk);
587 1392
                        AZ(chunk);
588
                }
589
        }
590 4146
        vtx->len = 0;
591 4146
        AN(vslq->n_outstanding);
592 4146
        vslq->n_outstanding--;
593
594 4146
        if (vslq->n_cache < VTX_CACHE) {
595 4074
                VTAILQ_INSERT_HEAD(&vslq->cache, vtx, list_child);
596 4074
                vslq->n_cache++;
597 4074
        } else
598 72
                FREE_OBJ(vtx);
599
600 4146
}
601
602
/* Lookup a vtx by vxid from the managed list */
603
static struct vtx *
604 30201
vtx_lookup(const struct VSLQ *vslq, uint64_t vxid)
605
{
606
        struct vtx_key lkey, *key;
607
        struct vtx *vtx;
608
609 30201
        AN(vslq);
610 30201
        lkey.vxid = vxid;
611 30201
        key = VRBT_FIND(vtx_tree, &vslq->tree, &lkey);
612 30201
        if (key == NULL)
613 6249
                return (NULL);
614 23952
        CAST_OBJ_NOTNULL(vtx, (void *)key, VTX_MAGIC);
615 23952
        return (vtx);
616 30201
}
617
618
/* Insert a new vtx into the managed list */
619
static struct vtx *
620 4148
vtx_add(struct VSLQ *vslq, uint64_t vxid)
621
{
622
        struct vtx *vtx;
623
624 4148
        AN(vslq);
625 4148
        vtx = vtx_new(vslq);
626 4148
        AN(vtx);
627 4148
        vtx->key.vxid = vxid;
628 4148
        AZ(VRBT_INSERT(vtx_tree, &vslq->tree, &vtx->key));
629 4148
        VTAILQ_INSERT_TAIL(&vslq->incomplete, vtx, list_vtx);
630 4148
        vslq->n_outstanding++;
631 4148
        return (vtx);
632
}
633
634
/* Mark a vtx complete, update child counters and if possible push it or
635
   it's top parent to the ready state */
636
static void
637 4147
vtx_mark_complete(struct VSLQ *vslq, struct vtx *vtx)
638
{
639
640 4147
        AN(vslq);
641 4147
        AN(vtx->flags & VTX_F_END);
642 4147
        AZ(vtx->flags & VTX_F_COMPLETE);
643
644 4147
        if (vtx->type == VSL_t_unknown)
645 0
                vtx_diag(vtx, "vtx of unknown type marked complete");
646
647 4147
        vtx->flags |= VTX_F_COMPLETE;
648 4147
        VTAILQ_REMOVE(&vslq->incomplete, vtx, list_vtx);
649
650 4567
        while (1) {
651 4567
                AZ(vtx->flags & VTX_F_READY);
652 4567
                if (vtx->flags & VTX_F_COMPLETE &&
653 4164
                    vtx->n_child == vtx->n_childready)
654 4146
                        vtx->flags |= VTX_F_READY;
655
                else
656 421
                        return;
657 4146
                if (vtx->parent == NULL) {
658
                        /* Top level vtx ready */
659 3726
                        VTAILQ_INSERT_TAIL(&vslq->ready, vtx, list_vtx);
660 3726
                        return;
661
                }
662 420
                vtx = vtx->parent;
663 420
                vtx->n_childready++;
664 420
                assert(vtx->n_child >= vtx->n_childready);
665
        }
666 4147
}
667
668
/* Add a child to a parent, and update child counters */
669
static void
670 418
vtx_set_parent(struct vtx *parent, struct vtx *child)
671
{
672
673 418
        CHECK_OBJ_NOTNULL(parent, VTX_MAGIC);
674 418
        CHECK_OBJ_NOTNULL(child, VTX_MAGIC);
675 418
        assert(parent != child);
676 418
        AZ(parent->flags & VTX_F_COMPLETE);
677 418
        AZ(child->flags & VTX_F_COMPLETE);
678 418
        AZ(child->parent);
679 418
        child->parent = parent;
680 418
        VTAILQ_INSERT_TAIL(&parent->child, child, list_child);
681 418
        parent->n_child++;
682 418
        do
683 501
                parent->n_descend += 1 + child->n_descend;
684 501
        while ((parent = parent->parent) != NULL);
685 418
}
686
687
/* Parse a begin or link record. Returns the number of elements that was
688
   successfully parsed. */
689
static int
690 11139
vtx_parse_link(const char *str, enum VSL_transaction_e *ptype,
691
    uint64_t *pvxid, enum VSL_reason_e *preason, uint64_t *psub)
692
{
693
        char type[16], reason[16];
694
        uintmax_t vxid, sub;
695
        int i;
696
        enum VSL_transaction_e et;
697
        enum VSL_reason_e er;
698
699 11139
        AN(str);
700 11139
        AN(ptype);
701 11139
        AN(pvxid);
702 11139
        AN(preason);
703
704 11139
        i = sscanf(str, "%15s %ju %15s %ju", type, &vxid, reason, &sub);
705 11139
        if (i < 1)
706 0
                return (0);
707
708
        /* transaction type */
709 34410
        for (et = VSL_t_unknown; et < VSL_t__MAX; et++)
710 34406
                if (!vstrcmp(type, vsl_t_names[et]))
711 11135
                        break;
712 11139
        if (et >= VSL_t__MAX)
713 0
                et = VSL_t_unknown;
714 11139
        *ptype = et;
715 11139
        if (i == 1)
716 0
                return (1);
717
718
        /* vxid */
719 11139
        assert((vxid & ~VSL_IDENTMASK) == 0);
720 11139
        *pvxid = vxid;
721 11139
        if (i == 2)
722 0
                return (2);
723
724
        /* transaction reason */
725 46238
        for (er = VSL_r_unknown; er < VSL_r__MAX; er++)
726 46022
                if (!vstrcmp(reason, vsl_r_names[er]))
727 10923
                        break;
728 11139
        if (er >= VSL_r__MAX)
729 216
                er = VSL_r_unknown;
730 11139
        *preason = er;
731 11139
        if (i == 3)
732 10683
                return (3);
733
734
        /* request sub-level */
735 456
        if (psub != NULL)
736 165
                *psub = sub;
737 456
        return (4);
738 11139
}
739
740
/* Parse and process a begin record */
741
static int
742 4148
vtx_scan_begin(struct VSLQ *vslq, struct vtx *vtx, const uint32_t *ptr)
743
{
744
        int i;
745
        enum VSL_transaction_e type;
746
        enum VSL_reason_e reason;
747
        uint64_t p_vxid;
748
        struct vtx *p_vtx;
749
750 4148
        assert(VSL_TAG(ptr) == SLT_Begin);
751
752 4148
        AZ(vtx->flags & VTX_F_READY);
753
754 4148
        i = vtx_parse_link(VSL_CDATA(ptr), &type, &p_vxid, &reason, NULL);
755 4148
        if (i < 3)
756 0
                return (vtx_diag_tag(vtx, ptr, "parse error"));
757 4148
        if (type == VSL_t_unknown)
758 0
                (void)vtx_diag_tag(vtx, ptr, "unknown vxid type");
759
760
        /* Check/set vtx type */
761 4148
        if (vtx->type != VSL_t_unknown && vtx->type != type)
762
                /* Type not matching the one previously set by a link
763
                   record */
764 0
                (void)vtx_diag_tag(vtx, ptr, "type mismatch");
765 4148
        vtx->type = type;
766 4148
        vtx->reason = reason;
767
768 4148
        if (p_vxid == 0)
769
                /* Zero means no parent */
770 1183
                return (0);
771 2965
        if (p_vxid == vtx->key.vxid)
772 0
                return (vtx_diag_tag(vtx, ptr, "link to self"));
773
774 2965
        if (vslq->grouping == VSL_g_vxid)
775 2460
                return (0);     /* No links */
776 505
        if (vslq->grouping == VSL_g_request && vtx->type == VSL_t_req &&
777 147
            vtx->reason == VSL_r_rxreq)
778 84
                return (0);     /* No links */
779
780 421
        if (vtx->parent != NULL) {
781 190
                if (vtx->parent->key.vxid != p_vxid) {
782
                        /* This vtx already belongs to a different
783
                           parent */
784 0
                        return (vtx_diag_tag(vtx, ptr, "link mismatch"));
785
                } else
786
                        /* Link already exists */
787 190
                        return (0);
788
        }
789
790 231
        p_vtx = vtx_lookup(vslq, p_vxid);
791 231
        if (p_vtx == NULL) {
792
                /* Not seen parent yet. Create it. */
793 93
                p_vtx = vtx_add(vslq, p_vxid);
794 93
                AN(p_vtx);
795 93
        } else {
796 138
                CHECK_OBJ_NOTNULL(p_vtx, VTX_MAGIC);
797 138
                if (p_vtx->flags & VTX_F_COMPLETE)
798 0
                        return (vtx_diag_tag(vtx, ptr, "link too late"));
799
        }
800
801
        /* Create link */
802 231
        vtx_set_parent(p_vtx, vtx);
803
804 231
        return (0);
805 4148
}
806
807
/* Parse and process a link record */
808
static int
809 3077
vtx_scan_link(struct VSLQ *vslq, struct vtx *vtx, const uint32_t *ptr)
810
{
811
        int i;
812
        enum VSL_transaction_e c_type;
813
        enum VSL_reason_e c_reason;
814
        uint64_t c_vxid;
815
        struct vtx *c_vtx;
816
817 3077
        assert(VSL_TAG(ptr) == SLT_Link);
818
819 3077
        AZ(vtx->flags & VTX_F_READY);
820
821 3077
        i = vtx_parse_link(VSL_CDATA(ptr), &c_type, &c_vxid, &c_reason, NULL);
822 3077
        if (i < 3)
823 0
                return (vtx_diag_tag(vtx, ptr, "parse error"));
824 3077
        if (c_type == VSL_t_unknown)
825 0
                (void)vtx_diag_tag(vtx, ptr, "unknown vxid type");
826
827 3077
        if (vslq->grouping == VSL_g_vxid)
828 2659
                return (0);     /* No links */
829 418
        if (vslq->grouping == VSL_g_request && vtx->type == VSL_t_sess)
830 0
                return (0);     /* No links */
831
832 418
        if (c_vxid == 0)
833 0
                return (vtx_diag_tag(vtx, ptr, "illegal link vxid"));
834 418
        if (c_vxid == vtx->key.vxid)
835 0
                return (vtx_diag_tag(vtx, ptr, "link to self"));
836
837
        /* Lookup and check child vtx */
838 418
        c_vtx = vtx_lookup(vslq, c_vxid);
839 418
        if (c_vtx == NULL) {
840
                /* Child not seen before. Insert it and create link */
841 187
                c_vtx = vtx_add(vslq, c_vxid);
842 187
                AN(c_vtx);
843 187
                AZ(c_vtx->parent);
844 187
                c_vtx->type = c_type;
845 187
                c_vtx->reason = c_reason;
846 187
                vtx_set_parent(vtx, c_vtx);
847 187
                return (0);
848
        }
849
850 231
        CHECK_OBJ(c_vtx, VTX_MAGIC);
851 231
        if (c_vtx->parent == vtx)
852
                /* Link already exists */
853 231
                return (0);
854 0
        if (c_vtx->parent != NULL && c_vtx->parent != vtx)
855 0
                return (vtx_diag_tag(vtx, ptr, "duplicate link"));
856 0
        if (c_vtx->flags & VTX_F_COMPLETE)
857 0
                return (vtx_diag_tag(vtx, ptr, "link too late"));
858 0
        if (c_vtx->type != VSL_t_unknown && c_vtx->type != c_type)
859 0
                (void)vtx_diag_tag(vtx, ptr, "type mismatch");
860
861 0
        c_vtx->type = c_type;
862 0
        c_vtx->reason = c_reason;
863 0
        vtx_set_parent(vtx, c_vtx);
864 0
        return (0);
865 3077
}
866
867
/* Scan the records of a vtx, performing processing actions on specific
868
   records */
869
static void
870 28397
vtx_scan(struct VSLQ *vslq, struct vtx *vtx)
871
{
872
        const uint32_t *ptr;
873
        enum VSL_tag_e tag;
874
875 320945
        while (!(vtx->flags & VTX_F_COMPLETE) &&
876 158324
            vslc_vtx_next(&vtx->c.cursor) == 1) {
877 134224
                ptr = vtx->c.cursor.rec.ptr;
878 134224
                if (VSL_ID(ptr) != vtx->key.vxid) {
879 0
                        (void)vtx_diag_tag(vtx, ptr, "vxid mismatch");
880 0
                        continue;
881
                }
882
883 134224
                tag = VSL_TAG(ptr);
884 134224
                assert(tag != SLT__Batch);
885
886 134224
                switch (tag) {
887
                case SLT_Begin:
888 4144
                        if (vtx->flags & VTX_F_BEGIN)
889 0
                                (void)vtx_diag_tag(vtx, ptr, "duplicate begin");
890
                        else {
891 4144
                                (void)vtx_scan_begin(vslq, vtx, ptr);
892 4144
                                vtx->flags |= VTX_F_BEGIN;
893
                        }
894 4144
                        break;
895
896
                case SLT_Link:
897 3077
                        (void)vtx_scan_link(vslq, vtx, ptr);
898 3077
                        break;
899
900
                case SLT_End:
901 4147
                        AZ(vtx->flags & VTX_F_END);
902 4147
                        vtx->flags |= VTX_F_END;
903 4147
                        vtx_mark_complete(vslq, vtx);
904 4147
                        break;
905
906
                default:
907 122856
                        break;
908
                }
909
        }
910 28397
}
911
912
/* Force a vtx into complete status by synthing the necessary outstanding
913
   records */
914
static void
915 483
vtx_force(struct VSLQ *vslq, struct vtx *vtx, const char *reason)
916
{
917
918 483
        AZ(vtx->flags & (VTX_F_COMPLETE | VTX_F_READY));
919 483
        vtx_scan(vslq, vtx);
920 483
        if (!(vtx->flags & VTX_F_BEGIN))
921 0
                vtx_synth_rec(vtx, SLT_Begin, "%s %u synth",
922 0
                    vsl_t_names[vtx->type], 0);
923 483
        vtx_diag(vtx, reason);
924 483
        if (!(vtx->flags & VTX_F_END))
925 483
                vtx_synth_rec(vtx, SLT_End, "synth");
926 483
        vtx_scan(vslq, vtx);
927 483
        AN(vtx->flags & VTX_F_COMPLETE);
928 483
}
929
930
static int
931 33
vslq_ratelimit(struct VSLQ *vslq)
932
{
933
        vtim_mono now;
934
        vtim_dur delta;
935
936 33
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
937 33
        CHECK_OBJ_NOTNULL(vslq->vsl, VSL_MAGIC);
938
939 33
        now = VTIM_mono();
940 33
        delta = now - vslq->last_use;
941 33
        vslq->credits += (delta / vslq->vsl->R_opt_p) * vslq->vsl->R_opt_l;
942 33
        vslq->credits = vmin_t(double, vslq->credits, vslq->vsl->R_opt_l);
943 33
        vslq->last_use = now;
944
945 33
        if (vslq->credits < 1.0)
946 0
                return (0);
947
948 33
        vslq->credits -= 1.0;
949 33
        return (1);
950 33
}
951
952
/* Build transaction array, do the query and callback. Returns 0 or the
953
   return value from func */
954
static int
955 3250
vslq_callback(struct VSLQ *vslq, struct vtx *vtx, VSLQ_dispatch_f *func,
956
    void *priv)
957
{
958 3250
        unsigned n = vtx->n_descend + 1;
959 3250
        struct vtx *vtxs[n];
960 3250
        struct VSL_transaction trans[n];
961 3250
        struct VSL_transaction *ptrans[n + 1];
962
        unsigned i, j;
963
964 3250
        AN(vslq);
965 3250
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
966 3250
        AN(vtx->flags & VTX_F_READY);
967 3250
        AN(func);
968
969 3250
        if (vslq->grouping == VSL_g_session &&
970 69
            vtx->type != VSL_t_sess)
971 0
                return (0);
972 3250
        if (vslq->grouping == VSL_g_request &&
973 84
            vtx->type != VSL_t_req)
974 0
                return (0);
975
976
        /* Build transaction array */
977 3250
        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
978 3250
        vtxs[0] = vtx;
979 3250
        trans[0].level = 1;
980 3250
        trans[0].vxid = vtx->key.vxid;
981 3250
        trans[0].vxid_parent = 0;
982 3250
        trans[0].type = vtx->type;
983 3250
        trans[0].reason = vtx->reason;
984 3250
        trans[0].c = &vtx->c.cursor;
985 3250
        i = 1;
986 3250
        j = 0;
987 6867
        while (j < i) {
988 3987
                VTAILQ_FOREACH(vtx, &vtxs[j]->child, list_child) {
989 370
                        assert(i < n);
990 370
                        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
991 370
                        vtxs[i] = vtx;
992 370
                        if (vtx->reason == VSL_r_restart)
993
                                /* Restarts stay at the same level as parent */
994 6
                                trans[i].level = trans[j].level;
995
                        else
996 364
                                trans[i].level = trans[j].level + 1;
997 370
                        trans[i].vxid = vtx->key.vxid;
998 370
                        trans[i].vxid_parent = trans[j].vxid;
999 370
                        trans[i].type = vtx->type;
1000 370
                        trans[i].reason = vtx->reason;
1001 370
                        trans[i].c = &vtx->c.cursor;
1002 370
                        i++;
1003 370
                }
1004 3617
                j++;
1005
        }
1006 3250
        assert(i == n);
1007
1008
        /* Build pointer array */
1009 6867
        for (i = 0; i < n; i++)
1010 3617
                ptrans[i] = &trans[i];
1011 3250
        ptrans[i] = NULL;
1012
1013
        /* Query test goes here */
1014 3250
        if (vslq->query != NULL && !vslq_runquery(vslq->query, ptrans))
1015 1186
                return (0);
1016
1017 2064
        if (vslq->vsl->R_opt_l != 0 && !vslq_ratelimit(vslq))
1018 0
                return (0);
1019
1020
        /* Callback */
1021 2064
        return ((func)(vslq->vsl, ptrans, priv));
1022 3250
}
1023
1024
/* Create a synthetic log record. The record will be inserted at the
1025
   current cursor offset */
1026
static void
1027 966
vtx_synth_rec(struct vtx *vtx, unsigned tag, const char *fmt, ...)
1028
{
1029
        struct synth *synth, *it;
1030
        va_list ap;
1031
        char *buf;
1032
        int l, buflen;
1033
        uint64_t vxid;
1034
1035 966
        ALLOC_OBJ(synth, SYNTH_MAGIC);
1036 966
        AN(synth);
1037
1038 966
        buf = VSL_DATA(synth->data);
1039 966
        buflen = sizeof(synth->data) - VSL_BYTES(VSL_OVERHEAD);
1040 966
        va_start(ap, fmt);
1041 966
        l = vsnprintf(buf, buflen, fmt, ap);
1042 966
        assert(l >= 0);
1043 966
        va_end(ap);
1044 966
        if (l > buflen - 1)
1045 0
                l = buflen - 1;
1046 966
        buf[l++] = '\0';        /* NUL-terminated */
1047 966
        vxid = vtx->key.vxid;
1048 966
        switch (vtx->type) {
1049
        case VSL_t_req:
1050 54
                vxid |= VSL_CLIENTMARKER;
1051 54
                break;
1052
        case VSL_t_bereq:
1053 6
                vxid |= VSL_BACKENDMARKER;
1054 6
                break;
1055
        default:
1056 906
                break;
1057
        }
1058 966
        synth->data[2] = vxid >> 32;
1059 966
        synth->data[1] = vxid;
1060 1932
        synth->data[0] = (((tag & VSL_IDMASK) << VSL_IDSHIFT) |
1061 966
            (VSL_VERSION_3 << VSL_VERSHIFT) | l);
1062 966
        synth->offset = vtx->c.offset;
1063
1064 966
        VTAILQ_FOREACH_REVERSE(it, &vtx->synth, synthhead, list) {
1065
                /* Make sure the synth list is sorted on offset */
1066 483
                CHECK_OBJ_NOTNULL(it, SYNTH_MAGIC);
1067 483
                if (synth->offset >= it->offset)
1068 483
                        break;
1069 0
        }
1070 966
        if (it != NULL)
1071 483
                VTAILQ_INSERT_AFTER(&vtx->synth, it, synth, list);
1072
        else
1073 483
                VTAILQ_INSERT_HEAD(&vtx->synth, synth, list);
1074
1075
        /* Update cursor */
1076 966
        CHECK_OBJ_ORNULL(vtx->c.synth, SYNTH_MAGIC);
1077 966
        if (vtx->c.synth == NULL || vtx->c.synth->offset > synth->offset)
1078 483
                vtx->c.synth = synth;
1079 966
}
1080
1081
/* Add a diagnostic SLT_VSL synth record to the vtx. */
1082
static int
1083 483
vtx_diag(struct vtx *vtx, const char *msg)
1084
{
1085
1086 483
        vtx_synth_rec(vtx, SLT_VSL, msg);
1087 483
        return (-1);
1088
}
1089
1090
/* Add a SLT_VSL diag synth record to the vtx. Takes an offending record
1091
   that will be included in the log record */
1092
static int
1093 0
vtx_diag_tag(struct vtx *vtx, const uint32_t *ptr, const char *reason)
1094
{
1095
1096 0
        vtx_synth_rec(vtx, SLT_VSL, "%s (%ju:%s \"%.*s\")", reason, VSL_ID(ptr),
1097 0
            VSL_tags[VSL_TAG(ptr)], (int)VSL_LEN(ptr), VSL_CDATA(ptr));
1098 0
        return (-1);
1099
}
1100
1101
struct VSLQ *
1102 1254
VSLQ_New(struct VSL_data *vsl, struct VSL_cursor **cp,
1103
    enum VSL_grouping_e grouping, const char *querystring)
1104
{
1105
        struct vslq_query *query;
1106
        struct VSLQ *vslq;
1107
1108 1254
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
1109 1254
        if (grouping >= VSL_g__MAX) {
1110 0
                (void)vsl_diag(vsl, "Illegal query grouping");
1111 0
                return (NULL);
1112
        }
1113 1254
        if (querystring != NULL) {
1114 579
                query = vslq_newquery(vsl, grouping, querystring);
1115 579
                if (query == NULL)
1116 96
                        return (NULL);
1117 483
        } else
1118 675
                query = NULL;
1119
1120 1158
        ALLOC_OBJ(vslq, VSLQ_MAGIC);
1121 1158
        AN(vslq);
1122 1158
        vslq->vsl = vsl;
1123 1158
        if (cp != NULL) {
1124 876
                vslq->c = *cp;
1125 876
                *cp = NULL;
1126 876
        }
1127 1158
        vslq->grouping = grouping;
1128 1158
        vslq->query = query;
1129 1158
        if (vslq->vsl->R_opt_l != 0) {
1130 6
                vslq->last_use = VTIM_mono();
1131 6
                vslq->credits = 1;
1132 6
        }
1133
1134
        /* Setup normal mode */
1135 1158
        VRBT_INIT(&vslq->tree);
1136 1158
        VTAILQ_INIT(&vslq->ready);
1137 1158
        VTAILQ_INIT(&vslq->incomplete);
1138 1158
        VTAILQ_INIT(&vslq->shmrefs);
1139 1158
        VTAILQ_INIT(&vslq->cache);
1140
1141
        /* Setup raw mode */
1142 1158
        vslq->raw.c.magic = VSLC_RAW_MAGIC;
1143 1158
        vslq->raw.c.cursor.priv_tbl = &vslc_raw_tbl;
1144 1158
        vslq->raw.c.cursor.priv_data = &vslq->raw.c;
1145 1158
        vslq->raw.trans.level = 0;
1146 1158
        vslq->raw.trans.type = VSL_t_raw;
1147 1158
        vslq->raw.trans.reason = VSL_r_unknown;
1148 1158
        vslq->raw.trans.c = &vslq->raw.c.cursor;
1149 1158
        vslq->raw.ptrans[0] = &vslq->raw.trans;
1150 1158
        vslq->raw.ptrans[1] = NULL;
1151
1152 1158
        return (vslq);
1153 1254
}
1154
1155
void
1156 1140
VSLQ_Delete(struct VSLQ **pvslq)
1157
{
1158
        struct VSLQ *vslq;
1159
        struct vtx *vtx;
1160
1161 1140
        TAKE_OBJ_NOTNULL(vslq, pvslq, VSLQ_MAGIC);
1162
1163 1140
        (void)VSLQ_Flush(vslq, NULL, NULL);
1164 1140
        AZ(vslq->n_outstanding);
1165
1166 1140
        if (vslq->c != NULL) {
1167 1140
                VSL_DeleteCursor(vslq->c);
1168 1140
                vslq->c = NULL;
1169 1140
        }
1170
1171 1140
        if (vslq->query != NULL)
1172 483
                vslq_deletequery(&vslq->query);
1173 1140
        AZ(vslq->query);
1174
1175 2982
        while (!VTAILQ_EMPTY(&vslq->cache)) {
1176 1842
                AN(vslq->n_cache);
1177 1842
                vtx = VTAILQ_FIRST(&vslq->cache);
1178 1842
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1179 1842
                VTAILQ_REMOVE(&vslq->cache, vtx, list_child);
1180 1842
                vslq->n_cache--;
1181 1842
                FREE_OBJ(vtx);
1182
        }
1183
1184 1140
        FREE_OBJ(vslq);
1185 1140
}
1186
1187
void
1188 264
VSLQ_SetCursor(struct VSLQ *vslq, struct VSL_cursor **cp)
1189
{
1190
1191 264
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1192
1193 264
        if (vslq->c != NULL) {
1194 0
                (void)VSLQ_Flush(vslq, NULL, NULL);
1195 0
                AZ(vslq->n_outstanding);
1196 0
                VSL_DeleteCursor(vslq->c);
1197 0
                vslq->c = NULL;
1198 0
        }
1199
1200 264
        if (cp != NULL) {
1201 264
                AN(*cp);
1202 264
                vslq->c = *cp;
1203 264
                *cp = NULL;
1204 264
        }
1205 264
}
1206
1207
/* Regard each log line as a single transaction, feed it through the query
1208
   and do the callback */
1209
static int
1210 65052
vslq_raw(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1211
{
1212 65052
        enum vsl_status r = vsl_more;
1213
        int i;
1214
1215 65052
        assert(vslq->grouping == VSL_g_raw);
1216
1217 65052
        assert(vslq->raw.offset <= vslq->raw.len);
1218 65052
        do {
1219 75442
                if (vslq->raw.offset == vslq->raw.len) {
1220 40313
                        r = VSL_Next(vslq->c);
1221 40313
                        if (r != vsl_more)
1222 19831
                                return (r);
1223 20482
                        AN(vslq->c->rec.ptr);
1224 20482
                        vslq->raw.start = vslq->c->rec;
1225 20482
                        if (VSL_TAG(vslq->c->rec.ptr) == SLT__Batch)
1226 20780
                                vslq->raw.len = VSL_END(vslq->c->rec.ptr,
1227 10390
                                    VSL_BATCHLEN(vslq->c->rec.ptr)) -
1228 10390
                                    vslq->c->rec.ptr;
1229
                        else
1230 20184
                                vslq->raw.len = VSL_NEXT(vslq->raw.start.ptr) -
1231 10092
                                    vslq->raw.start.ptr;
1232 20482
                        assert(vslq->raw.len > 0);
1233 20482
                        vslq->raw.offset = 0;
1234 20482
                }
1235
1236 55611
                vslq->raw.c.ptr = vslq->raw.start.ptr + vslq->raw.offset;
1237 55611
                vslq->raw.c.cursor.rec.ptr = NULL;
1238 55611
                vslq->raw.trans.vxid = VSL_ID(vslq->raw.c.ptr);
1239 55611
                vslq->raw.offset += VSL_NEXT(vslq->raw.c.ptr) - vslq->raw.c.ptr;
1240 55611
        } while (VSL_TAG(vslq->raw.c.ptr) == SLT__Batch);
1241
1242 45221
        assert (r == vsl_more);
1243
1244 45221
        if (func == NULL)
1245 0
                return (r);
1246
1247 45221
        if (vslq->query != NULL &&
1248 6645
            !vslq_runquery(vslq->query, vslq->raw.ptrans))
1249 6117
                return (r);
1250
1251 39104
        if (vslq->vsl->R_opt_l != 0 && !vslq_ratelimit(vslq))
1252 0
                return (r);
1253
1254 39104
        i = (func)(vslq->vsl, vslq->raw.ptrans, priv);
1255 39104
        if (i)
1256 300
                return (i);
1257
1258 38804
        return (r);
1259 65052
}
1260
1261
/* Check the beginning of the shmref list, and buffer refs that are at
1262
 * warning level.
1263
 */
1264
static enum vsl_status
1265 37930
vslq_shmref_check(struct VSLQ *vslq)
1266
{
1267
        struct chunk *chunk;
1268
        enum vsl_check i;
1269
1270 37930
        while ((chunk = VTAILQ_FIRST(&vslq->shmrefs)) != NULL) {
1271 27986
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
1272 27986
                assert(chunk->type == chunk_t_shm);
1273 27986
                i = VSL_Check(vslq->c, &chunk->shm.start);
1274 27986
                switch (i) {
1275
                case vsl_check_valid:
1276
                        /* First on list is OK, refs behind it must also
1277
                           be OK */
1278 27986
                        return (vsl_more);
1279
                case vsl_check_warn:
1280
                        /* Buffer this chunk */
1281 0
                        chunk_shm_to_buf(vslq, chunk);
1282 0
                        break;
1283
                default:
1284
                        /* Too late to buffer */
1285 0
                        return (vsl_e_overrun);
1286
                }
1287
        }
1288
1289 9944
        return (vsl_more);
1290 37930
}
1291
1292
static unsigned
1293 4624
vslq_candidate(struct VSLQ *vslq, const uint32_t *ptr)
1294
{
1295
        enum VSL_transaction_e type;
1296
        enum VSL_reason_e reason;
1297
        struct VSL_data *vsl;
1298
        enum VSL_tag_e tag;
1299
        uint64_t p_vxid, sub;
1300
        int i;
1301
1302 4624
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1303 4624
        AN(ptr);
1304
1305 4624
        assert(vslq->grouping != VSL_g_raw);
1306 4624
        if (vslq->grouping == VSL_g_session)
1307 336
                return (1); /* All are needed */
1308
1309 4288
        vsl = vslq->vsl;
1310 4288
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
1311 4288
        if (vslq->grouping == VSL_g_vxid) {
1312 3975
                if (!vsl->c_opt && !vsl->b_opt)
1313 2898
                        AZ(vsl->E_opt);
1314 1077
                else if (!vsl->b_opt && !VSL_CLIENT(ptr))
1315 249
                        return (0);
1316 828
                else if (!vsl->c_opt && !VSL_BACKEND(ptr))
1317 126
                        return (0);
1318
                /* Need to parse the Begin tag - fallthrough to below */
1319 3600
        }
1320
1321 3913
        tag = VSL_TAG(ptr);
1322 3913
        assert(tag == SLT_Begin);
1323 3913
        i = vtx_parse_link(VSL_CDATA(ptr), &type, &p_vxid, &reason, &sub);
1324 3913
        if (i < 3 || type == VSL_t_unknown)
1325 0
                return (0);
1326
1327 3913
        if (vslq->grouping == VSL_g_request && type == VSL_t_sess)
1328 60
                return (0);
1329
1330 3853
        if (vslq->grouping == VSL_g_vxid && i > 3 && sub > 0 && !vsl->E_opt)
1331 45
                return (0);
1332
1333 3808
        return (1);
1334 4624
}
1335
1336
/* Process next input record */
1337
static enum vsl_status
1338 67440
vslq_next(struct VSLQ *vslq)
1339
{
1340
        const uint32_t *ptr;
1341
        struct VSL_cursor *c;
1342
        enum vsl_status r;
1343
        enum VSL_tag_e tag;
1344
        ssize_t len;
1345
        uint64_t vxid;
1346
        unsigned keep;
1347
        struct vtx *vtx;
1348
1349 67440
        c = vslq->c;
1350 67440
        r = VSL_Next(c);
1351 67440
        if (r != vsl_more)
1352 29526
                return (r);
1353
1354 37914
        assert (r == vsl_more);
1355
1356 37914
        tag = (enum VSL_tag_e)VSL_TAG(c->rec.ptr);
1357 37914
        if (tag == SLT__Batch) {
1358 12081
                vxid = VSL_BATCHID(c->rec.ptr);
1359 24162
                len = VSL_END(c->rec.ptr, VSL_BATCHLEN(c->rec.ptr)) -
1360 12081
                    c->rec.ptr;
1361 12081
                if (len == 0)
1362 0
                        return (r);
1363 12081
                ptr = VSL_NEXT(c->rec.ptr);
1364 12081
                tag = (enum VSL_tag_e)VSL_TAG(ptr);
1365 12081
        } else {
1366 25833
                vxid = VSL_ID(c->rec.ptr);
1367 25833
                len = VSL_NEXT(c->rec.ptr) - c->rec.ptr;
1368 25833
                ptr = c->rec.ptr;
1369
        }
1370 37914
        assert(len > 0);
1371 37914
        if (vxid == 0)
1372
                /* Skip non-transactional records */
1373 8362
                return (r);
1374
1375 29552
        vtx = vtx_lookup(vslq, vxid);
1376 29552
        keep = tag != SLT_Begin || vslq_candidate(vslq, ptr);
1377 29552
        if (vtx == NULL && tag == SLT_Begin && keep) {
1378 3865
                vtx = vtx_add(vslq, vxid);
1379 3865
                AN(vtx);
1380 3865
        }
1381 29552
        if (vtx != NULL) {
1382 27447
                AN(keep);
1383 27447
                r = vtx_append(vslq, vtx, &c->rec, len);
1384 27447
                if (r == vsl_more)
1385 27438
                        vtx_scan(vslq, vtx);
1386 27447
        }
1387
1388 29552
        return (r);
1389 67440
}
1390
1391
/* Test query and report any ready transactions */
1392
static int
1393 4390
vslq_process_ready(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1394
{
1395
        struct vtx *vtx;
1396 4390
        int i = 0;
1397
1398 4390
        AN(vslq);
1399
1400 7538
        while (!VTAILQ_EMPTY(&vslq->ready)) {
1401 3727
                vtx = VTAILQ_FIRST(&vslq->ready);
1402 3727
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1403 3727
                VTAILQ_REMOVE(&vslq->ready, vtx, list_vtx);
1404 3727
                AN(vtx->flags & VTX_F_READY);
1405 3727
                if (func != NULL)
1406 3250
                        i = vslq_callback(vslq, vtx, func, priv);
1407 3727
                vtx_retire(vslq, &vtx);
1408 3727
                AZ(vtx);
1409 3727
                if (i)
1410 579
                        return (i);
1411
        }
1412
1413 3811
        return (0);
1414 4390
}
1415
1416
/* Process the input cursor, calling the callback function on matching
1417
   transaction sets */
1418
int
1419 132467
VSLQ_Dispatch(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1420
{
1421
        enum vsl_status r;
1422
        int i;
1423
        double now;
1424
        struct vtx *vtx;
1425
1426 132467
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1427
1428
        /* Check that we have a cursor */
1429 132467
        if (vslq->c == NULL)
1430 0
                return (vsl_e_abandon);
1431
1432 132467
        if (vslq->grouping == VSL_g_raw)
1433 65052
                return (vslq_raw(vslq, func, priv));
1434
1435
        /* Process next cursor input */
1436 67415
        r = vslq_next(vslq);
1437 67415
        if (r != vsl_more)
1438
                /* At end of log or cursor reports error condition */
1439 29486
                return (r);
1440
1441
        /* Check shmref list and buffer if necessary */
1442 37929
        r = vslq_shmref_check(vslq);
1443 37929
        if (r != vsl_more)
1444
                /* Buffering of shm ref failed */
1445 0
                return (r);
1446
1447 37929
        assert (r == vsl_more);
1448
1449
        /* Check vtx timeout */
1450 37929
        now = VTIM_mono();
1451 37935
        while (!VTAILQ_EMPTY(&vslq->incomplete)) {
1452 27398
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1453 27398
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1454 27398
                if (now - vtx->t_start < vslq->vsl->T_opt)
1455 27392
                        break;
1456 6
                vtx_force(vslq, vtx, "timeout");
1457 6
                AN(vtx->flags & VTX_F_COMPLETE);
1458
        }
1459
1460
        /* Check store limit */
1461 37929
        while (vslq->n_outstanding > vslq->vsl->L_opt &&
1462 0
            !(VTAILQ_EMPTY(&vslq->incomplete))) {
1463 0
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1464 0
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1465 0
                vtx_force(vslq, vtx, "store overflow");
1466 0
                AN(vtx->flags & VTX_F_COMPLETE);
1467 0
                i = vslq_process_ready(vslq, func, priv);
1468 0
                if (i)
1469
                        /* User return code */
1470 0
                        return (i);
1471
        }
1472
1473
        /* Check ready list */
1474 37929
        if (!VTAILQ_EMPTY(&vslq->ready)) {
1475 3250
                i = vslq_process_ready(vslq, func, priv);
1476 3250
                if (i)
1477
                        /* User return code */
1478 579
                        return (i);
1479 2671
        }
1480
1481 37350
        return (vsl_more);
1482 132467
}
1483
1484
/* Flush any incomplete vtx held on to. Do callbacks if func != NULL */
1485
int
1486 1140
VSLQ_Flush(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1487
{
1488
        struct vtx *vtx;
1489
1490 1140
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1491
1492 1617
        while (!VTAILQ_EMPTY(&vslq->incomplete)) {
1493 477
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1494 477
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1495 477
                AZ(vtx->flags & VTX_F_COMPLETE);
1496 477
                vtx_force(vslq, vtx, "flush");
1497
        }
1498
1499 1140
        return (vslq_process_ready(vslq, func, priv));
1500
}