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 55403
vtx_keycmp(const struct vtx_key *a, const struct vtx_key *b)
223
{
224 55403
        if (a->vxid < b->vxid)
225 1588
                return (-1);
226 53815
        if (a->vxid > b->vxid)
227 22100
                return (1);
228 31715
        return (0);
229 55403
}
230
231 3481
VRBT_GENERATE_REMOVE_COLOR(vtx_tree, vtx_key, entry, static)
232 8628
VRBT_GENERATE_REMOVE(vtx_tree, vtx_key, entry, static)
233 4322
VRBT_GENERATE_INSERT_COLOR(vtx_tree, vtx_key, entry, static)
234 5320
VRBT_GENERATE_INSERT_FINISH(vtx_tree, vtx_key, entry, static)
235 9784
VRBT_GENERATE_INSERT(vtx_tree, vtx_key, entry, vtx_keycmp, static)
236 59239
VRBT_GENERATE_FIND(vtx_tree, vtx_key, entry, vtx_keycmp, static)
237
238
static enum vsl_status v_matchproto_(vslc_next_f)
239 115976
vslc_raw_next(const struct VSL_cursor *cursor)
240
{
241
        struct vslc_raw *c;
242
243 115976
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
244 115976
        assert(&c->cursor == cursor);
245
246 115976
        AN(c->ptr);
247 115976
        if (c->cursor.rec.ptr == NULL) {
248 58390
                c->cursor.rec.ptr = c->ptr;
249 58390
                return (vsl_more);
250
        } else {
251 57586
                c->cursor.rec.ptr = NULL;
252 57586
                return (vsl_end);
253
        }
254 115976
}
255
256
static enum vsl_status v_matchproto_(vslc_reset_f)
257 15444
vslc_raw_reset(const struct VSL_cursor *cursor)
258
{
259
        struct vslc_raw *c;
260
261 15444
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
262 15444
        assert(&c->cursor == cursor);
263
264 15444
        AN(c->ptr);
265 15444
        c->cursor.rec.ptr = NULL;
266
267 15444
        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 334218
vslc_vtx_next(const struct VSL_cursor *cursor)
280
{
281
        struct vslc_vtx *c;
282
        const uint32_t *ptr;
283
        unsigned overrun;
284
285 334218
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
286 334218
        assert(&c->cursor == cursor);
287 334218
        CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
288
289 334218
        do {
290 362476
                CHECK_OBJ_ORNULL(c->synth, SYNTH_MAGIC);
291 362476
                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 1699
                        c->cursor.rec.ptr = c->synth->data;
295 1699
                        c->synth = VTAILQ_NEXT(c->synth, list);
296 1699
                } else {
297 360777
                        overrun = c->offset > c->vtx->len;
298 360777
                        AZ(overrun);
299 360777
                        if (c->offset == c->vtx->len)
300 35587
                                return (vsl_end);
301
302
                        /* Advance chunk pointer */
303 325190
                        if (c->chunk == NULL) {
304 10036
                                c->chunk = VTAILQ_FIRST(&c->vtx->chunks);
305 10036
                                c->chunkstart = 0;
306 10036
                        }
307 325190
                        CHECK_OBJ_NOTNULL(c->chunk, CHUNK_MAGIC);
308 334267
                        while (c->offset >= c->chunkstart + c->chunk->len) {
309 9077
                                c->chunkstart += c->chunk->len;
310 9077
                                c->chunk = VTAILQ_NEXT(c->chunk, list);
311 9077
                                CHECK_OBJ_NOTNULL(c->chunk, CHUNK_MAGIC);
312
                        }
313
314
                        /* Point to the next stored record */
315 325190
                        if (c->chunk->type == chunk_t_shm)
316 262237
                                ptr = c->chunk->shm.start.ptr;
317
                        else {
318 62953
                                assert(c->chunk->type == chunk_t_buf);
319 62953
                                ptr = c->chunk->buf.data;
320
                        }
321 325190
                        c->cursor.rec.ptr = ptr + c->offset - c->chunkstart;
322 650380
                        c->offset += VSL_NEXT(c->cursor.rec.ptr) -
323 325190
                            c->cursor.rec.ptr;
324
                }
325 326889
        } while (VSL_TAG(c->cursor.rec.ptr) == SLT__Batch);
326
327 298631
        return (vsl_more);
328 334218
}
329
330
static enum vsl_status v_matchproto_(vslc_reset_f)
331 13702
vslc_vtx_reset(const struct VSL_cursor *cursor)
332
{
333
        struct vslc_vtx *c;
334
335 13702
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
336 13702
        assert(&c->cursor == cursor);
337 13702
        CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
338 13702
        c->synth = VTAILQ_FIRST(&c->vtx->synth);
339 13702
        c->chunk = NULL;
340 13702
        c->chunkstart = 0;
341 13702
        c->offset = 0;
342 13702
        c->cursor.rec.ptr = NULL;
343
344 13702
        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 1874
chunk_newbuf(struct vtx *vtx, const uint32_t *ptr, size_t len)
358
{
359
        struct chunk *chunk;
360
361 1874
        ALLOC_OBJ(chunk, CHUNK_MAGIC);
362 1874
        XXXAN(chunk);
363 1874
        chunk->type = chunk_t_buf;
364 1874
        chunk->vtx = vtx;
365 1874
        chunk->buf.space = VTX_BUFSIZE_MIN;
366 1874
        while (chunk->buf.space < len)
367 0
                chunk->buf.space *= 2;
368 1874
        chunk->buf.data = malloc(sizeof (uint32_t) * chunk->buf.space);
369 1874
        AN(chunk->buf.data);
370 1874
        vmemcpy(chunk->buf.data, ptr, sizeof(uint32_t) * len);
371 1874
        chunk->len = len;
372 1874
        return (chunk);
373
}
374
375
/* Free a buf chunk */
376
static void
377 1873
chunk_freebuf(struct chunk **pchunk)
378
{
379
        struct chunk *chunk;
380
381 1873
        TAKE_OBJ_NOTNULL(chunk, pchunk, CHUNK_MAGIC);
382 1873
        assert(chunk->type == chunk_t_buf);
383 1873
        free(chunk->buf.data);
384 1873
        FREE_OBJ(chunk);
385 1873
}
386
387
/* Append a set of records to a chunk */
388
static void
389 25057
chunk_appendbuf(struct chunk *chunk, const uint32_t *ptr, size_t len)
390
{
391
392 25057
        CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
393 25057
        assert(chunk->type == chunk_t_buf);
394 25057
        if (chunk->buf.space < chunk->len + len) {
395 7938
                while (chunk->buf.space < chunk->len + len)
396 3969
                        chunk->buf.space *= 2;
397 7938
                chunk->buf.data = realloc(chunk->buf.data,
398 3969
                    sizeof (uint32_t) * chunk->buf.space);
399 3969
        }
400 25057
        vmemcpy(chunk->buf.data + chunk->len, ptr, sizeof(uint32_t) * len);
401 25057
        chunk->len += len;
402 25057
}
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 36167
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 36167
        AN(vtx);
446 36167
        AN(len);
447 36167
        AN(start);
448
449 36167
        i = VSL_Check(vslq->c, start);
450 36167
        if (i == vsl_check_e_inval)
451 0
                return (vsl_e_overrun);
452
453 36167
        if (i == vsl_check_valid && !VTAILQ_EMPTY(&vtx->shmchunks_free)) {
454
                /* Shmref it */
455 9238
                chunk = VTAILQ_FIRST(&vtx->shmchunks_free);
456 9238
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
457 9238
                assert(chunk->type == chunk_t_shm);
458 9238
                assert(chunk->vtx == vtx);
459 9238
                VTAILQ_REMOVE(&vtx->shmchunks_free, chunk, list);
460 9238
                chunk->shm.start = *start;
461 9238
                chunk->len = len;
462 9238
                VTAILQ_INSERT_TAIL(&vtx->chunks, chunk, list);
463
464
                /* Append to shmref list */
465 9238
                VTAILQ_INSERT_TAIL(&vslq->shmrefs, chunk, shm.shmref);
466 9238
        } else {
467
                /* Buffer it */
468 26929
                chunk = VTAILQ_LAST(&vtx->chunks, chunkhead);
469 26929
                CHECK_OBJ_ORNULL(chunk, CHUNK_MAGIC);
470 26929
                if (chunk != NULL && chunk->type == chunk_t_buf) {
471
                        /* Tail is a buf chunk, append to that */
472 25055
                        chunk_appendbuf(chunk, start->ptr, len);
473 25055
                } else {
474
                        /* Append new buf chunk */
475 1874
                        chunk = chunk_newbuf(vtx, start->ptr, len);
476 1874
                        AN(chunk);
477 1874
                        VTAILQ_INSERT_TAIL(&vtx->chunks, chunk, list);
478
                }
479
        }
480 36167
        vtx->len += len;
481 36167
        return (vsl_more);
482 36167
}
483
484
/* Allocate a new vtx structure */
485
static struct vtx *
486 5320
vtx_new(struct VSLQ *vslq)
487
{
488
        struct vtx *vtx;
489
        int i;
490
491 5320
        AN(vslq);
492 5320
        if (vslq->n_cache) {
493 2411
                AZ(VTAILQ_EMPTY(&vslq->cache));
494 2411
                vtx = VTAILQ_FIRST(&vslq->cache);
495 2411
                VTAILQ_REMOVE(&vslq->cache, vtx, list_child);
496 2411
                vslq->n_cache--;
497 2411
        } else {
498 2909
                ALLOC_OBJ(vtx, VTX_MAGIC);
499 2909
                AN(vtx);
500
501 2909
                VTAILQ_INIT(&vtx->child);
502 2909
                VTAILQ_INIT(&vtx->shmchunks_free);
503 11637
                for (i = 0; i < VTX_SHMCHUNKS; i++) {
504 8728
                        vtx->shmchunks[i].magic = CHUNK_MAGIC;
505 8728
                        vtx->shmchunks[i].type = chunk_t_shm;
506 8728
                        vtx->shmchunks[i].vtx = vtx;
507 8728
                        VTAILQ_INSERT_TAIL(&vtx->shmchunks_free,
508
                            &vtx->shmchunks[i], list);
509 8728
                }
510 2909
                VTAILQ_INIT(&vtx->chunks);
511 2909
                VTAILQ_INIT(&vtx->synth);
512 2909
                vtx->c.magic = VSLC_VTX_MAGIC;
513 2909
                vtx->c.vtx = vtx;
514 2909
                vtx->c.cursor.priv_tbl = &vslc_vtx_tbl;
515 2909
                vtx->c.cursor.priv_data = &vtx->c;
516
        }
517
518 5320
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
519 5320
        vtx->key.vxid = 0;
520 5320
        vtx->t_start = VTIM_mono();
521 5320
        vtx->flags = 0;
522 5320
        vtx->type = VSL_t_unknown;
523 5320
        vtx->reason = VSL_r_unknown;
524 5320
        vtx->parent = NULL;
525 5320
        vtx->n_child = 0;
526 5320
        vtx->n_childready = 0;
527 5320
        vtx->n_descend = 0;
528 5320
        vtx->len = 0;
529 5320
        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
530
531 5320
        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 5321
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 5321
        AN(vslq);
545 5321
        TAKE_OBJ_NOTNULL(vtx, pvtx, VTX_MAGIC);
546
547 5321
        assert((vtx->flags & (VTX_F_COMPLETE | VTX_F_READY)) == (VTX_F_COMPLETE | VTX_F_READY));
548 5321
        AZ(vtx->parent);
549
550 5880
        while (!VTAILQ_EMPTY(&vtx->child)) {
551 559
                child = VTAILQ_FIRST(&vtx->child);
552 559
                assert(child->parent == vtx);
553 559
                AN(vtx->n_child);
554 559
                assert(vtx->n_descend >= child->n_descend + 1);
555 559
                VTAILQ_REMOVE(&vtx->child, child, list_child);
556 559
                child->parent = NULL;
557 559
                vtx->n_child--;
558 559
                vtx->n_descend -= child->n_descend + 1;
559 559
                vtx_retire(vslq, &child);
560 559
                AZ(child);
561
        }
562 5321
        AZ(vtx->n_child);
563 5321
        AZ(vtx->n_descend);
564 5321
        vtx->n_childready = 0;
565
        // remove rval is no way to check if element was present
566 5321
        (void)VRBT_REMOVE(vtx_tree, &vslq->tree, &vtx->key);
567 5321
        vtx->key.vxid = 0;
568 5321
        vtx->flags = 0;
569
570 7004
        while (!VTAILQ_EMPTY(&vtx->synth)) {
571 1683
                synth = VTAILQ_FIRST(&vtx->synth);
572 1683
                CHECK_OBJ_NOTNULL(synth, SYNTH_MAGIC);
573 1683
                VTAILQ_REMOVE(&vtx->synth, synth, list);
574 1683
                FREE_OBJ(synth);
575
        }
576
577 16435
        while (!VTAILQ_EMPTY(&vtx->chunks)) {
578 11114
                chunk = VTAILQ_FIRST(&vtx->chunks);
579 11114
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
580 11114
                VTAILQ_REMOVE(&vtx->chunks, chunk, list);
581 11114
                if (chunk->type == chunk_t_shm) {
582 9241
                        VTAILQ_REMOVE(&vslq->shmrefs, chunk, shm.shmref);
583 9241
                        VTAILQ_INSERT_HEAD(&vtx->shmchunks_free, chunk, list);
584 9241
                } else {
585 1873
                        assert(chunk->type == chunk_t_buf);
586 1873
                        chunk_freebuf(&chunk);
587 1873
                        AZ(chunk);
588
                }
589
        }
590 5321
        vtx->len = 0;
591 5321
        AN(vslq->n_outstanding);
592 5321
        vslq->n_outstanding--;
593
594 5321
        if (vslq->n_cache < VTX_CACHE) {
595 5233
                VTAILQ_INSERT_HEAD(&vslq->cache, vtx, list_child);
596 5233
                vslq->n_cache++;
597 5233
        } else
598 88
                FREE_OBJ(vtx);
599
600 5321
}
601
602
/* Lookup a vtx by vxid from the managed list */
603
static struct vtx *
604 40005
vtx_lookup(const struct VSLQ *vslq, uint64_t vxid)
605
{
606
        struct vtx_key lkey, *key;
607
        struct vtx *vtx;
608
609 40005
        AN(vslq);
610 40005
        lkey.vxid = vxid;
611 40005
        key = VRBT_FIND(vtx_tree, &vslq->tree, &lkey);
612 40005
        if (key == NULL)
613 8299
                return (NULL);
614 31706
        CAST_OBJ_NOTNULL(vtx, (void *)key, VTX_MAGIC);
615 31706
        return (vtx);
616 40005
}
617
618
/* Insert a new vtx into the managed list */
619
static struct vtx *
620 5320
vtx_add(struct VSLQ *vslq, uint64_t vxid)
621
{
622
        struct vtx *vtx;
623
624 5320
        AN(vslq);
625 5320
        vtx = vtx_new(vslq);
626 5320
        AN(vtx);
627 5320
        vtx->key.vxid = vxid;
628 5320
        AZ(VRBT_INSERT(vtx_tree, &vslq->tree, &vtx->key));
629 5320
        VTAILQ_INSERT_TAIL(&vslq->incomplete, vtx, list_vtx);
630 5320
        vslq->n_outstanding++;
631 5320
        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 5319
vtx_mark_complete(struct VSLQ *vslq, struct vtx *vtx)
638
{
639
640 5319
        AN(vslq);
641 5319
        AN(vtx->flags & VTX_F_END);
642 5319
        AZ(vtx->flags & VTX_F_COMPLETE);
643
644 5319
        if (vtx->type == VSL_t_unknown)
645 0
                vtx_diag(vtx, "vtx of unknown type marked complete");
646
647 5319
        vtx->flags |= VTX_F_COMPLETE;
648 5319
        VTAILQ_REMOVE(&vslq->incomplete, vtx, list_vtx);
649
650 5877
        while (1) {
651 5877
                AZ(vtx->flags & VTX_F_READY);
652 5877
                if (vtx->flags & VTX_F_COMPLETE &&
653 5346
                    vtx->n_child == vtx->n_childready)
654 5318
                        vtx->flags |= VTX_F_READY;
655
                else
656 559
                        return;
657 5318
                if (vtx->parent == NULL) {
658
                        /* Top level vtx ready */
659 4760
                        VTAILQ_INSERT_TAIL(&vslq->ready, vtx, list_vtx);
660 4760
                        return;
661
                }
662 558
                vtx = vtx->parent;
663 558
                vtx->n_childready++;
664 558
                assert(vtx->n_child >= vtx->n_childready);
665
        }
666 5319
}
667
668
/* Add a child to a parent, and update child counters */
669
static void
670 559
vtx_set_parent(struct vtx *parent, struct vtx *child)
671
{
672
673 559
        CHECK_OBJ_NOTNULL(parent, VTX_MAGIC);
674 559
        CHECK_OBJ_NOTNULL(child, VTX_MAGIC);
675 559
        assert(parent != child);
676 559
        AZ(parent->flags & VTX_F_COMPLETE);
677 559
        AZ(child->flags & VTX_F_COMPLETE);
678 559
        AZ(child->parent);
679 559
        child->parent = parent;
680 559
        VTAILQ_INSERT_TAIL(&parent->child, child, list_child);
681 559
        parent->n_child++;
682 559
        do
683 670
                parent->n_descend += 1 + child->n_descend;
684 670
        while ((parent = parent->parent) != NULL);
685 559
}
686
687
/* Parse a begin or link record. Returns the number of elements that was
688
   successfully parsed. */
689
static int
690 14185
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 14185
        AN(str);
700 14185
        AN(ptype);
701 14185
        AN(pvxid);
702 14185
        AN(preason);
703
704 14185
        i = sscanf(str, "%15s %ju %15s %ju", type, &vxid, reason, &sub);
705 14185
        if (i < 1)
706 0
                return (0);
707
708
        /* transaction type */
709 43057
        for (et = VSL_t_unknown; et < VSL_t__MAX; et++)
710 43056
                if (!vstrcmp(type, vsl_t_names[et]))
711 14184
                        break;
712 14185
        if (et >= VSL_t__MAX)
713 0
                et = VSL_t_unknown;
714 14183
        *ptype = et;
715 14183
        if (i == 1)
716 0
                return (1);
717
718
        /* vxid */
719 14183
        assert((vxid & ~VSL_IDENTMASK) == 0);
720 14183
        *pvxid = vxid;
721 14183
        if (i == 2)
722 0
                return (2);
723
724
        /* transaction reason */
725 56494
        for (er = VSL_r_unknown; er < VSL_r__MAX; er++)
726 56203
                if (!vstrcmp(reason, vsl_r_names[er]))
727 13892
                        break;
728 14183
        if (er >= VSL_r__MAX)
729 291
                er = VSL_r_unknown;
730 14183
        *preason = er;
731 14183
        if (i == 3)
732 13575
                return (3);
733
734
        /* request sub-level */
735 608
        if (psub != NULL)
736 220
                *psub = sub;
737 608
        return (4);
738 14183
}
739
740
/* Parse and process a begin record */
741
static int
742 5321
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 5321
        assert(VSL_TAG(ptr) == SLT_Begin);
