ewma.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "ewma.h"
  2. #include "metric.h"
  3. #include <atomic>
  4. #include <cmath>
  5. namespace NMonitoring {
  6. namespace {
  7. constexpr auto DEFAULT_INTERVAL = TDuration::Seconds(5);
  8. const double ALPHA1 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./1.);
  9. const double ALPHA5 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./5.);
  10. const double ALPHA15 = 1. - std::exp(-double(DEFAULT_INTERVAL.Seconds())/60./15.);
  11. class TExpMovingAverage final: public IExpMovingAverage {
  12. public:
  13. explicit TExpMovingAverage(IGauge* metric, double alpha, TDuration interval)
  14. : Metric_{metric}
  15. , Alpha_{alpha}
  16. , Interval_{interval.Seconds()}
  17. {
  18. Y_ABORT_UNLESS(metric != nullptr, "Passing nullptr metric is not allowed");
  19. }
  20. ~TExpMovingAverage() override = default;
  21. // This method NOT thread safe
  22. void Tick() override {
  23. const auto current = Uncounted_.fetch_and(0);
  24. const double instantRate = double(current) / Interval_;
  25. if (Y_UNLIKELY(!IsInitialized())) {
  26. Metric_->Set(instantRate);
  27. Init_ = true;
  28. } else {
  29. const double currentRate = Metric_->Get();
  30. Metric_->Set(Alpha_ * (instantRate - currentRate) + currentRate);
  31. }
  32. }
  33. void Update(i64 value) override {
  34. Uncounted_ += value;
  35. }
  36. double Rate() const override {
  37. return Metric_->Get();
  38. }
  39. void Reset() override {
  40. Init_ = false;
  41. Uncounted_ = 0;
  42. }
  43. private:
  44. bool IsInitialized() const {
  45. return Init_;
  46. }
  47. private:
  48. std::atomic<i64> Uncounted_{0};
  49. std::atomic<bool> Init_{false};
  50. IGauge* Metric_{nullptr};
  51. double Alpha_;
  52. ui64 Interval_;
  53. };
  54. struct TFakeEwma: IExpMovingAverage {
  55. void Tick() override {}
  56. void Update(i64) override {}
  57. double Rate() const override { return 0; }
  58. void Reset() override {}
  59. };
  60. } // namespace
  61. TEwmaMeter::TEwmaMeter()
  62. : Ewma_{MakeHolder<TFakeEwma>()}
  63. {
  64. }
  65. TEwmaMeter::TEwmaMeter(IExpMovingAveragePtr&& ewma)
  66. : Ewma_{std::move(ewma)}
  67. {
  68. }
  69. TEwmaMeter::TEwmaMeter(TEwmaMeter&& other) {
  70. if (&other == this) {
  71. return;
  72. }
  73. *this = std::move(other);
  74. }
  75. TEwmaMeter& TEwmaMeter::operator=(TEwmaMeter&& other) {
  76. Ewma_ = std::move(other.Ewma_);
  77. LastTick_.store(other.LastTick_);
  78. return *this;
  79. }
  80. void TEwmaMeter::TickIfNeeded() {
  81. constexpr ui64 INTERVAL_SECONDS = DEFAULT_INTERVAL.Seconds();
  82. const auto now = TInstant::Now().Seconds();
  83. ui64 old = LastTick_.load();
  84. const auto secondsSinceLastTick = now - old;
  85. if (secondsSinceLastTick > INTERVAL_SECONDS) {
  86. // round to the interval grid
  87. const ui64 newLast = now - (secondsSinceLastTick % INTERVAL_SECONDS);
  88. if (LastTick_.compare_exchange_strong(old, newLast)) {
  89. for (size_t i = 0; i < secondsSinceLastTick / INTERVAL_SECONDS; ++i) {
  90. Ewma_->Tick();
  91. }
  92. }
  93. }
  94. }
  95. void TEwmaMeter::Mark() {
  96. TickIfNeeded();
  97. Ewma_->Update(1);
  98. }
  99. void TEwmaMeter::Mark(i64 value) {
  100. TickIfNeeded();
  101. Ewma_->Update(value);
  102. }
  103. double TEwmaMeter::Get() {
  104. TickIfNeeded();
  105. return Ewma_->Rate();
  106. }
  107. IExpMovingAveragePtr OneMinuteEwma(IGauge* metric) {
  108. return MakeHolder<TExpMovingAverage>(metric, ALPHA1, DEFAULT_INTERVAL);
  109. }
  110. IExpMovingAveragePtr FiveMinuteEwma(IGauge* metric) {
  111. return MakeHolder<TExpMovingAverage>(metric, ALPHA5, DEFAULT_INTERVAL);
  112. }
  113. IExpMovingAveragePtr FiveteenMinuteEwma(IGauge* metric) {
  114. return MakeHolder<TExpMovingAverage>(metric, ALPHA15, DEFAULT_INTERVAL);
  115. }
  116. IExpMovingAveragePtr CreateEwma(IGauge* metric, double alpha, TDuration interval) {
  117. return MakeHolder<TExpMovingAverage>(metric, alpha, interval);
  118. }
  119. } // namespace NMonitoring