| 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 |
|
| /* What sa_sb_take answers when it has no row to give. Two answers, because |
| 66 |
|
| * they ask two different things of the caller: FULL means every row is a live |
| 67 |
|
| * worker's and the board was sized too small; BUSY means the claim stripe was |
| 68 |
|
| * held for the whole of a bounded wait, and asking again will work. */ |
| 69 |
|
| #define SA_SB_FULL (-1) |
| 70 |
|
| #define SA_SB_BUSY (-2) |
| 71 |
|
| |
| 72 |
0 |
| static const char *sa_sb_refusal(int rc) { |
| 73 |
|
| return rc == SA_SB_BUSY |
| 74 |
|
| ? "the claim was held by another process for the whole of a bounded wait" |
| 75 |
0 |
▶ | : "the board is full"; |
Branch: 1 branch
| Branch | True | False | Total |
| : "the board is full"; |
0 |
0 |
0 |
|
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| typedef struct { |
| 79 |
|
| volatile uint32_t state; /* SA_SB_EMPTY / SA_SB_LIVE */ |
| 80 |
|
| volatile uint32_t version; /* odd while the owner is writing */ |
| 81 |
|
| volatile uint64_t pid; /* the owner */ |
| 82 |
|
| volatile uint64_t epoch; /* bumped on every (re)claim: an identity */ |
| 83 |
|
| volatile uint64_t updated; /* ms wall clock of the last write */ |
| 84 |
|
| volatile uint64_t gauges[SA_SB_FIELDS]; |
| 85 |
|
| volatile uint32_t statuslen; |
| 86 |
|
| char status[SA_SB_STATUS]; |
| 87 |
|
| } sa_sb_slot; |
| 88 |
|
| |
| 89 |
|
| typedef struct { |
| 90 |
|
| volatile uint32_t magic; /* published LAST at init */ |
| 91 |
|
| uint32_t nslots; |
| 92 |
|
| uint32_t nfields; |
| 93 |
|
| uint32_t pad; |
| 94 |
|
| uint64_t slots_off; /* from the BOARD's base */ |
| 95 |
|
| char fields[SA_SB_FIELDS][SA_SB_NAME]; /* the schema */ |
| 96 |
|
| volatile unsigned char locks[SA_LOCK_STRIPES]; /* claim only */ |
| 97 |
|
| } sa_sb_hdr; |
| 98 |
|
| |
| 99 |
|
| #ifndef SA_SB_FWD |
| 100 |
|
| #define SA_SB_FWD |
| 101 |
|
| typedef struct sa_sb sa_sb; |
| 102 |
|
| #endif |
| 103 |
|
| |
| 104 |
|
| /* The process-local handle. `mine` is the index of the row this process |
| 105 |
|
| * claimed, or -1; `epoch` is the identity of that claim, so a stale handle |
| 106 |
|
| * cannot write a row a later worker now owns. */ |
| 107 |
|
| struct sa_sb { |
| 108 |
|
| sa_region *arena; |
| 109 |
|
| sa_sb_hdr *hdr; |
| 110 |
|
| sa_sb_slot *slots; |
| 111 |
|
| uint32_t nslots; |
| 112 |
|
| uint32_t nfields; |
| 113 |
|
| int mine; |
| 114 |
|
| uint64_t epoch; |
| 115 |
|
| }; |
| 116 |
|
| |
| 117 |
|
| /* One row read out coherently, for a reader. Guarded so the identical struct in |
| 118 |
|
| * sa_abi.h (which a C consumer sees WITHOUT this header) is not defined twice; |
| 119 |
|
| * SA_SB_FIELDS is 8 and SA_SB_STATUS 64, matching the literals there. */ |
| 120 |
|
| #ifndef SA_SB_READING_DEFINED |
| 121 |
|
| #define SA_SB_READING_DEFINED |
| 122 |
|
| typedef struct sa_sb_reading { |
| 123 |
|
| uint64_t pid; |
| 124 |
|
| uint64_t epoch; |
| 125 |
|
| uint64_t updated; |
| 126 |
|
| int alive; /* is the owner still running */ |
| 127 |
|
| uint64_t gauges[SA_SB_FIELDS]; |
| 128 |
|
| uint32_t statuslen; |
| 129 |
|
| char status[SA_SB_STATUS]; |
| 130 |
|
| } sa_sb_reading; |
| 131 |
|
| #endif |
| 132 |
|
| |
| 133 |
105 |
| static uint64_t sa_sb_bytes(uint32_t nslots) { |
| 134 |
105 |
| return sa_align_up((uint64_t)sizeof(sa_sb_hdr)) |
| 135 |
105 |
| + sa_align_up((uint64_t)nslots * (uint64_t)sizeof(sa_sb_slot)); |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| #define SA_SB_SLOTS(h) \ |
| 139 |
|
| ((sa_sb_slot *)((char *)(h) + (size_t)(h)->slots_off)) |
| 140 |
|
| |
| 141 |
|
| #if SA_HAVE_ATOMICS |
| 142 |
|
| |
| 143 |
|
| /* The index of a named field, or -1. A small linear scan the writer does once |
| 144 |
|
| * per update; a caller that cares caches it. */ |
| 145 |
814055 |
| static int sa_sb_field(const sa_sb *b, const char *name, uint32_t nlen) { |
| 146 |
|
| uint32_t i; |
| 147 |
814055 |
▶ | if (!name || !nlen || nlen >= SA_SB_NAME) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!name || !nlen || nlen >= SA_SB_NAME) return -1; |
814055 |
0 |
814055 |
| if (!name || !nlen || nlen >= SA_SB_NAME) return -1; |
814055 |
0 |
814055 |
| if (!name || !nlen || nlen >= SA_SB_NAME) return -1; |
0 |
814055 |
814055 |
|
| 148 |
814094 |
▶ | for (i = 0; i < b->nfields; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < b->nfields; i++) { |
814084 |
10 |
814094 |
|
| 149 |
814084 |
▶ | if (strncmp(b->hdr->fields[i], name, nlen) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(b->hdr->fields[i], name, nlen) == 0 |
814045 |
39 |
814084 |
|
| 150 |
814045 |
▶ | && b->hdr->fields[i][nlen] == '\0') |
Branch: 1 branch
| Branch | True | False | Total |
| && b->hdr->fields[i][nlen] == '\0') |
814045 |
0 |
814045 |
|
| 151 |
814045 |
| return (int)i; |
| 152 |
|
| } |
| 153 |
10 |
| return -1; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
61 |
| static sa_sb *sa_sb_bind(sa_region *arena, sa_reg *e, |
| 157 |
|
| const char *const *fields, uint32_t nfields, int *err) |
| 158 |
|
| { |
| 159 |
|
| sa_sb *b; |
| 160 |
|
| sa_sb_hdr *h; |
| 161 |
|
| uint64_t hash; |
| 162 |
|
| |
| 163 |
61 |
▶ | if (err) *err = SA_E_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = SA_E_OK; |
61 |
0 |
61 |
|
| 164 |
61 |
▶ | if (!arena || !e) { if (err) *err = SA_E_NOENT; return NULL; } |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 165 |
61 |
▶ | if (nfields > SA_SB_FIELDS) { if (err) *err = SA_E_NAME; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 166 |
|
| |
| 167 |
61 |
| h = (sa_sb_hdr *)sa_ptr_len(arena, e->off, sizeof(sa_sb_hdr)); |
| 168 |
61 |
▶ | if (!h) { if (err) *err = SA_E_NOENT; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 169 |
|
| |
| 170 |
61 |
▶ | if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) { |
35 |
26 |
61 |
|
| 171 |
35 |
| hash = sa_at_fnv(e->name, strlen(e->name)); |
| 172 |
35 |
▶ | if (sa_at_lock(h->locks, hash)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_lock(h->locks, hash)) { |
35 |
0 |
35 |
|
| 173 |
35 |
▶ | if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&h->magic) != SA_SB_MAGIC) { |
35 |
0 |
35 |
|
| 174 |
35 |
| uint32_t nsl = (uint32_t)((e->len - sizeof(sa_sb_hdr)) |
| 175 |
35 |
| / sizeof(sa_sb_slot)); |
| 176 |
|
| uint32_t i; |
| 177 |
35 |
▶ | if (nsl < 1 || sa_sb_bytes(nsl) > e->len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nsl < 1 || sa_sb_bytes(nsl) > e->len) { |
35 |
0 |
35 |
| if (nsl < 1 || sa_sb_bytes(nsl) > e->len) { |
0 |
35 |
35 |
|
| 178 |
0 |
| sa_at_unlock(h->locks, hash); |
| 179 |
0 |
▶ | if (err) *err = SA_E_FULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = SA_E_FULL; |
0 |
0 |
0 |
|
| 180 |
0 |
| return NULL; |
| 181 |
|
| } |
| 182 |
35 |
▶ | if (nsl > SA_SB_SLOTS_MAX) nsl = SA_SB_SLOTS_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nsl > SA_SB_SLOTS_MAX) nsl = SA_SB_SLOTS_MAX; |
0 |
35 |
35 |
|
| 183 |
35 |
| memset((void *)h, 0, (size_t)sa_sb_bytes(nsl)); |
| 184 |
35 |
| h->nslots = nsl; |
| 185 |
35 |
| h->nfields = nfields; |
| 186 |
35 |
| h->slots_off = sa_align_up((uint64_t)sizeof(sa_sb_hdr)); |
| 187 |
89 |
▶ | for (i = 0; i < nfields && fields; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < nfields && fields; i++) { |
54 |
35 |
89 |
| for (i = 0; i < nfields && fields; i++) { |
54 |
0 |
54 |
|
| 188 |
54 |
| size_t l = strlen(fields[i]); |
| 189 |
54 |
▶ | if (l >= SA_SB_NAME) l = SA_SB_NAME - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (l >= SA_SB_NAME) l = SA_SB_NAME - 1; |
0 |
54 |
54 |
|
| 190 |
54 |
| memcpy(h->fields[i], fields[i], l); |
| 191 |
|
| } |
| 192 |
35 |
| sa_at_store32_rel(&h->magic, SA_SB_MAGIC); |
| 193 |
|
| } |
| 194 |
35 |
| sa_at_unlock(h->locks, hash); |
| 195 |
|
| } |
| 196 |
35 |
▶ | if (!sa_wait_magic32(&h->magic, SA_SB_MAGIC)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sa_wait_magic32(&h->magic, SA_SB_MAGIC)) { |
0 |
35 |
35 |
|
| 197 |
0 |
▶ | if (err) *err = SA_E_MAGIC; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = SA_E_MAGIC; |
0 |
0 |
0 |
|
| 198 |
0 |
| return NULL; |
| 199 |
|
| } |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
|
| /* A caller that named fields must name the SAME ones, for the reason every |
| 203 |
|
| * tenant checks its shape: two processes disagreeing about the columns is |
| 204 |
|
| * worse than either failing. A caller that named none inherits them. */ |
| 205 |
61 |
▶ | if (nfields) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nfields) { |
44 |
17 |
61 |
|
| 206 |
|
| uint32_t i; |
| 207 |
44 |
▶ | if (h->nfields != nfields) { if (err) *err = SA_E_SHAPE; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 208 |
107 |
▶ | for (i = 0; i < nfields && fields; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < nfields && fields; i++) { |
72 |
35 |
107 |
| for (i = 0; i < nfields && fields; i++) { |
72 |
0 |
72 |
|
| 209 |
72 |
▶ | if (strncmp(h->fields[i], fields[i], SA_SB_NAME) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strncmp(h->fields[i], fields[i], SA_SB_NAME) != 0) { |
9 |
63 |
72 |
|
| 210 |
9 |
▶ | if (err) *err = SA_E_SHAPE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = SA_E_SHAPE; |
9 |
0 |
9 |
|
| 211 |
9 |
| return NULL; |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
52 |
| b = (sa_sb *)calloc(1, sizeof(sa_sb)); |
| 217 |
52 |
▶ | if (!b) { if (err) *err = SA_E_NOMEM; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 218 |
52 |
| b->arena = arena; |
| 219 |
52 |
| b->hdr = h; |
| 220 |
52 |
| b->slots = SA_SB_SLOTS(h); |
| 221 |
52 |
| b->nslots = h->nslots; |
| 222 |
52 |
| b->nfields = h->nfields; |
| 223 |
52 |
| b->mine = -1; |
| 224 |
52 |
| b->epoch = 0; |
| 225 |
52 |
| return b; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
52 |
| static void sa_sb_free(sa_sb *b) { free(b); } |
| 229 |
|
| |
| 230 |
|
| /* Claim a row for this process, reclaiming a dead worker's row when the board |
| 231 |
|
| * is full. Returns the row index, SA_SB_FULL when every row belongs to a live |
| 232 |
|
| * worker, or SA_SB_BUSY when the claim stripe was held for the whole wait. |
| 233 |
|
| * Idempotent: called again in the same process it returns the row it already |
| 234 |
|
| * holds. |
| 235 |
|
| * |
| 236 |
|
| * The stripe lock serialises claimers - the ONE place this tenant locks - so a |
| 237 |
|
| * row is never handed to two workers at once. A claim happens once per worker, |
| 238 |
|
| * at start, so it waits for the stripe as a carve does and not on sa_at_lock's |
| 239 |
|
| * spin: an OpenBSD smoker had five children claim at once, one lost the spin |
| 240 |
|
| * to the other four, and that read as a full board with eleven rows empty. */ |
| 241 |
18 |
| static int sa_sb_take(sa_sb *b) { |
| 242 |
|
| sa_sb_hdr *h; |
| 243 |
|
| uint64_t me, hash; |
| 244 |
|
| uint32_t i; |
| 245 |
18 |
| int dead = -1; |
| 246 |
|
| |
| 247 |
18 |
▶ | if (!b) return SA_SB_FULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return SA_SB_FULL; |
0 |
18 |
18 |
|
| 248 |
18 |
▶ | if (b->mine >= 0) return b->mine; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->mine >= 0) return b->mine; |
0 |
18 |
18 |
|
| 249 |
18 |
| h = b->hdr; |
| 250 |
18 |
| me = sa_getpid(); |
| 251 |
18 |
| hash = sa_at_fnv("\0scoreboard-claim", 17); |
| 252 |
|
| |
| 253 |
18 |
▶ | if (!sa_lock_wait(h->locks, hash)) return SA_SB_BUSY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sa_lock_wait(h->locks, hash)) return SA_SB_BUSY; |
0 |
18 |
18 |
|
| 254 |
|
| |
| 255 |
|
| /* Already ours from an earlier take in this same process? */ |
| 256 |
2416 |
▶ | for (i = 0; i < b->nslots; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < b->nslots; i++) { |
2398 |
18 |
2416 |
|
| 257 |
2398 |
| sa_sb_slot *s = &b->slots[i]; |
| 258 |
2398 |
▶ | if (sa_at_load32_acq(&s->state) == SA_SB_LIVE |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&s->state) == SA_SB_LIVE |
11 |
2387 |
2398 |
|
| 259 |
11 |
▶ | && sa_at_load64_acq(&s->pid) == me) { |
Branch: 1 branch
| Branch | True | False | Total |
| && sa_at_load64_acq(&s->pid) == me) { |
0 |
11 |
11 |
|
| 260 |
0 |
| b->mine = (int)i; |
| 261 |
0 |
| b->epoch = sa_at_load64_acq(&s->epoch); |
| 262 |
0 |
| sa_at_unlock(h->locks, hash); |
| 263 |
0 |
| return b->mine; |
| 264 |
|
| } |
| 265 |
|
| } |
| 266 |
|
| /* First empty row; remember the first dead-owner row as a fallback. */ |
| 267 |
29 |
▶ | for (i = 0; i < b->nslots; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < b->nslots; i++) { |
28 |
1 |
29 |
|
| 268 |
28 |
| sa_sb_slot *s = &b->slots[i]; |
| 269 |
28 |
| uint32_t st = sa_at_load32_acq(&s->state); |
| 270 |
28 |
▶ | if (st == SA_SB_EMPTY) { dead = (int)i; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (st == SA_SB_EMPTY) { dead = (int)i; break; } |
17 |
11 |
28 |
|
| 271 |
11 |
▶ | if (dead < 0 && !sa_pid_alive(sa_at_load64_acq(&s->pid))) |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 272 |
1 |
| dead = (int)i; |
| 273 |
|
| } |
| 274 |
18 |
▶ | if (dead < 0) { sa_at_unlock(h->locks, hash); return SA_SB_FULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (dead < 0) { sa_at_unlock(h->locks, hash); return SA_SB_FULL; } |
0 |
18 |
18 |
|
| 275 |
|
| { |
| 276 |
18 |
| sa_sb_slot *s = &b->slots[dead]; |
| 277 |
18 |
| uint64_t ep = sa_at_load64_acq(&s->epoch) + 1; |
| 278 |
|
| uint32_t f; |
| 279 |
18 |
| sa_at_store32_rel(&s->version, sa_at_load32_acq(&s->version) | 1u); |
| 280 |
18 |
| sa_at_fence_rel(); |
| 281 |
18 |
| sa_at_store64_rel(&s->pid, me); |
| 282 |
18 |
| sa_at_store64_rel(&s->epoch, ep); |
| 283 |
18 |
| sa_at_store64_rel(&s->updated, sa_now_ms()); |
| 284 |
162 |
▶ | for (f = 0; f < SA_SB_FIELDS; f++) sa_at_store64_rel(&s->gauges[f], 0); |
Branch: 1 branch
| Branch | True | False | Total |
| for (f = 0; f < SA_SB_FIELDS; f++) sa_at_store64_rel(&s->gauges[f], 0); |
144 |
18 |
162 |
|
| 285 |
18 |
| s->statuslen = 0; |
| 286 |
18 |
| s->status[0] = '\0'; |
| 287 |
18 |
| sa_at_fence_rel(); |
| 288 |
18 |
| sa_at_store32_rel(&s->version, (sa_at_load32_acq(&s->version) + 1u)); |
| 289 |
18 |
| sa_at_store32_rel(&s->state, SA_SB_LIVE); |
| 290 |
18 |
| b->mine = dead; |
| 291 |
18 |
| b->epoch = ep; |
| 292 |
|
| } |
| 293 |
18 |
| sa_at_unlock(h->locks, hash); |
| 294 |
18 |
| return b->mine; |
| 295 |
|
| } |
| 296 |
|
| |
| 297 |
|
| /* Open a write to our own row: version odd, and a barrier so the stores below |
| 298 |
|
| * cannot be seen before the version moves. Returns our row, or NULL if we hold |
| 299 |
|
| * none or a later worker has taken ours over (a stale handle must not write). |
| 300 |
|
| * The caller pairs it with sa_sb_end. Being the only writer to this row, no |
| 301 |
|
| * lock is taken. */ |
| 302 |
814045 |
| static sa_sb_slot *sa_sb_begin(sa_sb *b) { |
| 303 |
|
| sa_sb_slot *s; |
| 304 |
814045 |
▶ | if (!b || b->mine < 0) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b || b->mine < 0) return NULL; |
814045 |
0 |
814045 |
| if (!b || b->mine < 0) return NULL; |
0 |
814045 |
814045 |
|
| 305 |
814045 |
| s = &b->slots[b->mine]; |
| 306 |
814045 |
▶ | if (sa_at_load64_acq(&s->pid) != sa_getpid() |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load64_acq(&s->pid) != sa_getpid() |
814045 |
0 |
814045 |
|
| 307 |
814045 |
▶ | || sa_at_load64_acq(&s->epoch) != b->epoch) |
Branch: 1 branch
| Branch | True | False | Total |
| || sa_at_load64_acq(&s->epoch) != b->epoch) |
0 |
814045 |
814045 |
|
| 308 |
0 |
| return NULL; /* our row was reclaimed: do not write it */ |
| 309 |
814045 |
| sa_at_store32_rel(&s->version, sa_at_load32_acq(&s->version) | 1u); |
| 310 |
814045 |
| sa_at_fence_rel(); |
| 311 |
814045 |
| return s; |
| 312 |
|
| } |
| 313 |
|
| |
| 314 |
814045 |
| static void sa_sb_end(sa_sb *b, sa_sb_slot *s) { |
| 315 |
814045 |
▶ | if (!b || !s) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b || !s) return; |
814045 |
0 |
814045 |
| if (!b || !s) return; |
0 |
814045 |
814045 |
|
| 316 |
814045 |
| sa_at_store64_rel(&s->updated, sa_now_ms()); |
| 317 |
814045 |
| sa_at_fence_rel(); |
| 318 |
814045 |
| sa_at_store32_rel(&s->version, (sa_at_load32_acq(&s->version) + 1u)); |
| 319 |
|
| } |
| 320 |
|
| |
| 321 |
|
| /* Between begin and end. The owner is the only writer, so these are plain |
| 322 |
|
| * release stores with no lock; the seqlock the bracket maintains is what makes |
| 323 |
|
| * the whole set coherent to a reader. */ |
| 324 |
814027 |
| static void sa_sb_set_gauge(sa_sb_slot *s, int idx, uint64_t v) { |
| 325 |
814027 |
▶ | if (s && idx >= 0 && idx < SA_SB_FIELDS) |
Branch: 3 branches
| Branch | True | False | Total |
| if (s && idx >= 0 && idx < SA_SB_FIELDS) |
814027 |
0 |
814027 |
| if (s && idx >= 0 && idx < SA_SB_FIELDS) |
814027 |
0 |
814027 |
| if (s && idx >= 0 && idx < SA_SB_FIELDS) |
814027 |
0 |
814027 |
|
| 326 |
814027 |
| sa_at_store64_rel(&s->gauges[idx], v); |
| 327 |
814027 |
| } |
| 328 |
|
| |
| 329 |
19 |
| static void sa_sb_add_gauge(sa_sb_slot *s, int idx, int64_t by) { |
| 330 |
19 |
▶ | if (s && idx >= 0 && idx < SA_SB_FIELDS) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 331 |
19 |
| sa_at_store64_rel(&s->gauges[idx], |
| 332 |
19 |
| sa_at_load64_acq(&s->gauges[idx]) + (uint64_t)by); |
| 333 |
19 |
| } |
| 334 |
|
| |
| 335 |
814017 |
| static void sa_sb_set_status(sa_sb_slot *s, const char *p, uint32_t n) { |
| 336 |
814017 |
▶ | if (!s) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return; |
0 |
814017 |
814017 |
|
| 337 |
814017 |
▶ | if (n >= SA_SB_STATUS) n = SA_SB_STATUS - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n >= SA_SB_STATUS) n = SA_SB_STATUS - 1; |
0 |
814017 |
814017 |
|
| 338 |
814017 |
▶ | if (n && p) memcpy(s->status, p, n); |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && p) memcpy(s->status, p, n); |
814017 |
0 |
814017 |
| if (n && p) memcpy(s->status, p, n); |
814017 |
0 |
814017 |
|
| 339 |
814017 |
| s->status[n] = '\0'; |
| 340 |
814017 |
| s->statuslen = n; |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
|
| /* Read row `i` coherently. Returns 1 with `out` filled for a live row, 0 for an |
| 344 |
|
| * empty one or one whose writer died mid-update (version stuck odd past the |
| 345 |
|
| * retry bound). `out->alive` says whether the owner is still running. */ |
| 346 |
661975 |
| static int sa_sb_read(sa_sb *b, uint32_t i, sa_sb_reading *out) { |
| 347 |
|
| sa_sb_slot *s; |
| 348 |
|
| long spin; |
| 349 |
|
| |
| 350 |
661975 |
▶ | if (!b || !out || i >= b->nslots) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || !out || i >= b->nslots) return 0; |
661975 |
0 |
661975 |
| if (!b || !out || i >= b->nslots) return 0; |
661975 |
0 |
661975 |
| if (!b || !out || i >= b->nslots) return 0; |
0 |
661975 |
661975 |
|
| 351 |
661975 |
| s = &b->slots[i]; |
| 352 |
661975 |
▶ | if (sa_at_load32_acq(&s->state) != SA_SB_LIVE) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&s->state) != SA_SB_LIVE) return 0; |
497432 |
164543 |
661975 |
|
| 353 |
|
| |
| 354 |
2042513 |
▶ | for (spin = 0; spin < SA_SB_RETRY; spin++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (spin = 0; spin < SA_SB_RETRY; spin++) { |
2041310 |
1203 |
2042513 |
|
| 355 |
2041310 |
| uint32_t v1 = sa_at_load32_acq(&s->version); |
| 356 |
|
| uint32_t f, sl; |
| 357 |
2041310 |
▶ | if (v1 & 1u) continue; /* mid-update: wait a moment */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (v1 & 1u) continue; /* mid-update: wait a moment */ |
1860738 |
180572 |
2041310 |
|
| 358 |
|
| |
| 359 |
180572 |
| out->pid = sa_at_load64_acq(&s->pid); |
| 360 |
180572 |
| out->epoch = sa_at_load64_acq(&s->epoch); |
| 361 |
180572 |
| out->updated = sa_at_load64_acq(&s->updated); |
| 362 |
1625148 |
▶ | for (f = 0; f < SA_SB_FIELDS; f++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (f = 0; f < SA_SB_FIELDS; f++) |
1444576 |
180572 |
1625148 |
|
| 363 |
1444576 |
| out->gauges[f] = sa_at_load64_acq(&s->gauges[f]); |
| 364 |
180572 |
| sl = s->statuslen; |
| 365 |
180572 |
▶ | if (sl >= SA_SB_STATUS) sl = SA_SB_STATUS - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sl >= SA_SB_STATUS) sl = SA_SB_STATUS - 1; |
0 |
180572 |
180572 |
|
| 366 |
180572 |
| memcpy(out->status, s->status, sl); |
| 367 |
180572 |
| out->status[sl] = '\0'; |
| 368 |
180572 |
| out->statuslen = sl; |
| 369 |
|
| |
| 370 |
180572 |
| sa_at_fence_acq(); |
| 371 |
180572 |
▶ | if (sa_at_load32_acq(&s->version) != v1) continue; /* it changed */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&s->version) != v1) continue; /* it changed */ |
17232 |
163340 |
180572 |
|
| 372 |
|
| |
| 373 |
163340 |
| out->alive = sa_pid_alive(out->pid) ? 1 : 0; |
| 374 |
163340 |
| return 1; |
| 375 |
|
| } |
| 376 |
1203 |
| return 0; /* stuck odd: the owner died mid-update */ |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
|
| #else /* !SA_HAVE_ATOMICS */ |
| 380 |
|
| |
| 381 |
|
| /* No atomics: no scoreboard. Defined so the ABI table has something to point |
| 382 |
|
| * at; inert so a build without atomics has an empty board rather than a crash. |
| 383 |
|
| * A publish is a no-op and a read finds no rows, which is what a status page |
| 384 |
|
| * with nothing behind it should show. */ |
| 385 |
|
| static int sa_sb_field(const sa_sb *b, const char *name, uint32_t nlen) { |
| 386 |
|
| (void)b; (void)name; (void)nlen; return -1; |
| 387 |
|
| } |
| 388 |
|
| static sa_sb *sa_sb_bind(sa_region *arena, sa_reg *e, |
| 389 |
|
| const char *const *fields, uint32_t nfields, int *err) { |
| 390 |
|
| (void)arena; (void)e; (void)fields; (void)nfields; |
| 391 |
|
| if (err) *err = SA_E_NOATOMICS; |
| 392 |
|
| return NULL; |
| 393 |
|
| } |
| 394 |
|
| static void sa_sb_free(sa_sb *b) { free(b); } |
| 395 |
|
| static int sa_sb_take(sa_sb *b) { (void)b; return SA_SB_FULL; } |
| 396 |
|
| static sa_sb_slot *sa_sb_begin(sa_sb *b) { (void)b; return NULL; } |
| 397 |
|
| static void sa_sb_end(sa_sb *b, sa_sb_slot *s) { (void)b; (void)s; } |
| 398 |
|
| static void sa_sb_set_gauge(sa_sb_slot *s, int idx, uint64_t v) { |
| 399 |
|
| (void)s; (void)idx; (void)v; |
| 400 |
|
| } |
| 401 |
|
| static void sa_sb_add_gauge(sa_sb_slot *s, int idx, int64_t by) { |
| 402 |
|
| (void)s; (void)idx; (void)by; |
| 403 |
|
| } |
| 404 |
|
| static void sa_sb_set_status(sa_sb_slot *s, const char *p, uint32_t n) { |
| 405 |
|
| (void)s; (void)p; (void)n; |
| 406 |
|
| } |
| 407 |
|
| static int sa_sb_read(sa_sb *b, uint32_t i, sa_sb_reading *out) { |
| 408 |
|
| (void)b; (void)i; (void)out; return 0; |
| 409 |
|
| } |
| 410 |
|
| |
| 411 |
|
| #endif /* SA_HAVE_ATOMICS */ |
| 412 |
|
| |
| 413 |
|
| #endif /* SA_SCOREBOARD_H */ |