varnish-cache/bin/varnishd/storage/storage_debug.c
0
/*-
1
 * Copyright 2021,2023 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Author: Nils Goroll <nils.goroll@uplex.de>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * debug helper storage based on malloc
30
 */
31
32
#include "config.h"
33
34
#include "cache/cache_varnishd.h"
35
#include "cache/cache_obj.h"
36
37
#include <stdio.h>
38
#include <stdlib.h>
39
40
#include "storage/storage.h"
41
#include "storage/storage_simple.h"
42
43
#include "vtim.h"
44
#include "vnum.h"
45
46
/* we cheat and make the open delay a static to avoid
47
 * having to wrap all callbacks to unpack the priv
48
 * pointer. Consequence: last dopen applies to all
49
 * debug stevedores
50
 */
51
static vtim_dur dopen = 0.0;
52
53
/* returns one byte less than requested */
54
static int v_matchproto_(objgetspace_f)
55 80
smd_lsp_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz,
56
    uint8_t **ptr)
57
{
58 80
        AN(sz);
59 80
        if (*sz > 2)
60 40
                (*sz)--;
61 80
        return (SML_methods.objgetspace(wrk, oc, sz, ptr));
62
}
63
64
#define dur_arg(a, s, d)                                        \
65
        (! strncmp((a), (s), strlen(s))                         \
66
         && (d = VNUM_duration(a + strlen(s))) != nan(""))
67
68 181
static void smd_open(struct stevedore *stv)
69
{
70 181
        sma_stevedore.open(stv);
71 181
        fprintf(stderr, "-sdebug open delay %fs\n", dopen);
72 181
        if (dopen > 0.0)
73 80
                VTIM_sleep(dopen);
74 181
}
75
76
static void v_matchproto_(storage_init_f)
77 190
smd_init(struct stevedore *parent, int aac, char * const *aav)
78
{
79
        struct obj_methods *methods;
80
        const char *ident;
81 190
        int i, ac = 0;
82
        size_t nac;
83 190
        vtim_dur d, dinit = 0.0;
84
        char **av;      //lint -e429
85
        char *a;
86
87 190
        ident = parent->ident;
88 190
        memcpy(parent, &sma_stevedore, sizeof *parent);
89 190
        parent->ident = ident;
90 190
        parent->name = smd_stevedore.name;
91
92 190
        methods = malloc(sizeof *methods);
93 190
        AN(methods);
94 190
        memcpy(methods, &SML_methods, sizeof *methods);
95 190
        parent->methods = methods;
96
97 190
        assert(aac >= 0);
98 190
        nac = aac;
99 190
        nac++;
100 190
        av = calloc(nac, sizeof *av);
101 190
        AN(av);
102 380
        for (i = 0; i < aac; i++) {
103 190
                a = aav[i];
104 190
                if (a != NULL) {
105 190
                        if (! strcmp(a, "lessspace")) {
106 40
                                methods->objgetspace = smd_lsp_getspace;
107 40
                                continue;
108
                        }
109 150
                        if (dur_arg(a, "dinit=", d)) {
110 70
                                dinit = d;
111 70
                                continue;
112
                        }
113 80
                        if (dur_arg(a, "dopen=", d)) {
114 80
                                dopen = d;
115 80
                                continue;
116
                        }
117 0
                }
118 0
                av[ac] = a;
119 0
                ac++;
120 0
        }
121 190
        assert(ac >= 0);
122 190
        assert(ac < (int)nac);
123 190
        AZ(av[ac]);
124
125 190
        sma_stevedore.init(parent, ac, av);
126 190
        free(av);
127 190
        fprintf(stderr, "-sdebug init delay %fs\n", dinit);
128 190
        fprintf(stderr, "-sdebug open delay in init %fs\n", dopen);
129 190
        if (dinit > 0.0) {
130 70
                VTIM_sleep(dinit);
131 70
        }
132 190
        parent->open = smd_open;
133 190
}
134
135
const struct stevedore smd_stevedore = {
136
        .magic          =       STEVEDORE_MAGIC,
137
        .name           =       "debug",
138
        .init           =       smd_init,
139
        // other callbacks initialized in smd_init()
140
};