include/punk/punk_filecache.h

filter: Statementstmt 87.7% 145 / 172 (6 uncoverable) Branchbran 71.3% 94 / 150 (21 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.1% 239 / 322 (27 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/idempotency.xs ↑ Summary Punk.xs »
1 /* punk_filecache.h - a per-worker cache of small static files: their stat,
2 * and their content.
3 *
4 * A static file hit is dominated by syscalls. Profiled under load, `open` was
5 * 55% of the whole request, the PerlIO layer around it another 3.5%, sendfile
6 * 7.7% and the close 1.2%. Holding the CONTENT removed all of it and left
7 * `stat` as the last file syscall on the path, at 15%; holding the STAT
8 * removes that one too, along with the sibling probes that go looking for a
9 * .gz that is usually not there.
10 *
11 * TWO CACHES, TWO DIFFERENT BARGAINS.
12 *
13 * The CONTENT cache is EXACTLY correct. It re-reads whenever the file's
14 * (dev, ino, mtime, size) changes, and it learns that from a stat the
15 * caller had to do anyway - so validating costs no syscall and it never
16 * serves bytes that disagree with the file it just looked at.
17 *
18 * The STAT cache is EVENTUALLY correct, bounded by PFC_STAT_TTL (1 second
19 * by default): inside that second a change on disk is not looked for.
20 * PUNK_STATIC_STAT_TTL=0 goes back to statting every request - still with
21 * the content cache, so still without the open - which is the exactly
22 * correct configuration at most of the speed.
23 *
24 * WHY CONTENT AND NOT AN OPEN DESCRIPTOR. Caching the descriptor and handing
25 * out `dup`s looks better: a dup is 33x cheaper than an open (0.187us against
26 * 6.205us, measured) and it keeps the sendfile path. It is also wrong, in the
27 * way that only shows up in production - a dup is a new descriptor onto the
28 * SAME open file description, so it SHARES THE OFFSET. Two concurrent
29 * requests for one file read through each other, and rewinding for one seeks
30 * the other back to the start mid-body. No sequential test can see it.
31 * Content has no offset, so the class goes away.
32 *
33 * WHY NOT fstat FOR VALIDATION. The question is "is the file at this PATH
34 * still the file I read", and fstat describes the inode already held - so a
35 * deploy that replaces the file leaves it reporting, truthfully and
36 * uselessly, that nothing has changed. Statting the PATH catches a
37 * replacement exactly, because a replacement gets a new inode.
38 *
39 * WHAT IT DOES NOT CACHE. Anything over PFC_MAX_FILE keeps the existing open
40 * + sendfile path, which is already zero-copy and right for a large file:
41 * caching a video would evict a site's whole stylesheet set to save one
42 * request, and lose the kernel's own page cache doing it. A stat entry is
43 * kept for any path, at any size.
44 *
45 * NEGATIVE ENTRIES ARE THE POINT. The precompressed sibling probe asks for
46 * `style.css.br` and `style.css.gz` on every request from every browser,
47 * because every browser sends Accept-Encoding, and on a site with no build
48 * step neither has ever existed. Remembering they are absent is worth two
49 * syscalls a request.
50 *
51 * It fails open everywhere: too big, unreadable, budget refused, all fall
52 * back to the syscall this sits in front of.
53 */
54  
55 #ifndef PUNK_FILECACHE_H
56 #define PUNK_FILECACHE_H
57  
58 /* The fraction of a second in a stat's mtime, where the platform keeps one.
59 * A whole-second mtime cannot see a file rewritten in place, to the same
60 * size, inside the second it was last written in; this can. Where there is
61 * none it is 0 and that one rewrite stays invisible until the ctime moves. */
62 #if defined(PUNK_STAT_MTIM)
63 # define PFC_MTIME_NS(st) ((UV)(st).st_mtim.tv_nsec)
64 #elif defined(PUNK_STAT_MTIMESPEC)
65 # define PFC_MTIME_NS(st) ((UV)(st).st_mtimespec.tv_nsec)
66 #elif defined(PUNK_STAT_MTIMENSEC)
67 # define PFC_MTIME_NS(st) ((UV)(st).st_mtimensec)
68 #else
69 # define PFC_MTIME_NS(st) ((UV)0)
70 #endif
71  
72 /* Budgets. Deliberately small: this is a hot-asset cache, not a CDN, and the
73 * memory is per worker. A site whose CSS/JS/icons do not fit in 8MB is serving
74 * them from the wrong place. */
75 #define PFC_MAX_BYTES (8 * 1024 * 1024)
76 #define PFC_MAX_FILE (512 * 1024)
77 #define PFC_BUCKETS 64
78 #define PFC_KEY_MAX 1024 /* longer paths simply do not cache */
79  
80 /* How long a stat is believed. One second, not the sixty nginx defaults to:
81 * the whole cost this avoids is a syscall that takes a microsecond, so a
82 * second of it is already all of the win, and the shorter the window the less
83 * there is to explain to somebody who edited a file and reloaded. */
84 #define PFC_STAT_TTL 1.0
85  
86 typedef struct pfc_entry {
87 struct pfc_entry *hnext; /* bucket chain */
88 struct pfc_entry *prev, *next; /* recency list; head is newest */
89 uint64_t hash;
90 SV *val; /* the bytes, or NULL: an entry may
91 hold only a stat, or only the fact
92 that there is nothing here */
93 Stat_t st; /* the last stat taken of this path */
94 UV vns, vctime; /* the rest of val's identity: its
95 mtime's nanoseconds and its ctime */
96 double checked; /* when st was taken */
97 unsigned char exists; /* 0 = a remembered absence */
98 /* the validators for `st`, formatted once per stat rather than once
99 * per hit (an ETag is a snprintf, a Last-Modified a gmtime_r and
100 * another); hdr_ok says they belong to the st held now */
101 unsigned char hdr_ok;
102 char hdr_etag[64];
103 char hdr_date[40];
104 uint32_t klen;
105 char *key;
106 } pfc_entry;
107  
108 typedef struct {
109 pfc_entry *buckets[PFC_BUCKETS];
110 pfc_entry *head, *tail; /* newest .. oldest */
111 uint32_t entries;
112 size_t bytes; /* held, incrementally maintained */
113 int disabled;
114 IV owner_pid;
115 uint64_t hits, misses, evictions, stale, stat_hits, stat_misses;
116 } punk_filecache;
117  
118 /* One per worker process: every mount in the process should share one memory
119 * budget rather than each keeping its own. */
120 static punk_filecache PFC_CACHE;
121  
122 /* Counting only the bytes would let a thousand tiny files - or a thousand
123 * remembered absences, which carry no bytes at all - sit under an "8MB" cap
124 * while costing far more in structs and allocator headers. */
125 #define PFC_ENTRY_OVERHEAD (sizeof(pfc_entry) + 64)
126  
127 211 static size_t pfc_cost(uint32_t klen, UV vlen) {
128 211 return PFC_ENTRY_OVERHEAD + (size_t)klen + (size_t)vlen;
129 }
130  
131 /* FNV-1a. The keys are filesystem paths - short, sharing long prefixes. */
132 420 static uint64_t pfc_hash(const char *s, size_t n) {
133 420 uint64_t h = 1469598103934665603ULL;
134 size_t i;
135 11485 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11065 420 11485
136 11065 h ^= (unsigned char)s[i];
137 11065 h *= 1099511628211ULL;
138 }
139 420 return h;
140 }
141  
142 236 static double pfc_now(void) {
143 struct timeval tv;
144 236 gettimeofday(&tv, NULL);
145 236 return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
146 }
147  
148 163 static void pfc_unlink_lru(punk_filecache *c, pfc_entry *e) {
149 163 ▶ if (e->prev) e->prev->next = e->next; else c->head = e->next;
Branch: 1 branch
BranchTrueFalseTotal
if (e->prev) e->prev->next = e->next; else c->head = e->next; 60 103 163
150 163 ▶ if (e->next) e->next->prev = e->prev; else c->tail = e->prev;
Branch: 1 branch
BranchTrueFalseTotal
if (e->next) e->next->prev = e->prev; else c->tail = e->prev; 137 26 163
151 163 e->prev = e->next = NULL;
152 163 }
153  
154 245 static void pfc_push_front(punk_filecache *c, pfc_entry *e) {
155 245 e->prev = NULL;
156 245 e->next = c->head;
157 245 ▶ if (c->head) c->head->prev = e;
Branch: 1 branch
BranchTrueFalseTotal
if (c->head) c->head->prev = e; 214 31 245
158 245 c->head = e;
159 245 ▶ if (!c->tail) c->tail = e;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tail) c->tail = e; 31 214 245
160 245 }
161  
162 /* The bytes an entry is charged for: a stat-only or negative entry holds
163 * none, and must not be charged as though it did. */
164 /*
165 # uncoverable statement
166 # uncoverable subroutine */
167 0 static UV pfc_vlen(pfc_entry *e) {
168 /*
169 # uncoverable statement
170 # uncoverable condition left count:1
171 # uncoverable condition right count:1 */
172 0 ▶ return e->val ? (UV)SvCUR(e->val) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return e->val ? (UV)SvCUR(e->val) : 0; 0 0 0
173 }
174  
175 /*
176 # uncoverable statement
177 # uncoverable subroutine */
178 0 static void pfc_drop(pTHX_ punk_filecache *c, pfc_entry *e) {
179 0 pfc_entry **pp = &c->buckets[e->hash % PFC_BUCKETS]; /* # uncoverable statement */
180 /*
181 # uncoverable statement
182 # uncoverable branch true
183 # uncoverable branch false
184 # uncoverable condition left count:1
185 # uncoverable condition right count:1
186 # uncoverable condition left count:2
187 # uncoverable condition right count:2 */
188 0 ▶ while (*pp && *pp != e) pp = &(*pp)->hnext;
Branch: 2 branches
BranchTrueFalseTotal
while (*pp && *pp != e) pp = &(*pp)->hnext; 0 0 0
while (*pp && *pp != e) pp = &(*pp)->hnext; 0 0 0
189 /*
190 # uncoverable statement
191 # uncoverable branch true
192 # uncoverable branch false
193 # uncoverable condition left count:1
194 # uncoverable condition right count:1 */
195 0 ▶ if (*pp == e) *pp = e->hnext;
Branch: 1 branch
BranchTrueFalseTotal
if (*pp == e) *pp = e->hnext; 0 0 0
196 0 pfc_unlink_lru(c, e); /* # uncoverable statement */
197 0 c->bytes -= pfc_cost(e->klen, pfc_vlen(e)); /* # uncoverable statement */
198 /*
199 # uncoverable statement
200 # uncoverable branch true
201 # uncoverable branch false
202 # uncoverable condition left count:1
203 # uncoverable condition right count:1 */
204 0 ▶ if (e->val) SvREFCNT_dec(e->val);
Branch: 1 branch
BranchTrueFalseTotal
if (e->val) SvREFCNT_dec(e->val); 0 0 0
205 0 Safefree(e->key); /* # uncoverable statement */
206 0 Safefree(e); /* # uncoverable statement */
207 0 c->entries--; /* # uncoverable statement */
208 0 }
209  
210 /* Release just the content, keeping the entry and its stat. What happens when
211 * a file changes: the bytes are wrong, the path is still worth remembering. */
212 7 static void pfc_drop_val(pTHX_ punk_filecache *c, pfc_entry *e) {
213 /*
214 # uncoverable branch true
215 # uncoverable condition left count:1 class:ignore_covered_err */
216 7 ▶ if (!e->val) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->val) return; 0 7 7
217 7 c->bytes -= (size_t)SvCUR(e->val);
218 7 SvREFCNT_dec(e->val);
219 7 e->val = NULL;
220 }
221  
222 /* The escape hatches, read once.
223 *
224 * OPS knobs rather than app ones: the reason to want either off is how a host
225 * publishes files, which is a property of the machine and not of the
226 * application's source.
227 *
228 * PUNK_NO_STATIC_FILE_CACHE=1 everything off, exactly the old path
229 * PUNK_STATIC_STAT_TTL=0 stat every request; keep the content cache,
230 * which is exactly correct on its own
231 * PUNK_STATIC_STAT_TTL=<secs> believe a stat for that long
232 */
233 422 static int pfc_enabled(void) {
234 static int cached = -1;
235 422 ▶ if (cached < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached < 0) { 20 402 422
236 20 const char *v = getenv("PUNK_NO_STATIC_FILE_CACHE");
237 /*
238 # uncoverable condition right count:1 class:ignore_covered_err
239 # uncoverable condition left count:2 class:ignore_covered_err
240 # uncoverable condition right count:2 class:ignore_covered_err
241 # uncoverable condition left count:3 class:ignore_covered_err
242 # uncoverable condition right count:3 class:ignore_covered_err */
243 20 ▶ cached = (v && *v && *v != '0') ? 0 : 1;
Branch: 3 branches
BranchTrueFalseTotal
cached = (v && *v && *v != '0') ? 0 : 1; 0 20 20
cached = (v && *v && *v != '0') ? 0 : 1; 0 0 0
cached = (v && *v && *v != '0') ? 0 : 1; 0 0 0
244 }
245 422 return cached;
246 }
247  
248 135 static double pfc_stat_ttl(void) {
249 static double cached = -1.0;
250 135 ▶ if (cached < 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached < 0.0) { 9 126 135
251 9 const char *v = getenv("PUNK_STATIC_STAT_TTL");
252 9 ▶ if (v && *v) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 7 9
if (v && *v) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
253 2 double d = atof(v);
254 2 ▶ cached = d > 0.0 ? d : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
cached = d > 0.0 ? d : 0.0; 1 1 2
255 }
256 7 else cached = PFC_STAT_TTL;
257 }
258 135 return cached;
259 }
260  
261 /* After a fork the entries are still valid - immutable bytes, and every hit
262 * revalidates them - so a worker inherits a warm cache rather than starting
263 * cold. Only the counters are per process. */
264 311 static void pfc_check_fork(pTHX_ punk_filecache *c) {
265 311 IV me = (IV)PerlProc_getpid();
266 PERL_UNUSED_CONTEXT;
267 311 ▶ if (c->owner_pid == me) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->owner_pid == me) return; 291 20 311
268 20 c->hits = c->misses = c->evictions = c->stale = 0;
269 20 c->stat_hits = c->stat_misses = 0;
270 20 c->owner_pid = me;
271 }
272  
273 420 static pfc_entry *pfc_lookup(punk_filecache *c, const char *path, STRLEN plen,
274 uint64_t h) {
275 pfc_entry *e;
276 433 ▶ for (e = c->buckets[h % PFC_BUCKETS]; e; e = e->hnext) {
Branch: 1 branch
BranchTrueFalseTotal
for (e = c->buckets[h % PFC_BUCKETS]; e; e = e->hnext) { 351 82 433
277 351 ▶ if (e->hash != h || e->klen != (uint32_t)plen) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (e->hash != h || e->klen != (uint32_t)plen) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 338 13 351
if (e->hash != h || e->klen != (uint32_t)plen) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 338 338
278 /*
279 # uncoverable branch false
280 # uncoverable condition right count:1 class:ignore_covered_err */
281 338 ▶ if (memcmp(e->key, path, plen) == 0) return e;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(e->key, path, plen) == 0) return e; 338 0 338
282 }
283 82 return NULL;
284 }
285  
286 82 static pfc_entry *pfc_new(pTHX_ punk_filecache *c, const char *path,
287 STRLEN plen, uint64_t h) {
288 pfc_entry *e;
289 PERL_UNUSED_CONTEXT;
290 /*
291 # uncoverable branch true class:ignore_covered_err
292 # uncoverable condition left count:1 class:ignore_covered_err
293 # uncoverable condition left count:2 class:ignore_covered_err
294 # uncoverable condition right count:2 class:ignore_covered_err */
295 82 ▶ while (c->bytes + pfc_cost((uint32_t)plen, 0) > PFC_MAX_BYTES && c->tail)
Branch: 2 branches
BranchTrueFalseTotal
while (c->bytes + pfc_cost((uint32_t)plen, 0) > PFC_MAX_BYTES && c->tail) 0 82 82
while (c->bytes + pfc_cost((uint32_t)plen, 0) > PFC_MAX_BYTES && c->tail) 0 0 0
296 {
297 0 pfc_drop(aTHX_ c, c->tail); /* # uncoverable statement */
298 0 c->evictions++; /* # uncoverable statement */
299 }
300 82 Newxz(e, 1, pfc_entry);
301 82 Newx(e->key, plen + 1, char);
302 82 memcpy(e->key, path, plen);
303 82 e->key[plen] = '\0';
304 82 e->klen = (uint32_t)plen;
305 82 e->hash = h;
306 82 e->hnext = c->buckets[h % PFC_BUCKETS];
307 82 c->buckets[h % PFC_BUCKETS] = e;
308 82 pfc_push_front(c, e);
309 82 c->entries++;
310 82 c->bytes += pfc_cost((uint32_t)plen, 0);
311 82 return e;
312 }
313  
314 /* Is the identity in `st` the same file as `b`? */
315 93 static int pfc_same(const Stat_t *a, const Stat_t *b) {
316 93 return (UV)a->st_dev == (UV)b->st_dev
317 93 ▶ && (UV)a->st_ino == (UV)b->st_ino
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)a->st_ino == (UV)b->st_ino 91 2 93
318 91 ▶ && (UV)a->st_mtime == (UV)b->st_mtime
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)a->st_mtime == (UV)b->st_mtime 89 2 91
319 89 ▶ && PFC_MTIME_NS(*a) == PFC_MTIME_NS(*b)
Branch: 1 branch
BranchTrueFalseTotal
&& PFC_MTIME_NS(*a) == PFC_MTIME_NS(*b) 82 7 89
320 82 ▶ && (UV)a->st_ctime == (UV)b->st_ctime
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)a->st_ctime == (UV)b->st_ctime 82 0 82
321 186 ▶ && (UV)a->st_size == (UV)b->st_size;
Branch: 2 branches
BranchTrueFalseTotal
&& (UV)a->st_size == (UV)b->st_size; 93 0 93
&& (UV)a->st_size == (UV)b->st_size; 82 0 82
322 }
323  
324 /* stat(2), possibly from memory. 1 = *out filled and the path exists; 0 = it
325 * does not exist or could not be statted, which the caller must treat exactly
326 * as a failed stat.
327 *
328 * Within the TTL this answers without a syscall, including for a path that is
329 * KNOWN ABSENT - which is what makes the precompressed-sibling probe free on
330 * the sites that have no siblings.
331 *
332 * When it does stat and the file has changed, it releases the cached content
333 * here, so everything downstream can trust that content and stat agree. */
334 217 static int pfc_stat(pTHX_ const char *path, STRLEN plen, Stat_t *out) {
335 217 punk_filecache *c = &PFC_CACHE;
336 uint64_t h;
337 pfc_entry *e;
338 Stat_t fresh;
339 int ok;
340  
341 /*
342 # uncoverable branch true
343 # uncoverable condition left count:1 class:ignore_covered_err
344 # uncoverable condition left count:2 class:ignore_covered_err
345 # uncoverable condition left count:3 class:ignore_covered_err
346 # uncoverable condition left count:4 class:ignore_covered_err */
347 217 ▶ if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX)
Branch: 4 branches
BranchTrueFalseTotal
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 217 0 217
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 217 0 217
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 217 0 217
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 0 217 217
348 0 return PerlLIO_stat(path, out) == 0 ? 1 : 0; /* # uncoverable statement */
349  
350 217 pfc_check_fork(aTHX_ c);
351 217 h = pfc_hash(path, plen);
352 217 e = pfc_lookup(c, path, plen, h);
353  
354 217 ▶ if (e && (pfc_now() - e->checked) < pfc_stat_ttl()) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && (pfc_now() - e->checked) < pfc_stat_ttl()) { 135 82 217
if (e && (pfc_now() - e->checked) < pfc_stat_ttl()) { 116 19 135
355 116 pfc_unlink_lru(c, e);
356 116 pfc_push_front(c, e);
357 116 c->stat_hits++;
358 116 ▶ if (!e->exists) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->exists) return 0; 8 108 116
359 108 *out = e->st;
360 108 return 1;
361 }
362  
363 101 c->stat_misses++;
364 101 ok = PerlLIO_stat(path, &fresh) == 0;
365  
366 101 ▶ if (!e) e = pfc_new(aTHX_ c, path, plen, h);
Branch: 1 branch
BranchTrueFalseTotal
if (!e) e = pfc_new(aTHX_ c, path, plen, h); 82 19 101
367 19 ▶ else if (e->val && (!ok || !pfc_same(&fresh, &e->st))) {
Branch: 3 branches
BranchTrueFalseTotal
else if (e->val && (!ok || !pfc_same(&fresh, &e->st))) { 17 2 19
else if (e->val && (!ok || !pfc_same(&fresh, &e->st))) { 15 2 17
else if (e->val && (!ok || !pfc_same(&fresh, &e->st))) { 5 10 15
368 /* the bytes we hold are no longer this file's */
369 7 c->stale++;
370 7 pfc_drop_val(aTHX_ c, e);
371 }
372 101 e->checked = pfc_now();
373 /* a different stat means different validators */
374 101 ▶ if (!ok || !e->exists || !pfc_same(&fresh, &e->st)) e->hdr_ok = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!ok || !e->exists || !pfc_same(&fresh, &e->st)) e->hdr_ok = 0; 74 27 101
if (!ok || !e->exists || !pfc_same(&fresh, &e->st)) e->hdr_ok = 0; 16 58 74
if (!ok || !e->exists || !pfc_same(&fresh, &e->st)) e->hdr_ok = 0; 6 10 16
375 101 e->exists = ok ? 1 : 0;
376 101 ▶ if (ok) { e->st = fresh; *out = fresh; }
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { e->st = fresh; *out = fresh; } 74 27 101
377 101 return ok;
378 }
379  
380 /* The file's bytes as an SV the caller owns (+1), or NULL to say "read it
381 * yourself". NULL is never "the file is missing".
382 *
383 * dev/ino/mtime/ns/ctime/size are the stat the caller is serving this request
384 * from - fresh, or from pfc_stat within the TTL - and are the check that the
385 * bytes held belong to that same file.
386 *
387 * The returned SV is newSVsv of the held one, which on any perl with
388 * copy-on-write strings (5.20+) is O(1) and shares the buffer until something
389 * writes to it. On an older perl it is a real copy, correct and still cheaper
390 * than the open it replaced. */
391 96 static SV *pfc_get(pTHX_ const char *path, STRLEN plen,
392 UV dev, UV ino, UV mtime, UV ns, UV ctime, UV size) {
393 96 punk_filecache *c = &PFC_CACHE;
394 uint64_t h;
395 pfc_entry *e;
396 PerlIO *fp;
397 SV *val;
398  
399 /*
400 # uncoverable branch true
401 # uncoverable condition left count:1 class:ignore_covered_err
402 # uncoverable condition left count:2 class:ignore_covered_err */
403 96 ▶ if (c->disabled || !pfc_enabled()) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (c->disabled || !pfc_enabled()) return NULL; 96 0 96
if (c->disabled || !pfc_enabled()) return NULL; 0 96 96
404 /*
405 # uncoverable branch true
406 # uncoverable condition left count:1 class:ignore_covered_err
407 # uncoverable condition left count:2 class:ignore_covered_err */
408 96 ▶ if (plen == 0 || plen >= PFC_KEY_MAX) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (plen == 0 || plen >= PFC_KEY_MAX) return NULL; 96 0 96
if (plen == 0 || plen >= PFC_KEY_MAX) return NULL; 0 96 96
409 96 ▶ if (size > PFC_MAX_FILE) return NULL; /* streams, as before */
Branch: 1 branch
BranchTrueFalseTotal
if (size > PFC_MAX_FILE) return NULL; /* streams, as before */ 2 94 96
410 94 pfc_check_fork(aTHX_ c);
411  
412 94 h = pfc_hash(path, plen);
413 94 e = pfc_lookup(c, path, plen, h);
414 94 ▶ if (e && e->val) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (e && e->val) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 94 0 94
if (e && e->val) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 47 47 94
415 /* Is what we hold this file? A new inode is a replaced file; a changed
416 * mtime or size is a rewritten one. */
417 /*
418 # uncoverable branch false
419 # uncoverable condition right count:1 class:ignore_covered_err
420 # uncoverable condition right count:2 class:ignore_covered_err
421 # uncoverable condition right count:3 class:ignore_covered_err
422 # uncoverable condition right count:4 class:ignore_covered_err
423 # uncoverable condition right count:5 class:ignore_covered_err
424 # uncoverable condition right count:6 class:ignore_covered_err */
425 47 ▶ if ((UV)e->st.st_dev == dev && (UV)e->st.st_ino == ino
Branch: 2 branches
BranchTrueFalseTotal
if ((UV)e->st.st_dev == dev && (UV)e->st.st_ino == ino 47 0 47
if ((UV)e->st.st_dev == dev && (UV)e->st.st_ino == ino 47 0 47
426 47 ▶ && (UV)e->st.st_mtime == mtime && (UV)e->st.st_size == size
Branch: 2 branches
BranchTrueFalseTotal
&& (UV)e->st.st_mtime == mtime && (UV)e->st.st_size == size 47 0 47
&& (UV)e->st.st_mtime == mtime && (UV)e->st.st_size == size 47 0 47
427 47 ▶ && e->vns == ns && e->vctime == ctime) {
Branch: 2 branches
BranchTrueFalseTotal
&& e->vns == ns && e->vctime == ctime) { 47 0 47
&& e->vns == ns && e->vctime == ctime) { 47 0 47
428 47 pfc_unlink_lru(c, e);
429 47 pfc_push_front(c, e);
430 47 c->hits++;
431 47 return newSVsv(e->val);
432 }
433 0 c->stale++; /* # uncoverable statement */
434 0 pfc_drop_val(aTHX_ c, e); /* # uncoverable statement */
435 }
436  
437 47 c->misses++;
438  
439 /* Not held: read it once, keep it, and serve from what we just read. This
440 * is the only request that pays. */
441 47 fp = PerlIO_open(path, "rb");
442 /*
443 # uncoverable branch true
444 # uncoverable condition left count:1 class:ignore_covered_err */
445 47 ▶ if (!fp) return NULL; /* caller's own missing-file */
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return NULL; /* caller's own missing-file */ 0 47 47
446  
447 47 ▶ val = newSV(size ? (STRLEN)size : 1); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
val = newSV(size ? (STRLEN)size : 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 47 0 47
448 47 SvPOK_on(val);
449 {
450 47 STRLEN got = 0;
451 94 ▶ while (got < (STRLEN)size) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < (STRLEN)size) { 47 47 94
452 47 SSize_t n = PerlIO_read(fp, SvPVX(val) + got, (STRLEN)size - got);
453 /*
454 # uncoverable branch true
455 # uncoverable condition left count:1 class:ignore_covered_err */
456 47 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 0 47 47
457 47 got += (STRLEN)n;
458 }
459 47 PerlIO_close(fp);
460 /*
461 # uncoverable branch true
462 # uncoverable condition left count:1 class:ignore_covered_err */
463 47 ▶ if (got != (STRLEN)size) {
Branch: 1 branch
BranchTrueFalseTotal
if (got != (STRLEN)size) { 0 47 47
464 /* The file changed under us between the stat and the read. Do not
465 * cache a half file, and do not serve one either - let the caller
466 * take the ordinary path and see it for itself. */
467 0 SvREFCNT_dec(val); /* # uncoverable statement */
468 0 return NULL; /* # uncoverable statement */
469 }
470 47 SvCUR_set(val, got);
471 47 SvPVX(val)[got] = '\0';
472 }
473  
474 /*
475 # uncoverable branch true
476 # uncoverable condition left count:1 class:ignore_covered_err */
477 47 ▶ if (pfc_cost((uint32_t)plen, size) > PFC_MAX_BYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (pfc_cost((uint32_t)plen, size) > PFC_MAX_BYTES) 0 47 47
478 0 return val; /* bigger than the budget: never hold it */ /* # uncoverable statement */
479  
480 /*
481 # uncoverable branch true
482 # uncoverable condition left count:1 class:ignore_covered_err */
483 47 ▶ if (!e) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { 0 47 47
484 0 e = pfc_new(aTHX_ c, path, plen, h); /* # uncoverable statement */
485 0 e->checked = pfc_now(); /* # uncoverable statement */
486 0 e->exists = 1; /* # uncoverable statement */
487 }
488 /*
489 # uncoverable branch true class:ignore_covered_err
490 # uncoverable condition left count:1 class:ignore_covered_err
491 # uncoverable condition left count:2 class:ignore_covered_err
492 # uncoverable condition right count:2 class:ignore_covered_err
493 # uncoverable condition left count:3 class:ignore_covered_err
494 # uncoverable condition right count:3 class:ignore_covered_err */
495 47 ▶ while (c->bytes + (size_t)size > PFC_MAX_BYTES && c->tail && c->tail != e) {
Branch: 3 branches
BranchTrueFalseTotal
while (c->bytes + (size_t)size > PFC_MAX_BYTES && c->tail && c->tail != e) { 0 47 47
while (c->bytes + (size_t)size > PFC_MAX_BYTES && c->tail && c->tail != e) { 0 0 0
while (c->bytes + (size_t)size > PFC_MAX_BYTES && c->tail && c->tail != e) { 0 0 0
496 0 pfc_drop(aTHX_ c, c->tail); /* # uncoverable statement */
497 0 c->evictions++; /* # uncoverable statement */
498 }
499 /* the stat this content belongs to, so pfc_get can check it next time */
500 47 e->st.st_dev = (dev_t)dev;
501 47 e->st.st_ino = (ino_t)ino;
502 47 e->st.st_mtime = (time_t)mtime;
503 47 e->st.st_size = (Off_t)size;
504 47 e->vns = ns;
505 47 e->vctime = ctime;
506 /* The entry keeps the SV it read and the caller gets a copy, the same
507 * shape a hit returns. Handing back the entry's own SV let a middleware
508 * that edits the body in place edit the cache, and every later hit
509 * served the edit. Read-only, so a write through some other path croaks
510 * rather than corrupts. */
511 47 e->val = val;
512 47 SvREADONLY_on(val);
513 47 c->bytes += (size_t)size;
514 47 return newSVsv(val);
515 }
516  
517 /* Forget when this path was last statted, so the next pfc_stat asks the
518 * filesystem. For a caller that has just seen the file disagree with the
519 * stat it was handed. */
520 1 static void pfc_expire(pTHX_ const char *path, STRLEN plen) {
521 1 punk_filecache *c = &PFC_CACHE;
522 pfc_entry *e;
523 PERL_UNUSED_CONTEXT;
524 /*
525 # uncoverable branch true
526 # uncoverable condition left count:1 class:ignore_covered_err
527 # uncoverable condition left count:2 class:ignore_covered_err
528 # uncoverable condition left count:3 class:ignore_covered_err
529 # uncoverable condition left count:4 class:ignore_covered_err */
530 1 ▶ if (c->disabled || !pfc_enabled() || plen == 0 || plen >= PFC_KEY_MAX)
Branch: 4 branches
BranchTrueFalseTotal
if (c->disabled || !pfc_enabled() || plen == 0 || plen >= PFC_KEY_MAX) 1 0 1
if (c->disabled || !pfc_enabled() || plen == 0 || plen >= PFC_KEY_MAX) 1 0 1
if (c->disabled || !pfc_enabled() || plen == 0 || plen >= PFC_KEY_MAX) 1 0 1
if (c->disabled || !pfc_enabled() || plen == 0 || plen >= PFC_KEY_MAX) 0 1 1
531 0 return; /* # uncoverable statement */
532 1 e = pfc_lookup(c, path, plen, pfc_hash(path, plen));
533 /*
534 # uncoverable branch false
535 # uncoverable condition right count:1 class:ignore_covered_err */
536 1 ▶ if (e) e->checked = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e) e->checked = 0; 1 0 1
537 }
538  
539 #endif /* PUNK_FILECACHE_H */
« xs/idempotency.xs ↑ Summary Punk.xs »