| | 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 |
|
#include "common/heritage.h" |
| 37 |
|
|
| 38 |
|
#include <stdio.h> |
| 39 |
|
#include <stdlib.h> |
| 40 |
|
|
| 41 |
|
#include "storage/storage.h" |
| 42 |
|
#include "storage/storage_simple.h" |
| 43 |
|
|
| 44 |
|
#include "vtim.h" |
| 45 |
|
#include "vnum.h" |
| 46 |
|
|
| 47 |
|
/* |
| 48 |
|
* if smd was to have its own configuration, we would have to wrap all function |
| 49 |
|
* pointers from the actual storage implementation (sma). To avoid these |
| 50 |
|
* complications, we limit to one smd instance and use statics. |
| 51 |
|
*/ |
| 52 |
|
static vtim_dur dopen = 0.0; |
| 53 |
|
static unsigned count = 0; |
| 54 |
|
static ssize_t max_size = 0; |
| 55 |
|
|
| 56 |
|
/* returns one byte less than requested */ |
| 57 |
|
static int v_matchproto_(objgetspace_f) |
| 58 |
80 |
smd_lsp_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz, |
| 59 |
|
uint8_t **ptr) |
| 60 |
|
{ |
| 61 |
80 |
AN(sz); |
| 62 |
80 |
if (*sz > 2) |
| 63 |
40 |
(*sz)--; |
| 64 |
80 |
return (SML_methods.objgetspace(wrk, oc, sz, ptr)); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/* |
| 68 |
|
* returns max_size at most, then fails |
| 69 |
|
* |
| 70 |
|
* relies on the actual storage implementation to not use priv2 |
| 71 |
|
*/ |
| 72 |
|
static int v_matchproto_(objgetspace_f) |
| 73 |
360 |
smd_max_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz, |
| 74 |
|
uint8_t **ptr) |
| 75 |
|
{ |
| 76 |
|
ssize_t used; |
| 77 |
|
int r; |
| 78 |
|
|
| 79 |
360 |
AN(sz); |
| 80 |
360 |
used = (ssize_t)oc->stobj->priv2; |
| 81 |
|
|
| 82 |
360 |
VSLb(wrk->vsl, SLT_Debug, "-sdebug getspace: %zd/%zd", used, max_size); |
| 83 |
|
|
| 84 |
360 |
if (used >= max_size) { |
| 85 |
120 |
VSLb(wrk->vsl, SLT_Storage, "-sdebug: max_size=%zd reached", max_size); |
| 86 |
120 |
return (0); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
240 |
assert(used < max_size); |
| 90 |
240 |
*sz = vmin_t(ssize_t, *sz, max_size - used); |
| 91 |
|
|
| 92 |
240 |
r = SML_methods.objgetspace(wrk, oc, sz, ptr); |
| 93 |
240 |
return (r); |
| 94 |
360 |
} |
| 95 |
|
|
| 96 |
|
static void v_matchproto_(objextend_f) |
| 97 |
240 |
smd_max_extend(struct worker *wrk, struct objcore *oc, ssize_t l) |
| 98 |
|
{ |
| 99 |
|
|
| 100 |
240 |
assert(l > 0); |
| 101 |
240 |
oc->stobj->priv2 += (uint64_t)l; |
| 102 |
240 |
VSLb(wrk->vsl, SLT_Debug, "-sdebug extend: %zd/%zd", (ssize_t)oc->stobj->priv2, max_size); |
| 103 |
240 |
SML_methods.objextend(wrk, oc, l); |
| 104 |
240 |
} |
| 105 |
|
|
| 106 |
|
#define dur_arg(a, s, d) \ |
| 107 |
|
(! strncmp((a), (s), strlen(s)) \ |
| 108 |
|
&& (d = VNUM_duration(a + strlen(s))) != nan("")) |
| 109 |
|
|
| 110 |
|
static int |
| 111 |
353 |
bytes_arg(char *a, const char *s, ssize_t *sz) |
| 112 |
|
{ |
| 113 |
|
const char *err; |
| 114 |
|
uintmax_t bytes; |
| 115 |
|
|
| 116 |
353 |
AN(sz); |
| 117 |
353 |
if (strncmp(a, s, strlen(s))) |
| 118 |
153 |
return (0); |
| 119 |
200 |
a += strlen(s); |
| 120 |
200 |
err = VNUM_2bytes(a, &bytes, 0); |
| 121 |
200 |
if (err != NULL) |
| 122 |
40 |
ARGV_ERR("%s\n", err); |
| 123 |
160 |
assert(bytes <= SSIZE_MAX); |
| 124 |
160 |
*sz = (ssize_t)bytes; |
| 125 |
|
|
| 126 |
160 |
return (1); |
| 127 |
313 |
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
307 |
static void smd_open(struct stevedore *stv) |
| 131 |
|
{ |
| 132 |
307 |
sma_stevedore.open(stv); |
| 133 |
307 |
fprintf(stderr, "-sdebug open delay %fs\n", dopen); |
| 134 |
307 |
if (dopen > 0.0) |
| 135 |
80 |
VTIM_sleep(dopen); |
| 136 |
307 |
} |
| 137 |
|
|
| 138 |
|
static void v_matchproto_(storage_init_f) |
| 139 |
433 |
smd_init(struct stevedore *parent, int aac, char * const *aav) |
| 140 |
|
{ |
| 141 |
|
struct obj_methods *methods; |
| 142 |
433 |
objgetspace_f *getspace = NULL; |
| 143 |
|
const char *ident; |
| 144 |
433 |
int i, ac = 0; |
| 145 |
|
size_t nac; |
| 146 |
433 |
vtim_dur d, dinit = 0.0; |
| 147 |
|
char **av; //lint -e429 |
| 148 |
|
char *a; |
| 149 |
|
|
| 150 |
433 |
if (count++ > 0) |
| 151 |
40 |
ARGV_ERR("Only one -s%s instance supported\n", smd_stevedore.name); |
| 152 |
|
|
| 153 |
393 |
ident = parent->ident; |
| 154 |
393 |
memcpy(parent, &sma_stevedore, sizeof *parent); |
| 155 |
393 |
parent->ident = ident; |
| 156 |
393 |
parent->name = smd_stevedore.name; |
| 157 |
|
|
| 158 |
393 |
methods = malloc(sizeof *methods); |
| 159 |
393 |
AN(methods); |
| 160 |
393 |
memcpy(methods, &SML_methods, sizeof *methods); |
| 161 |
393 |
parent->methods = methods; |
| 162 |
|
|
| 163 |
393 |
assert(aac >= 0); |
| 164 |
393 |
nac = aac; |
| 165 |
393 |
nac++; |
| 166 |
393 |
av = calloc(nac, sizeof *av); |
| 167 |
393 |
AN(av); |
| 168 |
746 |
for (i = 0; i < aac; i++) { |
| 169 |
393 |
a = aav[i]; |
| 170 |
393 |
if (a != NULL) { |
| 171 |
393 |
if (! strcmp(a, "lessspace")) { |
| 172 |
80 |
if (getspace != NULL) { |
| 173 |
0 |
ARGV_ERR("-s%s conflicting options\n", |
| 174 |
|
smd_stevedore.name); |
| 175 |
0 |
} |
| 176 |
80 |
getspace = smd_lsp_getspace; |
| 177 |
80 |
continue; |
| 178 |
|
} |
| 179 |
313 |
if (bytes_arg(a, "maxspace=", &max_size)) { |
| 180 |
160 |
if (getspace != NULL) { |
| 181 |
40 |
ARGV_ERR("-s%s conflicting options\n", |
| 182 |
|
smd_stevedore.name); |
| 183 |
0 |
} |
| 184 |
120 |
getspace = smd_max_getspace; |
| 185 |
120 |
methods->objextend = smd_max_extend; |
| 186 |
120 |
continue; |
| 187 |
|
} |
| 188 |
153 |
if (dur_arg(a, "dinit=", d)) { |
| 189 |
73 |
dinit = d; |
| 190 |
73 |
continue; |
| 191 |
|
} |
| 192 |
80 |
if (dur_arg(a, "dopen=", d)) { |
| 193 |
80 |
dopen = d; |
| 194 |
80 |
continue; |
| 195 |
|
} |
| 196 |
0 |
} |
| 197 |
0 |
av[ac] = a; |
| 198 |
0 |
ac++; |
| 199 |
0 |
} |
| 200 |
353 |
assert(ac >= 0); |
| 201 |
353 |
assert(ac < (int)nac); |
| 202 |
353 |
AZ(av[ac]); |
| 203 |
|
|
| 204 |
353 |
if (getspace != NULL) |
| 205 |
160 |
methods->objgetspace = getspace; |
| 206 |
|
|
| 207 |
353 |
sma_stevedore.init(parent, ac, av); |
| 208 |
353 |
free(av); |
| 209 |
353 |
fprintf(stderr, "-sdebug init delay %fs\n", dinit); |
| 210 |
353 |
fprintf(stderr, "-sdebug open delay in init %fs\n", dopen); |
| 211 |
353 |
if (dinit > 0.0) { |
| 212 |
73 |
VTIM_sleep(dinit); |
| 213 |
73 |
} |
| 214 |
353 |
parent->open = smd_open; |
| 215 |
353 |
} |
| 216 |
|
|
| 217 |
|
const struct stevedore smd_stevedore = { |
| 218 |
|
.magic = STEVEDORE_MAGIC, |
| 219 |
|
.name = "debug", |
| 220 |
|
.init = smd_init, |
| 221 |
|
// other callbacks initialized in smd_init() |
| 222 |
|
}; |