include/sa/sa_scoreboard.h

filter: Statementstmt 93.2% 138 / 148 Branchbran 57.6% 83 / 144 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.6% 221 / 292 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/sa/sa_hll.h ↑ Summary include/sa/sa_hist.h »
1 #ifndef SA_SCOREBOARD_H
2 #define SA_SCOREBOARD_H
3  
4 /* sa_scoreboard.h - one row per worker, published live. Perl-free.
5 *
6 * ---- the contention model, inverted -----------------------------------------
7 *
8 * Every other shared table here has many writers to one structure, and pays a
9 * stripe lock to keep them from tearing each other's writes. A scoreboard is
10 * the opposite: each worker owns ONE row and is the only writer to it, and a
11 * reader - a status endpoint, a supervisor - reads every row in one pass.
12 *
13 * So there is no write contention at all. A worker updating its own row takes
14 * no lock, because nobody else writes that row; the only lock in the whole
15 * tenant is the one taken once to CLAIM a row, which happens at worker start.
16 *
17 * This is Apache's scoreboard, for a fork-shared pool: a live per-worker view -
18 * in-flight requests, total served, bytes, what each worker is doing right now -
19 * that the supervisor reads directly out of memory instead of collecting over a
20 * pipe or a stats socket per worker.
21 *
22 * ---- a coherent snapshot, with a seqlock and no lock -------------------------
23 *
24 * A reader wants a row's fields together: the in-flight count and the status
25 * line as they were at one instant, not the count from before an update and the
26 * string from after. The single writer bumps a version odd before it touches
27 * the row and even after, and the reader reads the version either side and
28 * retries if it moved - the ring's two-word discipline, but with exactly one
29 * writer so no stripe lock is needed.
30 *
31 * ---- a dead worker's row is reclaimed, not leaked ---------------------------
32 *
33 * A row records its owner's pid. A worker that dies leaves its row occupied and
34 * stale; the next worker that starts and needs a row reclaims one whose owner is
35 * gone, resets it, and takes it over. A reader shows a dead worker's row as not
36 * alive rather than as current, and a worker that died MID-UPDATE - version
37 * stuck odd - is read as stale after a bounded retry rather than spun on for
38 * ever. The bound is the same reasoning as everywhere: a writer holds a row odd
39 * for a few stores, so any longer means it died holding it.
40 *
41 * ---- the schema ------------------------------------------------------------
42 *
43 * The gauge fields are named once, by whoever creates the board, so every row
44 * has the same columns and a reader gets them back by name. A later attacher
45 * either names the same fields or inherits them; a different set is refused, the
46 * way every tenant refuses a shape it does not share.
47 *
48 * Needs sa_arena.h and sa_peer.h (for sa_pid_alive / sa_getpid).
49 */
50  
51 #include "sa/sa_arena.h"
52 #include "sa/sa_peer.h"
53  
54 #define SA_SB_MAGIC 0x44524253u /* 'S','B','R','D' little-endian */
55  
56 #define SA_SB_SLOTS_MAX 4096 /* workers a board can hold */
57 #define SA_SB_FIELDS 8 /* named gauges per row */
58 #define SA_SB_NAME 16 /* a field name, including the NUL */
59 #define SA_SB_STATUS 64 /* the free-text status line */
60 #define SA_SB_RETRY 1000 /* seqlock retries before "stale" */
61  
62 #define SA_SB_EMPTY 0u
63 #define SA_SB_LIVE 1u
64  
65 typedef struct {
66 volatile uint32_t state; /* SA_SB_EMPTY / SA_SB_LIVE */
67 volatile uint32_t version; /* odd while the owner is writing */
68 volatile uint64_t pid; /* the owner */
69 volatile uint64_t epoch; /* bumped on every (re)claim: an identity */
70 volatile uint64_t updated; /* ms wall clock of the last write */
71 volatile uint64_t gauges[SA_SB_FIELDS];
72 volatile uint32_t statuslen;
73 char status[SA_SB_STATUS];
74 } sa_sb_slot;
75  
76 typedef struct {
77 volatile uint32_t magic; /* published LAST at init */
78 uint32_t nslots;
79 uint32_t nfields;
80 uint32_t pad;
81 uint64_t slots_off; /* from the BOARD's base */
82 char fields[SA_SB_FIELDS][SA_SB_NAME]; /* the schema */
83 volatile unsigned char locks[SA_LOCK_STRIPES]; /* claim only */
84 } sa_sb_hdr;
85  
86 #ifndef SA_SB_FWD
87 #define SA_SB_FWD
88 typedef struct sa_sb sa_sb;
89 #endif
90  
91 /* The process-local handle. `mine` is the index of the row this process
92 * claimed, or -1; `epoch` is the identity of that claim, so a stale handle
93 * cannot write a row a later worker now owns. */
94 struct sa_sb {
95 sa_region *arena;
96 sa_sb_hdr *hdr;
97 sa_sb_slot *slots;
98 uint32_t nslots;
99 uint32_t nfields;
100 int mine;
101 uint64_t epoch;
102 };
103  
104 /* One row read out coherently, for a reader. Guarded so the identical struct in
105 * sa_abi.h (which a C consumer sees WITHOUT this header) is not defined twice;
106 * SA_SB_FIELDS is 8 and SA_SB_STATUS 64, matching the literals there. */
107 #ifndef SA_SB_READING_DEFINED
108 #define SA_SB_READING_DEFINED
109 typedef struct sa_sb_reading {
110 uint64_t pid;
111 uint64_t epoch;
112 uint64_t updated;
113 int alive; /* is the owner still running */
114 uint64_t gauges[SA_SB_FIELDS];
115 uint32_t statuslen;
116 char status[SA_SB_STATUS];
117 } sa_sb_reading;
118 #endif
119  
120 105 static uint64_t sa_sb_bytes(uint32_t nslots) {
121 105 return sa_align_up((uint64_t)sizeof(sa_sb_hdr))
122 105 + sa_align_up((uint64_t)nslots * (uint64_t)sizeof(sa_sb_slot));
123 }
124  
125 #define SA_SB_SLOTS(h) \
126 ((sa_sb_slot *)((char *)(h) + (size_t)(h)->slots_off))
127  
128 #if SA_HAVE_ATOMICS
129  
130 /* The index of a named field, or -1. A small linear scan the writer does once
131 * per update; a caller that cares caches it. */
132 1110655 static int sa_sb_field(const sa_sb *b, const char *name, uint32_t nlen) {
133 uint32_t i;
134 1110655 ▶ if (!name || !nlen || nlen >= SA_SB_NAME) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!name || !nlen || nlen >= SA_SB_NAME) return -1; 1110655 0 1110655
if (!name || !nlen || nlen >= SA_SB_NAME) return -1; 1110655 0 1110655
if (!name || !nlen || nlen >= SA_SB_NAME) return -1; 0 1110655 1110655
135 1110694 ▶ for (i = 0; i < b->nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nfields; i++) { 1110684 10 1110694
136 1110684 ▶ if (strncmp(b->hdr->fields[i], name, nlen) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(b->hdr->fields[i], name, nlen) == 0 1110645 39 1110684
137 1110645 ▶ && b->hdr->fields[i][nlen] == '\0')
Branch: 1 branch
BranchTrueFalseTotal
&& b->hdr->fields[i][nlen] == '\0') 1110645 0 1110645
138 1110645 return (int)i;
139 }
140 10 return -1;
141 }
142  
143 61 static sa_sb *sa_sb_bind(sa_region *arena, sa_reg *e,
144 const char *const *fields, uint32_t nfields, int *err)
145 {
146 sa_sb *b;
147 sa_sb_hdr *h;
148 uint64_t hash;
149  
150 61 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 61 0 61
151 61 ▶ if (!arena || !e) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 3 branches
BranchTrueFalseTotal
if (!arena || !e) { if (err) *err = SA_E_NOENT; return NULL; } 61 0 61
if (!arena || !e) { if (err) *err = SA_E_NOENT; return NULL; } 0 61 61
if (!arena || !e) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
152 61 ▶ if (nfields > SA_SB_FIELDS) { if (err) *err = SA_E_NAME; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (nfields > SA_SB_FIELDS) { if (err) *err = SA_E_NAME; return NULL; } 0 61 61
if (nfields > SA_SB_FIELDS) { if (err) *err = SA_E_NAME; return NULL; } 0 0 0
153  
154 61 h = (sa_sb_hdr *)sa_ptr_len(arena, e->off, sizeof(sa_sb_hdr));
155 61 ▶ if (!h) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!h) { if (err) *err = SA_E_NOENT; return NULL; } 0 61 61
if (!h) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
156  
157 61 ▶ if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) { 35 26 61
158 35 hash = sa_at_fnv(e->name, strlen(e->name));
159 35 ▶ if (sa_at_lock(h->locks, hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_lock(h->locks, hash)) { 35 0 35
160 35 ▶ if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) { 35 0 35
161 35 uint32_t nsl = (uint32_t)((e->len - sizeof(sa_sb_hdr))
162 35 / sizeof(sa_sb_slot));
163 uint32_t i;
164 35 ▶ if (nsl < 1 || sa_sb_bytes(nsl) > e->len) {
Branch: 2 branches
BranchTrueFalseTotal
if (nsl < 1 || sa_sb_bytes(nsl) > e->len) { 35 0 35
if (nsl < 1 || sa_sb_bytes(nsl) > e->len) { 0 35 35
165 0 sa_at_unlock(h->locks, hash);
166 0 ▶ if (err) *err = SA_E_FULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_FULL; 0 0 0
167 0 return NULL;
168 }
169 35 ▶ if (nsl > SA_SB_SLOTS_MAX) nsl = SA_SB_SLOTS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (nsl > SA_SB_SLOTS_MAX) nsl = SA_SB_SLOTS_MAX; 0 35 35
170 35 memset((void *)h, 0, (size_t)sa_sb_bytes(nsl));
171 35 h->nslots = nsl;
172 35 h->nfields = nfields;
173 35 h->slots_off = sa_align_up((uint64_t)sizeof(sa_sb_hdr));
174 89 ▶ for (i = 0; i < nfields && fields; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nfields && fields; i++) { 54 35 89
for (i = 0; i < nfields && fields; i++) { 54 0 54
175 54 size_t l = strlen(fields[i]);
176 54 ▶ if (l >= SA_SB_NAME) l = SA_SB_NAME - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (l >= SA_SB_NAME) l = SA_SB_NAME - 1; 0 54 54
177 54 memcpy(h->fields[i], fields[i], l);
178 }
179 35 sa_at_store32_rel(&h->magic, SA_SB_MAGIC);
180 }
181 35 sa_at_unlock(h->locks, hash);
182 }
183 35 ▶ if (!sa_wait_magic32(&h->magic, SA_SB_MAGIC)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_wait_magic32(&h->magic, SA_SB_MAGIC)) { 0 35 35
184 0 ▶ if (err) *err = SA_E_MAGIC;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_MAGIC; 0 0 0
185 0 return NULL;
186 }
187 }
188  
189 /* A caller that named fields must name the SAME ones, for the reason every
190 * tenant checks its shape: two processes disagreeing about the columns is
191 * worse than either failing. A caller that named none inherits them. */
192 61 ▶ if (nfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (nfields) { 44 17 61
193 uint32_t i;
194 44 ▶ if (h->nfields != nfields) { if (err) *err = SA_E_SHAPE; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (h->nfields != nfields) { if (err) *err = SA_E_SHAPE; return NULL; } 0 44 44
if (h->nfields != nfields) { if (err) *err = SA_E_SHAPE; return NULL; } 0 0 0
195 107 ▶ for (i = 0; i < nfields && fields; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nfields && fields; i++) { 72 35 107
for (i = 0; i < nfields && fields; i++) { 72 0 72
196 72 ▶ if (strncmp(h->fields[i], fields[i], SA_SB_NAME) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(h->fields[i], fields[i], SA_SB_NAME) != 0) { 9 63 72
197 9 ▶ if (err) *err = SA_E_SHAPE;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_SHAPE; 9 0 9
198 9 return NULL;
199 }
200 }
201 }
202  
203 52 b = (sa_sb *)calloc(1, sizeof(sa_sb));
204 52 ▶ if (!b) { if (err) *err = SA_E_NOMEM; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!b) { if (err) *err = SA_E_NOMEM; return NULL; } 0 52 52
if (!b) { if (err) *err = SA_E_NOMEM; return NULL; } 0 0 0
205 52 b->arena = arena;
206 52 b->hdr = h;
207 52 b->slots = SA_SB_SLOTS(h);
208 52 b->nslots = h->nslots;
209 52 b->nfields = h->nfields;
210 52 b->mine = -1;
211 52 b->epoch = 0;
212 52 return b;
213 }
214  
215 52 static void sa_sb_free(sa_sb *b) { free(b); }
216  
217 /* Claim a row for this process, reclaiming a dead worker's row when the board
218 * is full. Returns the row index, or -1 when every row belongs to a live
219 * worker. Idempotent: called again in the same process it returns the row it
220 * already holds.
221 *
222 * The stripe lock serialises claimers - the ONE place this tenant locks - so a
223 * row is never handed to two workers at once. */
224 18 static int sa_sb_take(sa_sb *b) {
225 sa_sb_hdr *h;
226 uint64_t me, hash;
227 uint32_t i;
228 18 int dead = -1;
229  
230 18 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 18 18
231 18 ▶ if (b->mine >= 0) return b->mine;
Branch: 1 branch
BranchTrueFalseTotal
if (b->mine >= 0) return b->mine; 0 18 18
232 18 h = b->hdr;
233 18 me = sa_getpid();
234 18 hash = sa_at_fnv("\0scoreboard-claim", 17);
235  
236 18 ▶ if (!sa_at_lock(h->locks, hash)) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_at_lock(h->locks, hash)) return -1; 0 18 18
237  
238 /* Already ours from an earlier take in this same process? */
239 2416 ▶ for (i = 0; i < b->nslots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nslots; i++) { 2398 18 2416
240 2398 sa_sb_slot *s = &b->slots[i];
241 2398 ▶ if (sa_at_load32_acq(&s->state) == SA_SB_LIVE
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&s->state) == SA_SB_LIVE 11 2387 2398
242 11 ▶ && sa_at_load64_acq(&s->pid) == me) {
Branch: 1 branch
BranchTrueFalseTotal
&& sa_at_load64_acq(&s->pid) == me) { 0 11 11
243 0 b->mine = (int)i;
244 0 b->epoch = sa_at_load64_acq(&s->epoch);
245 0 sa_at_unlock(h->locks, hash);
246 0 return b->mine;
247 }
248 }
249 /* First empty row; remember the first dead-owner row as a fallback. */
250 29 ▶ for (i = 0; i < b->nslots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nslots; i++) { 28 1 29
251 28 sa_sb_slot *s = &b->slots[i];
252 28 uint32_t st = sa_at_load32_acq(&s->state);
253 28 ▶ if (st == SA_SB_EMPTY) { dead = (int)i; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (st == SA_SB_EMPTY) { dead = (int)i; break; } 17 11 28
254 11 ▶ if (dead < 0 && !sa_pid_alive(sa_at_load64_acq(&s->pid)))
Branch: 2 branches
BranchTrueFalseTotal
if (dead < 0 && !sa_pid_alive(sa_at_load64_acq(&s->pid))) 11 0 11
if (dead < 0 && !sa_pid_alive(sa_at_load64_acq(&s->pid))) 1 10 11
255 1 dead = (int)i;
256 }
257 18 ▶ if (dead < 0) { sa_at_unlock(h->locks, hash); return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (dead < 0) { sa_at_unlock(h->locks, hash); return -1; } 0 18 18
258 {
259 18 sa_sb_slot *s = &b->slots[dead];
260 18 uint64_t ep = sa_at_load64_acq(&s->epoch) + 1;
261 uint32_t f;
262 18 sa_at_store32_rel(&s->version, sa_at_load32_acq(&s->version) | 1u);
263 18 sa_at_fence_rel();
264 18 sa_at_store64_rel(&s->pid, me);
265 18 sa_at_store64_rel(&s->epoch, ep);
266 18 sa_at_store64_rel(&s->updated, sa_now_ms());
267 162 ▶ for (f = 0; f < SA_SB_FIELDS; f++) sa_at_store64_rel(&s->gauges[f], 0);
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < SA_SB_FIELDS; f++) sa_at_store64_rel(&s->gauges[f], 0); 144 18 162
268 18 s->statuslen = 0;
269 18 s->status[0] = '\0';
270 18 sa_at_fence_rel();
271 18 sa_at_store32_rel(&s->version, (sa_at_load32_acq(&s->version) + 1u));
272 18 sa_at_store32_rel(&s->state, SA_SB_LIVE);
273 18 b->mine = dead;
274 18 b->epoch = ep;
275 }
276 18 sa_at_unlock(h->locks, hash);
277 18 return b->mine;
278 }
279  
280 /* Open a write to our own row: version odd, and a barrier so the stores below
281 * cannot be seen before the version moves. Returns our row, or NULL if we hold
282 * none or a later worker has taken ours over (a stale handle must not write).
283 * The caller pairs it with sa_sb_end. Being the only writer to this row, no
284 * lock is taken. */
285 1110645 static sa_sb_slot *sa_sb_begin(sa_sb *b) {
286 sa_sb_slot *s;
287 1110645 ▶ if (!b || b->mine < 0) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || b->mine < 0) return NULL; 1110645 0 1110645
if (!b || b->mine < 0) return NULL; 0 1110645 1110645
288 1110645 s = &b->slots[b->mine];
289 1110645 ▶ if (sa_at_load64_acq(&s->pid) != sa_getpid()
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load64_acq(&s->pid) != sa_getpid() 1110645 0 1110645
290 1110645 ▶ || sa_at_load64_acq(&s->epoch) != b->epoch)
Branch: 1 branch
BranchTrueFalseTotal
|| sa_at_load64_acq(&s->epoch) != b->epoch) 0 1110645 1110645
291 0 return NULL; /* our row was reclaimed: do not write it */
292 1110645 sa_at_store32_rel(&s->version, sa_at_load32_acq(&s->version) | 1u);
293 1110645 sa_at_fence_rel();
294 1110645 return s;
295 }
296  
297 1110645 static void sa_sb_end(sa_sb *b, sa_sb_slot *s) {
298 1110645 ▶ if (!b || !s) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !s) return; 1110645 0 1110645
if (!b || !s) return; 0 1110645 1110645
299 1110645 sa_at_store64_rel(&s->updated, sa_now_ms());
300 1110645 sa_at_fence_rel();
301 1110645 sa_at_store32_rel(&s->version, (sa_at_load32_acq(&s->version) + 1u));
302 }
303  
304 /* Between begin and end. The owner is the only writer, so these are plain
305 * release stores with no lock; the seqlock the bracket maintains is what makes
306 * the whole set coherent to a reader. */
307 1110627 static void sa_sb_set_gauge(sa_sb_slot *s, int idx, uint64_t v) {
308 1110627 ▶ if (s && idx >= 0 && idx < SA_SB_FIELDS)
Branch: 3 branches
BranchTrueFalseTotal
if (s && idx >= 0 && idx < SA_SB_FIELDS) 1110627 0 1110627
if (s && idx >= 0 && idx < SA_SB_FIELDS) 1110627 0 1110627
if (s && idx >= 0 && idx < SA_SB_FIELDS) 1110627 0 1110627
309 1110627 sa_at_store64_rel(&s->gauges[idx], v);
310 1110627 }
311  
312 19 static void sa_sb_add_gauge(sa_sb_slot *s, int idx, int64_t by) {
313 19 ▶ if (s && idx >= 0 && idx < SA_SB_FIELDS)
Branch: 3 branches
BranchTrueFalseTotal
if (s && idx >= 0 && idx < SA_SB_FIELDS) 19 0 19
if (s && idx >= 0 && idx < SA_SB_FIELDS) 19 0 19
if (s && idx >= 0 && idx < SA_SB_FIELDS) 19 0 19
314 19 sa_at_store64_rel(&s->gauges[idx],
315 19 sa_at_load64_acq(&s->gauges[idx]) + (uint64_t)by);
316 19 }
317  
318 1110617 static void sa_sb_set_status(sa_sb_slot *s, const char *p, uint32_t n) {
319 1110617 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 1110617 1110617
320 1110617 ▶ if (n >= SA_SB_STATUS) n = SA_SB_STATUS - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= SA_SB_STATUS) n = SA_SB_STATUS - 1; 0 1110617 1110617
321 1110617 ▶ if (n && p) memcpy(s->status, p, n);
Branch: 2 branches
BranchTrueFalseTotal
if (n && p) memcpy(s->status, p, n); 1110617 0 1110617
if (n && p) memcpy(s->status, p, n); 1110617 0 1110617
322 1110617 s->status[n] = '\0';
323 1110617 s->statuslen = n;
324 }
325  
326 /* Read row `i` coherently. Returns 1 with `out` filled for a live row, 0 for an
327 * empty one or one whose writer died mid-update (version stuck odd past the
328 * retry bound). `out->alive` says whether the owner is still running. */
329 778363 static int sa_sb_read(sa_sb *b, uint32_t i, sa_sb_reading *out) {
330 sa_sb_slot *s;
331 long spin;
332  
333 778363 ▶ if (!b || !out || i >= b->nslots) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !out || i >= b->nslots) return 0; 778363 0 778363
if (!b || !out || i >= b->nslots) return 0; 778363 0 778363
if (!b || !out || i >= b->nslots) return 0; 0 778363 778363
334 778363 s = &b->slots[i];
335 778363 ▶ if (sa_at_load32_acq(&s->state) != SA_SB_LIVE) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&s->state) != SA_SB_LIVE) return 0; 584852 193511 778363
336  
337 693710 ▶ for (spin = 0; spin < SA_SB_RETRY; spin++) {
Branch: 1 branch
BranchTrueFalseTotal
for (spin = 0; spin < SA_SB_RETRY; spin++) { 693698 12 693710
338 693698 uint32_t v1 = sa_at_load32_acq(&s->version);
339 uint32_t f, sl;
340 693698 ▶ if (v1 & 1u) continue; /* mid-update: wait a moment */
Branch: 1 branch
BranchTrueFalseTotal
if (v1 & 1u) continue; /* mid-update: wait a moment */ 456906 236792 693698
341  
342 236792 out->pid = sa_at_load64_acq(&s->pid);
343 236792 out->epoch = sa_at_load64_acq(&s->epoch);
344 236792 out->updated = sa_at_load64_acq(&s->updated);
345 2131128 ▶ for (f = 0; f < SA_SB_FIELDS; f++)
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < SA_SB_FIELDS; f++) 1894336 236792 2131128
346 1894336 out->gauges[f] = sa_at_load64_acq(&s->gauges[f]);
347 236792 sl = s->statuslen;
348 236792 ▶ if (sl >= SA_SB_STATUS) sl = SA_SB_STATUS - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sl >= SA_SB_STATUS) sl = SA_SB_STATUS - 1; 0 236792 236792
349 236792 memcpy(out->status, s->status, sl);
350 236792 out->status[sl] = '\0';
351 236792 out->statuslen = sl;
352  
353 236792 sa_at_fence_acq();
354 236792 ▶ if (sa_at_load32_acq(&s->version) != v1) continue; /* it changed */
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load32_acq(&s->version) != v1) continue; /* it changed */ 43293 193499 236792
355  
356 193499 out->alive = sa_pid_alive(out->pid) ? 1 : 0;
357 193499 return 1;
358 }
359 12 return 0; /* stuck odd: the owner died mid-update */
360 }
361  
362 #else /* !SA_HAVE_ATOMICS */
363  
364 /* No atomics: no scoreboard. Defined so the ABI table has something to point
365 * at; inert so a build without atomics has an empty board rather than a crash.
366 * A publish is a no-op and a read finds no rows, which is what a status page
367 * with nothing behind it should show. */
368 static int sa_sb_field(const sa_sb *b, const char *name, uint32_t nlen) {
369 (void)b; (void)name; (void)nlen; return -1;
370 }
371 static sa_sb *sa_sb_bind(sa_region *arena, sa_reg *e,
372 const char *const *fields, uint32_t nfields, int *err) {
373 (void)arena; (void)e; (void)fields; (void)nfields;
374 if (err) *err = SA_E_NOATOMICS;
375 return NULL;
376 }
377 static void sa_sb_free(sa_sb *b) { free(b); }
378 static int sa_sb_take(sa_sb *b) { (void)b; return -1; }
379 static sa_sb_slot *sa_sb_begin(sa_sb *b) { (void)b; return NULL; }
380 static void sa_sb_end(sa_sb *b, sa_sb_slot *s) { (void)b; (void)s; }
381 static void sa_sb_set_gauge(sa_sb_slot *s, int idx, uint64_t v) {
382 (void)s; (void)idx; (void)v;
383 }
384 static void sa_sb_add_gauge(sa_sb_slot *s, int idx, int64_t by) {
385 (void)s; (void)idx; (void)by;
386 }
387 static void sa_sb_set_status(sa_sb_slot *s, const char *p, uint32_t n) {
388 (void)s; (void)p; (void)n;
389 }
390 static int sa_sb_read(sa_sb *b, uint32_t i, sa_sb_reading *out) {
391 (void)b; (void)i; (void)out; return 0;
392 }
393  
394 #endif /* SA_HAVE_ATOMICS */
395  
396 #endif /* SA_SCOREBOARD_H */
« include/sa/sa_hll.h ↑ Summary include/sa/sa_hist.h »