-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathhistogram_impl.cc
More file actions
145 lines (123 loc) · 5.49 KB
/
histogram_impl.cc
File metadata and controls
145 lines (123 loc) · 5.49 KB
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
#include "source/common/stats/histogram_impl.h"
#include <algorithm>
#include <string>
#include "source/common/common/utility.h"
#include "source/common/protobuf/utility.h"
#include "absl/strings/str_join.h"
namespace Envoy {
namespace Stats {
namespace {
const ConstSupportedBuckets default_buckets{};
}
HistogramStatisticsImpl::HistogramStatisticsImpl()
: supported_buckets_(default_buckets), computed_quantiles_(supportedQuantiles().size(), 0.0) {}
HistogramStatisticsImpl::HistogramStatisticsImpl(const histogram_t* histogram_ptr,
Histogram::Unit unit,
ConstSupportedBuckets& supported_buckets)
: supported_buckets_(supported_buckets),
computed_quantiles_(HistogramStatisticsImpl::supportedQuantiles().size(), 0.0), unit_(unit) {
refresh(histogram_ptr);
}
const std::vector<double>& HistogramStatisticsImpl::supportedQuantiles() const {
CONSTRUCT_ON_FIRST_USE(std::vector<double>,
{0, 0.25, 0.5, 0.75, 0.90, 0.95, 0.99, 0.995, 0.999, 1});
}
std::vector<uint64_t> HistogramStatisticsImpl::computeDisjointBuckets() const {
std::vector<uint64_t> buckets;
buckets.reserve(computed_buckets_.size());
uint64_t previous_computed_bucket = 0;
for (uint64_t computed_bucket : computed_buckets_) {
buckets.push_back(computed_bucket - previous_computed_bucket);
previous_computed_bucket = computed_bucket;
}
return buckets;
}
std::string HistogramStatisticsImpl::quantileSummary() const {
std::vector<std::string> summary;
const std::vector<double>& supported_quantiles = supportedQuantiles();
summary.reserve(supported_quantiles.size());
for (size_t i = 0; i < supported_quantiles.size(); ++i) {
summary.push_back(
fmt::format("P{:g}: {:g}", 100 * supported_quantiles[i], computed_quantiles_[i]));
}
return absl::StrJoin(summary, ", ");
}
std::string HistogramStatisticsImpl::bucketSummary() const {
std::vector<std::string> bucket_summary;
ConstSupportedBuckets& supported_buckets = supportedBuckets();
bucket_summary.reserve(supported_buckets.size());
for (size_t i = 0; i < supported_buckets.size(); ++i) {
bucket_summary.push_back(fmt::format("B{:g}: {}", supported_buckets[i], computed_buckets_[i]));
}
return absl::StrJoin(bucket_summary, ", ");
}
/**
* Clears the old computed values and refreshes it with values computed from passed histogram.
*/
void HistogramStatisticsImpl::refresh(const histogram_t* new_histogram_ptr) {
// Convert to double once to avoid needing to cast it on every use. Use a double
// to ensure the compiler doesn't try to convert the expression to integer math.
constexpr double percent_scale = Histogram::PercentScale;
std::fill(computed_quantiles_.begin(), computed_quantiles_.end(), 0.0);
ASSERT(supportedQuantiles().size() == computed_quantiles_.size());
hist_approx_quantile(new_histogram_ptr, supportedQuantiles().data(), supportedQuantiles().size(),
computed_quantiles_.data());
if (unit_ == Histogram::Unit::Percent) {
for (double& val : computed_quantiles_) {
val /= percent_scale;
}
}
sample_count_ = hist_sample_count(new_histogram_ptr);
sample_sum_ = hist_approx_sum(new_histogram_ptr);
if (unit_ == Histogram::Unit::Percent) {
sample_sum_ /= percent_scale;
}
computed_buckets_.clear();
ConstSupportedBuckets& supported_buckets = supportedBuckets();
computed_buckets_.reserve(supported_buckets.size());
for (auto bucket : supported_buckets) {
if (unit_ == Histogram::Unit::Percent) {
bucket *= percent_scale;
}
computed_buckets_.emplace_back(hist_approx_count_below(new_histogram_ptr, bucket));
}
out_of_bound_count_ = hist_approx_count_above(new_histogram_ptr, supported_buckets.back());
}
HistogramSettingsImpl::HistogramSettingsImpl(const envoy::config::metrics::v3::StatsConfig& config,
Server::Configuration::CommonFactoryContext& context)
: configs_([&config, &context]() {
std::vector<Config> configs;
for (const auto& matcher : config.histogram_bucket_settings()) {
std::vector<double> buckets{matcher.buckets().begin(), matcher.buckets().end()};
std::sort(buckets.begin(), buckets.end());
configs.emplace_back(Matchers::StringMatcherImpl(matcher.match(), context),
buckets.empty()
? absl::nullopt
: absl::make_optional<ConstSupportedBuckets>(std::move(buckets)),
PROTOBUF_GET_OPTIONAL_WRAPPED(matcher, bins));
}
return configs;
}()) {}
const ConstSupportedBuckets& HistogramSettingsImpl::buckets(absl::string_view stat_name) const {
for (const auto& config : configs_) {
if (config.matcher_.match(stat_name) && config.buckets_.has_value()) {
return config.buckets_.value();
}
}
return defaultBuckets();
}
absl::optional<uint32_t> HistogramSettingsImpl::bins(absl::string_view stat_name) const {
for (const auto& config : configs_) {
if (config.matcher_.match(stat_name) && config.bins_.has_value()) {
return config.bins_;
}
}
return {};
}
const ConstSupportedBuckets& HistogramSettingsImpl::defaultBuckets() {
CONSTRUCT_ON_FIRST_USE(ConstSupportedBuckets,
{0.5, 1, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 30000,
60000, 300000, 600000, 1800000, 3600000});
}
} // namespace Stats
} // namespace Envoy