xs/retain.xs

filter: Statementstmt 94.4% 407 / 431 Branchbran 59.9% 272 / 454 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.7% 679 / 885 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_traceset.h ↑ Summary include/punk_observe/po_ingest.h »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Retain PREFIX = por_
2  
3 # --- the merge --------------------------------------------------------------
4  
5 # Merge several sorted runs of (t, series, kind) and report the order plus how
6 # many duplicates were collapsed.
7 void
8 por_merge(SV *runs)
9 PPCODE:
10 {
11 po_merge m;
12 AV *av;
13 SSize_t i, n;
14 po_rec **bufs;
15 16 AV *out = newAV();
16  
17 16 ▶ if (!SvROK(runs)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(runs)) croak("arrayref required"); 0 16 16
18 16 av = (AV *)SvRV(runs);
19 16 n = av_len(av) + 1;
20 16 ▶ Newxz(bufs, n ? n : 1, po_rec *);
Branch: 3 branches
BranchTrueFalseTotal
Newxz(bufs, n ? n : 1, po_rec *); 16 0 16
Newxz(bufs, n ? n : 1, po_rec *); 0 16 16
Newxz(bufs, n ? n : 1, po_rec *); 16 0 16
21 16 po_merge_init(&m);
22  
23 84 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 68 16 84
24 68 SV **e = av_fetch(av, i, 0);
25 AV *inner;
26 SSize_t j, cnt;
27 68 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 68 0 68
if (!e || !SvROK(*e)) continue; 0 68 68
28 68 inner = (AV *)SvRV(*e);
29 68 cnt = av_len(inner) + 1;
30 68 ▶ Newxz(bufs[i], cnt ? cnt : 1, po_rec);
Branch: 3 branches
BranchTrueFalseTotal
Newxz(bufs[i], cnt ? cnt : 1, po_rec); 64 4 68
Newxz(bufs[i], cnt ? cnt : 1, po_rec); 0 64 64
Newxz(bufs[i], cnt ? cnt : 1, po_rec); 64 4 68
31 1897 ▶ for (j = 0; j < cnt; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < cnt; j++) { 1829 68 1897
32 1829 SV **r = av_fetch(inner, j, 0);
33 HV *h; SV **f;
34 1829 po_u64 v = 0;
35 1829 ▶ if (!r || !SvROK(*r)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!r || !SvROK(*r)) continue; 1829 0 1829
if (!r || !SvROK(*r)) continue; 0 1829 1829
36 1829 h = (HV *)SvRV(*r);
37 1829 po_rec_zero(&bufs[i][j]);
38 1829 ▶ if ((f = hv_fetchs(h, "t", 0)) && po_sv_to_u64(aTHX_ *f, &v))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0)) && po_sv_to_u64(aTHX_ *f, &v)) 1829 0 1829
if ((f = hv_fetchs(h, "t", 0)) && po_sv_to_u64(aTHX_ *f, &v)) 1829 0 1829
39 1829 bufs[i][j].t_unix_nano = v;
40 1829 ▶ if ((f = hv_fetchs(h, "series", 0)) && po_sv_to_u64(aTHX_ *f, &v))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "series", 0)) && po_sv_to_u64(aTHX_ *f, &v)) 1829 0 1829
if ((f = hv_fetchs(h, "series", 0)) && po_sv_to_u64(aTHX_ *f, &v)) 1829 0 1829
41 1829 bufs[i][j].series = v;
42 1829 ▶ if ((f = hv_fetchs(h, "kind", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "kind", 0))) 1829 0 1829
43 1829 bufs[i][j].kind = (uint8_t)SvUV(*f);
44 }
45 68 po_merge_add_run(&m, bufs[i], (size_t)cnt);
46 }
47  
48 {
49 const po_rec *r;
50 1829 ▶ while ((r = po_merge_next(&m))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((r = po_merge_next(&m))) { 1813 16 1829
51 1813 HV *h = newHV();
52 1813 hv_stores(h, "t", po_u64_to_sv(r->t_unix_nano));
53 1813 hv_stores(h, "series", po_u64_to_sv(r->series));
54 1813 av_push(out, newRV_noinc((SV *)h));
55 }
56 }
57 {
58 16 HV *res = newHV();
59 16 hv_stores(res, "records", newRV_noinc((SV *)out));
60 16 hv_stores(res, "emitted", po_u64_to_sv(m.emitted));
61 16 hv_stores(res, "duplicates", po_u64_to_sv(m.duplicates));
62 16 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 16 16
63 }
64 84 ▶ for (i = 0; i < n; i++) Safefree(bufs[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) Safefree(bufs[i]); 68 16 84
65 16 Safefree(bufs);
66 }
67  
68 # --- rollups ----------------------------------------------------------------
69  
70 # Fold raw points into a 5m tier, promote to 1h, and answer aggregates from
71 # each. Everything a test needs in one call.
72 void
73 por_rollup(SV *points, SV *agg)
74 PPCODE:
75 {
76 po_rollup t5, t1h;
77 AV *av;
78 SSize_t i, n;
79 22 int a = (int)SvIV(agg);
80 22 double v5 = 0, v1 = 0;
81 int ok5, ok1;
82  
83 22 ▶ if (!SvROK(points)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(points)) croak("arrayref required"); 0 22 22
84 22 av = (AV *)SvRV(points);
85 22 n = av_len(av) + 1;
86  
87 22 ▶ if (!po_rollup_init(&t5, PO_TIER_5M)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_rollup_init(&t5, PO_TIER_5M)) croak("oom"); 0 22 22
88 4315 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4293 22 4315
89 4293 SV **e = av_fetch(av, i, 0);
90 HV *h; SV **f;
91 4293 po_u64 t = 0;
92 4293 double val = 0;
93 4293 int reset = 0;
94 4293 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 4293 0 4293
if (!e || !SvROK(*e)) continue; 0 4293 4293
95 4293 h = (HV *)SvRV(*e);
96 4293 ▶ if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &t);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &t); 4293 0 4293
97 4293 ▶ if ((f = hv_fetchs(h, "v", 0))) val = SvNV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "v", 0))) val = SvNV(*f); 4293 0 4293
98 4293 ▶ if ((f = hv_fetchs(h, "reset", 0))) reset = (int)SvIV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "reset", 0))) reset = (int)SvIV(*f); 300 3993 4293
99 4293 po_rollup_add(&t5, t, val, reset);
100 }
101 22 ▶ if (!po_rollup_promote(&t5, &t1h, PO_TIER_1H)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_rollup_promote(&t5, &t1h, PO_TIER_1H)) { 0 22 22
102 0 po_rollup_free(&t5); croak("promote");
103 }
104  
105 22 ok5 = po_rollup_agg(&t5, a, &v5);
106 22 ok1 = po_rollup_agg(&t1h, a, &v1);
107  
108 {
109 22 HV *res = newHV();
110 22 AV *b5 = newAV();
111 uint32_t k;
112 126 ▶ for (k = 0; k < t5.n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < t5.n; k++) { 104 22 126
113 104 HV *h = newHV();
114 104 hv_stores(h, "t", po_u64_to_sv(t5.p[k].t));
115 104 hv_stores(h, "count", po_u64_to_sv(t5.p[k].count));
116 104 hv_stores(h, "sum", newSVnv((NV)t5.p[k].sum));
117 104 hv_stores(h, "min", newSVnv((NV)t5.p[k].min));
118 104 hv_stores(h, "max", newSVnv((NV)t5.p[k].max));
119 104 hv_stores(h, "last", newSVnv((NV)t5.p[k].last));
120 104 hv_stores(h, "resets", newSViv(t5.p[k].resets));
121 104 av_push(b5, newRV_noinc((SV *)h));
122 }
123 22 hv_stores(res, "buckets_5m", newRV_noinc((SV *)b5));
124 22 hv_stores(res, "n_5m", newSVuv((UV)t5.n));
125 22 hv_stores(res, "n_1h", newSVuv((UV)t1h.n));
126 22 hv_stores(res, "ok_5m", newSViv(ok5));
127 22 hv_stores(res, "ok_1h", newSViv(ok1));
128 22 ▶ if (ok5) hv_stores(res, "value_5m", newSVnv((NV)v5));
Branch: 1 branch
BranchTrueFalseTotal
if (ok5) hv_stores(res, "value_5m", newSVnv((NV)v5)); 18 4 22
129 22 ▶ if (ok1) hv_stores(res, "value_1h", newSVnv((NV)v1));
Branch: 1 branch
BranchTrueFalseTotal
if (ok1) hv_stores(res, "value_1h", newSVnv((NV)v1)); 18 4 22
130 22 ▶ if (!ok5) hv_stores(res, "refusal", newSVpv(po_rollup_refusal(a), 0));
Branch: 1 branch
BranchTrueFalseTotal
if (!ok5) hv_stores(res, "refusal", newSVpv(po_rollup_refusal(a), 0)); 4 18 22
131 22 hv_stores(res, "resets_5m", newSViv(po_rollup_resets(&t5)));
132 22 hv_stores(res, "resets_1h", newSViv(po_rollup_resets(&t1h)));
133 22 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 22 22
134 }
135 22 po_rollup_free(&t5);
136 22 po_rollup_free(&t1h);
137 }
138  
139 # --- retention --------------------------------------------------------------
140  
141 void
142 por_sweep(SV *paths, SV *cutoff, ...)
143 PPCODE:
144 {
145 po_retain r;
146 AV *av;
147 SSize_t i, n;
148 7 po_u64 cut = 0;
149 uint32_t marked;
150 /* A DRY RUN IS THE MARK WITHOUT THE SWEEP. The mark already reads
151 * every footer and decides; skipping only the unlink means the
152 * numbers a --dry-run prints are the numbers the real run would
153 * act on, not an estimate produced by different code. */
154 7 ▶ int dry = items > 2 && SvTRUE(ST(2));
Branch: 2 branches
BranchTrueFalseTotal
int dry = items > 2 && SvTRUE(ST(2)); 0 7 7
int dry = items > 2 && SvTRUE(ST(2)); 0 0 0
155  
156 7 (void)po_sv_to_u64(aTHX_ cutoff, &cut);
157 7 ▶ if (!SvROK(paths)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(paths)) croak("arrayref required"); 0 7 7
158 7 av = (AV *)SvRV(paths);
159 7 n = av_len(av) + 1;
160 7 ▶ if (!po_retain_init(&r)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_retain_init(&r)) croak("oom"); 0 7 7
161  
162 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 7 24
163 17 SV **e = av_fetch(av, i, 0);
164 34 ▶ if (e) po_retain_add(&r, SvPV_nolen(*e));
Branch: 1 branch
BranchTrueFalseTotal
if (e) po_retain_add(&r, SvPV_nolen(*e)); 17 0 17
165 }
166 7 marked = po_retain_mark(&r, cut);
167 7 ▶ if (!dry) po_retain_sweep(&r);
Branch: 1 branch
BranchTrueFalseTotal
if (!dry) po_retain_sweep(&r); 7 0 7
168  
169 {
170 7 HV *res = newHV();
171 7 hv_stores(res, "considered", newSVuv((UV)r.n));
172 7 hv_stores(res, "marked", newSVuv((UV)marked));
173 7 hv_stores(res, "unlinked", newSVuv((UV)r.unlinked));
174 7 hv_stores(res, "kept", newSVuv((UV)r.kept));
175 7 hv_stores(res, "bytes_freed", po_u64_to_sv(r.bytes_freed));
176 /* MUST be zero. ftruncate on a mapped segment is SIGBUS. */
177 7 hv_stores(res, "truncate_calls", newSVuv((UV)r.truncate_calls));
178 7 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 7 7
179 }
180 7 po_retain_free(&r);
181 }
182  
183 # Open a segment, keep the mapping, and report whether it still reads
184 # correctly after its name is unlinked underneath.
185 void
186 por_read_through_unlink(SV *path)
187 PPCODE:
188 {
189 po_seg_r s;
190 STRLEN plen;
191 2 const char *p = SvPV(path, plen);
192 2 HV *res = newHV();
193 2 po_u64 before = 0, after = 0;
194 size_t i;
195  
196 2 ▶ if (!po_seg_open(&s, p)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_seg_open(&s, p)) { 0 2 2
197 0 hv_stores(res, "opened", newSViv(0));
198 0 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 0 0
199 0 XSRETURN(1);
200 }
201 2 hv_stores(res, "opened", newSViv(1));
202  
203 252 ▶ for (i = 0; i < s.n; i++) before += s.rec[i].t_unix_nano;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s.n; i++) before += s.rec[i].t_unix_nano; 250 2 252
204  
205 /* Delete the NAME while the mapping is held. */
206 2 hv_stores(res, "unlinked", newSViv(unlink(p) == 0 ? 1 : 0));
207  
208 /* And read every record again, through the same mapping. On POSIX
209 * this is defined and correct; with ftruncate it would be SIGBUS. */
210 252 ▶ for (i = 0; i < s.n; i++) after += s.rec[i].t_unix_nano;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s.n; i++) after += s.rec[i].t_unix_nano; 250 2 252
211  
212 2 hv_stores(res, "records", po_u64_to_sv((po_u64)s.n));
213 2 hv_stores(res, "sum_before", po_u64_to_sv(before));
214 2 hv_stores(res, "sum_after", po_u64_to_sv(after));
215 2 hv_stores(res, "same", newSViv(before == after ? 1 : 0));
216 2 po_seg_close(&s);
217 2 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 2 2
218 }
219  
220 # --- generations ------------------------------------------------------------
221  
222 void
223 por_generations(SV *ops)
224 PPCODE:
225 {
226 po_gen_table t;
227 AV *av;
228 SSize_t i, n;
229 2 AV *busy = newAV();
230  
231 2 memset(&t, 0, sizeof(t));
232 2 ▶ if (!SvROK(ops)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ops)) croak("arrayref required"); 0 2 2
233 2 av = (AV *)SvRV(ops);
234 2 n = av_len(av) + 1;
235 15 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 13 2 15
236 13 SV **op = av_fetch(av, i, 0);
237 13 SV **g = av_fetch(av, i + 1, 0);
238 13 po_u64 gen = 0;
239 STRLEN ol;
240 const char *o;
241 13 ▶ if (!op || !g) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!op || !g) break; 13 0 13
if (!op || !g) break; 13 0 13
242 13 o = SvPV(*op, ol);
243 13 (void)po_sv_to_u64(aTHX_ *g, &gen);
244 13 ▶ if (ol == 7 && memcmp(o, "acquire", 7) == 0) po_gen_acquire(&t, gen);
Branch: 2 branches
BranchTrueFalseTotal
if (ol == 7 && memcmp(o, "acquire", 7) == 0) po_gen_acquire(&t, gen); 6 7 13
if (ol == 7 && memcmp(o, "acquire", 7) == 0) po_gen_acquire(&t, gen); 3 3 6
245 10 ▶ else if (ol == 7 && memcmp(o, "release", 7) == 0) po_gen_release(&t, gen);
Branch: 2 branches
BranchTrueFalseTotal
else if (ol == 7 && memcmp(o, "release", 7) == 0) po_gen_release(&t, gen); 3 7 10
else if (ol == 7 && memcmp(o, "release", 7) == 0) po_gen_release(&t, gen); 3 0 3
246 7 ▶ else if (ol == 4 && memcmp(o, "busy", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (ol == 4 && memcmp(o, "busy", 4) == 0) 7 0 7
else if (ol == 4 && memcmp(o, "busy", 4) == 0) 7 0 7
247 7 av_push(busy, newSViv(po_gen_busy(&t, gen)));
248 }
249 2 ▶ mXPUSHs(newRV_noinc((SV *)busy));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)busy)); 0 2 2
250 }
251  
252 int
253 por_block_removable(SV *segments, SV *expired_all)
254 CODE:
255 3 RETVAL = po_block_removable((uint32_t)SvUV(segments), (int)SvIV(expired_all));
256 OUTPUT:
257 RETVAL
258  
259 # --- the window, as an operator writes it ------------------------------------
260 #
261 # "30d" into nanoseconds, against the query language's OWN unit table - the
262 # same po_dur_unit the lexer uses - so the two cannot disagree about what a
263 # week is, and there is no month here for the reason po_lex.h gives.
264 #
265 # A WINDOW TOO LARGE TO REPRESENT IS REFUSED, NEVER WRAPPED, and that is the
266 # one that had to be caught rather than documented: the cutoff is `now - keep`
267 # in unsigned nanoseconds, so a keep past the top comes back round as a cutoff
268 # of NOW, which marks every segment in the store and deletes the lot. Refusing
269 # looks like a typo caught at boot; wrapping looks like an empty store on
270 # Monday.
271 SV *
272 por_parse_keep(SV *str)
273 CODE:
274 {
275 17 STRLEN sl = 0;
276 34 ▶ const char *sp = SvOK(str) ? SvPV(str, sl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *sp = SvOK(str) ? SvPV(str, sl) : ""; 17 0 17
277 17 size_t i = 0, ds, de, us;
278 17 int dot = 0;
279 17 po_u64 unit, whole = 0, ns;
280 17 double frac = 0;
281  
282 17 RETVAL = &PL_sv_undef;
283 17 ▶ while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; 17 0 17
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; 0 17 17
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; 0 17 17
284 17 ds = i;
285 46 ▶ while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++; 46 0 46
while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++; 45 1 46
while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++; 29 16 45
286 17 ▶ if (i == ds) goto out; /* no digits at all */
Branch: 1 branch
BranchTrueFalseTotal
if (i == ds) goto out; /* no digits at all */ 1 16 17
287 16 de = i;
288 16 ▶ if (i < sl && sp[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < sl && sp[i] == '.') { 16 0 16
if (i < sl && sp[i] == '.') { 0 16 16
289 size_t fs;
290 0 dot = 1; i++;
291 0 fs = i;
292 0 ▶ while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++; 0 0 0
while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++; 0 0 0
while (i < sl && sp[i] >= '0' && sp[i] <= '9') i++; 0 0 0
293 0 ▶ if (i == fs) goto out;
Branch: 1 branch
BranchTrueFalseTotal
if (i == fs) goto out; 0 0 0
294 { /* the fraction, as a double: it only ever scales a unit */
295 0 double d = 0, s = 0.1;
296 size_t k;
297 0 ▶ for (k = fs; k < i; k++) { d += (sp[k] - '0') * s; s /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
for (k = fs; k < i; k++) { d += (sp[k] - '0') * s; s /= 10; } 0 0 0
298 0 frac = d;
299 }
300 }
301 17 ▶ while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; /* "30 d" */
Branch: 3 branches
BranchTrueFalseTotal
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; /* "30 d" */ 17 0 17
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; /* "30 d" */ 1 16 17
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; /* "30 d" */ 0 16 16
302 16 us = i;
303 42 ▶ while (i < sl && sp[i] >= 'a' && sp[i] <= 'z') i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < sl && sp[i] >= 'a' && sp[i] <= 'z') i++; 26 16 42
while (i < sl && sp[i] >= 'a' && sp[i] <= 'z') i++; 26 0 26
while (i < sl && sp[i] >= 'a' && sp[i] <= 'z') i++; 26 0 26
304 16 ▶ if (i == us) goto out; /* no unit */
Branch: 1 branch
BranchTrueFalseTotal
if (i == us) goto out; /* no unit */ 0 16 16
305 16 unit = po_dur_unit(sp + us, i - us);
306 16 ▶ if (!unit) goto out;
Branch: 1 branch
BranchTrueFalseTotal
if (!unit) goto out; 2 14 16
307 14 ▶ while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; 0 14 14
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; 0 0 0
while (i < sl && (sp[i] == ' ' || sp[i] == '\t')) i++; 0 0 0
308 14 ▶ if (i != sl) goto out; /* trailing rubbish */
Branch: 1 branch
BranchTrueFalseTotal
if (i != sl) goto out; /* trailing rubbish */ 0 14 14
309  
310 { /* The integer part in u64, refusing the multiply that would wrap
311 * rather than letting it come out the top. */
312 size_t k;
313 41 ▶ for (k = ds; k < de; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = ds; k < de; k++) { 27 14 41
314 27 ▶ if (whole > (PO_U64_MAX - (po_u64)(sp[k] - '0')) / 10) goto out;
Branch: 1 branch
BranchTrueFalseTotal
if (whole > (PO_U64_MAX - (po_u64)(sp[k] - '0')) / 10) goto out; 0 27 27
315 27 whole = whole * 10 + (po_u64)(sp[k] - '0');
316 }
317 14 ▶ if (whole && unit > PO_U64_MAX / whole) goto out;
Branch: 2 branches
BranchTrueFalseTotal
if (whole && unit > PO_U64_MAX / whole) goto out; 14 0 14
if (whole && unit > PO_U64_MAX / whole) goto out; 1 13 14
318 13 ns = whole * unit;
319 13 ▶ if (dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (dot) { 0 13 13
320 0 double add = frac * (double)unit;
321 0 ▶ if (add > (double)(PO_U64_MAX - ns)) goto out;
Branch: 1 branch
BranchTrueFalseTotal
if (add > (double)(PO_U64_MAX - ns)) goto out; 0 0 0
322 0 ns += (po_u64)(add + 0.5);
323 }
324 }
325 13 RETVAL = po_u64_to_sv(ns);
326 17 out: ;
327 }
328 OUTPUT:
329 RETVAL
330  
331 # --- logs whose worker is gone -----------------------------------------------
332 #
333 # adopt_orphans(store => $s, grace_s => 600, now => $t, dry_run => 0)
334 #
335 # Retention considers SEALED segments, and the live log is deliberately never
336 # touched - a worker owns the descriptor it appends to. A log left by a worker
337 # that DIED is neither: never indexed, never expired, never counted against a
338 # byte budget, and every restart leaves another. Measured on a demo store
339 # after a day: 261MB across 127 files, none of which `keep` could reach.
340 #
341 # SEALED, NOT DELETED. Its records are usually inside the retention window,
342 # and deleting them would be retention destroying what `keep` promised to
343 # hold. Sealing puts the log into the ordinary lifecycle: it gains a sidecar,
344 # queries prune it, and the next sweep expires it on the same t_max rule.
345 #
346 # TWO INDEPENDENT PROOFS THAT NOBODY OWNS IT, because sealing a log somebody
347 # is still appending to renames the file under their descriptor and lets them
348 # write records past the seal trailer:
349 #
350 # * the owning process is gone. The pid is in the file name, and kill(0)
351 # answers for it - EPERM means it EXISTS and is somebody else's, which
352 # counts as alive. The mistake to avoid is reading "not mine" as "dead".
353 # * the file has not been written for grace_s. A live worker appends
354 # constantly, so a stale log is unowned. This covers what a pid check
355 # cannot: a recycled pid now belonging to something else.
356 #
357 # Both must hold. A recycled pid makes this SKIP a log it could have
358 # reclaimed, which costs disk; the reverse would cost data.
359 SV *
360 por_adopt_orphans(...)
361 CODE:
362 {
363 16 HV *out = newHV();
364 16 SV *store = NULL;
365 16 IV grace = 600, dry = 0, ai;
366 16 po_u64 now = 0;
367 16 int have_now = 0;
368 16 IV seen = 0, adopted = 0, skipped_live = 0, skipped_recent = 0,
369 16 failed = 0;
370 16 po_u64 bytes = 0;
371 char dir[PO_PATHMAX];
372 SV *dsv;
373  
374 49 ▶ for (ai = 0; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai + 1 < items; ai += 2) { 33 16 49
375 33 const char *k = SvPV_nolen(ST(ai));
376 33 SV *v = ST(ai + 1);
377 33 ▶ if (strEQ(k, "store")) store = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "store")) store = v; 16 17 33
378 17 ▶ else if (strEQ(k, "grace_s") && SvOK(v)) grace = SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "grace_s") && SvOK(v)) grace = SvIV(v); 3 14 17
else if (strEQ(k, "grace_s") && SvOK(v)) grace = SvIV(v); 3 0 3
379 14 ▶ else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0; 14 0 14
380 0 ▶ else if (strEQ(k, "now") && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "now") && SvOK(v)) { 0 0 0
else if (strEQ(k, "now") && SvOK(v)) { 0 0 0
381 0 now = (po_u64)SvNV(v); have_now = 1;
382 }
383 }
384 16 ▶ if (!store || !SvOK(store))
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !SvOK(store)) 16 0 16
if (!store || !SvOK(store)) 0 16 16
385 0 croak("adopt_orphans needs a store");
386 16 ▶ if (!have_now) now = (po_u64)time(NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (!have_now) now = (po_u64)time(NULL); 16 0 16
387  
388 16 dsv = por_call_str(aTHX_ store, "wal_dir");
389 16 ▶ if (!dsv) goto emit;
Branch: 1 branch
BranchTrueFalseTotal
if (!dsv) goto emit; 0 16 16
390 {
391 16 STRLEN dl = 0;
392 16 const char *dp = SvPV(dsv, dl);
393 16 ▶ if (dl >= sizeof(dir)) { SvREFCNT_dec(dsv); goto emit; }
Branch: 1 branch
BranchTrueFalseTotal
if (dl >= sizeof(dir)) { SvREFCNT_dec(dsv); goto emit; } 0 16 16
394 16 memcpy(dir, dp, dl);
395 16 dir[dl] = '\0';
396 }
397 16 SvREFCNT_dec(dsv);
398  
399 {
400 po_dir d;
401 const char *name;
402 16 ▶ if (!po_opendir(&d, dir)) goto emit;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_opendir(&d, dir)) goto emit; 0 16 16
403 108 ▶ while ((name = po_readdir(&d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((name = po_readdir(&d))) { 92 16 108
404 char path[PO_PATHMAX];
405 long pid;
406 92 po_u64 mtime = 0, size = 0;
407 92 size_t nl = strlen(name);
408  
409 99 ▶ if (nl < 6 || name[0] != 'w') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (nl < 6 || name[0] != 'w') continue; 60 32 92
if (nl < 6 || name[0] != 'w') continue; 52 8 60
410 8 ▶ if (memcmp(name + nl - 4, ".wal", 4) != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(name + nl - 4, ".wal", 4) != 0) continue; 0 8 8
411 { /* w<digits>.wal, and nothing else */
412 size_t k;
413 65 ▶ for (k = 1; k < nl - 4; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 1; k < nl - 4; k++) 57 8 65
414 57 ▶ if (name[k] < '0' || name[k] > '9') break;
Branch: 2 branches
BranchTrueFalseTotal
if (name[k] < '0' || name[k] > '9') break; 57 0 57
if (name[k] < '0' || name[k] > '9') break; 57 0 57
415 8 ▶ if (k != nl - 4) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (k != nl - 4) continue; 0 8 8
416 }
417 8 pid = atol(name + 1);
418 8 ▶ if (pid <= 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pid <= 0) continue; 0 8 8
419 8 ▶ if (pid == (long)po_getpid()) continue; /* our own live log */
Branch: 1 branch
BranchTrueFalseTotal
if (pid == (long)po_getpid()) continue; /* our own live log */ 3 5 8
420 5 seen++;
421  
422 5 ▶ if (!po_path_join(path, sizeof(path), dir, name)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, name)) continue; 0 5 5
423 5 ▶ if (!por_stat(path, &size, &mtime)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!por_stat(path, &size, &mtime)) continue; 0 5 5
424  
425 5 ▶ if ((po_i64)mtime > (po_i64)now - (po_i64)grace) {
Branch: 1 branch
BranchTrueFalseTotal
if ((po_i64)mtime > (po_i64)now - (po_i64)grace) { 3 2 5
426 3 skipped_recent++;
427 3 continue;
428 }
429 2 ▶ if (por_pid_alive((int)pid)) { skipped_live++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (por_pid_alive((int)pid)) { skipped_live++; continue; } 0 2 2
430  
431 /* DRY RUN TAKES EVERY DECISION AND SKIPS ONLY THE SEAL, which
432 * is what dry_run means for `pass`. Counting the seal instead
433 * of the decision made a dry run report nothing to do on a
434 * store with a hundred and twenty-seven reclaimable logs. */
435 2 ▶ if (dry) { adopted++; bytes += size; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (dry) { adopted++; bytes += size; continue; } 1 1 2
436  
437 {
438 1 SV *seg = por_call_seal(aTHX_ store, path);
439 1 ▶ if (seg) { adopted++; bytes += size; SvREFCNT_dec(seg); }
Branch: 1 branch
BranchTrueFalseTotal
if (seg) { adopted++; bytes += size; SvREFCNT_dec(seg); } 1 0 1
440 0 else failed++;
441 }
442 }
443 16 po_closedir(&d);
444 }
445  
446 16 emit:
447 16 hv_stores(out, "seen", newSViv(seen));
448 16 hv_stores(out, "adopted", newSViv(adopted));
449 16 hv_stores(out, "bytes", po_u64_to_sv(bytes));
450 16 hv_stores(out, "skipped_live", newSViv(skipped_live));
451 16 hv_stores(out, "skipped_recent", newSViv(skipped_recent));
452 16 hv_stores(out, "failed", newSViv(failed));
453 16 RETVAL = newRV_noinc((SV *)out);
454 }
455 OUTPUT:
456 RETVAL
457  
458 # --- one retention pass ------------------------------------------------------
459 #
460 # Adopt, mark, sweep, and take the orphaned sidecars with it.
461 #
462 # Deletion is by whole segment, keyed on the data's END time: a segment whose
463 # newest record is older than the cutoff goes, and one still holding anything
464 # inside the window stays whole. The sweep never truncates - a truncated
465 # segment is SIGBUS for every reader mapping it - and a reader holding a
466 # mapping keeps reading the unlinked file to the end. Both are proven by
467 # t/0082-retention.t; this orchestrates.
468 #
469 # EXPIRY IS DECIDED FROM THE SIDECAR SUMMARIES rather than by opening each
470 # segment: the seal writes t_min and t_max beside every one, which is the same
471 # span a query prunes on. Keyed on t_max deliberately - keying on t_min would
472 # delete a segment still holding data inside the window - and a segment whose
473 # sidecar cannot be read is KEPT and counted, because deleting on an unknown
474 # age is deletion.
475 SV *
476 por_pass(...)
477 CODE:
478 {
479 14 HV *out = newHV();
480 14 SV *store = NULL, *segs = NULL;
481 14 SV *keep_sv = NULL, *bytes_sv = NULL;
482 14 IV dry = 0, ai;
483 14 po_u64 keep = 0, now, cutoff;
484 14 IV considered = 0, marked = 0, unlinked = 0, kept = 0,
485 14 unknown_kept = 0, orphan_idx = 0, adopted = 0;
486 14 po_u64 freed = 0, adopted_bytes = 0;
487 char dir[PO_PATHMAX];
488 14 int have_dir = 0;
489  
490 46 ▶ for (ai = 0; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai + 1 < items; ai += 2) { 32 14 46
491 32 const char *k = SvPV_nolen(ST(ai));
492 32 SV *v = ST(ai + 1);
493 32 ▶ if (strEQ(k, "store")) store = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "store")) store = v; 14 18 32
494 18 ▶ else if (strEQ(k, "keep_ns")) keep_sv = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keep_ns")) keep_sv = v; 13 5 18
495 5 ▶ else if (strEQ(k, "bytes")) bytes_sv = SvTRUE(v) ? v : NULL;
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "bytes")) bytes_sv = SvTRUE(v) ? v : NULL; 3 2 5
else if (strEQ(k, "bytes")) bytes_sv = SvTRUE(v) ? v : NULL; 3 0 3
496 2 ▶ else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "dry_run")) dry = SvTRUE(v) ? 1 : 0; 2 0 2
497 }
498 14 ▶ if (!store || !SvOK(store)) croak("pass needs a store");
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !SvOK(store)) croak("pass needs a store"); 14 0 14
if (!store || !SvOK(store)) croak("pass needs a store"); 0 14 14
499  
500 /* NO DEFAULT WINDOW, AND NOT A NUMBER-ISH ONE EITHER. A retention job
501 * with a silently defaulted window is a deletion job, and a keep that
502 * is not plain digits is a typo somebody would rather hear about at
503 * boot than discover as an empty store. */
504 14 ▶ if (!keep_sv || !SvOK(keep_sv) || !por_all_digits(aTHX_ keep_sv))
Branch: 3 branches
BranchTrueFalseTotal
if (!keep_sv || !SvOK(keep_sv) || !por_all_digits(aTHX_ keep_sv)) 13 1 14
if (!keep_sv || !SvOK(keep_sv) || !por_all_digits(aTHX_ keep_sv)) 13 0 13
if (!keep_sv || !SvOK(keep_sv) || !por_all_digits(aTHX_ keep_sv)) 0 13 13
505 1 croak("pass needs keep_ns - a retention job with no window is a "
506 "deletion job");
507 13 (void)po_sv_to_u64(aTHX_ keep_sv, &keep);
508  
509 13 now = po_now_ns();
510 13 cutoff = po_ns_sub(now, keep);
511  
512 /* BEFORE THE SWEEP, so a log adopted now is expired by this same pass
513 * if it is old enough rather than waiting an hour for the next. */
514 {
515 13 SV *ad = por_adopt(aTHX_ store, dry);
516 13 ▶ if (ad) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad) { 13 0 13
517 13 HV *h = (HV *)SvRV(ad);
518 13 SV **f = hv_fetchs(h, "adopted", 0);
519 13 ▶ if (f && SvOK(*f)) adopted = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) adopted = SvIV(*f); 13 0 13
if (f && SvOK(*f)) adopted = SvIV(*f); 13 0 13
520 13 f = hv_fetchs(h, "bytes", 0);
521 13 ▶ if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &adopted_bytes);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &adopted_bytes); 13 0 13
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &adopted_bytes); 13 0 13
522 13 SvREFCNT_dec(ad);
523 }
524 }
525  
526 13 segs = por_call_str(aTHX_ store, "segments");
527 13 ▶ if (segs && SvROK(segs) && SvTYPE(SvRV(segs)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (segs && SvROK(segs) && SvTYPE(SvRV(segs)) == SVt_PVAV) { 13 0 13
if (segs && SvROK(segs) && SvTYPE(SvRV(segs)) == SVt_PVAV) { 13 0 13
if (segs && SvROK(segs) && SvTYPE(SvRV(segs)) == SVt_PVAV) { 13 0 13
528 13 AV *av = (AV *)SvRV(segs);
529 13 SSize_t i, n = av_len(av) + 1;
530 37 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 24 13 37
531 24 SV **e = av_fetch(av, i, 0);
532 HV *seg;
533 SV **f, **idx;
534 24 po_u64 t_max = 0, sbytes = 0;
535 const char *path;
536 24 STRLEN pl = 0;
537  
538 42 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 24 0 24
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 24 0 24
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 0 24 24
539 24 seg = (HV *)SvRV(*e);
540 24 f = hv_fetchs(seg, "sealed", 0);
541 24 ▶ if (!f || !SvTRUE(*f)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvTRUE(*f)) continue; 24 0 24
if (!f || !SvTRUE(*f)) continue; 0 24 24
542 24 considered++;
543  
544 24 idx = hv_fetchs(seg, "index", 0);
545 24 ▶ f = (idx && SvROK(*idx) && SvTYPE(SvRV(*idx)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
f = (idx && SvROK(*idx) && SvTYPE(SvRV(*idx)) == SVt_PVHV) 23 1 24
f = (idx && SvROK(*idx) && SvTYPE(SvRV(*idx)) == SVt_PVHV) 23 0 23
546 48 ▶ ? hv_fetchs((HV *)SvRV(*idx), "t_max", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(*idx), "t_max", 0) : NULL; 24 0 24
547 24 ▶ if (!f || !SvOK(*f)) { kept++; unknown_kept++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) { kept++; unknown_kept++; continue; } 23 1 24
if (!f || !SvOK(*f)) { kept++; unknown_kept++; continue; } 0 23 23
548 23 (void)po_sv_to_u64(aTHX_ *f, &t_max);
549 23 ▶ if (t_max >= cutoff) { kept++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (t_max >= cutoff) { kept++; continue; } 16 7 23
550  
551 7 marked++;
552 7 ▶ if (dry) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (dry) continue; 1 6 7
553  
554 6 f = hv_fetchs(seg, "path", 0);
555 6 ▶ if (!f || !SvOK(*f)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) continue; 6 0 6
if (!f || !SvOK(*f)) continue; 0 6 6
556 6 path = SvPV(*f, pl);
557 6 f = hv_fetchs(seg, "bytes", 0);
558 6 ▶ if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &sbytes);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &sbytes); 6 0 6
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &sbytes); 6 0 6
559  
560 6 ▶ if (po_unlink(path) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_unlink(path) == 0) { 6 0 6
561 char ip[PO_PATHMAX];
562 6 unlinked++;
563 6 freed += sbytes;
564 /* The sidecar goes with it: an index naming a segment
565 * that is gone is what `orphan_index` on the status page
566 * counts, and leaving one behind here would create the
567 * very thing the next block cleans up. */
568 6 ▶ if (pl > 4 && pl < sizeof(ip)
Branch: 2 branches
BranchTrueFalseTotal
if (pl > 4 && pl < sizeof(ip) 6 0 6
if (pl > 4 && pl < sizeof(ip) 6 0 6
569 6 ▶ && memcmp(path + pl - 4, ".seg", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(path + pl - 4, ".seg", 4) == 0) { 6 0 6
570 6 memcpy(ip, path, pl - 4);
571 6 memcpy(ip + pl - 4, ".idx", 5);
572 6 (void)po_unlink(ip);
573 }
574 }
575 }
576 }
577 13 ▶ if (segs) SvREFCNT_dec(segs);
Branch: 1 branch
BranchTrueFalseTotal
if (segs) SvREFCNT_dec(segs); 13 0 13
578  
579 13 ▶ if (!dry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dry) { 11 2 13
580 11 SV *dsv = por_call_str(aTHX_ store, "wal_dir");
581 11 ▶ if (dsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (dsv) { 11 0 11
582 11 STRLEN dl = 0;
583 11 const char *dp = SvPV(dsv, dl);
584 11 ▶ if (dl < sizeof(dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (dl < sizeof(dir)) { 11 0 11
585 11 memcpy(dir, dp, dl); dir[dl] = '\0'; have_dir = 1;
586 }
587 11 SvREFCNT_dec(dsv);
588 }
589 }
590 13 ▶ if (have_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_dir) { 11 2 13
591 /* A SIDECAR WHOSE SEGMENT IS GONE, from a crash between the two
592 * unlinks above. Harmless but not free, and it is counted so an
593 * operator can see it happening rather than wonder. */
594 po_dir d;
595 const char *name;
596 11 ▶ if (po_opendir(&d, dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_opendir(&d, dir)) { 11 0 11
597 61 ▶ while ((name = po_readdir(&d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((name = po_readdir(&d))) { 50 11 61
598 char ip[PO_PATHMAX], sp2[PO_PATHMAX];
599 50 size_t nl = strlen(name);
600 50 ▶ if (nl < 5 || memcmp(name + nl - 4, ".idx", 4) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (nl < 5 || memcmp(name + nl - 4, ".idx", 4) != 0) 28 22 50
if (nl < 5 || memcmp(name + nl - 4, ".idx", 4) != 0) 14 14 28
601 49 continue;
602 14 ▶ if (!po_path_join(sp2, sizeof(sp2), dir, name)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(sp2, sizeof(sp2), dir, name)) continue; 0 14 14
603 14 memcpy(ip, sp2, strlen(sp2) + 1);
604 {
605 14 size_t sl2 = strlen(sp2);
606 14 memcpy(sp2 + sl2 - 4, ".seg", 5);
607 }
608 14 ▶ if (po_file_exists(sp2)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (po_file_exists(sp2)) continue; 13 1 14
609 1 ▶ if (po_unlink(ip) == 0) orphan_idx++;
Branch: 1 branch
BranchTrueFalseTotal
if (po_unlink(ip) == 0) orphan_idx++; 1 0 1
610 }
611 11 po_closedir(&d);
612 }
613 }
614  
615 13 hv_stores(out, "considered", newSViv(considered));
616 13 hv_stores(out, "marked", newSViv(marked));
617 13 hv_stores(out, "unlinked", newSViv(unlinked));
618 13 hv_stores(out, "kept", newSViv(kept));
619 13 hv_stores(out, "unknown_kept", newSViv(unknown_kept));
620 13 hv_stores(out, "truncate_calls", newSViv(0));
621 13 hv_stores(out, "orphan_idx_removed", newSViv(orphan_idx));
622 13 hv_stores(out, "adopted", newSViv(adopted));
623 13 hv_stores(out, "adopted_bytes", po_u64_to_sv(adopted_bytes));
624 13 hv_stores(out, "cutoff", po_u64_to_sv(cutoff));
625  
626 /* THE BYTE BUDGET, over and above the window: a store still past it
627 * after the time sweep loses its oldest segments even inside the
628 * window, because a full disk loses everything rather than the oldest
629 * hour. Never under dry_run - the store's own pass does the deleting
630 * and has no rehearsal. */
631 13 ▶ if (bytes_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (bytes_sv) { 3 10 13
632 3 ▶ if (!por_all_digits(aTHX_ bytes_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!por_all_digits(aTHX_ bytes_sv)) { 1 2 3
633 1 STRLEN bl = 0;
634 1 const char *bp = SvPV(bytes_sv, bl);
635 1 SvREFCNT_dec((SV *)out);
636 1 croak("pass bytes '%.*s' is not a byte count", (int)bl, bp);
637 }
638 2 ▶ if (dry) hv_stores(out, "budget_skipped", newSVpvs("dry_run"));
Branch: 1 branch
BranchTrueFalseTotal
if (dry) hv_stores(out, "budget_skipped", newSVpvs("dry_run")); 1 1 2
639 else {
640 1 SV *b = por_call_retain(aTHX_ store, bytes_sv);
641 1 IV bd = 0; po_u64 bf = 0;
642 1 ▶ if (b && SvROK(b) && SvTYPE(SvRV(b)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (b && SvROK(b) && SvTYPE(SvRV(b)) == SVt_PVHV) { 1 0 1
if (b && SvROK(b) && SvTYPE(SvRV(b)) == SVt_PVHV) { 1 0 1
if (b && SvROK(b) && SvTYPE(SvRV(b)) == SVt_PVHV) { 1 0 1
643 1 HV *bh = (HV *)SvRV(b);
644 1 SV **f = hv_fetchs(bh, "deleted", 0);
645 1 ▶ if (f && SvOK(*f)) bd = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) bd = SvIV(*f); 1 0 1
if (f && SvOK(*f)) bd = SvIV(*f); 1 0 1
646 1 f = hv_fetchs(bh, "freed", 0);
647 1 ▶ if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &bf);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &bf); 1 0 1
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &bf); 1 0 1
648 1 f = hv_fetchs(bh, "bytes", 0);
649 1 ▶ if (f && SvOK(*f)) hv_stores(out, "bytes", newSVsv(*f));
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) hv_stores(out, "bytes", newSVsv(*f)); 1 0 1
if (f && SvOK(*f)) hv_stores(out, "bytes", newSVsv(*f)); 1 0 1
650 }
651 1 ▶ if (b) SvREFCNT_dec(b);
Branch: 1 branch
BranchTrueFalseTotal
if (b) SvREFCNT_dec(b); 1 0 1
652 1 hv_stores(out, "budget_deleted", newSViv(bd));
653 1 hv_stores(out, "budget_freed", po_u64_to_sv(bf));
654 1 unlinked += bd;
655 1 freed += bf;
656 1 hv_stores(out, "unlinked", newSViv(unlinked));
657 }
658 }
659 12 hv_stores(out, "bytes_freed", po_u64_to_sv(freed));
660 12 RETVAL = newRV_noinc((SV *)out);
661 }
662 OUTPUT:
663 RETVAL
664  
665 # --- the scheduled forms -----------------------------------------------------
666  
667 # cron_task(store => $s, keep_ns => $ns, bytes => $b, owner => $$, ...)
668 #
669 # The closure a host schedules when it would rather declare its own cron than
670 # let the plugin register one. Takes the queue, runs one pass under the leader
671 # lease, returns how many segments went.
672 SV *
673 por_cron_task(...)
674 CODE:
675 {
676 3 HV *o = newHV();
677 CV *cv;
678 3 SV *keep = NULL;
679 3 IV ai, lease = 30, owner = -1;
680  
681 9 ▶ for (ai = 0; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 0; ai + 1 < items; ai += 2) { 6 3 9
682 6 const char *k = SvPV_nolen(ST(ai));
683 6 SV *v = ST(ai + 1);
684 6 ▶ if (strEQ(k, "store")) hv_stores(o, "store", newSVsv(v));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "store")) hv_stores(o, "store", newSVsv(v)); 3 3 6
685 3 ▶ else if (strEQ(k, "keep_ns")) { keep = v; hv_stores(o, "keep_ns", newSVsv(v)); }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keep_ns")) { keep = v; hv_stores(o, "keep_ns", newSVsv(v)); } 2 1 3
686 1 ▶ else if (strEQ(k, "bytes") && SvTRUE(v)) hv_stores(o, "bytes", newSVsv(v));
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "bytes") && SvTRUE(v)) hv_stores(o, "bytes", newSVsv(v)); 0 1 1
else if (strEQ(k, "bytes") && SvTRUE(v)) hv_stores(o, "bytes", newSVsv(v)); 0 0 0
687 1 ▶ else if (strEQ(k, "lease_seconds") && SvTRUE(v)) lease = SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "lease_seconds") && SvTRUE(v)) lease = SvIV(v); 0 1 1
else if (strEQ(k, "lease_seconds") && SvTRUE(v)) lease = SvIV(v); 0 0 0
688 1 ▶ else if (strEQ(k, "owner") && SvOK(v) && por_all_digits(aTHX_ v))
Branch: 3 branches
BranchTrueFalseTotal
else if (strEQ(k, "owner") && SvOK(v) && por_all_digits(aTHX_ v)) 1 0 1
else if (strEQ(k, "owner") && SvOK(v) && por_all_digits(aTHX_ v)) 1 0 1
else if (strEQ(k, "owner") && SvOK(v) && por_all_digits(aTHX_ v)) 1 0 1
689 1 owner = SvIV(v);
690 }
691 3 ▶ if (!keep || !SvOK(keep)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!keep || !SvOK(keep)) { 2 1 3
if (!keep || !SvOK(keep)) { 0 2 2
692 1 SvREFCNT_dec((SV *)o);
693 1 croak("cron_task needs keep_ns");
694 }
695 2 hv_stores(o, "lease", newSViv(lease));
696 2 ▶ hv_stores(o, "owner", newSViv(owner >= 0 ? owner : (IV)po_getpid()));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(o, "owner", newSViv(owner >= 0 ? owner : (IV)po_getpid())); 1 1 2
697  
698 2 cv = newXS(NULL, por_cron_thunk, __FILE__);
699 /* The options ride on the CV as magic, which is also what frees them:
700 * the closure owns its captures the way a Perl one would. */
701 2 sv_magicext((SV *)cv, sv_2mortal(newRV_noinc((SV *)o)),
702 PERL_MAGIC_ext, &por_cron_vtbl, NULL, 0);
703 2 RETVAL = newRV_inc((SV *)cv);
704 }
705 OUTPUT:
706 RETVAL
707  
708 # retain_job($job, $class) - the Punk::Queue task body.
709 #
710 # Recovers the plugin's state from the app class in the args, because a task
711 # body receives its job and its arguments and nothing else, and the worker
712 # compiled the same application class the server did.
713 SV *
714 por_retain_job(SV *job, SV *class)
715 CODE:
716 {
717 3 HV *out = newHV();
718 3 SV *st = NULL, *q = NULL, *store = NULL, *keep = NULL, *bytes = NULL;
719 3 SV *res = NULL, *err = NULL;
720 3 IV owner = (IV)po_getpid();
721 int locked;
722  
723 3 st = por_state_for(aTHX_ class);
724 3 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 1 2 3
725 1 STRLEN cl = 0;
726 2 ▶ const char *cp = SvOK(class) ? SvPV(class, cl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *cp = SvOK(class) ? SvPV(class, cl) : ""; 1 0 1
727 1 SvREFCNT_dec((SV *)out);
728 1 croak("Punk::Observe::Retain: no Observe plugin state for %.*s - "
729 "is the worker running the same application class as the "
730 "server?", (int)cl, cp);
731 }
732  
733 2 q = por_call_str(aTHX_ job, "queue_object");
734 2 ▶ if (!q) { SvREFCNT_dec(st); SvREFCNT_dec((SV *)out);
Branch: 1 branch
BranchTrueFalseTotal
if (!q) { SvREFCNT_dec(st); SvREFCNT_dec((SV *)out); 0 2 2
735 0 croak("Punk::Observe::Retain: the job has no queue"); }
736  
737 2 locked = por_q_lock(aTHX_ q, "observe.retain", 60, owner);
738 2 ▶ if (!locked) {
Branch: 1 branch
BranchTrueFalseTotal
if (!locked) { 1 1 2
739 1 hv_stores(out, "skipped", newSVpvs("lock"));
740 1 SvREFCNT_dec(q); SvREFCNT_dec(st);
741 1 RETVAL = newRV_noinc((SV *)out);
742 1 goto done;
743 }
744  
745 1 por_job_opts(aTHX_ st, &store, &keep, &bytes);
746 {
747 1 dSP;
748 int n;
749 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
750 1 ▶ XPUSHs(sv_2mortal(newSVpvs("store")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("store"))); 0 1 1
751 1 ▶ XPUSHs(store ? store : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(store ? store : &PL_sv_undef); 0 1 1
XPUSHs(store ? store : &PL_sv_undef); 1 0 1
752 1 ▶ XPUSHs(sv_2mortal(newSVpvs("keep_ns")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("keep_ns"))); 0 1 1
753 1 ▶ XPUSHs(keep ? keep : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(keep ? keep : &PL_sv_undef); 0 1 1
XPUSHs(keep ? keep : &PL_sv_undef); 1 0 1
754 1 ▶ if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); }
Branch: 3 branches
BranchTrueFalseTotal
if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); } 0 1 1
if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); } 0 0 0
if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); } 0 0 0
755 1 PUTBACK;
756 1 n = call_pv("Punk::Observe::Retain::pass", G_SCALAR | G_EVAL);
757 1 SPAGAIN;
758 1 ▶ res = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
res = n ? SvREFCNT_inc(POPs) : NULL; 1 0 1
759 1 PUTBACK;
760 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
761 1 ▶ if (SvTRUE(ERRSV)) err = newSVsv(ERRSV);
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) err = newSVsv(ERRSV); 1 0 1
if (SvTRUE(ERRSV)) err = newSVsv(ERRSV); 0 1 1
if (SvTRUE(ERRSV)) err = newSVsv(ERRSV); 0 0 0
762 }
763  
764 /* THE LEASE IS RELEASED WHETHER OR NOT THE PASS THREW, or the next
765 * occurrence waits out a lease nobody is holding. */
766 {
767 SV *a[2];
768 1 a[0] = sv_2mortal(newSVpvs("observe.retain"));
769 1 a[1] = sv_2mortal(newSViv(owner));
770 1 por_q_call(aTHX_ q, "unlock", a, 2);
771 }
772 1 SvREFCNT_dec(q);
773 1 SvREFCNT_dec(st);
774  
775 1 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 1 1
776 0 ▶ if (res) SvREFCNT_dec(res);
Branch: 1 branch
BranchTrueFalseTotal
if (res) SvREFCNT_dec(res); 0 0 0
777 0 SvREFCNT_dec((SV *)out);
778 0 ▶ sv_setsv(ERRSV, err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, err); 0 0 0
779 0 SvREFCNT_dec(err);
780 0 croak(NULL);
781 }
782  
783 1 ▶ if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 1 0 1
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 1 0 1
if (res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV) { 1 0 1
784 1 HV *r = (HV *)SvRV(res);
785 static const char *K[] = { "unlinked", "kept", "bytes_freed",
786 "unknown_kept", NULL };
787 int i;
788 5 ▶ for (i = 0; K[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; K[i]; i++) { 4 1 5
789 4 SV **f = hv_fetch(r, K[i], (I32)strlen(K[i]), 0);
790 4 ▶ hv_store(out, K[i], (I32)strlen(K[i]),
Branch: 1 branch
BranchTrueFalseTotal
hv_store(out, K[i], (I32)strlen(K[i]), 4 0 4
791 f ? newSVsv(*f) : newSViv(0), 0);
792 }
793 }
794 1 ▶ if (res) SvREFCNT_dec(res);
Branch: 1 branch
BranchTrueFalseTotal
if (res) SvREFCNT_dec(res); 1 0 1
795 1 RETVAL = newRV_noinc((SV *)out);
796 2 done: ;
797 }
798 OUTPUT:
799 RETVAL
« include/punk_observe/po_traceset.h ↑ Summary include/punk_observe/po_ingest.h »