751
752 5321
        AZ(vtx->flags & VTX_F_READY);
753
754 5321
        i = vtx_parse_link(VSL_CDATA(ptr), &type, &p_vxid, &reason, NULL);
755 5321
        if (i < 3)
756 0
                return (vtx_diag_tag(vtx, ptr, "parse error"));
757 5321
        if (type == VSL_t_unknown)
758 0
                (void)vtx_diag_tag(vtx, ptr, "unknown vxid type");
759
760
        /* Check/set vtx type */
761 5321
        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 5321
        vtx->type = type;
766 5321
        vtx->reason = reason;
767
768 5321
        if (p_vxid == 0)
769
                /* Zero means no parent */
770 1597
                return (0);
771 3724
        if (p_vxid == vtx->key.vxid)
772 0
                return (vtx_diag_tag(vtx, ptr, "link to self"));
773
774 3724
        if (vslq->grouping == VSL_g_vxid)
775 3057
                return (0);     /* No links */
776 667
        if (vslq->grouping == VSL_g_request && vtx->type == VSL_t_req &&
777 196
            vtx->reason == VSL_r_rxreq)
778 112
                return (0);     /* No links */
779
780 555
        if (vtx->parent != NULL) {
781 247
                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 247
                        return (0);
788
        }
789
790 308
        p_vtx = vtx_lookup(vslq, p_vxid);
