#include "histogram_collector.h" #include "atomics_array.h" #include #include #include #include #include namespace NMonitoring { /////////////////////////////////////////////////////////////////////////// // TLinearHistogramCollector /////////////////////////////////////////////////////////////////////////// class TLinearHistogramCollector: public IHistogramCollector { public: TLinearHistogramCollector( ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) : Values_(bucketsCount) , StartValue_(startValue) , BucketWidth_(bucketWidth) , MaxValue_(startValue + bucketWidth * (bucketsCount - 2)) { } void Collect(double value, ui64 count) noexcept override { ui32 index = Max(); if (value <= StartValue_) { index = 0; } else if (value > MaxValue_) { index = Values_.Size() - 1; } else { double buckets = (value - StartValue_) / BucketWidth_; index = static_cast(std::ceil(buckets)); } Values_.Add(index, count); } void Reset() noexcept override { Values_.Reset(); } IHistogramSnapshotPtr Snapshot() const override { return new TLinearHistogramSnapshot( StartValue_, BucketWidth_, Values_.Copy()); } THolder Clone() override { return MakeHolder(TLinearHistogramCollector(Values_, StartValue_, BucketWidth_)); } private: TLinearHistogramCollector(TAtomicsArray const& values, TBucketBound startValue, TBucketBound bucketWidth) : Values_(values.Size()) , StartValue_(startValue) , BucketWidth_(bucketWidth) , MaxValue_(startValue + bucketWidth * (values.Size() - 2)) { for(size_t i = 0; i < Values_.Size(); ++i) { Values_.Add(i, values[i]); } } private: TAtomicsArray Values_; TBucketBound StartValue_; double BucketWidth_; TBucketBound MaxValue_; }; IHistogramCollectorPtr LinearHistogram( ui32 bucketsCount, TBucketBound startValue, TBucketBound bucketWidth) { Y_ENSURE(bucketsCount >= 2, "linear histogram must contain at least two buckets"); Y_ENSURE(bucketsCount <= HISTOGRAM_MAX_BUCKETS_COUNT, "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT << ", but got: " << bucketsCount); Y_ENSURE(bucketWidth >= 1, "bucketWidth must be >= 1, got: " << bucketWidth); return MakeHolder(bucketsCount, startValue, bucketWidth); } }