Code Coverage: vinyl-cache/lib/libvinylapi/vsl_dispatch.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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 1408
#define VTX_CACHE 10
54 518
#define VTX_BUFSIZE_MIN 64
55 3088
#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 452
#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 2316
#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 772
#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 3048
#define VTX_F_BEGIN             0x1 /* Begin record processed */
154 1638
#define VTX_F_END               0x2 /* End record processed */
155 57043
#define VTX_F_COMPLETE          0x4 /* Marked complete. No new children
156
                                       should be appended */
157 1408
#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
vtx_keycmp(const struct vtx_key *a, const struct vtx_key *b)
223 15058
{
224 15058
        if (a->vxid < b->vxid)
225 397
                return (-1);
226 14661
        if (a->vxid > b->vxid)
227 5963
                return (1);
228 8698
        return (0);
229 14661
}
230
231 886
VRBT_GENERATE_REMOVE_COLOR(vtx_tree, vtx_key, entry, static)
232 1408
VRBT_GENERATE_REMOVE(vtx_tree, vtx_key, entry, static)
233 937
VRBT_GENERATE_INSERT_COLOR(vtx_tree, vtx_key, entry, static)
234 1409
VRBT_GENERATE_INSERT_FINISH(vtx_tree, vtx_key, entry, static)
235 1409
VRBT_GENERATE_INSERT(vtx_tree, vtx_key, entry, vtx_keycmp, static)
236 10868
VRBT_GENERATE_FIND(vtx_tree, vtx_key, entry, vtx_keycmp, static)
237
238
static enum vsl_status v_matchproto_(vslc_next_f)
239
vslc_raw_next(const struct VSL_cursor *cursor)
240 29092
{
241 29092
        struct vslc_raw *c;
242
243 29092
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
244 29092
        assert(&c->cursor == cursor);
245
246 29092
        AN(c->ptr);
247 29092
        if (c->cursor.rec.ptr == NULL) {
248 14648
                c->cursor.rec.ptr = c->ptr;
249 14648
                return (vsl_more);
250 14648
        } else {
251 14444
                c->cursor.rec.ptr = NULL;
252 14444
                return (vsl_end);
253 14444
        }
254 29092
}
255
256
static enum vsl_status v_matchproto_(vslc_reset_f)
257
vslc_raw_reset(const struct VSL_cursor *cursor)
258 3942
{
259 3942
        struct vslc_raw *c;
260
261 3942
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_RAW_MAGIC);
262 3942
        assert(&c->cursor == cursor);
263
264 3942
        AN(c->ptr);
265 3942
        c->cursor.rec.ptr = NULL;
266
267 3942
        return (vsl_end);
268 3942
}
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
vslc_vtx_next(const struct VSL_cursor *cursor)
280 85281
{
281 85281
        struct vslc_vtx *c;
282 85281
        const uint32_t *ptr;
283 85281
        unsigned overrun;
284
285 85281
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
286 85281
        assert(&c->cursor == cursor);
287 85281
        CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
288
289 92923
        do {
290 92923
                CHECK_OBJ_ORNULL(c->synth, SYNTH_MAGIC);
291 92923
                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 465
                        c->cursor.rec.ptr = c->synth->data;
295 465
                        c->synth = VTAILQ_NEXT(c->synth, list);
296 92458
                } else {
297 92458
                        overrun = c->offset > c->vtx->len;
298 92458
                        AZ(overrun);
299 92458
                        if (c->offset == c->vtx->len)
300 9670
                                return (vsl_end);
301
302
                        /* Advance chunk pointer */
303 82788
                        if (c->chunk == NULL) {
304 2610
                                c->chunk = VTAILQ_FIRST(&c->vtx->chunks);
305 2610
                                c->chunkstart = 0;
306 2610
                        }
307 82788
                        CHECK_OBJ_NOTNULL(c->chunk, CHUNK_MAGIC);
308 85237
                        while (c->offset >= c->chunkstart + c->chunk->len) {
309 2449
                                c->chunkstart += c->chunk->len;
310 2449
                                c->chunk = VTAILQ_NEXT(c->chunk, list);
311 2449
                                CHECK_OBJ_NOTNULL(c->chunk, CHUNK_MAGIC);
312 2449
                        }
313
314
                        /* Point to the next stored record */
315 82788
                        if (c->chunk->type == chunk_t_shm)
316 66031
                                ptr = c->chunk->shm.start.ptr;
317 16757
                        else {
318 16757
                                assert(c->chunk->type == chunk_t_buf);
319 16757
                                ptr = c->chunk->buf.data;
320 16757
                        }
321 82788
                        c->cursor.rec.ptr = ptr + c->offset - c->chunkstart;
322 82788
                        c->offset += VSL_NEXT(c->cursor.rec.ptr) -
323 82788
                            c->cursor.rec.ptr;
324 82788
                }
325 92923
        } while (VSL_TAG(c->cursor.rec.ptr) == SLT__Batch);
326
327 75611
        return (vsl_more);
328 85281
}
329
330
static enum vsl_status v_matchproto_(vslc_reset_f)
331
vslc_vtx_reset(const struct VSL_cursor *cursor)
332 3624
{
333 3624
        struct vslc_vtx *c;
334
335 3624
        CAST_OBJ_NOTNULL(c, cursor->priv_data, VSLC_VTX_MAGIC);
336 3624
        assert(&c->cursor == cursor);
337 3624
        CHECK_OBJ_NOTNULL(c->vtx, VTX_MAGIC);
338 3624
        c->synth = VTAILQ_FIRST(&c->vtx->synth);
339 3624
        c->chunk = NULL;
340 3624
        c->chunkstart = 0;
341 3624
        c->offset = 0;
342 3624
        c->cursor.rec.ptr = NULL;
343
344 3624
        return (vsl_end);
345 3624
}
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
chunk_newbuf(struct vtx *vtx, const uint32_t *ptr, size_t len)
358 518
{
359 518
        struct chunk *chunk;
360
361 518
        ALLOC_OBJ(chunk, CHUNK_MAGIC);
362 518
        XXXAN(chunk);
363 518
        chunk->type = chunk_t_buf;
364 518
        chunk->vtx = vtx;
365 518
        chunk->buf.space = VTX_BUFSIZE_MIN;
366 518
        while (chunk->buf.space < len)
367 0
                chunk->buf.space *= 2;
368 518
        chunk->buf.data = malloc(sizeof (uint32_t) * chunk->buf.space);
369 518
        AN(chunk->buf.data);
370 518
        vmemcpy(chunk->buf.data, ptr, sizeof(uint32_t) * len);
371 518
        chunk->len = len;
372 518
        return (chunk);
373 518
}
374
375
/* Free a buf chunk */
376
static void
377
chunk_freebuf(struct chunk **pchunk)
378 518
{
379 518
        struct chunk *chunk;
380
381 518
        TAKE_OBJ_NOTNULL(chunk, pchunk, CHUNK_MAGIC);
382 518
        assert(chunk->type == chunk_t_buf);
383 518
        free(chunk->buf.data);
384 518
        FREE_OBJ(chunk);
385 518
}
386
387
/* Append a set of records to a chunk */
388
static void
389
chunk_appendbuf(struct chunk *chunk, const uint32_t *ptr, size_t len)
390 6880
{
391
392 6880
        CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
393 6880
        assert(chunk->type == chunk_t_buf);
394 6880
        if (chunk->buf.space < chunk->len + len) {
395 2173
                while (chunk->buf.space < chunk->len + len)
396 1087
                        chunk->buf.space *= 2;
397 1086
                chunk->buf.data = realloc(chunk->buf.data,
398 1086
                    sizeof (uint32_t) * chunk->buf.space);
399 1086
        }
400 6880
        vmemcpy(chunk->buf.data + chunk->len, ptr, sizeof(uint32_t) * len);
401 6880
        chunk->len += len;
402 6880
}
403
404
/* Transform a shm chunk to a buf chunk */
405
static void
406
chunk_shm_to_buf(struct VSLQ *vslq, struct chunk *chunk)
407 0
{
408 0
        struct vtx *vtx;
409 0
        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 0
        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 0
        }
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
vtx_append(struct VSLQ *vslq, struct vtx *vtx, const struct VSLC_ptr *start,
440
    size_t len)
