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 50
smd_lsp_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz,
56
    uint8_t **ptr)
57
{
58 50
        AN(sz);
59 50
        if (*sz > 2)
60 25
                (*sz)--;
61 50
        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 119
static void smd_open(struct stevedore *stv)
69
{
70 119
        sma_stevedore.open(stv);
71 119
        fprintf(stderr, "-sdebug open delay %fs\n", dopen);
72 119
        if (dopen > 0.0)
73 50
                VTIM_sleep(dopen);
74 119
}
75
76
static void v_matchproto_(storage_init_f)
77 122
smd_init(struct stevedore *parent, int aac, char * const *aav)
78
{
79
        struct obj_methods *methods;
80
        const char *ident;
81 122
        int i, ac = 0;
82
        size_t nac;
83 122
        vtim_dur d, dinit = 0.0;
84
        char **av;      //lint -e429
85
        char *a;
86
87 122
        ident = parent->ident;
88 122
        memcpy(parent, &sma_stevedore, sizeof *parent);
89 122
        parent->ident = ident;
90 122
        parent->name = smd_stevedore.name;
91
92 122
        methods = malloc(sizeof *methods);
93 122
        AN(methods);
94 122
        memcpy(methods, &SML_methods, sizeof *methods);
95 122
        parent->methods = methods;
96
97 122
        assert(aac >= 0);
98 122
        nac = aac;
99 122
        nac++;
100 122
        av = calloc(nac, sizeof *av);
101 122
        AN(av);
102 244
        for (i = 0; i < aac; i++) {
103 122
                a = aav[i];
104 122
                if (a != NULL) {
105 122
                        if (! strcmp(a, "lessspace")) {
106 25
                                methods->objgetspace = smd_lsp_getspace;
107 25
                                continue;
108
                        }
109 97
                        if (dur_arg(a, "dinit=", d)) {
110 47
                                dinit = d;
111 47
                                continue;
112
                        }
113 50
                        if (dur_arg(a, "dopen=", d)) {
114 50
                                dopen = d;
115 50
                                continue;
116
                        }
117 0
                }
118 0
                av[ac] = a;
119 0
                ac++;
120 0
        }
121 122
        assert(ac >= 0);
122 122
        assert(ac < (int)nac);
123 122
        AZ(av[ac]);
124
125 122
        sma_stevedore.init(parent, ac, av);
126 122
        free(av);
127 122
        fprintf(stderr, "-sdebug init delay %fs\n", dinit);
128 122
        fprintf(stderr, "-sdebug open delay in init %fs\n", dopen);
129 122
        if (dinit > 0.0) {
130 47
                VTIM_sleep(dinit);
131 47
        }
132 122
        parent->open = smd_open;
133 122
}
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
};