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 2898
VSC_New(void)
139
{
140
        struct vsc *vsc;
141
142 2898
        ALLOC_OBJ(vsc, VSC_MAGIC);
143 2898
        if (vsc == NULL)
144 0
                return (vsc);
145 2898
        VTAILQ_INIT(&vsc->sf_list);
146 2898
        VTAILQ_INIT(&vsc->segs);
147 2898
        VTAILQ_INIT(&vsc->docs);
148 2898
        return (vsc);
149 2898
}
150
151
/*--------------------------------------------------------------------*/
152
153
static int
154 111
vsc_sf_arg(struct vsc *vsc, const char *glob, const struct vsc_sf_mode *mode)
155
{
156
        struct vsc_sf *sf;
157
158 111
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
159 111
        AN(glob);
160 111
        AN(mode);
161
162 111
        ALLOC_OBJ(sf, VSC_SF_MAGIC);
163 111
        AN(sf);
164 111
        REPLACE(sf->pattern, glob);
165 111
        sf->mode = mode;
166 111
        AN(mode->name);
167 111
        if (mode->append)
168 87
                VTAILQ_INSERT_TAIL(&vsc->sf_list, sf, list);
169
        else
170 24
                VTAILQ_INSERT_HEAD(&vsc->sf_list, sf, list);
171 111
        return (1);
172
}
173
174
static int
175 63
vsc_f_arg(struct vsc *vsc, const char *opt)
176
{
177
178 63
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
179 63
        AN(opt);
180
181 63
        if (opt[0] == '^')
182 15
                return (vsc_sf_arg(vsc, opt + 1, VSC_SF_EXCLUDE));
183 48
        return (vsc_sf_arg(vsc, opt, VSC_SF_INCLUDE));
184 63
}
185
186
/*--------------------------------------------------------------------*/
187
188
int
189 111
VSC_Arg(struct vsc *vsc, char arg, const char *opt)
190
{
191
192 111
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
193
194 111
        switch (arg) {
195 21
        case 'I': return (vsc_sf_arg(vsc, opt, VSC_SF_INCLUDE));
196 3
        case 'X': return (vsc_sf_arg(vsc, opt, VSC_SF_EXCLUDE));
197 24
        case 'R': return (vsc_sf_arg(vsc, opt, VSC_SF_REQUIRE));
198 63
        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 111
}
203
204
unsigned
205 18
VSC_IsRaw(const struct vsc *vsc)
206
{
207
208 18
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
209 18
        return (vsc->raw);
210
}
211
212
/*--------------------------------------------------------------------
213
 */
214
215
static int
216 161248
vsc_filter(const struct vsc *vsc, const char *nm)
217
{
218
        struct vsc_sf *sf;
219 161248
        unsigned res = 0;
220
221 161248
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
222 194599
        VTAILQ_FOREACH(sf, &vsc->sf_list, list) {
223 33960
                if (!fnmatch(sf->pattern, nm, 0))
224 609
                        return (!sf->mode->include);
225 33351
                res |= sf->mode->fail;
226 33351
        }
227 160639
        return (res);
228 161248
}
229
230
/*--------------------------------------------------------------------
231
 */
232
233
static void
234 137740
vsc_clean_point(struct vsc_pt *point)
235
{
236 137740
        REPLACE(point->name, NULL);
237 137740
}
238
239
static void
240 161248
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 161248
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
246 161248
        memset(point, 0, sizeof *point);
247
248 161248
        vt = vjsn_child(vv, "name");
249 161248
        AN(vt);
250 161248
        assert(vjsn_is_string(vt));
251
252 161248
        VSB_clear(vsb);
253 161248
        VSB_printf(vsb, "%s.%s", seg->fantom->ident, vt->value);
254 161248
        AZ(VSB_finish(vsb));
255
256 161248
        if (vsc_filter(vsc, VSB_data(vsb)))
257 11622
                return;
258
259 149626
        point->name = strdup(VSB_data(vsb));
260 149626
        AN(point->name);
261 149626
        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 149626
        DOF(ctype, "ctype");
270 149626
        DOF(sdesc, "oneliner");
271 149626
        DOF(ldesc, "docs");
272
#undef DOF
273 149626
        vt = vjsn_child(vv, "type");
274 149626
        AN(vt);
275 149626
        assert(vjsn_is_string(vt));
276
277 149626
        if (!strcmp(vt->value, "counter")) {
278 131528
                point->point.semantics = 'c';
279 149626
        } else if (!strcmp(vt->value, "gauge")) {
280 17684
                point->point.semantics = 'g';
281 18098
        } else if (!strcmp(vt->value, "bitmap")) {
282 414
                point->point.semantics = 'b';
283 414
        } else {
284 0
                point->point.semantics = '?';
285
        }
286
287 149626
        vt = vjsn_child(vv, "format");
288 149626
        AN(vt);
289 149626
        assert(vjsn_is_string(vt));
290
291 149626
        if (!strcmp(vt->value, "integer")) {
292 135255
                point->point.format = 'i';
293 149626
        } else if (!strcmp(vt->value, "bytes")) {
294 12580
                point->point.format = 'B';
295 14371
        } else if (!strcmp(vt->value, "bitmap")) {
296 414
                point->point.format = 'b';
297 1791
        } else if (!strcmp(vt->value, "duration")) {
298 1377
                point->point.format = 'd';
299 1377
        } else {
300 0
                point->point.format = '?';
301
        }
302
303 149626
        vt = vjsn_child(vv, "level");
304 149626
        AN(vt);
305 149626
        assert(vjsn_is_string(vt));
306
307 149626
        if (!strcmp(vt->value, "info"))  {
308 70968
                point->point.level = &levels[info];
309 149626
        } else if (!strcmp(vt->value, "diag")) {
310 45984
                point->point.level = &levels[diag];
311 78658
        } else if (!strcmp(vt->value, "debug")) {
312 32674
                point->point.level = &levels[debug];
313 32674
        } else {
314 0
                WRONG("Illegal level");
315
        }
316
317 149626
        vt = vjsn_child(vv, "index");
318 149626
        AN(vt);
319
320 149626
        point->point.ptr = (volatile const void*)(seg->body + atoi(vt->value));
321 149626
        point->point.raw = vsc->raw;
322 161248
}
323
324
static struct vsc_seg *
325 31298
vsc_new_seg(const struct vsm_fantom *fp, enum vsc_seg_type type)
326
{
327
        struct vsc_seg *sp;
328
329 31298
        ALLOC_OBJ(sp, VSC_SEG_MAGIC);
330 31298
        AN(sp);
331 31298
        *sp->fantom = *fp;
332 31298
        sp->type = type;
333
334 31298
        return (sp);
335
}
336
337
static void
338 29033
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 29033
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
344 29033
        AN(vsm);