441 9885
{
442 9885
        struct chunk *chunk;
443 9885
        enum vsl_check i;
444
445 9885
        AN(vtx);
446 9885
        AN(len);
447 9885
        AN(start);
448
449 9885
        i = VSL_Check(vslq->c, start);
450 9885
        if (i == vsl_check_e_inval)
451 0
                return (vsl_e_overrun);
452
453 9885
        if (i == vsl_check_valid && !VTAILQ_EMPTY(&vtx->shmchunks_free)) {
454
                /* Shmref it */
455 2488
                chunk = VTAILQ_FIRST(&vtx->shmchunks_free);
456 2488
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
457 2488
                assert(chunk->type == chunk_t_shm);
458 2488
                assert(chunk->vtx == vtx);
459 2488
                VTAILQ_REMOVE(&vtx->shmchunks_free, chunk, list);
460 2488
                chunk->shm.start = *start;
461 2488
                chunk->len = len;
462 2488
                VTAILQ_INSERT_TAIL(&vtx->chunks, chunk, list);
463
464
                /* Append to shmref list */
465 2488
                VTAILQ_INSERT_TAIL(&vslq->shmrefs, chunk, shm.shmref);
466 7397
        } else {
467
                /* Buffer it */
468 7397
                chunk = VTAILQ_LAST(&vtx->chunks, chunkhead);
469 7397
                CHECK_OBJ_ORNULL(chunk, CHUNK_MAGIC);
470 7397
                if (chunk != NULL && chunk->type == chunk_t_buf) {
471
                        /* Tail is a buf chunk, append to that */
472 6879
                        chunk_appendbuf(chunk, start->ptr, len);
473 6879
                } else {
474
                        /* Append new buf chunk */
475 518
                        chunk = chunk_newbuf(vtx, start->ptr, len);
476 518
                        AN(chunk);
477 518
                        VTAILQ_INSERT_TAIL(&vtx->chunks, chunk, list);
478 518
                }
479 7397
        }
480 9885
        vtx->len += len;
481 9885
        return (vsl_more);
482 9885
}
483
484
/* Allocate a new vtx structure */
485
static struct vtx *
486
vtx_new(struct VSLQ *vslq)
487 1408
{
488 1408
        struct vtx *vtx;
489 1408
        int i;
490
491 1408
        AN(vslq);
492 1408
        if (vslq->n_cache) {
493 636
                AZ(VTAILQ_EMPTY(&vslq->cache));
494 636
                vtx = VTAILQ_FIRST(&vslq->cache);
495 636
                VTAILQ_REMOVE(&vslq->cache, vtx, list_child);
496 636
                vslq->n_cache--;
497 772
        } else {
498 772
                ALLOC_OBJ(vtx, VTX_MAGIC);
499 772
                AN(vtx);
500
501 772
                VTAILQ_INIT(&vtx->child);
502 772
                VTAILQ_INIT(&vtx->shmchunks_free);
503 3088
                for (i = 0; i < VTX_SHMCHUNKS; i++) {
504 2316
                        vtx->shmchunks[i].magic = CHUNK_MAGIC;
505 2316
                        vtx->shmchunks[i].type = chunk_t_shm;
506 2316
                        vtx->shmchunks[i].vtx = vtx;
507 2316
                        VTAILQ_INSERT_TAIL(&vtx->shmchunks_free,
508 2316
                            &vtx->shmchunks[i], list);
509 2316
                }
510 772
                VTAILQ_INIT(&vtx->chunks);
511 772
                VTAILQ_INIT(&vtx->synth);
512 772
                vtx->c.magic = VSLC_VTX_MAGIC;
513 772
                vtx->c.vtx = vtx;
514 772
                vtx->c.cursor.priv_tbl = &vslc_vtx_tbl;
515 772
                vtx->c.cursor.priv_data = &vtx->c;
516 772
        }
517
518 1408
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
519 1408
        vtx->key.vxid = 0;
520 1408
        vtx->t_start = VTIM_mono();
521 1408
        vtx->flags = 0;
522 1408
        vtx->type = VSL_t_unknown;
523 1408
        vtx->reason = VSL_r_unknown;
524 1408
        vtx->parent = NULL;
525 1408
        vtx->n_child = 0;
526 1408
        vtx->n_childready = 0;
527 1408
        vtx->n_descend = 0;
528 1408
        vtx->len = 0;
529 1408
        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
530
531 1408
        return (vtx);
532 1408
}
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
vtx_retire(struct VSLQ *vslq, struct vtx **pvtx)
538 1408
{
539 1408
        struct vtx *vtx;
540 1408
        struct vtx *child;
541 1408
        struct synth *synth;
542 1408
        struct chunk *chunk;
543
544 1408
        AN(vslq);
545 1408
        TAKE_OBJ_NOTNULL(vtx, pvtx, VTX_MAGIC);
546
547 1408
        assert((vtx->flags & (VTX_F_COMPLETE | VTX_F_READY)) == (VTX_F_COMPLETE | VTX_F_READY));
548 1408
        AZ(vtx->parent);
549
550 1548
        while (!VTAILQ_EMPTY(&vtx->child)) {
551 140
                child = VTAILQ_FIRST(&vtx->child);
552 140
                assert(child->parent == vtx);
553 140
                AN(vtx->n_child);
554 140
                assert(vtx->n_descend >= child->n_descend + 1);
555 140
                VTAILQ_REMOVE(&vtx->child, child, list_child);
556 140
                child->parent = NULL;
557 140
                vtx->n_child--;
558 140
                vtx->n_descend -= child->n_descend + 1;
559 140
                vtx_retire(vslq, &child);
560 140
                AZ(child);
561 140
        }
562 1408
        AZ(vtx->n_child);
563 1408
        AZ(vtx->n_descend);
564 1408
        vtx->n_childready = 0;
565
        // remove rval is no way to check if element was present
566 1408
        (void)VRBT_REMOVE(vtx_tree, &vslq->tree, &vtx->key);
567 1408
        vtx->key.vxid = 0;
568 1408
        vtx->flags = 0;
569
570 1869
        while (!VTAILQ_EMPTY(&vtx->synth)) {
571 461
                synth = VTAILQ_FIRST(&vtx->synth);
572 461
                CHECK_OBJ_NOTNULL(synth, SYNTH_MAGIC);
573 461
                VTAILQ_REMOVE(&vtx->synth, synth, list);
574 461
                FREE_OBJ(synth);
575 461
        }
576
577 4414
        while (!VTAILQ_EMPTY(&vtx->chunks)) {
578 3006
                chunk = VTAILQ_FIRST(&vtx->chunks);
579 3006
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
580 3006
                VTAILQ_REMOVE(&vtx->chunks, chunk, list);
581 3006
                if (chunk->type == chunk_t_shm) {
582 2488
                        VTAILQ_REMOVE(&vslq->shmrefs, chunk, shm.shmref);
583 2488
                        VTAILQ_INSERT_HEAD(&vtx->shmchunks_free, chunk, list);
584 2488
                } else {
585 518
                        assert(chunk->type == chunk_t_buf);
586 518
                        chunk_freebuf(&chunk);
587 518
                        AZ(chunk);
588 518
                }
589 3006
        }
590 1408
        vtx->len = 0;
591 1408
        AN(vslq->n_outstanding);
592 1408
        vslq->n_outstanding--;
593
594 1408
        if (vslq->n_cache < VTX_CACHE) {
595 1384
                VTAILQ_INSERT_HEAD(&vslq->cache, vtx, list_child);
596 1384
                vslq->n_cache++;
597 1384
        } else
598 24
                FREE_OBJ(vtx);
599
600 1408
}
601
602
/* Lookup a vtx by vxid from the managed list */
603
static struct vtx *
604
vtx_lookup(const struct VSLQ *vslq, uint64_t vxid)
605 10867
{
606 10867
        struct vtx_key lkey, *key;
607 10867
        struct vtx *vtx;
608
609 10867
        AN(vslq);
610 10867
        lkey.vxid = vxid;
611 10867
        key = VRBT_FIND(vtx_tree, &vslq->tree, &lkey);
612 10867
        if (key == NULL)
613 2165
                return (NULL);
614 8702
        CAST_OBJ_NOTNULL(vtx, (void *)key, VTX_MAGIC);
615 8702
        return (vtx);
616 8702
}
617
618
/* Insert a new vtx into the managed list */
619
static struct vtx *
620
vtx_add(struct VSLQ *vslq, uint64_t vxid)
621 1408
{
622 1408
        struct vtx *vtx;
623
624 1408
        AN(vslq);
625 1408
        vtx = vtx_new(vslq);
626 1408
        AN(vtx);
627 1408
        vtx->key.vxid = vxid;
628 1408
        AZ(VRBT_INSERT(vtx_tree, &vslq->tree, &vtx->key));
629 1408
        VTAILQ_INSERT_TAIL(&vslq->incomplete, vtx, list_vtx);
630 1408
        vslq->n_outstanding++;
631 1408
        return (vtx);
632 1408
}
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
vtx_mark_complete(struct VSLQ *vslq, struct vtx *vtx)
638 1408
{
639
640 1408
        AN(vslq);
641 1408
        AN(vtx->flags & VTX_F_END);
642 1408
        AZ(vtx->flags & VTX_F_COMPLETE);
643
644 1408
        if (vtx->type == VSL_t_unknown)
645 0
                vtx_diag(vtx, "vtx of unknown type marked complete");
646
647 1408
        vtx->flags |= VTX_F_COMPLETE;
648 1408
        VTAILQ_REMOVE(&vslq->incomplete, vtx, list_vtx);
649
650 1548
        while (1) {
651 1548
                AZ(vtx->flags & VTX_F_READY);
652 1548
                if (vtx->flags & VTX_F_COMPLETE &&
653 1548
                    vtx->n_child == vtx->n_childready)
654 1408
                        vtx->flags |= VTX_F_READY;
655 140
                else
656 140
                        return;
657 1408
                if (vtx->parent == NULL) {
658
                        /* Top level vtx ready */
659 1268
                        VTAILQ_INSERT_TAIL(&vslq->ready, vtx, list_vtx);
660 1268
                        return;
661 1268
                }
662 140
                vtx = vtx->parent;
663 140
                vtx->n_childready++;
664 140
                assert(vtx->n_child >= vtx->n_childready);
665 140
        }
666 1408
}
667
668
/* Add a child to a parent, and update child counters */
669
static void
670
vtx_set_parent(struct vtx *parent, struct vtx *child)
671 141
{
672
673 141
        CHECK_OBJ_NOTNULL(parent, VTX_MAGIC);
674 141
        CHECK_OBJ_NOTNULL(child, VTX_MAGIC);
675 141
        assert(parent != child);
676 141
        AZ(parent->flags & VTX_F_COMPLETE);
677 141
        AZ(child->flags & VTX_F_COMPLETE);
678 141
        AZ(child->parent);
679 141
        child->parent = parent;
680 141
        VTAILQ_INSERT_TAIL(&parent->child, child, list_child);
681 141
        parent->n_child++;
682 141
        do
683 169
                parent->n_descend += 1 + child->n_descend;
684 169
        while ((parent = parent->parent) != NULL);
685 141
}
686
687
/* Parse a begin or link record. Returns the number of elements that was
688
   successfully parsed. */
