mkql_stats_registry.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #include <util/generic/strbuf.h>
  3. #include <util/generic/intrlist.h>
  4. #include <util/system/datetime.h>
  5. #include <util/system/hp_timer.h>
  6. #include <util/system/yassert.h>
  7. namespace NKikimr {
  8. namespace NMiniKQL {
  9. struct IStatsRegistry;
  10. /**
  11. * @brief Statistic unique named key.
  12. *
  13. * NOTE: Instances of this class must be static objects.
  14. */
  15. class TStatKey: public TIntrusiveSListItem<TStatKey> {
  16. friend struct IStatsRegistry;
  17. // -- static part --
  18. static ui32 IdSequence_;
  19. static TStatKey* KeysChain_;
  20. // -- instance part --
  21. public:
  22. TStatKey(TStringBuf name, bool deriv);
  23. TStringBuf GetName() const noexcept { return Name_; }
  24. bool IsDeriv() const noexcept { return Deriv_; }
  25. ui32 GetId() const noexcept { return Id_; }
  26. static ui32 GetMaxId() { return IdSequence_; }
  27. template <typename F>
  28. static void ForEach(F f) {
  29. for (TStatKey* p = KeysChain_; p;) {
  30. f(*p);
  31. auto n = p->Next();
  32. p = n ? n->Node() : nullptr;
  33. }
  34. }
  35. private:
  36. TStringBuf Name_;
  37. bool Deriv_;
  38. ui32 Id_;
  39. };
  40. /**
  41. * @brief Sase interface for statistics registry implementations.
  42. */
  43. struct IStatsRegistry {
  44. virtual ~IStatsRegistry() = default;
  45. virtual i64 GetStat(const TStatKey& key) const = 0;
  46. virtual void SetStat(const TStatKey& key, i64 value) = 0;
  47. virtual void SetMaxStat(const TStatKey& key, i64 value) = 0;
  48. virtual void SetMinStat(const TStatKey& key, i64 value) = 0;
  49. virtual void AddStat(const TStatKey& key, i64 value) = 0;
  50. void IncStat(const TStatKey& key) {
  51. AddStat(key, 1);
  52. }
  53. void DecStat(const TStatKey& key) {
  54. AddStat(key, -1);
  55. }
  56. // TConsumer = void(const TStatKey& key, i64 value)
  57. template <typename TConsumer>
  58. void ForEachStat(TConsumer c) const {
  59. TStatKey::ForEach([this, &c](const auto& key) {
  60. c(key, GetStat(key));
  61. });
  62. }
  63. };
  64. using IStatsRegistryPtr = TAutoPtr<IStatsRegistry>;
  65. IStatsRegistryPtr CreateDefaultStatsRegistry();
  66. #define MKQL_STAT_MOD_IMPL(method, nullableStatsRegistry, key, value) \
  67. do { \
  68. if (nullableStatsRegistry) \
  69. nullableStatsRegistry->method(key, value); \
  70. } while (0)
  71. #define MKQL_SET_STAT(statRegistry, key, value) \
  72. MKQL_STAT_MOD_IMPL(SetStat, statRegistry, key, value)
  73. #define MKQL_SET_MIN_STAT(statRegistry, key, value) \
  74. MKQL_STAT_MOD_IMPL(SetMinStat, statRegistry, key, value)
  75. #define MKQL_SET_MAX_STAT(statRegistry, key, value) \
  76. MKQL_STAT_MOD_IMPL(SetMaxStat, statRegistry, key, value)
  77. #define MKQL_ADD_STAT(statRegistry, key, value) \
  78. MKQL_STAT_MOD_IMPL(AddStat, statRegistry, key, value)
  79. #define MKQL_INC_STAT(statRegistry, key) MKQL_ADD_STAT(statRegistry, key, 1)
  80. #define MKQL_DEC_STAT(statRegistry, key) MKQL_ADD_STAT(statRegistry, key, -1)
  81. class TStatTimerBase {
  82. public:
  83. TStatTimerBase(const TStatKey& key)
  84. : Key_(key)
  85. , Total_(0)
  86. , Start_(0)
  87. {}
  88. void Reset() {
  89. Total_ = 0;
  90. }
  91. void Report(IStatsRegistry* statRegistry) const {
  92. auto value = (i64)(1e3 * Total_ / NHPTimer::GetClockRate());
  93. MKQL_ADD_STAT(statRegistry, Key_, value);
  94. }
  95. protected:
  96. const TStatKey& Key_;
  97. i64 Total_;
  98. i64 Start_;
  99. };
  100. class TStatTimer: public TStatTimerBase {
  101. public:
  102. TStatTimer(const TStatKey& key)
  103. : TStatTimerBase(key)
  104. {}
  105. void Acquire() {
  106. Start_ = GetCycleCount();
  107. }
  108. void Release() {
  109. Total_ += GetCycleCount() - Start_;
  110. }
  111. };
  112. class TSamplingStatTimer: public TStatTimerBase {
  113. public:
  114. TSamplingStatTimer(const TStatKey& key, ui64 frequency = 100)
  115. : TStatTimerBase(key)
  116. , Frequency_(frequency)
  117. {
  118. Y_ASSERT(Frequency_ > 0);
  119. }
  120. void Acquire() {
  121. if (Counter_ == 0) {
  122. Start_ = GetCycleCount();
  123. }
  124. }
  125. void Release() {
  126. if (Counter_ == 0) {
  127. Total_ += (GetCycleCount() - Start_) * Frequency_;
  128. }
  129. if (++Counter_ == Frequency_) {
  130. Counter_ = 0;
  131. }
  132. }
  133. private:
  134. const ui64 Frequency_;
  135. ui64 Counter_ = 0;
  136. };
  137. } // namespace NMiniKQL
  138. } // namespace NKikimr