include/sa/sa_abi_impl.h

filter: Statementstmt 83.1% 653 / 785 Branchbran 51.0% 448 / 878 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.2% 1101 / 1663 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/scoreboard.xs ↑ Summary include/sa/sa_xop.h »
1 #ifndef SA_ABI_IMPL_H
2 #define SA_ABI_IMPL_H
3  
4 /* The table behind Shared::Arena::_abi_ptr. Private to this build; consumers
5 * see only include/sa_abi.h and resolve the address at runtime.
6 *
7 * Every entry points at a core function, wrapped only where the ABI's signature
8 * differs from the core's - an out parameter where the core returns a struct
9 * pointer, a size_t where the core takes its own type. Nothing is IMPLEMENTED
10 * here: logic in this file means a core function has the wrong shape.
11 *
12 * Needs sa_abi.h and every core header the entries name.
13 */
14  
15 /* ---- the two ends of every shared constant --------------------------------
16 *
17 * sa_abi.h is a PUBLIC copy of numbers the core also defines, and the two are
18 * different files: an edit to one and not the other is silent, and the symptom
19 * is a consumer reading a different answer from the same bytes. These make it a
20 * build failure instead. The array-size trick rather than static_assert, which
21 * is C11 and this dist compiles as C89. */
22 typedef char sa_abi_assert_map[
23 (SA_MAP_HIT == SA_H_HIT && SA_MAP_MISS == SA_H_MISS
24 && SA_MAP_BUSY == SA_H_BUSY) ? 1 : -1];
25 typedef char sa_abi_assert_pub[
26 (SA_NO_RING == SA_PUB_NORING && SA_TOO_BIG == SA_PUB_OVERSIZE) ? 1 : -1];
27 /* SA_READ_* is one set of names in both headers, guarded in sa_ring.h, so the
28 * pin is against the published numbers themselves. */
29 typedef char sa_abi_assert_read[
30 (SA_READ_OK == 0 && SA_READ_PENDING == 1 && SA_READ_LAPPED == 2) ? 1 : -1];
31 typedef char sa_abi_assert_store[
32 (SA_MAP_STORE_OK == SA_H_OK && SA_MAP_STORE_FULL == SA_H_FULL
33 && SA_MAP_STORE_TOOBIG == SA_H_TOOBIG
34 && SA_MAP_STORE_NOTNUM == SA_H_NOTNUM
35 && SA_MAP_STORE_BUSY == SA_H_WBUSY) ? 1 : -1];
36 typedef char sa_abi_assert_cache[
37 (SA_CACHE_OK == SA_C_OK && SA_CACHE_TOOBIG == SA_C_TOOBIG) ? 1 : -1];
38 typedef char sa_abi_assert_err[
39 (SA_ERR_OK == SA_E_OK && SA_ERR_MAGIC == SA_E_MAGIC
40 && SA_ERR_LAYOUT == SA_E_LAYOUT && SA_ERR_HDRSIZE == SA_E_HDRSIZE
41 && SA_ERR_ENDIAN == SA_E_ENDIAN && SA_ERR_WORD == SA_E_WORD
42 && SA_ERR_SHORT == SA_E_SHORT && SA_ERR_NOMEM == SA_E_NOMEM
43 && SA_ERR_MAP == SA_E_MAP && SA_ERR_EXISTS == SA_E_EXISTS
44 && SA_ERR_NOENT == SA_E_NOENT && SA_ERR_NAME == SA_E_NAME
45 && SA_ERR_FULL == SA_E_FULL && SA_ERR_NOATOMICS == SA_E_NOATOMICS
46 && SA_ERR_SHAPE == SA_E_SHAPE && SA_ERR_BUSY == SA_E_BUSY) ? 1 : -1];
47 typedef char sa_abi_assert_type[
48 (SA_TYPE_RAW == SA_T_RAW && SA_TYPE_RING == SA_T_RING
49 && SA_TYPE_MAP == SA_T_MAP && SA_TYPE_BLOOM == SA_T_BLOOM
50 && SA_TYPE_HIST == SA_T_HIST && SA_TYPE_CACHE == SA_T_CACHE
51 && SA_TYPE_RATE == SA_T_RATE && SA_TYPE_CMS == SA_T_CMS
52 && SA_TYPE_FROZEN == SA_T_FROZEN && SA_TYPE_CUCKOO == SA_T_CUCKOO
53 && SA_TYPE_LEASE == SA_T_LEASE && SA_TYPE_SCOREBOARD == SA_T_SCOREBOARD
54 && SA_TYPE_HLL == SA_T_HLL) ? 1 : -1];
55  
56 1 static void sa_abi_config_init(sa_config *cfg) {
57 1 ▶ if (!cfg) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) return; 0 1 1
58 1 cfg->bytes = 1024 * 1024;
59 1 cfg->regions = SA_REGIONS_MAX;
60 1 cfg->name = NULL;
61 1 cfg->nlen = 0;
62 }
63  
64 1 static sa_region *sa_abi_create(const sa_config *cfg, int *err) {
65 sa_config d;
66 1 ▶ if (!cfg) { sa_abi_config_init(&d); cfg = &d; }
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) { sa_abi_config_init(&d); cfg = &d; } 0 1 1
67 1 return sa_region_open(cfg->name, cfg->nlen,
68 1 cfg->bytes + sa_header_bytes(cfg->regions),
69 1 cfg->regions, 1, err);
70 }
71  
72 1 static sa_region *sa_abi_attach(const char *name, size_t nlen, int *err) {
73 1 return sa_region_open(name, nlen,
74 sa_header_bytes(SA_REGIONS_MAX),
75 SA_REGIONS_MAX, 0, err);
76 }
77  
78 1 static void sa_abi_release(sa_region *r) {
79 1 sa_wake_leave(r);
80 1 sa_peer_leave(r);
81 1 sa_region_release(r);
82 1 }
83  
84 0 static int sa_abi_destroy(const char *name, size_t nlen) {
85 0 return sa_region_destroy(name, nlen);
86 }
87  
88 2 static uint64_t sa_abi_bytes(const sa_region *r) {
89 2 ▶ return r ? r->total : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? r->total : 0; 2 0 2
90 }
91  
92 1 static int sa_abi_is_creator(const sa_region *r) {
93 1 ▶ return (r && r->created) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (r && r->created) ? 1 : 0; 1 0 1
return (r && r->created) ? 1 : 0; 1 0 1
94 }
95  
96 2 static uint64_t sa_abi_carve(sa_region *r, const char *name, size_t nlen,
97 uint64_t bytes, uint32_t type,
98 uint64_t *out_bytes, int *err)
99 {
100 2 sa_reg *e = sa_carve(r, name, nlen, bytes, type, err);
101 2 ▶ if (!e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 0; 0 2 2
102 2 ▶ if (out_bytes) *out_bytes = e->len;
Branch: 1 branch
BranchTrueFalseTotal
if (out_bytes) *out_bytes = e->len; 1 1 2
103 2 return e->off;
104 }
105  
106 2 static uint64_t sa_abi_locate(sa_region *r, const char *name, size_t nlen,
107 uint64_t *out_bytes)
108 {
109 2 sa_reg *e = sa_find(r, name, nlen);
110 2 ▶ if (!e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 0; 1 1 2
111 1 ▶ if (out_bytes) *out_bytes = e->len;
Branch: 1 branch
BranchTrueFalseTotal
if (out_bytes) *out_bytes = e->len; 0 1 1
112 1 return e->off;
113 }
114  
115 2 static sa_ring *sa_abi_ring_open(sa_region *r, const char *name, size_t nlen,
116 uint64_t slots, uint32_t slot_size, int *err)
117 {
118 sa_reg *e;
119 2 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 2 0 2
120 2 e = sa_carve(r, name, nlen, sa_ring_bytes(slots, slot_size),
121 SA_T_RING, err);
122 2 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 2 2
123 2 return sa_ring_bind(r, e, slots, slot_size, err);
124 }
125  
126 114 static int64_t sa_abi_publish(sa_ring *r, const char *topic, uint32_t tlen,
127 const char *data, uint32_t dlen)
128 {
129 114 uint64_t seq = 0;
130 114 int rc = sa_ring_publish(r, topic, tlen, data, dlen, &seq);
131 114 ▶ return (rc == SA_PUB_OK) ? (int64_t)seq : (int64_t)rc;
Branch: 1 branch
BranchTrueFalseTotal
return (rc == SA_PUB_OK) ? (int64_t)seq : (int64_t)rc; 1 113 114
132 }
133  
134 4 static uint64_t sa_abi_max_record(const sa_ring *r) {
135 /* The SPANNED maximum, which is what a caller needs before it publishes,
136 * and the same number the Perl surface reports. Returning the per-slot
137 * capacity here instead is a disagreement between the two doors that no
138 * compiler can see - the selftest caught exactly that. */
139 4 ▶ return r ? r->record_max : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? r->record_max : 0; 4 0 4
140 }
141  
142 3 static uint64_t sa_abi_slot_bytes(const sa_ring *r) {
143 3 ▶ return r ? r->payload_max : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? r->payload_max : 0; 3 0 3
144 }
145  
146 1 static uint64_t sa_abi_ring_slots(const sa_ring *r) {
147 1 ▶ return r ? r->nslots : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? r->nslots : 0; 1 0 1
148 }
149  
150 3 static uint64_t sa_abi_ring_position(const sa_ring *r) {
151 #if SA_HAVE_ATOMICS
152 3 ▶ return r ? sa_at_load64_acq(&r->hdr->seq) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return r ? sa_at_load64_acq(&r->hdr->seq) : 0; 3 0 3
153 #else
154 (void)r;
155 return 0;
156 #endif
157 }
158  
159 2 static void sa_abi_counts(const sa_cursor *c, sa_counts *out) {
160 2 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 2 2
161 2 ▶ if (!c) { memset(out, 0, sizeof *out); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!c) { memset(out, 0, sizeof *out); return; } 0 2 2
162 2 out->delivered = c->delivered;
163 2 out->lapped = c->lapped;
164 2 out->abandoned = c->abandoned;
165 2 out->unattributed = c->unattributed;
166 2 out->position = c->seq;
167 }
168  
169 3 static sa_hash *sa_abi_map_open(sa_region *r, const char *name, size_t nlen,
170 uint64_t slots, uint32_t slot_size, int *err)
171 {
172 sa_reg *e;
173 3 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 3 0 3
174 3 e = sa_carve(r, name, nlen, sa_hash_bytes(slots, slot_size),
175 SA_T_MAP, err);
176 3 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 3 3
177 3 return sa_hash_bind(r, e, slots, slot_size, SA_SER_ANY, err);
178 }
179  
180 2 static void sa_abi_map_counts(const sa_hash *m, sa_map_counts *out) {
181 2 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 2 2
182 2 ▶ if (!m) { memset(out, 0, sizeof *out); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!m) { memset(out, 0, sizeof *out); return; } 0 2 2
183 2 out->used = sa_at_load64_acq(&m->hdr->used);
184 2 out->capacity = m->nslots;
185 2 out->tombstones = sa_at_load64_acq(&m->hdr->tombstones);
186 2 out->busy = sa_at_load64_acq(&m->hdr->busy);
187 2 out->full = sa_at_load64_acq(&m->hdr->full);
188 2 out->expired = sa_at_load64_acq(&m->hdr->expired);
189 }
190  
191 1 static uint64_t sa_abi_map_pair_max(const sa_hash *m) {
192 1 ▶ return m ? m->pair_max : 0;
Branch: 1 branch
BranchTrueFalseTotal
return m ? m->pair_max : 0; 1 0 1
193 }
194  
195 1 static uint64_t sa_abi_map_capacity(const sa_hash *m) {
196 1 ▶ return m ? m->nslots : 0;
Branch: 1 branch
BranchTrueFalseTotal
return m ? m->nslots : 0; 1 0 1
197 }
198  
199 1 static sa_bloom *sa_abi_bloom_open(sa_region *r, const char *name, size_t nlen,
200 uint64_t capacity, double fp_rate,
201 uint64_t bits, uint32_t hashes, int *err)
202 {
203 sa_reg *e;
204 1 uint64_t nbits = 0;
205 1 uint32_t k = 0;
206  
207 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
208 1 ▶ if (bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits) { 0 1 1
209 0 nbits = sa_bloom_words(bits) * 64u;
210 0 ▶ k = hashes ? hashes : 5;
Branch: 1 branch
BranchTrueFalseTotal
k = hashes ? hashes : 5; 0 0 0
211 }
212 else {
213 1 sa_bloom_size(capacity, fp_rate, &nbits, &k);
214 1 ▶ if (hashes) k = hashes;
Branch: 1 branch
BranchTrueFalseTotal
if (hashes) k = hashes; 0 1 1
215 }
216 1 ▶ if (k > SA_BLOOM_MAX_K) k = SA_BLOOM_MAX_K;
Branch: 1 branch
BranchTrueFalseTotal
if (k > SA_BLOOM_MAX_K) k = SA_BLOOM_MAX_K; 0 1 1
217  
218 1 e = sa_carve(r, name, nlen, sa_bloom_bytes(nbits), SA_T_BLOOM, err);
219 1 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 1 1
220 {
221 1 sa_bloom *b = sa_bloom_bind(r, e, nbits, k, err);
222 1 ▶ if (b) b->hdr->capacity = capacity;
Branch: 1 branch
BranchTrueFalseTotal
if (b) b->hdr->capacity = capacity; 1 0 1
223 1 return b;
224 }
225 }
226  
227 1 ▶static uint64_t sa_abi_bloom_bits(const sa_bloom *b) { return b ? b->nbits : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint64_t sa_abi_bloom_bits(const sa_bloom *b) { return b ? b->nbits : 0; } 1 0 1
228 1 ▶static uint32_t sa_abi_bloom_hashes(const sa_bloom *b) { return b ? b->nhash : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint32_t sa_abi_bloom_hashes(const sa_bloom *b) { return b ? b->nhash : 0; } 1 0 1
229  
230 1 static sa_hist *sa_abi_hist_open(sa_region *r, const char *name, size_t nlen,
231 uint64_t max_value, uint32_t sigbits,
232 int *err)
233 {
234 sa_reg *e;
235 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
236 1 ▶ if (!sigbits) sigbits = 4;
Branch: 1 branch
BranchTrueFalseTotal
if (!sigbits) sigbits = 4; 0 1 1
237 1 e = sa_carve(r, name, nlen, sa_hist_bytes(max_value, sigbits),
238 SA_T_HIST, err);
239 1 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 1 1
240 1 return sa_hist_bind(r, e, max_value, sigbits, err);
241 }
242  
243 3 static void sa_abi_hist_counts(const sa_hist *h, sa_hist_counts *out) {
244 3 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 3 3
245 3 ▶ if (!h) { memset(out, 0, sizeof *out); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!h) { memset(out, 0, sizeof *out); return; } 0 3 3
246 3 out->count = sa_at_load64_acq(&h->hdr->count);
247 3 out->sum = sa_at_load64_acq(&h->hdr->sum);
248 3 ▶ out->min = out->count ? sa_at_load64_acq(&h->hdr->min) : 0;
Branch: 1 branch
BranchTrueFalseTotal
out->min = out->count ? sa_at_load64_acq(&h->hdr->min) : 0; 2 1 3
249 3 out->max = sa_at_load64_acq(&h->hdr->max);
250 3 out->over = sa_at_load64_acq(&h->hdr->over);
251 3 out->nbuckets = h->nbuckets;
252 3 out->sigbits = h->sigbits;
253 }
254  
255 1 static uint64_t sa_abi_hist_low(const sa_hist *h, uint64_t i) {
256 1 ▶ return (h && i < h->nbuckets) ? sa_hist_low(i, h->sigbits) : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (h && i < h->nbuckets) ? sa_hist_low(i, h->sigbits) : 0; 1 0 1
return (h && i < h->nbuckets) ? sa_hist_low(i, h->sigbits) : 0; 1 0 1
257 }
258 1 static uint64_t sa_abi_hist_high(const sa_hist *h, uint64_t i) {
259 1 ▶ return (h && i < h->nbuckets) ? sa_hist_high(i, h->sigbits) : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (h && i < h->nbuckets) ? sa_hist_high(i, h->sigbits) : 0; 1 0 1
return (h && i < h->nbuckets) ? sa_hist_high(i, h->sigbits) : 0; 1 0 1
260 }
261 1 static uint64_t sa_abi_hist_count(const sa_hist *h, uint64_t i) {
262 1 ▶ return (h && i < h->nbuckets) ? sa_at_load64_acq(&h->buckets[i]) : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (h && i < h->nbuckets) ? sa_at_load64_acq(&h->buckets[i]) : 0; 1 0 1
return (h && i < h->nbuckets) ? sa_at_load64_acq(&h->buckets[i]) : 0; 1 0 1
263 }
264  
265 1 static sa_cache *sa_abi_cache_open(sa_region *r, const char *name, size_t nlen,
266 uint64_t capacity, uint32_t ways,
267 uint32_t entry_size, int *err)
268 {
269 sa_reg *e;
270 uint64_t nbuckets;
271 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
272 1 ▶ if (!ways) ways = 8;
Branch: 1 branch
BranchTrueFalseTotal
if (!ways) ways = 8; 0 1 1
273 1 ▶ if (ways > SA_CACHE_MAX_WAYS) ways = SA_CACHE_MAX_WAYS;
Branch: 1 branch
BranchTrueFalseTotal
if (ways > SA_CACHE_MAX_WAYS) ways = SA_CACHE_MAX_WAYS; 0 1 1
274 1 ▶ if (capacity < ways) capacity = ways;
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < ways) capacity = ways; 0 1 1
275 1 nbuckets = (capacity + ways - 1) / ways;
276 1 e = sa_carve(r, name, nlen, sa_cache_bytes(nbuckets, ways, entry_size),
277 SA_T_CACHE, err);
278 1 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 1 1
279 1 return sa_cache_bind(r, e, nbuckets, ways, entry_size, SA_SER_ANY, err);
280 }
281  
282 2 static void sa_abi_cache_counts(sa_cache *c, sa_cache_counts *out) {
283 2 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 2 2
284 2 ▶ if (!c) { memset(out, 0, sizeof *out); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!c) { memset(out, 0, sizeof *out); return; } 0 2 2
285 2 sa_cache_flush(c); /* this process's own counts, exactly */
286 2 out->hits = sa_at_load64_acq(&c->hdr->hits);
287 2 out->misses = sa_at_load64_acq(&c->hdr->misses);
288 2 out->evictions = sa_at_load64_acq(&c->hdr->evictions);
289 2 out->expired = sa_at_load64_acq(&c->hdr->expired);
290 2 out->live = sa_cache_live(c);
291 2 out->capacity = c->nbuckets * c->nways;
292 2 out->ways = c->nways;
293 }
294  
295 1 static uint64_t sa_abi_cache_pair_max(const sa_cache *c) {
296 1 ▶ return c ? c->pair_max : 0;
Branch: 1 branch
BranchTrueFalseTotal
return c ? c->pair_max : 0; 1 0 1
297 }
298  
299 /* ---- the rate limiter ------------------------------------------------------
300 *
301 * The Perl surface scales `limit` into 1/1024ths and rounds `slots` up to a
302 * power of two; a C consumer must get the same limiter for the same numbers,
303 * so the conversion lives here rather than in either caller. */
304 2 static sa_rate *sa_abi_rate_open(sa_region *r, const char *name, size_t nlen,
305 uint64_t limit, uint64_t window_ms,
306 uint64_t slots, int *err)
307 {
308 sa_reg *e;
309 uint64_t nslots, burst_u;
310  
311 2 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 2 0 2
312 2 ▶ if (!limit) limit = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!limit) limit = 1; 0 2 2
313 2 ▶ if (limit > 0x3FFFFFu) limit = 0x3FFFFFu;
Branch: 1 branch
BranchTrueFalseTotal
if (limit > 0x3FFFFFu) limit = 0x3FFFFFu; 0 2 2
314 2 ▶ if (!window_ms) window_ms = 60000;
Branch: 1 branch
BranchTrueFalseTotal
if (!window_ms) window_ms = 60000; 0 2 2
315 2 burst_u = limit * SA_RATE_SCALE;
316 2 ▶ nslots = sa_rate_pow2(slots ? slots : 4096);
Branch: 1 branch
BranchTrueFalseTotal
nslots = sa_rate_pow2(slots ? slots : 4096); 2 0 2
317  
318 2 e = sa_carve(r, name, nlen, sa_rate_bytes(nslots), SA_T_RATE, err);
319 2 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 2 2
320 2 return sa_rate_bind(r, e, nslots, burst_u, window_ms, err);
321 }
322  
323 /* Whole requests in, whole requests out: the 1/1024ths are this dist's
324 * business and not a consumer's. */
325 10 static int sa_abi_rate_allow(sa_rate *rl, const char *key, uint32_t klen,
326 uint64_t cost, uint64_t *left, uint64_t *retry_ms)
327 {
328 10 uint64_t l = 0;
329 int ok;
330 10 ▶ if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; } 0 10 10
if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; } 0 0 0
if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; } 0 0 0
331 10 ▶ if (!cost) cost = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!cost) cost = 1; 0 10 10
332 10 ok = sa_rate_take(rl, key, klen, cost * SA_RATE_SCALE, 0, &l, retry_ms);
333 10 ▶ if (left) *left = l / SA_RATE_SCALE;
Branch: 1 branch
BranchTrueFalseTotal
if (left) *left = l / SA_RATE_SCALE; 10 0 10
334 10 return ok;
335 }
336  
337 1 static int sa_abi_rate_peek(sa_rate *rl, const char *key, uint32_t klen,
338 uint64_t *left, uint64_t *retry_ms)
339 {
340 1 uint64_t l = 0;
341 int ok;
342 1 ▶ if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; }
Branch: 3 branches
BranchTrueFalseTotal
if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; } 0 1 1
if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; } 0 0 0
if (!rl) { if (left) *left = 0; if (retry_ms) *retry_ms = 0; return 1; } 0 0 0
343 1 ok = sa_rate_take(rl, key, klen, SA_RATE_SCALE, 1, &l, retry_ms);
344 1 ▶ if (left) *left = l / SA_RATE_SCALE;
Branch: 1 branch
BranchTrueFalseTotal
if (left) *left = l / SA_RATE_SCALE; 1 0 1
345 1 return ok;
346 }
347  
348 1 static uint64_t sa_abi_rate_slots(const sa_rate *rl) {
349 1 ▶ return rl ? rl->nslots : 0;
Branch: 1 branch
BranchTrueFalseTotal
return rl ? rl->nslots : 0; 1 0 1
350 }
351  
352 1 static uint64_t sa_abi_rate_used(sa_rate *rl) {
353 1 ▶ return rl ? sa_rate_used(rl) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return rl ? sa_rate_used(rl) : 0; 1 0 1
354 }
355  
356 2 static void sa_abi_rate_counts(sa_rate *rl, sa_rate_counts *out) {
357 2 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 2 2
358 2 ▶ if (!rl) { memset(out, 0, sizeof *out); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rl) { memset(out, 0, sizeof *out); return; } 1 1 2
359 1 out->allowed = sa_at_load64_acq(&rl->hdr->allowed);
360 1 out->denied = sa_at_load64_acq(&rl->hdr->denied);
361 1 out->evicted = sa_at_load64_acq(&rl->hdr->evicted);
362 1 out->contended = sa_at_load64_acq(&rl->hdr->contended);
363 }
364  
365 /* ---- the count-min sketch --------------------------------------------------
366 *
367 * The Perl surface rounds the width up to a power of two and derives the shape
368 * from error and confidence; a C consumer must get the same sketch for the
369 * same numbers, so the conversion lives here rather than in either caller. */
370 1 static sa_cms *sa_abi_cms_open(sa_region *r, const char *name, size_t nlen,
371 double error, double confidence,
372 uint32_t rows, uint64_t width, int *err)
373 {
374 sa_reg *e;
375 1 uint32_t d = 0;
376 1 uint64_t w = 0;
377  
378 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
379 1 ▶ if (rows || width) {
Branch: 2 branches
BranchTrueFalseTotal
if (rows || width) { 0 1 1
if (rows || width) { 0 0 0
380 1 ▶ d = rows ? rows : 5;
Branch: 1 branch
BranchTrueFalseTotal
d = rows ? rows : 5; 1 0 1
381 1 ▶ w = sa_cms_pow2(width ? width : 2048);
Branch: 1 branch
BranchTrueFalseTotal
w = sa_cms_pow2(width ? width : 2048); 1 0 1
382 1 ▶ if (d > SA_CMS_MAX_ROWS) d = SA_CMS_MAX_ROWS;
Branch: 1 branch
BranchTrueFalseTotal
if (d > SA_CMS_MAX_ROWS) d = SA_CMS_MAX_ROWS; 0 1 1
383 }
384 else {
385 0 sa_cms_size(error, confidence, &d, &w);
386 }
387  
388 1 e = sa_carve(r, name, nlen, sa_cms_bytes(d, w), SA_T_CMS, err);
389 1 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 1 1
390 1 return sa_cms_bind(r, e, d, w, err);
391 }
392  
393 2 static uint64_t sa_abi_cms_total(sa_cms *s) {
394 2 ▶ return s ? sa_at_load64_acq(&s->hdr->total) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return s ? sa_at_load64_acq(&s->hdr->total) : 0; 2 0 2
395 }
396 1 ▶static uint32_t sa_abi_cms_rows(const sa_cms *s) { return s ? s->rows : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint32_t sa_abi_cms_rows(const sa_cms *s) { return s ? s->rows : 0; } 1 0 1
397 1 ▶static uint64_t sa_abi_cms_width(const sa_cms *s) { return s ? s->width : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint64_t sa_abi_cms_width(const sa_cms *s) { return s ? s->width : 0; } 1 0 1
398  
399 /* ---- the cuckoo filter ------------------------------------------------------
400 *
401 * Capacity is the one number either surface takes, and both turn it into
402 * buckets through sa_cuckoo_buckets, so a C consumer and a Perl one naming the
403 * same capacity get the same filter and can attach to each other's. */
404 1 static sa_cuckoo *sa_abi_cuckoo_open(sa_region *r, const char *name,
405 size_t nlen, uint64_t capacity, int *err)
406 {
407 sa_reg *e;
408 uint64_t b;
409  
410 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
411 1 b = sa_cuckoo_buckets(capacity);
412 1 e = sa_carve(r, name, nlen, sa_cuckoo_bytes(b), SA_T_CUCKOO, err);
413 1 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 1 1
414 1 return sa_cuckoo_bind(r, e, b, capacity, err);
415 }
416  
417 3 static void sa_abi_cuckoo_counts(sa_cuckoo *c, sa_cuckoo_counts *out) {
418 3 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 3 3
419 3 memset(out, 0, sizeof *out);
420 3 ▶ if (!c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return; 0 3 3
421 3 out->slots = c->buckets * SA_CK_LANES;
422 3 out->buckets = c->buckets;
423 3 out->capacity = c->hdr->capacity;
424 #if SA_HAVE_ATOMICS
425 3 out->count = sa_at_load64_acq(&c->hdr->count);
426 3 out->kicks = sa_at_load64_acq(&c->hdr->kicks);
427 3 out->moves = sa_at_load64_acq(&c->hdr->moves);
428 3 out->full = sa_at_load64_acq(&c->hdr->full);
429 3 out->recovered = sa_at_load64_acq(&c->hdr->recovered);
430 #endif
431 }
432  
433 /* ---- the lease ------------------------------------------------------------- */
434  
435 1 static sa_lease *sa_abi_lease_open(sa_region *r, const char *name, size_t nlen,
436 int *err)
437 {
438 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
439 1 ▶ if (!r) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 1 1
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
440 #if SA_HAVE_ATOMICS
441 1 return sa_lease_bind(r, name, (uint32_t)nlen, err);
442 #else
443 (void)name; (void)nlen;
444 if (err) *err = SA_E_NOATOMICS;
445 return NULL;
446 #endif
447 }
448  
449 /* sa_lease_acquire / _renew / _release / _mine / _holder / _fence and
450 * sa_lease_free already match the ABI signatures exactly, so the table points
451 * straight at them. */
452  
453 /* ---- the scoreboard -------------------------------------------------------- */
454  
455 1 static sa_sb *sa_abi_sb_open(sa_region *r, const char *name, size_t nlen,
456 const char *const *fields, uint32_t nfields,
457 uint32_t slots, int *err)
458 {
459 sa_reg *e;
460 uint64_t want;
461  
462 1 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 1 0 1
463 1 ▶ if (!r) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 1 1
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
464 #if SA_HAVE_ATOMICS
465 /* `slots` of 0 inherits an existing board, the same as the Perl surface. */
466 1 ▶ if (slots) {
Branch: 1 branch
BranchTrueFalseTotal
if (slots) { 1 0 1
467 1 ▶ 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 1 1
468 1 want = sa_sb_bytes(slots);
469 }
470 0 ▶ else if (sa_find(r, name, nlen)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sa_find(r, name, nlen)) { 0 0 0
471 0 want = 0;
472 }
473 else {
474 0 want = sa_sb_bytes(256);
475 }
476 1 e = sa_carve(r, name, nlen, want, SA_T_SCOREBOARD, err);
477 1 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 1 1
478 1 return sa_sb_bind(r, e, fields, nfields, err);
479 #else
480 (void)name; (void)nlen; (void)fields; (void)nfields; (void)slots; (void)want;
481 if (err) *err = SA_E_NOATOMICS;
482 return NULL;
483 #endif
484 }
485  
486 /* The write side passes the row as an opaque void* in the ABI, so these cast it
487 * back. sa_sb_take / sa_sb_field / sa_sb_read / sa_sb_free match directly. */
488 2 static void *sa_abi_sb_begin(sa_sb *b) {
489 #if SA_HAVE_ATOMICS
490 2 return (void *)sa_sb_begin(b);
491 #else
492 (void)b; return NULL;
493 #endif
494 }
495 2 static void sa_abi_sb_set_gauge(void *row, int idx, uint64_t v) {
496 #if SA_HAVE_ATOMICS
497 2 sa_sb_set_gauge((sa_sb_slot *)row, idx, v);
498 #else
499 (void)row; (void)idx; (void)v;
500 #endif
501 2 }
502 1 static void sa_abi_sb_add_gauge(void *row, int idx, int64_t by) {
503 #if SA_HAVE_ATOMICS
504 1 sa_sb_add_gauge((sa_sb_slot *)row, idx, by);
505 #else
506 (void)row; (void)idx; (void)by;
507 #endif
508 1 }
509 1 static void sa_abi_sb_set_status(void *row, const char *p, uint32_t n) {
510 #if SA_HAVE_ATOMICS
511 1 sa_sb_set_status((sa_sb_slot *)row, p, n);
512 #else
513 (void)row; (void)p; (void)n;
514 #endif
515 1 }
516 2 static void sa_abi_sb_end(sa_sb *b, void *row) {
517 #if SA_HAVE_ATOMICS
518 2 sa_sb_end(b, (sa_sb_slot *)row);
519 #else
520 (void)b; (void)row;
521 #endif
522 2 }
523 1 ▶static uint64_t sa_abi_sb_slots(const sa_sb *b) { return b ? b->nslots : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint64_t sa_abi_sb_slots(const sa_sb *b) { return b ? b->nslots : 0; } 1 0 1
524 1 ▶static uint32_t sa_abi_sb_nfields(const sa_sb *b) { return b ? b->nfields : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint32_t sa_abi_sb_nfields(const sa_sb *b) { return b ? b->nfields : 0; } 1 0 1
525 2 static const char *sa_abi_sb_field_name(const sa_sb *b, uint32_t idx) {
526 2 ▶ if (!b || idx >= b->nfields) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || idx >= b->nfields) return NULL; 2 0 2
if (!b || idx >= b->nfields) return NULL; 0 2 2
527 2 return b->hdr->fields[idx];
528 }
529  
530 /* ---- the HyperLogLog ------------------------------------------------------- */
531  
532 3 static sa_hll *sa_abi_hll_open(sa_region *r, const char *name, size_t nlen,
533 uint32_t precision, int *err)
534 {
535 sa_reg *e;
536 uint64_t want;
537  
538 3 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 3 0 3
539 3 ▶ if (!r) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 3 3
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
540 3 ▶ if (precision && (precision < SA_HLL_MIN_P || precision > SA_HLL_MAX_P)) {
Branch: 3 branches
BranchTrueFalseTotal
if (precision && (precision < SA_HLL_MIN_P || precision > SA_HLL_MAX_P)) { 2 1 3
if (precision && (precision < SA_HLL_MIN_P || precision > SA_HLL_MAX_P)) { 2 0 2
if (precision && (precision < SA_HLL_MIN_P || precision > SA_HLL_MAX_P)) { 0 2 2
541 0 ▶ if (err) *err = SA_E_SHAPE;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_SHAPE; 0 0 0
542 0 return NULL;
543 }
544 /* precision 0 inherits an existing sketch's, as the Perl surface does. */
545 3 ▶ if (precision) want = sa_hll_bytes(precision);
Branch: 1 branch
BranchTrueFalseTotal
if (precision) want = sa_hll_bytes(precision); 2 1 3
546 1 ▶ else if (sa_find(r, name, nlen)) want = 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (sa_find(r, name, nlen)) want = 0; 1 0 1
547 0 else want = sa_hll_bytes(14);
548 3 e = sa_carve(r, name, nlen, want, SA_T_HLL, err);
549 3 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 3 3
550 3 return sa_hll_bind(r, e, precision, err);
551 }
552  
553 2 ▶static uint32_t sa_abi_hll_precision(const sa_hll *h) { return h ? h->p : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static uint32_t sa_abi_hll_precision(const sa_hll *h) { return h ? h->p : 0; } 2 0 2
554  
555 /* sa_hll_free / _add / _count / _merge / _reset / _filled match the ABI
556 * signatures, so the table points straight at them. */
557  
558 /* ---- queue groups ---------------------------------------------------------- */
559  
560 3 static sa_group_h *sa_abi_group_open(sa_ring *r, const char *name, size_t nlen,
561 const char *topic, size_t tlen,
562 int from_start, int *err)
563 {
564 3 uint64_t from = 0;
565 3 ▶ if (err) *err = SA_E_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SA_E_OK; 3 0 3
566 3 ▶ if (!r) { if (err) *err = SA_E_NOENT; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 3 3
if (!r) { if (err) *err = SA_E_NOENT; return NULL; } 0 0 0
567 #if SA_HAVE_ATOMICS
568 3 ▶ if (from_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (from_start) { 0 3 3
569 0 uint64_t end = sa_at_load64_acq(&r->hdr->seq);
570 0 ▶ from = (end > r->nslots) ? end - r->nslots : 1;
Branch: 1 branch
BranchTrueFalseTotal
from = (end > r->nslots) ? end - r->nslots : 1; 0 0 0
571 }
572 3 return sa_group_new(r, name, (uint32_t)nlen, topic, (uint32_t)tlen,
573 from, err);
574 #else
575 (void)name; (void)nlen; (void)topic; (void)tlen; (void)from_start;
576 (void)from;
577 if (err) *err = SA_E_NOATOMICS;
578 return NULL;
579 #endif
580 }
581  
582 5 static int sa_abi_group_claim(sa_group_h *gh, uint64_t *seq,
583 const char **topic, uint32_t *tlen,
584 const char **data, uint32_t *dlen,
585 uint32_t *flags)
586 {
587 #if SA_HAVE_ATOMICS
588 5 ▶ if (!gh) return SA_READ_PENDING;
Branch: 1 branch
BranchTrueFalseTotal
if (!gh) return SA_READ_PENDING; 0 5 5
589 /* Claiming is proof of life, exactly as draining is: a member that only
590 * ever consumes must still tick, or a publisher's hole check would judge
591 * it wedged. */
592 5 sa_peer_join(gh->ring->arena);
593 5 sa_peer_beat(gh->ring->arena);
594 {
595 5 int rc = sa_group_claim(gh->ring, gh->g, gh->scratch, seq, topic, tlen,
596 data, dlen, flags);
597 5 ▶ if (rc == SA_READ_OK) gh->claimed++;
Branch: 1 branch
BranchTrueFalseTotal
if (rc == SA_READ_OK) gh->claimed++; 3 2 5
598 5 return rc;
599 }
600 #else
601 (void)gh; (void)seq; (void)topic; (void)tlen; (void)data; (void)dlen;
602 (void)flags;
603 return SA_READ_PENDING;
604 #endif
605 }
606  
607 1 static uint64_t sa_abi_group_position(const sa_group_h *gh) {
608 #if SA_HAVE_ATOMICS
609 1 ▶ return gh ? sa_at_load64_acq(&gh->g->cursor) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return gh ? sa_at_load64_acq(&gh->g->cursor) : 0; 1 0 1
610 #else
611 (void)gh;
612 return 0;
613 #endif
614 }
615  
616 2 static void sa_abi_group_counts(const sa_group_h *gh, sa_group_counts *out) {
617 2 ▶ if (!out) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return; 0 2 2
618 2 ▶ if (!gh) { memset(out, 0, sizeof *out); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!gh) { memset(out, 0, sizeof *out); return; } 0 2 2
619 #if SA_HAVE_ATOMICS
620 2 out->position = sa_at_load64_acq(&gh->g->cursor);
621 2 out->delivered = sa_at_load64_acq(&gh->g->delivered);
622 2 out->lapped = sa_at_load64_acq(&gh->g->lapped);
623 2 out->skipped = sa_at_load64_acq(&gh->g->skipped);
624 #else
625 memset(out, 0, sizeof *out);
626 #endif
627 2 out->mine = gh->claimed;
628 }
629  
630 static const sa_abi SA_ABI = {
631 SA_ABI_VERSION,
632  
633 sa_abi_config_init,
634 sa_abi_create,
635 sa_abi_attach,
636 sa_abi_release,
637 sa_abi_destroy,
638 sa_strerror,
639 sa_abi_bytes,
640 sa_abi_is_creator,
641  
642 sa_abi_carve,
643 sa_abi_locate,
644 sa_ptr,
645  
646 sa_abi_ring_open,
647 sa_ring_free,
648 sa_abi_publish,
649 sa_abi_max_record,
650 sa_abi_slot_bytes,
651 sa_abi_ring_slots,
652 sa_abi_ring_position,
653  
654 sa_cursor_new,
655 sa_cursor_free,
656 sa_cursor_drain,
657 sa_abi_counts,
658  
659 sa_peer_join,
660 sa_peer_beat,
661  
662 /* the map */
663 sa_abi_map_open,
664 sa_hash_free,
665 sa_hash_store,
666 sa_hash_fetch,
667 sa_hash_delete,
668 sa_hash_incr,
669 sa_abi_map_counts,
670 sa_abi_map_pair_max,
671 sa_abi_map_capacity,
672  
673 /* the filter */
674 sa_abi_bloom_open,
675 sa_bloom_free,
676 sa_bloom_add,
677 sa_bloom_check,
678 sa_bloom_reset,
679 sa_abi_bloom_bits,
680 sa_abi_bloom_hashes,
681 sa_bloom_popcount,
682 sa_bloom_estimate,
683  
684 /* the histogram */
685 sa_abi_hist_open,
686 sa_hist_free,
687 sa_hist_record,
688 sa_hist_quantile,
689 sa_hist_reset,
690 sa_abi_hist_counts,
691 sa_abi_hist_low,
692 sa_abi_hist_high,
693 sa_abi_hist_count,
694  
695 /* the cache */
696 sa_abi_cache_open,
697 sa_cache_free,
698 sa_cache_set,
699 sa_cache_get,
700 sa_cache_remove,
701 sa_cache_clear,
702 sa_abi_cache_counts,
703 sa_abi_cache_pair_max,
704  
705 /* the rate limiter */
706 sa_abi_rate_open,
707 sa_rate_free,
708 sa_abi_rate_allow,
709 sa_abi_rate_peek,
710 sa_rate_reset_key,
711 sa_rate_forget,
712 sa_abi_rate_slots,
713 sa_abi_rate_used,
714  
715 /* the count-min sketch */
716 sa_abi_cms_open,
717 sa_cms_free,
718 sa_cms_add,
719 sa_cms_estimate,
720 sa_cms_reset,
721 sa_abi_cms_total,
722 sa_cms_error,
723 sa_abi_cms_rows,
724 sa_abi_cms_width,
725  
726 /* queue groups */
727 sa_abi_group_open,
728 sa_group_free,
729 sa_abi_group_claim,
730 sa_abi_group_position,
731 sa_abi_group_counts,
732  
733 /* map, with a TTL */
734 sa_hash_store_ttl,
735  
736 /* the cuckoo filter */
737 sa_abi_cuckoo_open,
738 sa_cuckoo_free,
739 sa_cuckoo_add,
740 sa_cuckoo_check,
741 sa_cuckoo_remove,
742 sa_cuckoo_reset,
743 sa_abi_cuckoo_counts,
744  
745 /* the lease */
746 sa_abi_lease_open,
747 sa_lease_free,
748 sa_lease_acquire,
749 sa_lease_renew,
750 sa_lease_release,
751 sa_lease_mine,
752 sa_lease_holder,
753 sa_lease_fence,
754  
755 /* the scoreboard */
756 sa_abi_sb_open,
757 sa_sb_free,
758 sa_sb_take,
759 sa_sb_field,
760 sa_abi_sb_begin,
761 sa_abi_sb_set_gauge,
762 sa_abi_sb_add_gauge,
763 sa_abi_sb_set_status,
764 sa_abi_sb_end,
765 sa_sb_read,
766 sa_abi_sb_slots,
767 sa_abi_sb_nfields,
768 sa_abi_sb_field_name,
769  
770 /* the HyperLogLog */
771 sa_abi_hll_open,
772 sa_hll_free,
773 sa_hll_add,
774 sa_hll_count,
775 sa_hll_merge,
776 sa_hll_reset,
777 sa_abi_hll_precision,
778 sa_hll_filled,
779  
780 /* the wakeup */
781 sa_wake_init,
782 sa_wake_take,
783 sa_wake_fd,
784 sa_wake_drained,
785  
786 /* a counter with a deadline */
787 sa_hash_incr_ttl,
788  
789 /* the limiter's account */
790 sa_abi_rate_counts,
791  
792 /* a counter with a deadline, on the caller's clock */
793 sa_hash_incr_at
794 };
795  
796 /* ---- the selftest ---------------------------------------------------------
797 *
798 * A region, a ring and a cursor driven through EVERY entry of the table, with
799 * every answer checked. It exists because a table is a list of addresses and
800 * nothing about compiling one proves an entry points where its declaration
801 * says: a member inserted in the middle, or two entries of the same shape
802 * swapped, compiles clean and answers wrong.
803 *
804 * Numbered steps, and the number of the first that failed comes back, so a
805 * failure says WHICH. A selftest that reports only "no" sends somebody to read
806 * the whole table.
807 */
808  
809 #define SA_STEP(n) do { *step = (n); } while (0)
810  
811 typedef struct {
812 int n;
813 int bad;
814 char last[64];
815 uint32_t lastlen;
816 uint32_t span; /* the length of the most recent record */
817 } sa_abi_seen;
818  
819 40 static void sa_abi_selftest_rec(void *ud, uint64_t seq,
820 const char *topic, uint32_t tlen,
821 const char *data, uint32_t dlen, uint32_t flags)
822 {
823 40 sa_abi_seen *s = (sa_abi_seen *)ud;
824 (void)seq; (void)flags;
825 40 s->n++;
826 40 ▶ if (tlen != 1 || topic[0] != 't') s->bad = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (tlen != 1 || topic[0] != 't') s->bad = 1; 40 0 40
if (tlen != 1 || topic[0] != 't') s->bad = 1; 0 40 40
827 40 s->span = dlen;
828 40 ▶ if (dlen >= sizeof s->last) return; /* a long one: length is enough */
Branch: 1 branch
BranchTrueFalseTotal
if (dlen >= sizeof s->last) return; /* a long one: length is enough */ 1 39 40
829 39 memcpy(s->last, data, dlen);
830 39 s->lastlen = dlen;
831 }
832  
833 /* Returns 0 when every entry answered as its declaration says, and otherwise
834 * the number of the check that did not. */
835 1 static int sa_abi_selftest(void)
836 {
837 1 const sa_abi *A = &SA_ABI;
838 1 int stepv = 0;
839 1 int *step = &stepv;
840 1 int err = SA_E_OK;
841 sa_config cfg;
842 1 sa_region *r = NULL;
843 1 sa_ring *ring = NULL;
844 1 sa_cursor *cur = NULL;
845 1 uint64_t off, len = 0;
846 sa_counts n;
847 sa_abi_seen seen;
848  
849 /* 1: the version the header promised */
850 1 ▶ if (A->abi_version < SA_ABI_VERSION) { SA_STEP(1); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->abi_version < SA_ABI_VERSION) { SA_STEP(1); goto done; } 0 1 1
851  
852 /* 2: defaults are a function, not a zeroed struct */
853 1 memset(&cfg, 0xFF, sizeof cfg);
854 1 A->config_init(&cfg);
855 1 ▶ if (!cfg.bytes || !cfg.regions || cfg.name) { SA_STEP(2); goto done; }
Branch: 3 branches
BranchTrueFalseTotal
if (!cfg.bytes || !cfg.regions || cfg.name) { SA_STEP(2); goto done; } 1 0 1
if (!cfg.bytes || !cfg.regions || cfg.name) { SA_STEP(2); goto done; } 1 0 1
if (!cfg.bytes || !cfg.regions || cfg.name) { SA_STEP(2); goto done; } 0 1 1
856  
857 /* 3: create an anonymous region */
858 1 cfg.bytes = 512 * 1024;
859 1 r = A->create(&cfg, &err);
860 #if !SA_HAVE_ATOMICS
861 /* With no atomics the table's answer is that there is no region, and it
862 * must give that answer as NULL and a code, never a pointer - for attach
863 * as for create. Every later check needs a region, so this is the whole of
864 * the test in this build. */
865 if (r || err != SA_E_NOATOMICS || !A->errstr(err)) { SA_STEP(3); goto done; }
866 err = SA_E_OK;
867 r = A->attach_named("sa-selftest", 11, &err);
868 if (r || err != SA_E_NOATOMICS) { SA_STEP(3); goto done; }
869 goto done;
870 #endif
871 1 ▶ if (!r || err != SA_E_OK) { SA_STEP(3); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!r || err != SA_E_OK) { SA_STEP(3); goto done; } 1 0 1
if (!r || err != SA_E_OK) { SA_STEP(3); goto done; } 0 1 1
872 1 ▶ if (!A->is_creator(r)) { SA_STEP(3); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->is_creator(r)) { SA_STEP(3); goto done; } 0 1 1
873 1 ▶ if (A->region_bytes(r) < cfg.bytes) { SA_STEP(3); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->region_bytes(r) < cfg.bytes) { SA_STEP(3); goto done; } 0 1 1
874  
875 /* 4: a refusal is NULL and a code, never a crash, and errstr renders it */
876 {
877 1 int e2 = SA_E_OK;
878 1 sa_region *bad = A->attach_named("sa-nope-nobody-made-this", 24, &e2);
879 1 ▶ if (bad) { A->release(bad); SA_STEP(4); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (bad) { A->release(bad); SA_STEP(4); goto done; } 0 1 1
880 1 ▶ if (e2 == SA_E_OK || !A->errstr(e2)) { SA_STEP(4); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (e2 == SA_E_OK || !A->errstr(e2)) { SA_STEP(4); goto done; } 1 0 1
if (e2 == SA_E_OK || !A->errstr(e2)) { SA_STEP(4); goto done; } 0 1 1
881 1 ▶ if (!A->errstr(SA_E_MAGIC)) { SA_STEP(4); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->errstr(SA_E_MAGIC)) { SA_STEP(4); goto done; } 0 1 1
882 }
883  
884 /* 5: carve, locate, and the pointer an offset resolves to */
885 1 off = A->carve(r, "scratch", 7, 1024, SA_T_RAW, &len, &err);
886 1 ▶ if (!off || len != 1024) { SA_STEP(5); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!off || len != 1024) { SA_STEP(5); goto done; } 1 0 1
if (!off || len != 1024) { SA_STEP(5); goto done; } 0 1 1
887 1 ▶ if (A->locate(r, "scratch", 7, NULL) != off) { SA_STEP(5); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->locate(r, "scratch", 7, NULL) != off) { SA_STEP(5); goto done; } 0 1 1
888 1 ▶ if (A->locate(r, "absent", 6, NULL) != 0) { SA_STEP(5); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->locate(r, "absent", 6, NULL) != 0) { SA_STEP(5); goto done; } 0 1 1
889 /* Carving the same name again finds it rather than making a second one. */
890 1 ▶ if (A->carve(r, "scratch", 7, 1024, SA_T_RAW, NULL, &err) != off)
Branch: 1 branch
BranchTrueFalseTotal
if (A->carve(r, "scratch", 7, 1024, SA_T_RAW, NULL, &err) != off) 0 1 1
891 0 { SA_STEP(5); goto done; }
892  
893 /* 6: `at` is the one place an offset becomes a pointer, and it is inside
894 * the mapping rather than anywhere else */
895 {
896 1 char *p = (char *)A->at(r, off);
897 1 ▶ if (!p) { SA_STEP(6); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { SA_STEP(6); goto done; } 0 1 1
898 1 memcpy(p, "written through the table", 25);
899 1 ▶ if (memcmp((char *)A->at(r, off), "written through the table", 25))
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp((char *)A->at(r, off), "written through the table", 25)) 0 1 1
900 0 { SA_STEP(6); goto done; }
901 1 ▶ if (A->at(r, A->region_bytes(r) + 4096)) { SA_STEP(6); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->at(r, A->region_bytes(r) + 4096)) { SA_STEP(6); goto done; } 0 1 1
902 }
903  
904 /* 7: a ring, and its geometry as runtime answers */
905 1 ring = A->ring_open(r, "events", 6, 32, 256, &err);
906 1 ▶ if (!ring || err != SA_E_OK) { SA_STEP(7); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ring || err != SA_E_OK) { SA_STEP(7); goto done; } 1 0 1
if (!ring || err != SA_E_OK) { SA_STEP(7); goto done; } 0 1 1
907 1 ▶ if (A->ring_slots(ring) != 32) { SA_STEP(7); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->ring_slots(ring) != 32) { SA_STEP(7); goto done; } 0 1 1
908 /* The spanned maximum: a record may cross up to half the ring, so it is
909 * larger than one slot and smaller than the whole thing. A consumer that
910 * compiled either number in would be wrong the day the ring is
911 * reconfigured, which is why this is an entry and not a constant. */
912 1 ▶ if (A->max_record(ring) <= 256) { SA_STEP(7); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->max_record(ring) <= 256) { SA_STEP(7); goto done; } 0 1 1
913 1 ▶ if (A->max_record(ring) >= 32 * 256) { SA_STEP(7); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->max_record(ring) >= 32 * 256) { SA_STEP(7); goto done; } 0 1 1
914 /* One slot carries less than its own size, because the header is in it,
915 * and the spanned maximum is a multiple of that. */
916 1 ▶ if (A->slot_bytes(ring) == 0 || A->slot_bytes(ring) >= 256)
Branch: 2 branches
BranchTrueFalseTotal
if (A->slot_bytes(ring) == 0 || A->slot_bytes(ring) >= 256) 1 0 1
if (A->slot_bytes(ring) == 0 || A->slot_bytes(ring) >= 256) 0 1 1
917 0 { SA_STEP(7); goto done; }
918 1 ▶ if (A->max_record(ring) % A->slot_bytes(ring) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (A->max_record(ring) % A->slot_bytes(ring) != 0) 0 1 1
919 0 { SA_STEP(7); goto done; }
920  
921 /* 8: publish returns the record's sequence, and refuses an oversize one */
922 {
923 1 int64_t s1 = A->publish(ring, "t", 1, "one", 3);
924 1 int64_t s2 = A->publish(ring, "t", 1, "two", 3);
925 1 ▶ if (!SA_PUBLISHED(s1) || !SA_PUBLISHED(s2) || s2 <= s1)
Branch: 3 branches
BranchTrueFalseTotal
if (!SA_PUBLISHED(s1) || !SA_PUBLISHED(s2) || s2 <= s1) 1 0 1
if (!SA_PUBLISHED(s1) || !SA_PUBLISHED(s2) || s2 <= s1) 1 0 1
if (!SA_PUBLISHED(s1) || !SA_PUBLISHED(s2) || s2 <= s1) 0 1 1
926 0 { SA_STEP(8); goto done; }
927 1 ▶ if (A->ring_position(ring) <= (uint64_t)s2) { SA_STEP(8); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->ring_position(ring) <= (uint64_t)s2) { SA_STEP(8); goto done; } 0 1 1
928 }
929 {
930 /* A record that spans several slots is carried; one larger than the
931 * ring will hold is refused, never truncated. */
932 char big[4096];
933 1 uint64_t cap = A->max_record(ring);
934 1 memset(big, 'x', sizeof big);
935 1 ▶ if (!SA_PUBLISHED(A->publish(ring, "t", 1, big, 600)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SA_PUBLISHED(A->publish(ring, "t", 1, big, 600))) 0 1 1
936 0 { SA_STEP(8); goto done; }
937 1 ▶ if (cap + 1 <= sizeof big
Branch: 1 branch
BranchTrueFalseTotal
if (cap + 1 <= sizeof big 1 0 1
938 1 ▶ && A->publish(ring, "t", 1, big, (uint32_t)(cap + 1)) != SA_TOO_BIG)
Branch: 1 branch
BranchTrueFalseTotal
&& A->publish(ring, "t", 1, big, (uint32_t)(cap + 1)) != SA_TOO_BIG) 0 1 1
939 0 { SA_STEP(8); goto done; }
940 }
941  
942 /* 9: a cursor from the start replays what the ring still holds */
943 1 cur = A->cursor_open(ring, 1);
944 1 ▶ if (!cur) { SA_STEP(9); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!cur) { SA_STEP(9); goto done; } 0 1 1
945 1 seen.n = 0; seen.bad = 0; seen.lastlen = 0; seen.span = 0;
946 1 ▶ if (A->drain(cur, 0, sa_abi_selftest_rec, &seen) != 3)
Branch: 1 branch
BranchTrueFalseTotal
if (A->drain(cur, 0, sa_abi_selftest_rec, &seen) != 3) 0 1 1
947 0 { SA_STEP(9); goto done; }
948 1 ▶ if (seen.bad || seen.n != 3) { SA_STEP(9); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (seen.bad || seen.n != 3) { SA_STEP(9); goto done; } 1 0 1
if (seen.bad || seen.n != 3) { SA_STEP(9); goto done; } 0 1 1
949 /* The last one was the 600-byte record, gathered from several slots and
950 * handed over as one. */
951 1 ▶ if (seen.span != 600) { SA_STEP(9); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (seen.span != 600) { SA_STEP(9); goto done; } 0 1 1
952  
953 /* 10: and a second drain has nothing to add */
954 1 ▶ if (A->drain(cur, 0, sa_abi_selftest_rec, &seen) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (A->drain(cur, 0, sa_abi_selftest_rec, &seen) != 0) 0 1 1
955 0 { SA_STEP(10); goto done; }
956  
957 /* 11: the counts, and the four kinds of outcome kept apart */
958 1 A->counts(cur, &n);
959 1 ▶ if (n.delivered != 3 || n.lapped != 0 || n.abandoned != 0
Branch: 3 branches
BranchTrueFalseTotal
if (n.delivered != 3 || n.lapped != 0 || n.abandoned != 0 1 0 1
if (n.delivered != 3 || n.lapped != 0 || n.abandoned != 0 1 0 1
if (n.delivered != 3 || n.lapped != 0 || n.abandoned != 0 1 0 1
960 1 ▶ || n.unattributed != 0) { SA_STEP(11); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
|| n.unattributed != 0) { SA_STEP(11); goto done; } 0 1 1
961 1 ▶ if (n.position != A->ring_position(ring)) { SA_STEP(11); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (n.position != A->ring_position(ring)) { SA_STEP(11); goto done; } 0 1 1
962  
963 /* 12: `max` limits a drain, and the rest is still there afterwards */
964 {
965 int i;
966 1 sa_cursor *c2 = A->cursor_open(ring, 0);
967 1 ▶ if (!c2) { SA_STEP(12); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!c2) { SA_STEP(12); goto done; } 0 1 1
968 6 ▶ for (i = 0; i < 5; i++) A->publish(ring, "t", 1, "n", 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 5; i++) A->publish(ring, "t", 1, "n", 1); 5 1 6
969 1 seen.n = 0; seen.bad = 0;
970 1 ▶ if (A->drain(c2, 2, sa_abi_selftest_rec, &seen) != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (A->drain(c2, 2, sa_abi_selftest_rec, &seen) != 2) 0 1 1
971 0 { A->cursor_release(c2); SA_STEP(12); goto done; }
972 1 ▶ if (A->drain(c2, 0, sa_abi_selftest_rec, &seen) != 3)
Branch: 1 branch
BranchTrueFalseTotal
if (A->drain(c2, 0, sa_abi_selftest_rec, &seen) != 3) 0 1 1
973 0 { A->cursor_release(c2); SA_STEP(12); goto done; }
974 1 A->cursor_release(c2);
975 }
976  
977 /* 13: overflow is drop-oldest, counted, and never silent */
978 {
979 int i;
980 1 sa_cursor *c3 = A->cursor_open(ring, 0);
981 sa_counts m;
982 1 ▶ if (!c3) { SA_STEP(13); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!c3) { SA_STEP(13); goto done; } 0 1 1
983 101 ▶ for (i = 0; i < 100; i++) A->publish(ring, "t", 1, "flood", 5);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 100; i++) A->publish(ring, "t", 1, "flood", 5); 100 1 101
984 1 seen.n = 0; seen.bad = 0;
985 1 A->drain(c3, 0, sa_abi_selftest_rec, &seen);
986 1 A->counts(c3, &m);
987 1 ▶ if (m.lapped == 0) { A->cursor_release(c3); SA_STEP(13); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (m.lapped == 0) { A->cursor_release(c3); SA_STEP(13); goto done; } 0 1 1
988 1 ▶ if (m.delivered + m.lapped != 100)
Branch: 1 branch
BranchTrueFalseTotal
if (m.delivered + m.lapped != 100) 0 1 1
989 0 { A->cursor_release(c3); SA_STEP(13); goto done; }
990 /* The counters must be DISTINGUISHABLE here, and this is the only
991 * place in the selftest where they are: at step 11 everything but
992 * `delivered` is zero, so a table that reported one counter where
993 * another belongs would answer correctly by accident. Here `lapped` is
994 * large and the other two must still be nothing. */
995 1 ▶ if (m.abandoned != 0 || m.unattributed != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (m.abandoned != 0 || m.unattributed != 0) 1 0 1
if (m.abandoned != 0 || m.unattributed != 0) 0 1 1
996 0 { A->cursor_release(c3); SA_STEP(13); goto done; }
997 1 ▶ if (m.position != A->ring_position(ring))
Branch: 1 branch
BranchTrueFalseTotal
if (m.position != A->ring_position(ring)) 0 1 1
998 0 { A->cursor_release(c3); SA_STEP(13); goto done; }
999 1 A->cursor_release(c3);
1000 }
1001  
1002 /* 14: liveness - this process is registered, and a beat moves it on */
1003 1 ▶ if (!A->join(r)) { SA_STEP(14); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->join(r)) { SA_STEP(14); goto done; } 0 1 1
1004 {
1005 1 sa_peer *p = &SA_PEERS(r->map.base, r->hdr)[r->peer_idx];
1006 1 uint64_t hb = sa_at_load64_acq(&p->heartbeat);
1007 1 A->beat(r);
1008 1 ▶ if (sa_at_load64_acq(&p->heartbeat) <= hb) { SA_STEP(14); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (sa_at_load64_acq(&p->heartbeat) <= hb) { SA_STEP(14); goto done; } 0 1 1
1009 }
1010  
1011 /* 15: the map, through the table: store, fetch, overwrite, delete, the
1012 * counter that is one atomic, and a full table that refuses rather than
1013 * growing. */
1014 {
1015 1 sa_hash *mp = A->map_open(r, "sel", 3, 16, 128, &err);
1016 char buf[128];
1017 1 uint32_t vl = 0;
1018 1 uint64_t now = 0;
1019 sa_map_counts mc;
1020 int i;
1021  
1022 1 ▶ if (!mp || err != SA_E_OK) { SA_STEP(15); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!mp || err != SA_E_OK) { SA_STEP(15); goto done; } 1 0 1
if (!mp || err != SA_E_OK) { SA_STEP(15); goto done; } 0 1 1
1023 1 ▶ if (A->map_capacity(mp) != 16 || A->map_pair_max(mp) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_capacity(mp) != 16 || A->map_pair_max(mp) == 0) 1 0 1
if (A->map_capacity(mp) != 16 || A->map_pair_max(mp) == 0) 0 1 1
1024 0 { A->map_release(mp); SA_STEP(15); goto done; }
1025  
1026 1 ▶ if (A->map_store(mp, "k", 1, "one", 3) != SA_H_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_store(mp, "k", 1, "one", 3) != SA_H_OK) 0 1 1
1027 0 { A->map_release(mp); SA_STEP(15); goto done; }
1028 1 ▶ if (A->map_fetch(mp, "k", 1, buf, sizeof buf, &vl) != SA_MAP_HIT
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_fetch(mp, "k", 1, buf, sizeof buf, &vl) != SA_MAP_HIT 1 0 1
1029 1 ▶ || vl != 3 || memcmp(buf, "one", 3))
Branch: 2 branches
BranchTrueFalseTotal
|| vl != 3 || memcmp(buf, "one", 3)) 1 0 1
|| vl != 3 || memcmp(buf, "one", 3)) 0 1 1
1030 0 { A->map_release(mp); SA_STEP(15); goto done; }
1031 1 ▶ if (A->map_fetch(mp, "nope", 4, buf, sizeof buf, &vl) != SA_MAP_MISS)
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_fetch(mp, "nope", 4, buf, sizeof buf, &vl) != SA_MAP_MISS) 0 1 1
1032 0 { A->map_release(mp); SA_STEP(15); goto done; }
1033  
1034 1 ▶ if (A->map_store(mp, "k", 1, "two", 3) != SA_H_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_store(mp, "k", 1, "two", 3) != SA_H_OK) 0 1 1
1035 0 { A->map_release(mp); SA_STEP(15); goto done; }
1036 1 ▶ if (A->map_fetch(mp, "k", 1, buf, sizeof buf, &vl) != SA_MAP_HIT
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_fetch(mp, "k", 1, buf, sizeof buf, &vl) != SA_MAP_HIT 1 0 1
1037 1 ▶ || vl != 3 || memcmp(buf, "two", 3))
Branch: 2 branches
BranchTrueFalseTotal
|| vl != 3 || memcmp(buf, "two", 3)) 1 0 1
|| vl != 3 || memcmp(buf, "two", 3)) 0 1 1
1038 0 { A->map_release(mp); SA_STEP(15); goto done; }
1039  
1040 1 ▶ if (A->map_incr(mp, "c", 1, 5, &now) != SA_H_OK || now != 5)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr(mp, "c", 1, 5, &now) != SA_H_OK || now != 5) 1 0 1
if (A->map_incr(mp, "c", 1, 5, &now) != SA_H_OK || now != 5) 0 1 1
1041 0 { A->map_release(mp); SA_STEP(15); goto done; }
1042 1 ▶ if (A->map_incr(mp, "c", 1, -2, &now) != SA_H_OK || now != 3)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr(mp, "c", 1, -2, &now) != SA_H_OK || now != 3) 1 0 1
if (A->map_incr(mp, "c", 1, -2, &now) != SA_H_OK || now != 3) 0 1 1
1043 0 { A->map_release(mp); SA_STEP(15); goto done; }
1044 /* a counter is refused on an entry that is not one */
1045 1 ▶ if (A->map_incr(mp, "k", 1, 1, &now) != SA_H_NOTNUM)
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_incr(mp, "k", 1, 1, &now) != SA_H_NOTNUM) 0 1 1
1046 0 { A->map_release(mp); SA_STEP(15); goto done; }
1047  
1048 1 ▶ if (!A->map_delete(mp, "k", 1) || A->map_delete(mp, "k", 1))
Branch: 2 branches
BranchTrueFalseTotal
if (!A->map_delete(mp, "k", 1) || A->map_delete(mp, "k", 1)) 1 0 1
if (!A->map_delete(mp, "k", 1) || A->map_delete(mp, "k", 1)) 0 1 1
1049 0 { A->map_release(mp); SA_STEP(15); goto done; }
1050  
1051 1 A->map_counts(mp, &mc);
1052 1 ▶ if (mc.capacity != 16 || mc.tombstones != 1 || mc.used != 1)
Branch: 3 branches
BranchTrueFalseTotal
if (mc.capacity != 16 || mc.tombstones != 1 || mc.used != 1) 1 0 1
if (mc.capacity != 16 || mc.tombstones != 1 || mc.used != 1) 1 0 1
if (mc.capacity != 16 || mc.tombstones != 1 || mc.used != 1) 0 1 1
1053 0 { A->map_release(mp); SA_STEP(15); goto done; }
1054  
1055 /* map_store_ttl through the table: a far deadline stores and reads
1056 * like any other entry. The expiry itself needs a clock the selftest
1057 * cannot advance, so real lapsing is a Perl test's job; this proves the
1058 * entry points at a working function. */
1059 1 ▶ if (A->map_store_ttl(mp, "ttl", 3, "live", 4, 3600000ULL) != SA_H_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_store_ttl(mp, "ttl", 3, "live", 4, 3600000ULL) != SA_H_OK) 0 1 1
1060 0 { A->map_release(mp); SA_STEP(15); goto done; }
1061 1 ▶ if (A->map_fetch(mp, "ttl", 3, buf, sizeof buf, &vl) != SA_MAP_HIT
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_fetch(mp, "ttl", 3, buf, sizeof buf, &vl) != SA_MAP_HIT 1 0 1
1062 1 ▶ || vl != 4 || memcmp(buf, "live", 4))
Branch: 2 branches
BranchTrueFalseTotal
|| vl != 4 || memcmp(buf, "live", 4)) 1 0 1
|| vl != 4 || memcmp(buf, "live", 4)) 0 1 1
1063 0 { A->map_release(mp); SA_STEP(15); goto done; }
1064 1 ▶ if (!A->map_delete(mp, "ttl", 3))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->map_delete(mp, "ttl", 3)) 0 1 1
1065 0 { A->map_release(mp); SA_STEP(15); goto done; }
1066  
1067 /* fill it and check the ceiling refuses rather than growing */
1068 33 ▶ for (i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) { 32 1 33
1069 char kb[16];
1070 32 int n = sprintf(kb, "f%d", i);
1071 32 (void)A->map_store(mp, kb, (uint32_t)n, "x", 1);
1072 }
1073 1 A->map_counts(mp, &mc);
1074 1 ▶ if (mc.used > mc.capacity || mc.full == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (mc.used > mc.capacity || mc.full == 0) 1 0 1
if (mc.used > mc.capacity || mc.full == 0) 0 1 1
1075 0 { A->map_release(mp); SA_STEP(15); goto done; }
1076  
1077 1 A->map_release(mp);
1078 }
1079  
1080 /* 16: the filter. The guarantee that matters is one-directional, so it is
1081 * the one asserted: everything added must check true. A false positive is
1082 * allowed and cannot be asserted against; a false NEGATIVE is a bug. */
1083 {
1084 1 sa_bloom *bl = A->bloom_open(r, "selb", 4, 1000, 0.01, 0, 0, &err);
1085 1 int i, bad = 0;
1086 char kb[16];
1087  
1088 1 ▶ if (!bl || err != SA_E_OK) { SA_STEP(16); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!bl || err != SA_E_OK) { SA_STEP(16); goto done; } 1 0 1
if (!bl || err != SA_E_OK) { SA_STEP(16); goto done; } 0 1 1
1089 1 ▶ if (A->bloom_bits(bl) == 0 || A->bloom_hashes(bl) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (A->bloom_bits(bl) == 0 || A->bloom_hashes(bl) == 0) 1 0 1
if (A->bloom_bits(bl) == 0 || A->bloom_hashes(bl) == 0) 0 1 1
1090 0 { A->bloom_release(bl); SA_STEP(16); goto done; }
1091 1 ▶ if (A->bloom_set(bl) != 0) { A->bloom_release(bl); SA_STEP(16); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->bloom_set(bl) != 0) { A->bloom_release(bl); SA_STEP(16); goto done; } 0 1 1
1092  
1093 1 ▶ if (A->bloom_add(bl, "one", 3) != 0) /* new */
Branch: 1 branch
BranchTrueFalseTotal
if (A->bloom_add(bl, "one", 3) != 0) /* new */ 0 1 1
1094 0 { A->bloom_release(bl); SA_STEP(16); goto done; }
1095 1 ▶ if (A->bloom_add(bl, "one", 3) != 1) /* and now seen */
Branch: 1 branch
BranchTrueFalseTotal
if (A->bloom_add(bl, "one", 3) != 1) /* and now seen */ 0 1 1
1096 0 { A->bloom_release(bl); SA_STEP(16); goto done; }
1097 1 ▶ if (!A->bloom_check(bl, "one", 3))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->bloom_check(bl, "one", 3)) 0 1 1
1098 0 { A->bloom_release(bl); SA_STEP(16); goto done; }
1099  
1100 201 ▶ for (i = 0; i < 200; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 200; i++) { 200 1 201
1101 200 int n = sprintf(kb, "b%d", i);
1102 200 (void)A->bloom_add(bl, kb, (uint32_t)n);
1103 }
1104 201 ▶ for (i = 0; i < 200; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 200; i++) { 200 1 201
1105 200 int n = sprintf(kb, "b%d", i);
1106 200 ▶ if (!A->bloom_check(bl, kb, (uint32_t)n)) bad++;
Branch: 1 branch
BranchTrueFalseTotal
if (!A->bloom_check(bl, kb, (uint32_t)n)) bad++; 0 200 200
1107 }
1108 1 ▶ if (bad) { A->bloom_release(bl); SA_STEP(16); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (bad) { A->bloom_release(bl); SA_STEP(16); goto done; } 0 1 1
1109 1 ▶ if (A->bloom_set(bl) == 0 || A->bloom_estimate(bl) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (A->bloom_set(bl) == 0 || A->bloom_estimate(bl) == 0) 1 0 1
if (A->bloom_set(bl) == 0 || A->bloom_estimate(bl) == 0) 0 1 1
1110 0 { A->bloom_release(bl); SA_STEP(16); goto done; }
1111  
1112 1 A->bloom_reset(bl);
1113 1 ▶ if (A->bloom_set(bl) != 0 || A->bloom_check(bl, "one", 3))
Branch: 2 branches
BranchTrueFalseTotal
if (A->bloom_set(bl) != 0 || A->bloom_check(bl, "one", 3)) 1 0 1
if (A->bloom_set(bl) != 0 || A->bloom_check(bl, "one", 3)) 0 1 1
1114 0 { A->bloom_release(bl); SA_STEP(16); goto done; }
1115  
1116 1 A->bloom_release(bl);
1117 }
1118  
1119 /* 17: the histogram. The assertion is the BOUND, not a number: a value
1120 * must come back at or above itself and within the relative error the
1121 * scheme promises, and small values must be exact. */
1122 {
1123 1 sa_hist *hh = A->hist_open(r, "selh", 4, 100000, 4, &err);
1124 sa_hist_counts hc;
1125 uint64_t q;
1126 int i;
1127  
1128 1 ▶ if (!hh || err != SA_E_OK) { SA_STEP(17); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!hh || err != SA_E_OK) { SA_STEP(17); goto done; } 1 0 1
if (!hh || err != SA_E_OK) { SA_STEP(17); goto done; } 0 1 1
1129  
1130 1001 ▶ for (i = 1; i <= 1000; i++) A->hist_record(hh, (uint64_t)i, 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= 1000; i++) A->hist_record(hh, (uint64_t)i, 1); 1000 1 1001
1131 1 A->hist_counts(hh, &hc);
1132 1 ▶ if (hc.count != 1000 || hc.sum != 500500) { A->hist_release(hh); SA_STEP(17); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (hc.count != 1000 || hc.sum != 500500) { A->hist_release(hh); SA_STEP(17); goto done; } 1 0 1
if (hc.count != 1000 || hc.sum != 500500) { A->hist_release(hh); SA_STEP(17); goto done; } 0 1 1
1133 1 ▶ if (hc.min != 1 || hc.max != 1000) { A->hist_release(hh); SA_STEP(17); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (hc.min != 1 || hc.max != 1000) { A->hist_release(hh); SA_STEP(17); goto done; } 1 0 1
if (hc.min != 1 || hc.max != 1000) { A->hist_release(hh); SA_STEP(17); goto done; } 0 1 1
1134 1 ▶ if (hc.over != 0 || hc.sigbits != 4) { A->hist_release(hh); SA_STEP(17); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (hc.over != 0 || hc.sigbits != 4) { A->hist_release(hh); SA_STEP(17); goto done; } 1 0 1
if (hc.over != 0 || hc.sigbits != 4) { A->hist_release(hh); SA_STEP(17); goto done; } 0 1 1
1135  
1136 /* the median, never below and never further above than 1/16 */
1137 1 q = A->hist_quantile(hh, 0.5);
1138 1 ▶ if (q < 500 || q > 500 + 500 / 16 + 1)
Branch: 2 branches
BranchTrueFalseTotal
if (q < 500 || q > 500 + 500 / 16 + 1) 1 0 1
if (q < 500 || q > 500 + 500 / 16 + 1) 0 1 1
1139 0 { A->hist_release(hh); SA_STEP(17); goto done; }
1140  
1141 /* below 2^sigbits a bucket is one unit wide, so this is exact */
1142 1 ▶ if (A->hist_bucket_low(hh, 7) != 7 || A->hist_bucket_high(hh, 7) != 7)
Branch: 2 branches
BranchTrueFalseTotal
if (A->hist_bucket_low(hh, 7) != 7 || A->hist_bucket_high(hh, 7) != 7) 1 0 1
if (A->hist_bucket_low(hh, 7) != 7 || A->hist_bucket_high(hh, 7) != 7) 0 1 1
1143 0 { A->hist_release(hh); SA_STEP(17); goto done; }
1144 1 ▶ if (A->hist_bucket_count(hh, 7) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (A->hist_bucket_count(hh, 7) != 1) 0 1 1
1145 0 { A->hist_release(hh); SA_STEP(17); goto done; }
1146  
1147 /* past the ceiling: counted apart, and the maximum does not move */
1148 1 A->hist_record(hh, 999999999ULL, 3);
1149 1 A->hist_counts(hh, &hc);
1150 1 ▶ if (hc.over != 3 || hc.count != 1000 || hc.max != 1000)
Branch: 3 branches
BranchTrueFalseTotal
if (hc.over != 3 || hc.count != 1000 || hc.max != 1000) 1 0 1
if (hc.over != 3 || hc.count != 1000 || hc.max != 1000) 1 0 1
if (hc.over != 3 || hc.count != 1000 || hc.max != 1000) 0 1 1
1151 0 { A->hist_release(hh); SA_STEP(17); goto done; }
1152  
1153 1 A->hist_reset(hh);
1154 1 A->hist_counts(hh, &hc);
1155 1 ▶ if (hc.count || hc.sum || hc.over)
Branch: 3 branches
BranchTrueFalseTotal
if (hc.count || hc.sum || hc.over) 1 0 1
if (hc.count || hc.sum || hc.over) 1 0 1
if (hc.count || hc.sum || hc.over) 0 1 1
1156 0 { A->hist_release(hh); SA_STEP(17); goto done; }
1157  
1158 1 A->hist_release(hh);
1159 }
1160  
1161 /* 18: the cache. What separates it from the map is that it never refuses
1162 * for want of room, so that is what is asserted: far more keys than it
1163 * holds, and every set succeeding. */
1164 {
1165 1 sa_cache *cc = A->cache_open(r, "selc", 4, 16, 8, 128, &err);
1166 sa_cache_counts cn;
1167 char buf[128], kb[16];
1168 1 uint32_t vl = 0;
1169 1 int i, refused = 0;
1170  
1171 1 ▶ if (!cc || err != SA_E_OK) { SA_STEP(18); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!cc || err != SA_E_OK) { SA_STEP(18); goto done; } 1 0 1
if (!cc || err != SA_E_OK) { SA_STEP(18); goto done; } 0 1 1
1172 1 ▶ if (A->cache_pair_max(cc) == 0) { A->cache_release(cc); SA_STEP(18); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_pair_max(cc) == 0) { A->cache_release(cc); SA_STEP(18); goto done; } 0 1 1
1173  
1174 1 ▶ if (A->cache_set(cc, "k", 1, "one", 3, 0) != SA_C_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_set(cc, "k", 1, "one", 3, 0) != SA_C_OK) 0 1 1
1175 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1176 1 ▶ if (A->cache_get(cc, "k", 1, buf, sizeof buf, &vl) != SA_MAP_HIT
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_get(cc, "k", 1, buf, sizeof buf, &vl) != SA_MAP_HIT 1 0 1
1177 1 ▶ || vl != 3 || memcmp(buf, "one", 3))
Branch: 2 branches
BranchTrueFalseTotal
|| vl != 3 || memcmp(buf, "one", 3)) 1 0 1
|| vl != 3 || memcmp(buf, "one", 3)) 0 1 1
1178 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1179 1 ▶ if (A->cache_get(cc, "no", 2, buf, sizeof buf, &vl) != SA_MAP_MISS)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_get(cc, "no", 2, buf, sizeof buf, &vl) != SA_MAP_MISS) 0 1 1
1180 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1181  
1182 /* an entry already past its deadline is a miss, not stale data */
1183 1 ▶ if (A->cache_set(cc, "dead", 4, "x", 1, 1) != SA_C_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_set(cc, "dead", 4, "x", 1, 1) != SA_C_OK) 0 1 1
1184 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1185 1 sa_stall(20000);
1186 1 ▶ if (A->cache_get(cc, "dead", 4, buf, sizeof buf, &vl) != SA_MAP_MISS)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_get(cc, "dead", 4, buf, sizeof buf, &vl) != SA_MAP_MISS) 0 1 1
1187 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1188  
1189 /* ten times its capacity, and not one refusal */
1190 161 ▶ for (i = 0; i < 160; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 160; i++) { 160 1 161
1191 160 int n = sprintf(kb, "c%d", i);
1192 160 ▶ if (A->cache_set(cc, kb, (uint32_t)n, "v", 1, 0) != SA_C_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cache_set(cc, kb, (uint32_t)n, "v", 1, 0) != SA_C_OK) 0 160 160
1193 0 refused++;
1194 }
1195 1 ▶ if (refused) { A->cache_release(cc); SA_STEP(18); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (refused) { A->cache_release(cc); SA_STEP(18); goto done; } 0 1 1
1196  
1197 1 A->cache_counts(cc, &cn);
1198 1 ▶ if (cn.evictions == 0 || cn.live > cn.capacity || cn.ways != 8)
Branch: 3 branches
BranchTrueFalseTotal
if (cn.evictions == 0 || cn.live > cn.capacity || cn.ways != 8) 1 0 1
if (cn.evictions == 0 || cn.live > cn.capacity || cn.ways != 8) 1 0 1
if (cn.evictions == 0 || cn.live > cn.capacity || cn.ways != 8) 0 1 1
1199 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1200 1 ▶ if (cn.hits == 0 || cn.misses == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (cn.hits == 0 || cn.misses == 0) 1 0 1
if (cn.hits == 0 || cn.misses == 0) 0 1 1
1201 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1202  
1203 1 ▶ if (!A->cache_remove(cc, "c159", 4))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->cache_remove(cc, "c159", 4)) 0 1 1
1204 0 { A->cache_release(cc); SA_STEP(18); goto done; }
1205 1 A->cache_clear(cc);
1206 1 A->cache_counts(cc, &cn);
1207 1 ▶ if (cn.live != 0) { A->cache_release(cc); SA_STEP(18); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (cn.live != 0) { A->cache_release(cc); SA_STEP(18); goto done; } 0 1 1
1208  
1209 1 A->cache_release(cc);
1210 }
1211  
1212 /* 19: the rate limiter. A burst of three with a refill window long enough
1213 * that nothing comes back while this runs, so the answers are exact rather
1214 * than a race against the clock. */
1215 {
1216 1 sa_rate *rl = A->rate_open(r, "selr", 4, 3, 3600000, 64, &err);
1217 1 uint64_t left = 0, retry = 0;
1218 int i;
1219  
1220 1 ▶ if (!rl || err != SA_E_OK) { SA_STEP(19); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!rl || err != SA_E_OK) { SA_STEP(19); goto done; } 1 0 1
if (!rl || err != SA_E_OK) { SA_STEP(19); goto done; } 0 1 1
1221 1 ▶ if (A->rate_slots(rl) < 64) { A->rate_release(rl); SA_STEP(19); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->rate_slots(rl) < 64) { A->rate_release(rl); SA_STEP(19); goto done; } 0 1 1
1222  
1223 4 ▶ for (i = 0; i < 3; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) 3 1 4
1224 3 ▶ if (!A->rate_allow(rl, "k", 1, 1, &left, &retry))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->rate_allow(rl, "k", 1, 1, &left, &retry)) 0 3 3
1225 0 { A->rate_release(rl); SA_STEP(19); goto done; }
1226 1 ▶ if (left != 0) { A->rate_release(rl); SA_STEP(19); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (left != 0) { A->rate_release(rl); SA_STEP(19); goto done; } 0 1 1
1227  
1228 /* Spent. The fourth is refused and told when to come back. */
1229 1 ▶ if (A->rate_allow(rl, "k", 1, 1, &left, &retry))
Branch: 1 branch
BranchTrueFalseTotal
if (A->rate_allow(rl, "k", 1, 1, &left, &retry)) 0 1 1
1230 0 { A->rate_release(rl); SA_STEP(19); goto done; }
1231 1 ▶ if (retry == 0) { A->rate_release(rl); SA_STEP(19); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (retry == 0) { A->rate_release(rl); SA_STEP(19); goto done; } 0 1 1
1232  
1233 /* Another key has its own bucket, and peeking spends nothing. */
1234 1 ▶ if (!A->rate_peek(rl, "other", 5, &left, &retry))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->rate_peek(rl, "other", 5, &left, &retry)) 0 1 1
1235 0 { A->rate_release(rl); SA_STEP(19); goto done; }
1236 1 ▶ if (left != 3) { A->rate_release(rl); SA_STEP(19); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (left != 3) { A->rate_release(rl); SA_STEP(19); goto done; } 0 1 1
1237 1 ▶ if (!A->rate_allow(rl, "other", 5, 3, &left, &retry))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->rate_allow(rl, "other", 5, 3, &left, &retry)) 0 1 1
1238 0 { A->rate_release(rl); SA_STEP(19); goto done; }
1239  
1240 1 A->rate_reset(rl, "k", 1);
1241 1 ▶ if (!A->rate_allow(rl, "k", 1, 1, &left, &retry))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->rate_allow(rl, "k", 1, 1, &left, &retry)) 0 1 1
1242 0 { A->rate_release(rl); SA_STEP(19); goto done; }
1243 1 ▶ if (A->rate_used(rl) < 2) { A->rate_release(rl); SA_STEP(19); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->rate_used(rl) < 2) { A->rate_release(rl); SA_STEP(19); goto done; } 0 1 1
1244  
1245 1 A->rate_forget(rl, "k", 1);
1246 1 A->rate_release(rl);
1247 }
1248  
1249 /* 20: the count-min sketch. A cramped one on purpose - three rows of 64 -
1250 * so the collisions the structure exists to tolerate actually happen, and
1251 * the assertion is the guarantee rather than the arithmetic: never low. */
1252 {
1253 1 sa_cms *cm = A->cms_open(r, "selm", 4, 0.0, 0.0, 3, 64, &err);
1254 char kb[16];
1255 1 int i, low = 0;
1256  
1257 1 ▶ if (!cm || err != SA_E_OK) { SA_STEP(20); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!cm || err != SA_E_OK) { SA_STEP(20); goto done; } 1 0 1
if (!cm || err != SA_E_OK) { SA_STEP(20); goto done; } 0 1 1
1258 1 ▶ if (A->cms_rows(cm) != 3 || A->cms_width(cm) != 64)
Branch: 2 branches
BranchTrueFalseTotal
if (A->cms_rows(cm) != 3 || A->cms_width(cm) != 64) 1 0 1
if (A->cms_rows(cm) != 3 || A->cms_width(cm) != 64) 0 1 1
1259 0 { A->cms_release(cm); SA_STEP(20); goto done; }
1260  
1261 /* add returns what the sketch now believes, which for the first add of
1262 * a key in an empty sketch is exactly one. */
1263 1 ▶ if (A->cms_add(cm, "one", 3, 1) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_add(cm, "one", 3, 1) != 1) 0 1 1
1264 0 { A->cms_release(cm); SA_STEP(20); goto done; }
1265 1 ▶ if (A->cms_estimate(cm, "one", 3) != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_estimate(cm, "one", 3) != 1) 0 1 1
1266 0 { A->cms_release(cm); SA_STEP(20); goto done; }
1267  
1268 501 ▶ for (i = 0; i < 500; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 500; i++) { 500 1 501
1269 500 int n = sprintf(kb, "c%d", i);
1270 500 (void)A->cms_add(cm, kb, (uint32_t)n, (uint64_t)(i % 7) + 1);
1271 }
1272 /* NEVER LOW, however hard the collisions push. */
1273 501 ▶ for (i = 0; i < 500; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 500; i++) { 500 1 501
1274 500 int n = sprintf(kb, "c%d", i);
1275 500 ▶ if (A->cms_estimate(cm, kb, (uint32_t)n) < (uint64_t)(i % 7) + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_estimate(cm, kb, (uint32_t)n) < (uint64_t)(i % 7) + 1) 0 500 500
1276 0 low++;
1277 }
1278 1 ▶ if (low) { A->cms_release(cm); SA_STEP(20); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (low) { A->cms_release(cm); SA_STEP(20); goto done; } 0 1 1
1279  
1280 1 ▶ if (A->cms_total(cm) == 0) { A->cms_release(cm); SA_STEP(20); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_total(cm) == 0) { A->cms_release(cm); SA_STEP(20); goto done; } 0 1 1
1281 1 ▶ if (A->cms_error(cm) == 0) { A->cms_release(cm); SA_STEP(20); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_error(cm) == 0) { A->cms_release(cm); SA_STEP(20); goto done; } 0 1 1
1282  
1283 1 A->cms_reset(cm);
1284 1 ▶ if (A->cms_estimate(cm, "one", 3) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_estimate(cm, "one", 3) != 0) 0 1 1
1285 0 { A->cms_release(cm); SA_STEP(20); goto done; }
1286 1 ▶ if (A->cms_total(cm) != 0) { A->cms_release(cm); SA_STEP(20); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->cms_total(cm) != 0) { A->cms_release(cm); SA_STEP(20); goto done; } 0 1 1
1287  
1288 1 A->cms_release(cm);
1289 }
1290  
1291 /* 21: queue groups. THE ASSERTION IS THE PARTITION - two handles on ONE
1292 * group must divide the records between them, never both get the same one.
1293 * A group that broadcast would pass a test of counts and fail this. */
1294 {
1295 1 sa_ring *gr = A->ring_open(r, "gring", 5, 64, 256, &err);
1296 1 sa_group_h *ga = NULL, *gb = NULL;
1297 sa_group_counts gc;
1298 1 uint64_t s1 = 0, s2 = 0, s3 = 0;
1299 1 const char *tp = NULL, *dp = NULL;
1300 1 uint32_t tl = 0, dl = 0, fl = 0;
1301  
1302 1 ▶ if (!gr || err != SA_E_OK) { SA_STEP(21); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!gr || err != SA_E_OK) { SA_STEP(21); goto done; } 1 0 1
if (!gr || err != SA_E_OK) { SA_STEP(21); goto done; } 0 1 1
1303  
1304 1 ga = A->group_open(gr, "pool", 4, NULL, 0, 0, &err);
1305 1 gb = A->group_open(gr, "pool", 4, NULL, 0, 0, &err);
1306 1 ▶ if (!ga || !gb || err != SA_E_OK) {
Branch: 3 branches
BranchTrueFalseTotal
if (!ga || !gb || err != SA_E_OK) { 1 0 1
if (!ga || !gb || err != SA_E_OK) { 1 0 1
if (!ga || !gb || err != SA_E_OK) { 0 1 1
1307 0 ▶ if (ga) A->group_release(ga);
Branch: 1 branch
BranchTrueFalseTotal
if (ga) A->group_release(ga); 0 0 0
1308 0 ▶ if (gb) A->group_release(gb);
Branch: 1 branch
BranchTrueFalseTotal
if (gb) A->group_release(gb); 0 0 0
1309 0 A->ring_release(gr); SA_STEP(21); goto done;
1310 }
1311  
1312 /* Nothing published: a group claims nothing rather than blocking. */
1313 1 ▶ if (A->group_claim(ga, &s1, &tp, &tl, &dp, &dl, &fl) == SA_READ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (A->group_claim(ga, &s1, &tp, &tl, &dp, &dl, &fl) == SA_READ_OK) { 0 1 1
1314 0 A->group_release(ga); A->group_release(gb);
1315 0 A->ring_release(gr); SA_STEP(21); goto done;
1316 }
1317  
1318 1 ▶ if (A->publish(gr, "t", 1, "one", 3) < 0
Branch: 1 branch
BranchTrueFalseTotal
if (A->publish(gr, "t", 1, "one", 3) < 0 1 0 1
1319 1 ▶ || A->publish(gr, "t", 1, "two", 3) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
|| A->publish(gr, "t", 1, "two", 3) < 0) { 0 1 1
1320 0 A->group_release(ga); A->group_release(gb);
1321 0 A->ring_release(gr); SA_STEP(21); goto done;
1322 }
1323  
1324 1 ▶ if (A->group_claim(ga, &s1, &tp, &tl, &dp, &dl, &fl) != SA_READ_OK
Branch: 1 branch
BranchTrueFalseTotal
if (A->group_claim(ga, &s1, &tp, &tl, &dp, &dl, &fl) != SA_READ_OK 1 0 1
1325 1 ▶ || A->group_claim(gb, &s2, &tp, &tl, &dp, &dl, &fl) != SA_READ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
|| A->group_claim(gb, &s2, &tp, &tl, &dp, &dl, &fl) != SA_READ_OK) { 0 1 1
1326 0 A->group_release(ga); A->group_release(gb);
1327 0 A->ring_release(gr); SA_STEP(21); goto done;
1328 }
1329 /* DIFFERENT records: one each, which is the whole mechanism. */
1330 1 ▶ if (s1 == s2) {
Branch: 1 branch
BranchTrueFalseTotal
if (s1 == s2) { 0 1 1
1331 0 A->group_release(ga); A->group_release(gb);
1332 0 A->ring_release(gr); SA_STEP(21); goto done;
1333 }
1334 /* And the third claim finds the queue empty. */
1335 1 ▶ if (A->group_claim(ga, &s3, &tp, &tl, &dp, &dl, &fl) == SA_READ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (A->group_claim(ga, &s3, &tp, &tl, &dp, &dl, &fl) == SA_READ_OK) { 0 1 1
1336 0 A->group_release(ga); A->group_release(gb);
1337 0 A->ring_release(gr); SA_STEP(21); goto done;
1338 }
1339  
1340 1 A->group_counts(ga, &gc);
1341 1 ▶ if (gc.delivered != 2 || gc.mine != 1) { /* the pool's, and ours */
Branch: 2 branches
BranchTrueFalseTotal
if (gc.delivered != 2 || gc.mine != 1) { /* the pool's, and ours */ 1 0 1
if (gc.delivered != 2 || gc.mine != 1) { /* the pool's, and ours */ 0 1 1
1342 0 A->group_release(ga); A->group_release(gb);
1343 0 A->ring_release(gr); SA_STEP(21); goto done;
1344 }
1345 1 ▶ if (A->group_position(ga) < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (A->group_position(ga) < 2) { 0 1 1
1346 0 A->group_release(ga); A->group_release(gb);
1347 0 A->ring_release(gr); SA_STEP(21); goto done;
1348 }
1349  
1350 /* A group bound to a topic takes only its own and advances past the
1351 * rest, which is what stops one pool eating another's work. */
1352 {
1353 1 sa_group_h *gt = A->group_open(gr, "jobs", 4, "j", 1, 0, &err);
1354 1 ▶ if (!gt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gt) { 0 1 1
1355 0 A->group_release(ga); A->group_release(gb);
1356 0 A->ring_release(gr); SA_STEP(21); goto done;
1357 }
1358 1 (void)A->publish(gr, "x", 1, "no", 2);
1359 1 (void)A->publish(gr, "j", 1, "yes", 3);
1360 1 ▶ if (A->group_claim(gt, &s3, &tp, &tl, &dp, &dl, &fl) != SA_READ_OK
Branch: 1 branch
BranchTrueFalseTotal
if (A->group_claim(gt, &s3, &tp, &tl, &dp, &dl, &fl) != SA_READ_OK 1 0 1
1361 1 ▶ || dl != 3 || memcmp(dp, "yes", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
|| dl != 3 || memcmp(dp, "yes", 3)) { 1 0 1
|| dl != 3 || memcmp(dp, "yes", 3)) { 0 1 1
1362 0 A->group_release(gt);
1363 0 A->group_release(ga); A->group_release(gb);
1364 0 A->ring_release(gr); SA_STEP(21); goto done;
1365 }
1366 1 A->group_counts(gt, &gc);
1367 1 ▶ if (gc.skipped != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (gc.skipped != 1) { 0 1 1
1368 0 A->group_release(gt);
1369 0 A->group_release(ga); A->group_release(gb);
1370 0 A->ring_release(gr); SA_STEP(21); goto done;
1371 }
1372 1 A->group_release(gt);
1373 }
1374  
1375 1 A->group_release(ga);
1376 1 A->group_release(gb);
1377 1 A->ring_release(gr);
1378 }
1379  
1380 /* 22: the cuckoo filter. Filled to 200 of its 256 slots, so keys may have
1381 * to move to make room, and the assertion is the guarantee rather than the
1382 * arithmetic: nothing added and not removed ever checks 0. */
1383 {
1384 1 sa_cuckoo *ck = A->cuckoo_open(r, "selk", 4, 200, &err);
1385 sa_cuckoo_counts cc;
1386 char kb[16];
1387 1 int i, n, lost = 0;
1388  
1389 1 ▶ if (!ck || err != SA_E_OK) { SA_STEP(22); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ck || err != SA_E_OK) { SA_STEP(22); goto done; } 1 0 1
if (!ck || err != SA_E_OK) { SA_STEP(22); goto done; } 0 1 1
1390 1 A->cuckoo_counts(ck, &cc);
1391 1 ▶ if (cc.slots < 200 || cc.count != 0 || cc.capacity != 200)
Branch: 3 branches
BranchTrueFalseTotal
if (cc.slots < 200 || cc.count != 0 || cc.capacity != 200) 1 0 1
if (cc.slots < 200 || cc.count != 0 || cc.capacity != 200) 1 0 1
if (cc.slots < 200 || cc.count != 0 || cc.capacity != 200) 0 1 1
1392 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1393  
1394 /* Empty: an exact no, and nothing to remove. */
1395 1 ▶ if (A->cuckoo_check(ck, "one", 3) || A->cuckoo_remove(ck, "one", 3))
Branch: 2 branches
BranchTrueFalseTotal
if (A->cuckoo_check(ck, "one", 3) || A->cuckoo_remove(ck, "one", 3)) 1 0 1
if (A->cuckoo_check(ck, "one", 3) || A->cuckoo_remove(ck, "one", 3)) 0 1 1
1396 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1397  
1398 /* In, found, out, gone - with nothing else in the filter for the
1399 * last answer to be a coincidence with. */
1400 1 ▶ if (!A->cuckoo_add(ck, "one", 3) || !A->cuckoo_check(ck, "one", 3))
Branch: 2 branches
BranchTrueFalseTotal
if (!A->cuckoo_add(ck, "one", 3) || !A->cuckoo_check(ck, "one", 3)) 1 0 1
if (!A->cuckoo_add(ck, "one", 3) || !A->cuckoo_check(ck, "one", 3)) 0 1 1
1401 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1402 1 ▶ if (!A->cuckoo_remove(ck, "one", 3) || A->cuckoo_check(ck, "one", 3))
Branch: 2 branches
BranchTrueFalseTotal
if (!A->cuckoo_remove(ck, "one", 3) || A->cuckoo_check(ck, "one", 3)) 1 0 1
if (!A->cuckoo_remove(ck, "one", 3) || A->cuckoo_check(ck, "one", 3)) 0 1 1
1403 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1404  
1405 201 ▶ for (i = 0; i < 200; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 200; i++) { 200 1 201
1406 200 n = sprintf(kb, "k%d", i);
1407 200 ▶ if (!A->cuckoo_add(ck, kb, (uint32_t)n))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->cuckoo_add(ck, kb, (uint32_t)n)) 0 200 200
1408 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1409 }
1410 201 ▶ for (i = 0; i < 200; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 200; i++) { 200 1 201
1411 200 n = sprintf(kb, "k%d", i);
1412 200 ▶ if (!A->cuckoo_check(ck, kb, (uint32_t)n)) lost++;
Branch: 1 branch
BranchTrueFalseTotal
if (!A->cuckoo_check(ck, kb, (uint32_t)n)) lost++; 0 200 200
1413 }
1414 1 ▶ if (lost) { A->cuckoo_release(ck); SA_STEP(22); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (lost) { A->cuckoo_release(ck); SA_STEP(22); goto done; } 0 1 1
1415  
1416 1 A->cuckoo_counts(ck, &cc);
1417 1 ▶ if (cc.count != 200 || cc.full != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (cc.count != 200 || cc.full != 0) 1 0 1
if (cc.count != 200 || cc.full != 0) 0 1 1
1418 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1419  
1420 1 A->cuckoo_reset(ck);
1421 1 A->cuckoo_counts(ck, &cc);
1422 1 ▶ if (cc.count != 0 || A->cuckoo_check(ck, "k7", 2))
Branch: 2 branches
BranchTrueFalseTotal
if (cc.count != 0 || A->cuckoo_check(ck, "k7", 2)) 1 0 1
if (cc.count != 0 || A->cuckoo_check(ck, "k7", 2)) 0 1 1
1423 0 { A->cuckoo_release(ck); SA_STEP(22); goto done; }
1424  
1425 1 A->cuckoo_release(ck);
1426 }
1427  
1428 /* 23: the lease. One process cannot demonstrate handoff to itself - the
1429 * holder is keyed on pid, so two handles here both "hold" it - so this
1430 * proves the single-process semantics and every table entry, and leaves
1431 * cross-process stealing to a fork test. */
1432 {
1433 1 sa_lease *ls = A->lease_open(r, "sele", 4, &err);
1434 1 int held = 0, stole = 0;
1435 uint64_t f1, f2;
1436  
1437 1 ▶ if (!ls || err != SA_E_OK) { SA_STEP(23); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!ls || err != SA_E_OK) { SA_STEP(23); goto done; } 1 0 1
if (!ls || err != SA_E_OK) { SA_STEP(23); goto done; } 0 1 1
1438  
1439 1 held = A->lease_acquire(ls, 30000, &stole);
1440 1 ▶ if (!held || stole) { A->lease_release_handle(ls); SA_STEP(23); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!held || stole) { A->lease_release_handle(ls); SA_STEP(23); goto done; } 1 0 1
if (!held || stole) { A->lease_release_handle(ls); SA_STEP(23); goto done; } 0 1 1
1441 1 ▶ if (!A->lease_mine(ls)) { A->lease_release_handle(ls); SA_STEP(23); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->lease_mine(ls)) { A->lease_release_handle(ls); SA_STEP(23); goto done; } 0 1 1
1442 1 ▶ if (!A->lease_holder(ls)) { A->lease_release_handle(ls); SA_STEP(23); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->lease_holder(ls)) { A->lease_release_handle(ls); SA_STEP(23); goto done; } 0 1 1
1443 1 f1 = A->lease_fence(ls);
1444 1 ▶ if (!f1) { A->lease_release_handle(ls); SA_STEP(23); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!f1) { A->lease_release_handle(ls); SA_STEP(23); goto done; } 0 1 1
1445  
1446 1 ▶ if (!A->lease_renew(ls, 30000))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->lease_renew(ls, 30000)) 0 1 1
1447 0 { A->lease_release_handle(ls); SA_STEP(23); goto done; }
1448 1 ▶ if (A->lease_fence(ls) != f1) /* renew keeps the tenure */
Branch: 1 branch
BranchTrueFalseTotal
if (A->lease_fence(ls) != f1) /* renew keeps the tenure */ 0 1 1
1449 0 { A->lease_release_handle(ls); SA_STEP(23); goto done; }
1450  
1451 1 ▶ if (!A->lease_release(ls))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->lease_release(ls)) 0 1 1
1452 0 { A->lease_release_handle(ls); SA_STEP(23); goto done; }
1453 1 ▶ if (A->lease_mine(ls) || A->lease_holder(ls))
Branch: 2 branches
BranchTrueFalseTotal
if (A->lease_mine(ls) || A->lease_holder(ls)) 1 0 1
if (A->lease_mine(ls) || A->lease_holder(ls)) 0 1 1
1454 0 { A->lease_release_handle(ls); SA_STEP(23); goto done; }
1455  
1456 /* Re-acquiring a lease we just released is a new tenure: the fence must
1457 * move, or a superseded holder could not be told from a current one. */
1458 1 ▶ if (!A->lease_acquire(ls, 30000, &stole))
Branch: 1 branch
BranchTrueFalseTotal
if (!A->lease_acquire(ls, 30000, &stole)) 0 1 1
1459 0 { A->lease_release_handle(ls); SA_STEP(23); goto done; }
1460 1 f2 = A->lease_fence(ls);
1461 1 ▶ if (f2 <= f1) { A->lease_release_handle(ls); SA_STEP(23); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (f2 <= f1) { A->lease_release_handle(ls); SA_STEP(23); goto done; } 0 1 1
1462  
1463 1 A->lease_release(ls);
1464 1 A->lease_release_handle(ls);
1465 }
1466  
1467 /* 24: the scoreboard. One process claims a row, writes it coherently
1468 * through the begin/set/end trio, and reads it back - the cross-worker
1469 * exclusion and reclaim are a fork test's job. */
1470 {
1471 const char *flds[2];
1472 sa_sb *sb;
1473 void *row;
1474 sa_sb_reading rd;
1475 int fi_a, fi_b;
1476  
1477 1 flds[0] = "a"; flds[1] = "b";
1478 1 sb = A->sb_open(r, "sele-sb", 7, flds, 2, 8, &err);
1479 1 ▶ if (!sb || err != SA_E_OK) { SA_STEP(24); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!sb || err != SA_E_OK) { SA_STEP(24); goto done; } 1 0 1
if (!sb || err != SA_E_OK) { SA_STEP(24); goto done; } 0 1 1
1480 1 ▶ if (A->sb_slots(sb) != 8 || A->sb_nfields(sb) != 2)
Branch: 2 branches
BranchTrueFalseTotal
if (A->sb_slots(sb) != 8 || A->sb_nfields(sb) != 2) 1 0 1
if (A->sb_slots(sb) != 8 || A->sb_nfields(sb) != 2) 0 1 1
1481 0 { A->sb_release(sb); SA_STEP(24); goto done; }
1482  
1483 1 fi_a = A->sb_field(sb, "a", 1);
1484 1 fi_b = A->sb_field(sb, "b", 1);
1485 1 ▶ if (fi_a < 0 || fi_b < 0 || A->sb_field(sb, "nope", 4) >= 0)
Branch: 3 branches
BranchTrueFalseTotal
if (fi_a < 0 || fi_b < 0 || A->sb_field(sb, "nope", 4) >= 0) 1 0 1
if (fi_a < 0 || fi_b < 0 || A->sb_field(sb, "nope", 4) >= 0) 1 0 1
if (fi_a < 0 || fi_b < 0 || A->sb_field(sb, "nope", 4) >= 0) 0 1 1
1486 0 { A->sb_release(sb); SA_STEP(24); goto done; }
1487 1 ▶ if (strcmp(A->sb_field_name(sb, 0), "a")
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(A->sb_field_name(sb, 0), "a") 1 0 1
1488 1 ▶ || strcmp(A->sb_field_name(sb, 1), "b"))
Branch: 1 branch
BranchTrueFalseTotal
|| strcmp(A->sb_field_name(sb, 1), "b")) 0 1 1
1489 0 { A->sb_release(sb); SA_STEP(24); goto done; }
1490  
1491 1 ▶ if (A->sb_take(sb) < 0) { A->sb_release(sb); SA_STEP(24); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->sb_take(sb) < 0) { A->sb_release(sb); SA_STEP(24); goto done; } 0 1 1
1492  
1493 1 row = A->sb_begin(sb);
1494 1 ▶ if (!row) { A->sb_release(sb); SA_STEP(24); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!row) { A->sb_release(sb); SA_STEP(24); goto done; } 0 1 1
1495 1 A->sb_set_gauge(row, fi_a, 100);
1496 1 A->sb_set_gauge(row, fi_b, 7);
1497 1 A->sb_set_status(row, "busy", 4);
1498 1 A->sb_end(sb, row);
1499  
1500 1 row = A->sb_begin(sb);
1501 1 ▶ if (row) { A->sb_add_gauge(row, fi_b, 5); A->sb_end(sb, row); }
Branch: 1 branch
BranchTrueFalseTotal
if (row) { A->sb_add_gauge(row, fi_b, 5); A->sb_end(sb, row); } 1 0 1
1502  
1503 /* Row 0 is ours; read it back coherently. */
1504 1 ▶ if (!A->sb_read(sb, 0, &rd)) { A->sb_release(sb); SA_STEP(24); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->sb_read(sb, 0, &rd)) { A->sb_release(sb); SA_STEP(24); goto done; } 0 1 1
1505 1 ▶ if (rd.gauges[fi_a] != 100 || rd.gauges[fi_b] != 12)
Branch: 2 branches
BranchTrueFalseTotal
if (rd.gauges[fi_a] != 100 || rd.gauges[fi_b] != 12) 1 0 1
if (rd.gauges[fi_a] != 100 || rd.gauges[fi_b] != 12) 0 1 1
1506 0 { A->sb_release(sb); SA_STEP(24); goto done; }
1507 1 ▶ if (rd.statuslen != 4 || memcmp(rd.status, "busy", 4))
Branch: 2 branches
BranchTrueFalseTotal
if (rd.statuslen != 4 || memcmp(rd.status, "busy", 4)) 1 0 1
if (rd.statuslen != 4 || memcmp(rd.status, "busy", 4)) 0 1 1
1508 0 { A->sb_release(sb); SA_STEP(24); goto done; }
1509 1 ▶ if (!rd.alive) /* the writer - us - is running */
Branch: 1 branch
BranchTrueFalseTotal
if (!rd.alive) /* the writer - us - is running */ 0 1 1
1510 0 { A->sb_release(sb); SA_STEP(24); goto done; }
1511  
1512 1 A->sb_release(sb);
1513 }
1514  
1515 /* 25: the HyperLogLog. Add a thousand distinct keys at p = 10 and the
1516 * estimate lands within its error bound; add them all AGAIN and it does
1517 * not move at all, which is the property the sketch exists for. Then a
1518 * merge widens it and a reset empties it. */
1519 {
1520 sa_hll *g1, *g2;
1521 /* volatile, so each estimate is stored as a 64-bit double before it
1522 * is compared. On i386 a double comes back on the x87 stack with 80
1523 * bits of precision, and `c1b != c1` compared a c1 spilled to memory
1524 * across a thousand calls with a c1b still in the register: the same
1525 * computation, unequal on both 32-bit Linux smokers. */
1526 volatile double c1, c1b, c2;
1527 char kb[32];
1528 int n, i;
1529  
1530 1 g1 = A->hll_open(r, "selg", 4, 10, &err);
1531 1 ▶ if (!g1 || err != SA_E_OK) { SA_STEP(25); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!g1 || err != SA_E_OK) { SA_STEP(25); goto done; } 1 0 1
if (!g1 || err != SA_E_OK) { SA_STEP(25); goto done; } 0 1 1
1532 1 ▶ if (A->hll_precision(g1) != 10) { A->hll_release(g1); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->hll_precision(g1) != 10) { A->hll_release(g1); SA_STEP(25); goto done; } 0 1 1
1533 1 ▶ if (A->hll_count(g1) != 0.0) { A->hll_release(g1); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->hll_count(g1) != 0.0) { A->hll_release(g1); SA_STEP(25); goto done; } 0 1 1
1534  
1535 1001 ▶ for (i = 0; i < 1000; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 1000; i++) { 1000 1 1001
1536 1000 n = sprintf(kb, "d%d", i);
1537 1000 A->hll_add(g1, kb, (size_t)n);
1538 }
1539 1 c1 = A->hll_count(g1);
1540 /* p = 10 is 3.25% one sigma; 10% is past three, and deterministic. */
1541 1 ▶ if (c1 < 900.0 || c1 > 1100.0) { A->hll_release(g1); SA_STEP(25); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (c1 < 900.0 || c1 > 1100.0) { A->hll_release(g1); SA_STEP(25); goto done; } 1 0 1
if (c1 < 900.0 || c1 > 1100.0) { A->hll_release(g1); SA_STEP(25); goto done; } 0 1 1
1542 1 ▶ if (!A->hll_filled(g1)) { A->hll_release(g1); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->hll_filled(g1)) { A->hll_release(g1); SA_STEP(25); goto done; } 0 1 1
1543  
1544 1001 ▶ for (i = 0; i < 1000; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 1000; i++) { 1000 1 1001
1545 1000 n = sprintf(kb, "d%d", i);
1546 1000 ▶ if (A->hll_add(g1, kb, (size_t)n)) /* a repeat never changes it */
Branch: 1 branch
BranchTrueFalseTotal
if (A->hll_add(g1, kb, (size_t)n)) /* a repeat never changes it */ 0 1000 1000
1547 0 { A->hll_release(g1); SA_STEP(25); goto done; }
1548 }
1549 1 c1b = A->hll_count(g1);
1550 1 ▶ if (c1b != c1) { A->hll_release(g1); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (c1b != c1) { A->hll_release(g1); SA_STEP(25); goto done; } 0 1 1
1551  
1552 /* A second sketch of OTHER keys, merged in: the union is bigger. A
1553 * precision of 0 must inherit the existing sketch's rather than
1554 * default to 14 and refuse the shape. */
1555 1 g2 = A->hll_open(r, "selg2", 5, 10, &err);
1556 1 ▶ if (!g2) { A->hll_release(g1); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!g2) { A->hll_release(g1); SA_STEP(25); goto done; } 0 1 1
1557 1001 ▶ for (i = 0; i < 1000; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 1000; i++) { 1000 1 1001
1558 1000 n = sprintf(kb, "e%d", i);
1559 1000 A->hll_add(g2, kb, (size_t)n);
1560 }
1561 1 ▶ if (!A->hll_merge(g1, g2)) { A->hll_release(g1); A->hll_release(g2); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (!A->hll_merge(g1, g2)) { A->hll_release(g1); A->hll_release(g2); SA_STEP(25); goto done; } 0 1 1
1562 1 c2 = A->hll_count(g1);
1563 1 ▶ if (c2 < 1800.0 || c2 > 2200.0) { A->hll_release(g1); A->hll_release(g2); SA_STEP(25); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (c2 < 1800.0 || c2 > 2200.0) { A->hll_release(g1); A->hll_release(g2); SA_STEP(25); goto done; } 1 0 1
if (c2 < 1800.0 || c2 > 2200.0) { A->hll_release(g1); A->hll_release(g2); SA_STEP(25); goto done; } 0 1 1
1564 1 A->hll_release(g2);
1565 1 g2 = A->hll_open(r, "selg2", 5, 0, &err); /* inherit p = 10 */
1566 1 ▶ if (!g2 || A->hll_precision(g2) != 10)
Branch: 2 branches
BranchTrueFalseTotal
if (!g2 || A->hll_precision(g2) != 10) 1 0 1
if (!g2 || A->hll_precision(g2) != 10) 0 1 1
1567 0 ▶ { A->hll_release(g1); if (g2) A->hll_release(g2); SA_STEP(25); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
{ A->hll_release(g1); if (g2) A->hll_release(g2); SA_STEP(25); goto done; } 0 0 0
1568 1 A->hll_release(g2);
1569  
1570 1 A->hll_reset(g1);
1571 1 ▶ if (A->hll_count(g1) != 0.0 || A->hll_filled(g1))
Branch: 2 branches
BranchTrueFalseTotal
if (A->hll_count(g1) != 0.0 || A->hll_filled(g1)) 1 0 1
if (A->hll_count(g1) != 0.0 || A->hll_filled(g1)) 0 1 1
1572 0 { A->hll_release(g1); SA_STEP(25); goto done; }
1573 1 A->hll_release(g1);
1574 }
1575  
1576 /* 26: the wakeup. Two pipes, this process takes slot 0, and a publish from
1577 * this same process pokes nobody: the publisher is excluded and slot 1 has
1578 * no owner. So the descriptor must NOT be readable afterwards, which is
1579 * the one thing a single process can prove about the mechanism; t/12 has
1580 * the rest. Drained is idempotent. On Windows every entry answers -1. */
1581 {
1582 #if defined(_WIN32)
1583 if (A->wake_init(r, 2) != 0) { SA_STEP(26); goto done; }
1584 if (A->wake_take(r, 0) != -1 || A->wake_fd(r) != -1)
1585 { SA_STEP(26); goto done; }
1586 A->wake_drained(r);
1587 #else
1588 int fd;
1589 char junk[8];
1590 1 ▶ if (A->wake_init(r, 2) != 1) { SA_STEP(26); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->wake_init(r, 2) != 1) { SA_STEP(26); goto done; } 0 1 1
1591 1 ▶ if (A->wake_take(r, 0) != 0) { SA_STEP(26); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->wake_take(r, 0) != 0) { SA_STEP(26); goto done; } 0 1 1
1592 1 fd = A->wake_fd(r);
1593 1 ▶ if (fd < 0) { SA_STEP(26); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { SA_STEP(26); goto done; } 0 1 1
1594 /* Past the slots given pipes: refused, never standard input. */
1595 1 ▶ if (A->wake_take(r, 2) != -1) { SA_STEP(26); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->wake_take(r, 2) != -1) { SA_STEP(26); goto done; } 0 1 1
1596 1 ▶ if (!SA_PUBLISHED(A->publish(ring, "t", 1, "poke", 4)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SA_PUBLISHED(A->publish(ring, "t", 1, "poke", 4))) 0 1 1
1597 0 { SA_STEP(26); goto done; }
1598 1 ▶ if (read(fd, junk, sizeof junk) > 0) { SA_STEP(26); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (read(fd, junk, sizeof junk) > 0) { SA_STEP(26); goto done; } 0 1 1
1599 1 A->wake_drained(r);
1600 1 A->wake_drained(r);
1601 1 ▶ if (A->wake_fd(r) != fd) { SA_STEP(26); goto done; }
Branch: 1 branch
BranchTrueFalseTotal
if (A->wake_fd(r) != fd) { SA_STEP(26); goto done; } 0 1 1
1602 #endif
1603 }
1604  
1605 /* 27: a counter with a deadline. The proof is that the deadline does not
1606 * SLIDE: a live counter keeps the one it was created with, however long a
1607 * ttl a later increment carries, and lapses when that first one is up. */
1608 {
1609 1 sa_hash *mp = A->map_open(r, "selt", 4, 16, 128, &err);
1610 1 uint64_t now = 0;
1611 1 ▶ if (!mp || err != SA_E_OK) { SA_STEP(27); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!mp || err != SA_E_OK) { SA_STEP(27); goto done; } 1 0 1
if (!mp || err != SA_E_OK) { SA_STEP(27); goto done; } 0 1 1
1612 1 ▶ if (A->map_incr_ttl(mp, "w", 1, 1, 1, &now) != SA_MAP_STORE_OK || now != 1)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_ttl(mp, "w", 1, 1, 1, &now) != SA_MAP_STORE_OK || now != 1) 1 0 1
if (A->map_incr_ttl(mp, "w", 1, 1, 1, &now) != SA_MAP_STORE_OK || now != 1) 0 1 1
1613 0 { A->map_release(mp); SA_STEP(27); goto done; }
1614 1 ▶ if (A->map_incr_ttl(mp, "w", 1, 1, 3600000ULL, &now) != SA_MAP_STORE_OK
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_incr_ttl(mp, "w", 1, 1, 3600000ULL, &now) != SA_MAP_STORE_OK 1 0 1
1615 1 ▶ || now != 2)
Branch: 1 branch
BranchTrueFalseTotal
|| now != 2) 0 1 1
1616 0 { A->map_release(mp); SA_STEP(27); goto done; }
1617 1 sa_stall(20000);
1618 /* The 1ms deadline from the create stood; the hour from the second
1619 * call was ignored. Reset, so it starts again at `by`. */
1620 1 ▶ if (A->map_incr_ttl(mp, "w", 1, 3, 3600000ULL, &now) != SA_MAP_STORE_OK
Branch: 1 branch
BranchTrueFalseTotal
if (A->map_incr_ttl(mp, "w", 1, 3, 3600000ULL, &now) != SA_MAP_STORE_OK 1 0 1
1621 1 ▶ || now != 3)
Branch: 1 branch
BranchTrueFalseTotal
|| now != 3) 0 1 1
1622 0 { A->map_release(mp); SA_STEP(27); goto done; }
1623 /* A ttl of 0 is a counter that never lapses, as map_incr makes. */
1624 1 ▶ if (A->map_incr_ttl(mp, "p", 1, 1, 0, &now) != SA_MAP_STORE_OK || now != 1)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_ttl(mp, "p", 1, 1, 0, &now) != SA_MAP_STORE_OK || now != 1) 1 0 1
if (A->map_incr_ttl(mp, "p", 1, 1, 0, &now) != SA_MAP_STORE_OK || now != 1) 0 1 1
1625 0 { A->map_release(mp); SA_STEP(27); goto done; }
1626 1 ▶ if (A->map_incr(mp, "p", 1, 1, &now) != SA_MAP_STORE_OK || now != 2)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr(mp, "p", 1, 1, &now) != SA_MAP_STORE_OK || now != 2) 1 0 1
if (A->map_incr(mp, "p", 1, 1, &now) != SA_MAP_STORE_OK || now != 2) 0 1 1
1627 0 { A->map_release(mp); SA_STEP(27); goto done; }
1628 1 A->map_release(mp);
1629 }
1630  
1631 /* 28: the limiter's account of itself: three allowed, one denied, and the
1632 * two counts that mean "the table is too small" both zero. */
1633 {
1634 1 sa_rate *rl = A->rate_open(r, "selq", 4, 3, 3600000, 64, &err);
1635 sa_rate_counts rc;
1636 1 uint64_t left = 0, retry = 0;
1637 int i;
1638 1 ▶ if (!rl || err != SA_E_OK) { SA_STEP(28); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!rl || err != SA_E_OK) { SA_STEP(28); goto done; } 1 0 1
if (!rl || err != SA_E_OK) { SA_STEP(28); goto done; } 0 1 1
1639 5 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 4 1 5
1640 4 (void)A->rate_allow(rl, "k", 1, 1, &left, &retry);
1641 1 A->rate_counts(rl, &rc);
1642 1 ▶ if (rc.allowed != 3 || rc.denied != 1 || rc.evicted || rc.contended)
Branch: 4 branches
BranchTrueFalseTotal
if (rc.allowed != 3 || rc.denied != 1 || rc.evicted || rc.contended) 1 0 1
if (rc.allowed != 3 || rc.denied != 1 || rc.evicted || rc.contended) 1 0 1
if (rc.allowed != 3 || rc.denied != 1 || rc.evicted || rc.contended) 1 0 1
if (rc.allowed != 3 || rc.denied != 1 || rc.evicted || rc.contended) 0 1 1
1643 0 { A->rate_release(rl); SA_STEP(28); goto done; }
1644 1 A->rate_counts(NULL, &rc);
1645 1 ▶ if (rc.allowed || rc.denied) { A->rate_release(rl); SA_STEP(28); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (rc.allowed || rc.denied) { A->rate_release(rl); SA_STEP(28); goto done; } 1 0 1
if (rc.allowed || rc.denied) { A->rate_release(rl); SA_STEP(28); goto done; } 0 1 1
1646 1 A->rate_release(rl);
1647 }
1648  
1649 /* 29: the caller's clock. With a clock the selftest controls, the whole
1650 * window is deterministic: created at 1000 with 5ms to live, still live at
1651 * 1004, lapsed at 1005 and reset to `by` with a deadline from THAT reading
1652 * (1010), so 1009 adds and 1010 resets again. A clock of 0 means the
1653 * map's own, which is what map_incr_ttl passes. */
1654 {
1655 1 sa_hash *mp = A->map_open(r, "selat", 5, 16, 128, &err);
1656 1 uint64_t now = 0;
1657 1 ▶ if (!mp || err != SA_E_OK) { SA_STEP(29); goto done; }
Branch: 2 branches
BranchTrueFalseTotal
if (!mp || err != SA_E_OK) { SA_STEP(29); goto done; } 1 0 1
if (!mp || err != SA_E_OK) { SA_STEP(29); goto done; } 0 1 1
1658 1 ▶ if (A->map_incr_at(mp, "a", 1, 1, 5, 1000, &now) != SA_MAP_STORE_OK || now != 1)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "a", 1, 1, 5, 1000, &now) != SA_MAP_STORE_OK || now != 1) 1 0 1
if (A->map_incr_at(mp, "a", 1, 1, 5, 1000, &now) != SA_MAP_STORE_OK || now != 1) 0 1 1
1659 0 { A->map_release(mp); SA_STEP(29); goto done; }
1660 1 ▶ if (A->map_incr_at(mp, "a", 1, 1, 5, 1004, &now) != SA_MAP_STORE_OK || now != 2)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "a", 1, 1, 5, 1004, &now) != SA_MAP_STORE_OK || now != 2) 1 0 1
if (A->map_incr_at(mp, "a", 1, 1, 5, 1004, &now) != SA_MAP_STORE_OK || now != 2) 0 1 1
1661 0 { A->map_release(mp); SA_STEP(29); goto done; }
1662 1 ▶ if (A->map_incr_at(mp, "a", 1, 3, 5, 1005, &now) != SA_MAP_STORE_OK || now != 3)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "a", 1, 3, 5, 1005, &now) != SA_MAP_STORE_OK || now != 3) 1 0 1
if (A->map_incr_at(mp, "a", 1, 3, 5, 1005, &now) != SA_MAP_STORE_OK || now != 3) 0 1 1
1663 0 { A->map_release(mp); SA_STEP(29); goto done; }
1664 1 ▶ if (A->map_incr_at(mp, "a", 1, 1, 5, 1009, &now) != SA_MAP_STORE_OK || now != 4)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "a", 1, 1, 5, 1009, &now) != SA_MAP_STORE_OK || now != 4) 1 0 1
if (A->map_incr_at(mp, "a", 1, 1, 5, 1009, &now) != SA_MAP_STORE_OK || now != 4) 0 1 1
1665 0 { A->map_release(mp); SA_STEP(29); goto done; }
1666 1 ▶ if (A->map_incr_at(mp, "a", 1, 1, 5, 1010, &now) != SA_MAP_STORE_OK || now != 1)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "a", 1, 1, 5, 1010, &now) != SA_MAP_STORE_OK || now != 1) 1 0 1
if (A->map_incr_at(mp, "a", 1, 1, 5, 1010, &now) != SA_MAP_STORE_OK || now != 1) 0 1 1
1667 0 { A->map_release(mp); SA_STEP(29); goto done; }
1668 /* No deadline, no clock: a plain counter whatever `now_ms` says. */
1669 1 ▶ if (A->map_incr_at(mp, "p", 1, 2, 0, 1, &now) != SA_MAP_STORE_OK || now != 2)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "p", 1, 2, 0, 1, &now) != SA_MAP_STORE_OK || now != 2) 1 0 1
if (A->map_incr_at(mp, "p", 1, 2, 0, 1, &now) != SA_MAP_STORE_OK || now != 2) 0 1 1
1670 0 { A->map_release(mp); SA_STEP(29); goto done; }
1671 1 ▶ if (A->map_incr_at(mp, "p", 1, 2, 0, 0, &now) != SA_MAP_STORE_OK || now != 4)
Branch: 2 branches
BranchTrueFalseTotal
if (A->map_incr_at(mp, "p", 1, 2, 0, 0, &now) != SA_MAP_STORE_OK || now != 4) 1 0 1
if (A->map_incr_at(mp, "p", 1, 2, 0, 0, &now) != SA_MAP_STORE_OK || now != 4) 0 1 1
1672 0 { A->map_release(mp); SA_STEP(29); goto done; }
1673 1 A->map_release(mp);
1674 }
1675  
1676 1 done:
1677 1 ▶ if (cur) A->cursor_release(cur);
Branch: 1 branch
BranchTrueFalseTotal
if (cur) A->cursor_release(cur); 1 0 1
1678 1 ▶ if (ring) A->ring_release(ring);
Branch: 1 branch
BranchTrueFalseTotal
if (ring) A->ring_release(ring); 1 0 1
1679 1 ▶ if (r) A->release(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) A->release(r); 1 0 1
1680 1 return stepv;
1681 }
1682  
1683 #undef SA_STEP
1684  
1685 #endif /* SA_ABI_IMPL_H */
« xs/scoreboard.xs ↑ Summary include/sa/sa_xop.h »