689
static int
690
vtx_parse_link(const char *str, enum VSL_transaction_e *ptype,
691
    uint64_t *pvxid, enum VSL_reason_e *preason, uint64_t *psub)
692 3742
{
693 3742
        char type[16], reason[16];
694 3742
        uintmax_t vxid, sub;
695 3742
        int i;
696 3742
        enum VSL_transaction_e et;
697 3742
        enum VSL_reason_e er;
698
699 3742
        AN(str);
700 3742
        AN(ptype);
701 3742
        AN(pvxid);
702 3742
        AN(preason);
703
704 3742
        i = sscanf(str, "%15s %ju %15s %ju", type, &vxid, reason, &sub);
705 3742
        if (i < 1)
706 0
                return (0);
707
708
        /* transaction type */
709 11304
        for (et = VSL_t_unknown; et < VSL_t__MAX; et++)
710 11305
                if (!vstrcmp(type, vsl_t_names[et]))
711 3743
                        break;
712 3742
        if (et >= VSL_t__MAX)
713 0
                et = VSL_t_unknown;
714 3742
        *ptype = et;
715 3742
        if (i == 1)
716 0
                return (1);
717
718
        /* vxid */
719 3742
        assert((vxid & ~VSL_IDENTMASK) == 0);
720 3742
        *pvxid = vxid;
721 3742
        if (i == 2)
722 0
                return (2);
723
724
        /* transaction reason */
725 14713
        for (er = VSL_r_unknown; er < VSL_r__MAX; er++)
726 14641
                if (!vstrcmp(reason, vsl_r_names[er]))
727 3670
                        break;
728 3742
        if (er >= VSL_r__MAX)
729 73
                er = VSL_r_unknown;
730 3742
        *preason = er;
731 3742
        if (i == 3)
732 3591
                return (3);
733
734
        /* request sub-level */
735 151
        if (psub != NULL)
736 55
                *psub = sub;
737 151
        return (4);
738 3742
}
739
740
/* Parse and process a begin record */
741
static int
742
vtx_scan_begin(struct VSLQ *vslq, struct vtx *vtx, const uint32_t *ptr)
743 1409
{
744 1409
        int i;
745 1409
        enum VSL_transaction_e type;
746 1409
        enum VSL_reason_e reason;
747 1409
        uint64_t p_vxid;
748 1409
        struct vtx *p_vtx;
749
750 1409
        assert(VSL_TAG(ptr) == SLT_Begin);
751
752 1409
        AZ(vtx->flags & VTX_F_READY);
753
754 1409
        i = vtx_parse_link(VSL_CDATA(ptr), &type, &p_vxid, &reason, NULL);
755 1409
        if (i < 3)
756 0
                return (vtx_diag_tag(vtx, ptr, "parse error"));
757 1409
        if (type == VSL_t_unknown)
758 0
                (void)vtx_diag_tag(vtx, ptr, "unknown vxid type");
759
760
        /* Check/set vtx type */
761 1409
        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 1409
        vtx->type = type;
766 1409
        vtx->reason = reason;
767
768 1409
        if (p_vxid == 0)
769
                /* Zero means no parent */
770 434
                return (0);
771 975
        if (p_vxid == vtx->key.vxid)
772 0
                return (vtx_diag_tag(vtx, ptr, "link to self"));
773
774 975
        if (vslq->grouping == VSL_g_vxid)
775 807
                return (0);     /* No links */
776 168
        if (vslq->grouping == VSL_g_request && vtx->type == VSL_t_req &&
777 168
            vtx->reason == VSL_r_rxreq)
778 28
                return (0);     /* No links */
779
780 140
        if (vtx->parent != NULL) {
781 63
                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 0
                } else
786
                        /* Link already exists */
787 63
                        return (0);
788 63
        }
