varnish-cache/lib/libvarnishapi/vsc.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
7
 *
8
 * SPDX-License-Identifier: BSD-2-Clause
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 * 1. Redistributions of source code must retain the above copyright
14
 *    notice, this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in the
17
 *    documentation and/or other materials provided with the distribution.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
32
#include "config.h"
33
34
#include <sys/stat.h>
35
36
#include <fnmatch.h>
37
#include <stdint.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <unistd.h>
42
43
#include "vdef.h"
44
#include "vas.h"
45
#include "miniobj.h"
46
#include "vqueue.h"
47
#include "vjsn.h"
48
#include "vsb.h"
49
#include "vsc_priv.h"
50
51
#include "vapi/vsc.h"
52
#include "vapi/vsm.h"
53
54
struct vsc_sf_mode {
55
        const char              *name;
56
        unsigned                include;
57
        unsigned                fail;
58
        unsigned                append;
59
};
60
61
static const struct vsc_sf_mode VSC_SF_INCLUDE[1] = {{"include", 1, 1, 1}};
62
static const struct vsc_sf_mode VSC_SF_EXCLUDE[1] = {{"exclude", 0, 0, 1}};
63
static const struct vsc_sf_mode VSC_SF_REQUIRE[1] = {{"require", 1, 0, 0}};
64
65
struct vsc_sf {
66
        unsigned                        magic;
67
#define VSC_SF_MAGIC                    0x558478dd
68
        VTAILQ_ENTRY(vsc_sf)            list;
69
        char                            *pattern;
70
        const struct vsc_sf_mode        *mode;
71
};
72
73
VTAILQ_HEAD(vsc_sf_head, vsc_sf);
74
75
struct vsc_pt {
76
        struct VSC_point        point;
77
        char                    *name;
78
};
79
80
enum vsc_seg_type {
81
        VSC_SEG_COUNTERS = 1,
82
        VSC_SEG_DOCS,
83
};
84
85
struct vsc_seg {
86
        unsigned                magic;
87
#define VSC_SEG_MAGIC           0x801177d4
88
        enum vsc_seg_type       type;
89
        VTAILQ_ENTRY(vsc_seg)   list;
90
        VTAILQ_ENTRY(vsc_seg)   doc_list;
91
        struct vsm_fantom       fantom[1];
92
        const struct vsc_head   *head;
93
        const char              *body;
94
95
        struct vjsn             *vj;
96
97
        unsigned                npoints;
98
        struct vsc_pt           *points;
99
100
        int                     mapped;
101
        int                     exposed;
102
};
103
VTAILQ_HEAD(vsc_seg_head, vsc_seg);
104
105
struct vsc {
106
        unsigned                magic;
107
#define VSC_MAGIC               0x3373554a
108
109
        unsigned                raw;
110
        struct vsc_sf_head      sf_list;
111
        struct vsc_seg_head     segs;
112
        struct vsc_seg_head     docs;
113
114
        VSC_new_f               *fnew;
115
        VSC_destroy_f           *fdestroy;
116
        void                    *priv;
117
};
118
119
/*--------------------------------------------------------------------
120
 * Build the static level, type and point descriptions
121
 */
