{{ message }}
Repository navigation
Expand file tree
/
Copy pathhistogram.h
More file actions
485 lines (390 loc) · 17.1 KB
/
Copy pathhistogram.h
File metadata and controls
485 lines (390 loc) · 17.1 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
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
#ifndef SRC_HISTOGRAM_H_
#define SRC_HISTOGRAM_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <hdr/hdr_histogram.h>
#include "base_object.h"
#include "memory_tracker.h"
#include "node_messaging.h"
#include "util.h"
#include "uv.h"
#include "v8.h"
#include <limits>
#include <vector>
namespace node {
class ExternalReferenceRegistry;
template <typename T>
void StartHandleHistogram(v8::Local<v8::Value> receiver, bool reset);
template <typename T>
void StopHandleHistogram(v8::Local<v8::Value> receiver);
constexpr int kDefaultHistogramFigures = 3;
class Histogram : public MemoryRetainer {
public:
struct Options {
int64_t lowest = 1;
int64_t highest = std::numeric_limits<int64_t>::max();
int figures = kDefaultHistogramFigures;
double half_life = 0; // EWMA half-life in number of samples (0 = off)
int64_t threshold = 0; // SLO threshold (0 = off). When set with
// half_life, tracks EWMA error rate for values
// exceeding this threshold.
};
using HistogramPointer = DeleteFnPtr<hdr_histogram, hdr_close>;
// Factory method that returns nullptr on hdr_init failure.
static std::shared_ptr<Histogram> Create(const Options& options);
Histogram(HistogramPointer histogram, const Options& options);
virtual ~Histogram() = default;
inline bool Record(int64_t value);
inline void Reset();
inline int64_t Min() const;
inline int64_t Max() const;
inline double Mean() const;
inline double Stddev() const;
inline double EwmaMean() const;
inline double EwmaStddev() const;
inline double EwmaErrorRate() const;
inline int64_t Percentile(double percentile) const;
inline size_t Exceeds() const;
inline size_t Count() const;
inline uint64_t RecordDelta();
inline double Add(const Histogram& other);
// Iterator is a function type that takes two doubles as argument, one for
// percentile and one for the value at that percentile.
template <typename Iterator>
inline void Percentiles(Iterator&& fn) const;
inline size_t GetMemorySize() const;
// Analysis methods
inline int64_t CountAt(int64_t value) const;
double Cdf(int64_t value) const;
double Skewness() const;
double Kurtosis() const;
double KsTest(const Histogram& other) const;
double Subtract(const Histogram& other);
void PercentilesAt(const double* percentiles,
int64_t* values,
size_t length) const;
// Statistical hypothesis testing
struct WelchTestResult {
double t_statistic;
double degrees_of_freedom;
double p_value;
double ci_lower;
double ci_upper;
};
struct MannWhitneyResult {
double u_statistic;
double z_score;
double p_value;
};
struct PercentileCIResult {
int64_t value;
int64_t lower;
int64_t upper;
};
WelchTestResult WelchTest(const Histogram& other,
double confidence = 0.95) const;
MannWhitneyResult MannWhitneyTest(const Histogram& other) const;
double CohensD(const Histogram& other) const;
double CliffsD(const Histogram& other) const;
PercentileCIResult PercentileCI(double percentile,
double confidence = 0.95) const;
// CBOR-encoded export/import for histogram exchange.
std::vector<uint8_t> Export() const;
static std::shared_ptr<Histogram> Import(const uint8_t* data, size_t len);
inline bool RecordCorrected(int64_t value, int64_t expected_interval);
template <typename Iterator>
void LinearBuckets(int64_t step_size, Iterator&& fn) const;
template <typename Iterator>
void LogBuckets(int64_t first_bucket, double log_base, Iterator&& fn) const;
bool IsCompatible(const Histogram& other) const;
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Histogram)
SET_SELF_SIZE(Histogram)
private:
inline void UpdateEwma(double value);
HistogramPointer histogram_;
uint64_t prev_ = 0;
size_t exceeds_ = 0;
// EWMA state (active when ewma_alpha_ > 0)
double ewma_alpha_ = 0;
double ewma_mean_ = 0;
double ewma_variance_ = 0;
bool ewma_initialized_ = false;
// SLO error rate EWMA (active when threshold_ > 0 and ewma_alpha_ > 0)
int64_t threshold_ = 0;
double ewma_error_rate_ = 0;
RwLock mutex_;
};
class HistogramImpl {
public:
enum InternalFields {
kSlot = BaseObject::kSlot,
kImplField = HandleWrap::kInternalFieldCount,
kInternalFieldCount
};
explicit HistogramImpl(
const Histogram::Options& options = Histogram::Options {});
explicit HistogramImpl(std::shared_ptr<Histogram> histogram);
Histogram* operator->() { return histogram_.get(); }
const std::shared_ptr<Histogram>& histogram() const { return histogram_; }
static void DoReset(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCountBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMinBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMaxBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetExceedsBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCount(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMin(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMax(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMean(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetExceeds(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetStddev(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPercentile(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPercentileBigInt(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPercentiles(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPercentilesBigInt(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetSkewness(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetKurtosis(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCdf(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCountAt(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetKsTest(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPercentilesAt(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetLinearBuckets(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetLogBuckets(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetWelchTest(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetMannWhitneyTest(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCohensD(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCliffsD(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPercentileCI(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetEwmaMean(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetEwmaStddev(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetEwmaErrorRate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DoExport(const v8::FunctionCallbackInfo<v8::Value>& args);
static void DoImport(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastReset(v8::Local<v8::Value> receiver);
static double FastGetCount(v8::Local<v8::Value> receiver);
static double FastGetMin(v8::Local<v8::Value> receiver);
static double FastGetMax(v8::Local<v8::Value> receiver);
static double FastGetMean(v8::Local<v8::Value> receiver);
static double FastGetExceeds(v8::Local<v8::Value> receiver);
static double FastGetStddev(v8::Local<v8::Value> receiver);
static double FastGetPercentile(v8::Local<v8::Value> receiver,
const double percentile);
static double FastGetSkewness(v8::Local<v8::Value> receiver);
static double FastGetKurtosis(v8::Local<v8::Value> receiver);
static double FastGetCdf(v8::Local<v8::Value> receiver, const int64_t value);
static double FastGetCountAt(v8::Local<v8::Value> receiver,
const int64_t value);
static double FastGetEwmaMean(v8::Local<v8::Value> receiver);
static double FastGetEwmaStddev(v8::Local<v8::Value> receiver);
static double FastGetEwmaErrorRate(v8::Local<v8::Value> receiver);
static void AddMethods(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> tmpl);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static HistogramImpl* FromJSObject(v8::Local<v8::Value> value);
private:
std::shared_ptr<Histogram> histogram_;
static v8::CFunction fast_reset_;
static v8::CFunction fast_get_count_;
static v8::CFunction fast_get_min_;
static v8::CFunction fast_get_max_;
static v8::CFunction fast_get_mean_;
static v8::CFunction fast_get_exceeds_;
static v8::CFunction fast_get_stddev_;
static v8::CFunction fast_get_percentile_;
static v8::CFunction fast_get_skewness_;
static v8::CFunction fast_get_kurtosis_;
static v8::CFunction fast_get_cdf_;
static v8::CFunction fast_get_count_at_;
static v8::CFunction fast_get_ewma_mean_;
static v8::CFunction fast_get_ewma_stddev_;
static v8::CFunction fast_get_ewma_error_rate_;
};
class HistogramBase final : public BaseObject, public HistogramImpl {
public:
enum InternalFields {
kInternalFieldCount = std::max<uint32_t>(
BaseObject::kInternalFieldCount, HistogramImpl::kInternalFieldCount),
};
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
IsolateData* isolate_data);
static void Initialize(IsolateData* isolate_data,
v8::Local<v8::ObjectTemplate> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static BaseObjectPtr<HistogramBase> Create(
Environment* env,
const Histogram::Options& options = Histogram::Options {});
static BaseObjectPtr<HistogramBase> Create(
Environment* env,
std::shared_ptr<Histogram> histogram);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(HistogramBase)
SET_SELF_SIZE(HistogramBase)
static void Record(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RecordDelta(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RecordCorrected(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Add(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Subtract(const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastRecord(v8::Local<v8::Value> receiver, const int64_t value);
static void FastRecordDelta(v8::Local<v8::Value> receiver);
HistogramBase(
Environment* env,
v8::Local<v8::Object> wrap,
const Histogram::Options& options = Histogram::Options {});
HistogramBase(
Environment* env,
v8::Local<v8::Object> wrap,
std::shared_ptr<Histogram> histogram);
BaseObject::TransferMode GetTransferMode() const override {
return TransferMode::kCloneable;
}
std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
class HistogramTransferData : public worker::TransferData {
public:
explicit HistogramTransferData(const HistogramBase* histogram)
: histogram_(histogram->histogram()) {}
explicit HistogramTransferData(std::shared_ptr<Histogram> histogram)
: histogram_(std::move(histogram)) {}
BaseObjectPtr<BaseObject> Deserialize(
Environment* env,
v8::Local<v8::Context> context,
std::unique_ptr<worker::TransferData> self) override;
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(HistogramTransferData)
SET_SELF_SIZE(HistogramTransferData)
private:
std::shared_ptr<Histogram> histogram_;
};
private:
static v8::CFunction fast_record_;
static v8::CFunction fast_record_delta_;
};
// CRTP mixin for HandleWrap-based histograms with start/stop support.
// Provides: StartFlags enum, Start/Stop slow-path handlers, enabled_ flag,
// and InitTemplate (shared GetConstructorTemplate body).
// Derived must provide: fast_start_, fast_stop_ (static CFunction),
// FastStart, FastStop, OnStart, OnStop.
template <typename Derived>
class HandleHistogramMixin {
public:
enum class StartFlags { NONE, RESET };
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args) {
StartHandleHistogram<Derived>(args.This(), args[0]->IsTrue());
}
static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args) {
StopHandleHistogram<Derived>(args.This());
}
protected:
static void InitTemplate(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> tmpl,
uint32_t internal_field_count) {
auto instance = tmpl->InstanceTemplate();
instance->SetInternalFieldCount(internal_field_count);
HistogramImpl::AddMethods(isolate, tmpl);
SetFastMethod(isolate, instance, "start", Start, &Derived::fast_start_);
SetFastMethod(isolate, instance, "stop", Stop, &Derived::fast_stop_);
}
bool enabled_ = false;
};
class IntervalHistogram final : public HandleWrap,
public HistogramImpl,
public HandleHistogramMixin<IntervalHistogram> {
public:
enum InternalFields {
kInternalFieldCount = std::max<uint32_t>(
HandleWrap::kInternalFieldCount, HistogramImpl::kInternalFieldCount),
};
using OnInterval = void (*)(Histogram&);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
static BaseObjectPtr<IntervalHistogram> Create(
Environment* env,
int32_t interval,
OnInterval on_interval,
const Histogram::Options& options,
AsyncWrap::ProviderType type = AsyncWrap::PROVIDER_ELDHISTOGRAM);
IntervalHistogram(Environment* env,
v8::Local<v8::Object> wrap,
AsyncWrap::ProviderType type,
int32_t interval,
OnInterval on_interval,
const Histogram::Options& options = Histogram::Options{});
static void FastStart(v8::Local<v8::Value> receiver, bool reset);
static void FastStop(v8::Local<v8::Value> receiver);
BaseObject::TransferMode GetTransferMode() const override {
return TransferMode::kCloneable;
}
std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(IntervalHistogram)
SET_SELF_SIZE(IntervalHistogram)
private:
static void TimerCB(uv_timer_t* handle);
void OnStart(StartFlags flags = StartFlags::RESET);
void OnStop();
friend class HandleHistogramMixin<IntervalHistogram>;
template <typename T>
friend void StartHandleHistogram(v8::Local<v8::Value>, bool);
template <typename T>
friend void StopHandleHistogram(v8::Local<v8::Value>);
int32_t interval_ = 0;
OnInterval on_interval_ = nullptr;
uv_timer_t timer_;
static v8::CFunction fast_start_;
static v8::CFunction fast_stop_;
};
class IterationHistogram final
: public HandleWrap,
public HistogramImpl,
public HandleHistogramMixin<IterationHistogram> {
public:
enum InternalFields {
kInternalFieldCount = std::max<uint32_t>(
HandleWrap::kInternalFieldCount, HistogramImpl::kInternalFieldCount),
};
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
static BaseObjectPtr<IterationHistogram> Create(
Environment* env,
const Histogram::Options& options,
AsyncWrap::ProviderType type = AsyncWrap::PROVIDER_ELDHISTOGRAM);
IterationHistogram(Environment* env,
v8::Local<v8::Object> wrap,
AsyncWrap::ProviderType type,
const Histogram::Options& options = Histogram::Options{});
static void FastStart(v8::Local<v8::Value> receiver, bool reset);
static void FastStop(v8::Local<v8::Value> receiver);
BaseObject::TransferMode GetTransferMode() const override {
return TransferMode::kCloneable;
}
std::unique_ptr<worker::TransferData> CloneForMessaging() const override;
void Close(
v8::Local<v8::Value> close_callback = v8::Local<v8::Value>()) override;
void MemoryInfo(MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(IterationHistogram)
SET_SELF_SIZE(IterationHistogram)
private:
static void PrepareCB(uv_prepare_t* handle);
static void CheckCB(uv_check_t* handle);
void OnStart(StartFlags flags = StartFlags::RESET);
void OnStop();
friend class HandleHistogramMixin<IterationHistogram>;
template <typename T>
friend void StartHandleHistogram(v8::Local<v8::Value>, bool);
template <typename T>
friend void StopHandleHistogram(v8::Local<v8::Value>);
uv_prepare_t prepare_handle_;
uv_check_t check_handle_;
uint64_t prepare_time_ = 0;
uint64_t check_time_ = 0;
int64_t timeout_ = 0;
static v8::CFunction fast_start_;
static v8::CFunction fast_stop_;
};
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_HISTOGRAM_H_
You can’t perform that action at this time.