789
790 77
        p_vtx = vtx_lookup(vslq, p_vxid);
791 77
        if (p_vtx == NULL) {
792
                /* Not seen parent yet. Create it. */
793 31
                p_vtx = vtx_add(vslq, p_vxid);
794 31
                AN(p_vtx);
795 46
        } else {
796 46
                CHECK_OBJ_NOTNULL(p_vtx, VTX_MAGIC);
797 46
                if (p_vtx->flags & VTX_F_COMPLETE)
798 0
                        return (vtx_diag_tag(vtx, ptr, "link too late"));
799 46
        }
800
801
        /* Create link */
802 77
        vtx_set_parent(p_vtx, vtx);
803
804 77
        return (0);
805 77
}
806
807
/* Parse and process a link record */
808
static int
809
vtx_scan_link(struct VSLQ *vslq, struct vtx *vtx, const uint32_t *ptr)
810 1004
{
811 1004
        int i;
812 1004
        enum VSL_transaction_e c_type;
813 1004
        enum VSL_reason_e c_reason;
814 1004
        uint64_t c_vxid;
815 1004
        struct vtx *c_vtx;
816
817 1004
        assert(VSL_TAG(ptr) == SLT_Link);
818
819 1004
        AZ(vtx->flags & VTX_F_READY);
820
821 1004
        i = vtx_parse_link(VSL_CDATA(ptr), &c_type, &c_vxid, &c_reason, NULL);
822 1004
        if (i < 3)
823 0
                return (vtx_diag_tag(vtx, ptr, "parse error"));
824 1004
        if (c_type == VSL_t_unknown)
825 0
                (void)vtx_diag_tag(vtx, ptr, "unknown vxid type");
826
827 1004
        if (vslq->grouping == VSL_g_vxid)
828 863
                return (0);     /* No links */
829 141
        if (vslq->grouping == VSL_g_request && vtx->type == VSL_t_sess)
830 0
                return (0);     /* No links */
831
832 141
        if (c_vxid == 0)
833 0
                return (vtx_diag_tag(vtx, ptr, "illegal link vxid"));
834 141
        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 141
        c_vtx = vtx_lookup(vslq, c_vxid);
839 141
        if (c_vtx == NULL) {
840
                /* Child not seen before. Insert it and create link */
841 63
                c_vtx = vtx_add(vslq, c_vxid);
842 63
                AN(c_vtx);
843 63
                AZ(c_vtx->parent);
844 63
                c_vtx->type = c_type;
845 63
                c_vtx->reason = c_reason;
846 63
                vtx_set_parent(vtx, c_vtx);
847 63
                return (0);
848 63
        }
849
850 78
        CHECK_OBJ(c_vtx, VTX_MAGIC);
851 78
        if (c_vtx->parent == vtx)
852
                /* Link already exists */
853 77
                return (0);
854 1
        if (c_vtx->parent != NULL && c_vtx->parent != vtx)
855 0
                return (vtx_diag_tag(vtx, ptr, "duplicate link"));
856 1
        if (c_vtx->flags & VTX_F_COMPLETE)
857 0
                return (vtx_diag_tag(vtx, ptr, "link too late"));
858 1
        if (c_vtx->type != VSL_t_unknown && c_vtx->type != c_type)
859 0
                (void)vtx_diag_tag(vtx, ptr, "type mismatch");
860
861 1
        c_vtx->type = c_type;
862 1
        c_vtx->reason = c_reason;
863 1
        vtx_set_parent(vtx, c_vtx);
864 1
        return (0);
865 1
}
866
867
/* Scan the records of a vtx, performing processing actions on specific
868
   records */
