xs/ring.xs

filter: Statementstmt 98.3% 120 / 122 Branchbran 61.1% 66 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.8% 186 / 230 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/bloom.xs ↑ Summary xs/hll.xs »
1 # Shared::Arena::Ring and its cursor.
2 #
3 # Both are objects holding a C pointer, and every entry takes one explicitly.
4 # A cursor in particular MUST be an object: two readers in one process that
5 # shared a position would consume each other's records, and whichever asked
6 # first would get everything.
7  
8 MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_
9  
10 # $arena->ring($name, slots => N, slot_size => N) -> Shared::Arena::Ring
11 #
12 # Carves the region if it is not there, binds to it if it is. Every process can
13 # call this with the same arguments; exactly one of them does the work.
14 SV *
15 sar_ring(self, name, ...)
16 SV *self
17 SV *name
18 PREINIT:
19 sa_region *arena;
20 sa_reg *e;
21 sa_ring *r;
22 const char *nm;
23 STRLEN nlen;
24 65 UV slots = 1024, slot_size = 512;
25 65 int err = SA_E_OK;
26 I32 i;
27 SV *obj;
28 CODE:
29 65 arena = SA_SELF(sa_region, self);
30 65 ▶ if (!arena || !arena->map.base)
Branch: 2 branches
BranchTrueFalseTotal
if (!arena || !arena->map.base) 65 0 65
if (!arena || !arena->map.base) 0 65 65
31 0 croak("Shared::Arena: this region is released");
32 65 nm = SvPV(name, nlen);
33 195 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 130 65 195
34 130 const char *o = SvPV_nolen(ST(i));
35 130 ▶ if (strEQ(o, "slots")) slots = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(o, "slots")) slots = SvUV(ST(i + 1)); 65 65 130
36 65 ▶ else if (strEQ(o, "slot_size")) slot_size = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(o, "slot_size")) slot_size = SvUV(ST(i + 1)); 65 0 65
37 }
38 65 ▶ if (slots < 2) slots = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (slots < 2) slots = 2; 0 65 65
39 65 ▶ if (sa_ring_capacity((uint32_t)slot_size) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sa_ring_capacity((uint32_t)slot_size) == 0) 0 65 65
40 0 croak("Shared::Arena: slot_size %lu is smaller than a slot header",
41 (unsigned long)slot_size);
42  
43 65 e = sa_carve(arena, nm, (size_t)nlen,
44 sa_ring_bytes((uint64_t)slots, (uint32_t)slot_size),
45 SA_T_RING, &err);
46 65 ▶ if (!e) croak("Shared::Arena: the ring '%s' %s", nm, sa_strerror(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!e) croak("Shared::Arena: the ring '%s' %s", nm, sa_strerror(err)); 0 65 65
47  
48 65 r = sa_ring_bind(arena, e, (uint64_t)slots, (uint32_t)slot_size, &err);
49 65 ▶ if (!r) croak("Shared::Arena: the ring '%s' %s", nm, sa_strerror(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena: the ring '%s' %s", nm, sa_strerror(err)); 0 65 65
50  
51 65 obj = newSV(0);
52 65 sv_setref_pv(obj, "Shared::Arena::Ring", (void *)r);
53 /* The ring borrows the arena's mapping, so the arena must outlive it.
54 * Holding a reference in the ring's own magic is what makes
55 * `my $r = Shared::Arena->create(...)->ring('x')` safe rather than a
56 * use-after-free the moment the temporary arena goes. */
57 65 sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0);
58 65 SvREFCNT_inc(SvRV(self));
59 65 RETVAL = obj;
60 OUTPUT:
61 RETVAL
62  
63 MODULE = Shared::Arena PACKAGE = Shared::Arena::Ring PREFIX = sarr_
64  
65 # The SEQUENCE the record was given, which is greater than zero for every
66 # published record; 0 when there is no ring, and -1 when the record was refused
67 # for size.
68 #
69 # One value rather than a pair, and not a context-sensitive one. `publish`
70 # returning (rc, seq) in list context reads well and then breaks the moment
71 # somebody writes `is($ring->publish(...), 1)` - which calls it in LIST context
72 # and compares the two returned values against each other. Sequences start at 1,
73 # so a single number carries both answers with no trap in it.
74 IV
75 sarr_publish(self, topic, payload)
76 SV *self
77 SV *topic
78 SV *payload
79 PREINIT:
80 sa_ring *r;
81 const char *t, *p;
82 STRLEN tlen, plen;
83 1 ▶ uint64_t seq = 0;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t seq = 0; 1 0 1
84 int rc;
85 CODE:
86 1 r = SA_SELF(sa_ring, self);
87 1 ▶ if (!r) croak("Shared::Arena::Ring: this ring is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena::Ring: this ring is released"); 0 1 1
88 1 ▶ t = SvPV(topic, tlen);
Branch: 1 branch
BranchTrueFalseTotal
t = SvPV(topic, tlen); 1 0 1
89 1 p = SvPV(payload, plen);
90 1 rc = sa_ring_publish(r, t, (uint32_t)tlen, p, (uint32_t)plen, &seq);
91 1 ▶ RETVAL = (rc == SA_PUB_OK) ? (IV)seq : (IV)rc;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (rc == SA_PUB_OK) ? (IV)seq : (IV)rc; 0 1 1
RETVAL = (rc == SA_PUB_OK) ? (IV)seq : (IV)rc; 0 1 1
92 OUTPUT:
93 RETVAL
94  
95 # The largest topic+payload one record can carry. A RUNTIME accessor and never
96 # a constant a caller compiles in: a consumer that hard-codes the number is a
97 # consumer that starts silently refusing records the day the ring is configured
98 # with a different slot size.
99 UV
100 sarr_max_record(self)
101 SV *self
102 PREINIT:
103 sa_ring *r;
104 CODE:
105 3 r = SA_SELF(sa_ring, self);
106 3 ▶ if (!r) croak("Shared::Arena::Ring: this ring is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena::Ring: this ring is released"); 0 3 3
107 3 ▶ RETVAL = (UV)r->record_max;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)r->record_max; 0 3 3
108 OUTPUT:
109 RETVAL
110  
111 # What ONE slot carries. `max_record` is this times the slots a record may
112 # span; a caller sizing a ring wants both numbers.
113 UV
114 sarr_slot_bytes(self)
115 SV *self
116 PREINIT:
117 sa_ring *r;
118 CODE:
119 1 r = SA_SELF(sa_ring, self);
120 1 ▶ if (!r) croak("Shared::Arena::Ring: this ring is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena::Ring: this ring is released"); 0 1 1
121 1 ▶ RETVAL = (UV)r->payload_max;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)r->payload_max; 0 1 1
122 OUTPUT:
123 RETVAL
124  
125 UV
126 sarr_slots(self)
127 SV *self
128 PREINIT:
129 sa_ring *r;
130 CODE:
131 1 r = SA_SELF(sa_ring, self);
132 1 ▶ if (!r) croak("Shared::Arena::Ring: this ring is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena::Ring: this ring is released"); 0 1 1
133 1 ▶ RETVAL = (UV)r->nslots;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)r->nslots; 0 1 1
134 OUTPUT:
135 RETVAL
136  
137 # published / oversize / seq, as a list of pairs.
138 void
139 sarr_stats(self)
140 SV *self
141 PREINIT:
142 sa_ring *r;
143 PPCODE:
144 342 r = SA_SELF(sa_ring, self);
145 342 ▶ if (!r) croak("Shared::Arena::Ring: this ring is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena::Ring: this ring is released"); 0 342 342
146 342 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 342 342
147 342 mPUSHp("published", 9);
148 342 mPUSHu((UV)sa_at_load64_acq(&r->hdr->published));
149 342 mPUSHp("oversize", 8);
150 342 mPUSHu((UV)sa_at_load64_acq(&r->hdr->oversize));
151 342 mPUSHp("seq", 3);
152 342 mPUSHu((UV)sa_at_load64_acq(&r->hdr->seq));
153  
154 # $ring->cursor(from_start => 1)
155 #
156 # A cursor starts at NOW by default, which is what a tail wants: a process that
157 # attaches to a busy ring is asking about what happens next, not about the last
158 # ten thousand records it missed.
159 SV *
160 sarr_cursor(self, ...)
161 SV *self
162 PREINIT:
163 sa_ring *r;
164 sa_cursor *c;
165 28 int from_start = 0;
166 I32 i;
167 SV *obj;
168 CODE:
169 28 r = SA_SELF(sa_ring, self);
170 28 ▶ if (!r) croak("Shared::Arena::Ring: this ring is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("Shared::Arena::Ring: this ring is released"); 0 28 28
171 30 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 2 28 30
172 2 const char *o = SvPV_nolen(ST(i));
173 2 ▶ if (strEQ(o, "from_start")) from_start = SvTRUE(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(o, "from_start")) from_start = SvTRUE(ST(i + 1)); 2 0 2
174 }
175 28 c = sa_cursor_new(r, from_start);
176 28 ▶ if (!c) croak("Shared::Arena::Ring: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Ring: out of memory"); 0 28 28
177 28 obj = newSV(0);
178 28 sv_setref_pv(obj, "Shared::Arena::Ring::Cursor", (void *)c);
179 28 sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0);
180 28 SvREFCNT_inc(SvRV(self));
181 28 RETVAL = obj;
182 OUTPUT:
183 RETVAL
184  
185 MODULE = Shared::Arena PACKAGE = Shared::Arena::Ring PREFIX = sarrd_
186  
187 void
188 sarrd_DESTROY(self)
189 SV *self
190 PREINIT:
191 sa_ring *r;
192 CODE:
193 65 r = SA_SELF(sa_ring, self);
194 65 ▶ if (r) { sa_ring_free(r); sv_setiv(SvRV(self), 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) { sa_ring_free(r); sv_setiv(SvRV(self), 0); } 65 0 65
195  
196 MODULE = Shared::Arena PACKAGE = Shared::Arena::Ring::Cursor PREFIX = sarc_
197  
198 # Everything published since this cursor last looked, as ([topic, payload], ...).
199 #
200 # The collector builds the list in C rather than calling back into Perl per
201 # record, because a drain of ten thousand records should not be ten thousand
202 # Perl frames.
203 #
204 # At a HOLE - a record reserved and never committed - the core never sleeps:
205 # the grace period runs on the cursor across calls, and this door keeps the
206 # blocking feel a Perl caller expects by napping between looks until the
207 # question "is the publisher dead" has been asked once, or the hole fills.
208 # `wait => 0` returns at once with whatever came before the hole, which is
209 # the C ABI's behaviour and what an event loop wants.
210 void
211 sarc_drain(self, ...)
212 SV *self
213 PREINIT:
214 sa_cursor *c;
215 299 IV max = 0;
216 299 int wait = 1;
217 I32 i;
218 PPCODE:
219 299 c = SA_SELF(sa_cursor, self);
220 299 ▶ if (!c) croak("Shared::Arena::Ring::Cursor: this cursor is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Ring::Cursor: this cursor is released"); 0 299 299
221 300 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 1 299 300
222 1 const char *o = SvPV_nolen(ST(i));
223 1 ▶ if (strEQ(o, "max")) max = SvIV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(o, "max")) max = SvIV(ST(i + 1)); 0 1 1
224 1 ▶ else if (strEQ(o, "wait")) wait = SvTRUE(ST(i + 1)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(o, "wait")) wait = SvTRUE(ST(i + 1)) ? 1 : 0; 1 0 1
225 }
226 {
227 299 uint64_t end, asks0 = c->hole_asks, t0 = sa_now_us(), budget;
228 299 uint32_t grace = sa_at_load32_acq(&c->ring->arena->hdr->reap_grace_us);
229 int stuck;
230 299 ▶ if (!grace) grace = 1000;
Branch: 1 branch
BranchTrueFalseTotal
if (!grace) grace = 1000; 0 299 299
231 /* Two grace periods and a little: one for the wait, one for a
232 * baseline that was moved by a claim that changed hands. Bounded,
233 * as every wait here is. */
234 299 budget = (uint64_t)grace * 2u + 100000u;
235 /* Draining is proof of life too: a peer that only ever reads must
236 * still tick, or a publisher's hole check would judge it wedged. */
237 299 sa_peer_join(c->ring->arena);
238 299 sa_peer_beat(c->ring->arena);
239 411 again:
240 411 stuck = 0;
241 411 sa_cursor_catchup(c);
242 411 end = sa_at_load64_acq(&c->ring->hdr->seq);
243 3064 ▶ while (c->seq < end && (max <= 0 || (IV)(SP - MARK) < max)) {
Branch: 3 branches
BranchTrueFalseTotal
while (c->seq < end && (max <= 0 || (IV)(SP - MARK) < max)) { 2774 290 3064
while (c->seq < end && (max <= 0 || (IV)(SP - MARK) < max)) { 2774 0 2774
while (c->seq < end && (max <= 0 || (IV)(SP - MARK) < max)) { 0 0 0
244 2774 const char *topic = NULL, *data = NULL;
245 2774 uint32_t tlen = 0, dlen = 0, flags = 0;
246 2774 uint64_t span = 1;
247 2774 int rc = sa_ring_read(c, c->seq, &topic, &tlen,
248 &data, &dlen, &flags, &span);
249 2774 ▶ if (rc == SA_READ_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SA_READ_PENDING) { 122 2652 2774
250 /* A hole. Wait on the QUESTION of whether its publisher is
251 * alive, not on a clock, and fill it if the answer is no. */
252 122 ▶ if (!sa_ring_tombstone(c, c->seq)) { stuck = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_ring_tombstone(c, c->seq)) { stuck = 1; break; } 121 1 122
253 1 rc = sa_ring_read(c, c->seq, &topic, &tlen,
254 &data, &dlen, &flags, &span);
255 1 ▶ if (rc != SA_READ_OK) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SA_READ_OK) break; 0 1 1
256 }
257 2664 ▶ if (rc == SA_READ_LAPPED) { c->lapped++; c->seq++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SA_READ_LAPPED) { c->lapped++; c->seq++; continue; } 30 2623 2653
258 2623 ▶ if (flags & SA_SF_CONT) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & SA_SF_CONT) { 10 2613 2623
259 /* A continuation reached on its own means its head was
260 * missed: a delivered record jumps the cursor past its own
261 * tail. Counted as lapped, because `lapped` is in SLOTS and
262 * a lost record cost every slot it spanned. */
263 10 c->lapped++;
264 10 c->seq++;
265 10 continue;
266 }
267 2613 ▶ if (flags & SA_SF_ABANDONED) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & SA_SF_ABANDONED) { 1 2612 2613
268 /* Counted apart from `lapped`, because they are different
269 * diagnoses: lapped means this reader is too slow or the
270 * ring too small, abandoned means a process died mid-write
271 * and somebody should go and look at why. */
272 1 c->abandoned++;
273 1 c->seq++;
274 1 continue;
275 }
276 {
277 /* [topic, payload, sequence]. The sequence is the record's
278 * identity: it is what a gap is counted in, and what two
279 * cursors compare when they disagree about what they saw. */
280 2612 AV *av = newAV();
281 2612 av_push(av, newSVpvn(topic, tlen));
282 2612 av_push(av, newSVpvn(data, dlen));
283 2612 av_push(av, newSVuv((UV)c->seq));
284 2612 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)av))); 0 2612 2612
285 }
286 2612 c->delivered++;
287 2612 c->seq += span;
288 }
289 /* Stuck on a hole nobody has been asked about yet: nap and look
290 * again, until the question is asked (alive: stop; dead: the
291 * hole fills and the loop above delivers past it), the cursor is
292 * inside the throttle after an answer, or the budget is spent. */
293 411 ▶ if (stuck && wait && c->hole_seq && c->hole_asks == asks0
Branch: 4 branches
BranchTrueFalseTotal
if (stuck && wait && c->hole_seq && c->hole_asks == asks0 121 290 411
if (stuck && wait && c->hole_seq && c->hole_asks == asks0 120 1 121
if (stuck && wait && c->hole_seq && c->hole_asks == asks0 120 0 120
if (stuck && wait && c->hole_seq && c->hole_asks == asks0 119 1 120
294 119 ▶ && (max <= 0 || (IV)(SP - MARK) < max)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (max <= 0 || (IV)(SP - MARK) < max)) { 0 119 119
&& (max <= 0 || (IV)(SP - MARK) < max)) { 0 0 0
295 119 uint64_t now = sa_now_us();
296 119 ▶ if (now - t0 < budget
Branch: 1 branch
BranchTrueFalseTotal
if (now - t0 < budget 119 0 119
297 119 ▶ && !(c->hole_next_us && now < c->hole_next_us)) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(c->hole_next_us && now < c->hole_next_us)) { 7 112 119
&& !(c->hole_next_us && now < c->hole_next_us)) { 0 7 7
298 112 sa_stall(1000);
299 112 goto again;
300 }
301 }
302 }
303  
304 # delivered / lapped / seq. `lapped` is the number this cursor LOST, and it is
305 # a per-cursor number on purpose: one slow reader being overtaken says nothing
306 # about any other.
307 void
308 sarc_stats(self)
309 SV *self
310 PREINIT:
311 sa_cursor *c;
312 PPCODE:
313 11 c = SA_SELF(sa_cursor, self);
314 11 ▶ if (!c) croak("Shared::Arena::Ring::Cursor: this cursor is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Ring::Cursor: this cursor is released"); 0 11 11
315 11 ▶ EXTEND(SP, 10);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 10); 0 11 11
316 11 mPUSHp("delivered", 9); mPUSHu((UV)c->delivered);
317 11 mPUSHp("lapped", 6); mPUSHu((UV)c->lapped);
318 11 mPUSHp("abandoned", 9); mPUSHu((UV)c->abandoned);
319 11 mPUSHp("unattributed", 12);mPUSHu((UV)c->unattributed);
320 11 mPUSHp("seq", 3); mPUSHu((UV)c->seq);
321  
322 void
323 sarc_DESTROY(self)
324 SV *self
325 PREINIT:
326 sa_cursor *c;
327 CODE:
328 28 c = SA_SELF(sa_cursor, self);
329 28 ▶ if (c) { sa_cursor_free(c); sv_setiv(SvRV(self), 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (c) { sa_cursor_free(c); sv_setiv(SvRV(self), 0); } 28 0 28
« xs/bloom.xs ↑ Summary xs/hll.xs »