Shared.xs

filter: Statementstmt 96.8% 92 / 95 Branchbran 51.6% 92 / 178 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.4% 184 / 273 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« timingwheel.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 "timingwheel.h"
7  
8 #define EXTRACT(sv) \
9 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::TimingWheel::Shared")) \
10 croak("Expected a Data::TimingWheel::Shared object"); \
11 TwHandle *h = INT2PTR(TwHandle*, SvIV(SvRV(sv))); \
12 if (!h) croak("Attempted to use a destroyed Data::TimingWheel::Shared object"); \
13 TwHandle *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::TimingWheel::Shared object was replaced during the call"); \
27 h = INT2PTR(TwHandle*, SvIV(SvRV(sv))); \
28 if (h != h0) croak("Data::TimingWheel::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::TimingWheel::Shared PACKAGE = Data::TimingWheel::Shared
37  
38 PROTOTYPES: DISABLE
39  
40 SV *
41 new(class, path = &PL_sv_undef, num_slots = 512, capacity = 0, ...)
42 const char *class
43 SV *path
44 UV num_slots
45 UV capacity
46 PREINIT:
47 char errbuf[TW_ERR_BUFLEN];
48 CODE:
49 /* Optional 5th arg: file mode for a newly-created file-backed segment
50 * (default 0600, owner-only). Pass e.g. 0660 for cross-user sharing.
51 * Resolve it BEFORE capturing the path PV: its get-magic can run Perl
52 * code that may realloc/free that PV, so capture p last, right before
53 * tw_create(). */
54 30 ▶ 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 30 30
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
55 30 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 1 29 30
56 1 croak("Data::TimingWheel::Shared->new: capacity must be >= 1");
57 45 ▶ 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; 16 13 29
58 29 TwHandle *h = tw_create(p, (uint64_t)num_slots, (uint64_t)capacity, mode, errbuf);
59 29 ▶ if (!h) croak("Data::TimingWheel::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::TimingWheel::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 5 24 29
if (!h) croak("Data::TimingWheel::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 5 0 5
60 24 MAKE_OBJ(class, h);
61 OUTPUT:
62 RETVAL
63  
64 SV *
65 new_memfd(class, name = &PL_sv_undef, num_slots = 512, capacity = 0)
66 const char *class
67 SV *name
68 UV num_slots
69 UV capacity
70 PREINIT:
71 char errbuf[TW_ERR_BUFLEN];
72 CODE:
73 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
74 1 ▶ if (capacity < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < 1) 0 1 1
75 0 croak("Data::TimingWheel::Shared->new_memfd: capacity must be >= 1");
76 1 TwHandle *h = tw_create_memfd(nm, (uint64_t)num_slots, (uint64_t)capacity, errbuf);
77 1 ▶ if (!h) croak("Data::TimingWheel::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::TimingWheel::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!h) croak("Data::TimingWheel::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
78 1 MAKE_OBJ(class, h);
79 OUTPUT:
80 RETVAL
81  
82 SV *
83 new_from_fd(class, fd)
84 const char *class
85 int fd
86 PREINIT:
87 char errbuf[TW_ERR_BUFLEN];
88 CODE:
89 1 TwHandle *h = tw_open_fd(fd, errbuf);
90 1 ▶ if (!h) croak("Data::TimingWheel::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::TimingWheel::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 1 1
if (!h) croak("Data::TimingWheel::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
91 1 MAKE_OBJ(class, h);
92 OUTPUT:
93 RETVAL
94  
95 void
96 DESTROY(self)
97 SV *self
98 CODE:
99 28 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::TimingWheel::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::TimingWheel::Shared")) { 28 0 28
if (sv_isobject(self) && sv_derived_from(self, "Data::TimingWheel::Shared")) { 28 0 28
100 28 TwHandle *h = INT2PTR(TwHandle*, SvIV(SvRV(self)));
101 28 ▶ if (h) { sv_setiv(SvRV(self), 0); tw_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); tw_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 26 2 28
102 }
103  
104 UV
105 add(self, delay, payload)
106 SV *self
107 IV delay
108 UV payload
109 PREINIT:
110 49 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 49 0 49
EXTRACT(self); 0 49 49
EXTRACT(self); 0 49 49
111 int64_t id;
112 CODE:
113 /* signed delay: a negative value (e.g. from clock skew) clamps to 1 tick per
114 * the POD, instead of UV-wrapping to a ~2^64 delay that never fires (slot leak) */
115 49 tw_rwlock_wrlock(h);
116 49 ▶ id = tw_add_locked(h, delay < 1 ? 1 : (uint64_t)delay, (uint64_t)payload);
Branch: 1 branch
BranchTrueFalseTotal
id = tw_add_locked(h, delay < 1 ? 1 : (uint64_t)delay, (uint64_t)payload); 48 1 49
117 49 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
118 49 tw_rwlock_wrunlock(h);
119 49 ▶ if (id < 0) croak("Data::TimingWheel::Shared->add: timer pool is full (capacity %u)", (unsigned)h->capacity);
Branch: 1 branch
BranchTrueFalseTotal
if (id < 0) croak("Data::TimingWheel::Shared->add: timer pool is full (capacity %u)", (unsigned)h->capacity); 1 48 49
120 48 ▶ RETVAL = (UV)id;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)id; 22 26 48
121 OUTPUT:
122 RETVAL
123  
124 int
125 cancel(self, timer_id)
126 SV *self
127 UV timer_id
128 PREINIT:
129 16 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 16 0 16
EXTRACT(self); 0 16 16
EXTRACT(self); 0 16 16
130 CODE:
131 16 tw_rwlock_wrlock(h);
132 16 RETVAL = tw_cancel_locked(h, (uint64_t)timer_id);
133 16 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
134 16 tw_rwlock_wrunlock(h);
135 OUTPUT:
136 RETVAL
137  
138 void
139 advance(self, ticks = 1)
140 SV *self
141 IV ticks
142 PREINIT:
143 338 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 338 0 338
EXTRACT(self); 0 338 338
EXTRACT(self); 0 338 338
144 PPCODE:
145 {
146 338 ▶ 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 338 338
REEXTRACT(self); /* `ticks = 1` is converted after PREINIT, so its magic already ran */ 0 338 338
147 338 uint64_t *out = NULL, fired = 0, i, cap = h->capacity;
148 /* signed: reject a negative tick count (would UV-wrap to ~2^64 and spin the
149 * per-tick loop under the write lock, wedging every sharer) */
150 338 ▶ if (ticks < 0) croak("Data::TimingWheel::Shared->advance: ticks must be >= 0");
Branch: 1 branch
BranchTrueFalseTotal
if (ticks < 0) croak("Data::TimingWheel::Shared->advance: ticks must be >= 0"); 0 338 338
151 338 ▶ 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 */ 338 0 338
if (cap) { Newx(out, (size_t)cap, uint64_t); SAVEFREEPV(out); } /* alloc BEFORE the lock */ 0 338 338
152 338 tw_rwlock_wrlock(h);
153 338 ▶ fired = cap ? tw_advance_locked(h, (uint64_t)ticks, out, cap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
fired = cap ? tw_advance_locked(h, (uint64_t)ticks, out, cap) : 0; 338 0 338
154 338 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
155 338 tw_rwlock_wrunlock(h);
156 338 ▶ EXTEND(SP, (SSize_t)fired);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)fired); 338 0 338
EXTEND(SP, (SSize_t)fired); 0 338 338
157 375 ▶ 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 */ 37 338 375
158 }
159  
160 UV
161 now(self)
162 SV *self
163 PREINIT:
164 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
165 UV v;
166 CODE:
167 5 tw_rwlock_rdlock(h);
168 5 v = (UV)h->hdr->now;
169 5 tw_rwlock_rdunlock(h);
170 5 ▶ RETVAL = v;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v; 0 5 5
171 OUTPUT:
172 RETVAL
173  
174 UV
175 count(self)
176 SV *self
177 PREINIT:
178 16 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 16 0 16
EXTRACT(self); 0 16 16
EXTRACT(self); 1 15 16
179 UV v;
180 CODE:
181 15 tw_rwlock_rdlock(h);
182 15 v = (UV)h->hdr->count;
183 15 tw_rwlock_rdunlock(h);
184 15 ▶ RETVAL = v;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = v; 2 13 15
185 OUTPUT:
186 RETVAL
187  
188 UV
189 capacity(self)
190 SV *self
191 PREINIT:
192 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
193 CODE:
194 2 ▶ RETVAL = (UV)h->hdr->capacity;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->capacity; 0 2 2
195 OUTPUT:
196 RETVAL
197  
198 UV
199 num_slots(self)
200 SV *self
201 PREINIT:
202 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
203 CODE:
204 3 ▶ RETVAL = (UV)h->hdr->num_slots;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->num_slots; 0 3 3
205 OUTPUT:
206 RETVAL
207  
208 void
209 clear(self)
210 SV *self
211 PREINIT:
212 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
213 CODE:
214 2 tw_rwlock_wrlock(h);
215 2 tw_clear_locked(h);
216 2 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
217 2 tw_rwlock_wrunlock(h);
218  
219 SV *
220 stats(self)
221 SV *self
222 PREINIT:
223 1 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1 0 1
EXTRACT(self); 0 1 1
EXTRACT(self); 0 1 1
224 CODE:
225 {
226 uint64_t now, count, ops;
227 uint32_t num_slots, cap, cur;
228 1 tw_rwlock_rdlock(h);
229 1 now = h->hdr->now;
230 1 count = h->hdr->count;
231 1 num_slots = h->hdr->num_slots;
232 1 cap = h->hdr->capacity;
233 1 cur = h->hdr->cur;
234 1 ops = h->hdr->stat_ops;
235 1 tw_rwlock_rdunlock(h);
236 1 HV *hv = newHV();
237 1 hv_stores(hv, "now", newSVuv((UV)now));
238 1 hv_stores(hv, "count", newSVuv((UV)count));
239 1 hv_stores(hv, "num_slots", newSVuv((UV)num_slots));
240 1 hv_stores(hv, "capacity", newSVuv((UV)cap));
241 1 hv_stores(hv, "cur", newSVuv((UV)cur));
242 1 hv_stores(hv, "ops", newSVuv((UV)ops));
243 1 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
244 1 RETVAL = newRV_noinc((SV *)hv);
245 }
246 OUTPUT:
247 RETVAL
248  
249 SV *
250 path(self)
251 SV *self
252 PREINIT:
253 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
254 CODE:
255 3 ▶ RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; 3 0 3
256 OUTPUT:
257 RETVAL
258  
259 int
260 memfd(self)
261 SV *self
262 PREINIT:
263 1 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 1 0 1
EXTRACT(self); 0 1 1
EXTRACT(self); 0 1 1
264 CODE:
265 1 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 1 1
266 OUTPUT:
267 RETVAL
268  
269 void
270 sync(self)
271 SV *self
272 PREINIT:
273 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
274 CODE:
275 3 ▶ if (tw_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (tw_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 3 3
276  
277 void
278 unlink(self, ...)
279 SV *self
280 CODE:
281 9 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::TimingWheel::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::TimingWheel::Shared")) { 4 1 5
if (sv_isobject(self) && sv_derived_from(self, "Data::TimingWheel::Shared")) { 4 0 4
282 4 TwHandle *h = INT2PTR(TwHandle*, SvIV(SvRV(self)));
283 4 ▶ if (h && h->path) {
Branch: 2 branches
BranchTrueFalseTotal
if (h && h->path) { 4 0 4
if (h && h->path) { 4 0 4
284 4 ▶ const char *up = h->unlink_path ? h->unlink_path : h->path;
Branch: 1 branch
BranchTrueFalseTotal
const char *up = h->unlink_path ? h->unlink_path : h->path; 4 0 4
285 4 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 2 2 4
if (unlink(up) != 0 && errno != ENOENT) 0 2 2
286 0 croak("Data::TimingWheel::Shared->unlink(%s): %s", h->path, strerror(errno));
287 }
288 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
289 {
290 1 const char *up = SvPV_nolen(ST(1));
291 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
292 0 croak("Data::TimingWheel::Shared->unlink(%s): %s", up, strerror(errno));
293 }
294 }
« timingwheel.h ↑ Summary