include/hyperman/hm_sa_board.h

filter: Statementstmt 98.9% 96 / 99 (2 uncoverable) Branchbran 83.7% 53 / 86 (21 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.8% 149 / 185 (23 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/backend_poll.c ↑ Summary include/hyperman/backend_epoll.c »
1 /* hm_sa_board.h - what the arena makes possible off the hot path: a
2 * scoreboard with one row per worker, and an opt-in distinct-client count.
3 *
4 * Perl-free; the includer defines `HM_SA` and `hm_sa_region` (hm_sa.h in the
5 * server, the benchmark harness with no interpreter).
6 *
7 * THE RULE: nothing here runs per request. The scoreboard row is MIRRORED
8 * from the loop's own counters by a one-second timer, one seqlock bracket per
9 * worker per second, so the request path never touches it. The one thing
10 * that may touch the accept path, the HyperLogLog of distinct clients, is
11 * opt-in and its cost is measured and documented rather than hidden; with it
12 * off the accept path pays one load of a NULL.
13 */
14  
15 #ifndef HM_SA_BOARD_H
16 #define HM_SA_BOARD_H
17  
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stddef.h>
22  
23 #include "sa_abi.h"
24  
25 /* The seven gauges, in row order. `h3` is the HTTP/3 request count; the
26 * other h3 counters have no column, a board's eight gauges being what they
27 * are. */
28 #define HM_SA_BOARD_FIELDS 7
29 static const char *const hm_sa_board_fields[HM_SA_BOARD_FIELDS] = {
30 "requests", "accepts", "denied", "conns", "bytes_out", "datagrams", "h3"
31 };
32 enum { HM_SB_REQUESTS, HM_SB_ACCEPTS, HM_SB_DENIED, HM_SB_CONNS,
33 HM_SB_BYTES_OUT, HM_SB_DATAGRAMS, HM_SB_H3 };
34  
35 /* The aggregate a reader wants: every live row summed. The same shape the
36 * ABI's pool_stats hands a C consumer. */
37 typedef struct hm_sa_pool_stats {
38 uint64_t workers; /* rows that read coherently */
39 uint64_t alive; /* ... whose writer is running */
40 uint64_t requests, accepts, denied, conns, bytes_out, datagrams, h3;
41 } hm_sa_pool_stats;
42  
43 static sa_sb *hm_sa_board = NULL; /* NULL = no board: fail open */
44 static int hm_sa_board_row = -1; /* this process's row, or -1 */
45 static sa_hll *hm_sa_hll = NULL; /* the distinct-client sketch, opt-in */
46  
47 176 static int hm_sa_board_live(void) { return hm_sa_board != NULL; }
48  
49 /* ---- lifecycle ------------------------------------------------------------- */
50  
51 /* Open the board before the fork, `slots` rows (workers + 1). Idempotent;
52 * -1 with *err on refusal, in which case there is no board and every door
53 * answers "none". */
54 97 static int hm_sa_board_open(uint32_t slots, int *err) {
55 97 int e = 0;
56 /*
57 # uncoverable branch false class:ignore_covered_err
58 # uncoverable condition right count:1 class:ignore_covered_err */
59 97 ▶ if (err) *err = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = 0; 97 0 97
60 97 ▶ if (!HM_SA || !hm_sa_region || hm_sa_board) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region || hm_sa_board) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 94 3 97
if (!HM_SA || !hm_sa_region || hm_sa_board) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 94 0 94
if (!HM_SA || !hm_sa_region || hm_sa_board) return 0; /* # uncoverable condition left count:2 class:ignore_covered_err */ 13 81 94
61 /*
62 # uncoverable branch true class:ignore_covered_err
63 # uncoverable condition left count:1 class:ignore_covered_err */
64 81 ▶ if (!slots) slots = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (!slots) slots = 2; 0 81 81
65 81 hm_sa_board = HM_SA->sb_open(hm_sa_region, "workers", 7,
66 hm_sa_board_fields, HM_SA_BOARD_FIELDS,
67 slots, &e);
68 /*
69 # uncoverable branch true class:ignore_covered_err
70 # uncoverable condition left count:1 class:ignore_covered_err
71 # uncoverable condition left count:2 class:ignore_covered_err
72 # uncoverable condition right count:2 class:ignore_covered_err */
73 81 ▶ if (!hm_sa_board) { if (err) *err = e; return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!hm_sa_board) { if (err) *err = e; return -1; } 0 81 81
if (!hm_sa_board) { if (err) *err = e; return -1; } 0 0 0
74 81 return 0;
75 }
76  
77 /* Take a row for this process, after the fork. -1 when the board is full,
78 * which costs this worker its row and nothing else. */
79 131 static int hm_sa_board_take(void) {
80 /*
81 # uncoverable branch true
82 # uncoverable condition left count:1 class:ignore_covered_err */
83 131 ▶ if (!hm_sa_board) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board) return -1; 0 131 131
84 131 hm_sa_board_row = HM_SA->sb_take(hm_sa_board);
85 131 return hm_sa_board_row;
86 }
87  
88 /* Copy the counters into this worker's row, coherently, and set the status.
89 * Called from the timer, never from a request. */
90 249 static void hm_sa_board_mirror(uint64_t requests, uint64_t accepts,
91 uint64_t denied, int conns,
92 uint64_t bytes_out, uint64_t datagrams,
93 uint64_t h3) {
94 void *row;
95 char st[24];
96 int n;
97 /*
98 # uncoverable branch true class:ignore_covered_err
99 # uncoverable condition left count:1 class:ignore_covered_err
100 # uncoverable condition left count:2 class:ignore_covered_err */
101 249 ▶ if (!hm_sa_board || hm_sa_board_row < 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!hm_sa_board || hm_sa_board_row < 0) return; 249 0 249
if (!hm_sa_board || hm_sa_board_row < 0) return; 0 249 249
102 249 row = HM_SA->sb_begin(hm_sa_board);
103 /*
104 # uncoverable branch true
105 # uncoverable condition left count:1 class:ignore_covered_err */
106 249 ▶ if (!row) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return; 0 249 249
107 249 HM_SA->sb_set_gauge(row, HM_SB_REQUESTS, requests);
108 249 HM_SA->sb_set_gauge(row, HM_SB_ACCEPTS, accepts);
109 249 HM_SA->sb_set_gauge(row, HM_SB_DENIED, denied);
110 249 HM_SA->sb_set_gauge(row, HM_SB_CONNS, (uint64_t)(conns < 0 ? 0 : conns));
111 249 HM_SA->sb_set_gauge(row, HM_SB_BYTES_OUT, bytes_out);
112 249 HM_SA->sb_set_gauge(row, HM_SB_DATAGRAMS, datagrams);
113 249 HM_SA->sb_set_gauge(row, HM_SB_H3, h3);
114 46 n = conns > 0 ? snprintf(st, sizeof st, "busy %d", conns)
115 249 ▶ : snprintf(st, sizeof st, "idle");
Branch: 1 branch
BranchTrueFalseTotal
: snprintf(st, sizeof st, "idle"); 46 203 249
116 249 HM_SA->sb_set_status(row, st, (uint32_t)n);
117 249 HM_SA->sb_end(hm_sa_board, row);
118 }
119  
120 /* ---- reading --------------------------------------------------------------- */
121  
122 /* Row `i`, coherently, into *out. 0 for an empty row or one whose writer
123 * died mid-update. */
124 147 static int hm_sa_board_read(uint32_t i, sa_sb_reading *out) {
125 /*
126 # uncoverable branch true
127 # uncoverable condition left count:1 class:ignore_covered_err */
128 147 ▶ if (!hm_sa_board) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board) return 0; 0 147 147
129 147 return HM_SA->sb_read(hm_sa_board, i, out);
130 }
131  
132 36 static uint64_t hm_sa_board_slots(void) {
133 36 ▶ return hm_sa_board ? HM_SA->sb_slots(hm_sa_board) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return hm_sa_board ? HM_SA->sb_slots(hm_sa_board) : 0; 35 1 36
134 }
135  
136 /* Every row summed. 1 when there is a board, 0 when there is not. */
137 9 static int hm_sa_board_pool(hm_sa_pool_stats *out) {
138 uint64_t i, n;
139 /*
140 # uncoverable branch true
141 # uncoverable condition left count:1 class:ignore_covered_err */
142 9 ▶ if (!out) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return 0; 0 9 9
143 9 memset(out, 0, sizeof *out);
144 9 ▶ if (!hm_sa_board) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board) return 0; 1 8 9
145 8 n = hm_sa_board_slots();
146 46 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 38 8 46
147 sa_sb_reading rd;
148 38 ▶ if (!hm_sa_board_read((uint32_t)i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_read((uint32_t)i, &rd)) continue; 22 16 38
149 16 out->workers++;
150 16 ▶ if (rd.alive) out->alive++;
Branch: 1 branch
BranchTrueFalseTotal
if (rd.alive) out->alive++; 11 5 16
151 16 out->requests += rd.gauges[HM_SB_REQUESTS];
152 16 out->accepts += rd.gauges[HM_SB_ACCEPTS];
153 16 out->denied += rd.gauges[HM_SB_DENIED];
154 16 out->conns += rd.gauges[HM_SB_CONNS];
155 16 out->bytes_out += rd.gauges[HM_SB_BYTES_OUT];
156 16 out->datagrams += rd.gauges[HM_SB_DATAGRAMS];
157 16 out->h3 += rd.gauges[HM_SB_H3];
158 }
159 8 return 1;
160 }
161  
162 /* The row a pid owns, or -1. For the supervisor's wedged check. */
163 19 static int hm_sa_board_row_of(uint64_t pid, sa_sb_reading *out) {
164 19 uint64_t i, n = hm_sa_board_slots();
165 67 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 63 4 67
166 63 ▶ if (!hm_sa_board_read((uint32_t)i, out)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_read((uint32_t)i, out)) continue; 15 48 63
167 48 ▶ if (out->pid == pid) return (int)i;
Branch: 1 branch
BranchTrueFalseTotal
if (out->pid == pid) return (int)i; 15 33 48
168 }
169 4 return -1;
170 }
171  
172 /* One aggregate line and one line per row, for USR1.
173 *
174 * ONE WRITE, not one per line. A dump is read by somebody else - a log tail, a
175 * test, another admin's USR1 - while it is being written, and a dump that
176 * reaches the stream in three writes can be read as an aggregate line with no
177 * rows behind it, which reads as a pool with no workers. So the lines are
178 * formatted into a buffer and handed over in one go; a pool too large for the
179 * buffer is flushed a bufferful at a time, always on a line boundary, so the
180 * worst case is a dump split between whole lines rather than mid-line. */
181 12 static int hm_sa_board_row_line(char *b, size_t cap, const sa_sb_reading *rd) {
182 12 return snprintf(b, cap,
183 "Hyperman worker %llu: requests=%llu accepts=%llu "
184 "denied=%llu conns=%llu bytes_out=%llu status=%.*s%s\n",
185 12 (unsigned long long)rd->pid,
186 12 (unsigned long long)rd->gauges[HM_SB_REQUESTS],
187 12 (unsigned long long)rd->gauges[HM_SB_ACCEPTS],
188 12 (unsigned long long)rd->gauges[HM_SB_DENIED],
189 12 (unsigned long long)rd->gauges[HM_SB_CONNS],
190 12 (unsigned long long)rd->gauges[HM_SB_BYTES_OUT],
191 24 ▶ (int)rd->statuslen, rd->status, rd->alive ? "" : " DEAD");
Branch: 1 branch
BranchTrueFalseTotal
(int)rd->statuslen, rd->status, rd->alive ? "" : " DEAD"); 7 5 12
192 }
193  
194 4 static void hm_sa_board_print(FILE *f) {
195 hm_sa_pool_stats p;
196 uint64_t i, n;
197 char buf[4096];
198 4 size_t off = 0;
199 int w;
200  
201 /*
202 # uncoverable branch true
203 # uncoverable condition left count:1 class:ignore_covered_err */
204 4 ▶ if (!hm_sa_board_pool(&p)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_pool(&p)) return; 0 4 4
205 4 w = snprintf(buf, sizeof buf,
206 "Hyperman pool: workers=%llu alive=%llu requests=%llu "
207 "accepts=%llu denied=%llu conns=%llu bytes_out=%llu "
208 "datagrams=%llu h3=%llu\n",
209 4 (unsigned long long)p.workers, (unsigned long long)p.alive,
210 4 (unsigned long long)p.requests, (unsigned long long)p.accepts,
211 4 (unsigned long long)p.denied, (unsigned long long)p.conns,
212 4 (unsigned long long)p.bytes_out,
213 4 (unsigned long long)p.datagrams, (unsigned long long)p.h3);
214 /*
215 # uncoverable branch true
216 # uncoverable condition left count:1 class:ignore_covered_err */
217 4 ▶ if (w < 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) return; 0 4 4
218 4 ▶ off = (size_t)w < sizeof buf ? (size_t)w : sizeof buf - 1; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
off = (size_t)w < sizeof buf ? (size_t)w : sizeof buf - 1; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4 0 4
219  
220 4 n = hm_sa_board_slots();
221 28 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 24 4 28
222 sa_sb_reading rd;
223 24 ▶ if (!hm_sa_board_read((uint32_t)i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hm_sa_board_read((uint32_t)i, &rd)) continue; 12 12 24
224 12 w = hm_sa_board_row_line(buf + off, sizeof buf - off, &rd);
225 /*
226 # uncoverable branch true
227 # uncoverable condition left count:1 class:ignore_covered_err */
228 12 ▶ if (w < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) continue; 0 12 12
229 /*
230 # uncoverable branch true class:ignore_covered_err
231 # uncoverable condition left count:1 class:ignore_covered_err */
232 12 ▶ if ((size_t)w >= sizeof buf - off) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)w >= sizeof buf - off) { 0 12 12
233 /* No room left: hand over the whole lines we have and format this
234 * row into the empty buffer instead. */
235 /*
236 # uncoverable statement
237 # uncoverable branch true
238 # uncoverable branch false
239 # uncoverable condition left count:1
240 # uncoverable condition right count:1 */
241 0 ▶ if (off) { fwrite(buf, 1, off, f); fflush(f); off = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (off) { fwrite(buf, 1, off, f); fflush(f); off = 0; } 0 0 0
242 0 w = hm_sa_board_row_line(buf, sizeof buf, &rd); /* # uncoverable statement */
243 /*
244 # uncoverable statement
245 # uncoverable branch true
246 # uncoverable branch false
247 # uncoverable condition left count:1
248 # uncoverable condition right count:1
249 # uncoverable condition left count:2
250 # uncoverable condition right count:2 */
251 0 ▶ if (w < 0 || (size_t)w >= sizeof buf) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (w < 0 || (size_t)w >= sizeof buf) continue; 0 0 0
if (w < 0 || (size_t)w >= sizeof buf) continue; 0 0 0
252 }
253 12 off += (size_t)w;
254 }
255 /*
256 # uncoverable branch false class:ignore_covered_err
257 # uncoverable condition right count:1 class:ignore_covered_err */
258 4 ▶ if (off) { fwrite(buf, 1, off, f); fflush(f); }
Branch: 1 branch
BranchTrueFalseTotal
if (off) { fwrite(buf, 1, off, f); fflush(f); } 4 0 4
259 }
260  
261 /* ---- distinct clients: opt-in, on the accept path ----------------------------- */
262  
263 /* The HyperLogLog of peers seen, at precision 14 (about 0.8% error, 16KB).
264 * Opened only when asked for. */
265 2 static int hm_sa_hll_open(int *err) {
266 2 int e = 0;
267 /*
268 # uncoverable branch false class:ignore_covered_err
269 # uncoverable condition right count:1 class:ignore_covered_err */
270 2 ▶ if (err) *err = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = 0; 2 0 2
271 /*
272 # uncoverable branch true class:ignore_covered_err
273 # uncoverable condition left count:1 class:ignore_covered_err
274 # uncoverable condition left count:2 class:ignore_covered_err
275 # uncoverable condition left count:3 class:ignore_covered_err */
276 2 ▶ if (!HM_SA || !hm_sa_region || hm_sa_hll) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!HM_SA || !hm_sa_region || hm_sa_hll) return 0; 2 0 2
if (!HM_SA || !hm_sa_region || hm_sa_hll) return 0; 2 0 2
if (!HM_SA || !hm_sa_region || hm_sa_hll) return 0; 0 2 2
277 2 hm_sa_hll = HM_SA->hll_open(hm_sa_region, "clients", 7, 14, &e);
278 /*
279 # uncoverable branch true
280 # uncoverable condition left count:1 class:ignore_covered_err
281 # uncoverable condition left count:2 class:ignore_covered_err
282 # uncoverable condition right count:2 class:ignore_covered_err */
283 2 ▶ if (!hm_sa_hll) { if (err) *err = e; return -1; }
Branch: 2 branches
BranchTrueFalseTotal
if (!hm_sa_hll) { if (err) *err = e; return -1; } 0 2 2
if (!hm_sa_hll) { if (err) *err = e; return -1; } 0 0 0
284 2 return 0;
285 }
286  
287 /* One hash and one CAS-max, about 20ns. The caller tests hm_sa_hll first,
288 * so the off path is a load of a NULL and nothing else. */
289 212 static void hm_sa_hll_add(const char *peer) {
290 /*
291 # uncoverable branch false class:ignore_covered_err
292 # uncoverable condition right count:1 class:ignore_covered_err
293 # uncoverable condition right count:2 class:ignore_covered_err */
294 212 ▶ if (hm_sa_hll && peer) HM_SA->hll_add(hm_sa_hll, peer, strlen(peer));
Branch: 2 branches
BranchTrueFalseTotal
if (hm_sa_hll && peer) HM_SA->hll_add(hm_sa_hll, peer, strlen(peer)); 212 0 212
if (hm_sa_hll && peer) HM_SA->hll_add(hm_sa_hll, peer, strlen(peer)); 212 0 212
295 212 }
296  
297 /* The estimate, or -1.0 with the sketch off. */
298 206 static double hm_sa_hll_count(void) {
299 206 ▶ return hm_sa_hll ? HM_SA->hll_count(hm_sa_hll) : -1.0;
Branch: 1 branch
BranchTrueFalseTotal
return hm_sa_hll ? HM_SA->hll_count(hm_sa_hll) : -1.0; 201 5 206
300 }
301  
302 #endif /* HM_SA_BOARD_H */
« include/hyperman/backend_poll.c ↑ Summary include/hyperman/backend_epoll.c »