xs/logs.xs

filter: Statementstmt 98.6% 141 / 143 Branchbran 62.2% 56 / 90 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.5% 197 / 233 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/traces.xs ↑ Summary xs/tenant.xs »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Log PREFIX = pol_
2  
3 # --- the bloom filter -------------------------------------------------------
4  
5 # Build a filter over a corpus, then answer a list of queries. Returns which
6 # queries the filter says are POSSIBLE - the caller compares that against
7 # which are actually present, which is how the false-negative assertion is
8 # made.
9 void
10 pol_bloom_probe(SV *corpus, SV *queries)
11 PPCODE:
12 {
13 po_bloom b;
14 po_tri_count *tc;
15 STRLEN clen;
16 const char *cp;
17 AV *qs;
18 SSize_t i, n;
19 208 AV *out = newAV();
20  
21 208 cp = SvPV(corpus, clen);
22 208 ▶ if (!SvROK(queries)) croak("queries must be an arrayref");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(queries)) croak("queries must be an arrayref"); 0 208 208
23 208 qs = (AV *)SvRV(queries);
24 208 n = av_len(qs) + 1;
25  
26 208 tc = (po_tri_count *)calloc(1, sizeof(po_tri_count));
27 208 ▶ if (!tc) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!tc) croak("oom"); 0 208 208
28 208 po_trigrams(cp, (size_t)clen, po_tri_count_cb, tc);
29 208 ▶ if (!po_bloom_init(&b, tc->n)) { free(tc); croak("oom"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_bloom_init(&b, tc->n)) { free(tc); croak("oom"); } 0 208 208
30 208 po_bloom_add_text(&b, cp, (size_t)clen);
31  
32 178561 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 178353 208 178561
33 178353 SV **q = av_fetch(qs, i, 0);
34 STRLEN ql; const char *qp;
35 178353 ▶ if (!q) { av_push(out, newSViv(0)); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!q) { av_push(out, newSViv(0)); continue; } 0 178353 178353
36 178353 qp = SvPV(*q, ql);
37 178353 av_push(out, newSViv(
38 po_bloom_may_contain(b.bits, b.mask, qp, (size_t)ql) ? 1 : 0));
39 }
40 {
41 208 HV *res = newHV();
42 208 hv_stores(res, "possible", newRV_noinc((SV *)out));
43 208 hv_stores(res, "bits", newSVuv((UV)b.nbits));
44 208 hv_stores(res, "distinct", newSVuv((UV)tc->n));
45 208 hv_stores(res, "added", newSVuv((UV)b.added));
46 208 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 208 208
47 }
48 208 free(tc);
49 208 po_bloom_free(&b);
50 }
51  
52 # Is a query long enough for the filter to say anything? A query under three
53 # bytes has no trigrams, so pruning on it would wrongly exclude every block.
54 int
55 pol_query_usable(SV *q)
56 CODE:
57 {
58 4 STRLEN l; const char *p = SvPV(q, l);
59 (void)p;
60 4 RETVAL = po_bloom_query_usable((size_t)l);
61 }
62 OUTPUT:
63 RETVAL
64  
65 # The exact, case-folded match that runs after the filter says "possible".
66 int
67 pol_contains(SV *hay, SV *needle)
68 CODE:
69 {
70 STRLEN hn, nn;
71 const char *h, *n;
72 6 ▶ h = SvPV(hay, hn);
Branch: 1 branch
BranchTrueFalseTotal
h = SvPV(hay, hn); 6 0 6
73 6 n = SvPV(needle, nn);
74 6 ▶ RETVAL = po_memfind(h, (size_t)hn, n, (size_t)nn) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = po_memfind(h, (size_t)hn, n, (size_t)nn) ? 1 : 0; 0 6 6
75 }
76 OUTPUT:
77 RETVAL
78  
79 # --- blocks -----------------------------------------------------------------
80  
81 # Build a block from line specs, seal it, inflate it, and read the lines back.
82 # The whole round trip in one call so a test asserts what came out equals what
83 # went in rather than trusting an intermediate.
84 void
85 pol_block_roundtrip(SV *lines, SV *stream)
86 PPCODE:
87 {
88 po_block_w b;
89 AV *av;
90 SSize_t i, n;
91 7 char *comp = NULL;
92 7 size_t clen = 0;
93 7 po_u64 st = 0;
94 7 AV *out = newAV();
95  
96 7 (void)po_sv_to_u64(aTHX_ stream, &st);
97 7 ▶ if (!SvROK(lines)) croak("lines must be an arrayref");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(lines)) croak("lines must be an arrayref"); 0 7 7
98 7 av = (AV *)SvRV(lines);
99 7 n = av_len(av) + 1;
100  
101 7 ▶ if (!po_block_w_init(&b, st)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_block_w_init(&b, st)) croak("oom"); 0 7 7
102  
103 3043 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3036 7 3043
104 3036 SV **e = av_fetch(av, i, 0);
105 HV *h; SV **f;
106 3036 po_u64 t = 0, tr_hi = 0, tr_lo = 0, span = 0;
107 3036 UV sev = 0;
108 3036 const char *body = ""; STRLEN blen = 0;
109 3036 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 3036 0 3036
if (!e || !SvROK(*e)) continue; 0 3036 3036
110 3036 h = (HV *)SvRV(*e);
111 3036 ▶ if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &t);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &t); 3036 0 3036
112 3036 ▶ if ((f = hv_fetchs(h, "severity", 0))) sev = SvUV(*f);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "severity", 0))) sev = SvUV(*f); 3036 0 3036
113 6072 ▶ if ((f = hv_fetchs(h, "body", 0))) body = SvPV(*f, blen);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "body", 0))) body = SvPV(*f, blen); 3036 0 3036
114 3036 ▶ if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &tr_hi);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &tr_hi); 2 3034 3036
115 3036 ▶ if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &tr_lo);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &tr_lo); 2 3034 3036
116 3036 ▶ if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &span);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &span); 2 3034 3036
117 3036 po_block_add(&b, t, (uint16_t)sev, body, (size_t)blen,
118 tr_hi, tr_lo, span);
119 }
120  
121 7 ▶ if (!po_block_seal(&b, &comp, &clen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_block_seal(&b, &comp, &clen)) { 0 7 7
122 0 po_block_w_free(&b);
123 0 croak("seal failed");
124 }
125  
126 {
127 7 char *raw = NULL;
128 7 ▶ if (po_block_inflate(&b.h, comp, clen, &raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_block_inflate(&b.h, comp, clen, &raw)) { 7 0 7
129 po_line_r r;
130 po_line ln;
131 7 po_line_r_init(&r, &b.h, raw);
132 3043 ▶ while (po_line_next(&r, &ln)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_line_next(&r, &ln)) { 3036 7 3043
133 3036 HV *h = newHV();
134 3036 hv_stores(h, "t", po_u64_to_sv(ln.t));
135 3036 hv_stores(h, "severity", newSVuv((UV)ln.severity));
136 3036 hv_stores(h, "body", newSVpvn(ln.body, ln.body_len));
137 3036 hv_stores(h, "trace_hi", po_u64_to_sv(ln.trace_hi));
138 3036 hv_stores(h, "trace_lo", po_u64_to_sv(ln.trace_lo));
139 3036 hv_stores(h, "span_id", po_u64_to_sv(ln.span_id));
140 3036 av_push(out, newRV_noinc((SV *)h));
141 }
142 7 free(raw);
143 }
144 }
145  
146 {
147 7 HV *res = newHV();
148 7 hv_stores(res, "lines", newRV_noinc((SV *)out));
149 7 hv_stores(res, "raw_len", newSVuv((UV)b.h.raw_len));
150 7 hv_stores(res, "comp_len", newSVuv((UV)b.h.comp_len));
151 7 hv_stores(res, "count", newSVuv((UV)b.h.lines));
152 7 hv_stores(res, "stored", newSViv(b.h.flags & PO_BLK_STORED ? 1 : 0));
153 7 hv_stores(res, "t_min", po_u64_to_sv(b.h.t_min));
154 7 hv_stores(res, "t_max", po_u64_to_sv(b.h.t_max));
155 7 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 7 7
156 }
157 7 free(comp);
158 7 po_block_w_free(&b);
159 }
160  
161 # Corrupt a byte of the compressed bytes and confirm the CRC catches it.
162 int
163 pol_block_corrupt_detected(SV *lines, SV *at)
164 CODE:
165 {
166 po_block_w b;
167 AV *av;
168 SSize_t i, n;
169 7 char *comp = NULL;
170 7 size_t clen = 0;
171 7 char *raw = NULL;
172 7 IV pos = SvIV(at);
173  
174 7 ▶ if (!SvROK(lines)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(lines)) croak("arrayref required"); 0 7 7
175 7 av = (AV *)SvRV(lines);
176 7 n = av_len(av) + 1;
177 7 ▶ if (!po_block_w_init(&b, 1)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_block_w_init(&b, 1)) croak("oom"); 0 7 7
178 1217 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1210 7 1217
179 1210 SV **e = av_fetch(av, i, 0);
180 STRLEN bl; const char *body;
181 1210 ▶ if (!e) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) continue; 0 1210 1210
182 1210 body = SvPV(*e, bl);
183 1210 po_block_add(&b, (po_u64)(1000 + i), 9, body, (size_t)bl, 0, 0, 0);
184 }
185 7 ▶ if (!po_block_seal(&b, &comp, &clen)) { po_block_w_free(&b); croak("seal"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_block_seal(&b, &comp, &clen)) { po_block_w_free(&b); croak("seal"); } 0 7 7
186  
187 7 ▶ if (pos >= 0 && (size_t)pos < clen)
Branch: 2 branches
BranchTrueFalseTotal
if (pos >= 0 && (size_t)pos < clen) 6 1 7
if (pos >= 0 && (size_t)pos < clen) 6 0 6
188 6 comp[pos] = (char)(comp[pos] ^ 0xFF);
189  
190 /* 1 means the corruption was DETECTED (inflate or CRC refused). */
191 7 RETVAL = po_block_inflate(&b.h, comp, clen, &raw) ? 0 : 1;
192 7 free(raw);
193 7 free(comp);
194 7 po_block_w_free(&b);
195 }
196 OUTPUT:
197 RETVAL
198  
199 # --- the label allowlist ----------------------------------------------------
200  
201 int
202 pol_is_label(SV *key)
203 CODE:
204 {
205 po_labelset s;
206 STRLEN l; const char *p;
207 8 po_labelset_default(&s);
208 8 p = SvPV(key, l);
209 8 RETVAL = po_is_label(&s, p, (size_t)l);
210 }
211 OUTPUT:
212 RETVAL
213  
214 void
215 pol_default_labels(...)
216 PPCODE:
217 {
218 po_labelset s;
219 int i;
220 1 po_labelset_default(&s);
221 5 ▶ for (i = 0; i < s.n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s.n; i++) 4 1 5
222 4 ▶ mXPUSHp(s.key[i], s.klen[i]);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(s.key[i], s.klen[i]); 0 4 4
223 }
224  
225 # --- pruning ----------------------------------------------------------------
226  
227 # Given block descriptions and a query, report which survive each test. The
228 # stats are the assertion that the pruning actually prunes.
229 void
230 pol_prune(SV *blocks, SV *from, SV *to, SV *query)
231 PPCODE:
232 {
233 AV *av;
234 SSize_t i, n;
235 4 po_u64 f = 0, t = 0;
236 STRLEN qlen;
237 const char *q;
238 po_prune_stats st;
239 4 AV *kept = newAV();
240  
241 4 memset(&st, 0, sizeof(st));
242 4 (void)po_sv_to_u64(aTHX_ from, &f);
243 4 (void)po_sv_to_u64(aTHX_ to, &t);
244 4 q = SvPV(query, qlen);
245 4 ▶ if (!SvROK(blocks)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(blocks)) croak("arrayref required"); 0 4 4
246 4 av = (AV *)SvRV(blocks);
247 4 n = av_len(av) + 1;
248  
249 20 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 16 4 20
250 16 SV **e = av_fetch(av, i, 0);
251 HV *h; SV **fl;
252 po_block_w b;
253 po_dir_ent ent;
254 16 char *comp = NULL; size_t clen = 0;
255 16 po_u64 bt_min = 0, bt_max = 0;
256 16 const char *text = ""; STRLEN tlen = 0;
257  
258 16 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 16 0 16
if (!e || !SvROK(*e)) continue; 0 16 16
259 16 h = (HV *)SvRV(*e);
260 16 ▶ if ((fl = hv_fetchs(h, "t_min", 0))) (void)po_sv_to_u64(aTHX_ *fl, &bt_min);
Branch: 1 branch
BranchTrueFalseTotal
if ((fl = hv_fetchs(h, "t_min", 0))) (void)po_sv_to_u64(aTHX_ *fl, &bt_min); 16 0 16
261 16 ▶ if ((fl = hv_fetchs(h, "t_max", 0))) (void)po_sv_to_u64(aTHX_ *fl, &bt_max);
Branch: 1 branch
BranchTrueFalseTotal
if ((fl = hv_fetchs(h, "t_max", 0))) (void)po_sv_to_u64(aTHX_ *fl, &bt_max); 16 0 16
262 32 ▶ if ((fl = hv_fetchs(h, "text", 0))) text = SvPV(*fl, tlen);
Branch: 1 branch
BranchTrueFalseTotal
if ((fl = hv_fetchs(h, "text", 0))) text = SvPV(*fl, tlen); 16 0 16
263  
264 16 ▶ if (!po_block_w_init(&b, 1)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_block_w_init(&b, 1)) croak("oom"); 0 16 16
265 16 po_block_add(&b, bt_min, 9, text, (size_t)tlen, 0, 0, 0);
266 16 ▶ if (bt_max > bt_min)
Branch: 1 branch
BranchTrueFalseTotal
if (bt_max > bt_min) 16 0 16
267 16 po_block_add(&b, bt_max, 9, "", 0, 0, 0, 0);
268 16 ▶ if (!po_block_seal(&b, &comp, &clen)) { po_block_w_free(&b); croak("seal"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_block_seal(&b, &comp, &clen)) { po_block_w_free(&b); croak("seal"); } 0 16 16
269  
270 16 memset(&ent, 0, sizeof(ent));
271 16 ent.h = b.h;
272 16 ▶ if (po_block_candidate(&ent, 0, 0, f, t,
Branch: 1 branch
BranchTrueFalseTotal
if (po_block_candidate(&ent, 0, 0, f, t, 7 9 16
273 16 b.bloom.bits, q, (size_t)qlen, &st))
274 7 av_push(kept, newSViv((IV)i));
275  
276 16 free(comp);
277 16 po_block_w_free(&b);
278 }
279 {
280 4 HV *res = newHV();
281 4 hv_stores(res, "kept", newRV_noinc((SV *)kept));
282 4 hv_stores(res, "considered", newSVuv((UV)st.considered));
283 4 hv_stores(res, "skipped_time", newSVuv((UV)st.skipped_time));
284 4 hv_stores(res, "skipped_bloom", newSVuv((UV)st.skipped_bloom));
285 4 hv_stores(res, "candidates", newSVuv((UV)st.candidates));
286 4 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 4 4
287 }
288 }
289  
290 int
291 pol_have_zlib(...)
292 CODE:
293 #ifdef PO_HAVE_ZLIB
294 3 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 3 3
295 #else
296 RETVAL = 0;
297 #endif
298 OUTPUT:
299 RETVAL
« xs/traces.xs ↑ Summary xs/tenant.xs »