| 1 |
|
| MODULE = Punk::Observe PACKAGE = Punk::Observe::Tenant PREFIX = potn_ |
| 2 |
|
| |
| 3 |
|
| void |
| 4 |
|
| potn_check(SV *id) |
| 5 |
|
| PPCODE: |
| 6 |
|
| { |
| 7 |
|
| STRLEN l; |
| 8 |
26 |
| const char *p = SvPV(id, l); |
| 9 |
26 |
| int rc = po_tenant_check(p, (size_t)l); |
| 10 |
26 |
| HV *res = newHV(); |
| 11 |
26 |
| hv_stores(res, "ok", newSViv(rc == PO_TN_OK)); |
| 12 |
26 |
| hv_stores(res, "code", newSViv(rc)); |
| 13 |
26 |
| hv_stores(res, "reason", newSVpv(po_tn_reason(rc), 0)); |
| 14 |
26 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
26 |
26 |
|
| 15 |
|
| } |
| 16 |
|
| |
| 17 |
|
| # Resolve through the seam. `resolver` is a coderef, or undef for the |
| 18 |
|
| # constant - and BOTH go through the same validation, which is the whole |
| 19 |
|
| # point of the seam existing. |
| 20 |
|
| void |
| 21 |
|
| potn_resolve(SV *fixed, SV *resolver) |
| 22 |
|
| PPCODE: |
| 23 |
|
| { |
| 24 |
|
| po_tenant_cfg cfg; |
| 25 |
|
| char out[PO_TENANT_MAX + 1]; |
| 26 |
28 |
| size_t len = 0; |
| 27 |
|
| STRLEN fl; |
| 28 |
56 |
▶ | const char *f = SvOK(fixed) ? SvPV(fixed, fl) : (fl = 0, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *f = SvOK(fixed) ? SvPV(fixed, fl) : (fl = 0, ""); |
28 |
0 |
28 |
|
| 29 |
|
| int rc; |
| 30 |
28 |
| HV *res = newHV(); |
| 31 |
|
| |
| 32 |
28 |
▶ | rc = po_tenant_set_fixed(&cfg, fl ? f : NULL, (size_t)fl); |
Branch: 1 branch
| Branch | True | False | Total |
| rc = po_tenant_set_fixed(&cfg, fl ? f : NULL, (size_t)fl); |
27 |
1 |
28 |
|
| 33 |
28 |
▶ | if (rc != PO_TN_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != PO_TN_OK) { |
1 |
27 |
28 |
|
| 34 |
1 |
| hv_stores(res, "ok", newSViv(0)); |
| 35 |
1 |
| hv_stores(res, "code", newSViv(rc)); |
| 36 |
1 |
| hv_stores(res, "reason", newSVpv(po_tn_reason(rc), 0)); |
| 37 |
1 |
| hv_stores(res, "at", newSVpvs("configuration")); |
| 38 |
1 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
1 |
1 |
|
| 39 |
1 |
| XSRETURN(1); |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
35 |
▶ | if (SvROK(resolver) && SvTYPE(SvRV(resolver)) == SVt_PVCV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(resolver) && SvTYPE(SvRV(resolver)) == SVt_PVCV) { |
8 |
19 |
27 |
| if (SvROK(resolver) && SvTYPE(SvRV(resolver)) == SVt_PVCV) { |
8 |
0 |
8 |
|
| 43 |
|
| /* The callback runs in Perl and hands back a string; the C |
| 44 |
|
| * seam validates whatever it produced. Host code that returns |
| 45 |
|
| * `../other` is a bug to CATCH, not to trust. |
| 46 |
|
| * |
| 47 |
|
| * The answer is copied but NOT TRUNCATED. Shortening an |
| 48 |
|
| * over-long id to fit would turn an invalid tenant into a |
| 49 |
|
| * valid one silently, which is the same class of mistake as |
| 50 |
|
| * accepting it - and the first version of this did exactly |
| 51 |
|
| * that. Anything past the buffer is too long by definition, |
| 52 |
|
| * so the length alone answers it without the bytes. */ |
| 53 |
|
| char buf[PO_TENANT_MAX + 2]; |
| 54 |
|
| po_tn_sv tn; |
| 55 |
|
| SV *ret; |
| 56 |
8 |
| int forced = 0; |
| 57 |
8 |
| dSP; |
| 58 |
8 |
| tn.p = NULL; tn.len = 0; |
| 59 |
8 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
8 |
8 |
|
| 60 |
8 |
| call_sv(resolver, G_SCALAR); |
| 61 |
8 |
| SPAGAIN; |
| 62 |
8 |
| ret = POPs; |
| 63 |
8 |
▶ | if (SvOK(ret)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(ret)) { |
7 |
1 |
8 |
|
| 64 |
|
| STRLEN rl; |
| 65 |
7 |
| const char *rp = SvPV(ret, rl); |
| 66 |
7 |
▶ | if (rl > sizeof(buf) - 1) forced = PO_TN_TOO_LONG; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rl > sizeof(buf) - 1) forced = PO_TN_TOO_LONG; |
1 |
6 |
7 |
|
| 67 |
|
| else { |
| 68 |
6 |
| memcpy(buf, rp, rl); |
| 69 |
6 |
| buf[rl] = '\0'; |
| 70 |
6 |
| tn.p = buf; tn.len = (size_t)rl; |
| 71 |
|
| } |
| 72 |
|
| } |
| 73 |
8 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
8 |
0 |
8 |
|
| 74 |
|
| /* The return stack moved under call_sv; re-fetch before this |
| 75 |
|
| * XSUB pushes anything of its own. */ |
| 76 |
8 |
| SPAGAIN; |
| 77 |
8 |
▶ | if (forced) rc = forced; |
Branch: 1 branch
| Branch | True | False | Total |
| if (forced) rc = forced; |
1 |
7 |
8 |
|
| 78 |
|
| else { |
| 79 |
7 |
| po_tenant_set_fn(&cfg, po_tn_cb, &tn); |
| 80 |
7 |
| rc = po_tenant_resolve(&cfg, out, sizeof(out), &len); |
| 81 |
|
| } |
| 82 |
|
| } |
| 83 |
19 |
| else rc = po_tenant_resolve(&cfg, out, sizeof(out), &len); |
| 84 |
27 |
| hv_stores(res, "ok", newSViv(rc == PO_TN_OK)); |
| 85 |
27 |
| hv_stores(res, "code", newSViv(rc)); |
| 86 |
27 |
| hv_stores(res, "reason", newSVpv(po_tn_reason(rc), 0)); |
| 87 |
27 |
▶ | if (rc == PO_TN_OK) hv_stores(res, "tenant", newSVpvn(out, len)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == PO_TN_OK) hv_stores(res, "tenant", newSVpvn(out, len)); |
20 |
7 |
27 |
|
| 88 |
27 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
27 |
27 |
|
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| SV * |
| 92 |
|
| potn_hash(SV *id) |
| 93 |
|
| CODE: |
| 94 |
|
| { |
| 95 |
|
| STRLEN l; |
| 96 |
2 |
| const char *p = SvPV(id, l); |
| 97 |
2 |
| RETVAL = newSVuv((UV)po_tenant_hash(p, (size_t)l)); |
| 98 |
|
| } |
| 99 |
|
| OUTPUT: |
| 100 |
|
| RETVAL |
| 101 |
|
| |
| 102 |
|
| int |
| 103 |
|
| potn_owns(SV *header_hash, SV *id) |
| 104 |
|
| CODE: |
| 105 |
|
| { |
| 106 |
|
| STRLEN l; |
| 107 |
3 |
| const char *p = SvPV(id, l); |
| 108 |
3 |
| RETVAL = po_tenant_owns((uint32_t)SvUV(header_hash), p, (size_t)l); |
| 109 |
|
| } |
| 110 |
|
| OUTPUT: |
| 111 |
|
| RETVAL |
| 112 |
|
| |
| 113 |
|
| MODULE = Punk::Observe PACKAGE = Punk::Observe::Key PREFIX = poky_ |
| 114 |
|
| |
| 115 |
|
| void |
| 116 |
|
| poky_bearer(SV *hdr) |
| 117 |
|
| PPCODE: |
| 118 |
|
| { |
| 119 |
|
| STRLEN l; |
| 120 |
23 |
▶ | const char *p = SvOK(hdr) ? SvPV(hdr, l) : (l = 0, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = SvOK(hdr) ? SvPV(hdr, l) : (l = 0, NULL); |
11 |
1 |
12 |
|
| 121 |
12 |
| const char *tok = NULL; |
| 122 |
12 |
| size_t tl = 0; |
| 123 |
12 |
▶ | if (p && po_bearer(p, (size_t)l, &tok, &tl)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (p && po_bearer(p, (size_t)l, &tok, &tl)) |
11 |
1 |
12 |
| if (p && po_bearer(p, (size_t)l, &tok, &tl)) |
5 |
6 |
11 |
|
| 124 |
5 |
▶ | mXPUSHs(newSVpvn(tok, tl)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newSVpvn(tok, tl)); |
0 |
5 |
5 |
|
| 125 |
7 |
| else XSRETURN_UNDEF; |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
|
| # Build a ring from (name => token) pairs, then check a presented header. |
| 129 |
|
| void |
| 130 |
|
| poky_check(SV *keys, SV *hdr, SV *revoke) |
| 131 |
|
| PPCODE: |
| 132 |
|
| { |
| 133 |
|
| po_keyring r; |
| 134 |
|
| AV *av; |
| 135 |
|
| SSize_t i, n; |
| 136 |
14 |
| const char *tok = NULL, *name = NULL; |
| 137 |
14 |
| size_t tl = 0, nl = 0; |
| 138 |
|
| STRLEN hl; |
| 139 |
|
| const char *h; |
| 140 |
|
| int rc; |
| 141 |
14 |
| HV *res = newHV(); |
| 142 |
|
| |
| 143 |
14 |
| po_keyring_init(&r); |
| 144 |
14 |
▶ | if (SvROK(keys) && SvTYPE(SvRV(keys)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(keys) && SvTYPE(SvRV(keys)) == SVt_PVAV) { |
14 |
0 |
14 |
| if (SvROK(keys) && SvTYPE(SvRV(keys)) == SVt_PVAV) { |
14 |
0 |
14 |
|
| 145 |
14 |
| av = (AV *)SvRV(keys); |
| 146 |
14 |
| n = av_len(av) + 1; |
| 147 |
40 |
▶ | for (i = 0; i + 1 < n; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 1 < n; i += 2) { |
26 |
14 |
40 |
|
| 148 |
26 |
| SV **k = av_fetch(av, i, 0); |
| 149 |
26 |
| SV **v = av_fetch(av, i + 1, 0); |
| 150 |
|
| STRLEN kl, vl; |
| 151 |
|
| const char *kp, *vp; |
| 152 |
26 |
▶ | if (!k || !v) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !v) break; |
26 |
0 |
26 |
| if (!k || !v) break; |
26 |
0 |
26 |
|
| 153 |
26 |
| kp = SvPV(*k, kl); |
| 154 |
26 |
▶ | vp = SvPV(*v, vl); |
Branch: 1 branch
| Branch | True | False | Total |
| vp = SvPV(*v, vl); |
26 |
0 |
26 |
|
| 155 |
26 |
| po_keyring_add(&r, kp, (size_t)kl, vp, (size_t)vl); |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
14 |
▶ | if (SvOK(revoke)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(revoke)) { |
2 |
12 |
14 |
|
| 159 |
|
| STRLEN rl; |
| 160 |
2 |
| const char *rp = SvPV(revoke, rl); |
| 161 |
2 |
| po_keyring_revoke(&r, rp, (size_t)rl); |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
14 |
▶ | if (SvOK(hdr)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(hdr)) { |
12 |
2 |
14 |
|
| 165 |
12 |
| h = SvPV(hdr, hl); |
| 166 |
12 |
▶ | if (!po_bearer(h, (size_t)hl, &tok, &tl)) { tok = NULL; tl = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_bearer(h, (size_t)hl, &tok, &tl)) { tok = NULL; tl = 0; } |
3 |
9 |
12 |
|
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
14 |
| rc = po_keyring_check(&r, tok, tl, &name, &nl); |
| 170 |
14 |
| hv_stores(res, "ok", newSViv(rc == PO_AUTH_OK)); |
| 171 |
14 |
| hv_stores(res, "code", newSViv(rc)); |
| 172 |
14 |
| hv_stores(res, "required", newSViv(r.required)); |
| 173 |
14 |
▶ | if (rc == PO_AUTH_OK && name) |
Branch: 2 branches
| Branch | True | False | Total |
| if (rc == PO_AUTH_OK && name) |
4 |
10 |
14 |
| if (rc == PO_AUTH_OK && name) |
3 |
1 |
4 |
|
| 174 |
3 |
| hv_stores(res, "name", newSVpvn(name, nl)); |
| 175 |
14 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
14 |
14 |
|
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
|
| # The comparison itself, so a test can drive it directly. |
| 179 |
|
| int |
| 180 |
|
| poky_ct_eq(SV *a, SV *b) |
| 181 |
|
| CODE: |
| 182 |
|
| { |
| 183 |
|
| po_u64 ah, al, bh, bl; |
| 184 |
|
| STRLEN la, lb; |
| 185 |
5 |
▶ | const char *pa = SvPV(a, la); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *pa = SvPV(a, la); |
5 |
0 |
5 |
|
| 186 |
5 |
| const char *pb = SvPV(b, lb); |
| 187 |
5 |
| po_key_hash(pa, (size_t)la, &ah, &al); |
| 188 |
5 |
| po_key_hash(pb, (size_t)lb, &bh, &bl); |
| 189 |
5 |
| RETVAL = po_ct_eq128(ah, al, bh, bl); |
| 190 |
|
| } |
| 191 |
|
| OUTPUT: |
| 192 |
|
| RETVAL |
| 193 |
|
| |
| 194 |
|
| MODULE = Punk::Observe PACKAGE = Punk::Observe::Limit PREFIX = poli_ |
| 195 |
|
| |
| 196 |
|
| # The ingest rate, over a sequence of batches with the clock moved between |
| 197 |
|
| # them. Returns what was accepted per batch, which is what becomes the OTLP |
| 198 |
|
| # partial success. |
| 199 |
|
| void |
| 200 |
|
| poli_rate(SV *cfg, SV *batches) |
| 201 |
|
| PPCODE: |
| 202 |
|
| { |
| 203 |
|
| po_rate_cfg c; |
| 204 |
|
| po_rate_win w; |
| 205 |
|
| po_rate_local local; |
| 206 |
|
| HV *ch; |
| 207 |
|
| SV **f; |
| 208 |
|
| AV *av; |
| 209 |
|
| SSize_t i, n; |
| 210 |
9 |
| po_u64 max_rec = 0, max_bytes = 0; |
| 211 |
9 |
| AV *out = newAV(); |
| 212 |
9 |
| HV *res = newHV(); |
| 213 |
|
| |
| 214 |
9 |
▶ | if (!SvROK(cfg) || SvTYPE(SvRV(cfg)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(cfg) || SvTYPE(SvRV(cfg)) != SVt_PVHV) |
9 |
0 |
9 |
| if (!SvROK(cfg) || SvTYPE(SvRV(cfg)) != SVt_PVHV) |
0 |
9 |
9 |
|
| 215 |
0 |
| croak("cfg must be a hashref"); |
| 216 |
9 |
| ch = (HV *)SvRV(cfg); |
| 217 |
9 |
▶ | if ((f = hv_fetchs(ch, "records", 0))) (void)po_sv_to_u64(aTHX_ *f, &max_rec); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(ch, "records", 0))) (void)po_sv_to_u64(aTHX_ *f, &max_rec); |
7 |
2 |
9 |
|
| 218 |
9 |
▶ | if ((f = hv_fetchs(ch, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *f, &max_bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(ch, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *f, &max_bytes); |
2 |
7 |
9 |
|
| 219 |
9 |
| po_rate_cfg_init(&c, max_rec, max_bytes); |
| 220 |
9 |
| po_rate_local_bind(&local, &w); |
| 221 |
|
| |
| 222 |
9 |
▶ | if (!SvROK(batches)) croak("batches must be an arrayref"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(batches)) croak("batches must be an arrayref"); |
0 |
9 |
9 |
|
| 223 |
9 |
| av = (AV *)SvRV(batches); |
| 224 |
9 |
| n = av_len(av) + 1; |
| 225 |
22 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
13 |
9 |
22 |
|
| 226 |
13 |
| SV **e = av_fetch(av, i, 0); |
| 227 |
|
| HV *b; SV **g; |
| 228 |
13 |
| po_u64 at = 0, rec = 0, bytes = 0, got; |
| 229 |
13 |
▶ | if (!e || !SvROK(*e)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !SvROK(*e)) continue; |
13 |
0 |
13 |
| if (!e || !SvROK(*e)) continue; |
0 |
13 |
13 |
|
| 230 |
13 |
| b = (HV *)SvRV(*e); |
| 231 |
13 |
▶ | if ((g = hv_fetchs(b, "at", 0))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((g = hv_fetchs(b, "at", 0))) { |
13 |
0 |
13 |
|
| 232 |
13 |
| (void)po_sv_to_u64(aTHX_ *g, &at); |
| 233 |
13 |
| po_clock_freeze(at); |
| 234 |
|
| } |
| 235 |
13 |
▶ | if ((g = hv_fetchs(b, "records", 0))) (void)po_sv_to_u64(aTHX_ *g, &rec); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((g = hv_fetchs(b, "records", 0))) (void)po_sv_to_u64(aTHX_ *g, &rec); |
13 |
0 |
13 |
|
| 236 |
13 |
▶ | if ((g = hv_fetchs(b, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *g, &bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((g = hv_fetchs(b, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *g, &bytes); |
13 |
0 |
13 |
|
| 237 |
13 |
| got = po_rate_admit(&c, &w, rec, bytes); |
| 238 |
|
| { |
| 239 |
13 |
| HV *r = newHV(); |
| 240 |
13 |
| hv_stores(r, "offered", po_u64_to_sv(rec)); |
| 241 |
13 |
| hv_stores(r, "accepted", po_u64_to_sv(got)); |
| 242 |
13 |
| hv_stores(r, "rejected", po_u64_to_sv(rec - got)); |
| 243 |
13 |
| av_push(out, newRV_noinc((SV *)r)); |
| 244 |
|
| } |
| 245 |
|
| } |
| 246 |
9 |
| po_clock_real(); |
| 247 |
9 |
| hv_stores(res, "batches", newRV_noinc((SV *)out)); |
| 248 |
9 |
| hv_stores(res, "rejected", po_u64_to_sv(local.rejected)); |
| 249 |
9 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
9 |
9 |
|
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
|
| # The storage budget: which blocks survive, and how far back that reaches. |
| 253 |
|
| void |
| 254 |
|
| poli_storage(SV *blocks, SV *budget) |
| 255 |
|
| PPCODE: |
| 256 |
|
| { |
| 257 |
|
| po_blk_size *b; |
| 258 |
|
| AV *av; |
| 259 |
|
| SSize_t i, n; |
| 260 |
6 |
| po_u64 bud = 0, kept = 0, horizon = 0; |
| 261 |
|
| uint32_t keep; |
| 262 |
6 |
| HV *res = newHV(); |
| 263 |
|
| |
| 264 |
6 |
▶ | if (!SvROK(blocks)) croak("blocks must be an arrayref"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(blocks)) croak("blocks must be an arrayref"); |
0 |
6 |
6 |
|
| 265 |
6 |
| (void)po_sv_to_u64(aTHX_ budget, &bud); |
| 266 |
6 |
| av = (AV *)SvRV(blocks); |
| 267 |
6 |
| n = av_len(av) + 1; |
| 268 |
6 |
▶ | Newxz(b, n ? n : 1, po_blk_size); |
Branch: 3 branches
| Branch | True | False | Total |
| Newxz(b, n ? n : 1, po_blk_size); |
5 |
1 |
6 |
| Newxz(b, n ? n : 1, po_blk_size); |
0 |
5 |
5 |
| Newxz(b, n ? n : 1, po_blk_size); |
5 |
1 |
6 |
|
| 269 |
56 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
50 |
6 |
56 |
|
| 270 |
50 |
| SV **e = av_fetch(av, i, 0); |
| 271 |
|
| HV *h; SV **g; |
| 272 |
50 |
▶ | if (!e || !SvROK(*e)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !SvROK(*e)) continue; |
50 |
0 |
50 |
| if (!e || !SvROK(*e)) continue; |
0 |
50 |
50 |
|
| 273 |
50 |
| h = (HV *)SvRV(*e); |
| 274 |
50 |
▶ | if ((g = hv_fetchs(h, "age", 0))) (void)po_sv_to_u64(aTHX_ *g, &b[i].age_ns); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((g = hv_fetchs(h, "age", 0))) (void)po_sv_to_u64(aTHX_ *g, &b[i].age_ns); |
50 |
0 |
50 |
|
| 275 |
50 |
▶ | if ((g = hv_fetchs(h, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *g, &b[i].bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((g = hv_fetchs(h, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *g, &b[i].bytes); |
50 |
0 |
50 |
|
| 276 |
|
| } |
| 277 |
6 |
| keep = po_limit_storage(b, (uint32_t)n, bud, &kept, &horizon); |
| 278 |
6 |
| Safefree(b); |
| 279 |
|
| |
| 280 |
6 |
| hv_stores(res, "keep", newSVuv((UV)keep)); |
| 281 |
6 |
| hv_stores(res, "bytes", po_u64_to_sv(kept)); |
| 282 |
6 |
| hv_stores(res, "horizon", po_u64_to_sv(horizon)); |
| 283 |
6 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
6 |
6 |
|
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
|
| # The indexed-attribute allowlist, and the overflow that NAMES the attribute. |
| 287 |
|
| void |
| 288 |
|
| poli_attrs(SV *allow, SV *keys) |
| 289 |
|
| PPCODE: |
| 290 |
|
| { |
| 291 |
|
| po_labelset ls; |
| 292 |
|
| po_attr_overflow ov; |
| 293 |
|
| AV *av; |
| 294 |
|
| SSize_t i, n; |
| 295 |
5 |
| AV *indexed = newAV(), *residual = newAV(), *named = newAV(); |
| 296 |
5 |
| HV *res = newHV(); |
| 297 |
|
| int worst; |
| 298 |
|
| |
| 299 |
5 |
| memset(&ls, 0, sizeof(ls)); |
| 300 |
5 |
| po_attr_overflow_init(&ov); |
| 301 |
|
| |
| 302 |
9 |
▶ | if (SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) { |
4 |
1 |
5 |
| if (SvROK(allow) && SvTYPE(SvRV(allow)) == SVt_PVAV) { |
4 |
0 |
4 |
|
| 303 |
4 |
| AV *a = (AV *)SvRV(allow); |
| 304 |
4 |
| SSize_t j, m = av_len(a) + 1; |
| 305 |
9 |
▶ | for (j = 0; j < m; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < m; j++) { |
5 |
4 |
9 |
|
| 306 |
5 |
| SV **e = av_fetch(a, j, 0); |
| 307 |
|
| STRLEN l; |
| 308 |
|
| const char *p; |
| 309 |
5 |
▶ | if (!e) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) continue; |
0 |
5 |
5 |
|
| 310 |
5 |
| p = SvPV(*e, l); |
| 311 |
5 |
| po_labelset_add(&ls, p, (size_t)l); |
| 312 |
|
| } |
| 313 |
|
| } |
| 314 |
1 |
| else po_labelset_default(&ls); |
| 315 |
|
| |
| 316 |
5 |
▶ | if (!SvROK(keys)) croak("keys must be an arrayref"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(keys)) croak("keys must be an arrayref"); |
0 |
5 |
5 |
|
| 317 |
5 |
| av = (AV *)SvRV(keys); |
| 318 |
5 |
| n = av_len(av) + 1; |
| 319 |
62 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
57 |
5 |
62 |
|
| 320 |
57 |
| SV **e = av_fetch(av, i, 0); |
| 321 |
|
| STRLEN l; |
| 322 |
|
| const char *p; |
| 323 |
57 |
▶ | if (!e) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) continue; |
0 |
57 |
57 |
|
| 324 |
57 |
| p = SvPV(*e, l); |
| 325 |
57 |
▶ | if (po_is_label(&ls, p, (size_t)l)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_is_label(&ls, p, (size_t)l)) |
6 |
51 |
57 |
|
| 326 |
6 |
| av_push(indexed, newSVpvn(p, l)); |
| 327 |
|
| else { |
| 328 |
|
| /* NOT dropped. It stays in the record and is reachable by |
| 329 |
|
| * a residual filter - only the INDEX is bounded. */ |
| 330 |
51 |
| av_push(residual, newSVpvn(p, l)); |
| 331 |
51 |
| po_attr_overflow_note(&ov, p, (size_t)l); |
| 332 |
|
| } |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
|
| { |
| 336 |
|
| uint32_t k; |
| 337 |
27 |
▶ | for (k = 0; k < ov.n; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < ov.n; k++) { |
22 |
5 |
27 |
|
| 338 |
22 |
| HV *h = newHV(); |
| 339 |
22 |
| hv_stores(h, "name", newSVpvn(ov.name[k], ov.name_len[k])); |
| 340 |
22 |
| hv_stores(h, "count", po_u64_to_sv(ov.count[k])); |
| 341 |
22 |
| av_push(named, newRV_noinc((SV *)h)); |
| 342 |
|
| } |
| 343 |
|
| } |
| 344 |
5 |
| worst = po_attr_overflow_worst(&ov); |
| 345 |
|
| |
| 346 |
5 |
| hv_stores(res, "indexed", newRV_noinc((SV *)indexed)); |
| 347 |
5 |
| hv_stores(res, "residual", newRV_noinc((SV *)residual)); |
| 348 |
5 |
| hv_stores(res, "overflow", newRV_noinc((SV *)named)); |
| 349 |
5 |
| hv_stores(res, "other", po_u64_to_sv(ov.dropped_other)); |
| 350 |
5 |
▶ | hv_stores(res, "worst", worst >= 0 |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores(res, "worst", worst >= 0 |
5 |
0 |
5 |
|
| 351 |
|
| ? newSVpvn(ov.name[worst], ov.name_len[worst]) |
| 352 |
|
| : newSVpvs("")); |
| 353 |
5 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
5 |
5 |
|
| 354 |
|
| } |
| 355 |
|
| |
| 356 |
|
| # The cardinality counter, across forked workers. The arena is mapped BEFORE |
| 357 |
|
| # the fork: one mapped after is private per worker, and the symptom is a limit |
| 358 |
|
| # silently N times what was configured. |
| 359 |
|
| void |
| 360 |
|
| poli_cardinality_forked(SV *cap, SV *workers, SV *per_worker) |
| 361 |
|
| PPCODE: |
| 362 |
|
| { |
| 363 |
|
| po_shared sh; |
| 364 |
2 |
| po_u64 c = 0; |
| 365 |
2 |
| IV nw = SvIV(workers), n = SvIV(per_worker), i; |
| 366 |
2 |
| HV *res = newHV(); |
| 367 |
|
| |
| 368 |
2 |
| (void)po_sv_to_u64(aTHX_ cap, &c); |
| 369 |
2 |
▶ | if (!po_shared_init(&sh, c)) croak("no shared arena"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_shared_init(&sh, c)) croak("no shared arena"); |
0 |
2 |
2 |
|
| 370 |
2 |
▶ | if (!po_shared_is_shared(&sh)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_shared_is_shared(&sh)) { |
0 |
2 |
2 |
|
| 371 |
0 |
| po_shared_free(&sh); |
| 372 |
0 |
| hv_stores(res, "shared", newSViv(0)); |
| 373 |
0 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
0 |
0 |
|
| 374 |
0 |
| XSRETURN(1); |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
10 |
▶ | for (i = 0; i < nw; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nw; i++) { |
8 |
2 |
10 |
|
| 378 |
8 |
| pid_t pid = fork(); |
| 379 |
8 |
▶ | if (pid == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pid == 0) { |
0 |
8 |
8 |
|
| 380 |
|
| IV j; |
| 381 |
0 |
▶ | for (j = 0; j < n; j++) (void)po_limit_series(&sh); |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < n; j++) (void)po_limit_series(&sh); |
0 |
0 |
0 |
|
| 382 |
0 |
| _exit(0); |
| 383 |
|
| } |
| 384 |
|
| } |
| 385 |
|
| { |
| 386 |
|
| int status; |
| 387 |
10 |
▶ | while (wait(&status) > 0) ; |
Branch: 1 branch
| Branch | True | False | Total |
| while (wait(&status) > 0) ; |
8 |
2 |
10 |
|
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
2 |
| hv_stores(res, "shared", newSViv(1)); |
| 391 |
2 |
| hv_stores(res, "admitted", po_u64_to_sv(sh.m->series)); |
| 392 |
2 |
| hv_stores(res, "rejected", po_u64_to_sv(sh.m->rejected)); |
| 393 |
2 |
| hv_stores(res, "offered", po_u64_to_sv((po_u64)(nw * n))); |
| 394 |
2 |
| po_shared_free(&sh); |
| 395 |
2 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
2 |
2 |
|
| 396 |
|
| } |