Shared.xs

filter: Statementstmt 99.0% 214 / 216 Branchbran 61.2% 213 / 348 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.7% 427 / 564 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ddsketch.h ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6 #include <math.h>
7 #include "ddsketch.h"
8  
9 #define EXTRACT(sv) \
10 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::DDSketch::Shared")) \
11 croak("Expected a Data::DDSketch::Shared object"); \
12 DdHandle *h = INT2PTR(DdHandle*, SvIV(SvRV(sv))); \
13 if (!h) croak("Attempted to use a destroyed Data::DDSketch::Shared object"); \
14 DdHandle *h0 = h; PERL_UNUSED_VAR(h0); \
15 sv_2mortal(SvREFCNT_inc(SvRV(sv))) /* pin the referent so a reentrant DESTROY (from overload/tie on an arg) can't free the handle mid-method */
16  
17 /* The pin above only blocks REFCOUNT-driven destruction. Perl run from argument
18 * magic can still call $obj->DESTROY explicitly, which frees the handle and
19 * zeroes the IV, leaving the local `h` dangling. Re-read it wherever such magic
20 * can intervene before `h` is used. Sources of that magic here: SvGETMAGIC on an
21 * argument, av_len on a TIED array (AvFILL -> mg_size -> FETCHSIZE), element
22 * fetches, and sv_isobject/sv_derived_from (both begin with SvGETMAGIC).
23 * The same Perl can also REPLACE the invocant ($obj = 42 mutates ST(0), because
24 * Perl passes aliases), hence the SvROK re-check before SvRV. */
25 #define REEXTRACT(sv) \
26 if (!SvROK(sv)) \
27 croak("Data::DDSketch::Shared object was replaced during the call"); \
28 h = INT2PTR(DdHandle*, SvIV(SvRV(sv))); \
29 if (h != h0) croak("Data::DDSketch::Shared object replaced or destroyed during the call")
30  
31 #define MAKE_OBJ(class, handle) \
32 SV *obj = newSViv(PTR2IV(handle)); \
33 SV *ref = newRV_noinc(obj); \
34 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
35 RETVAL = ref
36  
37 /* Re-read the class-name PV immediately before blessing the new object.
38 * `class` is captured by the typemap in the INPUT section; a tied/overloaded
39 * later constructor argument can run get-magic that reallocs or frees ST(0)'s
40 * PV, dangling that pointer before it is used to bless. Same fix as
41 * Data::CuckooFilter::Shared already carries. */
42 #define REREAD_CLASS() \
43 class = SvPV_nolen(ST(0))
44  
45 MODULE = Data::DDSketch::Shared PACKAGE = Data::DDSketch::Shared
46  
47 PROTOTYPES: DISABLE
48  
49 SV *
50 new(class, path = &PL_sv_undef, alpha = 0.01, num_buckets = 2048, ...)
51 const char *class
52 SV *path
53 double alpha
54 UV num_buckets
55 PREINIT:
56 char errbuf[DD_ERR_BUFLEN];
57 CODE:
58 /* Optional 5th arg: file mode for a newly-created file-backed segment
59 * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing.
60 * Resolve it FIRST: its get-magic runs arbitrary Perl that can realloc or
61 * free the path PV, so capture the path LAST, immediately before use. */
62 40 ▶ mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600;
Branch: 4 branches
BranchTrueFalseTotal
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 40 40
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
mode_t mode = (items > 4 && (SvGETMAGIC(ST(4)), SvOK(ST(4)))) ? (mode_t)SvUV(ST(4)) : 0600; 0 0 0
63 59 ▶ const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 40 40
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 0 0
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 19 21 40
64 40 DdHandle *h = dd_create(p, alpha, (uint64_t)num_buckets, mode, errbuf);
65 40 ▶ if (!h) croak("Data::DDSketch::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::DDSketch::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 8 32 40
if (!h) croak("Data::DDSketch::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 8 0 8
66 32 REREAD_CLASS();
67 32 MAKE_OBJ(class, h);
68 OUTPUT:
69 RETVAL
70  
71 SV *
72 new_memfd(class, name = &PL_sv_undef, alpha = 0.01, num_buckets = 2048)
73 const char *class
74 SV *name
75 double alpha
76 UV num_buckets
77 PREINIT:
78 char errbuf[DD_ERR_BUFLEN];
79 CODE:
80 3 ▶ const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */
Branch: 3 branches
BranchTrueFalseTotal
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ 0 2 2
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ 0 0 0
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; /* undef -> default label */ 1 1 2
81 2 DdHandle *h = dd_create_memfd(nm, alpha, (uint64_t)num_buckets, errbuf);
82 2 ▶ if (!h) croak("Data::DDSketch::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::DDSketch::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 2 2
if (!h) croak("Data::DDSketch::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
83 2 REREAD_CLASS();
84 2 MAKE_OBJ(class, h);
85 OUTPUT:
86 RETVAL
87  
88 SV *
89 new_from_fd(class, fd)
90 const char *class
91 int fd
92 PREINIT:
93 char errbuf[DD_ERR_BUFLEN];
94 CODE:
95 1 DdHandle *h = dd_open_fd(fd, errbuf);
96 1 ▶ if (!h) croak("Data::DDSketch::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::DDSketch::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!h) croak("Data::DDSketch::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
97 1 REREAD_CLASS();
98 1 MAKE_OBJ(class, h);
99 OUTPUT:
100 RETVAL
101  
102 SV *
103 new_readonly(class, path)
104 const char *class
105 SV *path
106 PREINIT:
107 char errbuf[DD_ERR_BUFLEN];
108 CODE:
109 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
110 ever. A sealed file is immutable and no read path writes the mapping,
111 so queries take no reader-slot / rwlock traffic and any number of
112 processes can share one PROT_READ mapping (same architecture; the
113 magic rejects a wrong-endian file). */
114 21 ▶ const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 11 11
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 0 0
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 10 1 11
115 11 ▶ if (!p) croak("Data::DDSketch::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::DDSketch::Shared->new_readonly: path is required"); 1 10 11
116 10 DdHandle *h = dd_open_readonly(p, errbuf);
117 10 ▶ if (!h) croak("Data::DDSketch::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::DDSketch::Shared->new_readonly: %s", errbuf); 5 5 10
118 5 REREAD_CLASS();
119 5 MAKE_OBJ(class, h);
120 OUTPUT:
121 RETVAL
122  
123 void
124 DESTROY(self)
125 SV *self
126 CODE:
127 42 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::DDSketch::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::DDSketch::Shared")) { 42 0 42
if (sv_isobject(self) && sv_derived_from(self, "Data::DDSketch::Shared")) { 42 0 42
128 42 DdHandle *h = INT2PTR(DdHandle*, SvIV(SvRV(self)));
129 42 ▶ if (h) { sv_setiv(SvRV(self), 0); dd_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */
Branch: 1 branch
BranchTrueFalseTotal
if (h) { sv_setiv(SvRV(self), 0); dd_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 40 2 42
130 }
131  
132 UV
133 add(self, value)
134 SV *self
135 double value
136 PREINIT:
137 33454 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 33454 0 33454
EXTRACT(self); 0 33454 33454
EXTRACT(self); 0 33454 33454
138 CODE:
139 33454 ▶ if (h->readonly) croak("Data::DDSketch::Shared->add: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::DDSketch::Shared->add: sketch is frozen (read-only)"); 2 33452 33454
140 33452 ▶ if (!isfinite(value)) /* reject NaN/Inf BEFORE the lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!isfinite(value)) /* reject NaN/Inf BEFORE the lock */ 2 33450 33452
141 2 croak("Data::DDSketch::Shared->add: value must be finite");
142 33450 dd_rwlock_wrlock(h);
143 33450 ▶ if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->add: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->add: sketch is frozen (read-only)"); } 1 33449 33450
144 33449 dd_insert_locked(h, value, 1);
145 33449 h->hdr->stat_ops++;
146 33449 RETVAL = (UV)h->hdr->total_count;
147 33449 dd_rwlock_wrunlock(h);
148 OUTPUT:
149 RETVAL
150  
151 UV
152 add_many(self, values)
153 SV *self
154 SV *values
155 PREINIT:
156 8 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 8 0 8
EXTRACT(self); 0 8 8
EXTRACT(self); 0 8 8
157 AV *av;
158 IV top;
159 8 ▶ UV added = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV added = 0; 8 0 8
160 CODE:
161 8 ▶ if (h->readonly) croak("Data::DDSketch::Shared->add_many: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::DDSketch::Shared->add_many: sketch is frozen (read-only)"); 2 6 8
162 6 ▶ SvGETMAGIC(values);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(values); 0 6 6
SvGETMAGIC(values); 0 0 0
163 6 ▶ if (!SvROK(values) || SvTYPE(SvRV(values)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(values) || SvTYPE(SvRV(values)) != SVt_PVAV) 5 1 6
if (!SvROK(values) || SvTYPE(SvRV(values)) != SVt_PVAV) 0 5 5
164 1 croak("Data::DDSketch::Shared->add_many: expected an array reference");
165 5 av = (AV *)SvRV(values);
166 5 sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
167 5 top = av_len(av); /* last index, -1 if empty */
168 {
169 5 ▶ STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i; 5 0 5
170 5 double *vs = NULL;
171 5 ▶ if (cnt) { /* resolve + validate all values BEFORE locking */
Branch: 1 branch
BranchTrueFalseTotal
if (cnt) { /* resolve + validate all values BEFORE locking */ 5 0 5
172 5 ▶ Newx(vs, cnt, double); SAVEFREEPV(vs); /* freed on return OR unwind */
Branch: 1 branch
BranchTrueFalseTotal
Newx(vs, cnt, double); SAVEFREEPV(vs); /* freed on return OR unwind */ 0 5 5
173 359 ▶ for (i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { 355 4 359
174 355 SV **el = av_fetch(av, (SSize_t)i, 0);
175 355 ▶ double v = (el && *el) ? SvNV(*el) : 0.0;
Branch: 2 branches
BranchTrueFalseTotal
double v = (el && *el) ? SvNV(*el) : 0.0; 355 0 355
double v = (el && *el) ? SvNV(*el) : 0.0; 355 0 355
176 355 ▶ if (!isfinite(v)) /* a croak here holds NO lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!isfinite(v)) /* a croak here holds NO lock */ 1 354 355
177 1 croak("Data::DDSketch::Shared->add_many: value at index %u is not finite", (unsigned)i);
178 354 vs[i] = v;
179 }
180 }
181 /* SvGETMAGIC(values), av_len (tied FETCHSIZE) and the element SvNV
182 * above all run Perl that can have destroyed self. This must sit
183 * OUTSIDE the `if (cnt)` block: an empty (or tied, size-0) array
184 * skips the loop but still reaches the lock below. */
185 4 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 4 4
REEXTRACT(self); 0 4 4
186 4 dd_rwlock_wrlock(h); /* locked region: NO croak-capable calls */
187 4 ▶ if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->add_many: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->add_many: sketch is frozen (read-only)"); } 1 3 4
188 353 ▶ for (i = 0; i < cnt; i++) { dd_insert_locked(h, vs[i], 1); added++; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { dd_insert_locked(h, vs[i], 1); added++; } 350 3 353
189 3 h->hdr->stat_ops++;
190 3 dd_rwlock_wrunlock(h);
191 }
192 3 ▶ RETVAL = added;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = added; 0 3 3
193 OUTPUT:
194 RETVAL
195  
196 SV *
197 quantile(self, q)
198 SV *self
199 double q
200 PREINIT:
201 40 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 40 0 40
EXTRACT(self); 0 40 40
EXTRACT(self); 0 40 40
202 double val;
203 int found;
204 uint64_t total;
205 CODE:
206 40 ▶ if (!(q >= 0.0 && q <= 1.0))
Branch: 2 branches
BranchTrueFalseTotal
if (!(q >= 0.0 && q <= 1.0)) 39 1 40
if (!(q >= 0.0 && q <= 1.0)) 1 38 39
207 2 croak("Data::DDSketch::Shared->quantile: q must be between 0 and 1");
208 38 ▶ if (h->readonly) { /* frozen: immutable stores, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable stores, no lock needed */ 4 34 38
209 4 total = h->hdr->total_count;
210 4 ▶ if (total == 0) { found = 0; val = 0.0; }
Branch: 1 branch
BranchTrueFalseTotal
if (total == 0) { found = 0; val = 0.0; } 0 4 4
211 4 else val = dd_value_at_rank(h, q * (double)(total - 1), &found);
212 } else {
213 34 dd_rwlock_rdlock(h);
214 34 total = h->hdr->total_count;
215 34 ▶ if (total == 0) { found = 0; val = 0.0; }
Branch: 1 branch
BranchTrueFalseTotal
if (total == 0) { found = 0; val = 0.0; } 3 31 34
216 31 else val = dd_value_at_rank(h, q * (double)(total - 1), &found);
217 34 dd_rwlock_rdunlock(h);
218 }
219 38 ▶ RETVAL = found ? newSVnv(val) : &PL_sv_undef; /* undef on an empty sketch */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = found ? newSVnv(val) : &PL_sv_undef; /* undef on an empty sketch */ 35 3 38
220 OUTPUT:
221 RETVAL
222  
223 SV *
224 min(self)
225 SV *self
226 PREINIT:
227 11 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 11 0 11
EXTRACT(self); 0 11 11
EXTRACT(self); 0 11 11
228 double v;
229 uint64_t total;
230 CODE:
231 11 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 1 10 11
232 1 total = h->hdr->total_count;
233 1 v = h->hdr->min_value;
234 } else {
235 10 dd_rwlock_rdlock(h);
236 10 total = h->hdr->total_count;
237 10 v = h->hdr->min_value;
238 10 dd_rwlock_rdunlock(h);
239 }
240 11 ▶ RETVAL = total ? newSVnv(v) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = total ? newSVnv(v) : &PL_sv_undef; 8 3 11
241 OUTPUT:
242 RETVAL
243  
244 SV *
245 max(self)
246 SV *self
247 PREINIT:
248 10 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 10 0 10
EXTRACT(self); 0 10 10
EXTRACT(self); 0 10 10
249 double v;
250 uint64_t total;
251 CODE:
252 10 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 1 9 10
253 1 total = h->hdr->total_count;
254 1 v = h->hdr->max_value;
255 } else {
256 9 dd_rwlock_rdlock(h);
257 9 total = h->hdr->total_count;
258 9 v = h->hdr->max_value;
259 9 dd_rwlock_rdunlock(h);
260 }
261 10 ▶ RETVAL = total ? newSVnv(v) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = total ? newSVnv(v) : &PL_sv_undef; 8 2 10
262 OUTPUT:
263 RETVAL
264  
265 SV *
266 mean(self)
267 SV *self
268 PREINIT:
269 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
270 double sum;
271 uint64_t total;
272 CODE:
273 4 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 1 3 4
274 1 total = h->hdr->total_count;
275 1 sum = h->hdr->sum;
276 } else {
277 3 dd_rwlock_rdlock(h);
278 3 total = h->hdr->total_count;
279 3 sum = h->hdr->sum;
280 3 dd_rwlock_rdunlock(h);
281 }
282 4 ▶ RETVAL = total ? newSVnv(sum / (double)total) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = total ? newSVnv(sum / (double)total) : &PL_sv_undef; 3 1 4
283 OUTPUT:
284 RETVAL
285  
286 NV
287 sum(self)
288 SV *self
289 PREINIT:
290 8 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 8 0 8
EXTRACT(self); 0 8 8
EXTRACT(self); 0 8 8
291 double v;
292 CODE:
293 8 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 2 6 8
294 2 v = h->hdr->sum;
295 } else {
296 6 dd_rwlock_rdlock(h);
297 6 v = h->hdr->sum;
298 6 dd_rwlock_rdunlock(h);
299 }
300 8 ▶ RETVAL = v;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v; 0 8 8
301 OUTPUT:
302 RETVAL
303  
304 UV
305 count(self)
306 SV *self
307 PREINIT:
308 22 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 22 0 22
EXTRACT(self); 0 22 22
EXTRACT(self); 1 21 22
309 UV n;
310 CODE:
311 21 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 4 17 21
312 4 n = (UV)h->hdr->total_count;
313 } else {
314 17 dd_rwlock_rdlock(h);
315 17 n = (UV)h->hdr->total_count;
316 17 dd_rwlock_rdunlock(h);
317 }
318 21 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 0 21 21
319 OUTPUT:
320 RETVAL
321  
322 UV
323 zero_count(self)
324 SV *self
325 PREINIT:
326 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
327 UV n;
328 CODE:
329 5 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 1 4 5
330 1 n = (UV)h->hdr->zero_count;
331 } else {
332 4 dd_rwlock_rdlock(h);
333 4 n = (UV)h->hdr->zero_count;
334 4 dd_rwlock_rdunlock(h);
335 }
336 5 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 0 5 5
337 OUTPUT:
338 RETVAL
339  
340 void
341 merge(self, other)
342 SV *self
343 SV *other
344 PREINIT:
345 6 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 6 0 6
EXTRACT(self); 0 6 6
EXTRACT(self); 0 6 6
346 CODE:
347 6 ▶ if (h->readonly) croak("Data::DDSketch::Shared->merge: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::DDSketch::Shared->merge: sketch is frozen (read-only)"); 1 5 6
348 5 ▶ if (!sv_isobject(other) || !sv_derived_from(other, "Data::DDSketch::Shared"))
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(other) || !sv_derived_from(other, "Data::DDSketch::Shared")) 5 0 5
if (!sv_isobject(other) || !sv_derived_from(other, "Data::DDSketch::Shared")) 0 5 5
349 0 croak("Data::DDSketch::Shared->merge: expected a Data::DDSketch::Shared object");
350 5 DdHandle *o = INT2PTR(DdHandle*, SvIV(SvRV(other)));
351 5 ▶ if (!o) croak("Attempted to use a destroyed Data::DDSketch::Shared object");
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Attempted to use a destroyed Data::DDSketch::Shared object"); 0 5 5
352 /* sv_isobject/sv_derived_from above begin with SvGETMAGIC(other), so a tied
353 * `other` can have run Perl that destroyed self before h is used below.
354 * `o` was read after that magic and needs no re-read. */
355 5 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 5 5
REEXTRACT(self); 0 5 5
356  
357 /* geometry is immutable after creation -- compare the attach-time-cached
358 * copies (the same values the merge math uses), not the peer-writable header,
359 * and croak BEFORE allocating so a mismatch holds no lock and leaks no buffer. */
360 5 ▶ if (o->num_buckets != h->num_buckets || o->gamma != h->gamma)
Branch: 2 branches
BranchTrueFalseTotal
if (o->num_buckets != h->num_buckets || o->gamma != h->gamma) 4 1 5
if (o->num_buckets != h->num_buckets || o->gamma != h->gamma) 1 3 4
361 2 croak("Data::DDSketch::Shared->merge: geometry mismatch (num_buckets/alpha differ)");
362  
363 /* Snapshot the other's stores + scalars under its read lock into temp
364 * buffers, then release before taking self's write lock (deadlock-free,
365 * two processes can merge each other). */
366 3 uint64_t nb = o->num_buckets;
367 3 uint64_t o_nmax = dd_store_max(o, o->neg_off); /* Layer B: never read past o's mapping */
368 3 uint64_t o_pmax = dd_store_max(o, o->pos_off);
369 3 ▶ if (nb > o_nmax) nb = o_nmax;
Branch: 1 branch
BranchTrueFalseTotal
if (nb > o_nmax) nb = o_nmax; 0 3 3
370 3 ▶ if (nb > o_pmax) nb = o_pmax;
Branch: 1 branch
BranchTrueFalseTotal
if (nb > o_pmax) nb = o_pmax; 0 3 3
371 uint64_t *tneg, *tpos, o_total, o_zero;
372 double o_sum, o_min, o_max;
373 3 ▶ Newx(tneg, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tneg);
Branch: 3 branches
BranchTrueFalseTotal
Newx(tneg, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tneg); 3 0 3
Newx(tneg, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tneg); 0 3 3
Newx(tneg, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tneg); 3 0 3
374 3 ▶ Newx(tpos, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tpos);
Branch: 3 branches
BranchTrueFalseTotal
Newx(tpos, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tpos); 3 0 3
Newx(tpos, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tpos); 0 3 3
Newx(tpos, (size_t)(nb ? nb : 1), uint64_t); SAVEFREEPV(tpos); 3 0 3
375 3 ▶ if (o->readonly) { /* frozen other: immutable stores, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (o->readonly) { /* frozen other: immutable stores, no lock */ 1 2 3
376 1 memcpy(tneg, dd_neg(o), (size_t)nb * sizeof(uint64_t));
377 1 memcpy(tpos, dd_pos(o), (size_t)nb * sizeof(uint64_t));
378 1 o_total = o->hdr->total_count; o_zero = o->hdr->zero_count;
379 1 o_sum = o->hdr->sum; o_min = o->hdr->min_value; o_max = o->hdr->max_value;
380 } else {
381 2 dd_rwlock_rdlock(o);
382 2 memcpy(tneg, dd_neg(o), (size_t)nb * sizeof(uint64_t));
383 2 memcpy(tpos, dd_pos(o), (size_t)nb * sizeof(uint64_t));
384 2 o_total = o->hdr->total_count; o_zero = o->hdr->zero_count;
385 2 o_sum = o->hdr->sum; o_min = o->hdr->min_value; o_max = o->hdr->max_value;
386 2 dd_rwlock_rdunlock(o);
387 }
388  
389 3 dd_rwlock_wrlock(h);
390 3 ▶ if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->merge: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->merge: sketch is frozen (read-only)"); } 1 2 3
391 2 dd_merge_locked(h, tneg, tpos, nb, o_total, o_zero, o_sum, o_min, o_max);
392 2 h->hdr->stat_ops++;
393 2 dd_rwlock_wrunlock(h);
394  
395 void
396 clear(self)
397 SV *self
398 PREINIT:
399 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
400 CODE:
401 4 ▶ if (h->readonly) croak("Data::DDSketch::Shared->clear: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::DDSketch::Shared->clear: sketch is frozen (read-only)"); 2 2 4
402 2 dd_rwlock_wrlock(h);
403 2 ▶ if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->clear: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { dd_rwlock_wrunlock(h); croak("Data::DDSketch::Shared->clear: sketch is frozen (read-only)"); } 1 1 2
404 1 dd_clear_locked(h);
405 1 h->hdr->stat_ops++;
406 1 dd_rwlock_wrunlock(h);
407  
408 void
409 freeze(self)
410 SV *self
411 PREINIT:
412 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
413 CODE:
414 4 ▶ if (h->readonly) croak("Data::DDSketch::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::DDSketch::Shared->freeze: cannot freeze a read-only handle"); 2 2 4
415 2 ▶ if (dd_freeze(h) != 0) croak("Data::DDSketch::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (dd_freeze(h) != 0) croak("Data::DDSketch::Shared->freeze: msync: %s", strerror(errno)); 0 2 2
416 2 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
417  
418 UV
419 frozen(self)
420 SV *self
421 PREINIT:
422 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
423 CODE:
424 3 ▶ RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE) ? 1 : 0; 0 3 3
425 OUTPUT:
426 RETVAL
427  
428 UV
429 readonly(self)
430 SV *self
431 PREINIT:
432 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
433 CODE:
434 3 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 3 3
435 OUTPUT:
436 RETVAL
437  
438 NV
439 alpha(self)
440 SV *self
441 PREINIT:
442 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
443 CODE:
444 3 ▶ RETVAL = h->hdr->alpha;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->hdr->alpha; 0 3 3
445 OUTPUT:
446 RETVAL
447  
448 NV
449 gamma(self)
450 SV *self
451 PREINIT:
452 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
453 CODE:
454 2 ▶ RETVAL = h->gamma; /* cached, not the peer-writable header */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->gamma; /* cached, not the peer-writable header */ 0 2 2
455 OUTPUT:
456 RETVAL
457  
458 UV
459 num_buckets(self)
460 SV *self
461 PREINIT:
462 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
463 CODE:
464 4 ▶ RETVAL = (UV)h->num_buckets; /* cached */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->num_buckets; /* cached */ 0 4 4
465 OUTPUT:
466 RETVAL
467  
468 SV *
469 stats(self)
470 SV *self
471 PREINIT:
472 3983 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3983 0 3983
EXTRACT(self); 0 3983 3983
EXTRACT(self); 0 3983 3983
473 CODE:
474 {
475 uint64_t total, zero, ops;
476 uint32_t nb;
477 uint8_t sealed;
478 double alpha, gamma, sum, mn, mx;
479 3983 ▶ if (!h->readonly) dd_rwlock_rdlock(h); /* frozen: immutable, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) dd_rwlock_rdlock(h); /* frozen: immutable, no lock */ 3982 1 3983
480 3983 total = h->hdr->total_count;
481 3983 zero = h->hdr->zero_count;
482 3983 ops = h->hdr->stat_ops;
483 3983 nb = h->num_buckets; /* cached geometry */
484 3983 alpha = h->hdr->alpha; /* reported-only (no math/bounds depend on it) */
485 3983 gamma = h->gamma; /* cached */
486 3983 sum = h->hdr->sum;
487 3983 mn = h->hdr->min_value;
488 3983 mx = h->hdr->max_value;
489 3983 sealed = __atomic_load_n(&h->hdr->sealed, __ATOMIC_ACQUIRE);
490 3983 ▶ if (!h->readonly) dd_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) dd_rwlock_rdunlock(h); 3982 1 3983
491  
492 3983 HV *hv = newHV();
493 3983 hv_stores(hv, "alpha", newSVnv(alpha));
494 3983 hv_stores(hv, "gamma", newSVnv(gamma));
495 3983 hv_stores(hv, "num_buckets", newSVuv((UV)nb));
496 3983 hv_stores(hv, "count", newSVuv((UV)total));
497 3983 hv_stores(hv, "zero_count", newSVuv((UV)zero));
498 3983 hv_stores(hv, "sum", newSVnv(sum));
499 3983 ▶ hv_stores(hv, "min", total ? newSVnv(mn) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(hv, "min", total ? newSVnv(mn) : newSV(0)); 3977 6 3983
500 3983 ▶ hv_stores(hv, "max", total ? newSVnv(mx) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(hv, "max", total ? newSVnv(mx) : newSV(0)); 3977 6 3983
501 3983 ▶ hv_stores(hv, "mean", total ? newSVnv(sum / (double)total) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(hv, "mean", total ? newSVnv(sum / (double)total) : newSV(0)); 3977 6 3983
502 3983 hv_stores(hv, "ops", newSVuv((UV)ops));
503 3983 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
504 3983 hv_stores(hv, "frozen", newSVuv(sealed ? 1 : 0));
505 3983 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
506 3983 RETVAL = newRV_noinc((SV *)hv);
507 }
508 OUTPUT:
509 RETVAL
510  
511 SV *
512 path(self)
513 SV *self
514 PREINIT:
515 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
516 CODE:
517 2 ▶ RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; 2 0 2
518 OUTPUT:
519 RETVAL
520  
521 int
522 memfd(self)
523 SV *self
524 PREINIT:
525 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
526 CODE:
527 2 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 2 2
528 OUTPUT:
529 RETVAL
530  
531 void
532 sync(self)
533 SV *self
534 PREINIT:
535 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
536 CODE:
537 3 ▶ if (!h->readonly && dd_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && dd_msync(h) != 0) croak("sync: %s", strerror(errno)); 2 1 3
if (!h->readonly && dd_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
538  
539 void
540 unlink(self, ...)
541 SV *self
542 CODE:
543 7 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::DDSketch::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::DDSketch::Shared")) { 3 2 5
if (sv_isobject(self) && sv_derived_from(self, "Data::DDSketch::Shared")) { 3 0 3
544 3 DdHandle *h = INT2PTR(DdHandle*, SvIV(SvRV(self)));
545 3 ▶ if (h && h->path && unlink(h->path) != 0 && errno != ENOENT)
Branch: 4 branches
BranchTrueFalseTotal
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 3 0 3
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 3 0 3
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 2 1 3
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 1 1 2
546 1 croak("Data::DDSketch::Shared->unlink(%s): %s", h->path, strerror(errno));
547 2 ▶ } else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 2 0 2
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 2 2
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 0 0
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 2 0 2
548 {
549 2 const char *up = SvPV_nolen(ST(1));
550 2 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 1 1 2
if (unlink(up) != 0 && errno != ENOENT) 0 1 1
551 0 croak("Data::DDSketch::Shared->unlink(%s): %s", up, strerror(errno));
552 }
553 }
« ddsketch.h ↑ Summary