aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/histogram/hdr/histogram.cpp
blob: a213d5d8fd3fe23e115ed9a454a10f082019fb0f (plain) (blame)
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
#include "histogram.h"

#include <util/generic/cast.h>
#include <util/generic/yexception.h>

#include <contrib/libs/hdr_histogram/src/hdr_histogram.h>

namespace NHdr {
    namespace {
        struct hdr_histogram* CreateHistogram(
            i64 lowestDiscernibleValue, i64 highestTrackableValue,
            i32 numberOfSignificantValueDigits, IAllocator* allocator) {
            struct hdr_histogram_bucket_config cfg;

            int r = hdr_calculate_bucket_config(
                lowestDiscernibleValue, highestTrackableValue,
                numberOfSignificantValueDigits, &cfg);
            if (r) {
                ythrow yexception() << "illegal arguments values";
            }

            size_t histogramSize = sizeof(struct hdr_histogram) +
                                   cfg.counts_len * sizeof(i64);

            IAllocator::TBlock mem = allocator->Allocate(histogramSize);
            struct hdr_histogram* histogram =
                reinterpret_cast<struct hdr_histogram*>(mem.Data);

            // memset will ensure that all of the function pointers are null
            memset(histogram, 0, histogramSize);

            hdr_init_preallocated(histogram, &cfg);
            return histogram;
        }

    }

    THistogram::THistogram(i64 lowestDiscernibleValue, i64 highestTrackableValue,
                           i32 numberOfSignificantValueDigits, IAllocator* allocator)
        : Data_(CreateHistogram(
              lowestDiscernibleValue, highestTrackableValue,
              numberOfSignificantValueDigits, allocator))
        , Allocator_(allocator)
    {
    }

    THistogram::~THistogram() {
        if (Data_) {
            size_t size = GetMemorySize();
            Allocator_->Release({Data_.Release(), size});
        }
    }

    // Histogram structure querying support -----------------------------------

    i64 THistogram::GetLowestDiscernibleValue() const {
        return Data_->lowest_trackable_value;
    }

    i64 THistogram::GetHighestTrackableValue() const {
        return Data_->highest_trackable_value;
    }

    i32 THistogram::GetNumberOfSignificantValueDigits() const {
        return Data_->significant_figures;
    }

    size_t THistogram::GetMemorySize() const {
        return hdr_get_memory_size(Data_.Get());
    }

    i32 THistogram::GetCountsLen() const {
        return Data_->counts_len;
    }

    i64 THistogram::GetTotalCount() const {
        return Data_->total_count;
    }

    // Value recording support ------------------------------------------------

    bool THistogram::RecordValue(i64 value) {
        return hdr_record_value(Data_.Get(), value);
    }

    bool THistogram::RecordValues(i64 value, i64 count) {
        return hdr_record_values(Data_.Get(), value, count);
    }

    bool THistogram::RecordValueWithExpectedInterval(i64 value, i64 expectedInterval) {
        return hdr_record_corrected_value(Data_.Get(), value, expectedInterval);
    }

    bool THistogram::RecordValuesWithExpectedInterval(
        i64 value, i64 count, i64 expectedInterval) {
        return hdr_record_corrected_values(
            Data_.Get(), value, count, expectedInterval);
    }

    i64 THistogram::Add(const THistogram& rhs) {
        return hdr_add(Data_.Get(), rhs.Data_.Get());
    }

    i64 THistogram::AddWithExpectedInterval(const THistogram& rhs, i64 expectedInterval) {
        return hdr_add_while_correcting_for_coordinated_omission(
            Data_.Get(), rhs.Data_.Get(), expectedInterval);
    }

    // Histogram Data access support ------------------------------------------

    i64 THistogram::GetMin() const {
        return hdr_min(Data_.Get());
    }

    i64 THistogram::GetMax() const {
        return hdr_max(Data_.Get());
    }

    double THistogram::GetMean() const {
        return hdr_mean(Data_.Get());
    }

    double THistogram::GetStdDeviation() const {
        return hdr_stddev(Data_.Get());
    }

    i64 THistogram::GetValueAtPercentile(double percentile) const {
        return hdr_value_at_percentile(Data_.Get(), percentile);
    }

    i64 THistogram::GetCountAtValue(i64 value) const {
        return hdr_count_at_value(Data_.Get(), value);
    }

    bool THistogram::ValuesAreEqual(i64 v1, i64 v2) const {
        return hdr_values_are_equivalent(Data_.Get(), v1, v2);
    }

    i64 THistogram::GetLowestEquivalentValue(i64 value) const {
        return hdr_lowest_equivalent_value(Data_.Get(), value);
    }

    i64 THistogram::GetHighestEquivalentValue(i64 value) const {
        return hdr_next_non_equivalent_value(Data_.Get(), value) - 1;
    }

    i64 THistogram::GetMedianEquivalentValue(i64 value) const {
        return hdr_median_equivalent_value(Data_.Get(), value);
    }

    void THistogram::Reset() {
        hdr_reset(Data_.Get());
    }

}