791 308
        if (p_vtx == NULL) {
792
                /* Not seen parent yet. Create it. */
793 124
                p_vtx = vtx_add(vslq, p_vxid);
794 124
                AN(p_vtx);
795 124
        } else {
796 184
                CHECK_OBJ_NOTNULL(p_vtx, VTX_MAGIC);
797 184
                if (p_vtx->flags & VTX_F_COMPLETE)
798 0
                        return (vtx_diag_tag(vtx, ptr, "link too late"));
799
        }
800
801
        /* Create link */
802 308
        vtx_set_parent(p_vtx, vtx);
803
804 308
        return (0);
805 5321
}
806
807
/* Parse and process a link record */
808
static int
809 3851
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 3851
        assert(VSL_TAG(ptr) == SLT_Link);
818
819 3851
        AZ(vtx->flags & VTX_F_READY);
820
821 3851
        i = vtx_parse_link(VSL_CDATA(ptr), &c_type, &c_vxid, &c_reason, NULL);
822 3851
        if (i < 3)
823 0
                return (vtx_diag_tag(vtx, ptr, "parse error"));
824 3851
        if (c_type == VSL_t_unknown)
825 0
                (void)vtx_diag_tag(vtx, ptr, "unknown vxid type");
826
827 3851
        if (vslq->grouping == VSL_g_vxid)
