Shared.xs

filter: Statementstmt 96.3% 183 / 190 Branchbran 53.2% 178 / 334 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 68.8% 361 / 524 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« topk.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 "topk.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::TopK::Shared")) \
10 croak("Expected a Data::TopK::Shared object"); \
11 TkHandle *h = INT2PTR(TkHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::TopK::Shared object"); \
13 TkHandle *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
19 * pins the referent only against refcount-driven destruction, not an
20 * explicit DESTROY, so the local `h` would dangle. Used only where magic
21 * can actually intervene between EXTRACT and the first use of h. */
22 #define REEXTRACT(sv) \
23 if (!SvROK(sv)) \
24 croak("Data::TopK::Shared object was replaced during the call"); \
25 h = INT2PTR(TkHandle*, SvIV(SvRV(sv))); \
26 if (h != h0) croak("Data::TopK::Shared object replaced or destroyed during the call")
27  
28 #define MAKE_OBJ(class, handle) \
29 SV *obj = newSViv(PTR2IV(handle)); \
30 SV *ref = newRV_noinc(obj); \
31 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
32 RETVAL = ref
33  
34 /* one snapshotted monitored entry, for sorting top() results.
35 * off is a byte offset into the key blob (used * key_size can exceed 4 GiB), so
36 * it must be 64-bit; klen <= key_size <= 4096 fits 32-bit. */
37 typedef struct { uint64_t count, error, off; uint32_t klen; } TkEnt;
38  
39 /* sort by count descending, then by error ascending (tighter estimate first) */
40 7956 static int tk_ent_cmp(const void *a, const void *b) {
41 7956 const TkEnt *x = (const TkEnt *)a, *y = (const TkEnt *)b;
42 7956 ▶ if (x->count > y->count) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (x->count > y->count) return -1; 3168 4788 7956
43 4788 ▶ if (x->count < y->count) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (x->count < y->count) return 1; 3692 1096 4788
44 1096 ▶ if (x->error < y->error) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (x->error < y->error) return -1; 47 1049 1096
45 1049 ▶ if (x->error > y->error) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (x->error > y->error) return 1; 81 968 1049
46 968 return 0;
47 }
48  
49 MODULE = Data::TopK::Shared PACKAGE = Data::TopK::Shared
50  
51 PROTOTYPES: DISABLE
52  
53 SV *
54 new(class, path = &PL_sv_undef, capacity = 0, key_size = 256, ...)
55 const char *class
56 SV *path
57 UV capacity
58 UV key_size
59 PREINIT:
60 char errbuf[TK_ERR_BUFLEN];
61 CODE:
62 30 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 29 30
63 1 croak("Data::TopK::Shared->new: capacity must be >= 1");
64 /* Optional 5th arg: file mode for a newly-created file-backed segment
65 * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing.
66 * Resolve its magic BEFORE capturing path's PV below: SvGETMAGIC runs
67 * arbitrary Perl that could realloc or free that PV. */
68 29 ▶ 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 29 29
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
69 /* Capture the path PV LAST, immediately before tk_create(): no get-magic
70 * may run between the capture and its use, or p could dangle. */
71 40 ▶ 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 29 29
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; 11 18 29
72 29 TkHandle *h = tk_create(p, (uint64_t)capacity, (uint64_t)key_size, TK_MODE_PLAIN, 0.0, mode, errbuf);
73 29 ▶ if (!h) croak("Data::TopK::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::TopK::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 23 29
if (!h) croak("Data::TopK::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 0 6
74 23 MAKE_OBJ(class, h);
75 OUTPUT:
76 RETVAL
77  
78 SV *
79 new_memfd(class, name = &PL_sv_undef, capacity = 0, key_size = 256)
80 const char *class
81 SV *name
82 UV capacity
83 UV key_size
84 PREINIT:
85 char errbuf[TK_ERR_BUFLEN];
86 CODE:
87 2 ▶ 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 1 1
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 0 1
88 1 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 0 1 1
89 0 croak("Data::TopK::Shared->new_memfd: capacity must be >= 1");
90 1 TkHandle *h = tk_create_memfd(nm, (uint64_t)capacity, (uint64_t)key_size, TK_MODE_PLAIN, 0.0, errbuf);
91 1 ▶ if (!h) croak("Data::TopK::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::TopK::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!h) croak("Data::TopK::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
92 1 MAKE_OBJ(class, h);
93 OUTPUT:
94 RETVAL
95  
96 SV *
97 new_decayed(class, path = &PL_sv_undef, capacity = 0, key_size = 256, half_life = 0, ...)
98 const char *class
99 SV *path
100 UV capacity
101 UV key_size
102 NV half_life
103 PREINIT:
104 char errbuf[TK_ERR_BUFLEN];
105 CODE:
106 18 ▶ if (capacity < 1) croak("Data::TopK::Shared->new_decayed: capacity must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) croak("Data::TopK::Shared->new_decayed: capacity must be >= 1"); 0 18 18
107 18 ▶ if (!((double)half_life > 0.0) || !isfinite((double)half_life))
Branch: 2 branches
BranchTrueFalseTotal
if (!((double)half_life > 0.0) || !isfinite((double)half_life)) 16 2 18
if (!((double)half_life > 0.0) || !isfinite((double)half_life)) 0 16 16
108 2 croak("Data::TopK::Shared->new_decayed: half_life must be a finite number > 0");
109 16 double alpha = log(2.0) / (double)half_life;
110 /* Resolve the optional mode arg's magic BEFORE capturing path's PV below:
111 * SvGETMAGIC runs arbitrary Perl that could realloc or free that PV. */
112 16 ▶ mode_t mode = (items > 5 && (SvGETMAGIC(ST(5)), SvOK(ST(5)))) ? (mode_t)SvUV(ST(5)) : 0600;
Branch: 4 branches
BranchTrueFalseTotal
mode_t mode = (items > 5 && (SvGETMAGIC(ST(5)), SvOK(ST(5)))) ? (mode_t)SvUV(ST(5)) : 0600; 0 16 16
mode_t mode = (items > 5 && (SvGETMAGIC(ST(5)), SvOK(ST(5)))) ? (mode_t)SvUV(ST(5)) : 0600; 0 0 0
mode_t mode = (items > 5 && (SvGETMAGIC(ST(5)), SvOK(ST(5)))) ? (mode_t)SvUV(ST(5)) : 0600; 0 0 0
mode_t mode = (items > 5 && (SvGETMAGIC(ST(5)), SvOK(ST(5)))) ? (mode_t)SvUV(ST(5)) : 0600; 0 0 0
113 /* Capture the path PV LAST, immediately before tk_create(): no get-magic
114 * may run between the capture and its use, or p could dangle. */
115 18 ▶ 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 16 16
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; 2 14 16
116 16 TkHandle *h = tk_create(p, (uint64_t)capacity, (uint64_t)key_size, TK_MODE_DECAYED, alpha, mode, errbuf);
117 16 ▶ if (!h) croak("Data::TopK::Shared->new_decayed: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::TopK::Shared->new_decayed: %s", errbuf); 0 16 16
118 16 MAKE_OBJ(class, h);
119 OUTPUT:
120 RETVAL
121  
122 SV *
123 new_decayed_memfd(class, name = &PL_sv_undef, capacity = 0, key_size = 256, half_life = 0)
124 const char *class
125 SV *name
126 UV capacity
127 UV key_size
128 NV half_life
129 PREINIT:
130 char errbuf[TK_ERR_BUFLEN];
131 CODE:
132 2 ▶ const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 0 1 1
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 0 0 0
const char *nm = (SvGETMAGIC(name), SvOK(name)) ? SvPV_nolen(name) : NULL; 1 0 1
133 1 ▶ if (capacity < 1) croak("Data::TopK::Shared->new_decayed_memfd: capacity must be >= 1");
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) croak("Data::TopK::Shared->new_decayed_memfd: capacity must be >= 1"); 0 1 1
134 1 ▶ if (!((double)half_life > 0.0) || !isfinite((double)half_life))
Branch: 2 branches
BranchTrueFalseTotal
if (!((double)half_life > 0.0) || !isfinite((double)half_life)) 1 0 1
if (!((double)half_life > 0.0) || !isfinite((double)half_life)) 0 1 1
135 0 croak("Data::TopK::Shared->new_decayed_memfd: half_life must be a finite number > 0");
136 1 double alpha = log(2.0) / (double)half_life;
137 1 TkHandle *h = tk_create_memfd(nm, (uint64_t)capacity, (uint64_t)key_size, TK_MODE_DECAYED, alpha, errbuf);
138 1 ▶ if (!h) croak("Data::TopK::Shared->new_decayed_memfd: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::TopK::Shared->new_decayed_memfd: %s", errbuf); 0 1 1
139 1 MAKE_OBJ(class, h);
140 OUTPUT:
141 RETVAL
142  
143 SV *
144 new_from_fd(class, fd)
145 const char *class
146 int fd
147 PREINIT:
148 char errbuf[TK_ERR_BUFLEN];
149 CODE:
150 2 TkHandle *h = tk_open_fd(fd, errbuf);
151 2 ▶ if (!h) croak("Data::TopK::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::TopK::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 2 2
if (!h) croak("Data::TopK::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
152 2 MAKE_OBJ(class, h);
153 OUTPUT:
154 RETVAL
155  
156 void
157 DESTROY(self)
158 SV *self
159 CODE:
160 45 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) { 45 0 45
if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) { 45 0 45
161 45 TkHandle *h = INT2PTR(TkHandle*, SvIV(SvRV(self)));
162 45 ▶ if (h) { sv_setiv(SvRV(self), 0); tk_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); tk_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 43 2 45
163 }
164  
165 SV *
166 add(self, item, timestamp = &PL_sv_undef)
167 SV *self
168 SV *item
169 SV *timestamp
170 PREINIT:
171 16184 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 16184 0 16184
EXTRACT(self); 0 16184 16184
EXTRACT(self); 0 16184 16184
172 STRLEN n;
173 const char *s;
174 16184 ▶ int has_ts; double ts = 0.0, g = 1.0; uint64_t raw;
Branch: 1 branch
BranchTrueFalseTotal
int has_ts; double ts = 0.0, g = 1.0; uint64_t raw; 6350 9834 16184
175 CODE:
176 /* Resolve the optional timestamp's magic FIRST: a tied/overloaded timestamp
177 * runs arbitrary Perl in FETCH/numify that could realloc or free item's SV
178 * buffer. Capture item's bytes LAST so nothing runs between the capture and
179 * the locked use (add_many guards the same hazard by copying to a mortal). */
180 16184 ▶ has_ts = (SvGETMAGIC(timestamp), SvOK(timestamp)); /* optional timestamp for decayed mode */
Branch: 2 branches
BranchTrueFalseTotal
has_ts = (SvGETMAGIC(timestamp), SvOK(timestamp)); /* optional timestamp for decayed mode */ 0 16184 16184
has_ts = (SvGETMAGIC(timestamp), SvOK(timestamp)); /* optional timestamp for decayed mode */ 0 0 0
181 16184 ▶ if (has_ts) { ts = (double)SvNV(timestamp); if (!isfinite(ts)) has_ts = 0; } /* ignore Inf/NaN -> per-add tick */
Branch: 2 branches
BranchTrueFalseTotal
if (has_ts) { ts = (double)SvNV(timestamp); if (!isfinite(ts)) has_ts = 0; } /* ignore Inf/NaN -> per-add tick */ 9834 6350 16184
if (has_ts) { ts = (double)SvNV(timestamp); if (!isfinite(ts)) has_ts = 0; } /* ignore Inf/NaN -> per-add tick */ 2 9832 9834
182 16183 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
183 16183 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 16183 16183
REEXTRACT(self); 0 16183 16183
184 16183 tk_rwlock_wrlock(h);
185 16183 raw = tk_observe_locked(h, s, n, has_ts, ts);
186 16183 ▶ if (h->mode == TK_MODE_DECAYED) g = tk_g(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode == TK_MODE_DECAYED) g = tk_g(h); 8354 7829 16183
187 16183 h->hdr->stat_ops++;
188 16183 tk_rwlock_wrunlock(h);
189 16183 ▶ RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(raw) / g) : newSVuv((UV)raw);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(raw) / g) : newSVuv((UV)raw); 8354 7829 16183
190 OUTPUT:
191 RETVAL
192  
193 UV
194 add_many(self, items)
195 SV *self
196 SV *items
197 PREINIT:
198 903 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 903 0 903
EXTRACT(self); 0 903 903
EXTRACT(self); 0 903 903
199 AV *av;
200 IV top;
201 903 ▶ UV processed = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV processed = 0; 903 0 903
202 CODE:
203 903 ▶ SvGETMAGIC(items);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(items); 0 903 903
SvGETMAGIC(items); 0 0 0
204 903 ▶ if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 902 1 903
if (!SvROK(items) || SvTYPE(SvRV(items)) != SVt_PVAV) 0 902 902
205 1 croak("Data::TopK::Shared->add_many: expected an array reference");
206 902 av = (AV *)SvRV(items);
207 902 sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin the arrayref: element magic below cannot free it mid-loop */
208 902 top = av_len(av); /* last index, -1 if empty */
209 {
210 902 ▶ STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN cnt = (top >= 0) ? (STRLEN)(top + 1) : 0, i; 902 0 902
211 902 const char **ps = NULL; STRLEN *ls = NULL;
212 902 ▶ if (cnt) { /* resolve all bytes BEFORE locking */
Branch: 1 branch
BranchTrueFalseTotal
if (cnt) { /* resolve all bytes BEFORE locking */ 902 0 902
213 902 ▶ 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 902 902
214 902 ▶ Newx(ls, cnt, STRLEN); SAVEFREEPV(ls);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ls, cnt, STRLEN); SAVEFREEPV(ls); 0 902 902
215 7234 ▶ 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 */ 6332 902 7234
216 6332 SV **el = av_fetch(av, (SSize_t)i, 0);
217 6332 ▶ if (el && *el) {
Branch: 2 branches
BranchTrueFalseTotal
if (el && *el) { 6332 0 6332
if (el && *el) { 6332 0 6332
218 STRLEN len;
219 6332 const char *src = SvPVbyte(*el, len); /* may run overload/tie/get-magic = arbitrary Perl */
220 /* Copy bytes into a private mortal SV NOW: a LATER element SvPVbyte can
221 * grow/free THIS element PV, dangling src before the locked loop uses it. */
222 6332 SV *copy = sv_2mortal(newSVpvn(src, len));
223 6332 ps[i] = SvPVX_const(copy);
224 6332 ls[i] = len;
225 0 } else { ps[i] = ""; ls[i] = 0; }
226 }
227 }
228 902 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 902 902
REEXTRACT(self); 0 902 902
229 902 tk_rwlock_wrlock(h); /* locked region: NO croak-capable calls */
230 7234 ▶ for (i = 0; i < cnt; i++) { tk_observe_locked(h, ps[i], ls[i], 0, 0.0); processed++; } /* decayed: per-add tick */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cnt; i++) { tk_observe_locked(h, ps[i], ls[i], 0, 0.0); processed++; } /* decayed: per-add tick */ 6332 902 7234
231 902 h->hdr->stat_ops++; /* a call always counts, even an empty batch */
232 902 tk_rwlock_wrunlock(h);
233 }
234 902 ▶ RETVAL = processed;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = processed; 899 3 902
235 OUTPUT:
236 RETVAL
237  
238 SV *
239 estimate(self, item)
240 SV *self
241 SV *item
242 PREINIT:
243 12130 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 12130 0 12130
EXTRACT(self); 0 12130 12130
EXTRACT(self); 0 12130 12130
244 STRLEN n;
245 const char *s;
246 12130 uint64_t raw; double g = 1.0;
247 CODE:
248 12129 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
249 12129 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 12129 12129
REEXTRACT(self); 0 12129 12129
250 12129 tk_rwlock_rdlock(h);
251 12129 raw = tk_estimate_locked(h, s, n, NULL);
252 12129 ▶ if (h->mode == TK_MODE_DECAYED) g = tk_g(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode == TK_MODE_DECAYED) g = tk_g(h); 6070 6059 12129
253 12129 tk_rwlock_rdunlock(h);
254 12129 ▶ RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(raw) / g) : newSVuv((UV)raw);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(raw) / g) : newSVuv((UV)raw); 6070 6059 12129
255 OUTPUT:
256 RETVAL
257  
258 SV *
259 error(self, item)
260 SV *self
261 SV *item
262 PREINIT:
263 3004 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3004 0 3004
EXTRACT(self); 0 3004 3004
EXTRACT(self); 0 3004 3004
264 STRLEN n;
265 const char *s;
266 3004 uint64_t err = 0; double g = 1.0;
267 CODE:
268 3004 s = SvPVbyte(item, n); /* may croak (wide char) -- BEFORE the lock */
269 3004 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 3004 3004
REEXTRACT(self); 0 3004 3004
270 3004 tk_rwlock_rdlock(h);
271 3004 (void)tk_estimate_locked(h, s, n, &err);
272 3004 ▶ if (h->mode == TK_MODE_DECAYED) g = tk_g(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode == TK_MODE_DECAYED) g = tk_g(h); 1500 1504 3004
273 3004 tk_rwlock_rdunlock(h);
274 3004 ▶ RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(err) / g) : newSVuv((UV)err);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(err) / g) : newSVuv((UV)err); 1500 1504 3004
275 OUTPUT:
276 RETVAL
277  
278 void
279 top(self, ...)
280 SV *self
281 PREINIT:
282 426 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 426 0 426
EXTRACT(self); 0 426 426
EXTRACT(self); 0 426 426
283 PPCODE:
284 {
285 426 ▶ UV k = (items > 1 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) ? SvUV(ST(1)) : 0; /* optional; undef/omitted = 0 = all */
Branch: 4 branches
BranchTrueFalseTotal
UV k = (items > 1 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) ? SvUV(ST(1)) : 0; /* optional; undef/omitted = 0 = all */ 11 415 426
UV k = (items > 1 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) ? SvUV(ST(1)) : 0; /* optional; undef/omitted = 0 = all */ 0 11 11
UV k = (items > 1 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) ? SvUV(ST(1)) : 0; /* optional; undef/omitted = 0 = all */ 0 0 0
UV k = (items > 1 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) ? SvUV(ST(1)) : 0; /* optional; undef/omitted = 0 = all */ 11 0 11
286 426 TkEnt *ents = NULL;
287 426 char *keys = NULL;
288 426 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 426 426
REEXTRACT(self); 0 426 426
289 426 uint64_t used, ks = h->key_size, i, cap = h->capacity;
290 /* Allocate worst-case snapshot buffers (all `capacity` slots) BEFORE the
291 lock: Newx can croak on OOM, and under the read lock that longjmp would
292 strand the lock. `used` (below) is clamped to capacity, so these fit. */
293 426 ▶ if (cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (cap) { 426 0 426
294 426 ▶ Newx(ents, (size_t)cap, TkEnt); SAVEFREEPV(ents);
Branch: 1 branch
BranchTrueFalseTotal
Newx(ents, (size_t)cap, TkEnt); SAVEFREEPV(ents); 0 426 426
295 426 Newx(keys, (size_t)(cap * ks), char); SAVEFREEPV(keys);
296 }
297 /* Snapshot every monitored (count, error, key) under the read lock; the
298 copied key bytes cannot then be evicted out from under us, and all
299 Perl-value building happens after the unlock. */
300 426 tk_rwlock_rdlock(h);
301 426 ▶ double g = (h->mode == TK_MODE_DECAYED) ? tk_g(h) : 1.0; /* decay factor snapshot */
Branch: 1 branch
BranchTrueFalseTotal
double g = (h->mode == TK_MODE_DECAYED) ? tk_g(h) : 1.0; /* decay factor snapshot */ 209 217 426
302 426 used = tk_heap_size(h); /* == used, clamped to capacity (Layer B) */
303 3509 ▶ for (i = 0; i < used; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < used; i++) { 3083 426 3509
304 3083 TkSlot *sl = tk_slot(h, i);
305 3083 ▶ uint32_t kl = sl->key_len; if (kl > h->key_size) kl = h->key_size; /* Layer B */
Branch: 1 branch
BranchTrueFalseTotal
uint32_t kl = sl->key_len; if (kl > h->key_size) kl = h->key_size; /* Layer B */ 0 3083 3083
306 3083 ents[i].count = sl->count;
307 3083 ents[i].error = sl->error;
308 3083 ents[i].off = i * ks; /* 64-bit byte offset into the key blob */
309 3083 ents[i].klen = kl;
310 3083 memcpy(keys + ents[i].off, tk_slot_key(sl), kl);
311 }
312 426 tk_rwlock_rdunlock(h);
313  
314 426 ▶ if (used) {
Branch: 1 branch
BranchTrueFalseTotal
if (used) { 413 13 426
315 413 qsort(ents, (size_t)used, sizeof(TkEnt), tk_ent_cmp);
316 413 ▶ uint64_t want = (k == 0 || k > used) ? used : (uint64_t)k;
Branch: 2 branches
BranchTrueFalseTotal
uint64_t want = (k == 0 || k > used) ? used : (uint64_t)k; 10 403 413
uint64_t want = (k == 0 || k > used) ? used : (uint64_t)k; 1 9 10
317 413 ▶ EXTEND(SP, (SSize_t)want);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)want); 413 0 413
EXTEND(SP, (SSize_t)want); 0 413 413
318 3446 ▶ for (i = 0; i < want; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < want; i++) { 3033 413 3446
319 3033 HV *hv = newHV();
320 3033 hv_stores(hv, "key", newSVpvn(keys + ents[i].off, ents[i].klen));
321 3033 ▶ if (h->mode == TK_MODE_DECAYED) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->mode == TK_MODE_DECAYED) { 1509 1524 3033
322 1509 hv_stores(hv, "count", newSVnv(tk_w_get(ents[i].count) / g));
323 1509 hv_stores(hv, "error", newSVnv(tk_w_get(ents[i].error) / g));
324 } else {
325 1524 hv_stores(hv, "count", newSVuv((UV)ents[i].count));
326 1524 hv_stores(hv, "error", newSVuv((UV)ents[i].error));
327 }
328 3033 PUSHs(sv_2mortal(newRV_noinc((SV *)hv)));
329 }
330 }
331 }
332  
333 void
334 clear(self)
335 SV *self
336 PREINIT:
337 13 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 13 0 13
EXTRACT(self); 0 13 13
EXTRACT(self); 0 13 13
338 CODE:
339 13 tk_rwlock_wrlock(h);
340 13 tk_clear_locked(h);
341 13 h->hdr->stat_ops++;
342 13 tk_rwlock_wrunlock(h);
343  
344 UV
345 capacity(self)
346 SV *self
347 PREINIT:
348 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
349 CODE:
350 3 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 3 3
351 OUTPUT:
352 RETVAL
353  
354 UV
355 key_size(self)
356 SV *self
357 PREINIT:
358 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
359 CODE:
360 2 ▶ RETVAL = (UV)h->hdr->key_size;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->key_size; 0 2 2
361 OUTPUT:
362 RETVAL
363  
364 int
365 is_decayed(self)
366 SV *self
367 PREINIT:
368 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
369 CODE:
370 3 ▶ RETVAL = (h->mode == TK_MODE_DECAYED) ? 1 : 0; /* cached mode, no lock */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (h->mode == TK_MODE_DECAYED) ? 1 : 0; /* cached mode, no lock */ 0 3 3
371 OUTPUT:
372 RETVAL
373  
374 NV
375 half_life(self)
376 SV *self
377 PREINIT:
378 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
379 CODE:
380 2 ▶ RETVAL = (h->mode == TK_MODE_DECAYED && h->alpha > 0.0) ? (NV)(log(2.0) / h->alpha) : (NV)0.0;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = (h->mode == TK_MODE_DECAYED && h->alpha > 0.0) ? (NV)(log(2.0) / h->alpha) : (NV)0.0; 2 0 2
RETVAL = (h->mode == TK_MODE_DECAYED && h->alpha > 0.0) ? (NV)(log(2.0) / h->alpha) : (NV)0.0; 2 0 2
RETVAL = (h->mode == TK_MODE_DECAYED && h->alpha > 0.0) ? (NV)(log(2.0) / h->alpha) : (NV)0.0; 0 2 2
381 OUTPUT:
382 RETVAL
383  
384 UV
385 tracked(self)
386 SV *self
387 PREINIT:
388 8 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 8 0 8
EXTRACT(self); 0 8 8
EXTRACT(self); 1 7 8
389 UV n;
390 CODE:
391 7 tk_rwlock_rdlock(h);
392 7 n = (UV)tk_heap_size(h);
393 7 tk_rwlock_rdunlock(h);
394 7 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 0 7 7
395 OUTPUT:
396 RETVAL
397  
398 UV
399 seen(self)
400 SV *self
401 PREINIT:
402 8 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 8 0 8
EXTRACT(self); 0 8 8
EXTRACT(self); 0 8 8
403 UV n;
404 CODE:
405 8 tk_rwlock_rdlock(h);
406 8 n = (UV)h->hdr->seen;
407 8 tk_rwlock_rdunlock(h);
408 8 ▶ RETVAL = n;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = n; 0 8 8
409 OUTPUT:
410 RETVAL
411  
412 SV *
413 stats(self)
414 SV *self
415 PREINIT:
416 414 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 414 0 414
EXTRACT(self); 0 414 414
EXTRACT(self); 0 414 414
417 CODE:
418 {
419 uint64_t used, seen, ops;
420 uint32_t cap, ks;
421 414 tk_rwlock_rdlock(h);
422 414 used = tk_heap_size(h);
423 414 seen = h->hdr->seen;
424 414 cap = h->hdr->capacity;
425 414 ks = h->hdr->key_size;
426 414 ops = h->hdr->stat_ops;
427 414 tk_rwlock_rdunlock(h);
428  
429 414 HV *hv = newHV();
430 414 hv_stores(hv, "capacity", newSVuv((UV)cap));
431 414 hv_stores(hv, "key_size", newSVuv((UV)ks));
432 414 hv_stores(hv, "tracked", newSVuv((UV)used));
433 414 hv_stores(hv, "seen", newSVuv((UV)seen));
434 414 hv_stores(hv, "ops", newSVuv((UV)ops));
435 414 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
436 414 hv_stores(hv, "decayed", newSViv(h->mode == TK_MODE_DECAYED ? 1 : 0));
437 414 ▶ if (h->mode == TK_MODE_DECAYED && h->alpha > 0.0)
Branch: 2 branches
BranchTrueFalseTotal
if (h->mode == TK_MODE_DECAYED && h->alpha > 0.0) 207 207 414
if (h->mode == TK_MODE_DECAYED && h->alpha > 0.0) 207 0 207
438 207 hv_stores(hv, "half_life", newSVnv(log(2.0) / h->alpha));
439 414 RETVAL = newRV_noinc((SV *)hv);
440 }
441 OUTPUT:
442 RETVAL
443  
444 SV *
445 path(self)
446 SV *self
447 PREINIT:
448 1 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1 0 1
EXTRACT(self); 0 1 1
EXTRACT(self); 0 1 1
449 CODE:
450 1 ▶ RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; 1 0 1
451 OUTPUT:
452 RETVAL
453  
454 int
455 memfd(self)
456 SV *self
457 PREINIT:
458 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
459 CODE:
460 2 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 2 2
461 OUTPUT:
462 RETVAL
463  
464 void
465 sync(self)
466 SV *self
467 PREINIT:
468 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
469 CODE:
470 3 ▶ if (tk_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (tk_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 3 3
471  
472 void
473 unlink(self, ...)
474 SV *self
475 CODE:
476 1 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) { 0 1 1
if (sv_isobject(self) && sv_derived_from(self, "Data::TopK::Shared")) { 0 0 0
477 0 TkHandle *h = INT2PTR(TkHandle*, SvIV(SvRV(self)));
478 0 ▶ if (h && h->path && unlink(h->path) != 0 && errno != ENOENT)
Branch: 4 branches
BranchTrueFalseTotal
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
479 0 croak("Data::TopK::Shared->unlink(%s): %s", h->path, strerror(errno));
480 1 ▶ } else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 1 0 1
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 1 1
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 0 0
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 1 0 1
481 {
482 1 const char *up = SvPV_nolen(ST(1));
483 1 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 0 1 1
if (unlink(up) != 0 && errno != ENOENT) 0 0 0
484 0 croak("Data::TopK::Shared->unlink(%s): %s", up, strerror(errno));
485 }
486 }
« topk.h ↑ Summary