xs/scoreboard.xs

filter: Statementstmt 76.5% 88 / 115 Branchbran 50.9% 53 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 64.3% 141 / 219 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 # Idempotent - call it again in the same process and it returns the row it
94 # already holds. Call it once at worker start, after the fork.
95 SV *
96 sasb_take(self)
97 SV *self
98 PREINIT:
99 sa_sb *b;
100 int idx;
101 CODE:
102 17 b = SA_SELF(sa_sb, self);
103 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
104 #if SA_HAVE_ATOMICS
105 17 idx = sa_sb_take(b);
106 #else
107 idx = -1;
108 #endif
109 17 ▶ RETVAL = (idx < 0) ? &PL_sv_undef : newSViv(idx);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (idx < 0) ? &PL_sv_undef : newSViv(idx); 17 0 17
110 OUTPUT:
111 RETVAL
112  
113 # Our row index, or undef if we hold none.
114 SV *
115 sasb_mine(self)
116 SV *self
117 PREINIT:
118 sa_sb *b;
119 CODE:
120 9 b = SA_SELF(sa_sb, self);
121 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
122 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
123 OUTPUT:
124 RETVAL
125  
126 # $sb->update(inflight => 3, served => 128, status => 'GET /x')
127 #
128 # Set gauge fields (by name) and/or the status line, in ONE coherent update: a
129 # reader sees them all as of one instant, never a mix of before and after. An
130 # unknown field name is a croak, not a silent no-op - a typo'd column is a bug.
131 # This is the call to put on the request path.
132 void
133 sasb_update(self, ...)
134 SV *self
135 PREINIT:
136 sa_sb *b;
137 sa_sb_slot *s;
138 I32 i;
139 CODE:
140 1110625 b = SA_SELF(sa_sb, self);
141 1110625 ▶ 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 1110625 1110625
142 1110625 ▶ if (b->mine < 0 && sa_sb_take(b) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b->mine < 0 && sa_sb_take(b) < 0) 0 1110625 1110625
if (b->mine < 0 && sa_sb_take(b) < 0) 0 0 0
143 0 croak("Shared::Arena::Scoreboard: the board is full, no row to write");
144 #if SA_HAVE_ATOMICS
145 1110625 s = sa_sb_begin(b);
146 1110625 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 1110625 0 1110625
147 3331866 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 2221250 1110616 3331866
148 STRLEN kl;
149 2221250 const char *k = SvPV(ST(i), kl);
150 2221250 ▶ if (strEQ(k, "status")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "status")) { 1110616 1110634 2221250
151 STRLEN vl;
152 1110616 const char *v = SvPV(ST(i + 1), vl);
153 1110616 sa_sb_set_status(s, v, (uint32_t)vl);
154 }
155 else {
156 1110634 int fi = sa_sb_field(b, k, (uint32_t)kl);
157 1110634 ▶ if (fi < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fi < 0) { 9 1110625 1110634
158 9 sa_sb_end(b, s);
159 9 croak("Shared::Arena::Scoreboard: no field '%s'", k);
160 }
161 1110625 sa_sb_set_gauge(s, fi, (uint64_t)SvUV(ST(i + 1)));
162 }
163 }
164 1110616 sa_sb_end(b, s);
165 }
166 #endif
167  
168 # $sb->incr(served => 1, bytes => $n) - add to gauge fields, coherent. Negative
169 # steps allowed. Same one-instant snapshot as update.
170 void
171 sasb_incr(self, ...)
172 SV *self
173 PREINIT:
174 sa_sb *b;
175 sa_sb_slot *s;
176 I32 i;
177 CODE:
178 18 b = SA_SELF(sa_sb, self);
179 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
180 18 ▶ if (b->mine < 0 && sa_sb_take(b) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b->mine < 0 && sa_sb_take(b) < 0) 0 18 18
if (b->mine < 0 && sa_sb_take(b) < 0) 0 0 0
181 0 croak("Shared::Arena::Scoreboard: the board is full, no row to write");
182 #if SA_HAVE_ATOMICS
183 18 s = sa_sb_begin(b);
184 18 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 18 0 18
185 36 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 18 18 36
186 STRLEN kl;
187 18 const char *k = SvPV(ST(i), kl);
188 18 int fi = sa_sb_field(b, k, (uint32_t)kl);
189 18 ▶ if (fi < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fi < 0) { 0 18 18
190 0 sa_sb_end(b, s);
191 0 croak("Shared::Arena::Scoreboard: no field '%s'", k);
192 }
193 18 sa_sb_add_gauge(s, fi, (int64_t)SvIV(ST(i + 1)));
194 }
195 18 sa_sb_end(b, s);
196 }
197 #endif
198  
199 # $sb->status('idle') - just the status line.
200 void
201 sasb_status(self, str)
202 SV *self
203 SV *str
204 PREINIT:
205 sa_sb *b;
206 sa_sb_slot *s;
207 const char *p;
208 STRLEN len;
209 CODE:
210 0 b = SA_SELF(sa_sb, self);
211 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
212 0 ▶ if (b->mine < 0 && sa_sb_take(b) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (b->mine < 0 && sa_sb_take(b) < 0) 0 0 0
if (b->mine < 0 && sa_sb_take(b) < 0) 0 0 0
213 0 croak("Shared::Arena::Scoreboard: the board is full, no row to write");
214 0 p = SvPV(str, len);
215 #if SA_HAVE_ATOMICS
216 0 s = sa_sb_begin(b);
217 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
218 #endif
219  
220 # The whole board, as a list of hashrefs - one per live row, in row order:
221 # { row, pid, epoch, updated, alive, status, <field> => <value>, ... }
222 #
223 # `alive` is whether the owner process is still running; a dead worker's row is
224 # shown with alive => 0 rather than dropped, so a supervisor can see who died.
225 # A row whose writer died MID-update is skipped: it cannot be read coherently
226 # and reporting a torn snapshot would be worse than reporting one fewer worker.
227 void
228 sasb_all(self)
229 SV *self
230 PREINIT:
231 sa_sb *b;
232 uint32_t i, f;
233 PPCODE:
234 194013 b = SA_SELF(sa_sb, self);
235 194013 ▶ 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 194013 194013
236 #if SA_HAVE_ATOMICS
237 972375 ▶ for (i = 0; i < b->nslots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nslots; i++) { 778362 194013 972375
238 sa_sb_reading rd;
239 HV *h;
240 778362 ▶ if (!sa_sb_read(b, i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_sb_read(b, i, &rd)) continue; 584864 193498 778362
241 193498 h = newHV();
242 193498 (void)hv_stores(h, "row", newSVuv((UV)i));
243 193498 (void)hv_stores(h, "pid", newSVuv((UV)rd.pid));
244 193498 (void)hv_stores(h, "epoch", newSVuv((UV)rd.epoch));
245 193498 (void)hv_stores(h, "updated", newSVuv((UV)rd.updated));
246 193498 (void)hv_stores(h, "alive", newSViv(rd.alive));
247 193498 (void)hv_stores(h, "status", newSVpvn(rd.status, rd.statuslen));
248 387005 ▶ for (f = 0; f < b->nfields; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < b->nfields; f++) { 193507 193498 387005
249 193507 const char *fn = b->hdr->fields[f];
250 193507 (void)hv_store(h, fn, (I32)strlen(fn),
251 newSVuv((UV)rd.gauges[f]), 0);
252 }
253 193498 ▶ mXPUSHs(newRV_noinc((SV *)h));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)h)); 0 193498 193498
254 }
255 #endif
256  
257 # The field names, in order.
258 void
259 sasb_fields(self)
260 SV *self
261 PREINIT:
262 sa_sb *b;
263 uint32_t f;
264 PPCODE:
265 18 b = SA_SELF(sa_sb, self);
266 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
267 54 ▶ for (f = 0; f < b->nfields; f++)
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < b->nfields; f++) 36 18 54
268 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
269  
270 UV
271 sasb_slots(self)
272 SV *self
273 PREINIT:
274 sa_sb *b;
275 CODE:
276 0 b = SA_SELF(sa_sb, self);
277 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
278 0 ▶ RETVAL = (UV)b->nslots;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)b->nslots; 0 0 0
279 OUTPUT:
280 RETVAL
281  
282 # live / alive / dead - a quick count for a header line: rows claimed, rows
283 # whose owner is running, rows whose owner has died but not yet been reclaimed.
284 void
285 sasb_stats(self)
286 SV *self
287 PREINIT:
288 sa_sb *b;
289 0 uint32_t i, live = 0, alive = 0;
290 PPCODE:
291 0 b = SA_SELF(sa_sb, self);
292 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
293 #if SA_HAVE_ATOMICS
294 0 ▶ for (i = 0; i < b->nslots; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->nslots; i++) { 0 0 0
295 sa_sb_reading rd;
296 0 ▶ if (!sa_sb_read(b, i, &rd)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!sa_sb_read(b, i, &rd)) continue; 0 0 0
297 0 live++;
298 0 ▶ if (rd.alive) alive++;
Branch: 1 branch
BranchTrueFalseTotal
if (rd.alive) alive++; 0 0 0
299 }
300 #endif
301 0 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 0 0
302 0 mPUSHp("slots", 5); mPUSHu((UV)b->nslots);
303 0 mPUSHp("live", 4); mPUSHu((UV)live);
304 0 mPUSHp("alive", 5); mPUSHu((UV)alive);
305  
306 void
307 sasb_DESTROY(self)
308 SV *self
309 PREINIT:
310 sa_sb *b;
311 CODE:
312 51 b = SA_SELF(sa_sb, self);
313 /* A handle going out of scope leaves this process's row where it is -
314 * the row belongs to the process, not the handle, and a supervisor
315 * still reads it until the process exits and a successor reclaims it. */
316 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 »