828 3293
                return (0);     /* No links */
829 558
        if (vslq->grouping == VSL_g_request && vtx->type == VSL_t_sess)
830 0
                return (0);     /* No links */
831
832 558
        if (c_vxid == 0)
833 0
                return (vtx_diag_tag(vtx, ptr, "illegal link vxid"));
834 558
        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 558
        c_vtx = vtx_lookup(vslq, c_vxid);
839 558
        if (c_vtx == NULL) {
840
                /* Child not seen before. Insert it and create link */
841 250
                c_vtx = vtx_add(vslq, c_vxid);
842 250
                AN(c_vtx);
843 250
                AZ(c_vtx->parent);
844 250
                c_vtx->type = c_type;
845 250
                c_vtx->reason = c_reason;
846 250
                vtx_set_parent(vtx, c_vtx);
847 250
                return (0);
848
        }
849
850 308
        CHECK_OBJ(c_vtx, VTX_MAGIC);
851 308
        if (c_vtx->parent == vtx)
852
                /* Link already exists */
853 308
                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 3851
}
866
867
/* Scan the records of a vtx, performing processing actions on specific
868
   records */
869
static void
870 37818
vtx_scan(struct VSLQ *vslq, struct vtx *vtx)
871
{
872
        const uint32_t *ptr;
873
        enum VSL_tag_e tag;
874
875 397861
        while (!(vtx->flags & VTX_F_COMPLETE) &&
876 196256
            vslc_vtx_next(&vtx->c.cursor) == 1) {
877 163787
                ptr = vtx->c.cursor.rec.ptr;
878 163787
                if (VSL_ID(ptr) != vtx->key.vxid) {
879 0
                        (void)vtx_diag_tag(vtx, ptr, "vxid mismatch");
880 0
                        continue;
881
                }
882
883 163787
                tag = VSL_TAG(ptr);
884 163787
                assert(tag != SLT__Batch);
885
886 163787
                switch (tag) {
887
                case SLT_Begin:
888 5321
                        if (vtx->flags & VTX_F_BEGIN)
889 0
                                (void)vtx_diag_tag(vtx, ptr, "duplicate begin");
890
                        else {
891 5321
                                (void)vtx_scan_begin(vslq, vtx, ptr);
892 5321
                                vtx->flags |= VTX_F_BEGIN;
893
                        }
894 5321
                        break;
895
896
                case SLT_Link:
897 3851
                        (void)vtx_scan_link(vslq, vtx, ptr);
898 3851
                        break;
899
900
                case SLT_End:
901 5320
                        AZ(vtx->flags & VTX_F_END);
902 5320
                        vtx->flags |= VTX_F_END;
903 5320
                        vtx_mark_complete(vslq, vtx);
904 5320
                        break;
905
906
                default:
907 149295
                        break;
908
                }
909
        }
910 37818
}
911
912
/* Force a vtx into complete status by synthing the necessary outstanding
913
   records */
