xs/cache.xs

filter: Statementstmt 94.0% 174 / 185 Branchbran 58.9% 106 / 178 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.8% 280 / 363 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/request.xs ↑ Summary include/punk/punk_obs.h »
1 MODULE = Punk PACKAGE = Punk::Cache::Memory
2  
3 PROTOTYPES: DISABLE
4  
5 # The in-memory cache store: the five contract methods over punk_cache.h.
6 #
7 # Bounded by BYTES, not entries. An entry cap does not bound the thing that
8 # runs out - a thousand entries of ten megabytes is ten gigabytes - and the
9 # whole reason this store exists is to have a bound that means something.
10  
11 SV *
12 new(class, ...)
13 SV *class
14 CODE:
15 {
16 55 UV max_bytes = 64 * 1024 * 1024; /* the documented default */
17 punk_cache *c;
18 int i;
19 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
20 110 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 7 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 55 55
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
21 112 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 57 55 112
22 57 ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
23 57 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 55 2 57
24 const char *k = SvPV_nolen(ST(i));
25 55 ▶ if (strEQ(k, "max_bytes")) max_bytes = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "max_bytes")) max_bytes = SvUV(ST(i + 1)); 1 54 55
26 1 }
27 54 if (!max_bytes)
28 54 croak("Punk::Cache::Memory: max_bytes must be greater than zero");
29 c = punk_cache_new(aTHX_ (size_t)max_bytes);
30 RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(c))),
31 gv_stashpv(cls, GV_ADD));
32 }
33 OUTPUT:
34 RETVAL
35  
36 void
37 DESTROY(self)
38 SV *self
39 CODE:
40 54 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 54 0 54
# uncoverable branch false 54 0 54
# uncoverable branch false 54 0 54
41 54 # uncoverable condition right count:1 class:ignore_covered_err
42 # uncoverable condition right count:2 class:ignore_covered_err
43 # uncoverable condition right count:3 class:ignore_covered_err
44 if (SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self)))
45 punk_cache_free(aTHX_ INT2PTR(punk_cache *, SvIV(SvRV(self))));
46  
47 # get($key) -> the bytes, or undef
48 #
49 # Undef means absent OR expired, deliberately: a caller does not need to know
50 # which, and a store that distinguished them would leak its expiry policy into
51 # every call site.
52 SV *
53 get(self, key)
54 SV *self
55 SV *key
56 CODE:
57 {
58 43 punk_cache *c = INT2PTR(punk_cache *, SvIV(SvRV(self)));
59 STRLEN kl;
60 43 const char *k = SvPV_const(key, kl);
61 43 uint32_t vlen = 0;
62 43 const char *v = punk_cache_get(aTHX_ c, k, (uint32_t)kl,
63 pc_now(aTHX), &vlen);
64 43 ▶ if (!v) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) XSRETURN_UNDEF; 11 32 43
65 32 RETVAL = newSVpvn(v, vlen);
66 }
67 OUTPUT:
68 RETVAL
69  
70 # set($key, $bytes, $ttl) -> 1 stored, 0 refused
71 #
72 # $ttl is seconds; 0 means NO EXPIRY, not "expire immediately". The opposite
73 # reading is the sort of thing that empties a cache in production, so it is
74 # stated here, in the contract, and asserted in the conformance suite.
75 #
76 # A value too big for the budget is refused rather than stored: making room
77 # would evict everything else and still not fit.
78 IV
79 set(self, key, value, ttl = 0)
80 SV *self
81 SV *key
82 SV *value
83 NV ttl
84 CODE:
85 {
86 745 punk_cache *c = INT2PTR(punk_cache *, SvIV(SvRV(self)));
87 STRLEN kl, vl;
88 745 ▶ const char *k = SvPV_const(key, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = SvPV_const(key, kl); 745 0 745
89 745 const char *v = SvPV_const(value, vl);
90 745 ▶ double expiry = ttl > 0 ? pc_now(aTHX) + (double)ttl : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
double expiry = ttl > 0 ? pc_now(aTHX) + (double)ttl : 0.0; 2 743 745
91 745 ▶ RETVAL = punk_cache_set(aTHX_ c, k, (uint32_t)kl, v, (uint32_t)vl,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_cache_set(aTHX_ c, k, (uint32_t)kl, v, (uint32_t)vl, 716 29 745
92 expiry);
93 }
94 OUTPUT:
95 RETVAL
96  
97 IV
98 delete(self, key)
99 SV *self
100 SV *key
101 CODE:
102 {
103 2 punk_cache *c = INT2PTR(punk_cache *, SvIV(SvRV(self)));
104 STRLEN kl;
105 2 const char *k = SvPV_const(key, kl);
106 2 ▶ RETVAL = punk_cache_delete(aTHX_ c, k, (uint32_t)kl);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_cache_delete(aTHX_ c, k, (uint32_t)kl); 0 2 2
107 }
108 OUTPUT:
109 RETVAL
110  
111 void
112 clear(self)
113 SV *self
114 CODE:
115 2 punk_cache_clear(aTHX_ INT2PTR(punk_cache *, SvIV(SvRV(self))));
116  
117 # stats() -> hits, misses, evictions, refused, expired, bytes, entries,
118 # max_bytes
119 #
120 # Cumulative for the counters, current for bytes and entries. A cache whose
121 # hit rate cannot be seen is a cache nobody can tune, and the eviction count
122 # is the only way to tell "the budget is too small" from "these keys are
123 # simply cold".
124 void
125 stats(self)
126 SV *self
127 PPCODE:
128 {
129 30 punk_cache *c = INT2PTR(punk_cache *, SvIV(SvRV(self)));
130 30 punk_cache_check_fork(aTHX_ c);
131 30 ▶ EXTEND(SP, 16);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 16); 0 30 30
132 30 mPUSHp("hits", 4); mPUSHu((UV)c->hits);
133 30 mPUSHp("misses", 6); mPUSHu((UV)c->misses);
134 30 mPUSHp("evictions", 9); mPUSHu((UV)c->evictions);
135 30 mPUSHp("refused", 7); mPUSHu((UV)c->refused);
136 30 mPUSHp("expired", 7); mPUSHu((UV)c->expired);
137 30 mPUSHp("bytes", 5); mPUSHu((UV)c->bytes);
138 30 mPUSHp("entries", 7); mPUSHu((UV)c->entries);
139 30 mPUSHp("max_bytes", 9); mPUSHu((UV)c->max_bytes);
140 }
141  
142 MODULE = Punk PACKAGE = Punk::Cache::File
143  
144 PROTOTYPES: DISABLE
145  
146 # The file cache store: the same five contract methods, on disk.
147 #
148 # One copy for the whole worker pool, because the filesystem is already
149 # shared - which is why this is Punk::Cache's default and the in-memory store
150 # is the opt-in.
151  
152 SV *
153 new(class, ...)
154 SV *class
155 CODE:
156 {
157 105 UV max_bytes = 512 * 1024 * 1024;
158 105 NV lock_wait = 5.0;
159 105 SV *dir = NULL;
160 punk_cachefile *c;
161 STRLEN dl;
162 const char *dp;
163 int i;
164 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
165 210 ▶ # uncoverable condition left count:1 class:ignore_covered_err
Branch: 7 branches
BranchTrueFalseTotal
# uncoverable condition left count:1 class:ignore_covered_err 0 105 105
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
# uncoverable condition left count:1 class:ignore_covered_err 0 0 0
166 # uncoverable condition right count:1 class:ignore_covered_err
167 295 ▶ ? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class);
Branch: 1 branch
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) : SvPV_nolen(class); 190 105 295
168 190  
169 190 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 52 138 190
170 138 ▶ const char *k = SvPV_nolen(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
const char *k = SvPV_nolen(ST(i)); 104 34 138
171 34 ▶ if (strEQ(k, "max_bytes")) max_bytes = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "max_bytes")) max_bytes = SvUV(ST(i + 1)); 30 4 34
172 else if (strEQ(k, "dir")) dir = ST(i + 1);
173 105 ▶ else if (strEQ(k, "lock_wait")) lock_wait = SvNV(ST(i + 1));
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "lock_wait")) lock_wait = SvNV(ST(i + 1)); 104 1 105
else if (strEQ(k, "lock_wait")) lock_wait = SvNV(ST(i + 1)); 0 104 104
174 1 }
175 104 ▶ if (!(dir && SvOK(dir))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
if (!(dir && SvOK(dir))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 103 104
176 1 croak("Punk::Cache::File: need a `dir`");
177 103 ▶ if (!max_bytes)
Branch: 2 branches
BranchTrueFalseTotal
if (!max_bytes) 102 1 103
if (!max_bytes) 1 101 102
178 2 croak("Punk::Cache::File: max_bytes must be greater than zero");
179 if (Perl_isinfnan(lock_wait) || lock_wait < 0)
180 croak("Punk::Cache::File: lock_wait must be a finite number of "
181 101 "seconds, zero or more");
182  
183 dp = SvPV_const(dir, dl);
184 /* Created and checked HERE, at construction - which is to_app - so an
185 101 ▶ * unwritable directory is a boot failure in front of whoever deployed
Branch: 2 branches
BranchTrueFalseTotal
* unwritable directory is a boot failure in front of whoever deployed 15 86 101
* unwritable directory is a boot failure in front of whoever deployed 2 13 15
186 2 * it, not a silent miss at three in the morning. */
187 if (mkdir(dp, 0700) != 0 && errno != EEXIST)
188 99 ▶ croak("Punk::Cache::File: cannot create '%s': %s", dp,
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Cache::File: cannot create '%s': %s", dp, 0 99 99
189 0 strerror(errno));
190 # uncoverable branch true
191 # uncoverable condition left count:1 class:ignore_covered_err
192 if (access(dp, W_OK | X_OK) != 0)
193 99 ▶ croak("Punk::Cache::File: '%s' is not writable", dp); /* # uncoverable statement */
Branch: 2 branches
BranchTrueFalseTotal
croak("Punk::Cache::File: '%s' is not writable", dp); /* # uncoverable statement */ 99 0 99
croak("Punk::Cache::File: '%s' is not writable", dp); /* # uncoverable statement */ 1 98 99
194 1 ▶ { /* A directory anyone may write to is one anyone may plant a
Branch: 1 branch
BranchTrueFalseTotal
{ /* A directory anyone may write to is one anyone may plant a 1 0 1
195 1 * symlink in, and the store follows paths under it. */
196 struct stat st;
197 # uncoverable branch true class:ignore_covered_err
198 # uncoverable condition left count:1 class:ignore_covered_err
199 98 # uncoverable condition right count:1 class:ignore_covered_err
200 # uncoverable condition left count:2 class:ignore_covered_err
201 98 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 0 98 98
202 98 # uncoverable condition right count:3 class:ignore_covered_err
203 if (stat(dp, &st) == 0 && (st.st_mode & S_IWOTH)
204 && !(st.st_mode & S_ISVTX)) /* # uncoverable statement class:ignore_covered_err */
205 croak("Punk::Cache::File: '%s' is world-writable; the cache " /* # uncoverable statement class:ignore_covered_err */
206 "directory must be private to the server", dp);
207 }
208  
209 c = punk_cachefile_new(aTHX_ dp, dl, (size_t)max_bytes,
210 (double)lock_wait);
211 # uncoverable branch true
212 # uncoverable condition left count:1 class:ignore_covered_err
213 if (!c) croak("Punk::Cache::File: '%s' is too long a path", dp);
214 RETVAL = sv_bless(newRV_noinc(newSViv(PTR2IV(c))),
215 gv_stashpv(cls, GV_ADD));
216 }
217 OUTPUT:
218 RETVAL
219  
220 void
221 DESTROY(self)
222 SV *self
223 CODE:
224 98 ▶ # uncoverable branch false
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable branch false 98 0 98
# uncoverable branch false 98 0 98
# uncoverable branch false 98 0 98
225 98 # uncoverable condition right count:1 class:ignore_covered_err
226 98 # uncoverable condition right count:2 class:ignore_covered_err
227 # uncoverable condition right count:3 class:ignore_covered_err
228 if (SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self)))
229 punk_cachefile_free(aTHX_
230 INT2PTR(punk_cachefile *, SvIV(SvRV(self))));
231  
232 SV *
233 get(self, key)
234 SV *self
235 SV *key
236 CODE:
237 {
238 37 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self)));
239 STRLEN kl;
240 37 const char *k = SvPV_const(key, kl);
241 37 SV *v = punk_cachefile_get_sv(aTHX_ c, k, (uint32_t)kl, NULL);
242 37 ▶ if (!v) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) XSRETURN_UNDEF; 8 29 37
243 29 RETVAL = v;
244 }
245 OUTPUT:
246 RETVAL
247  
248 IV
249 set(self, key, value, ttl = 0)
250 SV *self
251 SV *key
252 SV *value
253 NV ttl
254 CODE:
255 {
256 435 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self)));
257 STRLEN kl, vl;
258 435 ▶ const char *k = SvPV_const(key, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = SvPV_const(key, kl); 435 0 435
259 435 const char *v = SvPV_const(value, vl);
260 435 ▶ double expiry = ttl > 0 ? pcf_now() + (double)ttl : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
double expiry = ttl > 0 ? pcf_now() + (double)ttl : 0.0; 2 433 435
261 435 ▶ RETVAL = punk_cachefile_set(aTHX_ c, k, (uint32_t)kl, v,
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_cachefile_set(aTHX_ c, k, (uint32_t)kl, v, 412 23 435
262 (uint32_t)vl, expiry);
263 }
264 OUTPUT:
265 RETVAL
266  
267 IV
268 delete(self, key)
269 SV *self
270 SV *key
271 CODE:
272 {
273 3 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self)));
274 STRLEN kl;
275 3 const char *k = SvPV_const(key, kl);
276 3 ▶ RETVAL = punk_cachefile_delete(aTHX_ c, k, (uint32_t)kl);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_cachefile_delete(aTHX_ c, k, (uint32_t)kl); 0 3 3
277 }
278 OUTPUT:
279 RETVAL
280  
281 void
282 clear(self)
283 SV *self
284 CODE:
285 1 punk_cachefile_clear(aTHX_
286 1 INT2PTR(punk_cachefile *, SvIV(SvRV(self))));
287  
288 void
289 stats(self)
290 SV *self
291 PPCODE:
292 {
293 35 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self)));
294 35 uint64_t bytes = 0, entries = 0;
295 35 punk_cachefile_check_fork(aTHX_ c);
296 35 punk_cachefile_usage(c, &bytes, &entries);
297 35 ▶ EXTEND(SP, 18);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 18); 0 35 35
298 35 mPUSHp("hits", 4); mPUSHu((UV)c->hits);
299 35 mPUSHp("misses", 6); mPUSHu((UV)c->misses);
300 35 mPUSHp("evictions", 9); mPUSHu((UV)c->evictions);
301 35 mPUSHp("refused", 7); mPUSHu((UV)c->refused);
302 35 mPUSHp("expired", 7); mPUSHu((UV)c->expired);
303 35 mPUSHp("bytes", 5); mPUSHu((UV)bytes);
304 35 mPUSHp("entries", 7); mPUSHu((UV)entries);
305 35 mPUSHp("max_bytes", 9); mPUSHu((UV)c->max_bytes);
306 /* not in the memory store's list: only the file store has a lock
307 * that can fail to be attempted at all */
308 35 mPUSHp("lock_errors", 11); mPUSHu((UV)c->lock_errors);
309 }
310  
311 # The single-flight seam, used by Punk::Cache::compute.
312 #
313 # _lock returns 1 to the caller that should compute and 0 to one that should
314 # look again. A loser NEVER blocks here: waiting is the caller's business, and
315 # it gives up and computes rather than hanging on a lock whose holder may have
316 # died.
317 IV
318 _lock(self, key)
319 SV *self
320 SV *key
321 CODE:
322 {
323 64 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self)));
324 STRLEN kl;
325 64 const char *k = SvPV_const(key, kl);
326 64 int r = punk_cachefile_lock(aTHX_ c, k, (uint32_t)kl);
327 /* 1 or 0 out here: a lock that could not be attempted answers
328 * "compute", the same degradation the C front end makes, so a Perl
329 * caller never sees a third value it would have to interpret. */
330 64 ▶ RETVAL = (r == PCF_LOCK_UNAVAILABLE) ? 1 : r; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (r == PCF_LOCK_UNAVAILABLE) ? 1 : r; /* # uncoverable condition right count:1 class:ignore_covered_err */ 64 0 64
RETVAL = (r == PCF_LOCK_UNAVAILABLE) ? 1 : r; /* # uncoverable condition right count:1 class:ignore_covered_err */ 48 16 64
331 }
332 OUTPUT:
333 RETVAL
334  
335 void
336 _unlock(self, key)
337 SV *self
338 SV *key
339 CODE:
340 {
341 1 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self)));
342 STRLEN kl;
343 1 const char *k = SvPV_const(key, kl);
344 1 punk_cachefile_unlock(aTHX_ c, k, (uint32_t)kl);
345 }
346  
347 NV
348 # uncoverable statement
349 # uncoverable subroutine
350 _lock_wait(self)
351 SV *self
352 CODE:
353 {
354 /* a braced block with a temporary: `RETVAL = INT2PTR(...)->field;`
355 * as a bare one-liner mis-parses in xsubpp */
356 0 punk_cachefile *c = INT2PTR(punk_cachefile *, SvIV(SvRV(self))); /* # uncoverable statement */
357 0 ▶ RETVAL = c->lock_wait; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = c->lock_wait; /* # uncoverable statement */ 0 0 0
358 }
359 OUTPUT:
360 RETVAL
361  
362 # Force the reconciling sweep. Normally amortised onto writes; exposed so a
363 # test can assert the budget without waiting for the threshold.
364 void
365 _sweep(self)
366 SV *self
367 CODE:
368 2 pcf_sweep(aTHX_ INT2PTR(punk_cachefile *, SvIV(SvRV(self))));
369  
370 MODULE = Punk PACKAGE = Punk::Cache
371  
372 PROTOTYPES: DISABLE
373  
374 # The front end over a backend: punk_cachefront.h holds the whole of it, and
375 # its header comment says why this tier is worth being C.
376 #
377 # A hit on either shipped store lands in punk_cache.h / punk_cachefile.h with
378 # no method dispatch at all; any other backend is reached through call_method
379 # exactly as it always was, because pluggability is the point of the tier.
380  
381 # new($spec, %opt)
382 #
383 # $spec is `memory`, `file`, a class name, or a ready-made backend object.
384 # The options go to the backend, less `name`, which is ours: it is the
385 # invalidation topic, and a store without one invalidates locally.
386 SV *
387 new(class, ...)
388 SV *class
389 CODE:
390 {
391 76 AV *args = (AV *)sv_2mortal((SV *)newAV());
392 int i;
393 /* The arguments are copied off the stack BEFORE anything below can
394 * call Perl: a require or a backend constructor may reallocate the
395 * value stack, and ST() would then be reading freed memory. */
396 488 ▶ for (i = 1; i < items; i++) av_push(args, newSVsv(ST(i)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) av_push(args, newSVsv(ST(i))); 412 76 488
397 76 RETVAL = pkc_new(aTHX_ class, args);
398 }
399 OUTPUT:
400 RETVAL
401  
402 void
403 DESTROY(self)
404 SV *self
405 CODE:
406 149 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 149 0 149
# uncoverable condition right count:1 class:ignore_covered_err 149 0 149
# uncoverable condition right count:1 class:ignore_covered_err 148 1 149
407 148 # uncoverable condition right count:2 class:ignore_covered_err
408 148 if (SvROK(self) && SvIOK(SvRV(self)) && SvIV(SvRV(self))) {
409 pkc_free(aTHX_ INT2PTR(punk_cachefront *, SvIV(SvRV(self))));
410 sv_setiv(SvRV(self), 0);
411 }
412  
413 # The compile step hands over what the `cache` keyword recorded. Separate from
414 # new() so the keyword's shape stays the keyword's business.
415 SV *
416 _from_spec(class, spec, opts, name = &PL_sv_undef)
417 SV *class
418 SV *spec
419 SV *opts
420 SV *name
421 CODE:
422 {
423 95 AV *args = (AV *)sv_2mortal((SV *)newAV());
424 95 av_push(args, newSVsv(spec));
425 95 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 95 0 95
# uncoverable condition right count:1 class:ignore_covered_err 94 1 95
# uncoverable condition right count:1 class:ignore_covered_err 94 0 94
426 94 # uncoverable condition right count:3 class:ignore_covered_err
427 if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) {
428 94 HV *h = (HV *)SvRV(opts);
429 234 ▶ HE *e;
Branch: 1 branch
BranchTrueFalseTotal
HE *e; 140 94 234
430 140 hv_iterinit(h);
431 140 while ((e = hv_iternext(h))) {
432 av_push(args, newSVsv(hv_iterkeysv(e)));
433 av_push(args, newSVsv(HeVAL(e)));
434 95 }
435 95 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 95 0 95
436 95 av_push(args, newSVpvs("name"));
437 av_push(args, SvOK(name) ? newSVsv(name) : newSVpvs("default")); /* # uncoverable condition right count:1 class:ignore_covered_err */
438 RETVAL = pkc_new(aTHX_ class, args);
439 }
440 OUTPUT:
441 RETVAL
442  
443 # 512M / 2G / 1024 - because everybody writes it the first way, and a cache
444 # configured in bare bytes by hand is a cache configured wrong by an order of
445 # magnitude. undef when it is not a size at all.
446 SV *
447 _bytes(v)
448 SV *v
449 CODE:
450 {
451 5 NV b = 0.0;
452 5 ▶ if (!pkc_bytes(aTHX_ v, &b)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!pkc_bytes(aTHX_ v, &b)) XSRETURN_UNDEF; 1 4 5
453 4 RETVAL = pkc_bytes_sv(aTHX_ b);
454 }
455 OUTPUT:
456 RETVAL
457  
458 # The underlying store, for its own statistics or anything backend-specific.
459 SV *
460 backend(self)
461 SV *self
462 CODE:
463 72 RETVAL = newSVsv(pkc_of(aTHX_ self)->backend);
464 OUTPUT:
465 RETVAL
466  
467 # This process's invalidation token. Private, and exposed only so a test can
468 # assert the property the pool depends on: that a store forked into two
469 # workers does not carry one token between them - see punk_cachefront.h.
470 SV *
471 _origin(self)
472 SV *self
473 CODE:
474 3 RETVAL = newSVsv(pkc_origin(aTHX_ pkc_of(aTHX_ self)));
475 OUTPUT:
476 RETVAL
477  
478 SV *
479 get(self, key)
480 SV *self
481 SV *key
482 CODE:
483 {
484 67 SV *v = pkc_get(aTHX_ pkc_of(aTHX_ self), key);
485 61 ▶ if (!v) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) XSRETURN_UNDEF; 9 52 61
486 52 RETVAL = v;
487 }
488 OUTPUT:
489 RETVAL
490  
491 SV *
492 set(self, key, value = &PL_sv_undef, ttl = 0)
493 SV *self
494 SV *key
495 SV *value
496 NV ttl
497 CODE:
498 {
499 291 SV *r = pkc_set(aTHX_ pkc_of(aTHX_ self), key, value, ttl);
500 290 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 290 290
501 290 # uncoverable condition left count:1 class:ignore_covered_err
502 if (!r) XSRETURN_UNDEF;
503 RETVAL = r;
504 }
505 OUTPUT:
506 RETVAL
507  
508 SV *
509 delete(self, key)
510 SV *self
511 SV *key
512 CODE:
513 {
514 20 SV *r = pkc_delete(aTHX_ pkc_of(aTHX_ self), key);
515 20 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 0 20 20
516 20 # uncoverable condition left count:1 class:ignore_covered_err
517 if (!r) XSRETURN_UNDEF;
518 RETVAL = r;
519 }
520 OUTPUT:
521 RETVAL
522  
523 SV *
524 clear(self)
525 SV *self
526 CODE:
527 {
528 3 SV *r = pkc_clear(aTHX_ pkc_of(aTHX_ self));
529 3 ▶ if (!r) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) XSRETURN_UNDEF; 1 2 3
530 2 RETVAL = r;
531 }
532 OUTPUT:
533 RETVAL
534  
535 # The backend's own statistics, plus what only this tier knows: whether the
536 # pool is connected at all, and what this worker has sent and received.
537 # Without those an operator cannot tell a coherent pool from one where every
538 # worker is quietly serving its own stale copy.
539 void
540 stats(self)
541 SV *self
542 PPCODE:
543 {
544 41 punk_cachefront *f = pkc_of(aTHX_ self);
545 41 AV *s = pkc_be_stats(aTHX_ f);
546 41 SSize_t n = av_len(s) + 1, i;
547 41 int pool = pkc_pool(aTHX);
548 /* Both calls above run Perl, which can have grown - and so
549 * reallocated - the value stack. SP was captured on entry, so it is
550 * re-derived from ax rather than trusted. */
551 41 SP = PL_stack_base + ax - 1;
552 /* n from the backend, 8 for this tier's four pairs, 18 for the tier's
553 * nine. Counted rather than estimated: a DEBUGGING perl records the
554 * high-water mark an XSUB asked for and PANICS if it pushes past it,
555 * so being four short is not a quiet overrun on the smokers, it is
556 * `failed to extend arg stack` and a dead test file. */
557 41 ▶ EXTEND(SP, n + 8 + 18);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n + 8 + 18); 41 0 41
EXTEND(SP, n + 8 + 18); 0 41 41
558 747 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 706 41 747
559 706 SV *e = *av_fetch(s, i, 0);
560 /* `hits` is the CACHE's, not the backend's: a read the tier
561 * answered is a hit that happens to have cost nothing, and a
562 * caller measuring its hit rate does not care which half of the
563 * store served it. Rewritten rather than reported twice, so
564 * hits + misses still equals the number of reads - `misses` needs
565 * no adjusting, because a read only reaches the backend, and only
566 * misses there, when the tier did not have it. */
567 706 ▶ if (f->tier && i > 0 && (i & 1)
Branch: 3 branches
BranchTrueFalseTotal
if (f->tier && i > 0 && (i & 1) 288 418 706
if (f->tier && i > 0 && (i & 1) 272 16 288
if (f->tier && i > 0 && (i & 1) 144 128 272
568 288 ▶ && strEQ(SvPV_nolen(*av_fetch(s, i - 1, 0)), "hits")) {
Branch: 1 branch
BranchTrueFalseTotal
&& strEQ(SvPV_nolen(*av_fetch(s, i - 1, 0)), "hits")) { 16 128 144
569 16 PUSHs(sv_2mortal(newSVuv(SvUV(e) + (UV)f->tier->hits)));
570 16 continue;
571 }
572 690 PUSHs(sv_2mortal(newSVsv(e)));
573 }
574 41 ▶ mPUSHp("shared", 6); mPUSHi(f->shared ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
mPUSHp("shared", 6); mPUSHi(f->shared ? 1 : 0); 9 32 41
575 41 mPUSHp("pool", 4); mPUSHi(pool);
576 41 mPUSHp("invalidations_sent", 18); mPUSHu(f->sent);
577 41 mPUSHp("invalidations_received", 22);mPUSHu(f->received);
578 /* The tier's own counters, and only when there is one: a store
579 * without a tier should not sprout keys that are always zero.
580 *
581 * `hits` and `misses` above stay the BACKEND's, so on a tiered store
582 * they count the reads that got past the tier. The effective hit rate
583 * is (memory_hits + hits) / (memory_hits + hits + misses) - said in
584 * the POD, because a tier whose hit rate nobody can see is 64MB a
585 * worker that nobody can justify. */
586 41 ▶ if (f->tier) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->tier) { 16 25 41
587 16 punk_cache *t = f->tier;
588 16 punk_cache_check_fork(aTHX_ t);
589 16 mPUSHp("memory_hits", 11); mPUSHu((UV)t->hits);
590 16 mPUSHp("memory_misses", 13); mPUSHu((UV)t->misses);
591 16 mPUSHp("memory_evictions", 16); mPUSHu((UV)t->evictions);
592 16 mPUSHp("memory_refused", 14); mPUSHu((UV)t->refused);
593 16 mPUSHp("memory_expired", 14); mPUSHu((UV)t->expired);
594 16 mPUSHp("memory_bytes", 12); mPUSHu((UV)t->bytes);
595 16 mPUSHp("memory_entries", 14); mPUSHu((UV)t->entries);
596 16 mPUSHp("memory_max_bytes", 16); mPUSHu((UV)t->max_bytes);
597 16 mPUSHp("memory_ttl", 10); mPUSHn(f->tier_ttl);
598 }
599 }
600  
601 # The backend's reconciling sweep, forwarded. Private, and here so that the
602 # conformance battery can assert the byte budget through the front end on
603 # exactly the terms it asserts it through the store.
604 void
605 _sweep(self)
606 SV *self
607 CODE:
608 {
609 1 punk_cachefront *f = pkc_of(aTHX_ self);
610 1 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 1 0 1
611 0 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 0 0
612 0 if (f->kind == PKC_K_FILE) pcf_sweep(aTHX_ pkc_file(aTHX_ f));
613 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
614 # uncoverable branch true
615 # uncoverable branch false
616 # uncoverable condition left count:1
617 # uncoverable condition right count:1
618 else if (f->caps & PKC_CAN_SWEEP) {
619 SV *r = pcx_call_meth(aTHX_ f->backend, "_sweep", NULL, 0, 0); /* # uncoverable statement */
620 # uncoverable statement
621 # uncoverable branch true
622 # uncoverable branch false
623 # uncoverable condition left count:1
624 # uncoverable condition right count:1
625 if (r) SvREFCNT_dec(r);
626 }
627 }
628  
629 # compute($key, $ttl, $code, %opt)
630 #
631 # Get, and on a miss run the code, store the result and return it. `json => 1`
632 # encodes and decodes a structure around the store.
633 SV *
634 compute(self, key, ttl, code, ...)
635 SV *self
636 SV *key
637 NV ttl
638 SV *code
639 CODE:
640 {
641 16 int json = 0, i;
642 SV *v;
643 18 ▶ for (i = 4; i + 1 < items; i += 2)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 4; i + 1 < items; i += 2) 2 16 18
644 4 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 2 0 2
# uncoverable branch false 2 0 2
645 2 # uncoverable condition right count:1 class:ignore_covered_err
646 16 # uncoverable condition right count:2 class:ignore_covered_err
647 14 ▶ if (strEQ(SvPV_nolen(ST(i)), "json") && SvTRUE(ST(i + 1)))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(SvPV_nolen(ST(i)), "json") && SvTRUE(ST(i + 1))) 2 12 14
648 12 json = 1;
649 v = pkc_compute(aTHX_ pkc_of(aTHX_ self), key, ttl, code, json);
650 if (!v) XSRETURN_UNDEF;
651 RETVAL = v;
652 }
653 OUTPUT:
654 RETVAL
655  
656 # The invalidation seam. Its own topic namespace, so a cache cannot be
657 # emptied by an application publishing to a name that happens to collide.
658  
659 IV
660 # uncoverable statement
661 # uncoverable subroutine
662 _invalidate(self, name, key)
663 SV *self
664 SV *name
665 SV *key
666 CODE:
667 PERL_UNUSED_VAR(self);
668 0 ▶ RETVAL = punk_bus_cache_publish(aTHX_ name, key); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_bus_cache_publish(aTHX_ name, key); /* # uncoverable statement */ 0 0 0
669 OUTPUT:
670 RETVAL
671  
672 # The callback is invoked with the key to drop. It MUST NOT publish in turn -
673 # the subscriber talks to the backend directly - or an invalidation would
674 # echo around the pool forever.
675 IV
676 # uncoverable statement
677 # uncoverable subroutine
678 _on_invalidate(self, name, cb)
679 SV *self
680 SV *name
681 SV *cb
682 CODE:
683 PERL_UNUSED_VAR(self);
684 0 ▶ RETVAL = punk_bus_cache_subscribe(aTHX_ name, cb); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_bus_cache_subscribe(aTHX_ name, cb); /* # uncoverable statement */ 0 0 0
685 OUTPUT:
686 RETVAL
687  
688 # Whether there is a pool to invalidate across at all.
689 IV
690 # uncoverable statement
691 # uncoverable subroutine
692 _pool_live(self = &PL_sv_undef)
693 SV *self
694 CODE:
695 PERL_UNUSED_VAR(self);
696 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
697 # uncoverable condition left count:1
698 # uncoverable condition right count:1
699 RETVAL = punk_bus_live(aTHX) ? 1 : 0;
700 OUTPUT:
701 RETVAL
« xs/request.xs ↑ Summary include/punk/punk_obs.h »