Repository navigation
Expand file tree
/
Copy pathhistogram.cc
More file actions
3358 lines (2994 loc) Β· 120 KB
/
Copy pathhistogram.cc
File metadata and controls
3358 lines (2994 loc) Β· 120 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
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
#include "histogram.h" // NOLINT(build/include_inline)
#include "base_object-inl.h"
#include "histogram-inl.h"
#include "memory_tracker-inl.h"
#include "node_debug.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "threadpoolwork-inl.h"
#include "util.h"
#include "v8-typed-array.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <numbers>
#include <vector>
namespace node {
using v8::Array;
using v8::ArrayBuffer;
using v8::BigInt;
using v8::CFunction;
using v8::Context;
using v8::Exception;
using v8::FastApiCallbackOptions;
using v8::Float64Array;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Global;
using v8::HandleScope;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::Map;
using v8::Name;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::Promise;
using v8::String;
using v8::Uint32;
using v8::Uint8Array;
using v8::Value;
template <typename T>
void StartHandleHistogram(Local<Value> receiver, bool reset) {
T* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, receiver);
histogram->OnStart(reset ? T::StartFlags::RESET : T::StartFlags::NONE);
}
template <typename T>
void StopHandleHistogram(Local<Value> receiver) {
T* histogram;
ASSIGN_OR_RETURN_UNWRAP(&histogram, receiver);
histogram->OnStop();
}
Histogram::Histogram(HistogramPointer histogram, const Options& options)
: histogram_(std::move(histogram)) {
// alpha = 1 - 2^(-1/halfLife). With halfLife <= 0, EWMA is disabled.
if (options.half_life > 0) {
ewma_alpha_ = 1.0 - std::exp(-std::log(2.0) / options.half_life);
}
threshold_ = options.threshold;
}
std::shared_ptr<Histogram> Histogram::Create(const Options& options) {
hdr_histogram* histogram;
if (hdr_init(options.lowest, options.highest, options.figures, &histogram) !=
0) {
return {};
}
return std::make_shared<Histogram>(HistogramPointer(histogram), options);
}
namespace {
// Copies the recorded data of `source` into `target`, which must have been
// initialized with the same layout. The caller must hold a lock that prevents
// `source` from being modified during the copy.
void CopyRecordedData(hdr_histogram* target, const hdr_histogram* source) {
CHECK_EQ(target->counts_len, source->counts_len);
target->min_value = source->min_value;
target->max_value = source->max_value;
target->normalizing_index_offset = source->normalizing_index_offset;
target->conversion_ratio = source->conversion_ratio;
target->total_count = source->total_count;
std::memcpy(target->counts,
source->counts,
source->counts_len * sizeof(*source->counts));
}
} // namespace
std::shared_ptr<Histogram> Histogram::CreateWithSameLayout() const {
// The layout is fixed when the histogram is created, so it can be read
// without holding the lock.
hdr_histogram* histogram;
if (hdr_init(histogram_->lowest_discernible_value,
histogram_->highest_trackable_value,
histogram_->significant_figures,
&histogram) != 0) {
return {};
}
return std::make_shared<Histogram>(HistogramPointer(histogram), Options{});
}
std::shared_ptr<Histogram> Histogram::Clone() const {
std::shared_ptr<Histogram> clone = CreateWithSameLayout();
if (!clone) return {};
// Every member that holds recorded or statistical state must be copied
// here. The recorded snapshot cache is not copied; the clone builds its own
// on demand.
RwLock::ScopedReadLock lock(mutex_);
CopyRecordedData(clone->histogram_.get(), histogram_.get());
clone->prev_ = prev_;
clone->exceeds_ = exceeds_;
clone->reset_count_ = reset_count_;
clone->ewma_alpha_ = ewma_alpha_;
clone->ewma_mean_ = ewma_mean_;
clone->ewma_variance_ = ewma_variance_;
clone->ewma_initialized_ = ewma_initialized_;
clone->threshold_ = threshold_;
clone->ewma_error_rate_ = ewma_error_rate_;
return clone;
}
std::shared_ptr<Histogram> Histogram::Diff(const Histogram& other,
DiffError* error) const {
// Counts are subtracted index by index, so both histograms must map values
// to the same indexes. None of these fields change after creation.
if (!IsCompatible(other) || histogram_->normalizing_index_offset !=
other.histogram_->normalizing_index_offset) {
*error = DiffError::kIncompatible;
return {};
}
std::shared_ptr<Histogram> diff = CreateWithSameLayout();
if (!diff) {
*error = DiffError::kOutOfMemory;
return {};
}
// Only the recorded values and the exceeds count carry over. EWMA and timing
// state cannot be subtracted.
uint64_t reset_count;
{
RwLock::ScopedReadLock lock(mutex_);
CopyRecordedData(diff->histogram_.get(), histogram_.get());
diff->exceeds_ = exceeds_;
reset_count = reset_count_;
}
// `diff` is not shared yet, so only the lock of `other` is needed from here
// on. Never holding both locks at once avoids lock ordering issues.
RwLock::ScopedReadLock lock(other.mutex_);
if (reset_count != other.reset_count_) {
*error = DiffError::kReset;
return {};
}
if (diff->exceeds_ < other.exceeds_) {
*error = DiffError::kNotEarlier;
return {};
}
hdr_histogram* target = diff->histogram_.get();
const hdr_histogram* source = other.histogram_.get();
for (int32_t i = 0; i < target->counts_len; i++) {
if (target->counts[i] < source->counts[i]) {
*error = DiffError::kNotEarlier;
return {};
}
target->counts[i] -= source->counts[i];
}
diff->exceeds_ -= other.exceeds_;
hdr_reset_internal_counters(target);
*error = DiffError::kNone;
return diff;
}
void Histogram::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("histogram", GetMemorySize());
tracker->TrackFieldWithSize("qrde_snapshot",
GetCachedRecordedSnapshotMemorySize());
}
bool Histogram::IsCompatible(const Histogram& other) const {
return histogram_->counts_len == other.histogram_->counts_len &&
histogram_->lowest_discernible_value ==
other.histogram_->lowest_discernible_value &&
histogram_->highest_trackable_value ==
other.histogram_->highest_trackable_value &&
histogram_->significant_figures ==
other.histogram_->significant_figures;
}
Histogram::RecordedSnapshot Histogram::BuildRecordedSnapshot(
const hdr_histogram* histogram) {
RecordedSnapshot snapshot;
snapshot.total_count = histogram->total_count;
if (snapshot.total_count == 0) return snapshot;
const size_t occupied =
std::count_if(histogram->counts,
histogram->counts + histogram->counts_len,
[](int64_t count) { return count != 0; });
snapshot.buckets.reserve(occupied);
hdr_iter iter;
hdr_iter_recorded_init(&iter, histogram);
while (hdr_iter_next(&iter)) {
snapshot.buckets.push_back({
static_cast<double>(iter.median_equivalent_value),
static_cast<double>(
hdr_size_of_equivalent_value_range(histogram, iter.value)),
iter.count,
iter.cumulative_count,
});
}
return snapshot;
}
Histogram::RecordedSnapshotSource Histogram::GetRecordedSnapshotSource(
bool use_cache) const {
RecordedSnapshotSource source;
int64_t lowest;
int64_t highest;
int figures;
{
RwLock::ScopedReadLock lock(mutex_);
source.generation = mutation_generation_;
if (use_cache && recorded_snapshot_cache_) {
source.snapshot = recorded_snapshot_cache_;
source.cache_hit = true;
return source;
}
if (histogram_->total_count == 0) {
source.snapshot = std::make_shared<const RecordedSnapshot>();
return source;
}
lowest = histogram_->lowest_discernible_value;
highest = histogram_->highest_trackable_value;
figures = histogram_->significant_figures;
}
hdr_histogram* copy;
if (hdr_init(lowest, highest, figures, ©) != 0) return source;
source.histogram.reset(copy);
RwLock::ScopedReadLock lock(mutex_);
source.generation = mutation_generation_;
if (use_cache && recorded_snapshot_cache_) {
source.histogram.reset();
source.snapshot = recorded_snapshot_cache_;
source.cache_hit = true;
return source;
}
if (histogram_->total_count == 0) {
source.histogram.reset();
source.snapshot = std::make_shared<const RecordedSnapshot>();
return source;
}
CopyRecordedData(source.histogram.get(), histogram_.get());
return source;
}
void Histogram::CacheRecordedSnapshot(
uint64_t generation, std::shared_ptr<const RecordedSnapshot> snapshot) {
RwLock::ScopedWriteLock lock(mutex_);
if (generation == mutation_generation_) {
recorded_snapshot_cache_ = std::move(snapshot);
}
}
size_t Histogram::GetCachedRecordedSnapshotMemorySize() const {
RwLock::ScopedReadLock lock(mutex_);
if (!recorded_snapshot_cache_) return 0;
return recorded_snapshot_cache_->buckets.capacity() * sizeof(RecordedBucket);
}
double Histogram::Cdf(int64_t value) const {
RwLock::ScopedReadLock lock(mutex_);
int64_t total = histogram_->total_count;
if (total == 0) return 0.0;
hdr_iter iter;
hdr_iter_init(&iter, histogram_.get());
while (hdr_iter_next(&iter)) {
if (iter.highest_equivalent_value >= value) {
return static_cast<double>(iter.cumulative_count) /
static_cast<double>(total);
}
// All recorded data accounted for; remaining buckets are empty.
if (iter.cumulative_count >= total) break;
}
return 1.0;
}
double Histogram::Skewness() const {
RwLock::ScopedReadLock lock(mutex_);
int64_t total = histogram_->total_count;
if (total < 3) return 0.0;
// Compute mean in one pass, then variance and skewness in a second
// pass. This avoids calling hdr_stddev (which internally recomputes
// hdr_mean), reducing the total from 4 iterations to 2.
double mean = hdr_mean(histogram_.get());
double m2 = 0.0;
double m3 = 0.0;
hdr_iter iter;
hdr_iter_recorded_init(&iter, histogram_.get());
while (hdr_iter_next(&iter)) {
double dev = static_cast<double>(hdr_median_equivalent_value(
histogram_.get(), iter.value)) -
mean;
double d2 = dev * dev;
m2 += static_cast<double>(iter.count) * d2;
m3 += static_cast<double>(iter.count) * d2 * dev;
}
double n = static_cast<double>(total);
double variance = m2 / n;
if (variance == 0.0) return 0.0;
double s3 = variance * std::sqrt(variance); // stddev^3
return (m3 / n) / s3;
}
double Histogram::Kurtosis() const {
RwLock::ScopedReadLock lock(mutex_);
int64_t total = histogram_->total_count;
if (total < 4) return 0.0;
// Same single-pass approach as Skewness: compute mean first, then
// variance and excess kurtosis together in one iteration.
double mean = hdr_mean(histogram_.get());
double m2 = 0.0;
double m4 = 0.0;
hdr_iter iter;
hdr_iter_recorded_init(&iter, histogram_.get());
while (hdr_iter_next(&iter)) {
double dev = static_cast<double>(hdr_median_equivalent_value(
histogram_.get(), iter.value)) -
mean;
double d2 = dev * dev;
m2 += static_cast<double>(iter.count) * d2;
m4 += static_cast<double>(iter.count) * d2 * d2;
}
double n = static_cast<double>(total);
double variance = m2 / n;
if (variance == 0.0) return 0.0;
double s4 = variance * variance; // stddev^4
return (m4 / n) / s4 - 3.0;
}
double Histogram::Subtract(const Histogram& other) {
auto do_subtract = [&]() -> double {
int64_t dropped = 0;
int32_t len =
std::min(histogram_->counts_len, other.histogram_->counts_len);
for (int32_t i = 0; i < len; i++) {
int64_t count = histogram_->counts[i] - other.histogram_->counts[i];
if (count < 0) {
dropped += -count;
count = 0;
}
histogram_->counts[i] = count;
}
hdr_reset_internal_counters(histogram_.get());
InvalidateRecordedSnapshot();
reset_count_++;
exceeds_ = (exceeds_ > other.exceeds_) ? exceeds_ - other.exceeds_ : 0;
return static_cast<double>(dropped);
};
if (this == &other) {
RwLock::ScopedWriteLock lock(mutex_);
return do_subtract();
}
if (this < &other) {
RwLock::ScopedWriteLock lock1(mutex_);
RwLock::ScopedReadLock lock2(other.mutex_);
return do_subtract();
}
RwLock::ScopedReadLock lock1(other.mutex_);
RwLock::ScopedWriteLock lock2(mutex_);
return do_subtract();
}
double Histogram::KsTest(const Histogram& other) const {
auto do_ks = [&]() -> double {
int64_t n1 = histogram_->total_count;
int64_t n2 = other.histogram_->total_count;
if (n1 == 0 || n2 == 0) return 0.0;
double max_d = 0.0;
int64_t cum1 = 0, cum2 = 0;
int32_t len =
std::max(histogram_->counts_len, other.histogram_->counts_len);
for (int32_t i = 0; i < len; i++) {
if (i < histogram_->counts_len) cum1 += histogram_->counts[i];
if (i < other.histogram_->counts_len) cum2 += other.histogram_->counts[i];
double cdf1 = static_cast<double>(cum1) / static_cast<double>(n1);
double cdf2 = static_cast<double>(cum2) / static_cast<double>(n2);
double d = cdf1 > cdf2 ? cdf1 - cdf2 : cdf2 - cdf1;
if (d > max_d) max_d = d;
}
return max_d;
};
if (this == &other) return 0.0;
if (this < &other) {
RwLock::ScopedReadLock lock1(mutex_);
RwLock::ScopedReadLock lock2(other.mutex_);
return do_ks();
}
RwLock::ScopedReadLock lock1(other.mutex_);
RwLock::ScopedReadLock lock2(mutex_);
return do_ks();
}
void Histogram::PercentilesAt(const double* percentiles,
int64_t* values,
size_t length) const {
RwLock::ScopedReadLock lock(mutex_);
hdr_value_at_percentiles(histogram_.get(), percentiles, values, length);
}
namespace {
// Continued fraction evaluation for the regularized incomplete beta
// function using Lentz's modified method. Reference: Numerical Recipes
// in C, 2nd edition, section 6.4.
static double BetaContinuedFraction(double a, double b, double x) {
constexpr double FPMIN = 1e-30;
constexpr int MAXIT = 200;
constexpr double EPS = 3e-12;
double qab = a + b;
double qap = a + 1.0;
double qam = a - 1.0;
double c = 1.0;
double d = 1.0 - qab * x / qap;
if (std::fabs(d) < FPMIN) d = FPMIN;
d = 1.0 / d;
double h = d;
for (int m = 1; m <= MAXIT; m++) {
int m2 = 2 * m;
// Even step.
double aa = m * (b - m) * x / ((qam + m2) * (a + m2));
d = 1.0 + aa * d;
if (std::fabs(d) < FPMIN) d = FPMIN;
c = 1.0 + aa / c;
if (std::fabs(c) < FPMIN) c = FPMIN;
d = 1.0 / d;
h *= d * c;
// Odd step.
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
d = 1.0 + aa * d;
if (std::fabs(d) < FPMIN) d = FPMIN;
c = 1.0 + aa / c;
if (std::fabs(c) < FPMIN) c = FPMIN;
d = 1.0 / d;
double del = d * c;
h *= del;
if (std::fabs(del - 1.0) <= EPS) break;
}
return h;
}
struct BetaParameters {
double a;
double b;
double log_normalization;
double mean;
double standard_deviation;
double skewness;
double excess_kurtosis;
double log_scale;
double symmetry_point;
bool use_asymptotic_front;
bool use_asymptotic_cdf;
};
static double StirlingCorrection(double x) {
const double inverse = 1.0 / x;
const double inverse_squared = inverse * inverse;
return inverse * (1.0 / 12.0 +
inverse_squared *
(-1.0 / 360.0 +
inverse_squared *
(1.0 / 1260.0 +
inverse_squared *
(-1.0 / 1680.0 + inverse_squared / 1188.0))));
}
static BetaParameters MakeBetaParameters(double a,
double b,
bool approximate_large_shapes) {
const double sum = a + b;
const double mean = a / sum;
const bool use_asymptotic_front =
approximate_large_shapes && a >= 8.0 && b >= 8.0;
// The continued fraction needs progressively more iterations as both
// beta shapes grow. At this concentration the Edgeworth error is smaller
// than the continued-fraction truncation error.
const bool use_asymptotic_cdf =
approximate_large_shapes && a * b / sum >= 25'000.0;
const double variance = mean * (1.0 - mean) / (sum + 1.0);
const double skewness =
2.0 * (b - a) * std::sqrt(sum + 1.0) / ((sum + 2.0) * std::sqrt(a * b));
const double excess_kurtosis =
6.0 * ((a - b) * (a - b) * (sum + 1.0) - a * b * (sum + 2.0)) /
(a * b * (sum + 2.0) * (sum + 3.0));
return {
a,
b,
use_asymptotic_front ? 0.0
: std::lgamma(sum) - std::lgamma(a) - std::lgamma(b),
mean,
std::sqrt(variance),
skewness,
excess_kurtosis,
use_asymptotic_front ? 0.5 * std::log(sum * mean * (1.0 - mean) /
(2.0 * std::numbers::pi)) +
StirlingCorrection(sum) -
StirlingCorrection(a) - StirlingCorrection(b)
: 0.0,
(a + 1.0) / (sum + 2.0),
use_asymptotic_front,
use_asymptotic_cdf,
};
}
static BetaParameters ReflectBetaParameters(const BetaParameters& params) {
return {
params.b,
params.a,
params.log_normalization,
1.0 - params.mean,
params.standard_deviation,
-params.skewness,
params.excess_kurtosis,
params.log_scale,
1.0 - params.symmetry_point,
params.use_asymptotic_front,
params.use_asymptotic_cdf,
};
}
static double BetaFront(const BetaParameters& params, double x) {
if (x <= 0.0 || x >= 1.0) return 0.0;
double log_front;
if (params.use_asymptotic_front) {
log_front = params.a * std::log1p((x - params.mean) / params.mean) +
params.b * std::log1p((params.mean - x) / (1.0 - params.mean)) +
params.log_scale;
} else {
log_front = params.log_normalization + params.a * std::log(x) +
params.b * std::log(1.0 - x);
}
return std::exp(log_front);
}
static double AsymptoticBetaCdf(const BetaParameters& params,
double x,
double* front_out) {
const double z = (x - params.mean) / params.standard_deviation;
const double normal_cdf = 0.5 * std::erfc(-z / std::numbers::sqrt2);
if (std::fabs(z) >= 8.0) {
if (front_out != nullptr) *front_out = 0.0;
return normal_cdf;
}
const double z2 = z * z;
const double z3 = z2 * z;
const double normal_pdf =
std::exp(-0.5 * z2) / std::sqrt(2.0 * std::numbers::pi);
if (front_out != nullptr) {
const double z4 = z2 * z2;
const double z6 = z3 * z3;
const double density_correction =
1.0 + params.skewness / 6.0 * (z3 - 3.0 * z) +
params.excess_kurtosis / 24.0 * (z4 - 6.0 * z2 + 3.0) +
params.skewness * params.skewness / 72.0 *
(z6 - 15.0 * z4 + 45.0 * z2 - 15.0);
*front_out = x * (1.0 - x) * normal_pdf / params.standard_deviation *
std::max(0.0, density_correction);
}
const double correction = params.skewness / 6.0 * (1.0 - z2) -
params.excess_kurtosis / 24.0 * (z3 - 3.0 * z) -
params.skewness * params.skewness / 72.0 *
(z3 * z2 - 10.0 * z3 + 15.0 * z);
return std::clamp(normal_cdf + normal_pdf * correction, 0.0, 1.0);
}
// Regularized incomplete beta function I_x(a, b).
// Returns the probability that a Beta(a,b) random variable is <= x.
static double RegularizedIncompleteBeta(const BetaParameters& params,
double x,
double* front_out = nullptr) {
if (front_out != nullptr) *front_out = 0.0;
if (x <= 0.0) return 0.0;
if (x >= 1.0) return 1.0;
if (params.use_asymptotic_cdf) {
return AsymptoticBetaCdf(params, x, front_out);
}
const double front = BetaFront(params, x);
if (front_out != nullptr) *front_out = front;
// Use the symmetry relation to ensure the continued fraction
// converges in the region where it is most accurate.
if (x < params.symmetry_point) {
return front * BetaContinuedFraction(params.a, params.b, x) / params.a;
}
return 1.0 -
front * BetaContinuedFraction(params.b, params.a, 1.0 - x) / params.b;
}
static double RegularizedIncompleteBeta(double a, double b, double x) {
return RegularizedIncompleteBeta(MakeBetaParameters(a, b, false), x);
}
// Standard normal CDF: Phi(x) = P(Z <= x).
static double NormalCdf(double x) {
return 0.5 * std::erfc(-x * std::numbers::sqrt2 / 2.0);
}
// Student's t-distribution CDF: P(T <= t) for df degrees of freedom.
static double StudentTCdf(double t, double df) {
double squared = t * t;
double denominator = df + squared;
if (squared < df) {
double x = squared / denominator;
double ibeta = RegularizedIncompleteBeta(0.5, df / 2.0, x);
return t >= 0.0 ? 0.5 + 0.5 * ibeta : 0.5 - 0.5 * ibeta;
}
double x = df / denominator;
double ibeta = RegularizedIncompleteBeta(df / 2.0, 0.5, x);
return t >= 0.0 ? 1.0 - 0.5 * ibeta : 0.5 * ibeta;
}
// Positive Student's t-distribution quantile for an upper-tail probability.
// Using the lower tail avoids loss of precision for probabilities near 1.
static double StudentTUpperQuantile(double p, double df) {
double lo = 0.0;
double hi = 1.0;
while (StudentTCdf(-hi, df) > p) hi *= 2.0;
for (int i = 0; i < 100; i++) {
double mid = (lo + hi) / 2.0;
if (StudentTCdf(-mid, df) > p) {
lo = mid;
} else {
hi = mid;
}
}
return (lo + hi) / 2.0;
}
// Binomial CDF: P(X <= k) for X ~ Binomial(n, p).
// Uses the identity P(X <= k) = I_{1-p}(n-k, k+1).
static double BinomialCdf(int64_t k, int64_t n, double p) {
if (k < 0) return 0.0;
if (k >= n) return 1.0;
return RegularizedIncompleteBeta(
static_cast<double>(n - k), static_cast<double>(k + 1), 1.0 - p);
}
struct QrdeResult {
std::vector<double> quantiles;
std::vector<double> densities;
size_t corrections = 0;
};
enum class QrdeDequantization : uint32_t {
kNone,
kHdr,
kAll,
};
static bool QrdeShouldDequantize(const Histogram::RecordedBucket& bucket,
QrdeDequantization dequantization) {
return bucket.count > 1 && (dequantization == QrdeDequantization::kAll ||
(dequantization == QrdeDequantization::kHdr &&
bucket.resolution > 1.0));
}
static std::pair<double, double> QrdeBucketRange(
const Histogram::RecordedSnapshot& snapshot,
size_t index,
QrdeDequantization dequantization) {
const auto& bucket = snapshot.buckets[index];
if (!QrdeShouldDequantize(bucket, dequantization)) {
return {bucket.value, bucket.value};
}
const double half_resolution = bucket.resolution / 2.0;
if (snapshot.buckets.size() == 1) {
return {bucket.value - half_resolution, bucket.value + half_resolution};
}
if (index == 0) {
return {bucket.value, bucket.value + half_resolution};
}
if (index + 1 == snapshot.buckets.size()) {
return {bucket.value - half_resolution, bucket.value};
}
return {bucket.value - half_resolution, bucket.value + half_resolution};
}
static bool QrdeExactSupport(const Histogram::RecordedSnapshot& snapshot,
const BetaParameters& params,
const BetaParameters& reflected,
double count,
size_t* begin_index,
size_t* end_index) {
// Collapsing both tails changes a quantile by at most 2^-79 times the
// histogram range, which is less than 2^-16 over the int64 value domain.
constexpr double tail_mass = 0x1p-80;
size_t lo = 0;
size_t hi = snapshot.buckets.size();
while (lo < hi) {
const size_t mid = lo + (hi - lo) / 2;
const double rank =
static_cast<double>(snapshot.buckets[mid].cumulative_count) / count;
const double cdf = RegularizedIncompleteBeta(params, rank);
if (!std::isfinite(cdf)) return false;
if (cdf < tail_mass) {
lo = mid + 1;
} else {
hi = mid;
}
}
if (lo == snapshot.buckets.size()) return false;
const size_t first = lo;
lo = first;
hi = snapshot.buckets.size();
while (lo < hi) {
const size_t mid = lo + (hi - lo) / 2;
const double remaining_rank =
static_cast<double>(snapshot.total_count -
snapshot.buckets[mid].cumulative_count) /
count;
const double survival =
RegularizedIncompleteBeta(reflected, remaining_rank);
if (!std::isfinite(survival)) return false;
if (survival > tail_mass) {
lo = mid + 1;
} else {
hi = mid;
}
}
if (lo == snapshot.buckets.size() || lo < first) return false;
*begin_index = first;
*end_index = lo + 1;
return true;
}
static double QrdeQuantile(const Histogram::RecordedSnapshot& snapshot,
double p,
QrdeDequantization dequantization) {
const double count = static_cast<double>(snapshot.total_count);
const double a = (count + 1.0) * p;
const double b = (count + 1.0) * (1.0 - p);
const BetaParameters mass_params = MakeBetaParameters(a, b, true);
const BetaParameters reflected_params = ReflectBetaParameters(mass_params);
size_t begin_index = 0;
size_t end_index = snapshot.buckets.size();
if (mass_params.use_asymptotic_cdf) {
// Outside this interval the normal-tail mass is below 2e-33.
constexpr double support_deviations = 12.0;
const double lower_rank = std::max(
0.0,
mass_params.mean - support_deviations * mass_params.standard_deviation);
const double upper_rank = std::min(
1.0,
mass_params.mean + support_deviations * mass_params.standard_deviation);
const auto first = std::lower_bound(
snapshot.buckets.begin(),
snapshot.buckets.end(),
lower_rank * count,
[](const Histogram::RecordedBucket& bucket, double cumulative) {
return static_cast<double>(bucket.cumulative_count) < cumulative;
});
const auto last = std::lower_bound(
first,
snapshot.buckets.end(),
upper_rank * count,
[](const Histogram::RecordedBucket& bucket, double cumulative) {
return static_cast<double>(bucket.cumulative_count) < cumulative;
});
begin_index = first - snapshot.buckets.begin();
if (last != snapshot.buckets.end()) {
end_index = last - snapshot.buckets.begin() + 1;
}
} else if (snapshot.buckets.size() >= 512) {
QrdeExactSupport(snapshot,
mass_params,
reflected_params,
count,
&begin_index,
&end_index);
}
const bool dequantize = dequantization != QrdeDequantization::kNone;
const auto first_range = QrdeBucketRange(snapshot, 0, dequantization);
const auto last_range =
QrdeBucketRange(snapshot, snapshot.buckets.size() - 1, dequantization);
const double lower_endpoint = first_range.first;
const double upper_endpoint = last_range.second;
double previous_cdf = 0.0;
double previous_survival = 0.0;
double previous_front = 0.0;
int64_t previous_count =
begin_index == 0 ? 0 : snapshot.buckets[begin_index - 1].cumulative_count;
if (previous_count != 0) {
const double previous_rank = static_cast<double>(previous_count) / count;
previous_cdf = RegularizedIncompleteBeta(
mass_params, previous_rank, dequantize ? &previous_front : nullptr);
if (!std::isfinite(previous_cdf) ||
(dequantize && !std::isfinite(previous_front))) {
begin_index = 0;
end_index = snapshot.buckets.size();
previous_count = 0;
previous_cdf = 0.0;
previous_front = 0.0;
} else {
previous_cdf = std::clamp(previous_cdf, 0.0, 1.0);
}
}
double quantile = lower_endpoint * previous_cdf;
bool using_survival = false;
for (size_t i = begin_index; i < end_index; i++) {
const auto& bucket = snapshot.buckets[i];
const double u0 = static_cast<double>(previous_count) / count;
const double u1 = static_cast<double>(bucket.cumulative_count) / count;
double front = 0.0;
double mass;
// Use the reflected CDF above the symmetry point so small upper-tail
// interval weights are not lost to subtraction from one.
if (u1 <= mass_params.symmetry_point) {
const double cdf =
std::clamp(RegularizedIncompleteBeta(
mass_params, u1, dequantize ? &front : nullptr),
previous_cdf,
1.0);
mass = cdf - previous_cdf;
previous_cdf = cdf;
} else {
const double remaining_rank =
static_cast<double>(snapshot.total_count - bucket.cumulative_count) /
count;
double survival = RegularizedIncompleteBeta(
reflected_params, remaining_rank, dequantize ? &front : nullptr);
if (using_survival) {
survival = std::clamp(survival, 0.0, previous_survival);
mass = previous_survival - survival;
} else {
survival = std::clamp(survival, 0.0, 1.0 - previous_cdf);
mass = 1.0 - previous_cdf - survival;
using_survival = true;
}
previous_survival = survival;
}
if (!QrdeShouldDequantize(bucket, dequantization)) {
quantile += bucket.value * mass;
} else {
// I_x(a + 1, b) = I_x(a, b) - front / a, so the first moment
// within this rank interval does not require a second beta CDF.
const double sum = a + b;
const double rank_width = static_cast<double>(bucket.count) / count;
const double local_moment =
std::clamp((a / sum - u0) * mass - (front - previous_front) / sum,
0.0,
rank_width * mass);
const auto [lower, upper] = QrdeBucketRange(snapshot, i, dequantization);
quantile += lower * mass + (upper - lower) * local_moment / rank_width;
}
previous_front = front;
previous_count = bucket.cumulative_count;
}
const double remaining_mass =
using_survival ? previous_survival : 1.0 - previous_cdf;
quantile += upper_endpoint * std::clamp(remaining_mass, 0.0, 1.0);
return quantile;
}
static QrdeResult CalculateQrde(const Histogram::RecordedSnapshot& snapshot,
const std::vector<double>& probabilities,
QrdeDequantization dequantization) {
QrdeResult result;
if (snapshot.buckets.empty()) return result;
result.quantiles.resize(probabilities.size());
result.densities.resize(probabilities.size() - 1);
const auto first_range = QrdeBucketRange(snapshot, 0, dequantization);
const auto last_range =
QrdeBucketRange(snapshot, snapshot.buckets.size() - 1, dequantization);
result.quantiles.front() = first_range.first;
result.quantiles.back() = last_range.second;
for (size_t i = 1; i + 1 < probabilities.size(); i++) {
double value = QrdeQuantile(snapshot, probabilities[i], dequantization);
value =
std::clamp(value, result.quantiles.front(), result.quantiles.back());
if (value < result.quantiles[i - 1]) {
value = result.quantiles[i - 1];
result.corrections++;
}
result.quantiles[i] = value;
}
for (size_t i = 0; i < result.densities.size(); i++) {
const double width = result.quantiles[i + 1] - result.quantiles[i];
const double probability_mass = probabilities[i + 1] - probabilities[i];
result.densities[i] = width == 0.0 ? std::numeric_limits<double>::infinity()
: probability_mass / width;
}
return result;
}
static Local<Float64Array> ToFloat64Array(Isolate* isolate,
const std::vector<double>& values) {
auto store =
ArrayBuffer::NewBackingStore(isolate, values.size() * sizeof(double));
if (!values.empty()) {
memcpy(store->Data(), values.data(), values.size() * sizeof(double));
}
Local<ArrayBuffer> buffer = ArrayBuffer::New(isolate, std::move(store));
return Float64Array::New(buffer, 0, values.size());
}
static const char* QrdeDequantizationName(QrdeDequantization dequantization) {
switch (dequantization) {
case QrdeDequantization::kNone:
return "none";
case QrdeDequantization::kHdr:
return "hdr";
case QrdeDequantization::kAll:
return "all";
}
UNREACHABLE();
}
class QrdeJob final : public ThreadPoolWork {
public:
QrdeJob(Environment* env,
Local<Promise::Resolver> resolver,
std::shared_ptr<Histogram> histogram,
Histogram::RecordedSnapshotSource snapshot_source,
std::vector<double> probabilities,
QrdeDequantization dequantization)
: ThreadPoolWork(env, "histogram.qrde"),
histogram_(std::move(histogram)),
snapshot_source_(std::move(snapshot_source)),
probabilities_(std::move(probabilities)),
dequantization_(dequantization) {
resolver_.Reset(env->isolate(), resolver);
}
void DoThreadPoolWork() override {
if (snapshot_source_.snapshot) {
snapshot_ = std::move(snapshot_source_.snapshot);
} else {
snapshot_ = std::make_shared<const Histogram::RecordedSnapshot>(
Histogram::BuildRecordedSnapshot(snapshot_source_.histogram.get()));
snapshot_source_.histogram.reset();
}
if (histogram_) {
histogram_->CacheRecordedSnapshot(snapshot_source_.generation, snapshot_);
histogram_.reset();
}
result_ = CalculateQrde(*snapshot_, probabilities_, dequantization_);
}
void AfterThreadPoolWork(int status) override {
std::unique_ptr<QrdeJob> self(this);
Environment* env = ThreadPoolWork::env();
CHECK(status == 0 || status == UV_ECANCELED);
if (!env->can_call_into_js()) {