914
static void
915 840
vtx_force(struct VSLQ *vslq, struct vtx *vtx, const char *reason)
916
{
917
918 840
        AZ(vtx->flags & (VTX_F_COMPLETE | VTX_F_READY));
919 840
        vtx_scan(vslq, vtx);
920 840
        if (!(vtx->flags & VTX_F_BEGIN))
921 6
                vtx_synth_rec(vtx, SLT_Begin, "%s %u synth",
922 3
                    vsl_t_names[vtx->type], 0);
923 840
        vtx_diag(vtx, reason);
924 840
        if (!(vtx->flags & VTX_F_END))
925 840
                vtx_synth_rec(vtx, SLT_End, "synth");
926 840
        vtx_scan(vslq, vtx);
927 840
        AN(vtx->flags & VTX_F_COMPLETE);
928 840
}
929
930
static int
931 44
vslq_ratelimit(struct VSLQ *vslq)
932
{
933
        vtim_mono now;
934
        vtim_dur delta;
935
936 44
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
937 44
        CHECK_OBJ_NOTNULL(vslq->vsl, VSL_MAGIC);
938
939 44
        now = VTIM_mono();
940 44
        delta = now - vslq->last_use;
941 44
        vslq->credits += (delta / vslq->vsl->R_opt_p) * vslq->vsl->R_opt_l;
942 44
        vslq->credits = vmin_t(double, vslq->credits, vslq->vsl->R_opt_l);
943 44
        vslq->last_use = now;
944
945 44
        if (vslq->credits < 1.0)
946 0
                return (0);
947
948 44
        vslq->credits -= 1.0;
949 44
        return (1);
950 44
}
951
952
/* Build transaction array, do the query and callback. Returns 0 or the
953
   return value from func */
954
static int
955 3934
vslq_callback(struct VSLQ *vslq, struct vtx *vtx, VSLQ_dispatch_f *func,
956
    void *priv)
957
{
958 3934
        unsigned n = vtx->n_descend + 1;
959 3934
        struct vtx *vtxs[n];
960 3934
        struct VSL_transaction trans[n];
961 3934
        struct VSL_transaction *ptrans[n + 1];
962
        unsigned i, j;
963
964 3934
        AN(vslq);
965 3934
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
966 3934
        AN(vtx->flags & VTX_F_READY);
967 3934
        AN(func);
968
969 3934
        if (vslq->grouping == VSL_g_session &&
970 91
            vtx->type != VSL_t_sess)
971 0
                return (0);
972 3934
        if (vslq->grouping == VSL_g_request &&
973 112
            vtx->type != VSL_t_req)
974 0
                return (0);
975
976
        /* Build transaction array */
977 3934
        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
978 3934
        vtxs[0] = vtx;
979 3934
        trans[0].level = 1;
980 3934
        trans[0].vxid = vtx->key.vxid;
981 3934
        trans[0].vxid_parent = 0;
982 3934
        trans[0].type = vtx->type;
983 3934
        trans[0].reason = vtx->reason;
984 3934
        trans[0].c = &vtx->c.cursor;
985 3934
        i = 1;
986 3934
        j = 0;
987 8358
        while (j < i) {
988 4919
                VTAILQ_FOREACH(vtx, &vtxs[j]->child, list_child) {
989 495
                        assert(i < n);
990 495
                        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
991 495
                        vtxs[i] = vtx;
992 495
                        if (vtx->reason == VSL_r_restart)
993
                                /* Restarts stay at the same level as parent */
994 8
                                trans[i].level = trans[j].level;
995
                        else
996 487
                                trans[i].level = trans[j].level + 1;
997 495
                        trans[i].vxid = vtx->key.vxid;
998 495
                        trans[i].vxid_parent = trans[j].vxid;
999 495
                        trans[i].type = vtx->type;
1000 495
                        trans[i].reason = vtx->reason;
1001 495
                        trans[i].c = &vtx->c.cursor;
1002 495
                        i++;
1003 495
                }
1004 4424
                j++;
1005
        }
1006 3934
        assert(i == n);
1007
1008
        /* Build pointer array */
1009 8364
        for (i = 0; i < n; i++)
1010 4430
                ptrans[i] = &trans[i];
1011 3934
        ptrans[i] = NULL;
1012
1013
        /* Query test goes here */
1014 3934
        if (vslq->query != NULL && !vslq_runquery(vslq->query, ptrans))
1015 1178
                return (0);
1016
1017 2756
        if (vslq->vsl->R_opt_l != 0 && !vslq_ratelimit(vslq))
1018 0
                return (0);
1019
1020
        /* Callback */
1021 2756
        return ((func)(vslq->vsl, ptrans, priv));
1022 3934
}
1023
1024
/* Create a synthetic log record. The record will be inserted at the
1025
   current cursor offset */