122
123
enum vsc_levels {
124
#define VSC_LEVEL_F(v,l,e,d) v,
125
#include "tbl/vsc_levels.h"
126
};
127
128
static const struct VSC_level_desc levels[] = {
129
#define VSC_LEVEL_F(v,l,e,d) [v] = {#v, l, e, d},
130
#include "tbl/vsc_levels.h"
131
};
132
133
static const ssize_t nlevels = sizeof(levels)/sizeof(*levels);
134
135
/*--------------------------------------------------------------------*/
136
137
struct vsc *
138 38400
VSC_New(void)
139
{
140
        struct vsc *vsc;
141
142 38400
        ALLOC_OBJ(vsc, VSC_MAGIC);
143 38400
        if (vsc == NULL)
144 0
                return (vsc);
145 38400
        VTAILQ_INIT(&vsc->sf_list);
146 38400
        VTAILQ_INIT(&vsc->segs);
147 38400
        VTAILQ_INIT(&vsc->docs);
148 38400
        return (vsc);
149 38400
}
150
151
/*--------------------------------------------------------------------*/
152
153
static int
154 1480
vsc_sf_arg(struct vsc *vsc, const char *glob, const struct vsc_sf_mode *mode)
155
{
156
        struct vsc_sf *sf;
157
158 1480
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
159 1480
        AN(glob);
160 1480
        AN(mode);
161
162 1480
        ALLOC_OBJ(sf, VSC_SF_MAGIC);
163 1480
        AN(sf);
164 1480
        REPLACE(sf->pattern, glob);
165 1480
        sf->mode = mode;
166 1480
        AN(mode->name);
167 1480
        if (mode->append)
168 1160
                VTAILQ_INSERT_TAIL(&vsc->sf_list, sf, list);
169
        else
170 320
                VTAILQ_INSERT_HEAD(&vsc->sf_list, sf, list);
171 1480
        return (1);
172
}
173
174
static int
175 840
vsc_f_arg(struct vsc *vsc, const char *opt)
176
{
177
178 840
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
179 840
        AN(opt);
180
181 840
        if (opt[0] == '^')
182 200
                return (vsc_sf_arg(vsc, opt + 1, VSC_SF_EXCLUDE));
183 640
        return (vsc_sf_arg(vsc, opt, VSC_SF_INCLUDE));
184 840
}
185
186
/*--------------------------------------------------------------------*/
187
188
int
189 1480
VSC_Arg(struct vsc *vsc, char arg, const char *opt)
190
{
191
192 1480
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
193
194 1480
        switch (arg) {
195 280
        case 'I': return (vsc_sf_arg(vsc, opt, VSC_SF_INCLUDE));
196 40
        case 'X': return (vsc_sf_arg(vsc, opt, VSC_SF_EXCLUDE));
197 320
        case 'R': return (vsc_sf_arg(vsc, opt, VSC_SF_REQUIRE));
198 840
        case 'f': return (vsc_f_arg(vsc, opt));
199 0
        case 'r': vsc->raw = !vsc->raw; return (1);
200 0
        default: return (0);
201
        }
202 1480
}
203
204
unsigned
205 240
VSC_IsRaw(const struct vsc *vsc)
206
{
207
208 240
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
209 240
        return (vsc->raw);
210
}
211
212
/*--------------------------------------------------------------------
213
 */
214
215
static int
216 2094623
vsc_filter(const struct vsc *vsc, const char *nm)
217
{
218
        struct vsc_sf *sf;
219 2094623
        unsigned res = 0;
220
221 2094623
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
222 2530663
        VTAILQ_FOREACH(sf, &vsc->sf_list, list) {
223 444160
                if (!fnmatch(sf->pattern, nm, 0))
224 8120
                        return (!sf->mode->include);
225 436040
                res |= sf->mode->fail;
226 436040
        }
227 2086503
        return (res);
228 2094623
}
229
230
/*--------------------------------------------------------------------
231
 */
232
233
static void
234 1786303
vsc_clean_point(struct vsc_pt *point)
235
{
236 1786303
        REPLACE(point->name, NULL);
237 1786303
}
238
239
static void
240 2094623
vsc_fill_point(const struct vsc *vsc, const struct vsc_seg *seg,
241
    const struct vjsn_val *vv, struct vsb *vsb, struct vsc_pt *point)
