xs/store.xs

filter: Statementstmt 79.9% 867 / 1085 Branchbran 56.5% 624 / 1104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.1% 1491 / 2189 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_plot.h ↑ Summary include/punk_observe/po_logidx.h »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Store PREFIX = post_
2  
3 # The three arithmetic primitives, so the Perl that still needs them one at a
4 # time is not doing digit loops.
5  
6 int
7 post_ncmp(SV *a, SV *b)
8 CODE:
9 {
10 468 STRLEN al = 0, bl = 0;
11 936 ▶ const char *ap = SvOK(a) ? SvPV(a, al) : "0";
Branch: 1 branch
BranchTrueFalseTotal
const char *ap = SvOK(a) ? SvPV(a, al) : "0"; 468 0 468
12 const char *bp;
13 int ok;
14 468 ▶ if (!SvOK(a)) al = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(a)) al = 1; 0 468 468
15 936 ▶ bp = SvOK(b) ? SvPV(b, bl) : "0";
Branch: 1 branch
BranchTrueFalseTotal
bp = SvOK(b) ? SvPV(b, bl) : "0"; 468 0 468
16 468 ▶ if (!SvOK(b)) bl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(b)) bl = 1; 0 468 468
17 468 ▶ if (!al) { ap = "0"; al = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!al) { ap = "0"; al = 1; } 0 468 468
18 468 ▶ if (!bl) { bp = "0"; bl = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!bl) { bp = "0"; bl = 1; } 0 468 468
19 468 RETVAL = po_ns_cmp_str(ap, (size_t)al, bp, (size_t)bl, &ok);
20 }
21 OUTPUT:
22 RETVAL
23  
24 SV *
25 post_nadd(SV *a, SV *b)
26 CODE:
27 {
28 9761 po_u64 x = 0, y = 0;
29 9761 (void)po_sv_to_u64(aTHX_ a, &x);
30 9761 (void)po_sv_to_u64(aTHX_ b, &y);
31 9761 RETVAL = po_u64_to_sv(po_ns_add(x, y));
32 }
33 OUTPUT:
34 RETVAL
35  
36 SV *
37 post_nsub(SV *a, SV *b)
38 CODE:
39 {
40 316 po_u64 x = 0, y = 0;
41 316 (void)po_sv_to_u64(aTHX_ a, &x);
42 316 (void)po_sv_to_u64(aTHX_ b, &y);
43 316 RETVAL = po_u64_to_sv(po_ns_sub(x, y));
44 }
45 OUTPUT:
46 RETVAL
47  
48 # An instant rounded DOWN to a multiple, which is where a bucket or a chunk
49 # begins.
50 #
51 # In u64, because the arithmetic is the thing that has to be exact: an instant
52 # is past 2^53, so `int($t / $w) * $w` in Perl rounds the product and lands on
53 # an edge that is close to the right one and is not it. The same division the
54 # executor buckets with, in the same width.
55 SV *
56 post_nfloor(SV *a, SV *b)
57 CODE:
58 {
59 6 po_u64 x = 0, w = 0;
60 6 (void)po_sv_to_u64(aTHX_ a, &x);
61 6 (void)po_sv_to_u64(aTHX_ b, &w);
62 6 ▶ RETVAL = po_u64_to_sv(w ? (x / w) * w : x);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = po_u64_to_sv(w ? (x / w) * w : x); 6 0 6
63 }
64 OUTPUT:
65 RETVAL
66  
67 # Replay one log, filter it, and hand back what survives - NEWEST FIRST.
68 #
69 # This is the whole of what the Perl scan loop used to do, and the reason it
70 # is one call rather than several: the loop it replaces made two Perl-level
71 # comparison calls per record and then sorted with a Perl comparator, so a
72 # half-million-row segment paid a million sub calls before the sort even
73 # started. The filtering is the same; only the number of times a Perl stack
74 # frame is built has changed.
75 #
76 # The rows come back in the executor's shape rather than the record's, for
77 # the same reason Store::row exists: `kind` is a name and `service` is lifted
78 # out of the attributes, because every query filters on it and no query
79 # should have to know where it lives.
80 void
81 post_scan(SV *buf, SV *opts)
82 PPCODE:
83 {
84 po_wal_replay rp;
85 STRLEN len;
86 const char *p;
87 0 HV *o = NULL;
88 SV **f;
89 0 AV *out = newAV();
90 0 HV *meta = newHV();
91 0 po_u64 from = 0, to = PO_U64_MAX, limit = 0;
92 0 int have_from = 0, have_to = 0, kind = 0, as_rows = 0;
93 0 IV scanned = 0, kept = 0;
94 0 SV **keep = NULL;
95 0 po_u64 *keys = NULL;
96 0 IV nkeep = 0, cap = 0;
97  
98 0 p = SvPV(buf, len);
99 0 ▶ if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) o = (HV *)SvRV(opts);
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) o = (HV *)SvRV(opts); 0 0 0
if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) o = (HV *)SvRV(opts); 0 0 0
100 0 ▶ if (o) {
Branch: 1 branch
BranchTrueFalseTotal
if (o) { 0 0 0
101 0 ▶ if ((f = hv_fetchs(o, "from", 0)) && SvOK(*f))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "from", 0)) && SvOK(*f)) 0 0 0
if ((f = hv_fetchs(o, "from", 0)) && SvOK(*f)) 0 0 0
102 0 have_from = po_sv_to_u64(aTHX_ *f, &from);
103 0 ▶ if ((f = hv_fetchs(o, "to", 0)) && SvOK(*f))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "to", 0)) && SvOK(*f)) 0 0 0
if ((f = hv_fetchs(o, "to", 0)) && SvOK(*f)) 0 0 0
104 0 have_to = po_sv_to_u64(aTHX_ *f, &to);
105 0 ▶ if ((f = hv_fetchs(o, "kind", 0)) && SvOK(*f)) kind = (int)SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "kind", 0)) && SvOK(*f)) kind = (int)SvIV(*f); 0 0 0
if ((f = hv_fetchs(o, "kind", 0)) && SvOK(*f)) kind = (int)SvIV(*f); 0 0 0
106 0 ▶ if ((f = hv_fetchs(o, "limit", 0)) && SvOK(*f))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "limit", 0)) && SvOK(*f)) 0 0 0
if ((f = hv_fetchs(o, "limit", 0)) && SvOK(*f)) 0 0 0
107 0 (void)po_sv_to_u64(aTHX_ *f, &limit);
108 0 ▶ if ((f = hv_fetchs(o, "rows", 0)) && SvTRUE(*f)) as_rows = 1;
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "rows", 0)) && SvTRUE(*f)) as_rows = 1; 0 0 0
if ((f = hv_fetchs(o, "rows", 0)) && SvTRUE(*f)) as_rows = 1; 0 0 0
109 }
110  
111 0 po_wal_replay_buf(p, (size_t)len, &rp, NULL, NULL);
112  
113 {
114 0 size_t off = 0;
115 0 for (;;) {
116 uint32_t magic, frame_len, n_recs, arena_len;
117 uint16_t flags, version;
118 const char *h;
119 const po_rec *recs;
120 po_arena view;
121 size_t i;
122  
123 0 ▶ if ((size_t)len - off < PO_WAL_HDR) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)len - off < PO_WAL_HDR) break; 0 0 0
124 0 h = p + off;
125 0 memcpy(&magic, h, 4); magic = po_le32(magic);
126 0 ▶ if (magic != PO_WAL_MAGIC) break;
Branch: 1 branch
BranchTrueFalseTotal
if (magic != PO_WAL_MAGIC) break; 0 0 0
127 0 memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len);
128 0 memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs);
129 0 memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len);
130 0 flags = (uint16_t)((unsigned char)h[36]
131 0 | ((unsigned char)h[37] << 8));
132 0 version = (uint16_t)((unsigned char)h[38]
133 0 | ((unsigned char)h[39] << 8));
134 0 ▶ if (version != PO_WAL_VERSION) break;
Branch: 1 branch
BranchTrueFalseTotal
if (version != PO_WAL_VERSION) break; 0 0 0
135 0 ▶ if (flags & PO_WAL_F_SEALED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & PO_WAL_F_SEALED) break; 0 0 0
136 0 ▶ if ((size_t)len - off - PO_WAL_HDR < (size_t)frame_len) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)len - off - PO_WAL_HDR < (size_t)frame_len) break; 0 0 0
137 0 ▶ if (off + PO_WAL_HDR + frame_len > rp.bytes_ok) break;
Branch: 1 branch
BranchTrueFalseTotal
if (off + PO_WAL_HDR + frame_len > rp.bytes_ok) break; 0 0 0
138  
139 0 recs = (const po_rec *)(h + PO_WAL_HDR);
140 0 view.base = (char *)(h + PO_WAL_HDR
141 0 + (size_t)n_recs * sizeof(po_rec));
142 0 view.len = (size_t)arena_len;
143 0 view.cap = (size_t)arena_len;
144  
145 0 ▶ for (i = 0; i < n_recs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_recs; i++) { 0 0 0
146 0 const po_rec *r = &recs[i];
147 0 scanned++;
148 /* The comparisons are integers here, which is the point.
149 * The Perl did them on decimal strings. */
150 0 ▶ if (have_from && r->t_unix_nano < from) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_from && r->t_unix_nano < from) continue; 0 0 0
if (have_from && r->t_unix_nano < from) continue; 0 0 0
151 0 ▶ if (have_to && r->t_unix_nano > to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_to && r->t_unix_nano > to) continue; 0 0 0
if (have_to && r->t_unix_nano > to) continue; 0 0 0
152 0 ▶ if (kind && r->kind != (uint8_t)kind) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kind && r->kind != (uint8_t)kind) continue; 0 0 0
if (kind && r->kind != (uint8_t)kind) continue; 0 0 0
153  
154 0 ▶ if (nkeep == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (nkeep == cap) { 0 0 0
155 0 ▶ IV want = cap ? cap * 2 : 256;
Branch: 1 branch
BranchTrueFalseTotal
IV want = cap ? cap * 2 : 256; 0 0 0
156 0 ▶ Renew(keep, want, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Renew(keep, want, SV *); 0 0 0
157 0 ▶ Renew(keys, want, po_u64);
Branch: 1 branch
BranchTrueFalseTotal
Renew(keys, want, po_u64); 0 0 0
158 0 cap = want;
159 }
160 0 keep[nkeep] = as_rows ? po_row_hv(aTHX_ r, &view)
161 0 ▶ : po_rec_hv(aTHX_ r, &view);
Branch: 1 branch
BranchTrueFalseTotal
: po_rec_hv(aTHX_ r, &view); 0 0 0
162 0 keys[nkeep] = r->t_unix_nano;
163 0 nkeep++;
164 }
165 0 off += PO_WAL_HDR + frame_len;
166 }
167 }
168  
169 /* NEWEST FIRST, which is the order every one of these screens wants.
170 *
171 * A WRITE-AHEAD LOG IS ALREADY IN TIME ORDER - it is append-only, so
172 * the records arrive oldest first. "Newest first" is therefore a
173 * REVERSE and not a sort, and the common case is one pass.
174 *
175 * The first version of this used an insertion sort, on the reasoning
176 * that nearly-ordered input is what insertion sort is best at. That
177 * reasoning was exactly backwards: the input is ascending and the
178 * output is descending, which is insertion sort's WORST case, and it
179 * turned a 50,000-record scan into 2.5 billion comparisons. Measured
180 * at five times slower than the Perl it replaced, which is the only
181 * reason it was found.
182 *
183 * Out-of-order records do happen - a clock stepping, a batch arriving
184 * late - so the order is checked rather than assumed, and anything
185 * that is not already sorted falls back to a real O(n log n) sort. */
186 {
187 IV i;
188 0 int ascending = 1;
189 0 ▶ for (i = 1; i < nkeep; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < nkeep; i++) 0 0 0
190 0 ▶ if (keys[i] < keys[i - 1]) { ascending = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (keys[i] < keys[i - 1]) { ascending = 0; break; } 0 0 0
191  
192 0 ▶ if (ascending) {
Branch: 1 branch
BranchTrueFalseTotal
if (ascending) { 0 0 0
193 0 ▶ for (i = 0; i < nkeep / 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nkeep / 2; i++) { 0 0 0
194 0 po_u64 tk = keys[i];
195 0 SV *tv = keep[i];
196 0 keys[i] = keys[nkeep - 1 - i];
197 0 keep[i] = keep[nkeep - 1 - i];
198 0 keys[nkeep - 1 - i] = tk;
199 0 keep[nkeep - 1 - i] = tv;
200 }
201 }
202 else {
203 po_sortpair *pairs;
204 0 ▶ Newx(pairs, nkeep, po_sortpair);
Branch: 1 branch
BranchTrueFalseTotal
Newx(pairs, nkeep, po_sortpair); 0 0 0
205 0 ▶ for (i = 0; i < nkeep; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nkeep; i++) { 0 0 0
206 0 pairs[i].key = keys[i];
207 0 pairs[i].sv = keep[i];
208 }
209 0 qsort(pairs, (size_t)nkeep, sizeof(po_sortpair), po_sortpair_desc);
210 0 ▶ for (i = 0; i < nkeep; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nkeep; i++) { 0 0 0
211 0 keys[i] = pairs[i].key;
212 0 keep[i] = pairs[i].sv;
213 }
214 0 Safefree(pairs);
215 }
216 }
217  
218 0 kept = nkeep;
219 {
220 IV i;
221 0 ▶ IV lim = limit ? (IV)limit : nkeep;
Branch: 1 branch
BranchTrueFalseTotal
IV lim = limit ? (IV)limit : nkeep; 0 0 0
222 0 ▶ for (i = 0; i < nkeep; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nkeep; i++) { 0 0 0
223 0 ▶ if (i < lim) av_push(out, keep[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (i < lim) av_push(out, keep[i]); 0 0 0
224 0 else SvREFCNT_dec(keep[i]); /* over the cap, and owned here */
225 }
226 0 hv_stores(meta, "truncated", newSViv(nkeep > lim ? 1 : 0));
227 }
228 0 Safefree(keep);
229 0 Safefree(keys);
230  
231 0 hv_stores(meta, "scanned", newSViv(scanned));
232 0 hv_stores(meta, "kept", newSViv(kept));
233 /* A log this build cannot read is REPORTED, never guessed at, and it
234 * is not fatal: the other segments still answer and the answer says
235 * it is short. */
236 0 hv_stores(meta, "reason",
237 newSVpv(po_replay_reason(rp.stopped_reason), 0));
238  
239 0 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 0 0
240 0 ▶ mXPUSHs(newRV_noinc((SV *)meta));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)meta)); 0 0 0
241 }
242  
243 # ---- the file layer -------------------------------------------------------
244  
245 # Every file the read side can see, sealed and live, oldest name first.
246 #
247 # The Perl this replaces did opendir, a sort, two regex matches and two stats
248 # per entry, then opened and parsed every sidecar in Perl. The sidecar parse
249 # is the part that mattered: one segment's index is a dozen lines, but a store
250 # with a thousand segments parsed twelve thousand lines through a regex on
251 # every single query.
252 void
253 post_scan_dir(SV *dir)
254 PPCODE:
255 {
256 po_dir d;
257 STRLEN dl;
258 24 const char *dp = SvPV(dir, dl);
259 24 AV *out = newAV();
260 const char *name;
261 24 AV *names = newAV();
262 SSize_t i, n;
263  
264 24 ▶ if (!po_is_dir(dp)) { mXPUSHs(newRV_noinc((SV *)out)); XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (!po_is_dir(dp)) { mXPUSHs(newRV_noinc((SV *)out)); XSRETURN(1); } 1 23 24
if (!po_is_dir(dp)) { mXPUSHs(newRV_noinc((SV *)out)); XSRETURN(1); } 0 1 1
265 23 ▶ if (!po_opendir(&d, dp)) { mXPUSHs(newRV_noinc((SV *)out)); XSRETURN(1); }
Branch: 2 branches
BranchTrueFalseTotal
if (!po_opendir(&d, dp)) { mXPUSHs(newRV_noinc((SV *)out)); XSRETURN(1); } 0 23 23
if (!po_opendir(&d, dp)) { mXPUSHs(newRV_noinc((SV *)out)); XSRETURN(1); } 0 0 0
266 144 ▶ while ((name = po_readdir(&d))) av_push(names, newSVpv(name, 0));
Branch: 1 branch
BranchTrueFalseTotal
while ((name = po_readdir(&d))) av_push(names, newSVpv(name, 0)); 121 23 144
267 23 po_closedir(&d);
268  
269 /* Sorted by NAME, and the name carries the sealing time - so this is
270 * chronological without a stat per entry. */
271 23 sortsv(AvARRAY(names), (SSize_t)(av_len(names) + 1), Perl_sv_cmp);
272  
273 23 n = av_len(names) + 1;
274 144 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 121 23 144
275 121 SV **e = av_fetch(names, i, 0);
276 STRLEN nl;
277 const char *np;
278 int sealed;
279 char path[PO_PATHMAX];
280 HV *h;
281 121 po_u64 sz = 0;
282  
283 204 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 121 121
284 121 np = SvPV(*e, nl);
285 121 ▶ if (nl > 4 && memcmp(np + nl - 4, ".seg", 4) == 0) sealed = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (nl > 4 && memcmp(np + nl - 4, ".seg", 4) == 0) sealed = 1; 75 46 121
if (nl > 4 && memcmp(np + nl - 4, ".seg", 4) == 0) sealed = 1; 37 38 75
286 84 ▶ else if (nl > 4 && memcmp(np + nl - 4, ".wal", 4) == 0) sealed = 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (nl > 4 && memcmp(np + nl - 4, ".wal", 4) == 0) sealed = 0; 38 46 84
else if (nl > 4 && memcmp(np + nl - 4, ".wal", 4) == 0) sealed = 0; 1 37 38
287 83 else continue;
288  
289 38 ▶ if (!po_path_join(path, sizeof(path), dp, np)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, np)) continue; 0 38 38
290 38 po_file_size(path, &sz);
291  
292 38 h = newHV();
293 38 hv_stores(h, "path", newSVpv(path, 0));
294 38 hv_stores(h, "name", newSVpvn(np, nl));
295 38 hv_stores(h, "sealed", newSViv(sealed));
296 38 hv_stores(h, "bytes", po_u64_to_sv(sz));
297  
298 38 ▶ if (sealed) {
Branch: 1 branch
BranchTrueFalseTotal
if (sealed) { 37 1 38
299 char idx[PO_PATHMAX];
300 37 memcpy(idx, path, strlen(path) + 1);
301 37 memcpy(idx + strlen(idx) - 4, ".idx", 5);
302 37 hv_stores(h, "idx_path", newSVpv(idx, 0));
303 37 hv_stores(h, "index", po_index_read_sv(aTHX_ idx));
304 }
305 1 else hv_stores(h, "index", newSV(0));
306  
307 38 av_push(out, newRV_noinc((SV *)h));
308 }
309 23 SvREFCNT_dec((SV *)names);
310 23 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 23 23
311 }
312  
313 # Read one sidecar. Exposed so a caller can read an index without listing a
314 # directory, and so the format has one reader rather than two.
315 SV *
316 post_read_index(SV *path)
317 CODE:
318 0 RETVAL = po_index_read_sv(aTHX_ SvPV_nolen(path));
319 OUTPUT:
320 RETVAL
321  
322 # Write one, atomically. A reader must never see half a sidecar, so this is
323 # write-to-temporary then rename rather than open-and-print.
324 int
325 post_write_index(SV *path, SV *summary)
326 CODE:
327 {
328 0 ▶ if (!SvROK(summary) || SvTYPE(SvRV(summary)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(summary) || SvTYPE(SvRV(summary)) != SVt_PVHV) 0 0 0
if (!SvROK(summary) || SvTYPE(SvRV(summary)) != SVt_PVHV) 0 0 0
329 0 croak("summary must be a hashref");
330 0 RETVAL = po_index_write(aTHX_ SvPV_nolen(path), (HV *)SvRV(summary));
331 }
332 OUTPUT:
333 RETVAL
334  
335 SV *
336 post_slurp(SV *path)
337 CODE:
338 {
339 0 size_t n = 0;
340 0 char *b = po_slurp(SvPV_nolen(path), &n);
341 0 ▶ if (!b) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) XSRETURN_UNDEF; 0 0 0
342 0 RETVAL = newSVpvn(b, n);
343 0 free(b);
344 }
345 OUTPUT:
346 RETVAL
347  
348 int
349 post_mkpath(SV *path)
350 CODE:
351 0 ▶ RETVAL = po_mkpath(SvPV_nolen(path)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = po_mkpath(SvPV_nolen(path)) ? 1 : 0; 0 0 0
352 OUTPUT:
353 RETVAL
354  
355 int
356 post_rename_file(SV *from, SV *to)
357 CODE:
358 0 ▶ RETVAL = po_seal_rename(SvPV_nolen(from), SvPV_nolen(to)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = po_seal_rename(SvPV_nolen(from), SvPV_nolen(to)) ? 1 : 0; 0 0 0
RETVAL = po_seal_rename(SvPV_nolen(from), SvPV_nolen(to)) ? 1 : 0; 0 0 0
359 OUTPUT:
360 RETVAL
361  
362 SV *
363 post_file_size(SV *path)
364 CODE:
365 {
366 0 po_u64 sz = 0;
367 0 ▶ if (!po_file_size(SvPV_nolen(path), &sz)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_file_size(SvPV_nolen(path), &sz)) XSRETURN_UNDEF; 0 0 0
368 0 RETVAL = po_u64_to_sv(sz);
369 }
370 OUTPUT:
371 RETVAL
372  
373 # Summarise a sealed log in ONE pass over its records.
374 #
375 # EVERY FIGURE HERE IS ONE A QUERY WOULD OTHERWISE SCAN THE WHOLE SEGMENT TO
376 # LEARN: the time span is what lets a range query skip the file without
377 # opening it, and the severity histogram is what makes "how many errors" a
378 # read of a hundred bytes.
379 #
380 # The COUNTS of a segment, from the raw records, without building a hash per
381 # record the way replay_bodies has to.
382 #
383 # COUNTS ONLY. It does not build the service graph and it does not count
384 # traces, and `seal` therefore does not use it: those two are the whole reason
385 # a sidecar makes the service map a read of a few hundred bytes instead of a
386 # scan, and a summary with `edges => []` in it would draw an empty map at full
387 # speed. Wiring this in means finishing it first - the interning and the span
388 # conversion the graph needs are the missing half.
389 void
390 post_summarise(SV *buf)
391 PPCODE:
392 {
393 po_wal_replay rp;
394 STRLEN len;
395 91 const char *p = SvPV(buf, len);
396 91 HV *s = newHV();
397 91 HV *sev = newHV(), *svc = newHV();
398 91 po_u64 t_min = PO_U64_MAX, t_max = 0;
399 91 IV records = 0, metrics = 0, logs = 0, spans = 0, errors = 0;
400 91 int have_t = 0;
401 po_sgraph g;
402 po_intern names;
403 int graph_ok;
404  
405 91 ▶ graph_ok = po_sgraph_init(&g) && po_intern_init(&names, 64);
Branch: 2 branches
BranchTrueFalseTotal
graph_ok = po_sgraph_init(&g) && po_intern_init(&names, 64); 91 0 91
graph_ok = po_sgraph_init(&g) && po_intern_init(&names, 64); 91 0 91
406  
407 91 po_wal_replay_buf(p, (size_t)len, &rp, NULL, NULL);
408  
409 {
410 91 size_t off = 0;
411 93 for (;;) {
412 uint32_t magic, frame_len, n_recs, arena_len;
413 uint16_t flags, version;
414 const char *h;
415 const po_rec *recs;
416 po_arena view;
417 size_t i;
418  
419 184 ▶ if ((size_t)len - off < PO_WAL_HDR) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)len - off < PO_WAL_HDR) break; 91 93 184
420 93 h = p + off;
421 93 memcpy(&magic, h, 4); magic = po_le32(magic);
422 93 ▶ if (magic != PO_WAL_MAGIC) break;
Branch: 1 branch
BranchTrueFalseTotal
if (magic != PO_WAL_MAGIC) break; 0 93 93
423 93 memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len);
424 93 memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs);
425 93 memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len);
426 93 version = (uint16_t)((unsigned char)h[38]
427 93 | ((unsigned char)h[39] << 8));
428 93 flags = (uint16_t)((unsigned char)h[36]
429 93 | ((unsigned char)h[37] << 8));
430 93 ▶ if (version != PO_WAL_VERSION) break;
Branch: 1 branch
BranchTrueFalseTotal
if (version != PO_WAL_VERSION) break; 0 93 93
431  
432 /* THE SEAL TRAILER IS NOT A FRAME OF RECORDS. It carries the
433 * FILE'S TOTAL RECORD COUNT in the field every other frame
434 * uses for its own, with frame_len zero - so walking it as
435 * data reads 88 bytes times every record the segment ever
436 * held, past the end of the buffer. The flag was read here
437 * and then thrown away with a (void) cast. */
438 93 ▶ if (flags & PO_WAL_F_SEALED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & PO_WAL_F_SEALED) break; 0 93 93
439  
440 93 ▶ if ((size_t)len - off - PO_WAL_HDR < (size_t)frame_len) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)len - off - PO_WAL_HDR < (size_t)frame_len) break; 0 93 93
441 93 ▶ if (off + PO_WAL_HDR + frame_len > rp.bytes_ok) break;
Branch: 1 branch
BranchTrueFalseTotal
if (off + PO_WAL_HDR + frame_len > rp.bytes_ok) break; 0 93 93
442  
443 93 recs = (const po_rec *)(h + PO_WAL_HDR);
444 93 view.base = (char *)(h + PO_WAL_HDR
445 93 + (size_t)n_recs * sizeof(po_rec));
446 93 view.len = (size_t)arena_len;
447 93 view.cap = (size_t)arena_len;
448  
449 12481 ▶ for (i = 0; i < n_recs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_recs; i++) { 12388 93 12481
450 12388 const po_rec *r = &recs[i];
451 12388 const char *service = NULL;
452 12388 size_t svc_len = 0;
453  
454 12388 records++;
455 12388 ▶ if (!have_t || r->t_unix_nano < t_min) t_min = r->t_unix_nano;
Branch: 2 branches
BranchTrueFalseTotal
if (!have_t || r->t_unix_nano < t_min) t_min = r->t_unix_nano; 12297 91 12388
if (!have_t || r->t_unix_nano < t_min) t_min = r->t_unix_nano; 0 12297 12297
456 12388 ▶ if (!have_t || r->t_unix_nano > t_max) t_max = r->t_unix_nano;
Branch: 2 branches
BranchTrueFalseTotal
if (!have_t || r->t_unix_nano > t_max) t_max = r->t_unix_nano; 12297 91 12388
if (!have_t || r->t_unix_nano > t_max) t_max = r->t_unix_nano; 11792 505 12297
457 12388 have_t = 1;
458  
459 12388 po_rec_service(r, &view, &service, &svc_len);
460 12388 ▶ if (!service || !svc_len) { service = "unknown"; svc_len = 7; }
Branch: 2 branches
BranchTrueFalseTotal
if (!service || !svc_len) { service = "unknown"; svc_len = 7; } 6203 6185 12388
if (!service || !svc_len) { service = "unknown"; svc_len = 7; } 0 6203 6203
461 {
462 12388 SV **slot = hv_fetch(svc, service, (I32)svc_len, 1);
463 12388 ▶ if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 12388 0 12388
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 12283 105 12388
464 }
465  
466 12388 ▶ if (r->kind == PO_LOG) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind == PO_LOG) { 1802 10586 12388
467 char kb[16];
468 1802 int kn = snprintf(kb, sizeof(kb), "%u",
469 1802 (unsigned)r->severity);
470 SV **slot;
471 1802 logs++;
472 1802 slot = hv_fetch(sev, kb, kn, 1);
473 1802 ▶ if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 1802 0 1802
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 1736 66 1802
474 }
475 10586 ▶ else if (r->kind == PO_METRIC) metrics++;
Branch: 1 branch
BranchTrueFalseTotal
else if (r->kind == PO_METRIC) metrics++; 4793 5793 10586
476 5793 ▶ else if (r->kind == PO_SPAN) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->kind == PO_SPAN) { 5793 0 5793
477 5793 spans++;
478 5793 ▶ if (po_rec_status(r) == 2) errors++;
Branch: 1 branch
BranchTrueFalseTotal
if (po_rec_status(r) == 2) errors++; 12 5781 5793
479 }
480 }
481 93 off += PO_WAL_HDR + frame_len;
482 }
483 }
484  
485 91 hv_stores(s, "records", newSViv(records));
486 91 ▶ hv_stores(s, "t_min", have_t ? po_u64_to_sv(t_min) : newSViv(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(s, "t_min", have_t ? po_u64_to_sv(t_min) : newSViv(0)); 91 0 91
487 91 ▶ hv_stores(s, "t_max", have_t ? po_u64_to_sv(t_max) : newSViv(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(s, "t_max", have_t ? po_u64_to_sv(t_max) : newSViv(0)); 91 0 91
488 91 hv_stores(s, "metrics", newSViv(metrics));
489 91 hv_stores(s, "logs", newSViv(logs));
490 91 hv_stores(s, "spans", newSViv(spans));
491 91 hv_stores(s, "errors", newSViv(errors));
492 91 hv_stores(s, "severity", newRV_noinc((SV *)sev));
493 91 hv_stores(s, "service", newRV_noinc((SV *)svc));
494  
495 /* THE GRAPH AND THE TRACE COUNT, IN THE SAME PASS.
496 *
497 * Both were Perl until now, and both walked the records a second time
498 * to get there - one to build spans for Trace::analyse, one to count
499 * distinct trace ids in a hash. The spans are gathered once here and
500 * both answers come off the same set.
501 *
502 * The graph is accumulated AT SEAL and never at read time. A map
503 * built by reading every span of every segment is a map nobody waits
504 * for. */
505 {
506 po_span_gather sg;
507 91 AV *edges = newAV();
508 91 IV ntraces = 0;
509  
510 91 ▶ if (po_gather_init(&sg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_gather_init(&sg)) { 91 0 91
511 91 po_gather_wal(&sg, p, (size_t)len, 0, 0, 0, 0);
512 91 po_span_w_seal(&sg.w);
513  
514 91 ▶ if (sg.w.n) {
Branch: 1 branch
BranchTrueFalseTotal
if (sg.w.n) { 23 68 91
515 po_sgraph gr;
516 po_tsum_set ts;
517  
518 23 ▶ if (po_sgraph_init(&gr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_sgraph_init(&gr)) { 23 0 23
519 23 ▶ if (po_sgraph_build(&gr, sg.w.s, (uint32_t)sg.w.n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_sgraph_build(&gr, sg.w.s, (uint32_t)sg.w.n)) { 23 0 23
520 uint32_t k;
521 56 ▶ for (k = 0; k < gr.n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < gr.n; k++) { 33 23 56
522 33 HV *e = newHV();
523 33 uint32_t cl = 0, el = 0;
524 66 const char *cp = (gr.e[k].caller == PO_SVC_UNKNOWN)
525 33 ▶ ? NULL : po_intern_get(&sg.sym, gr.e[k].caller, &cl);
Branch: 1 branch
BranchTrueFalseTotal
? NULL : po_intern_get(&sg.sym, gr.e[k].caller, &cl); 3 30 33
526 const char *ep =
527 33 po_intern_get(&sg.sym, gr.e[k].callee, &el);
528 33 ▶ hv_stores(e, "caller", cp ? newSVpvn(cp, cl)
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "caller", cp ? newSVpvn(cp, cl) 3 30 33
529 : newSVpvs("*"));
530 33 ▶ hv_stores(e, "callee", ep ? newSVpvn(ep, el)
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(e, "callee", ep ? newSVpvn(ep, el) 33 0 33
531 : newSVpvs("unknown"));
532 33 hv_stores(e, "count", po_u64_to_sv(gr.e[k].count));
533 33 hv_stores(e, "errors", po_u64_to_sv(gr.e[k].errors));
534 33 hv_stores(e, "dur_max", po_u64_to_sv(gr.e[k].dur_max));
535 33 av_push(edges, newRV_noinc((SV *)e));
536 }
537 }
538 23 po_sgraph_free(&gr);
539 }
540  
541 23 ▶ if (po_tsum_init(&ts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tsum_init(&ts)) { 23 0 23
542 23 ▶ if (po_tsum_build(&ts, sg.w.s, (uint32_t)sg.w.n))
Branch: 1 branch
BranchTrueFalseTotal
if (po_tsum_build(&ts, sg.w.s, (uint32_t)sg.w.n)) 23 0 23
543 23 ntraces = (IV)ts.n;
544 23 po_tsum_free(&ts);
545 }
546 }
547 91 po_gather_free(&sg);
548 }
549 91 hv_stores(s, "traces", newSViv(ntraces));
550 91 hv_stores(s, "edges", newRV_noinc((SV *)edges));
551 }
552  
553 91 ▶ if (graph_ok) { po_sgraph_free(&g); po_intern_free(&names); }
Branch: 1 branch
BranchTrueFalseTotal
if (graph_ok) { po_sgraph_free(&g); po_intern_free(&names); } 91 0 91
554 91 ▶ mXPUSHs(newRV_noinc((SV *)s));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)s)); 0 91 91
555 }
556  
557 # Retention: delete WHOLE SEGMENTS, oldest first, until the store is under
558 # budget.
559 #
560 # DELETION IS BY UNLINK AND NEVER BY TRUNCATION. A reader mid-query keeps the
561 # copy it read; the directory entry goes immediately, which is what makes the
562 # space reclaimable rather than reclaimed. Truncating a file another process
563 # has mapped is how a reader gets a SIGBUS instead of an answer.
564 #
565 # The Perl this replaces listed the directory, stat-ed every entry, sorted in
566 # Perl and unlinked in a loop - which is four passes over a listing the C
567 # already has in one.
568 void
569 post_retain(SV *self, ...)
570 PPCODE:
571 {
572 po_dir d;
573 char dpbuf[PO_PATHMAX];
574 6 const char *dp = dpbuf;
575 const char *name;
576 6 AV *names = newAV();
577 SSize_t i, n;
578 6 po_u64 bytes = 0, total = 0, budget_v = 0;
579 6 IV keep = 0, ai;
580 HV *empty;
581 6 IV sealed = 0, deleted = 0;
582 6 po_u64 freed = 0;
583 6 HV *out = newHV();
584  
585 /* Named arguments off the stack: retain(bytes => N, keep => N). */
586 12 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 6 6 12
587 6 const char *k = SvPV_nolen(ST(ai));
588 6 ▶ if (strEQ(k, "bytes")) (void)po_sv_to_u64(aTHX_ ST(ai + 1), &budget_v);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "bytes")) (void)po_sv_to_u64(aTHX_ ST(ai + 1), &budget_v); 5 1 6
589 1 ▶ else if (strEQ(k, "keep")) keep = SvIV(ST(ai + 1));
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "keep")) keep = SvIV(ST(ai + 1)); 1 0 1
590 }
591  
592 6 ▶ if (!po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) { 0 6 6
593 0 empty = newHV();
594 0 hv_stores(empty, "deleted", newSViv(0));
595 0 hv_stores(empty, "freed", newSViv(0));
596 0 hv_stores(empty, "kept", newSViv(0));
597 0 hv_stores(empty, "bytes", newSViv(0));
598 0 ▶ mXPUSHs(newRV_noinc((SV *)empty));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)empty)); 0 0 0
599 0 XSRETURN(1);
600 }
601  
602 6 ▶ if (po_opendir(&d, dp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_opendir(&d, dp)) { 5 1 6
603 39 ▶ while ((name = po_readdir(&d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((name = po_readdir(&d))) { 34 5 39
604 34 size_t nl = strlen(name);
605 34 ▶ if (nl > 4 && memcmp(name + nl - 4, ".seg", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (nl > 4 && memcmp(name + nl - 4, ".seg", 4) == 0) 24 10 34
if (nl > 4 && memcmp(name + nl - 4, ".seg", 4) == 0) 12 12 24
606 12 av_push(names, newSVpv(name, 0));
607 }
608 5 po_closedir(&d);
609 }
610  
611 /* OLDEST FIRST, and the name carries the sealing time - so this is
612 * chronological without a stat per entry to ask. */
613 6 sortsv(AvARRAY(names), (SSize_t)(av_len(names) + 1), Perl_sv_cmp);
614 6 n = av_len(names) + 1;
615 6 sealed = (IV)n;
616  
617 18 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 6 18
618 12 SV **e = av_fetch(names, i, 0);
619 char path[PO_PATHMAX];
620 12 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 12 12
621 24 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) continue; 0 12 12
622 12 bytes = 0;
623 12 po_file_size(path, &bytes);
624 12 total += bytes;
625 }
626  
627 6 ▶ if (budget_v && total > budget_v) {
Branch: 2 branches
BranchTrueFalseTotal
if (budget_v && total > budget_v) { 5 1 6
if (budget_v && total > budget_v) { 3 2 5
628 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 1 9
629 8 SV **e = av_fetch(names, i, 0);
630 char path[PO_PATHMAX], idx[PO_PATHMAX];
631 size_t pl;
632 9 ▶ if (total <= budget_v) break;
Branch: 1 branch
BranchTrueFalseTotal
if (total <= budget_v) break; 1 7 8
633 7 ▶ if (sealed - deleted <= keep) break;
Branch: 1 branch
BranchTrueFalseTotal
if (sealed - deleted <= keep) break; 1 6 7
634 6 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 6 6
635 12 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) continue; 0 6 6
636  
637 6 bytes = 0;
638 6 po_file_size(path, &bytes);
639 6 ▶ if (remove(path) != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (remove(path) != 0) continue; 0 6 6
640  
641 /* The sidecar goes with it. Leaving one behind is the other
642 * half of an interrupted pass, and `stats` counts those. */
643 6 pl = strlen(path);
644 6 memcpy(idx, path, pl + 1);
645 6 memcpy(idx + pl - 4, ".idx", 5);
646 6 remove(idx);
647  
648 6 total -= bytes;
649 6 freed += bytes;
650 6 deleted++;
651 }
652 }
653 6 SvREFCNT_dec((SV *)names);
654  
655 6 hv_stores(out, "deleted", newSViv(deleted));
656 6 hv_stores(out, "freed", po_u64_to_sv(freed));
657 6 hv_stores(out, "kept", newSViv(sealed - deleted));
658 6 hv_stores(out, "bytes", po_u64_to_sv(total));
659 6 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 6 6
660 }
661  
662 # What the status screen shows.
663 #
664 # EVERY FIGURE COMES FROM THE SIDECARS RATHER THAN FROM A SCAN, so the page is
665 # cheap enough to leave open. The Perl this replaces parsed every sidecar into
666 # a Perl hash to add up eight integers and throw the rest away; this reads the
667 # numbers straight out of the bytes.
668 void
669 post_stats(SV *self)
670 PPCODE:
671 {
672 char dpbuf[PO_PATHMAX];
673 22 const char *dp = dpbuf;
674 22 HV *out = newHV();
675 22 HV *svc = newHV();
676 22 HV *snap = NULL;
677 22 AV *segs = NULL, *wals = NULL;
678 SSize_t i, n;
679 22 IV segments = 0, wal_depth = 0, unindexed = 0, orphan = 0;
680 22 po_u64 records = 0, bytes = 0, logs = 0, spans = 0,
681 22 metrics = 0, errors = 0, traces = 0;
682  
683 /* Everything about the sealed segments - counts, sizes, services,
684 * even the orphaned-index tally - comes out of the snapshot, so the
685 * page this feeds can be left open without re-reading a sidecar per
686 * segment per refresh. Only the LIVE logs are walked per call: their
687 * numbers change with every append and nothing about them is cached. */
688 22 ▶ if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf)))
Branch: 1 branch
BranchTrueFalseTotal
if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) 22 0 22
689 22 snap = po_snap_get(aTHX_ self, dp);
690 22 ▶ if (snap) {
Branch: 1 branch
BranchTrueFalseTotal
if (snap) { 20 2 22
691 20 SV **f = hv_fetchs(snap, "segs", 0);
692 20 ▶ if (f && SvROK(*f)) segs = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 20 0 20
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 20 0 20
693 20 f = hv_fetchs(snap, "wals", 0);
694 20 ▶ if (f && SvROK(*f)) wals = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 20 0 20
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 20 0 20
695 20 f = hv_fetchs(snap, "orphan", 0);
696 20 ▶ if (f && SvOK(*f)) orphan = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) orphan = SvIV(*f); 20 0 20
if (f && SvOK(*f)) orphan = SvIV(*f); 20 0 20
697 }
698  
699 22 ▶ n = segs ? av_len(segs) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = segs ? av_len(segs) + 1 : 0; 20 2 22
700 52 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 30 22 52
701 30 SV **e = av_fetch(segs, i, 0);
702 AV *ea;
703 SV **f;
704  
705 30 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 30 0 30
if (!e || !SvROK(*e)) continue; 0 30 30
706 30 ea = (AV *)SvRV(*e);
707 30 segments++;
708 30 bytes += po_snap_u64(aTHX_ ea, PO_SNAP_SIZE);
709  
710 /* A sealed segment with no summary is a seal that was
711 * interrupted. It is still readable and still counted, and saying
712 * so is the difference between a number that is low and a number
713 * that is wrong. */
714 30 ▶ if (!(po_snap_flags(aTHX_ ea) & PO_SNAP_HAS_IDX)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(po_snap_flags(aTHX_ ea) & PO_SNAP_HAS_IDX)) { 5 25 30
715 5 unindexed++;
716 5 continue;
717 }
718 25 records += po_snap_u64(aTHX_ ea, PO_SNAP_RECORDS);
719 25 logs += po_snap_u64(aTHX_ ea, PO_SNAP_LOGS);
720 25 spans += po_snap_u64(aTHX_ ea, PO_SNAP_SPANS);
721 25 metrics += po_snap_u64(aTHX_ ea, PO_SNAP_METRICS);
722 25 errors += po_snap_u64(aTHX_ ea, PO_SNAP_ERRORS);
723 25 traces += po_snap_u64(aTHX_ ea, PO_SNAP_TRACES);
724  
725 25 f = av_fetch(ea, PO_SNAP_SVC, 0);
726 25 ▶ if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 25 0 25
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 25 0 25
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 25 0 25
727 25 HV *sh = (HV *)SvRV(*f);
728 HE *he;
729 25 hv_iterinit(sh);
730 53 ▶ while ((he = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(sh))) { 28 25 53
731 STRLEN kl;
732 28 ▶ const char *kp = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *kp = HePV(he, kl); 0 28 28
733 28 SV **slot = hv_fetch(svc, kp, (I32)kl, 1);
734 28 ▶ if (slot)
Branch: 1 branch
BranchTrueFalseTotal
if (slot) 28 0 28
735 28 ▶ sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0)
Branch: 1 branch
BranchTrueFalseTotal
sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) 12 16 28
736 + SvIV(HeVAL(he)));
737 }
738 }
739 }
740  
741 /* A LIVE LOG IS COUNTED TOO, from its own frames. Counting only
742 * the sealed segments made a receiver that had accepted ten
743 * thousand records and not yet reached its seal threshold report
744 * nothing at all - which reads as "no telemetry is arriving"
745 * while every other screen shows it arriving. */
746 22 ▶ n = wals ? av_len(wals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = wals ? av_len(wals) + 1 : 0; 20 2 22
747 32 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 22 32
748 10 SV **e = av_fetch(wals, i, 0);
749 char path[PO_PATHMAX];
750 10 po_u64 sz = 0;
751 po_idx_nums ix;
752  
753 10 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 10 10
754 20 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) 0 10 10
755 0 continue;
756 10 po_file_size(path, &sz);
757 10 bytes += sz;
758 10 wal_depth++;
759 10 ▶ if (po_wal_nums(path, &ix, svc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_wal_nums(path, &ix, svc)) { 10 0 10
760 10 records += ix.records;
761 10 logs += ix.logs;
762 10 spans += ix.spans;
763 10 metrics += ix.metrics;
764 10 errors += ix.errors;
765 }
766 }
767  
768 22 hv_stores(out, "segments", newSViv(segments));
769 22 hv_stores(out, "wal_depth", newSViv(wal_depth));
770 22 hv_stores(out, "unindexed", newSViv(unindexed));
771 22 hv_stores(out, "orphan_index", newSViv(orphan));
772 22 hv_stores(out, "records", po_u64_to_sv(records));
773 22 hv_stores(out, "bytes", po_u64_to_sv(bytes));
774 22 hv_stores(out, "logs", po_u64_to_sv(logs));
775 22 hv_stores(out, "spans", po_u64_to_sv(spans));
776 22 hv_stores(out, "metrics", po_u64_to_sv(metrics));
777 22 hv_stores(out, "errors", po_u64_to_sv(errors));
778 22 hv_stores(out, "traces", po_u64_to_sv(traces));
779 22 ▶ hv_stores(out, "services", newSViv((IV)HvUSEDKEYS(svc)));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(out, "services", newSViv((IV)HvUSEDKEYS(svc))); 0 22 22
780 22 hv_stores(out, "service", newRV_noinc((SV *)svc));
781  
782 /* A deleted segment a reader still holds open is still occupying the
783 * disk and is invisible to du. This store cannot accumulate one: a
784 * read copies the segment and lets go of it inside the call, so a
785 * retention pass never runs against a live mapping. Reported anyway -
786 * a design property nothing displays is one nobody can check. */
787 {
788 22 SV **md = hv_fetchs((HV *)SvRV(self), "mapped_deleted", 0);
789 22 ▶ hv_stores(out, "mapped_deleted",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(out, "mapped_deleted", 0 22 22
hv_stores(out, "mapped_deleted", 0 0 0
790 newSViv(md && SvOK(*md) ? SvIV(*md) : 0));
791 }
792 22 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 22 22
793 }
794  
795 # The store's read generation: an opaque token that changes whenever the
796 # SET of files changes - a seal, a new live log, a retention unlink. One
797 # stat. An append to an existing live log does NOT move it, which is the
798 # advertised meaning: "the sealed data is as it was", not "nothing arrived".
799 void
800 post_generation(SV *self)
801 PPCODE:
802 {
803 char dpbuf[PO_PATHMAX];
804 struct stat st;
805 0 ▶ if (!po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf)) 0 0 0
806 0 ▶ || stat(dpbuf, &st) != 0)
Branch: 1 branch
BranchTrueFalseTotal
|| stat(dpbuf, &st) != 0) 0 0 0
807 0 XSRETURN_PV("");
808 0 ▶ mXPUSHs(newSVpvf("%" IVdf, (IV)st.st_mtime));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvf("%" IVdf, (IV)st.st_mtime)); 0 0 0
809 }
810  
811 # The distinct metric names in a window, with how often each was seen.
812 #
813 # The metrics landing page's question, answered without building a row for
814 # every point: the walk prunes segments by span and by per-kind count from
815 # the snapshot, and tallies names straight out of the frame arenas. The cap
816 # is `max_rows` metric records; a capped answer says so in `truncated`.
817 void
818 post_metric_names(SV *self, ...)
819 PPCODE:
820 {
821 char dpbuf[PO_PATHMAX];
822 6 const char *dp = dpbuf;
823 6 po_u64 from = 0, to = PO_U64_MAX, cap = 0, tallied = 0;
824 6 int have_from = 0, have_to = 0;
825 6 IV ai, files = 0, skipped = 0, degraded = 0;
826 6 HV *seen = newHV();
827 6 HV *meta = newHV();
828 6 HV *snap = NULL;
829 6 AV *segs = NULL, *wals = NULL;
830 SSize_t i, n;
831 6 int stopped = 0;
832  
833 18 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 12 6 18
834 12 const char *k = SvPV_nolen(ST(ai));
835 12 SV *v = ST(ai + 1);
836 12 ▶ if (strEQ(k, "from") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "from") && SvOK(v)) 6 6 12
if (strEQ(k, "from") && SvOK(v)) 6 0 6
837 6 have_from = po_sv_to_u64(aTHX_ v, &from);
838 6 ▶ else if (strEQ(k, "to") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "to") && SvOK(v)) 6 0 6
else if (strEQ(k, "to") && SvOK(v)) 6 0 6
839 6 have_to = po_sv_to_u64(aTHX_ v, &to);
840 }
841 {
842 6 SV **f = hv_fetchs((HV *)SvRV(self), "max_rows", 0);
843 6 ▶ cap = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000;
Branch: 2 branches
BranchTrueFalseTotal
cap = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000; 6 0 6
cap = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000; 6 0 6
844 }
845  
846 6 ▶ if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf)))
Branch: 1 branch
BranchTrueFalseTotal
if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) 6 0 6
847 6 snap = po_snap_get(aTHX_ self, dp);
848 6 ▶ if (snap) {
Branch: 1 branch
BranchTrueFalseTotal
if (snap) { 6 0 6
849 6 SV **f = hv_fetchs(snap, "segs", 0);
850 6 ▶ if (f && SvROK(*f)) segs = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 6 0 6
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 6 0 6
851 6 f = hv_fetchs(snap, "wals", 0);
852 6 ▶ if (f && SvROK(*f)) wals = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 6 0 6
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 6 0 6
853 }
854  
855 6 ▶ n = wals ? av_len(wals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = wals ? av_len(wals) + 1 : 0; 6 0 6
856 8 ▶ for (i = 0; i < n && !stopped; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && !stopped; i++) { 2 6 8
for (i = 0; i < n && !stopped; i++) { 2 0 2
857 2 SV **e = av_fetch(wals, i, 0);
858 char path[PO_PATHMAX];
859 2 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 2 2
860 4 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) 0 2 2
861 0 continue;
862 2 tallied += po_metric_names_file(aTHX_ path, from, have_from,
863 to, have_to, seen,
864 &files, &degraded);
865 2 ▶ if (tallied >= cap) stopped = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (tallied >= cap) stopped = 1; 0 2 2
866 }
867  
868 6 ▶ n = segs ? av_len(segs) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = segs ? av_len(segs) + 1 : 0; 6 0 6
869 14 ▶ for (i = 0; i < n && !stopped; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && !stopped; i++) { 9 5 14
for (i = 0; i < n && !stopped; i++) { 8 1 9
870 8 SV **e = av_fetch(segs, i, 0);
871 AV *ea;
872 SV **nm;
873 char path[PO_PATHMAX];
874  
875 11 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 8 0 8
if (!e || !SvROK(*e)) continue; 0 8 8
876 8 ea = (AV *)SvRV(*e);
877  
878 8 ▶ if ((have_from || have_to)
Branch: 2 branches
BranchTrueFalseTotal
if ((have_from || have_to) 0 8 8
if ((have_from || have_to) 0 0 0
879 8 ▶ && (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN)) { 8 0 8
880 8 po_u64 ix_min = po_snap_u64(aTHX_ ea, PO_SNAP_TMIN);
881 8 po_u64 ix_max = po_snap_u64(aTHX_ ea, PO_SNAP_TMAX);
882 8 ▶ if ((have_from && ix_max < from)
Branch: 2 branches
BranchTrueFalseTotal
if ((have_from && ix_max < from) 8 0 8
if ((have_from && ix_max < from) 7 1 8
883 8 ▶ || (have_to && ix_min > to)) { skipped++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
|| (have_to && ix_min > to)) { skipped++; continue; } 7 0 7
|| (have_to && ix_min > to)) { skipped++; continue; } 1 6 7
884 }
885 6 ▶ if (po_snap_kind_absent(aTHX_ ea, PO_METRIC)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_snap_kind_absent(aTHX_ ea, PO_METRIC)) { 1 5 6
886 1 skipped++;
887 1 continue;
888 }
889  
890 5 nm = av_fetch(ea, PO_SNAP_NAME, 0);
891 5 ▶ if (!nm) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) continue; 0 5 5
892 10 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*nm)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*nm))) 0 5 5
893 0 continue;
894 5 tallied += po_metric_names_file(aTHX_ path, from, have_from,
895 to, have_to, seen,
896 &files, &degraded);
897 5 ▶ if (tallied >= cap) stopped = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (tallied >= cap) stopped = 1; 1 4 5
898 }
899  
900 6 hv_stores(meta, "scanned", po_u64_to_sv(tallied));
901 6 hv_stores(meta, "files", newSViv(files));
902 6 hv_stores(meta, "skipped", newSViv(skipped));
903 6 hv_stores(meta, "degraded", newSViv(degraded));
904 6 hv_stores(meta, "truncated", newSViv(stopped ? 1 : 0));
905 6 ▶ mXPUSHs(newRV_noinc((SV *)seen));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)seen)); 0 6 6
906 6 ▶ mXPUSHs(newRV_noinc((SV *)meta));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)meta)); 0 6 6
907 }
908  
909 # The service graph, merged across every segment.
910 #
911 # A sealed segment answers from its sidecar - a read of a few hundred bytes
912 # ONE SPAN SET ACROSS EVERY FILE, ANALYSED ONCE.
913 #
914 # The obvious optimisation is to answer a sealed segment from its sidecar - a
915 # read of a few hundred bytes rather than a scan of its spans, which is the
916 # whole reason a summary is written at seal. It is also WRONG, and wrong in a
917 # way that looks like working:
918 #
919 # a graph derived from one file can only see the parents that are IN that
920 # file. A trace whose caller and callee were received by different workers, or
921 # landed either side of a seal, has each half summarised on its own - and a
922 # span whose parent is not present is attributed to the SYNTHETIC ROOT. The
923 # service map then draws every service as if traffic arrived at it from
924 # outside, which is exactly the topology it exists to disprove. In the demo
925 # that turned ninety-six `shop -> cards` calls into six.
926 #
927 # So the spans are gathered from every file first and the graph is built from
928 # all of them together. That is a scan, and the map is the screen that can
929 # afford one: it is looked at occasionally and it has to be right.
930 void
931 post_graph(SV *self, ...)
932 PPCODE:
933 {
934 char dpbuf[PO_PATHMAX];
935 11 const char *dp = dpbuf;
936 11 po_u64 from = 0, to = PO_U64_MAX;
937 11 int have_from = 0, have_to = 0;
938 IV ai;
939 11 HV *edges = newHV(), *svc = newHV();
940 po_span_gather all;
941 SSize_t i, n;
942 11 IV gfiles = 0, gskipped = 0;
943 11 HV *out = newHV();
944  
945 25 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 14 11 25
946 14 const char *k = SvPV_nolen(ST(ai));
947 14 ▶ if (strEQ(k, "from")) have_from = po_sv_to_u64(aTHX_ ST(ai+1), &from);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "from")) have_from = po_sv_to_u64(aTHX_ ST(ai+1), &from); 7 7 14
948 7 ▶ else if (strEQ(k, "to")) have_to = po_sv_to_u64(aTHX_ ST(ai+1), &to);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "to")) have_to = po_sv_to_u64(aTHX_ ST(ai+1), &to); 7 0 7
949 }
950  
951 11 ▶ if (!po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) { 0 11 11
952 0 hv_stores(out, "edges", newRV_noinc((SV *)newAV()));
953 0 hv_stores(out, "services", newRV_noinc((SV *)svc));
954 0 SvREFCNT_dec((SV *)edges);
955 0 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 0 0
956 0 XSRETURN(1);
957 }
958  
959 11 ▶ if (!po_gather_init(&all)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_gather_init(&all)) { 0 11 11
960 0 hv_stores(out, "edges", newRV_noinc((SV *)newAV()));
961 0 hv_stores(out, "services", newRV_noinc((SV *)svc));
962 0 SvREFCNT_dec((SV *)edges);
963 0 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 0 0
964 0 XSRETURN(1);
965 }
966  
967 { /* The snapshot drives both halves of this walk.
968 *
969 * THE SERVICE COUNTS ARE PER RECORD, not per span - a log line
970 * counts too - so they never come from the span set. A sealed
971 * file has them tallied in its snapshot entry (parsed from its
972 * sidecar once, ever); a live one is walked for them. And they
973 * are merged for EVERY segment, windowed or not, as they always
974 * were: the services table is the store's, the window is the
975 * spans'.
976 *
977 * The span gather is the expensive half and the window prunes
978 * it: a sealed segment outside [from,to], or one whose sidecar
979 * proves it holds no spans, is never slurped. Every span INSIDE
980 * the window still lands in one set - see the invariant above -
981 * because pruning picks files, never how spans merge. */
982 11 HV *snap = po_snap_get(aTHX_ self, dp);
983 11 AV *segs = NULL, *wals = NULL;
984 11 ▶ if (snap) {
Branch: 1 branch
BranchTrueFalseTotal
if (snap) { 11 0 11
985 11 SV **f = hv_fetchs(snap, "segs", 0);
986 11 ▶ if (f && SvROK(*f)) segs = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 11 0 11
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 11 0 11
987 11 f = hv_fetchs(snap, "wals", 0);
988 11 ▶ if (f && SvROK(*f)) wals = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 11 0 11
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 11 0 11
989 }
990  
991 11 ▶ n = segs ? av_len(segs) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = segs ? av_len(segs) + 1 : 0; 11 0 11
992 25 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 14 11 25
993 14 SV **e = av_fetch(segs, i, 0);
994 AV *ea;
995 SV **f, **nm;
996 char path[PO_PATHMAX];
997 14 size_t blen = 0;
998 char *bytes;
999  
1000 17 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 14 0 14
if (!e || !SvROK(*e)) continue; 0 14 14
1001 14 ea = (AV *)SvRV(*e);
1002  
1003 14 f = av_fetch(ea, PO_SNAP_SVC, 0);
1004 14 ▶ if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 14 0 14
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 14 0 14
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 14 0 14
1005 14 HV *sh = (HV *)SvRV(*f);
1006 HE *he;
1007 14 hv_iterinit(sh);
1008 35 ▶ while ((he = hv_iternext(sh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(sh))) { 21 14 35
1009 STRLEN kl;
1010 21 ▶ const char *kp = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *kp = HePV(he, kl); 0 21 21
1011 21 SV **slot = hv_fetch(svc, kp, (I32)kl, 1);
1012 21 ▶ if (slot)
Branch: 1 branch
BranchTrueFalseTotal
if (slot) 21 0 21
1013 21 ▶ sv_setiv(*slot,
Branch: 1 branch
BranchTrueFalseTotal
sv_setiv(*slot, 0 21 21
1014 (SvOK(*slot) ? SvIV(*slot) : 0)
1015 + SvIV(HeVAL(he)));
1016 }
1017 }
1018  
1019 14 ▶ if ((have_from || have_to)
Branch: 2 branches
BranchTrueFalseTotal
if ((have_from || have_to) 4 10 14
if ((have_from || have_to) 0 4 4
1020 10 ▶ && (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN)) { 10 0 10
1021 10 po_u64 ix_min = po_snap_u64(aTHX_ ea, PO_SNAP_TMIN);
1022 10 po_u64 ix_max = po_snap_u64(aTHX_ ea, PO_SNAP_TMAX);
1023 10 ▶ if ((have_from && ix_max < from)
Branch: 2 branches
BranchTrueFalseTotal
if ((have_from && ix_max < from) 10 0 10
if ((have_from && ix_max < from) 8 2 10
1024 10 ▶ || (have_to && ix_min > to)) { gskipped++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
|| (have_to && ix_min > to)) { gskipped++; continue; } 8 0 8
|| (have_to && ix_min > to)) { gskipped++; continue; } 0 8 8
1025 }
1026 12 ▶ if (po_snap_kind_absent(aTHX_ ea, PO_SPAN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_snap_kind_absent(aTHX_ ea, PO_SPAN)) { 1 11 12
1027 1 gskipped++;
1028 1 continue;
1029 }
1030  
1031 11 nm = av_fetch(ea, PO_SNAP_NAME, 0);
1032 11 ▶ if (!nm) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) continue; 0 11 11
1033 22 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*nm)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*nm))) 0 11 11
1034 0 continue;
1035 11 bytes = po_slurp(path, &blen);
1036 11 ▶ if (!bytes) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) continue; 0 11 11
1037 11 gfiles++;
1038 11 po_gather_wal(&all, bytes, blen, have_from, from, have_to, to);
1039 11 free(bytes);
1040 }
1041  
1042 11 ▶ n = wals ? av_len(wals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = wals ? av_len(wals) + 1 : 0; 11 0 11
1043 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 11 12
1044 1 SV **e = av_fetch(wals, i, 0);
1045 char path[PO_PATHMAX];
1046 1 size_t blen = 0;
1047 char *bytes;
1048 po_idx_nums nums;
1049 1 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 1 1
1050 2 ▶ if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dp, SvPV_nolen(*e))) 0 1 1
1051 0 continue;
1052 1 (void)po_wal_nums(path, &nums, svc);
1053 1 bytes = po_slurp(path, &blen);
1054 1 ▶ if (!bytes) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) continue; 0 1 1
1055 1 gfiles++;
1056 1 po_gather_wal(&all, bytes, blen, have_from, from, have_to, to);
1057 1 free(bytes);
1058 }
1059 }
1060  
1061 { /* The graph, built once over everything that was gathered. */
1062 po_sgraph sg;
1063 11 po_span_w_seal(&all.w);
1064 11 ▶ if (all.w.n && po_sgraph_init(&sg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (all.w.n && po_sgraph_init(&sg)) { 10 1 11
if (all.w.n && po_sgraph_init(&sg)) { 10 0 10
1065 10 ▶ if (po_sgraph_build(&sg, all.w.s, (uint32_t)all.w.n))
Branch: 1 branch
BranchTrueFalseTotal
if (po_sgraph_build(&sg, all.w.s, (uint32_t)all.w.n)) 10 0 10
1066 10 po_graph_merge_live(aTHX_ &sg, &all, edges, NULL);
1067 10 po_sgraph_free(&sg);
1068 }
1069 11 po_gather_free(&all);
1070 }
1071  
1072 { /* Sorted, so two calls against the same store agree. */
1073 11 AV *out_edges = newAV();
1074 11 AV *keys = newAV();
1075 HE *he;
1076 SSize_t j, m;
1077 11 hv_iterinit(edges);
1078 30 ▶ while ((he = hv_iternext(edges)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(edges))) 19 11 30
1079 19 av_push(keys, newSVsv(hv_iterkeysv(he)));
1080 11 sortsv(AvARRAY(keys), (SSize_t)(av_len(keys) + 1), Perl_sv_cmp);
1081 11 m = av_len(keys) + 1;
1082 30 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 19 11 30
1083 19 SV **k = av_fetch(keys, j, 0);
1084 STRLEN kl;
1085 const char *kp;
1086 SV **v;
1087 19 ▶ if (!k) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) continue; 0 19 19
1088 19 kp = SvPV(*k, kl);
1089 19 v = hv_fetch(edges, kp, (I32)kl, 0);
1090 19 ▶ if (v) av_push(out_edges, newSVsv(*v));
Branch: 1 branch
BranchTrueFalseTotal
if (v) av_push(out_edges, newSVsv(*v)); 19 0 19
1091 }
1092 11 SvREFCNT_dec((SV *)keys);
1093 11 hv_stores(out, "edges", newRV_noinc((SV *)out_edges));
1094 }
1095 11 hv_stores(out, "services", newRV_noinc((SV *)svc));
1096 11 hv_stores(out, "files", newSViv(gfiles));
1097 11 hv_stores(out, "skipped", newSViv(gskipped));
1098 11 SvREFCNT_dec((SV *)edges);
1099 11 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 11 11
1100 }
1101  
1102 # The trace summaries in a window, slowest first. This is the SEARCH; one
1103 # trace is assembled by trace_dir below.
1104 void
1105 post_traces(SV *self, ...)
1106 PPCODE:
1107 {
1108 po_span_gather g;
1109 po_tsum_set sums;
1110 char dpbuf[PO_PATHMAX];
1111 37 po_u64 from = 0, to = PO_U64_MAX, min_dur = 0, max_dur = 0;
1112 37 po_u64 after_dur = 0, after_hi = 0, after_lo = 0;
1113 37 int have_from = 0, have_to = 0, errors_only = 0, have_max = 0;
1114 37 int have_after = 0;
1115 37 IV limit = 50, ai;
1116 37 const char *want_svc = NULL;
1117 37 STRLEN want_len = 0;
1118 37 const char *want_match = NULL;
1119 37 STRLEN match_len = 0;
1120 37 AV *out = newAV();
1121 37 HV *res = newHV();
1122 uint32_t k;
1123 37 IV kept = 0;
1124 37 IV gfiles = 0, gskipped = 0;
1125  
1126 279 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 242 37 279
1127 242 const char *k = SvPV_nolen(ST(ai));
1128 242 SV *v = ST(ai + 1);
1129 242 ▶ if (strEQ(k, "from")) have_from = po_sv_to_u64(aTHX_ v, &from);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "from")) have_from = po_sv_to_u64(aTHX_ v, &from); 32 210 242
1130 210 ▶ else if (strEQ(k, "to")) have_to = po_sv_to_u64(aTHX_ v, &to);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "to")) have_to = po_sv_to_u64(aTHX_ v, &to); 32 178 210
1131 178 ▶ else if (strEQ(k, "min_duration")) (void)po_sv_to_u64(aTHX_ v, &min_dur);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "min_duration")) (void)po_sv_to_u64(aTHX_ v, &min_dur); 27 151 178
1132 /* The other end of the same column. A zero MAXIMUM is a real
1133 * bound - it selects the instantaneous traces - so it cannot be
1134 * its own "unset", the way a zero minimum can. */
1135 151 ▶ else if (strEQ(k, "max_duration") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "max_duration") && SvOK(v)) 5 146 151
else if (strEQ(k, "max_duration") && SvOK(v)) 5 0 5
1136 5 have_max = po_sv_to_u64(aTHX_ v, &max_dur);
1137 /* THE PAGE CURSOR: the (duration, trace) triple of the last row
1138 * shown. by_dur's tie-break on the trace id is what makes this a
1139 * total order, which is what makes "the next fifty" a fixed set
1140 * rather than one that shifts when two traces share a duration. */
1141 146 ▶ else if (strEQ(k, "after_dur") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "after_dur") && SvOK(v)) 6 140 146
else if (strEQ(k, "after_dur") && SvOK(v)) 6 0 6
1142 6 have_after = po_sv_to_u64(aTHX_ v, &after_dur);
1143 140 ▶ else if (strEQ(k, "after_hi") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "after_hi") && SvOK(v)) 6 134 140
else if (strEQ(k, "after_hi") && SvOK(v)) 6 0 6
1144 6 (void)po_sv_to_u64(aTHX_ v, &after_hi);
1145 134 ▶ else if (strEQ(k, "after_lo") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "after_lo") && SvOK(v)) 6 128 134
else if (strEQ(k, "after_lo") && SvOK(v)) 6 0 6
1146 6 (void)po_sv_to_u64(aTHX_ v, &after_lo);
1147 128 ▶ else if (strEQ(k, "errors_only")) errors_only = SvTRUE(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "errors_only")) errors_only = SvTRUE(v); 32 96 128
1148 96 ▶ else if (strEQ(k, "limit")) limit = SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "limit")) limit = SvIV(v); 34 62 96
1149 62 ▶ else if (strEQ(k, "service") && SvOK(v)) want_svc = SvPV(v, want_len);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "service") && SvOK(v)) want_svc = SvPV(v, want_len); 31 31 62
else if (strEQ(k, "service") && SvOK(v)) want_svc = SvPV(v, want_len); 0 31 31
1150 62 ▶ else if (strEQ(k, "match") && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "match") && SvOK(v)) { 31 31 62
else if (strEQ(k, "match") && SvOK(v)) { 5 26 31
1151 5 want_match = SvPV(v, match_len);
1152 5 ▶ if (!match_len) want_match = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!match_len) want_match = NULL; 0 5 5
1153 }
1154 }
1155 37 ▶ if (limit <= 0) limit = 50;
Branch: 1 branch
BranchTrueFalseTotal
if (limit <= 0) limit = 50; 0 37 37
1156  
1157 37 ▶ if (!po_gather_init(&g)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_gather_init(&g)) croak("oom"); 0 37 37
1158 37 ▶ if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf)))
Branch: 1 branch
BranchTrueFalseTotal
if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) 37 0 37
1159 37 po_gather_dir(aTHX_ self, dpbuf, &g, have_from, from, have_to, to,
1160 &gfiles, &gskipped);
1161 37 po_span_w_seal(&g.w);
1162  
1163 37 hv_stores(res, "files", newSViv(gfiles));
1164 37 hv_stores(res, "skipped", newSViv(gskipped));
1165 37 ▶ if (!g.w.n || !po_tsum_init(&sums)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!g.w.n || !po_tsum_init(&sums)) { 36 1 37
if (!g.w.n || !po_tsum_init(&sums)) { 0 36 36
1166 1 po_gather_free(&g);
1167 1 hv_stores(res, "traces", newRV_noinc((SV *)out));
1168 1 hv_stores(res, "spans", newSViv(0));
1169 1 hv_stores(res, "total", newSViv(0));
1170 1 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 1 1
1171 1 XSRETURN(1);
1172 }
1173 36 ▶ if (!po_tsum_build(&sums, g.w.s, (uint32_t)g.w.n) || !po_tsum_index(&sums)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!po_tsum_build(&sums, g.w.s, (uint32_t)g.w.n) || !po_tsum_index(&sums)) { 36 0 36
if (!po_tsum_build(&sums, g.w.s, (uint32_t)g.w.n) || !po_tsum_index(&sums)) { 0 36 36
1174 0 po_tsum_free(&sums); po_gather_free(&g);
1175 0 croak("summaries");
1176 }
1177  
1178 /* WHICH FIFTY, and the answer is not "the first fifty".
1179 *
1180 * by_dur is ordinals into t sorted FASTEST first - the binary search
1181 * beside it depends on that - and this loop used to walk it from
1182 * index 0 while a comment here claimed the opposite. So the traces
1183 * screen showed the fifty quickest requests in the window, and
1184 * "slower than 100ms" with more than fifty matches showed the fifty
1185 * quickest of those: the right rows by the filter and the wrong end
1186 * of them, on the one screen whose whole purpose is finding the slow
1187 * ones.
1188 *
1189 * A maximum is the exception and walks forwards, because somebody who
1190 * asked for the fast ones wants the fast ones. */
1191 952 ▶ for (k = 0; k < sums.n && kept < limit; k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < sums.n && kept < limit; k++) { 924 28 952
for (k = 0; k < sums.n && kept < limit; k++) { 916 8 924
1192 916 const po_tsummary *t =
1193 916 ▶ &sums.t[sums.by_dur[have_max ? k : sums.n - 1 - k]];
Branch: 1 branch
BranchTrueFalseTotal
&sums.t[sums.by_dur[have_max ? k : sums.n - 1 - k]]; 274 642 916
1194 HV *h;
1195  
1196 916 ▶ if (min_dur && t->dur_ns < min_dur) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (min_dur && t->dur_ns < min_dur) continue; 5 911 916
if (min_dur && t->dur_ns < min_dur) continue; 5 0 5
1197 911 ▶ if (have_max && t->dur_ns > max_dur) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_max && t->dur_ns > max_dur) continue; 274 637 911
if (have_max && t->dur_ns > max_dur) continue; 2 272 274
1198  
1199 /* Skip everything at or before the cursor IN WALK ORDER. The
1200 * default walk is slowest first, so "past the cursor" means
1201 * strictly faster, or the same duration with a smaller trace id;
1202 * a max_duration walk runs the other way and mirrors. Positional
1203 * rather than find-the-exact-entry, so a cursor whose trace has
1204 * aged out of the window still lands on the right next page. */
1205 909 ▶ if (have_after) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_after) { 660 249 909
1206 int past;
1207 660 ▶ if (t->dur_ns != after_dur)
Branch: 1 branch
BranchTrueFalseTotal
if (t->dur_ns != after_dur) 436 224 660
1208 436 past = have_max ? (t->dur_ns > after_dur)
1209 436 ▶ : (t->dur_ns < after_dur);
Branch: 1 branch
BranchTrueFalseTotal
: (t->dur_ns < after_dur); 218 218 436
1210 224 ▶ else if (t->trace_hi != after_hi)
Branch: 1 branch
BranchTrueFalseTotal
else if (t->trace_hi != after_hi) 0 224 224
1211 0 past = have_max ? (t->trace_hi > after_hi)
1212 0 ▶ : (t->trace_hi < after_hi);
Branch: 1 branch
BranchTrueFalseTotal
: (t->trace_hi < after_hi); 0 0 0
1213 else
1214 224 past = have_max ? (t->trace_lo > after_lo)
1215 224 ▶ : (t->trace_lo < after_lo);
Branch: 1 branch
BranchTrueFalseTotal
: (t->trace_lo < after_lo); 2 222 224
1216 660 ▶ if (!past) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!past) continue; 450 210 660
1217 }
1218 459 ▶ if (errors_only && !t->errors) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (errors_only && !t->errors) continue; 3 456 459
if (errors_only && !t->errors) continue; 1 2 3
1219 458 ▶ if (want_svc) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_svc) { 0 458 458
1220 0 uint32_t sl = 0;
1221 0 const char *sp = po_intern_get(&g.sym, t->root_service_sym, &sl);
1222 0 ▶ if (!sp || sl != (uint32_t)want_len
Branch: 2 branches
BranchTrueFalseTotal
if (!sp || sl != (uint32_t)want_len 0 0 0
if (!sp || sl != (uint32_t)want_len 0 0 0
1223 0 ▶ || memcmp(sp, want_svc, want_len) != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(sp, want_svc, want_len) != 0) continue; 0 0 0
1224 }
1225 /* A SUBSTRING OF WHAT THE TRACE IS: its root span's name, or the
1226 * service that served it. "POST" finds every POST because the
1227 * span name is the method and the route pattern; "checkout"
1228 * finds the route; "cards" finds the service.
1229 *
1230 * Case-insensitive, because a search box is typed into rather
1231 * than copied from, and nobody types the method in capitals
1232 * twice. */
1233 458 ▶ if (want_match) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_match) { 10 448 458
1234 10 uint32_t nl2 = 0, sl2 = 0;
1235 10 const char *np2 = po_intern_get(&g.sym, t->root_name_sym, &nl2);
1236 10 const char *sp2 = po_intern_get(&g.sym, t->root_service_sym, &sl2);
1237 10 ▶ if (!po_substr_i(np2, nl2, want_match, match_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!po_substr_i(np2, nl2, want_match, match_len) 4 6 10
1238 4 ▶ && !po_substr_i(sp2, sl2, want_match, match_len)) continue;
Branch: 1 branch
BranchTrueFalseTotal
&& !po_substr_i(sp2, sl2, want_match, match_len)) continue; 2 2 4
1239 }
1240  
1241 456 h = newHV();
1242 456 hv_stores(h, "trace_hi", po_u64_to_sv(t->trace_hi));
1243 456 hv_stores(h, "trace_lo", po_u64_to_sv(t->trace_lo));
1244 456 hv_stores(h, "duration", po_u64_to_sv(t->dur_ns));
1245 /* WHEN it happened, not only how long it took. A table can rank
1246 * traces by duration without this; a chart cannot place one
1247 * without it, and where the slow ones CLUSTER is the thing a
1248 * table of the fifty slowest cannot show. */
1249 456 hv_stores(h, "t", po_u64_to_sv(t->start_ns));
1250 456 hv_stores(h, "spans", newSVuv((UV)t->spans));
1251 456 hv_stores(h, "errors", newSVuv((UV)t->errors));
1252 456 hv_stores(h, "service", po_sym_sv(aTHX_ &g.sym, t->root_service_sym));
1253 456 hv_stores(h, "name", po_sym_sv(aTHX_ &g.sym, t->root_name_sym));
1254 456 av_push(out, newRV_noinc((SV *)h));
1255 456 kept++;
1256 }
1257  
1258 36 hv_stores(res, "traces", newRV_noinc((SV *)out));
1259 36 hv_stores(res, "spans", newSVuv((UV)g.w.n));
1260 36 hv_stores(res, "total", newSVuv((UV)sums.n));
1261 36 po_tsum_free(&sums);
1262 36 po_gather_free(&g);
1263 36 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 36 36
1264 }
1265  
1266 # One trace, assembled: every span in TREE ORDER with its depth and its offset
1267 # from the trace's start, which is what a waterfall needs and what the
1268 # executor cannot produce, because a tree is not a row shape.
1269 void
1270 post_trace(SV *self, SV *hi_sv, SV *lo_sv, ...)
1271 PPCODE:
1272 {
1273 po_span_gather g;
1274 po_tree tr;
1275 char dpbuf[PO_PATHMAX];
1276 10 po_u64 from = 0, to = PO_U64_MAX, hi = 0, lo = 0;
1277 10 int have_from = 0, have_to = 0;
1278 IV ai;
1279 10 AV *out = newAV();
1280 10 HV *res = newHV();
1281 10 uint32_t i, first = 0, count = 0;
1282 10 po_u64 t0 = PO_U64_MAX, dur = 0;
1283 10 IV errors = 0;
1284  
1285 10 (void)po_sv_to_u64(aTHX_ hi_sv, &hi);
1286 10 (void)po_sv_to_u64(aTHX_ lo_sv, &lo);
1287 22 ▶ for (ai = 3; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 3; ai + 1 < items; ai += 2) { 12 10 22
1288 12 const char *k = SvPV_nolen(ST(ai));
1289 12 ▶ if (strEQ(k, "from")) have_from = po_sv_to_u64(aTHX_ ST(ai+1), &from);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "from")) have_from = po_sv_to_u64(aTHX_ ST(ai+1), &from); 6 6 12
1290 6 ▶ else if (strEQ(k, "to")) have_to = po_sv_to_u64(aTHX_ ST(ai+1), &to);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "to")) have_to = po_sv_to_u64(aTHX_ ST(ai+1), &to); 6 0 6
1291 }
1292  
1293 10 ▶ if (!po_gather_init(&g)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_gather_init(&g)) croak("oom"); 0 10 10
1294 10 ▶ if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf)))
Branch: 1 branch
BranchTrueFalseTotal
if (po_store_waldir(aTHX_ self, dpbuf, sizeof(dpbuf))) 10 0 10
1295 10 po_gather_dir(aTHX_ self, dpbuf, &g, have_from, from, have_to, to,
1296 NULL, NULL);
1297 10 po_span_w_seal(&g.w);
1298  
1299 /* The spans of one trace are CONTIGUOUS after the seal, because the
1300 * seal sorts by (trace, start). So this is a scan for the run rather
1301 * than a filter over everything. */
1302 2041 ▶ for (i = 0; i < g.w.n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g.w.n; i++) { 2031 10 2041
1303 2031 ▶ if (g.w.s[i].trace_hi != hi || g.w.s[i].trace_lo != lo) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (g.w.s[i].trace_hi != hi || g.w.s[i].trace_lo != lo) continue; 54 1977 2031
if (g.w.s[i].trace_hi != hi || g.w.s[i].trace_lo != lo) continue; 0 54 54
1304 54 ▶ if (!count) first = i;
Branch: 1 branch
BranchTrueFalseTotal
if (!count) first = i; 8 46 54
1305 54 count++;
1306 }
1307 10 ▶ if (!count) { po_gather_free(&g); XSRETURN_UNDEF; }
Branch: 1 branch
BranchTrueFalseTotal
if (!count) { po_gather_free(&g); XSRETURN_UNDEF; } 2 8 10
1308  
1309 62 ▶ for (i = first; i < first + count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = first; i < first + count; i++) { 54 8 62
1310 54 po_u64 end = g.w.s[i].start_ns + g.w.s[i].dur_ns;
1311 54 ▶ if (g.w.s[i].start_ns < t0) t0 = g.w.s[i].start_ns;
Branch: 1 branch
BranchTrueFalseTotal
if (g.w.s[i].start_ns < t0) t0 = g.w.s[i].start_ns; 8 46 54
1312 54 ▶ if (end - t0 > dur && end > t0) dur = end - t0;
Branch: 2 branches
BranchTrueFalseTotal
if (end - t0 > dur && end > t0) dur = end - t0; 48 6 54
if (end - t0 > dur && end > t0) dur = end - t0; 48 0 48
1313 54 ▶ if (g.w.s[i].status == 2) errors++;
Branch: 1 branch
BranchTrueFalseTotal
if (g.w.s[i].status == 2) errors++; 6 48 54
1314 }
1315  
1316 8 ▶ if (po_tree_build(&tr, g.w.s + first, count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tree_build(&tr, g.w.s + first, count)) { 8 0 8
1317 /* Index order, which after the seal is (trace, start) - so the
1318 * waterfall comes out chronological. `depth` carries the nesting;
1319 * there is no separate ordering array and there does not need to
1320 * be one. */
1321 62 ▶ for (i = 0; i < tr.n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tr.n; i++) { 54 8 62
1322 54 const po_span *sp = &g.w.s[first + i];
1323 54 HV *h = newHV();
1324 54 hv_stores(h, "span_id", po_u64_to_sv(sp->span_id));
1325 54 hv_stores(h, "parent_span_id", po_u64_to_sv(sp->parent_span_id));
1326 54 hv_stores(h, "depth", newSViv((IV)tr.depth[i]));
1327 54 hv_stores(h, "name", po_sym_sv(aTHX_ &g.sym, sp->name_sym));
1328 54 hv_stores(h, "service", po_sym_sv(aTHX_ &g.sym, sp->service_sym));
1329 54 hv_stores(h, "start", po_u64_to_sv(sp->start_ns));
1330 54 hv_stores(h, "offset", po_u64_to_sv(po_ns_sub(sp->start_ns, t0)));
1331 54 hv_stores(h, "duration", po_u64_to_sv(sp->dur_ns));
1332 54 hv_stores(h, "status", newSVuv((UV)sp->status));
1333 54 hv_stores(h, "kind", newSVuv((UV)sp->kind));
1334 54 av_push(out, newRV_noinc((SV *)h));
1335 }
1336 8 hv_stores(res, "roots", newSVuv((UV)tr.roots));
1337 8 hv_stores(res, "cycles", newSVuv((UV)tr.cycles));
1338 8 hv_stores(res, "orphans", newSVuv((UV)tr.orphans));
1339 8 po_tree_free(&tr);
1340 }
1341  
1342 8 hv_stores(res, "trace_hi", po_u64_to_sv(hi));
1343 8 hv_stores(res, "trace_lo", po_u64_to_sv(lo));
1344 8 hv_stores(res, "spans", newRV_noinc((SV *)out));
1345 8 hv_stores(res, "span_count", newSViv((IV)av_len(out) + 1));
1346 8 hv_stores(res, "duration", po_u64_to_sv(dur));
1347 8 ▶ hv_stores(res, "t_min", po_u64_to_sv(t0 == PO_U64_MAX ? 0 : t0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(res, "t_min", po_u64_to_sv(t0 == PO_U64_MAX ? 0 : t0)); 8 0 8
1348 8 hv_stores(res, "errors", newSViv(errors));
1349 8 po_gather_free(&g);
1350 8 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 8 8
1351 }
1352  
1353 # ---- the object's own paths ------------------------------------------------
1354  
1355 # The live log this worker appends to.
1356 #
1357 # PER WORKER AND NEVER LOCKED, which is the decision the whole storage design
1358 # rests on: the hot path takes no lock, and a leader-elected compactor turns
1359 # sealed logs into shared segments later. The pid in the name is what makes
1360 # that safe without one.
1361 SV *
1362 post_wal_path(SV *self)
1363 CODE:
1364 {
1365 char dir[PO_PATHMAX], path[PO_PATHMAX];
1366 char name[64];
1367 int n;
1368 123 ▶ if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_UNDEF; 0 123 123
1369 123 po_mkpath(dir);
1370 123 n = snprintf(name, sizeof(name), "w%ld.wal", (long)po_getpid());
1371 123 ▶ if (n <= 0 || (size_t)n >= sizeof(name)) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || (size_t)n >= sizeof(name)) XSRETURN_UNDEF; 123 0 123
if (n <= 0 || (size_t)n >= sizeof(name)) XSRETURN_UNDEF; 0 123 123
1372 123 ▶ if (!po_path_join(path, sizeof(path), dir, name)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, name)) XSRETURN_UNDEF; 0 123 123
1373 123 RETVAL = newSVpv(path, 0);
1374 }
1375 OUTPUT:
1376 RETVAL
1377  
1378 SV *
1379 post_wal_dir(SV *self)
1380 CODE:
1381 {
1382 char dir[PO_PATHMAX];
1383 28 ▶ if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_UNDEF; 0 28 28
1384 28 RETVAL = newSVpv(dir, 0);
1385 }
1386 OUTPUT:
1387 RETVAL
1388  
1389 # Every file the read side can see, sealed and live.
1390 void
1391 post_segments(SV *self)
1392 PPCODE:
1393 {
1394 char dir[PO_PATHMAX];
1395 SV *arg;
1396 24 ▶ if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) { 0 24 24
1397 0 ▶ mXPUSHs(newRV_noinc((SV *)newAV()));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)newAV())); 0 0 0
1398 0 XSRETURN(1);
1399 }
1400 24 arg = sv_2mortal(newSVpv(dir, 0));
1401 24 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 24 24
1402 24 ▶ XPUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(arg); 0 24 24
1403 24 PUTBACK;
1404 {
1405 24 int n = call_pv("Punk::Observe::Store::scan_dir", G_SCALAR);
1406 24 SPAGAIN;
1407 24 ▶ if (n < 1) { PUSHs(sv_2mortal(newRV_noinc((SV *)newAV()))); }
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) { PUSHs(sv_2mortal(newRV_noinc((SV *)newAV()))); } 0 24 24
1408 }
1409 }
1410  
1411 # Seal the live log and write its summary beside it.
1412 #
1413 # A SEALED LOG IS THE UNIT OF THE READ SIDE. The live one is being appended to
1414 # by the worker that owns it, so a reader gets whatever complete frames exist
1415 # at the moment it looks - correct, but not stable, and not something to build
1416 # an index over. After the rename the file never changes again, so its index
1417 # can never be stale.
1418 void
1419 post_seal(SV *self, ...)
1420 PPCODE:
1421 {
1422 char dir[PO_PATHMAX], live[PO_PATHMAX], seg[PO_PATHMAX], idx[PO_PATHMAX];
1423 char name[96];
1424 91 size_t blen = 0;
1425 char *bytes;
1426 po_wal_replay rp;
1427 HV *h;
1428 SV **f;
1429 91 IV counter = 0;
1430 int n;
1431 91 long owner_pid = (long)po_getpid();
1432  
1433 91 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_UNDEF; 91 0 91
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_UNDEF; 0 91 91
1434 91 h = (HV *)SvRV(self);
1435 91 ▶ if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_UNDEF; 0 91 91
1436  
1437 /* ADOPTING ANOTHER WORKER'S LOG, when one is named.
1438 *
1439 * A worker seals its own log and owns the descriptor while it does,
1440 * which is what makes the rename safe. A log left behind by a worker
1441 * that DIED has no such owner, and without this it is never sealed,
1442 * never indexed and never expired - so its bytes sit on the disk for
1443 * ever while retention reports success. The caller is responsible for
1444 * proving the owner is gone; see Punk::Observe::Retain::adopt_orphans,
1445 * which is the only caller that passes a path.
1446 *
1447 * The ORIGINAL worker's pid stays in the segment name. It is only
1448 * needed for uniqueness, but a segment named for the process that
1449 * wrote it is a segment somebody can trace back. */
1450 92 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 1 90 91
if (items > 1 && SvOK(ST(1))) { 1 0 1
1451 1 STRLEN pl = 0;
1452 1 const char *pp = SvPV(ST(1), pl);
1453 const char *slash;
1454 1 ▶ if (pl >= sizeof(live)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (pl >= sizeof(live)) XSRETURN_UNDEF; 0 1 1
1455 1 memcpy(live, pp, pl);
1456 1 live[pl] = '\0';
1457 1 slash = strrchr(live, '/');
1458 {
1459 1 ▶ const char *base = slash ? slash + 1 : live;
Branch: 1 branch
BranchTrueFalseTotal
const char *base = slash ? slash + 1 : live; 1 0 1
1460 1 ▶ if (*base == 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if (*base == 'w') { 1 0 1
1461 1 long got = atol(base + 1);
1462 1 ▶ if (got > 0) owner_pid = got;
Branch: 1 branch
BranchTrueFalseTotal
if (got > 0) owner_pid = got; 1 0 1
1463 }
1464 }
1465 }
1466 else {
1467 char wn[64];
1468 90 int wl = snprintf(wn, sizeof(wn), "w%ld.wal", (long)po_getpid());
1469 90 ▶ if (wl <= 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (wl <= 0) XSRETURN_UNDEF; 0 90 90
1470 90 ▶ if (!po_path_join(live, sizeof(live), dir, wn)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(live, sizeof(live), dir, wn)) XSRETURN_UNDEF; 0 90 90
1471 }
1472  
1473 91 bytes = po_slurp(live, &blen);
1474 91 ▶ if (!bytes || !blen) { free(bytes); XSRETURN_UNDEF; }
Branch: 2 branches
BranchTrueFalseTotal
if (!bytes || !blen) { free(bytes); XSRETURN_UNDEF; } 91 0 91
if (!bytes || !blen) { free(bytes); XSRETURN_UNDEF; } 0 91 91
1475  
1476 91 po_wal_replay_buf(bytes, blen, &rp, NULL, NULL);
1477 91 ▶ if (!rp.records) { free(bytes); XSRETURN_UNDEF; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rp.records) { free(bytes); XSRETURN_UNDEF; } 0 91 91
1478  
1479 /* The trailer goes on the file, not on the copy in hand: the summary
1480 * below is computed from the bytes as they were, which is the same
1481 * set of records the trailer is about. */
1482 {
1483 SV *args[2];
1484 91 dSP;
1485 91 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 91 91
1486 91 args[0] = sv_2mortal(newSVpv(live, 0));
1487 91 args[1] = sv_2mortal(po_u64_to_sv(rp.records));
1488 91 ▶ XPUSHs(args[0]); XPUSHs(args[1]);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(args[0]); XPUSHs(args[1]); 0 91 91
XPUSHs(args[0]); XPUSHs(args[1]); 0 91 91
1489 91 PUTBACK;
1490 91 call_pv("Punk::Observe::WAL::seal", G_SCALAR | G_DISCARD);
1491 91 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 91 0 91
1492 }
1493  
1494 /* The name carries the worker and the sealing time, so two workers
1495 * sealing in the same second still land on different files and the
1496 * read side never has to break a tie. */
1497 91 f = hv_fetchs(h, "sealed", 0);
1498 91 ▶ if (f && SvOK(*f)) counter = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) counter = SvIV(*f); 91 0 91
if (f && SvOK(*f)) counter = SvIV(*f); 91 0 91
1499 91 n = snprintf(name, sizeof(name), "s%ld-%ld-%ld",
1500 91 (long)time(NULL), owner_pid, (long)counter);
1501 91 ▶ if (n <= 0 || (size_t)n + 5 >= sizeof(name)) { free(bytes); XSRETURN_UNDEF; }
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || (size_t)n + 5 >= sizeof(name)) { free(bytes); XSRETURN_UNDEF; } 91 0 91
if (n <= 0 || (size_t)n + 5 >= sizeof(name)) { free(bytes); XSRETURN_UNDEF; } 0 91 91
1502 91 hv_stores(h, "sealed", newSViv(counter + 1));
1503  
1504 {
1505 char base[96];
1506 91 memcpy(base, name, (size_t)n + 1);
1507 91 memcpy(base + n, ".seg", 5);
1508 91 ▶ if (!po_path_join(seg, sizeof(seg), dir, base)) { free(bytes); XSRETURN_UNDEF; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(seg, sizeof(seg), dir, base)) { free(bytes); XSRETURN_UNDEF; } 0 91 91
1509 91 memcpy(base + n, ".idx", 5);
1510 91 ▶ if (!po_path_join(idx, sizeof(idx), dir, base)) { free(bytes); XSRETURN_UNDEF; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(idx, sizeof(idx), dir, base)) { free(bytes); XSRETURN_UNDEF; } 0 91 91
1511 }
1512  
1513 91 ▶ if (!po_seal_rename(live, seg)) { free(bytes); XSRETURN_UNDEF; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_seal_rename(live, seg)) { free(bytes); XSRETURN_UNDEF; } 0 91 91
1514  
1515 { /* the summary, from the bytes as they were before the trailer */
1516 SV *sum;
1517 91 dSP;
1518 91 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 91 91
1519 91 ▶ XPUSHs(sv_2mortal(newSVpvn(bytes, blen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(bytes, blen))); 0 91 91
1520 91 PUTBACK;
1521 91 ▶ if (call_pv("Punk::Observe::Store::summarise", G_SCALAR) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (call_pv("Punk::Observe::Store::summarise", G_SCALAR) == 1) { 91 0 91
1522 91 SPAGAIN;
1523 91 sum = POPs;
1524 91 ▶ if (SvROK(sum) && SvTYPE(SvRV(sum)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sum) && SvTYPE(SvRV(sum)) == SVt_PVHV) 91 0 91
if (SvROK(sum) && SvTYPE(SvRV(sum)) == SVt_PVHV) 91 0 91
1525 91 (void)po_index_write(aTHX_ idx, (HV *)SvRV(sum));
1526 91 PUTBACK;
1527 }
1528 91 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 91 0 91
1529 }
1530 91 free(bytes);
1531  
1532 91 hv_stores(h, "wal_bytes", newSViv(0));
1533 91 SPAGAIN;
1534 91 ▶ mXPUSHs(newSVpv(seg, 0));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpv(seg, 0)); 0 91 91
1535 }
1536  
1537 # Records from every segment whose time span the range allows.
1538 #
1539 # A SEALED SEGMENT IS SKIPPED FROM ITS SIDECAR, so a query over the last five
1540 # minutes does not open a file holding yesterday. A live log has no sidecar -
1541 # it is still being written - so it is always read; that is bounded by the
1542 # seal threshold rather than by hope.
1543 #
1544 # The cross-segment merge is here too. Each segment comes back sorted, so what
1545 # used to be a Perl sort over everything is a merge of sorted runs, and the
1546 # comparison is on integers rather than on decimal strings.
1547 void
1548 post_records(SV *self, ...)
1549 PPCODE:
1550 {
1551 26 AV *out = newAV();
1552 26 HV *meta = newHV();
1553 26 po_u64 from = 0, to = PO_U64_MAX, limit = 0, trace_hi = 0, trace_lo = 0;
1554 26 int have_from = 0, have_to = 0, kind = 0, as_rows = 0, have_trace = 0;
1555 IV ai;
1556  
1557 50 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 24 26 50
1558 24 const char *k = SvPV_nolen(ST(ai));
1559 24 SV *v = ST(ai + 1);
1560 24 ▶ if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from);
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from); 6 18 24
if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from); 6 0 6
1561 18 ▶ else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to); 4 14 18
else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to); 4 0 4
1562 14 ▶ else if (strEQ(k, "kind") && SvOK(v)) kind = (int)SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "kind") && SvOK(v)) kind = (int)SvIV(v); 5 9 14
else if (strEQ(k, "kind") && SvOK(v)) kind = (int)SvIV(v); 5 0 5
1563 9 ▶ else if (strEQ(k, "limit")&& SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "limit")&& SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit); 9 0 9
else if (strEQ(k, "limit")&& SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit); 9 0 9
1564 0 ▶ else if (strEQ(k, "rows")) as_rows = SvTRUE(v);
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "rows")) as_rows = SvTRUE(v); 0 0 0
1565 0 ▶ else if (strEQ(k, "trace_hi") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "trace_hi") && SvOK(v)) 0 0 0
else if (strEQ(k, "trace_hi") && SvOK(v)) 0 0 0
1566 0 have_trace |= po_sv_to_u64(aTHX_ v, &trace_hi);
1567 0 ▶ else if (strEQ(k, "trace_lo") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "trace_lo") && SvOK(v)) 0 0 0
else if (strEQ(k, "trace_lo") && SvOK(v)) 0 0 0
1568 0 have_trace |= po_sv_to_u64(aTHX_ v, &trace_lo);
1569 }
1570 26 ▶ if (!limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!limit) { 17 9 26
1571 17 SV **f = hv_fetchs((HV *)SvRV(self), "max_rows", 0);
1572 17 ▶ limit = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000;
Branch: 2 branches
BranchTrueFalseTotal
limit = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000; 17 0 17
limit = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000; 17 0 17
1573 }
1574  
1575 26 po_records_run(aTHX_ self, from, have_from, to, have_to,
1576 kind, limit, as_rows, trace_hi, trace_lo, have_trace,
1577 NULL,
1578 NULL, 0, NULL, 0, out, meta);
1579 26 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 26 26
1580 26 ▶ mXPUSHs(newRV_noinc((SV *)meta));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)meta)); 0 26 26
1581 }
1582  
1583 # One record in the shape the executor reads.
1584 #
1585 # `records` and `rows` build this during the scan and never call here; this is
1586 # for a caller that has a record already - the record view of a single row,
1587 # say - and wants the other shape without going back to the log.
1588 #
1589 # The two differ in exactly two places and both are load-bearing: the kind is
1590 # a name rather than a number, and `service` is lifted out of the attributes
1591 # because every query filters on it and no query should have to know it lives
1592 # there.
1593 SV *
1594 post_row(SV *class, SV *rec)
1595 CODE:
1596 {
1597 HV *in, *out;
1598 SV **f;
1599 0 const char *kind = "log";
1600 static const char *KEYS[] = {
1601 "t", "duration", "severity", "status", "value",
1602 "trace_hi", "trace_lo", "span_id", "parent_id", "span_kind"
1603 };
1604 int i;
1605  
1606 PERL_UNUSED_VAR(class);
1607 0 ▶ if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) 0 0 0
if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) 0 0 0
1608 0 croak("Punk::Observe::Store::row: expected a record hashref");
1609 0 in = (HV *)SvRV(rec);
1610 0 out = newHV();
1611  
1612 0 ▶ if ((f = hv_fetchs(in, "kind", 0)) && SvOK(*f)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(in, "kind", 0)) && SvOK(*f)) { 0 0 0
if ((f = hv_fetchs(in, "kind", 0)) && SvOK(*f)) { 0 0 0
1613 0 IV k = SvIV(*f);
1614 0 ▶ kind = (k == PO_METRIC) ? "metric" : (k == PO_SPAN) ? "span" : "log";
Branch: 2 branches
BranchTrueFalseTotal
kind = (k == PO_METRIC) ? "metric" : (k == PO_SPAN) ? "span" : "log"; 0 0 0
kind = (k == PO_METRIC) ? "metric" : (k == PO_SPAN) ? "span" : "log"; 0 0 0
1615 }
1616 0 hv_stores(out, "kind", newSVpv(kind, 0));
1617  
1618 0 ▶ for (i = 0; i < 10; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 10; i++) { 0 0 0
1619 0 f = hv_fetch(in, KEYS[i], (I32)strlen(KEYS[i]), 0);
1620 0 ▶ hv_store(out, KEYS[i], (I32)strlen(KEYS[i]),
Branch: 1 branch
BranchTrueFalseTotal
hv_store(out, KEYS[i], (I32)strlen(KEYS[i]), 0 0 0
1621 f ? newSVsv(*f) : newSV(0), 0);
1622 }
1623  
1624 0 f = hv_fetchs(in, "body", 0);
1625 0 ▶ hv_stores(out, "body", (f && SvOK(*f)) ? newSVsv(*f) : newSVpvs(""));
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(out, "body", (f && SvOK(*f)) ? newSVsv(*f) : newSVpvs("")); 0 0 0
hv_stores(out, "body", (f && SvOK(*f)) ? newSVsv(*f) : newSVpvs("")); 0 0 0
1626  
1627 0 f = hv_fetchs(in, "attrs", 0);
1628 0 ▶ if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 0 0 0
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 0 0 0
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 0 0 0
1629 0 HV *at = (HV *)SvRV(*f);
1630 0 SV **svc = hv_fetchs(at, "service.name", 0);
1631 0 hv_stores(out, "attrs", newSVsv(*f));
1632 0 ▶ hv_stores(out, "service",
Branch: 3 branches
BranchTrueFalseTotal
hv_stores(out, "service", 0 0 0
hv_stores(out, "service", 0 0 0
hv_stores(out, "service", 0 0 0
1633 (svc && SvOK(*svc) && SvCUR(*svc))
1634 ? newSVsv(*svc) : newSVpvs("unknown"));
1635 }
1636 else {
1637 0 hv_stores(out, "attrs", newRV_noinc((SV *)newHV()));
1638 0 hv_stores(out, "service", newSVpvs("unknown"));
1639 }
1640  
1641 0 RETVAL = newRV_noinc((SV *)out);
1642 }
1643 OUTPUT:
1644 RETVAL
1645  
1646 # ---- the object -----------------------------------------------------------
1647  
1648 SV *
1649 post_new(SV *class, ...)
1650 CODE:
1651 {
1652 87 HV *self = newHV();
1653 IV ai;
1654 87 const char *tenant = "default";
1655 87 STRLEN tlen = 7;
1656 87 po_u64 seal_bytes = 8 * 1024 * 1024, max_rows = 500000;
1657 87 SV *dir = NULL;
1658 87 SV *cache = NULL;
1659  
1660 220 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 133 87 220
1661 133 const char *k = SvPV_nolen(ST(ai));
1662 133 SV *v = ST(ai + 1);
1663 133 ▶ if (strEQ(k, "dir")) dir = v;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "dir")) dir = v; 87 46 133
1664 46 ▶ else if (strEQ(k, "tenant") && SvOK(v) && SvCUR(v))
Branch: 3 branches
BranchTrueFalseTotal
else if (strEQ(k, "tenant") && SvOK(v) && SvCUR(v)) 26 20 46
else if (strEQ(k, "tenant") && SvOK(v) && SvCUR(v)) 26 0 26
else if (strEQ(k, "tenant") && SvOK(v) && SvCUR(v)) 26 0 26
1665 26 tenant = SvPV(v, tlen);
1666 20 ▶ else if (strEQ(k, "seal_bytes") && SvTRUE(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "seal_bytes") && SvTRUE(v)) 3 17 20
else if (strEQ(k, "seal_bytes") && SvTRUE(v)) 0 3 3
1667 0 (void)po_sv_to_u64(aTHX_ v, &seal_bytes);
1668 20 ▶ else if (strEQ(k, "max_rows") && SvTRUE(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "max_rows") && SvTRUE(v)) 5 15 20
else if (strEQ(k, "max_rows") && SvTRUE(v)) 2 3 5
1669 2 (void)po_sv_to_u64(aTHX_ v, &max_rows);
1670 /* The chunk cache, kept as it was given. Unrecognised keys are
1671 * dropped here, so a store built with one and asked for a
1672 * `cached_query` would quietly have answered without it. */
1673 18 ▶ else if (strEQ(k, "cache") && SvTRUE(v)) cache = v;
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "cache") && SvTRUE(v)) cache = v; 12 6 18
else if (strEQ(k, "cache") && SvTRUE(v)) cache = v; 12 0 12
1674 }
1675  
1676 87 ▶ hv_stores(self, "dir", dir ? newSVsv(dir) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(self, "dir", dir ? newSVsv(dir) : newSV(0)); 87 0 87
1677 87 hv_stores(self, "tenant", newSVpvn(tenant, tlen));
1678 87 hv_stores(self, "seal_bytes", po_u64_to_sv(seal_bytes));
1679 87 hv_stores(self, "max_rows", po_u64_to_sv(max_rows));
1680 87 ▶ if (cache) hv_stores(self, "cache", newSVsv(cache));
Branch: 1 branch
BranchTrueFalseTotal
if (cache) hv_stores(self, "cache", newSVsv(cache)); 12 75 87
1681 /* undef, not zero: a worker that restarts inherits whatever its
1682 * predecessor left on disk, and the first seal_if_full is what reads
1683 * it. Zero here would seal a full log one batch late, every restart. */
1684 87 hv_stores(self, "wal_bytes", newSV(0));
1685 87 hv_stores(self, "sealed", newSViv(0));
1686  
1687 87 RETVAL = sv_bless(newRV_noinc((SV *)self), gv_stashsv(class, GV_ADD));
1688 }
1689 OUTPUT:
1690 RETVAL
1691  
1692 SV *
1693 post_tenant(SV *self)
1694 ALIAS:
1695 dir = 1
1696 CODE:
1697 {
1698 HV *h;
1699 SV **f;
1700 1 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_UNDEF; 1 0 1
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_UNDEF; 0 1 1
1701 1 h = (HV *)SvRV(self);
1702 1 ▶ f = ix ? hv_fetchs(h, "dir", 0) : hv_fetchs(h, "tenant", 0);
Branch: 1 branch
BranchTrueFalseTotal
f = ix ? hv_fetchs(h, "dir", 0) : hv_fetchs(h, "tenant", 0); 0 1 1
1703 1 ▶ RETVAL = (f && SvOK(*f)) ? newSVsv(*f) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (f && SvOK(*f)) ? newSVsv(*f) : newSV(0); 1 0 1
RETVAL = (f && SvOK(*f)) ? newSVsv(*f) : newSV(0); 1 0 1
1704 }
1705 OUTPUT:
1706 RETVAL
1707  
1708 # Called after every append. Returns true when the log was sealed.
1709 int
1710 post_seal_if_full(SV *self, ...)
1711 CODE:
1712 {
1713 HV *h;
1714 SV **f;
1715 1 po_u64 added = 0, have = 0, threshold = 0;
1716 char dir[PO_PATHMAX];
1717  
1718 2 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_IV(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_IV(0); 1 0 1
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) XSRETURN_IV(0); 0 1 1
1719 1 h = (HV *)SvRV(self);
1720 1 ▶ if (items > 1 && SvOK(ST(1))) (void)po_sv_to_u64(aTHX_ ST(1), &added);
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) (void)po_sv_to_u64(aTHX_ ST(1), &added); 1 0 1
if (items > 1 && SvOK(ST(1))) (void)po_sv_to_u64(aTHX_ ST(1), &added); 1 0 1
1721 1 ▶ if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_IV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) XSRETURN_IV(0); 0 1 1
1722  
1723 /* READ THE SIZE FROM DISK ONCE per worker and then track it, rather
1724 * than stat-ing per batch. A worker that restarts inherits whatever
1725 * its predecessor left, which is why this is not simply zero. */
1726 1 f = hv_fetchs(h, "wal_bytes", 0);
1727 1 ▶ if (!f || !SvOK(*f)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) { 1 0 1
if (!f || !SvOK(*f)) { 1 0 1
1728 char live[PO_PATHMAX];
1729 char wn[64];
1730 1 po_u64 sz = 0;
1731 1 int wl = snprintf(wn, sizeof(wn), "w%ld.wal", (long)po_getpid());
1732 1 ▶ if (wl > 0 && po_path_join(live, sizeof(live), dir, wn))
Branch: 2 branches
BranchTrueFalseTotal
if (wl > 0 && po_path_join(live, sizeof(live), dir, wn)) 1 0 1
if (wl > 0 && po_path_join(live, sizeof(live), dir, wn)) 1 0 1
1733 1 po_file_size(live, &sz);
1734 1 have = sz;
1735 }
1736 0 else (void)po_sv_to_u64(aTHX_ *f, &have);
1737  
1738 1 have += added;
1739 1 hv_stores(h, "wal_bytes", po_u64_to_sv(have));
1740  
1741 1 f = hv_fetchs(h, "seal_bytes", 0);
1742 1 ▶ if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &threshold);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &threshold); 1 0 1
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &threshold); 1 0 1
1743 1 ▶ if (!threshold || have < threshold) XSRETURN_IV(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!threshold || have < threshold) XSRETURN_IV(0); 1 0 1
if (!threshold || have < threshold) XSRETURN_IV(0); 1 0 1
1744  
1745 { /* seal() is the method beside this one */
1746 SV *sealed;
1747 int ok;
1748 0 dSP;
1749 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
1750 0 ▶ XPUSHs(self);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(self); 0 0 0
1751 0 PUTBACK;
1752 0 ok = call_method("seal", G_SCALAR);
1753 0 SPAGAIN;
1754 0 ▶ sealed = ok ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
sealed = ok ? POPs : &PL_sv_undef; 0 0 0
1755 0 ▶ RETVAL = (SvOK(sealed) && SvTRUE(sealed)) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (SvOK(sealed) && SvTRUE(sealed)) ? 1 : 0; 0 0 0
RETVAL = (SvOK(sealed) && SvTRUE(sealed)) ? 1 : 0; 0 0 0
1756 0 PUTBACK;
1757 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1758 }
1759 }
1760 OUTPUT:
1761 RETVAL
1762  
1763 # The executor's shape. `records` builds it during the scan, so this is that
1764 # call with a flag rather than a second pass and a sub call per row.
1765 void
1766 post_rows(SV *self, ...)
1767 PPCODE:
1768 {
1769 24 AV *out = newAV();
1770 24 HV *meta = newHV();
1771 24 po_u64 from = 0, to = PO_U64_MAX, limit = 0, trace_hi = 0, trace_lo = 0;
1772 24 int have_from = 0, have_to = 0, kind = 0, have_trace = 0;
1773 IV ai;
1774  
1775 83 ▶ for (ai = 1; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 1; ai + 1 < items; ai += 2) { 59 24 83
1776 59 const char *k = SvPV_nolen(ST(ai));
1777 59 SV *v = ST(ai + 1);
1778 59 ▶ if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from);
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from); 9 50 59
if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from); 9 0 9
1779 50 ▶ else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to); 8 42 50
else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to); 8 0 8
1780 42 ▶ else if (strEQ(k, "kind") && SvOK(v)) kind = (int)SvIV(v);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "kind") && SvOK(v)) kind = (int)SvIV(v); 12 30 42
else if (strEQ(k, "kind") && SvOK(v)) kind = (int)SvIV(v); 12 0 12
1781 30 ▶ else if (strEQ(k, "limit")&& SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "limit")&& SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit); 10 20 30
else if (strEQ(k, "limit")&& SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit); 10 0 10
1782 20 ▶ else if (strEQ(k, "trace_hi") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "trace_hi") && SvOK(v)) 10 10 20
else if (strEQ(k, "trace_hi") && SvOK(v)) 10 0 10
1783 10 have_trace |= po_sv_to_u64(aTHX_ v, &trace_hi);
1784 10 ▶ else if (strEQ(k, "trace_lo") && SvOK(v))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "trace_lo") && SvOK(v)) 10 0 10
else if (strEQ(k, "trace_lo") && SvOK(v)) 10 0 10
1785 10 have_trace |= po_sv_to_u64(aTHX_ v, &trace_lo);
1786 }
1787 24 ▶ if (!limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!limit) { 14 10 24
1788 14 SV **f = hv_fetchs((HV *)SvRV(self), "max_rows", 0);
1789 14 ▶ limit = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000;
Branch: 2 branches
BranchTrueFalseTotal
limit = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000; 14 0 14
limit = (f && SvOK(*f)) ? (po_u64)SvUV(*f) : 500000; 14 0 14
1790 }
1791  
1792 /* The same scan with the row shape asked for, not a second pass. */
1793 24 po_records_run(aTHX_ self, from, have_from, to, have_to,
1794 kind, limit, 1, trace_hi, trace_lo, have_trace,
1795 NULL,
1796 NULL, 0, NULL, 0, out, meta);
1797 24 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 24 24
1798 24 ▶ mXPUSHs(newRV_noinc((SV *)meta));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)meta)); 0 24 24
1799 }
1800  
1801 # Parse, plan and run one query against the store.
1802 #
1803 # The result is the executor's, unchanged, plus `store` describing what was
1804 # read to produce it. A caller that renders the rows without reading the
1805 # metadata is rendering a partial answer as a complete one.
1806 void
1807 post_query(SV *self, SV *q, ...)
1808 PPCODE:
1809 {
1810 po_query pq;
1811 po_plan plan;
1812 po_pushdown pd;
1813 STRLEN qlen;
1814 359 const char *qp = SvPV(q, qlen);
1815 359 po_u64 from = 0, to = PO_U64_MAX, limit = 0, hard_max = 0,
1816 359 step = 0, max_rows = 0, dflt = 500000;
1817 359 char vizbuf[8]; size_t viz_len = 0;
1818 359 int have_from = 0, have_to = 0, kind = 0, have_pd = 0;
1819 359 int rekeys[8]; int nrekey = 0;
1820 IV ai;
1821 359 AV *rows = newAV();
1822 359 HV *meta = newHV();
1823 HV *res;
1824 SV **f;
1825  
1826 1292 ▶ for (ai = 2; ai + 1 < items; ai += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (ai = 2; ai + 1 < items; ai += 2) { 933 359 1292
1827 933 const char *k = SvPV_nolen(ST(ai));
1828 933 SV *v = ST(ai + 1);
1829 933 ▶ if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from);
Branch: 2 branches
BranchTrueFalseTotal
if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from); 338 595 933
if (strEQ(k, "from") && SvOK(v)) have_from = po_sv_to_u64(aTHX_ v, &from); 336 2 338
1830 597 ▶ else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to); 338 259 597
else if (strEQ(k, "to") && SvOK(v)) have_to = po_sv_to_u64(aTHX_ v, &to); 336 2 338
1831 261 ▶ else if (strEQ(k, "limit") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "limit") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit); 104 157 261
else if (strEQ(k, "limit") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &limit); 104 0 104
1832 157 ▶ else if (strEQ(k, "hard_max") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &hard_max);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "hard_max") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &hard_max); 70 87 157
else if (strEQ(k, "hard_max") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &hard_max); 70 0 70
1833 87 ▶ else if (strEQ(k, "step") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &step);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "step") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &step); 0 87 87
else if (strEQ(k, "step") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &step); 0 0 0
1834 87 ▶ else if (strEQ(k, "max_rows") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &max_rows);
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(k, "max_rows") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &max_rows); 70 17 87
else if (strEQ(k, "max_rows") && SvOK(v)) (void)po_sv_to_u64(aTHX_ v, &max_rows); 70 0 70
1835 }
1836 359 f = hv_fetchs((HV *)SvRV(self), "max_rows", 0);
1837 359 ▶ if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &dflt);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &dflt); 359 0 359
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &dflt); 359 0 359
1838 359 ▶ if (!limit) limit = dflt;
Branch: 1 branch
BranchTrueFalseTotal
if (!limit) limit = dflt; 255 104 359
1839 359 ▶ if (!hard_max) hard_max = dflt;
Branch: 1 branch
BranchTrueFalseTotal
if (!hard_max) hard_max = dflt; 289 70 359
1840 359 ▶ if (!max_rows) max_rows = dflt;
Branch: 1 branch
BranchTrueFalseTotal
if (!max_rows) max_rows = dflt; 289 70 359
1841  
1842 359 ▶ if (!po_parse(&pq, qp, (size_t)qlen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_parse(&pq, qp, (size_t)qlen)) { 4 355 359
1843 4 res = newHV();
1844 4 hv_stores(res, "ok", newSViv(0));
1845 4 hv_stores(res, "stage", newSVpvs("parse"));
1846 4 hv_stores(res, "error", newSVpv(pq.err, 0));
1847 4 hv_stores(res, "offset", newSViv((IV)pq.err_off));
1848 4 SvREFCNT_dec((SV *)rows); SvREFCNT_dec((SV *)meta);
1849 4 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 4 4
1850 4 XSRETURN(1);
1851 }
1852  
1853 /* The chart kind, copied out NOW: pq is freed on one exit path
1854 * before the result is assembled, and a pointer into a freed bump is
1855 * the quiet kind of wrong. Eight bytes covers the longest name. */
1856 {
1857 710 viz_len = pq.viz_len < sizeof(vizbuf) - 1
1858 355 ▶ ? pq.viz_len : 0;
Branch: 1 branch
BranchTrueFalseTotal
? pq.viz_len : 0; 355 0 355
1859 355 ▶ if (pq.viz && viz_len) memcpy(vizbuf, pq.viz, viz_len);
Branch: 2 branches
BranchTrueFalseTotal
if (pq.viz && viz_len) memcpy(vizbuf, pq.viz, viz_len); 9 346 355
if (pq.viz && viz_len) memcpy(vizbuf, pq.viz, viz_len); 9 0 9
1860 }
1861  
1862 /* The source decides which records to read. `trace` and `spans` are
1863 * two views of the same records - one assembled, one not. */
1864 355 switch (pq.source) {
1865 45 case PO_SRC_METRIC: kind = PO_METRIC; break;
1866 95 case PO_SRC_LOG: kind = PO_LOG; break;
1867 215 default: kind = PO_SPAN; break;
1868 }
1869  
1870 /* WHAT THE QUERY PROVES BEFORE ANYTHING IS READ. A range it proved
1871 * narrows the read; predicates that cannot be satisfied stop it. */
1872 355 ▶ if (po_plan_build(&plan, &pq, NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_plan_build(&plan, &pq, NULL)) { 355 0 355
1873 355 po_pushdown_build(&pd, &plan);
1874 355 ▶ if (po_pushdown_empty(&pd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_pushdown_empty(&pd)) { 0 355 355
1875 0 HV *m = newHV(), *st = newHV();
1876 0 res = newHV();
1877 0 hv_stores(m, "scanned_rows", newSViv(0));
1878 0 hv_stores(m, "scanned_bytes", newSViv(0));
1879 0 hv_stores(m, "truncated", newSViv(0));
1880 0 hv_stores(m, "degraded", newSViv(0));
1881 0 hv_stores(m, "exact", newSViv(1));
1882 0 hv_stores(m, "steps", newSViv(0));
1883 0 hv_stores(st, "scanned", newSViv(0));
1884 0 hv_stores(st, "skipped", newSViv(0));
1885 0 hv_stores(st, "files", newSViv(0));
1886 0 hv_stores(st, "degraded", newSViv(0));
1887 0 hv_stores(st, "truncated", newSViv(0));
1888 0 hv_stores(st, "empty", newSViv(1));
1889 0 hv_stores(res, "ok", newSViv(1));
1890 0 ▶ if (viz_len)
Branch: 1 branch
BranchTrueFalseTotal
if (viz_len) 0 0 0
1891 0 hv_stores(res, "viz", newSVpvn(vizbuf, viz_len));
1892 0 hv_stores(res, "shape", newSVpvs("rows"));
1893 0 hv_stores(res, "rows", newRV_noinc((SV *)rows));
1894 0 hv_stores(res, "groups", newRV_noinc((SV *)newAV()));
1895 0 hv_stores(res, "meta", newRV_noinc((SV *)m));
1896 0 hv_stores(res, "store", newRV_noinc((SV *)st));
1897 0 SvREFCNT_dec((SV *)meta);
1898 0 po_query_free(&pq);
1899 0 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 0 0
1900 0 XSRETURN(1);
1901 }
1902 355 ▶ if (pd.time_bounded) {
Branch: 1 branch
BranchTrueFalseTotal
if (pd.time_bounded) { 3 352 355
1903 3 ▶ if (!have_from || pd.from > from) { from = pd.from; have_from = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!have_from || pd.from > from) { from = pd.from; have_from = 1; } 2 1 3
if (!have_from || pd.from > from) { from = pd.from; have_from = 1; } 2 0 2
1904 3 ▶ if (!have_to || pd.to < to) { to = pd.to; have_to = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!have_to || pd.to < to) { to = pd.to; have_to = 1; } 2 1 3
if (!have_to || pd.to < to) { to = pd.to; have_to = 1; } 0 2 2
1905 }
1906 355 have_pd = 1;
1907 }
1908  
1909 /* pq stays alive across the scan: the needles below point into its
1910 * bump allocator, and the free moved past the read the day the scan
1911 * learned to use them. The executor re-applies both filters row by
1912 * row - the needle is only ever a "nothing here" proof (see
1913 * po_records_scan_file). */
1914 400 ▶ po_records_run(aTHX_ self, from, have_from, to, have_to,
Branch: 4 branches
BranchTrueFalseTotal
po_records_run(aTHX_ self, from, have_from, to, have_to, 45 310 355
po_records_run(aTHX_ self, from, have_from, to, have_to, 45 310 355
po_records_run(aTHX_ self, from, have_from, to, have_to, 355 0 355
po_records_run(aTHX_ self, from, have_from, to, have_to, 355 0 355
1915 kind, limit, 1, 0, 0, 0, NULL,
1916 have_pd ? pd.search : NULL,
1917 have_pd ? pd.search_len : 0,
1918 45 ▶ (kind == PO_METRIC && pq.name_len) ? pq.name : NULL,
Branch: 1 branch
BranchTrueFalseTotal
(kind == PO_METRIC && pq.name_len) ? pq.name : NULL, 45 0 45
1919 (kind == PO_METRIC) ? pq.name_len : 0,
1920 rows, meta);
1921  
1922 /* THE RE-KEY CHAIN, COPIED OUT BEFORE `pq` GOES. The second pass
1923 * needs to know which cross-signal stages were asked for and in what
1924 * order, and it runs after the executor - long past the bump
1925 * allocator's life. Four ints outlive it; a pointer into the bump
1926 * would be the quiet kind of wrong the `viz` copy above exists to
1927 * avoid. */
1928 {
1929 po_stage *s;
1930 736 ▶ for (s = pq.stages; s; s = s->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = pq.stages; s; s = s->next) { 381 355 736
1931 381 ▶ if (s->kind < PO_ST_EXEMPLARS || s->kind > PO_ST_SPANS) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (s->kind < PO_ST_EXEMPLARS || s->kind > PO_ST_SPANS) continue; 251 130 381
if (s->kind < PO_ST_EXEMPLARS || s->kind > PO_ST_SPANS) continue; 236 15 251
1932 15 ▶ if (nrekey < (int)(sizeof(rekeys) / sizeof(rekeys[0])))
Branch: 1 branch
BranchTrueFalseTotal
if (nrekey < (int)(sizeof(rekeys) / sizeof(rekeys[0]))) 15 0 15
1933 15 rekeys[nrekey++] = s->kind;
1934 }
1935 }
1936 355 po_query_free(&pq);
1937  
1938 { /* the executor, over the rows just read */
1939 355 SV *rv = NULL;
1940 355 HV *opt = newHV();
1941 int n;
1942  
1943 355 hv_stores(opt, "max_rows", po_u64_to_sv(max_rows));
1944 355 hv_stores(opt, "rows_available", newSViv((IV)av_len(rows) + 1));
1945 355 hv_stores(opt, "hard_max", po_u64_to_sv(hard_max));
1946 355 hv_stores(opt, "step", po_u64_to_sv(step));
1947  
1948 /* No dSP here. In a PPCODE body `SP` is already xsubpp's, and a
1949 * second one shadows it - the call refreshes the shadow, the
1950 * XSUB's own pointer stays stale, and everything pushed
1951 * afterwards lands nowhere. That is why `rows` returned an empty
1952 * list before it was written this way. */
1953 355 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 355 355
1954 355 ▶ XPUSHs(sv_2mortal(newSVsv(q)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(q))); 0 355 355
1955 355 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)rows)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)rows))); 0 355 355
1956 355 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)opt)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)opt))); 0 355 355
1957 355 PUTBACK;
1958 355 n = call_pv("Punk::Observe::Exec::run", G_SCALAR);
1959 355 SPAGAIN;
1960 355 ▶ rv = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
rv = n ? SvREFCNT_inc(POPs) : NULL; 355 0 355
1961 355 PUTBACK;
1962 355 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 355 0 355
1963 355 SPAGAIN;
1964  
1965 355 ▶ if (!rv || !SvROK(rv) || SvTYPE(SvRV(rv)) != SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rv || !SvROK(rv) || SvTYPE(SvRV(rv)) != SVt_PVHV) { 355 0 355
if (!rv || !SvROK(rv) || SvTYPE(SvRV(rv)) != SVt_PVHV) { 355 0 355
if (!rv || !SvROK(rv) || SvTYPE(SvRV(rv)) != SVt_PVHV) { 0 355 355
1966 0 ▶ if (rv) SvREFCNT_dec(rv);
Branch: 1 branch
BranchTrueFalseTotal
if (rv) SvREFCNT_dec(rv); 0 0 0
1967 0 res = newHV();
1968 0 hv_stores(res, "ok", newSViv(0));
1969 0 hv_stores(res, "stage", newSVpvs("plan"));
1970 0 hv_stores(res, "error", newSVpvs("the query could not be run"));
1971 0 SvREFCNT_dec((SV *)meta);
1972 0 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 0 0
1973 0 XSRETURN(1);
1974 }
1975 355 res = (HV *)SvRV(rv);
1976  
1977 /* ---- THE CROSS-SIGNAL SECOND PASS -------------------------
1978 *
1979 * `| exemplars | traces | logs` is a JOIN, and this is it: take
1980 * the trace ids the surviving rows carried, then read the target
1981 * signal filtered to that set. Each re-key stage is the same
1982 * operation, which is why a chain of them is a loop and not
1983 * three cases.
1984 *
1985 * It runs HERE rather than inside the executor because the
1986 * executor is handed rows and cannot read more; the store is
1987 * what knows how to scan, and it is holding both.
1988 */
1989 355 ▶ if (nrekey) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrekey) { 8 347 355
1990 int ri;
1991 21 ▶ for (ri = 0; ri < nrekey; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < nrekey; ri++) { 14 7 21
1992 po_traceset tset;
1993 14 AV *out, *kh = NULL, *kl = NULL;
1994 14 HV *m2 = newHV();
1995 14 SSize_t j, sn = 0;
1996 14 int want_kind, over = 0;
1997 14 SV **a = hv_fetchs(res, "rekey_hi", 0);
1998 14 SV **b = hv_fetchs(res, "rekey_lo", 0);
1999 14 SV **ov = hv_fetchs(res, "rekey_overflow", 0);
2000  
2001 /* THE IDS COME FROM THE EXECUTOR, not from the result's
2002 * rows: after `| bucket(5m) p99` the rows are gone and
2003 * only buckets remain, so a join reading rows would find
2004 * nothing and report success. The executor collects them
2005 * where each row passes the filters instead. */
2006 14 ▶ if (a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVAV) 10 4 14
if (a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVAV) 10 0 10
if (a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVAV) 10 0 10
2007 10 kh = (AV *)SvRV(*a);
2008 14 ▶ if (b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) 10 4 14
if (b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) 10 0 10
if (b && SvROK(*b) && SvTYPE(SvRV(*b)) == SVt_PVAV) 10 0 10
2009 10 kl = (AV *)SvRV(*b);
2010 14 ▶ if (ov && SvTRUE(*ov)) over = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (ov && SvTRUE(*ov)) over = 1; 1 13 14
if (ov && SvTRUE(*ov)) over = 1; 1 0 1
2011  
2012 15 ▶ if (!po_traceset_init(&tset)) { SvREFCNT_dec((SV *)m2); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_traceset_init(&tset)) { SvREFCNT_dec((SV *)m2); break; } 0 14 14
2013 14 ▶ sn = kh ? av_len(kh) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
sn = kh ? av_len(kh) + 1 : 0; 10 4 14
2014 4128 ▶ for (j = 0; j < sn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < sn; j++) { 4114 14 4128
2015 4114 SV **h = av_fetch(kh, j, 0);
2016 4114 ▶ SV **l = kl ? av_fetch(kl, j, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **l = kl ? av_fetch(kl, j, 0) : NULL; 4114 0 4114
2017 4114 po_u64 hi = 0, lo = 0;
2018 4114 ▶ if (h && SvOK(*h)) (void)po_sv_to_u64(aTHX_ *h, &hi);
Branch: 2 branches
BranchTrueFalseTotal
if (h && SvOK(*h)) (void)po_sv_to_u64(aTHX_ *h, &hi); 4114 0 4114
if (h && SvOK(*h)) (void)po_sv_to_u64(aTHX_ *h, &hi); 4114 0 4114
2019 4114 ▶ if (l && SvOK(*l)) (void)po_sv_to_u64(aTHX_ *l, &lo);
Branch: 2 branches
BranchTrueFalseTotal
if (l && SvOK(*l)) (void)po_sv_to_u64(aTHX_ *l, &lo); 4114 0 4114
if (l && SvOK(*l)) (void)po_sv_to_u64(aTHX_ *l, &lo); 4114 0 4114
2020 4114 ▶ if (!po_traceset_add(&tset, hi, lo)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_traceset_add(&tset, hi, lo)) break; 0 4114 4114
2021 }
2022  
2023 /* THE SET IS THE ANSWER'S SIZE, AND IT IS CAPPED. Over
2024 * the cap the join is refused rather than trimmed: a
2025 * truncated join is a wrong answer that looks like a
2026 * complete one, and the row it dropped is the row
2027 * somebody was looking for. */
2028 14 ▶ if (over || tset.overflowed) {
Branch: 2 branches
BranchTrueFalseTotal
if (over || tset.overflowed) { 13 1 14
if (over || tset.overflowed) { 0 13 13
2029 1 po_traceset_free(&tset);
2030 1 SvREFCNT_dec((SV *)m2);
2031 1 hv_stores(res, "ok", newSViv(0));
2032 1 hv_stores(res, "stage", newSVpvs("plan"));
2033 /* A refusal carries NO ROWS. Leaving the left side's
2034 * rows behind a false `ok` is how a refusal gets
2035 * rendered as an answer. */
2036 1 hv_stores(res, "rows", newRV_noinc((SV *)newAV()));
2037 1 hv_stores(res, "error", newSVpvf(
2038 "that join covers more than %d traces",
2039 (int)PO_TRACESET_MAX));
2040 1 hv_stores(res, "hint", newSVpvs(
2041 "narrowing the range, or aggregating before the "
2042 "jump - as in | bucket(5m) p99 | exemplars"));
2043 1 break;
2044 }
2045 13 po_traceset_seal(&tset);
2046  
2047 /* `exemplars` re-keys the stream onto its trace ids
2048 * without changing signal: the answer is the points that
2049 * carried one. The others are dropped here, which is
2050 * what makes the stage mean something rather than being
2051 * a no-op the planner recorded. */
2052 13 ▶ if (rekeys[ri] == PO_ST_EXEMPLARS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rekeys[ri] == PO_ST_EXEMPLARS) { 7 6 13
2053 /* Over rows, the stage MEANS something on its own:
2054 * keep the points that carry an id and drop the rest.
2055 * Over an aggregate there are no rows to filter - the
2056 * ids are carried to the next stage and the buckets
2057 * are left exactly as the aggregate drew them, because
2058 * silently replacing a chart with a row list is not
2059 * what `| exemplars` was asked to do. */
2060 7 SV **rf = hv_fetchs(res, "rows", 0);
2061 7 ▶ if (rf && SvROK(*rf) && SvTYPE(SvRV(*rf)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (rf && SvROK(*rf) && SvTYPE(SvRV(*rf)) == SVt_PVAV) { 5 2 7
if (rf && SvROK(*rf) && SvTYPE(SvRV(*rf)) == SVt_PVAV) { 5 0 5
if (rf && SvROK(*rf) && SvTYPE(SvRV(*rf)) == SVt_PVAV) { 5 0 5
2062 5 AV *cur = (AV *)SvRV(*rf);
2063 5 AV *keep = newAV();
2064 5 SSize_t k, cn = av_len(cur) + 1;
2065 16 ▶ for (k = 0; k < cn; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < cn; k++) { 11 5 16
2066 11 SV **e = av_fetch(cur, k, 0);
2067 SV **x1, **x2;
2068 11 po_u64 hi = 0, lo = 0;
2069 16 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 11 0 11
if (!e || !SvROK(*e)) continue; 0 11 11
2070 11 x1 = hv_fetchs((HV *)SvRV(*e), "trace_hi", 0);
2071 11 x2 = hv_fetchs((HV *)SvRV(*e), "trace_lo", 0);
2072 11 ▶ if (x1 && SvOK(*x1)) (void)po_sv_to_u64(aTHX_ *x1, &hi);
Branch: 2 branches
BranchTrueFalseTotal
if (x1 && SvOK(*x1)) (void)po_sv_to_u64(aTHX_ *x1, &hi); 6 5 11
if (x1 && SvOK(*x1)) (void)po_sv_to_u64(aTHX_ *x1, &hi); 6 0 6
2073 11 ▶ if (x2 && SvOK(*x2)) (void)po_sv_to_u64(aTHX_ *x2, &lo);
Branch: 2 branches
BranchTrueFalseTotal
if (x2 && SvOK(*x2)) (void)po_sv_to_u64(aTHX_ *x2, &lo); 6 5 11
if (x2 && SvOK(*x2)) (void)po_sv_to_u64(aTHX_ *x2, &lo); 6 0 6
2074 11 ▶ if (!po_trace_id_valid(hi, lo)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!po_trace_id_valid(hi, lo)) continue; 5 6 11
if (!po_trace_id_valid(hi, lo)) continue; 5 0 5
2075 6 av_push(keep, SvREFCNT_inc(*e));
2076 }
2077 5 hv_stores(res, "rows", newRV_noinc((SV *)keep));
2078 }
2079 7 po_traceset_free(&tset);
2080 7 SvREFCNT_dec((SV *)m2);
2081 7 continue;
2082 }
2083  
2084 6 ▶ want_kind = rekeys[ri] == PO_ST_LOGS ? PO_LOG : PO_SPAN;
Branch: 1 branch
BranchTrueFalseTotal
want_kind = rekeys[ri] == PO_ST_LOGS ? PO_LOG : PO_SPAN; 3 3 6
2085 6 out = newAV();
2086 /* Nothing to join to is an EMPTY answer, not the whole
2087 * store: an empty set must never read as "no filter". */
2088 6 ▶ if (tset.n)
Branch: 1 branch
BranchTrueFalseTotal
if (tset.n) 4 2 6
2089 4 po_records_run(aTHX_ self, from, have_from, to, have_to,
2090 want_kind, limit, 1, 0, 0, 0, &tset,
2091 NULL, 0, NULL, 0, out, m2);
2092 6 po_traceset_free(&tset);
2093  
2094 6 hv_stores(res, "rows", newRV_noinc((SV *)out));
2095 6 hv_stores(res, "shape", newSVpvs("rows"));
2096 6 hv_stores(res, "groups", newRV_noinc((SV *)newAV()));
2097  
2098 /* TRUNCATION SURVIVES THE JOIN. A short left side makes a
2099 * short right side, and the page has to be able to say
2100 * so - the second scan's own ceiling counts too. */
2101 {
2102 6 SV **rm = hv_fetchs(res, "meta", 0);
2103 6 ▶ if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) { 6 0 6
if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) { 6 0 6
if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) { 6 0 6
2104 6 HV *m = (HV *)SvRV(*rm);
2105 6 SV **t2 = hv_fetchs(m2, "truncated", 0);
2106 6 SV **d2 = hv_fetchs(m2, "degraded", 0);
2107 6 ▶ if (t2 && SvTRUE(*t2))
Branch: 2 branches
BranchTrueFalseTotal
if (t2 && SvTRUE(*t2)) 4 2 6
if (t2 && SvTRUE(*t2)) 0 4 4
2108 0 hv_stores(m, "truncated", newSViv(1));
2109 6 ▶ if (d2 && SvTRUE(*d2))
Branch: 2 branches
BranchTrueFalseTotal
if (d2 && SvTRUE(*d2)) 4 2 6
if (d2 && SvTRUE(*d2)) 0 4 4
2110 0 hv_stores(m, "degraded", newSViv(1));
2111 }
2112 }
2113 6 SvREFCNT_dec((SV *)m2);
2114 }
2115 }
2116  
2117 355 hv_stores(res, "store", newRV_inc((SV *)meta));
2118 355 ▶ if (viz_len)
Branch: 1 branch
BranchTrueFalseTotal
if (viz_len) 9 346 355
2119 9 hv_stores(res, "viz", newSVpvn(vizbuf, viz_len));
2120  
2121 /* A range that skipped a segment and a scan that hit its ceiling
2122 * are both reasons an answer is short, and the page says so
2123 * either way. */
2124 {
2125 355 SV **rm = hv_fetchs(res, "meta", 0);
2126 355 ▶ if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) { 355 0 355
if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) { 355 0 355
if (rm && SvROK(*rm) && SvTYPE(SvRV(*rm)) == SVt_PVHV) { 355 0 355
2127 355 HV *m = (HV *)SvRV(*rm);
2128 static const char *K[] = { "truncated", "degraded" };
2129 int j;
2130 1065 ▶ for (j = 0; j < 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 2; j++) { 710 355 1065
2131 710 SV **a = hv_fetch(m, K[j], (I32)strlen(K[j]), 1);
2132 710 SV **b = hv_fetch(meta, K[j], (I32)strlen(K[j]), 0);
2133 710 ▶ if (a && b && !SvTRUE(*a) && SvTRUE(*b)) sv_setsv(*a, *b);
Branch: 4 branches
BranchTrueFalseTotal
if (a && b && !SvTRUE(*a) && SvTRUE(*b)) sv_setsv(*a, *b); 710 0 710
if (a && b && !SvTRUE(*a) && SvTRUE(*b)) sv_setsv(*a, *b); 710 0 710
if (a && b && !SvTRUE(*a) && SvTRUE(*b)) sv_setsv(*a, *b); 685 25 710
if (a && b && !SvTRUE(*a) && SvTRUE(*b)) sv_setsv(*a, *b); 2 683 685
2134 }
2135 }
2136 }
2137 355 SvREFCNT_dec((SV *)meta);
2138 355 ▶ mXPUSHs(rv);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(rv); 0 355 355
2139 }
2140 }
« include/punk_observe/po_plot.h ↑ Summary include/punk_observe/po_logidx.h »