Shared.xs

filter: Statementstmt 98.2% 165 / 168 Branchbran 60.7% 175 / 288 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.5% 340 / 456 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« cms.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 "cms.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::CountMinSketch::Shared")) \
10 croak("Expected a Data::CountMinSketch::Shared object"); \
11 CmsHandle *h = INT2PTR(CmsHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::CountMinSketch::Shared object"); \
13 CmsHandle *h0 = h; PERL_UNUSED_VAR(h0); \
14 sv_2mortal(SvREFCNT_inc(SvRV(sv)))
15  
16 /* Re-read the handle after a call that can run Perl code (tied/overloaded
17 * argument magic, tied-array fetches). That code may call $obj->DESTROY
18 * explicitly, which frees the handle and zeroes the IV; EXTRACT's mortal pins
19 * the referent only against refcount-driven destruction, not an explicit
20 * DESTROY, so the local `h` would dangle. Used only where magic can actually
21 * intervene between EXTRACT and the first use of h -- a load and a branch,
22 * immediately before a lock that costs orders of magnitude more. */
23 #define REEXTRACT(sv) \
24 if (!SvROK(sv)) \
25 croak("Data::CountMinSketch::Shared object was replaced during the call"); \
26 h = INT2PTR(CmsHandle*, SvIV(SvRV(sv))); \
27 if (h != h0) croak("Data::CountMinSketch::Shared object replaced or destroyed during the call")
28  
29 #define MAKE_OBJ(class, handle) \
30 SV *obj = newSViv(PTR2IV(handle)); \
31 SV *ref = newRV_noinc(obj); \
32 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
33 RETVAL = ref
34  
35 /* Re-read the class-name PV immediately before blessing the new object.
36 * `class` is captured by the typemap in the INPUT section; a tied/overloaded
37 * later constructor argument can run get-magic that reallocs or frees ST(0)'s
38 * PV, dangling that pointer before it is used to bless. Same fix as
39 * Data::CuckooFilter::Shared already carries. */
40 #define REREAD_CLASS() \
41 class = SvPV_nolen(ST(0))
42  
43 MODULE = Data::CountMinSketch::Shared PACKAGE = Data::CountMinSketch::Shared
44  
45 PROTOTYPES: DISABLE
46  
47 SV *
48 new(class, path = &PL_sv_undef, epsilon = 0.001, delta = 0.001, ...)
49 const char *class
50 SV *path
51 double epsilon
52 double delta
53 PREINIT:
54 char errbuf[CMS_ERR_BUFLEN];
55 CODE:
56 /* Resolve the optional mode arg FIRST: its SvGETMAGIC can realloc/free the
57 * path PV, so capture the path pointer LAST, right before cms_create(). */
58 /* Optional 4th arg: file mode for a newly-created backing file (default 0600,
59 * owner-only). Pass a wider mode (e.g. 0660) to opt in to cross-user sharing. */
60 48 ▶ 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 48 48
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
61 67 ▶ 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 48 48
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 29 48
62 48 CmsHandle *h = cms_create(p, epsilon, delta, mode, errbuf); /* validates epsilon/delta into errbuf */
63 48 ▶ if (!h) croak("Data::CountMinSketch::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CountMinSketch::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 11 37 48
if (!h) croak("Data::CountMinSketch::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 11 0 11
64 37 REREAD_CLASS();
65 37 MAKE_OBJ(class, h);
66 OUTPUT:
67 RETVAL
68  
69 SV *
70 new_memfd(class, name = &PL_sv_undef, epsilon = 0.001, delta = 0.001)
71 const char *class
72 SV *name
73 double epsilon
74 double delta
75 PREINIT:
76 char errbuf[CMS_ERR_BUFLEN];
77 CODE:
78 7 ▶ 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 4 4
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 */ 3 1 4
79 4 CmsHandle *h = cms_create_memfd(nm, epsilon, delta, errbuf); /* validates epsilon/delta into errbuf */
80 4 ▶ if (!h) croak("Data::CountMinSketch::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CountMinSketch::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 2 2 4
if (!h) croak("Data::CountMinSketch::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 2 0 2
81 2 REREAD_CLASS();
82 2 MAKE_OBJ(class, h);
83 OUTPUT:
84 RETVAL
85  
86 SV *
87 new_from_fd(class, fd)
88 const char *class
89 int fd
90 PREINIT:
91 char errbuf[CMS_ERR_BUFLEN];
92 CODE:
93 2 CmsHandle *h = cms_open_fd(fd, errbuf);
94 2 ▶ if (!h) croak("Data::CountMinSketch::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::CountMinSketch::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 1 2
if (!h) croak("Data::CountMinSketch::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 1 0 1
95 1 REREAD_CLASS();
96 1 MAKE_OBJ(class, h);
97 OUTPUT:
98 RETVAL
99  
100 SV *
101 new_readonly(class, path)
102 const char *class
103 SV *path
104 PREINIT:
105 char errbuf[CMS_ERR_BUFLEN];
106 CODE:
107 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
108 ever. A sealed file is immutable and no read path writes the mapping,
109 so queries take no reader-slot / rwlock traffic and any number of
110 processes can share one PROT_READ mapping (same architecture; the
111 magic rejects a wrong-endian file). */
112 15 ▶ 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 8 8
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; 7 1 8
113 8 ▶ if (!p) croak("Data::CountMinSketch::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::CountMinSketch::Shared->new_readonly: path is required"); 1 7 8
114 7 CmsHandle *h = cms_open_readonly(p, errbuf);
115 7 ▶ if (!h) croak("Data::CountMinSketch::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::CountMinSketch::Shared->new_readonly: %s", errbuf); 2 5 7
116 5 REREAD_CLASS();
117 5 MAKE_OBJ(class, h);
118 OUTPUT:
119 RETVAL
120  
121 void
122 DESTROY(self)
123 SV *self
124 CODE:
125 47 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::CountMinSketch::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::CountMinSketch::Shared")) { 47 0 47
if (sv_isobject(self) && sv_derived_from(self, "Data::CountMinSketch::Shared")) { 47 0 47
126 47 CmsHandle *h = INT2PTR(CmsHandle*, SvIV(SvRV(self)));
127 47 ▶ if (h) { sv_setiv(SvRV(self), 0); cms_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); cms_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 45 2 47
128 }
129  
130 UV
131 add(self, item, n = 1)
132 SV *self
133 SV *item
134 UV n
135 PREINIT:
136 6948 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 6948 0 6948
EXTRACT(self); 0 6948 6948
EXTRACT(self); 0 6948 6948
137 STRLEN len;
138 const char *s;
139 UV total;
140 CODE:
141 6948 ▶ if (h->readonly) croak("Data::CountMinSketch::Shared->add: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountMinSketch::Shared->add: sketch is frozen (read-only)"); 2 6946 6948
142 6945 s = SvPVbyte(item, len); /* may croak (wide char) -- BEFORE the lock */
143 6945 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 6945 6945
REEXTRACT(self); 0 6945 6945
144 6945 cms_rwlock_wrlock(h);
145 6945 ▶ if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->add: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->add: sketch is frozen (read-only)"); } 1 6944 6945
146 6944 cms_add_locked(h, s, len, (uint64_t)n);
147 6944 total = (UV)h->hdr->total;
148 6944 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
149 6944 cms_rwlock_wrunlock(h);
150 6944 ▶ RETVAL = total;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = total; 6915 29 6944
151 OUTPUT:
152 RETVAL
153  
154 UV
155 add_many(self, items)
156 SV *self
157 SV *items
158 PREINIT:
159 11 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 11 0 11
EXTRACT(self); 0 11 11
EXTRACT(self); 0 11 11
160 AV *av;
161 IV top;
162 CODE:
163 11 ▶ if (h->readonly) croak("Data::CountMinSketch::Shared->add_many: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountMinSketch::Shared->add_many: sketch is frozen (read-only)"); 2 9 11
164 9 ▶ SvGETMAGIC(items);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(items); 0 9 9
SvGETMAGIC(items); 0 0 0
165 9 ▶ if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 8 1 9
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 1 7 8
166 2 croak("Data::CountMinSketch::Shared->add_many: expected an array reference");
167 7 av = (AV *)SvRV(items);
168 7 sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
169 7 top = av_len(av); /* last index, -1 if empty */
170 {
171 7 ▶ STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i; 6 1 7
172 7 const char **ps = NULL; STRLEN *ls = NULL;
173 7 ▶ if (cnt) { /* resolve all bytes BEFORE locking */
Branch: 1 branch
BranchTrueFalseTotal
if (cnt) { /* resolve all bytes BEFORE locking */ 6 1 7
174 6 ▶ Newx(ps, cnt, const char *); SAVEFREEPV(ps); /* freed on return OR unwind */
Branch: 1 branch
BranchTrueFalseTotal
Newx(ps, cnt, const char *); SAVEFREEPV(ps); /* freed on return OR unwind */ 0 6 6
175 6 ▶ Newx(ls, cnt, STRLEN); SAVEFREEPV(ls);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ls, cnt, STRLEN); SAVEFREEPV(ls); 0 6 6
176 2511 ▶ for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV cleans up */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { /* a croak here holds NO lock; SAVEFREEPV cleans up */ 2506 5 2511
177 2506 SV **el = av_fetch(av, (SSize_t)i, 0);
178 2506 ▶ if (el && *el) {
Branch: 2 branches
BranchTrueFalseTotal
if (el && *el) { 2506 0 2506
if (el && *el) { 2506 0 2506
179 STRLEN len;
180 2506 const char *src = SvPVbyte(*el, len); /* may run overload/tie/get-magic = arbitrary Perl */
181 /* Copy bytes into a private mortal SV NOW: a LATER element SvPVbyte can
182 * grow/free THIS element PV, dangling src before the locked loop uses it. */
183 2505 SV *copy = sv_2mortal(newSVpvn(src, len));
184 2505 ps[i] = SvPVX_const(copy);
185 2505 ls[i] = len;
186 0 } else { ps[i] = ""; ls[i] = 0; }
187 }
188 }
189 6 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 6 6
REEXTRACT(self); 0 6 6
190 6 cms_rwlock_wrlock(h); /* locked region: NO croak-capable calls */
191 6 ▶ if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->add_many: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->add_many: sketch is frozen (read-only)"); } 1 5 6
192 2508 ▶ for (i = 0; i < cnt; i++) cms_add_locked(h, ps[i], ls[i], 1);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) cms_add_locked(h, ps[i], ls[i], 1); 2503 5 2508
193 5 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED); /* a call always counts, even an empty batch */
194 5 cms_rwlock_wrunlock(h);
195 5 ▶ RETVAL = (UV)cnt; /* every element is added (CMS add never fails) */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)cnt; /* every element is added (CMS add never fails) */ 0 5 5
196 }
197 OUTPUT:
198 RETVAL
199  
200 UV
201 estimate(self, item)
202 SV *self
203 SV *item
204 PREINIT:
205 9438 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9438 0 9438
EXTRACT(self); 0 9438 9438
EXTRACT(self); 1 9437 9438
206 STRLEN len;
207 const char *s;
208 UV est;
209 CODE:
210 9437 s = SvPVbyte(item, len); /* may croak (wide char) -- BEFORE the lock */
211 9437 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 9437 9437
REEXTRACT(self); 0 9437 9437
212 9437 ▶ if (h->readonly) { /* frozen: immutable cells, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable cells, no lock needed */ 12 9425 9437
213 12 est = (UV)cms_estimate_locked(h, s, len);
214 } else {
215 9425 cms_rwlock_rdlock(h);
216 9425 est = (UV)cms_estimate_locked(h, s, len);
217 9425 cms_rwlock_rdunlock(h);
218 }
219 9437 ▶ RETVAL = est;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = est; 9401 36 9437
220 OUTPUT:
221 RETVAL
222  
223 void
224 merge(self, other)
225 SV *self
226 SV *other
227 PREINIT:
228 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
229 CODE:
230 7 ▶ if (h->readonly) croak("Data::CountMinSketch::Shared->merge: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountMinSketch::Shared->merge: sketch is frozen (read-only)"); 1 6 7
231 6 ▶ if (!sv_isobject(other) || !sv_derived_from(other, "Data::CountMinSketch::Shared"))
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(other) || !sv_derived_from(other, "Data::CountMinSketch::Shared")) 6 0 6
if (!sv_isobject(other) || !sv_derived_from(other, "Data::CountMinSketch::Shared")) 0 6 6
232 0 croak("Data::CountMinSketch::Shared->merge: expected a Data::CountMinSketch::Shared object");
233 6 CmsHandle *o = INT2PTR(CmsHandle*, SvIV(SvRV(other)));
234 6 ▶ if (!o) croak("Attempted to use a destroyed Data::CountMinSketch::Shared object");
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Attempted to use a destroyed Data::CountMinSketch::Shared object"); 0 6 6
235 /* sv_isobject/sv_derived_from above begin with SvGETMAGIC(other), so a
236 * tied `other` can have destroyed self before h is used below. `o` was
237 * read after that magic and needs no re-read. */
238 6 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 6 6
REEXTRACT(self); 0 6 6
239  
240 /* w and d are immutable after creation -- compare lock-free, croak BEFORE
241 * allocating, so a mismatch holds no lock and leaks no buffer. */
242 6 uint64_t ow = o->hdr->w;
243 6 uint32_t od = o->hdr->d;
244 6 ▶ if (ow != h->hdr->w || od != h->hdr->d)
Branch: 2 branches
BranchTrueFalseTotal
if (ow != h->hdr->w || od != h->hdr->d) 5 1 6
if (ow != h->hdr->w || od != h->hdr->d) 0 5 5
245 1 croak("Data::CountMinSketch::Shared->merge: geometry mismatch (w=%llu/d=%u vs w=%llu/d=%u)",
246 (unsigned long long)h->hdr->w, h->hdr->d,
247 (unsigned long long)ow, od);
248  
249 /* Snapshot the other's cells (+ its total) under its read lock into a temp
250 * buffer, then release before taking self's write lock. Copying to a temp
251 * avoids holding two locks at once (deadlock-free regardless of acquisition
252 * order between two processes merging each other). */
253 5 uint64_t cells = (uint64_t)od * ow;
254 /* od/ow/counters_off are read from the peer's shared segment and can be
255 * poisoned by a local writer after we validated its header; bound the
256 * copy/merge length to what BOTH mappings actually back (process-local
257 * mmap_size, trusted) so a poisoned geometry can neither over-read `other`
258 * nor over-write self. Snapshot each counters_off once and use it for both
259 * the pointer and the bound. Equal to od*ow for valid data. */
260 5 uint64_t ooff = o->hdr->counters_off;
261 5 uint64_t hoff = h->hdr->counters_off;
262 5 ▶ uint64_t avail_o = (ooff <= o->mmap_size) ? (o->mmap_size - ooff) / sizeof(uint64_t) : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t avail_o = (ooff <= o->mmap_size) ? (o->mmap_size - ooff) / sizeof(uint64_t) : 0; 5 0 5
263 5 ▶ uint64_t avail_h = (hoff <= h->mmap_size) ? (h->mmap_size - hoff) / sizeof(uint64_t) : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint64_t avail_h = (hoff <= h->mmap_size) ? (h->mmap_size - hoff) / sizeof(uint64_t) : 0; 5 0 5
264 5 ▶ if (cells > avail_o) cells = avail_o;
Branch: 1 branch
BranchTrueFalseTotal
if (cells > avail_o) cells = avail_o; 0 5 5
265 5 ▶ if (cells > avail_h) cells = avail_h;
Branch: 1 branch
BranchTrueFalseTotal
if (cells > avail_h) cells = avail_h; 0 5 5
266 uint64_t other_total;
267 uint64_t *tmp;
268 5 ▶ Newx(tmp, (size_t)cells, uint64_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(tmp, (size_t)cells, uint64_t); 0 5 5
269 5 SAVEFREEPV(tmp); /* freed on normal return OR croak unwind */
270 5 ▶ if (o->readonly) { /* frozen other: immutable cells, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (o->readonly) { /* frozen other: immutable cells, no lock */ 2 3 5
271 2 memcpy(tmp, (char *)o->base + ooff, (size_t)cells * sizeof(uint64_t));
272 2 other_total = o->hdr->total;
273 } else {
274 3 cms_rwlock_rdlock(o);
275 3 memcpy(tmp, (char *)o->base + ooff, (size_t)cells * sizeof(uint64_t));
276 3 other_total = o->hdr->total;
277 3 cms_rwlock_rdunlock(o);
278 }
279  
280 5 cms_rwlock_wrlock(h);
281 5 ▶ if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->merge: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->merge: sketch is frozen (read-only)"); } 1 4 5
282 4 cms_merge_counters((uint64_t *)((char *)h->base + hoff), tmp, cells);
283 4 ▶ if (h->hdr->total > UINT64_MAX - other_total) h->hdr->total = UINT64_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->total > UINT64_MAX - other_total) h->hdr->total = UINT64_MAX; 0 4 4
284 4 else h->hdr->total += other_total;
285 4 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
286 4 cms_rwlock_wrunlock(h);
287  
288 void
289 clear(self)
290 SV *self
291 PREINIT:
292 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
293 CODE:
294 4 ▶ if (h->readonly) croak("Data::CountMinSketch::Shared->clear: sketch is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountMinSketch::Shared->clear: sketch is frozen (read-only)"); 2 2 4
295 2 cms_rwlock_wrlock(h);
296 2 ▶ if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->clear: sketch is frozen (read-only)"); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { cms_rwlock_wrunlock(h); croak("Data::CountMinSketch::Shared->clear: sketch is frozen (read-only)"); } 1 1 2
297 1 cms_clear_locked(h);
298 1 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
299 1 cms_rwlock_wrunlock(h);
300  
301 void
302 freeze(self)
303 SV *self
304 PREINIT:
305 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
306 CODE:
307 4 ▶ if (h->readonly) croak("Data::CountMinSketch::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::CountMinSketch::Shared->freeze: cannot freeze a read-only handle"); 2 2 4
308 2 ▶ if (cms_freeze(h) != 0) croak("Data::CountMinSketch::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (cms_freeze(h) != 0) croak("Data::CountMinSketch::Shared->freeze: msync: %s", strerror(errno)); 0 2 2
309 2 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
310  
311 UV
312 frozen(self)
313 SV *self
314 PREINIT:
315 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
316 CODE:
317 4 ▶ RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_RELAXED) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = __atomic_load_n(&h->hdr->sealed, __ATOMIC_RELAXED) ? 1 : 0; 0 4 4
318 OUTPUT:
319 RETVAL
320  
321 UV
322 readonly(self)
323 SV *self
324 PREINIT:
325 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
326 CODE:
327 4 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 4 4
328 OUTPUT:
329 RETVAL
330  
331 UV
332 total(self)
333 SV *self
334 PREINIT:
335 20 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 20 0 20
EXTRACT(self); 0 20 20
EXTRACT(self); 0 20 20
336 UV t;
337 CODE:
338 20 ▶ if (h->readonly) { /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { /* frozen: immutable, no lock needed */ 3 17 20
339 3 t = (UV)h->hdr->total;
340 } else {
341 17 cms_rwlock_rdlock(h);
342 17 t = (UV)h->hdr->total;
343 17 cms_rwlock_rdunlock(h);
344 }
345 20 ▶ RETVAL = t;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = t; 0 20 20
346 OUTPUT:
347 RETVAL
348  
349 UV
350 width(self)
351 SV *self
352 PREINIT:
353 9 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9 0 9
EXTRACT(self); 0 9 9
EXTRACT(self); 0 9 9
354 CODE:
355 9 ▶ RETVAL = (UV)h->hdr->w;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->w; 0 9 9
356 OUTPUT:
357 RETVAL
358  
359 UV
360 depth(self)
361 SV *self
362 PREINIT:
363 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
364 CODE:
365 7 ▶ RETVAL = (UV)h->hdr->d;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->d; 0 7 7
366 OUTPUT:
367 RETVAL
368  
369 UV
370 cells(self)
371 SV *self
372 PREINIT:
373 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
374 CODE:
375 2 ▶ RETVAL = (UV)((uint64_t)h->hdr->d * h->hdr->w);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)((uint64_t)h->hdr->d * h->hdr->w); 0 2 2
376 OUTPUT:
377 RETVAL
378  
379 SV *
380 stats(self)
381 SV *self
382 PREINIT:
383 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
384 CODE:
385 {
386 uint64_t w, total, ops, cells;
387 uint32_t d;
388 /* Snapshot under the lock; do all (croak-capable) Perl allocation after
389 releasing it -- so an OOM in newHV/newSVuv can never strand the lock. */
390 4 ▶ if (!h->readonly) cms_rwlock_rdlock(h); /* frozen: immutable, no lock */
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) cms_rwlock_rdlock(h); /* frozen: immutable, no lock */ 3 1 4
391 4 w = h->hdr->w;
392 4 d = h->hdr->d;
393 4 total = h->hdr->total;
394 4 ops = h->hdr->stat_ops;
395 4 ▶ if (!h->readonly) cms_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (!h->readonly) cms_rwlock_rdunlock(h); 3 1 4
396 4 cells = (uint64_t)d * w;
397  
398 4 HV *hv = newHV();
399 4 hv_stores(hv, "width", newSVuv((UV)w));
400 4 hv_stores(hv, "depth", newSVuv((UV)d));
401 4 hv_stores(hv, "total", newSVuv((UV)total));
402 4 hv_stores(hv, "cells", newSVuv((UV)cells));
403 4 hv_stores(hv, "epsilon", newSVnv(M_E / (double)w)); /* nominal error factor */
404 /* Nominal failure parameter: rows repeat every w; double-hashed rows
405 * are correlated, so this is not a strict probability guarantee. */
406 4 ▶ hv_stores(hv, "delta", newSVnv(exp(-(double)((uint64_t)d < w ? (uint64_t)d : w))));
Branch: 1 branch
BranchTrueFalseTotal
hv_stores(hv, "delta", newSVnv(exp(-(double)((uint64_t)d < w ? (uint64_t)d : w)))); 4 0 4
407 4 hv_stores(hv, "ops", newSVuv((UV)ops));
408 4 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
409 4 hv_stores(hv, "frozen", newSVuv(__atomic_load_n(&h->hdr->sealed, __ATOMIC_RELAXED) ? 1 : 0));
410 4 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
411 4 RETVAL = newRV_noinc((SV *)hv);
412 }
413 OUTPUT:
414 RETVAL
415  
416 SV *
417 path(self)
418 SV *self
419 PREINIT:
420 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
421 CODE:
422 4 ▶ 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 2 4
423 OUTPUT:
424 RETVAL
425  
426 int
427 memfd(self)
428 SV *self
429 PREINIT:
430 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
431 CODE:
432 4 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 4 4
433 OUTPUT:
434 RETVAL
435  
436 void
437 sync(self)
438 SV *self
439 PREINIT:
440 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
441 CODE:
442 3 ▶ if (!h->readonly && cms_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && cms_msync(h) != 0) croak("sync: %s", strerror(errno)); 2 1 3
if (!h->readonly && cms_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
443  
444 void
445 unlink(self, ...)
446 SV *self
447 CODE:
448 9 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::CountMinSketch::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::CountMinSketch::Shared")) { 4 2 6
if (sv_isobject(self) && sv_derived_from(self, "Data::CountMinSketch::Shared")) { 4 0 4
449 4 CmsHandle *h = INT2PTR(CmsHandle*, SvIV(SvRV(self)));
450 4 ▶ if (h && h->path && unlink(h->path) != 0 && errno != ENOENT)
Branch: 4 branches
BranchTrueFalseTotal
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 4 0 4
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 4 0 4
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 2 2 4
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 1 1 2
451 1 croak("Data::CountMinSketch::Shared->unlink(%s): %s", h->path, strerror(errno));
452 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
453 {
454 2 const char *up = SvPV_nolen(ST(1));
455 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
456 0 croak("Data::CountMinSketch::Shared->unlink(%s): %s", up, strerror(errno));
457 }
458 }
« cms.h ↑ Summary