Arena.xs

filter: Statementstmt 95.8% 23 / 24 Branchbran 75.0% 12 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.5% 35 / 40 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/cuckoo.xs ↑ Summary blib/arch/Shared/Arena/Install/Files.pm »
1 /*
2 * Arena.xs - root XS file for Shared::Arena.
3 *
4 * The perl headers, then the compat shim that puts back what PERL_IMPLICIT_SYS
5 * takes away, then the C implementation headers from include/sa/ in dependency
6 * order, then the per-package XS fragments from xs/ via INCLUDE:.
7 *
8 * This file sits at the TOP of the dist rather than under lib/. An XSMULTI
9 * build from lib/Shared/Arena.xs writes its linker export list to
10 * lib/Shared/Arena.def, while the import-library rule Strawberry adds for
11 * MinGW reads $(EXPORT_LIST), which is always $(BASEEXT).def in the top
12 * directory - the two names never meet and dlltool fails the build.
13 *
14 * The rules every header under include/sa/ follows:
15 *
16 * 1. Perl-free. A region is bytes and offsets; nothing below the XS layer
17 * needs an interpreter, which is what lets a harness with no perl drive it.
18 * 2. NOTHING INSIDE A REGION IS EVER A POINTER. Offsets from the base, always,
19 * so a process that maps at a different address reads the same structure.
20 * t/03-relocatable.t is the test, and it maps one region twice in one
21 * process precisely so that a stray pointer fails deterministically.
22 * 3. Errors are a return value, never a croak. Every function is ABI-shaped
23 * from the start, because the ABI is how the real consumer reaches it.
24 * 4. Fail open. No atomics, no mapping, a name that will not open: a NULL
25 * handle and a caller that degrades. Never a crash.
26 * 5. Every wait is bounded. A process that died holding something must not
27 * wedge the ones that did not.
28 * 6. C89 declarations at block top. No VLAs, no designated initialisers,
29 * no %zu, no //.
30 *
31 * Two exceptions to rule 1, both named for it: sa_xop.h, which is the doors as
32 * opcodes, and sa_serialise.h, which is a codec and has nothing to do without
33 * an interpreter.
34 */
35  
36 #define PERL_NO_GET_CONTEXT
37 #include "EXTERN.h"
38 #include "perl.h"
39 #include "XSUB.h"
40  
41 #include "sa/sa_compat.h" /* puts back what XSUB.h took; must be first */
42  
43 #include "sa/sa_atomic.h" /* the probe and the operations (perl-free) */
44 #include "sa/sa_format.h" /* the layout: offsets only */
45 #include "sa/sa_map.h" /* where the memory comes from */
46 #include "sa/sa_arena.h" /* the handle, the bump, the registry */
47 #include "sa/sa_peer.h" /* who is here, and how to prove one has died */
48 #include "sa/sa_ring.h" /* the first tenant: a record ring */
49 #include "sa/sa_hash.h" /* the second tenant: a map */
50 #include "sa/sa_bloom.h" /* the third tenant: a probabilistic set */
51 #include "sa/sa_hist.h" /* the fourth tenant: a distribution */
52 #include "sa/sa_cache.h" /* the fifth tenant: a cache that evicts */
53 #include "sa/sa_rate.h" /* the sixth tenant: a token bucket per key */
54 #include "sa/sa_cms.h" /* the seventh: how often, in fixed space */
55 #include "sa/sa_cuckoo.h" /* a set that can forget */
56 #include "sa/sa_frozen.h" /* one structure, republished, read in place */
57 #include "sa/sa_lease.h" /* one holder at a time, successor on death */
58 #include "sa/sa_scoreboard.h" /* one row per worker, published live */
59 #include "sa/sa_hll.h" /* how many distinct, in a few kilobytes */
60 #include "sa/sa_serialise.h" /* a value as a structure, via Struct::Codec */
61 #include "sa/sa_xop.h" /* the hot doors as opcodes */
62 #include "sa_abi.h"
63  
64 /* ---- Frozen, a hard prerequisite ------------------------------------------
65 *
66 * Every other tenant stores bytes a caller flattened itself. This one stores a
67 * structure that is READ WHERE IT LIES: no rebuilding on the way out, so a
68 * worker looks up one key without materialising the rest. Frozen is the format
69 * and the reader, reached through its ABI table so a read costs no Perl frame.
70 *
71 * The table is fetched once at BOOT through Frozen::_abi_ptr. The version is
72 * checked against what THIS file was compiled against, with >= rather than ==:
73 * the table is append-only, so a newer Frozen is fine and an older one is not.
74 * Refusing at load is the point - the alternative is calling through a null
75 * entry at the first publish, in production, in a worker. */
76 #include "fz_abi.h"
77  
78 static const fz_abi *SA_FZ = NULL;
79  
80 /* A borrowed view: the container, the tenant it came from, and which slot and
81 * generation it was opened on, because a borrowed block can be republished
82 * underneath it and `fresh` is how a reader finds out. The length is kept here
83 * rather than read back off the container, which is opaque to a consumer. */
84 typedef struct {
85 fz_container *c;
86 sa_frozen *f;
87 uint64_t len;
88 uint64_t gen;
89 uint32_t slot;
90 } sa_fzview; /* the PUBLIC table a consumer resolves */
91 #include "sa/sa_abi_impl.h" /* and what it points at */
92  
93 /* The Frozen tenant's own reader, behind `$frozen->get` and friends.
94 *
95 * A view per read is two allocations, a bless, magic and a Perl-level DESTROY:
96 * 151ns to take and drop, against 51ns for the read through it. So the tenant
97 * handle keeps one borrowed container over the live slot and borrows again
98 * only when a publish has moved `current`, which makes a read cost a
99 * generation check. NULL when nothing is published or the bytes will not
100 * borrow. */
101 5 static void sa_fz_reader_free(void *c) {
102 5 (void)(SA_FZ->release)((fz_container *)c);
103 5 }
104  
105 15 static fz_container *sa_fz_reader(sa_frozen *f) {
106 15 const unsigned char *bytes = NULL;
107 15 uint64_t len = 0, gen = 0;
108 15 uint32_t slot = 0;
109 fz_container *c;
110 15 int err = 0;
111  
112 15 ▶ if (sa_frozen_current(f, &bytes, &len, &gen, &slot) != SA_FZ_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (sa_frozen_current(f, &bytes, &len, &gen, &slot) != SA_FZ_OK) 3 12 15
113 3 return NULL;
114 12 ▶ if (f->reader && f->reader_gen == gen && f->reader_slot == slot)
Branch: 3 branches
BranchTrueFalseTotal
if (f->reader && f->reader_gen == gen && f->reader_slot == slot) 11 1 12
if (f->reader && f->reader_gen == gen && f->reader_slot == slot) 7 4 11
if (f->reader && f->reader_gen == gen && f->reader_slot == slot) 7 0 7
115 7 return (fz_container *)f->reader;
116 5 c = (SA_FZ->borrow)(bytes, (size_t)len, &err);
117 5 ▶ if (!c) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return NULL; 0 5 5
118 5 ▶ if (f->reader) sa_fz_reader_free(f->reader);
Branch: 1 branch
BranchTrueFalseTotal
if (f->reader) sa_fz_reader_free(f->reader); 4 1 5
119 5 f->reader = c;
120 5 f->reader_free = sa_fz_reader_free;
121 5 f->reader_gen = gen;
122 5 f->reader_slot = slot;
123 5 return c;
124 }
125  
126 /* ---- THE INVOCANT IS AN ARGUMENT THAT ARRIVED FROM PERL --------------------
127 *
128 * Every object in this dist is a blessed SV carrying a C pointer as its IV, and
129 * every entry used to reach it with INT2PTR(T *, SvIV(SvRV(self))) directly.
130 * SvRV on something that is not a reference reads the union member a plain
131 * string keeps its PV in, so `Shared::Arena->size` - a CLASS NAME invocant,
132 * which is a typo away from every documented call - dereferenced a char buffer
133 * as a struct. Measured: SIGBUS, not a croak.
134 *
135 * One guard, in one place, in front of every entry. It cannot tell a forged
136 * integer from a real handle, and nothing with this representation can; what it
137 * does is turn the two mistakes a caller actually makes - a class name, and an
138 * object of the wrong kind - into an error message.
139 *
140 * This lives here rather than under include/sa/, because everything there is
141 * perl-free and this is the opposite of that. */
142 2159668 static void *sa_self_ptr(pTHX_ SV *sv, const char *what) {
143 2159668 ▶ if (!SvROK(sv) || !SvOBJECT(SvRV(sv)))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || !SvOBJECT(SvRV(sv))) 2159668 0 2159668
if (!SvROK(sv) || !SvOBJECT(SvRV(sv))) 0 2159668 2159668
144 0 croak("Shared::Arena: not a %s object", what);
145 2159668 return INT2PTR(void *, SvIV(SvRV(sv)));
146 }
147 #define SA_SELF(type, sv) ((type *)sa_self_ptr(aTHX_ (sv), #type))
148  
149 MODULE = Shared::Arena PACKAGE = Shared::Arena
150  
151 PROTOTYPES: DISABLE
152  
153 INCLUDE: xs/region.xs
154 INCLUDE: xs/ring.xs
155 INCLUDE: xs/group.xs
156 INCLUDE: xs/map.xs
157 INCLUDE: xs/bloom.xs
158 INCLUDE: xs/hist.xs
159 INCLUDE: xs/cache.xs
160 INCLUDE: xs/rate.xs
161 INCLUDE: xs/cms.xs
162 INCLUDE: xs/cuckoo.xs
163 INCLUDE: xs/frozen.xs
164 INCLUDE: xs/lease.xs
165 INCLUDE: xs/scoreboard.xs
166 INCLUDE: xs/hll.xs
167 INCLUDE: xs/abi.xs
168 INCLUDE: xs/xop.xs
« xs/cuckoo.xs ↑ Summary blib/arch/Shared/Arena/Install/Files.pm »