Shared.xs

filter: Statementstmt 95.0% 95 / 100 Branchbran 50.0% 98 / 196 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.2% 193 / 296 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hiertimingwheel.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 "hiertimingwheel.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::HierTimingWheel::Shared")) \
10 croak("Expected a Data::HierTimingWheel::Shared object"); \
11 HwHandle *h = INT2PTR(HwHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::HierTimingWheel::Shared object"); \
13 HwHandle *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. EXTRACT's
17 * sv_2mortal(SvREFCNT_inc(...)) pin only blocks REFCOUNT-driven destruction;
18 * an explicit $obj->DESTROY frees the handle regardless and zeroes the IV.
19 * NOTE: a typed parameter with a DEFAULT (e.g. `ticks = 1`) is converted by
20 * xsubpp AFTER the PREINIT block, not in INPUT -- so its magic runs after
21 * EXTRACT captured the handle, unlike a typed parameter with no default.
22 * The same Perl can also REPLACE the invocant ($obj = 42 mutates ST(0),
23 * because Perl passes aliases), hence the SvROK re-check before SvRV. */
24 #define REEXTRACT(sv) \
25 if (!SvROK(sv)) \
26 croak("Data::HierTimingWheel::Shared object was replaced during the call"); \
27 h = INT2PTR(HwHandle*, SvIV(SvRV(sv))); \
28 if (h != h0) croak("Data::HierTimingWheel::Shared object replaced or destroyed during the call")
29  
30 #define MAKE_OBJ(class, handle) \
31 SV *obj = newSViv(PTR2IV(handle)); \
32 SV *ref = newRV_noinc(obj); \
33 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
34 RETVAL = ref
35  
36 MODULE = Data::HierTimingWheel::Shared PACKAGE = Data::HierTimingWheel::Shared
37  
38 PROTOTYPES: DISABLE
39  
40 SV *
41 new(class, path = &PL_sv_undef, num_slots = 256, num_levels = 4, capacity = 0, ...)
42 const char *class
43 SV *path
44 UV num_slots
45 UV num_levels
46 UV capacity
47 PREINIT:
48 char errbuf[HW_ERR_BUFLEN];
49 CODE:
50 74 ▶ 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 49 49
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; 25 24 49
51 49 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 48 49
52 1 croak("Data::HierTimingWheel::Shared->new: capacity must be >= 1");
53 /* Optional 6th arg: file mode for a newly-created file-backed segment
54 * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing. */
55 48 ▶ 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 48 48
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
56 48 HwHandle *h = hw_create(p, (uint64_t)num_slots, (uint64_t)num_levels, (uint64_t)capacity, mode, errbuf);
57 48 ▶ if (!h) croak("Data::HierTimingWheel::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::HierTimingWheel::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 7 41 48
if (!h) croak("Data::HierTimingWheel::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 7 0 7
58 41 MAKE_OBJ(class, h);
59 OUTPUT:
60 RETVAL
61  
62 SV *
63 new_memfd(class, name = &PL_sv_undef, num_slots = 256, num_levels = 4, capacity = 0)
64 const char *class
65 SV *name
66 UV num_slots
67 UV num_levels
68 UV capacity
69 PREINIT:
70 char errbuf[HW_ERR_BUFLEN];
71 CODE:
72 18 ▶ 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 9 9
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 */ 9 0 9
73 9 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 0 9 9
74 0 croak("Data::HierTimingWheel::Shared->new_memfd: capacity must be >= 1");
75 9 HwHandle *h = hw_create_memfd(nm, (uint64_t)num_slots, (uint64_t)num_levels, (uint64_t)capacity, errbuf);
76 9 ▶ if (!h) croak("Data::HierTimingWheel::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::HierTimingWheel::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 9 9
if (!h) croak("Data::HierTimingWheel::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
77 9 MAKE_OBJ(class, h);
78 OUTPUT:
79 RETVAL
80  
81 SV *
82 new_from_fd(class, fd)
83 const char *class
84 int fd
85 PREINIT:
86 char errbuf[HW_ERR_BUFLEN];
87 CODE:
88 8 HwHandle *h = hw_open_fd(fd, errbuf);
89 8 ▶ if (!h) croak("Data::HierTimingWheel::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::HierTimingWheel::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 8 8
if (!h) croak("Data::HierTimingWheel::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
90 8 MAKE_OBJ(class, h);
91 OUTPUT:
92 RETVAL
93  
94 void
95 DESTROY(self)
96 SV *self
97 CODE:
98 60 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::HierTimingWheel::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::HierTimingWheel::Shared")) { 60 0 60
if (sv_isobject(self) && sv_derived_from(self, "Data::HierTimingWheel::Shared")) { 60 0 60
99 60 HwHandle *h = INT2PTR(HwHandle*, SvIV(SvRV(self)));
100 60 ▶ if (h) { sv_setiv(SvRV(self), 0); hw_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); hw_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 58 2 60
101 }
102  
103 UV
104 add(self, delay, payload)
105 SV *self
106 UV delay
107 UV payload
108 PREINIT:
109 10553 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 10553 0 10553
EXTRACT(self); 0 10553 10553
EXTRACT(self); 0 10553 10553
110 int64_t id;
111 CODE:
112 10553 hw_rwlock_wrlock(h);
113 10553 id = hw_add_locked(h, (uint64_t)delay, (uint64_t)payload);
114 10553 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
115 10553 hw_rwlock_wrunlock(h);
116 10553 ▶ if (id == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (id == -2) { 5 10548 10553
117 5 ▶ if (delay > (UV)IV_MAX && SvIOKp(ST(1)) && !SvIsUV(ST(1))) {
Branch: 3 branches
BranchTrueFalseTotal
if (delay > (UV)IV_MAX && SvIOKp(ST(1)) && !SvIsUV(ST(1))) { 4 1 5
if (delay > (UV)IV_MAX && SvIOKp(ST(1)) && !SvIsUV(ST(1))) { 4 0 4
if (delay > (UV)IV_MAX && SvIOKp(ST(1)) && !SvIsUV(ST(1))) { 4 0 4
118 4 hw_rwlock_wrlock(h);
119 4 id = hw_add_locked(h, 1, (uint64_t)payload);
120 4 hw_rwlock_wrunlock(h);
121 } else
122 1 croak("Data::HierTimingWheel::Shared->add: delay %" UVuf " exceeds the wheel range (max %" UVuf "); use more levels or slots",
123 (UV)delay, (UV)(h->tick[h->num_levels] - 1));
124 }
125 10552 ▶ if (id < 0) croak("Data::HierTimingWheel::Shared->add: timer pool is full (capacity %u)", (unsigned)h->capacity);
Branch: 1 branch
BranchTrueFalseTotal
if (id < 0) croak("Data::HierTimingWheel::Shared->add: timer pool is full (capacity %u)", (unsigned)h->capacity); 83 10469 10552
126 10469 ▶ RETVAL = (UV)id;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)id; 10441 28 10469
127 OUTPUT:
128 RETVAL
129  
130 int
131 cancel(self, timer_id)
132 SV *self
133 UV timer_id
134 PREINIT:
135 24597 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 24597 0 24597
EXTRACT(self); 0 24597 24597
EXTRACT(self); 0 24597 24597
136 CODE:
137 24597 hw_rwlock_wrlock(h);
138 24597 RETVAL = hw_cancel_locked(h, (uint64_t)timer_id);
139 24597 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
140 24597 hw_rwlock_wrunlock(h);
141 OUTPUT:
142 RETVAL
143  
144 void
145 advance(self, ticks = 1)
146 SV *self
147 UV ticks
148 PREINIT:
149 7435 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7435 0 7435
EXTRACT(self); 0 7435 7435
EXTRACT(self); 0 7435 7435
150 PPCODE:
151 {
152 7435 ▶ REEXTRACT(self); /* `ticks = 1` is converted after PREINIT, so its magic already ran */
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); /* `ticks = 1` is converted after PREINIT, so its magic already ran */ 0 7435 7435
REEXTRACT(self); /* `ticks = 1` is converted after PREINIT, so its magic already ran */ 0 7435 7435
153 7435 uint64_t *out = NULL, fired = 0, i, cap = h->capacity;
154 7435 ▶ if (cap) { Newx(out, (size_t)cap, uint64_t); SAVEFREEPV(out); } /* alloc BEFORE the lock */
Branch: 2 branches
BranchTrueFalseTotal
if (cap) { Newx(out, (size_t)cap, uint64_t); SAVEFREEPV(out); } /* alloc BEFORE the lock */ 7435 0 7435
if (cap) { Newx(out, (size_t)cap, uint64_t); SAVEFREEPV(out); } /* alloc BEFORE the lock */ 0 7435 7435
155 7435 hw_rwlock_wrlock(h);
156 7435 ▶ fired = cap ? hw_advance_locked(h, (uint64_t)ticks, out, cap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
fired = cap ? hw_advance_locked(h, (uint64_t)ticks, out, cap) : 0; 7435 0 7435
157 7435 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
158 7435 hw_rwlock_wrunlock(h);
159 7435 ▶ EXTEND(SP, (SSize_t)fired);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)fired); 7435 0 7435
EXTEND(SP, (SSize_t)fired); 0 7435 7435
160 11807 ▶ for (i = 0; i < fired; i++) PUSHs(sv_2mortal(newSVuv((UV)out[i]))); /* Perl alloc AFTER unlock */
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < fired; i++) PUSHs(sv_2mortal(newSVuv((UV)out[i]))); /* Perl alloc AFTER unlock */ 4372 7435 11807
161 }
162  
163 UV
164 now(self)
165 SV *self
166 PREINIT:
167 21006 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 21006 0 21006
EXTRACT(self); 0 21006 21006
EXTRACT(self); 0 21006 21006
168 UV v;
169 CODE:
170 21006 hw_rwlock_rdlock(h);
171 21006 v = (UV)h->hdr->now;
172 21006 hw_rwlock_rdunlock(h);
173 21006 ▶ RETVAL = v;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v; 20999 7 21006
174 OUTPUT:
175 RETVAL
176  
177 UV
178 count(self)
179 SV *self
180 PREINIT:
181 21034 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 21034 0 21034
EXTRACT(self); 0 21034 21034
EXTRACT(self); 1 21033 21034
182 UV v;
183 CODE:
184 21033 hw_rwlock_rdlock(h);
185 21033 v = (UV)h->hdr->count;
186 21033 hw_rwlock_rdunlock(h);
187 21033 ▶ RETVAL = v;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v; 21019 14 21033
188 OUTPUT:
189 RETVAL
190  
191 UV
192 capacity(self)
193 SV *self
194 PREINIT:
195 1 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1 0 1
EXTRACT(self); 0 1 1
EXTRACT(self); 0 1 1
196 CODE:
197 1 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 1 1
198 OUTPUT:
199 RETVAL
200  
201 UV
202 num_slots(self)
203 SV *self
204 PREINIT:
205 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
206 CODE:
207 3 ▶ RETVAL = (UV)h->hdr->num_slots;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->num_slots; 0 3 3
208 OUTPUT:
209 RETVAL
210  
211 UV
212 num_levels(self)
213 SV *self
214 PREINIT:
215 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
216 CODE:
217 2 ▶ RETVAL = (UV)h->num_levels;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->num_levels; 0 2 2
218 OUTPUT:
219 RETVAL
220  
221 UV
222 max_delay(self)
223 SV *self
224 PREINIT:
225 22 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 22 0 22
EXTRACT(self); 0 22 22
EXTRACT(self); 0 22 22
226 CODE:
227 22 ▶ RETVAL = (UV)(h->tick[h->num_levels] - 1); /* largest schedulable delay = S^L - 1 */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)(h->tick[h->num_levels] - 1); /* largest schedulable delay = S^L - 1 */ 20 2 22
228 OUTPUT:
229 RETVAL
230  
231 void
232 clear(self)
233 SV *self
234 PREINIT:
235 172 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 172 0 172
EXTRACT(self); 0 172 172
EXTRACT(self); 0 172 172
236 CODE:
237 172 hw_rwlock_wrlock(h);
238 172 hw_clear_locked(h);
239 172 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
240 172 hw_rwlock_wrunlock(h);
241  
242 SV *
243 stats(self)
244 SV *self
245 PREINIT:
246 1 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1 0 1
EXTRACT(self); 0 1 1
EXTRACT(self); 0 1 1
247 CODE:
248 {
249 uint64_t now, count, ops;
250 1 hw_rwlock_rdlock(h);
251 1 now = h->hdr->now;
252 1 count = h->hdr->count;
253 1 ops = h->hdr->stat_ops;
254 1 hw_rwlock_rdunlock(h);
255 1 HV *hv = newHV();
256 1 hv_stores(hv, "now", newSVuv((UV)now));
257 1 hv_stores(hv, "count", newSVuv((UV)count));
258 1 hv_stores(hv, "num_slots", newSVuv((UV)h->num_slots)); /* cached geometry */
259 1 hv_stores(hv, "num_levels", newSVuv((UV)h->num_levels));
260 1 hv_stores(hv, "max_delay", newSVuv((UV)(h->tick[h->num_levels] - 1)));
261 1 hv_stores(hv, "capacity", newSVuv((UV)h->capacity));
262 1 hv_stores(hv, "ops", newSVuv((UV)ops));
263 1 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
264 1 RETVAL = newRV_noinc((SV *)hv);
265 }
266 OUTPUT:
267 RETVAL
268  
269 SV *
270 path(self)
271 SV *self
272 PREINIT:
273 1 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1 0 1
EXTRACT(self); 0 1 1
EXTRACT(self); 0 1 1
274 CODE:
275 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
276 OUTPUT:
277 RETVAL
278  
279 int
280 memfd(self)
281 SV *self
282 PREINIT:
283 9 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9 0 9
EXTRACT(self); 0 9 9
EXTRACT(self); 0 9 9
284 CODE:
285 9 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 6 3 9
286 OUTPUT:
287 RETVAL
288  
289 void
290 sync(self)
291 SV *self
292 PREINIT:
293 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
294 CODE:
295 2 ▶ if (hw_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (hw_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
296  
297 void
298 unlink(self, ...)
299 SV *self
300 CODE:
301 1 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::HierTimingWheel::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::HierTimingWheel::Shared")) { 0 1 1
if (sv_isobject(self) && sv_derived_from(self, "Data::HierTimingWheel::Shared")) { 0 0 0
302 0 HwHandle *h = INT2PTR(HwHandle*, SvIV(SvRV(self)));
303 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
304 0 croak("Data::HierTimingWheel::Shared->unlink(%s): %s", h->path, strerror(errno));
305 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
306 {
307 1 const char *up = SvPV_nolen(ST(1));
308 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
309 0 croak("Data::HierTimingWheel::Shared->unlink(%s): %s", up, strerror(errno));
310 }
311 }
« hiertimingwheel.h ↑ Summary