869
static void
870
vtx_scan(struct VSLQ *vslq, struct vtx *vtx)
871 10336
{
872 10336
        const uint32_t *ptr;
873 10336
        enum VSL_tag_e tag;
874
875 52492
        while (!(vtx->flags & VTX_F_COMPLETE) &&
876 52492
            vslc_vtx_next(&vtx->c.cursor) == 1) {
877 42163
                ptr = vtx->c.cursor.rec.ptr;
878 42163
                if (VSL_ID(ptr) != vtx->key.vxid) {
879 0
                        (void)vtx_diag_tag(vtx, ptr, "vxid mismatch");
880 0
                        continue;
881 0
                }
882
883 42163
                tag = VSL_TAG(ptr);
884 42163
                assert(tag != SLT__Batch);
885
886 42163
                switch (tag) {
887 1409
                case SLT_Begin:
888 1409
                        if (vtx->flags & VTX_F_BEGIN)
889 0
                                (void)vtx_diag_tag(vtx, ptr, "duplicate begin");
890 1409
                        else {
891 1409
                                (void)vtx_scan_begin(vslq, vtx, ptr);
892 1409
                                vtx->flags |= VTX_F_BEGIN;
893 1409
                        }
894 1409
                        break;
895
896 1004
                case SLT_Link:
897 1004
                        (void)vtx_scan_link(vslq, vtx, ptr);
898 1004
                        break;
899
900 1408
                case SLT_End:
901 1408
                        AZ(vtx->flags & VTX_F_END);
902 1408
                        vtx->flags |= VTX_F_END;
903 1408
                        vtx_mark_complete(vslq, vtx);
904 1408
                        break;
905
906 38361
                default:
907 38361
                        break;
908 42163
                }
909 42163
        }
910 10336
}
911
912
/* Force a vtx into complete status by synthing the necessary outstanding
913
   records */
914
static void
915
vtx_force(struct VSLQ *vslq, struct vtx *vtx, const char *reason)
916 230
{
917
918 230
        AZ(vtx->flags & (VTX_F_COMPLETE | VTX_F_READY));
919 230
        vtx_scan(vslq, vtx);
920 230
        if (!(vtx->flags & VTX_F_BEGIN))
921 1
                vtx_synth_rec(vtx, SLT_Begin, "%s %u synth",
922 1
                    vsl_t_names[vtx->type], 0);
923 230
        vtx_diag(vtx, reason);
924 230
        if (!(vtx->flags & VTX_F_END))
925 230
                vtx_synth_rec(vtx, SLT_End, "synth");
926 230
        vtx_scan(vslq, vtx);
927 230
        AN(vtx->flags & VTX_F_COMPLETE);
928 230
}
929
930
static int
931
vslq_ratelimit(struct VSLQ *vslq)
932 11
{
933 11
        vtim_mono now;
934 11
        vtim_dur delta;
935
936 11
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
937 11
        CHECK_OBJ_NOTNULL(vslq->vsl, VSL_MAGIC);
938
939 11
        now = VTIM_mono();
940 11
        delta = now - vslq->last_use;
941 11
        vslq->credits += (delta / vslq->vsl->R_opt_p) * vslq->vsl->R_opt_l;
942 11
        vslq->credits = vmin_t(double, vslq->credits, vslq->vsl->R_opt_l);
943 11
        vslq->last_use = now;
944
945 11
        if (vslq->credits < 1.0)
946 0
                return (0);
947
948 11
        vslq->credits -= 1.0;
949 11
        return (1);
950 11
}
951
952
/* Build transaction array, do the query and callback. Returns 0 or the
953
   return value from func */
954
static int
955
vslq_callback(struct VSLQ *vslq, struct vtx *vtx, VSLQ_dispatch_f *func,
956
    void *priv)
957 1041
{
958 1041
        unsigned n = vtx->n_descend + 1;
959 1041
        struct vtx *vtxs[n];
960 1041
        struct VSL_transaction trans[n];
961 1041
        struct VSL_transaction *ptrans[n + 1];
962 1041
        unsigned i, j;
963
964 1041
        AN(vslq);
965 1041
        CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
966 1041
        AN(vtx->flags & VTX_F_READY);
967 1041
        AN(func);
968
969 1041
        if (vslq->grouping == VSL_g_session &&
970 1041
            vtx->type != VSL_t_sess)
971 0
                return (0);
972 1041
        if (vslq->grouping == VSL_g_request &&
973 1041
            vtx->type != VSL_t_req)
974 0
                return (0);
975
976
        /* Build transaction array */
977 1041
        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
978 1041
        vtxs[0] = vtx;
979 1041
        trans[0].level = 1;
980 1041
        trans[0].vxid = vtx->key.vxid;
981 1041
        trans[0].vxid_parent = 0;
982 1041
        trans[0].type = vtx->type;
983 1041
        trans[0].reason = vtx->reason;
984 1041
        trans[0].c = &vtx->c.cursor;
985 1041
        i = 1;
986 1041
        j = 0;
987 2206
        while (j < i) {
988 1165
                VTAILQ_FOREACH(vtx, &vtxs[j]->child, list_child) {
989 124
                        assert(i < n);
990 124
                        AN(vslc_vtx_reset(&vtx->c.cursor) == vsl_end);
991 124
                        vtxs[i] = vtx;
992 124
                        if (vtx->reason == VSL_r_restart)
993
                                /* Restarts stay at the same level as parent */
994 2
                                trans[i].level = trans[j].level;
995 122
                        else
996 122
                                trans[i].level = trans[j].level + 1;
997 124
                        trans[i].vxid = vtx->key.vxid;
998 124
                        trans[i].vxid_parent = trans[j].vxid;
999 124
                        trans[i].type = vtx->type;
1000 124
                        trans[i].reason = vtx->reason;
1001 124
                        trans[i].c = &vtx->c.cursor;
1002 124
                        i++;
1003 124
                }
1004 1165
                j++;
1005 1165
        }
1006 1041
        assert(i == n);
1007
1008
        /* Build pointer array */
1009 2207
        for (i = 0; i < n; i++)
1010 1166
                ptrans[i] = &trans[i];
1011 1041
        ptrans[i] = NULL;
1012
1013
        /* Query test goes here */
1014 1041
        if (vslq->query != NULL && !vslq_runquery(vslq->query, ptrans))
1015 331
                return (0);
1016
1017 710
        if (vslq->vsl->R_opt_l != 0 && !vslq_ratelimit(vslq))
1018 0
                return (0);
1019
1020
        /* Callback */
1021 710
        return ((func)(vslq->vsl, ptrans, priv));
1022 710
}
1023
1024
/* Create a synthetic log record. The record will be inserted at the
1025
   current cursor offset */
