include/punk/punk_cachefront.h

filter: Statementstmt 94.3% 464 / 492 Branchbran 65.3% 357 / 546 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.0% 821 / 1038 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/abi.xs ↑ Summary include/punk/punk_mpstream.h »
1 #ifndef PUNK_CACHEFRONT_H
2 #define PUNK_CACHEFRONT_H
3  
4 #include <string.h>
5 #ifndef WIN32
6 # include <sys/select.h>
7 #endif
8  
9 /* A cached undef is a VALUE, not a miss.
10 *
11 * If compute() stores nothing when the code returns undef, an expensive
12 * lookup that legitimately finds nothing is repeated on every single request
13 * - which is exactly the traffic pattern a cache exists to stop, and the case
14 * people hit when they cache "does this user exist".
15 *
16 * So undef is stored as a marker no caller can produce by accident: a NUL, a
17 * byte no text carries, and a tag. get() turns it back into undef and nothing
18 * above can tell the difference. */
19 #define PKC_UNDEF "\0PunkCache\0undef\0"
20 #define PKC_UNDEF_LEN (sizeof(PKC_UNDEF) - 1)
21  
22 /* The payload that means "drop everything", distinct from any real key by the
23 * same trick. */
24 #define PKC_CLEAR "\0PunkCache\0clear\0"
25 #define PKC_CLEAR_LEN (sizeof(PKC_CLEAR) - 1)
26  
27 #define PKC_KEY_MAX 4096
28 #define PKC_LOCK_WAIT 5.0 /* the default single-flight budget */
29  
30 /* Which store this is, decided once at construction. GENERIC means "reach it
31 * through call_method", which is every backend but the two shipped ones. */
32 #define PKC_K_GENERIC 0
33 #define PKC_K_MEMORY 1
34 #define PKC_K_FILE 2
35  
36 /* The single-flight seam a backend offers, probed once at construction rather
37 * than on every compute: `can` three times per cached miss is three method
38 * resolutions to answer a question whose answer cannot change for a store
39 * that is already built. */
40 #define PKC_CAN_LOCK 1
41 #define PKC_CAN_UNLOCK 2
42 #define PKC_CAN_LOCKWAIT 4
43 #define PKC_CAN_SWEEP 8
44  
45 /* THE MEMORY TIER.
46 *
47 * A file hit is 7.2us and 6.2us of that is the `open` syscall, measured.
48 * Nothing around the syscall is worth tuning, so the only way a hit gets
49 * faster is not to open the file, and a
50 * per-worker memory cache in front does exactly that.
51 *
52 * It is the byte-budgeted LRU from punk_cache.h, held HERE rather than
53 * wrapped in a backend of its own, so that any store gets it - including a
54 * Redis one nobody has written yet - and so that the rule below can live next
55 * to the is_shared it depends on.
56 *
57 * WHAT IT COSTS, WHICH COMES FIRST. A tier turns a shared store from
58 * instantly consistent across the pool into eventually consistent, bounded by
59 * tier_ttl and the bus. That is why it is opt-in, and why `shared` below
60 * means "shared AND untiered": a tiered store must publish and subscribe like
61 * any unshared one, or worker A writes and workers B through H go on serving
62 * their own copies.
63 *
64 * TIER_TTL IS NOT OPTIONAL, and not only for staleness. punk_cachefile.h
65 * evicts coldest-first by st_atime, and a tier absorbs exactly the reads that
66 * would keep a hot key's atime fresh - so without a ceiling the sweep evicts
67 * the hottest entries FIRST. A ceiling means a hot key is re-read from disk
68 * at least that often, which keeps the eviction signal alive and bounds
69 * staleness when the bus drops a message. One ceiling, two jobs. */
70 #define PKC_TIER_TTL 5.0 /* seconds, the default ceiling */
71 /* One value may not take more than this share of the tier. punk_cache_set
72 * refuses only what cannot fit the WHOLE budget, so without this a 50MB page
73 * is admitted to a 64M tier and flushes the entire hot set on its way in. */
74 #define PKC_TIER_MAX_SHARE 8
75  
76 typedef struct {
77 SV *backend; /* the store, owned */
78 SV *name; /* the invalidation topic, owned; NULL if none */
79 SV *origin; /* this process's token, owned, minted lazily */
80 IV origin_pid; /* the pid it was minted in */
81 int kind;
82 int shared; /* is_shared AND untiered - see above */
83 int caps;
84 UV sent, received;
85  
86 punk_cache *tier; /* the memory tier, or NULL */
87 double tier_ttl; /* the ceiling, seconds */
88 size_t tier_max_entry;
89 } punk_cachefront;
90  
91 static UV PKC_ORIGIN_SEQ = 0;
92  
93 619 static punk_cachefront *pkc_of(pTHX_ SV *self) {
94 619 ▶ if (!(self && SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self))))
Branch: 4 branches
BranchTrueFalseTotal
if (!(self && SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self)))) 619 0 619
if (!(self && SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self)))) 619 0 619
if (!(self && SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self)))) 619 0 619
if (!(self && SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self)))) 0 619 619
95 0 croak("Punk::Cache: not a cache handle");
96 619 return INT2PTR(punk_cachefront *, SvIV(SvRV(self)));
97 }
98  
99 /* ---- sizes ---------------------------------------------------------------
100 *
101 * 512M / 2G / 1024 - because everybody writes it the first way, and a cache
102 * configured in bare bytes by hand is a cache configured wrong by an order of
103 * magnitude. 0 when it is not a size at all, which is a boot croak rather
104 * than a silently enormous or silently tiny budget. */
105 100 static int pkc_bytes(pTHX_ SV *v, NV *out) {
106 STRLEN len;
107 const char *s, *p, *end;
108 100 NV n = 0.0, mult = 1.0, frac = 0.1;
109 100 int digits = 0;
110  
111 100 ▶ if (!(v && SvOK(v))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(v && SvOK(v))) return 0; 100 0 100
if (!(v && SvOK(v))) return 0; 0 100 100
112 100 s = SvPV_const(v, len);
113 100 p = s; end = s + len;
114  
115 /* a plain byte count, taken as it stands */
116 275 ▶ while (p < end && isDIGIT(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isDIGIT(*p)) p++; 264 11 275
while (p < end && isDIGIT(*p)) p++; 175 89 264
117 100 ▶ if (p > s && p == end) {
Branch: 2 branches
BranchTrueFalseTotal
if (p > s && p == end) { 96 4 100
if (p > s && p == end) { 11 85 96
118 81 ▶ for (p = s, n = 0.0; p < end; p++) n = n * 10.0 + (NV)(*p - '0');
Branch: 1 branch
BranchTrueFalseTotal
for (p = s, n = 0.0; p < end; p++) n = n * 10.0 + (NV)(*p - '0'); 70 11 81
119 11 *out = n;
120 11 return 1;
121 }
122  
123 89 p = s;
124 89 ▶ while (p < end && isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE(*p)) p++; 89 0 89
while (p < end && isSPACE(*p)) p++; 0 89 89
125 194 ▶ while (p < end && isDIGIT(*p)) { n = n * 10.0 + (NV)(*p - '0'); p++; digits++; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isDIGIT(*p)) { n = n * 10.0 + (NV)(*p - '0'); p++; digits++; } 194 0 194
while (p < end && isDIGIT(*p)) { n = n * 10.0 + (NV)(*p - '0'); p++; digits++; } 105 89 194
126 89 ▶ if (!digits) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!digits) return 0; 4 85 89
127 85 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 85 0 85
if (p < end && *p == '.') { 0 85 85
128 0 p++;
129 0 digits = 0;
130 0 ▶ while (p < end && isDIGIT(*p)) { n += frac * (NV)(*p - '0'); frac /= 10.0; p++; digits++; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isDIGIT(*p)) { n += frac * (NV)(*p - '0'); frac /= 10.0; p++; digits++; } 0 0 0
while (p < end && isDIGIT(*p)) { n += frac * (NV)(*p - '0'); frac /= 10.0; p++; digits++; } 0 0 0
131 0 ▶ if (!digits) return 0; /* a trailing dot is not a number */
Branch: 1 branch
BranchTrueFalseTotal
if (!digits) return 0; /* a trailing dot is not a number */ 0 0 0
132 }
133 85 ▶ while (p < end && isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE(*p)) p++; 85 0 85
while (p < end && isSPACE(*p)) p++; 0 85 85
134 85 ▶ if (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) { 85 0 85
135 85 switch (*p) {
136 3 case 'K': case 'k': mult = 1024.0; p++; break;
137 81 case 'M': case 'm': mult = 1024.0 * 1024.0; p++; break;
138 1 case 'G': case 'g': mult = 1024.0 * 1024.0 * 1024.0; p++; break;
139 0 default: break;
140 }
141 }
142 85 ▶ if (p < end && *p == 'B') p++; /* 512MB, as people write it */
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == 'B') p++; /* 512MB, as people write it */ 0 85 85
if (p < end && *p == 'B') p++; /* 512MB, as people write it */ 0 0 0
143 85 ▶ while (p < end && isSPACE(*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isSPACE(*p)) p++; 0 85 85
while (p < end && isSPACE(*p)) p++; 0 0 0
144 85 ▶ if (p != end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p != end) return 0; 0 85 85
145  
146 85 *out = Perl_floor(n * mult);
147 85 return 1;
148 }
149  
150 /* An IV where one is big enough, an NV where it is not: 2G overflows a 32-bit
151 * IV, and a budget that silently wrapped would be worse than no budget. */
152 67 static SV *pkc_bytes_sv(pTHX_ NV n) {
153 67 ▶ if (n >= 0.0 && n <= (NV)UV_MAX) return newSVuv((UV)n);
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 0.0 && n <= (NV)UV_MAX) return newSVuv((UV)n); 67 0 67
if (n >= 0.0 && n <= (NV)UV_MAX) return newSVuv((UV)n); 67 0 67
154 0 return newSVnv(n);
155 }
156  
157 /* ---- keys ----------------------------------------------------------------
158 *
159 * A key is bytes with a length limit. The rules live HERE so every backend
160 * inherits the same ones rather than each inventing its own - which is how
161 * two backends end up disagreeing about what is storable, and how an
162 * application developed against one breaks on the other.
163 *
164 * The limit is counted in BYTES, which is what the message and the
165 * documentation say. Counting characters would let a wide-character key past
166 * a check the file store then cannot honour - its header holds 4096 bytes of
167 * key - so such a key would write successfully and never read back. */
168 382 static const char *pkc_key(pTHX_ SV *key, STRLEN *len) {
169 const char *k;
170 382 ▶ if (!(key && SvOK(key))) croak("Punk::Cache: a key must be defined");
Branch: 2 branches
BranchTrueFalseTotal
if (!(key && SvOK(key))) croak("Punk::Cache: a key must be defined"); 382 0 382
if (!(key && SvOK(key))) croak("Punk::Cache: a key must be defined"); 1 381 382
171 381 k = SvPV_const(key, *len);
172 381 ▶ if (!*len) croak("Punk::Cache: a key must not be empty");
Branch: 1 branch
BranchTrueFalseTotal
if (!*len) croak("Punk::Cache: a key must not be empty"); 1 380 381
173 380 ▶ if (*len > PKC_KEY_MAX) croak("Punk::Cache: a key is limited to %d bytes",
Branch: 1 branch
BranchTrueFalseTotal
if (*len > PKC_KEY_MAX) croak("Punk::Cache: a key is limited to %d bytes", 1 379 380
174 PKC_KEY_MAX);
175 379 return k;
176 }
177  
178 73 static int pkc_is_undef_marker(pTHX_ SV *v) {
179 STRLEN l;
180 const char *p;
181 73 ▶ if (!(v && SvOK(v))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(v && SvOK(v))) return 0; 73 0 73
if (!(v && SvOK(v))) return 0; 0 73 73
182 73 p = SvPV_const(v, l);
183 73 ▶ return l == PKC_UNDEF_LEN && memEQ(p, PKC_UNDEF, PKC_UNDEF_LEN);
Branch: 2 branches
BranchTrueFalseTotal
return l == PKC_UNDEF_LEN && memEQ(p, PKC_UNDEF, PKC_UNDEF_LEN); 1 72 73
return l == PKC_UNDEF_LEN && memEQ(p, PKC_UNDEF, PKC_UNDEF_LEN); 1 0 1
184 }
185  
186 /* ---- the backend, reached the fast way where it is known ------------------ */
187  
188 37 static punk_cache *pkc_mem(pTHX_ punk_cachefront *f) {
189 37 return INT2PTR(punk_cache *, SvIV(SvRV(f->backend)));
190 }
191 846 static punk_cachefile *pkc_file(pTHX_ punk_cachefront *f) {
192 846 return INT2PTR(punk_cachefile *, SvIV(SvRV(f->backend)));
193 }
194  
195 /* Every one of these takes the key as an SV and reads its bytes HERE, at the
196 * point of use, rather than being handed a pointer taken earlier.
197 *
198 * That is not fussiness. A backend written in Perl receives the key as an
199 * alias in @_ and a compute callback can hold the caller's own variable;
200 * either can assign to it, which reallocates the SV's buffer. A char* taken
201 * before the call and used after it would then be reading freed memory - and
202 * the read would be a plausible-looking key, so the failure would be a wrong
203 * answer rather than a crash. An SvPV is a load and a flag test; it is not
204 * worth being clever about. */
205  
206 /* The stored bytes (+1), or NULL for absent OR expired - a caller does not
207 * need to know which.
208 *
209 * `expiry`, when not NULL, is filled with the entry's own absolute expiry so
210 * the tier can be populated with it rather than with a fresh one. Only the
211 * file store carries an expiry a reader can see; for everything else it stays
212 * 0 and the tier falls back to its ceiling, which is the safe direction. */
213 419 static SV *pkc_be_get(pTHX_ punk_cachefront *f, SV *key, double *expiry) {
214 STRLEN kl;
215 const char *k;
216 419 ▶ if (expiry) *expiry = 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if (expiry) *expiry = 0.0; 39 380 419
217 419 switch (f->kind) {
218 18 case PKC_K_MEMORY: {
219 18 uint32_t vlen = 0;
220 const char *v;
221 18 k = SvPV_const(key, kl);
222 18 v = punk_cache_get(aTHX_ pkc_mem(aTHX_ f), k, (uint32_t)kl,
223 pc_now(aTHX), &vlen);
224 18 ▶ return v ? newSVpvn(v, vlen) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return v ? newSVpvn(v, vlen) : NULL; 13 5 18
225 }
226 310 case PKC_K_FILE:
227 310 k = SvPV_const(key, kl);
228 310 return punk_cachefile_get_sv(aTHX_ pkc_file(aTHX_ f), k,
229 (uint32_t)kl, expiry);
230 91 default: {
231 91 SV *r = pcx_call_meth(aTHX_ f->backend, "get", &key, 1, 1);
232 91 ▶ if (r && !SvOK(r)) { SvREFCNT_dec(r); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (r && !SvOK(r)) { SvREFCNT_dec(r); return NULL; } 91 0 91
if (r && !SvOK(r)) { SvREFCNT_dec(r); return NULL; } 50 41 91
233 41 return r;
234 }
235 }
236 }
237  
238 /* ---- the tier ------------------------------------------------------------- */
239  
240 /* Drop one key from the tier. Used on a local write and on a received
241 * invalidation, and a no-op on a store without one. */
242 307 static void pkc_tier_drop(pTHX_ punk_cachefront *f, SV *key) {
243 STRLEN kl;
244 const char *k;
245 307 ▶ if (!f->tier) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->tier) return; 69 238 307
246 238 k = SvPV_const(key, kl);
247 238 (void)punk_cache_delete(aTHX_ f->tier, k, (uint32_t)kl);
248 }
249  
250 /* Put a value the backend just gave us into the tier.
251 *
252 * The expiry is the STORE's, capped by the ceiling - never one counted fresh
253 * from now, which would let the tier outlive the entry it is standing in for.
254 * An entry with no expiry of its own gets the ceiling, which is why a store
255 * that cannot report one degrades to short-lived tier entries rather than to
256 * permanent ones. */
257 52 static void pkc_tier_put(pTHX_ punk_cachefront *f, SV *key, SV *value,
258 double expiry) {
259 STRLEN kl, vl;
260 const char *k, *v;
261 double now, ceiling;
262  
263 57 ▶ if (!f->tier) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->tier) return; 21 31 52
264 31 v = SvPV_const(value, vl);
265 /* One value may not flush the hot set on its way in. */
266 31 ▶ if (vl > f->tier_max_entry) return;
Branch: 1 branch
BranchTrueFalseTotal
if (vl > f->tier_max_entry) return; 5 26 31
267  
268 26 k = SvPV_const(key, kl);
269 26 now = pc_now(aTHX);
270 26 ceiling = now + f->tier_ttl;
271 26 ▶ if (expiry > 0.0 && expiry < ceiling) ceiling = expiry;
Branch: 2 branches
BranchTrueFalseTotal
if (expiry > 0.0 && expiry < ceiling) ceiling = expiry; 4 22 26
if (expiry > 0.0 && expiry < ceiling) ceiling = expiry; 2 2 4
272 26 (void)punk_cache_set(aTHX_ f->tier, k, (uint32_t)kl, v, (uint32_t)vl,
273 ceiling);
274 }
275  
276 /* The read every public method goes through: the tier, then the backend, and
277 * the backend's answer put into the tier on the way back. */
278 316 static SV *pkc_read(pTHX_ punk_cachefront *f, SV *key) {
279 SV *v;
280 316 double expiry = 0.0;
281  
282 316 ▶ if (f->tier) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier) { 49 267 316
283 STRLEN kl;
284 49 const char *k = SvPV_const(key, kl);
285 49 uint32_t vlen = 0;
286 49 const char *hit = punk_cache_get(aTHX_ f->tier, k, (uint32_t)kl,
287 pc_now(aTHX), &vlen);
288 49 ▶ if (hit) return newSVpvn(hit, vlen);
Branch: 1 branch
BranchTrueFalseTotal
if (hit) return newSVpvn(hit, vlen); 10 39 49
289 }
290  
291 306 ▶ v = pkc_be_get(aTHX_ f, key, f->tier ? &expiry : NULL);
Branch: 1 branch
BranchTrueFalseTotal
v = pkc_be_get(aTHX_ f, key, f->tier ? &expiry : NULL); 39 267 306
292 306 ▶ if (v) pkc_tier_put(aTHX_ f, key, v, expiry);
Branch: 1 branch
BranchTrueFalseTotal
if (v) pkc_tier_put(aTHX_ f, key, v, expiry); 52 254 306
293 306 return v;
294 }
295  
296 /* What the backend said (+1): 1 stored, 0 refused for the shipped stores, and
297 * whatever a custom one returns. */
298 319 static SV *pkc_be_set(pTHX_ punk_cachefront *f, SV *key, SV *value, NV ttl) {
299 STRLEN kl, vl;
300 const char *k, *v;
301 319 switch (f->kind) {
302 16 case PKC_K_MEMORY: {
303 16 double now = pc_now(aTHX);
304 16 ▶ k = SvPV_const(key, kl);
Branch: 1 branch
BranchTrueFalseTotal
k = SvPV_const(key, kl); 16 0 16
305 16 v = SvPV_const(value, vl);
306 16 ▶ return newSViv(punk_cache_set(aTHX_ pkc_mem(aTHX_ f), k,
Branch: 1 branch
BranchTrueFalseTotal
return newSViv(punk_cache_set(aTHX_ pkc_mem(aTHX_ f), k, 1 15 16
307 (uint32_t)kl, v, (uint32_t)vl,
308 ttl > 0 ? now + (double)ttl : 0.0));
309 }
310 258 case PKC_K_FILE: {
311 258 ▶ k = SvPV_const(key, kl);
Branch: 1 branch
BranchTrueFalseTotal
k = SvPV_const(key, kl); 258 0 258
312 258 v = SvPV_const(value, vl);
313 258 ▶ return newSViv(punk_cachefile_set(aTHX_ pkc_file(aTHX_ f), k,
Branch: 1 branch
BranchTrueFalseTotal
return newSViv(punk_cachefile_set(aTHX_ pkc_file(aTHX_ f), k, 27 231 258
314 (uint32_t)kl, v, (uint32_t)vl,
315 ttl > 0 ? pcf_now() + (double)ttl
316 : 0.0));
317 }
318 45 default: {
319 SV *argv[3];
320 45 argv[0] = key;
321 45 argv[1] = value;
322 45 argv[2] = sv_2mortal(newSVnv(ttl));
323 45 return pcx_call_meth(aTHX_ f->backend, "set", argv, 3, 1);
324 }
325 }
326 }
327  
328 19 static SV *pkc_be_delete(pTHX_ punk_cachefront *f, SV *key) {
329 STRLEN kl;
330 const char *k;
331 19 switch (f->kind) {
332 3 case PKC_K_MEMORY:
333 3 k = SvPV_const(key, kl);
334 3 return newSViv(punk_cache_delete(aTHX_ pkc_mem(aTHX_ f), k,
335 (uint32_t)kl));
336 6 case PKC_K_FILE:
337 6 k = SvPV_const(key, kl);
338 6 return newSViv(punk_cachefile_delete(aTHX_ pkc_file(aTHX_ f), k,
339 (uint32_t)kl));
340 10 default:
341 10 return pcx_call_meth(aTHX_ f->backend, "delete", &key, 1, 1);
342 }
343 }
344  
345 3 static SV *pkc_be_clear(pTHX_ punk_cachefront *f) {
346 3 switch (f->kind) {
347 0 case PKC_K_MEMORY:
348 0 punk_cache_clear(aTHX_ pkc_mem(aTHX_ f));
349 0 return NULL; /* the shipped stores return nothing */
350 1 case PKC_K_FILE:
351 1 punk_cachefile_clear(aTHX_ pkc_file(aTHX_ f));
352 1 return NULL;
353 2 default:
354 2 return pcx_call_meth(aTHX_ f->backend, "clear", NULL, 0, 1);
355 }
356 }
357  
358 /* 1 to the caller that should compute, 0 to one that should look again. */
359 /* `budget` is how long the caller expects to hold the lock, which is how old
360 * it may get before another caller takes it for abandoned. Negative means
361 * the store's own lock_wait, which is what a cache compute wants. A custom
362 * backend is handed it as a second argument and may ignore it. */
363 269 static int pkc_be_lock_for(pTHX_ punk_cachefront *f, SV *key, double budget) {
364 269 ▶ if (f->kind == PKC_K_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->kind == PKC_K_FILE) { 235 34 269
365 STRLEN kl;
366 235 const char *k = SvPV_const(key, kl);
367 235 int r = punk_cachefile_lock_for(aTHX_ pkc_file(aTHX_ f), k,
368 (uint32_t)kl, budget);
369 /* A lock that could not be attempted becomes "compute": the store is
370 * unusable for this key, so waiting for a holder that can never
371 * appear would be an outage in exchange for nothing. The backend has
372 * counted it; this layer's contract stays 1 or 0 so no caller has to
373 * know there is a third answer. */
374 235 ▶ return (r == PCF_LOCK_UNAVAILABLE) ? 1 : r;
Branch: 1 branch
BranchTrueFalseTotal
return (r == PCF_LOCK_UNAVAILABLE) ? 1 : r; 235 0 235
375 }
376 {
377 SV *argv[2], *r;
378 int won;
379 34 argv[0] = key;
380 34 ▶ if (budget >= 0) argv[1] = sv_2mortal(newSVnv(budget));
Branch: 1 branch
BranchTrueFalseTotal
if (budget >= 0) argv[1] = sv_2mortal(newSVnv(budget)); 7 27 34
381 34 ▶ r = pcx_call_meth(aTHX_ f->backend, "_lock", argv,
Branch: 1 branch
BranchTrueFalseTotal
r = pcx_call_meth(aTHX_ f->backend, "_lock", argv, 7 27 34
382 budget >= 0 ? 2 : 1, 1);
383 34 ▶ won = (r && SvTRUE(r)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
won = (r && SvTRUE(r)) ? 1 : 0; 34 0 34
won = (r && SvTRUE(r)) ? 1 : 0; 8 26 34
384 34 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 34 0 34
385 34 return won;
386 }
387 }
388  
389 239 static int pkc_be_lock(pTHX_ punk_cachefront *f, SV *key) {
390 239 return pkc_be_lock_for(aTHX_ f, key, -1.0);
391 }
392  
393 46 static void pkc_be_unlock(pTHX_ punk_cachefront *f, SV *key) {
394 46 ▶ if (f->kind == PKC_K_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->kind == PKC_K_FILE) { 34 12 46
395 STRLEN kl;
396 34 const char *k = SvPV_const(key, kl);
397 34 punk_cachefile_unlock(aTHX_ pkc_file(aTHX_ f), k, (uint32_t)kl);
398 34 return;
399 }
400 {
401 12 SV *r = pcx_call_meth(aTHX_ f->backend, "_unlock", &key, 1, 0);
402 12 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 12 12
403 }
404 }
405  
406 3 static double pkc_be_lock_wait(pTHX_ punk_cachefront *f) {
407 3 ▶ if (f->kind == PKC_K_FILE) return pkc_file(aTHX_ f)->lock_wait;
Branch: 1 branch
BranchTrueFalseTotal
if (f->kind == PKC_K_FILE) return pkc_file(aTHX_ f)->lock_wait; 1 2 3
408 2 ▶ if (f->caps & PKC_CAN_LOCKWAIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->caps & PKC_CAN_LOCKWAIT) { 2 0 2
409 2 SV *r = pcx_call_meth(aTHX_ f->backend, "_lock_wait", NULL, 0, 1);
410 2 ▶ double w = (r && SvOK(r)) ? (double)SvNV(r) : PKC_LOCK_WAIT;
Branch: 2 branches
BranchTrueFalseTotal
double w = (r && SvOK(r)) ? (double)SvNV(r) : PKC_LOCK_WAIT; 2 0 2
double w = (r && SvOK(r)) ? (double)SvNV(r) : PKC_LOCK_WAIT; 2 0 2
411 2 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 2 0 2
412 2 return w;
413 }
414 0 return PKC_LOCK_WAIT;
415 }
416  
417 /* The backend's stats as a flat list of pairs (+1 each), in its own order. */
418 41 static AV *pkc_be_stats(pTHX_ punk_cachefront *f) {
419 41 dSP;
420 41 AV *out = (AV *)sv_2mortal((SV *)newAV());
421 int count, i;
422 41 ENTER; SAVETMPS;
423 41 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 41 41
424 41 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 41 41
425 41 PUSHs(f->backend);
426 41 PUTBACK;
427 41 count = call_method("stats", G_ARRAY);
428 41 SPAGAIN;
429 41 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 41 0 41
430 41 av_extend(out, (SSize_t)count - 1);
431 747 ▶ for (i = count - 1; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = count - 1; i >= 0; i--) 706 41 747
432 706 (void)av_store(out, (SSize_t)i, newSVsv(POPs));
433 }
434 41 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 41 0 41
435 41 return out;
436 }
437  
438 /* ---- the invalidation topic ---------------------------------------------- */
439  
440 /* This process's origin token, minted on first use here and re-minted after a
441 * fork - see the header comment: a token inherited from the parent is shared
442 * by every worker, and a shared token makes every worker ignore every other
443 * worker's invalidation. */
444 35 static SV *pkc_origin(pTHX_ punk_cachefront *f) {
445 35 IV pid = (IV)PerlProc_getpid();
446 35 ▶ if (!f->origin || f->origin_pid != pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (!f->origin || f->origin_pid != pid) { 15 20 35
if (!f->origin || f->origin_pid != pid) { 0 15 15
447 struct timeval tv;
448 UV salt;
449 20 gettimeofday(&tv, NULL);
450 20 salt = ((UV)tv.tv_sec ^ ((UV)tv.tv_usec << 8)) ^ PTR2UV(f);
451 20 ▶ if (f->origin) SvREFCNT_dec(f->origin);
Branch: 1 branch
BranchTrueFalseTotal
if (f->origin) SvREFCNT_dec(f->origin); 0 20 20
452 20 f->origin = newSVpvf("%" UVuf ":%" IVdf ":%" UVuf,
453 ++PKC_ORIGIN_SEQ, pid, salt);
454 20 f->origin_pid = pid;
455 }
456 35 return f->origin;
457 }
458  
459 /* Tell the rest of the pool to drop this key.
460 *
461 * Best effort, and the documentation says so: the bus is bounded and drops
462 * oldest under pressure, so a worker far enough behind can miss one and keep
463 * a stale value. That is why a TTL should always be set - it bounds staleness
464 * even when a message is lost. Invalidation makes a cache fresh quickly; TTL
465 * is what makes it eventually correct.
466 *
467 * What travels is the KEY, never the value. Publishing values would make this
468 * a replication system, and a bus slot is 2KB - a cached page would be
469 * refused outright and the pool would silently diverge. */
470 308 static void pkc_publish(pTHX_ punk_cachefront *f, SV *key) {
471 SV *msg;
472 STRLEN kl;
473 const char *k;
474 308 ▶ if (f->shared || !f->name) return;
Branch: 2 branches
BranchTrueFalseTotal
if (f->shared || !f->name) return; 256 52 308
if (f->shared || !f->name) return; 234 22 256
475 22 k = SvPV_const(key, kl);
476 22 msg = sv_2mortal(newSVsv(pkc_origin(aTHX_ f)));
477 22 sv_catpvn(msg, "\0", 1);
478 22 sv_catpvn(msg, k, kl);
479 22 ▶ if (punk_bus_cache_publish(aTHX_ f->name, msg)) f->sent++;
Branch: 1 branch
BranchTrueFalseTotal
if (punk_bus_cache_publish(aTHX_ f->name, msg)) f->sent++; 14 8 22
480 }
481  
482 /* The subscriber. Reached from the event loop with the published message, so
483 * it talks STRAIGHT to the backend: going through delete() would publish in
484 * turn and the invalidation would echo around the pool forever. */
485 XS_INTERNAL(pkc_inval_cb);
486 16 XS_INTERNAL(pkc_inval_cb) {
487 16 dXSARGS;
488 16 AV *cap = punk_clos_cap(aTHX_ cv);
489 16 ▶ SV **slot = cap ? av_fetch(cap, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **slot = cap ? av_fetch(cap, 0, 0) : NULL; 16 0 16
490 16 ▶ SV *weak = slot ? *slot : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *weak = slot ? *slot : NULL; 16 0 16
491 16 ▶ SV *msg = items > 0 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *msg = items > 0 ? ST(0) : NULL; 16 0 16
492 punk_cachefront *f;
493 const char *m, *o, *nul, *key;
494 STRLEN ml, ol, kl;
495 SV *r;
496  
497 /* The store is held weakly: a cache that has gone away must not be kept
498 * alive by its own subscription, and its messages are simply dropped. */
499 16 ▶ if (!(weak && SvROK(weak) && SvIOK(SvRV(weak)) && SvIV(SvRV(weak))))
Branch: 4 branches
BranchTrueFalseTotal
if (!(weak && SvROK(weak) && SvIOK(SvRV(weak)) && SvIV(SvRV(weak)))) 16 0 16
if (!(weak && SvROK(weak) && SvIOK(SvRV(weak)) && SvIV(SvRV(weak)))) 10 6 16
if (!(weak && SvROK(weak) && SvIOK(SvRV(weak)) && SvIV(SvRV(weak)))) 10 0 10
if (!(weak && SvROK(weak) && SvIOK(SvRV(weak)) && SvIV(SvRV(weak)))) 0 10 10
500 6 XSRETURN_EMPTY;
501 10 ▶ if (!(msg && SvOK(msg))) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!(msg && SvOK(msg))) XSRETURN_EMPTY; 10 0 10
if (!(msg && SvOK(msg))) XSRETURN_EMPTY; 0 10 10
502 10 f = INT2PTR(punk_cachefront *, SvIV(SvRV(weak)));
503  
504 10 m = SvPV_const(msg, ml);
505 10 nul = (const char *)memchr(m, '\0', ml);
506 10 ▶ if (!nul) XSRETURN_EMPTY; /* no key: nothing to drop */
Branch: 1 branch
BranchTrueFalseTotal
if (!nul) XSRETURN_EMPTY; /* no key: nothing to drop */ 0 10 10
507  
508 /* Our own message. Acting on it would mean a `set` telling this very
509 * store to drop the value it has just written. */
510 10 o = SvPV_const(pkc_origin(aTHX_ f), ol);
511 10 ▶ if (ol == (STRLEN)(nul - m) && memEQ(m, o, ol)) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (ol == (STRLEN)(nul - m) && memEQ(m, o, ol)) XSRETURN_EMPTY; 10 0 10
if (ol == (STRLEN)(nul - m) && memEQ(m, o, ol)) XSRETURN_EMPTY; 8 2 10
512  
513 2 f->received++;
514 2 key = nul + 1;
515 2 kl = ml - (STRLEN)(nul - m) - 1;
516  
517 /* A TIERED store drops its own copy and STOPS. It must not reach the
518 * backend: the backend is shared - a tier is only permitted in front of
519 * one - so the writer has already changed the one copy there is, and for
520 * the file store `delete` is `unlink`. Going further would mean every
521 * worker deleting the file the writer had just written, on every write,
522 * which empties the cache while looking like a tier that never hits. */
523 2 ▶ if (f->tier) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier) { 2 0 2
524 2 ▶ if (kl == PKC_CLEAR_LEN && memEQ(key, PKC_CLEAR, PKC_CLEAR_LEN))
Branch: 2 branches
BranchTrueFalseTotal
if (kl == PKC_CLEAR_LEN && memEQ(key, PKC_CLEAR, PKC_CLEAR_LEN)) 0 2 2
if (kl == PKC_CLEAR_LEN && memEQ(key, PKC_CLEAR, PKC_CLEAR_LEN)) 0 0 0
525 0 punk_cache_clear(aTHX_ f->tier);
526 else
527 2 (void)punk_cache_delete(aTHX_ f->tier, key, (uint32_t)kl);
528 2 XSRETURN_EMPTY;
529 }
530  
531 0 ▶ if (kl == PKC_CLEAR_LEN && memEQ(key, PKC_CLEAR, PKC_CLEAR_LEN)) {
Branch: 2 branches
BranchTrueFalseTotal
if (kl == PKC_CLEAR_LEN && memEQ(key, PKC_CLEAR, PKC_CLEAR_LEN)) { 0 0 0
if (kl == PKC_CLEAR_LEN && memEQ(key, PKC_CLEAR, PKC_CLEAR_LEN)) { 0 0 0
532 0 r = pkc_be_clear(aTHX_ f);
533 }
534 0 else r = pkc_be_delete(aTHX_ f, sv_2mortal(newSVpvn(key, kl)));
535 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
536 0 XSRETURN_EMPTY;
537 }
538  
539 /* Cross-worker invalidation.
540 *
541 * A SHARED store needs none: with the file backend a write is visible to
542 * every worker the moment it lands, which is the same property that makes it
543 * the default. An unshared one has to be told, and Punk has a bus.
544 *
545 * Registered HERE, which is compile time - in the parent, before the server
546 * forks. That is the only moment a subscription reaches every worker; one
547 * made inside a request lands in exactly one of them. */
548 113 static void pkc_wire(pTHX_ punk_cachefront *f, SV *self) {
549 AV *cap;
550 SV *cb, *weak;
551 113 ▶ if (f->shared || !f->name) return;
Branch: 2 branches
BranchTrueFalseTotal
if (f->shared || !f->name) return; 62 51 113
if (f->shared || !f->name) return; 23 39 62
552  
553 39 cap = newAV();
554 39 weak = newSVsv(self);
555 39 sv_rvweaken(weak);
556 39 av_push(cap, weak);
557 39 cb = sv_2mortal(punk_closure(aTHX_ pkc_inval_cb, cap));
558 39 (void)punk_bus_cache_subscribe(aTHX_ f->name, cb);
559 }
560  
561 /* Whether a shared ring actually exists, not merely whether the ABI is
562 * present: without an arena a publish reaches this process alone, and
563 * reporting a connected pool would be exactly the false comfort this is meant
564 * to remove. Hyperman is asked only if it is already loaded. */
565 41 static int pkc_pool(pTHX) {
566 41 ▶ HV *inc = GvHVn(PL_incgv);
Branch: 1 branch
BranchTrueFalseTotal
HV *inc = GvHVn(PL_incgv); 41 0 41
567 HV *stash;
568 GV *gv;
569 SV *r;
570 int live;
571  
572 41 ▶ if (!(inc && hv_exists(inc, "Hyperman.pm", 11))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(inc && hv_exists(inc, "Hyperman.pm", 11))) return 0; 41 0 41
if (!(inc && hv_exists(inc, "Hyperman.pm", 11))) return 0; 14 27 41
573 27 stash = gv_stashpvs("Hyperman", 0);
574 27 ▶ if (!stash) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) return 0; 0 27 27
575 27 gv = gv_fetchmethod_autoload(stash, "bus_live", FALSE);
576 27 ▶ if (!(gv && isGV(gv) && GvCV(gv))) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(gv && isGV(gv) && GvCV(gv))) return 0; 27 0 27
if (!(gv && isGV(gv) && GvCV(gv))) return 0; 27 0 27
if (!(gv && isGV(gv) && GvCV(gv))) return 0; 0 27 27
577  
578 27 r = pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Hyperman")), "bus_live",
579 NULL, 0, 1);
580 27 ▶ live = (r && SvTRUE(r)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
live = (r && SvTRUE(r)) ? 1 : 0; 27 0 27
live = (r && SvTRUE(r)) ? 1 : 0; 10 17 27
581 27 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 27 0 27
582 27 return live;
583 }
584  
585 /* ---- construction --------------------------------------------------------- */
586  
587 /* The five methods, checked wherever the backend came from. A custom store
588 * missing one fails at BOOT with the name of what is missing, rather than on
589 * the first request that happens to call it. */
590 116 static void pkc_contract(pTHX_ SV *backend) {
591 static const char *const M[5] = { "get", "set", "delete", "clear", "stats" };
592 116 ▶ const char *who = (SvROK(backend) && SvOBJECT(SvRV(backend)))
Branch: 1 branch
BranchTrueFalseTotal
const char *who = (SvROK(backend) && SvOBJECT(SvRV(backend))) 116 0 116
593 232 ▶ ? HvNAME(SvSTASH(SvRV(backend)))
Branch: 4 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(backend))) 116 0 116
? HvNAME(SvSTASH(SvRV(backend))) 0 0 0
? HvNAME(SvSTASH(SvRV(backend))) 116 0 116
? HvNAME(SvSTASH(SvRV(backend))) 0 116 116
594 464 ▶ : (SvOK(backend) ? SvPV_nolen(backend) : "undef");
Branch: 4 branches
BranchTrueFalseTotal
: (SvOK(backend) ? SvPV_nolen(backend) : "undef"); 116 0 116
: (SvOK(backend) ? SvPV_nolen(backend) : "undef"); 116 0 116
: (SvOK(backend) ? SvPV_nolen(backend) : "undef"); 0 116 116
: (SvOK(backend) ? SvPV_nolen(backend) : "undef"); 0 0 0
595 int i;
596 690 ▶ for (i = 0; i < 5; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) { 576 114 690
597 576 SV *meth = sv_2mortal(newSVpv(M[i], 0));
598 576 SV *can = pcx_call_meth(aTHX_ backend, "can", &meth, 1, 1);
599 576 ▶ int ok = (can && SvTRUE(can)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
int ok = (can && SvTRUE(can)) ? 1 : 0; 576 0 576
int ok = (can && SvTRUE(can)) ? 1 : 0; 574 2 576
600 576 ▶ if (can) SvREFCNT_dec(can);
Branch: 1 branch
BranchTrueFalseTotal
if (can) SvREFCNT_dec(can); 576 0 576
601 576 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 2 574 576
602 2 ▶ croak("Punk::Cache: %s is not a usable backend - "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Cache: %s is not a usable backend - " 2 0 2
603 "it must implement %s", who ? who : "the backend", M[i]);
604 }
605 114 }
606  
607 341 static int pkc_can(pTHX_ SV *obj, const char *meth) {
608 341 SV *m = sv_2mortal(newSVpv(meth, 0));
609 341 SV *r = pcx_call_meth(aTHX_ obj, "can", &m, 1, 1);
610 341 ▶ int ok = (r && SvTRUE(r)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
int ok = (r && SvTRUE(r)) ? 1 : 0; 341 0 341
int ok = (r && SvTRUE(r)) ? 1 : 0; 188 153 341
611 341 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 341 0 341
612 341 return ok;
613 }
614  
615 /* Which of the two shipped stores this is, by EXACT class: a subclass that
616 * overrides `get` in Perl must still be dispatched to, so it does not qualify
617 * for the fast path even though its guts are the same struct. */
618 113 static int pkc_kind(pTHX_ SV *backend) {
619 HV *stash;
620 const char *n;
621 113 ▶ if (!(SvROK(backend) && SvOBJECT(SvRV(backend)) && SvIOK(SvRV(backend))))
Branch: 3 branches
BranchTrueFalseTotal
if (!(SvROK(backend) && SvOBJECT(SvRV(backend)) && SvIOK(SvRV(backend)))) 113 0 113
if (!(SvROK(backend) && SvOBJECT(SvRV(backend)) && SvIOK(SvRV(backend)))) 113 0 113
if (!(SvROK(backend) && SvOBJECT(SvRV(backend)) && SvIOK(SvRV(backend)))) 39 74 113
622 39 return PKC_K_GENERIC;
623 74 stash = SvSTASH(SvRV(backend));
624 74 ▶ n = stash ? HvNAME(stash) : NULL;
Branch: 7 branches
BranchTrueFalseTotal
n = stash ? HvNAME(stash) : NULL; 74 0 74
n = stash ? HvNAME(stash) : NULL; 74 0 74
n = stash ? HvNAME(stash) : NULL; 74 0 74
n = stash ? HvNAME(stash) : NULL; 0 74 74
n = stash ? HvNAME(stash) : NULL; 0 0 0
n = stash ? HvNAME(stash) : NULL; 74 0 74
n = stash ? HvNAME(stash) : NULL; 0 74 74
625 74 ▶ if (!n) return PKC_K_GENERIC;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return PKC_K_GENERIC; 0 74 74
626 74 ▶ if (strEQ(n, "Punk::Cache::Memory")) return PKC_K_MEMORY;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(n, "Punk::Cache::Memory")) return PKC_K_MEMORY; 26 48 74
627 48 ▶ if (strEQ(n, "Punk::Cache::File")) return PKC_K_FILE;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(n, "Punk::Cache::File")) return PKC_K_FILE; 48 0 48
628 0 return PKC_K_GENERIC;
629 }
630  
631 113 static SV *pkc_bless(pTHX_ SV *class, punk_cachefront *f) {
632 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
633 0 ▶ ? HvNAME(SvSTASH(SvRV(class)))
Branch: 4 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
634 226 ▶ : SvPV_nolen(class);
Branch: 3 branches
BranchTrueFalseTotal
: SvPV_nolen(class); 0 113 113
: SvPV_nolen(class); 0 0 0
: SvPV_nolen(class); 0 0 0
635 113 ▶ return sv_bless(newRV_noinc(newSViv(PTR2IV(f))),
Branch: 1 branch
BranchTrueFalseTotal
return sv_bless(newRV_noinc(newSViv(PTR2IV(f))), 113 0 113
636 gv_stashpv(cls ? cls : "Punk::Cache", GV_ADD));
637 }
638  
639 /* The store, wrapped, contract-checked and wired to the bus. `name` is the
640 * invalidation topic and may be NULL - a store nobody named cannot address a
641 * topic, so it invalidates locally and says so through stats. */
642 116 static SV *pkc_wrap(pTHX_ SV *class, SV *backend, SV *name,
643 size_t tier_bytes, double tier_ttl) {
644 punk_cachefront *f;
645 SV *self;
646 116 int backend_shared = 0;
647  
648 116 pkc_contract(aTHX_ backend);
649  
650 /* A backend may implement is_shared, returning true when every worker
651 * sees the same data. Leaving it out means unshared, which is the safe
652 * way round: an unnecessary invalidation costs a message, a missing one
653 * serves stale data. */
654 114 ▶ if (pkc_can(aTHX_ backend, "is_shared")) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_can(aTHX_ backend, "is_shared")) { 110 4 114
655 110 SV *r = pcx_call_meth(aTHX_ backend, "is_shared", NULL, 0, 1);
656 110 ▶ backend_shared = (r && SvTRUE(r)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
backend_shared = (r && SvTRUE(r)) ? 1 : 0; 110 0 110
backend_shared = (r && SvTRUE(r)) ? 1 : 0; 77 33 110
657 110 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 110 0 110
658 }
659  
660 /* A tier in front of a store that is already per-process multiplies the
661 * memory and buys nothing, so it is refused rather than quietly built.
662 * The strictness also earns something downstream: because a tier implies
663 * a shared backend, a received invalidation can drop the tier entry and
664 * NEVER touch the backend, with no condition to get wrong - and getting
665 * it wrong means unlinking the file another worker has just written. */
666 114 ▶ if (tier_bytes && !backend_shared)
Branch: 2 branches
BranchTrueFalseTotal
if (tier_bytes && !backend_shared) 27 87 114
if (tier_bytes && !backend_shared) 1 26 27
667 1 croak("Punk::Cache: `memory` is a tier in front of a SHARED store, "
668 "and this backend reports that it is not shared - a memory "
669 "cache in front of a per-process one multiplies the footprint "
670 "and caches nothing new");
671  
672 113 Newxz(f, 1, punk_cachefront);
673 113 f->backend = newSVsv(backend);
674 113 ▶ f->name = (name && SvOK(name)) ? newSVsv(name) : NULL;
Branch: 2 branches
BranchTrueFalseTotal
f->name = (name && SvOK(name)) ? newSVsv(name) : NULL; 87 26 113
f->name = (name && SvOK(name)) ? newSVsv(name) : NULL; 87 0 87
675 113 f->kind = pkc_kind(aTHX_ backend);
676  
677 113 ▶ if (tier_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (tier_bytes) { 26 87 113
678 26 f->tier = punk_cache_new(aTHX_ tier_bytes);
679 26 ▶ f->tier_ttl = tier_ttl > 0 ? tier_ttl : PKC_TIER_TTL;
Branch: 1 branch
BranchTrueFalseTotal
f->tier_ttl = tier_ttl > 0 ? tier_ttl : PKC_TIER_TTL; 26 0 26
680 26 f->tier_max_entry = tier_bytes / PKC_TIER_MAX_SHARE;
681 26 ▶ if (!f->tier_max_entry) f->tier_max_entry = tier_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->tier_max_entry) f->tier_max_entry = tier_bytes; 0 26 26
682 }
683  
684 /* SHARED AND UNTIERED. A tiered store holds per-worker copies, so it
685 * needs telling when a key changes exactly as an unshared one does -
686 * without this the whole pool serves its own stale memory. */
687 113 ▶ f->shared = backend_shared && !f->tier;
Branch: 2 branches
BranchTrueFalseTotal
f->shared = backend_shared && !f->tier; 77 36 113
f->shared = backend_shared && !f->tier; 51 26 77
688  
689 113 ▶ if (f->kind == PKC_K_FILE)
Branch: 1 branch
BranchTrueFalseTotal
if (f->kind == PKC_K_FILE) 48 65 113
690 48 f->caps = PKC_CAN_LOCK | PKC_CAN_UNLOCK | PKC_CAN_LOCKWAIT;
691 65 ▶ else if (f->kind == PKC_K_GENERIC) {
Branch: 1 branch
BranchTrueFalseTotal
else if (f->kind == PKC_K_GENERIC) { 39 26 65
692 39 ▶ if (pkc_can(aTHX_ backend, "_lock")) f->caps |= PKC_CAN_LOCK;
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_can(aTHX_ backend, "_lock")) f->caps |= PKC_CAN_LOCK; 4 35 39
693 39 ▶ if (pkc_can(aTHX_ backend, "_unlock")) f->caps |= PKC_CAN_UNLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_can(aTHX_ backend, "_unlock")) f->caps |= PKC_CAN_UNLOCK; 4 35 39
694 39 ▶ if (pkc_can(aTHX_ backend, "_lock_wait")) f->caps |= PKC_CAN_LOCKWAIT;
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_can(aTHX_ backend, "_lock_wait")) f->caps |= PKC_CAN_LOCKWAIT; 2 37 39
695 39 ▶ if (pkc_can(aTHX_ backend, "_sweep")) f->caps |= PKC_CAN_SWEEP;
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_can(aTHX_ backend, "_sweep")) f->caps |= PKC_CAN_SWEEP; 0 39 39
696 }
697  
698 113 self = pkc_bless(aTHX_ class, f);
699 113 pkc_wire(aTHX_ f, self);
700 113 return self;
701 }
702  
703 /* `memory` and `file` are aliases for the two shipped stores. Anything else
704 * is a CLASS: given in full if it contains `::`, and otherwise the short form
705 * of a Punk::Cache:: backend, so `cache 'custom'` reaches Punk::Cache::Custom.
706 *
707 * That convention is what makes the interface pluggable rather than a fixed
708 * list of two. A backend nobody here wrote is configured exactly the way the
709 * shipped ones are, with no registration step to forget. */
710 119 static SV *pkc_backend_class(pTHX_ SV *spec) {
711 STRLEN sl;
712 119 const char *s = SvPV_const(spec, sl);
713 SV *pkg;
714 119 ▶ if (sl == 6 && memEQ(s, "memory", 6)) return newSVpvs("Punk::Cache::Memory");
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 6 && memEQ(s, "memory", 6)) return newSVpvs("Punk::Cache::Memory"); 34 85 119
if (sl == 6 && memEQ(s, "memory", 6)) return newSVpvs("Punk::Cache::Memory"); 29 5 34
715 90 ▶ if (sl == 4 && memEQ(s, "file", 4)) return newSVpvs("Punk::Cache::File");
Branch: 2 branches
BranchTrueFalseTotal
if (sl == 4 && memEQ(s, "file", 4)) return newSVpvs("Punk::Cache::File"); 51 39 90
if (sl == 4 && memEQ(s, "file", 4)) return newSVpvs("Punk::Cache::File"); 51 0 51
716 39 ▶ if (memchr(s, ':', sl)) return newSVpvn(s, sl);
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(s, ':', sl)) return newSVpvn(s, sl); 31 8 39
717 8 pkg = newSVpvs("Punk::Cache::");
718 8 ▶ if (sl) {
Branch: 1 branch
BranchTrueFalseTotal
if (sl) { 8 0 8
719 8 ▶ char first = (char)toUPPER(s[0]);
Branch: 1 branch
BranchTrueFalseTotal
char first = (char)toUPPER(s[0]); 7 1 8
720 8 sv_catpvn(pkg, &first, 1);
721 8 sv_catpvn(pkg, s + 1, sl - 1);
722 }
723 8 return pkg;
724 }
725  
726 /* A backend name reaches `require`, so it is checked against the shape of a
727 * package name first. Configuration is not attacker data, but a name that
728 * turns into a path is the bug class this workspace keeps meeting, and the
729 * check costs one pass at boot. */
730 119 static int pkc_pkg_ok(const char *p, STRLEN len) {
731 119 STRLEN i = 0;
732 119 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 119 0 119
733 for (;;) {
734 333 ▶ if (i >= len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= len) return 0; 0 333 333
735 333 ▶ if (!(isALPHA(p[i]) || p[i] == '_')) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(isALPHA(p[i]) || p[i] == '_')) return 0; 1 332 333
if (!(isALPHA(p[i]) || p[i] == '_')) return 0; 1 0 1
736 332 i++;
737 1510 ▶ while (i < len && (isALNUM(p[i]) || p[i] == '_')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < len && (isALNUM(p[i]) || p[i] == '_')) i++; 1392 118 1510
while (i < len && (isALNUM(p[i]) || p[i] == '_')) i++; 1178 214 1392
while (i < len && (isALNUM(p[i]) || p[i] == '_')) i++; 0 214 214
738 332 ▶ if (i == len) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (i == len) return 1; 118 214 332
739 214 ▶ if (!(p[i] == ':' && i + 1 < len && p[i + 1] == ':')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!(p[i] == ':' && i + 1 < len && p[i + 1] == ':')) return 0; 214 0 214
if (!(p[i] == ':' && i + 1 < len && p[i + 1] == ':')) return 0; 214 0 214
if (!(p[i] == ':' && i + 1 < len && p[i + 1] == ':')) return 0; 0 214 214
740 214 i += 2;
741 }
742 }
743  
744 /* Load the backend class, telling a module that is MISSING from one that is
745 * BROKEN apart: a backend whose module has a syntax error must report the
746 * syntax error, not be dismissed as an unknown backend. A backend declared
747 * inline - in the application file itself, or in a test - has no .pm to find,
748 * and that is legitimate. */
749 114 static void pkc_load(pTHX_ SV *spec, SV *pkgsv) {
750 STRLEN pl;
751 114 const char *pkg = SvPV_const(pkgsv, pl);
752 114 SV *file = sv_2mortal(newSVpvn("", 0));
753 HV *inc;
754 STRLEN i;
755 SV *err;
756  
757 1763 ▶ for (i = 0; i < pl; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pl; i++) { 1649 114 1763
758 1649 ▶ if (pkg[i] == ':' && i + 1 < pl && pkg[i + 1] == ':') {
Branch: 3 branches
BranchTrueFalseTotal
if (pkg[i] == ':' && i + 1 < pl && pkg[i + 1] == ':') { 204 1445 1649
if (pkg[i] == ':' && i + 1 < pl && pkg[i + 1] == ':') { 204 0 204
if (pkg[i] == ':' && i + 1 < pl && pkg[i + 1] == ':') { 204 0 204
759 204 sv_catpvs(file, "/");
760 204 i++;
761 }
762 1445 else sv_catpvn(file, pkg + i, 1);
763 }
764 114 sv_catpvs(file, ".pm");
765  
766 /* Unconditionally, not `unless $pkg->can('new')` - that guard
767 * short-circuits every time for the shipped stores, because the XS
768 * supplies `new`, so the .pm - and the is_shared that decides whether
769 * this store needs cross-worker invalidation - would never be loaded. */
770 114 ▶ inc = GvHVn(PL_incgv);
Branch: 1 branch
BranchTrueFalseTotal
inc = GvHVn(PL_incgv); 114 0 114
771 138 ▶ if (inc && hv_exists_ent(inc, file, 0)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (inc && hv_exists_ent(inc, file, 0)) return; 114 0 114
if (inc && hv_exists_ent(inc, file, 0)) return; 57 57 114
772  
773 57 eval_pv(form("require %s;", pkg), FALSE);
774 57 ▶ if (!SvTRUE(ERRSV)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) return; 57 0 57
if (!SvTRUE(ERRSV)) return; 24 33 57
775  
776 33 ▶ err = sv_2mortal(newSVsv(ERRSV));
Branch: 1 branch
BranchTrueFalseTotal
err = sv_2mortal(newSVsv(ERRSV)); 33 0 33
777 {
778 STRLEN el, fl;
779 33 ▶ const char *e = SvPV_const(err, el);
Branch: 1 branch
BranchTrueFalseTotal
const char *e = SvPV_const(err, el); 33 0 33
780 33 const char *fp = SvPV_const(file, fl);
781 33 SV *want = sv_2mortal(newSVpvs("Can't locate "));
782 STRLEN wl;
783 const char *wp;
784 33 sv_catpvn(want, fp, fl);
785 33 sv_catpvs(want, " in @INC");
786 33 wp = SvPV_const(want, wl);
787 33 ▶ if (!(el >= wl && memEQ(e, wp, wl)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(el >= wl && memEQ(e, wp, wl))) 33 0 33
if (!(el >= wl && memEQ(e, wp, wl))) 0 33 33
788 0 croak_sv(err); /* the module exists and is broken: THAT */
789 }
790 33 ▶ if (!pkc_can(aTHX_ pkgsv, "new"))
Branch: 1 branch
BranchTrueFalseTotal
if (!pkc_can(aTHX_ pkgsv, "new")) 3 30 33
791 /* Deliberately not the raw require error: an @INC dump buries the one
792 * fact that helps, which is the class name that was looked for. */
793 3 croak("Punk::Cache: unknown backend '%s' - %s is not installed "
794 "(shipped backends: file, memory; a custom backend is loaded "
795 "from Punk::Cache::<Name>)", SvPV_nolen(spec), pkg);
796 }
797  
798 /* The options that belong to this tier rather than to the backend: `name`,
799 * the invalidation topic, and `memory` / `memory_ttl`, the tier. Pulled out
800 * of the option list wherever it came from, so a ready-made backend object
801 * and a named class are configured the same way. */
802 301 static void pkc_own_opt(pTHX_ SV *k, SV *v, SV **name,
803 size_t *tier_bytes, double *tier_ttl, int *taken) {
804 301 const char *ks = SvPV_nolen(k);
805 301 *taken = 1;
806 301 ▶ if (strEQ(ks, "name")) { *name = v; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(ks, "name")) { *name = v; return; } 89 212 301
807 212 ▶ if (strEQ(ks, "memory")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(ks, "memory")) { 30 182 212
808 30 NV b = 0.0;
809 30 ▶ if (!pkc_bytes(aTHX_ v, &b) || !(b > 0.0))
Branch: 2 branches
BranchTrueFalseTotal
if (!pkc_bytes(aTHX_ v, &b) || !(b > 0.0)) 29 1 30
if (!pkc_bytes(aTHX_ v, &b) || !(b > 0.0)) 0 29 29
810 2 ▶ croak("Punk::Cache: memory '%s' is not a size "
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk::Cache: memory '%s' is not a size " 1 0 1
croak("Punk::Cache: memory '%s' is not a size " 1 0 1
811 "(try 64M, 512M, or a plain byte count)",
812 (v && SvOK(v)) ? SvPV_nolen(v) : "");
813 29 *tier_bytes = (size_t)b;
814 29 return;
815 }
816 182 ▶ if (strEQ(ks, "memory_ttl")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(ks, "memory_ttl")) { 30 152 182
817 30 ▶ NV t = (v && SvOK(v)) ? SvNV(v) : 0.0;
Branch: 2 branches
BranchTrueFalseTotal
NV t = (v && SvOK(v)) ? SvNV(v) : 0.0; 30 0 30
NV t = (v && SvOK(v)) ? SvNV(v) : 0.0; 30 0 30
818 30 ▶ if (!(t > 0.0))
Branch: 1 branch
BranchTrueFalseTotal
if (!(t > 0.0)) 1 29 30
819 1 croak("Punk::Cache: memory_ttl must be greater than zero - it is "
820 "the ceiling that bounds how stale a tiered read can be, "
821 "and how long a hot entry can go unread on disk");
822 29 *tier_ttl = (double)t;
823 29 return;
824 }
825 152 *taken = 0;
826 }
827  
828 /* new($class, $spec, %opt): args[0] is the spec, the rest are the backend's
829 * options, less the ones pkc_own_opt keeps. */
830 125 static SV *pkc_new(pTHX_ SV *class, AV *args) {
831 125 SSize_t n = av_len(args) + 1, i;
832 125 ▶ SV *spec = n > 0 ? *av_fetch(args, 0, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *spec = n > 0 ? *av_fetch(args, 0, 0) : &PL_sv_undef; 125 0 125
833 125 SV *name = NULL, *pkgsv, *backend, *self;
834 AV *opts;
835 STRLEN pl;
836 const char *pkg;
837 125 size_t tier_bytes = 0;
838 125 double tier_ttl = PKC_TIER_TTL;
839  
840 /* a ready-made backend object: anything with the five contract methods */
841 125 ▶ if (SvROK(spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(spec)) { 6 119 125
842 10 ▶ for (i = 1; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < n; i += 2) { 4 6 10
843 int taken;
844 4 pkc_own_opt(aTHX_ *av_fetch(args, i, 0), *av_fetch(args, i + 1, 0),
845 &name, &tier_bytes, &tier_ttl, &taken);
846 }
847 6 return pkc_wrap(aTHX_ class, spec, name, tier_bytes, tier_ttl);
848 }
849  
850 119 ▶ if (!SvOK(spec)) spec = sv_2mortal(newSVpvs("file"));
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(spec)) spec = sv_2mortal(newSVpvs("file")); 0 119 119
851 119 pkgsv = sv_2mortal(pkc_backend_class(aTHX_ spec));
852 119 pkg = SvPV_const(pkgsv, pl);
853 119 ▶ if (!pkc_pkg_ok(pkg, pl))
Branch: 1 branch
BranchTrueFalseTotal
if (!pkc_pkg_ok(pkg, pl)) 1 118 119
854 1 croak("Punk::Cache: '%s' is not a usable backend name",
855 SvPV_nolen(spec));
856  
857 /* the options the backend gets: ours removed, and max_bytes normalised
858 * from 512M / 2G into bytes */
859 118 opts = (AV *)sv_2mortal((SV *)newAV());
860 411 ▶ for (i = 1; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < n; i += 2) { 297 114 411
861 297 SV *k = *av_fetch(args, i, 0);
862 297 SV *v = *av_fetch(args, i + 1, 0);
863 297 const char *ks = SvPV_nolen(k);
864 int taken;
865 297 pkc_own_opt(aTHX_ k, v, &name, &tier_bytes, &tier_ttl, &taken);
866 358 ▶ if (taken) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (taken) continue; 143 152 295
867 152 ▶ if (strEQ(ks, "max_bytes")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(ks, "max_bytes")) { 65 87 152
868 65 NV b = 0.0;
869 65 ▶ if (!pkc_bytes(aTHX_ v, &b) || !(b > 0.0))
Branch: 2 branches
BranchTrueFalseTotal
if (!pkc_bytes(aTHX_ v, &b) || !(b > 0.0)) 63 2 65
if (!pkc_bytes(aTHX_ v, &b) || !(b > 0.0)) 0 63 63
870 4 ▶ croak("Punk::Cache: max_bytes '%s' is not a size "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Cache: max_bytes '%s' is not a size " 2 0 2
871 "(try 64M, 512M, 2G, or a plain byte count)",
872 SvOK(v) ? SvPV_nolen(v) : "");
873 63 av_push(opts, newSVsv(k));
874 63 av_push(opts, pkc_bytes_sv(aTHX_ b));
875 63 continue;
876 }
877 87 av_push(opts, newSVsv(k));
878 87 av_push(opts, newSVsv(v));
879 }
880  
881 114 pkc_load(aTHX_ spec, pkgsv);
882  
883 {
884 111 SSize_t on = av_len(opts) + 1;
885 SV **argv;
886 111 ▶ Newx(argv, on > 0 ? on : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newx(argv, on > 0 ? on : 1, SV *); 107 4 111
Newx(argv, on > 0 ? on : 1, SV *); 0 107 107
Newx(argv, on > 0 ? on : 1, SV *); 107 4 111
887 403 ▶ for (i = 0; i < on; i++) argv[i] = *av_fetch(opts, i, 0);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < on; i++) argv[i] = *av_fetch(opts, i, 0); 292 111 403
888 111 backend = pcx_call_meth(aTHX_ pkgsv, "new", argv, (int)on, 1);
889 110 Safefree(argv);
890 }
891 110 ▶ if (!(backend && SvOK(backend))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(backend && SvOK(backend))) { 110 0 110
if (!(backend && SvOK(backend))) { 0 110 110
892 0 ▶ if (backend) SvREFCNT_dec(backend);
Branch: 1 branch
BranchTrueFalseTotal
if (backend) SvREFCNT_dec(backend); 0 0 0
893 0 croak("Punk::Cache: %s->new returned nothing usable", pkg);
894 }
895 110 self = pkc_wrap(aTHX_ class, sv_2mortal(backend), name, tier_bytes,
896 tier_ttl);
897 108 return self;
898 }
899  
900 /* ---- the operations -------------------------------------------------------
901 *
902 * SET invalidates too, not only delete. Worker A writing a new value while B
903 * through H serve the old one out of memory until it expires is the same
904 * staleness bug wearing a different hat - and the one that actually bites,
905 * because updating a cached thing is far commoner than deleting it. */
906 63 static SV *pkc_get(pTHX_ punk_cachefront *f, SV *key) {
907 STRLEN kl;
908 SV *v;
909 63 (void)pkc_key(aTHX_ key, &kl);
910 60 v = pkc_read(aTHX_ f, key);
911 60 ▶ if (v && pkc_is_undef_marker(aTHX_ v)) { SvREFCNT_dec(v); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (v && pkc_is_undef_marker(aTHX_ v)) { SvREFCNT_dec(v); return NULL; } 52 8 60
if (v && pkc_is_undef_marker(aTHX_ v)) { SvREFCNT_dec(v); return NULL; } 0 52 52
912 60 return v;
913 }
914  
915 /* A local write DROPS the tier entry rather than replacing it with the value
916 * just written.
917 *
918 * Write-through looks better and is wrong: two workers setting the same key
919 * would each fill their own tier with their own value while the shared store
920 * keeps only one of them, and the origin token means a writer never acts on
921 * its own invalidation - so the loser would serve a value the store does not
922 * hold for as long as the entry lived. Dropping costs one backend read on the
923 * next request and cannot diverge. */
924 288 static SV *pkc_set(pTHX_ punk_cachefront *f, SV *key, SV *value, NV ttl) {
925 STRLEN kl;
926 288 ▶ SV *stored = (value && SvOK(value))
Branch: 1 branch
BranchTrueFalseTotal
SV *stored = (value && SvOK(value)) 0 288 288
927 576 ▶ ? value : sv_2mortal(newSVpvn(PKC_UNDEF, PKC_UNDEF_LEN));
Branch: 1 branch
BranchTrueFalseTotal
? value : sv_2mortal(newSVpvn(PKC_UNDEF, PKC_UNDEF_LEN)); 288 0 288
928 SV *r;
929 288 (void)pkc_key(aTHX_ key, &kl);
930 288 pkc_tier_drop(aTHX_ f, key);
931 288 r = pkc_be_set(aTHX_ f, key, stored, ttl);
932 288 ▶ if (r && SvTRUE(r)) pkc_publish(aTHX_ f, key);
Branch: 2 branches
BranchTrueFalseTotal
if (r && SvTRUE(r)) pkc_publish(aTHX_ f, key); 288 0 288
if (r && SvTRUE(r)) pkc_publish(aTHX_ f, key); 286 2 288
933 288 return r;
934 }
935  
936 19 static SV *pkc_delete(pTHX_ punk_cachefront *f, SV *key) {
937 STRLEN kl;
938 SV *r;
939 19 (void)pkc_key(aTHX_ key, &kl);
940 19 pkc_tier_drop(aTHX_ f, key);
941 19 r = pkc_be_delete(aTHX_ f, key);
942 19 pkc_publish(aTHX_ f, key);
943 19 return r;
944 }
945  
946 3 static SV *pkc_clear(pTHX_ punk_cachefront *f) {
947 SV *r;
948 3 ▶ if (f->tier) punk_cache_clear(aTHX_ f->tier);
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier) punk_cache_clear(aTHX_ f->tier); 1 2 3
949 3 r = pkc_be_clear(aTHX_ f);
950 3 pkc_publish(aTHX_ f, sv_2mortal(newSVpvn(PKC_CLEAR, PKC_CLEAR_LEN)));
951 3 return r;
952 }
953  
954 /* ---- compute --------------------------------------------------------------
955 *
956 * The method that matters. Offering only get/set invites every application to
957 * write the same three lines by hand and to get the undef case wrong. */
958 236 static void pkc_sleep(double secs) {
959 #ifdef WIN32
960 Sleep((DWORD)(secs * 1000.0));
961 #else
962 struct timeval tv;
963 236 tv.tv_sec = (long)secs;
964 236 tv.tv_usec = (long)((secs - (double)(long)secs) * 1000000.0);
965 236 (void)select(0, NULL, NULL, NULL, &tv);
966 #endif
967 236 }
968  
969 /* Bytes back into whatever the caller stored: structures go through frj, the
970 * ABI Punk already consumes. The cache itself stores BYTES - that rule is
971 * what keeps the backends interchangeable - so this decodes at the edge
972 * rather than letting one backend hold things another cannot. */
973 21 static SV *pkc_ready(pTHX_ SV *raw, int json) {
974 SV *out;
975 21 ▶ if (pkc_is_undef_marker(aTHX_ raw)) { SvREFCNT_dec(raw); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_is_undef_marker(aTHX_ raw)) { SvREFCNT_dec(raw); return NULL; } 1 20 21
976 20 ▶ if (!json) return raw;
Branch: 1 branch
BranchTrueFalseTotal
if (!json) return raw; 3 17 20
977 {
978 STRLEN l;
979 17 const char *p = SvPV_const(raw, l);
980 17 out = punk_frj(aTHX)->decode(aTHX_ p, l, NULL);
981 }
982 17 SvREFCNT_dec(raw);
983 17 return out;
984 }
985  
986 12 static SV *pkc_compute(pTHX_ punk_cachefront *f, SV *key, NV ttl, SV *code,
987 int json) {
988 STRLEN kl;
989 12 SV *raw, *val = NULL, *err = NULL, *store;
990 12 int won = 0;
991  
992 12 ▶ if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV))
Branch: 3 branches
BranchTrueFalseTotal
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 12 0 12
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 12 0 12
if (!(code && SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV)) 0 12 12
993 0 croak("Punk::Cache::compute: need a code reference");
994  
995 12 (void)pkc_key(aTHX_ key, &kl);
996 12 raw = pkc_read(aTHX_ f, key);
997 12 ▶ if (raw) return pkc_ready(aTHX_ raw, json);
Branch: 1 branch
BranchTrueFalseTotal
if (raw) return pkc_ready(aTHX_ raw, json); 4 8 12
998  
999 /* SINGLE-FLIGHT, where the backend offers it.
1000 *
1001 * When a hot key expires under load every worker misses at once and every
1002 * one of them recomputes - the moment a cache is most valuable is the
1003 * moment it stops helping.
1004 *
1005 * Three rules keep this from becoming a new way to hang, and they matter
1006 * more than the optimisation does:
1007 *
1008 * - a loser that waits out its budget COMPUTES ANYWAY. Correctness
1009 * never depends on the lock: duplicated work is a cost, a stalled
1010 * request is an outage, and a request hanging because the winner died
1011 * is worse than both;
1012 * - a stale lock is stolen by the backend rather than obeyed, or one
1013 * crash poisons a key until somebody notices;
1014 * - the lock covers one compute and nothing unbounded. */
1015 8 ▶ if (f->caps & PKC_CAN_LOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->caps & PKC_CAN_LOCK) { 3 5 8
1016 3 won = pkc_be_lock(aTHX_ f, key);
1017 3 ▶ if (!won) {
Branch: 1 branch
BranchTrueFalseTotal
if (!won) { 3 0 3
1018 3 double deadline = pc_now(aTHX) + pkc_be_lock_wait(aTHX_ f);
1019 237 ▶ while (pc_now(aTHX) < deadline) {
Branch: 1 branch
BranchTrueFalseTotal
while (pc_now(aTHX) < deadline) { 236 1 237
1020 SV *ready;
1021 236 pkc_sleep(0.002);
1022 236 ready = pkc_read(aTHX_ f, key);
1023 236 ▶ if (ready) return pkc_ready(aTHX_ ready, json);
Branch: 1 branch
BranchTrueFalseTotal
if (ready) return pkc_ready(aTHX_ ready, json); 0 236 236
1024 236 ▶ if (pkc_be_lock(aTHX_ f, key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkc_be_lock(aTHX_ f, key)) { 2 234 236
1025 /* The holder finished or died between our read and
1026 * this acquisition - and a winner WRITES before it
1027 * unlocks, so if it finished, its value is visible
1028 * to a re-read made while holding the lock. Look
1029 * again or a herd of two computes twice. */
1030 2 SV *again = pkc_read(aTHX_ f, key);
1031 2 ▶ if (again) {
Branch: 1 branch
BranchTrueFalseTotal
if (again) { 1 1 2
1032 1 ▶ if (f->caps & PKC_CAN_UNLOCK)
Branch: 1 branch
BranchTrueFalseTotal
if (f->caps & PKC_CAN_UNLOCK) 1 0 1
1033 1 pkc_be_unlock(aTHX_ f, key);
1034 1 return pkc_ready(aTHX_ again, json);
1035 }
1036 1 won = 1;
1037 1 break;
1038 }
1039 }
1040 /* waited it out: compute rather than hang */
1041 }
1042 }
1043  
1044 /* dSP HERE and not at the top: every call above can have grown - and so
1045 * reallocated - the value stack, and a stack pointer captured before them
1046 * would be pushed through into freed memory. */
1047 {
1048 7 dSP;
1049 int count;
1050 7 ENTER; SAVETMPS;
1051 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
1052 7 PUTBACK;
1053 7 count = call_sv(code, G_SCALAR | G_EVAL);
1054 7 SPAGAIN;
1055 7 ▶ if (count > 0) val = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) val = SvREFCNT_inc(POPs); 7 0 7
1056 7 PUTBACK;
1057 7 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 7 0 7
if (SvTRUE(ERRSV)) { 0 7 7
1058 0 ▶ err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
err = newSVsv(ERRSV); 0 0 0
1059 0 ▶ if (val) { SvREFCNT_dec(val); val = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (val) { SvREFCNT_dec(val); val = NULL; } 0 0 0
1060 }
1061 7 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 7 0 7
1062 }
1063  
1064 7 ▶ if (!err) {
Branch: 1 branch
BranchTrueFalseTotal
if (!err) { 7 0 7
1065 7 ▶ if (!(val && SvOK(val)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(val && SvOK(val))) 7 0 7
if (!(val && SvOK(val))) 1 6 7
1066 1 store = sv_2mortal(newSVpvn(PKC_UNDEF, PKC_UNDEF_LEN));
1067 6 ▶ else if (json)
Branch: 1 branch
BranchTrueFalseTotal
else if (json) 1 5 6
1068 1 store = sv_2mortal(punk_frj(aTHX)->encode(aTHX_ val, NULL));
1069 else
1070 5 store = val;
1071 {
1072 7 SV *r = pkc_be_set(aTHX_ f, key, store, ttl);
1073 7 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 7 0 7
1074 }
1075 }
1076  
1077 /* The lock is released whether the compute worked or not: holding it
1078 * after a failure would make one bad call block every other worker for
1079 * the whole wait budget. Only the holder releases it - a loser that
1080 * waited out its budget and computed anyway does NOT own the lock,
1081 * and unlinking it here would free the real winner's lock early and
1082 * hand a third worker the same computation. */
1083 7 ▶ if (won && (f->caps & PKC_CAN_UNLOCK)) pkc_be_unlock(aTHX_ f, key);
Branch: 2 branches
BranchTrueFalseTotal
if (won && (f->caps & PKC_CAN_UNLOCK)) pkc_be_unlock(aTHX_ f, key); 1 6 7
if (won && (f->caps & PKC_CAN_UNLOCK)) pkc_be_unlock(aTHX_ f, key); 1 0 1
1084  
1085 7 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 7 7
1086 0 ▶ if (val) SvREFCNT_dec(val);
Branch: 1 branch
BranchTrueFalseTotal
if (val) SvREFCNT_dec(val); 0 0 0
1087 0 croak_sv(sv_2mortal(err));
1088 }
1089 7 ▶ if (val && !SvOK(val)) { SvREFCNT_dec(val); return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (val && !SvOK(val)) { SvREFCNT_dec(val); return NULL; } 7 0 7
if (val && !SvOK(val)) { SvREFCNT_dec(val); return NULL; } 1 6 7
1090 6 return val;
1091 }
1092  
1093 113 static void pkc_free(pTHX_ punk_cachefront *f) {
1094 113 ▶ if (!f) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!f) return; 0 113 113
1095 113 ▶ if (f->tier) punk_cache_free(aTHX_ f->tier);
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier) punk_cache_free(aTHX_ f->tier); 26 87 113
1096 113 ▶ if (f->backend) SvREFCNT_dec(f->backend);
Branch: 1 branch
BranchTrueFalseTotal
if (f->backend) SvREFCNT_dec(f->backend); 113 0 113
1097 113 ▶ if (f->name) SvREFCNT_dec(f->name);
Branch: 1 branch
BranchTrueFalseTotal
if (f->name) SvREFCNT_dec(f->name); 87 26 113
1098 113 ▶ if (f->origin) SvREFCNT_dec(f->origin);
Branch: 1 branch
BranchTrueFalseTotal
if (f->origin) SvREFCNT_dec(f->origin); 20 93 113
1099 113 Safefree(f);
1100 }
1101  
1102 #endif /* PUNK_CACHEFRONT_H */
« xs/abi.xs ↑ Summary include/punk/punk_mpstream.h »