1026
static void
1027 1683
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 1683
        ALLOC_OBJ(synth, SYNTH_MAGIC);
1036 1683
        AN(synth);
1037
1038 1683
        buf = VSL_DATA(synth->data);
1039 1683
        buflen = sizeof(synth->data) - VSL_BYTES(VSL_OVERHEAD);
1040 1683
        va_start(ap, fmt);
1041 1683
        l = vsnprintf(buf, buflen, fmt, ap);
1042 1683
        assert(l >= 0);
1043 1683
        va_end(ap);
1044 1683
        if (l > buflen - 1)
1045 0
                l = buflen - 1;
1046 1683
        buf[l++] = '\0';        /* NUL-terminated */
1047 1683
        vxid = vtx->key.vxid;
1048 1683
        switch (vtx->type) {
1049
        case VSL_t_req:
1050 81
                vxid |= VSL_CLIENTMARKER;
1051 81
                break;
1052
        case VSL_t_bereq:
1053 8
                vxid |= VSL_BACKENDMARKER;
1054 8
                break;
1055
        default:
1056 1594
                break;
1057
        }
1058 1683
        synth->data[2] = vxid >> 32;
1059 1683
        synth->data[1] = vxid;
1060 3366
        synth->data[0] = (((tag & VSL_IDMASK) << VSL_IDSHIFT) |
1061 1683
            (VSL_VERSION_3 << VSL_VERSHIFT) | l);
1062 1683
        synth->offset = vtx->c.offset;
1063
1064 1683
        VTAILQ_FOREACH_REVERSE(it, &vtx->synth, synthhead, list) {
1065
                /* Make sure the synth list is sorted on offset */
1066 843
                CHECK_OBJ_NOTNULL(it, SYNTH_MAGIC);
1067 843
                if (synth->offset >= it->offset)
1068 843
                        break;
1069 0
        }
1070 1683
        if (it != NULL)
1071 843
                VTAILQ_INSERT_AFTER(&vtx->synth, it, synth, list);
1072
        else
1073 840
                VTAILQ_INSERT_HEAD(&vtx->synth, synth, list);
1074
1075
        /* Update cursor */
1076 1683
        CHECK_OBJ_ORNULL(vtx->c.synth, SYNTH_MAGIC);
1077 1683
        if (vtx->c.synth == NULL || vtx->c.synth->offset > synth->offset)
1078 840
                vtx->c.synth = synth;
1079 1683
}
1080
1081
/* Add a diagnostic SLT_VSL synth record to the vtx. */
1082
static int
1083 840
vtx_diag(struct vtx *vtx, const char *msg)
1084
{
1085
1086 840
        vtx_synth_rec(vtx, SLT_VSL, msg);
1087 840
        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 1844
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 1844
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
1109 1844
        if (grouping >= VSL_g__MAX) {
1110 0
                (void)vsl_diag(vsl, "Illegal query grouping");
1111 0
                return (NULL);
1112
        }
1113 1844
        if (querystring != NULL) {
1114 944
                query = vslq_newquery(vsl, grouping, querystring);
1115 944
                if (query == NULL)
1116 128
                        return (NULL);
1117 816
        } else
1118 900
                query = NULL;
1119
1120 1716
        ALLOC_OBJ(vslq, VSLQ_MAGIC);
1121 1716
        AN(vslq);
1122 1716
        vslq->vsl = vsl;
1123 1716
        if (cp != NULL) {
1124 1340
                vslq->c = *cp;
1125 1340
                *cp = NULL;
1126 1340
        }
1127 1716
        vslq->grouping = grouping;
1128 1716
        vslq->query = query;
1129 1716
        if (vslq->vsl->R_opt_l != 0) {
1130 8
                vslq->last_use = VTIM_mono();
1131 8
                vslq->credits = 1;
1132 8
        }
1133
1134
        /* Setup normal mode */
1135 1716
        VRBT_INIT(&vslq->tree);
1136 1716
        VTAILQ_INIT(&vslq->ready);
1137 1716
        VTAILQ_INIT(&vslq->incomplete);
1138 1716
        VTAILQ_INIT(&vslq->shmrefs);
1139 1716
        VTAILQ_INIT(&vslq->cache);
1140
1141
        /* Setup raw mode */
1142 1716
        vslq->raw.c.magic = VSLC_RAW_MAGIC;
1143 1716
        vslq->raw.c.cursor.priv_tbl = &vslc_raw_tbl;
1144 1716
        vslq->raw.c.cursor.priv_data = &vslq->raw.c;
1145 1716
        vslq->raw.trans.level = 0;
1146 1716
        vslq->raw.trans.type = VSL_t_raw;
1147 1716
        vslq->raw.trans.reason = VSL_r_unknown;
1148 1716
        vslq->raw.trans.c = &vslq->raw.c.cursor;
1149 1716
        vslq->raw.ptrans[0] = &vslq->raw.trans;
1150 1716
        vslq->raw.ptrans[1] = NULL;
1151
1152 1716
        return (vslq);
1153 1844
}
1154
1155
void
1156 1692
VSLQ_Delete(struct VSLQ **pvslq)
1157
{
1158
        struct VSLQ *vslq;
1159
        struct vtx *vtx;
1160
1161 1692
        TAKE_OBJ_NOTNULL(vslq, pvslq, VSLQ_MAGIC);
1162
1163 1692
        (void)VSLQ_Flush(vslq, NULL, NULL);
1164 1692
        AZ(vslq->n_outstanding);
1165
1166 1692
        if (vslq->c != NULL) {
1167 1692
                VSL_DeleteCursor(vslq->c);
1168 1692
                vslq->c = NULL;
1169 1692
        }
1170
1171 1692
        if (vslq->query != NULL)
1172 816
                vslq_deletequery(&vslq->query);
1173 1692
        AZ(vslq->query);
1174
1175 4514
        while (!VTAILQ_EMPTY(&vslq->cache)) {
1176 2822
                AN(vslq->n_cache);
1177 2822
                vtx = VTAILQ_FIRST(&vslq->cache);
1178 2822
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1179 2822
                VTAILQ_REMOVE(&vslq->cache, vtx, list_child);
1180 2822
                vslq->n_cache--;
1181 2822
                FREE_OBJ(vtx);
1182
        }
1183
1184 1692
        FREE_OBJ(vslq);
1185 1692
}
1186
1187
void
1188 352
VSLQ_SetCursor(struct VSLQ *vslq, struct VSL_cursor **cp)
1189
{
1190
1191 352
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1192
1193 352
        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 352
        if (cp != NULL) {
1201 352
                AN(*cp);
1202 352
                vslq->c = *cp;
1203 352
                *cp = NULL;
1204 352
        }
1205 352
}
1206
1207
/* Regard each log line as a single transaction, feed it through the query
1208
   and do the callback */