1026
static void
1027
vtx_synth_rec(struct vtx *vtx, unsigned tag, const char *fmt, ...)
1028 461
{
1029 461
        struct synth *synth, *it;
1030 461
        va_list ap;
1031 461
        char *buf;
1032 461
        int l, buflen;
1033 461
        uint64_t vxid;
1034
1035 461
        ALLOC_OBJ(synth, SYNTH_MAGIC);
1036 461
        AN(synth);
1037
1038 461
        buf = VSL_DATA(synth->data);
1039 461
        buflen = sizeof(synth->data) - VSL_BYTES(VSL_OVERHEAD);
1040 461
        va_start(ap, fmt);
1041 461
        l = vsnprintf(buf, buflen, fmt, ap);
1042 461
        assert(l >= 0);
1043 461
        va_end(ap);
1044 461
        if (l > buflen - 1)
1045 0
                l = buflen - 1;
1046 461
        buf[l++] = '\0';        /* NUL-terminated */
1047 461
        vxid = vtx->key.vxid;
1048 461
        switch (vtx->type) {
1049 21
        case VSL_t_req:
1050 21
                vxid |= VSL_CLIENTMARKER;
1051 21
                break;
1052 2
        case VSL_t_bereq:
1053 2
                vxid |= VSL_BACKENDMARKER;
1054 2
                break;
1055 438
        default:
1056 438
                break;
1057 461
        }
1058 461
        synth->data[2] = vxid >> 32;
1059 461
        synth->data[1] = vxid;
1060 461
        synth->data[0] = (((tag & VSL_IDMASK) << VSL_IDSHIFT) |
1061 461
            (VSL_VERSION_3 << VSL_VERSHIFT) | l);
1062 461
        synth->offset = vtx->c.offset;
1063
1064 461
        VTAILQ_FOREACH_REVERSE(it, &vtx->synth, synthhead, list) {
1065
                /* Make sure the synth list is sorted on offset */
1066 231
                CHECK_OBJ_NOTNULL(it, SYNTH_MAGIC);
1067 231
                if (synth->offset >= it->offset)
1068 231
                        break;
1069 231
        }
1070 461
        if (it != NULL)
1071 231
                VTAILQ_INSERT_AFTER(&vtx->synth, it, synth, list);
1072 230
        else
1073 230
                VTAILQ_INSERT_HEAD(&vtx->synth, synth, list);
1074
1075
        /* Update cursor */
1076 461
        CHECK_OBJ_ORNULL(vtx->c.synth, SYNTH_MAGIC);
1077 461
        if (vtx->c.synth == NULL || vtx->c.synth->offset > synth->offset)
1078 230
                vtx->c.synth = synth;
1079 461
}
1080
1081
/* Add a diagnostic SLT_VSL synth record to the vtx. */
1082
static int
1083
vtx_diag(struct vtx *vtx, const char *msg)
1084 230
{
1085
1086 230
        vtx_synth_rec(vtx, SLT_VSL, msg);
1087 230
        return (-1);
1088 230
}
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
vtx_diag_tag(struct vtx *vtx, const uint32_t *ptr, const char *reason)
1094 0
{
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 0
}
1100
1101
struct VSLQ *
1102
VSLQ_New(struct VSL_data *vsl, struct VSL_cursor **cp,
1103
    enum VSL_grouping_e grouping, const char *querystring)
1104 484
{
1105 484
        struct vslq_query *query;
1106 484
        struct VSLQ *vslq;
1107
1108 484
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
1109 484
        if (grouping >= VSL_g__MAX) {
1110 0
                (void)vsl_diag(vsl, "Illegal query grouping");
1111 0
                return (NULL);
1112 0
        }
1113 484
        if (querystring != NULL) {
1114 259
                query = vslq_newquery(vsl, grouping, querystring);
1115 259
                if (query == NULL)
1116 32
                        return (NULL);
1117 259
        } else
1118 225
                query = NULL;
1119
1120 452
        ALLOC_OBJ(vslq, VSLQ_MAGIC);
1121 452
        AN(vslq);
1122 452
        vslq->vsl = vsl;
1123 452
        if (cp != NULL) {
1124 358
                vslq->c = *cp;
1125 358
                *cp = NULL;
1126 358
        }
1127 452
        vslq->grouping = grouping;
1128 452
        vslq->query = query;
1129 452
        if (vslq->vsl->R_opt_l != 0) {
1130 2
                vslq->last_use = VTIM_mono();
1131 2
                vslq->credits = 1;
1132 2
        }
1133
1134
        /* Setup normal mode */
1135 452
        VRBT_INIT(&vslq->tree);
1136 452
        VTAILQ_INIT(&vslq->ready);
1137 452
        VTAILQ_INIT(&vslq->incomplete);
1138 452
        VTAILQ_INIT(&vslq->shmrefs);
1139 452
        VTAILQ_INIT(&vslq->cache);
1140
1141
        /* Setup raw mode */
1142 452
        vslq->raw.c.magic = VSLC_RAW_MAGIC;
1143 452
        vslq->raw.c.cursor.priv_tbl = &vslc_raw_tbl;
1144 452
        vslq->raw.c.cursor.priv_data = &vslq->raw.c;
1145 452
        vslq->raw.trans.level = 0;
1146 452
        vslq->raw.trans.type = VSL_t_raw;
1147 452
        vslq->raw.trans.reason = VSL_r_unknown;
1148 452
        vslq->raw.trans.c = &vslq->raw.c.cursor;
1149 452
        vslq->raw.ptrans[0] = &vslq->raw.trans;
1150 452
        vslq->raw.ptrans[1] = NULL;
1151
1152 452
        return (vslq);
1153 452
}
1154
1155
void
1156
VSLQ_Delete(struct VSLQ **pvslq)
1157 446
{
1158 446
        struct VSLQ *vslq;
1159 446
        struct vtx *vtx;
1160
1161 446
        TAKE_OBJ_NOTNULL(vslq, pvslq, VSLQ_MAGIC);
1162
1163 446
        (void)VSLQ_Flush(vslq, NULL, NULL);
1164 446
        AZ(vslq->n_outstanding);
1165
1166 446
        if (vslq->c != NULL) {
1167 446
                VSL_DeleteCursor(vslq->c);
1168 446
                vslq->c = NULL;
1169 446
        }
1170
1171 446
        if (vslq->query != NULL)
1172 227
                vslq_deletequery(&vslq->query);
1173 446
        AZ(vslq->query);
1174
1175 1194
        while (!VTAILQ_EMPTY(&vslq->cache)) {
1176 748
                AN(vslq->n_cache);
1177 748
                vtx = VTAILQ_FIRST(&vslq->cache);
1178 748
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1179 748
                VTAILQ_REMOVE(&vslq->cache, vtx, list_child);
1180 748
                vslq->n_cache--;
1181 748
                FREE_OBJ(vtx);
1182 748
        }
1183
1184 446
        FREE_OBJ(vslq);
1185 446
}
1186
1187
void
1188
VSLQ_SetCursor(struct VSLQ *vslq, struct VSL_cursor **cp)
1189 88
{
1190
1191 88
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1192
1193 88
        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 88
        if (cp != NULL) {
1201 88
                AN(*cp);
1202 88
                vslq->c = *cp;
1203 88
                *cp = NULL;
1204 88
        }
1205 88
}
1206
1207
/* Regard each log line as a single transaction, feed it through the query
1208
   and do the callback */
