include/punk/punk_cachefront.h

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