| 1 |
|
| # Shared::Arena::Ring::Group - one record to one member of the pool. |
| 2 |
|
| # |
| 3 |
|
| # A cursor is process-local, so every reader holding one sees every record. |
| 4 |
|
| # A group's cursor is in the mapping, so whoever wins the compare-and-swap owns |
| 5 |
|
| # that record and nobody else does. The difference between fanout and a queue |
| 6 |
|
| # is WHERE THE CURSOR LIVES, and nothing else. |
| 7 |
|
| |
| 8 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena::Ring PREFIX = sarrg_ |
| 9 |
|
| |
| 10 |
|
| # $ring->group($name, topic => 'jobs', from_start => 1) |
| 11 |
|
| SV * |
| 12 |
|
| sarrg_group(self, name, ...) |
| 13 |
|
| SV *self |
| 14 |
|
| SV *name |
| 15 |
|
| PREINIT: |
| 16 |
|
| sa_ring *r; |
| 17 |
|
| sa_group *g; |
| 18 |
|
| sa_group_h *gh; |
| 19 |
67 |
| const char *nm, *tp = NULL; |
| 20 |
67 |
| STRLEN nlen, tlen = 0; |
| 21 |
67 |
| int from_start = 0, err = SA_E_OK; |
| 22 |
67 |
| uint64_t from = 0; |
| 23 |
|
| I32 i; |
| 24 |
|
| SV *obj; |
| 25 |
|
| CODE: |
| 26 |
67 |
| r = SA_SELF(sa_ring, self); |
| 27 |
67 |
▶ | if (!r) croak("Shared::Arena::Ring: this ring is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r) croak("Shared::Arena::Ring: this ring is released"); |
0 |
67 |
67 |
|
| 28 |
67 |
| nm = SvPV(name, nlen); |
| 29 |
85 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
18 |
67 |
85 |
|
| 30 |
18 |
| const char *o = SvPV_nolen(ST(i)); |
| 31 |
18 |
▶ | if (strEQ(o, "topic") && SvOK(ST(i + 1))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (strEQ(o, "topic") && SvOK(ST(i + 1))) |
18 |
0 |
18 |
| if (strEQ(o, "topic") && SvOK(ST(i + 1))) |
18 |
0 |
18 |
|
| 32 |
36 |
| tp = SvPV(ST(i + 1), tlen); |
| 33 |
0 |
▶ | else if (strEQ(o, "from_start")) from_start = SvTRUE(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "from_start")) from_start = SvTRUE(ST(i + 1)); |
0 |
0 |
0 |
|
| 34 |
|
| } |
| 35 |
|
| #if SA_HAVE_ATOMICS |
| 36 |
67 |
▶ | if (from_start) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (from_start) { |
0 |
67 |
67 |
|
| 37 |
0 |
| uint64_t end = sa_at_load64_acq(&r->hdr->seq); |
| 38 |
0 |
▶ | from = (end > r->nslots) ? end - r->nslots : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| from = (end > r->nslots) ? end - r->nslots : 1; |
0 |
0 |
0 |
|
| 39 |
|
| } |
| 40 |
67 |
| gh = sa_group_new(r, nm, (uint32_t)nlen, tp, (uint32_t)tlen, from, &err); |
| 41 |
67 |
▶ | if (!gh) croak("Shared::Arena::Ring: the group '%s' %s", |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gh) croak("Shared::Arena::Ring: the group '%s' %s", |
6 |
61 |
67 |
|
| 42 |
|
| nm, sa_strerror(err)); |
| 43 |
|
| PERL_UNUSED_VAR(g); |
| 44 |
|
| #else |
| 45 |
|
| croak("Shared::Arena::Ring: groups need atomics this build does not have"); |
| 46 |
|
| #endif |
| 47 |
61 |
| obj = newSV(0); |
| 48 |
61 |
| sv_setref_pv(obj, "Shared::Arena::Ring::Group", (void *)gh); |
| 49 |
61 |
| sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0); |
| 50 |
61 |
| SvREFCNT_inc(SvRV(self)); |
| 51 |
61 |
| RETVAL = obj; |
| 52 |
|
| OUTPUT: |
| 53 |
|
| RETVAL |
| 54 |
|
| |
| 55 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena::Ring::Group PREFIX = sarg_ |
| 56 |
|
| |
| 57 |
|
| # Records this worker has claimed, as [topic, payload, sequence] arrayrefs. |
| 58 |
|
| # Each one is this process's alone: no other member of the group will be given |
| 59 |
|
| # it, and if this process dies before acting on it, nobody will. |
| 60 |
|
| void |
| 61 |
|
| sarg_claim(self, ...) |
| 62 |
|
| SV *self |
| 63 |
|
| PREINIT: |
| 64 |
|
| sa_group_h *gh; |
| 65 |
159 |
| IV max = 1; |
| 66 |
|
| I32 i; |
| 67 |
|
| PPCODE: |
| 68 |
159 |
| gh = SA_SELF(sa_group_h, self); |
| 69 |
159 |
▶ | if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
0 |
159 |
159 |
|
| 70 |
299 |
▶ | for (i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) { |
140 |
159 |
299 |
|
| 71 |
140 |
| const char *o = SvPV_nolen(ST(i)); |
| 72 |
140 |
▶ | if (strEQ(o, "max")) max = SvIV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "max")) max = SvIV(ST(i + 1)); |
140 |
0 |
140 |
|
| 73 |
|
| } |
| 74 |
|
| #if SA_HAVE_ATOMICS |
| 75 |
|
| { |
| 76 |
|
| /* Claiming is proof of life, exactly as draining is: a worker that |
| 77 |
|
| * only ever consumes must still tick, or a publisher's hole check |
| 78 |
|
| * would judge it wedged. */ |
| 79 |
159 |
| sa_peer_join(gh->ring->arena); |
| 80 |
159 |
| sa_peer_beat(gh->ring->arena); |
| 81 |
643 |
▶ | while (max <= 0 || (IV)(SP - MARK) < max) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (max <= 0 || (IV)(SP - MARK) < max) { |
0 |
643 |
643 |
| while (max <= 0 || (IV)(SP - MARK) < max) { |
503 |
140 |
643 |
|
| 82 |
503 |
| uint64_t seq = 0; |
| 83 |
503 |
| const char *topic = NULL, *data = NULL; |
| 84 |
503 |
| uint32_t tlen = 0, dlen = 0, flags = 0; |
| 85 |
503 |
▶ | if (sa_group_claim(gh->ring, gh->g, gh->scratch, &seq, |
Branch: 1 branch
| Branch | True | False | Total |
| if (sa_group_claim(gh->ring, gh->g, gh->scratch, &seq, |
19 |
484 |
503 |
|
| 86 |
|
| &topic, &tlen, &data, &dlen, &flags) |
| 87 |
|
| != SA_READ_OK) |
| 88 |
19 |
| break; |
| 89 |
|
| { |
| 90 |
484 |
| AV *av = newAV(); |
| 91 |
484 |
| av_push(av, newSVpvn(topic, tlen)); |
| 92 |
484 |
| av_push(av, newSVpvn(data, dlen)); |
| 93 |
484 |
| av_push(av, newSVuv((UV)seq)); |
| 94 |
484 |
▶ | XPUSHs(sv_2mortal(newRV_noinc((SV *)av))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newRV_noinc((SV *)av))); |
0 |
484 |
484 |
|
| 95 |
|
| } |
| 96 |
484 |
| gh->claimed++; |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
|
| #endif |
| 100 |
|
| |
| 101 |
|
| # The group's shared position: what every member is working from. |
| 102 |
|
| UV |
| 103 |
|
| sarg_position(self) |
| 104 |
|
| SV *self |
| 105 |
|
| PREINIT: |
| 106 |
|
| sa_group_h *gh; |
| 107 |
|
| CODE: |
| 108 |
4 |
| gh = SA_SELF(sa_group_h, self); |
| 109 |
4 |
▶ | if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
0 |
4 |
4 |
|
| 110 |
|
| #if SA_HAVE_ATOMICS |
| 111 |
4 |
| RETVAL = (UV)sa_at_load64_acq(&gh->g->cursor); |
| 112 |
|
| #else |
| 113 |
|
| RETVAL = 0; |
| 114 |
|
| #endif |
| 115 |
|
| OUTPUT: |
| 116 |
|
| RETVAL |
| 117 |
|
| |
| 118 |
|
| SV * |
| 119 |
|
| sarg_topic(self) |
| 120 |
|
| SV *self |
| 121 |
|
| PREINIT: |
| 122 |
|
| sa_group_h *gh; |
| 123 |
|
| CODE: |
| 124 |
6 |
| gh = SA_SELF(sa_group_h, self); |
| 125 |
6 |
▶ | if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
0 |
6 |
6 |
|
| 126 |
6 |
| RETVAL = gh->g->tlen ? newSVpvn(gh->g->topic, gh->g->tlen) |
| 127 |
6 |
▶ | : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| : &PL_sv_undef; |
6 |
0 |
6 |
|
| 128 |
|
| OUTPUT: |
| 129 |
|
| RETVAL |
| 130 |
|
| |
| 131 |
|
| # name / topic / position / delivered / lapped / skipped / mine |
| 132 |
|
| # |
| 133 |
|
| # `delivered` is the GROUP's, across every member: the number the pool got |
| 134 |
|
| # through. `mine` is this process's share of it, which is the one that says |
| 135 |
|
| # whether the work is spread or whether one worker is doing all of it. |
| 136 |
|
| void |
| 137 |
|
| sarg_stats(self) |
| 138 |
|
| SV *self |
| 139 |
|
| PREINIT: |
| 140 |
|
| sa_group_h *gh; |
| 141 |
|
| PPCODE: |
| 142 |
8 |
| gh = SA_SELF(sa_group_h, self); |
| 143 |
8 |
▶ | if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gh) croak("Shared::Arena::Ring::Group: this group is released"); |
0 |
8 |
8 |
|
| 144 |
8 |
▶ | EXTEND(SP, 14); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 14); |
0 |
8 |
8 |
|
| 145 |
8 |
| mPUSHp("name", 4); |
| 146 |
8 |
| mPUSHp(gh->g->name, strlen(gh->g->name)); |
| 147 |
8 |
| mPUSHp("topic", 5); |
| 148 |
8 |
| mPUSHp(gh->g->topic, gh->g->tlen); |
| 149 |
|
| #if SA_HAVE_ATOMICS |
| 150 |
8 |
| mPUSHp("position", 8); |
| 151 |
8 |
| mPUSHu((UV)sa_at_load64_acq(&gh->g->cursor)); |
| 152 |
8 |
| mPUSHp("delivered", 9); |
| 153 |
8 |
| mPUSHu((UV)sa_at_load64_acq(&gh->g->delivered)); |
| 154 |
8 |
| mPUSHp("lapped", 6); |
| 155 |
8 |
| mPUSHu((UV)sa_at_load64_acq(&gh->g->lapped)); |
| 156 |
8 |
| mPUSHp("skipped", 7); |
| 157 |
8 |
| mPUSHu((UV)sa_at_load64_acq(&gh->g->skipped)); |
| 158 |
|
| #endif |
| 159 |
8 |
| mPUSHp("mine", 4); |
| 160 |
8 |
| mPUSHu((UV)gh->claimed); |
| 161 |
|
| |
| 162 |
|
| void |
| 163 |
|
| sarg_DESTROY(self) |
| 164 |
|
| SV *self |
| 165 |
|
| PREINIT: |
| 166 |
|
| sa_group_h *gh; |
| 167 |
|
| CODE: |
| 168 |
61 |
| gh = SA_SELF(sa_group_h, self); |
| 169 |
61 |
▶ | if (gh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (gh) { |
61 |
0 |
61 |
|
| 170 |
|
| /* The group in the mapping OUTLIVES this handle. Leaving is not |
| 171 |
|
| * releasing: another worker is still claiming from that cursor, and |
| 172 |
|
| * a member that exits must not take the pool's position with it. */ |
| 173 |
61 |
| sa_group_free(gh); |
| 174 |
61 |
| sv_setiv(SvRV(self), 0); |
| 175 |
|
| } |