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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "zencore/stats.h"
#include <zencore/compactbinarybuilder.h>
#include "zencore/intmath.h"
#include "zencore/thread.h"
#include "zencore/timer.h"
#include <cmath>
#include <gsl/gsl-lite.hpp>
#if ZEN_WITH_TESTS
# include <zencore/testing.h>
#endif
//
// Derived from https://github.com/dln/medida/blob/master/src/medida/stats/ewma.cc
//
namespace zen::metrics {
static constinit int kTickIntervalInSeconds = 5;
static constinit double kSecondsPerMinute = 60.0;
static constinit int kOneMinute = 1;
static constinit int kFiveMinutes = 5;
static constinit int kFifteenMinutes = 15;
static const double kM1_ALPHA = 1.0 - std::exp(-kTickIntervalInSeconds / kSecondsPerMinute / kOneMinute);
static const double kM5_ALPHA = 1.0 - std::exp(-kTickIntervalInSeconds / kSecondsPerMinute / kFiveMinutes);
static const double kM15_ALPHA = 1.0 - std::exp(-kTickIntervalInSeconds / kSecondsPerMinute / kFifteenMinutes);
static const uint64_t CountPerTick = GetHifreqTimerFrequencySafe() * kTickIntervalInSeconds;
static const uint64_t CountPerSecond = GetHifreqTimerFrequencySafe();
//////////////////////////////////////////////////////////////////////////
void
RawEWMA::Tick(double Alpha, uint64_t Interval, uint64_t Count, bool IsInitialUpdate)
{
const double InstantRate = double(Count) / Interval;
if (IsInitialUpdate)
{
m_Rate.store(InstantRate, std::memory_order_release);
}
else
{
double Delta = Alpha * (InstantRate - m_Rate);
#if defined(__cpp_lib_atomic_float)
m_Rate.fetch_add(Delta);
#else
double Value = m_Rate.load(std::memory_order_acquire);
double Next;
do
{
Next = Value + Delta;
} while (!m_Rate.compare_exchange_weak(Value, Next, std::memory_order_relaxed));
#endif
}
}
double
RawEWMA::Rate() const
{
return m_Rate.load(std::memory_order_relaxed) * CountPerSecond;
}
//////////////////////////////////////////////////////////////////////////
Meter::Meter() : m_StartTick{GetHifreqTimerValue()}, m_LastTick(m_StartTick.load())
{
}
Meter::~Meter()
{
}
void
Meter::TickIfNecessary()
{
uint64_t OldTick = m_LastTick.load();
const uint64_t NewTick = GetHifreqTimerValue();
const uint64_t Age = NewTick - OldTick;
if (Age > CountPerTick)
{
// Ensure only one thread at a time updates the time. This
// works because our tick interval should be sufficiently
// long to ensure two threads don't end up inside this block
if (m_LastTick.compare_exchange_strong(OldTick, NewTick))
{
m_Remainder.fetch_add(Age);
do
{
int64_t Remain = m_Remainder.load(std::memory_order_relaxed);
if (Remain < 0)
{
return;
}
if (m_Remainder.compare_exchange_strong(Remain, Remain - CountPerTick))
{
Tick();
}
} while (true);
}
}
}
void
Meter::Tick()
{
const uint64_t PendingCount = m_PendingCount.exchange(0);
const bool IsFirstTick = m_IsFirstTick;
if (IsFirstTick)
{
m_IsFirstTick = false;
}
m_RateM1.Tick(kM1_ALPHA, CountPerTick, PendingCount, IsFirstTick);
m_RateM5.Tick(kM5_ALPHA, CountPerTick, PendingCount, IsFirstTick);
m_RateM15.Tick(kM15_ALPHA, CountPerTick, PendingCount, IsFirstTick);
}
double
Meter::Rate1()
{
TickIfNecessary();
return m_RateM1.Rate();
}
double
Meter::Rate5()
{
TickIfNecessary();
return m_RateM5.Rate();
}
double
Meter::Rate15()
{
TickIfNecessary();
return m_RateM15.Rate();
}
double
Meter::MeanRate() const
{
const uint64_t Count = m_TotalCount.load(std::memory_order_relaxed);
if (Count == 0)
{
return 0.0;
}
const uint64_t Elapsed = GetHifreqTimerValue() - m_StartTick;
return (double(Count) * GetHifreqTimerFrequency()) / Elapsed;
}
void
Meter::Mark(uint64_t Count)
{
TickIfNecessary();
m_TotalCount.fetch_add(Count);
m_PendingCount.fetch_add(Count);
}
//////////////////////////////////////////////////////////////////////////
// TODO: should consider a cheaper RNG here, this will run for every thread
// that gets created
thread_local std::mt19937_64 ThreadLocalRng;
UniformSample::UniformSample(uint32_t ReservoirSize) : m_Values(ReservoirSize)
{
}
UniformSample::~UniformSample()
{
}
void
UniformSample::Clear()
{
for (auto& Value : m_Values)
{
Value.store(0);
}
m_SampleCounter = 0;
}
uint32_t
UniformSample::Size() const
{
return gsl::narrow_cast<uint32_t>(Min(m_SampleCounter.load(), m_Values.size()));
}
void
UniformSample::Update(int64_t Value)
{
const uint64_t Count = m_SampleCounter++;
const uint64_t Size = m_Values.size();
if (Count < Size)
{
m_Values[Count] = Value;
}
else
{
// Randomly choose an old entry to potentially replace (the probability
// of replacing an entry diminishes with time)
std::uniform_int_distribution<uint64_t> UniformDist(0, Count);
uint64_t SampleIndex = UniformDist(ThreadLocalRng);
if (SampleIndex < Size)
{
m_Values[SampleIndex].store(Value, std::memory_order_release);
}
}
}
SampleSnapshot
UniformSample::Snapshot() const
{
uint64_t ValuesSize = Size();
std::vector<double> Values(ValuesSize);
for (int i = 0, n = int(ValuesSize); i < n; ++i)
{
Values[i] = double(m_Values[i]);
}
return SampleSnapshot(std::move(Values));
}
//////////////////////////////////////////////////////////////////////////
Histogram::Histogram(int32_t SampleCount) : m_Sample(SampleCount)
{
}
Histogram::~Histogram()
{
}
void
Histogram::Clear()
{
m_Min = m_Max = m_Sum = m_Count = 0;
m_Sample.Clear();
}
void
Histogram::Update(int64_t Value)
{
m_Sample.Update(Value);
if (m_Count == 0)
{
m_Min = m_Max = Value;
}
else
{
int64_t CurrentMax = m_Max.load(std::memory_order_relaxed);
while ((CurrentMax < Value) && !m_Max.compare_exchange_weak(CurrentMax, Value))
{
}
int64_t CurrentMin = m_Min.load(std::memory_order_relaxed);
while ((CurrentMin > Value) && !m_Min.compare_exchange_weak(CurrentMin, Value))
{
}
}
m_Sum += Value;
++m_Count;
}
int64_t
Histogram::Max() const
{
return m_Max.load(std::memory_order_relaxed);
}
int64_t
Histogram::Min() const
{
return m_Min.load(std::memory_order_relaxed);
}
double
Histogram::Mean() const
{
if (m_Count)
{
return double(m_Sum.load(std::memory_order_relaxed)) / m_Count;
}
else
{
return 0.0;
}
}
uint64_t
Histogram::Count() const
{
return m_Count.load(std::memory_order_relaxed);
}
//////////////////////////////////////////////////////////////////////////
SampleSnapshot::SampleSnapshot(std::vector<double>&& Values) : m_Values(std::move(Values))
{
std::sort(begin(m_Values), end(m_Values));
}
SampleSnapshot::~SampleSnapshot()
{
}
double
SampleSnapshot::GetQuantileValue(double Quantile)
{
ZEN_ASSERT((Quantile >= 0.0) && (Quantile <= 1.0));
if (m_Values.empty())
{
return 0.0;
}
const double Pos = Quantile * (m_Values.size() + 1);
if (Pos < 1)
{
return m_Values.front();
}
if (Pos >= m_Values.size())
{
return m_Values.back();
}
const int32_t Index = (int32_t)Pos;
const double Lower = m_Values[Index - 1];
const double Upper = m_Values[Index];
// Lerp
return Lower + (Pos - std::floor(Pos)) * (Upper - Lower);
}
const std::vector<double>&
SampleSnapshot::GetValues() const
{
return m_Values;
}
//////////////////////////////////////////////////////////////////////////
OperationTiming::OperationTiming(int32_t SampleCount) : m_Histogram{SampleCount}
{
}
OperationTiming::~OperationTiming()
{
}
void
OperationTiming::Update(int64_t Duration)
{
m_Meter.Mark(1);
m_Histogram.Update(Duration);
}
int64_t
OperationTiming::Max() const
{
return m_Histogram.Max();
}
int64_t
OperationTiming::Min() const
{
return m_Histogram.Min();
}
double
OperationTiming::Mean() const
{
return m_Histogram.Mean();
}
uint64_t
OperationTiming::Count() const
{
return m_Meter.Count();
}
OperationTiming::Scope::Scope(OperationTiming& Outer) : m_Outer(Outer), m_StartTick(GetHifreqTimerValue())
{
}
OperationTiming::Scope::~Scope()
{
if (m_StartTick != 0)
{
m_Outer.Update(GetHifreqTimerValue() - m_StartTick);
}
}
void
OperationTiming::Scope::Cancel()
{
m_StartTick = 0;
}
//////////////////////////////////////////////////////////////////////////
RequestStats::RequestStats(int32_t SampleCount) : m_RequestTimeHistogram{SampleCount}, m_BytesHistogram{SampleCount}
{
}
RequestStats::~RequestStats()
{
}
void
RequestStats::Update(int64_t Duration, int64_t Bytes)
{
m_RequestMeter.Mark(1);
m_RequestTimeHistogram.Update(Duration);
m_BytesMeter.Mark(Bytes);
m_BytesHistogram.Update(Bytes);
}
uint64_t
RequestStats::Count() const
{
return m_RequestMeter.Count();
}
//////////////////////////////////////////////////////////////////////////
void
EmitSnapshot(Meter& Stat, CbObjectWriter& Cbo)
{
Cbo << "count" << Stat.Count();
Cbo << "rate_mean" << Stat.MeanRate();
Cbo << "rate_1" << Stat.Rate1() << "rate_5" << Stat.Rate5() << "rate_15" << Stat.Rate15();
}
void
RequestStats::EmitSnapshot(std::string_view Tag, CbObjectWriter& Cbo)
{
Cbo.BeginObject(Tag);
Cbo.BeginObject("requests");
metrics::EmitSnapshot(m_RequestMeter, Cbo);
metrics::EmitSnapshot(m_RequestTimeHistogram, Cbo, GetHifreqTimerToSeconds());
Cbo.EndObject();
Cbo.BeginObject("bytes");
metrics::EmitSnapshot(m_BytesMeter, Cbo);
metrics::EmitSnapshot(m_BytesHistogram, Cbo, 1.0);
Cbo.EndObject();
Cbo.EndObject();
}
void
EmitSnapshot(std::string_view Tag, OperationTiming& Stat, CbObjectWriter& Cbo)
{
Cbo.BeginObject(Tag);
SampleSnapshot Snap = Stat.Snapshot();
Cbo << "count" << Stat.Count();
Cbo << "rate_mean" << Stat.MeanRate();
Cbo << "rate_1" << Stat.Rate1() << "rate_5" << Stat.Rate5() << "rate_15" << Stat.Rate15();
const double ToSeconds = GetHifreqTimerToSeconds();
Cbo << "t_avg" << Stat.Mean() * ToSeconds;
Cbo << "t_min" << Stat.Min() * ToSeconds << "t_max" << Stat.Max() * ToSeconds;
Cbo << "t_p75" << Snap.Get75Percentile() * ToSeconds << "t_p95" << Snap.Get95Percentile() * ToSeconds << "t_p99"
<< Snap.Get99Percentile() * ToSeconds << "t_p999" << Snap.Get999Percentile() * ToSeconds;
Cbo.EndObject();
}
void
EmitSnapshot(std::string_view Tag, const Histogram& Stat, CbObjectWriter& Cbo, double ConversionFactor)
{
Cbo.BeginObject(Tag);
EmitSnapshot(Stat, Cbo, ConversionFactor);
Cbo.EndObject();
}
void
EmitSnapshot(const Histogram& Stat, CbObjectWriter& Cbo, double ConversionFactor)
{
SampleSnapshot Snap = Stat.Snapshot();
Cbo << "count" << Stat.Count() * ConversionFactor << "avg" << Stat.Mean() * ConversionFactor;
Cbo << "min" << Stat.Min() * ConversionFactor << "max" << Stat.Max() * ConversionFactor;
Cbo << "p75" << Snap.Get75Percentile() * ConversionFactor << "p95" << Snap.Get95Percentile() * ConversionFactor << "p99"
<< Snap.Get99Percentile() * ConversionFactor << "p999" << Snap.Get999Percentile() * ConversionFactor;
}
void
EmitSnapshot(std::string_view Tag, Meter& Stat, CbObjectWriter& Cbo)
{
Cbo.BeginObject(Tag);
Cbo << "count" << Stat.Count() << "rate_mean" << Stat.MeanRate();
Cbo << "rate_1" << Stat.Rate1() << "rate_5" << Stat.Rate5() << "rate_15" << Stat.Rate15();
Cbo.EndObject();
}
//////////////////////////////////////////////////////////////////////////
#if ZEN_WITH_TESTS
TEST_CASE("Core.Stats.Histogram")
{
Histogram Histo{258};
SampleSnapshot Snap1 = Histo.Snapshot();
CHECK_EQ(Snap1.Size(), 0);
CHECK_EQ(Snap1.GetMedian(), 0);
Histo.Update(1);
CHECK_EQ(Histo.Min(), 1);
CHECK_EQ(Histo.Max(), 1);
SampleSnapshot Snap2 = Histo.Snapshot();
CHECK_EQ(Snap2.Size(), 1);
Histo.Update(2);
CHECK_EQ(Histo.Min(), 1);
CHECK_EQ(Histo.Max(), 2);
SampleSnapshot Snap3 = Histo.Snapshot();
CHECK_EQ(Snap3.Size(), 2);
Histo.Update(-2);
CHECK_EQ(Histo.Min(), -2);
CHECK_EQ(Histo.Max(), 2);
CHECK_EQ(Histo.Mean(), 1 / 3.0);
SampleSnapshot Snap4 = Histo.Snapshot();
CHECK_EQ(Snap4.Size(), 3);
CHECK_EQ(Snap4.GetMedian(), 1);
CHECK_EQ(Snap4.Get999Percentile(), 2);
CHECK_EQ(Snap4.GetQuantileValue(0), -2);
}
TEST_CASE("Core.Stats.UniformSample")
{
UniformSample Sample1{100};
for (int i = 0; i < 100; ++i)
{
for (int j = 1; j <= 100; ++j)
{
Sample1.Update(j);
}
}
int64_t Sum = 0;
int64_t Count = 0;
Sample1.IterateValues([&](int64_t Value) {
++Count;
Sum += Value;
});
double Average = double(Sum) / Count;
CHECK(fabs(Average - 50) < 10); // What's the right test here? The result could vary massively and still be technically correct
}
TEST_CASE("Core.Stats.EWMA")
{
SUBCASE("Simple_1")
{
RawEWMA Ewma1;
Ewma1.Tick(kM1_ALPHA, CountPerSecond, 5, true);
CHECK(fabs(Ewma1.Rate() - 5) < 0.1);
for (int i = 0; i < 60; ++i)
{
Ewma1.Tick(kM1_ALPHA, CountPerSecond, 10, false);
}
CHECK(fabs(Ewma1.Rate() - 10) < 0.1);
for (int i = 0; i < 60; ++i)
{
Ewma1.Tick(kM1_ALPHA, CountPerSecond, 20, false);
}
CHECK(fabs(Ewma1.Rate() - 20) < 0.1);
}
SUBCASE("Simple_10")
{
RawEWMA Ewma1;
RawEWMA Ewma5;
RawEWMA Ewma15;
Ewma1.Tick(kM1_ALPHA, CountPerSecond, 5, true);
Ewma5.Tick(kM5_ALPHA, CountPerSecond, 5, true);
Ewma15.Tick(kM15_ALPHA, CountPerSecond, 5, true);
CHECK(fabs(Ewma1.Rate() - 5) < 0.1);
CHECK(fabs(Ewma5.Rate() - 5) < 0.1);
CHECK(fabs(Ewma15.Rate() - 5) < 0.1);
auto Tick1 = [&Ewma1](auto Value) { Ewma1.Tick(kM1_ALPHA, CountPerSecond, Value, false); };
auto Tick5 = [&Ewma5](auto Value) { Ewma5.Tick(kM5_ALPHA, CountPerSecond, Value, false); };
auto Tick15 = [&Ewma15](auto Value) { Ewma15.Tick(kM15_ALPHA, CountPerSecond, Value, false); };
for (int i = 0; i < 60; ++i)
{
Tick1(10);
Tick5(10);
Tick15(10);
}
CHECK(fabs(Ewma1.Rate() - 10) < 0.1);
for (int i = 0; i < 5 * 60; ++i)
{
Tick1(20);
Tick5(20);
Tick15(20);
}
CHECK(fabs(Ewma1.Rate() - 20) < 0.1);
CHECK(fabs(Ewma5.Rate() - 20) < 0.1);
for (int i = 0; i < 16 * 60; ++i)
{
Tick1(100);
Tick5(100);
Tick15(100);
}
CHECK(fabs(Ewma1.Rate() - 100) < 0.1);
CHECK(fabs(Ewma5.Rate() - 100) < 0.1);
CHECK(fabs(Ewma15.Rate() - 100) < 0.5);
}
}
# if 0 // This is not really a unit test, but mildly useful to exercise some code
TEST_CASE("Meter")
{
Meter Meter1;
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
Meter1.Mark(1);
Sleep(1000);
[[maybe_unused]] double Rate = Meter1.MeanRate();
}
# endif
}
namespace zen {
void
stats_forcelink()
{
}
#endif
} // namespace zen::metrics
|