| | varnish-cache/bin/varnishd/cache/cache_hash.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 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
* |
30 |
|
* This is the central hash-table code, it relies on a chosen hash |
31 |
|
* implementation only for the actual hashing, all the housekeeping |
32 |
|
* happens here. |
33 |
|
* |
34 |
|
* We have two kinds of structures, objecthead and object. An objecthead |
35 |
|
* corresponds to a given (Host:, URL) tuple, and the objects hung from |
36 |
|
* the objecthead may represent various variations (ie: Vary: header, |
37 |
|
* different TTL etc) instances of that web-entity. |
38 |
|
* |
39 |
|
* Each objecthead has a mutex which locks both its own fields, the |
40 |
|
* list of objects and fields in the objects. |
41 |
|
* |
42 |
|
* The hash implementation must supply a reference count facility on |
43 |
|
* the objecthead, and return with a reference held after a lookup. |
44 |
|
* |
45 |
|
* Lookups in the hash implementation returns with a ref held and each |
46 |
|
* object hung from the objhead holds a ref as well. |
47 |
|
* |
48 |
|
* Objects have refcounts which are locked by the objecthead mutex. |
49 |
|
* |
50 |
|
* New objects are always marked busy, and they can go from busy to |
51 |
|
* not busy only once. |
52 |
|
*/ |
53 |
|
|
54 |
|
#include "config.h" |
55 |
|
|
56 |
|
#include <stdio.h> |
57 |
|
#include <stdlib.h> |
58 |
|
|
59 |
|
#include "cache_varnishd.h" |
60 |
|
|
61 |
|
#include "cache/cache_objhead.h" |
62 |
|
#include "cache/cache_transport.h" |
63 |
|
|
64 |
|
#include "hash/hash_slinger.h" |
65 |
|
|
66 |
|
#include "vsha256.h" |
67 |
|
|
68 |
|
struct rush { |
69 |
|
unsigned magic; |
70 |
|
#define RUSH_MAGIC 0xa1af5f01 |
71 |
|
VTAILQ_HEAD(,req) reqs; |
72 |
|
}; |
73 |
|
|
74 |
|
static const struct hash_slinger *hash; |
75 |
|
static struct objhead *private_oh; |
76 |
|
|
77 |
|
static void hsh_rush1(const struct worker *, struct objhead *, |
78 |
|
struct rush *, int); |
79 |
|
static void hsh_rush2(struct worker *, struct rush *); |
80 |
|
static int hsh_deref_objhead(struct worker *wrk, struct objhead **poh); |
81 |
|
static int hsh_deref_objhead_unlock(struct worker *wrk, struct objhead **poh, |
82 |
|
int); |
83 |
|
|
84 |
|
/*---------------------------------------------------------------------*/ |
85 |
|
|
86 |
|
#define VCF_RETURN(x) const struct vcf_return VCF_##x[1] = { \ |
87 |
|
{ .name = #x, } \ |
88 |
|
}; |
89 |
|
|
90 |
|
VCF_RETURNS() |
91 |
|
#undef VCF_RETURN |
92 |
|
|
93 |
|
/*---------------------------------------------------------------------*/ |
94 |
|
|
95 |
|
static struct objhead * |
96 |
97027 |
hsh_newobjhead(void) |
97 |
|
{ |
98 |
|
struct objhead *oh; |
99 |
|
|
100 |
97027 |
ALLOC_OBJ(oh, OBJHEAD_MAGIC); |
101 |
97027 |
XXXAN(oh); |
102 |
97027 |
oh->refcnt = 1; |
103 |
97027 |
VTAILQ_INIT(&oh->objcs); |
104 |
97027 |
VTAILQ_INIT(&oh->waitinglist); |
105 |
97027 |
Lck_New(&oh->mtx, lck_objhdr); |
106 |
97027 |
return (oh); |
107 |
|
} |
108 |
|
|
109 |
|
/*---------------------------------------------------------------------*/ |
110 |
|
/* Precreate an objhead and object for later use */ |
111 |
|
static void |
112 |
101312 |
hsh_prealloc(struct worker *wrk) |
113 |
|
{ |
114 |
|
|
115 |
101312 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
116 |
|
|
117 |
101312 |
if (wrk->wpriv->nobjcore == NULL) |
118 |
69036 |
wrk->wpriv->nobjcore = ObjNew(wrk); |
119 |
101312 |
CHECK_OBJ_NOTNULL(wrk->wpriv->nobjcore, OBJCORE_MAGIC); |
120 |
|
|
121 |
101312 |
if (wrk->wpriv->nobjhead == NULL) { |
122 |
60359 |
wrk->wpriv->nobjhead = hsh_newobjhead(); |
123 |
60359 |
wrk->stats->n_objecthead++; |
124 |
60359 |
} |
125 |
101312 |
CHECK_OBJ_NOTNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC); |
126 |
|
|
127 |
101312 |
if (hash->prep != NULL) |
128 |
101072 |
hash->prep(wrk); |
129 |
101312 |
} |
130 |
|
|
131 |
|
/*---------------------------------------------------------------------*/ |
132 |
|
|
133 |
|
struct objcore * |
134 |
55359 |
HSH_Private(const struct worker *wrk) |
135 |
|
{ |
136 |
|
struct objcore *oc; |
137 |
|
|
138 |
55359 |
CHECK_OBJ_NOTNULL(private_oh, OBJHEAD_MAGIC); |
139 |
|
|
140 |
55359 |
oc = ObjNew(wrk); |
141 |
55359 |
AN(oc); |
142 |
55359 |
oc->refcnt = 1; |
143 |
55359 |
oc->objhead = private_oh; |
144 |
55359 |
oc->flags |= OC_F_PRIVATE; |
145 |
55359 |
Lck_Lock(&private_oh->mtx); |
146 |
55359 |
VTAILQ_INSERT_TAIL(&private_oh->objcs, oc, hsh_list); |
147 |
55359 |
private_oh->refcnt++; |
148 |
55359 |
Lck_Unlock(&private_oh->mtx); |
149 |
55359 |
return (oc); |
150 |
|
} |
151 |
|
|
152 |
|
/*---------------------------------------------------------------------*/ |
153 |
|
|
154 |
|
void |
155 |
37038 |
HSH_Cleanup(const struct worker *wrk) |
156 |
|
{ |
157 |
|
|
158 |
37038 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
159 |
37038 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
160 |
37038 |
if (wrk->wpriv->nobjcore != NULL) |
161 |
76 |
ObjDestroy(wrk, &wrk->wpriv->nobjcore); |
162 |
|
|
163 |
37038 |
if (wrk->wpriv->nobjhead != NULL) { |
164 |
76 |
CHECK_OBJ(wrk->wpriv->nobjhead, OBJHEAD_MAGIC); |
165 |
76 |
Lck_Delete(&wrk->wpriv->nobjhead->mtx); |
166 |
76 |
FREE_OBJ(wrk->wpriv->nobjhead); |
167 |
76 |
wrk->stats->n_objecthead--; |
168 |
76 |
} |
169 |
37038 |
if (wrk->wpriv->nhashpriv != NULL) { |
170 |
|
/* XXX: If needed, add slinger method for this */ |
171 |
80 |
free(wrk->wpriv->nhashpriv); |
172 |
80 |
wrk->wpriv->nhashpriv = NULL; |
173 |
80 |
} |
174 |
37038 |
} |
175 |
|
|
176 |
|
void |
177 |
0 |
HSH_DeleteObjHead(const struct worker *wrk, struct objhead *oh) |
178 |
|
{ |
179 |
0 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
180 |
0 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
181 |
|
|
182 |
0 |
AZ(oh->refcnt); |
183 |
0 |
assert(VTAILQ_EMPTY(&oh->objcs)); |
184 |
0 |
assert(VTAILQ_EMPTY(&oh->waitinglist)); |
185 |
0 |
Lck_Delete(&oh->mtx); |
186 |
0 |
wrk->stats->n_objecthead--; |
187 |
0 |
FREE_OBJ(oh); |
188 |
0 |
} |
189 |
|
|
190 |
|
void |
191 |
569636 |
HSH_AddString(struct req *req, void *ctx, const char *str) |
192 |
|
{ |
193 |
|
|
194 |
569636 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
195 |
569636 |
AN(ctx); |
196 |
569636 |
if (str != NULL) { |
197 |
284794 |
VSHA256_Update(ctx, str, strlen(str)); |
198 |
284794 |
VSLbs(req->vsl, SLT_Hash, TOSTRAND(str)); |
199 |
284794 |
} else |
200 |
284842 |
VSHA256_Update(ctx, &str, 1); |
201 |
569636 |
} |
202 |
|
|
203 |
|
/*--------------------------------------------------------------------- |
204 |
|
* This is a debugging hack to enable testing of boundary conditions |
205 |
|
* in the hash algorithm. |
206 |
|
* We trap the first 9 different digests and translate them to different |
207 |
|
* digests with edge bit conditions |
208 |
|
*/ |
209 |
|
|
210 |
|
static struct hsh_magiclist { |
211 |
|
unsigned char was[VSHA256_LEN]; |
212 |
|
unsigned char now[VSHA256_LEN]; |
213 |
|
} hsh_magiclist[] = { |
214 |
|
{ .now = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
215 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
216 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
217 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, |
218 |
|
{ .now = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
219 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
220 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
221 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }, |
222 |
|
{ .now = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
223 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
224 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
225 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } }, |
226 |
|
{ .now = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
227 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
228 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
229 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40 } }, |
230 |
|
{ .now = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
231 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
232 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
233 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 } }, |
234 |
|
{ .now = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
235 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
236 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
237 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, |
238 |
|
{ .now = { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
239 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
240 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
241 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, |
242 |
|
{ .now = { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
243 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
244 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
245 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, |
246 |
|
{ .now = { 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
247 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
248 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
249 |
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }, |
250 |
|
}; |
251 |
|
|
252 |
|
#define HSH_NMAGIC (sizeof hsh_magiclist / sizeof hsh_magiclist[0]) |
253 |
|
|
254 |
|
static void |
255 |
720 |
hsh_testmagic(void *result) |
256 |
|
{ |
257 |
|
size_t i, j; |
258 |
|
static size_t nused = 0; |
259 |
|
|
260 |
3600 |
for (i = 0; i < nused; i++) |
261 |
3240 |
if (!memcmp(hsh_magiclist[i].was, result, VSHA256_LEN)) |
262 |
360 |
break; |
263 |
720 |
if (i == nused && i < HSH_NMAGIC) |
264 |
360 |
memcpy(hsh_magiclist[nused++].was, result, VSHA256_LEN); |
265 |
720 |
if (i == nused) |
266 |
0 |
return; |
267 |
720 |
assert(i < HSH_NMAGIC); |
268 |
720 |
fprintf(stderr, "HASHMAGIC: <"); |
269 |
23760 |
for (j = 0; j < VSHA256_LEN; j++) |
270 |
23040 |
fprintf(stderr, "%02x", ((unsigned char*)result)[j]); |
271 |
720 |
fprintf(stderr, "> -> <"); |
272 |
720 |
memcpy(result, hsh_magiclist[i].now, VSHA256_LEN); |
273 |
23760 |
for (j = 0; j < VSHA256_LEN; j++) |
274 |
23040 |
fprintf(stderr, "%02x", ((unsigned char*)result)[j]); |
275 |
720 |
fprintf(stderr, ">\n"); |
276 |
720 |
} |
277 |
|
|
278 |
|
/*--------------------------------------------------------------------- |
279 |
|
* Insert an object which magically appears out of nowhere or, more likely, |
280 |
|
* comes off some persistent storage device. |
281 |
|
* Insert it with a reference held. |
282 |
|
*/ |
283 |
|
|
284 |
|
void |
285 |
680 |
HSH_Insert(struct worker *wrk, const void *digest, struct objcore *oc, |
286 |
|
struct ban *ban) |
287 |
|
{ |
288 |
|
struct objhead *oh; |
289 |
|
struct rush rush; |
290 |
|
|
291 |
680 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
292 |
680 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
293 |
680 |
AN(digest); |
294 |
680 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
295 |
680 |
AN(ban); |
296 |
680 |
AN(oc->flags & OC_F_BUSY); |
297 |
680 |
AZ(oc->flags & OC_F_PRIVATE); |
298 |
680 |
assert(oc->refcnt == 1); |
299 |
680 |
INIT_OBJ(&rush, RUSH_MAGIC); |
300 |
|
|
301 |
680 |
hsh_prealloc(wrk); |
302 |
|
|
303 |
680 |
AN(wrk->wpriv->nobjhead); |
304 |
680 |
oh = hash->lookup(wrk, digest, &wrk->wpriv->nobjhead); |
305 |
680 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
306 |
680 |
Lck_AssertHeld(&oh->mtx); |
307 |
680 |
assert(oh->refcnt > 0); |
308 |
|
|
309 |
|
/* Mark object busy and insert (precreated) objcore in |
310 |
|
objecthead. The new object inherits our objhead reference. */ |
311 |
680 |
oc->objhead = oh; |
312 |
680 |
VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list); |
313 |
680 |
EXP_RefNewObjcore(oc); |
314 |
680 |
Lck_Unlock(&oh->mtx); |
315 |
|
|
316 |
680 |
BAN_RefBan(oc, ban); |
317 |
680 |
AN(oc->ban); |
318 |
|
|
319 |
|
/* Move the object first in the oh list, unbusy it and run the |
320 |
|
waitinglist if necessary */ |
321 |
680 |
Lck_Lock(&oh->mtx); |
322 |
680 |
VTAILQ_REMOVE(&oh->objcs, oc, hsh_list); |
323 |
680 |
VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list); |
324 |
680 |
oc->flags &= ~OC_F_BUSY; |
325 |
680 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) |
326 |
0 |
hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY); |
327 |
680 |
Lck_Unlock(&oh->mtx); |
328 |
680 |
hsh_rush2(wrk, &rush); |
329 |
|
|
330 |
680 |
EXP_Insert(wrk, oc); |
331 |
680 |
} |
332 |
|
|
333 |
|
/*--------------------------------------------------------------------- |
334 |
|
*/ |
335 |
|
|
336 |
|
static struct objcore * |
337 |
58499 |
hsh_insert_busyobj(const struct worker *wrk, struct objhead *oh) |
338 |
|
{ |
339 |
|
struct objcore *oc; |
340 |
|
|
341 |
58499 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
342 |
58499 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
343 |
58499 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
344 |
58499 |
Lck_AssertHeld(&oh->mtx); |
345 |
|
|
346 |
58499 |
oc = wrk->wpriv->nobjcore; |
347 |
58499 |
wrk->wpriv->nobjcore = NULL; |
348 |
58499 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
349 |
|
|
350 |
58499 |
AN(oc->flags & OC_F_BUSY); |
351 |
58499 |
oc->refcnt = 1; /* Owned by busyobj */ |
352 |
58499 |
oc->objhead = oh; |
353 |
58499 |
VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list); |
354 |
58499 |
return (oc); |
355 |
|
} |
356 |
|
|
357 |
|
/*--------------------------------------------------------------------- |
358 |
|
*/ |
359 |
|
|
360 |
|
enum lookup_e |
361 |
100635 |
HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp) |
362 |
|
{ |
363 |
|
struct worker *wrk; |
364 |
|
struct objhead *oh; |
365 |
|
struct objcore *oc; |
366 |
|
struct objcore *exp_oc; |
367 |
|
const struct vcf_return *vr; |
368 |
|
vtim_real exp_t_origin; |
369 |
|
int busy_found; |
370 |
|
const uint8_t *vary; |
371 |
|
intmax_t boc_progress; |
372 |
100635 |
unsigned xid = 0; |
373 |
100635 |
float dttl = 0.0; |
374 |
|
|
375 |
100635 |
AN(ocp); |
376 |
100635 |
*ocp = NULL; |
377 |
100635 |
AN(bocp); |
378 |
100635 |
*bocp = NULL; |
379 |
|
|
380 |
100635 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
381 |
100635 |
wrk = req->wrk; |
382 |
100635 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
383 |
100635 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
384 |
100635 |
CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC); |
385 |
100635 |
CHECK_OBJ_ORNULL(req->vcf, VCF_MAGIC); |
386 |
100635 |
AN(hash); |
387 |
|
|
388 |
100635 |
hsh_prealloc(wrk); |
389 |
100635 |
if (DO_DEBUG(DBG_HASHEDGE)) |
390 |
720 |
hsh_testmagic(req->digest); |
391 |
|
|
392 |
100635 |
if (req->hash_objhead != NULL) { |
393 |
|
/* |
394 |
|
* This req came off the waiting list, and brings an |
395 |
|
* oh refcnt with it. |
396 |
|
*/ |
397 |
1902 |
CHECK_OBJ_NOTNULL(req->hash_objhead, OBJHEAD_MAGIC); |
398 |
1902 |
oh = req->hash_objhead; |
399 |
1902 |
Lck_Lock(&oh->mtx); |
400 |
1902 |
req->hash_objhead = NULL; |
401 |
1902 |
} else { |
402 |
98733 |
AN(wrk->wpriv->nobjhead); |
403 |
98733 |
oh = hash->lookup(wrk, req->digest, &wrk->wpriv->nobjhead); |
404 |
|
} |
405 |
|
|
406 |
100635 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
407 |
100635 |
Lck_AssertHeld(&oh->mtx); |
408 |
|
|
409 |
100635 |
if (req->hash_always_miss) { |
410 |
|
/* XXX: should we do predictive Vary in this case ? */ |
411 |
|
/* Insert new objcore in objecthead and release mutex */ |
412 |
560 |
*bocp = hsh_insert_busyobj(wrk, oh); |
413 |
|
/* NB: no deref of objhead, new object inherits reference */ |
414 |
560 |
Lck_Unlock(&oh->mtx); |
415 |
560 |
return (HSH_MISS); |
416 |
|
} |
417 |
|
|
418 |
100075 |
assert(oh->refcnt > 0); |
419 |
100075 |
busy_found = 0; |
420 |
100075 |
exp_oc = NULL; |
421 |
100075 |
exp_t_origin = 0.0; |
422 |
215635 |
VTAILQ_FOREACH(oc, &oh->objcs, hsh_list) { |
423 |
|
/* Must be at least our own ref + the objcore we examine */ |
424 |
156996 |
assert(oh->refcnt > 1); |
425 |
156996 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
426 |
156996 |
assert(oc->objhead == oh); |
427 |
156996 |
assert(oc->refcnt > 0); |
428 |
|
|
429 |
156996 |
if (oc->flags & OC_F_DYING) |
430 |
0 |
continue; |
431 |
156996 |
if (oc->flags & OC_F_FAILED) |
432 |
0 |
continue; |
433 |
|
|
434 |
156996 |
CHECK_OBJ_ORNULL(oc->boc, BOC_MAGIC); |
435 |
156996 |
if (oc->flags & OC_F_BUSY) { |
436 |
2140 |
if (req->hash_ignore_busy) |
437 |
40 |
continue; |
438 |
|
|
439 |
2219 |
if (oc->boc && oc->boc->vary != NULL && |
440 |
119 |
!req->hash_ignore_vary && |
441 |
119 |
!VRY_Match(req, oc->boc->vary)) { |
442 |
40 |
wrk->strangelove++; |
443 |
40 |
continue; |
444 |
|
} |
445 |
|
|
446 |
2060 |
busy_found = 1; |
447 |
2060 |
continue; |
448 |
|
} |
449 |
|
|
450 |
154856 |
if (oc->ttl <= 0.) |
451 |
1840 |
continue; |
452 |
|
|
453 |
153016 |
if (BAN_CheckObject(wrk, oc, req)) { |
454 |
1280 |
oc->flags |= OC_F_DYING; |
455 |
1280 |
EXP_Remove(oc, NULL); |
456 |
1280 |
continue; |
457 |
|
} |
458 |
|
|
459 |
151736 |
if (!req->hash_ignore_vary && ObjHasAttr(wrk, oc, OA_VARY)) { |
460 |
110705 |
vary = ObjGetAttr(wrk, oc, OA_VARY, NULL); |
461 |
110705 |
AN(vary); |
462 |
110705 |
if (!VRY_Match(req, vary)) { |
463 |
104745 |
wrk->strangelove++; |
464 |
104745 |
continue; |
465 |
|
} |
466 |
5960 |
} |
467 |
|
|
468 |
46991 |
if (req->vcf != NULL) { |
469 |
320 |
vr = req->vcf->func(req, &oc, &exp_oc, 0); |
470 |
320 |
if (vr == VCF_CONTINUE) |
471 |
160 |
continue; |
472 |
160 |
if (vr == VCF_MISS) { |
473 |
120 |
oc = NULL; |
474 |
120 |
break; |
475 |
|
} |
476 |
40 |
if (vr == VCF_HIT) |
477 |
40 |
break; |
478 |
0 |
assert(vr == VCF_DEFAULT); |
479 |
0 |
} |
480 |
|
|
481 |
46671 |
if (EXP_Ttl(req, oc) > req->t_req) { |
482 |
41276 |
assert(oh->refcnt > 1); |
483 |
41276 |
assert(oc->objhead == oh); |
484 |
41276 |
break; |
485 |
|
} |
486 |
|
|
487 |
5395 |
if (EXP_Ttl(NULL, oc) <= req->t_req && /* ignore req.ttl */ |
488 |
5240 |
oc->t_origin > exp_t_origin) { |
489 |
|
/* record the newest object */ |
490 |
5200 |
exp_oc = oc; |
491 |
5200 |
exp_t_origin = oc->t_origin; |
492 |
5200 |
assert(oh->refcnt > 1); |
493 |
5200 |
assert(exp_oc->objhead == oh); |
494 |
5200 |
} |
495 |
5395 |
} |
496 |
|
|
497 |
100075 |
if (req->vcf != NULL) |
498 |
240 |
(void)req->vcf->func(req, &oc, &exp_oc, 1); |
499 |
|
|
500 |
100075 |
if (oc != NULL && oc->flags & OC_F_HFP) { |
501 |
400 |
xid = VXID(ObjGetXID(wrk, oc)); |
502 |
400 |
dttl = EXP_Dttl(req, oc); |
503 |
400 |
AN(hsh_deref_objhead_unlock(wrk, &oh, HSH_RUSH_POLICY)); |
504 |
400 |
wrk->stats->cache_hitpass++; |
505 |
400 |
VSLb(req->vsl, SLT_HitPass, "%u %.6f", xid, dttl); |
506 |
400 |
return (HSH_HITPASS); |
507 |
|
} |
508 |
|
|
509 |
99675 |
if (oc != NULL) { |
510 |
40956 |
*ocp = oc; |
511 |
40956 |
oc->refcnt++; |
512 |
40956 |
if (oc->flags & OC_F_HFM) { |
513 |
1240 |
xid = VXID(ObjGetXID(wrk, oc)); |
514 |
1240 |
dttl = EXP_Dttl(req, oc); |
515 |
1240 |
*bocp = hsh_insert_busyobj(wrk, oh); |
516 |
1240 |
Lck_Unlock(&oh->mtx); |
517 |
1240 |
wrk->stats->cache_hitmiss++; |
518 |
1240 |
VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl); |
519 |
1240 |
return (HSH_HITMISS); |
520 |
|
} |
521 |
39716 |
oc->hits++; |
522 |
39716 |
boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far; |
523 |
39716 |
AN(hsh_deref_objhead_unlock(wrk, &oh, HSH_RUSH_POLICY)); |
524 |
39716 |
Req_LogHit(wrk, req, oc, boc_progress); |
525 |
39716 |
return (HSH_HIT); |
526 |
|
} |
527 |
|
|
528 |
58719 |
if (exp_oc != NULL && exp_oc->flags & OC_F_HFM) { |
529 |
|
/* |
530 |
|
* expired HFM ("grace/keep HFM") |
531 |
|
* |
532 |
|
* XXX should HFM objects actually have grace/keep ? |
533 |
|
* XXX also: why isn't *ocp = exp_oc ? |
534 |
|
*/ |
535 |
160 |
xid = VXID(ObjGetXID(wrk, exp_oc)); |
536 |
160 |
dttl = EXP_Dttl(req, exp_oc); |
537 |
160 |
*bocp = hsh_insert_busyobj(wrk, oh); |
538 |
160 |
Lck_Unlock(&oh->mtx); |
539 |
160 |
wrk->stats->cache_hitmiss++; |
540 |
160 |
VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl); |
541 |
160 |
return (HSH_HITMISS); |
542 |
|
} |
543 |
|
|
544 |
58559 |
if (exp_oc != NULL && exp_oc->boc != NULL) |
545 |
160 |
boc_progress = exp_oc->boc->fetched_so_far; |
546 |
|
else |
547 |
58399 |
boc_progress = -1; |
548 |
|
|
549 |
58559 |
if (!busy_found) { |
550 |
56539 |
*bocp = hsh_insert_busyobj(wrk, oh); |
551 |
|
|
552 |
56539 |
if (exp_oc != NULL) { |
553 |
4800 |
exp_oc->refcnt++; |
554 |
4800 |
*ocp = exp_oc; |
555 |
4800 |
if (EXP_Ttl_grace(req, exp_oc) >= req->t_req) { |
556 |
3680 |
exp_oc->hits++; |
557 |
3680 |
Lck_Unlock(&oh->mtx); |
558 |
3680 |
Req_LogHit(wrk, req, exp_oc, boc_progress); |
559 |
3680 |
return (HSH_GRACE); |
560 |
|
} |
561 |
1120 |
} |
562 |
52859 |
Lck_Unlock(&oh->mtx); |
563 |
52859 |
return (HSH_MISS); |
564 |
|
} |
565 |
|
|
566 |
2020 |
AN(busy_found); |
567 |
2020 |
if (exp_oc != NULL && EXP_Ttl_grace(req, exp_oc) >= req->t_req) { |
568 |
|
/* we do not wait on the busy object if in grace */ |
569 |
120 |
exp_oc->refcnt++; |
570 |
120 |
*ocp = exp_oc; |
571 |
120 |
exp_oc->hits++; |
572 |
120 |
AN(hsh_deref_objhead_unlock(wrk, &oh, 0)); |
573 |
120 |
Req_LogHit(wrk, req, exp_oc, boc_progress); |
574 |
120 |
return (HSH_GRACE); |
575 |
|
} |
576 |
|
|
577 |
|
/* There are one or more busy objects, wait for them */ |
578 |
1900 |
VTAILQ_INSERT_TAIL(&oh->waitinglist, req, w_list); |
579 |
|
|
580 |
1900 |
AZ(req->hash_ignore_busy); |
581 |
|
|
582 |
|
/* |
583 |
|
* The objhead reference transfers to the sess, we get it |
584 |
|
* back when the sess comes off the waiting list and |
585 |
|
* calls us again |
586 |
|
*/ |
587 |
1900 |
req->hash_objhead = oh; |
588 |
1900 |
req->wrk = NULL; |
589 |
1900 |
req->waitinglist = 1; |
590 |
|
|
591 |
1900 |
if (DO_DEBUG(DBG_WAITINGLIST)) |
592 |
560 |
VSLb(req->vsl, SLT_Debug, "on waiting list <%p>", oh); |
593 |
|
|
594 |
1900 |
Lck_Unlock(&oh->mtx); |
595 |
|
|
596 |
1900 |
wrk->stats->busy_sleep++; |
597 |
1900 |
return (HSH_BUSY); |
598 |
100635 |
} |
599 |
|
|
600 |
|
/*--------------------------------------------------------------------- |
601 |
|
* Pick the req's we are going to rush from the waiting list |
602 |
|
*/ |
603 |
|
|
604 |
|
static void |
605 |
1494 |
hsh_rush1(const struct worker *wrk, struct objhead *oh, struct rush *r, int max) |
606 |
|
{ |
607 |
|
int i; |
608 |
|
struct req *req; |
609 |
|
|
610 |
1494 |
if (max == 0) |
611 |
65 |
return; |
612 |
1429 |
if (max == HSH_RUSH_POLICY) |
613 |
1429 |
max = cache_param->rush_exponent; |
614 |
1429 |
assert(max > 0); |
615 |
|
|
616 |
1429 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
617 |
1429 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
618 |
1429 |
CHECK_OBJ_NOTNULL(r, RUSH_MAGIC); |
619 |
1429 |
VTAILQ_INIT(&r->reqs); |
620 |
1429 |
Lck_AssertHeld(&oh->mtx); |
621 |
3329 |
for (i = 0; i < max; i++) { |
622 |
3009 |
req = VTAILQ_FIRST(&oh->waitinglist); |
623 |
3009 |
if (req == NULL) |
624 |
1109 |
break; |
625 |
1900 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
626 |
1900 |
wrk->stats->busy_wakeup++; |
627 |
1900 |
AZ(req->wrk); |
628 |
1900 |
VTAILQ_REMOVE(&oh->waitinglist, req, w_list); |
629 |
1900 |
VTAILQ_INSERT_TAIL(&r->reqs, req, w_list); |
630 |
1900 |
req->waitinglist = 0; |
631 |
1900 |
} |
632 |
1494 |
} |
633 |
|
|
634 |
|
/*--------------------------------------------------------------------- |
635 |
|
* Rush req's that came from waiting list. |
636 |
|
*/ |
637 |
|
|
638 |
|
static void |
639 |
418695 |
hsh_rush2(struct worker *wrk, struct rush *r) |
640 |
|
{ |
641 |
|
struct req *req; |
642 |
|
|
643 |
418695 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
644 |
418695 |
CHECK_OBJ_NOTNULL(r, RUSH_MAGIC); |
645 |
|
|
646 |
420595 |
while (!VTAILQ_EMPTY(&r->reqs)) { |
647 |
1900 |
req = VTAILQ_FIRST(&r->reqs); |
648 |
1900 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
649 |
1900 |
VTAILQ_REMOVE(&r->reqs, req, w_list); |
650 |
1900 |
DSL(DBG_WAITINGLIST, req->vsl->wid, "off waiting list"); |
651 |
1900 |
if (req->transport->reembark != NULL) { |
652 |
|
// For ESI includes |
653 |
41 |
req->transport->reembark(wrk, req); |
654 |
41 |
} else { |
655 |
|
/* |
656 |
|
* We ignore the queue limits which apply to new |
657 |
|
* requests because if we fail to reschedule there |
658 |
|
* may be vmod_privs to cleanup and we need a proper |
659 |
|
* workerthread for that. |
660 |
|
*/ |
661 |
1859 |
AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH)); |
662 |
|
} |
663 |
|
} |
664 |
418695 |
} |
665 |
|
|
666 |
|
/*--------------------------------------------------------------------- |
667 |
|
* Purge an entire objhead |
668 |
|
*/ |
669 |
|
|
670 |
|
unsigned |
671 |
840 |
HSH_Purge(struct worker *wrk, struct objhead *oh, vtim_real ttl_now, |
672 |
|
vtim_dur ttl, vtim_dur grace, vtim_dur keep) |
673 |
|
{ |
674 |
|
struct objcore *oc, *oc_nows[2], **ocp; |
675 |
840 |
unsigned i, j, n, n_max, total = 0; |
676 |
|
int is_purge; |
677 |
|
|
678 |
840 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
679 |
840 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
680 |
|
|
681 |
840 |
is_purge = (ttl == 0 && grace == 0 && keep == 0); |
682 |
840 |
n_max = WS_ReserveLumps(wrk->aws, sizeof *ocp); |
683 |
840 |
if (n_max < 2) { |
684 |
|
/* No space on the workspace. Give it a stack buffer of 2 |
685 |
|
* elements, which is the minimum for the algorithm |
686 |
|
* below. */ |
687 |
0 |
ocp = oc_nows; |
688 |
0 |
n_max = 2; |
689 |
0 |
} else |
690 |
840 |
ocp = WS_Reservation(wrk->aws); |
691 |
840 |
AN(ocp); |
692 |
|
|
693 |
|
/* Note: This algorithm uses OC references in the list as |
694 |
|
* bookmarks, in order to know how far into the list we were when |
695 |
|
* releasing the mutex partway through and want to resume |
696 |
|
* again. This relies on the list not being reordered while we are |
697 |
|
* not holding the mutex. The only place where that happens is in |
698 |
|
* HSH_Unbusy(), where an OC_F_BUSY OC is moved first in the |
699 |
|
* list. This does not cause problems because we skip OC_F_BUSY |
700 |
|
* OCs. */ |
701 |
|
|
702 |
840 |
Lck_Lock(&oh->mtx); |
703 |
840 |
oc = VTAILQ_FIRST(&oh->objcs); |
704 |
840 |
n = 0; |
705 |
880 |
while (1) { |
706 |
5200 |
for (; n < n_max && oc != NULL; oc = VTAILQ_NEXT(oc, hsh_list)) |
707 |
|
{ |
708 |
4320 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
709 |
4320 |
assert(oc->objhead == oh); |
710 |
4320 |
if (oc->flags & OC_F_BUSY) { |
711 |
|
/* We cannot purge busy objects here, because |
712 |
|
* their owners have special rights to them, |
713 |
|
* and may nuke them without concern for the |
714 |
|
* refcount, which by definition always must |
715 |
|
* be one, so they don't check. */ |
716 |
680 |
continue; |
717 |
|
} |
718 |
3640 |
if (oc->flags & OC_F_DYING) |
719 |
0 |
continue; |
720 |
3640 |
if (is_purge) |
721 |
3240 |
oc->flags |= OC_F_DYING; |
722 |
3640 |
oc->refcnt++; |
723 |
3640 |
ocp[n++] = oc; |
724 |
3640 |
} |
725 |
|
|
726 |
880 |
Lck_Unlock(&oh->mtx); |
727 |
|
|
728 |
880 |
if (n == 0) { |
729 |
|
/* No eligible objcores found. We are finished. */ |
730 |
160 |
break; |
731 |
|
} |
732 |
|
|
733 |
720 |
j = n; |
734 |
720 |
if (oc != NULL) { |
735 |
|
/* There are more objects on the objhead that we |
736 |
|
* have not yet looked at, but no more space on |
737 |
|
* the objcore reference list. Do not process the |
738 |
|
* last one, it will be used as the bookmark into |
739 |
|
* the objcore list for the next iteration of the |
740 |
|
* outer loop. */ |
741 |
40 |
j--; |
742 |
40 |
assert(j >= 1); /* True because n_max >= 2 */ |
743 |
40 |
} |
744 |
4360 |
for (i = 0; i < j; i++) { |
745 |
3640 |
CHECK_OBJ_NOTNULL(ocp[i], OBJCORE_MAGIC); |
746 |
3640 |
if (is_purge) |
747 |
3240 |
EXP_Remove(ocp[i], NULL); |
748 |
|
else |
749 |
400 |
EXP_Reduce(ocp[i], ttl_now, ttl, grace, keep); |
750 |
3640 |
(void)HSH_DerefObjCore(wrk, &ocp[i], 0); |
751 |
3640 |
AZ(ocp[i]); |
752 |
3640 |
total++; |
753 |
3640 |
} |
754 |
|
|
755 |
720 |
if (j == n) { |
756 |
|
/* No bookmark set, that means we got to the end |
757 |
|
* of the objcore list in the previous run and are |
758 |
|
* finished. */ |
759 |
680 |
break; |
760 |
|
} |
761 |
|
|
762 |
40 |
Lck_Lock(&oh->mtx); |
763 |
|
|
764 |
|
/* Move the bookmark first and continue scanning the |
765 |
|
* objcores */ |
766 |
40 |
CHECK_OBJ_NOTNULL(ocp[j], OBJCORE_MAGIC); |
767 |
40 |
ocp[0] = ocp[j]; |
768 |
40 |
n = 1; |
769 |
40 |
oc = VTAILQ_NEXT(ocp[0], hsh_list); |
770 |
40 |
CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC); |
771 |
|
} |
772 |
|
|
773 |
840 |
WS_Release(wrk->aws, 0); |
774 |
840 |
if (is_purge) |
775 |
440 |
Pool_PurgeStat(total); |
776 |
840 |
return (total); |
777 |
|
} |
778 |
|
|
779 |
|
/*--------------------------------------------------------------------- |
780 |
|
* Fail an objcore |
781 |
|
*/ |
782 |
|
|
783 |
|
void |
784 |
2600 |
HSH_Fail(struct objcore *oc) |
785 |
|
{ |
786 |
|
struct objhead *oh; |
787 |
|
|
788 |
2600 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
789 |
2600 |
oh = oc->objhead; |
790 |
2600 |
CHECK_OBJ(oh, OBJHEAD_MAGIC); |
791 |
|
|
792 |
|
/* |
793 |
|
* We have to have either a busy bit, so that HSH_Lookup |
794 |
|
* will not consider this oc, or an object hung of the oc |
795 |
|
* so that it can consider it. |
796 |
|
*/ |
797 |
2600 |
assert((oc->flags & OC_F_BUSY) || (oc->stobj->stevedore != NULL)); |
798 |
|
|
799 |
2600 |
Lck_Lock(&oh->mtx); |
800 |
2600 |
oc->flags |= OC_F_FAILED; |
801 |
2600 |
Lck_Unlock(&oh->mtx); |
802 |
2600 |
} |
803 |
|
|
804 |
|
/*--------------------------------------------------------------------- |
805 |
|
* Mark a fetch we will not need as cancelled |
806 |
|
*/ |
807 |
|
|
808 |
|
static void |
809 |
13952 |
hsh_cancel(struct objcore *oc) |
810 |
|
{ |
811 |
|
struct objhead *oh; |
812 |
|
|
813 |
13952 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
814 |
13952 |
oh = oc->objhead; |
815 |
13952 |
CHECK_OBJ(oh, OBJHEAD_MAGIC); |
816 |
|
|
817 |
13952 |
Lck_Lock(&oh->mtx); |
818 |
13952 |
oc->flags |= OC_F_CANCEL; |
819 |
13952 |
Lck_Unlock(&oh->mtx); |
820 |
13952 |
} |
821 |
|
|
822 |
|
/*--------------------------------------------------------------------- |
823 |
|
* Cancel a fetch when the client does not need it any more |
824 |
|
*/ |
825 |
|
|
826 |
|
void |
827 |
146059 |
HSH_Cancel(struct worker *wrk, struct objcore *oc, struct boc *boc) |
828 |
|
{ |
829 |
146059 |
struct boc *bocref = NULL; |
830 |
|
|
831 |
146059 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
832 |
|
|
833 |
146059 |
if ((oc->flags & OC_F_TRANSIENT) == 0) |
834 |
91300 |
return; |
835 |
|
|
836 |
|
/* |
837 |
|
* NB: we use two distinct variables to only release the reference if |
838 |
|
* we had to acquire one. The caller-provided boc is optional. |
839 |
|
*/ |
840 |
54759 |
if (boc == NULL) |
841 |
40933 |
bocref = boc = HSH_RefBoc(oc); |
842 |
|
|
843 |
54759 |
CHECK_OBJ_ORNULL(boc, BOC_MAGIC); |
844 |
|
|
845 |
54759 |
if (oc->flags & OC_F_HFP) |
846 |
800 |
AN(oc->flags & OC_F_HFM); |
847 |
|
|
848 |
54759 |
if (boc != NULL) { |
849 |
13952 |
hsh_cancel(oc); |
850 |
13952 |
ObjWaitState(oc, BOS_FINISHED); |
851 |
13952 |
} |
852 |
|
|
853 |
54759 |
if (bocref != NULL) |
854 |
128 |
HSH_DerefBoc(wrk, oc); |
855 |
|
|
856 |
54759 |
ObjSlim(wrk, oc); |
857 |
146059 |
} |
858 |
|
|
859 |
|
/*--------------------------------------------------------------------- |
860 |
|
* Unbusy an objcore when the object is completely fetched. |
861 |
|
*/ |
862 |
|
|
863 |
|
void |
864 |
85920 |
HSH_Unbusy(struct worker *wrk, struct objcore *oc) |
865 |
|
{ |
866 |
|
struct objhead *oh; |
867 |
|
struct rush rush; |
868 |
|
|
869 |
85920 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
870 |
85920 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
871 |
85920 |
CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC); |
872 |
|
|
873 |
85920 |
oh = oc->objhead; |
874 |
85920 |
CHECK_OBJ(oh, OBJHEAD_MAGIC); |
875 |
85920 |
INIT_OBJ(&rush, RUSH_MAGIC); |
876 |
|
|
877 |
85920 |
AN(oc->stobj->stevedore); |
878 |
85920 |
AN(oc->flags & OC_F_BUSY); |
879 |
85920 |
assert(oh->refcnt > 0); |
880 |
85920 |
assert(oc->refcnt > 0); |
881 |
|
|
882 |
85920 |
if (!(oc->flags & OC_F_PRIVATE)) { |
883 |
55400 |
BAN_NewObjCore(oc); |
884 |
55400 |
AN(oc->ban); |
885 |
55400 |
} |
886 |
|
|
887 |
|
/* XXX: pretouch neighbors on oh->objcs to prevent page-on under mtx */ |
888 |
85920 |
Lck_Lock(&oh->mtx); |
889 |
85920 |
assert(oh->refcnt > 0); |
890 |
85920 |
assert(oc->refcnt > 0); |
891 |
85920 |
if (!(oc->flags & OC_F_PRIVATE)) |
892 |
55400 |
EXP_RefNewObjcore(oc); /* Takes a ref for expiry */ |
893 |
|
/* XXX: strictly speaking, we should sort in Date: order. */ |
894 |
85920 |
VTAILQ_REMOVE(&oh->objcs, oc, hsh_list); |
895 |
85920 |
VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list); |
896 |
85920 |
oc->flags &= ~OC_F_BUSY; |
897 |
85920 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) { |
898 |
1109 |
assert(oh->refcnt > 1); |
899 |
1109 |
hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY); |
900 |
1109 |
} |
901 |
85920 |
Lck_Unlock(&oh->mtx); |
902 |
85920 |
EXP_Insert(wrk, oc); /* Does nothing unless EXP_RefNewObjcore was |
903 |
|
* called */ |
904 |
85920 |
hsh_rush2(wrk, &rush); |
905 |
85920 |
} |
906 |
|
|
907 |
|
/*==================================================================== |
908 |
|
* HSH_Kill() |
909 |
|
* |
910 |
|
* It's dead Jim, kick it... |
911 |
|
*/ |
912 |
|
|
913 |
|
void |
914 |
6915 |
HSH_Kill(struct objcore *oc) |
915 |
|
{ |
916 |
|
|
917 |
6915 |
HSH_Replace(oc, NULL); |
918 |
6915 |
} |
919 |
|
|
920 |
|
void |
921 |
9795 |
HSH_Replace(struct objcore *oc, const struct objcore *new_oc) |
922 |
|
{ |
923 |
|
|
924 |
9795 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
925 |
9795 |
CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC); |
926 |
9795 |
if (new_oc != NULL) { |
927 |
2880 |
CHECK_OBJ(new_oc, OBJCORE_MAGIC); |
928 |
2880 |
assert(oc->objhead == new_oc->objhead); |
929 |
2880 |
} |
930 |
|
|
931 |
9795 |
Lck_Lock(&oc->objhead->mtx); |
932 |
9795 |
oc->flags |= OC_F_DYING; |
933 |
9795 |
Lck_Unlock(&oc->objhead->mtx); |
934 |
9795 |
EXP_Remove(oc, new_oc); |
935 |
9795 |
} |
936 |
|
|
937 |
|
/*==================================================================== |
938 |
|
* HSH_Snipe() |
939 |
|
* |
940 |
|
* If objcore is idle, gain a ref and mark it dead. |
941 |
|
*/ |
942 |
|
|
943 |
|
int |
944 |
440 |
HSH_Snipe(const struct worker *wrk, struct objcore *oc) |
945 |
|
{ |
946 |
440 |
int retval = 0; |
947 |
|
|
948 |
440 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
949 |
440 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
950 |
440 |
CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC); |
951 |
|
|
952 |
440 |
if (oc->refcnt == 1 && !Lck_Trylock(&oc->objhead->mtx)) { |
953 |
440 |
if (oc->refcnt == 1 && !(oc->flags & OC_F_DYING)) { |
954 |
440 |
oc->flags |= OC_F_DYING; |
955 |
440 |
oc->refcnt++; |
956 |
440 |
retval = 1; |
957 |
440 |
} |
958 |
440 |
Lck_Unlock(&oc->objhead->mtx); |
959 |
440 |
} |
960 |
440 |
if (retval) |
961 |
440 |
EXP_Remove(oc, NULL); |
962 |
440 |
return (retval); |
963 |
|
} |
964 |
|
|
965 |
|
|
966 |
|
/*--------------------------------------------------------------------- |
967 |
|
* Gain a reference on an objcore |
968 |
|
*/ |
969 |
|
|
970 |
|
void |
971 |
94640 |
HSH_Ref(struct objcore *oc) |
972 |
|
{ |
973 |
|
struct objhead *oh; |
974 |
|
|
975 |
94640 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
976 |
94640 |
oh = oc->objhead; |
977 |
94640 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
978 |
94640 |
Lck_Lock(&oh->mtx); |
979 |
94640 |
assert(oc->refcnt > 0); |
980 |
94640 |
oc->refcnt++; |
981 |
94640 |
Lck_Unlock(&oh->mtx); |
982 |
94640 |
} |
983 |
|
|
984 |
|
/*--------------------------------------------------------------------- |
985 |
|
* Gain a reference on the busyobj, if the objcore has one |
986 |
|
*/ |
987 |
|
|
988 |
|
struct boc * |
989 |
368386 |
HSH_RefBoc(const struct objcore *oc) |
990 |
|
{ |
991 |
|
struct objhead *oh; |
992 |
|
struct boc *boc; |
993 |
|
|
994 |
368386 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
995 |
368386 |
oh = oc->objhead; |
996 |
368386 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
997 |
368386 |
if (oc->boc == NULL) |
998 |
215231 |
return (NULL); |
999 |
153155 |
Lck_Lock(&oh->mtx); |
1000 |
153155 |
assert(oc->refcnt > 0); |
1001 |
153155 |
boc = oc->boc; |
1002 |
153155 |
CHECK_OBJ_ORNULL(boc, BOC_MAGIC); |
1003 |
153155 |
if (boc != NULL) { |
1004 |
153072 |
assert(boc->refcount > 0); |
1005 |
153072 |
if (boc->state < BOS_FINISHED) |
1006 |
151827 |
boc->refcount++; |
1007 |
|
else |
1008 |
1245 |
boc = NULL; |
1009 |
153072 |
} |
1010 |
153155 |
Lck_Unlock(&oh->mtx); |
1011 |
153155 |
return (boc); |
1012 |
368386 |
} |
1013 |
|
|
1014 |
|
void |
1015 |
265022 |
HSH_DerefBoc(struct worker *wrk, struct objcore *oc) |
1016 |
|
{ |
1017 |
|
struct boc *boc; |
1018 |
|
unsigned r; |
1019 |
|
|
1020 |
265022 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1021 |
265022 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
1022 |
265022 |
boc = oc->boc; |
1023 |
265022 |
CHECK_OBJ_NOTNULL(boc, BOC_MAGIC); |
1024 |
265022 |
Lck_Lock(&oc->objhead->mtx); |
1025 |
265022 |
assert(oc->refcnt > 0); |
1026 |
265022 |
assert(boc->refcount > 0); |
1027 |
265022 |
r = --boc->refcount; |
1028 |
265022 |
if (r == 0) |
1029 |
113240 |
oc->boc = NULL; |
1030 |
265022 |
Lck_Unlock(&oc->objhead->mtx); |
1031 |
265022 |
if (r == 0) |
1032 |
113240 |
ObjBocDone(wrk, oc, &boc); |
1033 |
265022 |
} |
1034 |
|
|
1035 |
|
/*-------------------------------------------------------------------- |
1036 |
|
* Dereference objcore |
1037 |
|
* |
1038 |
|
* Returns zero if target was destroyed. |
1039 |
|
*/ |
1040 |
|
|
1041 |
|
int |
1042 |
274269 |
HSH_DerefObjCore(struct worker *wrk, struct objcore **ocp, int rushmax) |
1043 |
|
{ |
1044 |
|
struct objcore *oc; |
1045 |
|
struct objhead *oh; |
1046 |
|
struct rush rush; |
1047 |
|
int r; |
1048 |
|
|
1049 |
274269 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1050 |
274269 |
TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC); |
1051 |
274269 |
assert(oc->refcnt > 0); |
1052 |
274269 |
INIT_OBJ(&rush, RUSH_MAGIC); |
1053 |
|
|
1054 |
274269 |
oh = oc->objhead; |
1055 |
274269 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
1056 |
|
|
1057 |
274269 |
Lck_Lock(&oh->mtx); |
1058 |
274269 |
assert(oh->refcnt > 0); |
1059 |
274269 |
r = --oc->refcnt; |
1060 |
274269 |
if (!r) |
1061 |
72960 |
VTAILQ_REMOVE(&oh->objcs, oc, hsh_list); |
1062 |
274269 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) { |
1063 |
156 |
assert(oh->refcnt > 1); |
1064 |
156 |
hsh_rush1(wrk, oh, &rush, rushmax); |
1065 |
156 |
} |
1066 |
274269 |
Lck_Unlock(&oh->mtx); |
1067 |
274269 |
hsh_rush2(wrk, &rush); |
1068 |
274269 |
if (r != 0) |
1069 |
201310 |
return (r); |
1070 |
|
|
1071 |
72959 |
AZ(oc->exp_flags); |
1072 |
|
|
1073 |
72959 |
BAN_DestroyObj(oc); |
1074 |
72959 |
AZ(oc->ban); |
1075 |
|
|
1076 |
72959 |
if (oc->stobj->stevedore != NULL) |
1077 |
69581 |
ObjFreeObj(wrk, oc); |
1078 |
72959 |
ObjDestroy(wrk, &oc); |
1079 |
|
|
1080 |
|
/* Drop our ref on the objhead */ |
1081 |
72959 |
assert(oh->refcnt > 0); |
1082 |
72959 |
(void)hsh_deref_objhead(wrk, &oh); |
1083 |
72959 |
return (0); |
1084 |
274269 |
} |
1085 |
|
|
1086 |
|
static int |
1087 |
113196 |
hsh_deref_objhead_unlock(struct worker *wrk, struct objhead **poh, int max) |
1088 |
|
{ |
1089 |
|
struct objhead *oh; |
1090 |
|
struct rush rush; |
1091 |
|
int r; |
1092 |
|
|
1093 |
113196 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1094 |
113196 |
TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC); |
1095 |
|
|
1096 |
113196 |
Lck_AssertHeld(&oh->mtx); |
1097 |
|
|
1098 |
113196 |
if (oh == private_oh) { |
1099 |
55360 |
assert(VTAILQ_EMPTY(&oh->waitinglist)); |
1100 |
55360 |
assert(oh->refcnt > 1); |
1101 |
55360 |
oh->refcnt--; |
1102 |
55360 |
Lck_Unlock(&oh->mtx); |
1103 |
55360 |
return (1); |
1104 |
|
} |
1105 |
|
|
1106 |
57836 |
INIT_OBJ(&rush, RUSH_MAGIC); |
1107 |
57836 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) { |
1108 |
229 |
assert(oh->refcnt > 1); |
1109 |
229 |
hsh_rush1(wrk, oh, &rush, max); |
1110 |
229 |
} |
1111 |
|
|
1112 |
57836 |
if (oh->refcnt == 1) |
1113 |
7018 |
assert(VTAILQ_EMPTY(&oh->waitinglist)); |
1114 |
|
|
1115 |
57836 |
assert(oh->refcnt > 0); |
1116 |
57836 |
r = hash->deref(wrk, oh); /* Unlocks oh->mtx */ |
1117 |
57836 |
hsh_rush2(wrk, &rush); |
1118 |
57836 |
return (r); |
1119 |
113196 |
} |
1120 |
|
|
1121 |
|
static int |
1122 |
72957 |
hsh_deref_objhead(struct worker *wrk, struct objhead **poh) |
1123 |
|
{ |
1124 |
|
struct objhead *oh; |
1125 |
|
|
1126 |
72957 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1127 |
72957 |
TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC); |
1128 |
|
|
1129 |
72957 |
Lck_Lock(&oh->mtx); |
1130 |
72957 |
return (hsh_deref_objhead_unlock(wrk, &oh, 0)); |
1131 |
|
} |
1132 |
|
|
1133 |
|
void |
1134 |
36670 |
HSH_Init(const struct hash_slinger *slinger) |
1135 |
|
{ |
1136 |
|
|
1137 |
36670 |
assert(DIGEST_LEN == VSHA256_LEN); /* avoid #include pollution */ |
1138 |
36670 |
hash = slinger; |
1139 |
36670 |
if (hash->start != NULL) |
1140 |
36670 |
hash->start(); |
1141 |
36670 |
private_oh = hsh_newobjhead(); |
1142 |
36670 |
private_oh->refcnt = 1; |
1143 |
36670 |
} |