242
{
243
        struct vjsn_val *vt;
244
245 2094623
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
246 2094623
        memset(point, 0, sizeof *point);
247
248 2094623
        vt = vjsn_child(vv, "name");
249 2094623
        AN(vt);
250 2094623
        assert(vjsn_is_string(vt));
251
252 2094623
        VSB_clear(vsb);
253 2094623
        VSB_printf(vsb, "%s.%s", seg->fantom->ident, vt->value);
254 2094623
        AZ(VSB_finish(vsb));
255
256 2094623
        if (vsc_filter(vsc, VSB_data(vsb)))
257 153040
                return;
258
259 1941583
        point->name = strdup(VSB_data(vsb));
260 1941583
        AN(point->name);
261 1941583
        point->point.name = point->name;
262
263
#define DOF(n, k)                               \
264
        vt = vjsn_child(vv, k);                 \
265
        AN(vt);                                 \
266
        assert(vjsn_is_string(vt));             \
267
        point->point.n = vt->value;
268
269 1941583
        DOF(ctype, "ctype");
270 1941583
        DOF(sdesc, "oneliner");
271 1941583
        DOF(ldesc, "docs");
272
#undef DOF
273 1941583
        vt = vjsn_child(vv, "type");
274 1941583
        AN(vt);
275 1941583
        assert(vjsn_is_string(vt));
276
277 1941583
        if (!strcmp(vt->value, "counter")) {
278 1707285
                point->point.semantics = 'c';
279 1941583
        } else if (!strcmp(vt->value, "gauge")) {
280 228830
                point->point.semantics = 'g';
281 234298
        } else if (!strcmp(vt->value, "bitmap")) {
282 5468
                point->point.semantics = 'b';
283 5468
        } else {
284 0
                point->point.semantics = '?';
285
        }
286
287 1941583
        vt = vjsn_child(vv, "format");
288 1941583
        AN(vt);
289 1941583
        assert(vjsn_is_string(vt));
290
291 1941583
        if (!strcmp(vt->value, "integer")) {
292 1752603
                point->point.format = 'i';
293 1941583
        } else if (!strcmp(vt->value, "bytes")) {
294 165472
                point->point.format = 'B';
295 188980
        } else if (!strcmp(vt->value, "bitmap")) {
296 5468
                point->point.format = 'b';
297 23508
        } else if (!strcmp(vt->value, "duration")) {
298 18040
                point->point.format = 'd';
299 18040
        } else {
300 0
                point->point.format = '?';
301
        }
302
303 1941583
        vt = vjsn_child(vv, "level");
304 1941583
        AN(vt);
305 1941583
        assert(vjsn_is_string(vt));
306
307 1941583
        if (!strcmp(vt->value, "info"))  {
308 927174
                point->point.level = &levels[info];
309 1941583
        } else if (!strcmp(vt->value, "diag")) {
310 600636
                point->point.level = &levels[diag];
311 1014409
        } else if (!strcmp(vt->value, "debug")) {
312 413773
                point->point.level = &levels[debug];
313 413773
        } else {
314 0
                WRONG("Illegal level");
315
        }
316
317 1941583
        vt = vjsn_child(vv, "index");
318 1941583
        AN(vt);
319
320 1941583
        point->point.ptr = (volatile const void*)(seg->body + atoi(vt->value));
321 1941583
        point->point.raw = vsc->raw;
322 2094623
}
323
324
static struct vsc_seg *
325 380441
vsc_new_seg(const struct vsm_fantom *fp, enum vsc_seg_type type)
326
{
327
        struct vsc_seg *sp;
328
329 380441
        ALLOC_OBJ(sp, VSC_SEG_MAGIC);
330 380441
        AN(sp);
331 380441
        *sp->fantom = *fp;
332 380441
        sp->type = type;
333
334 380441
        return (sp);
335
}
336
337
static void
338 352161
vsc_unmap_seg(const struct vsc *vsc, struct vsm *vsm, struct vsc_seg *sp)
339
{
340
        unsigned u;
341
        struct vsc_pt *pp;
342
343 352161
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
344 352161
        AN(vsm);
345 352161
        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
346
347 352161
        AZ(sp->exposed);
348 352161
        if (!sp->mapped)
349 252507
                return;
350
351 99654
        if (sp->type == VSC_SEG_COUNTERS) {
352 75071
                pp = sp->points;
353 1861374
                for (u = 0; u < sp->npoints; u++, pp++)
354 1786303
                        vsc_clean_point(pp);
355 75071
                free(sp->points);
356 75071
                sp->points = NULL;
357 75071
                sp->npoints = 0;
358 75071
                AZ(sp->vj);
359 99654
        } else if (sp->type == VSC_SEG_DOCS) {
360 24583
                if (sp->vj != NULL)
361 24583
                        vjsn_delete(&sp->vj);
362 24583
                AZ(sp->vj);
363 24583
                AZ(sp->points);
364 24583
        } else {
365 0
                WRONG("Invalid segment type");
366
        }
367
368 99654
        AZ(VSM_Unmap(vsm, sp->fantom));
369 99654
        sp->head = NULL;
370 99654
        sp->body = NULL;
371 99654
        sp->mapped = 0;
372 352161
}
373
374
static int
375 799903
vsc_map_seg(const struct vsc *vsc, struct vsm *vsm, struct vsc_seg *sp)
376
{
377
        const struct vsc_head *head;
378
        struct vsc_seg *spd;
379
        const char *e;
380
        struct vjsn_val *vv, *vve;
381
        struct vsb *vsb;
382
        struct vsc_pt *pp;
383
        int retry;
384
385 799903
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
386 799903
        AN(vsm);
387 799903
        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
388
389 799903
        if (sp->mapped)
390 671968
                return (0);
391
392 127935
        AZ(sp->exposed);
393
394 127935
        if (VSM_Map(vsm, sp->fantom))
395 1
                return (-1);
396 127934
        head = sp->fantom->b;
397
398
        /* It isn't ready yet. Sleep and try again. If it still
399
         * isn't ready, fail the mapping. The transitions inside
400
         * varnishd that we are waiting for are just some memcpy()
401
         * operations, so there is no reason to allow a long retry
402
         * time. */
403 127934
        for (retry = 10; retry > 0 && head->ready == 0; retry--)
404 0
                usleep(10000);
405
406 127934
        if (head->ready == 0) {
407 0
                AZ(VSM_Unmap(vsm, sp->fantom));
408 0
                return (-1);
409
        }
410
411 127934
        sp->head = head;
412 127934
        sp->body = (char*)sp->fantom->b + sp->head->body_offset;
413 127934
        sp->mapped = 1;
414
415 127934
        if (sp->type == VSC_SEG_DOCS) {
416
                /* Parse the DOCS json */
417 28423
                sp->vj = vjsn_parse(sp->body, &e);
418 28423
                XXXAZ(e);
419 28423
                AN(sp->vj);
420 28423
                return (0);
421
        }
422
423 99511
        assert(sp->type == VSC_SEG_COUNTERS);
424
425
        /* Find the corresponding DOCS seg. We are not able to
426
         * read and match on the doc_id until the DOCS section is
427
         * mapped. Iterate over all the DOCS sections, attempt to
428
         * map if needed, and then check the doc_id. */
429 308535
        VTAILQ_FOREACH(spd, &vsc->docs, doc_list) {
430 308535
                CHECK_OBJ_NOTNULL(spd, VSC_SEG_MAGIC);
431 308535
                assert(spd->type == VSC_SEG_DOCS);
432 308535
                if (!spd->mapped && vsc_map_seg(vsc, vsm, spd))
433 0
                        continue; /* Failed to map it */
434 308535
                AN(spd->mapped);
435 308535
                if (spd->head->doc_id == sp->head->doc_id)
436 99511
                        break; /* We have a match */
437 209024
        }
438 99511
        if (spd == NULL) {
439
                /* Could not find the right DOCS seg. Leave this
440
                 * seg as unmapped. */
441 0
                vsc_unmap_seg(vsc, vsm, sp);
442 0
                return (-1);
443
        }
444
445
        /* Create the VSC points list */
446 99511
        vve = vjsn_child(spd->vj->value, "elements");
447 99511
        AN(vve);
448 99511
        sp->npoints = strtoul(vve->value, NULL, 0);
449 99511
        sp->points = calloc(sp->npoints, sizeof *sp->points);
450 99511
        AN(sp->points);
451 99511
        vsb = VSB_new_auto();
452 99511
        AN(vsb);
453 99511
        vve = vjsn_child(spd->vj->value, "elem");
454 99511
        AN(vve);
455 99511
        pp = sp->points;
456 2194134
        VTAILQ_FOREACH(vv, &vve->children, list) {
457 2094623
                vsc_fill_point(vsc, sp, vv, vsb, pp);
458 2094623
                pp++;
459 2094623
        }
460 99511
        VSB_destroy(&vsb);
461 99511
        return (0);
462 799903
}
463
464
/*--------------------------------------------------------------------
465
 */
