Repository navigation
Expand file tree
/
Copy pathhistogram-inl.h
More file actions
231 lines (198 loc) Β· 6.07 KB
/
Copy pathhistogram-inl.h
File metadata and controls
231 lines (198 loc) Β· 6.07 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
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
#ifndef SRC_HISTOGRAM_INL_H_
#define SRC_HISTOGRAM_INL_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "histogram.h"
#include "base_object-inl.h"
#include "node_internals.h"
namespace node {
void Histogram::UpdateEwma(double value) {
// Called inside a write lock. No-op when EWMA is disabled.
if (ewma_alpha_ <= 0) return;
if (!ewma_initialized_) {
ewma_mean_ = value;
ewma_variance_ = 0;
ewma_initialized_ = true;
if (threshold_ > 0) {
ewma_error_rate_ = (value > static_cast<double>(threshold_)) ? 1.0 : 0.0;
}
return;
}
double diff = value - ewma_mean_;
ewma_mean_ += ewma_alpha_ * diff;
ewma_variance_ =
(1.0 - ewma_alpha_) * (ewma_variance_ + ewma_alpha_ * diff * diff);
// Binary EWMA for SLO error rate: feed 1 if over threshold, 0 otherwise.
if (threshold_ > 0) {
double exceeded = (value > static_cast<double>(threshold_)) ? 1.0 : 0.0;
ewma_error_rate_ += ewma_alpha_ * (exceeded - ewma_error_rate_);
}
}
void Histogram::InvalidateRecordedSnapshot() {
mutation_generation_++;
recorded_snapshot_cache_.reset();
}
void Histogram::Reset() {
RwLock::ScopedWriteLock lock(mutex_);
hdr_reset(histogram_.get());
InvalidateRecordedSnapshot();
reset_count_++;
exceeds_ = 0;
prev_ = 0;
ewma_mean_ = 0;
ewma_variance_ = 0;
ewma_error_rate_ = 0;
ewma_initialized_ = false;
}
double Histogram::Add(const Histogram& other) {
auto do_add = [&]() {
exceeds_ += other.exceeds_;
if (other.prev_ > prev_) prev_ = other.prev_;
// hdr_add merges all bucket counts and total_count internally.
const double dropped =
static_cast<double>(hdr_add(histogram_.get(), other.histogram_.get()));
InvalidateRecordedSnapshot();
return dropped;
};
// When adding a histogram to itself, a single write lock suffices.
if (this == &other) {
RwLock::ScopedWriteLock lock(mutex_);
return do_add();
}
// Write-lock this (modified), read-lock other (only read).
// Lock in pointer order to prevent deadlock.
if (this < &other) {
RwLock::ScopedWriteLock lock1(mutex_);
RwLock::ScopedReadLock lock2(other.mutex_);
return do_add();
}
RwLock::ScopedReadLock lock1(other.mutex_);
RwLock::ScopedWriteLock lock2(mutex_);
return do_add();
}
size_t Histogram::Count() const {
RwLock::ScopedReadLock lock(mutex_);
return static_cast<size_t>(histogram_->total_count);
}
size_t Histogram::Exceeds() const {
RwLock::ScopedReadLock lock(mutex_);
return exceeds_;
}
uint64_t Histogram::ResetCount() const {
RwLock::ScopedReadLock lock(mutex_);
return reset_count_;
}
int64_t Histogram::Min() const {
RwLock::ScopedReadLock lock(mutex_);
return hdr_min(histogram_.get());
}
int64_t Histogram::Max() const {
RwLock::ScopedReadLock lock(mutex_);
return hdr_max(histogram_.get());
}
double Histogram::Mean() const {
RwLock::ScopedReadLock lock(mutex_);
return hdr_mean(histogram_.get());
}
double Histogram::Stddev() const {
RwLock::ScopedReadLock lock(mutex_);
return hdr_stddev(histogram_.get());
}
double Histogram::EwmaMean() const {
RwLock::ScopedReadLock lock(mutex_);
return ewma_initialized_ ? ewma_mean_ : 0;
}
double Histogram::EwmaStddev() const {
RwLock::ScopedReadLock lock(mutex_);
return ewma_initialized_ ? std::sqrt(ewma_variance_) : 0;
}
double Histogram::EwmaErrorRate() const {
RwLock::ScopedReadLock lock(mutex_);
return ewma_initialized_ ? ewma_error_rate_ : 0;
}
int64_t Histogram::Percentile(double percentile) const {
RwLock::ScopedReadLock lock(mutex_);
CHECK_GT(percentile, 0);
CHECK_LE(percentile, 100);
return hdr_value_at_percentile(histogram_.get(), percentile);
}
template <typename Iterator>
void Histogram::Percentiles(Iterator&& fn) const {
RwLock::ScopedReadLock lock(mutex_);
hdr_iter iter;
hdr_iter_percentile_init(&iter, histogram_.get(), 1);
while (hdr_iter_next(&iter)) {
double key = iter.specifics.percentiles.percentile;
fn(key, iter.value);
}
}
int64_t Histogram::CountAt(int64_t value) const {
RwLock::ScopedReadLock lock(mutex_);
return hdr_count_at_value(histogram_.get(), value);
}
bool Histogram::RecordCorrected(int64_t value, int64_t expected_interval) {
RwLock::ScopedWriteLock lock(mutex_);
bool recorded =
hdr_record_corrected_value(histogram_.get(), value, expected_interval);
if (!recorded)
exceeds_++;
else {
InvalidateRecordedSnapshot();
UpdateEwma(static_cast<double>(value));
}
return recorded;
}
bool Histogram::Record(int64_t value) {
RwLock::ScopedWriteLock lock(mutex_);
bool recorded = hdr_record_value(histogram_.get(), value);
if (!recorded)
exceeds_++;
else {
InvalidateRecordedSnapshot();
UpdateEwma(static_cast<double>(value));
}
return recorded;
}
uint64_t Histogram::RecordDelta() {
RwLock::ScopedWriteLock lock(mutex_);
uint64_t time = uv_hrtime();
int64_t delta = 0;
if (prev_ > 0) {
CHECK_GE(time, prev_);
delta = time - prev_;
if (!hdr_record_value(histogram_.get(), delta))
exceeds_++;
else {
InvalidateRecordedSnapshot();
UpdateEwma(static_cast<double>(delta));
}
}
prev_ = time;
return delta;
}
size_t Histogram::GetMemorySize() const {
RwLock::ScopedReadLock lock(mutex_);
return hdr_get_memory_size(histogram_.get());
}
template <typename Iterator>
void Histogram::LinearBuckets(int64_t step_size, Iterator&& fn) const {
RwLock::ScopedReadLock lock(mutex_);
hdr_iter iter;
hdr_iter_linear_init(&iter, histogram_.get(), step_size);
while (hdr_iter_next(&iter)) {
fn(iter.value, iter.specifics.linear.count_added_in_this_iteration_step);
}
}
template <typename Iterator>
void Histogram::LogBuckets(int64_t first_bucket,
double log_base,
Iterator&& fn) const {
RwLock::ScopedReadLock lock(mutex_);
hdr_iter iter;
hdr_iter_log_init(&iter, histogram_.get(), first_bucket, log_base);
while (hdr_iter_next(&iter)) {
fn(iter.value, iter.specifics.log.count_added_in_this_iteration_step);
}
}
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_HISTOGRAM_INL_H_