From 8a48929c2212c830cf54d2c874e1a55a8f68e09e Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 30 Sep 2021 11:52:05 +0200 Subject: stats: Added EmitSnapshot functions to emit metrics into CbObjects --- zencore/stats.cpp | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'zencore/stats.cpp') diff --git a/zencore/stats.cpp b/zencore/stats.cpp index 2a9b0a06f..53086f3ec 100644 --- a/zencore/stats.cpp +++ b/zencore/stats.cpp @@ -1,6 +1,8 @@ // Copyright Epic Games, Inc. All Rights Reserved. #include "zencore/stats.h" + +#include #include "zencore/intmath.h" #include "zencore/thread.h" #include "zencore/timer.h" @@ -79,18 +81,18 @@ Meter::TickIfNecessary() if (m_LastTick.compare_exchange_strong(OldTick, NewTick)) { - m_Remain.fetch_add(Age); + m_Remainder.fetch_add(Age); do { - int64_t Remain = m_Remain.load(std::memory_order_relaxed); + int64_t Remain = m_Remainder.load(std::memory_order_relaxed); if (Remain < 0) { return; } - if (m_Remain.compare_exchange_strong(Remain, Remain - CountPerTick)) + if (m_Remainder.compare_exchange_strong(Remain, Remain - CountPerTick)) { Tick(); } @@ -140,7 +142,7 @@ Meter::Rate15() } double -Meter::MeanRate() +Meter::MeanRate() const { const uint64_t Count = m_TotalCount.load(std::memory_order_relaxed); @@ -222,7 +224,7 @@ UniformSample::Update(int64_t Value) SampleSnapshot UniformSample::Snapshot() const { - uint64_t ValuesSize = Size(); + uint64_t ValuesSize = Size(); std::vector Values(ValuesSize); for (int i = 0; i < ValuesSize; ++i) @@ -336,8 +338,8 @@ SampleSnapshot::GetQuantileValue(double Quantile) } const int32_t Index = (int32_t)Pos; - const double Lower = m_Values[Index - 1]; - const double Upper = m_Values[Index]; + const double Lower = m_Values[Index - 1]; + const double Upper = m_Values[Index]; // Lerp return Lower + (Pos - std::floor(Pos)) * (Upper - Lower); @@ -351,6 +353,34 @@ SampleSnapshot::GetValues() const ////////////////////////////////////////////////////////////////////////// +void +EmitSnapshot(std::string_view Tag, const Histogram& Stat, CbObjectWriter& Cbo) +{ + Cbo.BeginObject(Tag); + + SampleSnapshot Snap = Stat.Snapshot(); + + Cbo << "count" << Stat.Count() << "avg" << Stat.Mean(); + Cbo << "min" << Stat.Min() << "max" << Stat.Max(); + Cbo << "p75" << Snap.Get75Percentile() << "p95" << Snap.Get95Percentile() << "p99" << Snap.Get99Percentile() << "p999" + << Snap.Get999Percentile(); + + Cbo.EndObject(); +} + +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") @@ -378,7 +408,7 @@ TEST_CASE("Core.Stats.Histogram") Histo.Update(-2); CHECK_EQ(Histo.Min(), -2); CHECK_EQ(Histo.Max(), 2); - CHECK_EQ(Histo.Mean(), 1/3.0); + CHECK_EQ(Histo.Mean(), 1 / 3.0); SampleSnapshot Snap4 = Histo.Snapshot(); CHECK_EQ(Snap4.Size(), 3); -- cgit v1.2.3