| | varnish-cache/bin/varnishd/storage/storage_simple.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2007-2015 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
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 |
|
*/ |
30 |
|
|
31 |
|
#include "config.h" |
32 |
|
|
33 |
|
#include "cache/cache_varnishd.h" |
34 |
|
|
35 |
13400 |
#include "cache/cache_obj.h" |
36 |
10899 |
#include "cache/cache_objhead.h" |
37 |
2570 |
|
38 |
1780 |
#include "storage/storage.h" |
39 |
10899 |
#include "storage/storage_simple.h" |
40 |
|
|
41 |
|
#include "vtim.h" |
42 |
|
|
43 |
|
/* Flags for allocating memory in sml_stv_alloc */ |
44 |
|
#define LESS_MEM_ALLOCED_IS_OK 1 |
45 |
15010 |
|
46 |
28074 |
// marker pointer for sml_trimstore |
47 |
|
static void *trim_once = &trim_once; |
48 |
|
|
49 |
|
/*-------------------------------------------------------------------*/ |
50 |
|
|
51 |
|
static struct storage * |
52 |
19606 |
objallocwithnuke(struct worker *, const struct stevedore *, ssize_t size, |
53 |
|
int flags); |
54 |
|
|
55 |
|
static struct storage * |
56 |
15547 |
sml_stv_alloc(const struct stevedore *stv, ssize_t size, int flags) |
57 |
|
{ |
58 |
|
struct storage *st; |
59 |
|
|
60 |
15547 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
61 |
15547 |
AN(stv->sml_alloc); |
62 |
|
|
63 |
15547 |
if (!(flags & LESS_MEM_ALLOCED_IS_OK)) { |
64 |
1675 |
if (size > cache_param->fetch_maxchunksize) |
65 |
0 |
return (NULL); |
66 |
|
else |
67 |
1675 |
return (stv->sml_alloc(stv, size)); |
68 |
|
} |
69 |
|
|
70 |
13872 |
if (size > cache_param->fetch_maxchunksize) |
71 |
0 |
size = cache_param->fetch_maxchunksize; |
72 |
|
|
73 |
13872 |
assert(size <= UINT_MAX); /* field limit in struct storage */ |
74 |
|
|
75 |
14547 |
for (;;) { |
76 |
|
/* try to allocate from it */ |
77 |
14547 |
assert(size > 0); |
78 |
14547 |
st = stv->sml_alloc(stv, size); |
79 |
14547 |
if (st != NULL) |
80 |
13817 |
break; |
81 |
|
|
82 |
730 |
if (size <= cache_param->fetch_chunksize) |
83 |
55 |
break; |
84 |
|
|
85 |
675 |
size /= 2; |
86 |
|
} |
87 |
13872 |
CHECK_OBJ_ORNULL(st, STORAGE_MAGIC); |
88 |
13872 |
return (st); |
89 |
15547 |
} |
90 |
|
|
91 |
|
static void |
92 |
20337 |
sml_stv_free(const struct stevedore *stv, struct storage *st) |
93 |
|
{ |
94 |
|
|
95 |
20337 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
96 |
20337 |
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); |
97 |
20337 |
if (stv->sml_free != NULL) |
98 |
20337 |
stv->sml_free(st); |
99 |
20337 |
} |
100 |
|
|
101 |
|
/*-------------------------------------------------------------------- |
102 |
|
* This function is called by stevedores ->allocobj() method, which |
103 |
|
* very often will be SML_allocobj() below, to convert a slab |
104 |
|
* of storage into object which the stevedore can then register in its |
105 |
|
* internal state, before returning it to STV_NewObject(). |
106 |
|
* As you probably guessed: All this for persistence. |
107 |
|
*/ |
108 |
|
|
109 |
|
struct object * |
110 |
13965 |
SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr) |
111 |
|
{ |
112 |
|
struct object *o; |
113 |
|
|
114 |
13965 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
115 |
13965 |
AN(stv->methods); |
116 |
13965 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
117 |
|
|
118 |
13965 |
assert(PAOK(ptr)); |
119 |
|
|
120 |
13965 |
o = ptr; |
121 |
13965 |
INIT_OBJ(o, OBJECT_MAGIC); |
122 |
|
|
123 |
13965 |
VTAILQ_INIT(&o->list); |
124 |
|
|
125 |
13965 |
oc->stobj->stevedore = stv; |
126 |
13965 |
oc->stobj->priv = o; |
127 |
13965 |
oc->stobj->priv2 = 0; |
128 |
13965 |
return (o); |
129 |
|
} |
130 |
|
|
131 |
|
/*-------------------------------------------------------------------- |
132 |
|
* This is the default ->allocobj() which all stevedores who do not |
133 |
|
* implement persistent storage can rely on. |
134 |
|
*/ |
135 |
|
|
136 |
|
int v_matchproto_(storage_allocobj_f) |
137 |
13908 |
SML_allocobj(struct worker *wrk, const struct stevedore *stv, |
138 |
|
struct objcore *oc, unsigned wsl) |
139 |
|
{ |
140 |
|
struct object *o; |
141 |
13908 |
struct storage *st = NULL; |
142 |
|
unsigned ltot; |
143 |
|
|
144 |
13908 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
145 |
13908 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
146 |
13908 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
147 |
|
|
148 |
13908 |
AN(stv->sml_alloc); |
149 |
|
|
150 |
13908 |
ltot = sizeof(*o) + PRNDUP(wsl); |
151 |
|
|
152 |
13908 |
do { |
153 |
13925 |
st = stv->sml_alloc(stv, ltot); |
154 |
13925 |
if (st != NULL && st->space < ltot) { |
155 |
0 |
stv->sml_free(st); |
156 |
0 |
st = NULL; |
157 |
0 |
} |
158 |
13925 |
} while (st == NULL && LRU_NukeOne(wrk, stv->lru)); |
159 |
13908 |
if (st == NULL) |
160 |
55 |
return (0); |
161 |
|
|
162 |
13853 |
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); |
163 |
13853 |
o = SML_MkObject(stv, oc, st->ptr); |
164 |
13853 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
165 |
13853 |
st->len = sizeof(*o); |
166 |
13853 |
o->objstore = st; |
167 |
13853 |
return (1); |
168 |
13908 |
} |
169 |
|
|
170 |
|
void * v_matchproto_(storage_allocbuf_t) |
171 |
110 |
SML_AllocBuf(struct worker *wrk, const struct stevedore *stv, size_t size, |
172 |
|
uintptr_t *ppriv) |
173 |
|
{ |
174 |
|
struct storage *st; |
175 |
|
|
176 |
110 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
177 |
110 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
178 |
110 |
AN(ppriv); |
179 |
|
|
180 |
110 |
if (size > UINT_MAX) |
181 |
0 |
return (NULL); |
182 |
110 |
st = objallocwithnuke(wrk, stv, size, 0); |
183 |
110 |
if (st == NULL) |
184 |
0 |
return (NULL); |
185 |
110 |
assert(st->space >= size); |
186 |
110 |
st->flags = STORAGE_F_BUFFER; |
187 |
110 |
st->len = size; |
188 |
110 |
*ppriv = (uintptr_t)st; |
189 |
110 |
return (st->ptr); |
190 |
110 |
} |
191 |
|
|
192 |
|
void v_matchproto_(storage_freebuf_t) |
193 |
110 |
SML_FreeBuf(struct worker *wrk, const struct stevedore *stv, uintptr_t priv) |
194 |
|
{ |
195 |
|
struct storage *st; |
196 |
|
|
197 |
110 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
198 |
110 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
199 |
|
|
200 |
110 |
CAST_OBJ_NOTNULL(st, (void *)priv, STORAGE_MAGIC); |
201 |
110 |
assert(st->flags == STORAGE_F_BUFFER); |
202 |
110 |
sml_stv_free(stv, st); |
203 |
110 |
} |
204 |
|
|
205 |
|
/*--------------------------------------------------------------------- |
206 |
|
*/ |
207 |
|
|
208 |
|
static struct object * |
209 |
769636 |
sml_getobj(struct worker *wrk, struct objcore *oc) |
210 |
|
{ |
211 |
|
const struct stevedore *stv; |
212 |
|
struct object *o; |
213 |
|
|
214 |
769636 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
215 |
769636 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
216 |
769636 |
stv = oc->stobj->stevedore; |
217 |
769636 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
218 |
769636 |
if (stv->sml_getobj != NULL) |
219 |
1450 |
return (stv->sml_getobj(wrk, oc)); |
220 |
768186 |
if (oc->stobj->priv == NULL) |
221 |
0 |
return (NULL); |
222 |
768186 |
CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC); |
223 |
768186 |
return (o); |
224 |
769636 |
} |
225 |
|
|
226 |
|
static void v_matchproto_(objslim_f) |
227 |
15721 |
sml_slim(struct worker *wrk, struct objcore *oc) |
228 |
|
{ |
229 |
|
const struct stevedore *stv; |
230 |
|
struct object *o; |
231 |
|
struct storage *st, *stn; |
232 |
|
|
233 |
15721 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
234 |
|
|
235 |
15721 |
stv = oc->stobj->stevedore; |
236 |
15721 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
237 |
15721 |
o = sml_getobj(wrk, oc); |
238 |
15721 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
239 |
|
|
240 |
|
#define OBJ_AUXATTR(U, l) \ |
241 |
|
do { \ |
242 |
|
if (o->aa_##l != NULL) { \ |
243 |
|
sml_stv_free(stv, o->aa_##l); \ |
244 |
|
o->aa_##l = NULL; \ |
245 |
|
} \ |
246 |
|
} while (0); |
247 |
|
#include "tbl/obj_attr.h" |
248 |
|
|
249 |
19825 |
VTAILQ_FOREACH_SAFE(st, &o->list, list, stn) { |
250 |
4104 |
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); |
251 |
4104 |
VTAILQ_REMOVE(&o->list, st, list); |
252 |
4104 |
sml_stv_free(stv, st); |
253 |
4104 |
} |
254 |
|
} |
255 |
|
|
256 |
|
static void |
257 |
14048 |
sml_bocfini(const struct stevedore *stv, struct boc *boc) |
258 |
|
{ |
259 |
|
struct storage *st; |
260 |
|
|
261 |
14048 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
262 |
14048 |
CHECK_OBJ_NOTNULL(boc, BOC_MAGIC); |
263 |
|
|
264 |
14048 |
if (boc->stevedore_priv == NULL || |
265 |
9304 |
boc->stevedore_priv == trim_once) |
266 |
13038 |
return; |
267 |
|
|
268 |
|
/* Free any leftovers from Trim */ |
269 |
1010 |
TAKE_OBJ_NOTNULL(st, &boc->stevedore_priv, STORAGE_MAGIC); |
270 |
1010 |
sml_stv_free(stv, st); |
271 |
14048 |
} |
272 |
|
|
273 |
|
/* |
274 |
|
* called in two cases: |
275 |
|
* - oc->boc == NULL: cache object on LRU freed |
276 |
|
* - oc->boc != NULL: cache object replaced for backend error |
277 |
|
*/ |
278 |
|
static void v_matchproto_(objfree_f) |
279 |
8802 |
sml_objfree(struct worker *wrk, struct objcore *oc) |
280 |
|
{ |
281 |
|
const struct stevedore *stv; |
282 |
|
struct storage *st; |
283 |
|
struct object *o; |
284 |
|
|
285 |
8802 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
286 |
8802 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
287 |
8802 |
stv = oc->stobj->stevedore; |
288 |
8802 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
289 |
8802 |
CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC); |
290 |
|
|
291 |
8802 |
sml_slim(wrk, oc); |
292 |
8802 |
st = o->objstore; |
293 |
8802 |
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); |
294 |
8802 |
FINI_OBJ(o); |
295 |
|
|
296 |
8802 |
if (oc->boc != NULL) |
297 |
105 |
sml_bocfini(stv, oc->boc); |
298 |
8697 |
else if (stv->lru != NULL) |
299 |
8696 |
LRU_Remove(oc); |
300 |
|
|
301 |
8802 |
sml_stv_free(stv, st); |
302 |
|
|
303 |
8802 |
memset(oc->stobj, 0, sizeof oc->stobj); |
304 |
|
|
305 |
8802 |
wrk->stats->n_object--; |
306 |
8802 |
} |
307 |
|
|
308 |
|
static int v_matchproto_(objiterator_f) |
309 |
11918 |
sml_iterator(struct worker *wrk, struct objcore *oc, |
310 |
|
void *priv, objiterate_f *func, int final) |
311 |
|
{ |
312 |
|
struct boc *boc; |
313 |
|
enum boc_state_e state; |
314 |
|
struct object *obj; |
315 |
|
struct storage *st; |
316 |
11918 |
struct storage *checkpoint = NULL; |
317 |
|
const struct stevedore *stv; |
318 |
11918 |
ssize_t checkpoint_len = 0; |
319 |
11918 |
ssize_t len = 0; |
320 |
11918 |
int ret = 0, ret2; |
321 |
|
ssize_t ol; |
322 |
|
ssize_t nl; |
323 |
|
ssize_t sl; |
324 |
|
void *p; |
325 |
|
ssize_t l; |
326 |
|
unsigned u; |
327 |
|
|
328 |
11918 |
obj = sml_getobj(wrk, oc); |
329 |
11918 |
CHECK_OBJ_NOTNULL(obj, OBJECT_MAGIC); |
330 |
11918 |
stv = oc->stobj->stevedore; |
331 |
11918 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
332 |
|
|
333 |
11918 |
boc = HSH_RefBoc(oc); |
334 |
|
|
335 |
11918 |
if (boc == NULL) { |
336 |
17067 |
VTAILQ_FOREACH_REVERSE_SAFE( |
337 |
|
st, &obj->list, storagehead, list, checkpoint) { |
338 |
|
|
339 |
9046 |
u = 0; |
340 |
9046 |
if (VTAILQ_PREV(st, storagehead, list) == NULL) |
341 |
8170 |
u |= OBJ_ITER_END; |
342 |
9046 |
if (final) |
343 |
3770 |
u |= OBJ_ITER_FLUSH; |
344 |
9046 |
if (ret == 0 && st->len > 0) |
345 |
9045 |
ret = func(priv, u, st->ptr, st->len); |
346 |
9046 |
if (final) { |
347 |
3771 |
VTAILQ_REMOVE(&obj->list, st, list); |
348 |
3771 |
sml_stv_free(stv, st); |
349 |
9046 |
} else if (ret) |
350 |
155 |
break; |
351 |
8891 |
} |
352 |
8176 |
return (ret); |
353 |
|
} |
354 |
|
|
355 |
3742 |
p = NULL; |
356 |
3742 |
l = 0; |
357 |
|
|
358 |
3742 |
u = 0; |
359 |
3742 |
if (boc->fetched_so_far == 0) { |
360 |
420 |
ret = func(priv, OBJ_ITER_FLUSH, NULL, 0); |
361 |
420 |
if (ret) |
362 |
0 |
return (ret); |
363 |
420 |
} |
364 |
10868 |
while (1) { |
365 |
10868 |
ol = len; |
366 |
10868 |
nl = ObjWaitExtend(wrk, oc, ol, &state); |
367 |
10868 |
if (state == BOS_FAILED) { |
368 |
70 |
ret = -1; |
369 |
70 |
break; |
370 |
|
} |
371 |
10798 |
if (nl == ol) { |
372 |
3585 |
assert(state == BOS_FINISHED); |
373 |
3585 |
break; |
374 |
|
} |
375 |
7213 |
assert(nl > ol); |
376 |
7213 |
Lck_Lock(&boc->mtx); |
377 |
7213 |
AZ(VTAILQ_EMPTY(&obj->list)); |
378 |
7213 |
if (checkpoint == NULL) { |
379 |
4396 |
st = VTAILQ_LAST(&obj->list, storagehead); |
380 |
4396 |
sl = 0; |
381 |
4396 |
} else { |
382 |
2817 |
st = checkpoint; |
383 |
2817 |
sl = checkpoint_len; |
384 |
2817 |
ol -= checkpoint_len; |
385 |
|
} |
386 |
9932 |
while (st != NULL) { |
387 |
9933 |
if (st->len > ol) { |
388 |
7214 |
p = st->ptr + ol; |
389 |
7214 |
l = st->len - ol; |
390 |
7214 |
len += l; |
391 |
7214 |
break; |
392 |
|
} |
393 |
2719 |
ol -= st->len; |
394 |
2719 |
assert(ol >= 0); |
395 |
2719 |
nl -= st->len; |
396 |
2719 |
assert(nl > 0); |
397 |
2719 |
sl += st->len; |
398 |
2719 |
st = VTAILQ_PREV(st, storagehead, list); |
399 |
2719 |
if (final && checkpoint != NULL) { |
400 |
2482 |
if (checkpoint == boc->stevedore_priv) |
401 |
0 |
boc->stevedore_priv = trim_once; |
402 |
|
else |
403 |
2482 |
VTAILQ_REMOVE(&obj->list, checkpoint, list); |
404 |
2482 |
sml_stv_free(stv, checkpoint); |
405 |
2482 |
} |
406 |
2719 |
checkpoint = st; |
407 |
2719 |
checkpoint_len = sl; |
408 |
|
} |
409 |
7215 |
CHECK_OBJ_NOTNULL(obj, OBJECT_MAGIC); |
410 |
7215 |
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); |
411 |
7215 |
st = VTAILQ_PREV(st, storagehead, list); |
412 |
7215 |
if (st != NULL && st->len == 0) |
413 |
1227 |
st = NULL; |
414 |
7215 |
Lck_Unlock(&boc->mtx); |
415 |
7215 |
assert(l > 0 || state == BOS_FINISHED); |
416 |
7215 |
u = 0; |
417 |
7215 |
if (st == NULL || final) |
418 |
7121 |
u |= OBJ_ITER_FLUSH; |
419 |
7215 |
if (st == NULL && state == BOS_FINISHED) |
420 |
38 |
u |= OBJ_ITER_END; |
421 |
7215 |
ret = func(priv, u, p, l); |
422 |
7215 |
if (ret) |
423 |
89 |
break; |
424 |
|
} |
425 |
3744 |
HSH_DerefBoc(wrk, oc); |
426 |
3744 |
if ((u & OBJ_ITER_END) == 0) { |
427 |
3705 |
ret2 = func(priv, OBJ_ITER_END, NULL, 0); |
428 |
3705 |
if (ret == 0) |
429 |
3546 |
ret = ret2; |
430 |
3705 |
} |
431 |
3744 |
return (ret); |
432 |
11920 |
} |
433 |
|
|
434 |
|
/*-------------------------------------------------------------------- |
435 |
|
*/ |
436 |
|
|
437 |
|
static struct storage * |
438 |
14537 |
objallocwithnuke(struct worker *wrk, const struct stevedore *stv, ssize_t size, |
439 |
|
int flags) |
440 |
|
{ |
441 |
14537 |
struct storage *st = NULL; |
442 |
|
|
443 |
14537 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
444 |
14537 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
445 |
|
|
446 |
14537 |
if (size > cache_param->fetch_maxchunksize) { |
447 |
10 |
if (!(flags & LESS_MEM_ALLOCED_IS_OK)) |
448 |
0 |
return (NULL); |
449 |
10 |
size = cache_param->fetch_maxchunksize; |
450 |
10 |
} |
451 |
|
|
452 |
14537 |
assert(size <= UINT_MAX); /* field limit in struct storage */ |
453 |
|
|
454 |
14537 |
do { |
455 |
|
/* try to allocate from it */ |
456 |
14577 |
st = sml_stv_alloc(stv, size, flags); |
457 |
14577 |
if (st != NULL) |
458 |
14512 |
break; |
459 |
|
|
460 |
|
/* no luck; try to free some space and keep trying */ |
461 |
65 |
if (stv->lru == NULL) |
462 |
0 |
break; |
463 |
65 |
} while (LRU_NukeOne(wrk, stv->lru)); |
464 |
|
|
465 |
14537 |
CHECK_OBJ_ORNULL(st, STORAGE_MAGIC); |
466 |
14537 |
return (st); |
467 |
14537 |
} |
468 |
|
|
469 |
|
static int v_matchproto_(objgetspace_f) |
470 |
289862 |
sml_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz, |
471 |
|
uint8_t **ptr) |
472 |
|
{ |
473 |
|
struct object *o; |
474 |
|
struct storage *st; |
475 |
|
|
476 |
289862 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
477 |
289862 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
478 |
289862 |
CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC); |
479 |
289862 |
AN(sz); |
480 |
289862 |
AN(ptr); |
481 |
289862 |
if (*sz == 0) |
482 |
271976 |
*sz = cache_param->fetch_chunksize; |
483 |
289862 |
assert(*sz > 0); |
484 |
289862 |
if (oc->boc->transit_buffer > 0) |
485 |
2407 |
*sz = vmin_t(ssize_t, *sz, oc->boc->transit_buffer); |
486 |
|
|
487 |
289862 |
o = sml_getobj(wrk, oc); |
488 |
289862 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
489 |
289862 |
CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC); |
490 |
|
|
491 |
289862 |
st = VTAILQ_FIRST(&o->list); |
492 |
289862 |
if (st != NULL && st->len < st->space) { |
493 |
276020 |
*sz = st->space - st->len; |
494 |
276020 |
*ptr = st->ptr + st->len; |
495 |
276020 |
assert (*sz > 0); |
496 |
276020 |
return (1); |
497 |
|
} |
498 |
|
|
499 |
13842 |
st = objallocwithnuke(wrk, oc->stobj->stevedore, *sz, |
500 |
|
LESS_MEM_ALLOCED_IS_OK); |
501 |
13842 |
if (st == NULL) |
502 |
25 |
return (0); |
503 |
|
|
504 |
13817 |
CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC); |
505 |
13817 |
Lck_Lock(&oc->boc->mtx); |
506 |
13817 |
VTAILQ_INSERT_HEAD(&o->list, st, list); |
507 |
13817 |
Lck_Unlock(&oc->boc->mtx); |
508 |
|
|
509 |
13817 |
*sz = st->space - st->len; |
510 |
13817 |
assert (*sz > 0); |
511 |
13817 |
*ptr = st->ptr + st->len; |
512 |
13817 |
return (1); |
513 |
289862 |
} |
514 |
|
|
515 |
|
static void v_matchproto_(objextend_f) |
516 |
282621 |
sml_extend(struct worker *wrk, struct objcore *oc, ssize_t l) |
517 |
|
{ |
518 |
|
struct object *o; |
519 |
|
struct storage *st; |
520 |
|
|
521 |
282621 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
522 |
282621 |
assert(l > 0); |
523 |
|
|
524 |
282621 |
o = sml_getobj(wrk, oc); |
525 |
282621 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
526 |
282621 |
st = VTAILQ_FIRST(&o->list); |
527 |
282621 |
CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC); |
528 |
282621 |
assert(st->len + l <= st->space); |
529 |
282621 |
st->len += l; |
530 |
282621 |
} |
531 |
|
|
532 |
|
static void v_matchproto_(objtrimstore_f) |
533 |
9305 |
sml_trimstore(struct worker *wrk, struct objcore *oc) |
534 |
|
{ |
535 |
|
const struct stevedore *stv; |
536 |
|
struct storage *st, *st1; |
537 |
|
struct object *o; |
538 |
|
|
539 |
9305 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
540 |
9305 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
541 |
9305 |
CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC); |
542 |
|
|
543 |
9305 |
stv = oc->stobj->stevedore; |
544 |
9305 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
545 |
|
|
546 |
9305 |
if (oc->boc->stevedore_priv != NULL) |
547 |
0 |
WRONG("sml_trimstore already called"); |
548 |
9305 |
oc->boc->stevedore_priv = trim_once; |
549 |
|
|
550 |
9305 |
if (stv->sml_free == NULL) |
551 |
0 |
return; |
552 |
|
|
553 |
9305 |
o = sml_getobj(wrk, oc); |
554 |
9305 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
555 |
9305 |
st = VTAILQ_FIRST(&o->list); |
556 |
|
|
557 |
9305 |
if (st == NULL) |
558 |
0 |
return; |
559 |
|
|
560 |
9305 |
if (st->len == 0) { |
561 |
40 |
Lck_Lock(&oc->boc->mtx); |
562 |
40 |
VTAILQ_REMOVE(&o->list, st, list); |
563 |
40 |
Lck_Unlock(&oc->boc->mtx); |
564 |
|
/* sml_bocdone frees this */ |
565 |
40 |
oc->boc->stevedore_priv = st; |
566 |
40 |
return; |
567 |
|
} |
568 |
|
|
569 |
9265 |
if (st->space - st->len < 512) |
570 |
8295 |
return; |
571 |
|
|
572 |
970 |
st1 = sml_stv_alloc(stv, st->len, 0); |
573 |
970 |
if (st1 == NULL) |
574 |
0 |
return; |
575 |
970 |
assert(st1->space >= st->len); |
576 |
|
|
577 |
970 |
memcpy(st1->ptr, st->ptr, st->len); |
578 |
970 |
st1->len = st->len; |
579 |
970 |
Lck_Lock(&oc->boc->mtx); |
580 |
970 |
VTAILQ_REMOVE(&o->list, st, list); |
581 |
970 |
VTAILQ_INSERT_HEAD(&o->list, st1, list); |
582 |
970 |
Lck_Unlock(&oc->boc->mtx); |
583 |
|
/* sml_bocdone frees this */ |
584 |
970 |
oc->boc->stevedore_priv = st; |
585 |
9305 |
} |
586 |
|
|
587 |
|
static void v_matchproto_(objbocdone_f) |
588 |
13943 |
sml_bocdone(struct worker *wrk, struct objcore *oc, struct boc *boc) |
589 |
|
{ |
590 |
|
const struct stevedore *stv; |
591 |
|
|
592 |
13943 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
593 |
13943 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
594 |
13943 |
CHECK_OBJ_NOTNULL(boc, BOC_MAGIC); |
595 |
13943 |
stv = oc->stobj->stevedore; |
596 |
13943 |
CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC); |
597 |
|
|
598 |
13943 |
sml_bocfini(stv, boc); |
599 |
|
|
600 |
13943 |
if (stv->lru != NULL) { |
601 |
13750 |
if (isnan(wrk->lastused)) |
602 |
0 |
wrk->lastused = VTIM_real(); |
603 |
13750 |
LRU_Add(oc, wrk->lastused); // approx timestamp is OK |
604 |
13750 |
} |
605 |
13943 |
} |
606 |
|
|
607 |
|
static const void * v_matchproto_(objgetattr_f) |
608 |
108234 |
sml_getattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr, |
609 |
|
ssize_t *len) |
610 |
|
{ |
611 |
|
struct object *o; |
612 |
|
ssize_t dummy; |
613 |
|
|
614 |
108234 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
615 |
|
|
616 |
108234 |
if (len == NULL) |
617 |
66453 |
len = &dummy; |
618 |
108234 |
o = sml_getobj(wrk, oc); |
619 |
108234 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
620 |
|
|
621 |
108234 |
switch (attr) { |
622 |
|
/* Fixed size attributes */ |
623 |
|
#define OBJ_FIXATTR(U, l, s) \ |
624 |
|
case OA_##U: \ |
625 |
|
*len = sizeof o->fa_##l; \ |
626 |
|
return (o->fa_##l); |
627 |
|
#include "tbl/obj_attr.h" |
628 |
|
|
629 |
|
/* Variable size attributes */ |
630 |
|
#define OBJ_VARATTR(U, l) \ |
631 |
|
case OA_##U: \ |
632 |
|
if (o->va_##l == NULL) \ |
633 |
|
return (NULL); \ |
634 |
|
*len = o->va_##l##_len; \ |
635 |
|
return (o->va_##l); |
636 |
|
#include "tbl/obj_attr.h" |
637 |
|
|
638 |
|
/* Auxiliary attributes */ |
639 |
|
#define OBJ_AUXATTR(U, l) \ |
640 |
|
case OA_##U: \ |
641 |
|
if (o->aa_##l == NULL) \ |
642 |
|
return (NULL); \ |
643 |
|
CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC); \ |
644 |
|
*len = o->aa_##l->len; \ |
645 |
|
return (o->aa_##l->ptr); |
646 |
|
#include "tbl/obj_attr.h" |
647 |
|
|
648 |
|
default: |
649 |
|
break; |
650 |
|
} |
651 |
0 |
WRONG("Unsupported OBJ_ATTR"); |
652 |
108234 |
} |
653 |
|
|
654 |
|
static void * v_matchproto_(objsetattr_f) |
655 |
52086 |
sml_setattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr, |
656 |
|
ssize_t len, const void *ptr) |
657 |
|
{ |
658 |
|
struct object *o; |
659 |
52086 |
void *retval = NULL; |
660 |
|
struct storage *st; |
661 |
|
|
662 |
52086 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
663 |
|
|
664 |
52086 |
o = sml_getobj(wrk, oc); |
665 |
52086 |
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC); |
666 |
52086 |
st = o->objstore; |
667 |
|
|
668 |
52086 |
switch (attr) { |
669 |
|
/* Fixed size attributes */ |
670 |
|
#define OBJ_FIXATTR(U, l, s) \ |
671 |
|
case OA_##U: \ |
672 |
|
assert(len == sizeof o->fa_##l); \ |
673 |
|
retval = o->fa_##l; \ |
674 |
|
break; |
675 |
|
#include "tbl/obj_attr.h" |
676 |
|
|
677 |
|
/* Variable size attributes */ |
678 |
|
#define OBJ_VARATTR(U, l) \ |
679 |
|
case OA_##U: \ |
680 |
|
if (o->va_##l##_len > 0) { \ |
681 |
|
AN(o->va_##l); \ |
682 |
|
assert(len == o->va_##l##_len); \ |
683 |
|
retval = o->va_##l; \ |
684 |
|
} else if (len > 0) { \ |
685 |
|
assert(len <= UINT_MAX); \ |
686 |
|
assert(st->len + len <= st->space); \ |
687 |
|
o->va_##l = st->ptr + st->len; \ |
688 |
|
st->len += len; \ |
689 |
|
o->va_##l##_len = len; \ |
690 |
|
retval = o->va_##l; \ |
691 |
|
} \ |
692 |
|
break; |
693 |
|
#include "tbl/obj_attr.h" |
694 |
|
|
695 |
|
/* Auxiliary attributes */ |
696 |
|
#define OBJ_AUXATTR(U, l) \ |
697 |
|
case OA_##U: \ |
698 |
|
if (o->aa_##l != NULL) { \ |
699 |
|
CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC); \ |
700 |
|
assert(len == o->aa_##l->len); \ |
701 |
|
retval = o->aa_##l->ptr; \ |
702 |
|
break; \ |
703 |
|
} \ |
704 |
|
if (len == 0) \ |
705 |
|
break; \ |
706 |
|
o->aa_##l = objallocwithnuke(wrk, oc->stobj->stevedore, \ |
707 |
|
len, 0); \ |
708 |
|
if (o->aa_##l == NULL) \ |
709 |
|
break; \ |
710 |
|
CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC); \ |
711 |
|
assert(len <= o->aa_##l->space); \ |
712 |
|
o->aa_##l->len = len; \ |
713 |
|
retval = o->aa_##l->ptr; \ |
714 |
|
break; |
715 |
|
#include "tbl/obj_attr.h" |
716 |
|
|
717 |
|
default: |
718 |
0 |
WRONG("Unsupported OBJ_ATTR"); |
719 |
|
break; |
720 |
|
} |
721 |
|
|
722 |
52086 |
if (retval != NULL && ptr != NULL) |
723 |
1880 |
memcpy(retval, ptr, len); |
724 |
52086 |
return (retval); |
725 |
|
} |
726 |
|
|
727 |
|
const struct obj_methods SML_methods = { |
728 |
|
.objfree = sml_objfree, |
729 |
|
.objiterator = sml_iterator, |
730 |
|
.objgetspace = sml_getspace, |
731 |
|
.objextend = sml_extend, |
732 |
|
.objtrimstore = sml_trimstore, |
733 |
|
.objbocdone = sml_bocdone, |
734 |
|
.objslim = sml_slim, |
735 |
|
.objgetattr = sml_getattr, |
736 |
|
.objsetattr = sml_setattr, |
737 |
|
.objtouch = LRU_Touch, |
738 |
|
}; |
739 |
|
|
740 |
|
static void |
741 |
15 |
sml_panic_st(struct vsb *vsb, const char *hd, const struct storage *st) |
742 |
|
{ |
743 |
30 |
VSB_printf(vsb, "%s = %p {priv=%p, ptr=%p, len=%u, space=%u},\n", |
744 |
15 |
hd, st, st->priv, st->ptr, st->len, st->space); |
745 |
15 |
} |
746 |
|
|
747 |
|
void |
748 |
10 |
SML_panic(struct vsb *vsb, const struct objcore *oc) |
749 |
|
{ |
750 |
|
struct object *o; |
751 |
|
struct storage *st; |
752 |
|
|
753 |
10 |
VSB_printf(vsb, "Simple = %p,\n", oc->stobj->priv); |
754 |
10 |
if (oc->stobj->priv == NULL) |
755 |
0 |
return; |
756 |
10 |
o = oc->stobj->priv; |
757 |
10 |
PAN_CheckMagic(vsb, o, OBJECT_MAGIC); |
758 |
10 |
sml_panic_st(vsb, "Obj", o->objstore); |
759 |
|
|
760 |
|
#define OBJ_FIXATTR(U, l, sz) \ |
761 |
|
VSB_printf(vsb, "%s = ", #U); \ |
762 |
|
VSB_quote(vsb, (const void*)o->fa_##l, sz, VSB_QUOTE_HEX); \ |
763 |
|
VSB_printf(vsb, ",\n"); |
764 |
|
|
765 |
|
#define OBJ_VARATTR(U, l) \ |
766 |
|
VSB_printf(vsb, "%s = {len=%u, ptr=%p},\n", \ |
767 |
|
#U, o->va_##l##_len, o->va_##l); |
768 |
|
|
769 |
|
#define OBJ_AUXATTR(U, l) \ |
770 |
|
do { \ |
771 |
|
if (o->aa_##l != NULL) sml_panic_st(vsb, #U, o->aa_##l);\ |
772 |
|
} while(0); |
773 |
|
|
774 |
|
#include "tbl/obj_attr.h" |
775 |
|
|
776 |
15 |
VTAILQ_FOREACH(st, &o->list, list) { |
777 |
5 |
sml_panic_st(vsb, "Body", st); |
778 |
5 |
} |
779 |
|
} |