-
Notifications
You must be signed in to change notification settings - Fork 18
/
circonusllhist.go
1150 lines (1025 loc) · 26.5 KB
/
circonusllhist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016, Circonus, Inc. All rights reserved.
// See the LICENSE file.
// Package circllhist provides an implementation of Circonus' fixed log-linear
// histogram data structure. This allows tracking of histograms in a
// composable way such that accurate error can be reasoned about.
package circonusllhist
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"math"
"strconv"
"strings"
"sync"
"time"
)
const (
defaultHistSize = uint16(100)
)
var powerOfTen = [...]float64{
1, 10, 100, 1000, 10000, 100000, 1e+06, 1e+07, 1e+08, 1e+09, 1e+10,
1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20,
1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30,
1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40,
1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50,
1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60,
1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70,
1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80,
1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90,
1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100,
1e+101, 1e+102, 1e+103, 1e+104, 1e+105, 1e+106, 1e+107, 1e+108, 1e+109,
1e+110, 1e+111, 1e+112, 1e+113, 1e+114, 1e+115, 1e+116, 1e+117, 1e+118,
1e+119, 1e+120, 1e+121, 1e+122, 1e+123, 1e+124, 1e+125, 1e+126, 1e+127,
1e-128, 1e-127, 1e-126, 1e-125, 1e-124, 1e-123, 1e-122, 1e-121, 1e-120,
1e-119, 1e-118, 1e-117, 1e-116, 1e-115, 1e-114, 1e-113, 1e-112, 1e-111,
1e-110, 1e-109, 1e-108, 1e-107, 1e-106, 1e-105, 1e-104, 1e-103, 1e-102,
1e-101, 1e-100, 1e-99, 1e-98, 1e-97, 1e-96,
1e-95, 1e-94, 1e-93, 1e-92, 1e-91, 1e-90, 1e-89, 1e-88, 1e-87, 1e-86,
1e-85, 1e-84, 1e-83, 1e-82, 1e-81, 1e-80, 1e-79, 1e-78, 1e-77, 1e-76,
1e-75, 1e-74, 1e-73, 1e-72, 1e-71, 1e-70, 1e-69, 1e-68, 1e-67, 1e-66,
1e-65, 1e-64, 1e-63, 1e-62, 1e-61, 1e-60, 1e-59, 1e-58, 1e-57, 1e-56,
1e-55, 1e-54, 1e-53, 1e-52, 1e-51, 1e-50, 1e-49, 1e-48, 1e-47, 1e-46,
1e-45, 1e-44, 1e-43, 1e-42, 1e-41, 1e-40, 1e-39, 1e-38, 1e-37, 1e-36,
1e-35, 1e-34, 1e-33, 1e-32, 1e-31, 1e-30, 1e-29, 1e-28, 1e-27, 1e-26,
1e-25, 1e-24, 1e-23, 1e-22, 1e-21, 1e-20, 1e-19, 1e-18, 1e-17, 1e-16,
1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-09, 1e-08, 1e-07, 1e-06,
1e-05, 0.0001, 0.001, 0.01, 0.1,
}
// A Bracket is a part of a cumulative distribution.
type bin struct {
count uint64
val int8
exp int8
}
func newBinRaw(val int8, exp int8, count uint64) *bin {
return &bin{
count: count,
val: val,
exp: exp,
}
}
// func newBin() *bin {
// return newBinRaw(0, 0, 0)
// }
func newBinFromFloat64(d float64) *bin {
hb := newBinRaw(0, 0, 0)
hb.setFromFloat64(d)
return hb
}
type fastL2 struct {
l1, l2 int
}
func (hb *bin) newFastL2() fastL2 {
return fastL2{l1: int(uint8(hb.exp)), l2: int(uint8(hb.val))}
}
func (hb *bin) setFromFloat64(d float64) *bin { //nolint:unparam
hb.val = -1
if math.IsInf(d, 0) || math.IsNaN(d) {
return hb
}
if d == 0.0 {
hb.val = 0
return hb
}
sign := 1
if math.Signbit(d) {
sign = -1
}
d = math.Abs(d)
bigExp := int(math.Floor(math.Log10(d)))
hb.exp = int8(bigExp)
if int(hb.exp) != bigExp { // rolled
hb.exp = 0
if bigExp < 0 {
hb.val = 0
}
return hb
}
d /= hb.powerOfTen()
d *= 10
hb.val = int8(sign * int(math.Floor(d+1e-13)))
if hb.val == 100 || hb.val == -100 {
if hb.exp < 127 {
hb.val /= 10
hb.exp++
} else {
hb.val = 0
hb.exp = 0
}
}
if hb.val == 0 {
hb.exp = 0
return hb
}
if !((hb.val >= 10 && hb.val < 100) ||
(hb.val <= -10 && hb.val > -100)) {
hb.val = -1
hb.exp = 0
}
return hb
}
func (hb *bin) powerOfTen() float64 {
idx := int(uint8(hb.exp))
return powerOfTen[idx]
}
func (hb *bin) isNaN() bool {
// aval := abs(hb.val)
aval := hb.val
if aval < 0 {
aval = -aval
}
if 99 < aval { // in [100... ]: nan
return true
}
if 9 < aval { // in [10 - 99]: valid range
return false
}
if 0 < aval { // in [1 - 9 ]: nan
return true
}
if 0 == aval { // in [0] : zero bucket
return false
}
return false
}
func (hb *bin) value() float64 {
if hb.isNaN() {
return math.NaN()
}
if hb.val < 10 && hb.val > -10 {
return 0.0
}
return (float64(hb.val) / 10.0) * hb.powerOfTen()
}
func (hb *bin) binWidth() float64 {
if hb.isNaN() {
return math.NaN()
}
if hb.val < 10 && hb.val > -10 {
return 0.0
}
return hb.powerOfTen() / 10.0
}
func (hb *bin) midpoint() float64 {
if hb.isNaN() {
return math.NaN()
}
out := hb.value()
if out == 0 {
return 0
}
interval := hb.binWidth()
if out < 0 {
interval *= -1
}
return out + interval/2.0
}
func (hb *bin) left() float64 {
if hb.isNaN() {
return math.NaN()
}
out := hb.value()
if out >= 0 {
return out
}
return out - hb.binWidth()
}
func (hb *bin) compare(h2 *bin) int {
var v1, v2 int
// 1) slide exp positive
// 2) shift by size of val multiple by (val != 0)
// 3) then add or subtract val accordingly
if hb.val >= 0 {
v1 = ((int(hb.exp)+256)<<8)*(((int(hb.val)|(^int(hb.val)+1))>>8)&1) + int(hb.val)
} else {
v1 = ((int(hb.exp)+256)<<8)*(((int(hb.val)|(^int(hb.val)+1))>>8)&1) - int(hb.val)
}
if h2.val >= 0 {
v2 = ((int(h2.exp)+256)<<8)*(((int(h2.val)|(^int(h2.val)+1))>>8)&1) + int(h2.val)
} else {
v2 = ((int(h2.exp)+256)<<8)*(((int(h2.val)|(^int(h2.val)+1))>>8)&1) - int(h2.val)
}
// return the difference
return v2 - v1
}
// Histogram tracks values are two decimal digits of precision
// with a bounded error that remains bounded upon composition.
type Histogram struct {
bvs []bin
lookup [][]uint16
mutex sync.RWMutex
used uint16
useLookup bool
useLocks bool
}
//nolint:golint,revive
const (
BVL1, BVL1MASK uint64 = iota, 0xff << (8 * iota)
BVL2, BVL2MASK
BVL3, BVL3MASK
BVL4, BVL4MASK
BVL5, BVL5MASK
BVL6, BVL6MASK
BVL7, BVL7MASK
BVL8, BVL8MASK
)
func getBytesRequired(val uint64) int8 {
if 0 != (BVL8MASK|BVL7MASK|BVL6MASK|BVL5MASK)&val {
if 0 != BVL8MASK&val {
return int8(BVL8)
}
if 0 != BVL7MASK&val {
return int8(BVL7)
}
if 0 != BVL6MASK&val {
return int8(BVL6)
}
if 0 != BVL5MASK&val {
return int8(BVL5)
}
} else {
if 0 != BVL4MASK&val {
return int8(BVL4)
}
if 0 != BVL3MASK&val {
return int8(BVL3)
}
if 0 != BVL2MASK&val {
return int8(BVL2)
}
}
return int8(BVL1)
}
func writeBin(out io.Writer, in bin) (err error) {
err = binary.Write(out, binary.BigEndian, in.val)
if err != nil {
return
}
err = binary.Write(out, binary.BigEndian, in.exp)
if err != nil {
return
}
var tgtType = getBytesRequired(in.count)
err = binary.Write(out, binary.BigEndian, tgtType)
if err != nil {
return
}
var bcount = make([]uint8, 8)
b := bcount[0 : tgtType+1]
for i := tgtType; i >= 0; i-- {
b[i] = uint8(uint64(in.count>>(uint8(i)*8)) & 0xff) //nolint:unconvert
}
err = binary.Write(out, binary.BigEndian, b)
if err != nil {
return
}
return
}
func readBin(in io.Reader) (bin, error) {
var out bin
err := binary.Read(in, binary.BigEndian, &out.val)
if err != nil {
return out, fmt.Errorf("read: %w", err)
}
err = binary.Read(in, binary.BigEndian, &out.exp)
if err != nil {
return out, fmt.Errorf("read: %w", err)
}
var bvl uint8
err = binary.Read(in, binary.BigEndian, &bvl)
if err != nil {
return out, fmt.Errorf("read: %w", err)
}
if bvl > uint8(BVL8) {
return out, fmt.Errorf("encoding error: bvl value is greater than max allowable") //nolint:goerr113
}
bcount := make([]byte, 8)
b := bcount[0 : bvl+1]
err = binary.Read(in, binary.BigEndian, b)
if err != nil {
return out, fmt.Errorf("read: %w", err)
}
count := uint64(0)
for i := int(bvl + 1); i >= 0; i-- {
count |= uint64(bcount[i]) << (uint8(i) * 8)
}
out.count = count
return out, nil
}
func Deserialize(in io.Reader) (h *Histogram, err error) {
return DeserializeWithOptions(in)
}
func DeserializeWithOptions(in io.Reader, options ...Option) (h *Histogram, err error) {
var nbin int16
err = binary.Read(in, binary.BigEndian, &nbin)
if err != nil {
return
}
options = append(options, Size(uint16(nbin)))
h = New(options...)
for ii := int16(0); ii < nbin; ii++ {
bb, err := readBin(in)
if err != nil {
return h, err
}
h.insertBin(&bb, int64(bb.count))
}
return h, nil
}
func (h *Histogram) Serialize(w io.Writer) error {
var nbin int16
for i := range h.bvs {
if h.bvs[i].count != 0 {
nbin++
}
}
if err := binary.Write(w, binary.BigEndian, nbin); err != nil {
return fmt.Errorf("write: %w", err)
}
for _, bv := range h.bvs {
if bv.count != 0 {
if err := writeBin(w, bv); err != nil {
return err
}
}
}
return nil
}
func (h *Histogram) SerializeB64(w io.Writer) error {
buf := bytes.NewBuffer([]byte{})
if err := h.Serialize(buf); err != nil {
return err
}
encoder := base64.NewEncoder(base64.StdEncoding, w)
if _, err := encoder.Write(buf.Bytes()); err != nil {
return fmt.Errorf("b64 encode write: %w", err)
}
if err := encoder.Close(); err != nil {
return fmt.Errorf("b64 encoder close: %w", err)
}
return nil
}
// Options are exposed options for initializing a histogram.
type Options struct {
// Size is the number of bins.
Size uint16
// UseLocks determines if the histogram should use locks
UseLocks bool
// UseLookup determines if the histogram should use a lookup table for bins
UseLookup bool
}
// Option knows how to mutate the Options to change initialization.
type Option func(*Options)
// NoLocks configures a histogram to not use locks.
func NoLocks() Option {
return func(options *Options) {
options.UseLocks = false
}
}
// NoLookup configures a histogram to not use a lookup table for bins.
// This is an appropriate option to use when the data set being operated
// over contains a large number of individual histograms and the insert
// speed into any histogram is not of the utmost importance. This option
// reduces the baseline memory consumption of one Histogram by at least
// 0.5kB and up to 130kB while increasing the insertion time by ~20%.
func NoLookup() Option {
return func(options *Options) {
options.UseLookup = false
}
}
// Size configures a histogram to initialize a specific number of bins.
// When more bins are required, allocations increase linearly by the default
// size (100).
func Size(size uint16) Option {
return func(options *Options) {
options.Size = size
}
}
// New returns a new Histogram, respecting the passed Options.
func New(options ...Option) *Histogram {
o := Options{
Size: defaultHistSize,
UseLocks: true,
UseLookup: true,
}
for _, opt := range options {
opt(&o)
}
h := &Histogram{
used: 0,
bvs: make([]bin, o.Size),
useLocks: o.UseLocks,
useLookup: o.UseLookup,
}
if h.useLookup {
h.lookup = make([][]uint16, 256)
}
return h
}
// NewNoLocks returns a new histogram not using locks.
// Deprecated: use New(NoLocks()) instead.
func NewNoLocks() *Histogram {
return New(NoLocks())
}
// NewFromStrings returns a Histogram created from DecStrings strings.
func NewFromStrings(strs []string, locks bool) (*Histogram, error) {
bin, err := stringsToBin(strs)
if err != nil {
return nil, err
}
return newFromBins(bin, locks), nil
}
// NewFromBins returns a Histogram created from a bins struct slice.
func newFromBins(bins []bin, locks bool) *Histogram {
return &Histogram{
used: uint16(len(bins)),
bvs: bins,
useLocks: locks,
lookup: make([][]uint16, 256),
useLookup: true,
}
}
// Max returns the approximate maximum recorded value.
func (h *Histogram) Max() float64 {
return h.ValueAtQuantile(1.0)
}
// Min returns the approximate minimum recorded value.
func (h *Histogram) Min() float64 {
return h.ValueAtQuantile(0.0)
}
// Mean returns the approximate arithmetic mean of the recorded values.
func (h *Histogram) Mean() float64 {
return h.ApproxMean()
}
// Count returns the number of recorded values.
func (h *Histogram) Count() uint64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
var count uint64
for _, bin := range h.bvs[0:h.used] {
if bin.isNaN() {
continue
}
count += bin.count
}
return count
}
// BinCount returns the number of used bins.
func (h *Histogram) BinCount() uint64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
binCount := h.used
return uint64(binCount)
}
// Reset forgets all bins in the histogram (they remain allocated).
func (h *Histogram) Reset() {
if h.useLocks {
h.mutex.Lock()
defer h.mutex.Unlock()
}
h.used = 0
if !h.useLookup {
return
}
for i := 0; i < 256; i++ {
if h.lookup[i] != nil {
for j := range h.lookup[i] {
h.lookup[i][j] = 0
}
}
}
}
// RecordIntScale records an integer scaler value, returning an error if the
// value is out of range.
func (h *Histogram) RecordIntScale(val int64, scale int) error {
return h.RecordIntScales(val, scale, 1)
}
// RecordValue records the given value, returning an error if the value is out
// of range.
func (h *Histogram) RecordValue(v float64) error {
return h.RecordValues(v, 1)
}
// RecordDuration records the given time.Duration in seconds, returning an error
// if the value is out of range.
func (h *Histogram) RecordDuration(v time.Duration) error {
return h.RecordIntScale(int64(v), -9)
}
// RecordCorrectedValue records the given value, correcting for stalls in the
// recording process. This only works for processes which are recording values
// at an expected interval (e.g., doing jitter analysis). Processes which are
// recording ad-hoc values (e.g., latency for incoming requests) can't take
// advantage of this.
// CH Compat.
func (h *Histogram) RecordCorrectedValue(v, expectedInterval int64) error {
if err := h.RecordValue(float64(v)); err != nil {
return err
}
if expectedInterval <= 0 || v <= expectedInterval {
return nil
}
missingValue := v - expectedInterval
for missingValue >= expectedInterval {
if err := h.RecordValue(float64(missingValue)); err != nil {
return err
}
missingValue -= expectedInterval
}
return nil
}
// find where a new bin should go.
func (h *Histogram) internalFind(hb *bin) (bool, uint16) {
if h.used == 0 {
return false, 0
}
if h.useLookup {
f2 := hb.newFastL2()
if h.lookup[f2.l1] != nil {
if idx := h.lookup[f2.l1][f2.l2]; idx != 0 {
return true, idx - 1
}
}
}
rv := -1
idx := uint16(0)
l := int(0)
r := int(h.used - 1)
for l < r {
check := (r + l) / 2
rv = h.bvs[check].compare(hb)
switch {
case rv == 0:
l = check
r = check
case rv > 0:
l = check + 1
default:
r = check - 1
}
}
if rv != 0 {
rv = h.bvs[l].compare(hb)
}
idx = uint16(l)
if rv == 0 {
return true, idx
}
if rv < 0 {
return false, idx
}
idx++
return false, idx
}
func (h *Histogram) insertBin(hb *bin, count int64) uint64 { //nolint:unparam
if h.useLocks {
h.mutex.Lock()
defer h.mutex.Unlock()
}
found, idx := h.internalFind(hb)
if !found {
count := h.insertNewBinAt(idx, hb, count)
// update the fast lookup table data after the index
h.updateFast(idx)
return count
}
return h.updateOldBinAt(idx, count)
}
func (h *Histogram) insertNewBinAt(idx uint16, hb *bin, count int64) uint64 {
h.bvs = append(h.bvs, bin{})
copy(h.bvs[idx+1:], h.bvs[idx:])
h.bvs[idx].val = hb.val
h.bvs[idx].exp = hb.exp
h.bvs[idx].count = uint64(count)
h.used++
return h.bvs[idx].count
}
func (h *Histogram) updateFast(start uint16) {
if !h.useLookup {
return
}
for i := start; i < h.used; i++ {
f2 := h.bvs[i].newFastL2()
if h.lookup[f2.l1] == nil {
h.lookup[f2.l1] = make([]uint16, 256)
}
h.lookup[f2.l1][f2.l2] = i + 1
}
}
func (h *Histogram) updateOldBinAt(idx uint16, count int64) uint64 {
var newval uint64
if count >= 0 {
newval = h.bvs[idx].count + uint64(count)
} else {
newval = h.bvs[idx].count - uint64(-count)
}
if newval < h.bvs[idx].count { // rolled
newval = ^uint64(0)
}
h.bvs[idx].count = newval
return newval - h.bvs[idx].count
}
// RecordIntScales records n occurrences of the given value, returning an error if
// the value is out of range.
func (h *Histogram) RecordIntScales(val int64, scale int, n int64) error {
sign := int64(1)
if val == 0 {
scale = 0
} else {
scale++
if val < 0 {
val = 0 - val
sign = -1
}
if val < 10 {
val *= 10
scale--
}
for val >= 100 {
val /= 10
scale++
}
}
if scale < -128 {
val = 0
scale = 0
} else if scale > 127 {
val = 0xff
scale = 0
}
val *= sign
hb := bin{val: int8(val), exp: int8(scale), count: 0}
h.insertBin(&hb, n)
return nil
}
// RecordValues records n occurrences of the given value, returning an error if
// the value is out of range.
func (h *Histogram) RecordValues(v float64, n int64) error {
var hb bin
hb.setFromFloat64(v)
h.insertBin(&hb, n)
return nil
}
// ApproxMean returns an approximate mean.
func (h *Histogram) ApproxMean() float64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
divisor := 0.0
sum := 0.0
for i := uint16(0); i < h.used; i++ {
midpoint := h.bvs[i].midpoint()
cardinality := float64(h.bvs[i].count)
divisor += cardinality
sum += midpoint * cardinality
}
if divisor == 0.0 {
return math.NaN()
}
return sum / divisor
}
// ApproxSum returns an approximate sum.
func (h *Histogram) ApproxSum() float64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
sum := 0.0
for i := uint16(0); i < h.used; i++ {
midpoint := h.bvs[i].midpoint()
cardinality := float64(h.bvs[i].count)
sum += midpoint * cardinality
}
return sum
}
func (h *Histogram) ApproxQuantile(qIn []float64) ([]float64, error) {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
qOut := make([]float64, len(qIn))
iq, ib := 0, uint16(0)
totalCnt, binWidth, binLeft, lowerCnt, upperCnt := 0.0, 0.0, 0.0, 0.0, 0.0
if len(qIn) == 0 {
return qOut, nil
}
// Make sure the requested quantiles are in order
for iq = 1; iq < len(qIn); iq++ {
if qIn[iq-1] > qIn[iq] {
return nil, fmt.Errorf("out of order") //nolint:goerr113
}
}
// Add up the bins
for ib = 0; ib < h.used; ib++ {
if !h.bvs[ib].isNaN() {
totalCnt += float64(h.bvs[ib].count)
}
}
if totalCnt == 0.0 {
return nil, fmt.Errorf("empty_histogram") //nolint:goerr113
}
for iq = 0; iq < len(qIn); iq++ {
if qIn[iq] < 0.0 || qIn[iq] > 1.0 {
return nil, fmt.Errorf("out of bound quantile") //nolint:goerr113
}
qOut[iq] = totalCnt * qIn[iq]
}
for ib = 0; ib < h.used; ib++ {
if h.bvs[ib].isNaN() {
continue
}
binWidth = h.bvs[ib].binWidth()
binLeft = h.bvs[ib].left()
lowerCnt = upperCnt
upperCnt = lowerCnt + float64(h.bvs[ib].count)
break
}
for iq = 0; iq < len(qIn); iq++ {
for ib < (h.used-1) && upperCnt < qOut[iq] {
ib++
binWidth = h.bvs[ib].binWidth()
binLeft = h.bvs[ib].left()
lowerCnt = upperCnt
upperCnt = lowerCnt + float64(h.bvs[ib].count)
}
switch {
case lowerCnt == qOut[iq]:
qOut[iq] = binLeft
case upperCnt == qOut[iq]:
qOut[iq] = binLeft + binWidth
default:
if binWidth == 0 {
qOut[iq] = binLeft
} else {
qOut[iq] = binLeft + (qOut[iq]-lowerCnt)/(upperCnt-lowerCnt)*binWidth
}
}
}
return qOut, nil
}
// ValueAtQuantile returns the recorded value at the given quantile (0..1).
func (h *Histogram) ValueAtQuantile(q float64) float64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
qIn := make([]float64, 1)
qIn[0] = q
qOut, err := h.ApproxQuantile(qIn)
if err == nil && len(qOut) == 1 {
return qOut[0]
}
return math.NaN()
}
// SignificantFigures returns the significant figures used to create the
// histogram
// CH Compat.
func (h *Histogram) SignificantFigures() int64 {
return 2
}
// Equals returns true if the two Histograms are equivalent, false if not.
func (h *Histogram) Equals(other *Histogram) bool {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
if other.useLocks {
other.mutex.RLock()
defer other.mutex.RUnlock()
}
switch {
case
h.used != other.used:
return false
default:
for i := uint16(0); i < h.used; i++ {
if h.bvs[i].compare(&other.bvs[i]) != 0 {
return false
}
if h.bvs[i].count != other.bvs[i].count {
return false
}
}
}
return true
}
// Copy creates and returns an exact copy of a histogram.
func (h *Histogram) Copy() *Histogram {
if h.useLocks {
h.mutex.Lock()
defer h.mutex.Unlock()
}
newhist := New()
newhist.used = h.used
newhist.useLocks = h.useLocks
newhist.bvs = make([]bin, len(h.bvs))
copy(h.bvs, newhist.bvs)
newhist.useLookup = h.useLookup
if h.useLookup {
newhist.lookup = make([][]uint16, 256)
for i, u := range h.lookup {
newhist.lookup[i] = append(newhist.lookup[i], u...)
}
}
return newhist
}
// FullReset resets a histogram to default empty values.
func (h *Histogram) FullReset() {
if h.useLocks {
h.mutex.Lock()
defer h.mutex.Unlock()
}
h.bvs = []bin{}
h.used = 0
if h.useLookup {
h.lookup = make([][]uint16, 256)
}
}
// CopyAndReset creates and returns an exact copy of a histogram,
// and resets it to default empty values.
func (h *Histogram) CopyAndReset() *Histogram {
newhist := h.Copy()
h.FullReset()
return newhist
}
func (h *Histogram) DecStrings() []string {
if h.useLocks {
h.mutex.Lock()
defer h.mutex.Unlock()
}
out := make([]string, h.used)
for i, bin := range h.bvs[0:h.used] {
var buffer bytes.Buffer
buffer.WriteString("H[")
buffer.WriteString(fmt.Sprintf("%3.1e", bin.value()))
buffer.WriteString("]=")
buffer.WriteString(fmt.Sprintf("%v", bin.count))
out[i] = buffer.String()
}
return out
}
// takes the output of DecStrings and deserializes it into a Bin struct slice.
func stringsToBin(strs []string) ([]bin, error) {
bins := make([]bin, len(strs))
for i, str := range strs {
// H[0.0e+00]=1
// H[0.0e+00]= <1>
countString := strings.Split(str, "=")[1]
countInt, err := strconv.ParseInt(countString, 10, 64)
if err != nil {
return nil, fmt.Errorf("parse int: %w", err)
}
// H[ <0.0> e+00]=1
valString := strings.Split(strings.Split(strings.Split(str, "=")[0], "e")[0], "[")[1]
valInt, err := strconv.ParseFloat(valString, 64)
if err != nil {
return nil, fmt.Errorf("parse float: %w", err)
}
// H[0.0e <+00> ]=1
expString := strings.Split(strings.Split(strings.Split(str, "=")[0], "e")[1], "]")[0]
expInt, err := strconv.ParseInt(expString, 10, 8)
if err != nil {
return nil, fmt.Errorf("parse int: %w", err)
}
bins[i] = *newBinRaw(int8(valInt*10), int8(expInt), uint64(countInt))
}
return bins, nil
}