1209
static int
1210 88866
vslq_raw(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1211
{
1212 88866
        enum vsl_status r = vsl_more;
1213
        int i;
1214
1215 88866
        assert(vslq->grouping == VSL_g_raw);
1216
1217 88866
        assert(vslq->raw.offset <= vslq->raw.len);
1218 88866
        do {
1219 102743
                if (vslq->raw.offset == vslq->raw.len) {
1220 55774
                        r = VSL_Next(vslq->c);
1221 55774
                        if (r != vsl_more)
1222 28144
                                return (r);
1223 27630
                        AN(vslq->c->rec.ptr);
1224 27630
                        vslq->raw.start = vslq->c->rec;
1225 27630
                        if (VSL_TAG(vslq->c->rec.ptr) == SLT__Batch)
1226 27754
                                vslq->raw.len = VSL_END(vslq->c->rec.ptr,
1227 13877
                                    VSL_BATCHLEN(vslq->c->rec.ptr)) -
1228 13877
                                    vslq->c->rec.ptr;
1229
                        else
1230 27506
                                vslq->raw.len = VSL_NEXT(vslq->raw.start.ptr) -
1231 13753
                                    vslq->raw.start.ptr;
1232 27630
                        assert(vslq->raw.len > 0);
1233 27630
                        vslq->raw.offset = 0;
1234 27630
                }
1235
1236 74599
                vslq->raw.c.ptr = vslq->raw.start.ptr + vslq->raw.offset;
1237 74599
                vslq->raw.c.cursor.rec.ptr = NULL;
1238 74599
                vslq->raw.trans.vxid = VSL_ID(vslq->raw.c.ptr);
1239 74599
                vslq->raw.offset += VSL_NEXT(vslq->raw.c.ptr) - vslq->raw.c.ptr;
1240 74599
        } while (VSL_TAG(vslq->raw.c.ptr) == SLT__Batch);
1241
1242 60722
        assert (r == vsl_more);
1243
1244 60722
        if (func == NULL)
1245 0
                return (r);
1246
1247 60722
        if (vslq->query != NULL &&
1248 9244
            !vslq_runquery(vslq->query, vslq->raw.ptrans))
1249 8532
                return (r);
1250
1251 52190
        if (vslq->vsl->R_opt_l != 0 && !vslq_ratelimit(vslq))
1252 0
                return (r);
1253
1254 52190
        i = (func)(vslq->vsl, vslq->raw.ptrans, priv);
1255 52190
        if (i)
1256 408
                return (i);
1257
1258 51782
        return (r);
1259 88866
}
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 49272
vslq_shmref_check(struct VSLQ *vslq)
1266
{
1267
        struct chunk *chunk;
1268
        enum vsl_check i;
1269
1270 49272
        while ((chunk = VTAILQ_FIRST(&vslq->shmrefs)) != NULL) {
1271 36864
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
1272 36864
                assert(chunk->type == chunk_t_shm);
1273 36864
                i = VSL_Check(vslq->c, &chunk->shm.start);
1274 36864
                switch (i) {
1275
                case vsl_check_valid:
1276
                        /* First on list is OK, refs behind it must also
1277
                           be OK */
1278 36864
                        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 12408
        return (vsl_more);
1290 49272
}
1291
1292
static unsigned
1293 5957
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 5957
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1303 5957
        AN(ptr);
1304
1305 5957
        assert(vslq->grouping != VSL_g_raw);
1306 5957
        if (vslq->grouping == VSL_g_session)
1307 447
                return (1); /* All are needed */
1308
1309 5510
        vsl = vslq->vsl;
1310 5510
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
1311 5510
        if (vslq->grouping == VSL_g_vxid) {
1312 5095
                if (!vsl->c_opt && !vsl->b_opt)
1313 3659
                        AZ(vsl->E_opt);
1314 1436
                else if (!vsl->b_opt && !VSL_CLIENT(ptr))
1315 332
                        return (0);
1316 1104
                else if (!vsl->c_opt && !VSL_BACKEND(ptr))
1317 168
                        return (0);
1318
                /* Need to parse the Begin tag - fallthrough to below */
1319 4595
        }
1320
1321 5010
        tag = VSL_TAG(ptr);
1322 5010
        assert(tag == SLT_Begin);
1323 5010
        i = vtx_parse_link(VSL_CDATA(ptr), &type, &p_vxid, &reason, &sub);
1324 5010
        if (i < 3 || type == VSL_t_unknown)
1325 0
                return (0);
1326
1327 5010
        if (vslq->grouping == VSL_g_request && type == VSL_t_sess)
1328 80
                return (0);
1329
1330 4930
        if (vslq->grouping == VSL_g_vxid && i > 3 && sub > 0 && !vsl->E_opt)
1331 60
                return (0);
1332
1333 4870
        return (1);
1334 5957
}
1335
1336
/* Process next input record */
1337
static enum vsl_status
1338 89560
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 89560
        c = vslq->c;
1350 89560
        r = VSL_Next(c);
1351 89560
        if (r != vsl_more)
1352 40276
                return (r);
1353
1354 49284
        assert (r == vsl_more);
1355
1356 49284
        tag = (enum VSL_tag_e)VSL_TAG(c->rec.ptr);
1357 49284
        if (tag == SLT__Batch) {
1358 15883
                vxid = VSL_BATCHID(c->rec.ptr);
1359 31766
                len = VSL_END(c->rec.ptr, VSL_BATCHLEN(c->rec.ptr)) -
1360 15883
                    c->rec.ptr;
1361 15883
                if (len == 0)
1362 0
                        return (r);
1363 15883
                ptr = VSL_NEXT(c->rec.ptr);
1364 15883
                tag = (enum VSL_tag_e)VSL_TAG(ptr);
1365 15883
        } else {
1366 33401
                vxid = VSL_ID(c->rec.ptr);
1367 33401
                len = VSL_NEXT(c->rec.ptr) - c->rec.ptr;
1368 33401
                ptr = c->rec.ptr;
1369
        }
1370 49284
        assert(len > 0);
1371 49284
        if (vxid == 0)
1372
                /* Skip non-transactional records */
1373 10141
                return (r);
1374
1375 39143
        vtx = vtx_lookup(vslq, vxid);
1376 39143
        keep = tag != SLT_Begin || vslq_candidate(vslq, ptr);
1377 39143
        if (vtx == NULL && tag == SLT_Begin && keep) {
1378 4946
                vtx = vtx_add(vslq, vxid);
1379 4946
                AN(vtx);
1380 4946
        }
1381 39143
        if (vtx != NULL) {
1382 36156
                AN(keep);
1383 36156
                r = vtx_append(vslq, vtx, &c->rec, len);
1384 36156
                if (r == vsl_more)
1385 36147
                        vtx_scan(vslq, vtx);
1386 36156
        }
1387
1388 39143
        return (r);
1389 89560
}
1390
1391
/* Test query and report any ready transactions */
1392
static int
1393 5625
vslq_process_ready(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1394
{
1395
        struct vtx *vtx;
1396 5625
        int i = 0;
1397
1398 5625
        AN(vslq);
1399
1400 9450
        while (!VTAILQ_EMPTY(&vslq->ready)) {
1401 4761
                vtx = VTAILQ_FIRST(&vslq->ready);
1402 4761
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1403 4761
                VTAILQ_REMOVE(&vslq->ready, vtx, list_vtx);
1404 4761
                AN(vtx->flags & VTX_F_READY);
1405 4761
                if (func != NULL)
1406 3932
                        i = vslq_callback(vslq, vtx, func, priv);
1407 4761
                vtx_retire(vslq, &vtx);
1408 4761
                AZ(vtx);
1409 4761
                if (i)
1410 936
                        return (i);
1411
        }
1412
1413 4689
        return (0);
1414 5625
}
1415
1416
/* Process the input cursor, calling the callback function on matching
1417
   transaction sets */
1418
int
1419 178376
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 178376
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1427
1428
        /* Check that we have a cursor */
1429 178376
        if (vslq->c == NULL)
1430 0
                return (vsl_e_abandon);
1431
1432 178376
        if (vslq->grouping == VSL_g_raw)
1433 88866
                return (vslq_raw(vslq, func, priv));
1434
1435
        /* Process next cursor input */
1436 89510
        r = vslq_next(vslq);
1437 89510
        if (r != vsl_more)
1438
                /* At end of log or cursor reports error condition */
1439 40236
                return (r);
1440
1441
        /* Check shmref list and buffer if necessary */
1442 49274
        r = vslq_shmref_check(vslq);
1443 49274
        if (r != vsl_more)
1444
                /* Buffering of shm ref failed */
1445 0
                return (r);
1446
1447 49274
        assert (r == vsl_more);
1448
1449
        /* Check vtx timeout */
1450 49274
        now = VTIM_mono();
1451 49282
        while (!VTAILQ_EMPTY(&vslq->incomplete)) {
1452 36225
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1453 36225
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1454 36225
                if (now - vtx->t_start < vslq->vsl->T_opt)
1455 36217
                        break;
1456 8
                vtx_force(vslq, vtx, "timeout");
1457 8
                AN(vtx->flags & VTX_F_COMPLETE);
1458
        }
1459
1460
        /* Check store limit */
1461 49274
        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 49274
        if (!VTAILQ_EMPTY(&vslq->ready)) {
1475 3933
                i = vslq_process_ready(vslq, func, priv);
1476 3933
                if (i)
1477
                        /* User return code */
1478 936
                        return (i);
1479 2997
        }
1480
1481 48338
        return (vsl_more);
1482 178376
}
1483
1484
/* Flush any incomplete vtx held on to. Do callbacks if func != NULL */
1485
int
1486 1692
VSLQ_Flush(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1487
{
1488
        struct vtx *vtx;
1489
1490 1692
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1491
1492 2524
        while (!VTAILQ_EMPTY(&vslq->incomplete)) {
1493 832
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1494 832
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1495 832
                AZ(vtx->flags & VTX_F_COMPLETE);
1496 832
                vtx_force(vslq, vtx, "flush");
1497
        }
1498
1499 1692
        return (vslq_process_ready(vslq, func, priv));
1500
}