| 1 |
|
| # Shared::Arena::Histogram - a distribution every process adds to at once. |
| 2 |
|
| |
| 3 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_ |
| 4 |
|
| |
| 5 |
|
| # $arena->histogram($name, max => N, sigbits => 4) |
| 6 |
|
| SV * |
| 7 |
|
| sar_histogram(self, name, ...) |
| 8 |
|
| SV *self |
| 9 |
|
| SV *name |
| 10 |
|
| PREINIT: |
| 11 |
|
| sa_region *arena; |
| 12 |
|
| sa_reg *e; |
| 13 |
|
| sa_hist *h; |
| 14 |
|
| const char *nm; |
| 15 |
|
| STRLEN nlen; |
| 16 |
53 |
| UV maxv = 3600000000UL; /* an hour in microseconds */ |
| 17 |
53 |
| UV sig = 4; |
| 18 |
53 |
| int err = SA_E_OK; |
| 19 |
|
| I32 i; |
| 20 |
|
| SV *obj; |
| 21 |
|
| CODE: |
| 22 |
53 |
| arena = SA_SELF(sa_region, self); |
| 23 |
53 |
▶ | if (!arena || !arena->map.base) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!arena || !arena->map.base) |
53 |
0 |
53 |
| if (!arena || !arena->map.base) |
0 |
53 |
53 |
|
| 24 |
0 |
| croak("Shared::Arena: this region is released"); |
| 25 |
53 |
| nm = SvPV(name, nlen); |
| 26 |
159 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
106 |
53 |
159 |
|
| 27 |
106 |
| const char *o = SvPV_nolen(ST(i)); |
| 28 |
106 |
▶ | if (strEQ(o, "max")) maxv = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "max")) maxv = SvUV(ST(i + 1)); |
53 |
53 |
106 |
|
| 29 |
53 |
▶ | else if (strEQ(o, "sigbits")) sig = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "sigbits")) sig = SvUV(ST(i + 1)); |
53 |
0 |
53 |
|
| 30 |
|
| } |
| 31 |
53 |
▶ | if (!maxv) croak("Shared::Arena: a histogram needs a max above zero"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!maxv) croak("Shared::Arena: a histogram needs a max above zero"); |
0 |
53 |
53 |
|
| 32 |
53 |
▶ | if (sig < SA_HIST_MIN_SIG || sig > SA_HIST_MAX_SIG) |
Branch: 2 branches
| Branch | True | False | Total |
| if (sig < SA_HIST_MIN_SIG || sig > SA_HIST_MAX_SIG) |
53 |
0 |
53 |
| if (sig < SA_HIST_MIN_SIG || sig > SA_HIST_MAX_SIG) |
0 |
53 |
53 |
|
| 33 |
0 |
| croak("Shared::Arena: sigbits must be between %d and %d", |
| 34 |
|
| SA_HIST_MIN_SIG, SA_HIST_MAX_SIG); |
| 35 |
|
| |
| 36 |
53 |
| e = sa_carve(arena, nm, (size_t)nlen, |
| 37 |
|
| sa_hist_bytes((uint64_t)maxv, (uint32_t)sig), |
| 38 |
|
| SA_T_HIST, &err); |
| 39 |
53 |
▶ | if (!e) croak("Shared::Arena: the histogram '%s' %s", nm, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("Shared::Arena: the histogram '%s' %s", nm, |
0 |
53 |
53 |
|
| 40 |
|
| sa_strerror(err)); |
| 41 |
|
| |
| 42 |
53 |
| h = sa_hist_bind(arena, e, (uint64_t)maxv, (uint32_t)sig, &err); |
| 43 |
53 |
▶ | if (!h) croak("Shared::Arena: the histogram '%s' %s", nm, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena: the histogram '%s' %s", nm, |
0 |
53 |
53 |
|
| 44 |
|
| sa_strerror(err)); |
| 45 |
|
| |
| 46 |
53 |
| obj = newSV(0); |
| 47 |
53 |
| sv_setref_pv(obj, "Shared::Arena::Histogram", (void *)h); |
| 48 |
53 |
| sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0); |
| 49 |
53 |
| SvREFCNT_inc(SvRV(self)); |
| 50 |
53 |
| RETVAL = obj; |
| 51 |
|
| OUTPUT: |
| 52 |
|
| RETVAL |
| 53 |
|
| |
| 54 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena::Histogram PREFIX = sah_ |
| 55 |
|
| |
| 56 |
|
| void |
| 57 |
|
| sah_record(self, value, ...) |
| 58 |
|
| SV *self |
| 59 |
|
| UV value |
| 60 |
|
| PREINIT: |
| 61 |
|
| sa_hist *h; |
| 62 |
500 |
| UV n = 1; |
| 63 |
|
| CODE: |
| 64 |
500 |
| h = SA_SELF(sa_hist, self); |
| 65 |
500 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
500 |
500 |
|
| 66 |
500 |
▶ | if (items > 2) n = SvUV(ST(2)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) n = SvUV(ST(2)); |
0 |
500 |
500 |
|
| 67 |
500 |
| sa_hist_record(h, (uint64_t)value, (uint64_t)n); |
| 68 |
|
| |
| 69 |
|
| # The value at a quantile, as the TOP of the bucket it falls in - so it never |
| 70 |
|
| # claims the service was faster than it was. |
| 71 |
|
| UV |
| 72 |
|
| sah_quantile(self, q) |
| 73 |
|
| SV *self |
| 74 |
|
| double q |
| 75 |
|
| PREINIT: |
| 76 |
|
| sa_hist *h; |
| 77 |
|
| CODE: |
| 78 |
51 |
| h = SA_SELF(sa_hist, self); |
| 79 |
51 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
51 |
51 |
|
| 80 |
51 |
| RETVAL = (UV)sa_hist_quantile(h, q); |
| 81 |
|
| OUTPUT: |
| 82 |
|
| RETVAL |
| 83 |
|
| |
| 84 |
|
| UV |
| 85 |
|
| sah_count(self) |
| 86 |
|
| SV *self |
| 87 |
|
| PREINIT: |
| 88 |
|
| sa_hist *h; |
| 89 |
|
| CODE: |
| 90 |
3 |
| h = SA_SELF(sa_hist, self); |
| 91 |
3 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
3 |
3 |
|
| 92 |
3 |
| RETVAL = (UV)sa_at_load64_acq(&h->hdr->count); |
| 93 |
|
| OUTPUT: |
| 94 |
|
| RETVAL |
| 95 |
|
| |
| 96 |
|
| void |
| 97 |
|
| sah_reset(self) |
| 98 |
|
| SV *self |
| 99 |
|
| PREINIT: |
| 100 |
|
| sa_hist *h; |
| 101 |
|
| CODE: |
| 102 |
1 |
| h = SA_SELF(sa_hist, self); |
| 103 |
1 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
1 |
1 |
|
| 104 |
1 |
| sa_hist_reset(h); |
| 105 |
|
| |
| 106 |
|
| # The error bound: the largest fraction by which a recorded value and what this |
| 107 |
|
| # reports back can differ. A runtime answer, because it follows from sigbits. |
| 108 |
|
| double |
| 109 |
|
| sah_error(self) |
| 110 |
|
| SV *self |
| 111 |
|
| PREINIT: |
| 112 |
|
| sa_hist *h; |
| 113 |
|
| CODE: |
| 114 |
6 |
| h = SA_SELF(sa_hist, self); |
| 115 |
6 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
6 |
6 |
|
| 116 |
6 |
▶ | RETVAL = 1.0 / (double)(1ULL << h->sigbits); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = 1.0 / (double)(1ULL << h->sigbits); |
0 |
6 |
6 |
|
| 117 |
|
| OUTPUT: |
| 118 |
|
| RETVAL |
| 119 |
|
| |
| 120 |
|
| # count / sum / min / max / mean / over / buckets / sigbits |
| 121 |
|
| # |
| 122 |
|
| # `over` is values above the ceiling. They are counted apart and NOT clamped |
| 123 |
|
| # into the top bucket: clamping would let a flood of enormous values look like |
| 124 |
|
| # a busy top bucket, and every quantile would read plausibly and be wrong. |
| 125 |
|
| void |
| 126 |
|
| sah_stats(self) |
| 127 |
|
| SV *self |
| 128 |
|
| PREINIT: |
| 129 |
|
| sa_hist *h; |
| 130 |
|
| uint64_t c, s, mn; |
| 131 |
|
| PPCODE: |
| 132 |
9 |
| h = SA_SELF(sa_hist, self); |
| 133 |
9 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
9 |
9 |
|
| 134 |
9 |
| c = sa_at_load64_acq(&h->hdr->count); |
| 135 |
9 |
| s = sa_at_load64_acq(&h->hdr->sum); |
| 136 |
9 |
| mn = sa_at_load64_acq(&h->hdr->min); |
| 137 |
9 |
▶ | EXTEND(SP, 16); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 16); |
0 |
9 |
9 |
|
| 138 |
9 |
| mPUSHp("count", 5); mPUSHu((UV)c); |
| 139 |
9 |
| mPUSHp("sum", 3); mPUSHu((UV)s); |
| 140 |
9 |
▶ | mPUSHp("min", 3); mPUSHu((UV)(c ? mn : 0)); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHp("min", 3); mPUSHu((UV)(c ? mn : 0)); |
6 |
3 |
9 |
|
| 141 |
9 |
| mPUSHp("max", 3); mPUSHu((UV)sa_at_load64_acq(&h->hdr->max)); |
| 142 |
9 |
▶ | mPUSHp("mean", 4); mPUSHn(c ? (double)s / (double)c : 0.0); |
Branch: 1 branch
| Branch | True | False | Total |
| mPUSHp("mean", 4); mPUSHn(c ? (double)s / (double)c : 0.0); |
6 |
3 |
9 |
|
| 143 |
9 |
| mPUSHp("over", 4); mPUSHu((UV)sa_at_load64_acq(&h->hdr->over)); |
| 144 |
9 |
| mPUSHp("buckets", 7); mPUSHu((UV)h->nbuckets); |
| 145 |
9 |
| mPUSHp("sigbits", 7); mPUSHu((UV)h->sigbits); |
| 146 |
|
| |
| 147 |
|
| # The non-empty buckets, as ([low, high, count], ...). For drawing one, or for |
| 148 |
|
| # handing to something that wants the shape rather than a number. |
| 149 |
|
| void |
| 150 |
|
| sah_buckets(self) |
| 151 |
|
| SV *self |
| 152 |
|
| PREINIT: |
| 153 |
|
| sa_hist *h; |
| 154 |
|
| uint64_t i; |
| 155 |
|
| PPCODE: |
| 156 |
2 |
| h = SA_SELF(sa_hist, self); |
| 157 |
2 |
▶ | if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) croak("Shared::Arena::Histogram: this histogram is released"); |
0 |
2 |
2 |
|
| 158 |
330 |
▶ | for (i = 0; i < h->nbuckets; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < h->nbuckets; i++) { |
328 |
2 |
330 |
|
| 159 |
328 |
| uint64_t n = sa_at_load64_acq(&h->buckets[i]); |
| 160 |
|
| AV *av; |
| 161 |
328 |
▶ | if (!n) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) continue; |
326 |
2 |
328 |
|
| 162 |
2 |
| av = newAV(); |
| 163 |
2 |
| av_push(av, newSVuv((UV)sa_hist_low(i, h->sigbits))); |
| 164 |
2 |
| av_push(av, newSVuv((UV)sa_hist_high(i, h->sigbits))); |
| 165 |
2 |
| av_push(av, newSVuv((UV)n)); |
| 166 |
2 |
▶ | mXPUSHs(newRV_noinc((SV *)av)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newRV_noinc((SV *)av)); |
0 |
2 |
2 |
|
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| void |
| 170 |
|
| sah_DESTROY(self) |
| 171 |
|
| SV *self |
| 172 |
|
| PREINIT: |
| 173 |
|
| sa_hist *h; |
| 174 |
|
| CODE: |
| 175 |
53 |
| h = SA_SELF(sa_hist, self); |
| 176 |
53 |
▶ | if (h) { sa_hist_free(h); sv_setiv(SvRV(self), 0); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (h) { sa_hist_free(h); sv_setiv(SvRV(self), 0); } |
53 |
0 |
53 |
|