| 1 |
|
| MODULE = Punk::Observe PACKAGE = Punk::Observe::Live PREFIX = poli_ |
| 2 |
|
| |
| 3 |
|
| # Can the tail cross workers in this process? Built with Shared::Arena's |
| 4 |
|
| # header AND its table resolved at boot. A test that needs a second worker |
| 5 |
|
| # asks, rather than assuming a layout. |
| 6 |
|
| int |
| 7 |
|
| poli_have_bus() |
| 8 |
|
| CODE: |
| 9 |
|
| #ifdef PO_HAVE_BUS |
| 10 |
3 |
▶ | RETVAL = PO_SA ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PO_SA ? 1 : 0; |
0 |
3 |
3 |
|
| 11 |
|
| #else |
| 12 |
|
| RETVAL = 0; |
| 13 |
|
| #endif |
| 14 |
|
| OUTPUT: |
| 15 |
|
| RETVAL |
| 16 |
|
| |
| 17 |
|
| # The slot size this build encodes for, and - once the ring is open - what |
| 18 |
|
| # one of its slots carries, a RUNTIME answer. A test asserts the ring holds |
| 19 |
|
| # at least the slot the record is sized against, because a record sized |
| 20 |
|
| # against the wrong number is refused rather than truncated. |
| 21 |
|
| void |
| 22 |
|
| poli_slot_sizes() |
| 23 |
|
| PPCODE: |
| 24 |
|
| { |
| 25 |
2 |
▶ | mXPUSHi(PO_TAIL_SLOT); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHi(PO_TAIL_SLOT); |
0 |
2 |
2 |
|
| 26 |
|
| #ifdef PO_HAVE_BUS |
| 27 |
2 |
▶ | mXPUSHi(po_sa_ring ? (IV)PO_SA->slot_bytes(po_sa_ring) : 0); |
Branch: 2 branches
| Branch | True | False | Total |
| mXPUSHi(po_sa_ring ? (IV)PO_SA->slot_bytes(po_sa_ring) : 0); |
0 |
2 |
2 |
| mXPUSHi(po_sa_ring ? (IV)PO_SA->slot_bytes(po_sa_ring) : 0); |
1 |
1 |
2 |
|
| 28 |
|
| #else |
| 29 |
|
| mXPUSHi(0); |
| 30 |
|
| #endif |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
|
| SV * |
| 34 |
|
| poli_topic(SV *tenant) |
| 35 |
|
| CODE: |
| 36 |
|
| { |
| 37 |
|
| STRLEN tl; |
| 38 |
13 |
| const char *t = SvPV(tenant, tl); |
| 39 |
|
| char out[PO_TAIL_TOPIC_MAX]; |
| 40 |
13 |
| size_t n = po_tail_topic(t, (size_t)tl, out, sizeof(out)); |
| 41 |
13 |
▶ | RETVAL = n ? newSVpvn(out, n) : newSVsv(&PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = n ? newSVpvn(out, n) : newSVsv(&PL_sv_undef); |
6 |
7 |
13 |
|
| 42 |
|
| } |
| 43 |
|
| OUTPUT: |
| 44 |
|
| RETVAL |
| 45 |
|
| |
| 46 |
|
| # Encode a record, then decode it straight back. The round trip is the test: |
| 47 |
|
| # a truncation that the decoder cannot see is the same as a lost line. |
| 48 |
|
| void |
| 49 |
|
| poli_roundtrip(SV *spec) |
| 50 |
|
| PPCODE: |
| 51 |
|
| { |
| 52 |
|
| po_tail_rec in, out; |
| 53 |
|
| HV *h; |
| 54 |
|
| SV **f; |
| 55 |
|
| char buf[PO_TAIL_SLOT]; |
| 56 |
|
| size_t n; |
| 57 |
12 |
| int cut = 0; |
| 58 |
12 |
| HV *res = newHV(); |
| 59 |
|
| |
| 60 |
12 |
▶ | if (!SvROK(spec) || SvTYPE(SvRV(spec)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(spec) || SvTYPE(SvRV(spec)) != SVt_PVHV) |
12 |
0 |
12 |
| if (!SvROK(spec) || SvTYPE(SvRV(spec)) != SVt_PVHV) |
0 |
12 |
12 |
|
| 61 |
0 |
| croak("spec must be a hashref"); |
| 62 |
12 |
| h = (HV *)SvRV(spec); |
| 63 |
12 |
| memset(&in, 0, sizeof(in)); |
| 64 |
12 |
▶ | if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &in.t_unix_nano); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &in.t_unix_nano); |
12 |
0 |
12 |
|
| 65 |
12 |
▶ | if ((f = hv_fetchs(h, "stream", 0))) (void)po_sv_to_u64(aTHX_ *f, &in.stream); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "stream", 0))) (void)po_sv_to_u64(aTHX_ *f, &in.stream); |
2 |
10 |
12 |
|
| 66 |
12 |
▶ | if ((f = hv_fetchs(h, "severity", 0))) in.severity = (uint32_t)SvUV(*f); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "severity", 0))) in.severity = (uint32_t)SvUV(*f); |
2 |
10 |
12 |
|
| 67 |
24 |
▶ | if ((f = hv_fetchs(h, "service", 0))) in.service = SvPV(*f, in.service_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "service", 0))) in.service = SvPV(*f, in.service_len); |
12 |
0 |
12 |
|
| 68 |
24 |
▶ | if ((f = hv_fetchs(h, "body", 0))) in.body = SvPV(*f, in.body_len); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "body", 0))) in.body = SvPV(*f, in.body_len); |
12 |
0 |
12 |
|
| 69 |
|
| |
| 70 |
12 |
| n = po_tail_encode(&in, buf, &cut); |
| 71 |
12 |
| hv_stores(res, "encoded_len", newSVuv((UV)n)); |
| 72 |
12 |
| hv_stores(res, "truncated", newSViv(cut)); |
| 73 |
12 |
| hv_stores(res, "fits_slot", newSViv(n <= PO_TAIL_SLOT)); |
| 74 |
|
| |
| 75 |
12 |
▶ | if (po_tail_decode(buf, n, &out)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_tail_decode(buf, n, &out)) { |
12 |
0 |
12 |
|
| 76 |
12 |
| hv_stores(res, "ok", newSViv(1)); |
| 77 |
12 |
| hv_stores(res, "t", po_u64_to_sv(out.t_unix_nano)); |
| 78 |
12 |
| hv_stores(res, "stream", po_u64_to_sv(out.stream)); |
| 79 |
12 |
| hv_stores(res, "severity", newSVuv((UV)out.severity)); |
| 80 |
12 |
▶ | hv_stores(res, "service", newSVpvn(out.service ? out.service : "", |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores(res, "service", newSVpvn(out.service ? out.service : "", |
11 |
1 |
12 |
|
| 81 |
|
| out.service_len)); |
| 82 |
12 |
▶ | hv_stores(res, "body", newSVpvn(out.body ? out.body : "", |
Branch: 1 branch
| Branch | True | False | Total |
| hv_stores(res, "body", newSVpvn(out.body ? out.body : "", |
11 |
1 |
12 |
|
| 83 |
|
| out.body_len)); |
| 84 |
12 |
| hv_stores(res, "flagged", newSViv(po_tail_is_truncated(&out))); |
| 85 |
|
| } |
| 86 |
0 |
| else hv_stores(res, "ok", newSViv(0)); |
| 87 |
12 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
12 |
12 |
|
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| # A slot that another process wrote is untrusted input. Feed the decoder a |
| 91 |
|
| # damaged one and require a refusal rather than a pointer past the end. |
| 92 |
|
| int |
| 93 |
|
| poli_decode_bad(SV *bytes) |
| 94 |
|
| CODE: |
| 95 |
|
| { |
| 96 |
|
| po_tail_rec r; |
| 97 |
|
| STRLEN len; |
| 98 |
7 |
| const char *p = SvPV(bytes, len); |
| 99 |
7 |
▶ | RETVAL = po_tail_decode(p, (size_t)len, &r) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = po_tail_decode(p, (size_t)len, &r) ? 1 : 0; |
0 |
7 |
7 |
|
| 100 |
|
| } |
| 101 |
|
| OUTPUT: |
| 102 |
|
| RETVAL |
| 103 |
|
| |
| 104 |
|
| # The resume ring: push rows, then ask what follows an id. |
| 105 |
|
| void |
| 106 |
|
| poli_ring(SV *opts) |
| 107 |
|
| PPCODE: |
| 108 |
|
| { |
| 109 |
|
| po_tail_ring ring; |
| 110 |
|
| HV *o; |
| 111 |
|
| SV **f; |
| 112 |
|
| AV *rows; |
| 113 |
|
| SSize_t i, n; |
| 114 |
6 |
| po_u64 since = 0, missed = 0; |
| 115 |
6 |
| uint32_t first = 0, avail; |
| 116 |
6 |
| AV *got = newAV(); |
| 117 |
6 |
| HV *res = newHV(); |
| 118 |
6 |
| UV cap = 0; |
| 119 |
6 |
| po_u64 bytes = 0; |
| 120 |
|
| |
| 121 |
6 |
▶ | if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) |
6 |
0 |
6 |
| if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) |
0 |
6 |
6 |
|
| 122 |
0 |
| croak("opts must be a hashref"); |
| 123 |
6 |
| o = (HV *)SvRV(opts); |
| 124 |
6 |
▶ | if ((f = hv_fetchs(o, "cap", 0))) cap = SvUV(*f); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(o, "cap", 0))) cap = SvUV(*f); |
6 |
0 |
6 |
|
| 125 |
6 |
▶ | if ((f = hv_fetchs(o, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *f, &bytes); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(o, "bytes", 0))) (void)po_sv_to_u64(aTHX_ *f, &bytes); |
1 |
5 |
6 |
|
| 126 |
6 |
▶ | if ((f = hv_fetchs(o, "since", 0))) (void)po_sv_to_u64(aTHX_ *f, &since); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((f = hv_fetchs(o, "since", 0))) (void)po_sv_to_u64(aTHX_ *f, &since); |
6 |
0 |
6 |
|
| 127 |
6 |
| f = hv_fetchs(o, "rows", 0); |
| 128 |
6 |
▶ | if (!f || !SvROK(*f)) croak("rows must be an arrayref"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!f || !SvROK(*f)) croak("rows must be an arrayref"); |
6 |
0 |
6 |
| if (!f || !SvROK(*f)) croak("rows must be an arrayref"); |
0 |
6 |
6 |
|
| 129 |
6 |
| rows = (AV *)SvRV(*f); |
| 130 |
6 |
| n = av_len(rows) + 1; |
| 131 |
|
| |
| 132 |
6 |
▶ | if (!po_tail_ring_init(&ring, (uint32_t)cap, bytes)) croak("oom"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_tail_ring_init(&ring, (uint32_t)cap, bytes)) croak("oom"); |
0 |
6 |
6 |
|
| 133 |
51 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
45 |
6 |
51 |
|
| 134 |
45 |
| SV **e = av_fetch(rows, i, 0); |
| 135 |
|
| STRLEN l; |
| 136 |
|
| const char *p; |
| 137 |
45 |
▶ | if (!e) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) continue; |
0 |
45 |
45 |
|
| 138 |
45 |
| p = SvPV(*e, l); |
| 139 |
45 |
| (void)po_tail_ring_push(&ring, p, (size_t)l); |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
6 |
| avail = po_tail_ring_since(&ring, since, &first, &missed); |
| 143 |
21 |
▶ | for (i = 0; i < (SSize_t)avail; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < (SSize_t)avail; i++) { |
15 |
6 |
21 |
|
| 144 |
15 |
| const po_ring_ent *e = po_tail_ring_at(&ring, first + (uint32_t)i); |
| 145 |
15 |
| HV *rh = newHV(); |
| 146 |
15 |
▶ | if (!e) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) break; |
0 |
15 |
15 |
|
| 147 |
15 |
| hv_stores(rh, "id", po_u64_to_sv(e->id)); |
| 148 |
15 |
| hv_stores(rh, "data", newSVpvn(e->p, e->len)); |
| 149 |
15 |
| av_push(got, newRV_noinc((SV *)rh)); |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
6 |
| hv_stores(res, "rows", newRV_noinc((SV *)got)); |
| 153 |
6 |
| hv_stores(res, "missed", po_u64_to_sv(missed)); |
| 154 |
6 |
| hv_stores(res, "evicted", po_u64_to_sv(ring.evicted)); |
| 155 |
6 |
| hv_stores(res, "held", newSVuv((UV)ring.n)); |
| 156 |
6 |
| hv_stores(res, "oldest", po_u64_to_sv(po_tail_ring_oldest(&ring))); |
| 157 |
6 |
| hv_stores(res, "bytes", po_u64_to_sv(ring.bytes)); |
| 158 |
6 |
| po_tail_ring_free(&ring); |
| 159 |
6 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
6 |
6 |
|
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
|
| # Backpressure: feed a client that never drains and require a close, not a |
| 163 |
|
| # queue that grows until something else fails. |
| 164 |
|
| void |
| 165 |
|
| poli_flow(SV *limit, SV *sizes, SV *drain_each) |
| 166 |
|
| PPCODE: |
| 167 |
|
| { |
| 168 |
|
| po_tail_flow f; |
| 169 |
|
| AV *av; |
| 170 |
|
| SSize_t i, n; |
| 171 |
3 |
| po_u64 lim = 0; |
| 172 |
3 |
| int drain = SvTRUE(drain_each); |
| 173 |
3 |
| IV admitted = 0, refused = 0; |
| 174 |
3 |
| HV *res = newHV(); |
| 175 |
|
| |
| 176 |
3 |
| (void)po_sv_to_u64(aTHX_ limit, &lim); |
| 177 |
3 |
▶ | if (!SvROK(sizes)) croak("sizes must be an arrayref"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(sizes)) croak("sizes must be an arrayref"); |
0 |
3 |
3 |
|
| 178 |
3 |
| av = (AV *)SvRV(sizes); |
| 179 |
3 |
| n = av_len(av) + 1; |
| 180 |
3 |
| po_tail_flow_init(&f, lim); |
| 181 |
104 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
101 |
3 |
104 |
|
| 182 |
101 |
| SV **e = av_fetch(av, i, 0); |
| 183 |
101 |
▶ | size_t sz = e ? (size_t)SvUV(*e) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t sz = e ? (size_t)SvUV(*e) : 0; |
101 |
0 |
101 |
|
| 184 |
101 |
▶ | if (po_tail_flow_admit(&f, sz)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_tail_flow_admit(&f, sz)) { |
60 |
41 |
101 |
|
| 185 |
60 |
| admitted++; |
| 186 |
60 |
▶ | if (drain) po_tail_flow_drained(&f, sz); |
Branch: 1 branch
| Branch | True | False | Total |
| if (drain) po_tail_flow_drained(&f, sz); |
50 |
10 |
60 |
|
| 187 |
|
| } |
| 188 |
41 |
| else refused++; |
| 189 |
|
| } |
| 190 |
3 |
| hv_stores(res, "admitted", newSViv(admitted)); |
| 191 |
3 |
| hv_stores(res, "refused", newSViv(refused)); |
| 192 |
3 |
| hv_stores(res, "pending", po_u64_to_sv(f.pending)); |
| 193 |
3 |
| hv_stores(res, "closed", newSViv(f.closed)); |
| 194 |
3 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
3 |
3 |
|
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| SV * |
| 198 |
|
| poli_sse(SV *id, SV *event, SV *data) |
| 199 |
|
| CODE: |
| 200 |
|
| { |
| 201 |
4 |
| po_u64 i = 0; |
| 202 |
|
| STRLEN el, dl; |
| 203 |
|
| const char *e, *d; |
| 204 |
|
| char out[8192]; |
| 205 |
|
| size_t n; |
| 206 |
4 |
| (void)po_sv_to_u64(aTHX_ id, &i); |
| 207 |
7 |
▶ | e = SvOK(event) ? SvPV(event, el) : (el = 0, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| e = SvOK(event) ? SvPV(event, el) : (el = 0, ""); |
3 |
1 |
4 |
|
| 208 |
4 |
| d = SvPV(data, dl); |
| 209 |
4 |
| n = po_sse_frame(i, e, (size_t)el, d, (size_t)dl, out, sizeof(out)); |
| 210 |
4 |
| RETVAL = newSVpvn(out, n); |
| 211 |
|
| } |
| 212 |
|
| OUTPUT: |
| 213 |
|
| RETVAL |
| 214 |
|
| |
| 215 |
|
| SV * |
| 216 |
|
| poli_heartbeat() |
| 217 |
|
| CODE: |
| 218 |
|
| { |
| 219 |
|
| char out[8]; |
| 220 |
1 |
| size_t n = po_sse_heartbeat(out, sizeof(out)); |
| 221 |
1 |
| RETVAL = newSVpvn(out, n); |
| 222 |
|
| } |
| 223 |
|
| OUTPUT: |
| 224 |
|
| RETVAL |
| 225 |
|
| |
| 226 |
|
| # ---- the bus, where this build has one ------------------------------------- |
| 227 |
|
| |
| 228 |
|
| # Set up the ring. Called BEFORE the fork, because a subscription made after |
| 229 |
|
| # one lands in a single process and a cursor that starts at "now" silently |
| 230 |
|
| # misses everything published before it. |
| 231 |
|
| # |
| 232 |
|
| # bus_init() an anonymous Shared::Arena of Observe's own |
| 233 |
|
| # bus_init(, ) the ring on a Shared::Arena the caller holds |
| 234 |
|
| # (Hyperman->arena, or a test's with its hooks) |
| 235 |
|
| # |
| 236 |
|
| # The ring's slot is 64 bytes wider than PO_TAIL_SLOT, so a record sized |
| 237 |
|
| # against PO_TAIL_SLOT always fits one slot and never spans. Idempotent. |
| 238 |
|
| int |
| 239 |
|
| poli_bus_init(int slots, ...) |
| 240 |
|
| CODE: |
| 241 |
|
| #ifdef PO_HAVE_BUS |
| 242 |
|
| { |
| 243 |
4 |
| int err = 0; |
| 244 |
4 |
▶ | if (!PO_SA) { RETVAL = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PO_SA) { RETVAL = 0; } |
0 |
4 |
4 |
|
| 245 |
4 |
▶ | else if (po_sa_ring) { RETVAL = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (po_sa_ring) { RETVAL = 1; } |
1 |
3 |
4 |
|
| 246 |
|
| else { |
| 247 |
3 |
▶ | if (slots < 2) slots = 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slots < 2) slots = 2; |
0 |
3 |
3 |
|
| 248 |
3 |
▶ | if (items > 1 && SvROK(ST(1))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (items > 1 && SvROK(ST(1))) { |
1 |
2 |
3 |
| if (items > 1 && SvROK(ST(1))) { |
1 |
0 |
1 |
|
| 249 |
|
| /* Ride on the caller's arena: Shared::Arena::_region_ptr */ |
| 250 |
1 |
| dSP; |
| 251 |
|
| int count; |
| 252 |
1 |
| ENTER; SAVETMPS; |
| 253 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 254 |
1 |
▶ | XPUSHs(ST(1)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(ST(1)); |
0 |
1 |
1 |
|
| 255 |
1 |
| PUTBACK; |
| 256 |
1 |
| count = call_method("_region_ptr", G_SCALAR | G_EVAL); |
| 257 |
1 |
| SPAGAIN; |
| 258 |
1 |
▶ | if (count == 1 && !SvTRUE(ERRSV)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (count == 1 && !SvTRUE(ERRSV)) { |
1 |
0 |
1 |
| if (count == 1 && !SvTRUE(ERRSV)) { |
1 |
0 |
1 |
| if (count == 1 && !SvTRUE(ERRSV)) { |
1 |
0 |
1 |
|
| 259 |
1 |
| SV *p = POPs; |
| 260 |
1 |
| po_sa_region = INT2PTR(sa_region *, SvUV(p)); |
| 261 |
|
| } |
| 262 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 263 |
|
| } |
| 264 |
3 |
▶ | if (!po_sa_region) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_sa_region) { |
2 |
1 |
3 |
|
| 265 |
|
| sa_config cfg; |
| 266 |
2 |
| PO_SA->config_init(&cfg); |
| 267 |
2 |
| cfg.bytes = (uint64_t)slots * (PO_TAIL_SLOT + 64) + 65536; |
| 268 |
2 |
| po_sa_region = PO_SA->create(&cfg, &err); |
| 269 |
|
| } |
| 270 |
3 |
▶ | if (po_sa_region) |
Branch: 1 branch
| Branch | True | False | Total |
| if (po_sa_region) |
3 |
0 |
3 |
|
| 271 |
3 |
| po_sa_ring = PO_SA->ring_open(po_sa_region, "po.tail", 7, |
| 272 |
|
| (uint64_t)slots, |
| 273 |
|
| PO_TAIL_SLOT + 64, &err); |
| 274 |
3 |
| RETVAL = po_sa_ring ? 1 : 0; |
| 275 |
|
| } |
| 276 |
|
| } |
| 277 |
|
| #else |
| 278 |
|
| (void)slots; |
| 279 |
|
| RETVAL = 0; |
| 280 |
|
| #endif |
| 281 |
|
| OUTPUT: |
| 282 |
|
| RETVAL |
| 283 |
|
| |
| 284 |
|
| int |
| 285 |
|
| poli_bus_publish(SV *topic, SV *payload) |
| 286 |
|
| CODE: |
| 287 |
|
| #ifdef PO_HAVE_BUS |
| 288 |
|
| { |
| 289 |
|
| STRLEN tl, pl; |
| 290 |
2052 |
▶ | const char *t = SvPV(topic, tl); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *t = SvPV(topic, tl); |
2052 |
0 |
2052 |
|
| 291 |
2052 |
| const char *p = SvPV(payload, pl); |
| 292 |
|
| /* 0 on the ring, -1 refused as oversize (never truncated: the |
| 293 |
|
| * record is truncated at ingest, where the reader can see it), |
| 294 |
|
| * 1 with no ring (this process only), -2 built without one. */ |
| 295 |
2052 |
▶ | if (!po_sa_ring) RETVAL = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!po_sa_ring) RETVAL = 1; |
0 |
2052 |
2052 |
|
| 296 |
2052 |
▶ | else if ((uint64_t)tl + pl > PO_TAIL_SLOT - 16) RETVAL = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| else if ((uint64_t)tl + pl > PO_TAIL_SLOT - 16) RETVAL = -1; |
0 |
2052 |
2052 |
|
| 297 |
|
| else { |
| 298 |
2052 |
| int64_t rc = PO_SA->publish(po_sa_ring, t, (uint32_t)tl, |
| 299 |
|
| p, (uint32_t)pl); |
| 300 |
2052 |
▶ | RETVAL = SA_PUBLISHED(rc) ? 0 : rc == SA_TOO_BIG ? -1 : 1; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = SA_PUBLISHED(rc) ? 0 : rc == SA_TOO_BIG ? -1 : 1; |
0 |
2052 |
2052 |
| RETVAL = SA_PUBLISHED(rc) ? 0 : rc == SA_TOO_BIG ? -1 : 1; |
0 |
0 |
0 |
|
| 301 |
|
| } |
| 302 |
|
| } |
| 303 |
|
| #else |
| 304 |
|
| (void)topic; (void)payload; |
| 305 |
|
| RETVAL = -2; |
| 306 |
|
| #endif |
| 307 |
|
| OUTPUT: |
| 308 |
|
| RETVAL |
| 309 |
|
| |
| 310 |
|
| # Drain the fanout cursor, returning every payload seen and the gap count. |
| 311 |
|
| # |
| 312 |
|
| # A GAP IS REPORTED, NOT SWALLOWED. A slow consumer that gets lapped and says |
| 313 |
|
| # nothing produces a stream indistinguishable from a quiet one. |
| 314 |
|
| void |
| 315 |
|
| poli_bus_drain(SV *topic, ...) |
| 316 |
|
| PPCODE: |
| 317 |
|
| { |
| 318 |
|
| #ifdef PO_HAVE_BUS |
| 319 |
264 |
| AV *out = newAV(); |
| 320 |
264 |
| HV *res = newHV(); |
| 321 |
|
| STRLEN tl; |
| 322 |
264 |
| const char *t = SvPV(topic, tl); |
| 323 |
|
| long got; |
| 324 |
264 |
| po_u64 before = po_live_gaps; |
| 325 |
|
| po_live_ud ud; |
| 326 |
264 |
| ud.av = out; ud.t = t; ud.tl = (size_t)tl; |
| 327 |
|
| { |
| 328 |
264 |
| sa_cursor *c = po_live_cursor(); |
| 329 |
|
| /* Draining is proof of life: a reader that only ever drains |
| 330 |
|
| * must still tick, or the ring's hole check would judge it |
| 331 |
|
| * wedged. Join once per process, beat on each drain. */ |
| 332 |
264 |
▶ | if (c && !po_sa_joined) { PO_SA->join(po_sa_region); po_sa_joined = 1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (c && !po_sa_joined) { PO_SA->join(po_sa_region); po_sa_joined = 1; } |
264 |
0 |
264 |
| if (c && !po_sa_joined) { PO_SA->join(po_sa_region); po_sa_joined = 1; } |
4 |
260 |
264 |
|
| 333 |
264 |
▶ | if (c) PO_SA->beat(po_sa_region); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) PO_SA->beat(po_sa_region); |
264 |
0 |
264 |
|
| 334 |
264 |
▶ | got = c ? PO_SA->drain(c, 0, po_live_collect, &ud) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| got = c ? PO_SA->drain(c, 0, po_live_collect, &ud) : 0; |
264 |
0 |
264 |
|
| 335 |
264 |
| po_live_gaps = po_live_cursor_gaps(); |
| 336 |
|
| } |
| 337 |
|
| /* THE DELTA GOES TO THE ARENA, when the caller passed one. |
| 338 |
|
| * |
| 339 |
|
| * po_live_gaps is a per-process cumulative: honest to this |
| 340 |
|
| * worker, invisible to the one that renders the status page. The |
| 341 |
|
| * arena is the cross-worker ledger, and it takes the DELTA |
| 342 |
|
| * because adding the cumulative on every drain would count each |
| 343 |
|
| * lost line once per drain that happened after it. */ |
| 344 |
264 |
▶ | if (items > 1 && SvOK(ST(1)) && po_live_gaps > before) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (items > 1 && SvOK(ST(1)) && po_live_gaps > before) { |
2 |
262 |
264 |
| if (items > 1 && SvOK(ST(1)) && po_live_gaps > before) { |
2 |
0 |
2 |
| if (items > 1 && SvOK(ST(1)) && po_live_gaps > before) { |
1 |
1 |
2 |
|
| 345 |
1 |
| po_shared *sh = INT2PTR(po_shared *, SvIV(ST(1))); |
| 346 |
1 |
▶ | if (sh && sh->ok) |
Branch: 2 branches
| Branch | True | False | Total |
| if (sh && sh->ok) |
1 |
0 |
1 |
| if (sh && sh->ok) |
1 |
0 |
1 |
|
| 347 |
1 |
| po_atomic_add(&sh->m->live_gaps, po_live_gaps - before); |
| 348 |
|
| } |
| 349 |
264 |
| hv_stores(res, "count", newSViv(got)); |
| 350 |
264 |
| hv_stores(res, "gaps", po_u64_to_sv(po_live_gaps)); |
| 351 |
264 |
| hv_stores(res, "rows", newRV_noinc((SV *)out)); |
| 352 |
264 |
▶ | mXPUSHs(newRV_noinc((SV *)res)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)res)); |
0 |
264 |
264 |
|
| 353 |
|
| #else |
| 354 |
|
| (void)topic; |
| 355 |
|
| XSRETURN_UNDEF; |
| 356 |
|
| #endif |
| 357 |
|
| } |
| 358 |
|
| |
| 359 |
|
| # THE POST-FORK HOOK. A worker forked after a publish must not replay it, and |
| 360 |
|
| # one forked before must not skip what arrives after: both are the same |
| 361 |
|
| # inherited cursor, and both look like a broken tail rather than a fork bug. |
| 362 |
|
| void |
| 363 |
|
| poli_bus_reset_cursors() |
| 364 |
|
| PPCODE: |
| 365 |
|
| #ifdef PO_HAVE_BUS |
| 366 |
4 |
| po_live_cursor_reset(); |
| 367 |
|
| #endif |
| 368 |
4 |
| XSRETURN_EMPTY; |