| 1 |
|
| # The region's Perl surface. |
| 2 |
|
| # |
| 3 |
|
| # The object is a blessed SV carrying the sa_region pointer, and every entry |
| 4 |
|
| # takes it explicitly. There is no file-scope state anywhere in this dist, which |
| 5 |
|
| # is the property that stops a second consumer quietly getting a second region |
| 6 |
|
| # instead of sharing the first. |
| 7 |
|
| |
| 8 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena |
| 9 |
|
| |
| 10 |
|
| # Shared::Arena->create(size => N, name => 'x', regions => N) |
| 11 |
|
| # |
| 12 |
|
| # A name makes it a named segment another process can attach; without one it is |
| 13 |
|
| # anonymous and reaches only fork children. Creating a name that already exists |
| 14 |
|
| # ATTACHES to it, which is what lets every worker run the same setup code. |
| 15 |
|
| SV * |
| 16 |
|
| create(class, ...) |
| 17 |
|
| SV *class |
| 18 |
|
| PREINIT: |
| 19 |
|
| sa_region *r; |
| 20 |
153 |
| const char *name = NULL; |
| 21 |
153 |
| STRLEN nlen = 0; |
| 22 |
153 |
| UV size = 1024 * 1024; |
| 23 |
153 |
| UV regions = SA_REGIONS_MAX; |
| 24 |
153 |
| int err = SA_E_OK; |
| 25 |
|
| I32 i; |
| 26 |
|
| SV *obj; |
| 27 |
|
| CODE: |
| 28 |
330 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
177 |
153 |
330 |
|
| 29 |
177 |
| const char *o = SvPV_nolen(ST(i)); |
| 30 |
177 |
▶ | if (strEQ(o, "size")) size = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "size")) size = SvUV(ST(i + 1)); |
153 |
24 |
177 |
|
| 31 |
24 |
▶ | else if (strEQ(o, "regions")) regions = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "regions")) regions = SvUV(ST(i + 1)); |
19 |
5 |
24 |
|
| 32 |
5 |
▶ | else if (strEQ(o, "name") && SvOK(ST(i + 1))) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (strEQ(o, "name") && SvOK(ST(i + 1))) |
5 |
0 |
5 |
| else if (strEQ(o, "name") && SvOK(ST(i + 1))) |
5 |
0 |
5 |
|
| 33 |
10 |
| name = SvPV(ST(i + 1), nlen); |
| 34 |
|
| } |
| 35 |
153 |
▶ | if (regions < 1) regions = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (regions < 1) regions = 1; |
0 |
153 |
153 |
|
| 36 |
153 |
▶ | if (regions > 4096) regions = 4096; |
Branch: 1 branch
| Branch | True | False | Total |
| if (regions > 4096) regions = 4096; |
0 |
153 |
153 |
|
| 37 |
|
| /* The header and its registry come out of the same mapping, so a caller |
| 38 |
|
| * asking for a megabyte gets a megabyte of usable region plus what the |
| 39 |
|
| * bookkeeping costs, rather than silently less than it asked for. |
| 40 |
|
| * |
| 41 |
|
| * CHECKED, because this is unsigned arithmetic on a number that came |
| 42 |
|
| * from Perl: `size => ~0` wrapped round to a few hundred bytes and |
| 43 |
|
| * created a region far SMALLER than the one that was refused, with a |
| 44 |
|
| * header that agreed with itself and a caller that had asked for |
| 45 |
|
| * everything. Refusing is the only honest answer to a size that cannot |
| 46 |
|
| * be represented. */ |
| 47 |
|
| { |
| 48 |
153 |
| UV bookkeeping = (UV)sa_header_bytes((uint32_t)regions); |
| 49 |
153 |
▶ | if (size > (UV)-1 - bookkeeping) |
Branch: 1 branch
| Branch | True | False | Total |
| if (size > (UV)-1 - bookkeeping) |
0 |
153 |
153 |
|
| 50 |
0 |
| croak("Shared::Arena: a region of %" UVuf " bytes plus its " |
| 51 |
|
| "header does not fit an unsigned integer", size); |
| 52 |
153 |
| size += bookkeeping; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
153 |
| r = sa_region_open(name, nlen, (uint64_t)size, (uint32_t)regions, |
| 56 |
|
| 1, &err); |
| 57 |
153 |
▶ | if (!r) croak("Shared::Arena: the region %s", sa_strerror(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) croak("Shared::Arena: the region %s", sa_strerror(err)); |
0 |
153 |
153 |
|
| 58 |
|
| |
| 59 |
153 |
| obj = newSV(0); |
| 60 |
153 |
| sv_setref_pv(obj, SvPV_nolen(class), (void *)r); |
| 61 |
153 |
| RETVAL = obj; |
| 62 |
|
| OUTPUT: |
| 63 |
|
| RETVAL |
| 64 |
|
| |
| 65 |
|
| # Shared::Arena->attach($name) - a process that is not a fork child. |
| 66 |
|
| # |
| 67 |
|
| # Named `attach` rather than `open` on purpose: a member or method called `open` |
| 68 |
|
| # is a function-like macro on every Strawberry perl, and this dist avoids those |
| 69 |
|
| # names rather than working around them at each call site. |
| 70 |
|
| SV * |
| 71 |
|
| attach(class, name) |
| 72 |
|
| SV *class |
| 73 |
|
| SV *name |
| 74 |
|
| PREINIT: |
| 75 |
|
| sa_region *r; |
| 76 |
|
| const char *nm; |
| 77 |
|
| STRLEN nlen; |
| 78 |
6 |
| int err = SA_E_OK; |
| 79 |
|
| SV *obj; |
| 80 |
|
| CODE: |
| 81 |
6 |
| nm = SvPV(name, nlen); |
| 82 |
|
| /* Length 0 here means "attach whatever is there", and the header the |
| 83 |
|
| * creator published says how big it really is. */ |
| 84 |
6 |
| r = sa_region_open(nm, nlen, (uint64_t)sa_header_bytes(SA_REGIONS_MAX), |
| 85 |
|
| SA_REGIONS_MAX, 0, &err); |
| 86 |
6 |
▶ | if (!r) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) XSRETURN_UNDEF; |
1 |
5 |
6 |
|
| 87 |
5 |
| obj = newSV(0); |
| 88 |
5 |
| sv_setref_pv(obj, SvPV_nolen(class), (void *)r); |
| 89 |
5 |
| RETVAL = obj; |
| 90 |
|
| OUTPUT: |
| 91 |
|
| RETVAL |
| 92 |
|
| |
| 93 |
|
| int |
| 94 |
|
| destroy(class, name) |
| 95 |
|
| SV *class |
| 96 |
|
| SV *name |
| 97 |
|
| PREINIT: |
| 98 |
|
| const char *nm; |
| 99 |
|
| STRLEN nlen; |
| 100 |
|
| CODE: |
| 101 |
|
| PERL_UNUSED_VAR(class); |
| 102 |
8 |
| nm = SvPV(name, nlen); |
| 103 |
8 |
▶ | RETVAL = (sa_region_destroy(nm, nlen) == SA_E_OK) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (sa_region_destroy(nm, nlen) == SA_E_OK) ? 1 : 0; |
0 |
8 |
8 |
|
| 104 |
|
| OUTPUT: |
| 105 |
|
| RETVAL |
| 106 |
|
| |
| 107 |
|
| int |
| 108 |
|
| have_atomics(...) |
| 109 |
|
| CODE: |
| 110 |
131 |
▶ | RETVAL = SA_HAVE_ATOMICS ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = SA_HAVE_ATOMICS ? 1 : 0; |
0 |
131 |
131 |
|
| 111 |
|
| OUTPUT: |
| 112 |
|
| RETVAL |
| 113 |
|
| |
| 114 |
|
| # A new thread gets no copy of anything here. |
| 115 |
|
| # |
| 116 |
|
| # Every object in this dist is a C pointer in a blessed scalar. Cloning an |
| 117 |
|
| # interpreter clones the scalar and shares the pointer, and whichever copy is |
| 118 |
|
| # destroyed first frees it under the other - or, for the region, unmaps the |
| 119 |
|
| # arena under the thread that made it. This makes the new thread's copies |
| 120 |
|
| # inert, and a thread that wants the arena attaches to it by name, as another |
| 121 |
|
| # process would. |
| 122 |
|
| int |
| 123 |
|
| CLONE_SKIP(...) |
| 124 |
|
| ALIAS: |
| 125 |
|
| Shared::Arena::Ring::CLONE_SKIP = 1 |
| 126 |
|
| Shared::Arena::Ring::Cursor::CLONE_SKIP = 2 |
| 127 |
|
| Shared::Arena::Ring::Group::CLONE_SKIP = 3 |
| 128 |
|
| Shared::Arena::Map::CLONE_SKIP = 4 |
| 129 |
|
| Shared::Arena::Bloom::CLONE_SKIP = 5 |
| 130 |
|
| Shared::Arena::Histogram::CLONE_SKIP = 6 |
| 131 |
|
| Shared::Arena::Cache::CLONE_SKIP = 7 |
| 132 |
|
| Shared::Arena::Rate::CLONE_SKIP = 8 |
| 133 |
|
| Shared::Arena::CountMin::CLONE_SKIP = 9 |
| 134 |
|
| Shared::Arena::Frozen::CLONE_SKIP = 10 |
| 135 |
|
| Shared::Arena::Frozen::View::CLONE_SKIP = 11 |
| 136 |
|
| Shared::Arena::Cuckoo::CLONE_SKIP = 12 |
| 137 |
|
| Shared::Arena::Lease::CLONE_SKIP = 13 |
| 138 |
|
| Shared::Arena::Scoreboard::CLONE_SKIP = 14 |
| 139 |
|
| Shared::Arena::HyperLogLog::CLONE_SKIP = 15 |
| 140 |
|
| CODE: |
| 141 |
|
| PERL_UNUSED_VAR(ix); |
| 142 |
0 |
▶ | RETVAL = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1; |
0 |
0 |
0 |
|
| 143 |
|
| OUTPUT: |
| 144 |
|
| RETVAL |
| 145 |
|
| |
| 146 |
|
| # The compiled layout, so t/01-format.t can assert from Perl what the C thinks |
| 147 |
|
| # it is. These numbers are in the region's own header at create, and a process |
| 148 |
|
| # whose build disagrees fails open rather than reading a shape it does not |
| 149 |
|
| # share - so they are a contract, not a curiosity. |
| 150 |
|
| void |
| 151 |
|
| _layout(...) |
| 152 |
|
| PPCODE: |
| 153 |
1 |
▶ | EXTEND(SP, 14); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 14); |
0 |
1 |
1 |
|
| 154 |
1 |
| mPUSHp("magic", 5); mPUSHu((UV)SA_MAGIC); |
| 155 |
1 |
| mPUSHp("layout", 6); mPUSHu((UV)SA_LAYOUT_VERSION); |
| 156 |
1 |
| mPUSHp("header", 6); mPUSHu((UV)sizeof(sa_header)); |
| 157 |
1 |
| mPUSHp("reg", 3); mPUSHu((UV)sizeof(sa_reg)); |
| 158 |
1 |
| mPUSHp("align", 5); mPUSHu((UV)SA_ALIGN); |
| 159 |
1 |
| mPUSHp("namelen", 7); mPUSHu((UV)SA_NAMELEN); |
| 160 |
1 |
| mPUSHp("word", 4); mPUSHu((UV)sizeof(void *)); |
| 161 |
|
| |
| 162 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_ |
| 163 |
|
| |
| 164 |
|
| UV |
| 165 |
|
| sar_size(self) |
| 166 |
|
| SV *self |
| 167 |
|
| PREINIT: |
| 168 |
|
| sa_region *r; |
| 169 |
|
| CODE: |
| 170 |
4 |
| r = SA_SELF(sa_region, self); |
| 171 |
4 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
4 |
0 |
4 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
4 |
4 |
|
| 172 |
4 |
▶ | RETVAL = (UV)r->total; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)r->total; |
0 |
4 |
4 |
|
| 173 |
|
| OUTPUT: |
| 174 |
|
| RETVAL |
| 175 |
|
| |
| 176 |
|
| # Did THIS process create the region, or attach to one that was already there? |
| 177 |
|
| # A caller that has to seed a region exactly once uses this to decide. |
| 178 |
|
| int |
| 179 |
|
| sar_created(self) |
| 180 |
|
| SV *self |
| 181 |
|
| PREINIT: |
| 182 |
|
| sa_region *r; |
| 183 |
|
| CODE: |
| 184 |
7 |
| r = SA_SELF(sa_region, self); |
| 185 |
7 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
7 |
0 |
7 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
7 |
7 |
|
| 186 |
7 |
▶ | RETVAL = r->created ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = r->created ? 1 : 0; |
0 |
7 |
7 |
|
| 187 |
|
| OUTPUT: |
| 188 |
|
| RETVAL |
| 189 |
|
| |
| 190 |
|
| # The address this process mapped at. Only a test has any business with it - |
| 191 |
|
| # t/03-relocatable.t asserts two mappings of one region land at DIFFERENT |
| 192 |
|
| # addresses, which is what makes that test prove anything. |
| 193 |
|
| UV |
| 194 |
|
| sar_base(self) |
| 195 |
|
| SV *self |
| 196 |
|
| PREINIT: |
| 197 |
|
| sa_region *r; |
| 198 |
|
| CODE: |
| 199 |
10 |
| r = SA_SELF(sa_region, self); |
| 200 |
10 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
10 |
0 |
10 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
10 |
10 |
|
| 201 |
10 |
▶ | RETVAL = PTR2UV(r->map.base); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV(r->map.base); |
0 |
10 |
10 |
|
| 202 |
|
| OUTPUT: |
| 203 |
|
| RETVAL |
| 204 |
|
| |
| 205 |
|
| # The handle itself, for a consumer that created the arena from Perl and drives |
| 206 |
|
| # the same region through the C ABI (Hyperman does). Private, like _abi_ptr, |
| 207 |
|
| # and unsigned for the same reason: PTR2UV, never PTR2IV. |
| 208 |
|
| UV |
| 209 |
|
| sar__region_ptr(self) |
| 210 |
|
| SV *self |
| 211 |
|
| PREINIT: |
| 212 |
|
| sa_region *r; |
| 213 |
|
| CODE: |
| 214 |
2 |
| r = SA_SELF(sa_region, self); |
| 215 |
2 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
2 |
0 |
2 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
2 |
2 |
|
| 216 |
2 |
▶ | RETVAL = PTR2UV(r); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV(r); |
0 |
2 |
2 |
|
| 217 |
|
| OUTPUT: |
| 218 |
|
| RETVAL |
| 219 |
|
| |
| 220 |
|
| # Registry entries this process has refused because their extent is not inside |
| 221 |
|
| # the mapping. Non-zero means the arena has been written by something that |
| 222 |
|
| # should not have: corruption, or another process. It is not a health metric to |
| 223 |
|
| # graph and forget - it is zero or it is a bug. |
| 224 |
|
| UV |
| 225 |
|
| sar_refused(self) |
| 226 |
|
| SV *self |
| 227 |
|
| PREINIT: |
| 228 |
|
| sa_region *r; |
| 229 |
|
| CODE: |
| 230 |
3 |
| r = SA_SELF(sa_region, self); |
| 231 |
3 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
3 |
0 |
3 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
3 |
3 |
|
| 232 |
|
| #if SA_HAVE_ATOMICS |
| 233 |
3 |
▶ | RETVAL = (UV)sa_at_load32_acq(&r->hdr->reg_refused); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)sa_at_load32_acq(&r->hdr->reg_refused); |
0 |
3 |
3 |
|
| 234 |
|
| #else |
| 235 |
|
| RETVAL = 0; |
| 236 |
|
| #endif |
| 237 |
|
| OUTPUT: |
| 238 |
|
| RETVAL |
| 239 |
|
| |
| 240 |
|
| # Why the last ->region call on this handle returned an empty list: SA_E_BUSY |
| 241 |
|
| # for a stripe held through the whole bounded wait, SA_E_FULL for a registry or |
| 242 |
|
| # an arena with no room, SA_E_SHAPE for a name already carved with another |
| 243 |
|
| # type or a smaller length, SA_E_NOENT for a plain lookup that missed. Private: |
| 244 |
|
| # a reason for a diagnostic, not a second way to ask whether a region exists. |
| 245 |
|
| IV |
| 246 |
|
| sar__last_err(self) |
| 247 |
|
| SV *self |
| 248 |
|
| PREINIT: |
| 249 |
|
| sa_region *r; |
| 250 |
|
| CODE: |
| 251 |
4 |
| r = SA_SELF(sa_region, self); |
| 252 |
4 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
4 |
0 |
4 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
4 |
4 |
|
| 253 |
4 |
▶ | RETVAL = (IV)r->last_err; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (IV)r->last_err; |
0 |
4 |
4 |
|
| 254 |
|
| OUTPUT: |
| 255 |
|
| RETVAL |
| 256 |
|
| |
| 257 |
|
| # $arena->region($name, size => N) -> ($offset, $length) |
| 258 |
|
| # |
| 259 |
|
| # Carves it, or hands back the one already there under that name. An empty list |
| 260 |
|
| # when the region is full or the name is unusable. |
| 261 |
|
| # |
| 262 |
|
| # The empty list carries no reason, and a CPAN smoker reporting "two carves |
| 263 |
|
| # refused" with no way to say whether the arena was full or a stripe was merely |
| 264 |
|
| # busy is how a transient contention failure got read as an exhausted registry. |
| 265 |
|
| # The code from the last refusal in this process is kept for _last_err below. |
| 266 |
|
| void |
| 267 |
|
| sar_region(self, name, ...) |
| 268 |
|
| SV *self |
| 269 |
|
| SV *name |
| 270 |
|
| PREINIT: |
| 271 |
|
| sa_region *r; |
| 272 |
|
| sa_reg *e; |
| 273 |
|
| const char *nm; |
| 274 |
|
| STRLEN nlen; |
| 275 |
44042 |
| UV size = 0; |
| 276 |
44042 |
| int err = SA_E_OK; |
| 277 |
|
| I32 i; |
| 278 |
|
| PPCODE: |
| 279 |
44042 |
| r = SA_SELF(sa_region, self); |
| 280 |
44042 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
44042 |
0 |
44042 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
44042 |
44042 |
|
| 281 |
44042 |
| nm = SvPV(name, nlen); |
| 282 |
84078 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
40036 |
44042 |
84078 |
|
| 283 |
40036 |
| const char *o = SvPV_nolen(ST(i)); |
| 284 |
40036 |
▶ | if (strEQ(o, "size")) size = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "size")) size = SvUV(ST(i + 1)); |
40036 |
0 |
40036 |
|
| 285 |
|
| } |
| 286 |
44042 |
| r->last_err = SA_E_OK; |
| 287 |
40036 |
| e = size ? sa_carve(r, nm, (size_t)nlen, (uint64_t)size, SA_T_RAW, &err) |
| 288 |
44042 |
▶ | : sa_find(r, nm, (size_t)nlen); |
Branch: 1 branch
| Branch | True | False | Total |
| : sa_find(r, nm, (size_t)nlen); |
40036 |
4006 |
44042 |
|
| 289 |
44042 |
▶ | if (!e) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) { |
16 |
44026 |
44042 |
|
| 290 |
|
| /* sa_find reports nothing, so a plain lookup that missed is |
| 291 |
|
| * SA_E_NOENT rather than whatever the last carve left behind. */ |
| 292 |
16 |
▶ | r->last_err = size ? err : SA_E_NOENT; |
Branch: 1 branch
| Branch | True | False | Total |
| r->last_err = size ? err : SA_E_NOENT; |
14 |
2 |
16 |
|
| 293 |
16 |
| XSRETURN_EMPTY; |
| 294 |
|
| } |
| 295 |
44026 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
44026 |
44026 |
|
| 296 |
44026 |
| mPUSHu((UV)e->off); |
| 297 |
44026 |
| mPUSHu((UV)e->len); |
| 298 |
|
| |
| 299 |
|
| # ---- wakeups --------------------------------------------------------------- |
| 300 |
|
| # |
| 301 |
|
| # $arena->wakers($n) BEFORE the fork, then $arena->waker in each child, then |
| 302 |
|
| # select on $arena->waker_fd and call $arena->drained before draining the ring. |
| 303 |
|
| # |
| 304 |
|
| # A wakeup means there is something to read: the poke follows the commit, never |
| 305 |
|
| # precedes it. sa_wake.h has the reasoning and t/12-wake.t holds a publisher |
| 306 |
|
| # inside its commit to prove it. |
| 307 |
|
| |
| 308 |
|
| # Create the pipes. Must run before the fork: the descriptors are inherited, |
| 309 |
|
| # which is what makes their numbers meaningful in every process. |
| 310 |
|
| int |
| 311 |
|
| sar_wakers(self, ...) |
| 312 |
|
| SV *self |
| 313 |
|
| PREINIT: |
| 314 |
|
| sa_region *r; |
| 315 |
4 |
▶ | UV n = 16; |
Branch: 1 branch
| Branch | True | False | Total |
| UV n = 16; |
4 |
0 |
4 |
|
| 316 |
|
| CODE: |
| 317 |
4 |
| r = SA_SELF(sa_region, self); |
| 318 |
4 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
4 |
0 |
4 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
4 |
4 |
|
| 319 |
4 |
▶ | if (items > 1) n = SvUV(ST(1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) n = SvUV(ST(1)); |
4 |
0 |
4 |
|
| 320 |
4 |
| RETVAL = sa_wake_init(r, (uint32_t)n); |
| 321 |
|
| OUTPUT: |
| 322 |
|
| RETVAL |
| 323 |
|
| |
| 324 |
|
| # Claim one for this process, after the fork. Returns its index, or -1 when |
| 325 |
|
| # there is none to be had - in which case the caller polls. |
| 326 |
|
| int |
| 327 |
|
| sar_waker(self, ...) |
| 328 |
|
| SV *self |
| 329 |
|
| PREINIT: |
| 330 |
|
| sa_region *r; |
| 331 |
3 |
▶ | IV idx = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| IV idx = -1; |
3 |
0 |
3 |
|
| 332 |
|
| CODE: |
| 333 |
3 |
| r = SA_SELF(sa_region, self); |
| 334 |
3 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
3 |
0 |
3 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
3 |
3 |
|
| 335 |
3 |
▶ | if (items > 1) idx = SvIV(ST(1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) idx = SvIV(ST(1)); |
0 |
3 |
3 |
|
| 336 |
3 |
| RETVAL = sa_wake_take(r, (int)idx); |
| 337 |
|
| OUTPUT: |
| 338 |
|
| RETVAL |
| 339 |
|
| |
| 340 |
|
| # The descriptor to select on, or -1. A process that attached to a NAMED region |
| 341 |
|
| # inherited no pipe and can never be given one, so it polls; the POD says so |
| 342 |
|
| # rather than pretending otherwise. |
| 343 |
|
| int |
| 344 |
|
| sar_waker_fd(self) |
| 345 |
|
| SV *self |
| 346 |
|
| PREINIT: |
| 347 |
|
| sa_region *r; |
| 348 |
|
| CODE: |
| 349 |
5 |
| r = SA_SELF(sa_region, self); |
| 350 |
5 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
5 |
0 |
5 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
5 |
5 |
|
| 351 |
5 |
| RETVAL = sa_wake_fd(r); |
| 352 |
|
| OUTPUT: |
| 353 |
|
| RETVAL |
| 354 |
|
| |
| 355 |
|
| void |
| 356 |
|
| sar_drained(self) |
| 357 |
|
| SV *self |
| 358 |
|
| PREINIT: |
| 359 |
|
| sa_region *r; |
| 360 |
|
| CODE: |
| 361 |
6 |
| r = SA_SELF(sa_region, self); |
| 362 |
6 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
6 |
0 |
6 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
6 |
6 |
|
| 363 |
6 |
| sa_wake_drained(r); |
| 364 |
|
| |
| 365 |
|
| # The pid as the library sees it. It is cached, and the cache is dropped by a |
| 366 |
|
| # pthread_atfork child handler; a handler that linked but never runs leaves |
| 367 |
|
| # every child answering as its parent, and every fork test failing at once |
| 368 |
|
| # with nothing naming the cause. t/06-fork.t compares this with $$ in a child. |
| 369 |
|
| UV |
| 370 |
|
| sar__pid() |
| 371 |
|
| CODE: |
| 372 |
1 |
| RETVAL = (UV)sa_getpid(); |
| 373 |
|
| OUTPUT: |
| 374 |
|
| RETVAL |
| 375 |
|
| |
| 376 |
|
| # The two knobs a crash test needs, and nothing else has any use for. |
| 377 |
|
| # |
| 378 |
|
| # `stall_us` widens the window between reserving a record and committing it, so |
| 379 |
|
| # a test can kill a publisher INSIDE it rather than racing to. `reap_grace_us` |
| 380 |
|
| # is how long a reader waits on a hole before it starts asking whether the |
| 381 |
|
| # publisher is dead. Both live in the region header rather than behind an |
| 382 |
|
| # #ifdef, because a forked child has to honour them and a child of a build that |
| 383 |
|
| # compiled them out cannot. |
| 384 |
|
| void |
| 385 |
|
| sar__test_timing(self, ...) |
| 386 |
|
| SV *self |
| 387 |
|
| PREINIT: |
| 388 |
|
| sa_region *r; |
| 389 |
|
| I32 i; |
| 390 |
|
| PPCODE: |
| 391 |
7 |
| r = SA_SELF(sa_region, self); |
| 392 |
7 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
7 |
0 |
7 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
7 |
7 |
|
| 393 |
17 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
10 |
7 |
17 |
|
| 394 |
10 |
| const char *o = SvPV_nolen(ST(i)); |
| 395 |
10 |
▶ | if (strEQ(o, "stall_us")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "stall_us")) |
7 |
3 |
10 |
|
| 396 |
7 |
| sa_at_store32_rel(&r->hdr->stall_us, (uint32_t)SvUV(ST(i + 1))); |
| 397 |
3 |
▶ | else if (strEQ(o, "reap_grace_us")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "reap_grace_us")) |
3 |
0 |
3 |
|
| 398 |
3 |
| sa_at_store32_rel(&r->hdr->reap_grace_us, |
| 399 |
3 |
| (uint32_t)SvUV(ST(i + 1))); |
| 400 |
|
| } |
| 401 |
7 |
▶ | EXTEND(SP, 4); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 4); |
0 |
7 |
7 |
|
| 402 |
7 |
| mPUSHp("stall_us", 8); |
| 403 |
7 |
| mPUSHu((UV)sa_at_load32_acq(&r->hdr->stall_us)); |
| 404 |
7 |
| mPUSHp("reap_grace_us", 13); |
| 405 |
7 |
| mPUSHu((UV)sa_at_load32_acq(&r->hdr->reap_grace_us)); |
| 406 |
|
| |
| 407 |
|
| # How many processes are registered as peers, and how many of those are live. |
| 408 |
|
| void |
| 409 |
|
| sar_peers(self) |
| 410 |
|
| SV *self |
| 411 |
|
| PREINIT: |
| 412 |
|
| sa_region *r; |
| 413 |
|
| sa_peer *peers; |
| 414 |
4 |
| uint32_t i, used, live = 0, reaped = 0; |
| 415 |
|
| PPCODE: |
| 416 |
4 |
| r = SA_SELF(sa_region, self); |
| 417 |
4 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
4 |
0 |
4 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
4 |
4 |
|
| 418 |
4 |
| peers = SA_PEERS(r->map.base, r->hdr); |
| 419 |
4 |
| used = sa_at_load32_acq(&r->hdr->peers_used); |
| 420 |
4 |
▶ | if (used > r->hdr->peers_max) used = r->hdr->peers_max; |
Branch: 1 branch
| Branch | True | False | Total |
| if (used > r->hdr->peers_max) used = r->hdr->peers_max; |
0 |
4 |
4 |
|
| 421 |
13 |
▶ | for (i = 0; i < used; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < used; i++) { |
9 |
4 |
13 |
|
| 422 |
9 |
| uint32_t st = sa_at_load32_acq(&peers[i].state); |
| 423 |
9 |
▶ | if (st == SA_P_LIVE) live++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st == SA_P_LIVE) live++; |
8 |
1 |
9 |
|
| 424 |
9 |
▶ | if (st == SA_P_REAPED) reaped++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (st == SA_P_REAPED) reaped++; |
1 |
8 |
9 |
|
| 425 |
|
| } |
| 426 |
4 |
▶ | EXTEND(SP, 6); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 6); |
0 |
4 |
4 |
|
| 427 |
4 |
| mPUSHp("used", 4); mPUSHu((UV)used); |
| 428 |
4 |
| mPUSHp("live", 4); mPUSHu((UV)live); |
| 429 |
4 |
| mPUSHp("reaped", 6); mPUSHu((UV)reaped); |
| 430 |
|
| |
| 431 |
|
| # Raw bytes into and out of a carved region. |
| 432 |
|
| # |
| 433 |
|
| # These exist because a region with no tenant is still worth testing, and |
| 434 |
|
| # because SA_T_RAW is a real use: a caller who wants a fixed-size shared |
| 435 |
|
| # structure of its own design carves one and addresses it itself. |
| 436 |
|
| void |
| 437 |
|
| sar_poke(self, name, off, bytes) |
| 438 |
|
| SV *self |
| 439 |
|
| SV *name |
| 440 |
|
| UV off |
| 441 |
|
| SV *bytes |
| 442 |
|
| PREINIT: |
| 443 |
|
| sa_region *r; |
| 444 |
|
| sa_reg *e; |
| 445 |
|
| const char *nm, *p; |
| 446 |
|
| STRLEN nlen, blen; |
| 447 |
|
| CODE: |
| 448 |
12 |
| r = SA_SELF(sa_region, self); |
| 449 |
12 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
12 |
0 |
12 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
12 |
12 |
|
| 450 |
12 |
| nm = SvPV(name, nlen); |
| 451 |
12 |
| e = sa_find(r, nm, (size_t)nlen); |
| 452 |
12 |
▶ | if (!e) croak("Shared::Arena: no region called '%s'", nm); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("Shared::Arena: no region called '%s'", nm); |
0 |
12 |
12 |
|
| 453 |
12 |
| p = SvPV(bytes, blen); |
| 454 |
12 |
▶ | if (off + blen > e->len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (off + blen > e->len) |
0 |
12 |
12 |
|
| 455 |
0 |
| croak("Shared::Arena: %lu bytes at %lu runs past the region's %lu", |
| 456 |
|
| (unsigned long)blen, (unsigned long)off, |
| 457 |
|
| (unsigned long)e->len); |
| 458 |
12 |
| memcpy((char *)sa_ptr(r, e->off) + off, p, blen); |
| 459 |
|
| |
| 460 |
|
| SV * |
| 461 |
|
| sar_peek(self, name, off, len) |
| 462 |
|
| SV *self |
| 463 |
|
| SV *name |
| 464 |
|
| UV off |
| 465 |
|
| UV len |
| 466 |
|
| PREINIT: |
| 467 |
|
| sa_region *r; |
| 468 |
|
| sa_reg *e; |
| 469 |
|
| const char *nm; |
| 470 |
|
| STRLEN nlen; |
| 471 |
|
| CODE: |
| 472 |
231 |
| r = SA_SELF(sa_region, self); |
| 473 |
231 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
231 |
0 |
231 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
231 |
231 |
|
| 474 |
231 |
| nm = SvPV(name, nlen); |
| 475 |
231 |
| e = sa_find(r, nm, (size_t)nlen); |
| 476 |
231 |
▶ | if (!e) croak("Shared::Arena: no region called '%s'", nm); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("Shared::Arena: no region called '%s'", nm); |
0 |
231 |
231 |
|
| 477 |
231 |
▶ | if (off + len > e->len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (off + len > e->len) |
0 |
231 |
231 |
|
| 478 |
0 |
| croak("Shared::Arena: %lu bytes at %lu runs past the region's %lu", |
| 479 |
|
| (unsigned long)len, (unsigned long)off, |
| 480 |
|
| (unsigned long)e->len); |
| 481 |
231 |
| RETVAL = newSVpvn((char *)sa_ptr(r, e->off) + off, (STRLEN)len); |
| 482 |
|
| OUTPUT: |
| 483 |
|
| RETVAL |
| 484 |
|
| |
| 485 |
|
| # The names carved so far, in the order they were carved. |
| 486 |
|
| void |
| 487 |
|
| sar_regions(self) |
| 488 |
|
| SV *self |
| 489 |
|
| PREINIT: |
| 490 |
|
| sa_region *r; |
| 491 |
|
| sa_reg *regs; |
| 492 |
|
| uint32_t used, i; |
| 493 |
|
| PPCODE: |
| 494 |
13 |
| r = SA_SELF(sa_region, self); |
| 495 |
13 |
▶ | if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
13 |
0 |
13 |
| if (!r || !r->map.base) croak("Shared::Arena: this region is released"); |
0 |
13 |
13 |
|
| 496 |
13 |
| regs = SA_REGS(r->map.base, r->hdr); |
| 497 |
13 |
| used = sa_at_load32_acq(&r->hdr->reg_used); |
| 498 |
13 |
▶ | if (used > r->hdr->reg_max) used = r->hdr->reg_max; |
Branch: 1 branch
| Branch | True | False | Total |
| if (used > r->hdr->reg_max) used = r->hdr->reg_max; |
0 |
13 |
13 |
|
| 499 |
40016 |
▶ | for (i = 0; i < used; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < used; i++) { |
40003 |
13 |
40016 |
|
| 500 |
40003 |
▶ | if (sa_at_load32_acq(®s[i].state) != SA_R_LIVE) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_at_load32_acq(®s[i].state) != SA_R_LIVE) continue; |
0 |
40003 |
40003 |
|
| 501 |
40003 |
▶ | mXPUSHp(regs[i].name, strlen(regs[i].name)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHp(regs[i].name, strlen(regs[i].name)); |
0 |
40003 |
40003 |
|
| 502 |
|
| } |
| 503 |
|
| |
| 504 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena |
| 505 |
|
| |
| 506 |
|
| void |
| 507 |
|
| DESTROY(self) |
| 508 |
|
| SV *self |
| 509 |
|
| PREINIT: |
| 510 |
|
| sa_region *r; |
| 511 |
|
| CODE: |
| 512 |
158 |
| r = SA_SELF(sa_region, self); |
| 513 |
158 |
▶ | if (r) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { |
158 |
0 |
158 |
|
| 514 |
158 |
| sa_region_release(r); |
| 515 |
158 |
| sv_setiv(SvRV(self), 0); |
| 516 |
|
| } |