1209
static int
1210
vslq_raw(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1211 24721
{
1212 24721
        enum vsl_status r = vsl_more;
1213 24721
        int i;
1214
1215 24721
        assert(vslq->grouping == VSL_g_raw);
1216
1217 24721
        assert(vslq->raw.offset <= vslq->raw.len);
1218 28173
        do {
1219 28173
                if (vslq->raw.offset == vslq->raw.len) {
1220 16450
                        r = VSL_Next(vslq->c);
1221 16450
                        if (r != vsl_more)
1222 9496
                                return (r);
1223 6954
                        AN(vslq->c->rec.ptr);
1224 6954
                        vslq->raw.start = vslq->c->rec;
1225 6954
                        if (VSL_TAG(vslq->c->rec.ptr) == SLT__Batch)
1226 3452
                                vslq->raw.len = VSL_END(vslq->c->rec.ptr,
1227 3452
                                    VSL_BATCHLEN(vslq->c->rec.ptr)) -
1228 3452
                                    vslq->c->rec.ptr;
1229 3502
                        else
1230 3502
                                vslq->raw.len = VSL_NEXT(vslq->raw.start.ptr) -
1231 3502
                                    vslq->raw.start.ptr;
1232 6954
                        assert(vslq->raw.len > 0);
1233 6954
                        vslq->raw.offset = 0;
1234 6954
                }
1235
1236 18677
                vslq->raw.c.ptr = vslq->raw.start.ptr + vslq->raw.offset;
1237 18677
                vslq->raw.c.cursor.rec.ptr = NULL;
1238 18677
                vslq->raw.trans.vxid = VSL_ID(vslq->raw.c.ptr);
1239 18677
                vslq->raw.offset += VSL_NEXT(vslq->raw.c.ptr) - vslq->raw.c.ptr;
1240 18677
        } while (VSL_TAG(vslq->raw.c.ptr) == SLT__Batch);
1241
1242 15225
        assert (r == vsl_more);
1243
1244 15225
        if (func == NULL)
1245 0
                return (r);
1246
1247 15225
        if (vslq->query != NULL &&
1248 15225
            !vslq_runquery(vslq->query, vslq->raw.ptrans))
1249 2170
                return (r);
1250
1251 13055
        if (vslq->vsl->R_opt_l != 0 && !vslq_ratelimit(vslq))
1252 0
                return (r);
1253
1254 13055
        i = (func)(vslq->vsl, vslq->raw.ptrans, priv);
1255 13055
        if (i)
1256 104
                return (i);
1257
1258 12951
        return (r);
1259 13055
}
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
vslq_shmref_check(struct VSLQ *vslq)
1266 13152
{
1267 13152
        struct chunk *chunk;
1268 13152
        enum vsl_check i;
1269
1270 13152
        while ((chunk = VTAILQ_FIRST(&vslq->shmrefs)) != NULL) {
1271 10038
                CHECK_OBJ_NOTNULL(chunk, CHUNK_MAGIC);
1272 10038
                assert(chunk->type == chunk_t_shm);
1273 10038
                i = VSL_Check(vslq->c, &chunk->shm.start);
1274 10038
                switch (i) {
1275 10048
                case vsl_check_valid:
1276
                        /* First on list is OK, refs behind it must also
1277
                           be OK */
1278 10048
                        return (vsl_more);
1279 0
                case vsl_check_warn:
1280
                        /* Buffer this chunk */
1281 0
                        chunk_shm_to_buf(vslq, chunk);
1282 0
                        break;
1283 0
                default:
1284
                        /* Too late to buffer */
1285 0
                        return (vsl_e_overrun);
1286 10038
                }
1287 10038
        }
1288
1289 3114
        return (vsl_more);
1290 13152
}
1291
1292
static unsigned
1293
vslq_candidate(struct VSLQ *vslq, const uint32_t *ptr)
1294 1568
{
1295 1568
        enum VSL_transaction_e type;
1296 1568
        enum VSL_reason_e reason;
1297 1568
        struct VSL_data *vsl;
1298 1568
        enum VSL_tag_e tag;
1299 1568
        uint64_t p_vxid, sub;
1300 1568
        int i;
1301
1302 1568
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1303 1568
        AN(ptr);
1304
1305 1568
        assert(vslq->grouping != VSL_g_raw);
1306 1568
        if (vslq->grouping == VSL_g_session)
1307 113
                return (1); /* All are needed */
1308
1309 1455
        vsl = vslq->vsl;
1310 1455
        CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
1311 1455
        if (vslq->grouping == VSL_g_vxid) {
1312 1351
                if (!vsl->c_opt && !vsl->b_opt)
1313 992
                        AZ(vsl->E_opt);
1314 359
                else if (!vsl->b_opt && !VSL_CLIENT(ptr))
1315 83
                        return (0);
1316 276
                else if (!vsl->c_opt && !VSL_BACKEND(ptr))
1317 42
                        return (0);
1318
                /* Need to parse the Begin tag - fallthrough to below */
1319 1351
        }
1320
1321 1330
        tag = VSL_TAG(ptr);
1322 1330
        assert(tag == SLT_Begin);
1323 1330
        i = vtx_parse_link(VSL_CDATA(ptr), &type, &p_vxid, &reason, &sub);
1324 1330
        if (i < 3 || type == VSL_t_unknown)
1325 0
                return (0);
1326
1327 1330
        if (vslq->grouping == VSL_g_request && type == VSL_t_sess)
1328 20
                return (0);
1329
1330 1310
        if (vslq->grouping == VSL_g_vxid && i > 3 && sub > 0 && !vsl->E_opt)
1331 15
                return (0);
1332
1333 1295
        return (1);
1334 1310
}
1335
1336
/* Process next input record */
1337
static enum vsl_status
1338
vslq_next(struct VSLQ *vslq)
1339 23451
{
1340 23451
        const uint32_t *ptr;
1341 23451
        struct VSL_cursor *c;
1342 23451
        enum vsl_status r;
1343 23451
        enum VSL_tag_e tag;
1344 23451
        ssize_t len;
1345 23451
        uint64_t vxid;
1346 23451
        unsigned keep;
1347 23451
        struct vtx *vtx;
1348
1349 23451
        c = vslq->c;
1350 23451
        r = VSL_Next(c);
1351 23451
        if (r != vsl_more)
1352 10254
                return (r);
1353
1354 13197
        assert (r == vsl_more);
1355
1356 13197
        tag = (enum VSL_tag_e)VSL_TAG(c->rec.ptr);
1357 13197
        if (tag == SLT__Batch) {
1358 4402
                vxid = VSL_BATCHID(c->rec.ptr);
1359 4402
                len = VSL_END(c->rec.ptr, VSL_BATCHLEN(c->rec.ptr)) -
1360 4402
                    c->rec.ptr;
1361 4402
                if (len == 0)
1362 0
                        return (r);
1363 4402
                ptr = VSL_NEXT(c->rec.ptr);
1364 4402
                tag = (enum VSL_tag_e)VSL_TAG(ptr);
1365 8795
        } else {
1366 8795
                vxid = VSL_ID(c->rec.ptr);
1367 8795
                len = VSL_NEXT(c->rec.ptr) - c->rec.ptr;
1368 8795
                ptr = c->rec.ptr;
1369 8795
        }
1370 13197
        assert(len > 0);
1371 13197
        if (vxid == 0)
1372
                /* Skip non-transactional records */
1373 2543
                return (r);
1374
1375 10654
        vtx = vtx_lookup(vslq, vxid);
1376 10654
        keep = tag != SLT_Begin || vslq_candidate(vslq, ptr);
1377 10654
        if (vtx == NULL && tag == SLT_Begin && keep) {
1378 1314
                vtx = vtx_add(vslq, vxid);
1379 1314
                AN(vtx);
1380 1314
        }
1381 10654
        if (vtx != NULL) {
1382 9885
                AN(keep);
1383 9885
                r = vtx_append(vslq, vtx, &c->rec, len);
1384 9885
                if (r == vsl_more)
1385 9877
                        vtx_scan(vslq, vtx);
1386 9885
        }
1387
1388 10654
        return (r);
1389 10654
}
1390
1391
/* Test query and report any ready transactions */
1392
static int
1393
vslq_process_ready(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1394 1487
{
1395 1487
        struct vtx *vtx;
1396 1487
        int i = 0;
1397
1398 1487
        AN(vslq);
1399
1400 2500
        while (!VTAILQ_EMPTY(&vslq->ready)) {
1401 1268
                vtx = VTAILQ_FIRST(&vslq->ready);
1402 1268
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1403 1268
                VTAILQ_REMOVE(&vslq->ready, vtx, list_vtx);
1404 1268
                AN(vtx->flags & VTX_F_READY);
1405 1268
                if (func != NULL)
1406 1041
                        i = vslq_callback(vslq, vtx, func, priv);
1407 1268
                vtx_retire(vslq, &vtx);
1408 1268
                AZ(vtx);
1409 1268
                if (i)
1410 255
                        return (i);
1411 1268
        }
1412
1413 1232
        return (0);
1414 1487
}
1415
1416
/* Process the input cursor, calling the callback function on matching
1417
   transaction sets */
1418
int
1419
VSLQ_Dispatch(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1420 48172
{
1421 48172
        enum vsl_status r;
1422 48172
        int i;
1423 48172
        double now;
1424 48172
        struct vtx *vtx;
1425
1426 48172
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1427
1428
        /* Check that we have a cursor */
1429 48172
        if (vslq->c == NULL)
1430 0
                return (vsl_e_abandon);
1431
1432 48172
        if (vslq->grouping == VSL_g_raw)
1433 24721
                return (vslq_raw(vslq, func, priv));
1434
1435
        /* Process next cursor input */
1436 23451
        r = vslq_next(vslq);
1437 23451
        if (r != vsl_more)
1438
                /* At end of log or cursor reports error condition */
1439 10250
                return (r);
1440
1441
        /* Check shmref list and buffer if necessary */
1442 13201
        r = vslq_shmref_check(vslq);
1443 13201
        if (r != vsl_more)
1444
                /* Buffering of shm ref failed */
1445 0
                return (r);
1446
1447 13201
        assert (r == vsl_more);
1448
1449
        /* Check vtx timeout */
1450 13201
        now = VTIM_mono();
1451 13202
        while (!VTAILQ_EMPTY(&vslq->incomplete)) {
1452 9899
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1453 9899
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1454 9899
                if (now - vtx->t_start < vslq->vsl->T_opt)
1455 9898
                        break;
1456 1
                vtx_force(vslq, vtx, "timeout");
1457 1
                AN(vtx->flags & VTX_F_COMPLETE);
1458 1
        }
1459
1460
        /* Check store limit */
1461 13201
        while (vslq->n_outstanding > vslq->vsl->L_opt &&
1462 13201
            !(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 0
        }
1472
1473
        /* Check ready list */
1474 13201
        if (!VTAILQ_EMPTY(&vslq->ready)) {
1475 1041
                i = vslq_process_ready(vslq, func, priv);
1476 1041
                if (i)
1477
                        /* User return code */
1478 255
                        return (i);
1479 1041
        }
1480
1481 12946
        return (vsl_more);
1482 13201
}
1483
1484
/* Flush any incomplete vtx held on to. Do callbacks if func != NULL */
1485
int
1486
VSLQ_Flush(struct VSLQ *vslq, VSLQ_dispatch_f *func, void *priv)
1487 446
{
1488 446
        struct vtx *vtx;
1489
1490 446
        CHECK_OBJ_NOTNULL(vslq, VSLQ_MAGIC);
1491
1492 674
        while (!VTAILQ_EMPTY(&vslq->incomplete)) {
1493 228
                vtx = VTAILQ_FIRST(&vslq->incomplete);
1494 228
                CHECK_OBJ_NOTNULL(vtx, VTX_MAGIC);
1495 228
                AZ(vtx->flags & VTX_F_COMPLETE);
1496 228
                vtx_force(vslq, vtx, "flush");
1497 228
        }
1498
1499 446
        return (vslq_process_ready(vslq, func, priv));
1500 446
}