| 1 |
|
| # Shared::Arena::CountMin - how often a key has been seen, in fixed space. |
| 2 |
|
| |
| 3 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena PREFIX = sar_ |
| 4 |
|
| |
| 5 |
|
| # $arena->countmin($name, error => 0.001, confidence => 0.99) |
| 6 |
|
| # |
| 7 |
|
| # Sized from the error you will accept and how sure you want to be, because |
| 8 |
|
| # those are the numbers a caller has. `rows` and `width` are there for one who |
| 9 |
|
| # has already done the arithmetic. |
| 10 |
|
| SV * |
| 11 |
|
| sar_countmin(self, name, ...) |
| 12 |
|
| SV *self |
| 13 |
|
| SV *name |
| 14 |
|
| PREINIT: |
| 15 |
|
| sa_region *arena; |
| 16 |
|
| sa_reg *e; |
| 17 |
|
| sa_cms *s; |
| 18 |
|
| const char *nm; |
| 19 |
|
| STRLEN nlen; |
| 20 |
31 |
| double error = 0.001, confidence = 0.99; |
| 21 |
31 |
| UV rows = 0, width = 0; |
| 22 |
31 |
| uint32_t d = 0; |
| 23 |
31 |
| uint64_t w = 0; |
| 24 |
31 |
| int err = SA_E_OK; |
| 25 |
|
| I32 i; |
| 26 |
|
| SV *obj; |
| 27 |
|
| CODE: |
| 28 |
31 |
| arena = SA_SELF(sa_region, self); |
| 29 |
31 |
▶ | if (!arena || !arena->map.base) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!arena || !arena->map.base) |
31 |
0 |
31 |
| if (!arena || !arena->map.base) |
0 |
31 |
31 |
|
| 30 |
0 |
| croak("Shared::Arena: this region is released"); |
| 31 |
31 |
| nm = SvPV(name, nlen); |
| 32 |
76 |
▶ | for (i = 2; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 2; i + 1 < items; i += 2) { |
45 |
31 |
76 |
|
| 33 |
45 |
| const char *o = SvPV_nolen(ST(i)); |
| 34 |
45 |
▶ | if (strEQ(o, "error")) error = SvNV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(o, "error")) error = SvNV(ST(i + 1)); |
24 |
21 |
45 |
|
| 35 |
21 |
▶ | else if (strEQ(o, "confidence")) confidence = SvNV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "confidence")) confidence = SvNV(ST(i + 1)); |
7 |
14 |
21 |
|
| 36 |
14 |
▶ | else if (strEQ(o, "rows")) rows = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "rows")) rows = SvUV(ST(i + 1)); |
7 |
7 |
14 |
|
| 37 |
7 |
▶ | else if (strEQ(o, "width")) width = SvUV(ST(i + 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(o, "width")) width = SvUV(ST(i + 1)); |
7 |
0 |
7 |
|
| 38 |
|
| } |
| 39 |
31 |
▶ | if (rows || width) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (rows || width) { |
24 |
7 |
31 |
| if (rows || width) { |
0 |
24 |
24 |
|
| 40 |
7 |
▶ | d = rows ? (uint32_t)rows : 5; |
Branch: 1 branch
| Branch | True | False | Total |
| d = rows ? (uint32_t)rows : 5; |
7 |
0 |
7 |
|
| 41 |
7 |
▶ | w = sa_cms_pow2(width ? (uint64_t)width : 2048); |
Branch: 1 branch
| Branch | True | False | Total |
| w = sa_cms_pow2(width ? (uint64_t)width : 2048); |
7 |
0 |
7 |
|
| 42 |
7 |
▶ | if (d > SA_CMS_MAX_ROWS) d = SA_CMS_MAX_ROWS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d > SA_CMS_MAX_ROWS) d = SA_CMS_MAX_ROWS; |
0 |
7 |
7 |
|
| 43 |
|
| } |
| 44 |
|
| else { |
| 45 |
24 |
| sa_cms_size(error, confidence, &d, &w); |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
31 |
| e = sa_carve(arena, nm, (size_t)nlen, sa_cms_bytes(d, w), |
| 49 |
|
| SA_T_CMS, &err); |
| 50 |
31 |
▶ | if (!e) croak("Shared::Arena: the sketch '%s' %s", nm, sa_strerror(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) croak("Shared::Arena: the sketch '%s' %s", nm, sa_strerror(err)); |
1 |
30 |
31 |
|
| 51 |
|
| |
| 52 |
30 |
| s = sa_cms_bind(arena, e, d, w, &err); |
| 53 |
30 |
▶ | if (!s) croak("Shared::Arena: the sketch '%s' %s", nm, sa_strerror(err)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena: the sketch '%s' %s", nm, sa_strerror(err)); |
0 |
30 |
30 |
|
| 54 |
|
| |
| 55 |
30 |
| obj = newSV(0); |
| 56 |
30 |
| sv_setref_pv(obj, "Shared::Arena::CountMin", (void *)s); |
| 57 |
30 |
| sv_magicext(SvRV(obj), SvRV(self), PERL_MAGIC_ext, NULL, NULL, 0); |
| 58 |
30 |
| SvREFCNT_inc(SvRV(self)); |
| 59 |
30 |
| RETVAL = obj; |
| 60 |
|
| OUTPUT: |
| 61 |
|
| RETVAL |
| 62 |
|
| |
| 63 |
|
| MODULE = Shared::Arena PACKAGE = Shared::Arena::CountMin PREFIX = sacm_ |
| 64 |
|
| |
| 65 |
|
| # Count one, or `n`, and hand back what the sketch now believes. Returning the |
| 66 |
|
| # estimate costs nothing - the numbers are already in hand - and it is what a |
| 67 |
|
| # caller asking "has this key just crossed my threshold" wants. |
| 68 |
|
| UV |
| 69 |
|
| sacm_add(self, key, ...) |
| 70 |
|
| SV *self |
| 71 |
|
| SV *key |
| 72 |
|
| PREINIT: |
| 73 |
|
| sa_cms *s; |
| 74 |
|
| const char *k; |
| 75 |
|
| STRLEN klen; |
| 76 |
1 |
▶ | UV n = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| UV n = 1; |
1 |
0 |
1 |
|
| 77 |
|
| CODE: |
| 78 |
1 |
| s = SA_SELF(sa_cms, self); |
| 79 |
1 |
▶ | if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
0 |
1 |
1 |
|
| 80 |
1 |
▶ | if (items > 2) n = SvUV(ST(2)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) n = SvUV(ST(2)); |
1 |
0 |
1 |
|
| 81 |
1 |
▶ | if (n < 1) n = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 1) n = 1; |
0 |
1 |
1 |
|
| 82 |
1 |
| k = SvPV(key, klen); |
| 83 |
|
| #if SA_HAVE_ATOMICS |
| 84 |
1 |
| RETVAL = (UV)sa_cms_add(s, k, (uint32_t)klen, (uint64_t)n); |
| 85 |
|
| #else |
| 86 |
|
| RETVAL = 0; |
| 87 |
|
| #endif |
| 88 |
|
| OUTPUT: |
| 89 |
|
| RETVAL |
| 90 |
|
| |
| 91 |
|
| # At most `error` too high, never too low. |
| 92 |
|
| UV |
| 93 |
|
| sacm_estimate(self, key) |
| 94 |
|
| SV *self |
| 95 |
|
| SV *key |
| 96 |
|
| PREINIT: |
| 97 |
|
| sa_cms *s; |
| 98 |
|
| const char *k; |
| 99 |
|
| STRLEN klen; |
| 100 |
|
| CODE: |
| 101 |
0 |
| s = SA_SELF(sa_cms, self); |
| 102 |
0 |
▶ | if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
0 |
0 |
0 |
|
| 103 |
0 |
| k = SvPV(key, klen); |
| 104 |
|
| #if SA_HAVE_ATOMICS |
| 105 |
0 |
| RETVAL = (UV)sa_cms_estimate(s, k, (uint32_t)klen); |
| 106 |
|
| #else |
| 107 |
|
| RETVAL = 0; |
| 108 |
|
| #endif |
| 109 |
|
| OUTPUT: |
| 110 |
|
| RETVAL |
| 111 |
|
| |
| 112 |
|
| void |
| 113 |
|
| sacm_reset(self) |
| 114 |
|
| SV *self |
| 115 |
|
| PREINIT: |
| 116 |
|
| sa_cms *s; |
| 117 |
|
| CODE: |
| 118 |
1 |
| s = SA_SELF(sa_cms, self); |
| 119 |
1 |
▶ | if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
0 |
1 |
1 |
|
| 120 |
|
| #if SA_HAVE_ATOMICS |
| 121 |
1 |
| sa_cms_reset(s); |
| 122 |
|
| #endif |
| 123 |
|
| |
| 124 |
|
| UV |
| 125 |
|
| sacm_rows(self) |
| 126 |
|
| SV *self |
| 127 |
|
| PREINIT: |
| 128 |
|
| sa_cms *s; |
| 129 |
|
| CODE: |
| 130 |
5 |
| s = SA_SELF(sa_cms, self); |
| 131 |
5 |
▶ | if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
0 |
5 |
5 |
|
| 132 |
5 |
▶ | RETVAL = (UV)s->rows; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)s->rows; |
0 |
5 |
5 |
|
| 133 |
|
| OUTPUT: |
| 134 |
|
| RETVAL |
| 135 |
|
| |
| 136 |
|
| UV |
| 137 |
|
| sacm_width(self) |
| 138 |
|
| SV *self |
| 139 |
|
| PREINIT: |
| 140 |
|
| sa_cms *s; |
| 141 |
|
| CODE: |
| 142 |
5 |
| s = SA_SELF(sa_cms, self); |
| 143 |
5 |
▶ | if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
0 |
5 |
5 |
|
| 144 |
5 |
▶ | RETVAL = (UV)s->width; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)s->width; |
0 |
5 |
5 |
|
| 145 |
|
| OUTPUT: |
| 146 |
|
| RETVAL |
| 147 |
|
| |
| 148 |
|
| # rows / width / total / adds / error / bytes |
| 149 |
|
| # |
| 150 |
|
| # `error` is the number that makes an estimate mean anything: an answer may be |
| 151 |
|
| # that much too high. It is a fraction of the TOTAL, not of the key's own count, |
| 152 |
|
| # so a sketch accurate enough to find the loudest client says nothing useful |
| 153 |
|
| # about one seen twice. |
| 154 |
|
| void |
| 155 |
|
| sacm_stats(self) |
| 156 |
|
| SV *self |
| 157 |
|
| PREINIT: |
| 158 |
|
| sa_cms *s; |
| 159 |
|
| PPCODE: |
| 160 |
17 |
| s = SA_SELF(sa_cms, self); |
| 161 |
17 |
▶ | if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Shared::Arena::CountMin: this sketch is released"); |
0 |
17 |
17 |
|
| 162 |
17 |
▶ | EXTEND(SP, 12); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 12); |
0 |
17 |
17 |
|
| 163 |
17 |
| mPUSHp("rows", 4); mPUSHu((UV)s->rows); |
| 164 |
17 |
| mPUSHp("width", 5); mPUSHu((UV)s->width); |
| 165 |
17 |
| mPUSHp("bytes", 5); mPUSHu((UV)sa_cms_bytes(s->rows, s->width)); |
| 166 |
|
| #if SA_HAVE_ATOMICS |
| 167 |
17 |
| mPUSHp("total", 5); mPUSHu((UV)sa_at_load64_acq(&s->hdr->total)); |
| 168 |
17 |
| mPUSHp("adds", 4); mPUSHu((UV)sa_at_load64_acq(&s->hdr->adds)); |
| 169 |
17 |
| mPUSHp("error", 5); mPUSHu((UV)sa_cms_error(s)); |
| 170 |
|
| #endif |
| 171 |
|
| |
| 172 |
|
| void |
| 173 |
|
| sacm_DESTROY(self) |
| 174 |
|
| SV *self |
| 175 |
|
| PREINIT: |
| 176 |
|
| sa_cms *s; |
| 177 |
|
| CODE: |
| 178 |
30 |
| s = SA_SELF(sa_cms, self); |
| 179 |
30 |
▶ | if (s) { sa_cms_free(s); sv_setiv(SvRV(self), 0); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) { sa_cms_free(s); sv_setiv(SvRV(self), 0); } |
30 |
0 |
30 |
|