345 29033
        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
346
347 29033
        AZ(sp->exposed);
348 29033
        if (!sp->mapped)
349 21024
                return;
350
351 8009
        if (sp->type == VSC_SEG_COUNTERS) {
352 5972
                pp = sp->points;
353 143712
                for (u = 0; u < sp->npoints; u++, pp++)
354 137740
                        vsc_clean_point(pp);
355 5972
                free(sp->points);
356 5972
                sp->points = NULL;
357 5972
                sp->npoints = 0;
358 5972
                AZ(sp->vj);
359 8009
        } else if (sp->type == VSC_SEG_DOCS) {
360 2037
                if (sp->vj != NULL)
361 2037
                        vjsn_delete(&sp->vj);
362 2037
                AZ(sp->vj);
363 2037
                AZ(sp->points);
364 2037
        } else {
365 0
                WRONG("Invalid segment type");
366
        }
367
368 8009
        AZ(VSM_Unmap(vsm, sp->fantom));
369 8009
        sp->head = NULL;
370 8009
        sp->body = NULL;
371 8009
        sp->mapped = 0;
372 29033
}
373
374
static int
375 68414
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 68414
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
386 68414
        AN(vsm);
387 68414
        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
388
389 68414
        if (sp->mapped)
390 58140
                return (0);
391
392 10274
        AZ(sp->exposed);
393
394 10274
        if (VSM_Map(vsm, sp->fantom))
395 0
                return (-1);
396 10274
        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 10274
        for (retry = 10; retry > 0 && head->ready == 0; retry--)
404 0
                usleep(10000);
405
406 10274
        if (head->ready == 0) {
407 0
                AZ(VSM_Unmap(vsm, sp->fantom));
408 0
                return (-1);
409
        }
410
411 10274
        sp->head = head;
412 10274
        sp->body = (char*)sp->fantom->b + sp->head->body_offset;
413 10274
        sp->mapped = 1;
414
415 10274
        if (sp->type == VSC_SEG_DOCS) {
416
                /* Parse the DOCS json */
417 2373
                sp->vj = vjsn_parse(sp->body, &e);
418 2373
                XXXAZ(e);
419 2373
                AN(sp->vj);
420 2373
                return (0);
421
        }
