xs/cache.xs

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