include/punk/punk_filecache.h

filter: Statementstmt 84.3% 145 / 172 Branchbran 62.6% 94 / 150 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.2% 239 / 322 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/oamount.xs ↑ Summary include/punk/punk_xml.h »
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 182 static size_t pfc_cost(uint32_t klen, UV vlen) {
128 182 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 382 static uint64_t pfc_hash(const char *s, size_t n) {
133 382 uint64_t h = 1469598103934665603ULL;
134 size_t i;
135 10452 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10070 382 10452
136 10070 h ^= (unsigned char)s[i];
137 10070 h *= 1099511628211ULL;
138 }
139 382 return h;
140 }
141  
142 216 static double pfc_now(void) {
143 struct timeval tv;
144 216 gettimeofday(&tv, NULL);
145 216 return (double)tv.tv_sec + (double)tv.tv_usec / 1e6;
146 }
147  
148 152 static void pfc_unlink_lru(punk_filecache *c, pfc_entry *e) {
149 152 ▶ 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; 57 95 152
150 152 ▶ 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; 128 24 152
151 152 e->prev = e->next = NULL;
152 152 }
153  
154 223 static void pfc_push_front(punk_filecache *c, pfc_entry *e) {
155 223 e->prev = NULL;
156 223 e->next = c->head;
157 223 ▶ if (c->head) c->head->prev = e;
Branch: 1 branch
BranchTrueFalseTotal
if (c->head) c->head->prev = e; 197 26 223
158 223 c->head = e;
159 223 ▶ if (!c->tail) c->tail = e;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tail) c->tail = e; 26 197 223
160 223 }
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 0 static UV pfc_vlen(pfc_entry *e) {
165 0 ▶ return e->val ? (UV)SvCUR(e->val) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return e->val ? (UV)SvCUR(e->val) : 0; 0 0 0
166 }
167  
168 0 static void pfc_drop(pTHX_ punk_filecache *c, pfc_entry *e) {
169 0 pfc_entry **pp = &c->buckets[e->hash % PFC_BUCKETS];
170 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
171 0 ▶ if (*pp == e) *pp = e->hnext;
Branch: 1 branch
BranchTrueFalseTotal
if (*pp == e) *pp = e->hnext; 0 0 0
172 0 pfc_unlink_lru(c, e);
173 0 c->bytes -= pfc_cost(e->klen, pfc_vlen(e));
174 0 ▶ if (e->val) SvREFCNT_dec(e->val);
Branch: 1 branch
BranchTrueFalseTotal
if (e->val) SvREFCNT_dec(e->val); 0 0 0
175 0 Safefree(e->key);
176 0 Safefree(e);
177 0 c->entries--;
178 0 }
179  
180 /* Release just the content, keeping the entry and its stat. What happens when
181 * a file changes: the bytes are wrong, the path is still worth remembering. */
182 7 static void pfc_drop_val(pTHX_ punk_filecache *c, pfc_entry *e) {
183 7 ▶ if (!e->val) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->val) return; 0 7 7
184 7 c->bytes -= (size_t)SvCUR(e->val);
185 7 SvREFCNT_dec(e->val);
186 7 e->val = NULL;
187 }
188  
189 /* The escape hatches, read once.
190 *
191 * OPS knobs rather than app ones: the reason to want either off is how a host
192 * publishes files, which is a property of the machine and not of the
193 * application's source.
194 *
195 * PUNK_NO_STATIC_FILE_CACHE=1 everything off, exactly the old path
196 * PUNK_STATIC_STAT_TTL=0 stat every request; keep the content cache,
197 * which is exactly correct on its own
198 * PUNK_STATIC_STAT_TTL=<secs> believe a stat for that long
199 */
200 384 static int pfc_enabled(void) {
201 static int cached = -1;
202 384 ▶ if (cached < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached < 0) { 15 369 384
203 15 const char *v = getenv("PUNK_NO_STATIC_FILE_CACHE");
204 15 ▶ cached = (v && *v && *v != '0') ? 0 : 1;
Branch: 3 branches
BranchTrueFalseTotal
cached = (v && *v && *v != '0') ? 0 : 1; 0 15 15
cached = (v && *v && *v != '0') ? 0 : 1; 0 0 0
cached = (v && *v && *v != '0') ? 0 : 1; 0 0 0
205 }
206 384 return cached;
207 }
208  
209 126 static double pfc_stat_ttl(void) {
210 static double cached = -1.0;
211 126 ▶ if (cached < 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cached < 0.0) { 8 118 126
212 8 const char *v = getenv("PUNK_STATIC_STAT_TTL");
213 8 ▶ if (v && *v) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) { 2 6 8
if (v && *v) { 2 0 2
214 2 double d = atof(v);
215 2 ▶ cached = d > 0.0 ? d : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
cached = d > 0.0 ? d : 0.0; 1 1 2
216 }
217 6 else cached = PFC_STAT_TTL;
218 }
219 126 return cached;
220 }
221  
222 /* After a fork the entries are still valid - immutable bytes, and every hit
223 * revalidates them - so a worker inherits a warm cache rather than starting
224 * cold. Only the counters are per process. */
225 282 static void pfc_check_fork(pTHX_ punk_filecache *c) {
226 282 IV me = (IV)PerlProc_getpid();
227 PERL_UNUSED_CONTEXT;
228 282 ▶ if (c->owner_pid == me) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->owner_pid == me) return; 267 15 282
229 15 c->hits = c->misses = c->evictions = c->stale = 0;
230 15 c->stat_hits = c->stat_misses = 0;
231 15 c->owner_pid = me;
232 }
233  
234 382 static pfc_entry *pfc_lookup(punk_filecache *c, const char *path, STRLEN plen,
235 uint64_t h) {
236 pfc_entry *e;
237 387 ▶ 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) { 316 71 387
238 316 ▶ if (e->hash != h || e->klen != (uint32_t)plen) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (e->hash != h || e->klen != (uint32_t)plen) continue; 311 5 316
if (e->hash != h || e->klen != (uint32_t)plen) continue; 0 311 311
239 311 ▶ if (memcmp(e->key, path, plen) == 0) return e;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(e->key, path, plen) == 0) return e; 311 0 311
240 }
241 71 return NULL;
242 }
243  
244 71 static pfc_entry *pfc_new(pTHX_ punk_filecache *c, const char *path,
245 STRLEN plen, uint64_t h) {
246 pfc_entry *e;
247 PERL_UNUSED_CONTEXT;
248 71 ▶ 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 71 71
while (c->bytes + pfc_cost((uint32_t)plen, 0) > PFC_MAX_BYTES && c->tail) 0 0 0
249 {
250 0 pfc_drop(aTHX_ c, c->tail);
251 0 c->evictions++;
252 }
253 71 Newxz(e, 1, pfc_entry);
254 71 Newx(e->key, plen + 1, char);
255 71 memcpy(e->key, path, plen);
256 71 e->key[plen] = '\0';
257 71 e->klen = (uint32_t)plen;
258 71 e->hash = h;
259 71 e->hnext = c->buckets[h % PFC_BUCKETS];
260 71 c->buckets[h % PFC_BUCKETS] = e;
261 71 pfc_push_front(c, e);
262 71 c->entries++;
263 71 c->bytes += pfc_cost((uint32_t)plen, 0);
264 71 return e;
265 }
266  
267 /* Is the identity in `st` the same file as `b`? */
268 91 static int pfc_same(const Stat_t *a, const Stat_t *b) {
269 91 return (UV)a->st_dev == (UV)b->st_dev
270 91 ▶ && (UV)a->st_ino == (UV)b->st_ino
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)a->st_ino == (UV)b->st_ino 89 2 91
271 89 ▶ && (UV)a->st_mtime == (UV)b->st_mtime
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)a->st_mtime == (UV)b->st_mtime 87 2 89
272 87 ▶ && PFC_MTIME_NS(*a) == PFC_MTIME_NS(*b)
Branch: 1 branch
BranchTrueFalseTotal
&& PFC_MTIME_NS(*a) == PFC_MTIME_NS(*b) 80 7 87
273 80 ▶ && (UV)a->st_ctime == (UV)b->st_ctime
Branch: 1 branch
BranchTrueFalseTotal
&& (UV)a->st_ctime == (UV)b->st_ctime 80 0 80
274 182 ▶ && (UV)a->st_size == (UV)b->st_size;
Branch: 2 branches
BranchTrueFalseTotal
&& (UV)a->st_size == (UV)b->st_size; 91 0 91
&& (UV)a->st_size == (UV)b->st_size; 80 0 80
275 }
276  
277 /* stat(2), possibly from memory. 1 = *out filled and the path exists; 0 = it
278 * does not exist or could not be statted, which the caller must treat exactly
279 * as a failed stat.
280 *
281 * Within the TTL this answers without a syscall, including for a path that is
282 * KNOWN ABSENT - which is what makes the precompressed-sibling probe free on
283 * the sites that have no siblings.
284 *
285 * When it does stat and the file has changed, it releases the cached content
286 * here, so everything downstream can trust that content and stat agree. */
287 197 static int pfc_stat(pTHX_ const char *path, STRLEN plen, Stat_t *out) {
288 197 punk_filecache *c = &PFC_CACHE;
289 uint64_t h;
290 pfc_entry *e;
291 Stat_t fresh;
292 int ok;
293  
294 197 ▶ 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) 197 0 197
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 197 0 197
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 197 0 197
if (!pfc_enabled() || c->disabled || plen == 0 || plen >= PFC_KEY_MAX) 0 197 197
295 0 return PerlLIO_stat(path, out) == 0 ? 1 : 0;
296  
297 197 pfc_check_fork(aTHX_ c);
298 197 h = pfc_hash(path, plen);
299 197 e = pfc_lookup(c, path, plen, h);
300  
301 197 ▶ if (e && (pfc_now() - e->checked) < pfc_stat_ttl()) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && (pfc_now() - e->checked) < pfc_stat_ttl()) { 126 71 197
if (e && (pfc_now() - e->checked) < pfc_stat_ttl()) { 107 19 126
302 107 pfc_unlink_lru(c, e);
303 107 pfc_push_front(c, e);
304 107 c->stat_hits++;
305 107 ▶ if (!e->exists) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->exists) return 0; 7 100 107
306 100 *out = e->st;
307 100 return 1;
308 }
309  
310 90 c->stat_misses++;
311 90 ok = PerlLIO_stat(path, &fresh) == 0;
312  
313 90 ▶ if (!e) e = pfc_new(aTHX_ c, path, plen, h);
Branch: 1 branch
BranchTrueFalseTotal
if (!e) e = pfc_new(aTHX_ c, path, plen, h); 71 19 90
314 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
315 /* the bytes we hold are no longer this file's */
316 7 c->stale++;
317 7 pfc_drop_val(aTHX_ c, e);
318 }
319 90 e->checked = pfc_now();
320 /* a different stat means different validators */
321 90 ▶ 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; 64 26 90
if (!ok || !e->exists || !pfc_same(&fresh, &e->st)) e->hdr_ok = 0; 16 48 64
if (!ok || !e->exists || !pfc_same(&fresh, &e->st)) e->hdr_ok = 0; 6 10 16
322 90 e->exists = ok ? 1 : 0;
323 90 ▶ if (ok) { e->st = fresh; *out = fresh; }
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { e->st = fresh; *out = fresh; } 64 26 90
324 90 return ok;
325 }
326  
327 /* The file's bytes as an SV the caller owns (+1), or NULL to say "read it
328 * yourself". NULL is never "the file is missing".
329 *
330 * dev/ino/mtime/ns/ctime/size are the stat the caller is serving this request
331 * from - fresh, or from pfc_stat within the TTL - and are the check that the
332 * bytes held belong to that same file.
333 *
334 * The returned SV is newSVsv of the held one, which on any perl with
335 * copy-on-write strings (5.20+) is O(1) and shares the buffer until something
336 * writes to it. On an older perl it is a real copy, correct and still cheaper
337 * than the open it replaced. */
338 87 static SV *pfc_get(pTHX_ const char *path, STRLEN plen,
339 UV dev, UV ino, UV mtime, UV ns, UV ctime, UV size) {
340 87 punk_filecache *c = &PFC_CACHE;
341 uint64_t h;
342 pfc_entry *e;
343 PerlIO *fp;
344 SV *val;
345  
346 87 ▶ if (c->disabled || !pfc_enabled()) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (c->disabled || !pfc_enabled()) return NULL; 87 0 87
if (c->disabled || !pfc_enabled()) return NULL; 0 87 87
347 87 ▶ if (plen == 0 || plen >= PFC_KEY_MAX) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (plen == 0 || plen >= PFC_KEY_MAX) return NULL; 87 0 87
if (plen == 0 || plen >= PFC_KEY_MAX) return NULL; 0 87 87
348 87 ▶ 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 85 87
349 85 pfc_check_fork(aTHX_ c);
350  
351 85 h = pfc_hash(path, plen);
352 85 e = pfc_lookup(c, path, plen, h);
353 85 ▶ if (e && e->val) {
Branch: 2 branches
BranchTrueFalseTotal
if (e && e->val) { 85 0 85
if (e && e->val) { 45 40 85
354 /* Is what we hold this file? A new inode is a replaced file; a changed
355 * mtime or size is a rewritten one. */
356 45 ▶ 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 45 0 45
if ((UV)e->st.st_dev == dev && (UV)e->st.st_ino == ino 45 0 45
357 45 ▶ && (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 45 0 45
&& (UV)e->st.st_mtime == mtime && (UV)e->st.st_size == size 45 0 45
358 45 ▶ && e->vns == ns && e->vctime == ctime) {
Branch: 2 branches
BranchTrueFalseTotal
&& e->vns == ns && e->vctime == ctime) { 45 0 45
&& e->vns == ns && e->vctime == ctime) { 45 0 45
359 45 pfc_unlink_lru(c, e);
360 45 pfc_push_front(c, e);
361 45 c->hits++;
362 45 return newSVsv(e->val);
363 }
364 0 c->stale++;
365 0 pfc_drop_val(aTHX_ c, e);
366 }
367  
368 40 c->misses++;
369  
370 /* Not held: read it once, keep it, and serve from what we just read. This
371 * is the only request that pays. */
372 40 fp = PerlIO_open(path, "rb");
373 40 ▶ if (!fp) return NULL; /* caller's own missing-file */
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return NULL; /* caller's own missing-file */ 0 40 40
374  
375 40 ▶ val = newSV(size ? (STRLEN)size : 1);
Branch: 1 branch
BranchTrueFalseTotal
val = newSV(size ? (STRLEN)size : 1); 40 0 40
376 40 SvPOK_on(val);
377 {
378 40 STRLEN got = 0;
379 80 ▶ while (got < (STRLEN)size) {
Branch: 1 branch
BranchTrueFalseTotal
while (got < (STRLEN)size) { 40 40 80
380 40 SSize_t n = PerlIO_read(fp, SvPVX(val) + got, (STRLEN)size - got);
381 40 ▶ if (n <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) break; 0 40 40
382 40 got += (STRLEN)n;
383 }
384 40 PerlIO_close(fp);
385 40 ▶ if (got != (STRLEN)size) {
Branch: 1 branch
BranchTrueFalseTotal
if (got != (STRLEN)size) { 0 40 40
386 /* The file changed under us between the stat and the read. Do not
387 * cache a half file, and do not serve one either - let the caller
388 * take the ordinary path and see it for itself. */
389 0 SvREFCNT_dec(val);
390 0 return NULL;
391 }
392 40 SvCUR_set(val, got);
393 40 SvPVX(val)[got] = '\0';
394 }
395  
396 40 ▶ 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 40 40
397 0 return val; /* bigger than the budget: never hold it */
398  
399 40 ▶ if (!e) {
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { 0 40 40
400 0 e = pfc_new(aTHX_ c, path, plen, h);
401 0 e->checked = pfc_now();
402 0 e->exists = 1;
403 }
404 40 ▶ 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 40 40
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
405 0 pfc_drop(aTHX_ c, c->tail);
406 0 c->evictions++;
407 }
408 /* the stat this content belongs to, so pfc_get can check it next time */
409 40 e->st.st_dev = (dev_t)dev;
410 40 e->st.st_ino = (ino_t)ino;
411 40 e->st.st_mtime = (time_t)mtime;
412 40 e->st.st_size = (Off_t)size;
413 40 e->vns = ns;
414 40 e->vctime = ctime;
415 /* The entry keeps the SV it read and the caller gets a copy, the same
416 * shape a hit returns. Handing back the entry's own SV let a middleware
417 * that edits the body in place edit the cache, and every later hit
418 * served the edit. Read-only, so a write through some other path croaks
419 * rather than corrupts. */
420 40 e->val = val;
421 40 SvREADONLY_on(val);
422 40 c->bytes += (size_t)size;
423 40 return newSVsv(val);
424 }
425  
426 /* Forget when this path was last statted, so the next pfc_stat asks the
427 * filesystem. For a caller that has just seen the file disagree with the
428 * stat it was handed. */
429 1 static void pfc_expire(pTHX_ const char *path, STRLEN plen) {
430 1 punk_filecache *c = &PFC_CACHE;
431 pfc_entry *e;
432 PERL_UNUSED_CONTEXT;
433 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
434 0 return;
435 1 e = pfc_lookup(c, path, plen, pfc_hash(path, plen));
436 1 ▶ if (e) e->checked = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (e) e->checked = 0; 1 0 1
437 }
438  
439 #endif /* PUNK_FILECACHE_H */
« xs/oamount.xs ↑ Summary include/punk/punk_xml.h »