Observe.xs

filter: Statementstmt 96.0% 2283 / 2376 Branchbran 62.5% 1640 / 2622 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.4% 3923 / 4998 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/segio.xs ↑ Summary include/punk_observe/po_key.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stddef.h> /* offsetof, for the layout assertions */
9 #include <errno.h> /* EPERM, which is how retention reads alive */
10 #ifndef _WIN32
11 # include <signal.h> /* kill(pid, 0): is that worker still there */
12 #endif
13  
14 /* The headers carry the whole contract; the XS below is a thin surface onto
15 * them. Perl headers first, because po_compat.h uses SV, UV and pTHX. */
16 #include "punk_observe/po_compat.h"
17 #include "punk_observe/po_time.h"
18 #include "punk_observe/po_rec.h"
19 #include "punk_observe/po_store.h"
20 #include "punk_observe/po_pb_read.h"
21 #include "punk_observe/po_attr.h"
22 #include "punk_observe/po_otlp_in.h"
23 #include "punk_observe/po_crc32c.h"
24 #include "punk_observe/po_wal.h"
25 #include "punk_observe/po_json_in.h"
26 #include "punk_observe/po_ingest.h"
27 #include "punk_observe/po_hash.h"
28 #include "punk_observe/po_intern.h"
29 #include "punk_observe/po_labels.h"
30 #include "punk_observe/po_seg.h"
31 #include "punk_observe/po_shared.h"
32 #include "punk_observe/po_manifest.h"
33 #include "punk_observe/po_bits.h"
34 #include "punk_observe/po_gorilla.h"
35 #include "punk_observe/po_metric.h"
36 #include "punk_observe/po_traceset.h"
37 #include "punk_observe/po_chunk.h"
38 #include "punk_observe/po_tsidx.h"
39 #include "punk_observe/po_bloom.h"
40 #include "punk_observe/po_log.h"
41 #include "punk_observe/po_logidx.h"
42 #include "punk_observe/po_span.h"
43 #include "punk_observe/po_traceidx.h"
44 #include "punk_observe/po_tattr.h"
45 #include "punk_observe/po_sgraph.h"
46 #include "punk_observe/po_lex.h"
47 #include "punk_observe/po_query.h"
48 #include "punk_observe/po_parse.h"
49 #include "punk_observe/po_row.h"
50 #include "punk_observe/po_expr.h"
51 #include "punk_observe/po_result.h"
52 #include "punk_observe/po_qexec.h"
53 #include "punk_observe/po_merge.h"
54 #include "punk_observe/po_rollup.h"
55 #include "punk_observe/po_retain.h"
56 #include "punk_observe/po_segio.h"
57 #include "punk_observe/po_svg.h"
58 #include "punk_observe/po_flame.h"
59 #include "punk_observe/po_maplayout.h"
60 #include "punk_observe/po_scan.h"
61 #ifdef PO_HAVE_BUS
62 # include "sa_abi.h" /* Shared::Arena's C ABI: the tail's ring */
63 #endif
64 #include "punk_observe/po_live.h"
65 #include "punk_observe/po_alert.h"
66 #include "punk_observe/po_route.h"
67 #include "punk_observe/po_target.h"
68 #include "punk_observe/po_panel.h"
69 #include "punk_observe/po_tenant.h"
70 #include "punk_observe/po_key.h"
71 #include "punk_observe/po_limit.h"
72 #include "punk_observe/po_nsarith.h"
73 #include "punk_observe/po_storeio.h"
74 #include "punk_observe/po_view.h"
75 #include "punk_observe/po_plot.h"
76  
77 #ifdef PO_HAVE_BUS
78 /* The fanout collector, and its cursor.
79 *
80 * Declared HERE rather than between XSUBs: xsubpp mangles a declaration that
81 * appears between two of them, and this dist has paid for that twice.
82 *
83 * The cursor is file-scope on purpose. A subscriber's cursor belongs to the
84 * process, and a forked worker that inherits one either replays what the
85 * parent already saw or skips past what it has not - both of which present as
86 * a broken tail rather than as a fork bug. po_live_cursor_reset() is what a
87 * post-fork hook calls. */
88 typedef struct { AV *av; const char *t; size_t tl; } po_live_ud;
89  
90 /* Shared::Arena's table, resolved once at BOOT; NULL means no tail across
91 * workers, which is the degrade this dist always had. The region is
92 * Observe's own anonymous one unless bus_init was handed a Shared::Arena to
93 * ride on, and the ring is "po.tail" in it. */
94 static const sa_abi *PO_SA = NULL;
95 static sa_region *po_sa_region = NULL;
96 static sa_ring *po_sa_ring = NULL;
97 static sa_cursor *po_sa_cur = NULL;
98 static int po_sa_joined = 0;
99 static po_u64 po_live_gaps = 0; /* lapped + abandoned, cumulative */
100  
101 81 static void po_sa_resolve(pTHX) {
102 SV *sv, *err;
103 81 ▶ if (PO_SA) return;
Branch: 1 branch
BranchTrueFalseTotal
if (PO_SA) return; 0 81 81
104 81 sv = eval_pv("require Shared::Arena; Shared::Arena::_abi_ptr()", 0);
105 81 err = get_sv("@", 0);
106 81 ▶ if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 81 0 81
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 81 0 81
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 81 0 81
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 0 81 81
107 {
108 81 const sa_abi *t = INT2PTR(const sa_abi *, SvUV(sv));
109 81 ▶ if (t && t->abi_version >= SA_ABI_VERSION) PO_SA = t;
Branch: 2 branches
BranchTrueFalseTotal
if (t && t->abi_version >= SA_ABI_VERSION) PO_SA = t; 81 0 81
if (t && t->abi_version >= SA_ABI_VERSION) PO_SA = t; 81 0 81
110 }
111 }
112  
113 /* The tail's cursor, opened on first use from the START of what the ring
114 * still holds (the old cursor was born at zero and caught up the same way). */
115 264 static sa_cursor *po_live_cursor(void) {
116 264 ▶ if (!po_sa_cur && po_sa_ring) po_sa_cur = PO_SA->cursor_open(po_sa_ring, 1);
Branch: 2 branches
BranchTrueFalseTotal
if (!po_sa_cur && po_sa_ring) po_sa_cur = PO_SA->cursor_open(po_sa_ring, 1); 1 263 264
if (!po_sa_cur && po_sa_ring) po_sa_cur = PO_SA->cursor_open(po_sa_ring, 1); 1 0 1
117 264 return po_sa_cur;
118 }
119  
120 /* A post-fork reset starts at NOW, not at zero.
121 *
122 * Zero means "replay the whole ring", which is the opposite of what a freshly
123 * forked worker wants: it would re-deliver every line the parent already
124 * forwarded. A cursor is a position in a stream this process has not been
125 * reading, so it is released and reopened at the current sequence; its gaps
126 * start again at nothing, as they did. */
127 4 static void po_live_cursor_reset(void) {
128 4 ▶ if (!po_sa_ring) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_sa_ring) return; 0 4 4
129 4 ▶ if (po_sa_cur) PO_SA->cursor_release(po_sa_cur);
Branch: 1 branch
BranchTrueFalseTotal
if (po_sa_cur) PO_SA->cursor_release(po_sa_cur); 2 2 4
130 4 po_sa_cur = PO_SA->cursor_open(po_sa_ring, 0);
131 4 po_live_gaps = 0;
132 4 po_sa_joined = 0; /* a new process after a fork: join again */
133 }
134  
135 /* What this cursor missed: lapped by the ring, or a hole a dead publisher
136 * left that the ring filled and this reader stepped over. */
137 264 static po_u64 po_live_cursor_gaps(void) {
138 sa_counts n;
139 264 ▶ if (!po_sa_cur) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_sa_cur) return 0; 0 264 264
140 264 PO_SA->counts(po_sa_cur, &n);
141 264 return (po_u64)(n.lapped + n.abandoned);
142 }
143  
144 105 static void po_live_collect(void *ud, uint64_t seq,
145 const char *topic, uint32_t tlen,
146 const char *payload, uint32_t plen,
147 uint32_t flags) {
148 105 po_live_ud *u = (po_live_ud *)ud;
149 dTHX;
150 (void)seq; (void)flags;
151 /* A fanout subscriber sees every topic, so the filter is here. A tail
152 * that forwarded another tenant's topic would be the tenancy boundary
153 * failing open. */
154 105 ▶ if (u->tl != (size_t)tlen || memcmp(u->t, topic, tlen) != 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (u->tl != (size_t)tlen || memcmp(u->t, topic, tlen) != 0) return; 104 1 105
if (u->tl != (size_t)tlen || memcmp(u->t, topic, tlen) != 0) return; 0 104 104
155 104 av_push(u->av, newSVpvn(payload, plen));
156 }
157 #endif
158  
159 /* A resolver's answer, borrowed. The seam takes a callback rather than a
160 * value so that the constant case and the host-callback case go through ONE
161 * validation, and this is the shim that lets an XSUB supply the second. */
162 typedef struct { const char *p; size_t len; } po_tn_sv;
163  
164 7 static int po_tn_cb(void *ud, const char **out, size_t *len) {
165 7 po_tn_sv *s = (po_tn_sv *)ud;
166 7 ▶ if (!s->p) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->p) return 0; 1 6 7
167 6 *out = s->p;
168 6 *len = s->len;
169 6 return 1;
170 }
171  
172 /* The service name out of a record's attribute block, WITHOUT building a
173 * hash for the whole thing.
174 *
175 * Summarising a segment reads one attribute per record and ignores the rest,
176 * so decoding all of them into a Perl hash - which is what the Perl this
177 * replaced did, by way of po_rec_hv - allocated an SV per attribute per
178 * record to throw every one of them away. The block is sorted, so this can
179 * also stop as soon as it is past where the key would be. */
180 56527 static void po_rec_service(const po_rec *rec, const po_arena *ar,
181 const char **out, size_t *out_len) {
182 const unsigned char *p, *e;
183 56527 po_u64 n = 0;
184 56527 int sh = 0;
185 static const char KEY[] = "service.name";
186 56527 const size_t KEYLEN = sizeof(KEY) - 1;
187  
188 56527 *out = NULL; *out_len = 0;
189 56527 ▶ if (!rec->attr_len) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rec->attr_len) return; 17119 39408 56527
190  
191 39408 p = (const unsigned char *)ar->base + rec->attr_off;
192 39408 e = p + rec->attr_len;
193 39408 ▶ while (p < e) { unsigned char c = *p++; n |= (po_u64)(c & 0x7F) << sh;
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { unsigned char c = *p++; n |= (po_u64)(c & 0x7F) << sh; 39408 0 39408
194 39408 ▶ if (!(c & 0x80)) break; sh += 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) break; sh += 7; } 39408 0 39408
195  
196 44020 ▶ while (n-- && p < e) {
Branch: 2 branches
BranchTrueFalseTotal
while (n-- && p < e) { 41500 2520 44020
while (n-- && p < e) { 41500 0 41500
197 41500 po_u64 kl = 0;
198 unsigned char tag;
199 const char *kp;
200 41500 sh = 0;
201 41500 ▶ while (p < e) { unsigned char c = *p++; kl |= (po_u64)(c & 0x7F) << sh;
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { unsigned char c = *p++; kl |= (po_u64)(c & 0x7F) << sh; 41500 0 41500
202 41500 ▶ if (!(c & 0x80)) break; sh += 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) break; sh += 7; } 41500 0 41500
203 41500 ▶ if ((po_u64)(e - p) < kl) return;
Branch: 1 branch
BranchTrueFalseTotal
if ((po_u64)(e - p) < kl) return; 0 41500 41500
204 41500 kp = (const char *)p;
205 41500 p += kl;
206 41500 ▶ if (p >= e) return;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= e) return; 0 41500 41500
207 41500 tag = *p++;
208  
209 {
210 41500 ▶ int is_key = (kl == KEYLEN && memcmp(kp, KEY, KEYLEN) == 0);
Branch: 2 branches
BranchTrueFalseTotal
int is_key = (kl == KEYLEN && memcmp(kp, KEY, KEYLEN) == 0); 37008 4492 41500
int is_key = (kl == KEYLEN && memcmp(kp, KEY, KEYLEN) == 0); 36888 120 37008
211 41500 switch (tag) {
212 40892 case PO_AV_STRING: case PO_AV_BYTES: case PO_AV_NESTED: {
213 40892 po_u64 vl = 0;
214 40892 sh = 0;
215 40894 ▶ while (p < e) { unsigned char c = *p++;
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { unsigned char c = *p++; 40894 0 40894
216 40894 vl |= (po_u64)(c & 0x7F) << sh;
217 40894 ▶ if (!(c & 0x80)) break; sh += 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) break; sh += 7; } 40892 2 40894
218 40892 ▶ if ((po_u64)(e - p) < vl) return;
Branch: 1 branch
BranchTrueFalseTotal
if ((po_u64)(e - p) < vl) return; 0 40892 40892
219 40892 ▶ if (is_key) { *out = (const char *)p; *out_len = (size_t)vl; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_key) { *out = (const char *)p; *out_len = (size_t)vl; return; } 36888 4004 40892
220 4004 p += vl;
221 4004 break;
222 }
223 2 case PO_AV_DOUBLE:
224 2 ▶ if ((size_t)(e - p) < 8) return;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(e - p) < 8) return; 0 2 2
225 2 p += 8;
226 2 break;
227 606 default:
228 /* An int or a bool: EIGHT FIXED BYTES, the same as a
229 * double, because that is what po_attrs_encode writes.
230 *
231 * This skipped a varint instead, and the effect was not a
232 * wrong integer - it was that the walk lost its place.
233 * One byte consumed where eight were written leaves the
234 * cursor inside the value, so every key after it is read
235 * out of the middle of something, `service.name` never
236 * matches, and every row renders as "unknown". The block
237 * is sorted, so a single `process.pid` was enough to hide
238 * every service name in the store. */
239 606 ▶ if ((size_t)(e - p) < 8) return;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(e - p) < 8) return; 0 606 606
240 606 p += 8;
241 606 break;
242 }
243 }
244 }
245 }
246  
247 /* ---- the sidecar, read and written in C ---------------------------------- */
248  
249 /* The scalar counters out of a sidecar, WITHOUT building a Perl hash for the
250 * whole thing.
251 *
252 * `stats` wants eight integers and the service table; parsing the file into a
253 * hash of SVs to add them up allocates a few dozen SVs per segment and throws
254 * every one away. On a store with a thousand segments that is the difference
255 * between a status page and a pause.
256 *
257 * The service counts DO go into an HV, because merging them across segments is
258 * the answer rather than a step towards it. */
259 typedef struct {
260 po_u64 records, logs, spans, metrics, errors, traces;
261 /* The time span too, so ONE parse serves both the skip decision and the
262 * totals. `span_seen` says both bounds were present - a sidecar from an
263 * interrupted seal may carry neither, and a segment whose span is unknown
264 * must never be skipped on it. */
265 po_u64 t_min, t_max;
266 int span_seen;
267 } po_idx_nums;
268  
269 4961 static int po_idx_num_field(const char *f, size_t fl, const char *want) {
270 4961 size_t wl = strlen(want);
271 4961 ▶ return fl == wl && memcmp(f, want, wl) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return fl == wl && memcmp(f, want, wl) == 0; 1370 3591 4961
return fl == wl && memcmp(f, want, wl) == 0; 821 549 1370
272 }
273  
274 91 static int po_index_nums(const char *path, po_idx_nums *out, HV *svc) {
275 91 size_t len = 0;
276 91 char *buf = po_slurp(path, &len);
277 char *p, *end;
278 dTHX;
279  
280 91 ▶ if (!buf) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return 0; 2 89 91
281 89 memset(out, 0, sizeof(*out));
282  
283 89 p = buf; end = buf + len;
284 1015 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 926 89 1015
285 926 char *nl = memchr(p, '\n', (size_t)(end - p));
286 926 char *line = p;
287 926 ▶ size_t llen = nl ? (size_t)(nl - p) : (size_t)(end - p);
Branch: 1 branch
BranchTrueFalseTotal
size_t llen = nl ? (size_t)(nl - p) : (size_t)(end - p); 926 0 926
288 char *f[8];
289 size_t fl[8];
290 926 int nf = 0;
291 926 size_t i, start = 0;
292  
293 926 ▶ p = nl ? nl + 1 : end;
Branch: 1 branch
BranchTrueFalseTotal
p = nl ? nl + 1 : end; 926 0 926
294 1038 ▶ if (!llen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!llen) continue; 0 926 926
295  
296 13538 ▶ for (i = 0; i <= llen && nf < 8; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i <= llen && nf < 8; i++) { 12612 926 13538
for (i = 0; i <= llen && nf < 8; i++) { 12612 0 12612
297 12612 ▶ if (i == llen || line[i] == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (i == llen || line[i] == '\t') { 11686 926 12612
if (i == llen || line[i] == '\t') { 1302 10384 11686
298 2228 f[nf] = line + start; fl[nf] = i - start; nf++;
299 2228 start = i + 1;
300 }
301 }
302 926 ▶ if (nf < 2) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (nf < 2) continue; 0 926 926
303  
304 926 ▶ if (po_idx_num_field(f[0], fl[0], "svc") && nf >= 3 && svc) {
Branch: 3 branches
BranchTrueFalseTotal
if (po_idx_num_field(f[0], fl[0], "svc") && nf >= 3 && svc) { 112 814 926
if (po_idx_num_field(f[0], fl[0], "svc") && nf >= 3 && svc) { 112 0 112
if (po_idx_num_field(f[0], fl[0], "svc") && nf >= 3 && svc) { 112 0 112
305 char key[512];
306 112 size_t kn = po_idx_unesc(f[1], fl[1], key, sizeof(key));
307 112 SV **slot = hv_fetch(svc, key, (I32)kn, 1);
308 112 ▶ if (slot) {
Branch: 1 branch
BranchTrueFalseTotal
if (slot) { 112 0 112
309 112 ▶ IV had = SvOK(*slot) ? SvIV(*slot) : 0;
Branch: 1 branch
BranchTrueFalseTotal
IV had = SvOK(*slot) ? SvIV(*slot) : 0; 0 112 112
310 112 sv_setiv(*slot, had + (IV)atol(f[2]));
311 }
312 112 continue;
313 }
314  
315 {
316 814 po_u64 v = (po_u64)strtoul(f[1], NULL, 10);
317 814 ▶ if (po_idx_num_field(f[0], fl[0], "records")) out->records = v;
Branch: 1 branch
BranchTrueFalseTotal
if (po_idx_num_field(f[0], fl[0], "records")) out->records = v; 89 725 814
318 725 ▶ else if (po_idx_num_field(f[0], fl[0], "logs")) out->logs = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "logs")) out->logs = v; 88 637 725
319 637 ▶ else if (po_idx_num_field(f[0], fl[0], "spans")) out->spans = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "spans")) out->spans = v; 88 549 637
320 549 ▶ else if (po_idx_num_field(f[0], fl[0], "metrics")) out->metrics = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "metrics")) out->metrics = v; 88 461 549
321 461 ▶ else if (po_idx_num_field(f[0], fl[0], "errors")) out->errors = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "errors")) out->errors = v; 89 372 461
322 372 ▶ else if (po_idx_num_field(f[0], fl[0], "traces")) out->traces = v;
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "traces")) out->traces = v; 89 283 372
323 /* strtoull, not the strtoul above: these are nanosecond instants
324 * and a 32-bit long truncates them. */
325 283 ▶ else if (po_idx_num_field(f[0], fl[0], "t_min")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "t_min")) { 89 194 283
326 89 out->t_min = (po_u64)strtoull(f[1], NULL, 10);
327 89 out->span_seen |= 1;
328 }
329 194 ▶ else if (po_idx_num_field(f[0], fl[0], "t_max")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (po_idx_num_field(f[0], fl[0], "t_max")) { 89 105 194
330 89 out->t_max = (po_u64)strtoull(f[1], NULL, 10);
331 89 out->span_seen |= 2;
332 }
333 }
334 }
335 89 out->span_seen = (out->span_seen == 3);
336 89 free(buf);
337 89 return 1;
338 }
339  
340 /* The same figures for a log that has NOT been sealed, counted from the
341 * frames themselves because there is no sidecar to read them from.
342 *
343 * WITHOUT THIS THE STATUS PAGE READS ZERO ON A WORKING STORE. It counted
344 * sealed segments only, and a live log contributed nothing but a tally of how
345 * many live logs there were - so a fresh receiver that had accepted ten
346 * thousand records but not yet passed its seal threshold reported no records,
347 * no services and no traces, while every other screen showed the data. A
348 * number that is late is a number that is wrong, and this one was wrong in
349 * the direction that reads as "nothing is arriving".
350 *
351 * Distinct traces are NOT counted here. That needs the span set assembled,
352 * which is a scan rather than a tally, and the status page is a page people
353 * leave open.
354 */
355 11 static int po_wal_nums(const char *path, po_idx_nums *out, HV *svc) {
356 11 size_t len = 0;
357 11 char *buf = po_slurp(path, &len);
358 po_wal_replay rp;
359 11 size_t off = 0;
360 dTHX;
361  
362 11 ▶ if (!buf) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return 0; 0 11 11
363 11 memset(out, 0, sizeof(*out));
364 11 po_wal_replay_buf(buf, len, &rp, NULL, NULL);
365  
366 14 for (;;) {
367 uint32_t magic, frame_len, n_recs, arena_len;
368 uint16_t flags, version;
369 const char *h;
370 const po_rec *recs;
371 po_arena view;
372 size_t i;
373  
374 25 ▶ if (len - off < PO_WAL_HDR) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len - off < PO_WAL_HDR) break; 11 14 25
375 14 h = buf + off;
376 14 memcpy(&magic, h, 4); magic = po_le32(magic);
377 14 ▶ if (magic != PO_WAL_MAGIC) break;
Branch: 1 branch
BranchTrueFalseTotal
if (magic != PO_WAL_MAGIC) break; 0 14 14
378 14 memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len);
379 14 memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs);
380 14 memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len);
381 14 flags = (uint16_t)((unsigned char)h[36] | ((unsigned char)h[37] << 8));
382 14 version = (uint16_t)((unsigned char)h[38] | ((unsigned char)h[39] << 8));
383 14 ▶ if (version != PO_WAL_VERSION) break;
Branch: 1 branch
BranchTrueFalseTotal
if (version != PO_WAL_VERSION) break; 0 14 14
384 14 ▶ if (flags & PO_WAL_F_SEALED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & PO_WAL_F_SEALED) break; 0 14 14
385 14 ▶ if (len - off - PO_WAL_HDR < (size_t)frame_len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len - off - PO_WAL_HDR < (size_t)frame_len) break; 0 14 14
386 14 ▶ 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 14 14
387  
388 14 recs = (const po_rec *)(h + PO_WAL_HDR);
389 14 view.base = (char *)(h + PO_WAL_HDR + (size_t)n_recs * sizeof(po_rec));
390 14 view.len = (size_t)arena_len;
391 14 view.cap = (size_t)arena_len;
392  
393 44 ▶ for (i = 0; i < n_recs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_recs; i++) { 30 14 44
394 30 const po_rec *r = &recs[i];
395 30 const char *service = NULL;
396 30 size_t svc_len = 0;
397  
398 30 out->records++;
399 30 ▶ if (r->kind == PO_LOG) out->logs++;
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind == PO_LOG) out->logs++; 28 2 30
400 2 ▶ else if (r->kind == PO_METRIC) out->metrics++;
Branch: 1 branch
BranchTrueFalseTotal
else if (r->kind == PO_METRIC) out->metrics++; 0 2 2
401 2 ▶ else if (r->kind == PO_SPAN) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->kind == PO_SPAN) { 2 0 2
402 2 out->spans++;
403 2 ▶ if (po_rec_status(r) == 2) out->errors++;
Branch: 1 branch
BranchTrueFalseTotal
if (po_rec_status(r) == 2) out->errors++; 0 2 2
404 }
405  
406 30 ▶ if (!svc) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!svc) continue; 0 30 30
407 30 po_rec_service(r, &view, &service, &svc_len);
408 30 ▶ if (!service || !svc_len) { service = "unknown"; svc_len = 7; }
Branch: 2 branches
BranchTrueFalseTotal
if (!service || !svc_len) { service = "unknown"; svc_len = 7; } 29 1 30
if (!service || !svc_len) { service = "unknown"; svc_len = 7; } 0 29 29
409 {
410 30 SV **slot = hv_fetch(svc, service, (I32)svc_len, 1);
411 30 ▶ 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); 30 0 30
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 20 10 30
412 }
413 }
414 14 off += PO_WAL_HDR + frame_len;
415 }
416 11 free(buf);
417 11 return 1;
418 }
419  
420 /* ---- the segment snapshot ------------------------------------------------ */
421 /*
422 * Everything a read wants to know about a SEALED segment - its span, its
423 * per-kind counts, its service table, its size - is immutable from the moment
424 * the seal renames the file. Rediscovering it cost every store call one
425 * readdir, one string sort of every name, and one sidecar open PER SEGMENT -
426 * on a 758-segment store that was most of every page load, paid eight times
427 * over on the pages that make eight store calls.
428 *
429 * So the store object carries a snapshot: the sorted segment names with each
430 * one's parsed sidecar, keyed on the wal directory's mtime. Every event that
431 * changes the set - a seal (rename in, sidecar written), a new worker's live
432 * log appearing, a retention unlink - touches the directory, so a stale name
433 * list cannot outlive one stat. Appends to an existing live log do NOT touch
434 * the directory, which is why nothing about live logs is ever cached here.
435 *
436 * The one hole in mtime keying is granularity: a second seal in the same
437 * second as the snapshot leaves the key unchanged. So a snapshot whose key
438 * second is within two seconds of now is rebuilt regardless - after that
439 * window closes, any later change lands in a newer second and misses the key.
440 * (The same trick git's index uses for racily-clean entries.) Rebuilds are
441 * incremental - an entry whose name is already known is reused, sealed
442 * segments being immutable - so the racy window costs a readdir, not a
443 * re-parse.
444 *
445 * The snapshot is plain Perl data in $self->{_snap}: it dies with the object,
446 * needs no magic, and a test can read it. Per-process, per-object, no shared
447 * state - two workers each pay one build and then stat.
448 */
449  
450 enum {
451 PO_SNAP_NAME = 0, /* segment file name */
452 PO_SNAP_FLAGS, /* PO_SNAP_HAS_IDX | PO_SNAP_SPAN_SEEN */
453 PO_SNAP_TMIN, /* nanosecond instants, po_u64_to_sv */
454 PO_SNAP_TMAX,
455 PO_SNAP_RECORDS,
456 PO_SNAP_LOGS,
457 PO_SNAP_METRICS,
458 PO_SNAP_SPANS,
459 PO_SNAP_ERRORS,
460 PO_SNAP_TRACES,
461 PO_SNAP_SVC, /* HV ref: service name => record count */
462 PO_SNAP_SIZE, /* file bytes at parse time */
463 PO_SNAP_FIELDS
464 };
465 #define PO_SNAP_HAS_IDX 1
466 #define PO_SNAP_SPAN_SEEN 2
467  
468 91 static SV *po_snap_entry_new(pTHX_ const char *dir, const char *name) {
469 91 AV *e = newAV();
470 char path[PO_PATHMAX], idx[PO_PATHMAX];
471 size_t pl;
472 91 po_u64 sz = 0;
473 po_idx_nums ix;
474 91 HV *svc = newHV();
475 91 int flags = 0;
476  
477 91 memset(&ix, 0, sizeof(ix));
478 91 ▶ if (po_path_join(path, sizeof(path), dir, name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_path_join(path, sizeof(path), dir, name)) { 91 0 91
479 91 po_file_size(path, &sz);
480 91 pl = strlen(path);
481 91 ▶ if (pl > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (pl > 4) { 91 0 91
482 91 memcpy(idx, path, pl + 1);
483 91 memcpy(idx + pl - 4, ".idx", 5);
484 91 ▶ if (po_index_nums(idx, &ix, svc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_index_nums(idx, &ix, svc)) { 89 2 91
485 89 flags |= PO_SNAP_HAS_IDX;
486 89 ▶ if (ix.span_seen) flags |= PO_SNAP_SPAN_SEEN;
Branch: 1 branch
BranchTrueFalseTotal
if (ix.span_seen) flags |= PO_SNAP_SPAN_SEEN; 89 0 89
487 }
488 }
489 }
490  
491 91 av_extend(e, PO_SNAP_FIELDS - 1);
492 91 av_store(e, PO_SNAP_NAME, newSVpv(name, 0));
493 91 av_store(e, PO_SNAP_FLAGS, newSViv(flags));
494 91 av_store(e, PO_SNAP_TMIN, po_u64_to_sv(ix.t_min));
495 91 av_store(e, PO_SNAP_TMAX, po_u64_to_sv(ix.t_max));
496 91 av_store(e, PO_SNAP_RECORDS, po_u64_to_sv(ix.records));
497 91 av_store(e, PO_SNAP_LOGS, po_u64_to_sv(ix.logs));
498 91 av_store(e, PO_SNAP_METRICS, po_u64_to_sv(ix.metrics));
499 91 av_store(e, PO_SNAP_SPANS, po_u64_to_sv(ix.spans));
500 91 av_store(e, PO_SNAP_ERRORS, po_u64_to_sv(ix.errors));
501 91 av_store(e, PO_SNAP_TRACES, po_u64_to_sv(ix.traces));
502 91 av_store(e, PO_SNAP_SVC, newRV_noinc((SV *)svc));
503 91 av_store(e, PO_SNAP_SIZE, po_u64_to_sv(sz));
504 91 return newRV_noinc((SV *)e);
505 }
506  
507 /* One u64 field back out of an entry. */
508 3031 static po_u64 po_snap_u64(pTHX_ AV *e, int field) {
509 3031 SV **f = av_fetch(e, field, 0);
510 3031 po_u64 v = 0;
511 3031 ▶ if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &v);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &v); 3031 0 3031
if (f && SvOK(*f)) (void)po_sv_to_u64(aTHX_ *f, &v); 3031 0 3031
512 3031 return v;
513 }
514  
515 1498 static IV po_snap_flags(pTHX_ AV *e) {
516 1498 SV **f = av_fetch(e, PO_SNAP_FLAGS, 0);
517 1498 ▶ return (f && SvOK(*f)) ? SvIV(*f) : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (f && SvOK(*f)) ? SvIV(*f) : 0; 1498 0 1498
return (f && SvOK(*f)) ? SvIV(*f) : 0; 1498 0 1498
518 }
519  
520 /* A sidecar that counts zero of the wanted kind proves the segment holds
521 * none - but only when its counts are COMPLETE, meaning the per-kind lines
522 * add up to `records`. A sidecar from before a counter existed reads as
523 * zero, and pruning on a zero that means "unrecorded" would silently lose
524 * every record the segment holds. */
525 478 static int po_snap_kind_absent(pTHX_ AV *ea, int kind) {
526 po_u64 kl, km, ks, kc;
527 478 ▶ if (!kind || !(po_snap_flags(aTHX_ ea) & PO_SNAP_HAS_IDX)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!kind || !(po_snap_flags(aTHX_ ea) & PO_SNAP_HAS_IDX)) return 0; 413 65 478
if (!kind || !(po_snap_flags(aTHX_ ea) & PO_SNAP_HAS_IDX)) return 0; 0 413 413
528 413 kl = po_snap_u64(aTHX_ ea, PO_SNAP_LOGS);
529 413 km = po_snap_u64(aTHX_ ea, PO_SNAP_METRICS);
530 413 ks = po_snap_u64(aTHX_ ea, PO_SNAP_SPANS);
531 413 ▶ kc = kind == PO_METRIC ? km : kind == PO_LOG ? kl : ks;
Branch: 2 branches
BranchTrueFalseTotal
kc = kind == PO_METRIC ? km : kind == PO_LOG ? kl : ks; 352 61 413
kc = kind == PO_METRIC ? km : kind == PO_LOG ? kl : ks; 89 263 352
532 413 ▶ return !kc && kl + km + ks == po_snap_u64(aTHX_ ea, PO_SNAP_RECORDS);
Branch: 2 branches
BranchTrueFalseTotal
return !kc && kl + km + ks == po_snap_u64(aTHX_ ea, PO_SNAP_RECORDS); 17 396 413
return !kc && kl + km + ks == po_snap_u64(aTHX_ ea, PO_SNAP_RECORDS); 16 1 17
533 }
534  
535 /* The scan order for a limited newest-first read: SEGMENT NAMES sort by seal
536 * time, and seal time is not record time - a worker that fell behind seals
537 * old records after its neighbours sealed newer ones. So the early-stop walk
538 * goes by each segment's own t_max, newest first, and a segment whose span
539 * is unknown comes before all of them: it can hold anything, so no stop rule
540 * may fire until it has been read. */
541 typedef struct { po_u64 tmax; IV idx; } po_snap_ord;
542  
543 118 static int po_snap_ord_desc(const void *a, const void *b) {
544 118 po_u64 x = ((const po_snap_ord *)a)->tmax;
545 118 po_u64 y = ((const po_snap_ord *)b)->tmax;
546 118 ▶ return x < y ? 1 : (x > y ? -1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
return x < y ? 1 : (x > y ? -1 : 0); 46 72 118
return x < y ? 1 : (x > y ? -1 : 0); 35 11 46
547 }
548  
549 495 static HV *po_snap_get(pTHX_ SV *self, const char *dir) {
550 HV *h;
551 struct stat st;
552 495 IV mtime, now = (IV)time(NULL);
553 SV **snapp;
554 495 HV *old = NULL;
555 495 AV *old_segs = NULL;
556 HV *snap;
557 AV *segs, *wals, *names, *idxs;
558 po_dir d;
559 const char *name;
560 495 SSize_t i, n, oi = 0, on = 0;
561 495 IV orphan = 0, builds = 0;
562  
563 495 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) return NULL; 495 0 495
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) return NULL; 0 495 495
564 495 h = (HV *)SvRV(self);
565 495 ▶ if (stat(dir, &st) != 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (stat(dir, &st) != 0) return NULL; 6 489 495
566 489 mtime = (IV)st.st_mtime;
567  
568 489 snapp = hv_fetchs(h, "_snap", 0);
569 489 ▶ if (snapp && SvROK(*snapp) && SvTYPE(SvRV(*snapp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (snapp && SvROK(*snapp) && SvTYPE(SvRV(*snapp)) == SVt_PVHV) { 421 68 489
if (snapp && SvROK(*snapp) && SvTYPE(SvRV(*snapp)) == SVt_PVHV) { 421 0 421
if (snapp && SvROK(*snapp) && SvTYPE(SvRV(*snapp)) == SVt_PVHV) { 421 0 421
570 SV **k, **b;
571 421 old = (HV *)SvRV(*snapp);
572 421 k = hv_fetchs(old, "key", 0);
573 421 b = hv_fetchs(old, "builds", 0);
574 421 ▶ if (b && SvOK(*b)) builds = SvIV(*b);
Branch: 2 branches
BranchTrueFalseTotal
if (b && SvOK(*b)) builds = SvIV(*b); 421 0 421
if (b && SvOK(*b)) builds = SvIV(*b); 421 0 421
575 /* Valid iff the directory has not changed AND its last change is old
576 * enough that a same-second change is impossible. Strictly greater
577 * than two: a skewed clock makes the difference negative, and
578 * negative must rebuild. */
579 421 ▶ if (k && SvOK(*k) && SvIV(*k) == mtime && now - mtime > 2)
Branch: 4 branches
BranchTrueFalseTotal
if (k && SvOK(*k) && SvIV(*k) == mtime && now - mtime > 2) 421 0 421
if (k && SvOK(*k) && SvIV(*k) == mtime && now - mtime > 2) 421 0 421
if (k && SvOK(*k) && SvIV(*k) == mtime && now - mtime > 2) 414 7 421
if (k && SvOK(*k) && SvIV(*k) == mtime && now - mtime > 2) 19 395 414
580 19 return old;
581 {
582 402 SV **sp = hv_fetchs(old, "segs", 0);
583 402 ▶ if (sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) 402 0 402
if (sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) 402 0 402
if (sp && SvROK(*sp) && SvTYPE(SvRV(*sp)) == SVt_PVAV) 402 0 402
584 402 old_segs = (AV *)SvRV(*sp);
585 }
586 }
587  
588 470 names = newAV(); wals = newAV(); idxs = newAV();
589 470 ▶ if (!po_opendir(&d, dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_opendir(&d, dir)) { 0 470 470
590 0 SvREFCNT_dec((SV *)names); SvREFCNT_dec((SV *)wals);
591 0 SvREFCNT_dec((SV *)idxs);
592 0 return NULL;
593 }
594 2511 ▶ while ((name = po_readdir(&d))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((name = po_readdir(&d))) { 2041 470 2511
595 2041 size_t nl = strlen(name);
596 2041 ▶ if (nl <= 4) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (nl <= 4) continue; 940 1101 2041
597 1101 ▶ if (memcmp(name + nl - 4, ".seg", 4) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(name + nl - 4, ".seg", 4) == 0) 529 572 1101
598 529 av_push(names, newSVpv(name, 0));
599 572 ▶ else if (memcmp(name + nl - 4, ".wal", 4) == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (memcmp(name + nl - 4, ".wal", 4) == 0) 42 530 572
600 42 av_push(wals, newSVpv(name, 0));
601 530 ▶ else if (memcmp(name + nl - 4, ".idx", 4) == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (memcmp(name + nl - 4, ".idx", 4) == 0) 530 0 530
602 530 av_push(idxs, newSVpv(name, 0));
603 }
604 470 po_closedir(&d);
605 470 sortsv(AvARRAY(names), (SSize_t)(av_len(names) + 1), Perl_sv_cmp);
606 470 sortsv(AvARRAY(wals), (SSize_t)(av_len(wals) + 1), Perl_sv_cmp);
607  
608 /* An index with no segment is half an interrupted retention pass;
609 * counted at build so stats can report it without a stat per call. */
610 470 n = av_len(idxs) + 1;
611 1000 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 530 470 1000
612 530 SV **e = av_fetch(idxs, i, 0);
613 char path[PO_PATHMAX];
614 size_t pl;
615 530 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 530 530
616 1060 ▶ if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*e))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*e))) continue; 0 530 530
617 530 pl = strlen(path);
618 530 memcpy(path + pl - 4, ".seg", 5);
619 530 ▶ if (!po_file_size(path, NULL)) orphan++;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_file_size(path, NULL)) orphan++; 5 525 530
620 }
621 470 SvREFCNT_dec((SV *)idxs);
622  
623 /* Both name lists are sorted, so reuse is a single merge walk: a name
624 * already in the old snapshot keeps its parsed entry (the segment is
625 * immutable), a new name pays one sidecar parse, a vanished name is
626 * dropped by never being reached. */
627 470 segs = newAV();
628 470 ▶ if (old_segs) on = av_len(old_segs) + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (old_segs) on = av_len(old_segs) + 1; 402 68 470
629 470 n = av_len(names) + 1;
630 470 av_extend(segs, n - 1);
631 999 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 529 470 999
632 529 SV **e = av_fetch(names, i, 0);
633 const char *np;
634 STRLEN nl;
635 529 SV *reuse = NULL;
636 529 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 529 529
637 529 np = SvPV(*e, nl);
638 532 ▶ while (oi < on) {
Branch: 1 branch
BranchTrueFalseTotal
while (oi < on) { 441 91 532
639 441 SV **oe = av_fetch(old_segs, oi, 0);
640 AV *oa;
641 SV **onm;
642 const char *op;
643 STRLEN ol;
644 int c;
645 444 ▶ if (!oe || !SvROK(*oe)) { oi++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!oe || !SvROK(*oe)) { oi++; continue; } 441 0 441
if (!oe || !SvROK(*oe)) { oi++; continue; } 0 441 441
646 441 oa = (AV *)SvRV(*oe);
647 441 onm = av_fetch(oa, PO_SNAP_NAME, 0);
648 441 ▶ if (!onm) { oi++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!onm) { oi++; continue; } 0 441 441
649 441 op = SvPV(*onm, ol);
650 441 c = memcmp(op, np, ol < nl ? ol : nl);
651 441 ▶ if (!c) c = ol == nl ? 0 : (ol < nl ? -1 : 1);
Branch: 3 branches
BranchTrueFalseTotal
if (!c) c = ol == nl ? 0 : (ol < nl ? -1 : 1); 438 3 441
if (!c) c = ol == nl ? 0 : (ol < nl ? -1 : 1); 0 438 438
if (!c) c = ol == nl ? 0 : (ol < nl ? -1 : 1); 0 0 0
652 441 ▶ if (c < 0) { oi++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) { oi++; continue; } 3 438 441
653 438 ▶ if (c == 0) { reuse = *oe; oi++; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == 0) { reuse = *oe; oi++; } 438 0 438
654 438 break;
655 }
656 529 ▶ av_push(segs, reuse ? SvREFCNT_inc(reuse)
Branch: 1 branch
BranchTrueFalseTotal
av_push(segs, reuse ? SvREFCNT_inc(reuse) 438 91 529
657 : po_snap_entry_new(aTHX_ dir, np));
658 }
659 470 SvREFCNT_dec((SV *)names);
660  
661 /* The t_max-descending order, unknown spans first. Rebuilt each build:
662 * a sort of S integers, next to nothing beside the sidecar parses it
663 * spares. */
664 {
665 470 AV *order = newAV();
666 po_snap_ord *ord;
667 470 SSize_t nspan = 0, j;
668 470 IV unspanned = 0;
669  
670 470 n = av_len(segs) + 1;
671 470 ▶ Newx(ord, n ? n : 1, po_snap_ord);
Branch: 3 branches
BranchTrueFalseTotal
Newx(ord, n ? n : 1, po_snap_ord); 431 39 470
Newx(ord, n ? n : 1, po_snap_ord); 0 431 431
Newx(ord, n ? n : 1, po_snap_ord); 431 39 470
672 999 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 529 470 999
673 529 SV **e = av_fetch(segs, i, 0);
674 AV *ea;
675 529 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 529 0 529
if (!e || !SvROK(*e)) continue; 0 529 529
676 529 ea = (AV *)SvRV(*e);
677 529 ▶ if (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN) { 525 4 529
678 525 ord[nspan].tmax = po_snap_u64(aTHX_ ea, PO_SNAP_TMAX);
679 525 ord[nspan].idx = (IV)i;
680 525 nspan++;
681 }
682 else {
683 4 av_push(order, newSViv((IV)i));
684 4 unspanned++;
685 }
686 }
687 470 ▶ if (nspan > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (nspan > 1) 60 410 470
688 60 qsort(ord, (size_t)nspan, sizeof(po_snap_ord), po_snap_ord_desc);
689 995 ▶ for (j = 0; j < nspan; j++) av_push(order, newSViv(ord[j].idx));
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nspan; j++) av_push(order, newSViv(ord[j].idx)); 525 470 995
690 470 Safefree(ord);
691  
692 470 snap = newHV();
693 470 hv_stores(snap, "order", newRV_noinc((SV *)order));
694 470 hv_stores(snap, "unspanned", newSViv(unspanned));
695 }
696 470 hv_stores(snap, "key", newSViv(mtime));
697 470 hv_stores(snap, "built", newSViv(now));
698 470 hv_stores(snap, "builds", newSViv(builds + 1));
699 470 hv_stores(snap, "orphan", newSViv(orphan));
700 470 hv_stores(snap, "segs", newRV_noinc((SV *)segs));
701 470 hv_stores(snap, "wals", newRV_noinc((SV *)wals));
702 470 hv_stores(h, "_snap", newRV_noinc((SV *)snap));
703 470 return snap;
704 }
705  
706 37 static SV *po_index_read_sv(pTHX_ const char *path) {
707 37 size_t len = 0;
708 37 char *buf = po_slurp(path, &len);
709 HV *s;
710 HV *sev, *svc;
711 AV *edges;
712 char *p, *end;
713  
714 37 ▶ if (!buf) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) return newSV(0); 1 36 37
715 36 s = newHV();
716 36 sev = newHV(); svc = newHV(); edges = newAV();
717  
718 36 p = buf; end = buf + len;
719 404 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 368 36 404
720 368 char *nl = memchr(p, '\n', (size_t)(end - p));
721 368 char *line = p;
722 368 ▶ size_t llen = nl ? (size_t)(nl - p) : (size_t)(end - p);
Branch: 1 branch
BranchTrueFalseTotal
size_t llen = nl ? (size_t)(nl - p) : (size_t)(end - p); 368 0 368
723 char *f[8];
724 size_t fl[8];
725 368 int nf = 0;
726 368 size_t i, start = 0;
727  
728 368 ▶ p = nl ? nl + 1 : end;
Branch: 1 branch
BranchTrueFalseTotal
p = nl ? nl + 1 : end; 368 0 368
729 368 ▶ if (!llen) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!llen) continue; 0 368 368
730  
731 5117 ▶ for (i = 0; i <= llen && nf < 8; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i <= llen && nf < 8; i++) { 4749 368 5117
for (i = 0; i <= llen && nf < 8; i++) { 4749 0 4749
732 4749 ▶ if (i == llen || line[i] == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (i == llen || line[i] == '\t') { 4381 368 4749
if (i == llen || line[i] == '\t') { 472 3909 4381
733 840 f[nf] = line + start;
734 840 fl[nf] = i - start;
735 840 nf++;
736 840 start = i + 1;
737 }
738 }
739 368 ▶ if (!nf) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nf) continue; 0 368 368
740  
741 402 ▶ if (fl[0] == 3 && memcmp(f[0], "sev", 3) == 0 && nf >= 3) {
Branch: 3 branches
BranchTrueFalseTotal
if (fl[0] == 3 && memcmp(f[0], "sev", 3) == 0 && nf >= 3) { 72 296 368
if (fl[0] == 3 && memcmp(f[0], "sev", 3) == 0 && nf >= 3) { 34 38 72
if (fl[0] == 3 && memcmp(f[0], "sev", 3) == 0 && nf >= 3) { 34 0 34
742 34 SV **slot = hv_fetch(sev, f[1], (I32)fl[1], 1);
743 34 ▶ if (slot) sv_setpvn(*slot, f[2], fl[2]);
Branch: 1 branch
BranchTrueFalseTotal
if (slot) sv_setpvn(*slot, f[2], fl[2]); 34 0 34
744 }
745 372 ▶ else if (fl[0] == 3 && memcmp(f[0], "svc", 3) == 0 && nf >= 3) {
Branch: 3 branches
BranchTrueFalseTotal
else if (fl[0] == 3 && memcmp(f[0], "svc", 3) == 0 && nf >= 3) { 38 296 334
else if (fl[0] == 3 && memcmp(f[0], "svc", 3) == 0 && nf >= 3) { 38 0 38
else if (fl[0] == 3 && memcmp(f[0], "svc", 3) == 0 && nf >= 3) { 38 0 38
746 char key[512];
747 38 size_t kn = po_idx_unesc(f[1], fl[1], key, sizeof(key));
748 38 SV **slot = hv_fetch(svc, key, (I32)kn, 1);
749 38 ▶ if (slot) sv_setpvn(*slot, f[2], fl[2]);
Branch: 1 branch
BranchTrueFalseTotal
if (slot) sv_setpvn(*slot, f[2], fl[2]); 38 0 38
750 }
751 304 ▶ else if (fl[0] == 4 && memcmp(f[0], "edge", 4) == 0 && nf >= 6) {
Branch: 3 branches
BranchTrueFalseTotal
else if (fl[0] == 4 && memcmp(f[0], "edge", 4) == 0 && nf >= 6) { 44 252 296
else if (fl[0] == 4 && memcmp(f[0], "edge", 4) == 0 && nf >= 6) { 8 36 44
else if (fl[0] == 4 && memcmp(f[0], "edge", 4) == 0 && nf >= 6) { 8 0 8
752 8 HV *e = newHV();
753 char a[512], b[512];
754 8 size_t an = po_idx_unesc(f[1], fl[1], a, sizeof(a));
755 8 size_t bn = po_idx_unesc(f[2], fl[2], b, sizeof(b));
756 8 hv_stores(e, "caller", newSVpvn(a, an));
757 8 hv_stores(e, "callee", newSVpvn(b, bn));
758 8 hv_stores(e, "count", newSVpvn(f[3], fl[3]));
759 8 hv_stores(e, "errors", newSVpvn(f[4], fl[4]));
760 8 hv_stores(e, "dur_max", newSVpvn(f[5], fl[5]));
761 8 av_push(edges, newRV_noinc((SV *)e));
762 }
763 288 ▶ else if (nf >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (nf >= 2) { 288 0 288
764 288 SV **slot = hv_fetch(s, f[0], (I32)fl[0], 1);
765 288 ▶ if (slot) sv_setpvn(*slot, f[1], fl[1]);
Branch: 1 branch
BranchTrueFalseTotal
if (slot) sv_setpvn(*slot, f[1], fl[1]); 288 0 288
766 }
767 }
768 36 free(buf);
769  
770 36 hv_stores(s, "severity", newRV_noinc((SV *)sev));
771 36 hv_stores(s, "service", newRV_noinc((SV *)svc));
772 36 hv_stores(s, "edges", newRV_noinc((SV *)edges));
773 36 return newRV_noinc((SV *)s);
774 }
775  
776 #define po_idx_cat(out, s, n) sv_catpvn((out), (s), (n))
777  
778 91 static int po_index_write(pTHX_ const char *path, HV *s) {
779 static const char *KEYS[] = { "records", "t_min", "t_max", "metrics",
780 "logs", "spans", "errors", "traces" };
781 91 SV *body = sv_2mortal(newSVpvs(""));
782 int i, ok;
783  
784 819 ▶ for (i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) { 728 91 819
785 728 SV **v = hv_fetch(s, KEYS[i], (I32)strlen(KEYS[i]), 0);
786 728 po_idx_cat(body, KEYS[i], strlen(KEYS[i]));
787 728 po_idx_cat(body, "\t", 1);
788 1456 ▶ if (v && SvOK(*v)) { STRLEN l; const char *p = SvPV(*v, l);
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(*v)) { STRLEN l; const char *p = SvPV(*v, l); 728 0 728
if (v && SvOK(*v)) { STRLEN l; const char *p = SvPV(*v, l); 728 0 728
789 728 po_idx_cat(body, p, l); }
790 0 else po_idx_cat(body, "0", 1);
791 728 po_idx_cat(body, "\n", 1);
792 }
793  
794 { /* severity, numerically, so the file reads in order */
795 91 SV **h = hv_fetch(s, "severity", 8, 0);
796 91 ▶ if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) { 91 0 91
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) { 91 0 91
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) { 91 0 91
797 91 HV *sev = (HV *)SvRV(*h);
798 IV k;
799 2366 ▶ for (k = 0; k <= 24; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k <= 24; k++) { 2275 91 2366
800 char kb[8];
801 2275 int kn = snprintf(kb, sizeof(kb), "%d", (int)k);
802 2275 SV **v = hv_fetch(sev, kb, kn, 0);
803 2275 ▶ if (!v || !SvOK(*v)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(*v)) continue; 66 2209 2275
if (!v || !SvOK(*v)) continue; 0 66 66
804 66 { STRLEN l; const char *p = SvPV(*v, l);
805 66 po_idx_cat(body, "sev\t", 4);
806 66 po_idx_cat(body, kb, (size_t)kn);
807 66 po_idx_cat(body, "\t", 1);
808 66 po_idx_cat(body, p, l);
809 66 po_idx_cat(body, "\n", 1); }
810 }
811 }
812 }
813  
814 { /* services, sorted, so two runs of the same segment agree */
815 91 SV **h = hv_fetch(s, "service", 7, 0);
816 91 ▶ if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) { 91 0 91
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) { 91 0 91
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVHV) { 91 0 91
817 91 HV *svc = (HV *)SvRV(*h);
818 91 AV *keys = newAV();
819 HE *he;
820 SSize_t j, n;
821 91 hv_iterinit(svc);
822 196 ▶ while ((he = hv_iternext(svc)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(svc))) 105 91 196
823 105 av_push(keys, newSVsv(hv_iterkeysv(he)));
824 91 sortsv(AvARRAY(keys), (SSize_t)(av_len(keys) + 1), Perl_sv_cmp);
825 91 n = av_len(keys) + 1;
826 196 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 105 91 196
827 105 SV **k = av_fetch(keys, j, 0);
828 STRLEN kl;
829 const char *kp;
830 SV **v;
831 char esc[512];
832 size_t en;
833 105 ▶ if (!k) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) continue; 0 105 105
834 105 kp = SvPV(*k, kl);
835 105 v = hv_fetch(svc, kp, (I32)kl, 0);
836 105 ▶ if (!v || !SvOK(*v)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(*v)) continue; 105 0 105
if (!v || !SvOK(*v)) continue; 0 105 105
837 105 en = po_idx_esc(kp, kl, esc, sizeof(esc));
838 105 po_idx_cat(body, "svc\t", 4);
839 105 po_idx_cat(body, esc, en);
840 105 po_idx_cat(body, "\t", 1);
841 210 { STRLEN l; const char *p = SvPV(*v, l); po_idx_cat(body, p, l); }
842 105 po_idx_cat(body, "\n", 1);
843 }
844 91 SvREFCNT_dec((SV *)keys);
845 }
846 }
847  
848 { /* the graph, one edge per line */
849 91 SV **h = hv_fetch(s, "edges", 5, 0);
850 91 ▶ if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) { 91 0 91
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) { 91 0 91
if (h && SvROK(*h) && SvTYPE(SvRV(*h)) == SVt_PVAV) { 91 0 91
851 91 AV *av = (AV *)SvRV(*h);
852 91 SSize_t j, n = av_len(av) + 1;
853 124 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 33 91 124
854 33 SV **e = av_fetch(av, j, 0);
855 HV *eh;
856 static const char *EK[] = { "caller", "callee", "count",
857 "errors", "dur_max" };
858 int f;
859 33 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 33 0 33
if (!e || !SvROK(*e)) continue; 0 33 33
860 33 eh = (HV *)SvRV(*e);
861 33 po_idx_cat(body, "edge", 4);
862 198 ▶ for (f = 0; f < 5; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < 5; f++) { 165 33 198
863 165 SV **v = hv_fetch(eh, EK[f], (I32)strlen(EK[f]), 0);
864 165 po_idx_cat(body, "\t", 1);
865 330 ▶ if (v && SvOK(*v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(*v)) { 165 0 165
if (v && SvOK(*v)) { 165 0 165
866 STRLEN l;
867 165 const char *p = SvPV(*v, l);
868 165 ▶ if (f < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (f < 2) { 66 99 165
869 char esc[512];
870 66 size_t en = po_idx_esc(p, l, esc, sizeof(esc));
871 66 po_idx_cat(body, esc, en);
872 }
873 99 else po_idx_cat(body, p, l);
874 }
875 0 ▶ else po_idx_cat(body, f < 2 ? "*" : "0", 1);
Branch: 1 branch
BranchTrueFalseTotal
else po_idx_cat(body, f < 2 ? "*" : "0", 1); 0 0 0
876 }
877 33 po_idx_cat(body, "\n", 1);
878 }
879 }
880 }
881  
882 {
883 STRLEN bl;
884 91 const char *bp = SvPV(body, bl);
885 91 ok = po_atomic_write(path, bp, (size_t)bl);
886 }
887 91 return ok;
888 }
889  
890 /* ---- spans, collected straight out of the logs ----------------------------
891 *
892 * THE PERL THIS REPLACES WENT THROUGH PERL TWICE FOR NO REASON.
893 *
894 * The records are already C structs in the write-ahead log. The old path
895 * decoded each one into a Perl hash, built a second Perl hash per span to
896 * describe it, handed the array to Trace::analyse - which converted every one
897 * back into the po_span struct it started as - and then converted the results
898 * back into Perl again to filter them. Four crossings to run an analysis that
899 * is C at both ends.
900 *
901 * This fills po_span directly. Names and services are INTERNED rather than
902 * copied, which is what po_span's name_sym and service_sym were for; the
903 * table is the reverse map for the output.
904 */
905 typedef struct {
906 po_span_w w;
907 po_intern sym; /* span names and service names share one table */
908 int ok;
909 } po_span_gather;
910  
911 149 static int po_gather_init(po_span_gather *g) {
912 149 memset(g, 0, sizeof(*g));
913 149 ▶ if (!po_span_w_init(&g->w)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_span_w_init(&g->w)) return 0; 0 149 149
914 149 ▶ if (!po_intern_init(&g->sym, 256)) { po_span_w_free(&g->w); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_intern_init(&g->sym, 256)) { po_span_w_free(&g->w); return 0; } 0 149 149
915 149 g->ok = 1;
916 149 return 1;
917 }
918  
919 149 static void po_gather_free(po_span_gather *g) {
920 149 ▶ if (!g->ok) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!g->ok) return; 0 149 149
921 149 po_span_w_free(&g->w);
922 149 po_intern_free(&g->sym);
923 149 g->ok = 0;
924 }
925  
926 /* Every span in one log that the range admits. */
927 154 static void po_gather_wal(po_span_gather *g, const char *p, size_t len,
928 int have_from, po_u64 from,
929 int have_to, po_u64 to) {
930 po_wal_replay rp;
931 154 size_t off = 0;
932  
933 154 po_wal_replay_buf(p, len, &rp, NULL, NULL);
934  
935 156 for (;;) {
936 uint32_t magic, frame_len, n_recs, arena_len;
937 uint16_t flags, version;
938 const char *h;
939 const po_rec *recs;
940 po_arena view;
941 size_t i;
942  
943 310 ▶ if (len - off < PO_WAL_HDR) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len - off < PO_WAL_HDR) break; 92 218 310
944 218 h = p + off;
945 218 memcpy(&magic, h, 4); magic = po_le32(magic);
946 218 ▶ if (magic != PO_WAL_MAGIC) break;
Branch: 1 branch
BranchTrueFalseTotal
if (magic != PO_WAL_MAGIC) break; 0 218 218
947 218 memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len);
948 218 memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs);
949 218 memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len);
950 218 flags = (uint16_t)((unsigned char)h[36] | ((unsigned char)h[37] << 8));
951 218 version = (uint16_t)((unsigned char)h[38] | ((unsigned char)h[39] << 8));
952 218 ▶ if (version != PO_WAL_VERSION) break;
Branch: 1 branch
BranchTrueFalseTotal
if (version != PO_WAL_VERSION) break; 0 218 218
953  
954 /* THE SEAL TRAILER IS NOT A FRAME OF RECORDS.
955 *
956 * It is the one frame allowed to have none, and it carries the FILE'S
957 * TOTAL RECORD COUNT in the field every other frame uses for its own
958 * count - with frame_len zero. Walked as if it were data, the loop
959 * below reads that many po_rec past the end of the buffer: 88 bytes
960 * times every record the segment ever held.
961 *
962 * Every other walker in this file checks this flag. This one read the
963 * version two bytes further along and never looked at it, so a store
964 * whose segments were small enough for the overread to land in heap
965 * slack worked, and a real one took SIGBUS on the trace screen. */
966 218 ▶ if (flags & PO_WAL_F_SEALED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & PO_WAL_F_SEALED) break; 62 156 218
967  
968 156 ▶ if (len - off - PO_WAL_HDR < (size_t)frame_len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len - off - PO_WAL_HDR < (size_t)frame_len) break; 0 156 156
969 156 ▶ 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 156 156
970  
971 156 recs = (const po_rec *)(h + PO_WAL_HDR);
972 156 view.base = (char *)(h + PO_WAL_HDR + (size_t)n_recs * sizeof(po_rec));
973 156 view.len = (size_t)arena_len;
974 156 view.cap = (size_t)arena_len;
975  
976 22422 ▶ for (i = 0; i < n_recs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_recs; i++) { 22266 156 22422
977 22266 const po_rec *r = &recs[i];
978 po_span sp;
979 22266 const char *svc = NULL;
980 22266 size_t svc_len = 0;
981  
982 22266 ▶ if (r->kind != PO_SPAN) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind != PO_SPAN) continue; 6727 15539 22266
983 15539 ▶ if (have_from && r->t_unix_nano < from) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_from && r->t_unix_nano < from) continue; 3680 11859 15539
if (have_from && r->t_unix_nano < from) continue; 0 3680 3680
984 15539 ▶ if (have_to && r->t_unix_nano > to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_to && r->t_unix_nano > to) continue; 3680 11859 15539
if (have_to && r->t_unix_nano > to) continue; 0 3680 3680
985  
986 15539 memset(&sp, 0, sizeof(sp));
987 15539 sp.trace_hi = r->trace_id_hi;
988 15539 sp.trace_lo = r->trace_id_lo;
989 15539 sp.span_id = r->span_id;
990 15539 sp.parent_span_id = r->parent_span_id;
991 15539 sp.start_ns = r->t_unix_nano;
992 15539 sp.dur_ns = r->dur_nano;
993 15539 sp.kind = po_rec_span_kind(r);
994 15539 sp.status = po_rec_status(r);
995  
996 15539 po_rec_service(r, &view, &svc, &svc_len);
997 15539 ▶ if (!svc || !svc_len) { svc = "unknown"; svc_len = 7; }
Branch: 2 branches
BranchTrueFalseTotal
if (!svc || !svc_len) { svc = "unknown"; svc_len = 7; } 15539 0 15539
if (!svc || !svc_len) { svc = "unknown"; svc_len = 7; } 0 15539 15539
998 15539 sp.service_sym = po_intern_put(&g->sym, svc, svc_len);
999 31078 sp.name_sym = r->body_len
1000 15539 ? po_intern_put(&g->sym, view.base + r->body_off, r->body_len)
1001 15539 ▶ : po_intern_put(&g->sym, "", 0);
Branch: 1 branch
BranchTrueFalseTotal
: po_intern_put(&g->sym, "", 0); 15539 0 15539
1002  
1003 15539 po_span_add(&g->w, &sp);
1004 }
1005 156 off += PO_WAL_HDR + frame_len;
1006 }
1007 154 }
1008  
1009 /* A case-insensitive substring test.
1010 *
1011 * Written out rather than reached for through strcasestr, which is not in
1012 * every libc this dist builds on, and through tolower, which is
1013 * locale-dependent: in a Turkish locale tolower('I') is not 'i', and a search
1014 * box that behaves differently under one LANG is a bug nobody can reproduce.
1015 */
1016 14 static int po_substr_i(const char *hay, size_t hn, const char *needle,
1017 size_t nn) {
1018 size_t i, j;
1019 14 ▶ if (!needle || !nn) return 1; /* an empty term matches everything */
Branch: 2 branches
BranchTrueFalseTotal
if (!needle || !nn) return 1; /* an empty term matches everything */ 14 0 14
if (!needle || !nn) return 1; /* an empty term matches everything */ 0 14 14
1020 14 ▶ if (!hay || hn < nn) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!hay || hn < nn) return 0; 14 0 14
if (!hay || hn < nn) return 0; 4 10 14
1021 42 ▶ for (i = 0; i + nn <= hn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nn <= hn; i++) { 40 2 42
1022 84 ▶ for (j = 0; j < nn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nn; j++) { 76 8 84
1023 76 char a = hay[i + j], b = needle[j];
1024 76 ▶ if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); 68 8 76
if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a'); 34 34 68
1025 76 ▶ if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a'); 64 12 76
if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a'); 8 56 64
1026 76 ▶ if (a != b) break;
Branch: 1 branch
BranchTrueFalseTotal
if (a != b) break; 32 44 76
1027 }
1028 40 ▶ if (j == nn) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (j == nn) return 1; 8 32 40
1029 }
1030 2 return 0;
1031 }
1032  
1033 /* A trace identifier as the 32 hex characters everybody else spells it with.
1034 *
1035 * In Observe.xs rather than beside its XSUB, because a bare function between
1036 * two XSUBs is something xsubpp tries to parse. */
1037 905 static SV *povw_trace_hex_sv(pTHX_ SV *hi_sv, SV *lo_sv) {
1038 static const char H[] = "0123456789abcdef";
1039 905 po_u64 hi = 0, lo = 0;
1040 char out[32];
1041 int i;
1042 905 (void)po_sv_to_u64(aTHX_ hi_sv, &hi);
1043 905 (void)po_sv_to_u64(aTHX_ lo_sv, &lo);
1044 15385 ▶ for (i = 0; i < 16; i++) out[15 - i] = H[(hi >> (i * 4)) & 0xF];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) out[15 - i] = H[(hi >> (i * 4)) & 0xF]; 14480 905 15385
1045 15385 ▶ for (i = 0; i < 16; i++) out[31 - i] = H[(lo >> (i * 4)) & 0xF];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; i++) out[31 - i] = H[(lo >> (i * 4)) & 0xF]; 14480 905 15385
1046 905 return newSVpvn(out, 32);
1047 }
1048  
1049 /* The record identifier, as C. It is called from three places now - the XSUB,
1050 * record_matches and the record page - and a call_pv back into Perl for a
1051 * function that lives here would be a round trip for nothing. */
1052 1472 static SV *povw_record_id_sv(pTHX_ SV *rec) {
1053 HV *h;
1054 SV **f;
1055 1472 const char *body = "", *svc = "";
1056 1472 STRLEN bl = 0, sl = 0;
1057 char buf[1024];
1058 1472 size_t n = 0;
1059 po_h128 hash;
1060 char idb[64];
1061 int k;
1062 1472 const char *t = "0";
1063 1472 STRLEN tl = 1;
1064  
1065 1472 ▶ if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) 1472 0 1472
if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) 0 1472 1472
1066 0 croak("Punk::Observe::View::record_id: expected a record hashref");
1067 1472 h = (HV *)SvRV(rec);
1068 2944 ▶ if ((f = hv_fetchs(h, "body", 0)) && SvOK(*f)) body = SvPV(*f, bl);
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "body", 0)) && SvOK(*f)) body = SvPV(*f, bl); 1472 0 1472
if ((f = hv_fetchs(h, "body", 0)) && SvOK(*f)) body = SvPV(*f, bl); 1472 0 1472
1069 2944 ▶ if ((f = hv_fetchs(h, "service", 0)) && SvOK(*f)) svc = SvPV(*f, sl);
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "service", 0)) && SvOK(*f)) svc = SvPV(*f, sl); 1472 0 1472
if ((f = hv_fetchs(h, "service", 0)) && SvOK(*f)) svc = SvPV(*f, sl); 1472 0 1472
1070 2944 ▶ if ((f = hv_fetchs(h, "t", 0)) && SvOK(*f)) t = SvPV(*f, tl);
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0)) && SvOK(*f)) t = SvPV(*f, tl); 1472 0 1472
if ((f = hv_fetchs(h, "t", 0)) && SvOK(*f)) t = SvPV(*f, tl); 1472 0 1472
1071  
1072 /* body NUL service, exactly as the Perl hashed it - the id has to be
1073 * stable across a version of this code, not merely unique within one. */
1074 1472 ▶ if (bl + 1 + sl <= sizeof(buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (bl + 1 + sl <= sizeof(buf)) { 1472 0 1472
1075 1472 memcpy(buf, body, bl); n = bl;
1076 1472 buf[n++] = '\0';
1077 1472 memcpy(buf + n, svc, sl); n += sl;
1078 1472 hash = po_murmur3_128(buf, n, 0);
1079 }
1080 else {
1081 char *big;
1082 0 Newx(big, bl + 1 + sl, char);
1083 0 memcpy(big, body, bl); n = bl;
1084 0 big[n++] = '\0';
1085 0 memcpy(big + n, svc, sl); n += sl;
1086 0 hash = po_murmur3_128(big, n, 0);
1087 0 Safefree(big);
1088 }
1089  
1090 /* The first twelve digits of the high half, decimal, as the Perl took
1091 * them. */
1092 {
1093 char d[24];
1094 1472 size_t dn = 0;
1095 1472 po_u64 v = hash.hi;
1096 1472 ▶ if (!v) d[dn++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (!v) d[dn++] = '0'; 0 1472 1472
1097 1472 else { char w[24]; size_t z = 0;
1098 30026 ▶ while (v) { w[z++] = (char)('0' + (int)(v % 10)); v /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (v) { w[z++] = (char)('0' + (int)(v % 10)); v /= 10; } 28554 1472 30026
1099 30026 ▶ while (z) d[dn++] = w[--z]; }
Branch: 1 branch
BranchTrueFalseTotal
while (z) d[dn++] = w[--z]; } 28554 1472 30026
1100 1472 ▶ if (dn > 12) dn = 12;
Branch: 1 branch
BranchTrueFalseTotal
if (dn > 12) dn = 12; 1472 0 1472
1101 {
1102 1472 size_t tn = tl < sizeof(idb) - 14 ? (size_t)tl : sizeof(idb) - 14;
1103 1472 memcpy(idb, t, tn); k = (int)tn;
1104 1472 idb[k++] = '.';
1105 1472 memcpy(idb + k, d, dn); k += (int)dn;
1106 }
1107 }
1108 1472 return newSVpvn(idb, (STRLEN)k);
1109 }
1110  
1111 54 static int povw_record_matches_c(pTHX_ SV *rec, SV *id) {
1112 54 STRLEN il = 0, tl = 0;
1113 const char *ip, *tp;
1114 HV *h;
1115 SV **f;
1116 const char *dot;
1117 SV *mine;
1118 int eq;
1119  
1120 54 ▶ if (!SvOK(id)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(id)) return 0; 0 54 54
1121 54 ▶ if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) return 0; 54 0 54
if (!SvROK(rec) || SvTYPE(SvRV(rec)) != SVt_PVHV) return 0; 0 54 54
1122 54 ip = SvPV(id, il);
1123 54 h = (HV *)SvRV(rec);
1124 54 f = hv_fetchs(h, "t", 0);
1125 54 ▶ if (!f || !SvOK(*f)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) return 0; 54 0 54
if (!f || !SvOK(*f)) return 0; 0 54 54
1126 54 tp = SvPV(*f, tl);
1127  
1128 /* THE TIMESTAMP FIRST. It is the cheap half of the comparison and it
1129 * rejects almost everything, so the hash is only computed for a row that
1130 * could actually be the one. */
1131 54 dot = (const char *)memchr(ip, '.', (size_t)il);
1132 54 ▶ if (!dot) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!dot) return 0; 8 46 54
1133 46 ▶ if ((size_t)(dot - ip) != (size_t)tl
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(dot - ip) != (size_t)tl 46 0 46
1134 46 ▶ || memcmp(ip, tp, (size_t)tl) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(ip, tp, (size_t)tl) != 0) return 0; 40 6 46
1135  
1136 6 mine = povw_record_id_sv(aTHX_ rec);
1137 6 eq = sv_eq(mine, id) ? 1 : 0;
1138 6 SvREFCNT_dec(mine);
1139 6 return eq;
1140 }
1141  
1142 /* The chart box. The same two numbers the pages report as `width` and
1143 * `height`, so a caller reading one and drawing with the other cannot
1144 * disagree. */
1145 #define POVW_CHART_W 720
1146 #define POVW_CHART_H 220
1147  
1148 /* The `d` attribute of a polyline, as C.
1149 *
1150 * The formatting is po_svg.h's, which is hand-rolled for the reason stated
1151 * there: `%f` in a Perl-flavoured formatter reads an NV, and a path attribute
1152 * containing `1e-05` is not a path - the chart silently does not draw, on one
1153 * platform, with no error. */
1154 1 static SV *povw_path_sv(pTHX_ const double *xs, const double *ys, size_t n) {
1155 1 SV *out = newSVpvs("");
1156 size_t i;
1157 31 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 30 1 31
1158 char xb[32], yb[32];
1159 size_t xn, yn;
1160 /* M for the first point, then a space and an L for each one after it -
1161 * the shape a path parser expects and the shape the Perl produced. */
1162 30 ▶ if (i) sv_catpvs(out, " L");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(out, " L"); 29 1 30
1163 1 else sv_catpvs(out, "M");
1164 30 xn = po_fmt(xs[i], xb);
1165 30 yn = po_fmt(ys[i], yb);
1166 30 sv_catpvn(out, xb, xn);
1167 30 sv_catpvs(out, ",");
1168 30 sv_catpvn(out, yb, yn);
1169 }
1170 1 return out;
1171 }
1172  
1173 /* sprintf('%.Nf'), spelled so the formatter reads the type it is given: a
1174 * Perl-flavoured %f takes an NV, and passing a plain double to one is
1175 * undefined on the quadmath and long-double perls. */
1176 71 static SV *povw_fixed(pTHX_ double v, int places) {
1177 71 switch (places) {
1178 5 case 0: return newSVpvf("%.0" NVff, (NV)v);
1179 46 case 1: return newSVpvf("%.1" NVff, (NV)v);
1180 20 case 4: return newSVpvf("%.4" NVff, (NV)v);
1181 0 default: return newSVpvf("%" NVff, (NV)v);
1182 }
1183 }
1184  
1185 /* A symbol back as an SV, for the output. */
1186 1020 static SV *po_sym_sv(pTHX_ const po_intern *t, uint32_t id) {
1187 1020 uint32_t len = 0;
1188 1020 const char *p = po_intern_get(t, id, &len);
1189 1020 ▶ return p ? newSVpvn(p, len) : newSVpvs("unknown");
Branch: 1 branch
BranchTrueFalseTotal
return p ? newSVpvn(p, len) : newSVpvs("unknown"); 1020 0 1020
1190 }
1191  
1192 /* Every log in a store directory, gathered into one span set.
1193 *
1194 * The file list and every skip decision come from the SNAPSHOT: a sealed
1195 * segment whose whole span misses the window, or whose sidecar proves it
1196 * holds no spans at all, is never slurped. Everything inside the window
1197 * still lands in ONE set - the cross-file parent invariant the graph
1198 * comment below guards - because pruning decides which files to read, never
1199 * how their spans merge. Live logs are always read; the window filters
1200 * inside the gather. `files`/`skipped` report what happened, so a test can
1201 * prove the pruning fired rather than inferring it from timing. */
1202 47 static void po_gather_dir(pTHX_ SV *self, const char *dir, po_span_gather *g,
1203 int have_from, po_u64 from,
1204 int have_to, po_u64 to,
1205 IV *files, IV *skipped) {
1206 47 HV *snap = po_snap_get(aTHX_ self, dir);
1207 47 AV *segs = NULL, *wals = NULL;
1208 SSize_t i, n;
1209  
1210 47 ▶ if (!snap) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!snap) return; 0 47 47
1211 {
1212 47 SV **f = hv_fetchs(snap, "segs", 0);
1213 47 ▶ if (f && SvROK(*f)) segs = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 47 0 47
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 47 0 47
1214 47 f = hv_fetchs(snap, "wals", 0);
1215 47 ▶ if (f && SvROK(*f)) wals = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 47 0 47
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 47 0 47
1216 }
1217  
1218 47 ▶ n = segs ? av_len(segs) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = segs ? av_len(segs) + 1 : 0; 47 0 47
1219 103 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 56 47 103
1220 56 SV **e = av_fetch(segs, i, 0);
1221 AV *ea;
1222 SV **nm;
1223 char path[PO_PATHMAX];
1224 56 size_t blen = 0;
1225 char *bytes;
1226  
1227 61 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 56 0 56
if (!e || !SvROK(*e)) continue; 0 56 56
1228 56 ea = (AV *)SvRV(*e);
1229  
1230 56 ▶ if ((have_from || have_to)
Branch: 2 branches
BranchTrueFalseTotal
if ((have_from || have_to) 26 30 56
if ((have_from || have_to) 0 26 26
1231 30 ▶ && (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN)) {
Branch: 1 branch
BranchTrueFalseTotal
&& (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN)) { 30 0 30
1232 30 po_u64 ix_min = po_snap_u64(aTHX_ ea, PO_SNAP_TMIN);
1233 30 po_u64 ix_max = po_snap_u64(aTHX_ ea, PO_SNAP_TMAX);
1234 30 ▶ if ((have_from && ix_max < from) || (have_to && ix_min > to)) {
Branch: 4 branches
BranchTrueFalseTotal
if ((have_from && ix_max < from) || (have_to && ix_min > to)) { 30 0 30
if ((have_from && ix_max < from) || (have_to && ix_min > to)) { 28 2 30
if ((have_from && ix_max < from) || (have_to && ix_min > to)) { 28 0 28
if ((have_from && ix_max < from) || (have_to && ix_min > to)) { 0 28 28
1235 2 ▶ if (skipped) (*skipped)++;
Branch: 1 branch
BranchTrueFalseTotal
if (skipped) (*skipped)++; 2 0 2
1236 2 continue;
1237 }
1238 }
1239 54 ▶ if (po_snap_kind_absent(aTHX_ ea, PO_SPAN)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_snap_kind_absent(aTHX_ ea, PO_SPAN)) { 3 51 54
1240 3 ▶ if (skipped) (*skipped)++;
Branch: 1 branch
BranchTrueFalseTotal
if (skipped) (*skipped)++; 2 1 3
1241 3 continue;
1242 }
1243  
1244 51 nm = av_fetch(ea, PO_SNAP_NAME, 0);
1245 51 ▶ if (!nm) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) continue; 0 51 51
1246 102 ▶ if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*nm))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*nm))) continue; 0 51 51
1247 51 bytes = po_slurp(path, &blen);
1248 51 ▶ if (!bytes) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) continue; 0 51 51
1249 51 ▶ if (files) (*files)++;
Branch: 1 branch
BranchTrueFalseTotal
if (files) (*files)++; 39 12 51
1250 51 po_gather_wal(g, bytes, blen, have_from, from, have_to, to);
1251 51 free(bytes);
1252 }
1253  
1254 47 ▶ n = wals ? av_len(wals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = wals ? av_len(wals) + 1 : 0; 47 0 47
1255 47 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 47 47
1256 0 SV **e = av_fetch(wals, i, 0);
1257 char path[PO_PATHMAX];
1258 0 size_t blen = 0;
1259 char *bytes;
1260 0 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 0 0
1261 0 ▶ if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*e))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*e))) continue; 0 0 0
1262 0 bytes = po_slurp(path, &blen);
1263 0 ▶ if (!bytes) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) continue; 0 0 0
1264 0 ▶ if (files) (*files)++;
Branch: 1 branch
BranchTrueFalseTotal
if (files) (*files)++; 0 0 0
1265 0 po_gather_wal(g, bytes, blen, have_from, from, have_to, to);
1266 0 free(bytes);
1267 }
1268 }
1269  
1270 /* One edge into the merge table, keyed on the pair of NAMES.
1271 *
1272 * A sidecar holds strings and a live log holds symbols from a table private
1273 * to it, so the names are the only thing the two sources agree on. */
1274 19 static void po_edge_merge(pTHX_ HV *edges, const char *caller, size_t cl,
1275 const char *callee, size_t el,
1276 po_u64 count, po_u64 errs, po_u64 dur_max) {
1277 char key[600];
1278 19 size_t kn = 0;
1279 SV **slot;
1280 HV *e;
1281  
1282 19 ▶ if (!cl) { caller = "*"; cl = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!cl) { caller = "*"; cl = 1; } 0 19 19
1283 19 ▶ if (!el) { callee = "unknown"; el = 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!el) { callee = "unknown"; el = 7; } 0 19 19
1284 19 ▶ if (cl + el + 1 >= sizeof(key)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (cl + el + 1 >= sizeof(key)) return; 0 19 19
1285 19 memcpy(key, caller, cl); kn = cl;
1286 19 key[kn++] = '\0';
1287 19 memcpy(key + kn, callee, el); kn += el;
1288  
1289 19 slot = hv_fetch(edges, key, (I32)kn, 1);
1290 19 ▶ if (!slot) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!slot) return; 0 19 19
1291 19 ▶ if (!SvOK(*slot) || !SvROK(*slot)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(*slot) || !SvROK(*slot)) { 0 19 19
if (!SvOK(*slot) || !SvROK(*slot)) { 0 0 0
1292 19 e = newHV();
1293 19 hv_stores(e, "caller", newSVpvn(caller, cl));
1294 19 hv_stores(e, "callee", newSVpvn(callee, el));
1295 19 hv_stores(e, "count", newSVuv(0));
1296 19 hv_stores(e, "errors", newSVuv(0));
1297 19 hv_stores(e, "dur_max", newSVuv(0));
1298 19 sv_setsv(*slot, sv_2mortal(newRV_noinc((SV *)e)));
1299 }
1300 19 e = (HV *)SvRV(*slot);
1301 {
1302 19 SV **c = hv_fetchs(e, "count", 0);
1303 19 SV **r = hv_fetchs(e, "errors", 0);
1304 19 SV **m = hv_fetchs(e, "dur_max", 0);
1305 19 po_u64 cv = 0, rv = 0, mv = 0;
1306 19 ▶ if (c) (void)po_sv_to_u64(aTHX_ *c, &cv);
Branch: 1 branch
BranchTrueFalseTotal
if (c) (void)po_sv_to_u64(aTHX_ *c, &cv); 19 0 19
1307 19 ▶ if (r) (void)po_sv_to_u64(aTHX_ *r, &rv);
Branch: 1 branch
BranchTrueFalseTotal
if (r) (void)po_sv_to_u64(aTHX_ *r, &rv); 19 0 19
1308 19 ▶ if (m) (void)po_sv_to_u64(aTHX_ *m, &mv);
Branch: 1 branch
BranchTrueFalseTotal
if (m) (void)po_sv_to_u64(aTHX_ *m, &mv); 19 0 19
1309 19 ▶ if (c) sv_setsv(*c, sv_2mortal(po_u64_to_sv(cv + count)));
Branch: 1 branch
BranchTrueFalseTotal
if (c) sv_setsv(*c, sv_2mortal(po_u64_to_sv(cv + count))); 19 0 19
1310 19 ▶ if (r) sv_setsv(*r, sv_2mortal(po_u64_to_sv(rv + errs)));
Branch: 1 branch
BranchTrueFalseTotal
if (r) sv_setsv(*r, sv_2mortal(po_u64_to_sv(rv + errs))); 19 0 19
1311 /* dur_max is a MAXIMUM, not a sum: adding two maxima is a number that
1312 * describes nothing. */
1313 19 ▶ if (m && dur_max > mv) sv_setsv(*m, sv_2mortal(po_u64_to_sv(dur_max)));
Branch: 2 branches
BranchTrueFalseTotal
if (m && dur_max > mv) sv_setsv(*m, sv_2mortal(po_u64_to_sv(dur_max))); 19 0 19
if (m && dur_max > mv) sv_setsv(*m, sv_2mortal(po_u64_to_sv(dur_max))); 19 0 19
1314 }
1315 }
1316  
1317 /* A live log's edges, from the graph just built over its spans. */
1318 /* `svc` may be NULL. The service counts are per-RECORD - a log line counts
1319 * too - so they come from the sidecar or from a record walk, never from the
1320 * span set: counting them here as well as there is how `shop` reported three
1321 * records when it had two. */
1322 10 static void po_graph_merge_live(pTHX_ const po_sgraph *sg,
1323 const po_span_gather *g, HV *edges, HV *svc) {
1324 uint32_t i;
1325 29 ▶ for (i = 0; i < sg->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sg->n; i++) { 19 10 29
1326 19 uint32_t cl = 0, el = 0;
1327 38 const char *cp = (sg->e[i].caller == PO_SVC_UNKNOWN)
1328 19 ▶ ? NULL : po_intern_get(&g->sym, sg->e[i].caller, &cl);
Branch: 1 branch
BranchTrueFalseTotal
? NULL : po_intern_get(&g->sym, sg->e[i].caller, &cl); 9 10 19
1329 19 const char *ep = po_intern_get(&g->sym, sg->e[i].callee, &el);
1330 19 ▶ po_edge_merge(aTHX_ edges, cp ? cp : "*", cp ? cl : 1,
Branch: 3 branches
BranchTrueFalseTotal
po_edge_merge(aTHX_ edges, cp ? cp : "*", cp ? cl : 1, 19 0 19
po_edge_merge(aTHX_ edges, cp ? cp : "*", cp ? cl : 1, 9 10 19
po_edge_merge(aTHX_ edges, cp ? cp : "*", cp ? cl : 1, 9 10 19
1331 19 ep ? ep : "unknown", ep ? el : 7,
1332 19 ▶ sg->e[i].count, sg->e[i].errors, sg->e[i].dur_max);
Branch: 1 branch
BranchTrueFalseTotal
sg->e[i].count, sg->e[i].errors, sg->e[i].dur_max); 19 0 19
1333 }
1334 10 ▶ if (!svc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!svc) return; 10 0 10
1335 0 ▶ for (i = 0; i < g->w.n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g->w.n; i++) { 0 0 0
1336 0 uint32_t sl = 0;
1337 0 const char *sp = po_intern_get(&g->sym, g->w.s[i].service_sym, &sl);
1338 SV **slot;
1339 0 ▶ if (!sp) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sp) continue; 0 0 0
1340 0 slot = hv_fetch(svc, sp, (I32)sl, 1);
1341 0 ▶ 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); 0 0 0
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 0 0 0
1342 }
1343 }
1344  
1345 /* The store's own directory, from the object.
1346 *
1347 * `dir/tenant/wal`, built in C so the methods below can be methods rather
1348 * than Perl wrappers that exist only to compute a path and call through. */
1349 768 static size_t po_store_waldir(pTHX_ SV *self, char *out, size_t cap) {
1350 HV *h;
1351 SV **f;
1352 768 const char *dir, *tenant = "default";
1353 768 STRLEN dl = 0, tl = 7;
1354 char base[PO_PATHMAX];
1355  
1356 768 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) return 0; 768 0 768
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) return 0; 0 768 768
1357 768 h = (HV *)SvRV(self);
1358 768 f = hv_fetchs(h, "dir", 0);
1359 768 ▶ if (!f || !SvOK(*f)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) return 0; 768 0 768
if (!f || !SvOK(*f)) return 0; 0 768 768
1360 768 dir = SvPV(*f, dl);
1361 768 ▶ if (!dl) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!dl) return 0; 0 768 768
1362  
1363 768 f = hv_fetchs(h, "tenant", 0);
1364 1536 ▶ if (f && SvOK(*f)) { tenant = SvPV(*f, tl); if (!tl) { tenant = "default"; tl = 7; } }
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) { tenant = SvPV(*f, tl); if (!tl) { tenant = "default"; tl = 7; } } 768 0 768
if (f && SvOK(*f)) { tenant = SvPV(*f, tl); if (!tl) { tenant = "default"; tl = 7; } } 768 0 768
if (f && SvOK(*f)) { tenant = SvPV(*f, tl); if (!tl) { tenant = "default"; tl = 7; } } 0 768 768
1365  
1366 {
1367 char t[PO_TENANT_MAX + 1];
1368 768 ▶ if (tl > PO_TENANT_MAX) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (tl > PO_TENANT_MAX) return 0; 0 768 768
1369 768 memcpy(t, tenant, tl); t[tl] = '\0';
1370 768 ▶ if (!po_path_join(base, sizeof(base), dir, t)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(base, sizeof(base), dir, t)) return 0; 0 768 768
1371 }
1372 768 return po_path_join(out, cap, base, "wal");
1373 }
1374  
1375 /* Two small helpers the page builders share.
1376 *
1377 * HERE rather than in the .xs file: a C declaration sitting between two XSUBs
1378 * is mangled by xsubpp, and the symptom is not a compile error - it is the
1379 * XSUB after it silently not being registered, so a method that plainly
1380 * exists in the source cannot be found at run time. This dist has now paid
1381 * for that three times. */
1382 117 static void povw_set_count(pTHX_ HV *out, const char *key, HV *src,
1383 const char *from) {
1384 117 SV **f = hv_fetch(src, from, (I32)strlen(from), 0);
1385 char buf[64];
1386 117 STRLEN l = 0;
1387 216 ▶ const char *p = (f && SvOK(*f)) ? SvPV(*f, l) : "0";
Branch: 2 branches
BranchTrueFalseTotal
const char *p = (f && SvOK(*f)) ? SvPV(*f, l) : "0"; 99 18 117
const char *p = (f && SvOK(*f)) ? SvPV(*f, l) : "0"; 99 0 99
1388 size_t n;
1389 117 ▶ if (!f || !SvOK(*f)) l = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) l = 1; 99 18 117
if (!f || !SvOK(*f)) l = 1; 0 99 99
1390 117 n = po_fmt_count(p, (size_t)l, buf, sizeof(buf));
1391 117 hv_store(out, key, (I32)strlen(key), newSVpvn(buf, n), 0);
1392 117 }
1393  
1394 /* A SEAM THAT DIED SAYS SO, ON THE PAGE.
1395 *
1396 * The `alerts` and `dashboards` readers are host code called through
1397 * G_EVAL, and the exception used to be discarded: a reader whose database was
1398 * gone fell through to the same empty state as a mount with nothing
1399 * configured. Two very different problems, one blank screen, and the one that
1400 * needs fixing looks like the one that does not.
1401 *
1402 * Since 0.02 it is worse than ambiguous. An absent seam now means the
1403 * built-in configuration store, so "empty" does not even imply "not
1404 * configured" any more - it means the reader ran and found nothing, which is
1405 * exactly what a died reader is not.
1406 *
1407 * Returns 1 if there was an exception, having put it on the page.
1408 */
1409 8 static int povw_seam_died(pTHX_ HV *v, const char *what) {
1410 8 STRLEN el = 0;
1411 16 ▶ const char *ep = SvOK(ERRSV) ? SvPV(ERRSV, el) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *ep = SvOK(ERRSV) ? SvPV(ERRSV, el) : NULL; 8 0 8
const char *ep = SvOK(ERRSV) ? SvPV(ERRSV, el) : NULL; 8 0 8
const char *ep = SvOK(ERRSV) ? SvPV(ERRSV, el) : NULL; 8 0 8
1412 SV *hint;
1413  
1414 8 ▶ if (!ep || !el) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ep || !el) return 0; 8 0 8
if (!ep || !el) return 0; 7 1 8
1415  
1416 1 hv_stores(v, "error", newSVpvf("The %s source could not be read.", what));
1417  
1418 /* The exception itself, because it is the only thing that says WHY, with
1419 * the trailing newline trimmed - " at Foo.pm line 40." is worth keeping
1420 * on an operator's screen and a dangling blank line is not. */
1421 1 hint = newSVpvn(ep, el);
1422 {
1423 char *hp; STRLEN hl;
1424 1 hp = SvPV(hint, hl);
1425 2 ▶ while (hl && (hp[hl - 1] == '\n' || hp[hl - 1] == '\r'
Branch: 3 branches
BranchTrueFalseTotal
while (hl && (hp[hl - 1] == '\n' || hp[hl - 1] == '\r' 2 0 2
while (hl && (hp[hl - 1] == '\n' || hp[hl - 1] == '\r' 1 1 2
while (hl && (hp[hl - 1] == '\n' || hp[hl - 1] == '\r' 0 1 1
1426 2 ▶ || hp[hl - 1] == ' ')) hl--;
Branch: 1 branch
BranchTrueFalseTotal
|| hp[hl - 1] == ' ')) hl--; 0 1 1
1427 1 SvCUR_set(hint, hl);
1428 1 hp[hl] = '\0';
1429 }
1430 1 hv_stores(v, "hint", hint);
1431 1 return 1;
1432 }
1433  
1434 175 static void povw_set_iv(pTHX_ HV *out, const char *key, HV *src,
1435 const char *from) {
1436 175 SV **f = hv_fetch(src, from, (I32)strlen(from), 0);
1437 175 ▶ hv_store(out, key, (I32)strlen(key),
Branch: 2 branches
BranchTrueFalseTotal
hv_store(out, key, (I32)strlen(key), 163 12 175
hv_store(out, key, (I32)strlen(key), 163 0 163
1438 newSViv(f && SvOK(*f) ? SvIV(*f) : 0), 0);
1439 175 }
1440  
1441 /* The variables every page that reads a window shares: the range control and
1442 * the bounds. In Observe.xs for the reason above - a declaration between two
1443 * XSUBs silently unregisters the next one. */
1444 /* The range control's own variables, for a caller that already knows which
1445 * range it is in. Split out because the trace pages resolve the window once
1446 * and then hand it to whichever of the two of them is going to answer. */
1447 55 static void povw_range_vars(pTHX_ HV *v, SV *req, SV *range) {
1448 int n, i;
1449 /* dSP HERE, unlike inside a PPCODE body where xsubpp has already
1450 * declared it and a second one shadows the real thing. */
1451 55 dSP;
1452  
1453 55 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 55 55
1454 55 ▶ XPUSHs(req);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(req); 0 55 55
1455 55 ▶ XPUSHs(sv_2mortal(newSVsv(range)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(range))); 0 55 55
1456 55 PUTBACK;
1457 55 n = call_pv("Punk::Observe::View::range_vars", G_ARRAY);
1458 55 SPAGAIN;
1459 {
1460 SV **got;
1461 55 ▶ Newx(got, n ? n : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(got, n ? n : 1, SV *); 55 0 55
1462 605 ▶ for (i = n - 1; i >= 0; i--) got[i] = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i >= 0; i--) got[i] = SvREFCNT_inc(POPs); 550 55 605
1463 55 PUTBACK;
1464 330 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 275 55 330
1465 STRLEN kl;
1466 275 const char *kp = SvPV(got[i], kl);
1467 275 hv_store(v, kp, (I32)kl, newSVsv(got[i + 1]), 0);
1468 }
1469 605 ▶ for (i = 0; i < n; i++) SvREFCNT_dec(got[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) SvREFCNT_dec(got[i]); 550 55 605
1470 55 Safefree(got);
1471 }
1472 55 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 55 0 55
1473 55 SPAGAIN;
1474 55 }
1475  
1476 /* The window a page reads, and everything the range control needs to draw
1477 * itself for it. Returns the range name; the caller owns it. */
1478 55 static SV *povw_window_range(pTHX_ SV *req, SV **from, SV **to) {
1479 int n;
1480 55 SV *range = NULL;
1481 55 dSP;
1482  
1483 55 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 55 55
1484 55 ▶ XPUSHs(req);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(req); 0 55 55
1485 55 PUTBACK;
1486 55 n = call_pv("Punk::Observe::View::window", G_ARRAY);
1487 55 SPAGAIN;
1488 55 ▶ if (n >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 3) { 55 0 55
1489 55 range = SvREFCNT_inc(POPs);
1490 55 *to = SvREFCNT_inc(POPs);
1491 55 *from = SvREFCNT_inc(POPs);
1492 }
1493 0 else { *from = newSV(0); *to = newSV(0); range = newSVpvs("1h"); }
1494 55 PUTBACK;
1495 55 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 55 0 55
1496 55 SPAGAIN;
1497 55 return range;
1498 }
1499  
1500 85 static void povw_window_vars(pTHX_ HV *v, SV *req, SV **from, SV **to) {
1501 int n, i;
1502 85 SV *range = NULL;
1503 /* dSP HERE, unlike inside a PPCODE body where xsubpp has already
1504 * declared it and a second one shadows the real thing. */
1505 85 dSP;
1506  
1507 85 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 85 85
1508 85 ▶ XPUSHs(req);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(req); 0 85 85
1509 85 PUTBACK;
1510 85 n = call_pv("Punk::Observe::View::window", G_ARRAY);
1511 85 SPAGAIN;
1512 85 ▶ if (n >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (n >= 3) { 85 0 85
1513 85 range = SvREFCNT_inc(POPs);
1514 85 *to = SvREFCNT_inc(POPs);
1515 85 *from = SvREFCNT_inc(POPs);
1516 }
1517 0 else { *from = newSV(0); *to = newSV(0); range = newSVpvs("1h"); }
1518 85 PUTBACK;
1519 85 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 85 0 85
1520 85 SPAGAIN;
1521  
1522 85 hv_stores(v, "from", newSVsv(*from));
1523 85 hv_stores(v, "to", newSVsv(*to));
1524  
1525 85 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 85 85
1526 85 ▶ XPUSHs(req);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(req); 0 85 85
1527 85 ▶ XPUSHs(sv_2mortal(newSVsv(range)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(range))); 0 85 85
1528 85 PUTBACK;
1529 85 n = call_pv("Punk::Observe::View::range_vars", G_ARRAY);
1530 85 SPAGAIN;
1531 {
1532 SV **got;
1533 85 ▶ Newx(got, n ? n : 1, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(got, n ? n : 1, SV *); 85 0 85
1534 935 ▶ for (i = n - 1; i >= 0; i--) got[i] = SvREFCNT_inc(POPs);
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i >= 0; i--) got[i] = SvREFCNT_inc(POPs); 850 85 935
1535 85 PUTBACK;
1536 510 ▶ for (i = 0; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < n; i += 2) { 425 85 510
1537 STRLEN kl;
1538 425 const char *kp = SvPV(got[i], kl);
1539 425 hv_store(v, kp, (I32)kl, newSVsv(got[i + 1]), 0);
1540 }
1541 935 ▶ for (i = 0; i < n; i++) SvREFCNT_dec(got[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) SvREFCNT_dec(got[i]); 850 85 935
1542 85 Safefree(got);
1543 }
1544 85 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 85 0 85
1545 85 SPAGAIN;
1546 85 SvREFCNT_dec(range);
1547 85 }
1548  
1549  
1550 /* Some of the static helpers above are used only by later phases. Silence the
1551 * unused warnings without hiding a real one by taking their addresses once. */
1552 81 static void po_keep_alive(void) {
1553 (void)po_le32; (void)po_tick_ns;
1554 (void)po_pbr_done; (void)po_pbr_float;
1555 (void)po_pb_packed_begin; (void)po_pb_packed_varint; (void)po_pb_packed_double;
1556 (void)po_store_path; (void)po_block_start; (void)po_duration;
1557 (void)po_wal_replay_buf; (void)po_write_all;
1558 (void)po_hash32; (void)po_seg_overlaps; (void)po_shared_record;
1559 (void)po_intern_get; (void)po_write_all_fd;
1560 (void)po_bw_put1; (void)po_chunk_full;
1561 (void)po_labelset_add; (void)po_blockdir_init; (void)po_blockdir_free;
1562 (void)po_blockdir_add; (void)po_bloom_test; (void)po_block_full;
1563 (void)po_seg_may_match; (void)po_sgraph_add_trace;
1564 (void)po_has_trace_key; (void)po_column_ok;
1565 (void)po_expr_selective; (void)po_cmp_str;
1566 (void)po_al_state_of; /* the question a UI asks; the routes are 15b */
1567 /* Reading the key FILE (hashes, never tokens) and binding the rate window
1568 * to the shared arena both belong to the mount's boot path, which lands
1569 * with the routes in 15b. Referenced here rather than left to warn. */
1570 (void)po_keyring_add_hash; (void)po_shared_rate;
1571 (void)po_retain_account_mapped; (void)po_tier_span;
1572 (void)po_seg_region; (void)po_buf_free;
1573 (void)po_fmt_i; (void)po_svg_s; (void)po_nice_num;
1574 81 }
1575  
1576 /* Render one record for the tests. The ingest path in phase 3 never does this
1577 * - it writes the record array straight to the WAL - so this exists purely so
1578 * that t/0003-otlp-in.t can assert field by field from Perl.
1579 *
1580 * Nanosecond fields go through po_u64_to_sv, which is a UV where that is
1581 * lossless and a decimal string where it is not. Never SvIV. */
1582 29079 static SV *po_rec_hv(pTHX_ const po_rec *rec, const po_arena *ar) {
1583 29079 HV *h = newHV();
1584 29079 hv_stores(h, "kind", newSVuv((UV)rec->kind));
1585 29079 hv_stores(h, "t", po_u64_to_sv(rec->t_unix_nano));
1586 29079 hv_stores(h, "duration", po_u64_to_sv(rec->dur_nano));
1587 29079 hv_stores(h, "severity", newSVuv((UV)rec->severity));
1588 29079 hv_stores(h, "flags", newSVuv((UV)rec->flags));
1589 29079 hv_stores(h, "span_kind", newSVuv((UV)po_rec_span_kind(rec)));
1590 29079 hv_stores(h, "status", newSVuv((UV)po_rec_status(rec)));
1591 29079 hv_stores(h, "trace_hi", po_u64_to_sv(rec->trace_id_hi));
1592 29079 hv_stores(h, "trace_lo", po_u64_to_sv(rec->trace_id_lo));
1593 29079 hv_stores(h, "span_id", po_u64_to_sv(rec->span_id));
1594 29079 hv_stores(h, "parent_id", po_u64_to_sv(rec->parent_span_id));
1595  
1596 29079 ▶ if (rec->flags & PO_F_VALUE_IS_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (rec->flags & PO_F_VALUE_IS_INT) { 2 29077 29079
1597 po_u64 iv;
1598 2 memcpy(&iv, &rec->value, 8);
1599 2 hv_stores(h, "value", po_u64_to_sv(iv));
1600 2 hv_stores(h, "value_is_int", newSViv(1));
1601 }
1602 else {
1603 29077 hv_stores(h, "value", newSVnv((NV)rec->value));
1604 29077 hv_stores(h, "value_is_int", newSViv(0));
1605 }
1606  
1607 29079 ▶ if (rec->body_len)
Branch: 1 branch
BranchTrueFalseTotal
if (rec->body_len) 29077 2 29079
1608 29077 hv_stores(h, "body", newSVpvn(ar->base + rec->body_off, rec->body_len));
1609 else
1610 2 hv_stores(h, "body", newSVpvs(""));
1611  
1612 /* Decode the sorted attribute block back into a hash, so the tests can
1613 * assert content, and into an ordered list, so they can assert that the
1614 * ORDER is canonical - which is what phase 4's content-derived series id
1615 * depends on. */
1616 {
1617 29079 HV *at = newHV();
1618 29079 AV *keys = newAV();
1619 29079 ▶ if (rec->attr_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (rec->attr_len) { 17380 11699 29079
1620 17380 const unsigned char *p = (const unsigned char *)ar->base + rec->attr_off;
1621 17380 const unsigned char *e = p + rec->attr_len;
1622 17380 po_u64 n = 0; int sh = 0;
1623 17380 ▶ while (p < e) { unsigned char c = *p++; n |= (po_u64)(c & 0x7F) << sh;
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { unsigned char c = *p++; n |= (po_u64)(c & 0x7F) << sh; 17380 0 17380
1624 17380 ▶ if (!(c & 0x80)) break; sh += 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) break; sh += 7; } 17380 0 17380
1625 36184 ▶ while (n-- && p < e) {
Branch: 2 branches
BranchTrueFalseTotal
while (n-- && p < e) { 18804 17380 36184
while (n-- && p < e) { 18804 0 18804
1626 18804 po_u64 kl = 0; unsigned char tag; SV *val = NULL;
1627 18804 sh = 0;
1628 18804 ▶ while (p < e) { unsigned char c = *p++; kl |= (po_u64)(c & 0x7F) << sh;
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { unsigned char c = *p++; kl |= (po_u64)(c & 0x7F) << sh; 18804 0 18804
1629 18804 ▶ if (!(c & 0x80)) break; sh += 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) break; sh += 7; } 18804 0 18804
1630 18804 ▶ if ((po_u64)(e - p) < kl) break;
Branch: 1 branch
BranchTrueFalseTotal
if ((po_u64)(e - p) < kl) break; 0 18804 18804
1631 18804 { const char *kp = (const char *)p; size_t klen = (size_t)kl;
1632 18804 p += kl;
1633 18804 ▶ if (p >= e) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= e) break; 0 18804 18804
1634 18804 tag = *p++;
1635 18804 switch (tag) {
1636 18248 case PO_AV_STRING: case PO_AV_BYTES: case PO_AV_NESTED: {
1637 18248 po_u64 vl = 0; sh = 0;
1638 18251 ▶ while (p < e) { unsigned char c = *p++;
Branch: 1 branch
BranchTrueFalseTotal
while (p < e) { unsigned char c = *p++; 18251 0 18251
1639 18251 vl |= (po_u64)(c & 0x7F) << sh;
1640 18251 ▶ if (!(c & 0x80)) break; sh += 7; }
Branch: 1 branch
BranchTrueFalseTotal
if (!(c & 0x80)) break; sh += 7; } 18248 3 18251
1641 18248 ▶ if ((po_u64)(e - p) < vl) { p = e; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((po_u64)(e - p) < vl) { p = e; break; } 0 18248 18248
1642 18248 val = newSVpvn((const char *)p, (STRLEN)vl);
1643 18248 p += vl;
1644 18248 break;
1645 }
1646 3 case PO_AV_DOUBLE: {
1647 po_u64 bits; double d;
1648 3 ▶ if ((size_t)(e - p) < 8) { p = e; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(e - p) < 8) { p = e; break; } 0 3 3
1649 3 memcpy(&bits, p, 8); p += 8;
1650 3 bits = po_le64(bits); memcpy(&d, &bits, 8);
1651 3 val = newSVnv((NV)d);
1652 3 break;
1653 }
1654 553 default: {
1655 po_u64 v;
1656 553 ▶ if ((size_t)(e - p) < 8) { p = e; break; }
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(e - p) < 8) { p = e; break; } 0 553 553
1657 553 memcpy(&v, p, 8); p += 8;
1658 553 v = po_le64(v);
1659 /* An INT is two's complement, because OTLP declares
1660 * the attribute int64. A bool is 0 or 1 and unsigned
1661 * either way. Rendering an int unsigned turns -1 into
1662 * 18446744073709551615, which sorts above every
1663 * positive value a query could compare it with. */
1664 552 val = (tag == PO_AV_INT) ? po_i64_to_sv(v)
1665 553 ▶ : po_u64_to_sv(v);
Branch: 1 branch
BranchTrueFalseTotal
: po_u64_to_sv(v); 552 1 553
1666 553 break;
1667 }
1668 }
1669 18804 ▶ if (val) {
Branch: 1 branch
BranchTrueFalseTotal
if (val) { 18804 0 18804
1670 18804 hv_store(at, kp, (I32)klen, val, 0);
1671 18804 av_push(keys, newSVpvn(kp, klen));
1672 }
1673 }
1674 }
1675 }
1676 29079 hv_stores(h, "attrs", newRV_noinc((SV *)at));
1677 29079 hv_stores(h, "attr_order", newRV_noinc((SV *)keys));
1678 }
1679 29079 return newRV_noinc((SV *)h);
1680 }
1681  
1682 /* Sorting a run of records by time, newest first. A pair rather than two
1683 * parallel arrays so qsort can move both together. */
1684 typedef struct { po_u64 key; SV *sv; } po_sortpair;
1685  
1686 127062 static int po_sortpair_desc(const void *a, const void *b) {
1687 127062 po_u64 x = ((const po_sortpair *)a)->key;
1688 127062 po_u64 y = ((const po_sortpair *)b)->key;
1689 /* Subtraction would wrap: these are nanosecond instants, and the
1690 * difference between two of them does not fit an int. */
1691 127062 ▶ return x < y ? 1 : (x > y ? -1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
return x < y ? 1 : (x > y ? -1 : 0); 3687 123375 127062
return x < y ? 1 : (x > y ? -1 : 0); 2127 1560 3687
1692 }
1693  
1694 /* A record in the shape the EXECUTOR reads, built directly rather than by
1695 * rewriting the record hash in Perl afterwards.
1696 *
1697 * The two shapes differ in exactly two places and both are load-bearing: the
1698 * kind is a name rather than a number, and `service` is lifted out of the
1699 * attributes because every query filters on it and no query should have to
1700 * know it lives there.
1701 *
1702 * Doing it here rather than in a Perl map saves one sub call and one hash
1703 * copy per record - which on a half-million-row answer is the difference
1704 * between the scan and everything else. */
1705 28570 static SV *po_row_hv(pTHX_ const po_rec *rec, const po_arena *ar) {
1706 28570 SV *rv = po_rec_hv(aTHX_ rec, ar);
1707 28570 HV *h = (HV *)SvRV(rv);
1708 28570 const char *svc = NULL;
1709 28570 size_t svc_len = 0;
1710 const char *kind;
1711  
1712 28570 switch (rec->kind) {
1713 5643 case PO_METRIC: kind = "metric"; break;
1714 14352 case PO_SPAN: kind = "span"; break;
1715 8575 default: kind = "log"; break;
1716 }
1717 28570 hv_stores(h, "kind", newSVpv(kind, 0));
1718  
1719 28570 po_rec_service(rec, ar, &svc, &svc_len);
1720 28570 ▶ hv_stores(h, "service", (svc && svc_len) ? newSVpvn(svc, svc_len)
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(h, "service", (svc && svc_len) ? newSVpvn(svc, svc_len) 15117 13453 28570
hv_stores(h, "service", (svc && svc_len) ? newSVpvn(svc, svc_len) 15117 0 15117
1721 : newSVpvs("unknown"));
1722 28570 return rv;
1723 }
1724  
1725 /* The collected result and counters of one records scan, threaded through
1726 * the per-file helper below so the walk over files stays one loop whatever
1727 * order the caller visits them in. */
1728 typedef struct {
1729 po_sortpair *all;
1730 IV nall, call;
1731 IV scanned, files, degraded;
1732 /* Early-stop bookkeeping: `kth` is the limit-th newest key collected so
1733 * far (valid while kth_set; any append invalidates it), `dropped` says
1734 * records beyond the limit were seen and thrown away - which is exactly
1735 * what `truncated` reports. */
1736 po_u64 kth;
1737 int kth_set;
1738 int dropped;
1739 } po_records_acc;
1740  
1741 /* Case-SENSITIVE byte search, the sibling of po_bloom.h's case-folded
1742 * po_memfind. The executor compares a metric name to the row body exactly,
1743 * so the file-level superset check can be exact too - a stronger prune that
1744 * is still provably conservative. */
1745 98 static const char *po_memfind_exact(const char *hay, size_t hn,
1746 const char *needle, size_t nn) {
1747 size_t i;
1748 98 ▶ if (nn == 0 || nn > hn) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (nn == 0 || nn > hn) return NULL; 98 0 98
if (nn == 0 || nn > hn) return NULL; 0 98 98
1749 207957 ▶ for (i = 0; i + nn <= hn; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + nn <= hn; i++) 207953 4 207957
1750 207953 ▶ if (hay[i] == needle[0] && memcmp(hay + i, needle, nn) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (hay[i] == needle[0] && memcmp(hay + i, needle, nn) == 0) 222 207731 207953
if (hay[i] == needle[0] && memcmp(hay + i, needle, nn) == 0) 94 128 222
1751 94 return hay + i;
1752 4 return NULL;
1753 }
1754  
1755 /* One file's worth of the scan: slurp, replay-check, frame walk.
1756 *
1757 * The two needles are CONSERVATIVE SUPERSETS of filters the executor will
1758 * apply after the scan - `search` (matched case-folded against row bodies)
1759 * and the metric name (matched exactly against row bodies). Bodies live
1760 * verbatim in the frame arenas, so a file or frame in which the needle
1761 * appears NOWHERE cannot contain a row the executor would keep, and the
1762 * scan skips building its hashes. A hit proves nothing - the needle may sit
1763 * in an attribute, another kind's body, half of a longer name - so the
1764 * executor's own filter still decides every row. The needle only ever says
1765 * "nothing here", never "this one matches". */
1766 425 static void po_records_scan_file(pTHX_ const char *path,
1767 po_u64 from, int have_from,
1768 po_u64 to, int have_to, int kind,
1769 po_u64 trace_hi, po_u64 trace_lo,
1770 int have_trace,
1771 const po_traceset *tset, int as_rows,
1772 const char *needle_ci, size_t needle_ci_len,
1773 const char *needle_ex, size_t needle_ex_len,
1774 po_records_acc *acc) {
1775 char *bytes;
1776 425 size_t blen = 0;
1777 po_wal_replay rp;
1778 425 size_t off = 0;
1779  
1780 425 bytes = po_slurp(path, &blen);
1781 431 ▶ if (!bytes || !blen) { free(bytes); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (!bytes || !blen) { free(bytes); return; } 424 1 425
if (!bytes || !blen) { free(bytes); return; } 0 424 424
1782 424 acc->files++;
1783  
1784 424 ▶ if ((needle_ci_len && !po_memfind(bytes, blen, needle_ci, needle_ci_len))
Branch: 2 branches
BranchTrueFalseTotal
if ((needle_ci_len && !po_memfind(bytes, blen, needle_ci, needle_ci_len)) 10 414 424
if ((needle_ci_len && !po_memfind(bytes, blen, needle_ci, needle_ci_len)) 8 2 10
1785 422 ▶ || (needle_ex_len && !po_memfind_exact(bytes, blen,
Branch: 2 branches
BranchTrueFalseTotal
|| (needle_ex_len && !po_memfind_exact(bytes, blen, 48 374 422
|| (needle_ex_len && !po_memfind_exact(bytes, blen, 4 44 48
1786 needle_ex, needle_ex_len))) {
1787 6 free(bytes);
1788 6 return;
1789 }
1790  
1791 418 po_wal_replay_buf(bytes, blen, &rp, NULL, NULL);
1792 /* A log this build cannot read is REPORTED, never guessed at, and it is
1793 * not fatal: the other segments still answer and the answer says it is
1794 * short. */
1795 418 ▶ if (rp.stopped_reason == PO_REPLAY_VERSION) acc->degraded++;
Branch: 1 branch
BranchTrueFalseTotal
if (rp.stopped_reason == PO_REPLAY_VERSION) acc->degraded++; 1 417 418
1796  
1797 424 for (;;) {
1798 uint32_t magic, frame_len, n_recs, arena_len;
1799 uint16_t version, flags;
1800 const char *h;
1801 const po_rec *recs;
1802 po_arena view;
1803 size_t j;
1804  
1805 842 ▶ if (blen - off < PO_WAL_HDR) break;
Branch: 1 branch
BranchTrueFalseTotal
if (blen - off < PO_WAL_HDR) break; 33 809 842
1806 809 h = bytes + off;
1807 809 memcpy(&magic, h, 4); magic = po_le32(magic);
1808 809 ▶ if (magic != PO_WAL_MAGIC) break;
Branch: 1 branch
BranchTrueFalseTotal
if (magic != PO_WAL_MAGIC) break; 0 809 809
1809 809 memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len);
1810 809 memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs);
1811 809 memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len);
1812 809 flags = (uint16_t)((unsigned char)h[36] | ((unsigned char)h[37] << 8));
1813 809 version = (uint16_t)((unsigned char)h[38] | ((unsigned char)h[39] << 8));
1814 809 ▶ if (version != PO_WAL_VERSION) break;
Branch: 1 branch
BranchTrueFalseTotal
if (version != PO_WAL_VERSION) break; 1 808 809
1815 808 ▶ if (flags & PO_WAL_F_SEALED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & PO_WAL_F_SEALED) break; 384 424 808
1816 424 ▶ if (blen - off - PO_WAL_HDR < (size_t)frame_len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (blen - off - PO_WAL_HDR < (size_t)frame_len) break; 0 424 424
1817 424 ▶ 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 424 424
1818  
1819 424 recs = (const po_rec *)(h + PO_WAL_HDR);
1820 424 view.base = (char *)(h + PO_WAL_HDR + (size_t)n_recs * sizeof(po_rec));
1821 424 view.len = (size_t)arena_len;
1822 424 view.cap = (size_t)arena_len;
1823  
1824 /* The frame-level needle check: bodies live in this arena, so a
1825 * frame without the needle has no row worth a hash. */
1826 424 ▶ if ((needle_ci_len && !po_memfind(view.base, view.len,
Branch: 2 branches
BranchTrueFalseTotal
if ((needle_ci_len && !po_memfind(view.base, view.len, 8 416 424
if ((needle_ci_len && !po_memfind(view.base, view.len, 8 0 8
1827 needle_ci, needle_ci_len))
1828 424 ▶ || (needle_ex_len && !po_memfind_exact(view.base, view.len,
Branch: 2 branches
BranchTrueFalseTotal
|| (needle_ex_len && !po_memfind_exact(view.base, view.len, 50 374 424
|| (needle_ex_len && !po_memfind_exact(view.base, view.len, 0 50 50
1829 needle_ex, needle_ex_len))) {
1830 0 off += PO_WAL_HDR + frame_len;
1831 0 continue;
1832 }
1833  
1834 73447 ▶ for (j = 0; j < n_recs; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n_recs; j++) { 73023 424 73447
1835 73023 const po_rec *r = &recs[j];
1836 73023 acc->scanned++;
1837 73023 ▶ if (have_from && r->t_unix_nano < from) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_from && r->t_unix_nano < from) continue; 72515 508 73023
if (have_from && r->t_unix_nano < from) continue; 21368 51147 72515
1838 51655 ▶ if (have_to && r->t_unix_nano > to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_to && r->t_unix_nano > to) continue; 51123 532 51655
if (have_to && r->t_unix_nano > to) continue; 22130 28993 51123
1839 29525 ▶ if (kind && r->kind != (uint8_t)kind) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (kind && r->kind != (uint8_t)kind) continue; 29277 248 29525
if (kind && r->kind != (uint8_t)kind) continue; 710 28567 29277
1840 /* THE CROSS-SIGNAL FILTER. A record's trace id is the only thing
1841 * that ties a log line to the request that produced it, and
1842 * comparing two integers here is what keeps "the logs for this
1843 * trace" a scan rather than a second pass in Perl over
1844 * everything the window held. */
1845 28815 ▶ if (have_trace && (r->trace_id_hi != trace_hi
Branch: 2 branches
BranchTrueFalseTotal
if (have_trace && (r->trace_id_hi != trace_hi 30 28785 28815
if (have_trace && (r->trace_id_hi != trace_hi 15 15 30
1846 30 ▶ || r->trace_id_lo != trace_lo)) continue;
Branch: 1 branch
BranchTrueFalseTotal
|| r->trace_id_lo != trace_lo)) continue; 0 15 15
1847 /* THE SET FORM OF THE SAME FILTER, which is what makes a
1848 * cross-signal join one scan rather than one scan per trace. */
1849 28812 if (tset && !po_traceset_has(tset, r->trace_id_hi,
1850 16 r->trace_id_lo)) continue;
1851  
1852 28796 ▶ if (acc->nall == acc->call) {
Branch: 1 branch
BranchTrueFalseTotal
if (acc->nall == acc->call) { 390 28406 28796
1853 390 ▶ IV want = acc->call ? acc->call * 2 : 256;
Branch: 1 branch
BranchTrueFalseTotal
IV want = acc->call ? acc->call * 2 : 256; 37 353 390
1854 390 ▶ Renew(acc->all, want, po_sortpair);
Branch: 1 branch
BranchTrueFalseTotal
Renew(acc->all, want, po_sortpair); 0 390 390
1855 390 acc->call = want;
1856 }
1857 28796 acc->all[acc->nall].key = r->t_unix_nano;
1858 28570 acc->all[acc->nall].sv = as_rows ? po_row_hv(aTHX_ r, &view)
1859 28796 ▶ : po_rec_hv(aTHX_ r, &view);
Branch: 1 branch
BranchTrueFalseTotal
: po_rec_hv(aTHX_ r, &view); 28570 226 28796
1860 28796 acc->nall++;
1861 28796 acc->kth_set = 0;
1862 }
1863 424 off += PO_WAL_HDR + frame_len;
1864 }
1865 418 free(bytes);
1866 }
1867  
1868 /* One file's metric names, tallied straight into an HV - no hash per
1869 * record, which is the entire point: the metrics landing page needs the
1870 * DISTINCT NAMES in a window with counts, and building half a million row
1871 * hashes to throw away everything but the bodies was the single largest
1872 * read in the UI. Returns the number of metric records tallied. */
1873 7 static po_u64 po_metric_names_file(pTHX_ const char *path,
1874 po_u64 from, int have_from,
1875 po_u64 to, int have_to,
1876 HV *seen, IV *files, IV *degraded) {
1877 char *bytes;
1878 7 size_t blen = 0;
1879 po_wal_replay rp;
1880 7 size_t off = 0;
1881 7 po_u64 tallied = 0;
1882  
1883 7 bytes = po_slurp(path, &blen);
1884 7 ▶ if (!bytes || !blen) { free(bytes); return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!bytes || !blen) { free(bytes); return 0; } 7 0 7
if (!bytes || !blen) { free(bytes); return 0; } 0 7 7
1885 7 ▶ if (files) (*files)++;
Branch: 1 branch
BranchTrueFalseTotal
if (files) (*files)++; 7 0 7
1886  
1887 7 po_wal_replay_buf(bytes, blen, &rp, NULL, NULL);
1888 7 ▶ if (rp.stopped_reason == PO_REPLAY_VERSION && degraded) (*degraded)++;
Branch: 2 branches
BranchTrueFalseTotal
if (rp.stopped_reason == PO_REPLAY_VERSION && degraded) (*degraded)++; 0 7 7
if (rp.stopped_reason == PO_REPLAY_VERSION && degraded) (*degraded)++; 0 0 0
1889  
1890 7 for (;;) {
1891 uint32_t magic, frame_len, n_recs, arena_len;
1892 uint16_t version, flags;
1893 const char *h;
1894 const po_rec *recs;
1895 const char *arena;
1896 size_t j;
1897  
1898 14 ▶ if (blen - off < PO_WAL_HDR) break;
Branch: 1 branch
BranchTrueFalseTotal
if (blen - off < PO_WAL_HDR) break; 2 12 14
1899 12 h = bytes + off;
1900 12 memcpy(&magic, h, 4); magic = po_le32(magic);
1901 12 ▶ if (magic != PO_WAL_MAGIC) break;
Branch: 1 branch
BranchTrueFalseTotal
if (magic != PO_WAL_MAGIC) break; 0 12 12
1902 12 memcpy(&frame_len, h + 4, 4); frame_len = po_le32(frame_len);
1903 12 memcpy(&n_recs, h + 8, 4); n_recs = po_le32(n_recs);
1904 12 memcpy(&arena_len, h + 12, 4); arena_len = po_le32(arena_len);
1905 12 flags = (uint16_t)((unsigned char)h[36] | ((unsigned char)h[37] << 8));
1906 12 version = (uint16_t)((unsigned char)h[38] | ((unsigned char)h[39] << 8));
1907 12 ▶ if (version != PO_WAL_VERSION) break;
Branch: 1 branch
BranchTrueFalseTotal
if (version != PO_WAL_VERSION) break; 0 12 12
1908 12 ▶ if (flags & PO_WAL_F_SEALED) break;
Branch: 1 branch
BranchTrueFalseTotal
if (flags & PO_WAL_F_SEALED) break; 5 7 12
1909 7 ▶ if (blen - off - PO_WAL_HDR < (size_t)frame_len) break;
Branch: 1 branch
BranchTrueFalseTotal
if (blen - off - PO_WAL_HDR < (size_t)frame_len) break; 0 7 7
1910 7 ▶ 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 7 7
1911  
1912 7 recs = (const po_rec *)(h + PO_WAL_HDR);
1913 7 arena = h + PO_WAL_HDR + (size_t)n_recs * sizeof(po_rec);
1914  
1915 43 ▶ for (j = 0; j < n_recs; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n_recs; j++) { 36 7 43
1916 36 const po_rec *r = &recs[j];
1917 SV **slot;
1918 36 ▶ if (r->kind != PO_METRIC) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind != PO_METRIC) continue; 20 16 36
1919 16 ▶ if (have_from && r->t_unix_nano < from) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_from && r->t_unix_nano < from) continue; 16 0 16
if (have_from && r->t_unix_nano < from) continue; 0 16 16
1920 16 ▶ if (have_to && r->t_unix_nano > to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_to && r->t_unix_nano > to) continue; 16 0 16
if (have_to && r->t_unix_nano > to) continue; 2 14 16
1921 14 ▶ if (!r->body_len
Branch: 1 branch
BranchTrueFalseTotal
if (!r->body_len 14 0 14
1922 14 ▶ || (size_t)r->body_off + r->body_len > (size_t)arena_len)
Branch: 1 branch
BranchTrueFalseTotal
|| (size_t)r->body_off + r->body_len > (size_t)arena_len) 0 14 14
1923 0 continue;
1924 14 slot = hv_fetch(seen, arena + r->body_off, (I32)r->body_len, 1);
1925 14 ▶ 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); 14 0 14
if (slot) sv_setiv(*slot, (SvOK(*slot) ? SvIV(*slot) : 0) + 1); 7 7 14
1926 14 tallied++;
1927 }
1928 7 off += PO_WAL_HDR + frame_len;
1929 }
1930 7 free(bytes);
1931 7 return tallied;
1932 }
1933  
1934 /* Sort the collected pairs newest-first and cut the run to `limit`, so the
1935 * memory high-water stays near the limit however wide the window is, and so
1936 * the limit-th key exists for the early-stop rule to compare against. */
1937 6 static void po_records_trim(pTHX_ po_records_acc *acc, po_u64 limit) {
1938 IV i;
1939 6 ▶ if (acc->kth_set || !limit || (po_u64)acc->nall < limit) return;
Branch: 3 branches
BranchTrueFalseTotal
if (acc->kth_set || !limit || (po_u64)acc->nall < limit) return; 6 0 6
if (acc->kth_set || !limit || (po_u64)acc->nall < limit) return; 6 0 6
if (acc->kth_set || !limit || (po_u64)acc->nall < limit) return; 0 6 6
1940 6 qsort(acc->all, (size_t)acc->nall, sizeof(po_sortpair), po_sortpair_desc);
1941 6 ▶ if ((po_u64)acc->nall > limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ((po_u64)acc->nall > limit) { 1 5 6
1942 9 ▶ for (i = (IV)limit; i < acc->nall; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = (IV)limit; i < acc->nall; i++) 8 1 9
1943 8 SvREFCNT_dec(acc->all[i].sv);
1944 1 acc->nall = (IV)limit;
1945 1 acc->dropped = 1;
1946 }
1947 6 acc->kth = acc->all[acc->nall - 1].key;
1948 6 acc->kth_set = 1;
1949 }
1950  
1951 /* The scan across every segment, shared by `records` and `rows`.
1952 *
1953 * A function rather than one XSUB calling the other: a call_method back into
1954 * Perl to reach the same C is a stack frame, a mortal stack and two argument
1955 * copies to arrive where the caller already was.
1956 *
1957 * The file list and every sealed segment's span come from the SNAPSHOT, so
1958 * the per-call cost of deciding what to read is one stat - not a readdir, a
1959 * sort and a sidecar open per segment. */
1960 409 static void po_records_run(pTHX_ SV *self, po_u64 from, int have_from,
1961 po_u64 to, int have_to, int kind, po_u64 limit,
1962 int as_rows, po_u64 trace_hi, po_u64 trace_lo,
1963 int have_trace,
1964 const po_traceset *tset,
1965 const char *needle_ci, size_t needle_ci_len,
1966 const char *needle_ex, size_t needle_ex_len,
1967 AV *out, HV *meta) {
1968 char dir[PO_PATHMAX];
1969 HV *snap;
1970 409 AV *segs = NULL, *wals = NULL, *order = NULL;
1971 SSize_t i, n;
1972 409 IV skipped = 0, unspanned = 0;
1973 po_records_acc acc;
1974  
1975 409 memset(&acc, 0, sizeof(acc));
1976 409 ▶ if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_store_waldir(aTHX_ self, dir, sizeof(dir))) goto done; 0 409 409
1977 409 snap = po_snap_get(aTHX_ self, dir);
1978 409 ▶ if (!snap) goto done;
Branch: 1 branch
BranchTrueFalseTotal
if (!snap) goto done; 4 405 409
1979 {
1980 405 SV **f = hv_fetchs(snap, "segs", 0);
1981 405 ▶ if (f && SvROK(*f)) segs = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 405 0 405
if (f && SvROK(*f)) segs = (AV *)SvRV(*f); 405 0 405
1982 405 f = hv_fetchs(snap, "wals", 0);
1983 405 ▶ if (f && SvROK(*f)) wals = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 405 0 405
if (f && SvROK(*f)) wals = (AV *)SvRV(*f); 405 0 405
1984 405 f = hv_fetchs(snap, "order", 0);
1985 405 ▶ if (f && SvROK(*f)) order = (AV *)SvRV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvROK(*f)) order = (AV *)SvRV(*f); 405 0 405
if (f && SvROK(*f)) order = (AV *)SvRV(*f); 405 0 405
1986 405 f = hv_fetchs(snap, "unspanned", 0);
1987 405 ▶ if (f && SvOK(*f)) unspanned = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) unspanned = SvIV(*f); 405 0 405
if (f && SvOK(*f)) unspanned = SvIV(*f); 405 0 405
1988 }
1989  
1990 /* The live logs first, always in full: nothing about them is cached,
1991 * because an append does not touch the directory the snapshot is
1992 * keyed on - and their records are usually the newest, which fills
1993 * the limit early and lets the stop rule below fire sooner. */
1994 405 ▶ n = wals ? av_len(wals) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = wals ? av_len(wals) + 1 : 0; 405 0 405
1995 439 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 34 405 439
1996 34 SV **e = av_fetch(wals, i, 0);
1997 char path[PO_PATHMAX];
1998 34 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 34 34
1999 68 ▶ if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*e)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*e))) 0 34 34
2000 0 continue;
2001 34 po_records_scan_file(aTHX_ path, from, have_from, to, have_to,
2002 kind, trace_hi, trace_lo, have_trace,
2003 tset, as_rows, needle_ci, needle_ci_len,
2004 needle_ex, needle_ex_len, &acc);
2005 }
2006  
2007 /* The sealed segments, newest t_max first, unknown spans ahead of
2008 * everything (see po_snap_ord). Once `limit` records are in hand,
2009 * a segment whose whole span is STRICTLY older than the limit-th
2010 * newest key cannot change the answer - and neither can anything
2011 * after it in this order - so the walk stops. Strictly: a tie on
2012 * the boundary still scans. */
2013 405 ▶ n = order ? av_len(order) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = order ? av_len(order) + 1 : 0; 405 0 405
2014 878 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 478 400 878
2015 478 SV **oe = av_fetch(order, i, 0);
2016 SV **e, **nm;
2017 AV *ea;
2018 char path[PO_PATHMAX];
2019 po_u64 ix_min, ix_max;
2020 int span_seen;
2021  
2022 560 ▶ if (!oe || !segs) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!oe || !segs) continue; 478 0 478
if (!oe || !segs) continue; 0 478 478
2023 478 e = av_fetch(segs, SvIV(*oe), 0);
2024 478 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 478 0 478
if (!e || !SvROK(*e)) continue; 0 478 478
2025 478 ea = (AV *)SvRV(*e);
2026  
2027 478 span_seen = (po_snap_flags(aTHX_ ea) & PO_SNAP_SPAN_SEEN) ? 1 : 0;
2028 478 ix_min = po_snap_u64(aTHX_ ea, PO_SNAP_TMIN);
2029 478 ix_max = po_snap_u64(aTHX_ ea, PO_SNAP_TMAX);
2030  
2031 478 ▶ if (i >= unspanned && limit
Branch: 2 branches
BranchTrueFalseTotal
if (i >= unspanned && limit 477 1 478
if (i >= unspanned && limit 477 0 477
2032 477 ▶ && (po_u64)acc.nall >= limit && span_seen) {
Branch: 2 branches
BranchTrueFalseTotal
&& (po_u64)acc.nall >= limit && span_seen) { 6 471 477
&& (po_u64)acc.nall >= limit && span_seen) { 6 0 6
2033 6 po_records_trim(aTHX_ &acc, limit);
2034 6 ▶ if (ix_max < acc.kth) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix_max < acc.kth) { 5 1 6
2035 /* Truncated iff anything left behind could actually have
2036 * answered: a remaining segment passing the window has
2037 * its t_max record inside it (t_max < kth <= to at this
2038 * point), so one more matching record provably exists. */
2039 SSize_t j;
2040 10 ▶ for (j = i; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i; j < n; j++) { 9 1 10
2041 9 SV **je = av_fetch(order, j, 0);
2042 SV **je2;
2043 AV *ja;
2044 po_u64 jmin, jmax;
2045 9 ▶ if (!je) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!je) continue; 0 9 9
2046 9 je2 = av_fetch(segs, SvIV(*je), 0);
2047 9 ▶ if (!je2 || !SvROK(*je2)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!je2 || !SvROK(*je2)) continue; 9 0 9
if (!je2 || !SvROK(*je2)) continue; 0 9 9
2048 9 ja = (AV *)SvRV(*je2);
2049 9 jmin = po_snap_u64(aTHX_ ja, PO_SNAP_TMIN);
2050 9 jmax = po_snap_u64(aTHX_ ja, PO_SNAP_TMAX);
2051 9 ▶ if (have_from && jmax < from) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_from && jmax < from) continue; 6 3 9
if (have_from && jmax < from) continue; 5 1 6
2052 4 ▶ if (have_to && jmin > to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (have_to && jmin > to) continue; 0 4 4
if (have_to && jmin > to) continue; 0 0 0
2053 4 ▶ if (po_snap_kind_absent(aTHX_ ja, kind)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (po_snap_kind_absent(aTHX_ ja, kind)) continue; 0 4 4
2054 4 acc.dropped = 1;
2055 4 break;
2056 }
2057 5 break;
2058 }
2059 }
2060  
2061 /* THE CHEAP SKIP, from the snapshot rather than the log. A
2062 * segment whose span is unknown is never skipped on it. */
2063 473 ▶ if ((have_from || have_to) && span_seen) {
Branch: 3 branches
BranchTrueFalseTotal
if ((have_from || have_to) && span_seen) { 92 381 473
if ((have_from || have_to) && span_seen) { 0 92 92
if ((have_from || have_to) && span_seen) { 380 1 381
2064 380 ▶ if (have_from && ix_max < from) { skipped++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (have_from && ix_max < from) { skipped++; continue; } 380 0 380
if (have_from && ix_max < from) { skipped++; continue; } 49 331 380
2065 331 ▶ if (have_to && ix_min > to) { skipped++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (have_to && ix_min > to) { skipped++; continue; } 331 0 331
if (have_to && ix_min > to) { skipped++; continue; } 22 309 331
2066 }
2067  
2068 /* The kind skip: see po_snap_kind_absent. */
2069 402 ▶ if (po_snap_kind_absent(aTHX_ ea, kind)) { skipped++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (po_snap_kind_absent(aTHX_ ea, kind)) { skipped++; continue; } 11 391 402
2070  
2071 391 nm = av_fetch(ea, PO_SNAP_NAME, 0);
2072 391 ▶ if (!nm) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!nm) continue; 0 391 391
2073 782 ▶ if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*nm)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_path_join(path, sizeof(path), dir, SvPV_nolen(*nm))) 0 391 391
2074 0 continue;
2075 391 po_records_scan_file(aTHX_ path, from, have_from, to, have_to,
2076 kind, trace_hi, trace_lo, have_trace,
2077 tset, as_rows, needle_ci, needle_ci_len,
2078 needle_ex, needle_ex_len, &acc);
2079 }
2080  
2081 /* Newest first. qsort rather than the reverse the single-segment scan
2082 * can use, because across segments the runs interleave. */
2083 405 ▶ if (acc.nall > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (acc.nall > 1) 329 76 405
2084 329 qsort(acc.all, (size_t)acc.nall, sizeof(po_sortpair),
2085 po_sortpair_desc);
2086  
2087 29193 ▶ for (i = 0; i < acc.nall; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < acc.nall; i++) { 28788 405 29193
2088 28788 ▶ if ((po_u64)i < limit) av_push(out, acc.all[i].sv);
Branch: 1 branch
BranchTrueFalseTotal
if ((po_u64)i < limit) av_push(out, acc.all[i].sv); 27337 1451 28788
2089 1451 else SvREFCNT_dec(acc.all[i].sv);
2090 }
2091 405 ▶ hv_stores(meta, "truncated",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(meta, "truncated", 377 28 405
hv_stores(meta, "truncated", 4 373 377
2092 newSViv((po_u64)acc.nall > limit || acc.dropped ? 1 : 0));
2093 405 Safefree(acc.all);
2094  
2095 409 done:
2096 409 hv_stores(meta, "scanned", newSViv(acc.scanned));
2097 409 hv_stores(meta, "skipped", newSViv(skipped));
2098 409 hv_stores(meta, "files", newSViv(acc.files));
2099 409 hv_stores(meta, "degraded", newSViv(acc.degraded));
2100 409 ▶ if (!hv_exists(meta, "truncated", 9))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(meta, "truncated", 9)) 4 405 409
2101 4 hv_stores(meta, "truncated", newSViv(0));
2102 409 return;
2103 }
2104  
2105  
2106  
2107  
2108 /* The inverse of po_rec_hv: one record read back out of a Perl hash, with its
2109 * variable-length bytes appended to `ar`.
2110 *
2111 * THE FIELD LIST HERE IS THE WHOLE POINT. Writing only t, kind and body -
2112 * which is what the WAL surface did until the store had a reader - loses
2113 * severity, the trace and span ids, the duration, the metric value and every
2114 * attribute, and loses them SILENTLY: the append succeeds, the replay
2115 * succeeds, and the records come back with zeros in the fields a query
2116 * filters on. The two functions are inverses or the log is lossy, so a field
2117 * added to one belongs in the other on the same commit.
2118 *
2119 * Returns 0 only when the arena refuses, which is a 4GB batch. */
2120 12428 static int po_rec_from_hv(pTHX_ HV *h, po_rec *rec, po_arena *ar) {
2121 SV **f;
2122 12428 po_u64 u = 0;
2123 12428 unsigned kind = 0, status = 0;
2124  
2125 12428 po_rec_zero(rec);
2126  
2127 12428 ▶ if ((f = hv_fetchs(h, "t", 0)) && po_sv_to_u64(aTHX_ *f, &u)) rec->t_unix_nano = u;
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0)) && po_sv_to_u64(aTHX_ *f, &u)) rec->t_unix_nano = u; 12428 0 12428
if ((f = hv_fetchs(h, "t", 0)) && po_sv_to_u64(aTHX_ *f, &u)) rec->t_unix_nano = u; 12428 0 12428
2128 12428 ▶ if ((f = hv_fetchs(h, "kind", 0))) rec->kind = (uint8_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "kind", 0))) rec->kind = (uint8_t)SvUV(*f); 12428 0 12428
2129 12428 ▶ if ((f = hv_fetchs(h, "duration", 0)) && po_sv_to_u64(aTHX_ *f, &u))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "duration", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 11810 618 12428
if ((f = hv_fetchs(h, "duration", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 11810 0 11810
2130 11810 rec->dur_nano = u;
2131 12428 ▶ if ((f = hv_fetchs(h, "severity", 0))) rec->severity = (uint16_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "severity", 0))) rec->severity = (uint16_t)SvUV(*f); 12334 94 12428
2132 12428 ▶ if ((f = hv_fetchs(h, "flags", 0))) rec->flags = (uint16_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "flags", 0))) rec->flags = (uint16_t)SvUV(*f); 0 12428 12428
2133 12428 ▶ if ((f = hv_fetchs(h, "series", 0)) && po_sv_to_u64(aTHX_ *f, &u)) rec->series = u;
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "series", 0)) && po_sv_to_u64(aTHX_ *f, &u)) rec->series = u; 0 12428 12428
if ((f = hv_fetchs(h, "series", 0)) && po_sv_to_u64(aTHX_ *f, &u)) rec->series = u; 0 0 0
2134 12428 ▶ if ((f = hv_fetchs(h, "trace_hi", 0)) && po_sv_to_u64(aTHX_ *f, &u))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_hi", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 109 12428
if ((f = hv_fetchs(h, "trace_hi", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 0 12319
2135 12319 rec->trace_id_hi = u;
2136 12428 ▶ if ((f = hv_fetchs(h, "trace_lo", 0)) && po_sv_to_u64(aTHX_ *f, &u))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_lo", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 109 12428
if ((f = hv_fetchs(h, "trace_lo", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 0 12319
2137 12319 rec->trace_id_lo = u;
2138 12428 ▶ if ((f = hv_fetchs(h, "span_id", 0)) && po_sv_to_u64(aTHX_ *f, &u))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_id", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 109 12428
if ((f = hv_fetchs(h, "span_id", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 0 12319
2139 12319 rec->span_id = u;
2140 12428 ▶ if ((f = hv_fetchs(h, "parent_id", 0)) && po_sv_to_u64(aTHX_ *f, &u))
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "parent_id", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 109 12428
if ((f = hv_fetchs(h, "parent_id", 0)) && po_sv_to_u64(aTHX_ *f, &u)) 12319 0 12319
2141 12319 rec->parent_span_id = u;
2142  
2143 12428 ▶ if ((f = hv_fetchs(h, "span_kind", 0))) kind = (unsigned)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_kind", 0))) kind = (unsigned)SvUV(*f); 12167 261 12428
2144 12428 ▶ if ((f = hv_fetchs(h, "status", 0))) status = (unsigned)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "status", 0))) status = (unsigned)SvUV(*f); 12165 263 12428
2145 12428 po_rec_set_aux(rec, kind, status);
2146  
2147 /* A metric's value is a BIT PATTERN when the point is an integer, so it
2148 * goes back through the same memcpy it came out through. SvNV on a
2149 * decimal string that happens to hold 2^63 would round it. */
2150 12428 ▶ if ((f = hv_fetchs(h, "value", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "value", 0))) { 4788 7640 12428
2151 4788 ▶ if (rec->flags & PO_F_VALUE_IS_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (rec->flags & PO_F_VALUE_IS_INT) { 0 4788 4788
2152 0 po_u64 iv = 0;
2153 0 (void)po_sv_to_u64(aTHX_ *f, &iv);
2154 0 memcpy(&rec->value, &iv, 8);
2155 }
2156 4788 else rec->value = (double)SvNV(*f);
2157 }
2158  
2159 12428 ▶ if ((f = hv_fetchs(h, "body", 0)) && SvOK(*f)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "body", 0)) && SvOK(*f)) { 12428 0 12428
if ((f = hv_fetchs(h, "body", 0)) && SvOK(*f)) { 12428 0 12428
2160 STRLEN bl;
2161 12428 const char *bp = SvPV(*f, bl);
2162 12428 ▶ if (bl) {
Branch: 1 branch
BranchTrueFalseTotal
if (bl) { 12427 1 12428
2163 12427 uint32_t off = po_arena_put(ar, bp, (size_t)bl);
2164 12427 ▶ if (off == PO_ARENA_ERR) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (off == PO_ARENA_ERR) return 0; 0 12427 12427
2165 12427 rec->body_off = off;
2166 12427 rec->body_len = (uint32_t)bl;
2167 }
2168 }
2169  
2170 /* The attribute block, re-encoded through po_attrs_encode so the ORDER on
2171 * disk is canonical however the hash happened to iterate. The content-
2172 * derived series id in phase 4 depends on that order, so a block written
2173 * in hash order would give the same labels two different series ids. */
2174 12428 ▶ if ((f = hv_fetchs(h, "attrs", 0)) && SvROK(*f)
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "attrs", 0)) && SvROK(*f) 12421 7 12428
if ((f = hv_fetchs(h, "attrs", 0)) && SvROK(*f) 12421 0 12421
2175 12421 ▶ && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*f)) == SVt_PVHV) { 12421 0 12421
2176 12421 HV *a = (HV *)SvRV(*f);
2177 HE *he;
2178 po_attrs at;
2179 12421 po_attrs_init(&at);
2180 12421 hv_iterinit(a);
2181 19789 ▶ while ((he = hv_iternext(a))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(a))) { 7368 12421 19789
2182 I32 klen;
2183 7368 char *k = hv_iterkey(he, &klen);
2184 7368 SV *v = hv_iterval(a, he);
2185 po_attr *slot;
2186 7368 ▶ if (klen <= 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (klen <= 0) continue; 0 7368 7368
2187 7368 slot = po_attrs_push(&at, k, (size_t)klen);
2188 7368 ▶ if (!slot) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!slot) continue; 0 7368 7368
2189 /* The TYPE is preserved, not flattened to a string. A numeric
2190 * attribute that came back as a string would compare as one, and
2191 * `where http.response.status_code >= 500` would then be a string
2192 * comparison that puts 99 above 500. */
2193 7368 ▶ if (SvPOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(v)) { 7303 65 7368
2194 STRLEN vl;
2195 7303 slot->tag = PO_AV_STRING;
2196 7303 slot->sp = (const uint8_t *)SvPV(v, vl);
2197 7303 slot->slen = (size_t)vl;
2198 }
2199 65 ▶ else if (SvIOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(v)) { 64 1 65
2200 64 slot->tag = PO_AV_INT;
2201 /* SvUV WHERE THE SV SAYS UNSIGNED, SvIV OTHERWISE.
2202 *
2203 * On a 32-bit-IV perl anything above 2^31-1 is stored as a UV,
2204 * and SvIV of one of those comes back NEGATIVE - so 2^31 was
2205 * written as two's-complement -2147483648 and read back as
2206 * 18446744071562067968. The value survived every round trip
2207 * intact; it was simply the wrong number, which is the worst
2208 * kind of surviving. */
2209 64 ▶ slot->u = SvIsUV(v) ? (po_u64)SvUV(v) : (po_u64)SvIV(v);
Branch: 1 branch
BranchTrueFalseTotal
slot->u = SvIsUV(v) ? (po_u64)SvUV(v) : (po_u64)SvIV(v); 0 64 64
2210 }
2211 1 ▶ else if (SvNOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(v)) { 1 0 1
2212 1 slot->tag = PO_AV_DOUBLE;
2213 1 slot->d = (double)SvNV(v);
2214 }
2215 else {
2216 STRLEN vl;
2217 0 slot->tag = PO_AV_STRING;
2218 0 slot->sp = (const uint8_t *)SvPV(v, vl);
2219 0 slot->slen = (size_t)vl;
2220 }
2221 }
2222 12421 ▶ if (at.n) {
Branch: 1 branch
BranchTrueFalseTotal
if (at.n) { 6844 5577 12421
2223 6844 uint32_t alen = 0;
2224 6844 uint32_t off = po_attrs_encode(&at, ar, &alen);
2225 6844 ▶ if (off == PO_ARENA_ERR) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (off == PO_ARENA_ERR) return 0; 0 6844 6844
2226 6844 rec->attr_off = off;
2227 6844 rec->attr_len = alen;
2228 }
2229 }
2230 12428 return 1;
2231 }
2232  
2233 /* Load span specs from Perl. Lives here rather than between XSUBs, because
2234 * xsubpp treats a bare function declaration in an .xs fragment as something
2235 * to parse rather than pass through. */
2236 34 static int pot_load(pTHX_ SV *specs, po_span_w *w) {
2237 AV *av;
2238 SSize_t i, n;
2239 34 ▶ if (!SvROK(specs) || SvTYPE(SvRV(specs)) != SVt_PVAV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(specs) || SvTYPE(SvRV(specs)) != SVt_PVAV) return 0; 34 0 34
if (!SvROK(specs) || SvTYPE(SvRV(specs)) != SVt_PVAV) return 0; 0 34 34
2240 34 av = (AV *)SvRV(specs);
2241 34 n = av_len(av) + 1;
2242 7036 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7002 34 7036
2243 7002 SV **e = av_fetch(av, i, 0);
2244 HV *h; SV **f;
2245 po_span sp;
2246 7002 po_u64 endv = 0;
2247 7002 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 7002 0 7002
if (!e || !SvROK(*e)) continue; 0 7002 7002
2248 7002 h = (HV *)SvRV(*e);
2249 7002 memset(&sp, 0, sizeof(sp));
2250 7002 ▶ if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.trace_hi);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.trace_hi); 7002 0 7002
2251 7002 ▶ if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.trace_lo);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.trace_lo); 7002 0 7002
2252 7002 ▶ if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.span_id);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.span_id); 7002 0 7002
2253 7002 ▶ if ((f = hv_fetchs(h, "parent", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.parent_span_id);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "parent", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.parent_span_id); 7002 0 7002
2254 7002 ▶ if ((f = hv_fetchs(h, "start", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.start_ns);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "start", 0))) (void)po_sv_to_u64(aTHX_ *f, &sp.start_ns); 7002 0 7002
2255 7002 ▶ if ((f = hv_fetchs(h, "end", 0))) (void)po_sv_to_u64(aTHX_ *f, &endv);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "end", 0))) (void)po_sv_to_u64(aTHX_ *f, &endv); 7002 0 7002
2256 7002 ▶ if ((f = hv_fetchs(h, "service", 0))) sp.service_sym = (uint32_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "service", 0))) sp.service_sym = (uint32_t)SvUV(*f); 7002 0 7002
2257 7002 ▶ if ((f = hv_fetchs(h, "name", 0))) sp.name_sym = (uint32_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "name", 0))) sp.name_sym = (uint32_t)SvUV(*f); 43 6959 7002
2258 7002 ▶ if ((f = hv_fetchs(h, "kind", 0))) sp.kind = (uint8_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "kind", 0))) sp.kind = (uint8_t)SvUV(*f); 10 6992 7002
2259 7002 ▶ if ((f = hv_fetchs(h, "status", 0))) sp.status = (uint8_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "status", 0))) sp.status = (uint8_t)SvUV(*f); 141 6861 7002
2260  
2261 /* end < start means the clock stepped; clamp and flag, exactly as
2262 * phase 0's rule requires. In a u64 the subtraction is ~1.8e19. */
2263 7002 ▶ if (endv >= sp.start_ns) sp.dur_ns = endv - sp.start_ns;
Branch: 1 branch
BranchTrueFalseTotal
if (endv >= sp.start_ns) sp.dur_ns = endv - sp.start_ns; 7001 1 7002
2264 1 else { sp.dur_ns = 0; sp.flags |= PO_SP_CLAMPED; }
2265 7002 ▶ if (sp.status == PO_ST_ERROR) sp.flags |= PO_SP_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (sp.status == PO_ST_ERROR) sp.flags |= PO_SP_ERROR; 16 6986 7002
2266  
2267 7002 ▶ if (!po_span_add(w, &sp)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_span_add(w, &sp)) return 0; 0 7002 7002
2268 }
2269 34 return 1;
2270 }
2271  
2272  
2273 /* Render an expression node for the tests. Canonical and parenthesised, so a
2274 * precedence assertion reads as a string comparison rather than a tree walk. */
2275 68 static const char *poq_agg_name(int a) {
2276 68 switch (a) {
2277 50 case PO_AGG_COUNT: return "count";
2278 1 case PO_AGG_SUM: return "sum";
2279 1 case PO_AGG_AVG: return "avg";
2280 1 case PO_AGG_MIN: return "min";
2281 1 case PO_AGG_MAX: return "max";
2282 1 case PO_AGG_P50: return "p50";
2283 1 case PO_AGG_P90: return "p90";
2284 6 case PO_AGG_P95: return "p95";
2285 3 case PO_AGG_P99: return "p99";
2286 3 case PO_AGG_DISTINCT: return "distinct";
2287 0 default: return "?";
2288 }
2289 }
2290  
2291 95 static const char *poq_op_name(int op) {
2292 95 switch (op) {
2293 36 case PO_OP_EQ: return "="; case PO_OP_NE: return "!=";
2294 4 case PO_OP_LT: return "<"; case PO_OP_LE: return "<=";
2295 50 case PO_OP_GT: return ">"; case PO_OP_GE: return ">=";
2296 5 case PO_OP_MATCH: return "=~"; case PO_OP_NMATCH: return "!~";
2297 0 default: return "?";
2298 }
2299 }
2300  
2301 106 static void poq_expr_str(pTHX_ const po_expr *e, SV *out) {
2302 106 ▶ if (!e) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return; 0 106 106
2303 106 switch (e->kind) {
2304 7 case PO_E_AND:
2305 7 sv_catpvs(out, "(");
2306 7 poq_expr_str(aTHX_ e->a, out);
2307 7 sv_catpvs(out, " and ");
2308 7 poq_expr_str(aTHX_ e->b, out);
2309 7 sv_catpvs(out, ")");
2310 7 break;
2311 3 case PO_E_OR:
2312 3 sv_catpvs(out, "(");
2313 3 poq_expr_str(aTHX_ e->a, out);
2314 3 sv_catpvs(out, " or ");
2315 3 poq_expr_str(aTHX_ e->b, out);
2316 3 sv_catpvs(out, ")");
2317 3 break;
2318 1 case PO_E_NOT:
2319 1 sv_catpvs(out, "(not ");
2320 1 poq_expr_str(aTHX_ e->a, out);
2321 1 sv_catpvs(out, ")");
2322 1 break;
2323 95 case PO_E_CMP:
2324 95 sv_catpvn(out, e->field, e->field_len);
2325 95 sv_catpvs(out, " ");
2326 95 sv_catpv(out, poq_op_name(e->op));
2327 95 sv_catpvs(out, " ");
2328 95 switch (e->vkind) {
2329 23 case PO_V_STRING:
2330 23 sv_catpvs(out, "\"");
2331 23 sv_catpvn(out, e->sval, e->sval_len);
2332 23 sv_catpvs(out, "\"");
2333 23 break;
2334 24 case PO_V_DURATION: {
2335 24 SV *d = po_u64_to_sv(e->uval);
2336 24 sv_catsv(out, d);
2337 24 sv_catpvs(out, "ns");
2338 24 SvREFCNT_dec(d);
2339 24 break;
2340 }
2341 23 case PO_V_SEVERITY: {
2342 23 SV *d = po_u64_to_sv(e->uval);
2343 23 sv_catpvs(out, "sev:");
2344 23 sv_catsv(out, d);
2345 23 SvREFCNT_dec(d);
2346 23 break;
2347 }
2348 25 default: {
2349 25 SV *d = newSVnv((NV)e->nval);
2350 25 sv_catsv(out, d);
2351 25 SvREFCNT_dec(d);
2352 25 break;
2353 }
2354 }
2355 95 break;
2356 0 default: break;
2357 }
2358 }
2359  
2360 85 static SV *poq_expr_sv(pTHX_ const po_expr *e) {
2361 85 SV *out = newSVpvs("");
2362 85 poq_expr_str(aTHX_ e, out);
2363 85 return out;
2364 }
2365  
2366  
2367 /* The log volume histogram, asked for from Perl.
2368 *
2369 * The figure itself is built in Punk::Observe::Plot, and called rather than
2370 * reimplemented here: it runs a second query, picks a bucket width from the
2371 * window and names the severity bands, none of which is faster in C and all
2372 * of which would then exist twice.
2373 *
2374 * The window comes back out of the page hash rather than being passed in.
2375 * povw_window_vars has already put it there for the template, and the
2376 * caller's own references are released before the shape of the answer is
2377 * known.
2378 *
2379 * G_EVAL because this is an ADORNMENT. A page whose chart could not be built
2380 * still has its table, and a store that refused the extra query - too many
2381 * buckets, most likely - must not take the screen down with it.
2382 */
2383 22 static void povw_add_figure(pTHX_ HV *v, const char *fn, SV *store,
2384 const char *q, STRLEN ql, const char *key) {
2385 22 SV **fromp = hv_fetchs(v, "from", 0);
2386 22 SV **top = hv_fetchs(v, "to", 0);
2387 22 SV *out = NULL;
2388 int n;
2389 22 dSP;
2390  
2391 /* An UNDEF endpoint is passed on rather than treated as absent. `all` is
2392 * a real range with no bounds, and the figure works out its own span. */
2393 22 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 22 22
2394 22 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 22 22
2395 22 ▶ XPUSHs(sv_2mortal(newSVpvn(q, ql)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(q, ql))); 0 22 22
2396 22 ▶ XPUSHs(fromp ? sv_2mortal(newSVsv(*fromp)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(fromp ? sv_2mortal(newSVsv(*fromp)) : &PL_sv_undef); 0 22 22
XPUSHs(fromp ? sv_2mortal(newSVsv(*fromp)) : &PL_sv_undef); 22 0 22
2397 22 ▶ XPUSHs(top ? sv_2mortal(newSVsv(*top)) : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(top ? sv_2mortal(newSVsv(*top)) : &PL_sv_undef); 0 22 22
XPUSHs(top ? sv_2mortal(newSVsv(*top)) : &PL_sv_undef); 22 0 22
2398 22 PUTBACK;
2399 22 n = call_pv(fn, G_SCALAR | G_EVAL);
2400 22 SPAGAIN;
2401 22 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 22 0 22
2402 22 SV *r = POPs;
2403 22 ▶ if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r);
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 22 0 22
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 22 0 22
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 15 7 22
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 15 0 15
2404 }
2405 22 PUTBACK;
2406 22 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 22 0 22
2407 22 ▶ if (out) hv_store(v, key, (I32)strlen(key), out, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (out) hv_store(v, key, (I32)strlen(key), out, 0); 15 7 22
2408 22 }
2409  
2410 /* The same thing for a figure built from one structure rather than from a
2411 * query: the caller has the data already and only needs it drawn. */
2412 2 static void povw_add_figure_sv(pTHX_ HV *v, const char *fn, SV *arg,
2413 const char *key) {
2414 2 SV *out = NULL;
2415 int n;
2416 2 dSP;
2417  
2418 2 ▶ if (!arg || !SvOK(arg)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!arg || !SvOK(arg)) return; 2 0 2
if (!arg || !SvOK(arg)) return; 0 2 2
2419  
2420 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
2421 2 ▶ XPUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(arg); 0 2 2
2422 2 PUTBACK;
2423 2 n = call_pv(fn, G_SCALAR | G_EVAL);
2424 2 SPAGAIN;
2425 2 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 2 0 2
2426 2 SV *r = POPs;
2427 2 ▶ if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r);
Branch: 4 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 2 0 2
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 2 0 2
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 2 0 2
if (!SvTRUE(ERRSV) && SvOK(r) && SvCUR(r)) out = newSVsv(r); 0 2 2
2428 }
2429 2 PUTBACK;
2430 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2 0 2
2431  
2432 2 ▶ if (out) hv_store(v, key, (I32)strlen(key), out, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (out) hv_store(v, key, (I32)strlen(key), out, 0); 0 2 2
2433 }
2434  
2435 /* Hand the page's variables to Perl and let it fill in several at once.
2436 *
2437 * The single-value helpers above return one string for one key, which is all
2438 * a figure needs. A shape that contributes a figure AND the table under it
2439 * would need two calls returning two types, so the hash goes across instead
2440 * and the Perl side sets what it likes. */
2441 /* Does this query group by severity?
2442 *
2443 * IT IS THE QUERY THAT SAYS SO, AND NOTHING ELSE CAN. A series keyed 13 is a
2444 * severity or an attempt count depending only on what was grouped by, and the
2445 * result does not carry that - so guessing from the values would rename a
2446 * `by attempt` grouping of 1, 2, 3 into trace and debug.
2447 *
2448 * The executor returns the NUMBER on purpose, and t/0903 asserts it: a query
2449 * compares severities numerically, and `severity >= error` has to keep
2450 * meaning that. The name is for the person reading the page, so it is put on
2451 * at the point of reading rather than in the engine.
2452 */
2453 16 static int povw_by_severity(const char *q, size_t n) {
2454 size_t i;
2455 16 ▶ if (!q) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!q) return 0; 0 16 16
2456 302 ▶ for (i = 0; i + 2 < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < n; i++) { 290 12 302
2457 size_t j;
2458 290 ▶ if ((q[i] != 'b' && q[i] != 'B') || (q[i+1] != 'y' && q[i+1] != 'Y'))
Branch: 4 branches
BranchTrueFalseTotal
if ((q[i] != 'b' && q[i] != 'B') || (q[i+1] != 'y' && q[i+1] != 'Y')) 273 17 290
if ((q[i] != 'b' && q[i] != 'B') || (q[i+1] != 'y' && q[i+1] != 'Y')) 0 273 273
if ((q[i] != 'b' && q[i] != 'B') || (q[i+1] != 'y' && q[i+1] != 'Y')) 12 5 17
if ((q[i] != 'b' && q[i] != 'B') || (q[i+1] != 'y' && q[i+1] != 'Y')) 12 0 12
2459 285 continue;
2460 5 ▶ if (i && !isspace((unsigned char)q[i-1]) && q[i-1] != '|') continue;
Branch: 3 branches
BranchTrueFalseTotal
if (i && !isspace((unsigned char)q[i-1]) && q[i-1] != '|') continue; 5 0 5
if (i && !isspace((unsigned char)q[i-1]) && q[i-1] != '|') continue; 0 5 5
if (i && !isspace((unsigned char)q[i-1]) && q[i-1] != '|') continue; 0 0 0
2461 5 ▶ if (!isspace((unsigned char)q[i+2])) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!isspace((unsigned char)q[i+2])) continue; 0 5 5
2462 5 j = i + 3;
2463 5 ▶ while (j < n && isspace((unsigned char)q[j])) j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < n && isspace((unsigned char)q[j])) j++; 5 0 5
while (j < n && isspace((unsigned char)q[j])) j++; 0 5 5
2464 5 ▶ if (n - j >= 8 && memcmp(q + j, "severity", 8) == 0
Branch: 2 branches
BranchTrueFalseTotal
if (n - j >= 8 && memcmp(q + j, "severity", 8) == 0 4 1 5
if (n - j >= 8 && memcmp(q + j, "severity", 8) == 0 4 0 4
2465 4 ▶ && (n - j == 8 || !isalnum((unsigned char)q[j+8]))) return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& (n - j == 8 || !isalnum((unsigned char)q[j+8]))) return 1; 0 4 4
&& (n - j == 8 || !isalnum((unsigned char)q[j+8]))) return 1; 0 0 0
2466 }
2467 12 return 0;
2468 }
2469  
2470 /* Rename a bucketed result's series keys from severity numbers to names.
2471 *
2472 * ON THE RESULT, BEFORE ANYTHING IS BUILT FROM IT, so the chart legend and
2473 * the table under it get the same labels from one place. Renaming the table
2474 * afterwards fixed the column and left the legend reading 13, 17, 5, 9 - two
2475 * renderings of one answer disagreeing about what the answer says.
2476 *
2477 * Only a key that is entirely digits is touched, and only when the caller has
2478 * already established that the query grouped by severity. */
2479 4 static void povw_name_severities(pTHX_ SV *res) {
2480 SV **se;
2481 AV *sa;
2482 SSize_t i, n;
2483  
2484 4 ▶ if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return; 4 0 4
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return; 4 0 4
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return; 0 4 4
2485 4 se = hv_fetchs((HV *)SvRV(res), "series", 0);
2486 4 ▶ if (!se || !SvROK(*se) || SvTYPE(SvRV(*se)) != SVt_PVAV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!se || !SvROK(*se) || SvTYPE(SvRV(*se)) != SVt_PVAV) return; 4 0 4
if (!se || !SvROK(*se) || SvTYPE(SvRV(*se)) != SVt_PVAV) return; 4 0 4
if (!se || !SvROK(*se) || SvTYPE(SvRV(*se)) != SVt_PVAV) return; 0 4 4
2487  
2488 4 sa = (AV *)SvRV(*se);
2489 4 n = av_len(sa) + 1;
2490 10 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 4 10
2491 6 SV **e = av_fetch(sa, i, 0);
2492 SV **k;
2493 const char *p;
2494 STRLEN l, j;
2495 6 int digits = 1;
2496  
2497 8 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 6 0 6
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 6 0 6
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 0 6 6
2498 6 k = hv_fetchs((HV *)SvRV(*e), "key", 0);
2499 6 ▶ if (!k || !SvOK(*k)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!k || !SvOK(*k)) continue; 6 0 6
if (!k || !SvOK(*k)) continue; 0 6 6
2500 6 p = SvPV(*k, l);
2501 6 ▶ if (!l) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) continue; 0 6 6
2502 12 ▶ for (j = 0; j < l; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < l; j++) 8 4 12
2503 8 ▶ if (p[j] < '0' || p[j] > '9') { digits = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[j] < '0' || p[j] > '9') { digits = 0; break; } 8 0 8
if (p[j] < '0' || p[j] > '9') { digits = 0; break; } 2 6 8
2504 6 ▶ if (!digits) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!digits) continue; 2 4 6
2505  
2506 4 (void)hv_stores((HV *)SvRV(*e), "key",
2507 newSVpv(po_severity_name(atoi(p)), 0));
2508 }
2509 }
2510  
2511 16 static void povw_fill_vars(pTHX_ HV *v, const char *fn, SV *arg) {
2512 16 dSP;
2513 16 ▶ if (!arg || !SvOK(arg)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!arg || !SvOK(arg)) return; 16 0 16
if (!arg || !SvOK(arg)) return; 0 16 16
2514  
2515 16 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 16 16
2516 16 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)v)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)v))); 0 16 16
2517 16 ▶ XPUSHs(arg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(arg); 0 16 16
2518 16 PUTBACK;
2519 16 (void)call_pv(fn, G_VOID | G_DISCARD | G_EVAL);
2520 16 SPAGAIN;
2521 16 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 16 0 16
2522 }
2523  
2524 /* `| viz` IN THE QUERY ITSELF, applied at the page boundary.
2525 *
2526 * One function because the rule is one vocabulary: a viz that draws in the
2527 * explorer and not in the logs page's box is the panel/explorer split all
2528 * over again. The result carries `viz` when the query did, and the rules are
2529 * the panel renderer's exactly. A kind the shape cannot take is refused with
2530 * the stage that fixes it; `bar` over a plain grouped answer draws the bars
2531 * figure the metrics page already uses, because one number per group IS a
2532 * bar chart; `table` is what a rows or groups answer already is.
2533 *
2534 * This also owns the bucketed fill, viz or no viz - a bucketed answer that
2535 * reaches a page and draws nothing is a heading over an empty panel, which
2536 * is how the explorer's own buckets gap was found. */
2537 38 static void povw_apply_viz(pTHX_ HV *v, HV *r, SV *res,
2538 const char *q, size_t ql,
2539 int rows_shape, int buckets_shape)
2540 {
2541 38 dSP;
2542 38 SV **vz = hv_fetchs(r, "viz", 0);
2543 38 ▶ if (vz && SvOK(*vz)) {
Branch: 2 branches
BranchTrueFalseTotal
if (vz && SvOK(*vz)) { 9 29 38
if (vz && SvOK(*vz)) { 9 0 9
2544 9 const char *vv = SvPV_nolen(*vz);
2545 9 ▶ int is_groups = !rows_shape && !buckets_shape;
Branch: 2 branches
BranchTrueFalseTotal
int is_groups = !rows_shape && !buckets_shape; 9 0 9
int is_groups = !rows_shape && !buckets_shape; 7 2 9
2546 9 ▶ if (buckets_shape) {
Branch: 1 branch
BranchTrueFalseTotal
if (buckets_shape) { 2 7 9
2547 2 hv_stores(v, "viz", newSVsv(*vz));
2548 }
2549 10 ▶ else if (is_groups && strEQ(vv, "bar")) {
Branch: 2 branches
BranchTrueFalseTotal
else if (is_groups && strEQ(vv, "bar")) { 7 0 7
else if (is_groups && strEQ(vv, "bar")) { 3 4 7
2550 3 SV **gv2 = hv_fetchs(r, "groups", 0);
2551 3 ▶ if (gv2 && SvROK(*gv2)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gv2 && SvROK(*gv2)) { 3 0 3
if (gv2 && SvROK(*gv2)) { 3 0 3
2552 3 SV *fig = NULL, *enc = NULL;
2553 int n2;
2554 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
2555 3 ▶ XPUSHs(*gv2);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*gv2); 0 3 3
2556 3 PUTBACK;
2557 3 n2 = call_pv("Punk::Observe::Plot::bars", G_SCALAR);
2558 3 SPAGAIN;
2559 3 ▶ fig = n2 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
fig = n2 ? SvREFCNT_inc(POPs) : NULL; 3 0 3
2560 3 ▶ PUTBACK; FREETMPS; LEAVE; SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; SPAGAIN; 3 0 3
2561 3 ▶ if (fig) {
Branch: 1 branch
BranchTrueFalseTotal
if (fig) { 3 0 3
2562 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
2563 3 ▶ XPUSHs(sv_2mortal(fig));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(fig)); 0 3 3
2564 3 PUTBACK;
2565 3 n2 = call_pv("Punk::Observe::Plot::encode", G_SCALAR);
2566 3 SPAGAIN;
2567 3 ▶ enc = n2 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
enc = n2 ? SvREFCNT_inc(POPs) : NULL; 3 0 3
2568 3 ▶ PUTBACK; FREETMPS; LEAVE; SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; SPAGAIN; 3 0 3
2569 3 ▶ if (enc) hv_stores(v, "series_plot", enc);
Branch: 1 branch
BranchTrueFalseTotal
if (enc) hv_stores(v, "series_plot", enc); 3 0 3
2570 }
2571 }
2572 }
2573 4 ▶ else if (strEQ(vv, "table")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(vv, "table")) { 2 2 4
2574 /* The table is what these shapes already are. */
2575 }
2576 else {
2577 2 hv_stores(v, "error",
2578 newSVpvf("%s needs a bucketed answer", vv));
2579 2 hv_stores(v, "hint",
2580 newSVpvs("add | bucket(30s) before the "
2581 "aggregate to get one row per window"));
2582 }
2583 }
2584  
2585 38 ▶ if (buckets_shape) {
Branch: 1 branch
BranchTrueFalseTotal
if (buckets_shape) { 6 32 38
2586 /* Renamed on the RESULT, so the chart legend and the table under it
2587 * agree about what the answer says. */
2588 6 ▶ if (povw_by_severity(q, ql))
Branch: 1 branch
BranchTrueFalseTotal
if (povw_by_severity(q, ql)) 2 4 6
2589 2 povw_name_severities(aTHX_ res);
2590 6 povw_fill_vars(aTHX_ v, "Punk::Observe::Plot::bucket_vars", res);
2591 }
2592 38 }
2593  
2594 /* A BUCKETED RESULT IS RESHAPED ONCE, HERE, AT THE BOUNDARY.
2595 *
2596 * The executor treats the bucket as the first component of the group key,
2597 * which is what lets every aggregate - percentiles included - compose with it
2598 * without knowing it exists. What a caller wants back is the other shape: one
2599 * entry per series, each carrying its points in time order.
2600 *
2601 * So the flat groups are split at the '\x1e' the key builder put in, sorted by
2602 * series and then by bucket, and walked once. Sorting rather than hashing
2603 * because the points have to come out in time order anyway - a chart handed
2604 * unordered points draws a line that goes backwards - and a sort gives that
2605 * and the grouping together. */
2606 typedef struct {
2607 const char *skey; size_t sklen; /* the `by` part, borrowed from the group */
2608 po_u64 idx; /* which bucket */
2609 double value;
2610 po_u64 count;
2611 } po_bpoint;
2612  
2613 3459 static int po_bpoint_cmp(const void *va, const void *vb) {
2614 3459 const po_bpoint *a = (const po_bpoint *)va, *b = (const po_bpoint *)vb;
2615 3459 size_t n = a->sklen < b->sklen ? a->sklen : b->sklen;
2616 3459 ▶ int c = n ? memcmp(a->skey, b->skey, n) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int c = n ? memcmp(a->skey, b->skey, n) : 0; 2762 697 3459
2617 3459 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 1124 2335 3459
2618 2335 ▶ if (a->sklen != b->sklen) return a->sklen < b->sklen ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->sklen != b->sklen) return a->sklen < b->sklen ? -1 : 1; 351 1984 2335
if (a->sklen != b->sklen) return a->sklen < b->sklen ? -1 : 1; 295 56 351
2619 1984 ▶ if (a->idx != b->idx) return a->idx < b->idx ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->idx != b->idx) return a->idx < b->idx ? -1 : 1; 1984 0 1984
if (a->idx != b->idx) return a->idx < b->idx ? -1 : 1; 227 1757 1984
2620 0 return 0;
2621 }
2622  
2623 254 static SV *po_buckets_sv(pTHX_ const po_result *res) {
2624 254 AV *series = newAV();
2625 po_bpoint *pt;
2626 254 uint32_t i, n = res->ng;
2627  
2628 254 ▶ if (!n) return newRV_noinc((SV *)series);
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return newRV_noinc((SV *)series); 44 210 254
2629 210 pt = (po_bpoint *)malloc((size_t)n * sizeof(po_bpoint));
2630 210 ▶ if (!pt) return newRV_noinc((SV *)series);
Branch: 1 branch
BranchTrueFalseTotal
if (!pt) return newRV_noinc((SV *)series); 0 210 210
2631  
2632 1645 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1435 210 1645
2633 1435 const char *k = res->g[i].key;
2634 1435 size_t kl = res->g[i].key_len, j = 0;
2635 1435 po_u64 idx = 0;
2636 /* The digits up to the separator. A key with no separator cannot
2637 * happen on a bucketed result, and is treated as bucket 0 of an
2638 * unnamed series rather than read past its end. */
2639 11116 ▶ while (j < kl && k[j] >= '0' && k[j] <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (j < kl && k[j] >= '0' && k[j] <= '9') 11116 0 11116
while (j < kl && k[j] >= '0' && k[j] <= '9') 9681 1435 11116
while (j < kl && k[j] >= '0' && k[j] <= '9') 9681 0 9681
2640 9681 idx = idx * 10 + (po_u64)(k[j++] - '0');
2641 1435 ▶ if (j < kl && k[j] == '\x1e') j++;
Branch: 2 branches
BranchTrueFalseTotal
if (j < kl && k[j] == '\x1e') j++; 1435 0 1435
if (j < kl && k[j] == '\x1e') j++; 1435 0 1435
2642 1435 pt[i].skey = k + j;
2643 1435 pt[i].sklen = kl - j;
2644 1435 pt[i].idx = idx;
2645 1435 pt[i].value = res->g[i].value;
2646 1435 pt[i].count = res->g[i].count;
2647 }
2648 210 qsort(pt, n, sizeof(po_bpoint), po_bpoint_cmp);
2649  
2650 210 i = 0;
2651 592 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 382 210 592
2652 382 HV *h = newHV();
2653 382 AV *points = newAV();
2654 382 uint32_t j = i;
2655 382 hv_stores(h, "key", newSVpvn(pt[i].skey, pt[i].sklen));
2656 1989 ▶ while (j < n && pt[j].sklen == pt[i].sklen
Branch: 1 branch
BranchTrueFalseTotal
while (j < n && pt[j].sklen == pt[i].sklen 1438 169 1607
2657 3255 ▶ && (pt[j].sklen == 0
Branch: 2 branches
BranchTrueFalseTotal
&& (pt[j].sklen == 0 1607 210 1817
&& (pt[j].sklen == 0 208 1230 1438
2658 1230 ▶ || memcmp(pt[j].skey, pt[i].skey, pt[j].sklen) == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(pt[j].skey, pt[i].skey, pt[j].sklen) == 0)) { 1227 3 1230
2659 1435 AV *p = newAV();
2660 /* The instant, not the index: a caller should never have to
2661 * multiply by a width it was told separately to know when a
2662 * point happened. */
2663 1435 av_push(p, po_u64_to_sv(pt[j].idx * res->bucket_ns));
2664 1435 av_push(p, newSVnv((NV)pt[j].value));
2665 1435 av_push(p, po_u64_to_sv(pt[j].count));
2666 1435 av_push(points, newRV_noinc((SV *)p));
2667 1435 j++;
2668 }
2669 382 hv_stores(h, "points", newRV_noinc((SV *)points));
2670 382 av_push(series, newRV_noinc((SV *)h));
2671 382 i = j;
2672 }
2673  
2674 210 free(pt);
2675 210 return newRV_noinc((SV *)series);
2676 }
2677  
2678 /* Load rows from Perl into po_row.
2679 *
2680 * The strings BORROW from the SVs, so the SVs must outlive the query - which
2681 * is why `keep` holds a reference to every one for the duration. Copying
2682 * every body would make a scan over a million rows a million allocations, and
2683 * borrowing is exactly what the segment-backed scan will do. */
2684 499 static IV poe_load_rows(pTHX_ SV *rows, po_row **out, SV ***keep) {
2685 AV *av;
2686 SSize_t i, n;
2687 po_row *rv;
2688 SV **kp;
2689  
2690 499 ▶ if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) return -1; 499 0 499
if (!SvROK(rows) || SvTYPE(SvRV(rows)) != SVt_PVAV) return -1; 0 499 499
2691 499 av = (AV *)SvRV(rows);
2692 499 n = av_len(av) + 1;
2693 499 ▶ Newxz(rv, n ? n : 1, po_row);
Branch: 3 branches
BranchTrueFalseTotal
Newxz(rv, n ? n : 1, po_row); 445 54 499
Newxz(rv, n ? n : 1, po_row); 0 445 445
Newxz(rv, n ? n : 1, po_row); 445 54 499
2694 499 ▶ Newxz(kp, n ? n : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newxz(kp, n ? n : 1, SV *); 445 54 499
Newxz(kp, n ? n : 1, SV *); 0 445 445
Newxz(kp, n ? n : 1, SV *); 445 54 499
2695  
2696 90958 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 90459 499 90958
2697 90459 SV **e = av_fetch(av, i, 0);
2698 HV *h; SV **f;
2699 90459 po_row *r = &rv[i];
2700 90459 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 90459 0 90459
if (!e || !SvROK(*e)) continue; 0 90459 90459
2701 90459 h = (HV *)SvRV(*e);
2702 90459 kp[i] = *e;
2703  
2704 90459 r->kind = PO_LOG;
2705 90459 ▶ if ((f = hv_fetchs(h, "kind", 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "kind", 0))) { 90459 0 90459
2706 90459 STRLEN kl; const char *k = SvPV(*f, kl);
2707 90459 ▶ if (kl == 6 && memcmp(k, "metric", 6) == 0) r->kind = PO_METRIC;
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 6 && memcmp(k, "metric", 6) == 0) r->kind = PO_METRIC; 5649 84810 90459
if (kl == 6 && memcmp(k, "metric", 6) == 0) r->kind = PO_METRIC; 5649 0 5649
2708 84810 ▶ else if (kl == 4 && memcmp(k, "span", 4) == 0) r->kind = PO_SPAN;
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memcmp(k, "span", 4) == 0) r->kind = PO_SPAN; 18571 66239 84810
else if (kl == 4 && memcmp(k, "span", 4) == 0) r->kind = PO_SPAN; 18571 0 18571
2709 66239 else r->kind = PO_LOG;
2710 }
2711 90459 ▶ if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->t);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->t); 90459 0 90459
2712 90459 ▶ if ((f = hv_fetchs(h, "duration", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->duration);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "duration", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->duration); 31732 58727 90459
2713 90459 ▶ if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->trace_hi);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->trace_hi); 28772 61687 90459
2714 90459 ▶ if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->trace_lo);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->trace_lo); 28772 61687 90459
2715 90459 ▶ if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->span_id);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &r->span_id); 26940 63519 90459
2716 90459 ▶ if ((f = hv_fetchs(h, "severity", 0))) r->severity = (uint16_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "severity", 0))) r->severity = (uint16_t)SvUV(*f); 68145 22314 90459
2717 90459 ▶ if ((f = hv_fetchs(h, "status", 0))) r->status = (uint8_t)SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "status", 0))) r->status = (uint8_t)SvUV(*f); 26940 63519 90459
2718 /* THE ROW'S `kind` AND THE QUERY'S `kind` ARE DIFFERENT THINGS.
2719 *
2720 * The row hash uses `kind` for which SIGNAL this is - metric, log,
2721 * span - and `span_kind` for OTLP's server/client/internal. The query
2722 * language's `kind` column is the second one, declared for the trace
2723 * and span sources.
2724 *
2725 * This loader read the first and never the second, so span_kind was
2726 * zero on every row and `where kind == 3` matched nothing at all -
2727 * silently, because a column with no value compares false rather
2728 * than complaining. */
2729 90459 ▶ if ((f = hv_fetchs(h, "span_kind", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_kind", 0))) 26940 63519 90459
2730 26940 r->span_kind = (uint8_t)SvUV(*f);
2731 90459 ▶ if ((f = hv_fetchs(h, "value", 0))) r->value = SvNV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "value", 0))) r->value = SvNV(*f); 26948 63511 90459
2732 179958 ▶ if ((f = hv_fetchs(h, "body", 0))) r->body = SvPV(*f, r->body_len);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "body", 0))) r->body = SvPV(*f, r->body_len); 89499 960 90459
2733 180918 ▶ if ((f = hv_fetchs(h, "service", 0))) r->service = SvPV(*f, r->service_len);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "service", 0))) r->service = SvPV(*f, r->service_len); 90459 0 90459
2734 90459 ▶ if ((f = hv_fetchs(h, "attrs", 0)) && SvROK(*f)
Branch: 2 branches
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "attrs", 0)) && SvROK(*f) 40215 50244 90459
if ((f = hv_fetchs(h, "attrs", 0)) && SvROK(*f) 40215 0 40215
2735 40215 ▶ && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*f)) == SVt_PVHV) { 40215 0 40215
2736 40215 HV *a = (HV *)SvRV(*f);
2737 HE *he;
2738 40215 hv_iterinit(a);
2739 71172 ▶ while ((he = hv_iternext(a)) && r->nattr < PO_ROW_ATTRS) {
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(a)) && r->nattr < PO_ROW_ATTRS) { 30957 40215 71172
while ((he = hv_iternext(a)) && r->nattr < PO_ROW_ATTRS) { 30957 0 30957
2740 I32 klen;
2741 30957 char *k = hv_iterkey(he, &klen);
2742 30957 SV *v = hv_iterval(a, he);
2743 30957 r->attr[r->nattr].key = k;
2744 30957 r->attr[r->nattr].key_len = (size_t)klen;
2745 30957 r->attr[r->nattr].val = SvPV(v, r->attr[r->nattr].val_len);
2746 30957 r->nattr++;
2747 }
2748 }
2749 }
2750 499 *out = rv;
2751 499 *keep = kp;
2752 499 return n;
2753 }
2754  
2755 /* ---- charting a row set --------------------------------------------------
2756 *
2757 * Two screens draw the same rows two ways - an SVG path server-side and a
2758 * plotly figure client-side - and both begin by splitting the rows into one
2759 * series per service and putting each series in time order. That grouping is
2760 * shared here because the two used to disagree about it: whichever one was
2761 * edited last decided which rows were a series and which were dropped, and a
2762 * chart that draws a different set of points from the table beside it is
2763 * worse than a chart that fails. */
2764 typedef struct {
2765 SV *name; /* the series name, owned here */
2766 AV *pts; /* the rows, owned here, in time order */
2767 } povw_series;
2768  
2769 10 static void povw_series_free(pTHX_ povw_series *g, SSize_t n) {
2770 SSize_t i;
2771 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 10 20
2772 10 SvREFCNT_dec(g[i].name);
2773 10 SvREFCNT_dec((SV *)g[i].pts);
2774 }
2775 10 Safefree(g);
2776 10 }
2777  
2778 /* The timestamp of a row, as bytes. Rows carry it as a decimal string
2779 * precisely because it does not fit a double. */
2780 70135 static const char *povw_row_t(pTHX_ SV *rowref, STRLEN *len) {
2781 SV **t;
2782 70135 ▶ if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) { 70135 0 70135
if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) { 70135 0 70135
if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) { 0 70135 70135
2783 0 *len = 0;
2784 0 return "";
2785 }
2786 70135 t = hv_fetchs((HV *)SvRV(rowref), "t", 0);
2787 70135 ▶ if (!t || !SvOK(*t)) { *len = 0; return ""; }
Branch: 2 branches
BranchTrueFalseTotal
if (!t || !SvOK(*t)) { *len = 0; return ""; } 70135 0 70135
if (!t || !SvOK(*t)) { *len = 0; return ""; } 0 70135 70135
2788 140270 return SvPV(*t, *len);
2789 }
2790  
2791 /* HOW MANY LOG LINES ONE SCREEN IS.
2792 *
2793 * Enough to scroll through and to search with the browser's own find, and
2794 * small enough that the page is a page. The reader narrows the window or the
2795 * query to see further back, and the partial-result banner says so. */
2796 #define POVW_LOG_PAGE 500
2797  
2798 /* HOW MUCH ONE PANEL MAY READ.
2799 *
2800 * A dashboard runs its panels serially in the request, and until now passed
2801 * no limit at all - so each one got Store::query's own default of 500,000
2802 * rows, and six panels was six of those before the first byte of HTML.
2803 *
2804 * A panel is a chart. The chart is decimated to 2,000 points before it is
2805 * drawn (POVW_PLOT_MAX), and a bucketed panel is a few hundred buckets, so
2806 * a budget in the tens of thousands is far more than any panel can show and
2807 * far less than a screen that stops answering. The truncation flag the store
2808 * already sets is what makes the difference visible when it bites. */
2809 #define POVW_PANEL_ROWS 20000
2810  
2811 /* A GRID THE STYLESHEET HAS RULES FOR.
2812 *
2813 * `cols-N` and `span-N` are static CSS classes, so a number outside the range
2814 * they cover renders as a class nothing matches and the panel silently
2815 * occupies one column. The schema has a CHECK constraint and the writers
2816 * clamp - but a host's own seam is a hashref that never went near either, so
2817 * the last chance to be right is here, at the point of drawing. */
2818 #define POVW_GRID_MIN 1
2819 #define POVW_GRID_MAX 6
2820 96 static int povw_grid(IV n, IV dflt) {
2821 96 ▶ if (!n) n = dflt;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) n = dflt; 51 45 96
2822 96 ▶ if (n < POVW_GRID_MIN) return POVW_GRID_MIN;
Branch: 1 branch
BranchTrueFalseTotal
if (n < POVW_GRID_MIN) return POVW_GRID_MIN; 1 95 96
2823 95 ▶ if (n > POVW_GRID_MAX) return POVW_GRID_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > POVW_GRID_MAX) return POVW_GRID_MAX; 2 93 95
2824 93 return (int)n;
2825 }
2826  
2827 /* The numeric value of a row, for deciding which points a chart can drop. */
2828 70000 static double povw_row_value(pTHX_ SV *rowref) {
2829 SV **v;
2830 70000 ▶ if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0; 70000 0 70000
if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0; 70000 0 70000
if (!rowref || !SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0; 0 70000 70000
2831 70000 v = hv_fetchs((HV *)SvRV(rowref), "value", 0);
2832 70000 ▶ return (v && SvOK(*v)) ? SvNV(*v) : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (v && SvOK(*v)) ? SvNV(*v) : 0; 70000 0 70000
return (v && SvOK(*v)) ? SvNV(*v) : 0; 70000 0 70000
2833 }
2834  
2835 /* HOW MANY POINTS A LINE IS WORTH DRAWING WITH.
2836 *
2837 * The chart is 720 pixels wide. A line through 152,000 points renders at most
2838 * 720 columns of them, so the other 151,000 cost a three-megabyte page and a
2839 * browser that stops responding while plotly lays out SVG it will then draw
2840 * on top of itself.
2841 *
2842 * Comfortably above the pixel count, so the decimation below is never visible
2843 * and never has to be reasoned about at the drawing end. */
2844 #define POVW_PLOT_MAX 2000
2845  
2846 /* WHICH points, and the answer is not "every eightieth one".
2847 *
2848 * Taking every nth sample drops whichever spike falls between two of them, so
2849 * the chart that exists to show a spike is the one that hides it. This keeps
2850 * the MINIMUM AND MAXIMUM of each bucket instead, which reproduces the
2851 * envelope of the full series exactly - the line covers the same vertical
2852 * extent it would have covered, drawn from a fraction of the points - and
2853 * the two ends, so it still spans the window it claims to.
2854 *
2855 * Fills `keep` with indices in increasing order and returns how many. */
2856 5 static SSize_t povw_decimate(pTHX_ AV *pts, SSize_t cnt, SSize_t *keep) {
2857 5 SSize_t nb = POVW_PLOT_MAX / 2, b, n = 0;
2858  
2859 5005 ▶ for (b = 0; b < nb; b++) {
Branch: 1 branch
BranchTrueFalseTotal
for (b = 0; b < nb; b++) { 5000 5 5005
2860 5000 SSize_t lo = (SSize_t)((double)b * (double)cnt / (double)nb);
2861 5000 SSize_t hi = (SSize_t)((double)(b + 1) * (double)cnt / (double)nb);
2862 SSize_t x, imin, imax;
2863 double vmin, vmax;
2864 SV **e;
2865  
2866 5000 ▶ if (hi > cnt) hi = cnt;
Branch: 1 branch
BranchTrueFalseTotal
if (hi > cnt) hi = cnt; 0 5000 5000
2867 5000 ▶ if (lo >= hi) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (lo >= hi) continue; 0 5000 5000
2868  
2869 5000 e = av_fetch(pts, lo, 0);
2870 5000 imin = imax = lo;
2871 5000 ▶ vmin = vmax = e ? povw_row_value(aTHX_ *e) : 0;
Branch: 1 branch
BranchTrueFalseTotal
vmin = vmax = e ? povw_row_value(aTHX_ *e) : 0; 5000 0 5000
2872 70000 ▶ for (x = lo + 1; x < hi; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = lo + 1; x < hi; x++) { 65000 5000 70000
2873 double vv;
2874 65000 e = av_fetch(pts, x, 0);
2875 65000 ▶ vv = e ? povw_row_value(aTHX_ *e) : 0;
Branch: 1 branch
BranchTrueFalseTotal
vv = e ? povw_row_value(aTHX_ *e) : 0; 65000 0 65000
2876 65000 ▶ if (vv < vmin) { vmin = vv; imin = x; }
Branch: 1 branch
BranchTrueFalseTotal
if (vv < vmin) { vmin = vv; imin = x; } 1 64999 65000
2877 65000 ▶ if (vv > vmax) { vmax = vv; imax = x; }
Branch: 1 branch
BranchTrueFalseTotal
if (vv > vmax) { vmax = vv; imax = x; } 27001 37999 65000
2878 }
2879 /* In index order, so x stays monotonic and the line does not double
2880 * back on itself. */
2881 5000 ▶ if (imin < imax) { keep[n++] = imin; keep[n++] = imax; }
Branch: 1 branch
BranchTrueFalseTotal
if (imin < imax) { keep[n++] = imin; keep[n++] = imax; } 3001 1999 5000
2882 1999 ▶ else if (imax < imin) { keep[n++] = imax; keep[n++] = imin; }
Branch: 1 branch
BranchTrueFalseTotal
else if (imax < imin) { keep[n++] = imax; keep[n++] = imin; } 1 1998 1999
2883 1998 else keep[n++] = imin;
2884 }
2885  
2886 /* The first and last sample, so the line starts and ends where the data
2887 * does rather than at whichever extreme the end buckets happened to hold. */
2888 5 ▶ if (n && keep[0] != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (n && keep[0] != 0) { 5 0 5
if (n && keep[0] != 0) { 0 5 5
2889 SSize_t x;
2890 0 ▶ for (x = n; x > 0; x--) keep[x] = keep[x - 1];
Branch: 1 branch
BranchTrueFalseTotal
for (x = n; x > 0; x--) keep[x] = keep[x - 1]; 0 0 0
2891 0 keep[0] = 0;
2892 0 n++;
2893 }
2894 5 ▶ if (n && keep[n - 1] != cnt - 1) keep[n++] = cnt - 1;
Branch: 2 branches
BranchTrueFalseTotal
if (n && keep[n - 1] != cnt - 1) keep[n++] = cnt - 1; 5 0 5
if (n && keep[n - 1] != cnt - 1) keep[n++] = cnt - 1; 2 3 5
2895 5 return n;
2896 }
2897  
2898 /* ONE POINT, WITH ITS TIMESTAMP ALREADY IN HAND.
2899 *
2900 * povw_row_t is a hash lookup. Calling it from inside a comparison made the
2901 * cost of ORDERING a series a multiple of the cost of reading it, and on a
2902 * metric with a hundred thousand points that multiple is the whole request.
2903 * Fetched once per row here, the sort touches no hash at all. */
2904 typedef struct { SV *sv; const char *t; STRLEN tl; } povw_pt;
2905  
2906 /* A bottom-up merge, for the same reason po_row.h uses one: the rows arrive
2907 * from the store in an order that has nothing to do with the key, and an
2908 * insertion sort is quadratic on exactly that. It was quadratic here, over
2909 * 152,000 points, with two hash lookups per comparison - so the metrics
2910 * screen did not render slowly, it never returned, and it held the worker at
2911 * a hundred per cent while not returning.
2912 *
2913 * O(n log n) whatever the input looks like, and unlike quicksort there is no
2914 * arrangement of the data that degrades it. */
2915 70161 static void povw_pts_merge(povw_pt *src, povw_pt *dst,
2916 SSize_t lo, SSize_t mid, SSize_t hi) {
2917 70161 SSize_t i = lo, j = mid, k = lo;
2918 616644 ▶ while (i < mid && j < hi) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < mid && j < hi) { 555504 61140 616644
while (i < mid && j < hi) { 546483 9021 555504
2919 /* `< 0` rather than `<= 0`: equal instants keep the order they
2920 * arrived in, so two runs of one query draw the same line. */
2921 1092966 dst[k++] = po_ns_cmp_str(src[j].t, (size_t)src[j].tl,
2922 546483 src[i].t, (size_t)src[i].tl, NULL) < 0
2923 546483 ▶ ? src[j++] : src[i++];
Branch: 1 branch
BranchTrueFalseTotal
? src[j++] : src[i++]; 58054 488429 546483
2924 }
2925 134944 ▶ while (i < mid) dst[k++] = src[i++];
Branch: 1 branch
BranchTrueFalseTotal
while (i < mid) dst[k++] = src[i++]; 64783 70161 134944
2926 489753 ▶ while (j < hi) dst[k++] = src[j++];
Branch: 1 branch
BranchTrueFalseTotal
while (j < hi) dst[k++] = src[j++]; 419592 70161 489753
2927 70161 }
2928  
2929 9 static void povw_pts_sort(povw_pt *a, povw_pt *buf, SSize_t n) {
2930 9 povw_pt *src = a, *dst = buf;
2931 SSize_t width;
2932 95 ▶ for (width = 1; width < n; width *= 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (width = 1; width < n; width *= 2) { 86 9 95
2933 SSize_t lo;
2934 70247 ▶ for (lo = 0; lo < n; lo += 2 * width) {
Branch: 1 branch
BranchTrueFalseTotal
for (lo = 0; lo < n; lo += 2 * width) { 70161 86 70247
2935 70161 SSize_t mid = lo + width < n ? lo + width : n;
2936 70161 SSize_t hi = lo + 2 * width < n ? lo + 2 * width : n;
2937 70161 povw_pts_merge(src, dst, lo, mid, hi);
2938 }
2939 86 { povw_pt *t = src; src = dst; dst = t; }
2940 }
2941 9 ▶ if (src != a) Copy(src, a, n, povw_pt);
Branch: 2 branches
BranchTrueFalseTotal
if (src != a) Copy(src, a, n, povw_pt); 8 1 9
if (src != a) Copy(src, a, n, povw_pt); 0 8 8
2942 9 }
2943  
2944 /* Split rows into series and order each one. Returns the count and fills
2945 * *out; the caller frees with povw_series_free. */
2946 10 static SSize_t povw_series_split(pTHX_ AV *rows, povw_series **out) {
2947 10 ▶ SSize_t n = rows ? av_len(rows) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t n = rows ? av_len(rows) + 1 : 0; 10 0 10
2948 10 SSize_t i, ng = 0;
2949 povw_series *g;
2950  
2951 10 ▶ Newx(g, n ? n : 1, povw_series);
Branch: 3 branches
BranchTrueFalseTotal
Newx(g, n ? n : 1, povw_series); 10 0 10
Newx(g, n ? n : 1, povw_series); 0 10 10
Newx(g, n ? n : 1, povw_series); 10 0 10
2952  
2953 70146 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 70136 10 70146
2954 70136 SV **e = av_fetch(rows, i, 0);
2955 HV *row;
2956 SV **v, **s;
2957 70136 const char *key = "value";
2958 70136 STRLEN klen = 5;
2959 SSize_t j;
2960  
2961 70136 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 70136 0 70136
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 70136 0 70136
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 0 70136 70136
2962 70136 row = (HV *)SvRV(*e);
2963 /* A ROW WITH NO VALUE IS NOT A GAP IN A LINE, it is a row of a
2964 * different shape that happened to come back in the same list. */
2965 70136 v = hv_fetchs(row, "value", 0);
2966 70136 ▶ if (!v || !SvOK(*v)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(*v)) continue; 70136 0 70136
if (!v || !SvOK(*v)) continue; 0 70136 70136
2967 70136 s = hv_fetchs(row, "service", 0);
2968 140272 ▶ if (s && SvOK(*s) && SvCUR(*s)) key = SvPV(*s, klen);
Branch: 3 branches
BranchTrueFalseTotal
if (s && SvOK(*s) && SvCUR(*s)) key = SvPV(*s, klen); 70136 0 70136
if (s && SvOK(*s) && SvCUR(*s)) key = SvPV(*s, klen); 70136 0 70136
if (s && SvOK(*s) && SvCUR(*s)) key = SvPV(*s, klen); 70136 0 70136
2969  
2970 70136 ▶ for (j = 0; j < ng; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < ng; j++) { 70126 10 70136
2971 STRLEN gl;
2972 70126 const char *gp = SvPV(g[j].name, gl);
2973 70126 ▶ if (gl == klen && memcmp(gp, key, klen) == 0) break;
Branch: 2 branches
BranchTrueFalseTotal
if (gl == klen && memcmp(gp, key, klen) == 0) break; 70126 0 70126
if (gl == klen && memcmp(gp, key, klen) == 0) break; 70126 0 70126
2974 }
2975 70136 ▶ if (j == ng) {
Branch: 1 branch
BranchTrueFalseTotal
if (j == ng) { 10 70126 70136
2976 10 g[ng].name = newSVpvn(key, klen);
2977 10 g[ng].pts = newAV();
2978 10 ng++;
2979 }
2980 70136 av_push(g[j].pts, SvREFCNT_inc(*e));
2981 }
2982  
2983 /* The series in name order, so the legend and the colour a series gets
2984 * do not move between two requests that returned the same data. */
2985 10 ▶ for (i = 1; i < ng; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < ng; i++) { 0 10 10
2986 0 povw_series k = g[i];
2987 SSize_t j;
2988 STRLEN kl;
2989 0 const char *kp = SvPV(k.name, kl);
2990 0 ▶ for (j = i - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = i - 1; j >= 0; j--) { 0 0 0
2991 STRLEN jl;
2992 0 const char *jp = SvPV(g[j].name, jl);
2993 0 size_t m = jl < kl ? jl : kl;
2994 0 ▶ int c = m ? memcmp(jp, kp, m) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int c = m ? memcmp(jp, kp, m) : 0; 0 0 0
2995 0 ▶ if (!c) c = jl == kl ? 0 : (jl < kl ? -1 : 1);
Branch: 3 branches
BranchTrueFalseTotal
if (!c) c = jl == kl ? 0 : (jl < kl ? -1 : 1); 0 0 0
if (!c) c = jl == kl ? 0 : (jl < kl ? -1 : 1); 0 0 0
if (!c) c = jl == kl ? 0 : (jl < kl ? -1 : 1); 0 0 0
2996 0 ▶ if (c <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (c <= 0) break; 0 0 0
2997 0 g[j + 1] = g[j];
2998 }
2999 0 g[j + 1] = k;
3000 }
3001  
3002 /* And the points inside each series in time order. Compared as STRINGS by
3003 * width then value, not numerically: a nanosecond instant past 2^53 does
3004 * not survive the conversion, and two points a microsecond apart would
3005 * sort as equal. */
3006 20 ▶ for (i = 0; i < ng; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ng; i++) { 10 10 20
3007 10 SSize_t cnt = av_len(g[i].pts) + 1;
3008 /* THE RAW SLOTS, not av_store: av_store frees whatever occupied the
3009 * slot, so shifting elements along with it drops a reference on every
3010 * row it passes over and the array is full of freed SVs by the time
3011 * the sort finishes. Moving the pointers changes no refcount at all,
3012 * which is exactly what a permutation should do. */
3013 10 SV **arr = AvARRAY(g[i].pts);
3014 povw_pt *pt, *buf;
3015 SSize_t a;
3016  
3017 10 ▶ if (cnt < 2) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (cnt < 2) continue; 1 9 10
3018 9 ▶ Newx(pt, cnt, povw_pt);
Branch: 1 branch
BranchTrueFalseTotal
Newx(pt, cnt, povw_pt); 0 9 9
3019 9 ▶ Newx(buf, cnt, povw_pt);
Branch: 1 branch
BranchTrueFalseTotal
Newx(buf, cnt, povw_pt); 0 9 9
3020 70144 ▶ for (a = 0; a < cnt; a++) {
Branch: 1 branch
BranchTrueFalseTotal
for (a = 0; a < cnt; a++) { 70135 9 70144
3021 70135 pt[a].sv = arr[a];
3022 70135 pt[a].t = povw_row_t(aTHX_ arr[a], &pt[a].tl);
3023 }
3024 9 povw_pts_sort(pt, buf, cnt);
3025 70144 ▶ for (a = 0; a < cnt; a++) arr[a] = pt[a].sv;
Branch: 1 branch
BranchTrueFalseTotal
for (a = 0; a < cnt; a++) arr[a] = pt[a].sv; 70135 9 70144
3026 9 Safefree(buf);
3027 9 Safefree(pt);
3028 }
3029  
3030 10 *out = g;
3031 10 return ng;
3032 }
3033  
3034 /* A trace identifier, in whichever spelling arrived.
3035 *
3036 * The links carry hex; a bookmark from before, or a decimal pair somebody
3037 * built by hand, still resolves. Returns 0 when the text is not an identifier
3038 * at all - which is the signal to treat it as a search term instead. */
3039 18 static int povw_trace_id_c(pTHX_ SV *text, po_u64 *hi_out, po_u64 *lo_out) {
3040 18 STRLEN len, i, start = 0, end;
3041 const char *p;
3042 18 po_u64 hi = 0, lo = 0;
3043  
3044 18 ▶ if (!text || !SvOK(text)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!text || !SvOK(text)) return 0; 18 0 18
if (!text || !SvOK(text)) return 0; 0 18 18
3045 18 p = SvPV(text, len);
3046  
3047 /* Leading and trailing space, because a paste brings it along. */
3048 20 ▶ while (start < len && (p[start] == ' ' || p[start] == '\t')) start++;
Branch: 3 branches
BranchTrueFalseTotal
while (start < len && (p[start] == ' ' || p[start] == '\t')) start++; 20 0 20
while (start < len && (p[start] == ' ' || p[start] == '\t')) start++; 2 18 20
while (start < len && (p[start] == ' ' || p[start] == '\t')) start++; 0 18 18
3049 18 end = len;
3050 20 ▶ while (end > start && (p[end - 1] == ' ' || p[end - 1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (p[end - 1] == ' ' || p[end - 1] == '\t')) end--; 20 0 20
while (end > start && (p[end - 1] == ' ' || p[end - 1] == '\t')) end--; 2 18 20
while (end > start && (p[end - 1] == ' ' || p[end - 1] == '\t')) end--; 0 18 18
3051 18 p += start;
3052 18 len = end - start;
3053 18 ▶ if (!len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!len) return 0; 0 18 18
3054  
3055 /* 32 hex characters: the wire form. */
3056 18 ▶ if (len == 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 32) { 10 8 18
3057 330 ▶ for (i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) { 320 10 330
3058 int d;
3059 320 char c = p[i];
3060 320 ▶ if (c >= '0' && c <= '9') d = c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') d = c - '0'; 320 0 320
if (c >= '0' && c <= '9') d = c - '0'; 311 9 320
3061 9 ▶ else if (c >= 'a' && c <= 'f') d = c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'a' && c <= 'f') d = c - 'a' + 10; 8 1 9
else if (c >= 'a' && c <= 'f') d = c - 'a' + 10; 8 0 8
3062 1 ▶ else if (c >= 'A' && c <= 'F') d = c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 'A' && c <= 'F') d = c - 'A' + 10; 1 0 1
else if (c >= 'A' && c <= 'F') d = c - 'A' + 10; 1 0 1
3063 0 else return 0;
3064 320 ▶ if (i < 16) hi = (hi << 4) | (po_u64)d;
Branch: 1 branch
BranchTrueFalseTotal
if (i < 16) hi = (hi << 4) | (po_u64)d; 160 160 320
3065 160 else lo = (lo << 4) | (po_u64)d;
3066 }
3067 /* An all-zero id is invalid and OTLP says so; it is also what a
3068 * truncated paste looks like. */
3069 10 ▶ if (!hi && !lo) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!hi && !lo) return 0; 1 9 10
if (!hi && !lo) return 0; 1 0 1
3070 9 *hi_out = hi; *lo_out = lo;
3071 9 return 1;
3072 }
3073  
3074 /* `<hi>-<lo>`, both decimal: the form this UI's own links carry. */
3075 {
3076 8 STRLEN dash = 0;
3077 8 int found = 0;
3078 SV *a, *b;
3079 41 ▶ for (i = 0; i < len; i++) if (p[i] == '-') { dash = i; found = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < len; i++) if (p[i] == '-') { dash = i; found = 1; break; } 3 33 36
for (i = 0; i < len; i++) if (p[i] == '-') { dash = i; found = 1; break; } 36 5 41
3080 8 ▶ if (!found || !dash || dash + 1 >= len) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!found || !dash || dash + 1 >= len) return 0; 3 5 8
if (!found || !dash || dash + 1 >= len) return 0; 3 0 3
if (!found || !dash || dash + 1 >= len) return 0; 0 3 3
3081 17 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 15 2 17
3082 15 ▶ if (i != dash && (p[i] < '0' || p[i] > '9')) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (i != dash && (p[i] < '0' || p[i] > '9')) return 0; 13 2 15
if (i != dash && (p[i] < '0' || p[i] > '9')) return 0; 13 0 13
if (i != dash && (p[i] < '0' || p[i] > '9')) return 0; 1 12 13
3083 2 a = sv_2mortal(newSVpvn(p, dash));
3084 2 b = sv_2mortal(newSVpvn(p + dash + 1, len - dash - 1));
3085 2 ▶ if (!po_sv_to_u64(aTHX_ a, &hi)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_sv_to_u64(aTHX_ a, &hi)) return 0; 0 2 2
3086 2 ▶ if (!po_sv_to_u64(aTHX_ b, &lo)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_sv_to_u64(aTHX_ b, &lo)) return 0; 0 2 2
3087 2 ▶ if (!hi && !lo) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!hi && !lo) return 0; 0 2 2
if (!hi && !lo) return 0; 0 0 0
3088 2 *hi_out = hi; *lo_out = lo;
3089 2 return 1;
3090 }
3091 }
3092  
3093 /* The single-trace page, reached from the trace list page when what arrived
3094 * turns out to be an identifier. Through Perl rather than as a direct C call
3095 * because the two are separate XSUBs and the dispatch is one call per page
3096 * render, against a store read that is orders of magnitude dearer. */
3097 6 static SV *povw_trace_one_sv(pTHX_ SV *class, SV *store, SV *req,
3098 SV *from, SV *to, SV *range) {
3099 SV *out;
3100 int n;
3101 6 dSP;
3102  
3103 6 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 6 6
3104 6 ▶ XPUSHs(class);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(class); 0 6 6
3105 6 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 6 6
3106 6 ▶ XPUSHs(req);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(req); 0 6 6
3107 6 ▶ XPUSHs(sv_2mortal(newSVsv(from)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(from))); 0 6 6
3108 6 ▶ XPUSHs(sv_2mortal(newSVsv(to)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(to))); 0 6 6
3109 6 ▶ XPUSHs(sv_2mortal(newSVsv(range)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(range))); 0 6 6
3110 6 PUTBACK;
3111 6 n = call_pv("Punk::Observe::View::_trace_one", G_SCALAR);
3112 6 SPAGAIN;
3113 6 ▶ out = n ? SvREFCNT_inc(POPs) : newRV_noinc((SV *)newHV());
Branch: 1 branch
BranchTrueFalseTotal
out = n ? SvREFCNT_inc(POPs) : newRV_noinc((SV *)newHV()); 6 0 6
3114 6 PUTBACK;
3115 6 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 0 6
3116 6 return out;
3117 }
3118  
3119 /* An INDEX into the tree's span list, resolved to that span's id.
3120 *
3121 * This is Trace::analyse's convention (parent_index), NOT the store's - the
3122 * store hands back a parent SPAN ID under parent_span_id, which needs no
3123 * resolving and must not be passed through here. The two were both called
3124 * `parent`, and feeding this one the other made every root its own parent.
3125 * They have different names now so that cannot be spelled. */
3126 0 static SV *povw_parent_id_sv(pTHX_ SV *tree, SV *span) {
3127 HV *t, *s;
3128 SV **f;
3129 IV i;
3130 AV *spans;
3131 SV **p;
3132  
3133 0 ▶ if (!SvROK(tree) || !SvROK(span)) return newSViv(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(tree) || !SvROK(span)) return newSViv(0); 0 0 0
if (!SvROK(tree) || !SvROK(span)) return newSViv(0); 0 0 0
3134 0 t = (HV *)SvRV(tree);
3135 0 s = (HV *)SvRV(span);
3136  
3137 0 f = hv_fetchs(s, "parent_index", 0);
3138 0 ▶ if (!f || !SvOK(*f)) return newSViv(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvOK(*f)) return newSViv(0); 0 0 0
if (!f || !SvOK(*f)) return newSViv(0); 0 0 0
3139 0 i = SvIV(*f);
3140 /* -1 is a ROOT, not an error: a span with no parent in this trace is the
3141 * top of it. */
3142 0 ▶ if (i < 0) return newSViv(0);
Branch: 1 branch
BranchTrueFalseTotal
if (i < 0) return newSViv(0); 0 0 0
3143  
3144 0 f = hv_fetchs(t, "spans", 0);
3145 0 ▶ if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return newSViv(0);
Branch: 3 branches
BranchTrueFalseTotal
if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return newSViv(0); 0 0 0
if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return newSViv(0); 0 0 0
if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return newSViv(0); 0 0 0
3146 0 spans = (AV *)SvRV(*f);
3147 0 p = av_fetch(spans, (SSize_t)i, 0);
3148 0 ▶ if (!p || !SvROK(*p)) return newSViv(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!p || !SvROK(*p)) return newSViv(0); 0 0 0
if (!p || !SvROK(*p)) return newSViv(0); 0 0 0
3149  
3150 0 f = hv_fetchs((HV *)SvRV(*p), "span_id", 0);
3151 0 ▶ return (f && SvOK(*f)) ? newSVsv(*f) : newSViv(0);
Branch: 2 branches
BranchTrueFalseTotal
return (f && SvOK(*f)) ? newSVsv(*f) : newSViv(0); 0 0 0
return (f && SvOK(*f)) ? newSVsv(*f) : newSViv(0); 0 0 0
3152 }
3153  
3154 /* Two questions the metrics page asks of the query TEXT.
3155 *
3156 * Written out rather than left as regular expressions because they decide
3157 * something a chart is wrong about if it is wrong: whether an absent bucket
3158 * is a zero or a gap. `\b` is perl's - [A-Za-z0-9_] on either side - and `$`
3159 * matches at the end or before a single trailing newline, as it does without
3160 * /m. The only backtracking either needs is the optional `count`, and it is
3161 * tried present-first, which is what a greedy `?` does. */
3162 120 static int povw_word_at(const char *q, size_t n, size_t i,
3163 const char *w, size_t wn) {
3164 120 ▶ if (i + wn > n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (i + wn > n) return 0; 16 104 120
3165 104 ▶ if (memcmp(q + i, w, wn) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(q + i, w, wn) != 0) return 0; 101 3 104
3166 3 ▶ if (i && isWORDCHAR_A((U8)q[i - 1])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (i && isWORDCHAR_A((U8)q[i - 1])) return 0; 3 0 3
if (i && isWORDCHAR_A((U8)q[i - 1])) return 0; 0 3 3
3167 3 return 1;
3168 }
3169  
3170 /* /\brate\s*\(/ */
3171 3 static int povw_q_rate(const char *q, size_t n) {
3172 size_t i;
3173 66 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 64 2 66
3174 size_t j;
3175 64 ▶ if (!povw_word_at(q, n, i, "rate", 4)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!povw_word_at(q, n, i, "rate", 4)) continue; 63 1 64
3176 1 j = i + 4;
3177 1 ▶ while (j < n && isSPACE((U8)q[j])) j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < n && isSPACE((U8)q[j])) j++; 1 0 1
while (j < n && isSPACE((U8)q[j])) j++; 0 1 1
3178 1 ▶ if (j < n && q[j] == '(') return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (j < n && q[j] == '(') return 1; 1 0 1
if (j < n && q[j] == '(') return 1; 1 0 1
3179 }
3180 2 return 0;
3181 }
3182  
3183 /* /\bbucket\s*\([^)]*\)\s*(?:count)?\s*(?:by|$)/ */
3184 3 static int povw_q_bucket(const char *q, size_t n) {
3185 size_t i;
3186 58 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 56 2 58
3187 size_t j;
3188 int attempt;
3189 56 ▶ if (!povw_word_at(q, n, i, "bucket", 6)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!povw_word_at(q, n, i, "bucket", 6)) continue; 54 2 56
3190 2 j = i + 6;
3191 2 ▶ while (j < n && isSPACE((U8)q[j])) j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < n && isSPACE((U8)q[j])) j++; 2 0 2
while (j < n && isSPACE((U8)q[j])) j++; 0 2 2
3192 2 ▶ if (j >= n || q[j] != '(') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (j >= n || q[j] != '(') continue; 2 0 2
if (j >= n || q[j] != '(') continue; 0 2 2
3193 2 j++;
3194 6 ▶ while (j < n && q[j] != ')') j++;
Branch: 2 branches
BranchTrueFalseTotal
while (j < n && q[j] != ')') j++; 6 0 6
while (j < n && q[j] != ')') j++; 4 2 6
3195 2 ▶ if (j >= n) continue; /* never closed */
Branch: 1 branch
BranchTrueFalseTotal
if (j >= n) continue; /* never closed */ 0 2 2
3196 2 j++;
3197 4 ▶ for (attempt = 0; attempt < 2; attempt++) {
Branch: 1 branch
BranchTrueFalseTotal
for (attempt = 0; attempt < 2; attempt++) { 3 1 4
3198 3 size_t k = j;
3199 6 ▶ while (k < n && isSPACE((U8)q[k])) k++;
Branch: 2 branches
BranchTrueFalseTotal
while (k < n && isSPACE((U8)q[k])) k++; 6 0 6
while (k < n && isSPACE((U8)q[k])) k++; 3 3 6
3200 3 ▶ if (attempt == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (attempt == 0) { 2 1 3
3201 2 ▶ if (!(k + 5 <= n && memcmp(q + k, "count", 5) == 0)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!(k + 5 <= n && memcmp(q + k, "count", 5) == 0)) continue; 1 1 2
if (!(k + 5 <= n && memcmp(q + k, "count", 5) == 0)) continue; 0 1 1
3202 1 k += 5;
3203 1 ▶ while (k < n && isSPACE((U8)q[k])) k++;
Branch: 2 branches
BranchTrueFalseTotal
while (k < n && isSPACE((U8)q[k])) k++; 0 1 1
while (k < n && isSPACE((U8)q[k])) k++; 0 0 0
3204 }
3205 2 ▶ if (k + 2 <= n && memcmp(q + k, "by", 2) == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (k + 2 <= n && memcmp(q + k, "by", 2) == 0) return 1; 1 1 2
if (k + 2 <= n && memcmp(q + k, "by", 2) == 0) return 1; 0 1 1
3206 2 ▶ if (k == n) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (k == n) return 1; 1 1 2
3207 1 ▶ if (k + 1 == n && q[k] == '\n') return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (k + 1 == n && q[k] == '\n') return 1; 0 1 1
if (k + 1 == n && q[k] == '\n') return 1; 0 0 0
3208 }
3209 }
3210 2 return 0;
3211 }
3212  
3213 /* A figure through Plot, encoded. Both halves are Perl, and calling them
3214 * separately from three places was three chances to encode one and forget the
3215 * other. */
3216 3 static SV *povw_plot_encode(pTHX_ const char *fn, SV **args, int nargs) {
3217 3 SV *fig = NULL, *enc = NULL;
3218 int n, k;
3219 3 dSP;
3220  
3221 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
3222 18 ▶ for (k = 0; k < nargs; k++) XPUSHs(args[k]);
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < nargs; k++) XPUSHs(args[k]); 0 15 15
for (k = 0; k < nargs; k++) XPUSHs(args[k]); 15 3 18
3223 3 PUTBACK;
3224 3 n = call_pv(fn, G_SCALAR);
3225 3 SPAGAIN;
3226 3 ▶ fig = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
fig = n ? SvREFCNT_inc(POPs) : NULL; 3 0 3
3227 3 PUTBACK;
3228 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
3229  
3230 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
3231 3 ▶ XPUSHs(sv_2mortal(fig ? newSVsv(fig) : newSV(0)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(fig ? newSVsv(fig) : newSV(0))); 0 3 3
XPUSHs(sv_2mortal(fig ? newSVsv(fig) : newSV(0))); 3 0 3
3232 3 PUTBACK;
3233 3 n = call_pv("Punk::Observe::Plot::encode", G_SCALAR);
3234 3 SPAGAIN;
3235 3 ▶ enc = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
enc = n ? SvREFCNT_inc(POPs) : NULL; 3 0 3
3236 3 PUTBACK;
3237 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
3238 3 ▶ if (fig) SvREFCNT_dec(fig);
Branch: 1 branch
BranchTrueFalseTotal
if (fig) SvREFCNT_dec(fig); 3 0 3
3239 3 return enc;
3240 }
3241  
3242 /* A value as `0 + $x` would have made it.
3243 *
3244 * NOT newSVnv: perl's own addition asks SvIV_please of both operands first,
3245 * so an NV that happens to be integral comes back an IV, and the JSON says
3246 * -2 where a plain NV would say -2.0. A chart library reading one of those as
3247 * a category and the other as a number is a real failure mode, and the
3248 * difference is invisible on the screen that produced it. */
3249 8243 static SV *povw_num(pTHX_ SV *v) {
3250 8243 ▶ if (!v || !SvOK(v)) return newSViv(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(v)) return newSViv(0); 8243 0 8243
if (!v || !SvOK(v)) return newSViv(0); 0 8243 8243
3251 8243 ▶ SvIV_please_nomg(v);
Branch: 2 branches
BranchTrueFalseTotal
SvIV_please_nomg(v); 116 8127 8243
SvIV_please_nomg(v); 116 0 116
3252 8243 ▶ if (SvIOK(v)) return SvIsUV(v) ? newSVuv(SvUV(v)) : newSViv(SvIV(v));
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(v)) return SvIsUV(v) ? newSVuv(SvUV(v)) : newSViv(SvIV(v)); 8241 2 8243
if (SvIOK(v)) return SvIsUV(v) ? newSVuv(SvUV(v)) : newSViv(SvIV(v)); 0 8241 8241
3253 2 return newSVnv(SvNV(v));
3254 }
3255  
3256 /* A VALUE IN A TABLE CELL. `%.4g` turned a count of 14,542 into
3257 * "1.454e+04" - four significant digits, sitting beside a rows column that
3258 * showed all five with commas. An integral value is an integer and is
3259 * written as one, comma-grouped exactly like the counts beside it; a large
3260 * fractional value rounds to the same shape, because its fraction is noise
3261 * at that magnitude; only a small genuine fraction keeps the short %.4g
3262 * form, where four digits is a choice rather than a loss. */
3263 23 static SV *povw_fmt_value(pTHX_ double d) {
3264 23 int integral = (d == Perl_floor(d));
3265 23 ▶ if ((integral || d >= 10000.0 || d <= -10000.0)
Branch: 3 branches
BranchTrueFalseTotal
if ((integral || d >= 10000.0 || d <= -10000.0) 2 21 23
if ((integral || d >= 10000.0 || d <= -10000.0) 1 1 2
if ((integral || d >= 10000.0 || d <= -10000.0) 0 1 1
3266 22 ▶ && d < 9e15 && d > -9e15) {
Branch: 2 branches
BranchTrueFalseTotal
&& d < 9e15 && d > -9e15) { 22 0 22
&& d < 9e15 && d > -9e15) { 22 0 22
3267 char raw[40], out[64];
3268 22 const char *p = raw;
3269 size_t n;
3270 22 int neg = 0;
3271 22 (void)snprintf(raw, sizeof(raw), "%.0f", d);
3272 22 ▶ if (raw[0] == '-') { neg = 1; p = raw + 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (raw[0] == '-') { neg = 1; p = raw + 1; } 0 22 22
3273 22 n = po_fmt_count(p, strlen(p), out, sizeof(out));
3274 22 ▶ return neg ? newSVpvf("-%.*s", (int)n, out) : newSVpvn(out, n);
Branch: 1 branch
BranchTrueFalseTotal
return neg ? newSVpvf("-%.*s", (int)n, out) : newSVpvn(out, n); 0 22 22
3275 }
3276 1 return newSVpvf("%.4g", d);
3277 }
3278  
3279 /* ---- one dashboard panel's body ------------------------------------------ */
3280 /*
3281 * The panel body build, factored out of the page loop so it can run from
3282 * TWO places: the page itself under ?full=1, and the per-panel fragment
3283 * route that defer.js polls. The shell render calls neither - a dashboard
3284 * used to run every panel's query serially in the request, and the editor
3285 * ran all of them for forms that render none of it.
3286 *
3287 * `panel` is the template hashref the meta pass built (query and viz are
3288 * already on it); `src_panel` is the SOURCE hashref, because check_panel
3289 * validates position and span off the stored shape, not the drawn one.
3290 */
3291 /* ---- retention's edges ---------------------------------------------------
3292 *
3293 * The store is an object with methods and a worker is a process, so these
3294 * four are where the retention C reaches both.
3295 */
3296  
3297 /* Size and mtime in one call. `po_file_size` exists and does not report the
3298 * time, and the two are one stat. */
3299 5 static int por_stat(const char *path, po_u64 *size, po_u64 *mtime) {
3300 struct stat st;
3301 5 ▶ if (stat(path, &st) != 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (stat(path, &st) != 0) return 0; 0 5 5
3302 5 ▶ if (size) *size = (po_u64)st.st_size;
Branch: 1 branch
BranchTrueFalseTotal
if (size) *size = (po_u64)st.st_size; 5 0 5
3303 5 ▶ if (mtime) *mtime = (po_u64)st.st_mtime;
Branch: 1 branch
BranchTrueFalseTotal
if (mtime) *mtime = (po_u64)st.st_mtime; 5 0 5
3304 5 return 1;
3305 }
3306  
3307 /* IS THAT PROCESS STILL RUNNING? `kill(pid, 0)` asks without signalling.
3308 *
3309 * EPERM IS ALIVE. It means the process exists and belongs to somebody else,
3310 * and reading "not mine" as "dead" is how a live worker's log gets sealed out
3311 * from under its descriptor. ESRCH is the only answer that means gone.
3312 *
3313 * On Windows there are no signals and no kill; a pid there is answered
3314 * conservatively as ALIVE, so adoption there rests on the staleness test
3315 * alone - which is the safe direction. */
3316 2 static int por_pid_alive(int pid) {
3317 #ifdef _WIN32
3318 PERL_UNUSED_VAR(pid);
3319 return 1;
3320 #else
3321 2 ▶ if (kill((pid_t)pid, 0) == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (kill((pid_t)pid, 0) == 0) return 1; 0 2 2
3322 2 return errno == EPERM ? 1 : 0;
3323 #endif
3324 }
3325  
3326 /* UNLINK, NEVER TRUNCATE. Named here so the one primitive retention is
3327 * allowed to use is the one it reaches for: a truncated segment is SIGBUS for
3328 * every reader mapping it, while an unlinked one keeps reading to the end for
3329 * anyone already holding it. */
3330 13 static int po_unlink(const char *path) { return remove(path); }
3331  
3332 14 static int po_file_exists(const char *path) {
3333 struct stat st;
3334 14 return stat(path, &st) == 0;
3335 }
3336  
3337 /* Plain digits, and nothing else. A window or a byte count that is merely
3338 * number-ISH - "7d", " 12", "1e6" - is a typo somebody would rather hear
3339 * about at boot than discover as an empty store. */
3340 17 static int por_all_digits(pTHX_ SV *sv) {
3341 17 STRLEN l = 0;
3342 const char *p;
3343 size_t i;
3344 17 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 17 0 17
if (!sv || !SvOK(sv)) return 0; 0 17 17
3345 17 p = SvPV(sv, l);
3346 17 ▶ if (!l) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return 0; 0 17 17
3347 221 ▶ for (i = 0; i < l; i++) if (p[i] < '0' || p[i] > '9') return 0;
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < l; i++) if (p[i] < '0' || p[i] > '9') return 0; 205 0 205
for (i = 0; i < l; i++) if (p[i] < '0' || p[i] > '9') return 0; 1 204 205
for (i = 0; i < l; i++) if (p[i] < '0' || p[i] > '9') return 0; 205 16 221
3348 16 return 1;
3349 }
3350  
3351 /* One string-returning method on the store, or NULL if it dies. */
3352 42 static SV *por_call_str(pTHX_ SV *obj, const char *meth) {
3353 42 dSP;
3354 42 SV *r = NULL;
3355 int n;
3356 42 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 42 42
3357 42 ▶ XPUSHs(obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(obj); 0 42 42
3358 42 PUTBACK;
3359 42 n = call_method(meth, G_SCALAR | G_EVAL);
3360 42 SPAGAIN;
3361 42 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 42 0 42
3362 42 PUTBACK;
3363 42 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 42 0 42
3364 42 ▶ if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; }
Branch: 5 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 42 0 42
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 42 0 42
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 42 0 42
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 0 42 42
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 0 0 0
3365 42 return r;
3366 }
3367  
3368 /* `$store->seal($path)` - adopting a named log rather than this worker's own.
3369 * A die is caught: one log that cannot be sealed is counted and the pass goes
3370 * on to the rest. */
3371 1 static SV *por_call_seal(pTHX_ SV *store, const char *path) {
3372 1 dSP;
3373 1 SV *r = NULL;
3374 int n;
3375 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
3376 1 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 1 1
3377 1 ▶ XPUSHs(sv_2mortal(newSVpv(path, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(path, 0))); 0 1 1
3378 1 PUTBACK;
3379 1 n = call_method("seal", G_SCALAR | G_EVAL);
3380 1 SPAGAIN;
3381 1 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 1 0 1
3382 1 PUTBACK;
3383 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
3384 1 ▶ if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; }
Branch: 5 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 1 0 1
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 1 0 1
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 1 0 1
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 0 1 1
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { if (r) SvREFCNT_dec(r); r = NULL; } 0 0 0
3385 1 return r;
3386 }
3387  
3388 /* ---- the cron closure ----------------------------------------------------
3389 *
3390 * `cron_task` hands back a coderef the host schedules, which means an XSUB
3391 * that has to CARRY the options it was built with - the caller passes only
3392 * the queue. An anonymous XSUB does that: the options ride on the CV as
3393 * magic, which is also what frees them, so the closure owns its captures the
3394 * way a Perl one would rather than leaking them for the life of the process.
3395 */
3396 static MGVTBL por_cron_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 };
3397  
3398 /* One method call on the queue, discarding the result. */
3399 5 static void por_q_call(pTHX_ SV *q, const char *meth, SV **args, int nargs) {
3400 5 dSP;
3401 int i;
3402 5 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 5 5
3403 5 ▶ XPUSHs(q);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(q); 0 5 5
3404 17 ▶ for (i = 0; i < nargs; i++) XPUSHs(args[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) XPUSHs(args[i]); 0 12 12
for (i = 0; i < nargs; i++) XPUSHs(args[i]); 12 5 17
3405 5 PUTBACK;
3406 5 (void)call_method(meth, G_SCALAR | G_EVAL | G_DISCARD);
3407 5 SPAGAIN;
3408 5 PUTBACK;
3409 5 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 5 0 5
3410 5 }
3411  
3412 5 static int por_q_lock(pTHX_ SV *q, const char *name, IV lease, IV owner) {
3413 5 dSP;
3414 5 int n, ok = 0;
3415 SV *r;
3416 5 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 5 5
3417 5 ▶ XPUSHs(q);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(q); 0 5 5
3418 5 ▶ XPUSHs(sv_2mortal(newSVpv(name, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(name, 0))); 0 5 5
3419 5 ▶ XPUSHs(sv_2mortal(newSViv(lease)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(lease))); 0 5 5
3420 5 ▶ XPUSHs(sv_2mortal(newSVpvs("owner")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("owner"))); 0 5 5
3421 5 ▶ XPUSHs(sv_2mortal(newSViv(owner)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(owner))); 0 5 5
3422 5 PUTBACK;
3423 5 n = call_method("lock", G_SCALAR | G_EVAL);
3424 5 SPAGAIN;
3425 /* POP FIRST, TEST AFTER. Before 5.32 SvTRUE mentions its argument up to
3426 * five times, so SvTRUE(POPs) pops repeatedly: the grant is read from
3427 * whatever sits below it, the lease reads as lost on a queue that granted
3428 * it, and the walk off the stack base is a SEGV on a debugging perl. */
3429 5 ▶ if (n) { r = POPs; ok = SvTRUE(r) ? 1 : 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (n) { r = POPs; ok = SvTRUE(r) ? 1 : 0; } 5 0 5
3430 5 PUTBACK;
3431 5 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 5 0 5
3432 5 ▶ if (SvTRUE(ERRSV)) ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) ok = 0; 5 0 5
if (SvTRUE(ERRSV)) ok = 0; 0 5 5
3433 5 return ok;
3434 }
3435  
3436 3 static void por_cron_thunk(pTHX_ CV *cv) {
3437 3 dXSARGS;
3438 /* `mg_find`, NOT `mg_findext`, which is 5.14 and this distribution says
3439 * 5.10 - the same trap G_LIST sprang, and one a smoker would have found
3440 * the same way. It is sufficient here rather than merely cheaper: the CV
3441 * was created a line above the magic was attached to it, so it carries
3442 * exactly one, and there is nothing for a vtable to disambiguate. */
3443 3 MAGIC *mg = mg_find((SV *)cv, PERL_MAGIC_ext);
3444 3 ▶ HV *o = (mg && mg->mg_obj && SvROK(mg->mg_obj)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (mg && mg->mg_obj && SvROK(mg->mg_obj) 3 0 3
HV *o = (mg && mg->mg_obj && SvROK(mg->mg_obj) 3 0 3
3445 3 ▶ && SvTYPE(SvRV(mg->mg_obj)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(mg->mg_obj)) == SVt_PVHV) 3 0 3
3446 6 ▶ ? (HV *)SvRV(mg->mg_obj) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(mg->mg_obj) : NULL; 3 0 3
3447 3 ▶ SV *q = items > 0 ? ST(0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *q = items > 0 ? ST(0) : NULL; 3 0 3
3448 SV **f;
3449 3 SV *store = NULL, *keep = NULL, *bytes = NULL;
3450 3 IV lease = 30, owner = 0, unlinked = 0;
3451 3 SV *out = NULL;
3452 3 SV *err = NULL;
3453  
3454 3 ▶ if (!o || !q || !SvOK(q)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); }
Branch: 3 branches
BranchTrueFalseTotal
if (!o || !q || !SvOK(q)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 3 0 3
if (!o || !q || !SvOK(q)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 3 0 3
if (!o || !q || !SvOK(q)) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 0 3 3
3455 3 ▶ f = hv_fetchs(o, "store", 0); if (f && SvOK(*f)) store = *f;
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(o, "store", 0); if (f && SvOK(*f)) store = *f; 3 0 3
f = hv_fetchs(o, "store", 0); if (f && SvOK(*f)) store = *f; 3 0 3
3456 3 ▶ f = hv_fetchs(o, "keep_ns", 0); if (f && SvOK(*f)) keep = *f;
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(o, "keep_ns", 0); if (f && SvOK(*f)) keep = *f; 3 0 3
f = hv_fetchs(o, "keep_ns", 0); if (f && SvOK(*f)) keep = *f; 3 0 3
3457 3 ▶ f = hv_fetchs(o, "bytes", 0); if (f && SvTRUE(*f)) bytes = *f;
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(o, "bytes", 0); if (f && SvTRUE(*f)) bytes = *f; 0 3 3
f = hv_fetchs(o, "bytes", 0); if (f && SvTRUE(*f)) bytes = *f; 0 0 0
3458 3 ▶ f = hv_fetchs(o, "lease", 0); if (f && SvOK(*f)) lease = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(o, "lease", 0); if (f && SvOK(*f)) lease = SvIV(*f); 3 0 3
f = hv_fetchs(o, "lease", 0); if (f && SvOK(*f)) lease = SvIV(*f); 3 0 3
3459 3 ▶ f = hv_fetchs(o, "owner", 0); if (f && SvOK(*f)) owner = SvIV(*f);
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(o, "owner", 0); if (f && SvOK(*f)) owner = SvIV(*f); 3 0 3
f = hv_fetchs(o, "owner", 0); if (f && SvOK(*f)) owner = SvIV(*f); 3 0 3
3460 3 ▶ if (!store) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (!store) { ST(0) = sv_2mortal(newSViv(0)); XSRETURN(1); } 0 3 3
3461  
3462 /* LOSING THE RACE IS THE NORMAL CASE ON A POOL, not an error: one worker
3463 * runs the pass and the rest report nothing done. */
3464 3 ▶ if (!por_q_lock(aTHX_ q, "leader", lease, owner)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!por_q_lock(aTHX_ q, "leader", lease, owner)) { 1 2 3
3465 1 ST(0) = sv_2mortal(newSViv(0));
3466 1 XSRETURN(1);
3467 }
3468  
3469 {
3470 2 dSP;
3471 int n;
3472 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
3473 2 ▶ XPUSHs(sv_2mortal(newSVpvs("store"))); XPUSHs(store);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("store"))); XPUSHs(store); 0 2 2
XPUSHs(sv_2mortal(newSVpvs("store"))); XPUSHs(store); 0 2 2
3474 2 ▶ XPUSHs(sv_2mortal(newSVpvs("keep_ns"))); XPUSHs(keep ? keep : &PL_sv_undef);
Branch: 3 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("keep_ns"))); XPUSHs(keep ? keep : &PL_sv_undef); 0 2 2
XPUSHs(sv_2mortal(newSVpvs("keep_ns"))); XPUSHs(keep ? keep : &PL_sv_undef); 0 2 2
XPUSHs(sv_2mortal(newSVpvs("keep_ns"))); XPUSHs(keep ? keep : &PL_sv_undef); 2 0 2
3475 2 ▶ if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); }
Branch: 3 branches
BranchTrueFalseTotal
if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); } 0 2 2
if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); } 0 0 0
if (bytes) { XPUSHs(sv_2mortal(newSVpvs("bytes"))); XPUSHs(bytes); } 0 0 0
3476 2 PUTBACK;
3477 2 n = call_pv("Punk::Observe::Retain::pass", G_SCALAR | G_EVAL);
3478 2 SPAGAIN;
3479 2 ▶ out = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
out = n ? SvREFCNT_inc(POPs) : NULL; 2 0 2
3480 2 PUTBACK;
3481 2 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2 0 2
3482 2 ▶ if (SvTRUE(ERRSV)) err = newSVsv(ERRSV);
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) err = newSVsv(ERRSV); 2 0 2
if (SvTRUE(ERRSV)) err = newSVsv(ERRSV); 0 2 2
if (SvTRUE(ERRSV)) err = newSVsv(ERRSV); 0 0 0
3483 }
3484  
3485 /* THE LEASE IS RENEWED AND RELEASED WHETHER OR NOT THE PASS THREW. A
3486 * holder that dies still holds it otherwise, and the next worker waits
3487 * out the whole lease for nothing. */
3488 {
3489 SV *a[3];
3490 2 a[0] = sv_2mortal(newSVpvs("leader"));
3491 2 a[1] = sv_2mortal(newSViv(owner));
3492 2 a[2] = sv_2mortal(newSViv(lease));
3493 2 por_q_call(aTHX_ q, "renew_lock", a, 3);
3494 2 por_q_call(aTHX_ q, "unlock", a, 2);
3495 }
3496  
3497 2 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 2 2
3498 0 ▶ if (out) SvREFCNT_dec(out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) SvREFCNT_dec(out); 0 0 0
3499 0 ▶ sv_setsv(ERRSV, err);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, err); 0 0 0
3500 0 SvREFCNT_dec(err);
3501 0 croak(NULL); /* rethrow, preserving the message */
3502 }
3503 2 ▶ if (out) {
Branch: 1 branch
BranchTrueFalseTotal
if (out) { 2 0 2
3504 2 ▶ if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) { 2 0 2
if (SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVHV) { 2 0 2
3505 2 SV **u = hv_fetchs((HV *)SvRV(out), "unlinked", 0);
3506 2 ▶ if (u && SvOK(*u)) unlinked = SvIV(*u);
Branch: 2 branches
BranchTrueFalseTotal
if (u && SvOK(*u)) unlinked = SvIV(*u); 2 0 2
if (u && SvOK(*u)) unlinked = SvIV(*u); 2 0 2
3507 }
3508 2 SvREFCNT_dec(out);
3509 }
3510 2 ST(0) = sv_2mortal(newSViv(unlinked));
3511 2 XSRETURN(1);
3512 }
3513  
3514 /* `Punk::Plugin::Observe->state_for($class)`, or NULL when the worker is not
3515 * running the same application the server compiled. */
3516 3 static SV *por_state_for(pTHX_ SV *class) {
3517 3 dSP;
3518 3 SV *r = NULL;
3519 int n;
3520 3 load_module(PERL_LOADMOD_NOIMPORT,
3521 newSVpvs("Punk::Plugin::Observe"), NULL, NULL);
3522 3 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 3 3
3523 3 ▶ XPUSHs(sv_2mortal(newSVpvs("Punk::Plugin::Observe")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("Punk::Plugin::Observe"))); 0 3 3
3524 3 ▶ XPUSHs(class);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(class); 0 3 3
3525 3 PUTBACK;
3526 3 n = call_method("state_for", G_SCALAR | G_EVAL);
3527 3 SPAGAIN;
3528 3 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 3 0 3
3529 3 PUTBACK;
3530 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
3531 3 ▶ if (SvTRUE(ERRSV) || (r && !SvROK(r))) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 3 0 3
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 3 0 3
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 3 0 3
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 1 2 3
3532 1 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 1 0 1
3533 1 r = NULL;
3534 }
3535 3 return r;
3536 }
3537  
3538 /* The store and the window the plugin was configured with. Borrowed from the
3539 * state rather than copied: the caller uses them within this call. */
3540 1 static void por_job_opts(pTHX_ SV *st, SV **store, SV **keep, SV **bytes) {
3541 HV *h;
3542 SV **f, **ro;
3543 1 *store = *keep = *bytes = NULL;
3544 1 ▶ if (!st || !SvROK(st) || SvTYPE(SvRV(st)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!st || !SvROK(st) || SvTYPE(SvRV(st)) != SVt_PVHV) return; 1 0 1
if (!st || !SvROK(st) || SvTYPE(SvRV(st)) != SVt_PVHV) return; 1 0 1
if (!st || !SvROK(st) || SvTYPE(SvRV(st)) != SVt_PVHV) return; 0 1 1
3545 1 h = (HV *)SvRV(st);
3546  
3547 { /* store_for($st, $tenant): the tenant's own directory, made on
3548 * demand, which is where a fixed-tenant mount keeps its data. */
3549 1 dSP;
3550 1 SV *tenant = NULL;
3551 int n;
3552 1 f = hv_fetchs(h, "tenant", 0);
3553 1 ▶ if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 1 0 1
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 1 0 1
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 1 0 1
3554 1 SV **fx = hv_fetchs((HV *)SvRV(*f), "fixed", 0);
3555 1 ▶ if (fx && SvTRUE(*fx)) tenant = *fx;
Branch: 2 branches
BranchTrueFalseTotal
if (fx && SvTRUE(*fx)) tenant = *fx; 1 0 1
if (fx && SvTRUE(*fx)) tenant = *fx; 1 0 1
3556 }
3557 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
3558 1 ▶ XPUSHs(st);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(st); 0 1 1
3559 1 ▶ XPUSHs(tenant ? tenant : sv_2mortal(newSVpvs("default")));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(tenant ? tenant : sv_2mortal(newSVpvs("default"))); 0 1 1
XPUSHs(tenant ? tenant : sv_2mortal(newSVpvs("default"))); 0 1 1
3560 1 PUTBACK;
3561 1 SV *got = NULL;
3562 1 n = call_pv("Punk::Plugin::Observe::store_for", G_SCALAR | G_EVAL);
3563 1 SPAGAIN;
3564 1 ▶ if (n) { SV *s = POPs; if (SvOK(s)) got = SvREFCNT_inc(s); }
Branch: 2 branches
BranchTrueFalseTotal
if (n) { SV *s = POPs; if (SvOK(s)) got = SvREFCNT_inc(s); } 1 0 1
if (n) { SV *s = POPs; if (SvOK(s)) got = SvREFCNT_inc(s); } 1 0 1
3565 1 PUTBACK;
3566 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
3567 /* MORTALISED AFTER THE SCOPE CLOSES, not inside it. A mortal made
3568 * between SAVETMPS and FREETMPS is freed by that FREETMPS, and the
3569 * caller was handed a store that had already gone - which arrived as
3570 * "pass needs a store" from a call that plainly passed one. */
3571 1 ▶ if (got) *store = sv_2mortal(got);
Branch: 1 branch
BranchTrueFalseTotal
if (got) *store = sv_2mortal(got); 1 0 1
3572 }
3573  
3574 1 ro = hv_fetchs(h, "retain_opts", 0);
3575 1 ▶ if (ro && SvROK(*ro) && SvTYPE(SvRV(*ro)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (ro && SvROK(*ro) && SvTYPE(SvRV(*ro)) == SVt_PVHV) { 1 0 1
if (ro && SvROK(*ro) && SvTYPE(SvRV(*ro)) == SVt_PVHV) { 1 0 1
if (ro && SvROK(*ro) && SvTYPE(SvRV(*ro)) == SVt_PVHV) { 1 0 1
3576 1 HV *r = (HV *)SvRV(*ro);
3577 1 ▶ f = hv_fetchs(r, "keep_ns", 0); if (f && SvOK(*f)) *keep = *f;
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(r, "keep_ns", 0); if (f && SvOK(*f)) *keep = *f; 1 0 1
f = hv_fetchs(r, "keep_ns", 0); if (f && SvOK(*f)) *keep = *f; 1 0 1
3578 1 ▶ f = hv_fetchs(r, "bytes", 0); if (f && SvTRUE(*f)) *bytes = *f;
Branch: 2 branches
BranchTrueFalseTotal
f = hv_fetchs(r, "bytes", 0); if (f && SvTRUE(*f)) *bytes = *f; 0 1 1
f = hv_fetchs(r, "bytes", 0); if (f && SvTRUE(*f)) *bytes = *f; 0 0 0
3579 }
3580 }
3581  
3582 /* `$store->retain(bytes => N)` - the store's own byte-budget pass. */
3583 1 static SV *por_call_retain(pTHX_ SV *store, SV *bytes) {
3584 1 dSP;
3585 1 SV *r = NULL;
3586 int n;
3587 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
3588 1 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 1 1
3589 1 ▶ XPUSHs(sv_2mortal(newSVpvs("bytes")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("bytes"))); 0 1 1
3590 1 ▶ XPUSHs(sv_2mortal(newSVsv(bytes)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(bytes))); 0 1 1
3591 1 PUTBACK;
3592 1 n = call_method("retain", G_SCALAR | G_EVAL);
3593 1 SPAGAIN;
3594 1 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 1 0 1
3595 1 PUTBACK;
3596 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
3597 1 ▶ if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; }
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; } 1 0 1
if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; } 0 1 1
if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; } 0 0 0
3598 1 return r;
3599 }
3600  
3601 /* `Punk::Observe::Retain::adopt_orphans` from inside `pass`, so the two share
3602 * one implementation rather than the pass growing a second copy. */
3603 13 static SV *por_adopt(pTHX_ SV *store, int dry) {
3604 13 dSP;
3605 13 SV *r = NULL;
3606 int n;
3607 13 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 13 13
3608 13 ▶ XPUSHs(sv_2mortal(newSVpvs("store")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("store"))); 0 13 13
3609 13 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 13 13
3610 13 ▶ XPUSHs(sv_2mortal(newSVpvs("dry_run")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("dry_run"))); 0 13 13
3611 13 ▶ XPUSHs(sv_2mortal(newSViv(dry)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(dry))); 0 13 13
3612 13 PUTBACK;
3613 13 n = call_pv("Punk::Observe::Retain::adopt_orphans", G_SCALAR | G_EVAL);
3614 13 SPAGAIN;
3615 13 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 13 0 13
3616 13 PUTBACK;
3617 13 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 13 0 13
3618 13 ▶ if (SvTRUE(ERRSV) || (r && !SvROK(r))) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 13 0 13
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 13 0 13
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 13 0 13
if (SvTRUE(ERRSV) || (r && !SvROK(r))) { 0 13 13
3619 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
3620 0 r = NULL;
3621 }
3622 13 return r;
3623 }
3624  
3625 /* Does this object answer to that method? Used where a newer name is
3626 * preferred and an older one still has to work. */
3627 272 static int povw_can(pTHX_ SV *obj, const char *name) {
3628 HV *stash;
3629 272 ▶ if (!obj || !SvROK(obj)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!obj || !SvROK(obj)) return 0; 272 0 272
if (!obj || !SvROK(obj)) return 0; 0 272 272
3630 272 stash = SvSTASH(SvRV(obj));
3631 272 ▶ if (!stash) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) return 0; 0 272 272
3632 272 return gv_fetchmethod_autoload(stash, name, 0) ? 1 : 0;
3633 }
3634  
3635 /* THE READ METHOD: `cached_query` where the store has it, `query` otherwise.
3636 *
3637 * The two differ only in whether the settled part of the window is recomputed,
3638 * and `cached_query` decides for itself whether a query can be split at all -
3639 * so a caller never has to know which it is getting. A store is a seam a host
3640 * may implement itself, which is why asking for the newer name must not be a
3641 * requirement to grow it. */
3642 88 static const char *povw_read_method(pTHX_ SV *store) {
3643 88 ▶ return povw_can(aTHX_ store, "cached_query") ? "cached_query" : "query";
Branch: 1 branch
BranchTrueFalseTotal
return povw_can(aTHX_ store, "cached_query") ? "cached_query" : "query"; 54 34 88
3644 }
3645  
3646 /* THE BUDGET AN AGGREGATE NEEDS, pushed onto a call being built.
3647 *
3648 * A PARTIAL GRAPH IS A POINTLESS GRAPH: one that stops scanning mid-window
3649 * draws some other window and labels it with this one. The store's budgets
3650 * default to 500,000 when left unset, so a chart that says nothing has to say
3651 * so explicitly - these are the "no ceiling" spellings of all three.
3652 *
3653 * It is also what keeps the chart cheap. A chunk that truncates is never
3654 * cached, because a short answer stored answers short for the whole life of
3655 * the entry - so a figure left on the default ceiling has its busiest chunk
3656 * rescanned on every single request, for ever. Measured on a 10GB store: one
3657 * chunk of twenty-four hit the ceiling, and the figure never got faster than
3658 * its cold cost until it was told not to stop. */
3659 #define POVW_NO_CEILING() STMT_START { \
3660 XPUSHs(sv_2mortal(newSVpvs("limit"))); \
3661 XPUSHs(sv_2mortal(newSViv((IV)1 << 62))); \
3662 XPUSHs(sv_2mortal(newSVpvs("hard_max"))); \
3663 XPUSHs(sv_2mortal(newSViv((IV)1 << 62))); \
3664 XPUSHs(sv_2mortal(newSVpvs("max_rows"))); \
3665 XPUSHs(sv_2mortal(newSViv((IV)1 << 62))); \
3666 } STMT_END
3667  
3668 36 static void povw_panel_fill(pTHX_ HV *panel, SV *src_panel, SV *store,
3669 SV *from, SV *to) {
3670 36 dSP;
3671 SV **x;
3672 36 SV *chk = NULL;
3673 36 const char *q = "";
3674 36 STRLEN qlen = 0;
3675  
3676 36 x = hv_fetchs(panel, "query", 0);
3677 72 ▶ if (x && SvOK(*x)) q = SvPV(*x, qlen);
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvOK(*x)) q = SvPV(*x, qlen); 36 0 36
if (x && SvOK(*x)) q = SvPV(*x, qlen); 36 0 36
3678  
3679 /* THE QUERY IS VALIDATED BY THE PARSER THAT WILL RUN IT. A panel saved
3680 * with a query nothing can execute is a dashboard broken for everybody
3681 * who opens it and for nobody who saved it. */
3682 {
3683 int n2;
3684 36 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 36 36
3685 36 ▶ XPUSHs(src_panel);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(src_panel); 0 36 36
3686 36 PUTBACK;
3687 36 n2 = call_pv("Punk::Observe::Dashboard::check_panel",
3688 G_SCALAR | G_EVAL);
3689 36 SPAGAIN;
3690 36 ▶ chk = n2 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
chk = n2 ? SvREFCNT_inc(POPs) : NULL; 36 0 36
3691 36 PUTBACK;
3692 36 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 36 0 36
3693 36 SPAGAIN;
3694 }
3695  
3696 36 ▶ if (chk && SvROK(chk) && SvTYPE(SvRV(chk)) == SVt_PVHV
Branch: 3 branches
BranchTrueFalseTotal
if (chk && SvROK(chk) && SvTYPE(SvRV(chk)) == SVt_PVHV 36 0 36
if (chk && SvROK(chk) && SvTYPE(SvRV(chk)) == SVt_PVHV 36 0 36
if (chk && SvROK(chk) && SvTYPE(SvRV(chk)) == SVt_PVHV 36 0 36
3697 36 ▶ && (!(x = hv_fetchs((HV *)SvRV(chk), "ok", 0))
Branch: 1 branch
BranchTrueFalseTotal
&& (!(x = hv_fetchs((HV *)SvRV(chk), "ok", 0)) 36 0 36
3698 37 ▶ || !SvTRUE(*x))) {
Branch: 1 branch
BranchTrueFalseTotal
|| !SvTRUE(*x))) { 1 35 36
3699 1 SV **e = hv_fetchs((HV *)SvRV(chk), "error", 0);
3700 1 ▶ hv_stores(panel, "refusal", (e && SvOK(*e))
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(panel, "refusal", (e && SvOK(*e)) 1 0 1
hv_stores(panel, "refusal", (e && SvOK(*e)) 1 0 1
3701 ? newSVsv(*e)
3702 : newSVpvs("that panel query is not valid"));
3703 }
3704 35 ▶ else if (SvOK(store) && SvROK(store)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvOK(store) && SvROK(store)) { 35 0 35
else if (SvOK(store) && SvROK(store)) { 35 0 35
3705 35 SV *r = NULL;
3706 int n2;
3707 35 int plain_rows = 0;
3708  
3709 /* THE BUDGET FOLLOWS THE SHAPE. A rows panel shows twenty lines, so
3710 * a 20,000-row newest-first scan is generous; an AGGREGATE eats
3711 * every row in the window, and the same budget silently narrowed a
3712 * 6h chart to the newest fifteen minutes of a busy store - the
3713 * range picker changed the URL and not the answer. So only a PLAIN
3714 * rows query (where, search, limit, sort) is capped; anything that
3715 * aggregates or ranks gets the store's own default, exactly what
3716 * the explorer gives the identical query. Decided by kind rather
3717 * than by naming the aggregate stages, so a future stage defaults
3718 * to the full window, not to a silent sliver. */
3719 {
3720 po_query pq;
3721 35 ▶ if (po_parse(&pq, q, (size_t)qlen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_parse(&pq, q, (size_t)qlen)) { 35 0 35
3722 po_stage *stg;
3723 35 plain_rows = 1;
3724 42 ▶ for (stg = pq.stages; stg; stg = stg->next)
Branch: 1 branch
BranchTrueFalseTotal
for (stg = pq.stages; stg; stg = stg->next) 18 24 42
3725 18 ▶ if (stg->kind != PO_ST_WHERE
Branch: 1 branch
BranchTrueFalseTotal
if (stg->kind != PO_ST_WHERE 17 1 18
3726 17 ▶ && stg->kind != PO_ST_SEARCH
Branch: 1 branch
BranchTrueFalseTotal
&& stg->kind != PO_ST_SEARCH 17 0 17
3727 17 ▶ && stg->kind != PO_ST_LIMIT
Branch: 1 branch
BranchTrueFalseTotal
&& stg->kind != PO_ST_LIMIT 11 6 17
3728 11 ▶ && stg->kind != PO_ST_SORT) {
Branch: 1 branch
BranchTrueFalseTotal
&& stg->kind != PO_ST_SORT) { 11 0 11
3729 11 plain_rows = 0;
3730 11 break;
3731 }
3732 35 po_query_free(&pq);
3733 }
3734 }
3735  
3736 35 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 35 35
3737 35 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 35 35
3738 35 ▶ XPUSHs(sv_2mortal(newSVpvn(q, qlen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(q, qlen))); 0 35 35
3739 35 ▶ XPUSHs(sv_2mortal(newSVpvs("from")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("from"))); 0 35 35
3740 35 ▶ XPUSHs(sv_2mortal(newSVsv(from)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(from))); 0 35 35
3741 35 ▶ XPUSHs(sv_2mortal(newSVpvs("to")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("to"))); 0 35 35
3742 35 ▶ XPUSHs(sv_2mortal(newSVsv(to)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(to))); 0 35 35
3743 35 ▶ if (plain_rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (plain_rows) { 24 11 35
3744 24 ▶ XPUSHs(sv_2mortal(newSVpvs("limit")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("limit"))); 0 24 24
3745 24 ▶ XPUSHs(sv_2mortal(newSViv(POVW_PANEL_ROWS)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(POVW_PANEL_ROWS))); 0 24 24
3746 }
3747 else {
3748 /* A PARTIAL GRAPH IS A POINTLESS GRAPH. An aggregate that stops
3749 * scanning mid-window draws a chart of some other window and
3750 * labels it with this one - so an aggregate panel scans
3751 * everything the range asks for. The store's budgets default to
3752 * 500,000 when left unset; these are the explicit "no ceiling"
3753 * spellings of all three. */
3754 11 ▶ XPUSHs(sv_2mortal(newSVpvs("limit")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("limit"))); 0 11 11
3755 11 ▶ XPUSHs(sv_2mortal(newSViv((IV)1 << 62)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv((IV)1 << 62))); 0 11 11
3756 11 ▶ XPUSHs(sv_2mortal(newSVpvs("hard_max")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("hard_max"))); 0 11 11
3757 11 ▶ XPUSHs(sv_2mortal(newSViv((IV)1 << 62)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv((IV)1 << 62))); 0 11 11
3758 11 ▶ XPUSHs(sv_2mortal(newSVpvs("max_rows")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("max_rows"))); 0 11 11
3759 11 ▶ XPUSHs(sv_2mortal(newSViv((IV)1 << 62)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv((IV)1 << 62))); 0 11 11
3760 }
3761 35 PUTBACK;
3762 /* THE CACHED PATH, WHICH IS THE PLAIN ONE WHEN NO CACHE IS
3763 * CONFIGURED. A panel is the query that is re-run most and changes
3764 * least - the same twenty-four hours re-scanned on every refresh, of
3765 * which all but the last few minutes has settled. `cached_query`
3766 * decides for itself whether the query can be split; it is not this
3767 * layer's business to know. */
3768 35 n2 = call_method(povw_read_method(aTHX_ store), G_SCALAR);
3769 35 SPAGAIN;
3770 35 ▶ r = n2 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n2 ? SvREFCNT_inc(POPs) : NULL; 35 0 35
3771 35 PUTBACK;
3772 35 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 35 0 35
3773 35 SPAGAIN;
3774  
3775 35 ▶ if (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVHV) { 35 0 35
if (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVHV) { 35 0 35
if (r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVHV) { 35 0 35
3776 35 HV *rh = (HV *)SvRV(r);
3777 35 SV **ok = hv_fetchs(rh, "ok", 0);
3778 35 ▶ if (!ok || !SvTRUE(*ok)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!ok || !SvTRUE(*ok)) { 35 0 35
if (!ok || !SvTRUE(*ok)) { 0 35 35
3779 0 SV **e = hv_fetchs(rh, "error", 0);
3780 0 ▶ hv_stores(panel, "refusal", e ? newSVsv(*e) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(panel, "refusal", e ? newSVsv(*e) : newSV(0)); 0 0 0
3781 }
3782 else {
3783 35 SV **sh = hv_fetchs(rh, "shape", 0);
3784 35 ▶ int rows = sh && SvOK(*sh)
Branch: 1 branch
BranchTrueFalseTotal
int rows = sh && SvOK(*sh) 35 0 35
3785 105 ▶ && strEQ(SvPV_nolen(*sh), "rows");
Branch: 2 branches
BranchTrueFalseTotal
&& strEQ(SvPV_nolen(*sh), "rows"); 35 0 35
&& strEQ(SvPV_nolen(*sh), "rows"); 18 17 35
3786 35 ▶ int buckets = sh && SvOK(*sh)
Branch: 1 branch
BranchTrueFalseTotal
int buckets = sh && SvOK(*sh) 35 0 35
3787 105 ▶ && strEQ(SvPV_nolen(*sh), "buckets");
Branch: 2 branches
BranchTrueFalseTotal
&& strEQ(SvPV_nolen(*sh), "buckets"); 35 0 35
&& strEQ(SvPV_nolen(*sh), "buckets"); 10 25 35
3788 35 int mismatch = 0;
3789  
3790 /* A TRUNCATED AGGREGATE IS A DIFFERENT ANSWER. Newest-first,
3791 * a truncated rows panel still shows the true newest twenty;
3792 * a truncated aggregate covers a sliver of the window and
3793 * looks complete. The chart cannot say it, so the panel
3794 * does. */
3795 35 ▶ if (!rows) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rows) { 17 18 35
3796 17 SV **mv2 = hv_fetchs(rh, "meta", 0);
3797 17 ▶ HV *mh2 = (mv2 && SvROK(*mv2)
Branch: 1 branch
BranchTrueFalseTotal
HV *mh2 = (mv2 && SvROK(*mv2) 17 0 17
3798 17 ▶ && SvTYPE(SvRV(*mv2)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*mv2)) == SVt_PVHV) 17 0 17
3799 34 ▶ ? (HV *)SvRV(*mv2) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*mv2) : NULL; 17 0 17
3800 17 SV **tr2 = mh2
3801 17 ? hv_fetchs(mh2, "truncated", 0)
3802 17 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 17 0 17
3803 17 ▶ if (tr2 && SvTRUE(*tr2))
Branch: 2 branches
BranchTrueFalseTotal
if (tr2 && SvTRUE(*tr2)) 6 11 17
if (tr2 && SvTRUE(*tr2)) 1 5 6
3804 1 povw_set_count(aTHX_ panel, "partial",
3805 mh2, "scanned_rows");
3806 }
3807 {
3808 /* THE QUERY'S OWN `| viz` WINS over the panel column.
3809 * Both name a chart; when they disagree, the one written
3810 * next to the question is the one somebody meant - and
3811 * it is the one a saved view or a pasted explorer URL
3812 * carries. */
3813 35 SV **qv = hv_fetchs(rh, "viz", 0);
3814 35 ▶ if (qv && SvOK(*qv))
Branch: 2 branches
BranchTrueFalseTotal
if (qv && SvOK(*qv)) 1 34 35
if (qv && SvOK(*qv)) 1 0 1
3815 1 hv_stores(panel, "viz", newSVsv(*qv));
3816 }
3817 {
3818 35 SV **vz2 = hv_fetchs(panel, "viz", 0);
3819 35 ▶ const char *vv = (vz2 && SvOK(*vz2))
Branch: 1 branch
BranchTrueFalseTotal
const char *vv = (vz2 && SvOK(*vz2)) 35 0 35
3820 105 ▶ ? SvPV_nolen(*vz2) : "line";
Branch: 1 branch
BranchTrueFalseTotal
? SvPV_nolen(*vz2) : "line"; 35 0 35
3821 35 ▶ int groups_shape = !rows && !buckets;
Branch: 2 branches
BranchTrueFalseTotal
int groups_shape = !rows && !buckets; 17 18 35
int groups_shape = !rows && !buckets; 7 10 17
3822 /* A CHART KIND THE ANSWER CANNOT TAKE IS REFUSED, NOT
3823 * QUIETLY SUBSTITUTED. `bar` over a plain grouped answer
3824 * is one bar per group - the chart the metrics page
3825 * already draws for exactly this shape. */
3826 70 mismatch = (strEQ(vv, "area")
3827 32 ▶ || strEQ(vv, "stat")
Branch: 1 branch
BranchTrueFalseTotal
|| strEQ(vv, "stat") 29 3 32
3828 29 ▶ || (strEQ(vv, "bar")
Branch: 1 branch
BranchTrueFalseTotal
|| (strEQ(vv, "bar") 4 25 29
3829 67 ▶ && !groups_shape)) && !buckets;
Branch: 3 branches
BranchTrueFalseTotal
&& !groups_shape)) && !buckets; 32 3 35
&& !groups_shape)) && !buckets; 2 2 4
&& !groups_shape)) && !buckets; 5 3 8
3830 35 ▶ if (mismatch)
Branch: 1 branch
BranchTrueFalseTotal
if (mismatch) 5 30 35
3831 5 hv_stores(panel, "refusal",
3832 newSVpvf("%s panels need a bucketed answer - "
3833 "add | bucket(30s) to the query", vv));
3834 }
3835  
3836 /* NOT an early return on mismatch: a refused panel still has
3837 * to reach the page, or the reader loses the one thing that
3838 * would tell them why it is not there. */
3839 35 ▶ if (mismatch) { }
Branch: 1 branch
BranchTrueFalseTotal
if (mismatch) { } 30 5 35
3840 30 ▶ else if (buckets) {
Branch: 1 branch
BranchTrueFalseTotal
else if (buckets) { 10 20 30
3841 /* 13, 17, 5, 9 is not information, and the legend needs
3842 * the names as much as the table does - so the result is
3843 * renamed BEFORE either is built. */
3844 10 ▶ if (povw_by_severity(q, (size_t)qlen))
Branch: 1 branch
BranchTrueFalseTotal
if (povw_by_severity(q, (size_t)qlen)) 2 8 10
3845 2 povw_name_severities(aTHX_ r);
3846  
3847 10 povw_fill_vars(aTHX_ panel,
3848 "Punk::Observe::Plot::bucket_vars", r);
3849 }
3850 20 ▶ else if (rows) {
Branch: 1 branch
BranchTrueFalseTotal
else if (rows) { 15 5 20
3851 /* ROWS ARE NOT ALWAYS A LINE. The chart path plots each
3852 * row's `value`, which a metric row has and a log or
3853 * span row does not. Valueless rows render as a TABLE,
3854 * and `viz table` forces one even for metric rows. */
3855 15 SV **rv = hv_fetchs(rh, "rows", 0);
3856 15 ▶ AV *ra = (rv && SvROK(*rv)
Branch: 1 branch
BranchTrueFalseTotal
AV *ra = (rv && SvROK(*rv) 15 0 15
3857 15 ▶ && SvTYPE(SvRV(*rv)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*rv)) == SVt_PVAV) 15 0 15
3858 30 ▶ ? (AV *)SvRV(*rv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*rv) : NULL; 15 0 15
3859 15 ▶ SSize_t nr = ra ? av_len(ra) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t nr = ra ? av_len(ra) + 1 : 0; 15 0 15
3860 15 int has_val = 0, want_table = 0;
3861 15 ▶ if (nr) {
Branch: 1 branch
BranchTrueFalseTotal
if (nr) { 8 7 15
3862 8 SV **e0 = av_fetch(ra, 0, 0);
3863 8 ▶ SV **v0 = (e0 && SvROK(*e0))
Branch: 1 branch
BranchTrueFalseTotal
SV **v0 = (e0 && SvROK(*e0)) 8 0 8
3864 16 ▶ ? hv_fetchs((HV *)SvRV(*e0), "value", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? hv_fetchs((HV *)SvRV(*e0), "value", 0) : NULL; 8 0 8
3865 8 ▶ has_val = v0 && SvOK(*v0);
Branch: 2 branches
BranchTrueFalseTotal
has_val = v0 && SvOK(*v0); 2 6 8
has_val = v0 && SvOK(*v0); 2 0 2
3866 }
3867 {
3868 15 SV **vz3 = hv_fetchs(panel, "viz", 0);
3869 15 ▶ want_table = vz3 && SvOK(*vz3)
Branch: 1 branch
BranchTrueFalseTotal
want_table = vz3 && SvOK(*vz3) 15 0 15
3870 45 ▶ && strEQ(SvPV_nolen(*vz3), "table");
Branch: 2 branches
BranchTrueFalseTotal
&& strEQ(SvPV_nolen(*vz3), "table"); 15 0 15
&& strEQ(SvPV_nolen(*vz3), "table"); 6 9 15
3871 }
3872 16 ▶ if (has_val && !want_table) {
Branch: 2 branches
BranchTrueFalseTotal
if (has_val && !want_table) { 2 13 15
if (has_val && !want_table) { 1 1 2
3873 1 SV *series = NULL;
3874 int n3;
3875 1 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 1 1
3876 1 ▶ XPUSHs(rv ? *rv
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(rv ? *rv 0 1 1
XPUSHs(rv ? *rv 1 0 1
3877 : sv_2mortal(newRV_noinc((SV *)newAV())));
3878 1 ▶ XPUSHs(sv_2mortal(newSVsv(from)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(from))); 0 1 1
3879 1 ▶ XPUSHs(sv_2mortal(newSVsv(to)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(to))); 0 1 1
3880 1 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)panel)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)panel))); 0 1 1
3881 1 PUTBACK;
3882 1 n3 = call_pv("Punk::Observe::View::_series_paths",
3883 G_SCALAR);
3884 1 SPAGAIN;
3885 1 ▶ series = n3 ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
series = n3 ? SvREFCNT_inc(POPs) : NULL; 1 0 1
3886 1 PUTBACK;
3887 1 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1 0 1
3888 1 SPAGAIN;
3889 1 ▶ if (series) hv_stores(panel, "series", series);
Branch: 1 branch
BranchTrueFalseTotal
if (series) hv_stores(panel, "series", series); 1 0 1
3890 }
3891 else {
3892 /* A GLANCE, NOT A LOG VIEWER: twenty rows, newest as
3893 * the result orders them. The explorer link under
3894 * the panel is the way to the rest. */
3895 14 AV *out = newAV();
3896 14 SSize_t i2, mx = nr < 20 ? nr : 20;
3897 59 ▶ for (i2 = 0; i2 < mx; i2++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i2 = 0; i2 < mx; i2++) { 45 14 59
3898 45 SV **e = av_fetch(ra, i2, 0);
3899 HV *row, *o;
3900 SV **x3;
3901 char tb[64];
3902 size_t tn;
3903 45 const char *tp = "0";
3904 45 STRLEN tl = 1;
3905 45 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 45 0 45
if (!e || !SvROK(*e)) continue; 0 45 45
3906 45 row = (HV *)SvRV(*e);
3907 45 o = newHV();
3908 45 x3 = hv_fetchs(row, "t", 0);
3909 45 ▶ if (x3 && SvOK(*x3))
Branch: 2 branches
BranchTrueFalseTotal
if (x3 && SvOK(*x3)) 45 0 45
if (x3 && SvOK(*x3)) 45 0 45
3910 90 tp = SvPV(*x3, tl);
3911 45 tn = po_fmt_time(tp, (size_t)tl, tb);
3912 45 hv_stores(o, "time", newSVpvn(tb, tn));
3913 45 x3 = hv_fetchs(row, "service", 0);
3914 45 ▶ hv_stores(o, "service", x3
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(o, "service", x3 45 0 45
3915 ? newSVsv(*x3) : newSVpvs(""));
3916 45 x3 = hv_fetchs(row, "body", 0);
3917 45 ▶ hv_stores(o, "body",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(o, "body", 45 0 45
hv_stores(o, "body", 45 0 45
3918 (x3 && SvOK(*x3))
3919 ? newSVsv(*x3) : newSVpvs(""));
3920 45 x3 = hv_fetchs(row, "value", 0);
3921 45 ▶ hv_stores(o, "value",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(o, "value", 20 25 45
hv_stores(o, "value", 20 0 20
3922 (x3 && SvOK(*x3))
3923 ? povw_num(aTHX_ *x3)
3924 : newSVpvs(""));
3925 45 av_push(out, newRV_noinc((SV *)o));
3926 }
3927 14 hv_stores(panel, "rows", newRV_noinc((SV *)out));
3928 14 hv_stores(panel, "has_value", newSViv(has_val));
3929 14 ▶ hv_stores(panel, "more",
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(panel, "more", 2 12 14
3930 newSViv(nr > mx ? (IV)nr : 0));
3931 /* HOW MANY ARE ACTUALLY SHOWN, so a cap change
3932 * cannot leave the note claiming eight. */
3933 14 hv_stores(panel, "shown", newSViv((IV)mx));
3934 }
3935 }
3936 else {
3937 5 AV *groups = newAV();
3938 5 SV **gv = hv_fetchs(rh, "groups", 0);
3939  
3940 /* One bar per group, when that is what the panel asked
3941 * for - the same figure the explorer draws. */
3942 5 SV **pv3 = hv_fetchs(panel, "viz", 0);
3943 5 ▶ if (pv3 && SvOK(*pv3)
Branch: 2 branches
BranchTrueFalseTotal
if (pv3 && SvOK(*pv3) 5 0 5
if (pv3 && SvOK(*pv3) 5 0 5
3944 10 ▶ && strEQ(SvPV_nolen(*pv3), "bar")
Branch: 1 branch
BranchTrueFalseTotal
&& strEQ(SvPV_nolen(*pv3), "bar") 2 3 5
3945 2 ▶ && gv && SvROK(*gv)) {
Branch: 2 branches
BranchTrueFalseTotal
&& gv && SvROK(*gv)) { 2 0 2
&& gv && SvROK(*gv)) { 2 0 2
3946 2 SV *fig3 = NULL, *enc3 = NULL;
3947 int n3b;
3948 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
3949 2 ▶ XPUSHs(*gv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(*gv); 0 2 2
3950 2 PUTBACK;
3951 2 n3b = call_pv("Punk::Observe::Plot::bars", G_SCALAR);
3952 2 SPAGAIN;
3953 2 ▶ fig3 = n3b ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
fig3 = n3b ? SvREFCNT_inc(POPs) : NULL; 2 0 2
3954 2 ▶ PUTBACK; FREETMPS; LEAVE; SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; SPAGAIN; 2 0 2
3955 2 ▶ if (fig3) {
Branch: 1 branch
BranchTrueFalseTotal
if (fig3) { 2 0 2
3956 2 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 2 2
3957 2 ▶ XPUSHs(sv_2mortal(fig3));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(fig3)); 0 2 2
3958 2 PUTBACK;
3959 2 n3b = call_pv("Punk::Observe::Plot::encode",
3960 G_SCALAR);
3961 2 SPAGAIN;
3962 2 ▶ enc3 = n3b ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
enc3 = n3b ? SvREFCNT_inc(POPs) : NULL; 2 0 2
3963 2 ▶ PUTBACK; FREETMPS; LEAVE; SPAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; SPAGAIN; 2 0 2
3964 2 ▶ if (enc3)
Branch: 1 branch
BranchTrueFalseTotal
if (enc3) 2 0 2
3965 2 hv_stores(panel, "series_plot", enc3);
3966 }
3967 }
3968 5 ▶ if (gv && SvROK(*gv)
Branch: 2 branches
BranchTrueFalseTotal
if (gv && SvROK(*gv) 5 0 5
if (gv && SvROK(*gv) 5 0 5
3969 5 ▶ && SvTYPE(SvRV(*gv)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*gv)) == SVt_PVAV) { 5 0 5
3970 5 AV *ga = (AV *)SvRV(*gv);
3971 5 SSize_t j, m = av_len(ga) + 1;
3972 7 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 2 5 7
3973 2 SV **e = av_fetch(ga, j, 0);
3974 HV *g, *o;
3975 SV **y;
3976 2 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 2 0 2
if (!e || !SvROK(*e)) continue; 0 2 2
3977 2 g = (HV *)SvRV(*e);
3978 2 o = newHV();
3979 2 y = hv_fetchs(g, "key", 0);
3980 2 ▶ hv_stores(o, "key", y ? newSVsv(*y) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(o, "key", y ? newSVsv(*y) : newSV(0)); 2 0 2
3981 2 y = hv_fetchs(g, "value", 0);
3982 2 ▶ hv_stores(o, "value", y
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(o, "value", y 2 0 2
3983 ? povw_fmt_value(aTHX_ (double)SvNV(*y))
3984 : newSVpvs("0"));
3985 2 av_push(groups, newRV_noinc((SV *)o));
3986 }
3987 }
3988 5 hv_stores(panel, "groups", newRV_noinc((SV *)groups));
3989 }
3990 }
3991 }
3992 35 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 35 0 35
3993 }
3994 36 ▶ if (chk) SvREFCNT_dec(chk);
Branch: 1 branch
BranchTrueFalseTotal
if (chk) SvREFCNT_dec(chk); 36 0 36
3995 36 }
3996  
3997 /* The panel's template hashref from its source hashref: the metadata every
3998 * render needs, body or no body. The KEY is what a deferred URL addresses:
3999 * the SQL id when the panel has one (stable across reordering), else the
4000 * index in the sorted panel list - a seam-supplied dashboard owes nobody an
4001 * id column. */
4002 62 static HV *povw_panel_meta(pTHX_ HV *p, SSize_t idx) {
4003 62 HV *panel = newHV();
4004 SV **x;
4005 62 const char *q = "";
4006 62 STRLEN qlen = 0;
4007  
4008 62 x = hv_fetchs(p, "title", 0);
4009 62 ▶ hv_stores(panel, "title", x ? newSVsv(*x) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(panel, "title", x ? newSVsv(*x) : newSV(0)); 62 0 62
4010 62 x = hv_fetchs(p, "span", 0);
4011 62 ▶ hv_stores(panel, "span",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(panel, "span", 32 30 62
hv_stores(panel, "span", 32 0 32
4012 newSViv(povw_grid((x && SvOK(*x)) ? SvIV(*x) : 0, 1)));
4013 62 x = hv_fetchs(p, "query", 0);
4014 124 ▶ if (x && SvOK(*x)) q = SvPV(*x, qlen);
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvOK(*x)) q = SvPV(*x, qlen); 62 0 62
if (x && SvOK(*x)) q = SvPV(*x, qlen); 62 0 62
4015 {
4016 char *esc;
4017 size_t en;
4018 62 Newx(esc, qlen * 3 + 1, char);
4019 62 en = po_url_esc(q, (size_t)qlen, esc, qlen * 3 + 1);
4020 62 hv_stores(panel, "query_esc", newSVpvn(esc, en));
4021 62 Safefree(esc);
4022 }
4023 /* THE EDITOR NEEDS THE PANEL, not only the drawing of it: an id to
4024 * address, the raw query to put back in the box, its order and its
4025 * chart kind. */
4026 62 hv_stores(panel, "query", newSVpvn(q, qlen));
4027 62 x = hv_fetchs(p, "id", 0);
4028 62 ▶ hv_stores(panel, "id", x ? newSVsv(*x) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(panel, "id", x ? newSVsv(*x) : newSV(0)); 26 36 62
4029 62 ▶ if (x && SvOK(*x) && SvTRUE(*x))
Branch: 3 branches
BranchTrueFalseTotal
if (x && SvOK(*x) && SvTRUE(*x)) 26 36 62
if (x && SvOK(*x) && SvTRUE(*x)) 26 0 26
if (x && SvOK(*x) && SvTRUE(*x)) 26 0 26
4030 26 hv_stores(panel, "key", newSVsv(*x));
4031 else
4032 36 hv_stores(panel, "key", newSVpvf("i%d", (int)idx));
4033 62 x = hv_fetchs(p, "position", 0);
4034 62 ▶ hv_stores(panel, "position",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(panel, "position", 33 29 62
hv_stores(panel, "position", 33 0 33
4035 newSViv((x && SvOK(*x)) ? SvIV(*x) : 0));
4036 62 x = hv_fetchs(p, "viz", 0);
4037 62 ▶ hv_stores(panel, "viz",
Branch: 2 branches
BranchTrueFalseTotal
hv_stores(panel, "viz", 45 17 62
hv_stores(panel, "viz", 45 0 45
4038 (x && SvOK(*x)) ? newSVsv(*x) : newSVpvs("line"));
4039 62 hv_stores(panel, "body", newSVpvs(""));
4040 62 hv_stores(panel, "refusal", newSVpvs(""));
4041 62 return panel;
4042 }
4043  
4044 /* Does this row carry a trace to jump to? */
4045 70136 static int povw_row_has_trace(pTHX_ SV *rowref) {
4046 HV *row;
4047 SV **hi, **lo;
4048 70136 ▶ if (!SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0; 70136 0 70136
if (!SvROK(rowref) || SvTYPE(SvRV(rowref)) != SVt_PVHV) return 0; 0 70136 70136
4049 70136 row = (HV *)SvRV(rowref);
4050 70136 hi = hv_fetchs(row, "trace_hi", 0);
4051 70136 lo = hv_fetchs(row, "trace_lo", 0);
4052 70136 ▶ return ((hi && SvTRUE(*hi)) || (lo && SvTRUE(*lo))) ? 1 : 0;
Branch: 4 branches
BranchTrueFalseTotal
return ((hi && SvTRUE(*hi)) || (lo && SvTRUE(*lo))) ? 1 : 0; 12 70124 70136
return ((hi && SvTRUE(*hi)) || (lo && SvTRUE(*lo))) ? 1 : 0; 0 12 12
return ((hi && SvTRUE(*hi)) || (lo && SvTRUE(*lo))) ? 1 : 0; 0 70124 70124
return ((hi && SvTRUE(*hi)) || (lo && SvTRUE(*lo))) ? 1 : 0; 0 0 0
4053 }
4054  
4055 /* Milliseconds since the epoch, for a chart axis.
4056 *
4057 * Milliseconds fit a double with room to spare - 1.8e12 against a 9e15
4058 * ceiling - so the axis is exact to the millisecond, which is finer than any
4059 * screen can resolve. What is NOT done is arithmetic on the nanoseconds
4060 * first: dividing by a million before the conversion rounds the value in the
4061 * digits a chart is drawn from. So the last six digits are DROPPED, as
4062 * characters, and what is left is converted once. */
4063 8122 static SV *povw_plot_ms(pTHX_ SV *ns_sv) {
4064 STRLEN len;
4065 const char *p;
4066 char buf[32];
4067 8122 size_t n = 0, i;
4068 8122 po_u64 v = 0;
4069  
4070 8122 ▶ if (!ns_sv || !SvOK(ns_sv)) return newSViv(0);
Branch: 2 branches
BranchTrueFalseTotal
if (!ns_sv || !SvOK(ns_sv)) return newSViv(0); 8122 0 8122
if (!ns_sv || !SvOK(ns_sv)) return newSViv(0); 0 8122 8122
4071 8122 p = SvPV(ns_sv, len);
4072 178676 ▶ for (i = 0; i < (size_t)len && n < sizeof(buf); i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < (size_t)len && n < sizeof(buf); i++) 170554 8122 178676
for (i = 0; i < (size_t)len && n < sizeof(buf); i++) 170554 0 170554
4073 170554 ▶ if (p[i] >= '0' && p[i] <= '9') buf[n++] = p[i];
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] >= '0' && p[i] <= '9') buf[n++] = p[i]; 170554 0 170554
if (p[i] >= '0' && p[i] <= '9') buf[n++] = p[i]; 170554 0 170554
4074 8122 ▶ if (n <= 6) return newSViv(0);
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 6) return newSViv(0); 0 8122 8122
4075 8122 n -= 6;
4076 129944 ▶ for (i = 0; i < n; i++) v = v * 10 + (po_u64)(buf[i] - '0');
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) v = v * 10 + (po_u64)(buf[i] - '0'); 121822 8122 129944
4077 /* An IV where it fits, as perl's own numification would have given, so
4078 * the JSON says 1787000000000 rather than 1.787e+12. */
4079 8122 ▶ if (v <= (po_u64)IV_MAX) return newSViv((IV)v);
Branch: 1 branch
BranchTrueFalseTotal
if (v <= (po_u64)IV_MAX) return newSViv((IV)v); 8122 0 8122
4080 0 return newSVnv((NV)v);
4081 }
4082  
4083 /* ---- the chunk cache's Perl-facing edges ---------------------------------
4084 *
4085 * The store and the cache are objects with methods, so these four are where
4086 * the C in po_chunk.h reaches them. Kept together and small: everything that
4087 * can go wrong at this seam - a store that dies, a cache that throws, a blob
4088 * that will not decode - has to come out as "compute it again", never as a
4089 * failed page.
4090 */
4091  
4092 /* THE OPTIONS THIS SEAM CONSUMES, and therefore the ones a chunk query must
4093 * not be given: the window is the chunk's, and the cache controls describe the
4094 * cache rather than the scan. Everything else the caller passed is its
4095 * business and travels through untouched - a rule rather than a list, so an
4096 * option added to `query` tomorrow reaches a chunk without anybody
4097 * remembering to come back here. */
4098 212 static int poc_consumed(const char *k) {
4099 168 ▶ return strEQ(k, "from") || strEQ(k, "to") || strEQ(k, "now")
Branch: 2 branches
BranchTrueFalseTotal
return strEQ(k, "from") || strEQ(k, "to") || strEQ(k, "now") 124 44 168
return strEQ(k, "from") || strEQ(k, "to") || strEQ(k, "now") 85 39 124
4100 85 ▶ || strEQ(k, "lag_ns") || strEQ(k, "ttl") || strEQ(k, "cache")
Branch: 3 branches
BranchTrueFalseTotal
|| strEQ(k, "lag_ns") || strEQ(k, "ttl") || strEQ(k, "cache") 85 0 85
|| strEQ(k, "lag_ns") || strEQ(k, "ttl") || strEQ(k, "cache") 75 10 85
|| strEQ(k, "lag_ns") || strEQ(k, "ttl") || strEQ(k, "cache") 33 42 75
4101 33 ▶ || strEQ(k, "tenant")
Branch: 1 branch
BranchTrueFalseTotal
|| strEQ(k, "tenant") 33 0 33
4102 33 ▶ || strEQ(k, "refresh_ns") || strEQ(k, "budget")
Branch: 2 branches
BranchTrueFalseTotal
|| strEQ(k, "refresh_ns") || strEQ(k, "budget") 27 6 33
|| strEQ(k, "refresh_ns") || strEQ(k, "budget") 22 5 27
4103 380 ▶ || strEQ(k, "deadline");
Branch: 2 branches
BranchTrueFalseTotal
|| strEQ(k, "deadline"); 168 44 212
|| strEQ(k, "deadline"); 6 16 22
4104 }
4105  
4106 /* One plain scan. A die inside it is caught and becomes undef, because a
4107 * chunk that cannot be computed should not take the panel with it.
4108 *
4109 * `extra` is the caller's own options, key and value alternating - the row
4110 * budgets above all. They are the caller's SVs rather than copies: they are
4111 * alive in @_ for the whole of the call that reached here. */
4112 171 static SV *poc_plain(pTHX_ SV *store, SV *q, po_u64 from, po_u64 to,
4113 SV **extra, int nextra) {
4114 171 dSP;
4115 171 SV *r = NULL;
4116 int n, i;
4117 171 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 171 171
4118 171 ▶ XPUSHs(store);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(store); 0 171 171
4119 171 ▶ XPUSHs(sv_2mortal(newSVsv(q)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(q))); 0 171 171
4120 171 ▶ XPUSHs(sv_2mortal(newSVpvs("from"))); XPUSHs(sv_2mortal(po_u64_to_sv(from)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("from"))); XPUSHs(sv_2mortal(po_u64_to_sv(from))); 0 171 171
XPUSHs(sv_2mortal(newSVpvs("from"))); XPUSHs(sv_2mortal(po_u64_to_sv(from))); 0 171 171
4121 171 ▶ XPUSHs(sv_2mortal(newSVpvs("to"))); XPUSHs(sv_2mortal(po_u64_to_sv(to)));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("to"))); XPUSHs(sv_2mortal(po_u64_to_sv(to))); 0 171 171
XPUSHs(sv_2mortal(newSVpvs("to"))); XPUSHs(sv_2mortal(po_u64_to_sv(to))); 0 171 171
4122 341 ▶ for (i = 0; i < nextra; i++) XPUSHs(extra[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nextra; i++) XPUSHs(extra[i]); 0 170 170
for (i = 0; i < nextra; i++) XPUSHs(extra[i]); 170 171 341
4123 171 PUTBACK;
4124 171 n = call_method("query", G_SCALAR | G_EVAL);
4125 171 SPAGAIN;
4126 171 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 171 0 171
4127 171 PUTBACK;
4128 171 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 171 0 171
4129 171 ▶ if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; }
Branch: 3 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; } 171 0 171
if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; } 0 171 171
if (SvTRUE(ERRSV)) { if (r) SvREFCNT_dec(r); r = NULL; } 0 0 0
4130 171 return r;
4131 }
4132  
4133 /* The key names everything the answer depends on: the tenant, because a
4134 * store is a directory per tenant and one cache may serve several; the chunk
4135 * start and width, because they are what the entry covers; and the query
4136 * VERBATIM rather than hashed - Punk::Cache takes any bytes and hashes the
4137 * key into a path itself, so a digest here would be a digest over a digest,
4138 * and a key somebody can read is a cache somebody can debug.
4139 *
4140 * NULL when the key would exceed what Punk::Cache accepts, which turns
4141 * chunking off for that query instead of croaking in the middle of a render.
4142 *
4143 * THE NAMESPACE CARRIES THE FORMAT. `po.chunk` entries were written before
4144 * the caller's row budget reached a chunk, so any of them may hold a count
4145 * that stopped short and a truncation flag that outlives the reason for it.
4146 * Moving the namespace retires the lot at once, and a pool part way through
4147 * an upgrade reads and writes two sets that cannot collide. */
4148 #define POC_KEY_MAX 4096
4149 189 static SV *poc_key(pTHX_ SV *store, SV *q, po_u64 start, po_u64 width,
4150 SV *tenant) {
4151 189 SV *k = newSVpvs("po.chunk2");
4152 189 SV *t = tenant;
4153  
4154 189 ▶ if (!t && SvROK(store) && SvTYPE(SvRV(store)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!t && SvROK(store) && SvTYPE(SvRV(store)) == SVt_PVHV) { 189 0 189
if (!t && SvROK(store) && SvTYPE(SvRV(store)) == SVt_PVHV) { 189 0 189
if (!t && SvROK(store) && SvTYPE(SvRV(store)) == SVt_PVHV) { 189 0 189
4155 189 SV **f = hv_fetchs((HV *)SvRV(store), "tenant", 0);
4156 189 ▶ if (f && SvOK(*f)) t = *f;
Branch: 2 branches
BranchTrueFalseTotal
if (f && SvOK(*f)) t = *f; 159 30 189
if (f && SvOK(*f)) t = *f; 159 0 159
4157 }
4158 189 sv_catpvn(k, "\0", 1);
4159 189 ▶ if (t && SvOK(t)) sv_catsv(k, t); else sv_catpvs(k, "default");
Branch: 2 branches
BranchTrueFalseTotal
if (t && SvOK(t)) sv_catsv(k, t); else sv_catpvs(k, "default"); 159 30 189
if (t && SvOK(t)) sv_catsv(k, t); else sv_catpvs(k, "default"); 159 0 159
4160 189 sv_catpvn(k, "\0", 1);
4161 189 sv_catsv(k, sv_2mortal(po_u64_to_sv(start)));
4162 189 sv_catpvn(k, "\0", 1);
4163 189 sv_catsv(k, sv_2mortal(po_u64_to_sv(width)));
4164 189 sv_catpvn(k, "\0", 1);
4165 189 sv_catsv(k, q);
4166 189 ▶ if (SvCUR(k) > POC_KEY_MAX) { SvREFCNT_dec(k); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(k) > POC_KEY_MAX) { SvREFCNT_dec(k); return NULL; } 0 189 189
4167 189 return k;
4168 }
4169  
4170 231 static SV *poc_cache_get(pTHX_ SV *cache, SV *key) {
4171 231 dSP;
4172 231 SV *r = NULL;
4173 int n;
4174 231 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 231 231
4175 231 ▶ XPUSHs(cache);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cache); 0 231 231
4176 231 ▶ XPUSHs(key);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(key); 0 231 231
4177 231 PUTBACK;
4178 231 n = call_method("get", G_SCALAR | G_EVAL);
4179 231 SPAGAIN;
4180 231 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 231 0 231
4181 231 PUTBACK;
4182 231 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 231 0 231
4183 231 ▶ if (SvTRUE(ERRSV) || (r && !SvOK(r))) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 231 0 231
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 225 6 231
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 225 0 225
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 120 105 225
4184 126 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 126 0 126
4185 126 r = NULL;
4186 }
4187 231 return r;
4188 }
4189  
4190 6 static void poc_cache_set(pTHX_ SV *cache, SV *key,
4191 const unsigned char *buf, size_t len, IV ttl) {
4192 6 dSP;
4193 6 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 6 6
4194 6 ▶ XPUSHs(cache);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cache); 0 6 6
4195 6 ▶ XPUSHs(key);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(key); 0 6 6
4196 6 ▶ XPUSHs(sv_2mortal(newSVpvn((const char *)buf, len)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn((const char *)buf, len))); 0 6 6
4197 6 ▶ XPUSHs(sv_2mortal(newSViv(ttl)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(ttl))); 0 6 6
4198 6 PUTBACK;
4199 6 (void)call_method("set", G_SCALAR | G_EVAL | G_DISCARD);
4200 6 SPAGAIN;
4201 6 PUTBACK;
4202 6 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 0 6
4203 /* A refused or failed store is not an error: the answer is already
4204 * computed, and the next call recomputes it. */
4205 6 }
4206  
4207 /* A query result into the accumulator. Only a bucketed answer has anything
4208 * to merge; anything else means the query was not what it was taken to be,
4209 * and the caller falls back. */
4210 165 static int poc_ingest(pTHX_ po_cres *acc, SV *res) {
4211 HV *h;
4212 SV **f;
4213 AV *series;
4214 SSize_t i, n;
4215  
4216 165 ▶ if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return 0; 165 0 165
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return 0; 165 0 165
if (!res || !SvROK(res) || SvTYPE(SvRV(res)) != SVt_PVHV) return 0; 0 165 165
4217 165 h = (HV *)SvRV(res);
4218 165 f = hv_fetchs(h, "ok", 0);
4219 165 ▶ if (!f || !SvTRUE(*f)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!f || !SvTRUE(*f)) return 0; 165 0 165
if (!f || !SvTRUE(*f)) return 0; 0 165 165
4220  
4221 165 f = hv_fetchs(h, "bucket_ns", 0);
4222 165 ▶ if (f && SvOK(*f) && !acc->bucket_ns)
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvOK(*f) && !acc->bucket_ns) 165 0 165
if (f && SvOK(*f) && !acc->bucket_ns) 165 0 165
if (f && SvOK(*f) && !acc->bucket_ns) 0 165 165
4223 0 (void)po_sv_to_u64(aTHX_ *f, &acc->bucket_ns);
4224  
4225 165 f = hv_fetchs(h, "meta", 0);
4226 165 ▶ if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 165 0 165
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 165 0 165
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 165 0 165
4227 165 HV *m = (HV *)SvRV(*f);
4228 165 SV **x = hv_fetchs(m, "truncated", 0);
4229 165 ▶ if (x && SvTRUE(*x)) acc->truncated = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvTRUE(*x)) acc->truncated = 1; 165 0 165
if (x && SvTRUE(*x)) acc->truncated = 1; 24 141 165
4230 165 x = hv_fetchs(m, "exact", 0);
4231 165 ▶ if (x && SvOK(*x) && !SvTRUE(*x)) acc->exact = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (x && SvOK(*x) && !SvTRUE(*x)) acc->exact = 0; 165 0 165
if (x && SvOK(*x) && !SvTRUE(*x)) acc->exact = 0; 165 0 165
if (x && SvOK(*x) && !SvTRUE(*x)) acc->exact = 0; 0 165 165
4232 165 x = hv_fetchs(m, "degraded", 0);
4233 165 ▶ if (x && SvTRUE(*x)) acc->degraded = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvTRUE(*x)) acc->degraded = 1; 165 0 165
if (x && SvTRUE(*x)) acc->degraded = 1; 0 165 165
4234 165 x = hv_fetchs(m, "scanned_rows", 0);
4235 165 ▶ if (x && SvOK(*x)) {
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvOK(*x)) { 165 0 165
if (x && SvOK(*x)) { 165 0 165
4236 165 po_u64 sc = 0;
4237 165 (void)po_sv_to_u64(aTHX_ *x, &sc);
4238 165 acc->scanned += sc;
4239 }
4240 165 x = hv_fetchs(m, "scanned_bytes", 0);
4241 165 ▶ if (x && SvOK(*x)) {
Branch: 2 branches
BranchTrueFalseTotal
if (x && SvOK(*x)) { 165 0 165
if (x && SvOK(*x)) { 165 0 165
4242 165 po_u64 sb = 0;
4243 165 (void)po_sv_to_u64(aTHX_ *x, &sb);
4244 165 acc->scanned_bytes += sb;
4245 }
4246 }
4247  
4248 165 f = hv_fetchs(h, "series", 0);
4249 165 ▶ if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return 0; 165 0 165
if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return 0; 165 0 165
if (!f || !SvROK(*f) || SvTYPE(SvRV(*f)) != SVt_PVAV) return 0; 0 165 165
4250 165 series = (AV *)SvRV(*f);
4251 165 n = av_len(series) + 1;
4252 436 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 271 165 436
4253 271 SV **e = av_fetch(series, i, 0);
4254 HV *sh;
4255 SV **kf, **pf;
4256 po_cser *cs;
4257 271 STRLEN kl = 0;
4258 271 const char *kp = "";
4259 AV *pts;
4260 SSize_t j, pn;
4261  
4262 271 ▶ if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 271 0 271
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 271 0 271
if (!e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVHV) continue; 0 271 271
4263 271 sh = (HV *)SvRV(*e);
4264 271 kf = hv_fetchs(sh, "key", 0);
4265 542 ▶ if (kf && SvOK(*kf)) kp = SvPV(*kf, kl);
Branch: 2 branches
BranchTrueFalseTotal
if (kf && SvOK(*kf)) kp = SvPV(*kf, kl); 271 0 271
if (kf && SvOK(*kf)) kp = SvPV(*kf, kl); 271 0 271
4266 271 cs = po_cres_series(acc, kp, (size_t)kl);
4267 271 ▶ if (!cs) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cs) return 0; 0 271 271
4268  
4269 271 pf = hv_fetchs(sh, "points", 0);
4270 271 ▶ if (!pf || !SvROK(*pf) || SvTYPE(SvRV(*pf)) != SVt_PVAV) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!pf || !SvROK(*pf) || SvTYPE(SvRV(*pf)) != SVt_PVAV) continue; 271 0 271
if (!pf || !SvROK(*pf) || SvTYPE(SvRV(*pf)) != SVt_PVAV) continue; 271 0 271
if (!pf || !SvROK(*pf) || SvTYPE(SvRV(*pf)) != SVt_PVAV) continue; 0 271 271
4271 271 pts = (AV *)SvRV(*pf);
4272 271 pn = av_len(pts) + 1;
4273 836 ▶ for (j = 0; j < pn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < pn; j++) { 565 271 836
4274 565 SV **p = av_fetch(pts, j, 0);
4275 AV *pa;
4276 SV **a0, **a1, **a2;
4277 565 po_u64 at = 0, cnt = 0;
4278 565 ▶ if (!p || !SvROK(*p) || SvTYPE(SvRV(*p)) != SVt_PVAV) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!p || !SvROK(*p) || SvTYPE(SvRV(*p)) != SVt_PVAV) continue; 565 0 565
if (!p || !SvROK(*p) || SvTYPE(SvRV(*p)) != SVt_PVAV) continue; 565 0 565
if (!p || !SvROK(*p) || SvTYPE(SvRV(*p)) != SVt_PVAV) continue; 0 565 565
4279 565 pa = (AV *)SvRV(*p);
4280 565 a0 = av_fetch(pa, 0, 0);
4281 565 a1 = av_fetch(pa, 1, 0);
4282 565 a2 = av_fetch(pa, 2, 0);
4283 565 ▶ if (a0 && SvOK(*a0)) (void)po_sv_to_u64(aTHX_ *a0, &at);
Branch: 2 branches
BranchTrueFalseTotal
if (a0 && SvOK(*a0)) (void)po_sv_to_u64(aTHX_ *a0, &at); 565 0 565
if (a0 && SvOK(*a0)) (void)po_sv_to_u64(aTHX_ *a0, &at); 565 0 565
4284 565 ▶ if (a2 && SvOK(*a2)) (void)po_sv_to_u64(aTHX_ *a2, &cnt);
Branch: 2 branches
BranchTrueFalseTotal
if (a2 && SvOK(*a2)) (void)po_sv_to_u64(aTHX_ *a2, &cnt); 565 0 565
if (a2 && SvOK(*a2)) (void)po_sv_to_u64(aTHX_ *a2, &cnt); 565 0 565
4285 565 ▶ if (!po_cser_put(cs, at, (a1 && SvOK(*a1)) ? SvNV(*a1) : 0, cnt))
Branch: 3 branches
BranchTrueFalseTotal
if (!po_cser_put(cs, at, (a1 && SvOK(*a1)) ? SvNV(*a1) : 0, cnt)) 565 0 565
if (!po_cser_put(cs, at, (a1 && SvOK(*a1)) ? SvNV(*a1) : 0, cnt)) 565 0 565
if (!po_cser_put(cs, at, (a1 && SvOK(*a1)) ? SvNV(*a1) : 0, cnt)) 0 565 565
4286 0 return 0;
4287 }
4288 }
4289 165 return 1;
4290 }
4291  
4292 /* ---- one chunk, computed once across the pool -----------------------------
4293 *
4294 * `get` and `set` have no gap between them for anybody to wait in, so five
4295 * workers rendering the same dashboard on a cold cache each scanned the same
4296 * day. `Punk::Cache::compute` has one: it takes an exclusive lock beside the
4297 * entry, and the workers that lose the race take the winner's answer rather
4298 * than repeating its work.
4299 *
4300 * It wants a code reference, and the chunk to compute is not something a
4301 * caller passes in - so this is an anonymous XSUB carrying its captures as
4302 * magic, the same shape `cron_task` uses. The magic is also what frees them.
4303 */
4304 static MGVTBL poc_blob_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 };
4305  
4306 /* WHAT THIS REFUSES IS THE POINT. It runs inside `compute`, which stores
4307 * whatever comes back - and a cached undef is a value. So a chunk that did not
4308 * compute, and a chunk that truncated, die here instead of returning: a scan
4309 * that stopped short must not become an entry that answers short for the whole
4310 * of its life. The caller computes those plainly and keeps them out. */
4311 126 static void poc_blob_thunk(pTHX_ CV *cv) {
4312 126 dXSARGS;
4313 126 MAGIC *mg = mg_find((SV *)cv, PERL_MAGIC_ext);
4314 126 ▶ HV *o = (mg && mg->mg_obj && SvROK(mg->mg_obj)
Branch: 2 branches
BranchTrueFalseTotal
HV *o = (mg && mg->mg_obj && SvROK(mg->mg_obj) 126 0 126
HV *o = (mg && mg->mg_obj && SvROK(mg->mg_obj) 126 0 126
4315 126 ▶ && SvTYPE(SvRV(mg->mg_obj)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(mg->mg_obj)) == SVt_PVHV) 126 0 126
4316 252 ▶ ? (HV *)SvRV(mg->mg_obj) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(mg->mg_obj) : NULL; 126 0 126
4317 126 SV **f, **extra = NULL;
4318 126 SV *store = NULL, *q = NULL, *r = NULL, *out = NULL;
4319 126 AV *ex = NULL;
4320 126 po_u64 from = 0, to = 0, b = 0;
4321 po_cres one;
4322 126 int nextra = 0;
4323 SSize_t i, n;
4324  
4325 126 ▶ if (!o) croak("Punk::Observe::Cache: the chunk closure lost its captures");
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Punk::Observe::Cache: the chunk closure lost its captures"); 0 126 126
4326 126 ▶ f = hv_fetchs(o, "store", 0); if (f) store = *f;
Branch: 1 branch
BranchTrueFalseTotal
f = hv_fetchs(o, "store", 0); if (f) store = *f; 126 0 126
4327 126 ▶ f = hv_fetchs(o, "q", 0); if (f) q = *f;
Branch: 1 branch
BranchTrueFalseTotal
f = hv_fetchs(o, "q", 0); if (f) q = *f; 126 0 126
4328 126 ▶ f = hv_fetchs(o, "from", 0); if (f) (void)po_sv_to_u64(aTHX_ *f, &from);
Branch: 1 branch
BranchTrueFalseTotal
f = hv_fetchs(o, "from", 0); if (f) (void)po_sv_to_u64(aTHX_ *f, &from); 126 0 126
4329 126 ▶ f = hv_fetchs(o, "to", 0); if (f) (void)po_sv_to_u64(aTHX_ *f, &to);
Branch: 1 branch
BranchTrueFalseTotal
f = hv_fetchs(o, "to", 0); if (f) (void)po_sv_to_u64(aTHX_ *f, &to); 126 0 126
4330 126 ▶ f = hv_fetchs(o, "bucket_ns", 0); if (f) (void)po_sv_to_u64(aTHX_ *f, &b);
Branch: 1 branch
BranchTrueFalseTotal
f = hv_fetchs(o, "bucket_ns", 0); if (f) (void)po_sv_to_u64(aTHX_ *f, &b); 126 0 126
4331 126 f = hv_fetchs(o, "extra", 0);
4332 126 ▶ if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVAV) ex = (AV *)SvRV(*f);
Branch: 3 branches
BranchTrueFalseTotal
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVAV) ex = (AV *)SvRV(*f); 126 0 126
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVAV) ex = (AV *)SvRV(*f); 126 0 126
if (f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVAV) ex = (AV *)SvRV(*f); 126 0 126
4333 126 ▶ if (!store || !q)
Branch: 2 branches
BranchTrueFalseTotal
if (!store || !q) 126 0 126
if (!store || !q) 0 126 126
4334 0 croak("Punk::Observe::Cache: the chunk closure lost its store");
4335  
4336 126 ▶ n = ex ? av_len(ex) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
n = ex ? av_len(ex) + 1 : 0; 126 0 126
4337 126 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 25 101 126
4338 25 ▶ Newx(extra, n, SV *);
Branch: 1 branch
BranchTrueFalseTotal
Newx(extra, n, SV *); 0 25 25
4339 151 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 126 25 151
4340 126 SV **e = av_fetch(ex, i, 0);
4341 126 ▶ extra[nextra++] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
extra[nextra++] = (e && *e) ? *e : &PL_sv_undef; 126 0 126
extra[nextra++] = (e && *e) ? *e : &PL_sv_undef; 126 0 126
4342 }
4343 }
4344 126 r = poc_plain(aTHX_ store, q, from, to, extra, nextra);
4345 126 Safefree(extra);
4346  
4347 126 po_cres_init(&one);
4348 126 one.bucket_ns = b;
4349 126 ▶ if (!poc_ingest(aTHX_ &one, r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!poc_ingest(aTHX_ &one, r)) { 0 126 126
4350 0 po_cres_free(&one);
4351 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
4352 0 croak("Punk::Observe::Cache: the chunk did not compute");
4353 }
4354 126 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 126 0 126
4355 126 ▶ if (one.truncated) {
Branch: 1 branch
BranchTrueFalseTotal
if (one.truncated) { 12 114 126
4356 12 po_cres_free(&one);
4357 12 croak("Punk::Observe::Cache: the chunk truncated");
4358 }
4359 {
4360 114 size_t need = po_chunk_size(&one);
4361 unsigned char *buf;
4362 size_t len;
4363 114 Newx(buf, need, unsigned char);
4364 114 len = po_chunk_encode(&one, buf);
4365 114 out = newSVpvn((const char *)buf, len);
4366 114 Safefree(buf);
4367 }
4368 114 po_cres_free(&one);
4369  
4370 /* `compute` calls this with no arguments, so there is no argument slot to
4371 * answer in until one is made. */
4372 114 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 114 114
4373 114 ST(0) = sv_2mortal(out);
4374 114 XSRETURN(1);
4375 }
4376  
4377 /* AN ENTRY THAT WILL NOT DECODE HAS TO GO BEFORE IT CAN BE REPLACED.
4378 *
4379 * `compute` runs its code on a MISS, and a blob written by a superseded format
4380 * is not a miss - it is a hit this version cannot read. Left in place it would
4381 * be re-read and re-refused on every call until it expired, with the chunk
4382 * scanned plainly each time behind it: a cache entry whose only effect is to
4383 * stop the cache working. */
4384 52 static void poc_cache_del(pTHX_ SV *cache, SV *key) {
4385 52 dSP;
4386 52 ▶ if (!povw_can(aTHX_ cache, "delete")) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!povw_can(aTHX_ cache, "delete")) return; 0 52 52
4387 52 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 52 52
4388 52 ▶ XPUSHs(cache);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cache); 0 52 52
4389 52 ▶ XPUSHs(key);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(key); 0 52 52
4390 52 PUTBACK;
4391 52 (void)call_method("delete", G_SCALAR | G_EVAL | G_DISCARD);
4392 52 SPAGAIN;
4393 52 PUTBACK;
4394 52 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 52 0 52
4395 }
4396  
4397 /* The closure, run for its answer. NULL when it declined to produce one. */
4398 6 static SV *poc_call_code(pTHX_ SV *code) {
4399 6 dSP;
4400 6 SV *r = NULL;
4401 int n;
4402 6 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 6 6
4403 6 PUTBACK;
4404 6 n = call_sv(code, G_SCALAR | G_EVAL);
4405 6 SPAGAIN;
4406 6 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 6 0 6
4407 6 PUTBACK;
4408 6 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 6 0 6
4409 6 ▶ if (SvTRUE(ERRSV) || (r && !SvOK(r))) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 6 0 6
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 6 0 6
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 6 0 6
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 0 6 6
4410 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
4411 0 r = NULL;
4412 }
4413 6 return r;
4414 }
4415  
4416 /* The stored bytes for one chunk, or NULL when there was nothing worth
4417 * storing. A cache without `compute` still gets its entry, just without the
4418 * single-flight: a cache is a seam a host may implement itself, and needing
4419 * the newer method in order to be cached at all would be a poor trade. */
4420 132 static SV *poc_fill(pTHX_ SV *cache, SV *key, IV ttl, SV *store, SV *q,
4421 po_u64 from, po_u64 to, po_u64 bucket_ns,
4422 SV **extra, int nextra) {
4423 132 HV *o = newHV();
4424 132 AV *ex = newAV();
4425 CV *cv;
4426 132 SV *code, *r = NULL;
4427 int i;
4428  
4429 258 ▶ for (i = 0; i < nextra; i++) av_push(ex, newSVsv(extra[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nextra; i++) av_push(ex, newSVsv(extra[i])); 126 132 258
4430 132 hv_stores(o, "store", newSVsv(store));
4431 132 hv_stores(o, "q", newSVsv(q));
4432 132 hv_stores(o, "from", po_u64_to_sv(from));
4433 132 hv_stores(o, "to", po_u64_to_sv(to));
4434 132 hv_stores(o, "bucket_ns", po_u64_to_sv(bucket_ns));
4435 132 hv_stores(o, "extra", newRV_noinc((SV *)ex));
4436  
4437 132 cv = newXS(NULL, poc_blob_thunk, __FILE__);
4438 132 ▶ if (!cv) { SvREFCNT_dec((SV *)o); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) { SvREFCNT_dec((SV *)o); return NULL; } 0 132 132
4439 /* Who owns the reference newXS returned depends on the perl: newer ones
4440 * hand back an unowned anonymous CV, older ones may have parked it in a
4441 * glob first. Take our own only in the second case. */
4442 132 ▶ if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv);
Branch: 3 branches
BranchTrueFalseTotal
if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv); 132 0 132
if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv); 0 132 132
if (CvGV(cv) && GvCV(CvGV(cv)) == cv) SvREFCNT_inc_simple_void(cv); 0 0 0
4443 132 sv_magicext((SV *)cv, sv_2mortal(newRV_noinc((SV *)o)),
4444 PERL_MAGIC_ext, &poc_blob_vtbl, NULL, 0);
4445 132 code = newRV_noinc((SV *)cv);
4446  
4447 132 ▶ if (povw_can(aTHX_ cache, "compute")) {
Branch: 1 branch
BranchTrueFalseTotal
if (povw_can(aTHX_ cache, "compute")) { 126 6 132
4448 126 dSP;
4449 int n;
4450 126 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 126 126
4451 126 ▶ XPUSHs(cache);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(cache); 0 126 126
4452 126 ▶ XPUSHs(key);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(key); 0 126 126
4453 126 ▶ XPUSHs(sv_2mortal(newSViv(ttl)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(ttl))); 0 126 126
4454 126 ▶ XPUSHs(code);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(code); 0 126 126
4455 126 PUTBACK;
4456 126 n = call_method("compute", G_SCALAR | G_EVAL);
4457 126 SPAGAIN;
4458 126 ▶ r = n ? SvREFCNT_inc(POPs) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
r = n ? SvREFCNT_inc(POPs) : NULL; 126 0 126
4459 126 PUTBACK;
4460 126 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 126 0 126
4461 126 ▶ if (SvTRUE(ERRSV) || (r && !SvOK(r))) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 126 0 126
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 108 18 126
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 108 0 108
if (SvTRUE(ERRSV) || (r && !SvOK(r))) { 0 108 108
4462 18 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 18 0 18
4463 18 r = NULL;
4464 }
4465 }
4466 else {
4467 6 r = poc_call_code(aTHX_ code);
4468 6 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 6 0 6
4469 6 STRLEN bl = 0;
4470 6 const unsigned char *bp = (const unsigned char *)SvPV(r, bl);
4471 6 poc_cache_set(aTHX_ cache, key, bp, (size_t)bl, ttl);
4472 }
4473 }
4474  
4475 132 SvREFCNT_dec(code);
4476 132 return r;
4477 }
4478  
4479 /* The merged answer, in the shape every caller of `query` already reads.
4480 * Points before `min_at` are dropped: a chunk starts on a chunk edge, which
4481 * can be earlier than the window asked for, and a plain query would not have
4482 * returned those buckets. */
4483 21 static SV *poc_emit(pTHX_ const po_cres *acc, po_u64 min_at, int chunks) {
4484 21 HV *res = newHV();
4485 21 HV *meta = newHV();
4486 21 AV *series = newAV();
4487 size_t i, j;
4488  
4489 62 ▶ for (i = 0; i < acc->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < acc->n; i++) { 41 21 62
4490 41 HV *sh = newHV();
4491 41 AV *pts = newAV();
4492 522 ▶ for (j = 0; j < acc->s[i].n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < acc->s[i].n; j++) { 481 41 522
4493 AV *p;
4494 481 ▶ if (acc->s[i].pt[j].at < min_at) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (acc->s[i].pt[j].at < min_at) continue; 0 481 481
4495 481 p = newAV();
4496 481 av_push(p, po_u64_to_sv(acc->s[i].pt[j].at));
4497 481 av_push(p, newSVnv((NV)acc->s[i].pt[j].value));
4498 481 av_push(p, po_u64_to_sv(acc->s[i].pt[j].count));
4499 481 av_push(pts, newRV_noinc((SV *)p));
4500 }
4501 41 hv_stores(sh, "key", newSVpvn(acc->s[i].key, acc->s[i].klen));
4502 41 hv_stores(sh, "points", newRV_noinc((SV *)pts));
4503 41 av_push(series, newRV_noinc((SV *)sh));
4504 }
4505  
4506 /* EVERY FIGURE A PLAIN QUERY REPORTS. A page that reads `degraded` off a
4507 * result got undef here and drew a store with an unreadable segment as a
4508 * healthy one - the chunked path answering with fewer facts than the path
4509 * it stands in for. */
4510 21 hv_stores(meta, "truncated", newSViv(acc->truncated ? 1 : 0));
4511 21 hv_stores(meta, "exact", newSViv(acc->exact ? 1 : 0));
4512 21 hv_stores(meta, "degraded", newSViv(acc->degraded ? 1 : 0));
4513 21 hv_stores(meta, "scanned_rows", po_u64_to_sv(acc->scanned));
4514 21 hv_stores(meta, "scanned_bytes", po_u64_to_sv(acc->scanned_bytes));
4515  
4516 21 hv_stores(res, "ok", newSViv(1));
4517 21 hv_stores(res, "shape", newSVpvs("buckets"));
4518 21 hv_stores(res, "series", newRV_noinc((SV *)series));
4519 21 hv_stores(res, "bucket_ns", po_u64_to_sv(acc->bucket_ns));
4520 21 hv_stores(res, "meta", newRV_noinc((SV *)meta));
4521 21 hv_stores(res, "groups", newRV_noinc((SV *)newAV()));
4522 21 hv_stores(res, "rows", newRV_noinc((SV *)newAV()));
4523 21 hv_stores(res, "cached_chunks", newSViv(chunks));
4524 21 return newRV_noinc((SV *)res);
4525 }
4526  
4527  
4528 MODULE = Punk::Observe PACKAGE = Punk::Observe
4529  
4530 BOOT:
4531 81 po_keep_alive();
4532 #ifdef PO_HAVE_BUS
4533 81 po_sa_resolve(aTHX);
4534 #endif
4535  
4536 INCLUDE: xs/contracts.xs
4537  
4538 INCLUDE: xs/decode.xs
4539  
4540 INCLUDE: xs/wal.xs
4541  
4542 INCLUDE: xs/ingest.xs
4543  
4544 INCLUDE: xs/segment.xs
4545  
4546 INCLUDE: xs/metrics.xs
4547  
4548 INCLUDE: xs/logs.xs
4549  
4550 INCLUDE: xs/traces.xs
4551  
4552 INCLUDE: xs/query.xs
4553  
4554 INCLUDE: xs/exec.xs
4555  
4556 INCLUDE: xs/retain.xs
4557  
4558 INCLUDE: xs/segio.xs
4559  
4560 INCLUDE: xs/svg.xs
4561  
4562 INCLUDE: xs/scan.xs
4563  
4564 INCLUDE: xs/live.xs
4565  
4566 INCLUDE: xs/alert.xs
4567  
4568 INCLUDE: xs/tenant.xs
4569  
4570 INCLUDE: xs/store.xs
4571  
4572 INCLUDE: xs/chunk.xs
4573  
4574 INCLUDE: xs/view.xs
4575  
4576 INCLUDE: xs/plot.xs
« xs/segio.xs ↑ Summary include/punk_observe/po_key.h »