466
467
static void
468 1123640
vsc_expose(const struct vsc *vsc, struct vsc_seg *sp, int del)
469
{
470
        struct vsc_pt *pp;
471
        unsigned u;
472
        int expose;
473
474 1123640
        if (!sp->mapped) {
475 252507
                AZ(sp->exposed);
476 252507
                return;
477
        }
478
479 878733
        if (vsc->fnew != NULL && !sp->exposed &&
480 8960
            !del && sp->head->ready == 1)
481 7600
                expose = 1;
482 916901
        else if (vsc->fdestroy != NULL && sp->exposed &&
483 60968
            (del || sp->head->ready == 2))
484 7600
                expose = 0;
485
        else
486 855933
                return;
487
488 15200
        pp = sp->points;
489 183840
        for (u = 0; u < sp->npoints; u++, pp++) {
490 168640
                if (pp->name == NULL)
491 26640
                        continue;
492 142000
                if (expose)
493 71000
                        pp->point.priv = vsc->fnew(vsc->priv, &pp->point);
494
                else
495 71000
                        vsc->fdestroy(vsc->priv, &pp->point);
496 142000
        }
497 15200
        sp->exposed = expose;
498 1123640
}
499
500
/*--------------------------------------------------------------------
501
 */
502
503
static void
504 85368
vsc_del_segs(struct vsc *vsc, struct vsm *vsm, struct vsc_seg_head *head)
505
{
506
        struct vsc_seg *sp, *sp2;
507
508 437529
        VTAILQ_FOREACH_SAFE(sp, head, list, sp2) {
509 352161
                CHECK_OBJ(sp, VSC_SEG_MAGIC);
510 352161
                VTAILQ_REMOVE(head, sp, list);
511 352161
                if (sp->type == VSC_SEG_DOCS)
512 54080
                        VTAILQ_REMOVE(&vsc->docs, sp, doc_list);
513 352161
                vsc_expose(vsc, sp, 1);
514 352161
                vsc_unmap_seg(vsc, vsm, sp);
515 352161
                FREE_OBJ(sp);
516 352161
        }
517 85368
}
518
519
/*--------------------------------------------------------------------
520
 */
