| 1 |
|
| # Shared::Arena::Map - a fixed-capacity map several processes share. |
| 2 |
|
| |
| 3 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_ |
| 4 |
|
| |
| 5 |
|
| # $arena->map($name, slots => N, slot_size => N, serialise => 0) |
| 6 |
|
| # -> Shared::Arena::Map |
| 7 |
|
| # |
| 8 |
|
| # Carves the region if it is not there, binds to it if it is. Every process may |
| 9 |
|
| # call this with the same arguments; exactly one of them does the work. |
| 10 |
|
| # |
| 11 |
|
| # `serialise` is part of the map's SHAPE, kept in the shared header: every |
| 12 |
|
| # process binding it must ask for the same thing, and one that does not is |
| 13 |
|
| # refused as a different slot_size would be. |
| 14 |
|
| SV * |
| 15 |
|
| sar_map(self, name, ...) |
| 16 |
|
| SV *self |
| 17 |
|
| SV *name |
| 18 |
|
| PREINIT: |
| 19 |
|
| sa_region *arena; |
| 20 |
|
| sa_reg *e; |
| 21 |
|
| sa_hash *m; |
| 22 |
|
| const char *nm; |
| 23 |
|
| STRLEN nlen; |
| 24 |
309 |
| UV slots = 1024, slot_size = 256; |
| 25 |
309 |
| int err = SA_E_OK, serialise = 0; |
| 26 |
|
| I32 i; |
| 27 |
|
| SV *obj; |
| 28 |
|
| CODE: |
| 29 |
309 |
| arena = SA_SELF(sa_region, self); |
| 30 |
309 |
▶ | if (!arena || !arena->map.base) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!arena || !arena->map.base) |
309 |
0 |
309 |
| if (!arena || !arena->map.base) |
0 |
309 |
309 |
|
| 31 |
0 |
| croak("Shared::Arena: this region is released"); |
| 32 |
309 |
| nm = SvPV(name, nlen); |
| 33 |
930 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
621 |
309 |
930 |
|
| 34 |
621 |
| const char *o = SvPV_nolen(ST(i)); |
| 35 |
621 |
▶ | if (strEQ(o, "slots")) slots = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "slots")) slots = SvUV(ST(i + 1)); |
309 |
312 |
621 |
|
| 36 |
312 |
▶ | else if (strEQ(o, "slot_size")) slot_size = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "slot_size")) slot_size = SvUV(ST(i + 1)); |
309 |
3 |
312 |
|
| 37 |
3 |
▶ | else if (strEQ(o, "serialise")) serialise = SvTRUE(ST(i + 1)) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "serialise")) serialise = SvTRUE(ST(i + 1)) ? 1 : 0; |
3 |
0 |
3 |
|
| 38 |
|
| } |
| 39 |
309 |
▶ | if (slots < 2) slots = 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (slots < 2) slots = 2; |
0 |
309 |
309 |
|
| 40 |
309 |
▶ | if (sa_hash_capacity((uint32_t)slot_size) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_hash_capacity((uint32_t)slot_size) == 0) |
0 |
309 |
309 |
|
| 41 |
0 |
| croak("Shared::Arena: slot_size %lu is smaller than a slot header", |
| 42 |
|
| (unsigned long)slot_size); |
| 43 |
|
| |
| 44 |
309 |
| e = sa_carve(arena, nm, (size_t)nlen, |
| 45 |
|
| sa_hash_bytes((uint64_t)slots, (uint32_t)slot_size), |
| 46 |
|
| SA_T_MAP, &err); |
| 47 |
309 |
▶ | if (!e) croak("Shared::Arena: the map '%s' %s", nm, sa_strerror(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("Shared::Arena: the map '%s' %s", nm, sa_strerror(err)); |
0 |
309 |
309 |
|
| 48 |
|
| |
| 49 |
309 |
| m = sa_hash_bind(arena, e, (uint64_t)slots, (uint32_t)slot_size, |
| 50 |
|
| serialise, &err); |
| 51 |
309 |
▶ | if (!m) croak("Shared::Arena: the map '%s' %s", nm, sa_strerror(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena: the map '%s' %s", nm, sa_strerror(err)); |
1 |
308 |
309 |
|
| 52 |
|
| |
| 53 |
308 |
| obj = newSV(0); |
| 54 |
308 |
| sv_setref_pv(obj, "Shared::Arena::Map", (void *)m); |
| 55 |
|
| /* The map borrows the arena's mapping, so the arena must outlive it. */ |
| 56 |
308 |
| sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0); |
| 57 |
308 |
| SvREFCNT_inc(SvRV(self)); |
| 58 |
308 |
| RETVAL = obj; |
| 59 |
|
| OUTPUT: |
| 60 |
|
| RETVAL |
| 61 |
|
| |
| 62 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena::Map PREFIX = sam_ |
| 63 |
|
| |
| 64 |
|
| # $map->store($key, $value) |
| 65 |
|
| # $map->store($key, $value, ttl => 300) # seconds; the entry expires after |
| 66 |
|
| # $map->store($key, $value, ttl_ms => 5000) # milliseconds |
| 67 |
|
| # |
| 68 |
|
| # 1 stored / 0 the table is full / -1 the pair does not fit a slot. |
| 69 |
|
| # |
| 70 |
|
| # On a serialised map the value is encoded first, and one whose encoding does |
| 71 |
|
| # not fit is the same -1. A value that cannot be encoded croaks and stores |
| 72 |
|
| # nothing. |
| 73 |
|
| IV |
| 74 |
|
| sam_store(self, key, value, ...) |
| 75 |
|
| SV *self |
| 76 |
|
| SV *key |
| 77 |
|
| SV *value |
| 78 |
|
| PREINIT: |
| 79 |
|
| sa_hash *m; |
| 80 |
|
| const char *k, *v; |
| 81 |
|
| STRLEN klen, vlen; |
| 82 |
634 |
▶ | uint64_t ttl_ms = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| uint64_t ttl_ms = 0; |
634 |
0 |
634 |
|
| 83 |
|
| I32 i; |
| 84 |
|
| char sbuf[SA_SER_STACK]; |
| 85 |
|
| CODE: |
| 86 |
634 |
| m = SA_SELF(sa_hash, self); |
| 87 |
634 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
634 |
634 |
|
| 88 |
634 |
| k = SvPV(key, klen); |
| 89 |
634 |
▶ | if (m->serialise) |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->serialise) |
1 |
633 |
634 |
|
| 90 |
1 |
| vlen = sa_ser_encode(aTHX_ value, m->pair_max, klen, sbuf, &v); |
| 91 |
|
| else |
| 92 |
633 |
| v = SvPV(value, vlen); |
| 93 |
1266 |
▶ | for (i = 3; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 3; i + 1 < items; i += 2) { |
632 |
634 |
1266 |
|
| 94 |
632 |
| const char *o = SvPV_nolen(ST(i)); |
| 95 |
632 |
▶ | if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0); |
42 |
590 |
632 |
|
| 96 |
590 |
▶ | else if (strEQ(o, "ttl_ms")) ttl_ms = (uint64_t)SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "ttl_ms")) ttl_ms = (uint64_t)SvUV(ST(i + 1)); |
590 |
0 |
590 |
|
| 97 |
|
| } |
| 98 |
635 |
▶ | RETVAL = (m->serialise && !vlen) |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (m->serialise && !vlen) |
1 |
0 |
1 |
| RETVAL = (m->serialise && !vlen) |
378 |
256 |
634 |
|
| 99 |
|
| ? SA_H_TOOBIG |
| 100 |
635 |
▶ | : sa_hash_store_ttl(m, k, (uint32_t)klen, v, (uint32_t)vlen, |
Branch: 1 branch
| Branch | True | False | Total |
| : sa_hash_store_ttl(m, k, (uint32_t)klen, v, (uint32_t)vlen, |
1 |
633 |
634 |
|
| 101 |
|
| ttl_ms); |
| 102 |
|
| OUTPUT: |
| 103 |
|
| RETVAL |
| 104 |
|
| |
| 105 |
|
| # The value, or an EMPTY LIST when the key is not there. |
| 106 |
|
| # |
| 107 |
|
| # Not undef for absent: undef is a value a caller may legitimately store, and a |
| 108 |
|
| # door that used it to mean absent could not tell the two apart. |
| 109 |
|
| # |
| 110 |
|
| # A key that is there but could not be read - because a writer held it through |
| 111 |
|
| # more retries than a reader was willing to spend - is also an empty list, and |
| 112 |
|
| # the map's `busy` count is what says so. See the POD: a non-zero busy means a |
| 113 |
|
| # fetch gave up, not that a key was missing. |
| 114 |
|
| void |
| 115 |
|
| sam_fetch(self, key) |
| 116 |
|
| SV *self |
| 117 |
|
| SV *key |
| 118 |
|
| PREINIT: |
| 119 |
|
| sa_hash *m; |
| 120 |
|
| const char *k; |
| 121 |
|
| STRLEN klen; |
| 122 |
|
| SV *out; |
| 123 |
|
| char buf[1024]; |
| 124 |
3 |
| uint32_t vlen = 0; |
| 125 |
|
| int rc; |
| 126 |
|
| PPCODE: |
| 127 |
3 |
| m = SA_SELF(sa_hash, self); |
| 128 |
3 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
3 |
3 |
|
| 129 |
3 |
| k = SvPV(key, klen); |
| 130 |
|
| /* Onto the stack first where a slot fits, so a miss allocates nothing |
| 131 |
|
| * and a hit allocates what the value needs rather than the most a slot |
| 132 |
|
| * could hold. */ |
| 133 |
|
| /* On a serialised map the copy is decoded from wherever it landed, |
| 134 |
|
| * and the answer goes through ST(0), which is reached from `ax` and |
| 135 |
|
| * survives a decode that grew the value stack. */ |
| 136 |
3 |
▶ | if (m->pair_max < sizeof buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->pair_max < sizeof buf) { |
3 |
0 |
3 |
|
| 137 |
3 |
| rc = sa_hash_fetch(m, k, (uint32_t)klen, buf, |
| 138 |
3 |
| (uint32_t)m->pair_max, &vlen); |
| 139 |
3 |
▶ | if (rc != SA_H_HIT) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != SA_H_HIT) XSRETURN_EMPTY; |
0 |
3 |
3 |
|
| 140 |
3 |
▶ | if (m->serialise) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->serialise) { |
2 |
1 |
3 |
|
| 141 |
2 |
| ST(0) = SA_SER_DECODE(buf, vlen); |
| 142 |
2 |
| XSRETURN(1); |
| 143 |
|
| } |
| 144 |
1 |
▶ | XPUSHs(sv_2mortal(newSVpvn(buf, (STRLEN)vlen))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(buf, (STRLEN)vlen))); |
0 |
1 |
1 |
|
| 145 |
|
| } |
| 146 |
|
| else { |
| 147 |
0 |
| out = sv_2mortal(newSV((STRLEN)m->pair_max + 1)); |
| 148 |
0 |
| SvPOK_on(out); |
| 149 |
0 |
| rc = sa_hash_fetch(m, k, (uint32_t)klen, SvPVX(out), |
| 150 |
0 |
| (uint32_t)m->pair_max, &vlen); |
| 151 |
0 |
▶ | if (rc != SA_H_HIT) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != SA_H_HIT) XSRETURN_EMPTY; |
0 |
0 |
0 |
|
| 152 |
0 |
▶ | if (m->serialise) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->serialise) { |
0 |
0 |
0 |
|
| 153 |
0 |
| ST(0) = SA_SER_DECODE(SvPVX(out), vlen); |
| 154 |
0 |
| XSRETURN(1); |
| 155 |
|
| } |
| 156 |
0 |
| SvCUR_set(out, (STRLEN)vlen); |
| 157 |
0 |
| SvPVX(out)[vlen] = '\0'; |
| 158 |
0 |
▶ | XPUSHs(out); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(out); |
0 |
0 |
0 |
|
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| int |
| 162 |
|
| sam_exists(self, key) |
| 163 |
|
| SV *self |
| 164 |
|
| SV *key |
| 165 |
|
| PREINIT: |
| 166 |
|
| sa_hash *m; |
| 167 |
|
| const char *k; |
| 168 |
|
| STRLEN klen; |
| 169 |
|
| char scratch[8]; |
| 170 |
2 |
▶ | uint32_t vlen = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t vlen = 0; |
2 |
0 |
2 |
|
| 171 |
|
| CODE: |
| 172 |
2 |
| m = SA_SELF(sa_hash, self); |
| 173 |
2 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
2 |
2 |
|
| 174 |
2 |
| k = SvPV(key, klen); |
| 175 |
|
| /* A zero-length buffer: a hit with a value too long to copy still |
| 176 |
|
| * answers the question this asks, which is only whether it is there. */ |
| 177 |
2 |
| RETVAL = (sa_hash_fetch(m, k, (uint32_t)klen, scratch, 0, &vlen) |
| 178 |
2 |
▶ | != SA_H_MISS) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| != SA_H_MISS) ? 1 : 0; |
0 |
2 |
2 |
|
| 179 |
|
| OUTPUT: |
| 180 |
|
| RETVAL |
| 181 |
|
| |
| 182 |
|
| # 1 if the key was there and is now gone, 0 if it was not there, and UNDEF if |
| 183 |
|
| # another process held the stripe through the whole wait - which is "try |
| 184 |
|
| # again", not "it was not there". A caller that deletes in order to rely on the |
| 185 |
|
| # key being gone has to tell those apart; one that deletes speculatively can go |
| 186 |
|
| # on ignoring the answer, since 0 and undef are both false. |
| 187 |
|
| SV * |
| 188 |
|
| sam_delete(self, key) |
| 189 |
|
| SV *self |
| 190 |
|
| SV *key |
| 191 |
|
| PREINIT: |
| 192 |
|
| sa_hash *m; |
| 193 |
|
| const char *k; |
| 194 |
|
| STRLEN klen; |
| 195 |
|
| int rc; |
| 196 |
|
| CODE: |
| 197 |
4 |
| m = SA_SELF(sa_hash, self); |
| 198 |
4 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
4 |
4 |
|
| 199 |
4 |
| k = SvPV(key, klen); |
| 200 |
4 |
| rc = sa_hash_delete(m, k, (uint32_t)klen); |
| 201 |
4 |
▶ | if (rc == SA_H_WBUSY) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == SA_H_WBUSY) XSRETURN_UNDEF; |
0 |
4 |
4 |
|
| 202 |
4 |
| RETVAL = newSViv((IV)rc); |
| 203 |
|
| OUTPUT: |
| 204 |
|
| RETVAL |
| 205 |
|
| |
| 206 |
|
| # $map->incr($key) |
| 207 |
|
| # $map->incr($key, $by) |
| 208 |
|
| # $map->incr($key, $by, ttl => 60) # a deadline when the counter is |
| 209 |
|
| # $map->incr($key, $by, ttl_ms => 60000) # CREATED or reset; never renewed |
| 210 |
|
| # |
| 211 |
|
| # Add to a counter, creating it at $by when it is absent. Returns the new |
| 212 |
|
| # value, or undef when the table is full or the entry is not a counter. |
| 213 |
|
| # |
| 214 |
|
| # A counter is a raw word and cannot also be an encoded value, so on a |
| 215 |
|
| # serialised map this croaks rather than planting bytes a fetch would then |
| 216 |
|
| # refuse. |
| 217 |
|
| SV * |
| 218 |
|
| sam_incr(self, key, ...) |
| 219 |
|
| SV *self |
| 220 |
|
| SV *key |
| 221 |
|
| PREINIT: |
| 222 |
|
| sa_hash *m; |
| 223 |
|
| const char *k; |
| 224 |
|
| STRLEN klen; |
| 225 |
956 |
| IV by = 1; |
| 226 |
956 |
| uint64_t now = 0, ttl_ms = 0; |
| 227 |
|
| int rc; |
| 228 |
|
| I32 i; |
| 229 |
|
| CODE: |
| 230 |
956 |
| m = SA_SELF(sa_hash, self); |
| 231 |
956 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
956 |
956 |
|
| 232 |
956 |
▶ | if (m->serialise) croak(SA_SER_INCR_MSG); |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->serialise) croak(SA_SER_INCR_MSG); |
1 |
955 |
956 |
|
| 233 |
955 |
| k = SvPV(key, klen); |
| 234 |
955 |
▶ | if (items > 2) by = SvIV(ST(2)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) by = SvIV(ST(2)); |
952 |
3 |
955 |
|
| 235 |
1907 |
▶ | for (i = 3; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 3; i + 1 < items; i += 2) { |
952 |
955 |
1907 |
|
| 236 |
952 |
| const char *o = SvPV_nolen(ST(i)); |
| 237 |
952 |
▶ | if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "ttl")) ttl_ms = (uint64_t)(SvNV(ST(i + 1)) * 1000.0); |
0 |
952 |
952 |
|
| 238 |
952 |
▶ | else if (strEQ(o, "ttl_ms")) ttl_ms = (uint64_t)SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "ttl_ms")) ttl_ms = (uint64_t)SvUV(ST(i + 1)); |
952 |
0 |
952 |
|
| 239 |
|
| } |
| 240 |
955 |
| rc = sa_hash_incr_ttl(m, k, (uint32_t)klen, (int64_t)by, ttl_ms, &now); |
| 241 |
955 |
▶ | if (rc != SA_H_OK) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc != SA_H_OK) XSRETURN_UNDEF; |
0 |
955 |
955 |
|
| 242 |
955 |
| RETVAL = newSVuv((UV)now); |
| 243 |
|
| OUTPUT: |
| 244 |
|
| RETVAL |
| 245 |
|
| |
| 246 |
|
| # A counter's value without changing it, or undef when it is not a counter. |
| 247 |
|
| # Nothing in a serialised map is one, whatever its length. |
| 248 |
|
| SV * |
| 249 |
|
| sam_counter(self, key) |
| 250 |
|
| SV *self |
| 251 |
|
| SV *key |
| 252 |
|
| PREINIT: |
| 253 |
|
| sa_hash *m; |
| 254 |
|
| const char *k; |
| 255 |
|
| STRLEN klen; |
| 256 |
|
| char buf[8]; |
| 257 |
2 |
| uint32_t vlen = 0; |
| 258 |
|
| uint64_t v; |
| 259 |
|
| CODE: |
| 260 |
2 |
| m = SA_SELF(sa_hash, self); |
| 261 |
2 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
2 |
2 |
|
| 262 |
2 |
▶ | if (m->serialise) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (m->serialise) XSRETURN_UNDEF; |
1 |
1 |
2 |
|
| 263 |
1 |
| k = SvPV(key, klen); |
| 264 |
1 |
▶ | if (sa_hash_fetch(m, k, (uint32_t)klen, buf, 8, &vlen) != SA_H_HIT |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_hash_fetch(m, k, (uint32_t)klen, buf, 8, &vlen) != SA_H_HIT |
1 |
0 |
1 |
|
| 265 |
1 |
▶ | || vlen != 8) |
Branch: 1 branch
| Branch | True | False | Total |
| || vlen != 8) |
0 |
1 |
1 |
|
| 266 |
0 |
| XSRETURN_UNDEF; |
| 267 |
1 |
| memcpy(&v, buf, 8); |
| 268 |
1 |
| RETVAL = newSVuv((UV)v); |
| 269 |
|
| OUTPUT: |
| 270 |
|
| RETVAL |
| 271 |
|
| |
| 272 |
|
| UV |
| 273 |
|
| sam_max_pair(self) |
| 274 |
|
| SV *self |
| 275 |
|
| PREINIT: |
| 276 |
|
| sa_hash *m; |
| 277 |
|
| CODE: |
| 278 |
2 |
| m = SA_SELF(sa_hash, self); |
| 279 |
2 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
2 |
2 |
|
| 280 |
2 |
▶ | RETVAL = (UV)m->pair_max; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)m->pair_max; |
0 |
2 |
2 |
|
| 281 |
|
| OUTPUT: |
| 282 |
|
| RETVAL |
| 283 |
|
| |
| 284 |
|
| UV |
| 285 |
|
| sam_capacity(self) |
| 286 |
|
| SV *self |
| 287 |
|
| PREINIT: |
| 288 |
|
| sa_hash *m; |
| 289 |
|
| CODE: |
| 290 |
1 |
| m = SA_SELF(sa_hash, self); |
| 291 |
1 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
1 |
1 |
|
| 292 |
1 |
▶ | RETVAL = (UV)m->nslots; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)m->nslots; |
0 |
1 |
1 |
|
| 293 |
|
| OUTPUT: |
| 294 |
|
| RETVAL |
| 295 |
|
| |
| 296 |
|
| # The keys, in the table's own order, which is neither insertion nor sorted. |
| 297 |
|
| # |
| 298 |
|
| # A SNAPSHOT AND NOT A LOCK: entries may be added or removed while this walks, |
| 299 |
|
| # so a key it returns may be gone by the time the caller looks at it, and one |
| 300 |
|
| # added behind the walk will not appear. It is for an operator looking at a |
| 301 |
|
| # table, not for iterating one that is being written. |
| 302 |
|
| void |
| 303 |
|
| sam_keys(self) |
| 304 |
|
| SV *self |
| 305 |
|
| PREINIT: |
| 306 |
|
| sa_hash *m; |
| 307 |
|
| uint64_t i; |
| 308 |
|
| PPCODE: |
| 309 |
3 |
| m = SA_SELF(sa_hash, self); |
| 310 |
3 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
3 |
3 |
|
| 311 |
131 |
▶ | for (i = 0; i < m->nslots; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m->nslots; i++) { |
128 |
3 |
131 |
|
| 312 |
128 |
| sa_hash_slot *s = SA_HSLOT_AT(m, i); |
| 313 |
|
| uint32_t v1, kl; |
| 314 |
|
| uint64_t exp; |
| 315 |
128 |
▶ | if (sa_at_load32_acq(&s->state) != SA_H_LIVE) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&s->state) != SA_H_LIVE) continue; |
116 |
12 |
128 |
|
| 316 |
12 |
| v1 = sa_at_load32_acq(&s->version); |
| 317 |
12 |
▶ | if (v1 & 1u) continue; /* mid-update: skip it */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (v1 & 1u) continue; /* mid-update: skip it */ |
0 |
12 |
12 |
|
| 318 |
|
| /* An expired entry is not a live key. It is left for a fetch to |
| 319 |
|
| * collect rather than tombstoned here: keys() is a read-only |
| 320 |
|
| * snapshot and takes no lock. */ |
| 321 |
12 |
| exp = sa_at_load64_acq(&s->expires); |
| 322 |
12 |
▶ | if (exp && sa_now_ms() >= exp) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (exp && sa_now_ms() >= exp) continue; |
0 |
12 |
12 |
| if (exp && sa_now_ms() >= exp) continue; |
0 |
0 |
0 |
|
| 323 |
12 |
| kl = sa_at_load32_acq(&s->klen); |
| 324 |
12 |
▶ | if (!kl || (uint64_t)kl > m->pair_max) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!kl || (uint64_t)kl > m->pair_max) continue; |
12 |
0 |
12 |
| if (!kl || (uint64_t)kl > m->pair_max) continue; |
0 |
12 |
12 |
|
| 325 |
|
| { |
| 326 |
12 |
| SV *k = newSVpvn(s->bytes, kl); |
| 327 |
12 |
| sa_at_fence_acq(); |
| 328 |
12 |
▶ | if (sa_at_load32_acq(&s->version) != v1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(&s->version) != v1) { |
0 |
12 |
12 |
|
| 329 |
0 |
| SvREFCNT_dec(k); |
| 330 |
0 |
| continue; |
| 331 |
|
| } |
| 332 |
12 |
▶ | mXPUSHs(k); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(k); |
0 |
12 |
12 |
|
| 333 |
|
| } |
| 334 |
|
| } |
| 335 |
|
| |
| 336 |
|
| # used / capacity / tombstones / busy / full / expired |
| 337 |
|
| void |
| 338 |
|
| sam_stats(self) |
| 339 |
|
| SV *self |
| 340 |
|
| PREINIT: |
| 341 |
|
| sa_hash *m; |
| 342 |
|
| PPCODE: |
| 343 |
92 |
| m = SA_SELF(sa_hash, self); |
| 344 |
92 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
92 |
92 |
|
| 345 |
92 |
▶ | EXTEND(SP, 12); /* six key/value pairs */ |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 12); /* six key/value pairs */ |
0 |
92 |
92 |
|
| 346 |
92 |
| mPUSHp("used", 4); mPUSHu((UV)sa_at_load64_acq(&m->hdr->used)); |
| 347 |
92 |
| mPUSHp("capacity", 8); mPUSHu((UV)m->nslots); |
| 348 |
92 |
| mPUSHp("tombstones", 10); |
| 349 |
92 |
| mPUSHu((UV)sa_at_load64_acq(&m->hdr->tombstones)); |
| 350 |
92 |
| mPUSHp("busy", 4); mPUSHu((UV)sa_at_load64_acq(&m->hdr->busy)); |
| 351 |
92 |
| mPUSHp("full", 4); mPUSHu((UV)sa_at_load64_acq(&m->hdr->full)); |
| 352 |
92 |
| mPUSHp("expired", 7); |
| 353 |
92 |
| mPUSHu((UV)sa_at_load64_acq(&m->hdr->expired)); |
| 354 |
|
| |
| 355 |
|
| void |
| 356 |
|
| sam_DESTROY(self) |
| 357 |
|
| SV *self |
| 358 |
|
| PREINIT: |
| 359 |
|
| sa_hash *m; |
| 360 |
|
| CODE: |
| 361 |
308 |
| m = SA_SELF(sa_hash, self); |
| 362 |
308 |
▶ | if (m) { sa_hash_free(m); sv_setiv(SvRV(self), 0); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (m) { sa_hash_free(m); sv_setiv(SvRV(self), 0); } |
308 |
0 |
308 |
|
| 363 |
|
| |
| 364 |
|
| # Whether this map was made with serialise => 1. |
| 365 |
|
| int |
| 366 |
|
| sam_serialised(self) |
| 367 |
|
| SV *self |
| 368 |
|
| PREINIT: |
| 369 |
|
| sa_hash *m; |
| 370 |
|
| CODE: |
| 371 |
1 |
| m = SA_SELF(sa_hash, self); |
| 372 |
1 |
▶ | if (!m) croak("Shared::Arena::Map: this map is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) croak("Shared::Arena::Map: this map is released"); |
0 |
1 |
1 |
|
| 373 |
1 |
▶ | RETVAL = m->serialise ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = m->serialise ? 1 : 0; |
0 |
1 |
1 |
|
| 374 |
|
| OUTPUT: |
| 375 |
|
| RETVAL |