include/hyperman/hm_sa.h

filter: Statementstmt 92.8% 52 / 56 Branchbran 57.6% 41 / 78 (6 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.3% 93 / 134 (6 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hyperman/hm_stream.h ↑ Summary xs/abi.xs »
1 /* hm_sa.h - Hyperman as a CONSUMER of Shared::Arena's C ABI.
2 *
3 * One arena per server process, created from Perl before the workers fork so
4 * every worker inherits the same mapping, and driven from C through the
5 * versioned table Shared::Arena::_abi_ptr hands out. The object is created
6 * from Perl rather than through the table's `create` so that Hyperman->arena
7 * is a real Shared::Arena with every tenant available to Perl, and the C side
8 * reaches the same region through `_region_ptr`. One owner, one lifetime: the
9 * object is held for the life of the process and never released while the
10 * server runs.
11 *
12 * Perl-facing on purpose - it calls into the interpreter to resolve the table
13 * and to make the object - which is why it lives beside hm_abi_impl.h and not
14 * among the perl-free headers.
15 *
16 * EVERYTHING FAILS OPEN. A NULL table (Shared::Arena absent, too old, or
17 * HYPERMAN_NO_SA_ABI set) or a NULL region (create refused) leaves every door
18 * that would use them answering exactly what it answers today with no arena.
19 * A missing provider is a missing optimisation, never a broken server.
20 */
21  
22 #ifndef HM_SA_H
23 #define HM_SA_H
24  
25 #include "sa_abi.h"
26  
27 static const sa_abi *HM_SA = NULL; /* the table, or NULL: fail open */
28 static sa_region *hm_sa_region = NULL; /* the arena, or NULL: fail open */
29 static SV *hm_sa_arena_sv = NULL; /* the Shared::Arena object, held */
30  
31 /* At BOOT, once. The VALUE eval_pv returns, not the top of the stack: eval_pv
32 * has already popped what it evaluated (sa_abi.h spells out the trap). */
33 149 static void hm_sa_resolve(pTHX) {
34 SV *sv, *err;
35 /*
36 # uncoverable branch true
37 # uncoverable condition left count:1 class:ignore_covered_err */
38 149 ▶ if (HM_SA) return;
Branch: 1 branch
BranchTrueFalseTotal
if (HM_SA) return; 0 149 149
39 149 ▶ if (getenv("HYPERMAN_NO_SA_ABI")) return;
Branch: 1 branch
BranchTrueFalseTotal
if (getenv("HYPERMAN_NO_SA_ABI")) return; 3 146 149
40 146 sv = eval_pv("require Shared::Arena; Shared::Arena::_abi_ptr()", 0);
41 146 err = get_sv("@", 0);
42 /*
43 # uncoverable branch true class:ignore_covered_err
44 # uncoverable condition left count:1 class:ignore_covered_err
45 # uncoverable condition left count:2 class:ignore_covered_err
46 # uncoverable condition left count:3 class:ignore_covered_err
47 # uncoverable condition right count:3 class:ignore_covered_err
48 # uncoverable condition left count:4 class:ignore_covered_err */
49 146 ▶ if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 146 0 146
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 146 0 146
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 146 0 146
if (!sv || !SvOK(sv) || (err && SvTRUE(err))) return; 0 146 146
50 {
51 146 const sa_abi *t = INT2PTR(const sa_abi *, SvUV(sv));
52 /*
53 # uncoverable branch false class:ignore_covered_err
54 # uncoverable condition right count:1 class:ignore_covered_err
55 # uncoverable condition right count:2 class:ignore_covered_err */
56 146 ▶ if (t && t->abi_version >= SA_ABI_VERSION) HM_SA = t;
Branch: 2 branches
BranchTrueFalseTotal
if (t && t->abi_version >= SA_ABI_VERSION) HM_SA = t; 146 0 146
if (t && t->abi_version >= SA_ABI_VERSION) HM_SA = t; 146 0 146
57 }
58 }
59  
60 /* How big an arena the server needs: the sum of every tenant the later
61 * phases carve, from the same numbers those tenants are sized with, plus
62 * headroom. Generous by design - a slot's exact bytes are the provider's
63 * business - and a carve that does not fit is refused by Shared::Arena and
64 * that tenant fails open with a warning naming the shortfall. */
65 99 static UV hm_sa_bytes(unsigned deny_cap, unsigned rate_cap,
66 unsigned bus_slots, unsigned bus_slot_size) {
67 99 UV b = 0;
68 99 ▶ if (!deny_cap) deny_cap = 1024;
Branch: 1 branch
BranchTrueFalseTotal
if (!deny_cap) deny_cap = 1024; 98 1 99
69 99 ▶ if (!rate_cap) rate_cap = 4096;
Branch: 1 branch
BranchTrueFalseTotal
if (!rate_cap) rate_cap = 4096; 98 1 99
70 99 ▶ if (!bus_slots) bus_slots = 2048;
Branch: 1 branch
BranchTrueFalseTotal
if (!bus_slots) bus_slots = 2048; 94 5 99
71 99 ▶ if (!bus_slot_size) bus_slot_size = 2048;
Branch: 1 branch
BranchTrueFalseTotal
if (!bus_slot_size) bus_slot_size = 2048; 94 5 99
72 99 b += (UV)deny_cap * 96 + 4096; /* hm_sa_abuse.h: the denylist */
73 99 b += (UV)rate_cap * 64 + 4096; /* hm_sa_abuse.h: the window */
74 99 b += (UV)bus_slots * (bus_slot_size + 64) /* hm_sa_bus.h: the ring, its */
75 + 64 * 128 + 4096; /* slot 64 wider, plus groups */
76 99 b += 256 * 256 + 4096; /* phase 06: the scoreboard */
77 99 b += 16384 + 4096 + 4096; /* phase 06: HLL p=14, leases */
78 99 b += 65536; /* headroom */
79 99 return b;
80 }
81  
82 /* Create the arena FROM PERL, before the fork. Idempotent: one arena per
83 * process. `name` NULL or empty is anonymous; a name that already exists is
84 * attached, not recreated, which is what a restart wants. */
85 101 static void hm_sa_open(pTHX_ const char *name, STRLEN nlen, UV bytes) {
86 101 dSP;
87 int count;
88 101 SV *ret = NULL;
89  
90 101 ▶ if (!HM_SA || hm_sa_arena_sv) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!HM_SA || hm_sa_arena_sv) return; 98 3 101
if (!HM_SA || hm_sa_arena_sv) return; 14 84 98
91  
92 84 ENTER; SAVETMPS;
93 84 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 84 84
94 84 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 84 84
95 84 mPUSHp("Shared::Arena", 13);
96 84 mPUSHp("size", 4);
97 84 mPUSHu(bytes);
98 84 ▶ if (name && nlen) { mPUSHp("name", 4); mPUSHp(name, nlen); } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (name && nlen) { mPUSHp("name", 4); mPUSHp(name, nlen); } /* # uncoverable condition right count:2 class:ignore_covered_err */ 7 77 84
if (name && nlen) { mPUSHp("name", 4); mPUSHp(name, nlen); } /* # uncoverable condition right count:2 class:ignore_covered_err */ 7 0 7
99 84 PUTBACK;
100 84 count = call_method("create", G_SCALAR | G_EVAL);
101 84 SPAGAIN;
102 /*
103 # uncoverable branch false class:ignore_covered_err
104 # uncoverable condition right count:1 class:ignore_covered_err */
105 84 ▶ if (count == 1) ret = POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count == 1) ret = POPs; 84 0 84
106 /*
107 # uncoverable branch true class:ignore_covered_err
108 # uncoverable condition left count:1 class:ignore_covered_err
109 # uncoverable condition left count:2 class:ignore_covered_err
110 # uncoverable condition left count:3 class:ignore_covered_err */
111 84 ▶ if (SvTRUE(ERRSV) || !ret || !SvROK(ret)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV) || !ret || !SvROK(ret)) { 84 0 84
if (SvTRUE(ERRSV) || !ret || !SvROK(ret)) { 84 0 84
if (SvTRUE(ERRSV) || !ret || !SvROK(ret)) { 84 0 84
if (SvTRUE(ERRSV) || !ret || !SvROK(ret)) { 0 84 84
112 0 ▶ warn("Hyperman: Shared::Arena->create failed (%s); running without " /* # uncoverable statement */
Branch: 3 branches
BranchTrueFalseTotal
warn("Hyperman: Shared::Arena->create failed (%s); running without " /* # uncoverable statement */ 0 0 0
warn("Hyperman: Shared::Arena->create failed (%s); running without " /* # uncoverable statement */ 0 0 0
warn("Hyperman: Shared::Arena->create failed (%s); running without " /* # uncoverable statement */ 0 0 0
113 /*
114 # uncoverable statement
115 # uncoverable condition left count:1
116 # uncoverable condition right count:1
117 # uncoverable condition left count:2
118 # uncoverable condition right count:2 */
119 "an arena", SvTRUE(ERRSV) ? SvPV_nolen(ERRSV) : "no object");
120 0 ▶ PUTBACK; FREETMPS; LEAVE; /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; /* # uncoverable statement */ 0 0 0
121 0 return; /* # uncoverable statement */
122 }
123 84 hm_sa_arena_sv = newSVsv(ret);
124 84 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 84 0 84
125  
126 /* The region behind the object, for the C side. */
127 84 ENTER; SAVETMPS;
128 84 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 84 84
129 84 ▶ XPUSHs(hm_sa_arena_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(hm_sa_arena_sv); 0 84 84
130 84 PUTBACK;
131 84 count = call_method("_region_ptr", G_SCALAR | G_EVAL);
132 84 SPAGAIN;
133 /*
134 # uncoverable branch false class:ignore_covered_err
135 # uncoverable condition right count:1 class:ignore_covered_err
136 # uncoverable condition right count:2 class:ignore_covered_err */
137 84 ▶ if (count == 1 && !SvTRUE(ERRSV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (count == 1 && !SvTRUE(ERRSV)) { 84 0 84
if (count == 1 && !SvTRUE(ERRSV)) { 84 0 84
if (count == 1 && !SvTRUE(ERRSV)) { 84 0 84
138 84 SV *p = POPs;
139 84 hm_sa_region = INT2PTR(sa_region *, SvUV(p));
140 }
141 else {
142 0 ▶ warn("Hyperman: Shared::Arena::_region_ptr failed (%s); the arena is " /* # uncoverable statement */
Branch: 3 branches
BranchTrueFalseTotal
warn("Hyperman: Shared::Arena::_region_ptr failed (%s); the arena is " /* # uncoverable statement */ 0 0 0
warn("Hyperman: Shared::Arena::_region_ptr failed (%s); the arena is " /* # uncoverable statement */ 0 0 0
warn("Hyperman: Shared::Arena::_region_ptr failed (%s); the arena is " /* # uncoverable statement */ 0 0 0
143 /*
144 # uncoverable statement
145 # uncoverable condition left count:1
146 # uncoverable condition right count:1
147 # uncoverable condition left count:2
148 # uncoverable condition right count:2 */
149 "reachable from Perl only", SvTRUE(ERRSV) ? SvPV_nolen(ERRSV) : "?");
150 }
151 84 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 84 0 84
152 }
153  
154 /* After the fork, once per worker: a peer slot of this worker's own, so a
155 * record it leaves unfinished can be attributed to it. */
156 134 static void hm_sa_worker_join(void) {
157 134 ▶ if (HM_SA && hm_sa_region) (void)HM_SA->join(hm_sa_region); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (HM_SA && hm_sa_region) (void)HM_SA->join(hm_sa_region); /* # uncoverable condition right count:2 class:ignore_covered_err */ 130 4 134
if (HM_SA && hm_sa_region) (void)HM_SA->join(hm_sa_region); /* # uncoverable condition right count:2 class:ignore_covered_err */ 130 0 130
158 134 }
159  
160 #endif /* HM_SA_H */
« include/hyperman/hm_stream.h ↑ Summary xs/abi.xs »