422
423 7901
        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 25805
        VTAILQ_FOREACH(spd, &vsc->docs, doc_list) {
430 25805
                CHECK_OBJ_NOTNULL(spd, VSC_SEG_MAGIC);
431 25805
                assert(spd->type == VSC_SEG_DOCS);
432 25805
                if (!spd->mapped && vsc_map_seg(vsc, vsm, spd))
433 0
                        continue; /* Failed to map it */
434 25805
                AN(spd->mapped);
435 25805
                if (spd->head->doc_id == sp->head->doc_id)
436 7901
                        break; /* We have a match */
437 17904
        }
438 7901
        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 7901
        vve = vjsn_child(spd->vj->value, "elements");
447 7901
        AN(vve);
448 7901
        sp->npoints = strtoul(vve->value, NULL, 0);
449 7901
        sp->points = calloc(sp->npoints, sizeof *sp->points);
450 7901
        AN(sp->points);
451 7901
        vsb = VSB_new_auto();
452 7901
        AN(vsb);
453 7901
        vve = vjsn_child(spd->vj->value, "elem");
454 7901
        AN(vve);
455 7901
        pp = sp->points;
456 169149
        VTAILQ_FOREACH(vv, &vve->children, list) {
457 161248
                vsc_fill_point(vsc, sp, vv, vsb, pp);
458 161248
                pp++;
459 161248
        }
460 7901
        VSB_destroy(&vsb);
461 7901
        return (0);
462 68414
}
463
464
/*--------------------------------------------------------------------
465
 */
466
467
static void
468 95074
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 95074
        if (!sp->mapped) {
475 21024
                AZ(sp->exposed);
476 21024
                return;
477
        }
478
479 74656
        if (vsc->fnew != NULL && !sp->exposed &&
480 726
            !del && sp->head->ready == 1)
481 606
                expose = 1;
482 77679
        else if (vsc->fdestroy != NULL && sp->exposed &&
483 4841
            (del || sp->head->ready == 2))
484 606
                expose = 0;
485
        else
486 72838
                return;
487
488 1212
        pp = sp->points;
489 14148
        for (u = 0; u < sp->npoints; u++, pp++) {
490 12936
                if (pp->name == NULL)
491 2046
                        continue;
492 10890
                if (expose)
493 5445
                        pp->point.priv = vsc->fnew(vsc->priv, &pp->point);
494
                else
495 5445
                        vsc->fdestroy(vsc->priv, &pp->point);
496 10890
        }
497 1212
        sp->exposed = expose;
498 95074
}
499
500
/*--------------------------------------------------------------------
501
 */
502
503
static void
504 6641
vsc_del_segs(struct vsc *vsc, struct vsm *vsm, struct vsc_seg_head *head)
505
{
506
        struct vsc_seg *sp, *sp2;
507
508 35674
        VTAILQ_FOREACH_SAFE(sp, head, list, sp2) {
509 29033
                CHECK_OBJ(sp, VSC_SEG_MAGIC);
510 29033
                VTAILQ_REMOVE(head, sp, list);
511 29033
                if (sp->type == VSC_SEG_DOCS)
512 4842
                        VTAILQ_REMOVE(&vsc->docs, sp, doc_list);
513 29033
                vsc_expose(vsc, sp, 1);
514 29033
                vsc_unmap_seg(vsc, vsm, sp);
515 29033
                FREE_OBJ(sp);
516 29033
        }
517 6641
}
518
519
/*--------------------------------------------------------------------
520
 */
521
522
static int
523 60597
vsc_iter_seg(const struct vsc *vsc, const struct vsc_seg *sp,
524
    VSC_iter_f *fiter, void *priv)
525
{
526
        unsigned u;
527 60597
        int i = 0;
528
        struct vsc_pt *pp;
529
530 60597
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
531 60597
        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
532 60597
        AN(fiter);
533 60597
        pp = sp->points;
534 843403
        for (u = 0; u < sp->npoints && i == 0; u++, pp++) {
535 782806
                if (pp->name != NULL)
536 776707
                        i = fiter(priv, &pp->point);
537 782806
        }
538 60597
        return (i);
539
}
540
541
int
542 3815
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 3815
        int i = 0;
549
550 3815
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
551 3815
        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 3815
        VTAILQ_INIT(&removed);
558 3815
        sp = VTAILQ_FIRST(&vsc->segs);