521
522
static int
523 702471
vsc_iter_seg(const struct vsc *vsc, const struct vsc_seg *sp,
524
    VSC_iter_f *fiter, void *priv)
525
{
526
        unsigned u;
527 702471
        int i = 0;
528
        struct vsc_pt *pp;
529
530 702471
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
531 702471
        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
532 702471
        AN(fiter);
533 702471
        pp = sp->points;
534 10185469
        for (u = 0; u < sp->npoints && i == 0; u++, pp++) {
535 9482998
                if (pp->name != NULL)
536 9403278
                        i = fiter(priv, &pp->point);
537 9482998
        }
538 702471
        return (i);
539
}
540
541
int
542 47928
VSC_Iter(struct vsc *vsc, struct vsm *vsm, VSC_iter_f *fiter, void *priv)
543
{
544
        enum vsc_seg_type type;
545
        struct vsm_fantom ifantom;
546
        struct vsc_seg *sp, *sp2;
547
        struct vsc_seg_head removed;
548 47928
        int i = 0;
549
550 47928
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
551 47928
        AN(vsm);
552
553
        /* First walk the VSM segment list and consolidate with the shadow
554
         * VSC seg list. We avoid calling any of the callback functions
555
         * while iterating the VSMs. This removes any headaches wrt to
556
         * callbacks calling VSM_Status(). */
557 47928
        VTAILQ_INIT(&removed);
558 47928
        sp = VTAILQ_FIRST(&vsc->segs);
559 2176273
        VSM_FOREACH(&ifantom, vsm) {
560 2128345
                AN(ifantom.category);
561 2128345
                if (!strcmp(ifantom.category, VSC_CLASS))
562 1556058
                        type = VSC_SEG_COUNTERS;
563 572287
                else if (!strcmp(ifantom.category, VSC_DOC_CLASS))
564 286017
                        type = VSC_SEG_DOCS;
565
                else {
566
                        /* Not one of the categories we care about */
567 286270
                        continue;
568
                }
569
570 1862937
                while (sp != NULL) {
571 1482496
                        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
572 1482496
                        if (VSM_StillValid(vsm, sp->fantom) == VSM_valid &&
573 1461634
                            !strcmp(ifantom.ident, sp->fantom->ident)) {
574
                                /* sp matches the expected value */
575 1461634
                                break;
576
                        }
577
578
                        /* sp is no longer in the VSM list. Remove it from
579
                         * our list. */
580 20862
                        sp2 = sp;
581 20862
                        sp = VTAILQ_NEXT(sp, list);
582 20862
                        VTAILQ_REMOVE(&vsc->segs, sp2, list);
583 20862
                        VTAILQ_INSERT_TAIL(&removed, sp2, list);
584
                }
585
586 1842075
                if (sp == NULL) {
587
                        /* New entries are always appended last in the VSM
588
                         * list. Since we have iterated past all the
589
                         * entries in our shadow list, the VSM entry is a
590
                         * new entry we have not seen before. */
591 380441
                        sp = vsc_new_seg(&ifantom, type);
592 380441
                        AN(sp);
593 380441
                        VTAILQ_INSERT_TAIL(&vsc->segs, sp, list);
594 380441
                        if (type == VSC_SEG_DOCS)
595 57920
                                VTAILQ_INSERT_TAIL(&vsc->docs, sp, doc_list);
596 380441
                }
597
598 1842075
                assert(sp->type == type);
599 1842075
                sp = VTAILQ_NEXT(sp, list);
600
        }
601 47928
        while (sp != NULL) {
602
                /* Clean up the tail end of the shadow list. */
603 0
                CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
604 0
                sp2 = sp;
605 0
                sp = VTAILQ_NEXT(sp, list);
606
607 0
                VTAILQ_REMOVE(&vsc->segs, sp2, list);
608 0
                VTAILQ_INSERT_TAIL(&removed, sp2, list);
609
        }
610
611 47928
        vsc_del_segs(vsc, vsm, &removed);
612
613
        /* Iterate our shadow list, reporting on each pointer value */
614 962211
        VTAILQ_FOREACH(sp, &vsc->segs, list) {
615 949257
                CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
616
617 949257
                if (sp->type != VSC_SEG_COUNTERS)
618 177777
                        continue;
619
620
                /* Attempt to map the VSM. This is a noop if it was
621
                 * already mapped. If we fail we skip this seg on this
622
                 * call to VSC_Iter(), but will attempt again the next
623
                 * time VSC_Iter() is called. */
624 771480
                if (vsc_map_seg(vsc, vsm, sp))
625 1
                        continue;
626
627
                /* Expose the counters if necessary */
628 771479
                vsc_expose(vsc, sp, 0);
629
630 771479
                if (fiter != NULL && sp->head->ready == 1)
631 702471
                        i = vsc_iter_seg(vsc, sp, fiter, priv);
632 771479
                if (i)
633 34974
                        break;
634 736505
        }
635
636 47928
        return (i);
637
}
638
639
/*--------------------------------------------------------------------
640
 */
