diff options
author | kbalakirev <kbalakirev@yandex-team.ru> | 2022-02-10 16:48:58 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:58 +0300 |
commit | 498a47e48d41e5ec64ee3aa622a76a80274f35bd (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/monlib/metrics | |
parent | 1906a186042870fd03a12180acd1a6fcee045e42 (diff) | |
download | ydb-498a47e48d41e5ec64ee3aa622a76a80274f35bd.tar.gz |
Restoring authorship annotation for <kbalakirev@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics')
18 files changed, 833 insertions, 833 deletions
diff --git a/library/cpp/monlib/metrics/histogram_snapshot.cpp b/library/cpp/monlib/metrics/histogram_snapshot.cpp index 7df9762c1a..75b5811546 100644 --- a/library/cpp/monlib/metrics/histogram_snapshot.cpp +++ b/library/cpp/monlib/metrics/histogram_snapshot.cpp @@ -1,7 +1,7 @@ #include "histogram_snapshot.h" -#include <util/stream/output.h> - +#include <util/stream/output.h> + #include <iostream> @@ -23,16 +23,16 @@ namespace NMonitoring { } } // namespace NMonitoring - + namespace { - + template <typename TStream> auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) { os << TStringBuf("{"); - + ui32 i = 0; ui32 count = hist.Count(); - + if (count > 0) { for (; i < count - 1; ++i) { os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i); @@ -44,12 +44,12 @@ auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) { } else { os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i); } - } - + } + os << TStringBuf("}"); return os; -} +} } // namespace diff --git a/library/cpp/monlib/metrics/labels.h b/library/cpp/monlib/metrics/labels.h index 80a78efa6c..63dc997c28 100644 --- a/library/cpp/monlib/metrics/labels.h +++ b/library/cpp/monlib/metrics/labels.h @@ -292,12 +292,12 @@ namespace NMonitoring { return tmp; } - void SortByName() { + void SortByName() { std::sort(Labels_.begin(), Labels_.end(), [](const auto& lhs, const auto& rhs) { - return lhs.Name() < rhs.Name(); - }); - } - + return lhs.Name() < rhs.Name(); + }); + } + inline size_t Hash() const noexcept override { return TSimpleRangeHash()(Labels_); } diff --git a/library/cpp/monlib/metrics/log_histogram_collector.h b/library/cpp/monlib/metrics/log_histogram_collector.h index 9d51c887bc..b81f84ebf3 100644 --- a/library/cpp/monlib/metrics/log_histogram_collector.h +++ b/library/cpp/monlib/metrics/log_histogram_collector.h @@ -1,158 +1,158 @@ -#pragma once - -#include "log_histogram_snapshot.h" - -#include <util/generic/algorithm.h> -#include <util/generic/utility.h> -#include <util/generic/yexception.h> - -#include <mutex> -#include <cmath> - -namespace NMonitoring { - - class TLogHistogramCollector { - public: - static constexpr int DEFAULT_START_POWER = -1; - - explicit TLogHistogramCollector(int startPower = DEFAULT_START_POWER) - : StartPower_(startPower) - , CountZero_(0u) - {} - - void Collect(TLogHistogramSnapshot* logHist) { - std::lock_guard guard(Mutex_); - Merge(logHist); - } - +#pragma once + +#include "log_histogram_snapshot.h" + +#include <util/generic/algorithm.h> +#include <util/generic/utility.h> +#include <util/generic/yexception.h> + +#include <mutex> +#include <cmath> + +namespace NMonitoring { + + class TLogHistogramCollector { + public: + static constexpr int DEFAULT_START_POWER = -1; + + explicit TLogHistogramCollector(int startPower = DEFAULT_START_POWER) + : StartPower_(startPower) + , CountZero_(0u) + {} + + void Collect(TLogHistogramSnapshot* logHist) { + std::lock_guard guard(Mutex_); + Merge(logHist); + } + bool Collect(double value) { - std::lock_guard guard(Mutex_); + std::lock_guard guard(Mutex_); return CollectDouble(value); - } - - TLogHistogramSnapshotPtr Snapshot() const { - std::lock_guard guard(Mutex_); - return MakeIntrusive<TLogHistogramSnapshot>(BASE, CountZero_, StartPower_, Buckets_); - } - - void AddZeros(ui64 zerosCount) noexcept { - std::lock_guard guard(Mutex_); - CountZero_ += zerosCount; - } - - private: - int StartPower_; - ui64 CountZero_; - TVector<double> Buckets_; - mutable std::mutex Mutex_; - - static constexpr size_t MAX_BUCKETS = LOG_HIST_MAX_BUCKETS; - static constexpr double BASE = 1.5; - - private: - int EstimateBucketIndex(double value) const { - return (int) (std::floor(std::log(value) / std::log(BASE)) - StartPower_); - } - - void CollectPositiveDouble(double value) { - ssize_t idx = std::floor(std::log(value) / std::log(BASE)) - StartPower_; - if (idx >= Buckets_.ysize()) { - idx = ExtendUp(idx); - } else if (idx <= 0) { - idx = Max<ssize_t>(0, ExtendDown(idx, 1)); - } - ++Buckets_[idx]; - } - + } + + TLogHistogramSnapshotPtr Snapshot() const { + std::lock_guard guard(Mutex_); + return MakeIntrusive<TLogHistogramSnapshot>(BASE, CountZero_, StartPower_, Buckets_); + } + + void AddZeros(ui64 zerosCount) noexcept { + std::lock_guard guard(Mutex_); + CountZero_ += zerosCount; + } + + private: + int StartPower_; + ui64 CountZero_; + TVector<double> Buckets_; + mutable std::mutex Mutex_; + + static constexpr size_t MAX_BUCKETS = LOG_HIST_MAX_BUCKETS; + static constexpr double BASE = 1.5; + + private: + int EstimateBucketIndex(double value) const { + return (int) (std::floor(std::log(value) / std::log(BASE)) - StartPower_); + } + + void CollectPositiveDouble(double value) { + ssize_t idx = std::floor(std::log(value) / std::log(BASE)) - StartPower_; + if (idx >= Buckets_.ysize()) { + idx = ExtendUp(idx); + } else if (idx <= 0) { + idx = Max<ssize_t>(0, ExtendDown(idx, 1)); + } + ++Buckets_[idx]; + } + bool CollectDouble(double value) { if (Y_UNLIKELY(std::isnan(value) || std::isinf(value))) { return false; } - if (value <= 0.0) { - ++CountZero_; - } else { - CollectPositiveDouble(value); - } + if (value <= 0.0) { + ++CountZero_; + } else { + CollectPositiveDouble(value); + } return true; - } - - void Merge(TLogHistogramSnapshot* logHist) { - CountZero_ += logHist->ZerosCount(); - const i32 firstIdxBeforeExtend = logHist->StartPower() - StartPower_; - const i32 lastIdxBeforeExtend = firstIdxBeforeExtend + logHist->Count() - 1; - if (firstIdxBeforeExtend > Max<i16>() || firstIdxBeforeExtend < Min<i16>()) { - ythrow yexception() << "i16 overflow on first index"; - } - if (lastIdxBeforeExtend > Max<i16>() || lastIdxBeforeExtend < Min<i16>()) { - ythrow yexception() << "i16 overflow on last index"; - } - i64 firstIdx = ExtendBounds(firstIdxBeforeExtend, lastIdxBeforeExtend, 0).first; - size_t toMerge = std::min<ui32>(std::max<i64>(-firstIdx, (i64) 0), logHist->Count()); - if (toMerge) { - for (size_t i = 0; i < toMerge; ++i) { - Buckets_[0] += logHist->Bucket(i); - } - firstIdx = 0; - } - for (size_t i = toMerge; i != logHist->Count(); ++i) { - Buckets_[firstIdx] += logHist->Bucket(i); - ++firstIdx; - } - } - - int ExtendUp(int expectedIndex) { - Y_VERIFY_DEBUG(expectedIndex >= (int) Buckets_.size()); - const size_t toAdd = expectedIndex - Buckets_.size() + 1; - const size_t newSize = Buckets_.size() + toAdd; - if (newSize <= MAX_BUCKETS) { - Buckets_.resize(newSize, 0.0); - return expectedIndex; - } - - const size_t toRemove = newSize - MAX_BUCKETS; - const size_t actualToRemove = std::min<size_t>(toRemove, Buckets_.size()); - if (actualToRemove > 0) { - const double firstWeight = std::accumulate(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove, 0.0); - Buckets_.erase(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove); - if (Buckets_.empty()) { - Buckets_.push_back(firstWeight); - } else { - Buckets_[0] = firstWeight; - } - } - Buckets_.resize(MAX_BUCKETS, 0.0); - StartPower_ += toRemove; - return expectedIndex - toRemove; - } - - int ExtendDown(int expectedIndex, int margin) { - Y_VERIFY_DEBUG(expectedIndex <= 0); - int toAdd = std::min<int>(MAX_BUCKETS - Buckets_.size(), margin - expectedIndex); - if (toAdd > 0) { - Buckets_.insert(Buckets_.begin(), toAdd, 0.0); - StartPower_ -= toAdd; - } - return expectedIndex + toAdd; - } - - std::pair<ssize_t, ssize_t> ExtendBounds(ssize_t startIdx, ssize_t endIdx, ui8 margin) { - ssize_t realEndIdx; - ssize_t realStartIdx; - if (endIdx >= Buckets_.ysize()) { - Buckets_.reserve(std::max<size_t>(std::min<ui32>(endIdx - startIdx + 1ul, MAX_BUCKETS), 0ul)); - realEndIdx = ExtendUp(endIdx); - startIdx += realEndIdx - endIdx; - } else { - realEndIdx = endIdx; - } - if (startIdx < 1) { - realStartIdx = ExtendDown(startIdx, margin); - realEndIdx += realStartIdx - startIdx; - } else { - realStartIdx = startIdx; - } - return std::make_pair(realStartIdx, realEndIdx); - } - }; - -} // namespace NMonitoring + } + + void Merge(TLogHistogramSnapshot* logHist) { + CountZero_ += logHist->ZerosCount(); + const i32 firstIdxBeforeExtend = logHist->StartPower() - StartPower_; + const i32 lastIdxBeforeExtend = firstIdxBeforeExtend + logHist->Count() - 1; + if (firstIdxBeforeExtend > Max<i16>() || firstIdxBeforeExtend < Min<i16>()) { + ythrow yexception() << "i16 overflow on first index"; + } + if (lastIdxBeforeExtend > Max<i16>() || lastIdxBeforeExtend < Min<i16>()) { + ythrow yexception() << "i16 overflow on last index"; + } + i64 firstIdx = ExtendBounds(firstIdxBeforeExtend, lastIdxBeforeExtend, 0).first; + size_t toMerge = std::min<ui32>(std::max<i64>(-firstIdx, (i64) 0), logHist->Count()); + if (toMerge) { + for (size_t i = 0; i < toMerge; ++i) { + Buckets_[0] += logHist->Bucket(i); + } + firstIdx = 0; + } + for (size_t i = toMerge; i != logHist->Count(); ++i) { + Buckets_[firstIdx] += logHist->Bucket(i); + ++firstIdx; + } + } + + int ExtendUp(int expectedIndex) { + Y_VERIFY_DEBUG(expectedIndex >= (int) Buckets_.size()); + const size_t toAdd = expectedIndex - Buckets_.size() + 1; + const size_t newSize = Buckets_.size() + toAdd; + if (newSize <= MAX_BUCKETS) { + Buckets_.resize(newSize, 0.0); + return expectedIndex; + } + + const size_t toRemove = newSize - MAX_BUCKETS; + const size_t actualToRemove = std::min<size_t>(toRemove, Buckets_.size()); + if (actualToRemove > 0) { + const double firstWeight = std::accumulate(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove, 0.0); + Buckets_.erase(Buckets_.cbegin(), Buckets_.cbegin() + actualToRemove); + if (Buckets_.empty()) { + Buckets_.push_back(firstWeight); + } else { + Buckets_[0] = firstWeight; + } + } + Buckets_.resize(MAX_BUCKETS, 0.0); + StartPower_ += toRemove; + return expectedIndex - toRemove; + } + + int ExtendDown(int expectedIndex, int margin) { + Y_VERIFY_DEBUG(expectedIndex <= 0); + int toAdd = std::min<int>(MAX_BUCKETS - Buckets_.size(), margin - expectedIndex); + if (toAdd > 0) { + Buckets_.insert(Buckets_.begin(), toAdd, 0.0); + StartPower_ -= toAdd; + } + return expectedIndex + toAdd; + } + + std::pair<ssize_t, ssize_t> ExtendBounds(ssize_t startIdx, ssize_t endIdx, ui8 margin) { + ssize_t realEndIdx; + ssize_t realStartIdx; + if (endIdx >= Buckets_.ysize()) { + Buckets_.reserve(std::max<size_t>(std::min<ui32>(endIdx - startIdx + 1ul, MAX_BUCKETS), 0ul)); + realEndIdx = ExtendUp(endIdx); + startIdx += realEndIdx - endIdx; + } else { + realEndIdx = endIdx; + } + if (startIdx < 1) { + realStartIdx = ExtendDown(startIdx, margin); + realEndIdx += realStartIdx - startIdx; + } else { + realStartIdx = startIdx; + } + return std::make_pair(realStartIdx, realEndIdx); + } + }; + +} // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp b/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp index 560ec83714..ac9a3522ce 100644 --- a/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp +++ b/library/cpp/monlib/metrics/log_histogram_collector_ut.cpp @@ -1,38 +1,38 @@ -#include "log_histogram_collector.h" - -#include <library/cpp/testing/unittest/registar.h> - -Y_UNIT_TEST_SUITE(LogHistogramCollector) { - - Y_UNIT_TEST(ExtendUpEmpty) { - NMonitoring::TLogHistogramCollector collector(-1); - collector.Collect(4.1944122207138854e+17); - auto s = collector.Snapshot(); - UNIT_ASSERT_EQUAL(s->ZerosCount(), 0); - UNIT_ASSERT_EQUAL(s->StartPower(), 1); - UNIT_ASSERT_EQUAL(s->Count(), 100); - UNIT_ASSERT_EQUAL(s->Bucket(s->Count() - 1), 1); - } - - Y_UNIT_TEST(ExtendUpNonEmpty) { - NMonitoring::TLogHistogramCollector collector(-1); - collector.Collect(0.0); - collector.Collect(1/(1.5*1.5*1.5)); - collector.Collect(1/1.5); - auto s = collector.Snapshot(); - - UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); - UNIT_ASSERT_EQUAL(s->StartPower(), -4); - UNIT_ASSERT_EQUAL(s->Count(), 3); - UNIT_ASSERT_EQUAL(s->Bucket(1), 1); - UNIT_ASSERT_EQUAL(s->Bucket(2), 1); - - collector.Collect(4.1944122207138854e+17); - s = collector.Snapshot(); - UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); - UNIT_ASSERT_EQUAL(s->StartPower(), 1); - UNIT_ASSERT_EQUAL(s->Count(), 100); - UNIT_ASSERT_EQUAL(s->Bucket(0), 2); - UNIT_ASSERT_EQUAL(s->Bucket(99), 1); - } -} +#include "log_histogram_collector.h" + +#include <library/cpp/testing/unittest/registar.h> + +Y_UNIT_TEST_SUITE(LogHistogramCollector) { + + Y_UNIT_TEST(ExtendUpEmpty) { + NMonitoring::TLogHistogramCollector collector(-1); + collector.Collect(4.1944122207138854e+17); + auto s = collector.Snapshot(); + UNIT_ASSERT_EQUAL(s->ZerosCount(), 0); + UNIT_ASSERT_EQUAL(s->StartPower(), 1); + UNIT_ASSERT_EQUAL(s->Count(), 100); + UNIT_ASSERT_EQUAL(s->Bucket(s->Count() - 1), 1); + } + + Y_UNIT_TEST(ExtendUpNonEmpty) { + NMonitoring::TLogHistogramCollector collector(-1); + collector.Collect(0.0); + collector.Collect(1/(1.5*1.5*1.5)); + collector.Collect(1/1.5); + auto s = collector.Snapshot(); + + UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); + UNIT_ASSERT_EQUAL(s->StartPower(), -4); + UNIT_ASSERT_EQUAL(s->Count(), 3); + UNIT_ASSERT_EQUAL(s->Bucket(1), 1); + UNIT_ASSERT_EQUAL(s->Bucket(2), 1); + + collector.Collect(4.1944122207138854e+17); + s = collector.Snapshot(); + UNIT_ASSERT_EQUAL(s->ZerosCount(), 1); + UNIT_ASSERT_EQUAL(s->StartPower(), 1); + UNIT_ASSERT_EQUAL(s->Count(), 100); + UNIT_ASSERT_EQUAL(s->Bucket(0), 2); + UNIT_ASSERT_EQUAL(s->Bucket(99), 1); + } +} diff --git a/library/cpp/monlib/metrics/log_histogram_snapshot.h b/library/cpp/monlib/metrics/log_histogram_snapshot.h index 7f7826d49d..7673b43751 100644 --- a/library/cpp/monlib/metrics/log_histogram_snapshot.h +++ b/library/cpp/monlib/metrics/log_histogram_snapshot.h @@ -1,71 +1,71 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/generic/vector.h> - -#include <cmath> - -namespace NMonitoring { - - constexpr ui32 LOG_HIST_MAX_BUCKETS = 100; - - class TLogHistogramSnapshot: public TAtomicRefCount<TLogHistogramSnapshot> { - public: - TLogHistogramSnapshot(double base, ui64 zerosCount, int startPower, TVector<double> buckets) - : Base_(base) - , ZerosCount_(zerosCount) - , StartPower_(startPower) - , Buckets_(std::move(buckets)) { - } - - /** - * @return buckets count. - */ - ui32 Count() const noexcept { - return Buckets_.size(); - } - - /** - * @return upper bound for the bucket with particular index. - */ - double UpperBound(int index) const noexcept { - return std::pow(Base_, StartPower_ + index); - } - - /** - * @return value stored in the bucket with particular index. - */ - double Bucket(ui32 index) const noexcept { - return Buckets_[index]; - } - - /** - * @return nonpositive values count - */ - ui64 ZerosCount() const noexcept { - return ZerosCount_; - } - - double Base() const noexcept { - return Base_; - } - - int StartPower() const noexcept { - return StartPower_; - } - - ui64 MemorySizeBytes() const noexcept { - return sizeof(*this) + Buckets_.capacity() * sizeof(double); - } - - private: - double Base_; - ui64 ZerosCount_; - int StartPower_; - TVector<double> Buckets_; - }; - - using TLogHistogramSnapshotPtr = TIntrusivePtr<TLogHistogramSnapshot>; -} +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/vector.h> + +#include <cmath> + +namespace NMonitoring { + + constexpr ui32 LOG_HIST_MAX_BUCKETS = 100; + + class TLogHistogramSnapshot: public TAtomicRefCount<TLogHistogramSnapshot> { + public: + TLogHistogramSnapshot(double base, ui64 zerosCount, int startPower, TVector<double> buckets) + : Base_(base) + , ZerosCount_(zerosCount) + , StartPower_(startPower) + , Buckets_(std::move(buckets)) { + } + + /** + * @return buckets count. + */ + ui32 Count() const noexcept { + return Buckets_.size(); + } + + /** + * @return upper bound for the bucket with particular index. + */ + double UpperBound(int index) const noexcept { + return std::pow(Base_, StartPower_ + index); + } + + /** + * @return value stored in the bucket with particular index. + */ + double Bucket(ui32 index) const noexcept { + return Buckets_[index]; + } + + /** + * @return nonpositive values count + */ + ui64 ZerosCount() const noexcept { + return ZerosCount_; + } + + double Base() const noexcept { + return Base_; + } + + int StartPower() const noexcept { + return StartPower_; + } + + ui64 MemorySizeBytes() const noexcept { + return sizeof(*this) + Buckets_.capacity() * sizeof(double); + } + + private: + double Base_; + ui64 ZerosCount_; + int StartPower_; + TVector<double> Buckets_; + }; + + using TLogHistogramSnapshotPtr = TIntrusivePtr<TLogHistogramSnapshot>; +} std::ostream& operator<<(std::ostream& os, const NMonitoring::TLogHistogramSnapshot& hist); diff --git a/library/cpp/monlib/metrics/metric_consumer.h b/library/cpp/monlib/metrics/metric_consumer.h index 7049bce68c..f7a727585a 100644 --- a/library/cpp/monlib/metrics/metric_consumer.h +++ b/library/cpp/monlib/metrics/metric_consumer.h @@ -2,8 +2,8 @@ #include "metric_type.h" #include "histogram_collector.h" -#include "summary_collector.h" -#include "log_histogram_snapshot.h" +#include "summary_collector.h" +#include "log_histogram_snapshot.h" class TInstant; @@ -31,7 +31,7 @@ namespace NMonitoring { virtual void OnUint64(TInstant time, ui64 value) = 0; virtual void OnHistogram(TInstant time, IHistogramSnapshotPtr snapshot) = 0; - virtual void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) = 0; + virtual void OnLogHistogram(TInstant time, TLogHistogramSnapshotPtr snapshot) = 0; virtual void OnSummaryDouble(TInstant time, ISummaryDoubleSnapshotPtr snapshot) = 0; }; diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp index b27214e62a..86d9a52ec0 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -25,12 +25,12 @@ void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMoni case NMonitoring::NProto::TSingleSample::ValueCase::kFloat64: os << "Float64"; break; - case NMonitoring::NProto::TSingleSample::ValueCase::kSummaryDouble: - os << "DSummary"; - break; - case NMonitoring::NProto::TSingleSample::ValueCase::kLogHistogram: - os << "LogHistogram"; - break; + case NMonitoring::NProto::TSingleSample::ValueCase::kSummaryDouble: + os << "DSummary"; + break; + case NMonitoring::NProto::TSingleSample::ValueCase::kLogHistogram: + os << "LogHistogram"; + break; case NMonitoring::NProto::TSingleSample::ValueCase::VALUE_NOT_SET: os << "NOT SET"; break; diff --git a/library/cpp/monlib/metrics/metric_type.h b/library/cpp/monlib/metrics/metric_type.h index 63ee9b8d1b..1984c42c1e 100644 --- a/library/cpp/monlib/metrics/metric_type.h +++ b/library/cpp/monlib/metrics/metric_type.h @@ -14,7 +14,7 @@ namespace NMonitoring { IGAUGE = 4, HIST = 5, HIST_RATE = 6, - DSUMMARY = 7, + DSUMMARY = 7, // ISUMMARY = 8, reserved LOGHIST = 9, }; diff --git a/library/cpp/monlib/metrics/metric_value.cpp b/library/cpp/monlib/metrics/metric_value.cpp index 9a7d6fe1a1..b95d7011c6 100644 --- a/library/cpp/monlib/metrics/metric_value.cpp +++ b/library/cpp/monlib/metrics/metric_value.cpp @@ -12,15 +12,15 @@ namespace NMonitoring { SnapshotUnRef<EMetricValueType::HISTOGRAM>(p); } } else if (ValueType_ == EMetricValueType::SUMMARY) { - for (TPoint& p: Points_) { + for (TPoint& p: Points_) { SnapshotUnRef<EMetricValueType::SUMMARY>(p); - } - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - for (TPoint& p: Points_) { - SnapshotUnRef<EMetricValueType::LOGHISTOGRAM>(p); - } + } + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + for (TPoint& p: Points_) { + SnapshotUnRef<EMetricValueType::LOGHISTOGRAM>(p); + } } - + Points_.clear(); ValueType_ = EMetricValueType::UNKNOWN; } diff --git a/library/cpp/monlib/metrics/metric_value.h b/library/cpp/monlib/metrics/metric_value.h index a9d15e75f6..607fcc8602 100644 --- a/library/cpp/monlib/metrics/metric_value.h +++ b/library/cpp/monlib/metrics/metric_value.h @@ -2,8 +2,8 @@ #include "histogram_collector.h" #include "metric_value_type.h" -#include "summary_collector.h" -#include "log_histogram_snapshot.h" +#include "summary_collector.h" +#include "log_histogram_snapshot.h" #include <util/datetime/base.h> #include <util/generic/algorithm.h> @@ -43,20 +43,20 @@ namespace NMonitoring { static constexpr auto Type = EMetricValueType::UINT64; }; - template <> - struct TValueType<TLogHistogramSnapshot*> { - static constexpr auto Type = EMetricValueType::LOGHISTOGRAM; - }; - + template <> + struct TValueType<TLogHistogramSnapshot*> { + static constexpr auto Type = EMetricValueType::LOGHISTOGRAM; + }; + template <typename T> struct TValueType<T*, typename std::enable_if_t<std::is_base_of<IHistogramSnapshot, T>::value>> { static constexpr auto Type = EMetricValueType::HISTOGRAM; }; - template <typename T> - struct TValueType<T*, typename std::enable_if_t<std::is_base_of<ISummaryDoubleSnapshot, T>::value>> { + template <typename T> + struct TValueType<T*, typename std::enable_if_t<std::is_base_of<ISummaryDoubleSnapshot, T>::value>> { static constexpr auto Type = EMetricValueType::SUMMARY; - }; + }; /////////////////////////////////////////////////////////////////////////// // TMetricValue @@ -90,13 +90,13 @@ namespace NMonitoring { } explicit TMetricValue(ISummaryDoubleSnapshot* summary) noexcept { - Value_.Summary = summary; - } - - explicit TMetricValue(TLogHistogramSnapshot* logHist) noexcept { - Value_.LogHistogram = logHist; - } - + Value_.Summary = summary; + } + + explicit TMetricValue(TLogHistogramSnapshot* logHist) noexcept { + Value_.LogHistogram = logHist; + } + double AsDouble() const noexcept { return Value_.Double; } @@ -114,9 +114,9 @@ namespace NMonitoring { case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Double"; case EMetricValueType::SUMMARY: - ythrow yexception() << "summary cannot be casted to Double"; - case EMetricValueType::LOGHISTOGRAM: - ythrow yexception() << "loghistogram cannot be casted to Double"; + ythrow yexception() << "summary cannot be casted to Double"; + case EMetricValueType::LOGHISTOGRAM: + ythrow yexception() << "loghistogram cannot be casted to Double"; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown value type"; } @@ -140,9 +140,9 @@ namespace NMonitoring { case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Uint64"; case EMetricValueType::SUMMARY: - ythrow yexception() << "summary cannot be casted to Uint64"; - case EMetricValueType::LOGHISTOGRAM: - ythrow yexception() << "loghistogram cannot be casted to Uint64"; + ythrow yexception() << "summary cannot be casted to Uint64"; + case EMetricValueType::LOGHISTOGRAM: + ythrow yexception() << "loghistogram cannot be casted to Uint64"; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown value type"; } @@ -166,9 +166,9 @@ namespace NMonitoring { case EMetricValueType::HISTOGRAM: ythrow yexception() << "histogram cannot be casted to Int64"; case EMetricValueType::SUMMARY: - ythrow yexception() << "summary cannot be casted to Int64"; - case EMetricValueType::LOGHISTOGRAM: - ythrow yexception() << "loghistogram cannot be casted to Int64"; + ythrow yexception() << "summary cannot be casted to Int64"; + case EMetricValueType::LOGHISTOGRAM: + ythrow yexception() << "loghistogram cannot be casted to Int64"; case EMetricValueType::UNKNOWN: ythrow yexception() << "unknown value type"; } @@ -187,10 +187,10 @@ namespace NMonitoring { return Value_.Histogram; } - ISummaryDoubleSnapshot* AsSummaryDouble() const noexcept { - return Value_.Summary; - } - + ISummaryDoubleSnapshot* AsSummaryDouble() const noexcept { + return Value_.Summary; + } + ISummaryDoubleSnapshot* AsSummaryDouble(EMetricValueType type) const { if (type != EMetricValueType::SUMMARY) { ythrow yexception() << type << " cannot be casted to SummaryDouble"; @@ -199,26 +199,26 @@ namespace NMonitoring { return Value_.Summary; } - TLogHistogramSnapshot* AsLogHistogram() const noexcept { - return Value_.LogHistogram; - } - - TLogHistogramSnapshot* AsLogHistogram(EMetricValueType type) const { - if (type != EMetricValueType::LOGHISTOGRAM) { - ythrow yexception() << type << " cannot be casted to LogHistogram"; - } - - return Value_.LogHistogram; - } - + TLogHistogramSnapshot* AsLogHistogram() const noexcept { + return Value_.LogHistogram; + } + + TLogHistogramSnapshot* AsLogHistogram(EMetricValueType type) const { + if (type != EMetricValueType::LOGHISTOGRAM) { + ythrow yexception() << type << " cannot be casted to LogHistogram"; + } + + return Value_.LogHistogram; + } + protected: union { double Double; i64 Int64; ui64 Uint64; IHistogramSnapshot* Histogram; - ISummaryDoubleSnapshot* Summary; - TLogHistogramSnapshot* LogHistogram; + ISummaryDoubleSnapshot* Summary; + TLogHistogramSnapshot* LogHistogram; } Value_; }; @@ -290,18 +290,18 @@ namespace NMonitoring { return TBase::AsSummaryDouble(ValueType_); } - TLogHistogramSnapshot* AsLogHistogram() const { - return TBase::AsLogHistogram(ValueType_); - } - + TLogHistogramSnapshot* AsLogHistogram() const { + return TBase::AsLogHistogram(ValueType_); + } + private: void Ref() { if (ValueType_ == EMetricValueType::SUMMARY) { TBase::AsSummaryDouble()->Ref(); } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TBase::AsHistogram()->Ref(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - TBase::AsLogHistogram()->Ref(); + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + TBase::AsLogHistogram()->Ref(); } } @@ -310,8 +310,8 @@ namespace NMonitoring { TBase::AsSummaryDouble()->UnRef(); } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TBase::AsHistogram()->UnRef(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - TBase::AsLogHistogram()->UnRef(); + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + TBase::AsLogHistogram()->UnRef(); } } @@ -401,7 +401,7 @@ namespace NMonitoring { } else if (ValueType_ == EMetricValueType::HISTOGRAM) { TPoint& p = Points_.back(); p.GetValue().AsHistogram()->Ref(); - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { TPoint& p = Points_.back(); p.GetValue().AsLogHistogram()->Ref(); } @@ -424,16 +424,16 @@ namespace NMonitoring { point.GetValue().AsHistogram()->Ref(); } } else if (ValueType_ == EMetricValueType::SUMMARY) { - for (size_t i = prevSize; i < Points_.size(); ++i) { - TPoint& point = Points_[i]; - point.GetValue().AsSummaryDouble()->Ref(); - } - } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { - for (size_t i = prevSize; i < Points_.size(); ++i) { - TPoint& point = Points_[i]; - point.GetValue().AsLogHistogram()->Ref(); - } - } + for (size_t i = prevSize; i < Points_.size(); ++i) { + TPoint& point = Points_[i]; + point.GetValue().AsSummaryDouble()->Ref(); + } + } else if (ValueType_ == EMetricValueType::LOGHISTOGRAM) { + for (size_t i = prevSize; i < Points_.size(); ++i) { + TPoint& point = Points_[i]; + point.GetValue().AsLogHistogram()->Ref(); + } + } } template <typename TConsumer> @@ -480,50 +480,50 @@ namespace NMonitoring { }; template <EMetricValueType valueType, typename TPoint> - static inline void SnapshotUnRef(TPoint& point) { + static inline void SnapshotUnRef(TPoint& point) { if constexpr (valueType == EMetricValueType::HISTOGRAM) { - if (auto* hist = point.GetValue().AsHistogram()) { - hist->UnRef(); - } + if (auto* hist = point.GetValue().AsHistogram()) { + hist->UnRef(); + } } else if constexpr (valueType == EMetricValueType::SUMMARY) { - if (auto* summary = point.GetValue().AsSummaryDouble()) { - summary->UnRef(); - } - } else if constexpr (valueType == EMetricValueType::LOGHISTOGRAM) { - if (auto* logHist = point.GetValue().AsLogHistogram()) { - logHist->UnRef(); - } - } - } - + if (auto* summary = point.GetValue().AsSummaryDouble()) { + summary->UnRef(); + } + } else if constexpr (valueType == EMetricValueType::LOGHISTOGRAM) { + if (auto* logHist = point.GetValue().AsLogHistogram()) { + logHist->UnRef(); + } + } + } + template <EMetricValueType valueType, typename TPoint> - static void EraseDuplicates(TVector<TPoint>& points) { - // we have to manually clean reference to a snapshot from point - // while removing duplicates - auto result = points.rbegin(); - for (auto it = result + 1; it != points.rend(); ++it) { - if (result->GetTime() != it->GetTime() && ++result != it) { + static void EraseDuplicates(TVector<TPoint>& points) { + // we have to manually clean reference to a snapshot from point + // while removing duplicates + auto result = points.rbegin(); + for (auto it = result + 1; it != points.rend(); ++it) { + if (result->GetTime() != it->GetTime() && ++result != it) { SnapshotUnRef<valueType>(*result); - *result = *it; // (2) copy - it->ClearValue(); // (3) clean pointer in the source - } - } - - // erase tail points - for (auto it = result + 1; it != points.rend(); ++it) { + *result = *it; // (2) copy + it->ClearValue(); // (3) clean pointer in the source + } + } + + // erase tail points + for (auto it = result + 1; it != points.rend(); ++it) { SnapshotUnRef<valueType>(*it); - } - points.erase(points.begin(), (result + 1).base()); - } - - template <typename TPoint> + } + points.erase(points.begin(), (result + 1).base()); + } + + template <typename TPoint> void SortPointsByTs(EMetricValueType valueType, TVector<TPoint>& points) { if (points.size() < 2) { return; } - if (valueType != EMetricValueType::HISTOGRAM && valueType != EMetricValueType::SUMMARY - && valueType != EMetricValueType::LOGHISTOGRAM) { + if (valueType != EMetricValueType::HISTOGRAM && valueType != EMetricValueType::SUMMARY + && valueType != EMetricValueType::LOGHISTOGRAM) { // Stable sort + saving only the last point inside a group of duplicates StableSortBy(points, NPrivate::POINT_KEY_FN); auto it = UniqueBy(points.rbegin(), points.rend(), NPrivate::POINT_KEY_FN); @@ -532,11 +532,11 @@ namespace NMonitoring { StableSortBy(points, NPrivate::POINT_KEY_FN); if (valueType == EMetricValueType::HISTOGRAM) { EraseDuplicates<EMetricValueType::HISTOGRAM>(points); - } else if (valueType == EMetricValueType::LOGHISTOGRAM) { - EraseDuplicates<EMetricValueType::LOGHISTOGRAM>(points); - } else { + } else if (valueType == EMetricValueType::LOGHISTOGRAM) { + EraseDuplicates<EMetricValueType::LOGHISTOGRAM>(points); + } else { EraseDuplicates<EMetricValueType::SUMMARY>(points); - } + } } } } diff --git a/library/cpp/monlib/metrics/metric_value_type.h b/library/cpp/monlib/metrics/metric_value_type.h index 500e9770b5..ab30a958c2 100644 --- a/library/cpp/monlib/metrics/metric_value_type.h +++ b/library/cpp/monlib/metrics/metric_value_type.h @@ -10,7 +10,7 @@ enum class EMetricValueType { UINT64, HISTOGRAM, SUMMARY, - LOGHISTOGRAM, + LOGHISTOGRAM, }; } // namespace NMonitoring diff --git a/library/cpp/monlib/metrics/metric_value_ut.cpp b/library/cpp/monlib/metrics/metric_value_ut.cpp index 261807c904..49b47c4057 100644 --- a/library/cpp/monlib/metrics/metric_value_ut.cpp +++ b/library/cpp/monlib/metrics/metric_value_ut.cpp @@ -32,18 +32,18 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { return MakeIntrusive<TTestHistogram>(); } - ISummaryDoubleSnapshotPtr MakeSummarySnapshot(ui64 count = 0u) { - return MakeIntrusive<TSummaryDoubleSnapshot>(0.0, 0.0, 0.0, 0.0, count); - } - - TLogHistogramSnapshotPtr MakeLogHistogram(ui64 count = 0) { - TVector<double> buckets; - for (ui64 i = 0; i < count; ++i) { - buckets.push_back(i); - } - return MakeIntrusive<TLogHistogramSnapshot>(1.5, 0u, 0, buckets); - } - + ISummaryDoubleSnapshotPtr MakeSummarySnapshot(ui64 count = 0u) { + return MakeIntrusive<TSummaryDoubleSnapshot>(0.0, 0.0, 0.0, 0.0, count); + } + + TLogHistogramSnapshotPtr MakeLogHistogram(ui64 count = 0) { + TVector<double> buckets; + for (ui64 i = 0; i < count; ++i) { + buckets.push_back(i); + } + return MakeIntrusive<TLogHistogramSnapshot>(1.5, 0u, 0, buckets); + } + Y_UNIT_TEST(Sorted) { auto ts1 = TInstant::Now(); auto ts2 = ts1 + TDuration::Seconds(1); @@ -78,30 +78,30 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, histogram->RefCount()); } - Y_UNIT_TEST(Summary) { - auto ts = TInstant::Now(); - auto summary = MakeSummarySnapshot(); - UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); - { + Y_UNIT_TEST(Summary) { + auto ts = TInstant::Now(); + auto summary = MakeSummarySnapshot(); + UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); + { TMetricTimeSeries timeSeries; - timeSeries.Add(ts, summary.Get()); - UNIT_ASSERT_VALUES_EQUAL(2, summary->RefCount()); - } - UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); - } - - Y_UNIT_TEST(LogHistogram) { - auto ts = TInstant::Now(); - auto logHist = MakeLogHistogram(); - UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts, logHist.Get()); - UNIT_ASSERT_VALUES_EQUAL(2, logHist->RefCount()); - } - UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); - } - + timeSeries.Add(ts, summary.Get()); + UNIT_ASSERT_VALUES_EQUAL(2, summary->RefCount()); + } + UNIT_ASSERT_VALUES_EQUAL(1, summary->RefCount()); + } + + Y_UNIT_TEST(LogHistogram) { + auto ts = TInstant::Now(); + auto logHist = MakeLogHistogram(); + UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts, logHist.Get()); + UNIT_ASSERT_VALUES_EQUAL(2, logHist->RefCount()); + } + UNIT_ASSERT_VALUES_EQUAL(1, logHist->RefCount()); + } + Y_UNIT_TEST(TimeSeriesMovable) { auto ts = TInstant::Now(); auto histogram = MakeIntrusive<TTestHistogram>(); @@ -170,98 +170,98 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); } - Y_UNIT_TEST(LogHistogramsUnique) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - - auto h1 = MakeLogHistogram(); - auto h2 = MakeLogHistogram(); - auto h3 = MakeLogHistogram(); - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); // drop at the head - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h1.Get()); - - timeSeries.Add(ts2, h2.Get()); // drop in the middle - timeSeries.Add(ts2, h2.Get()); - timeSeries.Add(ts2, h2.Get()); - - timeSeries.Add(ts3, h3.Get()); // drop at the end - timeSeries.Add(ts3, h3.Get()); - timeSeries.Add(ts3, h3.Get()); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); - - UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); - - timeSeries.SortByTs(); - UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); - - UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - } - - Y_UNIT_TEST(SummaryUnique) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - - auto h1 = MakeSummarySnapshot(); - auto h2 = MakeSummarySnapshot(); - auto h3 = MakeSummarySnapshot(); - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - - { + Y_UNIT_TEST(LogHistogramsUnique) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + + auto h1 = MakeLogHistogram(); + auto h2 = MakeLogHistogram(); + auto h3 = MakeLogHistogram(); + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + + { TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); // drop at the head - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h1.Get()); - - timeSeries.Add(ts2, h2.Get()); // drop in the middle - timeSeries.Add(ts2, h2.Get()); - timeSeries.Add(ts2, h2.Get()); - - timeSeries.Add(ts3, h3.Get()); // drop at the end - timeSeries.Add(ts3, h3.Get()); - timeSeries.Add(ts3, h3.Get()); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); - - UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); - - timeSeries.SortByTs(); - UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); - - UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); - } - - UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); - UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); - } - + timeSeries.Add(ts1, h1.Get()); // drop at the head + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h1.Get()); + + timeSeries.Add(ts2, h2.Get()); // drop in the middle + timeSeries.Add(ts2, h2.Get()); + timeSeries.Add(ts2, h2.Get()); + + timeSeries.Add(ts3, h3.Get()); // drop at the end + timeSeries.Add(ts3, h3.Get()); + timeSeries.Add(ts3, h3.Get()); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); + + UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); + + timeSeries.SortByTs(); + UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); + + UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + } + + Y_UNIT_TEST(SummaryUnique) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + + auto h1 = MakeSummarySnapshot(); + auto h2 = MakeSummarySnapshot(); + auto h3 = MakeSummarySnapshot(); + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts1, h1.Get()); // drop at the head + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h1.Get()); + + timeSeries.Add(ts2, h2.Get()); // drop in the middle + timeSeries.Add(ts2, h2.Get()); + timeSeries.Add(ts2, h2.Get()); + + timeSeries.Add(ts3, h3.Get()); // drop at the end + timeSeries.Add(ts3, h3.Get()); + timeSeries.Add(ts3, h3.Get()); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 9); + + UNIT_ASSERT_VALUES_EQUAL(4, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(4, h3->RefCount()); + + timeSeries.SortByTs(); + UNIT_ASSERT_EQUAL(timeSeries.Size(), 3); + + UNIT_ASSERT_VALUES_EQUAL(2, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(2, h3->RefCount()); + } + + UNIT_ASSERT_VALUES_EQUAL(1, h1->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h2->RefCount()); + UNIT_ASSERT_VALUES_EQUAL(1, h3->RefCount()); + } + Y_UNIT_TEST(HistogramsUnique2) { auto ts1 = TInstant::Now(); auto ts2 = ts1 + TDuration::Seconds(1); @@ -300,84 +300,84 @@ Y_UNIT_TEST_SUITE(TMetricValueTest) { UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsHistogram()->Count(), 7); } } - - Y_UNIT_TEST(LogHistogramsUnique2) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - auto ts4 = ts3 + TDuration::Seconds(1); - auto ts5 = ts4 + TDuration::Seconds(1); - - auto h1 = MakeLogHistogram(1u); - auto h2 = MakeLogHistogram(2u); - auto h3 = MakeLogHistogram(3u); - auto h4 = MakeLogHistogram(4u); - auto h5 = MakeLogHistogram(5u); - auto h6 = MakeLogHistogram(6u); - auto h7 = MakeLogHistogram(7u); - - { - TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h2.Get()); - - timeSeries.Add(ts2, h3.Get()); - - timeSeries.Add(ts3, h4.Get()); - timeSeries.Add(ts3, h5.Get()); - - timeSeries.Add(ts4, h6.Get()); - timeSeries.Add(ts5, h7.Get()); - - timeSeries.SortByTs(); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); - UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsLogHistogram()->Count(), 2); - UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsLogHistogram()->Count(), 3); - UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsLogHistogram()->Count(), 5); - UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsLogHistogram()->Count(), 6); - UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsLogHistogram()->Count(), 7); - } - } - - Y_UNIT_TEST(SummaryUnique2) { - auto ts1 = TInstant::Now(); - auto ts2 = ts1 + TDuration::Seconds(1); - auto ts3 = ts2 + TDuration::Seconds(1); - auto ts4 = ts3 + TDuration::Seconds(1); - auto ts5 = ts4 + TDuration::Seconds(1); - - auto h1 = MakeSummarySnapshot(1u); - auto h2 = MakeSummarySnapshot(2u); - auto h3 = MakeSummarySnapshot(3u); - auto h4 = MakeSummarySnapshot(4u); - auto h5 = MakeSummarySnapshot(5u); - auto h6 = MakeSummarySnapshot(6u); - auto h7 = MakeSummarySnapshot(7u); - - { + + Y_UNIT_TEST(LogHistogramsUnique2) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + auto ts4 = ts3 + TDuration::Seconds(1); + auto ts5 = ts4 + TDuration::Seconds(1); + + auto h1 = MakeLogHistogram(1u); + auto h2 = MakeLogHistogram(2u); + auto h3 = MakeLogHistogram(3u); + auto h4 = MakeLogHistogram(4u); + auto h5 = MakeLogHistogram(5u); + auto h6 = MakeLogHistogram(6u); + auto h7 = MakeLogHistogram(7u); + + { TMetricTimeSeries timeSeries; - timeSeries.Add(ts1, h1.Get()); - timeSeries.Add(ts1, h2.Get()); - - timeSeries.Add(ts2, h3.Get()); - - timeSeries.Add(ts3, h4.Get()); - timeSeries.Add(ts3, h5.Get()); - - timeSeries.Add(ts4, h6.Get()); - timeSeries.Add(ts5, h7.Get()); - - timeSeries.SortByTs(); - - UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); - UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsSummaryDouble()->GetCount(), 2); - UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsSummaryDouble()->GetCount(), 3); - UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsSummaryDouble()->GetCount(), 5); - UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsSummaryDouble()->GetCount(), 6); - UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsSummaryDouble()->GetCount(), 7); - } - } + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h2.Get()); + + timeSeries.Add(ts2, h3.Get()); + + timeSeries.Add(ts3, h4.Get()); + timeSeries.Add(ts3, h5.Get()); + + timeSeries.Add(ts4, h6.Get()); + timeSeries.Add(ts5, h7.Get()); + + timeSeries.SortByTs(); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); + UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsLogHistogram()->Count(), 2); + UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsLogHistogram()->Count(), 3); + UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsLogHistogram()->Count(), 5); + UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsLogHistogram()->Count(), 6); + UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsLogHistogram()->Count(), 7); + } + } + + Y_UNIT_TEST(SummaryUnique2) { + auto ts1 = TInstant::Now(); + auto ts2 = ts1 + TDuration::Seconds(1); + auto ts3 = ts2 + TDuration::Seconds(1); + auto ts4 = ts3 + TDuration::Seconds(1); + auto ts5 = ts4 + TDuration::Seconds(1); + + auto h1 = MakeSummarySnapshot(1u); + auto h2 = MakeSummarySnapshot(2u); + auto h3 = MakeSummarySnapshot(3u); + auto h4 = MakeSummarySnapshot(4u); + auto h5 = MakeSummarySnapshot(5u); + auto h6 = MakeSummarySnapshot(6u); + auto h7 = MakeSummarySnapshot(7u); + + { + TMetricTimeSeries timeSeries; + timeSeries.Add(ts1, h1.Get()); + timeSeries.Add(ts1, h2.Get()); + + timeSeries.Add(ts2, h3.Get()); + + timeSeries.Add(ts3, h4.Get()); + timeSeries.Add(ts3, h5.Get()); + + timeSeries.Add(ts4, h6.Get()); + timeSeries.Add(ts5, h7.Get()); + + timeSeries.SortByTs(); + + UNIT_ASSERT_EQUAL(timeSeries.Size(), 5); + UNIT_ASSERT_EQUAL(timeSeries[0].GetValue().AsSummaryDouble()->GetCount(), 2); + UNIT_ASSERT_EQUAL(timeSeries[1].GetValue().AsSummaryDouble()->GetCount(), 3); + UNIT_ASSERT_EQUAL(timeSeries[2].GetValue().AsSummaryDouble()->GetCount(), 5); + UNIT_ASSERT_EQUAL(timeSeries[3].GetValue().AsSummaryDouble()->GetCount(), 6); + UNIT_ASSERT_EQUAL(timeSeries[4].GetValue().AsSummaryDouble()->GetCount(), 7); + } + } Y_UNIT_TEST(TMetricValueWithType) { // correct usage diff --git a/library/cpp/monlib/metrics/summary_collector.cpp b/library/cpp/monlib/metrics/summary_collector.cpp index 7496ee17b0..cae8560891 100644 --- a/library/cpp/monlib/metrics/summary_collector.cpp +++ b/library/cpp/monlib/metrics/summary_collector.cpp @@ -1 +1 @@ -#include "summary_collector.h" +#include "summary_collector.h" diff --git a/library/cpp/monlib/metrics/summary_collector.h b/library/cpp/monlib/metrics/summary_collector.h index f09f2cfcf7..acba0fddf9 100644 --- a/library/cpp/monlib/metrics/summary_collector.h +++ b/library/cpp/monlib/metrics/summary_collector.h @@ -1,104 +1,104 @@ -#pragma once - -#include "summary_snapshot.h" - -#include <atomic> -#include <limits> -#include <cmath> - -namespace NMonitoring { - - class ISummaryDoubleCollector { - public: - virtual ~ISummaryDoubleCollector() = default; - - virtual void Collect(double value) = 0; - - virtual ISummaryDoubleSnapshotPtr Snapshot() const = 0; +#pragma once + +#include "summary_snapshot.h" + +#include <atomic> +#include <limits> +#include <cmath> + +namespace NMonitoring { + + class ISummaryDoubleCollector { + public: + virtual ~ISummaryDoubleCollector() = default; + + virtual void Collect(double value) = 0; + + virtual ISummaryDoubleSnapshotPtr Snapshot() const = 0; virtual size_t SizeBytes() const = 0; - }; - - using ISummaryDoubleCollectorPtr = THolder<ISummaryDoubleCollector>; - - class TSummaryDoubleCollector final: public ISummaryDoubleCollector { - public: - TSummaryDoubleCollector() { - Sum_.store(0, std::memory_order_relaxed); - Min_.store(std::numeric_limits<double>::max(), std::memory_order_relaxed); - Max_.store(std::numeric_limits<double>::lowest(), std::memory_order_relaxed); - Count_.store(0, std::memory_order_relaxed); - } - - void Collect(double value) noexcept override { - if (std::isnan(value)) { - return; - } - UpdateSum(value); - UpdateMin(value); - UpdateMax(value); - Last_.store(value, std::memory_order_relaxed); - Count_.fetch_add(1ul, std::memory_order_relaxed); - } - + }; + + using ISummaryDoubleCollectorPtr = THolder<ISummaryDoubleCollector>; + + class TSummaryDoubleCollector final: public ISummaryDoubleCollector { + public: + TSummaryDoubleCollector() { + Sum_.store(0, std::memory_order_relaxed); + Min_.store(std::numeric_limits<double>::max(), std::memory_order_relaxed); + Max_.store(std::numeric_limits<double>::lowest(), std::memory_order_relaxed); + Count_.store(0, std::memory_order_relaxed); + } + + void Collect(double value) noexcept override { + if (std::isnan(value)) { + return; + } + UpdateSum(value); + UpdateMin(value); + UpdateMax(value); + Last_.store(value, std::memory_order_relaxed); + Count_.fetch_add(1ul, std::memory_order_relaxed); + } + ISummaryDoubleSnapshotPtr Snapshot() const override { - return new TSummaryDoubleSnapshot( - Sum_.load(std::memory_order_relaxed), - Min_.load(std::memory_order_relaxed), - Max_.load(std::memory_order_relaxed), - Last_.load(std::memory_order_relaxed), - Count_.load(std::memory_order_relaxed)); - } - + return new TSummaryDoubleSnapshot( + Sum_.load(std::memory_order_relaxed), + Min_.load(std::memory_order_relaxed), + Max_.load(std::memory_order_relaxed), + Last_.load(std::memory_order_relaxed), + Count_.load(std::memory_order_relaxed)); + } + size_t SizeBytes() const override { return sizeof(*this); } - private: - std::atomic<double> Sum_; - std::atomic<double> Min_; - std::atomic<double> Max_; - std::atomic<double> Last_; - std::atomic_uint64_t Count_; - - void UpdateSum(double add) noexcept { - double newValue; - double oldValue = Sum_.load(std::memory_order_relaxed); - do { - newValue = oldValue + add; - } while (!Sum_.compare_exchange_weak( - oldValue, - newValue, - std::memory_order_release, - std::memory_order_consume)); - } - - void UpdateMin(double candidate) noexcept { - double oldValue = Min_.load(std::memory_order_relaxed); - do { - if (oldValue <= candidate) { - break; - } - } while (!Min_.compare_exchange_weak( - oldValue, - candidate, - std::memory_order_release, - std::memory_order_consume)); - } - - void UpdateMax(double candidate) noexcept { - double oldValue = Max_.load(std::memory_order_relaxed); - do { - if (oldValue >= candidate) { - break; - } - } while (!Max_.compare_exchange_weak( - oldValue, - candidate, - std::memory_order_release, - std::memory_order_consume)); - } - - }; - -} + private: + std::atomic<double> Sum_; + std::atomic<double> Min_; + std::atomic<double> Max_; + std::atomic<double> Last_; + std::atomic_uint64_t Count_; + + void UpdateSum(double add) noexcept { + double newValue; + double oldValue = Sum_.load(std::memory_order_relaxed); + do { + newValue = oldValue + add; + } while (!Sum_.compare_exchange_weak( + oldValue, + newValue, + std::memory_order_release, + std::memory_order_consume)); + } + + void UpdateMin(double candidate) noexcept { + double oldValue = Min_.load(std::memory_order_relaxed); + do { + if (oldValue <= candidate) { + break; + } + } while (!Min_.compare_exchange_weak( + oldValue, + candidate, + std::memory_order_release, + std::memory_order_consume)); + } + + void UpdateMax(double candidate) noexcept { + double oldValue = Max_.load(std::memory_order_relaxed); + do { + if (oldValue >= candidate) { + break; + } + } while (!Max_.compare_exchange_weak( + oldValue, + candidate, + std::memory_order_release, + std::memory_order_consume)); + } + + }; + +} diff --git a/library/cpp/monlib/metrics/summary_collector_ut.cpp b/library/cpp/monlib/metrics/summary_collector_ut.cpp index 600148f814..191929550f 100644 --- a/library/cpp/monlib/metrics/summary_collector_ut.cpp +++ b/library/cpp/monlib/metrics/summary_collector_ut.cpp @@ -1,64 +1,64 @@ -#include "summary_collector.h" - +#include "summary_collector.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/random/random.h> - -#include <numeric> -#include <algorithm> - -namespace NMonitoring { - -Y_UNIT_TEST_SUITE(SummaryCollectorTest) { - - void CheckSnapshot(ISummaryDoubleSnapshotPtr snapshot, const TVector<double> values) { - const double eps = 1e-9; - - double sum = std::accumulate(values.begin(), values.end(), 0.0); - double min = *std::min_element(values.begin(), values.end()); - double max = *std::max_element(values.begin(), values.end()); - double last = values.back(); - ui64 count = values.size(); - - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetSum(), sum, eps); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMin(), min, eps); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMax(), max, eps); - UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetLast(), last, eps); - UNIT_ASSERT_EQUAL(snapshot->GetCount(), count); - } - - Y_UNIT_TEST(Simple) { - { - TVector<double> test{05, -1.5, 0.0, 2.5, 0.25, -1.0}; - TSummaryDoubleCollector summary; - for (auto value : test) { - summary.Collect(value); - } - CheckSnapshot(summary.Snapshot(), test); - } - { - TVector<double> test{-1.0, 1.0, 9.0, -5000.0, 5000.0, 5.0, -5.0}; - TSummaryDoubleCollector summary; - for (auto value : test) { - summary.Collect(value); - } - CheckSnapshot(summary.Snapshot(), test); - } - } - - Y_UNIT_TEST(RandomStressTest) { - const ui32 attemts = 100; - for (ui32 i = 0; i < attemts; ++i) { - const ui32 size = 100; - TVector<double> values(size); - TSummaryDoubleCollector summary; - for (auto& value : values) { - value = RandomNumber<double>() - 0.5; - summary.Collect(value); - } - CheckSnapshot(summary.Snapshot(), values); - } - } -} - -} + +#include <util/random/random.h> + +#include <numeric> +#include <algorithm> + +namespace NMonitoring { + +Y_UNIT_TEST_SUITE(SummaryCollectorTest) { + + void CheckSnapshot(ISummaryDoubleSnapshotPtr snapshot, const TVector<double> values) { + const double eps = 1e-9; + + double sum = std::accumulate(values.begin(), values.end(), 0.0); + double min = *std::min_element(values.begin(), values.end()); + double max = *std::max_element(values.begin(), values.end()); + double last = values.back(); + ui64 count = values.size(); + + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetSum(), sum, eps); + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMin(), min, eps); + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetMax(), max, eps); + UNIT_ASSERT_DOUBLES_EQUAL(snapshot->GetLast(), last, eps); + UNIT_ASSERT_EQUAL(snapshot->GetCount(), count); + } + + Y_UNIT_TEST(Simple) { + { + TVector<double> test{05, -1.5, 0.0, 2.5, 0.25, -1.0}; + TSummaryDoubleCollector summary; + for (auto value : test) { + summary.Collect(value); + } + CheckSnapshot(summary.Snapshot(), test); + } + { + TVector<double> test{-1.0, 1.0, 9.0, -5000.0, 5000.0, 5.0, -5.0}; + TSummaryDoubleCollector summary; + for (auto value : test) { + summary.Collect(value); + } + CheckSnapshot(summary.Snapshot(), test); + } + } + + Y_UNIT_TEST(RandomStressTest) { + const ui32 attemts = 100; + for (ui32 i = 0; i < attemts; ++i) { + const ui32 size = 100; + TVector<double> values(size); + TSummaryDoubleCollector summary; + for (auto& value : values) { + value = RandomNumber<double>() - 0.5; + summary.Collect(value); + } + CheckSnapshot(summary.Snapshot(), values); + } + } +} + +} diff --git a/library/cpp/monlib/metrics/summary_snapshot.cpp b/library/cpp/monlib/metrics/summary_snapshot.cpp index 39f34902f3..0b13263337 100644 --- a/library/cpp/monlib/metrics/summary_snapshot.cpp +++ b/library/cpp/monlib/metrics/summary_snapshot.cpp @@ -1,9 +1,9 @@ -#include "summary_snapshot.h" - -#include <util/stream/output.h> - +#include "summary_snapshot.h" + +#include <util/stream/output.h> + #include <iostream> - + namespace { @@ -16,11 +16,11 @@ auto& Output(TStream& o, const NMonitoring::ISummaryDoubleSnapshot& s) { o << TStringBuf("max: ") << s.GetMax() << TStringBuf(", "); o << TStringBuf("last: ") << s.GetLast() << TStringBuf(", "); o << TStringBuf("count: ") << s.GetCount(); - + o << TStringBuf("}"); return o; -} +} } // namespace diff --git a/library/cpp/monlib/metrics/summary_snapshot.h b/library/cpp/monlib/metrics/summary_snapshot.h index d90b5f1b01..afcc895fd3 100644 --- a/library/cpp/monlib/metrics/summary_snapshot.h +++ b/library/cpp/monlib/metrics/summary_snapshot.h @@ -1,72 +1,72 @@ -#pragma once - -#include <util/generic/ptr.h> - -namespace NMonitoring { - - class ISummaryDoubleSnapshot: public TAtomicRefCount<ISummaryDoubleSnapshot> { - public: - virtual ~ISummaryDoubleSnapshot() = default; - - // TODO: write documentation - - virtual ui64 GetCount() const = 0; - - virtual double GetSum() const = 0; - - virtual double GetMin() const = 0; - - virtual double GetMax() const = 0; - - virtual double GetLast() const = 0; - - virtual ui64 MemorySizeBytes() const = 0; - }; - - using ISummaryDoubleSnapshotPtr = TIntrusivePtr<ISummaryDoubleSnapshot>; - - class TSummaryDoubleSnapshot final: public ISummaryDoubleSnapshot { - public: - TSummaryDoubleSnapshot(double sum, double min, double max, double last, ui64 count) - : Sum_(sum) - , Min_(min) - , Max_(max) - , Last_(last) - , Count_(count) - {} - - ui64 GetCount() const noexcept override { - return Count_; - } - - double GetSum() const noexcept override { - return Sum_; - } - - double GetMin() const noexcept override { - return Min_; - } - - double GetMax() const noexcept override { - return Max_; - } - - virtual double GetLast() const noexcept override { - return Last_; - } - - ui64 MemorySizeBytes() const noexcept override { - return sizeof(*this); - } - - private: - double Sum_; - double Min_; - double Max_; - double Last_; - ui64 Count_; - }; - -} +#pragma once + +#include <util/generic/ptr.h> + +namespace NMonitoring { + + class ISummaryDoubleSnapshot: public TAtomicRefCount<ISummaryDoubleSnapshot> { + public: + virtual ~ISummaryDoubleSnapshot() = default; + + // TODO: write documentation + + virtual ui64 GetCount() const = 0; + + virtual double GetSum() const = 0; + + virtual double GetMin() const = 0; + + virtual double GetMax() const = 0; + + virtual double GetLast() const = 0; + + virtual ui64 MemorySizeBytes() const = 0; + }; + + using ISummaryDoubleSnapshotPtr = TIntrusivePtr<ISummaryDoubleSnapshot>; + + class TSummaryDoubleSnapshot final: public ISummaryDoubleSnapshot { + public: + TSummaryDoubleSnapshot(double sum, double min, double max, double last, ui64 count) + : Sum_(sum) + , Min_(min) + , Max_(max) + , Last_(last) + , Count_(count) + {} + + ui64 GetCount() const noexcept override { + return Count_; + } + + double GetSum() const noexcept override { + return Sum_; + } + + double GetMin() const noexcept override { + return Min_; + } + + double GetMax() const noexcept override { + return Max_; + } + + virtual double GetLast() const noexcept override { + return Last_; + } + + ui64 MemorySizeBytes() const noexcept override { + return sizeof(*this); + } + + private: + double Sum_; + double Min_; + double Max_; + double Last_; + ui64 Count_; + }; + +} std::ostream& operator<<(std::ostream& os, const NMonitoring::ISummaryDoubleSnapshot& s); diff --git a/library/cpp/monlib/metrics/ut/ya.make b/library/cpp/monlib/metrics/ut/ya.make index cb55da7822..aec9974fbd 100644 --- a/library/cpp/monlib/metrics/ut/ya.make +++ b/library/cpp/monlib/metrics/ut/ya.make @@ -10,7 +10,7 @@ SRCS( fake_ut.cpp histogram_collector_ut.cpp labels_ut.cpp - log_histogram_collector_ut.cpp + log_histogram_collector_ut.cpp metric_registry_ut.cpp metric_sub_registry_ut.cpp metric_value_ut.cpp |