559 183782
        VSM_FOREACH(&ifantom, vsm) {
560 179967
                AN(ifantom.category);
561 179967
                if (!strcmp(ifantom.category, VSC_CLASS))
562 130586
                        type = VSC_SEG_COUNTERS;
563 49381
                else if (!strcmp(ifantom.category, VSC_DOC_CLASS))
564 26587
                        type = VSC_SEG_DOCS;
565
                else {
566
                        /* Not one of the categories we care about */
567 22794
                        continue;
568
                }
569
570 159102
                while (sp != NULL) {
571 127804
                        CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
572 127804
                        if (VSM_StillValid(vsm, sp->fantom) == VSM_valid &&
573 125875
                            !strcmp(ifantom.ident, sp->fantom->ident)) {
574
                                /* sp matches the expected value */
575 125875
                                break;
576
                        }
577
578
                        /* sp is no longer in the VSM list. Remove it from
579
                         * our list. */
580 1929
                        sp2 = sp;
581 1929
                        sp = VTAILQ_NEXT(sp, list);
582 1929
                        VTAILQ_REMOVE(&vsc->segs, sp2, list);
583 1929
                        VTAILQ_INSERT_TAIL(&removed, sp2, list);
584
                }
585
586 157173
                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 31298
                        sp = vsc_new_seg(&ifantom, type);
592 31298
                        AN(sp);
593 31298
                        VTAILQ_INSERT_TAIL(&vsc->segs, sp, list);
594 31298
                        if (type == VSC_SEG_DOCS)
595 5178
                                VTAILQ_INSERT_TAIL(&vsc->docs, sp, doc_list);
596 31298
                }
597
598 157173
                assert(sp->type == type);
599 157173
                sp = VTAILQ_NEXT(sp, list);
600
        }
601 3815
        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 3815
        vsc_del_segs(vsc, vsm, &removed);
612
613
        /* Iterate our shadow list, reporting on each pointer value */
614 83048
        VTAILQ_FOREACH(sp, &vsc->segs, list) {
615 82077
                CHECK_OBJ_NOTNULL(sp, VSC_SEG_MAGIC);
616
617 82077
                if (sp->type != VSC_SEG_COUNTERS)
618 16036
                        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 66041
                if (vsc_map_seg(vsc, vsm, sp))
625 0
                        continue;
626
627
                /* Expose the counters if necessary */
628 66041
                vsc_expose(vsc, sp, 0);
629
630 66041
                if (fiter != NULL && sp->head->ready == 1)
631 60597
                        i = vsc_iter_seg(vsc, sp, fiter, priv);
632 66041
                if (i)
633 2844
                        break;
634 63197
        }
635
636 3815
        return (i);
637
}
638
639
/*--------------------------------------------------------------------
640
 */
641
642
void
643 18
VSC_State(struct vsc *vsc, VSC_new_f *fn, VSC_destroy_f *fd, void *priv)
644
{
645
        struct vsc_seg *sp;
646
647 18
        CHECK_OBJ_NOTNULL(vsc, VSC_MAGIC);
648 18
        assert((fn == NULL && fd == NULL) || (fn != NULL && fd != NULL));
649 18
        if (fd == NULL) {
650 0
                VTAILQ_FOREACH(sp, &vsc->segs, list)
651 0
                        vsc_expose(vsc, sp, 1);
652 0
        }
653 18
        vsc->fnew = fn;
654 18
        vsc->fdestroy = fd;
655 18
        vsc->priv = priv;
656 18
}
657
658
/*--------------------------------------------------------------------
659
 */
660
661
const struct VSC_level_desc *
662 30
VSC_ChangeLevel(const struct VSC_level_desc *old, int chg)
663
{
664
        int i;
665
666 30
        if (old == NULL)
667 18
                old = &levels[0];
668 33
        for (i = 0; i < nlevels; i++)
669 33
                if (old == &levels[i])
670 30
                        break;
671 30
        if (i == nlevels)
672 0
                i = 0;
673
674 30
        i += chg;
675 30
        if (i >= nlevels)
676 0
                i = nlevels - 1;
677 30
        if (i < 0)
678 0
                i = 0;
679 30
        return (&levels[i]);
680
}
681
682
/*--------------------------------------------------------------------*/
683
684
void
685 2826
VSC_Destroy(struct vsc **vscp, struct vsm *vsm)
686
{
687
        struct vsc *vsc;
688
        struct vsc_sf *sf, *sf2;
689
690 2826
        TAKE_OBJ_NOTNULL(vsc, vscp, VSC_MAGIC);
691
692 2886
        VTAILQ_FOREACH_SAFE(sf, &vsc->sf_list, list, sf2) {
693 60
                CHECK_OBJ_NOTNULL(sf, VSC_SF_MAGIC);
694 60
                VTAILQ_REMOVE(&vsc->sf_list, sf, list);
695 60
                free(sf->pattern);
696 60
                FREE_OBJ(sf);
697 60
        }
698
699 2826
        vsc_del_segs(vsc, vsm, &vsc->segs);
700 2826
        assert(VTAILQ_EMPTY(&vsc->docs));
701 2826
        FREE_OBJ(vsc);
702 2826
}