641
642
void
643 240
VSC_State(struct vsc *vsc, VSC_new_f *fn, VSC_destroy_f *fd, void *priv)
644
{
645
        struct vsc_seg *sp;
646
647 240
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
648 240
        assert((fn == NULL && fd == NULL) || (fn != NULL && fd != NULL));
649 240
        if (fd == NULL) {
650 0
                VTAILQ_FOREACH(sp, &vsc->segs, list)
651 0
                        vsc_expose(vsc, sp, 1);
652 0
        }
653 240
        vsc->fnew = fn;
654 240
        vsc->fdestroy = fd;
655 240
        vsc->priv = priv;
656 240
}
657
658
/*--------------------------------------------------------------------
659
 */
660
661
const struct VSC_level_desc *
662 400
VSC_ChangeLevel(const struct VSC_level_desc *old, int chg)
663
{
664
        int i;
665
666 400
        if (old == NULL)
667 240
                old = &levels[0];
668 440
        for (i = 0; i < nlevels; i++)
669 440
                if (old == &levels[i])
670 400
                        break;
671 400
        if (i == nlevels)
672 0
                i = 0;
673
674 400
        i += chg;
675 400
        if (i >= nlevels)
676 0
                i = nlevels - 1;
677 400
        if (i < 0)
678 0
                i = 0;
679 400
        return (&levels[i]);
680
}
681
682
/*--------------------------------------------------------------------*/
683
684
void
685 37440
VSC_Destroy(struct vsc **vscp, struct vsm *vsm)
686
{
687
        struct vsc *vsc;
688
        struct vsc_sf *sf, *sf2;
689
690 37440
        TAKE_OBJ_NOTNULL(vsc, vscp, VSC_MAGIC);
691
692 38240
        VTAILQ_FOREACH_SAFE(sf, &vsc->sf_list, list, sf2) {
693 800
                CHECK_OBJ_NOTNULL(sf, VSC_SF_MAGIC);
694 800
                VTAILQ_REMOVE(&vsc->sf_list, sf, list);
695 800
                free(sf->pattern);
696 800
                FREE_OBJ(sf);
697 800
        }
698
699 37440
        vsc_del_segs(vsc, vsm, &vsc->segs);
700 37440
        assert(VTAILQ_EMPTY(&vsc->docs));
701 37440
        FREE_OBJ(vsc);
702 37440
}