xs/cache.xs

filter: Statementstmt 93.6% 104 / 111 Branchbran 66.3% 61 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.2% 165 / 203 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/sa/sa_arena.h ↑ Summary xs/group.xs »
1 # Shared::Arena::Cache - a map that evicts instead of refusing.
2  
3 MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_
4  
5 # $arena->cache($name, capacity => N, ways => 8, entry_size => N,
6 # serialise => 0)
7 #
8 # `serialise` is part of the cache's SHAPE, kept in the shared header: every
9 # process binding it must ask for the same thing, and one that does not is
10 # refused as a different entry_size would be.
11 SV *
12 sar_cache(self, name, ...)
13 SV *self
14 SV *name
15 PREINIT:
16 sa_region *arena;
17 sa_reg *e;
18 sa_cache *c;
19 const char *nm;
20 STRLEN nlen;
21 19 UV capacity = 1024, ways = 8, entry = 512;
22 uint64_t nbuckets;
23 19 int err = SA_E_OK, serialise = 0;
24 I32 i;
25 SV *obj;
26 CODE:
27 19 arena = SA_SELF(sa_region, self);
28 19 ▶ if (!arena || !arena->map.base)
Branch: 2 branches
BranchTrueFalseTotal
if (!arena || !arena->map.base) 19 0 19
if (!arena || !arena->map.base) 0 19 19
29 0 croak("Shared::Arena: this region is released");
30 19 nm = SvPV(name, nlen);
31 71 ▶ for (i = 2; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < items; i += 2) { 52 19 71
32 52 const char *o = SvPV_nolen(ST(i));
33 52 ▶ if (strEQ(o, "capacity")) capacity = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(o, "capacity")) capacity = SvUV(ST(i + 1)); 19 33 52
34 33 ▶ else if (strEQ(o, "ways")) ways = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(o, "ways")) ways = SvUV(ST(i + 1)); 10 23 33
35 23 ▶ else if (strEQ(o, "entry_size")) entry = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(o, "entry_size")) entry = SvUV(ST(i + 1)); 18 5 23
36 5 ▶ else if (strEQ(o, "serialise")) serialise = SvTRUE(ST(i + 1)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(o, "serialise")) serialise = SvTRUE(ST(i + 1)) ? 1 : 0; 5 0 5
37 }
38 19 ▶ if (ways < SA_CACHE_MIN_WAYS) ways = SA_CACHE_MIN_WAYS;
Branch: 1 branch
BranchTrueFalseTotal
if (ways < SA_CACHE_MIN_WAYS) ways = SA_CACHE_MIN_WAYS; 0 19 19
39 19 ▶ if (ways > SA_CACHE_MAX_WAYS) ways = SA_CACHE_MAX_WAYS;
Branch: 1 branch
BranchTrueFalseTotal
if (ways > SA_CACHE_MAX_WAYS) ways = SA_CACHE_MAX_WAYS; 0 19 19
40 19 ▶ if (capacity < ways) capacity = ways;
Branch: 1 branch
BranchTrueFalseTotal
if (capacity < ways) capacity = ways; 0 19 19
41 19 ▶ if (sa_cache_capacity((uint32_t)entry) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sa_cache_capacity((uint32_t)entry) == 0) 0 19 19
42 0 croak("Shared::Arena: entry_size %lu is smaller than an entry header",
43 (unsigned long)entry);
44 19 nbuckets = ((uint64_t)capacity + ways - 1) / ways;
45  
46 19 e = sa_carve(arena, nm, (size_t)nlen,
47 sa_cache_bytes(nbuckets, (uint32_t)ways, (uint32_t)entry),
48 SA_T_CACHE, &err);
49 19 ▶ if (!e) croak("Shared::Arena: the cache '%s' %s", nm, sa_strerror(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!e) croak("Shared::Arena: the cache '%s' %s", nm, sa_strerror(err)); 0 19 19
50  
51 19 c = sa_cache_bind(arena, e, nbuckets, (uint32_t)ways, (uint32_t)entry,
52 serialise, &err);
53 19 ▶ if (!c) croak("Shared::Arena: the cache '%s' %s", nm, sa_strerror(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena: the cache '%s' %s", nm, sa_strerror(err)); 3 16 19
54  
55 16 obj = newSV(0);
56 16 sv_setref_pv(obj, "Shared::Arena::Cache", (void *)c);
57 16 sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0);
58 16 SvREFCNT_inc(SvRV(self));
59 16 RETVAL = obj;
60 OUTPUT:
61 RETVAL
62  
63 MODULE = Shared::Arena PACKAGE = Shared::Arena::Cache PREFIX = sac_
64  
65 # 1 when stored, -1 when the pair does not fit an entry. There is no "full":
66 # that is the whole difference between this and a map.
67 #
68 # On a serialised cache the value is encoded first, and one whose encoding
69 # does not fit is the same -1. A value that cannot be encoded croaks and stores
70 # nothing.
71 IV
72 sac_set(self, key, value, ...)
73 SV *self
74 SV *key
75 SV *value
76 PREINIT:
77 sa_cache *c;
78 const char *k, *v;
79 STRLEN klen, vlen;
80 7 ▶ UV ttl = 0;
Branch: 1 branch
BranchTrueFalseTotal
UV ttl = 0; 7 0 7
81 I32 i;
82 char sbuf[SA_SER_STACK];
83 CODE:
84 7 c = SA_SELF(sa_cache, self);
85 7 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 1 6 7
86 6 k = SvPV(key, klen);
87 6 ▶ if (c->serialise)
Branch: 1 branch
BranchTrueFalseTotal
if (c->serialise) 3 3 6
88 3 vlen = sa_ser_encode(aTHX_ value, c->pair_max, klen, sbuf, &v);
89 else
90 3 v = SvPV(value, vlen);
91 7 ▶ for (i = 3; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i + 1 < items; i += 2) { 2 5 7
92 2 const char *o = SvPV_nolen(ST(i));
93 /* seconds, because that is what a caller thinks in; milliseconds
94 * for anybody who needs them. */
95 2 ▶ if (strEQ(o, "ttl")) ttl = (UV)(SvNV(ST(i + 1)) * 1000.0);
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(o, "ttl")) ttl = (UV)(SvNV(ST(i + 1)) * 1000.0); 1 1 2
96 1 ▶ else if (strEQ(o, "ttl_ms")) ttl = SvUV(ST(i + 1));
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(o, "ttl_ms")) ttl = SvUV(ST(i + 1)); 0 1 1
97 }
98 7 ▶ RETVAL = (c->serialise && !vlen)
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (c->serialise && !vlen) 1 1 2
RETVAL = (c->serialise && !vlen) 0 5 5
99 ? SA_C_TOOBIG
100 7 ▶ : sa_cache_set(c, k, (uint32_t)klen, v, (uint32_t)vlen,
Branch: 1 branch
BranchTrueFalseTotal
: sa_cache_set(c, k, (uint32_t)klen, v, (uint32_t)vlen, 2 3 5
101 (uint64_t)ttl);
102 OUTPUT:
103 RETVAL
104  
105 # The value, or an EMPTY LIST for a miss - which includes an entry whose
106 # deadline has passed, because a cache that returns stale data is not a cache.
107 void
108 sac_get(self, key)
109 SV *self
110 SV *key
111 PREINIT:
112 sa_cache *c;
113 const char *k;
114 STRLEN klen;
115 SV *out;
116 char buf[1024];
117 5 uint32_t vlen = 0;
118 int rc;
119 PPCODE:
120 5 c = SA_SELF(sa_cache, self);
121 5 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 1 4 5
122 4 k = SvPV(key, klen);
123 /* Onto the stack first where an entry fits, so a miss allocates
124 * nothing and a hit allocates what the value needs rather than the
125 * most an entry could hold, which a caller keeping the value would
126 * otherwise carry for its life. */
127 /* On a serialised cache the copy is decoded from wherever it landed,
128 * and the answer goes through ST(0), which is reached from `ax` and
129 * survives a decode that grew the value stack. */
130 4 ▶ if (c->pair_max < sizeof buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->pair_max < sizeof buf) { 3 1 4
131 3 rc = sa_cache_get(c, k, (uint32_t)klen, buf,
132 3 (uint32_t)c->pair_max, &vlen);
133 3 ▶ if (rc != SA_C_HIT) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SA_C_HIT) XSRETURN_EMPTY; 0 3 3
134 3 ▶ if (c->serialise) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->serialise) { 2 1 3
135 2 ST(0) = SA_SER_DECODE(buf, vlen);
136 2 XSRETURN(1);
137 }
138 1 ▶ XPUSHs(sv_2mortal(newSVpvn(buf, (STRLEN)vlen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(buf, (STRLEN)vlen))); 0 1 1
139 }
140 else {
141 1 out = sv_2mortal(newSV((STRLEN)c->pair_max + 1));
142 1 SvPOK_on(out);
143 1 rc = sa_cache_get(c, k, (uint32_t)klen, SvPVX(out),
144 1 (uint32_t)c->pair_max, &vlen);
145 1 ▶ if (rc != SA_C_HIT) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (rc != SA_C_HIT) XSRETURN_EMPTY; 0 1 1
146 1 ▶ if (c->serialise) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->serialise) { 1 0 1
147 1 ST(0) = SA_SER_DECODE(SvPVX(out), vlen);
148 1 XSRETURN(1);
149 }
150 0 SvCUR_set(out, (STRLEN)vlen);
151 0 SvPVX(out)[vlen] = '\0';
152 0 ▶ XPUSHs(out);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(out); 0 0 0
153 }
154  
155 int
156 sac_remove(self, key)
157 SV *self
158 SV *key
159 PREINIT:
160 sa_cache *c;
161 const char *k;
162 STRLEN klen;
163 CODE:
164 2 c = SA_SELF(sa_cache, self);
165 2 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 0 2 2
166 2 k = SvPV(key, klen);
167 2 RETVAL = sa_cache_remove(c, k, (uint32_t)klen);
168 OUTPUT:
169 RETVAL
170  
171 void
172 sac_clear(self)
173 SV *self
174 PREINIT:
175 sa_cache *c;
176 CODE:
177 1 c = SA_SELF(sa_cache, self);
178 1 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 0 1 1
179 1 sa_cache_clear(c);
180  
181 UV
182 sac_capacity(self)
183 SV *self
184 PREINIT:
185 sa_cache *c;
186 CODE:
187 2 c = SA_SELF(sa_cache, self);
188 2 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 0 2 2
189 2 ▶ RETVAL = (UV)(c->nbuckets * c->nways);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)(c->nbuckets * c->nways); 0 2 2
190 OUTPUT:
191 RETVAL
192  
193 UV
194 sac_max_pair(self)
195 SV *self
196 PREINIT:
197 sa_cache *c;
198 CODE:
199 4 c = SA_SELF(sa_cache, self);
200 4 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 0 4 4
201 4 ▶ RETVAL = (UV)c->pair_max;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)c->pair_max; 0 4 4
202 OUTPUT:
203 RETVAL
204  
205 # hits / misses / hit_rate / evictions / expired / live / capacity / ways
206 #
207 # The hit rate is the number a cache exists to produce. `live` walks every
208 # entry, so this belongs on a status page rather than in a request.
209 void
210 sac_stats(self)
211 SV *self
212 PREINIT:
213 sa_cache *c;
214 uint64_t h, m;
215 PPCODE:
216 10 c = SA_SELF(sa_cache, self);
217 10 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 0 10 10
218 10 sa_cache_flush(c); /* this process's own counts, exactly */
219 10 h = sa_at_load64_acq(&c->hdr->hits);
220 10 m = sa_at_load64_acq(&c->hdr->misses);
221 10 ▶ EXTEND(SP, 16);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 16); 0 10 10
222 10 mPUSHp("hits", 4); mPUSHu((UV)h);
223 10 mPUSHp("misses", 6); mPUSHu((UV)m);
224 10 ▶ mPUSHp("hit_rate", 8); mPUSHn((h + m) ? (double)h / (double)(h + m) : 0.0);
Branch: 1 branch
BranchTrueFalseTotal
mPUSHp("hit_rate", 8); mPUSHn((h + m) ? (double)h / (double)(h + m) : 0.0); 3 7 10
225 10 mPUSHp("evictions", 9); mPUSHu((UV)sa_at_load64_acq(&c->hdr->evictions));
226 10 mPUSHp("expired", 7); mPUSHu((UV)sa_at_load64_acq(&c->hdr->expired));
227 10 mPUSHp("live", 4); mPUSHu((UV)sa_cache_live(c));
228 10 mPUSHp("capacity", 8); mPUSHu((UV)(c->nbuckets * c->nways));
229 10 mPUSHp("ways", 4); mPUSHu((UV)c->nways);
230  
231 void
232 sac_DESTROY(self)
233 SV *self
234 PREINIT:
235 sa_cache *c;
236 CODE:
237 18 c = SA_SELF(sa_cache, self);
238 18 ▶ if (c) { sa_cache_free(c); sv_setiv(SvRV(self), 0); }
Branch: 1 branch
BranchTrueFalseTotal
if (c) { sa_cache_free(c); sv_setiv(SvRV(self), 0); } 16 2 18
239  
240 # Whether this cache was made with serialise => 1.
241 int
242 sac_serialised(self)
243 SV *self
244 PREINIT:
245 sa_cache *c;
246 CODE:
247 2 c = SA_SELF(sa_cache, self);
248 2 ▶ if (!c) croak("Shared::Arena::Cache: this cache is released");
Branch: 1 branch
BranchTrueFalseTotal
if (!c) croak("Shared::Arena::Cache: this cache is released"); 0 2 2
249 2 ▶ RETVAL = c->serialise ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = c->serialise ? 1 : 0; 0 2 2
250 OUTPUT:
251 RETVAL
252  
253 MODULE = Shared::Arena PACKAGE = Shared::Arena
254  
255 BOOT:
256 {
257 /* Struct::Codec's table, on the same terms as Frozen's in xs/frozen.xs:
258 * fetched once, `>=` against what this file was compiled with, and a
259 * refusal here rather than a null entry at the first serialised set. The
260 * VALUE call_pv returned, read back after SPAGAIN, and SvUV rather than
261 * SvIV: an address is unsigned. */
262 SV *scp;
263 134 dSP;
264 134 ENTER;
265 134 SAVETMPS;
266 134 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 134 134
267 134 PUTBACK;
268 134 call_pv("Struct::Codec::_abi_ptr", G_SCALAR);
269 134 SPAGAIN;
270 134 scp = POPs;
271 134 SA_SC = INT2PTR(const sc_abi *, SvUV(scp));
272 134 PUTBACK;
273 134 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 134 0 134
274 134 LEAVE;
275  
276 134 ▶ if (!SA_SC)
Branch: 1 branch
BranchTrueFalseTotal
if (!SA_SC) 0 134 134
277 0 croak("Shared::Arena: Struct::Codec gave no ABI table");
278 134 ▶ if (SA_SC->abi_version < SC_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if (SA_SC->abi_version < SC_ABI_VERSION) 0 134 134
279 0 croak("Shared::Arena needs Struct::Codec ABI %d or newer, and the "
280 "Struct::Codec that is installed publishes %d. Upgrade "
281 "Struct::Codec.",
282 (int)SC_ABI_VERSION, (int)SA_SC->abi_version);
283 }
« include/sa/sa_arena.h ↑ Summary xs/group.xs »