include/hyperman/hm_sa_bus.h

filter: Statementstmt 98.7% 151 / 154 (1 uncoverable) Branchbran 78.5% 117 / 168 (24 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.2% 268 / 322 (25 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_sa_abuse.h ↑ Summary xs/hyperman.xs »
1 /* hm_sa_bus.h - the cross-worker message bus, on a Shared::Arena ring.
2 *
3 * One ring in the arena hm_sa.h creates, opened before the fork so every
4 * worker publishes into and reads from the same copy. Perl-free: the includer
5 * defines `HM_SA` and `hm_sa_region` (hm_sa.h in the server; the benchmark
6 * harness with no interpreter).
7 *
8 * A message is published once; the delivery mode is WHERE THE CURSOR LIVES. A
9 * fanout subscriber keeps its cursor in its own process, so every subscriber
10 * reads every message. A queue group keeps one cursor in the arena, so
11 * exactly one member gets each message, and the load balancing falls out of
12 * the claim: a busy worker is not in the drain loop and not claiming.
13 *
14 * ---- what this header keeps, and what the ring changes -----------------------
15 *
16 * Every contract the old ring gave is kept here, at the seam:
17 *
18 * - publish answers HM_BUS_OK / HM_BUS_LOCAL / HM_BUS_OVERSIZE, and an
19 * oversize message is REFUSED, never truncated. The ring would ACCEPT it -
20 * a record may span up to half the ring - so the ceiling is Hyperman's,
21 * checked BEFORE the ring sees the record: `bus_slot_size - 16`, the bytes
22 * the old slot header left, so a caller that sized its messages against
23 * the old bus sizes them the same against this one. The ring's own slot
24 * is 64 bytes wider than the configured one, so one message always fits
25 * ONE slot and never spans.
26 * - two fanout cursors per process, the dispatcher's and receive()'s, so two
27 * readers in one process never consume each other's records.
28 * - a group's identity is the (topic, name) PAIR. The ring's is the name
29 * alone with the topic bound at open, so the ring's group name is derived
30 * from both, and two subscriptions sharing a name across two topics get
31 * two groups, as they did.
32 * - a slot still being written is waited for, not counted lapped: the ring's
33 * claim reads before it claims and answers PENDING with the cursor
34 * unmoved, and the next dispatch retries. It never sleeps doing so.
35 * - queue groups are AT-MOST-ONCE. A member that claims and then dies loses
36 * the record; Punk::Queue is the durable one.
37 *
38 * Gaps are the cursor's lapped + abandoned: what this reader missed because
39 * the ring wrapped, plus the holes a dead publisher left that the ring filled
40 * and this reader stepped over. Both are counted, never silent.
41 *
42 * Everything FAILS OPEN. With no table, no arena or no ring, publish answers
43 * HM_BUS_LOCAL and the caller delivers to its own subscribers, which is the
44 * answer the abuse controls give with no arena.
45 */
46  
47 #ifndef HM_SA_BUS_H
48 #define HM_SA_BUS_H
49  
50 #include <string.h>
51 #include <stdio.h>
52 #include <stdint.h>
53 #include <stddef.h>
54  
55 #include "sa_abi.h"
56 #include "hm_atomic.h"
57  
58 #define HM_BUS_SLOTS 2048
59 #define HM_BUS_SLOT_SIZE 2048
60 #define HM_BUS_NAMELEN 64
61 #define HM_BUS_SUBS 64 /* registrations per process */
62 #define HM_SA_BUS_GROUPS 64 /* the ring's own table size */
63  
64 /* The ring slot is this much wider than the configured slot, and a message
65 * may use this much less than the configured slot: together they say one
66 * message is one slot. Two numbers, one comment, one test (t/35 oversize). */
67 #define HM_SA_BUS_SLOT_PAD 64
68 #define HM_SA_BUS_HDR 16
69  
70 /* publish outcomes */
71 #define HM_BUS_OK 0 /* on the ring, visible to the pool */
72 #define HM_BUS_LOCAL 1 /* no ring: the caller's own subscribers only */
73 #define HM_BUS_OVERSIZE (-1) /* refused; never truncated */
74  
75 typedef void (*hm_bus_cb)(void *ud, uint64_t seq,
76 const char *topic, uint32_t tlen,
77 const char *payload, uint32_t plen);
78  
79 static sa_ring *hm_sa_ring = NULL; /* NULL = fail open */
80 static uint32_t hm_sa_bus_max_msg = 0; /* topic + payload ceiling */
81 static sa_cursor *hm_sa_disp_cur = NULL; /* the dispatcher's fanout cursor */
82 static sa_cursor *hm_sa_recv_cur = NULL; /* receive()'s */
83  
84 140 static int hm_sa_bus_live(void) { return hm_sa_ring != NULL; }
85  
86 /* ---- lifecycle ------------------------------------------------------------- */
87  
88 /* Open the ring in the arena. MUST run before the fork. Idempotent; zero takes
89 * the default. 0, or -1 with *err set when the ring could not be carved, in
90 * which case the bus is local-only. */
91 101 static int hm_sa_bus_open(uint32_t slots, uint32_t slot_size, int *err) {
92 101 int e = 0;
93 /*
94 # uncoverable branch false class:ignore_covered_err
95 # uncoverable condition right count:1 class:ignore_covered_err */
96 101 ▶ if (err) *err = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = 0; 101 0 101
97 101 ▶ if (!HM_SA || !hm_sa_region || hm_sa_ring) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region || hm_sa_ring) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 98 3 101
if (!HM_SA || !hm_sa_region || hm_sa_ring) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 98 0 98
if (!HM_SA || !hm_sa_region || hm_sa_ring) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 14 84 98
98 84 ▶ if (!slots) slots = HM_BUS_SLOTS;
Branch: 1 branch
BranchTrueFalseTotal
if (!slots) slots = HM_BUS_SLOTS; 80 4 84
99 84 ▶ if (!slot_size) slot_size = HM_BUS_SLOT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if (!slot_size) slot_size = HM_BUS_SLOT_SIZE; 79 5 84
100 /*
101 # uncoverable branch true class:ignore_covered_err
102 # uncoverable condition left count:1 class:ignore_covered_err */
103 84 ▶ if (slot_size < HM_SA_BUS_HDR + 16) slot_size = HM_BUS_SLOT_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if (slot_size < HM_SA_BUS_HDR + 16) slot_size = HM_BUS_SLOT_SIZE; 0 84 84
104 84 hm_sa_ring = HM_SA->ring_open(hm_sa_region, "bus", 3, slots,
105 slot_size + HM_SA_BUS_SLOT_PAD, &e);
106 84 ▶ if (!hm_sa_ring) { if (err) *err = e; return -1; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!hm_sa_ring) { if (err) *err = e; return -1; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 82 84
if (!hm_sa_ring) { if (err) *err = e; return -1; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
107 82 hm_sa_bus_max_msg = slot_size - HM_SA_BUS_HDR;
108 82 return 0;
109 }
110  
111 /* ---- the wakeup ------------------------------------------------------------ */
112  
113 /* Make `n` wakers. MUST run before the fork: the descriptors are inherited,
114 * which is what lets their numbers live in the region. */
115 100 static int hm_sa_bus_wake_init(uint32_t n) {
116 100 ▶ if (!HM_SA || !hm_sa_region) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 97 3 100
if (!HM_SA || !hm_sa_region) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 97 97
117 97 return HM_SA->wake_init(hm_sa_region, n);
118 }
119  
120 /* Claim waker `idx` for this process, after the fork, and return its read
121 * end, or -1. The entry drains stale bytes and clears the flag itself. */
122 143 static int hm_sa_bus_waker_take(int idx) {
123 143 ▶ if (!HM_SA || !hm_sa_region) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region) return -1; 141 2 143
if (!HM_SA || !hm_sa_region) return -1; 1 140 141
124 140 ▶ if (HM_SA->wake_take(hm_sa_region, idx) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (HM_SA->wake_take(hm_sa_region, idx) < 0) return -1; 16 124 140
125 124 return HM_SA->wake_fd(hm_sa_region);
126 }
127  
128 184 static int hm_sa_bus_waker_fd(void) {
129 /*
130 # uncoverable branch true class:ignore_covered_err
131 # uncoverable condition left count:1 class:ignore_covered_err
132 # uncoverable condition left count:2 class:ignore_covered_err */
133 184 ▶ if (!HM_SA || !hm_sa_region) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region) return -1; 184 0 184
if (!HM_SA || !hm_sa_region) return -1; 0 184 184
134 184 return HM_SA->wake_fd(hm_sa_region);
135 }
136  
137 /* Empty the pipe, THEN clear the flag, and only then read the ring. The order
138 * is the whole correctness of the wakeup (the ring's wake_drained keeps it):
139 * clear first and a publisher can set the flag and write its byte while the
140 * read loop still runs, the byte is swallowed, and this worker goes deaf. */
141 6 static void hm_sa_bus_waker_drained(void) {
142 6 ▶ if (!HM_SA || !hm_sa_region) return; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 6 0 6
if (!HM_SA || !hm_sa_region) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 5 6
143 5 HM_SA->wake_drained(hm_sa_region);
144 }
145  
146 /* ---- publishing ------------------------------------------------------------ */
147  
148 21516 static int hm_sa_bus_publish(const char *topic, uint32_t tlen,
149 const char *payload, uint32_t plen) {
150 int64_t rc;
151 /*
152 # uncoverable branch true
153 # uncoverable condition left count:1 class:ignore_covered_err */
154 21516 ▶ if (!hm_sa_ring) return HM_BUS_LOCAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_ring) return HM_BUS_LOCAL; 0 21516 21516
155 21516 ▶ if ((uint64_t)tlen + plen > hm_sa_bus_max_msg) return HM_BUS_OVERSIZE;
Branch: 1 branch
BranchTrueFalseTotal
if ((uint64_t)tlen + plen > hm_sa_bus_max_msg) return HM_BUS_OVERSIZE; 3 21513 21516
156 21513 rc = HM_SA->publish(hm_sa_ring, topic, tlen, payload, plen);
157 /*
158 # uncoverable branch false
159 # uncoverable condition right count:1 class:ignore_covered_err */
160 21513 ▶ if (SA_PUBLISHED(rc)) return HM_BUS_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (SA_PUBLISHED(rc)) return HM_BUS_OK; 21513 0 21513
161 /*
162 # uncoverable statement
163 # uncoverable condition left count:1
164 # uncoverable condition right count:1 */
165 0 ▶ return rc == SA_TOO_BIG ? HM_BUS_OVERSIZE : HM_BUS_LOCAL;
Branch: 1 branch
BranchTrueFalseTotal
return rc == SA_TOO_BIG ? HM_BUS_OVERSIZE : HM_BUS_LOCAL; 0 0 0
166 }
167  
168 /* The next sequence to be published: what a cursor is reset TO. */
169 10 static uint64_t hm_sa_bus_seq(void) {
170 10 ▶ return hm_sa_ring ? HM_SA->ring_position(hm_sa_ring) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return hm_sa_ring ? HM_SA->ring_position(hm_sa_ring) : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 10 0 10
171 }
172  
173 /* Records published so far. Sequences start at one and a message is one
174 * slot, so the position less one is the count. */
175 5 static uint64_t hm_sa_bus_published(void) {
176 5 uint64_t p = hm_sa_bus_seq();
177 5 ▶ return p ? p - 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return p ? p - 1 : 0; /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
178 }
179  
180 /* ---- fanout cursors -------------------------------------------------------- */
181  
182 typedef struct { hm_bus_cb cb; void *ud; } hm_sa_bus_ctx;
183  
184 /* The ring hands a record with a flags word; the bus callback never had one. */
185 86 static void hm_sa_bus_rec(void *ud, uint64_t seq, const char *topic,
186 uint32_t tlen, const char *data, uint32_t dlen,
187 uint32_t flags) {
188 86 hm_sa_bus_ctx *x = (hm_sa_bus_ctx *)ud;
189 (void)flags;
190 /*
191 # uncoverable branch false class:ignore_covered_err
192 # uncoverable condition right count:1 class:ignore_covered_err */
193 86 ▶ if (x->cb) x->cb(x->ud, seq, topic, tlen, data, dlen);
Branch: 1 branch
BranchTrueFalseTotal
if (x->cb) x->cb(x->ud, seq, topic, tlen, data, dlen); 86 0 86
194 86 }
195  
196 /* A cursor, opened on first use. From the START of what the ring still holds,
197 * which is what a cursor born at sequence one did before: a worker resets it
198 * to "from now on" in its attach, a script that never resets replays. */
199 20 static sa_cursor *hm_sa_bus_cursor(sa_cursor **c) {
200 20 ▶ if (!*c && hm_sa_ring) *c = HM_SA->cursor_open(hm_sa_ring, 1); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!*c && hm_sa_ring) *c = HM_SA->cursor_open(hm_sa_ring, 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 17 20
if (!*c && hm_sa_ring) *c = HM_SA->cursor_open(hm_sa_ring, 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
201 20 return *c;
202 }
203  
204 /* Point a cursor at "from now on": release and reopen, which also zeroes its
205 * gaps, as the old reset did. */
206 262 static void hm_sa_bus_reset_cursor(sa_cursor **c) {
207 /*
208 # uncoverable branch true
209 # uncoverable condition left count:1 class:ignore_covered_err */
210 262 ▶ if (!hm_sa_ring) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_ring) return; 0 262 262
211 262 ▶ if (*c) HM_SA->cursor_release(*c);
Branch: 1 branch
BranchTrueFalseTotal
if (*c) HM_SA->cursor_release(*c); 7 255 262
212 262 *c = HM_SA->cursor_open(hm_sa_ring, 0);
213 }
214  
215 /* Both, because a caller saying "from now on" means the process, not one of
216 * its two halves; resetting only one leaves the other replaying history. */
217 131 static void hm_sa_bus_reset_cursors(void) {
218 131 hm_sa_bus_reset_cursor(&hm_sa_disp_cur);
219 131 hm_sa_bus_reset_cursor(&hm_sa_recv_cur);
220 131 }
221  
222 /* Everything this cursor has not seen. Stops at a record still being written
223 * rather than waiting; the ring's escalation never sleeps and a hole left by
224 * a dead publisher is filled and stepped over on a later turn. */
225 20 static long hm_sa_bus_drain(sa_cursor **c, hm_bus_cb cb, void *ud) {
226 hm_sa_bus_ctx x;
227 sa_cursor *cur;
228 /*
229 # uncoverable branch true
230 # uncoverable condition left count:1 class:ignore_covered_err */
231 20 ▶ if (!hm_sa_ring) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_ring) return 0; 0 20 20
232 20 cur = hm_sa_bus_cursor(c);
233 /*
234 # uncoverable branch true
235 # uncoverable condition left count:1 class:ignore_covered_err */
236 20 ▶ if (!cur) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cur) return 0; 0 20 20
237 20 x.cb = cb;
238 20 x.ud = ud;
239 20 return HM_SA->drain(cur, 0, hm_sa_bus_rec, &x);
240 }
241  
242 /* What this cursor MISSED: lapped by the ring, or a hole left by a dead
243 * publisher. Different diagnoses, one number the caller was promised. */
244 16 static uint64_t hm_sa_bus_gaps_of(sa_cursor *c) {
245 sa_counts n;
246 /*
247 # uncoverable branch true class:ignore_covered_err
248 # uncoverable condition left count:1 class:ignore_covered_err
249 # uncoverable condition left count:2 class:ignore_covered_err */
250 16 ▶ if (!c || !HM_SA) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!c || !HM_SA) return 0; 16 0 16
if (!c || !HM_SA) return 0; 0 16 16
251 16 HM_SA->counts(c, &n);
252 16 return n.lapped + n.abandoned;
253 }
254  
255 /* ---- queue groups ---------------------------------------------------------- */
256  
257 /* The ring's group name for a (topic, name) pair: 14 bytes of the name, a
258 * colon, and the topic's hash, 31 bytes at most. Two names sharing a prefix
259 * are told apart by the topic; two topics sharing a name get two groups. */
260 13 static uint32_t hm_sa_bus_gname(char out[32], const char *topic, uint32_t tlen,
261 const char *name, uint32_t nlen) {
262 char nm[15];
263 13 uint32_t n = nlen > 14 ? 14 : nlen;
264 13 memcpy(nm, name, n);
265 13 nm[n] = '\0';
266 13 return (uint32_t)snprintf(out, 32, "%s:%016llx", nm,
267 13 (unsigned long long)hm_at_fnv(topic, tlen));
268 }
269  
270 /* The group handles this process holds, one per (topic, name) it has used.
271 * A handle is process-local; the group it names is in the ring. */
272 typedef struct {
273 int used;
274 uint32_t glen;
275 char gname[32];
276 sa_group_h *gh;
277 } hm_sa_bus_grp;
278  
279 static hm_sa_bus_grp hm_sa_bus_grps[HM_SA_BUS_GROUPS];
280  
281 /* Find or open the group for (topic, name). NULL with no ring, a bad name, or
282 * a full table. A new group starts at the CURRENT sequence: joining means
283 * "from now on", and replaying the ring to a worker that just started is a
284 * surprise nobody asked for. */
285 13 static sa_group_h *hm_sa_bus_group(const char *topic, uint32_t tlen,
286 const char *name, uint32_t nlen) {
287 char g[32];
288 uint32_t gl;
289 13 int i, free_at = -1, err = 0;
290 sa_group_h *gh;
291  
292 /*
293 # uncoverable branch true class:ignore_covered_err
294 # uncoverable condition left count:1 class:ignore_covered_err
295 # uncoverable condition left count:2 class:ignore_covered_err
296 # uncoverable condition left count:3 class:ignore_covered_err
297 # uncoverable condition left count:4 class:ignore_covered_err */
298 13 ▶ if (!hm_sa_ring || !name || !nlen || nlen >= HM_BUS_NAMELEN) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!hm_sa_ring || !name || !nlen || nlen >= HM_BUS_NAMELEN) return NULL; 13 0 13
if (!hm_sa_ring || !name || !nlen || nlen >= HM_BUS_NAMELEN) return NULL; 13 0 13
if (!hm_sa_ring || !name || !nlen || nlen >= HM_BUS_NAMELEN) return NULL; 13 0 13
if (!hm_sa_ring || !name || !nlen || nlen >= HM_BUS_NAMELEN) return NULL; 0 13 13
299 /*
300 # uncoverable branch true class:ignore_covered_err
301 # uncoverable condition left count:1 class:ignore_covered_err
302 # uncoverable condition left count:2 class:ignore_covered_err
303 # uncoverable condition left count:3 class:ignore_covered_err */
304 13 ▶ if (!topic || !tlen || tlen >= HM_BUS_NAMELEN) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN) return NULL; 13 0 13
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN) return NULL; 13 0 13
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN) return NULL; 0 13 13
305 13 gl = hm_sa_bus_gname(g, topic, tlen, name, nlen);
306  
307 469 ▶ for (i = 0; i < HM_SA_BUS_GROUPS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < HM_SA_BUS_GROUPS; i++) { 462 7 469
308 462 hm_sa_bus_grp *e = &hm_sa_bus_grps[i];
309 462 ▶ if (!e->used) { if (free_at < 0) free_at = i; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!e->used) { if (free_at < 0) free_at = i; continue; } 441 21 462
if (!e->used) { if (free_at < 0) free_at = i; continue; } 7 434 441
310 21 ▶ if (e->glen == gl && memcmp(e->gname, g, gl) == 0) return e->gh;
Branch: 2 branches
BranchTrueFalseTotal
if (e->glen == gl && memcmp(e->gname, g, gl) == 0) return e->gh; 13 8 21
if (e->glen == gl && memcmp(e->gname, g, gl) == 0) return e->gh; 6 7 13
311 }
312 /*
313 # uncoverable branch true
314 # uncoverable condition left count:1 class:ignore_covered_err */
315 7 ▶ if (free_at < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (free_at < 0) return NULL; 0 7 7
316 7 gh = HM_SA->group_open(hm_sa_ring, g, gl, topic, tlen, 0, &err);
317 /*
318 # uncoverable branch true
319 # uncoverable condition left count:1 class:ignore_covered_err */
320 7 ▶ if (!gh) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!gh) return NULL; 0 7 7
321 7 hm_sa_bus_grps[free_at].used = 1;
322 7 hm_sa_bus_grps[free_at].glen = gl;
323 7 memcpy(hm_sa_bus_grps[free_at].gname, g, gl);
324 7 hm_sa_bus_grps[free_at].gh = gh;
325 7 return gh;
326 }
327  
328 /* Claim and handle whatever this group has that nobody else has taken; how
329 * many this caller handled. The ring's claim reads BEFORE it claims, so a
330 * record still being written comes back PENDING with the cursor unmoved and
331 * is picked up next time, never counted lost. Records on other topics are
332 * stepped over by the ring and never handed here. */
333 12 static long hm_sa_bus_claim(sa_group_h *gh, hm_bus_cb cb, void *ud) {
334 12 long n = 0;
335 12 uint64_t seq = 0;
336 12 const char *tp = NULL, *dp = NULL;
337 12 uint32_t tl = 0, dl = 0, fl = 0;
338 /*
339 # uncoverable branch true class:ignore_covered_err
340 # uncoverable condition left count:1 class:ignore_covered_err
341 # uncoverable condition left count:2 class:ignore_covered_err */
342 12 ▶ if (!gh || !HM_SA) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!gh || !HM_SA) return 0; 12 0 12
if (!gh || !HM_SA) return 0; 0 12 12
343 33 ▶ while (HM_SA->group_claim(gh, &seq, &tp, &tl, &dp, &dl, &fl) == SA_READ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
while (HM_SA->group_claim(gh, &seq, &tp, &tl, &dp, &dl, &fl) == SA_READ_OK) { 21 12 33
344 /*
345 # uncoverable branch false class:ignore_covered_err
346 # uncoverable condition right count:1 class:ignore_covered_err */
347 21 ▶ if (cb) cb(ud, seq, tp, tl, dp, dl);
Branch: 1 branch
BranchTrueFalseTotal
if (cb) cb(ud, seq, tp, tl, dp, dl); 21 0 21
348 21 n++;
349 }
350 12 return n;
351 }
352  
353 /* Records the GROUP lost before any member claimed them: the loss belongs to
354 * the group, not to whichever member noticed. */
355 3 static uint64_t hm_sa_bus_group_gaps(sa_group_h *gh) {
356 sa_group_counts c;
357 /*
358 # uncoverable branch true class:ignore_covered_err
359 # uncoverable condition left count:1 class:ignore_covered_err
360 # uncoverable condition left count:2 class:ignore_covered_err */
361 3 ▶ if (!gh || !HM_SA) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!gh || !HM_SA) return 0; 3 0 3
if (!gh || !HM_SA) return 0; 0 3 3
362 3 HM_SA->group_counts(gh, &c);
363 3 return c.lapped;
364 }
365  
366 /* ---- subscribers ----------------------------------------------------------- */
367 /*
368 * PROCESS-LOCAL: a registration is a callback and a callback cannot cross a
369 * fork. The shared thing is the ring, not the list of people reading it. A
370 * subscription is FANOUT (group empty) or a QUEUE GROUP; one registration
371 * function, because they are one mechanism. The table keeps the old name
372 * and shape, so hm_abi_impl.h and the Perl side alias `ud` as they did.
373 */
374 typedef struct {
375 int used;
376 hm_bus_cb cb;
377 void *ud;
378 uint32_t tlen, glen;
379 sa_group_h *gh; /* the group, or NULL for fanout */
380 char topic[HM_BUS_NAMELEN];
381 char group[HM_BUS_NAMELEN];
382 } hm_bus_sub;
383  
384 static hm_bus_sub hm_bus_subs[HM_BUS_SUBS];
385 static int hm_bus_nsubs = 0;
386  
387 /* Register. An id, or -1 when the table is full. `group` NULL or empty is
388 * fanout. A group is resolved NOW when there is a ring, so it starts where the
389 * caller subscribed, and lazily at first dispatch when there is not yet -
390 * subscriptions belong at boot, in the parent, before run() opens the ring,
391 * and nothing can have been published before the ring existed. */
392 12 static int hm_sa_bus_subscribe(const char *topic, uint32_t tlen,
393 const char *group, uint32_t glen,
394 hm_bus_cb cb, void *ud) {
395 int i;
396 /*
397 # uncoverable branch true class:ignore_covered_err
398 # uncoverable condition left count:1 class:ignore_covered_err
399 # uncoverable condition left count:2 class:ignore_covered_err
400 # uncoverable condition left count:3 class:ignore_covered_err
401 # uncoverable condition left count:4 class:ignore_covered_err */
402 12 ▶ if (!topic || !tlen || tlen >= HM_BUS_NAMELEN || !cb) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN || !cb) return -1; 12 0 12
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN || !cb) return -1; 12 0 12
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN || !cb) return -1; 12 0 12
if (!topic || !tlen || tlen >= HM_BUS_NAMELEN || !cb) return -1; 0 12 12
403 /*
404 # uncoverable branch true class:ignore_covered_err
405 # uncoverable condition left count:1 class:ignore_covered_err
406 # uncoverable condition left count:2 class:ignore_covered_err */
407 12 ▶ if (group && glen >= HM_BUS_NAMELEN) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (group && glen >= HM_BUS_NAMELEN) return -1; 3 9 12
if (group && glen >= HM_BUS_NAMELEN) return -1; 0 3 3
408 /*
409 # uncoverable branch false
410 # uncoverable condition right count:1 class:ignore_covered_err */
411 15 ▶ for (i = 0; i < HM_BUS_SUBS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < HM_BUS_SUBS; i++) { 15 0 15
412 15 hm_bus_sub *sb = &hm_bus_subs[i];
413 15 ▶ if (sb->used) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (sb->used) continue; 3 12 15
414 12 memset(sb, 0, sizeof(*sb));
415 12 memcpy(sb->topic, topic, tlen);
416 12 sb->tlen = tlen;
417 12 ▶ if (group && glen) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (group && glen) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 9 12
if (group && glen) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
418 3 memcpy(sb->group, group, glen);
419 3 sb->glen = glen;
420 3 ▶ sb->gh = hm_sa_ring ? hm_sa_bus_group(topic, tlen, group, glen) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
sb->gh = hm_sa_ring ? hm_sa_bus_group(topic, tlen, group, glen) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
421 }
422 12 sb->cb = cb;
423 12 sb->ud = ud;
424 12 sb->used = 1;
425 12 ▶ if (i >= hm_bus_nsubs) hm_bus_nsubs = i + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= hm_bus_nsubs) hm_bus_nsubs = i + 1; 7 5 12
426 12 return i;
427 }
428 0 return -1; /* # uncoverable statement */
429 }
430  
431 9 static int hm_sa_bus_unsubscribe(int id) {
432 /*
433 # uncoverable branch true class:ignore_covered_err
434 # uncoverable condition left count:1 class:ignore_covered_err
435 # uncoverable condition left count:2 class:ignore_covered_err
436 # uncoverable condition left count:3 class:ignore_covered_err */
437 9 ▶ if (id < 0 || id >= HM_BUS_SUBS || !hm_bus_subs[id].used) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (id < 0 || id >= HM_BUS_SUBS || !hm_bus_subs[id].used) return 0; 9 0 9
if (id < 0 || id >= HM_BUS_SUBS || !hm_bus_subs[id].used) return 0; 9 0 9
if (id < 0 || id >= HM_BUS_SUBS || !hm_bus_subs[id].used) return 0; 0 9 9
438 9 hm_bus_subs[id].used = 0;
439 9 return 1;
440 }
441  
442 /* Counted here rather than taken from the drain's return, because they answer
443 * different questions: the drain says how many records it READ, a caller
444 * asking what dispatch did means how many reached a subscriber. */
445 static long hm_bus_delivered = 0;
446  
447 20 static void hm_sa_bus_fan_cb(void *ud, uint64_t seq, const char *topic,
448 uint32_t tlen, const char *payload, uint32_t plen) {
449 int i;
450 (void)ud;
451 50 ▶ for (i = 0; i < hm_bus_nsubs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_bus_nsubs; i++) { 30 20 50
452 30 hm_bus_sub *sb = &hm_bus_subs[i];
453 30 ▶ if (!sb->used || sb->glen) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!sb->used || sb->glen) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ 30 0 30
if (!sb->used || sb->glen) continue; /* # uncoverable condition left count:1 class:ignore_covered_err */ 5 25 30
454 25 ▶ if (sb->tlen != tlen || memcmp(sb->topic, topic, tlen)) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (sb->tlen != tlen || memcmp(sb->topic, topic, tlen)) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 16 9 25
if (sb->tlen != tlen || memcmp(sb->topic, topic, tlen)) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 16 16
455 16 sb->cb(sb->ud, seq, topic, tlen, payload, plen);
456 16 hm_bus_delivered++;
457 }
458 20 }
459  
460 15 static void hm_sa_bus_grp_cb(void *ud, uint64_t seq, const char *topic,
461 uint32_t tlen, const char *payload, uint32_t plen) {
462 15 hm_bus_sub *sb = (hm_bus_sub *)ud;
463 15 sb->cb(sb->ud, seq, topic, tlen, payload, plen);
464 15 hm_bus_delivered++;
465 15 }
466  
467 /* Everything waiting, to everybody registered. Called from the wakeup, and
468 * safe to call at any other time - it is how a caller with no loop polls.
469 * Fanout first, in ONE pass over the ring however many subscribers there are;
470 * then each group subscriber claims until the group is empty. */
471 202 static long hm_sa_bus_dispatch(void) {
472 202 int i, any_fan = 0;
473  
474 /*
475 # uncoverable branch true
476 # uncoverable condition left count:1 class:ignore_covered_err */
477 202 ▶ if (!hm_sa_ring) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_ring) return 0; 0 202 202
478 202 hm_bus_delivered = 0;
479 207 ▶ for (i = 0; i < hm_bus_nsubs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_bus_nsubs; i++) 19 188 207
480 19 ▶ if (hm_bus_subs[i].used && !hm_bus_subs[i].glen) { any_fan = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (hm_bus_subs[i].used && !hm_bus_subs[i].glen) { any_fan = 1; break; } 18 1 19
if (hm_bus_subs[i].used && !hm_bus_subs[i].glen) { any_fan = 1; break; } 14 4 18
481  
482 202 ▶ if (any_fan)
Branch: 1 branch
BranchTrueFalseTotal
if (any_fan) 14 188 202
483 14 (void)hm_sa_bus_drain(&hm_sa_disp_cur, hm_sa_bus_fan_cb, NULL);
484 188 ▶ else if (hm_sa_disp_cur) {
Branch: 1 branch
BranchTrueFalseTotal
else if (hm_sa_disp_cur) { 125 63 188
485 /* Nobody is fanning out: drop the cursor rather than let it fall a
486 * ring behind, so the next fanout subscriber starts from now. */
487 125 HM_SA->cursor_release(hm_sa_disp_cur);
488 125 hm_sa_disp_cur = NULL;
489 }
490  
491 224 ▶ for (i = 0; i < hm_bus_nsubs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hm_bus_nsubs; i++) { 22 202 224
492 22 hm_bus_sub *sb = &hm_bus_subs[i];
493 22 ▶ if (!sb->used || !sb->glen) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!sb->used || !sb->glen) continue; 20 2 22
if (!sb->used || !sb->glen) continue; 15 5 20
494 /*
495 # uncoverable branch true count:1 class:ignore_covered_err
496 # uncoverable branch true count:2 class:ignore_covered_err
497 # uncoverable branch false count:3 class:ignore_covered_err
498 # uncoverable condition left count:1 class:ignore_covered_err */
499 5 ▶ if (!sb->gh) sb->gh = hm_sa_bus_group(sb->topic, sb->tlen,
Branch: 1 branch
BranchTrueFalseTotal
if (!sb->gh) sb->gh = hm_sa_bus_group(sb->topic, sb->tlen, 0 5 5
500 0 sb->group, sb->glen); /* # uncoverable statement */
501 /*
502 # uncoverable branch true
503 # uncoverable condition left count:1 class:ignore_covered_err */
504 5 ▶ if (!sb->gh) continue; /* the group table is full */
Branch: 1 branch
BranchTrueFalseTotal
if (!sb->gh) continue; /* the group table is full */ 0 5 5
505 5 (void)hm_sa_bus_claim(sb->gh, hm_sa_bus_grp_cb, sb);
506 }
507 202 return hm_bus_delivered;
508 }
509  
510 #endif /* HM_SA_BUS_H */
« include/hyperman/hm_sa_abuse.h ↑ Summary xs/hyperman.xs »