| 1 |
|
| /* punk_cachefile.h - the file cache store. |
| 2 |
|
| * |
| 3 |
|
| * WHY THIS IS THE DEFAULT. |
| 4 |
|
| * |
| 5 |
|
| * An in-memory store lives in one process, so under a prefork server every |
| 6 |
|
| * worker keeps its own: `workers => 8` with a 512M cap is four gigabytes of |
| 7 |
|
| * RSS, and every worker caches the same things separately. The filesystem is |
| 8 |
|
| * already shared, so this is ONE copy for the whole pool - and it survives a |
| 9 |
|
| * restart, which memory does not. |
| 10 |
|
| * |
| 11 |
|
| * It costs about six microseconds a hit against five nanoseconds for memory. |
| 12 |
|
| * That ratio sounds decisive and is not: six microseconds is nothing beside a |
| 13 |
|
| * request about to render a template or query a database. |
| 14 |
|
| * |
| 15 |
|
| * THE READ SHAPE, WHICH WAS MEASURED RATHER THAN GUESSED. |
| 16 |
|
| * |
| 17 |
|
| * One `read` for the header and key, then `pread` for the value straight into |
| 18 |
|
| * the caller's buffer. Two syscalls, zero copies. |
| 19 |
|
| * |
| 20 |
|
| * The first design read header, key and value separately - three syscalls, |
| 21 |
|
| * and at 1KB the syscalls ARE the cost, so it lost to a plain stat+read. The |
| 22 |
|
| * obvious correction, one big read plus a memmove to strip the header, is |
| 23 |
|
| * worse still at 1MB (41.4us against 28.6): stripping a header off a megabyte |
| 24 |
|
| * means moving a megabyte, and the copy costs more than the syscall it |
| 25 |
|
| * saves. |
| 26 |
|
| * |
| 27 |
|
| * THE KEY NEVER BECOMES A PATH. |
| 28 |
|
| * |
| 29 |
|
| * A cache key is application data and often user data: `../../etc/passwd` is a |
| 30 |
|
| * valid key, so is one with a NUL, so is a four-kilobyte one. The key is |
| 31 |
|
| * hashed and the hex sharded two levels deep, and the key itself is stored in |
| 32 |
|
| * the header and compared on read - so a hash collision is a MISS rather than |
| 33 |
|
| * a silently wrong value, which makes the hash width a performance question |
| 34 |
|
| * instead of a correctness one. |
| 35 |
|
| * |
| 36 |
|
| * ATOMICITY. |
| 37 |
|
| * |
| 38 |
|
| * Write a temp file in the DESTINATION directory and rename. Same directory |
| 39 |
|
| * because a cross-filesystem rename is not atomic and /tmp is very often |
| 40 |
|
| * another filesystem. Rename is also why no checksum is needed: a reader |
| 41 |
|
| * never sees a partial entry, so there is no torn state to detect. A crash |
| 42 |
|
| * mid-write leaves a temp file, which the sweep removes. |
| 43 |
|
| */ |
| 44 |
|
| |
| 45 |
|
| #ifndef PUNK_CACHEFILE_H |
| 46 |
|
| #define PUNK_CACHEFILE_H |
| 47 |
|
| |
| 48 |
|
| #include <stdio.h> |
| 49 |
|
| #include <stdlib.h> |
| 50 |
|
| #include <string.h> |
| 51 |
|
| #include <stdint.h> |
| 52 |
|
| #include <fcntl.h> |
| 53 |
|
| #include <unistd.h> |
| 54 |
|
| #include <dirent.h> |
| 55 |
|
| #include <errno.h> |
| 56 |
|
| #include <sys/stat.h> |
| 57 |
|
| #include <sys/time.h> |
| 58 |
|
| |
| 59 |
|
| #define PCF_MAGIC 0x504b4331u /* "PKC1" */ |
| 60 |
|
| #define PCF_PATHMAX 1024 |
| 61 |
|
| |
| 62 |
|
| /* a snprintf that fit its buffer: a truncated path names some other file */ |
| 63 |
|
| #define PCF_FITS(r, buf) ((r) >= 0 && (size_t)(r) < sizeof (buf)) |
| 64 |
|
| #ifdef O_NOFOLLOW |
| 65 |
|
| # define PCF_O_NOFOLLOW O_NOFOLLOW |
| 66 |
|
| #else |
| 67 |
|
| # define PCF_O_NOFOLLOW 0 |
| 68 |
|
| #endif |
| 69 |
|
| #define PCF_HEADMAX 4352 /* header + a 4096-byte key */ |
| 70 |
|
| |
| 71 |
|
| /* On disk: magic, expiry, key length, value length, then key, then value. |
| 72 |
|
| * |
| 73 |
|
| * The expiry lives HERE and not in mtime: an expiry stamped in the file |
| 74 |
|
| * survives a cache-wide default changing, and does not lose to a filesystem |
| 75 |
|
| * with coarse or lazily updated timestamps. */ |
| 76 |
|
| typedef struct { |
| 77 |
|
| uint32_t magic; |
| 78 |
|
| double expiry; /* epoch seconds; 0 = never */ |
| 79 |
|
| uint32_t klen; |
| 80 |
|
| uint32_t vlen; |
| 81 |
|
| } pcf_hdr; |
| 82 |
|
| |
| 83 |
|
| typedef struct { |
| 84 |
|
| char dir[PCF_PATHMAX]; |
| 85 |
|
| size_t dirlen; |
| 86 |
|
| size_t max_bytes; |
| 87 |
|
| double lock_wait; /* single-flight budget, seconds */ |
| 88 |
|
| |
| 89 |
|
| /* Amortised eviction. A sweep is a full scan - 273ms for 100k entries - |
| 90 |
|
| * so it must never run on a write. Instead each process counts what IT |
| 91 |
|
| * has written and sweeps once that passes a slice of the budget, which |
| 92 |
|
| * bounds how often the scan happens without putting it in the hot path. */ |
| 93 |
|
| size_t written_since_sweep; |
| 94 |
|
| |
| 95 |
|
| uint64_t hits, misses, evictions, refused, expired; |
| 96 |
|
| /* single-flight locks that could not be attempted - a path that would |
| 97 |
|
| * not form. Every caller is then told to compute, so a non-zero count |
| 98 |
|
| * is the herd running unprotected and is worth seeing in the stats. */ |
| 99 |
|
| uint64_t lock_errors; |
| 100 |
|
| IV owner_pid; |
| 101 |
|
| } punk_cachefile; |
| 102 |
|
| |
| 103 |
571 |
| static double pcf_now(void) { |
| 104 |
|
| struct timeval tv; |
| 105 |
571 |
| gettimeofday(&tv, NULL); |
| 106 |
571 |
| return (double)tv.tv_sec + (double)tv.tv_usec / 1e6; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| /* A file's mtime as epoch seconds, to the best resolution this platform |
| 110 |
|
| * spells - and PCF_MTIME_SLACK, how wrong that can be. |
| 111 |
|
| * |
| 112 |
|
| * The lock below judges its holder dead by the age of the lock file. With |
| 113 |
|
| * whole-second st_mtime a file written at 100.9 reports 100, so at 101.0 its |
| 114 |
|
| * age reads as 1.0 when it is really 0.1: an age that is too LARGE by up to |
| 115 |
|
| * a second, which steals a live lock and lets two callers compute at once. |
| 116 |
|
| * The slack is added to the budget rather than subtracted from the age, |
| 117 |
|
| * because erring the other way only delays a steal by a second, and a |
| 118 |
|
| * request that waits a second longer is cheaper than work done twice. |
| 119 |
|
| * |
| 120 |
|
| * THE SLACK IS A WHOLE SECOND EVEN WHERE A NANOSECOND FIELD EXISTS. That a |
| 121 |
|
| * struct stat HAS st_mtim.tv_nsec says the libc spells it, not that the |
| 122 |
|
| * filesystem fills it: GNU/Hurd's ext2fs has the field and stores zero in it |
| 123 |
|
| * (CPAN Testers, Punk 0.67, t/0803 on two perls), so the configure probe |
| 124 |
|
| * that found the field reported sub-second mtime on a box with none. The |
| 125 |
|
| * lock therefore no longer judges its age by mtime at all - see |
| 126 |
|
| * pcf_lock_age - and this is only the fallback for a lock file that has no |
| 127 |
|
| * stamp in it yet. */ |
| 128 |
|
| #if defined(PUNK_STAT_MTIM) |
| 129 |
|
| # define PCF_MTIME(st) ((double)(st).st_mtime + (double)(st).st_mtim.tv_nsec / 1e9) |
| 130 |
|
| #elif defined(PUNK_STAT_MTIMESPEC) |
| 131 |
|
| # define PCF_MTIME(st) ((double)(st).st_mtime + (double)(st).st_mtimespec.tv_nsec / 1e9) |
| 132 |
|
| #elif defined(PUNK_STAT_MTIMENSEC) |
| 133 |
|
| # define PCF_MTIME(st) ((double)(st).st_mtime + (double)(st).st_mtimensec / 1e9) |
| 134 |
|
| #else |
| 135 |
|
| # define PCF_MTIME(st) ((double)(st).st_mtime) |
| 136 |
|
| #endif |
| 137 |
|
| #define PCF_MTIME_SLACK 1.0 |
| 138 |
|
| |
| 139 |
|
| /* ---- paths --------------------------------------------------------------- */ |
| 140 |
|
| |
| 141 |
1497 |
| static void pcf_hex16(uint64_t h, char *out) { |
| 142 |
|
| static const char *H = "0123456789abcdef"; |
| 143 |
|
| int i; |
| 144 |
25449 |
▶ | for (i = 15; i >= 0; i--) { out[i] = H[h & 0xF]; h >>= 4; } |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 15; i >= 0; i--) { out[i] = H[h & 0xF]; h >>= 4; } |
23952 |
1497 |
25449 |
|
| 145 |
1497 |
| out[16] = '\0'; |
| 146 |
1497 |
| } |
| 147 |
|
| |
| 148 |
|
| /* <dir>/<h0h1>/<h2h3>/<hex>. Two shard levels, because one directory holding |
| 149 |
|
| * 100k entries is slow to scan on more filesystems than not. */ |
| 150 |
1497 |
| static int pcf_path(punk_cachefile *c, const char *key, uint32_t klen, |
| 151 |
|
| char *out, size_t outn, int mkdirs) { |
| 152 |
|
| char hex[17]; |
| 153 |
1497 |
| pcf_hex16(pc_hash(key, klen), hex); |
| 154 |
1497 |
▶ | if (mkdirs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mkdirs) { |
1042 |
455 |
1497 |
|
| 155 |
|
| char d[PCF_PATHMAX]; |
| 156 |
1042 |
| int n = snprintf(d, sizeof d, "%s/%c%c", c->dir, hex[0], hex[1]); |
| 157 |
|
| /* |
| 158 |
|
| # uncoverable branch true |
| 159 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 160 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 161 |
1042 |
▶ | if (n < 0 || (size_t)n >= sizeof d) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 || (size_t)n >= sizeof d) return -1; |
1042 |
0 |
1042 |
| if (n < 0 || (size_t)n >= sizeof d) return -1; |
0 |
1042 |
1042 |
|
| 162 |
|
| /* |
| 163 |
|
| # uncoverable branch true |
| 164 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 165 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 166 |
1042 |
▶ | if (mkdir(d, 0700) != 0 && errno != EEXIST) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mkdir(d, 0700) != 0 && errno != EEXIST) return -1; |
941 |
101 |
1042 |
| if (mkdir(d, 0700) != 0 && errno != EEXIST) return -1; |
0 |
941 |
941 |
|
| 167 |
1042 |
| n = snprintf(d, sizeof d, "%s/%c%c/%c%c", c->dir, hex[0], hex[1], |
| 168 |
1042 |
| hex[2], hex[3]); |
| 169 |
|
| /* |
| 170 |
|
| # uncoverable branch true |
| 171 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 172 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 173 |
1042 |
▶ | if (n < 0 || (size_t)n >= sizeof d) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 || (size_t)n >= sizeof d) return -1; |
1042 |
0 |
1042 |
| if (n < 0 || (size_t)n >= sizeof d) return -1; |
0 |
1042 |
1042 |
|
| 174 |
|
| /* |
| 175 |
|
| # uncoverable branch true |
| 176 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 177 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 178 |
1042 |
▶ | if (mkdir(d, 0700) != 0 && errno != EEXIST) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (mkdir(d, 0700) != 0 && errno != EEXIST) return -1; |
890 |
152 |
1042 |
| if (mkdir(d, 0700) != 0 && errno != EEXIST) return -1; |
0 |
890 |
890 |
|
| 179 |
|
| } |
| 180 |
|
| { |
| 181 |
1497 |
| int n = snprintf(out, outn, "%s/%c%c/%c%c/%s", c->dir, |
| 182 |
1497 |
| hex[0], hex[1], hex[2], hex[3], hex); |
| 183 |
|
| /* |
| 184 |
|
| # uncoverable branch true |
| 185 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 186 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 187 |
1497 |
▶ | if (n < 0 || (size_t)n >= (int)outn) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0 || (size_t)n >= (int)outn) return -1; |
1497 |
0 |
1497 |
| if (n < 0 || (size_t)n >= (int)outn) return -1; |
0 |
1497 |
1497 |
|
| 188 |
|
| } |
| 189 |
1497 |
| return 0; |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
|
| /* ---- lifecycle ----------------------------------------------------------- */ |
| 193 |
|
| |
| 194 |
98 |
| static punk_cachefile *punk_cachefile_new(pTHX_ const char *dir, size_t dirlen, |
| 195 |
|
| size_t max_bytes, double lock_wait) { |
| 196 |
|
| punk_cachefile *c; |
| 197 |
|
| /* |
| 198 |
|
| # uncoverable branch true |
| 199 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 200 |
98 |
▶ | if (dirlen >= PCF_PATHMAX - 64) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dirlen >= PCF_PATHMAX - 64) return NULL; |
0 |
98 |
98 |
|
| 201 |
98 |
| Newxz(c, 1, punk_cachefile); |
| 202 |
98 |
| memcpy(c->dir, dir, dirlen); |
| 203 |
98 |
| c->dir[dirlen] = '\0'; |
| 204 |
98 |
| c->dirlen = dirlen; |
| 205 |
98 |
| c->max_bytes = max_bytes; |
| 206 |
98 |
| c->lock_wait = lock_wait; |
| 207 |
98 |
| c->owner_pid = (IV)PerlProc_getpid(); |
| 208 |
98 |
| return c; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
98 |
| static void punk_cachefile_free(pTHX_ punk_cachefile *c) { |
| 212 |
|
| /* |
| 213 |
|
| # uncoverable branch false |
| 214 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 215 |
98 |
▶ | if (c) Safefree(c); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) Safefree(c); |
98 |
0 |
98 |
|
| 216 |
98 |
| } |
| 217 |
|
| |
| 218 |
|
| /* The entries are on disk and shared, so a fork changes nothing about them. |
| 219 |
|
| * The COUNTERS are this process's: a child reporting its parent's hit rate is |
| 220 |
|
| * reporting a number about a different process. */ |
| 221 |
1150 |
| static void punk_cachefile_check_fork(pTHX_ punk_cachefile *c) { |
| 222 |
1150 |
| IV me = (IV)PerlProc_getpid(); |
| 223 |
1150 |
▶ | if (c->owner_pid == me) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->owner_pid == me) return; |
1137 |
13 |
1150 |
|
| 224 |
13 |
| c->hits = c->misses = c->evictions = c->refused = c->expired = 0; |
| 225 |
13 |
| c->lock_errors = 0; |
| 226 |
13 |
| c->written_since_sweep = 0; |
| 227 |
13 |
| c->owner_pid = me; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| /* ---- reading ------------------------------------------------------------- */ |
| 231 |
|
| |
| 232 |
|
| /* The value as a new SV (+1), or NULL for absent, expired, or a collision. |
| 233 |
|
| * |
| 234 |
|
| * An SV rather than a malloc'd buffer the caller copies out of, because the |
| 235 |
|
| * caller ALWAYS wanted an SV and the copy was pure loss: a 64K value was |
| 236 |
|
| * allocated twice, copied twice and freed once on the way to a scalar that |
| 237 |
|
| * could have been filled directly. On the large path the value is now pread |
| 238 |
|
| * straight into the SV's own buffer, so there is no copy at all. |
| 239 |
|
| * |
| 240 |
|
| * `expiry`, when not NULL, is filled with the entry's own absolute expiry |
| 241 |
|
| * (0 = never). A memory tier in front of this store has to hold the entry to |
| 242 |
|
| * the expiry the STORE recorded rather than one counted from the moment it |
| 243 |
|
| * was faulted in, or the tier serves a value the store considers gone. */ |
| 244 |
404 |
| static SV *punk_cachefile_get_sv(pTHX_ punk_cachefile *c, const char *key, |
| 245 |
|
| uint32_t klen, double *expiry) { |
| 246 |
|
| char path[PCF_PATHMAX]; |
| 247 |
|
| char head[PCF_HEADMAX]; |
| 248 |
|
| pcf_hdr h; |
| 249 |
|
| int fd; |
| 250 |
|
| ssize_t n; |
| 251 |
|
| SV *sv; |
| 252 |
|
| |
| 253 |
404 |
| punk_cachefile_check_fork(aTHX_ c); |
| 254 |
|
| /* |
| 255 |
|
| # uncoverable branch true |
| 256 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 257 |
404 |
▶ | if (pcf_path(c, key, klen, path, sizeof path, 0) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_path(c, key, klen, path, sizeof path, 0) != 0) { |
0 |
404 |
404 |
|
| 258 |
0 |
| c->misses++; /* # uncoverable statement */ |
| 259 |
0 |
| return NULL; /* # uncoverable statement */ |
| 260 |
|
| } |
| 261 |
404 |
| fd = open(path, O_RDONLY); |
| 262 |
404 |
▶ | if (fd < 0) { c->misses++; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) { c->misses++; return NULL; } |
321 |
83 |
404 |
|
| 263 |
|
| |
| 264 |
83 |
| n = read(fd, head, sizeof head); |
| 265 |
|
| /* |
| 266 |
|
| # uncoverable branch true |
| 267 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 268 |
83 |
▶ | if (n < (ssize_t)sizeof h) { close(fd); c->misses++; return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < (ssize_t)sizeof h) { close(fd); c->misses++; return NULL; } |
0 |
83 |
83 |
|
| 269 |
83 |
| memcpy(&h, head, sizeof h); |
| 270 |
|
| |
| 271 |
|
| /* |
| 272 |
|
| # uncoverable branch true |
| 273 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 274 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 275 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 276 |
83 |
▶ | if (h.magic != PCF_MAGIC || h.klen != klen |
Branch: 2 branches
| Branch | True | False | Total |
| if (h.magic != PCF_MAGIC || h.klen != klen |
83 |
0 |
83 |
| if (h.magic != PCF_MAGIC || h.klen != klen |
83 |
0 |
83 |
|
| 277 |
83 |
▶ | || (size_t)n < sizeof h + h.klen) { |
Branch: 1 branch
| Branch | True | False | Total |
| || (size_t)n < sizeof h + h.klen) { |
0 |
83 |
83 |
|
| 278 |
0 |
| close(fd); c->misses++; return NULL; /* # uncoverable statement */ |
| 279 |
|
| } |
| 280 |
|
| /* The stored key decides it. A hash collision is a MISS, never a wrong |
| 281 |
|
| * value handed back as if it were the right one. */ |
| 282 |
83 |
▶ | if (memcmp(head + sizeof h, key, klen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(head + sizeof h, key, klen)) { |
1 |
82 |
83 |
|
| 283 |
1 |
| close(fd); c->misses++; return NULL; |
| 284 |
|
| } |
| 285 |
82 |
▶ | if (h.expiry && h.expiry <= pcf_now()) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (h.expiry && h.expiry <= pcf_now()) { |
31 |
51 |
82 |
| if (h.expiry && h.expiry <= pcf_now()) { |
4 |
27 |
31 |
|
| 286 |
|
| /* Lazy expiry, noticed on the read - of the file that was READ. A |
| 287 |
|
| * writer renames a fresh entry over this path whenever it likes, and |
| 288 |
|
| * unlinking by name after that would delete the new value because |
| 289 |
|
| * the old one had run out. */ |
| 290 |
|
| struct stat held, now; |
| 291 |
8 |
▶ | int same = fstat(fd, &held) == 0 && stat(path, &now) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| int same = fstat(fd, &held) == 0 && stat(path, &now) == 0 |
4 |
0 |
4 |
|
| 292 |
8 |
▶ | && held.st_dev == now.st_dev && held.st_ino == now.st_ino; |
Branch: 3 branches
| Branch | True | False | Total |
| && held.st_dev == now.st_dev && held.st_ino == now.st_ino; |
4 |
0 |
4 |
| && held.st_dev == now.st_dev && held.st_ino == now.st_ino; |
4 |
0 |
4 |
| && held.st_dev == now.st_dev && held.st_ino == now.st_ino; |
4 |
0 |
4 |
|
| 293 |
4 |
| close(fd); |
| 294 |
|
| /* |
| 295 |
|
| # uncoverable branch false |
| 296 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 297 |
4 |
▶ | if (same) unlink(path); |
Branch: 1 branch
| Branch | True | False | Total |
| if (same) unlink(path); |
4 |
0 |
4 |
|
| 298 |
4 |
| c->expired++; |
| 299 |
4 |
| c->misses++; |
| 300 |
4 |
| return NULL; |
| 301 |
|
| } |
| 302 |
|
| |
| 303 |
78 |
▶ | if (expiry) *expiry = h.expiry; |
Branch: 1 branch
| Branch | True | False | Total |
| if (expiry) *expiry = h.expiry; |
28 |
50 |
78 |
|
| 304 |
|
| |
| 305 |
78 |
▶ | if ((size_t)n >= sizeof h + h.klen + h.vlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)n >= sizeof h + h.klen + h.vlen) { |
71 |
7 |
78 |
|
| 306 |
|
| /* a small entry arrived whole in the first read */ |
| 307 |
71 |
| sv = newSVpvn(head + sizeof h + h.klen, h.vlen); |
| 308 |
|
| } |
| 309 |
|
| else { |
| 310 |
|
| /* pread straight into the scalar: no copy, and no memmove to strip a |
| 311 |
|
| * header off a megabyte */ |
| 312 |
|
| ssize_t got; |
| 313 |
7 |
| sv = newSV(h.vlen + 1); |
| 314 |
7 |
| SvPOK_only(sv); |
| 315 |
7 |
| got = pread(fd, SvPVX(sv), h.vlen, (off_t)(sizeof h + h.klen)); |
| 316 |
|
| /* |
| 317 |
|
| # uncoverable branch true |
| 318 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 319 |
7 |
▶ | if (got != (ssize_t)h.vlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (got != (ssize_t)h.vlen) { |
0 |
7 |
7 |
|
| 320 |
0 |
| close(fd); SvREFCNT_dec(sv); c->misses++; return NULL; /* # uncoverable statement */ |
| 321 |
|
| } |
| 322 |
7 |
| SvCUR_set(sv, (STRLEN)h.vlen); |
| 323 |
7 |
| *SvEND(sv) = '\0'; |
| 324 |
|
| } |
| 325 |
78 |
| close(fd); |
| 326 |
78 |
| c->hits++; |
| 327 |
78 |
| return sv; |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
|
| /* ---- eviction ------------------------------------------------------------ */ |
| 331 |
|
| |
| 332 |
|
| typedef struct { char path[PCF_PATHMAX]; double atime; off_t size; } pcf_ent; |
| 333 |
|
| |
| 334 |
|
| /* Walk the cache, dropping expired entries and orphaned temp files, and |
| 335 |
|
| * evicting the coldest until the budget is met. |
| 336 |
|
| * |
| 337 |
|
| * A full scan, deliberately kept OFF the write path: 273ms for 100k entries |
| 338 |
|
| * is not something a request may pay, and it grows with the cache. */ |
| 339 |
155 |
| static void pcf_sweep(pTHX_ punk_cachefile *c) { |
| 340 |
155 |
| pcf_ent *ents = NULL; |
| 341 |
155 |
| size_t n = 0, cap = 0; |
| 342 |
155 |
| uint64_t total = 0; |
| 343 |
|
| char l1[PCF_PATHMAX]; |
| 344 |
|
| DIR *d1; |
| 345 |
|
| struct dirent *e1; |
| 346 |
155 |
| double now = pcf_now(); |
| 347 |
|
| |
| 348 |
155 |
| d1 = opendir(c->dir); |
| 349 |
|
| /* |
| 350 |
|
| # uncoverable branch true |
| 351 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 352 |
155 |
▶ | if (!d1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d1) return; |
0 |
155 |
155 |
|
| 353 |
856 |
▶ | while ((e1 = readdir(d1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e1 = readdir(d1))) { |
701 |
155 |
856 |
|
| 354 |
|
| DIR *d2; |
| 355 |
|
| struct dirent *e2; |
| 356 |
701 |
▶ | if (e1->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e1->d_name[0] == '.') continue; |
310 |
391 |
701 |
|
| 357 |
391 |
▶ | if (!PCF_FITS(snprintf(l1, sizeof l1, "%s/%s", c->dir, e1->d_name), l1)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(l1, sizeof l1, "%s/%s", c->dir, e1->d_name), l1)) /* # uncoverable branch true */ |
391 |
0 |
391 |
| if (!PCF_FITS(snprintf(l1, sizeof l1, "%s/%s", c->dir, e1->d_name), l1)) /* # uncoverable branch true */ |
0 |
391 |
391 |
|
| 358 |
0 |
| continue; /* # uncoverable statement */ |
| 359 |
391 |
| d2 = opendir(l1); |
| 360 |
|
| /* |
| 361 |
|
| # uncoverable branch true |
| 362 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 363 |
391 |
▶ | if (!d2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d2) continue; |
0 |
391 |
391 |
|
| 364 |
2860 |
▶ | while ((e2 = readdir(d2))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e2 = readdir(d2))) { |
2469 |
391 |
2860 |
|
| 365 |
|
| char l2[PCF_PATHMAX]; |
| 366 |
|
| DIR *d3; |
| 367 |
|
| struct dirent *e3; |
| 368 |
2469 |
▶ | if (e2->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e2->d_name[0] == '.') continue; |
782 |
1687 |
2469 |
|
| 369 |
1687 |
▶ | if (!PCF_FITS(snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name), l2)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name), l2)) /* # uncoverable branch true */ |
1687 |
0 |
1687 |
| if (!PCF_FITS(snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name), l2)) /* # uncoverable branch true */ |
0 |
1687 |
1687 |
|
| 370 |
0 |
| continue; /* # uncoverable statement */ |
| 371 |
1687 |
| d3 = opendir(l2); |
| 372 |
|
| /* |
| 373 |
|
| # uncoverable branch true |
| 374 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 375 |
1687 |
▶ | if (!d3) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d3) continue; |
0 |
1687 |
1687 |
|
| 376 |
10002 |
▶ | while ((e3 = readdir(d3))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e3 = readdir(d3))) { |
8315 |
1687 |
10002 |
|
| 377 |
|
| char p[PCF_PATHMAX]; |
| 378 |
|
| struct stat st; |
| 379 |
8315 |
▶ | if (e3->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e3->d_name[0] == '.') continue; |
3374 |
4941 |
8315 |
|
| 380 |
4941 |
▶ | if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", l2, e3->d_name), p)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", l2, e3->d_name), p)) /* # uncoverable branch true */ |
4941 |
0 |
4941 |
| if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", l2, e3->d_name), p)) /* # uncoverable branch true */ |
0 |
4941 |
4941 |
|
| 381 |
0 |
| continue; /* # uncoverable statement */ |
| 382 |
|
| /* |
| 383 |
|
| # uncoverable branch true |
| 384 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 385 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 386 |
4941 |
▶ | if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue; |
4941 |
0 |
4941 |
| if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue; |
0 |
4941 |
4941 |
|
| 387 |
|
| |
| 388 |
|
| /* an orphan from a writer that died between create and |
| 389 |
|
| * rename, or a lock whose holder never came back */ |
| 390 |
|
| /* |
| 391 |
|
| # uncoverable branch true |
| 392 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 393 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 394 |
4941 |
▶ | if (strstr(e3->d_name, ".tmp") || strstr(e3->d_name, ".lock")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (strstr(e3->d_name, ".tmp") || strstr(e3->d_name, ".lock")) { |
4941 |
0 |
4941 |
| if (strstr(e3->d_name, ".tmp") || strstr(e3->d_name, ".lock")) { |
0 |
4941 |
4941 |
|
| 395 |
|
| /* |
| 396 |
|
| # uncoverable statement |
| 397 |
|
| # uncoverable branch true |
| 398 |
|
| # uncoverable branch false |
| 399 |
|
| # uncoverable condition left count:1 |
| 400 |
|
| # uncoverable condition right count:1 */ |
| 401 |
0 |
▶ | if (now - (double)st.st_mtime > 60.0) unlink(p); |
Branch: 1 branch
| Branch | True | False | Total |
| if (now - (double)st.st_mtime > 60.0) unlink(p); |
0 |
0 |
0 |
|
| 402 |
0 |
| continue; /* # uncoverable statement */ |
| 403 |
|
| } |
| 404 |
|
| /* expired entries go now rather than waiting to be read */ |
| 405 |
|
| { |
| 406 |
4941 |
| int fd = open(p, O_RDONLY); |
| 407 |
|
| pcf_hdr h; |
| 408 |
|
| /* |
| 409 |
|
| # uncoverable branch false |
| 410 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 411 |
4941 |
▶ | if (fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { |
4941 |
0 |
4941 |
|
| 412 |
4941 |
| ssize_t got = read(fd, &h, sizeof h); |
| 413 |
4941 |
| close(fd); |
| 414 |
|
| /* |
| 415 |
|
| # uncoverable branch true |
| 416 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 417 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 418 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 419 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 420 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 421 |
|
| # uncoverable condition left count:4 class:ignore_covered_err |
| 422 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 423 |
4941 |
▶ | if (got == (ssize_t)sizeof h && h.magic == PCF_MAGIC |
Branch: 2 branches
| Branch | True | False | Total |
| if (got == (ssize_t)sizeof h && h.magic == PCF_MAGIC |
4941 |
0 |
4941 |
| if (got == (ssize_t)sizeof h && h.magic == PCF_MAGIC |
4941 |
0 |
4941 |
|
| 424 |
4941 |
▶ | && h.expiry && h.expiry <= now) { |
Branch: 2 branches
| Branch | True | False | Total |
| && h.expiry && h.expiry <= now) { |
0 |
4941 |
4941 |
| && h.expiry && h.expiry <= now) { |
0 |
0 |
0 |
|
| 425 |
0 |
| unlink(p); /* # uncoverable statement */ |
| 426 |
0 |
| c->expired++; /* # uncoverable statement */ |
| 427 |
0 |
| continue; /* # uncoverable statement */ |
| 428 |
|
| } |
| 429 |
|
| } |
| 430 |
|
| } |
| 431 |
4941 |
▶ | if (n == cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == cap) { |
155 |
4786 |
4941 |
|
| 432 |
155 |
▶ | cap = cap ? cap * 2 : 256; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| cap = cap ? cap * 2 : 256; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
0 |
155 |
155 |
|
| 433 |
155 |
▶ | Renew(ents, cap, pcf_ent); |
Branch: 1 branch
| Branch | True | False | Total |
| Renew(ents, cap, pcf_ent); |
0 |
155 |
155 |
|
| 434 |
|
| } |
| 435 |
4941 |
| strncpy(ents[n].path, p, PCF_PATHMAX - 1); |
| 436 |
4941 |
| ents[n].path[PCF_PATHMAX - 1] = '\0'; |
| 437 |
4941 |
| ents[n].atime = (double)st.st_atime; |
| 438 |
4941 |
| ents[n].size = st.st_size; |
| 439 |
4941 |
| total += (uint64_t)st.st_size; |
| 440 |
4941 |
| n++; |
| 441 |
|
| } |
| 442 |
1687 |
| closedir(d3); |
| 443 |
|
| } |
| 444 |
391 |
| closedir(d2); |
| 445 |
|
| } |
| 446 |
155 |
| closedir(d1); |
| 447 |
|
| |
| 448 |
|
| /* Evict coldest-first until the budget is met. A selection pass rather |
| 449 |
|
| * than a sort: eviction is rare and n can be large, and repeatedly |
| 450 |
|
| * picking the minimum costs less than ordering everything when only a |
| 451 |
|
| * few need to go. */ |
| 452 |
662 |
▶ | while (total > c->max_bytes && n > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| while (total > c->max_bytes && n > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
507 |
155 |
662 |
| while (total > c->max_bytes && n > 0) { /* # uncoverable condition right count:2 class:ignore_covered_err */ |
507 |
0 |
507 |
|
| 453 |
507 |
| size_t i, oldest = 0; |
| 454 |
16980 |
▶ | for (i = 1; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < n; i++) |
16473 |
507 |
16980 |
|
| 455 |
|
| /* |
| 456 |
|
| # uncoverable branch true class:ignore_covered_err |
| 457 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 458 |
16473 |
▶ | if (ents[i].atime < ents[oldest].atime) oldest = i; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ents[i].atime < ents[oldest].atime) oldest = i; |
0 |
16473 |
16473 |
|
| 459 |
507 |
| unlink(ents[oldest].path); |
| 460 |
507 |
| total -= (uint64_t)ents[oldest].size; |
| 461 |
507 |
| c->evictions++; |
| 462 |
507 |
| ents[oldest] = ents[n - 1]; |
| 463 |
507 |
| n--; |
| 464 |
|
| } |
| 465 |
|
| /* |
| 466 |
|
| # uncoverable branch false |
| 467 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 468 |
155 |
▶ | if (ents) Safefree(ents); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ents) Safefree(ents); |
155 |
0 |
155 |
|
| 469 |
155 |
| c->written_since_sweep = 0; |
| 470 |
|
| } |
| 471 |
|
| |
| 472 |
|
| /* ---- writing ------------------------------------------------------------- */ |
| 473 |
|
| |
| 474 |
|
| /* Returns 1 stored, 0 refused. */ |
| 475 |
699 |
| static int punk_cachefile_set(pTHX_ punk_cachefile *c, const char *key, |
| 476 |
|
| uint32_t klen, const char *val, uint32_t vlen, |
| 477 |
|
| double expiry) { |
| 478 |
|
| char path[PCF_PATHMAX], tmp[PCF_PATHMAX]; |
| 479 |
|
| pcf_hdr h; |
| 480 |
699 |
| int fd, ok = 0; |
| 481 |
|
| |
| 482 |
699 |
| punk_cachefile_check_fork(aTHX_ c); |
| 483 |
|
| |
| 484 |
|
| /* A value that cannot fit the budget even in an empty cache is refused: |
| 485 |
|
| * making room would evict everything else and still not fit. */ |
| 486 |
699 |
▶ | if ((size_t)sizeof h + klen + vlen > c->max_bytes) { c->refused++; return 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)sizeof h + klen + vlen > c->max_bytes) { c->refused++; return 0; } |
3 |
696 |
699 |
|
| 487 |
|
| |
| 488 |
|
| /* |
| 489 |
|
| # uncoverable branch true |
| 490 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 491 |
696 |
▶ | if (pcf_path(c, key, klen, path, sizeof path, 1) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_path(c, key, klen, path, sizeof path, 1) != 0) return 0; |
0 |
696 |
696 |
|
| 492 |
696 |
▶ | if (!PCF_FITS(snprintf(tmp, sizeof tmp, "%s.tmp%ld", path, /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(tmp, sizeof tmp, "%s.tmp%ld", path, /* # uncoverable branch true */ |
696 |
0 |
696 |
| if (!PCF_FITS(snprintf(tmp, sizeof tmp, "%s.tmp%ld", path, /* # uncoverable branch true */ |
0 |
696 |
696 |
|
| 493 |
0 |
| (long)PerlProc_getpid()), tmp)) return 0; /* # uncoverable statement */ |
| 494 |
|
| |
| 495 |
|
| /* Exclusive and never through a symlink: a name somebody planted in |
| 496 |
|
| * the directory must not become a file this writes to. A leftover from |
| 497 |
|
| * a writer of this pid that died mid-write is unlinked and retried. */ |
| 498 |
696 |
| fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | PCF_O_NOFOLLOW, 0600); |
| 499 |
|
| /* |
| 500 |
|
| # uncoverable branch true |
| 501 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 502 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 503 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 504 |
696 |
▶ | if (fd < 0 && errno == EEXIST) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fd < 0 && errno == EEXIST) { |
0 |
696 |
696 |
| if (fd < 0 && errno == EEXIST) { |
0 |
0 |
0 |
|
| 505 |
0 |
| unlink(tmp); /* # uncoverable statement */ |
| 506 |
0 |
| fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | PCF_O_NOFOLLOW, 0600); /* # uncoverable statement */ |
| 507 |
|
| } |
| 508 |
|
| /* |
| 509 |
|
| # uncoverable branch true |
| 510 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 511 |
696 |
▶ | if (fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return 0; |
0 |
696 |
696 |
|
| 512 |
|
| |
| 513 |
696 |
| h.magic = PCF_MAGIC; h.expiry = expiry; h.klen = klen; h.vlen = vlen; |
| 514 |
|
| /* |
| 515 |
|
| # uncoverable branch false |
| 516 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 517 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 518 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 519 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 520 |
|
| # uncoverable condition right count:4 class:ignore_covered_err |
| 521 |
|
| # uncoverable condition right count:5 class:ignore_covered_err */ |
| 522 |
696 |
▶ | if (write(fd, &h, sizeof h) == (ssize_t)sizeof h |
Branch: 1 branch
| Branch | True | False | Total |
| if (write(fd, &h, sizeof h) == (ssize_t)sizeof h |
696 |
0 |
696 |
|
| 523 |
696 |
▶ | && (!klen || write(fd, key, klen) == (ssize_t)klen) |
Branch: 2 branches
| Branch | True | False | Total |
| && (!klen || write(fd, key, klen) == (ssize_t)klen) |
696 |
0 |
696 |
| && (!klen || write(fd, key, klen) == (ssize_t)klen) |
696 |
0 |
696 |
|
| 524 |
696 |
▶ | && (!vlen || write(fd, val, vlen) == (ssize_t)vlen)) |
Branch: 2 branches
| Branch | True | False | Total |
| && (!vlen || write(fd, val, vlen) == (ssize_t)vlen)) |
694 |
2 |
696 |
| && (!vlen || write(fd, val, vlen) == (ssize_t)vlen)) |
694 |
0 |
694 |
|
| 525 |
696 |
| ok = 1; |
| 526 |
696 |
| close(fd); |
| 527 |
|
| |
| 528 |
|
| /* |
| 529 |
|
| # uncoverable branch true |
| 530 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 531 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 532 |
696 |
▶ | if (!ok || rename(tmp, path) != 0) { unlink(tmp); return 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ok || rename(tmp, path) != 0) { unlink(tmp); return 0; } |
696 |
0 |
696 |
| if (!ok || rename(tmp, path) != 0) { unlink(tmp); return 0; } |
0 |
696 |
696 |
|
| 533 |
|
| |
| 534 |
|
| /* Amortised: a sweep is a full scan, so it runs once this process has |
| 535 |
|
| * written a slice of the budget rather than on every set. */ |
| 536 |
696 |
| c->written_since_sweep += sizeof h + klen + vlen; |
| 537 |
696 |
▶ | if (c->written_since_sweep > c->max_bytes / 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->written_since_sweep > c->max_bytes / 8) |
152 |
544 |
696 |
|
| 538 |
152 |
| pcf_sweep(aTHX_ c); |
| 539 |
696 |
| return 1; |
| 540 |
|
| } |
| 541 |
|
| |
| 542 |
10 |
| static int punk_cachefile_delete(pTHX_ punk_cachefile *c, const char *key, |
| 543 |
|
| uint32_t klen) { |
| 544 |
|
| char path[PCF_PATHMAX]; |
| 545 |
10 |
| punk_cachefile_check_fork(aTHX_ c); |
| 546 |
|
| /* |
| 547 |
|
| # uncoverable branch true |
| 548 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 549 |
10 |
▶ | if (pcf_path(c, key, klen, path, sizeof path, 0) != 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_path(c, key, klen, path, sizeof path, 0) != 0) return 0; |
0 |
10 |
10 |
|
| 550 |
10 |
| return unlink(path) == 0 ? 1 : 0; |
| 551 |
|
| } |
| 552 |
|
| |
| 553 |
6 |
| static void pcf_rmtree(const char *dir, int depth) { |
| 554 |
6 |
| DIR *d = opendir(dir); |
| 555 |
|
| struct dirent *e; |
| 556 |
|
| /* |
| 557 |
|
| # uncoverable branch true |
| 558 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 559 |
6 |
▶ | if (!d) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d) return; |
0 |
6 |
6 |
|
| 560 |
32 |
▶ | while ((e = readdir(d))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = readdir(d))) { |
26 |
6 |
32 |
|
| 561 |
|
| char p[PCF_PATHMAX]; |
| 562 |
|
| struct stat st; |
| 563 |
26 |
▶ | if (e->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->d_name[0] == '.') continue; |
12 |
14 |
26 |
|
| 564 |
14 |
▶ | if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", dir, e->d_name), p)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", dir, e->d_name), p)) /* # uncoverable branch true */ |
14 |
0 |
14 |
| if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", dir, e->d_name), p)) /* # uncoverable branch true */ |
0 |
14 |
14 |
|
| 565 |
0 |
| continue; /* # uncoverable statement */ |
| 566 |
|
| /* lstat: a symlink to a directory elsewhere is unlinked as the |
| 567 |
|
| * link it is, never descended into */ |
| 568 |
|
| /* |
| 569 |
|
| # uncoverable branch true |
| 570 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 571 |
14 |
▶ | if (lstat(p, &st) != 0) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (lstat(p, &st) != 0) continue; |
0 |
14 |
14 |
|
| 572 |
14 |
▶ | if (S_ISDIR(st.st_mode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (S_ISDIR(st.st_mode)) { |
4 |
10 |
14 |
|
| 573 |
|
| /* |
| 574 |
|
| # uncoverable branch false |
| 575 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 576 |
4 |
▶ | if (depth > 0) { pcf_rmtree(p, depth - 1); rmdir(p); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > 0) { pcf_rmtree(p, depth - 1); rmdir(p); } |
4 |
0 |
4 |
|
| 577 |
|
| } |
| 578 |
10 |
| else unlink(p); |
| 579 |
|
| } |
| 580 |
6 |
| closedir(d); |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
|
| /* Empty the cache. Bounded to the two shard levels this store creates, so a |
| 584 |
|
| * misconfigured `dir` cannot turn a clear into a recursive delete of |
| 585 |
|
| * somebody's home directory. */ |
| 586 |
2 |
| static void punk_cachefile_clear(pTHX_ punk_cachefile *c) { |
| 587 |
2 |
| punk_cachefile_check_fork(aTHX_ c); |
| 588 |
2 |
| pcf_rmtree(c->dir, 2); |
| 589 |
2 |
| c->written_since_sweep = 0; |
| 590 |
2 |
| } |
| 591 |
|
| |
| 592 |
|
| /* Current bytes held, for stats. A full scan, so it is asked for rather than |
| 593 |
|
| * maintained - `stats` is an operator action, not a hot path. */ |
| 594 |
35 |
| static void punk_cachefile_usage(punk_cachefile *c, uint64_t *bytes, |
| 595 |
|
| uint64_t *entries) { |
| 596 |
|
| char l1[PCF_PATHMAX]; |
| 597 |
35 |
| DIR *d1 = opendir(c->dir); |
| 598 |
|
| struct dirent *e1; |
| 599 |
35 |
| *bytes = *entries = 0; |
| 600 |
|
| /* |
| 601 |
|
| # uncoverable branch true |
| 602 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 603 |
35 |
▶ | if (!d1) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d1) return; |
0 |
35 |
35 |
|
| 604 |
156 |
▶ | while ((e1 = readdir(d1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e1 = readdir(d1))) { |
121 |
35 |
156 |
|
| 605 |
|
| DIR *d2; |
| 606 |
|
| struct dirent *e2; |
| 607 |
121 |
▶ | if (e1->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e1->d_name[0] == '.') continue; |
70 |
51 |
121 |
|
| 608 |
51 |
▶ | if (!PCF_FITS(snprintf(l1, sizeof l1, "%s/%s", c->dir, e1->d_name), l1)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(l1, sizeof l1, "%s/%s", c->dir, e1->d_name), l1)) /* # uncoverable branch true */ |
51 |
0 |
51 |
| if (!PCF_FITS(snprintf(l1, sizeof l1, "%s/%s", c->dir, e1->d_name), l1)) /* # uncoverable branch true */ |
0 |
51 |
51 |
|
| 609 |
0 |
| continue; /* # uncoverable statement */ |
| 610 |
51 |
| d2 = opendir(l1); |
| 611 |
|
| /* |
| 612 |
|
| # uncoverable branch true |
| 613 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 614 |
51 |
▶ | if (!d2) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d2) continue; |
0 |
51 |
51 |
|
| 615 |
255 |
▶ | while ((e2 = readdir(d2))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e2 = readdir(d2))) { |
204 |
51 |
255 |
|
| 616 |
|
| char l2[PCF_PATHMAX]; |
| 617 |
|
| DIR *d3; |
| 618 |
|
| struct dirent *e3; |
| 619 |
204 |
▶ | if (e2->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e2->d_name[0] == '.') continue; |
102 |
102 |
204 |
|
| 620 |
102 |
▶ | if (!PCF_FITS(snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name), l2)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name), l2)) /* # uncoverable branch true */ |
102 |
0 |
102 |
| if (!PCF_FITS(snprintf(l2, sizeof l2, "%s/%s", l1, e2->d_name), l2)) /* # uncoverable branch true */ |
0 |
102 |
102 |
|
| 621 |
0 |
| continue; /* # uncoverable statement */ |
| 622 |
102 |
| d3 = opendir(l2); |
| 623 |
|
| /* |
| 624 |
|
| # uncoverable branch true |
| 625 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 626 |
102 |
▶ | if (!d3) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!d3) continue; |
0 |
102 |
102 |
|
| 627 |
434 |
▶ | while ((e3 = readdir(d3))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e3 = readdir(d3))) { |
332 |
102 |
434 |
|
| 628 |
|
| char p[PCF_PATHMAX]; |
| 629 |
|
| struct stat st; |
| 630 |
333 |
▶ | if (e3->d_name[0] == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e3->d_name[0] == '.') continue; |
204 |
128 |
332 |
|
| 631 |
128 |
▶ | if (strstr(e3->d_name, ".tmp") || strstr(e3->d_name, ".lock")) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (strstr(e3->d_name, ".tmp") || strstr(e3->d_name, ".lock")) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
128 |
0 |
128 |
| if (strstr(e3->d_name, ".tmp") || strstr(e3->d_name, ".lock")) /* # uncoverable condition left count:1 class:ignore_covered_err */ |
1 |
127 |
128 |
|
| 632 |
1 |
| continue; |
| 633 |
127 |
▶ | if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", l2, e3->d_name), p)) /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", l2, e3->d_name), p)) /* # uncoverable branch true */ |
127 |
0 |
127 |
| if (!PCF_FITS(snprintf(p, sizeof p, "%s/%s", l2, e3->d_name), p)) /* # uncoverable branch true */ |
0 |
127 |
127 |
|
| 634 |
0 |
| continue; /* # uncoverable statement */ |
| 635 |
|
| /* |
| 636 |
|
| # uncoverable branch true |
| 637 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 638 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 639 |
127 |
▶ | if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue; |
127 |
0 |
127 |
| if (stat(p, &st) != 0 || !S_ISREG(st.st_mode)) continue; |
0 |
127 |
127 |
|
| 640 |
127 |
| *bytes += (uint64_t)st.st_size; |
| 641 |
127 |
| (*entries)++; |
| 642 |
|
| } |
| 643 |
102 |
| closedir(d3); |
| 644 |
|
| } |
| 645 |
51 |
| closedir(d2); |
| 646 |
|
| } |
| 647 |
35 |
| closedir(d1); |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| /* ---- single-flight ------------------------------------------------------- * |
| 651 |
|
| * |
| 652 |
|
| * When a hot key expires under load every worker misses at once and every one |
| 653 |
|
| * of them recomputes - the moment a cache is most valuable is the moment it |
| 654 |
|
| * stops helping. |
| 655 |
|
| * |
| 656 |
|
| * O_EXCL makes the winner. Three rules stop it becoming a new way to hang: |
| 657 |
|
| * |
| 658 |
|
| * - a loser that waits out its budget COMPUTES ANYWAY. Correctness never |
| 659 |
|
| * depends on the lock: duplicated work is a cost, a stalled request is an |
| 660 |
|
| * outage, and a request hanging because another worker died holding the |
| 661 |
|
| * lock is the worst of the three; |
| 662 |
|
| * - a STALE lock is stolen, not respected, or one crash poisons a key until |
| 663 |
|
| * somebody notices; |
| 664 |
|
| * - the lock covers one compute and nothing unbounded. |
| 665 |
|
| * |
| 666 |
|
| * So it is best effort: it collapses the herd in the common case and degrades |
| 667 |
|
| * to today's behaviour in every unusual one. |
| 668 |
|
| */ |
| 669 |
|
| |
| 670 |
|
| /* 1 if this caller won and should compute; 0 if it should look again; |
| 671 |
|
| * PCF_LOCK_UNAVAILABLE if no lock could be attempted at all. |
| 672 |
|
| * |
| 673 |
|
| * The third answer used to be spelled 1, which reads as "you won" and is a |
| 674 |
|
| * lie: a path that cannot be formed means EVERY caller is told it won, so |
| 675 |
|
| * the herd this exists to collapse all computes at once, silently. It is |
| 676 |
|
| * still the caller's business to decide what to do about it - the store is |
| 677 |
|
| * unusable for this key either way, so computing is the right degradation - |
| 678 |
|
| * but it is counted now, and it is no longer indistinguishable from |
| 679 |
|
| * actually holding the lock. */ |
| 680 |
|
| #define PCF_LOCK_UNAVAILABLE (-1) |
| 681 |
|
| |
| 682 |
|
| /* The lock file carries its own birth time: the holder's clock, in whole |
| 683 |
|
| * microseconds, as decimal text. A later caller judges the lock's age with |
| 684 |
|
| * the SAME clock, so the judgement owes nothing to the filesystem's idea of |
| 685 |
|
| * time - neither its resolution nor how lazily it stamps a new inode. The |
| 686 |
|
| * stamp is an integer rather than a "%f" because a perl inside `use locale` |
| 687 |
|
| * can leave LC_NUMERIC set to a comma, and strtod would then read "1.5" as 1. |
| 688 |
|
| * |
| 689 |
|
| * Create and stamp are two steps, and a reader can land between them, so an |
| 690 |
|
| * EMPTY lock file is read as one being written this instant - unless its |
| 691 |
|
| * mtime, with the full second of slack, says the writer died in that gap. */ |
| 692 |
350 |
| static int pcf_lock_create(const char *lock) { |
| 693 |
|
| char buf[32]; |
| 694 |
|
| int n, fd; |
| 695 |
|
| |
| 696 |
350 |
| fd = open(lock, O_WRONLY | O_CREAT | O_EXCL, 0600); |
| 697 |
350 |
▶ | if (fd < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) return 0; |
312 |
38 |
350 |
|
| 698 |
38 |
| n = snprintf(buf, sizeof buf, "%llu", |
| 699 |
38 |
| (unsigned long long)(pcf_now() * 1e6)); |
| 700 |
|
| /* |
| 701 |
|
| # uncoverable branch false |
| 702 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 703 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 704 |
38 |
▶ | if (n > 0 && (size_t)n < sizeof buf) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0 && (size_t)n < sizeof buf) { |
38 |
0 |
38 |
| if (n > 0 && (size_t)n < sizeof buf) { |
38 |
0 |
38 |
|
| 705 |
38 |
| ssize_t w = write(fd, buf, (size_t)n); |
| 706 |
|
| (void)w; /* an unwritten stamp falls back to mtime below */ |
| 707 |
|
| } |
| 708 |
38 |
| close(fd); |
| 709 |
38 |
| return 1; |
| 710 |
|
| } |
| 711 |
|
| |
| 712 |
|
| /* How long ago the lock at `lock` was taken, or a negative number when there |
| 713 |
|
| * is no lock there to judge (it was released between the caller's failed |
| 714 |
|
| * create and now). */ |
| 715 |
312 |
| static double pcf_lock_age(const char *lock) { |
| 716 |
|
| char buf[32]; |
| 717 |
|
| struct stat st; |
| 718 |
312 |
| ssize_t got = -1; |
| 719 |
312 |
| double now = pcf_now(); |
| 720 |
|
| int fd; |
| 721 |
|
| |
| 722 |
312 |
| fd = open(lock, O_RDONLY | PCF_O_NOFOLLOW); |
| 723 |
|
| /* |
| 724 |
|
| # uncoverable branch false |
| 725 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 726 |
312 |
▶ | if (fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd >= 0) { |
312 |
0 |
312 |
|
| 727 |
312 |
| got = read(fd, buf, sizeof buf - 1); |
| 728 |
312 |
| close(fd); |
| 729 |
|
| } |
| 730 |
312 |
▶ | if (got > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (got > 0) { |
310 |
2 |
312 |
|
| 731 |
|
| char *end; |
| 732 |
|
| unsigned long long us; |
| 733 |
310 |
| buf[got] = '\0'; |
| 734 |
310 |
| errno = 0; |
| 735 |
310 |
| us = strtoull(buf, &end, 10); |
| 736 |
|
| /* |
| 737 |
|
| # uncoverable branch false |
| 738 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 739 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 740 |
310 |
▶ | if (end != buf && errno == 0) return now - (double)us / 1e6; |
Branch: 2 branches
| Branch | True | False | Total |
| if (end != buf && errno == 0) return now - (double)us / 1e6; |
310 |
0 |
310 |
| if (end != buf && errno == 0) return now - (double)us / 1e6; |
310 |
0 |
310 |
|
| 741 |
|
| } |
| 742 |
|
| /* |
| 743 |
|
| # uncoverable branch true |
| 744 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 745 |
2 |
▶ | if (stat(lock, &st) != 0) return -1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stat(lock, &st) != 0) return -1.0; |
0 |
2 |
2 |
|
| 746 |
2 |
| return now - PCF_MTIME(st) - PCF_MTIME_SLACK; |
| 747 |
|
| } |
| 748 |
|
| |
| 749 |
|
| /* `stale` is how old a held lock may be before it is taken for abandoned |
| 750 |
|
| * and stolen; negative means the store's own lock_wait. */ |
| 751 |
346 |
| static int punk_cachefile_lock_for(pTHX_ punk_cachefile *c, const char *key, |
| 752 |
|
| uint32_t klen, double stale) { |
| 753 |
|
| char path[PCF_PATHMAX], lock[PCF_PATHMAX]; |
| 754 |
|
| |
| 755 |
346 |
▶ | if (pcf_path(c, key, klen, path, sizeof path, 1) != 0 /* # uncoverable branch true */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_path(c, key, klen, path, sizeof path, 1) != 0 /* # uncoverable branch true */ |
346 |
0 |
346 |
|
| 756 |
346 |
▶ | || !PCF_FITS(snprintf(lock, sizeof lock, "%s.lock", path), lock)) { |
Branch: 2 branches
| Branch | True | False | Total |
| || !PCF_FITS(snprintf(lock, sizeof lock, "%s.lock", path), lock)) { |
346 |
0 |
346 |
| || !PCF_FITS(snprintf(lock, sizeof lock, "%s.lock", path), lock)) { |
0 |
346 |
346 |
|
| 757 |
0 |
| c->lock_errors++; /* # uncoverable statement */ |
| 758 |
0 |
| return PCF_LOCK_UNAVAILABLE; /* # uncoverable statement */ |
| 759 |
|
| } |
| 760 |
|
| |
| 761 |
346 |
▶ | if (pcf_lock_create(lock)) return 1; /* won */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_lock_create(lock)) return 1; /* won */ |
34 |
312 |
346 |
|
| 762 |
|
| |
| 763 |
|
| /* Somebody holds it. If it is older than the wait budget its holder is |
| 764 |
|
| * not coming back - steal it rather than obey it. */ |
| 765 |
312 |
▶ | if (pcf_lock_age(lock) > (stale < 0 ? c->lock_wait : stale)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pcf_lock_age(lock) > (stale < 0 ? c->lock_wait : stale)) { |
261 |
51 |
312 |
| if (pcf_lock_age(lock) > (stale < 0 ? c->lock_wait : stale)) { |
4 |
308 |
312 |
|
| 766 |
4 |
| unlink(lock); |
| 767 |
|
| /* |
| 768 |
|
| # uncoverable branch false |
| 769 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 770 |
4 |
▶ | if (pcf_lock_create(lock)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_lock_create(lock)) return 1; |
4 |
0 |
4 |
|
| 771 |
|
| } |
| 772 |
308 |
| return 0; |
| 773 |
|
| } |
| 774 |
|
| |
| 775 |
64 |
| static int punk_cachefile_lock(pTHX_ punk_cachefile *c, const char *key, |
| 776 |
|
| uint32_t klen) { |
| 777 |
64 |
| return punk_cachefile_lock_for(aTHX_ c, key, klen, -1.0); |
| 778 |
|
| } |
| 779 |
|
| |
| 780 |
41 |
| static void punk_cachefile_unlock(pTHX_ punk_cachefile *c, const char *key, |
| 781 |
|
| uint32_t klen) { |
| 782 |
|
| char path[PCF_PATHMAX], lock[PCF_PATHMAX]; |
| 783 |
|
| /* |
| 784 |
|
| # uncoverable branch true |
| 785 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 786 |
41 |
▶ | if (pcf_path(c, key, klen, path, sizeof path, 0) != 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pcf_path(c, key, klen, path, sizeof path, 0) != 0) return; |
0 |
41 |
41 |
|
| 787 |
41 |
▶ | if (!PCF_FITS(snprintf(lock, sizeof lock, "%s.lock", path), lock)) return; /* # uncoverable branch true */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!PCF_FITS(snprintf(lock, sizeof lock, "%s.lock", path), lock)) return; /* # uncoverable branch true */ |
41 |
0 |
41 |
| if (!PCF_FITS(snprintf(lock, sizeof lock, "%s.lock", path), lock)) return; /* # uncoverable branch true */ |
0 |
41 |
41 |
|
| 788 |
41 |
| unlink(lock); |
| 789 |
|
| } |
| 790 |
|
| |
| 791 |
|
| #endif /* PUNK_CACHEFILE_H */ |