| | varnish-cache/bin/varnishd/cache/cache_expire.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2011 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 |
|
* LRU and object timer handling. |
| 31 |
|
* |
| 32 |
|
*/ |
| 33 |
|
|
| 34 |
|
#include "config.h" |
| 35 |
|
|
| 36 |
|
#include <stdlib.h> |
| 37 |
|
|
| 38 |
|
#include "cache_varnishd.h" |
| 39 |
|
#include "cache_objhead.h" |
| 40 |
|
|
| 41 |
|
#include "vbh.h" |
| 42 |
|
#include "vtim.h" |
| 43 |
|
|
| 44 |
|
struct exp_priv { |
| 45 |
|
unsigned magic; |
| 46 |
|
#define EXP_PRIV_MAGIC 0x9db22482 |
| 47 |
|
/* shared */ |
| 48 |
|
struct lock mtx; |
| 49 |
|
VSTAILQ_HEAD(,objcore) inbox; |
| 50 |
|
pthread_cond_t condvar; |
| 51 |
|
|
| 52 |
|
/* owned by exp thread */ |
| 53 |
|
struct worker *wrk; |
| 54 |
|
struct vsl_log vsl; |
| 55 |
|
struct vbh *heap; |
| 56 |
|
pthread_t thread; |
| 57 |
|
}; |
| 58 |
|
|
| 59 |
|
static struct exp_priv *exphdl; |
| 60 |
|
static int exp_shutdown = 0; |
| 61 |
|
|
| 62 |
|
/*--------------------------------------------------------------------- |
| 63 |
|
* Calculate the point in time when an object will become stale, taking |
| 64 |
|
* req.max_age into account, if available |
| 65 |
|
*/ |
| 66 |
|
|
| 67 |
|
vtim_real |
| 68 |
75573 |
EXP_Ttl(const struct req *req, const struct objcore *oc) |
| 69 |
|
{ |
| 70 |
|
vtim_dur r; |
| 71 |
|
|
| 72 |
75573 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 73 |
|
|
| 74 |
75573 |
r = oc->ttl; |
| 75 |
75573 |
if (req != NULL && req->d_ttl > 0. && req->d_ttl < r) |
| 76 |
135 |
r = req->d_ttl; |
| 77 |
75573 |
return (oc->t_origin + r); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
/*-------------------------------------------------------------------- |
| 81 |
|
* Calculate an object's effective ttl+grace time, taking req.grace into |
| 82 |
|
* account if it is available. |
| 83 |
|
*/ |
| 84 |
|
|
| 85 |
|
vtim_real |
| 86 |
5560 |
EXP_Ttl_grace(const struct req *req, const struct objcore *oc) |
| 87 |
|
{ |
| 88 |
|
vtim_dur g; |
| 89 |
|
|
| 90 |
5560 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 91 |
|
|
| 92 |
5560 |
g = oc->grace; |
| 93 |
5560 |
if (req != NULL && req->d_grace >= 0. && req->d_grace < g) |
| 94 |
80 |
g = req->d_grace; |
| 95 |
5560 |
return (EXP_Ttl(req, oc) + g); |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
/*-------------------------------------------------------------------- |
| 99 |
|
* Post an objcore to the exp_thread's inbox. |
| 100 |
|
*/ |
| 101 |
|
|
| 102 |
|
static void |
| 103 |
69075 |
exp_mail_it(struct objcore *oc, uint8_t cmds) |
| 104 |
|
{ |
| 105 |
69075 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 106 |
69075 |
assert(oc->refcnt > 0); |
| 107 |
69075 |
AZ(cmds & OC_EF_REFD); |
| 108 |
|
|
| 109 |
69075 |
Lck_AssertHeld(&exphdl->mtx); |
| 110 |
|
|
| 111 |
69075 |
if (oc->exp_flags & OC_EF_REFD) { |
| 112 |
69075 |
if (!(oc->exp_flags & OC_EF_POSTED)) { |
| 113 |
69063 |
if (cmds & OC_EF_REMOVE) |
| 114 |
10143 |
VSTAILQ_INSERT_HEAD(&exphdl->inbox, |
| 115 |
|
oc, exp_list); |
| 116 |
|
else |
| 117 |
58920 |
VSTAILQ_INSERT_TAIL(&exphdl->inbox, |
| 118 |
|
oc, exp_list); |
| 119 |
69063 |
VSC_C_main->exp_mailed++; |
| 120 |
69063 |
} |
| 121 |
69075 |
oc->exp_flags |= cmds | OC_EF_POSTED; |
| 122 |
69075 |
PTOK(pthread_cond_signal(&exphdl->condvar)); |
| 123 |
69075 |
} |
| 124 |
69075 |
} |
| 125 |
|
|
| 126 |
|
/*-------------------------------------------------------------------- |
| 127 |
|
* Setup a new ObjCore for control by expire. Should be called with the |
| 128 |
|
* ObjHead locked by HSH_Unbusy(/HSH_Insert) (in private access). |
| 129 |
|
*/ |
| 130 |
|
|
| 131 |
|
void |
| 132 |
58680 |
EXP_RefNewObjcore(struct objcore *oc) |
| 133 |
|
{ |
| 134 |
58680 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 135 |
|
|
| 136 |
58680 |
Lck_AssertHeld(&oc->objhead->mtx); |
| 137 |
|
|
| 138 |
58680 |
AZ(oc->exp_flags); |
| 139 |
58680 |
assert(oc->refcnt >= 1); |
| 140 |
58680 |
oc->refcnt++; |
| 141 |
58680 |
oc->exp_flags |= OC_EF_REFD | OC_EF_NEW; |
| 142 |
58680 |
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
/*-------------------------------------------------------------------- |
| 147 |
|
* Call EXP's attention to an oc |
| 148 |
|
*/ |
| 149 |
|
|
| 150 |
|
void |
| 151 |
17760 |
EXP_Remove(struct objcore *oc, const struct objcore *new_oc) |
| 152 |
|
{ |
| 153 |
|
|
| 154 |
17760 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 155 |
17760 |
CHECK_OBJ_ORNULL(new_oc, OBJCORE_MAGIC); |
| 156 |
|
|
| 157 |
17760 |
if (oc->exp_flags & OC_EF_REFD) { |
| 158 |
10155 |
Lck_Lock(&exphdl->mtx); |
| 159 |
10155 |
if (new_oc != NULL) |
| 160 |
2995 |
VSC_C_main->n_superseded++; |
| 161 |
10155 |
if (oc->exp_flags & OC_EF_NEW) { |
| 162 |
|
/* EXP_Insert has not been called for this object |
| 163 |
|
* yet. Mark it for removal, and EXP_Insert will |
| 164 |
|
* clean up once it is called. */ |
| 165 |
0 |
AZ(oc->exp_flags & OC_EF_POSTED); |
| 166 |
0 |
oc->exp_flags |= OC_EF_REMOVE; |
| 167 |
0 |
} else |
| 168 |
10155 |
exp_mail_it(oc, OC_EF_REMOVE); |
| 169 |
10155 |
Lck_Unlock(&exphdl->mtx); |
| 170 |
10155 |
} |
| 171 |
17760 |
} |
| 172 |
|
|
| 173 |
|
/*-------------------------------------------------------------------- |
| 174 |
|
* Insert new object. |
| 175 |
|
* |
| 176 |
|
* Caller got a oc->refcnt for us. |
| 177 |
|
*/ |
| 178 |
|
|
| 179 |
|
void |
| 180 |
58680 |
EXP_Insert(struct worker *wrk, struct objcore *oc) |
| 181 |
|
{ |
| 182 |
58680 |
unsigned remove_race = 0; |
| 183 |
|
struct objcore *tmpoc; |
| 184 |
|
|
| 185 |
58680 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 186 |
58680 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 187 |
|
|
| 188 |
58680 |
AZ(oc->flags & OC_F_BUSY); |
| 189 |
|
|
| 190 |
58680 |
if (!(oc->exp_flags & OC_EF_REFD)) |
| 191 |
0 |
return; |
| 192 |
|
|
| 193 |
|
/* One ref held by the caller, and one that will be owned by |
| 194 |
|
* expiry. */ |
| 195 |
58680 |
assert(oc->refcnt >= 2); |
| 196 |
|
|
| 197 |
58680 |
ObjSendEvent(wrk, oc, OEV_INSERT); |
| 198 |
|
|
| 199 |
58680 |
Lck_Lock(&exphdl->mtx); |
| 200 |
58680 |
AN(oc->exp_flags & OC_EF_NEW); |
| 201 |
58680 |
oc->exp_flags &= ~OC_EF_NEW; |
| 202 |
58680 |
AZ(oc->exp_flags & (OC_EF_INSERT | OC_EF_MOVE | OC_EF_POSTED)); |
| 203 |
58680 |
if (oc->exp_flags & OC_EF_REMOVE) { |
| 204 |
|
/* We raced some other thread executing EXP_Remove */ |
| 205 |
0 |
remove_race = 1; |
| 206 |
0 |
oc->exp_flags &= ~(OC_EF_REFD | OC_EF_REMOVE); |
| 207 |
0 |
} else |
| 208 |
58680 |
exp_mail_it(oc, OC_EF_INSERT | OC_EF_MOVE); |
| 209 |
58680 |
Lck_Unlock(&exphdl->mtx); |
| 210 |
|
|
| 211 |
58680 |
if (remove_race) { |
| 212 |
0 |
ObjSendEvent(wrk, oc, OEV_EXPIRE); |
| 213 |
0 |
tmpoc = oc; |
| 214 |
0 |
assert(oc->refcnt >= 2); /* Silence coverity */ |
| 215 |
0 |
(void)HSH_DerefObjCore(wrk, &oc); |
| 216 |
0 |
AZ(oc); |
| 217 |
0 |
assert(tmpoc->refcnt >= 1); /* Silence coverity */ |
| 218 |
0 |
} |
| 219 |
58680 |
} |
| 220 |
|
|
| 221 |
|
/*-------------------------------------------------------------------- |
| 222 |
|
* Reduce object timers |
| 223 |
|
*/ |
| 224 |
|
|
| 225 |
|
void |
| 226 |
400 |
EXP_Reduce(struct objcore *oc, vtim_real now, |
| 227 |
|
vtim_dur ttl, vtim_dur grace, vtim_dur keep) |
| 228 |
|
{ |
| 229 |
|
|
| 230 |
400 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 231 |
400 |
assert(oc->refcnt > 0); |
| 232 |
|
|
| 233 |
400 |
if (!isnan(ttl) && now + ttl - oc->t_origin >= oc->ttl) |
| 234 |
240 |
ttl = NAN; |
| 235 |
400 |
if (!isnan(grace) && grace >= oc->grace) |
| 236 |
80 |
grace = NAN; |
| 237 |
400 |
if (!isnan(keep) && keep >= oc->keep) |
| 238 |
40 |
keep = NAN; |
| 239 |
|
|
| 240 |
400 |
EXP_Rearm(oc, now, ttl, grace, keep); |
| 241 |
400 |
} |
| 242 |
|
|
| 243 |
|
/*-------------------------------------------------------------------- |
| 244 |
|
* We have changed one or more of the object timers, tell the exp_thread |
| 245 |
|
* |
| 246 |
|
*/ |
| 247 |
|
|
| 248 |
|
void |
| 249 |
400 |
EXP_Rearm(struct objcore *oc, vtim_real now, |
| 250 |
|
vtim_dur ttl, vtim_dur grace, vtim_dur keep) |
| 251 |
|
{ |
| 252 |
|
vtim_real when; |
| 253 |
|
|
| 254 |
400 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 255 |
400 |
assert(oc->refcnt > 0); |
| 256 |
|
|
| 257 |
400 |
if (!(oc->exp_flags & OC_EF_REFD)) |
| 258 |
0 |
return; |
| 259 |
|
|
| 260 |
400 |
if (!isnan(ttl)) |
| 261 |
160 |
oc->ttl = now + ttl - oc->t_origin; |
| 262 |
400 |
if (!isnan(grace)) |
| 263 |
80 |
oc->grace = grace; |
| 264 |
400 |
if (!isnan(keep)) |
| 265 |
80 |
oc->keep = keep; |
| 266 |
|
|
| 267 |
400 |
when = EXP_WHEN(oc); |
| 268 |
|
|
| 269 |
800 |
VSL(SLT_ExpKill, NO_VXID, "EXP_Rearm p=%p E=%.6f e=%.6f f=0x%x", oc, |
| 270 |
400 |
oc->timer_when, when, oc->flags); |
| 271 |
|
|
| 272 |
400 |
if (when < oc->t_origin || when < oc->timer_when) { |
| 273 |
240 |
Lck_Lock(&exphdl->mtx); |
| 274 |
240 |
if (oc->exp_flags & OC_EF_NEW) { |
| 275 |
|
/* EXP_Insert has not been called yet, do nothing |
| 276 |
|
* as the initial insert will execute the move |
| 277 |
|
* operation. */ |
| 278 |
0 |
} else |
| 279 |
240 |
exp_mail_it(oc, OC_EF_MOVE); |
| 280 |
240 |
Lck_Unlock(&exphdl->mtx); |
| 281 |
240 |
} |
| 282 |
400 |
} |
| 283 |
|
|
| 284 |
|
/*-------------------------------------------------------------------- |
| 285 |
|
* Handle stuff in the inbox |
| 286 |
|
*/ |
| 287 |
|
|
| 288 |
|
static void |
| 289 |
69063 |
exp_inbox(struct exp_priv *ep, struct objcore *oc, unsigned flags, vtim_real now) |
| 290 |
|
{ |
| 291 |
|
|
| 292 |
69063 |
CHECK_OBJ_NOTNULL(ep, EXP_PRIV_MAGIC); |
| 293 |
69063 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 294 |
69063 |
assert(oc->refcnt > 0); |
| 295 |
|
|
| 296 |
138126 |
VSLb(&ep->vsl, SLT_ExpKill, "EXP_Inbox flg=%x p=%p e=%.6f f=0x%x", |
| 297 |
69063 |
flags, oc, oc->timer_when, oc->flags); |
| 298 |
|
|
| 299 |
69063 |
if (flags & OC_EF_REMOVE) { |
| 300 |
10143 |
if (!(flags & OC_EF_INSERT)) { |
| 301 |
10143 |
assert(oc->timer_idx != VBH_NOIDX); |
| 302 |
10143 |
VBH_delete(ep->heap, oc->timer_idx); |
| 303 |
10143 |
} |
| 304 |
10143 |
assert(oc->timer_idx == VBH_NOIDX); |
| 305 |
10143 |
assert(oc->refcnt > 0); |
| 306 |
10143 |
AZ(oc->exp_flags); |
| 307 |
20286 |
VSLb(&ep->vsl, SLT_ExpKill, "EXP_Removed x=%ju t=%.0f h=%jd", |
| 308 |
10143 |
VXID(ObjGetXID(ep->wrk, oc)), EXP_Ttl(NULL, oc) - now, |
| 309 |
10143 |
(intmax_t)oc->hits); |
| 310 |
10143 |
ObjSendEvent(ep->wrk, oc, OEV_EXPIRE); |
| 311 |
10143 |
(void)HSH_DerefObjCore(ep->wrk, &oc); |
| 312 |
10143 |
return; |
| 313 |
|
} |
| 314 |
|
|
| 315 |
58920 |
if (flags & OC_EF_MOVE) { |
| 316 |
58920 |
oc->timer_when = EXP_WHEN(oc); |
| 317 |
58920 |
ObjSendEvent(ep->wrk, oc, OEV_TTLCHG); |
| 318 |
58920 |
} |
| 319 |
|
|
| 320 |
117840 |
VSLb(&ep->vsl, SLT_ExpKill, "EXP_When p=%p e=%.6f f=0x%x", oc, |
| 321 |
58920 |
oc->timer_when, flags); |
| 322 |
|
|
| 323 |
|
/* |
| 324 |
|
* XXX: There are some pathological cases here, were we |
| 325 |
|
* XXX: insert or move an expired object, only to find out |
| 326 |
|
* XXX: the next moment and rip them out again. |
| 327 |
|
*/ |
| 328 |
|
|
| 329 |
58920 |
if (flags & OC_EF_INSERT) { |
| 330 |
58680 |
assert(oc->timer_idx == VBH_NOIDX); |
| 331 |
58680 |
VBH_insert(exphdl->heap, oc); |
| 332 |
58680 |
assert(oc->timer_idx != VBH_NOIDX); |
| 333 |
58920 |
} else if (flags & OC_EF_MOVE) { |
| 334 |
240 |
assert(oc->timer_idx != VBH_NOIDX); |
| 335 |
240 |
VBH_reorder(exphdl->heap, oc->timer_idx); |
| 336 |
240 |
assert(oc->timer_idx != VBH_NOIDX); |
| 337 |
240 |
} else { |
| 338 |
0 |
WRONG("Objcore state wrong in inbox"); |
| 339 |
|
} |
| 340 |
69063 |
} |
| 341 |
|
|
| 342 |
|
/*-------------------------------------------------------------------- |
| 343 |
|
* Expire stuff from the binheap |
| 344 |
|
*/ |
| 345 |
|
|
| 346 |
|
static vtim_real |
| 347 |
212995 |
exp_expire(struct exp_priv *ep, vtim_real now) |
| 348 |
|
{ |
| 349 |
|
struct objcore *oc; |
| 350 |
|
|
| 351 |
212995 |
CHECK_OBJ_NOTNULL(ep, EXP_PRIV_MAGIC); |
| 352 |
|
|
| 353 |
212995 |
oc = VBH_root(ep->heap); |
| 354 |
212995 |
if (oc == NULL) |
| 355 |
88461 |
return (now + 355. / 113.); |
| 356 |
249068 |
VSLb(&ep->vsl, SLT_ExpKill, "EXP_Inspect p=%p e=%.6f f=0x%x", oc, |
| 357 |
124534 |
oc->timer_when - now, oc->flags); |
| 358 |
|
|
| 359 |
124534 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
| 360 |
|
|
| 361 |
|
/* Ready ? */ |
| 362 |
124534 |
if (oc->timer_when > now) |
| 363 |
119294 |
return (oc->timer_when); |
| 364 |
|
|
| 365 |
5240 |
VSC_C_main->n_expired++; |
| 366 |
|
|
| 367 |
5240 |
Lck_Lock(&ep->mtx); |
| 368 |
5240 |
if (oc->exp_flags & OC_EF_POSTED) { |
| 369 |
0 |
oc->exp_flags |= OC_EF_REMOVE; |
| 370 |
0 |
oc = NULL; |
| 371 |
0 |
} else { |
| 372 |
5240 |
oc->exp_flags &= ~OC_EF_REFD; |
| 373 |
|
} |
| 374 |
5240 |
Lck_Unlock(&ep->mtx); |
| 375 |
5240 |
if (oc != NULL) { |
| 376 |
5240 |
if (!(oc->flags & OC_F_DYING)) |
| 377 |
5240 |
HSH_Kill(oc); |
| 378 |
|
|
| 379 |
|
/* Remove from binheap */ |
| 380 |
5240 |
assert(oc->timer_idx != VBH_NOIDX); |
| 381 |
5240 |
VBH_delete(ep->heap, oc->timer_idx); |
| 382 |
5240 |
assert(oc->timer_idx == VBH_NOIDX); |
| 383 |
|
|
| 384 |
5240 |
CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC); |
| 385 |
10480 |
VSLb(&ep->vsl, SLT_ExpKill, "EXP_Expired x=%ju t=%.0f h=%jd", |
| 386 |
5240 |
VXID(ObjGetXID(ep->wrk, oc)), EXP_Ttl(NULL, oc) - now, |
| 387 |
5240 |
(intmax_t)oc->hits); |
| 388 |
5240 |
ObjSendEvent(ep->wrk, oc, OEV_EXPIRE); |
| 389 |
5240 |
(void)HSH_DerefObjCore(ep->wrk, &oc); |
| 390 |
5240 |
} |
| 391 |
5240 |
return (0); |
| 392 |
212995 |
} |
| 393 |
|
|
| 394 |
|
/*-------------------------------------------------------------------- |
| 395 |
|
* This thread monitors the root of the binary heap and whenever an |
| 396 |
|
* object expires, accounting also for graceability, it is killed. |
| 397 |
|
*/ |
| 398 |
|
|
| 399 |
|
static int v_matchproto_(vbh_cmp_t) |
| 400 |
50792 |
object_cmp(void *priv, const void *a, const void *b) |
| 401 |
|
{ |
| 402 |
|
const struct objcore *aa, *bb; |
| 403 |
|
|
| 404 |
50792 |
(void)priv; |
| 405 |
50792 |
CAST_OBJ_NOTNULL(aa, a, OBJCORE_MAGIC); |
| 406 |
50792 |
CAST_OBJ_NOTNULL(bb, b, OBJCORE_MAGIC); |
| 407 |
50792 |
return (aa->timer_when < bb->timer_when); |
| 408 |
|
} |
| 409 |
|
|
| 410 |
|
static void v_matchproto_(vbh_update_t) |
| 411 |
109910 |
object_update(void *priv, void *p, unsigned u) |
| 412 |
|
{ |
| 413 |
|
struct objcore *oc; |
| 414 |
|
|
| 415 |
109910 |
(void)priv; |
| 416 |
109910 |
CAST_OBJ_NOTNULL(oc, p, OBJCORE_MAGIC); |
| 417 |
109910 |
oc->timer_idx = u; |
| 418 |
109910 |
} |
| 419 |
|
|
| 420 |
|
static void * v_matchproto_(bgthread_t) |
| 421 |
38016 |
exp_thread(struct worker *wrk, void *priv) |
| 422 |
|
{ |
| 423 |
|
struct objcore *oc; |
| 424 |
38016 |
vtim_real t = 0, tnext = 0; |
| 425 |
|
struct exp_priv *ep; |
| 426 |
38016 |
unsigned flags = 0; |
| 427 |
|
|
| 428 |
38016 |
CAST_OBJ_NOTNULL(ep, priv, EXP_PRIV_MAGIC); |
| 429 |
38016 |
ep->wrk = wrk; |
| 430 |
38016 |
VSL_Setup(&ep->vsl, NULL, 0); |
| 431 |
38016 |
AZ(wrk->vsl); |
| 432 |
38016 |
wrk->vsl = &ep->vsl; |
| 433 |
38016 |
ep->heap = VBH_new(NULL, object_cmp, object_update); |
| 434 |
38016 |
AN(ep->heap); |
| 435 |
320074 |
while (exp_shutdown == 0) { |
| 436 |
|
|
| 437 |
282058 |
Lck_Lock(&ep->mtx); |
| 438 |
282058 |
oc = VSTAILQ_FIRST(&ep->inbox); |
| 439 |
282058 |
CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC); |
| 440 |
282058 |
if (oc != NULL) { |
| 441 |
69063 |
assert(oc->refcnt >= 1); |
| 442 |
69063 |
assert(oc->exp_flags & OC_EF_POSTED); |
| 443 |
69063 |
VSTAILQ_REMOVE(&ep->inbox, oc, objcore, exp_list); |
| 444 |
69063 |
VSC_C_main->exp_received++; |
| 445 |
69063 |
tnext = 0; |
| 446 |
69063 |
flags = oc->exp_flags; |
| 447 |
69063 |
if (flags & OC_EF_REMOVE) |
| 448 |
10143 |
oc->exp_flags = 0; |
| 449 |
|
else |
| 450 |
58920 |
oc->exp_flags &= OC_EF_REFD; |
| 451 |
282058 |
} else if (tnext > t) { |
| 452 |
106161 |
VSL_Flush(&ep->vsl, 0); |
| 453 |
106161 |
Pool_Sumstat(wrk); |
| 454 |
106161 |
(void)Lck_CondWaitUntil(&ep->condvar, &ep->mtx, tnext); |
| 455 |
106161 |
} |
| 456 |
282058 |
Lck_Unlock(&ep->mtx); |
| 457 |
|
|
| 458 |
282058 |
t = VTIM_real(); |
| 459 |
|
|
| 460 |
282058 |
if (oc != NULL) |
| 461 |
69063 |
exp_inbox(ep, oc, flags, t); |
| 462 |
|
else |
| 463 |
212995 |
tnext = exp_expire(ep, t); |
| 464 |
|
} |
| 465 |
38016 |
wrk->vsl = NULL; |
| 466 |
38016 |
return (NULL); |
| 467 |
|
} |
| 468 |
|
|
| 469 |
|
/*--------------------------------------------------------------------*/ |
| 470 |
|
|
| 471 |
|
void |
| 472 |
38016 |
EXP_Init(void) |
| 473 |
|
{ |
| 474 |
|
struct exp_priv *ep; |
| 475 |
|
pthread_t pt; |
| 476 |
|
|
| 477 |
38016 |
ALLOC_OBJ(ep, EXP_PRIV_MAGIC); |
| 478 |
38016 |
AN(ep); |
| 479 |
|
|
| 480 |
38016 |
Lck_New(&ep->mtx, lck_exp); |
| 481 |
38016 |
PTOK(pthread_cond_init(&ep->condvar, NULL)); |
| 482 |
38016 |
VSTAILQ_INIT(&ep->inbox); |
| 483 |
38016 |
WRK_BgThread(&pt, "cache-exp", exp_thread, ep); |
| 484 |
38016 |
ep->thread = pt; |
| 485 |
38016 |
exphdl = ep; |
| 486 |
38016 |
} |
| 487 |
|
|
| 488 |
|
void |
| 489 |
37520 |
EXP_Shutdown(void) |
| 490 |
|
{ |
| 491 |
37520 |
struct exp_priv *ep = exphdl; |
| 492 |
|
void *status; |
| 493 |
|
|
| 494 |
37520 |
Lck_Lock(&ep->mtx); |
| 495 |
37520 |
exp_shutdown = 1; |
| 496 |
37520 |
PTOK(pthread_cond_signal(&ep->condvar)); |
| 497 |
37520 |
Lck_Unlock(&ep->mtx); |
| 498 |
|
|
| 499 |
37520 |
AN(ep->thread); |
| 500 |
37520 |
PTOK(pthread_join(ep->thread, &status)); |
| 501 |
37520 |
AZ(status); |
| 502 |
37520 |
memset(&ep->thread, 0, sizeof ep->thread); |
| 503 |
|
|
| 504 |
|
/* XXX could cleanup more - not worth it for now */ |
| 505 |
37520 |
} |