xs/scoreboard.xs

filter: Statementstmt 76.0% 89 / 117 Branchbran 50.0% 54 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.5% 143 / 225 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/lease.xs ↑ Summary include/sa/sa_abi_impl.h »
1 # Shared::Arena::Scoreboard - one row per worker, published live.
2 #
3 # Every other shared table has many writers to one structure. This is the
4 # inverse: each worker owns one row and is its only writer, so an update takes
5 # no lock, and a reader - a status page, the supervisor - reads every row in one
6 # pass. Apache's scoreboard, for a fork-shared pool.
7  
8 MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_
9  
10 # $arena->scoreboard($name, fields => ['inflight','served'], slots => 256)
11 #
12 # `fields` names the gauge columns, set once by whoever creates the board; a
13 # later caller names the same ones or inherits them. `slots` is how many workers
14 # it holds; both cost memory for the life of the board.
15 SV *
16 sar_scoreboard(self, name, ...)
17 SV *self
18 SV *name
19 PREINIT:
20 sa_region *arena;
21 sa_reg *e;
22 sa_sb *b;
23 const char *nm;
24 STRLEN nlen;
25 60 UV slots = 0; /* 0 = not given: inherit or default */
26 uint64_t want;
27 const char *fbuf[SA_SB_FIELDS];
28 60 uint32_t nfields = 0;
29 60 int err = SA_E_OK;
30 I32 i;
31 SV *obj;
32 CODE:
33 60 arena = SA_SELF(sa_region, self);
34 60 ▶ if (!arena || !arena->map.base)
Branch: 2 branches
BranchTrueFalseTotal
if (!arena || !arena->map.base) 60 0 60
if (!arena || !arena->map.base) 0 60 60
35 0 croak("Shared::Arena: this region is released");
36 60 nm = SvPV(name, nlen);
37 119 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 59 60 119
38 59 const char *o = SvPV_nolen(ST(i));
39 59 ▶ if (strEQ(o, "slots")) slots = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(o, "slots")) slots = SvUV(ST(i + 1)); 16 43 59
40 43 ▶ else if (strEQ(o, "fields") && SvROK(ST(i + 1))
Branch: 2 branches
BranchTrueFalseTotal
else if (strEQ(o, "fields") && SvROK(ST(i + 1)) 43 0 43
else if (strEQ(o, "fields") && SvROK(ST(i + 1)) 43 0 43
41 43 ▶ && SvTYPE(SvRV(ST(i + 1))) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(i + 1))) == SVt_PVAV) { 43 0 43
42 43 AV *av = (AV *)SvRV(ST(i + 1));
43 43 I32 n = av_len(av) + 1, j;
44 43 ▶ if (n > SA_SB_FIELDS)
Branch: 1 branch
BranchTrueFalseTotal
if (n > SA_SB_FIELDS) 0 43 43
45 0 croak("Shared::Arena: a scoreboard holds at most %d fields",
46 (int)SA_SB_FIELDS);
47 113 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 70 43 113
48 70 SV **el = av_fetch(av, j, 0);
49 140 ▶ fbuf[j] = el ? SvPV_nolen(*el) : "";
Branch: 1 branch
BranchTrueFalseTotal
fbuf[j] = el ? SvPV_nolen(*el) : ""; 70 0 70
50 }
51 43 nfields = (uint32_t)n;
52 }
53 }
54 /* `slots` is inherited, like `fields`: a worker asking for an existing
55 * board by name should not have to know how big the supervisor made it.
56 * If it was not given, bind to whatever is already there; only invent a
57 * default when creating the board fresh. This carves at the requested
58 * size only when a size was actually requested - passing a default here
59 * would make every re-bind a shape mismatch against the real board. */
60 60 ▶ if (slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (slots) { 16 44 60
61 16 ▶ if (slots > SA_SB_SLOTS_MAX) slots = SA_SB_SLOTS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (slots > SA_SB_SLOTS_MAX) slots = SA_SB_SLOTS_MAX; 0 16 16
62 16 want = sa_sb_bytes((uint32_t)slots);
63 }
64 44 ▶ else if (sa_find(arena, nm, (size_t)nlen)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sa_find(arena, nm, (size_t)nlen)) { 26 18 44
65 26 want = 0; /* it exists: find it, do not resize */
66 }
67 else {
68 18 want = sa_sb_bytes(256); /* creating fresh: a default */
69 }
70  
71 60 e = sa_carve(arena, nm, (size_t)nlen, want, SA_T_SCOREBOARD, &err);
72 60 ▶ if (!e) croak("Shared::Arena: the scoreboard '%s' %s", nm,
Branch: 1 branch
BranchTrueFalseTotal
if (!e) croak("Shared::Arena: the scoreboard '%s' %s", nm, 0 60 60
73 sa_strerror(err));
74 #if SA_HAVE_ATOMICS
75 60 b = sa_sb_bind(arena, e, fbuf, nfields, &err);
76 60 ▶ if (!b) croak("Shared::Arena: the scoreboard '%s' %s", nm,
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena: the scoreboard '%s' %s", nm, 9 51 60
77 sa_strerror(err));
78 #else
79 croak("Shared::Arena: scoreboards need atomics this build does not have");
80 #endif
81 51 obj = newSV(0);
82 51 sv_setref_pv(obj, "Shared::Arena::Scoreboard", (void *)b);
83 51 sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0);
84 51 SvREFCNT_inc(SvRV(self));
85 51 RETVAL = obj;
86 OUTPUT:
87 RETVAL
88  
89 MODULE = Shared::Arena PACKAGE = Shared::Arena::Scoreboard PREFIX = sasb_
90  
91 # Claim a row for this process, reclaiming a dead worker's row if the board is
92 # full. Returns the row index, or undef when every row belongs to a live worker
93 # or the claim stripe stayed held through the wait; _last_err on the arena says
94 # which, as it does for a refused carve. Idempotent - call it again in the same
95 # process and it returns the row it already holds. Call it once at worker
96 # start, after the fork.
97 SV *
98 sasb_take(self)
99 SV *self
100 PREINIT:
101 sa_sb *b;
102 int idx;
103 CODE:
104 17 b = SA_SELF(sa_sb, self);
105 17 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 17 17
106 #if SA_HAVE_ATOMICS
107 17 idx = sa_sb_take(b);
108 #else
109 idx = SA_SB_FULL;
110 #endif
111 17 ▶ if (idx < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (idx < 0) 0 17 17
112 0 ▶ b->arena->last_err = (idx == SA_SB_BUSY) ? SA_E_BUSY : SA_E_FULL;
Branch: 1 branch
BranchTrueFalseTotal
b->arena->last_err = (idx == SA_SB_BUSY) ? SA_E_BUSY : SA_E_FULL; 0 0 0
113 17 ▶ RETVAL = (idx < 0) ? &PL_sv_undef : newSViv(idx);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (idx < 0) ? &PL_sv_undef : newSViv(idx); 17 0 17
114 OUTPUT:
115 RETVAL
116  
117 # Our row index, or undef if we hold none.
118 SV *
119 sasb_mine(self)
120 SV *self
121 PREINIT:
122 sa_sb *b;
123 CODE:
124 9 b = SA_SELF(sa_sb, self);
125 9 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 9 9
126 9 ▶ RETVAL = (b->mine < 0) ? &PL_sv_undef : newSViv(b->mine);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (b->mine < 0) ? &PL_sv_undef : newSViv(b->mine); 9 0 9
127 OUTPUT:
128 RETVAL
129  
130 # $sb->update(inflight => 3, served => 128, status => 'GET /x')
131 #
132 # Set gauge fields (by name) and/or the status line, in ONE coherent update: a
133 # reader sees them all as of one instant, never a mix of before and after. An
134 # unknown field name is a croak, not a silent no-op - a typo'd column is a bug.
135 # This is the call to put on the request path.
136 void
137 sasb_update(self, ...)
138 SV *self
139 PREINIT:
140 sa_sb *b;
141 sa_sb_slot *s;
142 int rc;
143 I32 i;
144 CODE:
145 1034425 b = SA_SELF(sa_sb, self);
146 1034425 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 1034425 1034425
147 1034425 ▶ if (b->mine < 0 && (rc = sa_sb_take(b)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b->mine < 0 && (rc = sa_sb_take(b)) < 0) 0 1034425 1034425
if (b->mine < 0 && (rc = sa_sb_take(b)) < 0) 0 0 0
148 0 croak("Shared::Arena::Scoreboard: %s, no row to write",
149 sa_sb_refusal(rc));
150 #if SA_HAVE_ATOMICS
151 1034425 s = sa_sb_begin(b);
152 1034425 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 1034425 0 1034425
153 3103266 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 2068850 1034416 3103266
154 STRLEN kl;
155 2068850 const char *k = SvPV(ST(i), kl);
156 2068850 ▶ if (strEQ(k, "status")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "status")) { 1034416 1034434 2068850
157 STRLEN vl;
158 1034416 const char *v = SvPV(ST(i + 1), vl);
159 1034416 sa_sb_set_status(s, v, (uint32_t)vl);
160 }
161 else {
162 1034434 int fi = sa_sb_field(b, k, (uint32_t)kl);
163 1034434 ▶ if (fi < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fi < 0) { 9 1034425 1034434
164 9 sa_sb_end(b, s);
165 9 croak("Shared::Arena::Scoreboard: no field '%s'", k);
166 }
167 1034425 sa_sb_set_gauge(s, fi, (uint64_t)SvUV(ST(i + 1)));
168 }
169 }
170 1034416 sa_sb_end(b, s);
171 }
172 #endif
173  
174 # $sb->incr(served => 1, bytes => $n) - add to gauge fields, coherent. Negative
175 # steps allowed. Same one-instant snapshot as update.
176 void
177 sasb_incr(self, ...)
178 SV *self
179 PREINIT:
180 sa_sb *b;
181 sa_sb_slot *s;
182 int rc;
183 I32 i;
184 CODE:
185 18 b = SA_SELF(sa_sb, self);
186 18 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 18 18
187 18 ▶ if (b->mine < 0 && (rc = sa_sb_take(b)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b->mine < 0 && (rc = sa_sb_take(b)) < 0) 0 18 18
if (b->mine < 0 && (rc = sa_sb_take(b)) < 0) 0 0 0
188 0 croak("Shared::Arena::Scoreboard: %s, no row to write",
189 sa_sb_refusal(rc));
190 #if SA_HAVE_ATOMICS
191 18 s = sa_sb_begin(b);
192 18 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 18 0 18
193 36 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 18 18 36
194 STRLEN kl;
195 18 const char *k = SvPV(ST(i), kl);
196 18 int fi = sa_sb_field(b, k, (uint32_t)kl);
197 18 ▶ if (fi < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fi < 0) { 0 18 18
198 0 sa_sb_end(b, s);
199 0 croak("Shared::Arena::Scoreboard: no field '%s'", k);
200 }
201 18 sa_sb_add_gauge(s, fi, (int64_t)SvIV(ST(i + 1)));
202 }
203 18 sa_sb_end(b, s);
204 }
205 #endif
206  
207 # $sb->status('idle') - just the status line.
208 void
209 sasb_status(self, str)
210 SV *self
211 SV *str
212 PREINIT:
213 sa_sb *b;
214 sa_sb_slot *s;
215 int rc;
216 const char *p;
217 STRLEN len;
218 CODE:
219 0 b = SA_SELF(sa_sb, self);
220 0 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 0 0
221 0 ▶ if (b->mine < 0 && (rc = sa_sb_take(b)) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b->mine < 0 && (rc = sa_sb_take(b)) < 0) 0 0 0
if (b->mine < 0 && (rc = sa_sb_take(b)) < 0) 0 0 0
222 0 croak("Shared::Arena::Scoreboard: %s, no row to write",
223 sa_sb_refusal(rc));
224 0 p = SvPV(str, len);
225 #if SA_HAVE_ATOMICS
226 0 s = sa_sb_begin(b);
227 0 ▶ if (s) { sa_sb_set_status(s, p, (uint32_t)len); sa_sb_end(b, s); }
Branch: 1 branch
BranchTrueFalseTotal
if (s) { sa_sb_set_status(s, p, (uint32_t)len); sa_sb_end(b, s); } 0 0 0
228 #endif
229  
230 # The whole board, as a list of hashrefs - one per live row, in row order:
231 # { row, pid, epoch, updated, alive, status, <field> => <value>, ... }
232 #
233 # `alive` is whether the owner process is still running; a dead worker's row is
234 # shown with alive => 0 rather than dropped, so a supervisor can see who died.
235 # A row whose writer died MID-update is skipped: it cannot be read coherently
236 # and reporting a torn snapshot would be worse than reporting one fewer worker.
237 void
238 sasb_all(self)
239 SV *self
240 PREINIT:
241 sa_sb *b;
242 uint32_t i, f;
243 PPCODE:
244 225141 b = SA_SELF(sa_sb, self);
245 225141 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 225141 225141
246 #if SA_HAVE_ATOMICS
247 1128015 ▶ for (i = 0; i < b->nslots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nslots; i++) { 902874 225141 1128015
248 sa_sb_reading rd;
249 HV *h;
250 902874 ▶ if (!sa_sb_read(b, i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_sb_read(b, i, &rd)) continue; 678977 223897 902874
251 223897 h = newHV();
252 223897 (void)hv_stores(h, "row", newSVuv((UV)i));
253 223897 (void)hv_stores(h, "pid", newSVuv((UV)rd.pid));
254 223897 (void)hv_stores(h, "epoch", newSVuv((UV)rd.epoch));
255 223897 (void)hv_stores(h, "updated", newSVuv((UV)rd.updated));
256 223897 (void)hv_stores(h, "alive", newSViv(rd.alive));
257 223897 (void)hv_stores(h, "status", newSVpvn(rd.status, rd.statuslen));
258 447803 ▶ for (f = 0; f < b->nfields; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < b->nfields; f++) { 223906 223897 447803
259 223906 const char *fn = b->hdr->fields[f];
260 223906 (void)hv_store(h, fn, (I32)strlen(fn),
261 newSVuv((UV)rd.gauges[f]), 0);
262 }
263 223897 ▶ mXPUSHs(newRV_noinc((SV *)h));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)h)); 0 223897 223897
264 }
265 #endif
266  
267 # The field names, in order.
268 void
269 sasb_fields(self)
270 SV *self
271 PREINIT:
272 sa_sb *b;
273 uint32_t f;
274 PPCODE:
275 18 b = SA_SELF(sa_sb, self);
276 18 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 18 18
277 54 ▶ for (f = 0; f < b->nfields; f++)
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < b->nfields; f++) 36 18 54
278 36 ▶ mXPUSHp(b->hdr->fields[f], strlen(b->hdr->fields[f]));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHp(b->hdr->fields[f], strlen(b->hdr->fields[f])); 0 36 36
279  
280 UV
281 sasb_slots(self)
282 SV *self
283 PREINIT:
284 sa_sb *b;
285 CODE:
286 0 b = SA_SELF(sa_sb, self);
287 0 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 0 0
288 0 ▶ RETVAL = (UV)b->nslots;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)b->nslots; 0 0 0
289 OUTPUT:
290 RETVAL
291  
292 # live / alive / dead - a quick count for a header line: rows claimed, rows
293 # whose owner is running, rows whose owner has died but not yet been reclaimed.
294 void
295 sasb_stats(self)
296 SV *self
297 PREINIT:
298 sa_sb *b;
299 0 uint32_t i, live = 0, alive = 0;
300 PPCODE:
301 0 b = SA_SELF(sa_sb, self);
302 0 ▶ if (!b) croak("Shared::Arena::Scoreboard: this board is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!b) croak("Shared::Arena::Scoreboard: this board is released"); 0 0 0
303 #if SA_HAVE_ATOMICS
304 0 ▶ for (i = 0; i < b->nslots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nslots; i++) { 0 0 0
305 sa_sb_reading rd;
306 0 ▶ if (!sa_sb_read(b, i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_sb_read(b, i, &rd)) continue; 0 0 0
307 0 live++;
308 0 ▶ if (rd.alive) alive++;
Branch: 1 branch
BranchTrueFalseTotal
if (rd.alive) alive++; 0 0 0
309 }
310 #endif
311 0 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 0 0
312 0 mPUSHp("slots", 5); mPUSHu((UV)b->nslots);
313 0 mPUSHp("live", 4); mPUSHu((UV)live);
314 0 mPUSHp("alive", 5); mPUSHu((UV)alive);
315  
316 void
317 sasb_DESTROY(self)
318 SV *self
319 PREINIT:
320 sa_sb *b;
321 CODE:
322 51 b = SA_SELF(sa_sb, self);
323 /* A handle going out of scope leaves this process's row where it is -
324 * the row belongs to the process, not the handle, and a supervisor
325 * still reads it until the process exits and a successor reclaims it. */
326 51 ▶ if (b) { sa_sb_free(b); sv_setiv(SvRV(self), 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (b) { sa_sb_free(b); sv_setiv(SvRV(self), 0); } 51 0 51
« xs/lease.xs ↑ Summary include/sa/sa_abi_impl.h »