mon_stats.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include "defs.h"
  3. //#include "actor.h"
  4. #include <library/cpp/actors/util/local_process_key.h>
  5. #include <library/cpp/monlib/metrics/histogram_snapshot.h>
  6. #include <util/system/hp_timer.h>
  7. namespace NActors {
  8. struct TLogHistogram : public NMonitoring::IHistogramSnapshot {
  9. TLogHistogram() {
  10. memset(Buckets, 0, sizeof(Buckets));
  11. }
  12. inline void Add(ui64 val, ui64 inc = 1) {
  13. size_t ind = 0;
  14. #if defined(__clang__) && __clang_major__ == 3 && __clang_minor__ == 7
  15. asm volatile("" ::
  16. : "memory");
  17. #endif
  18. if (val > 1) {
  19. ind = GetValueBitCount(val - 1);
  20. }
  21. #if defined(__clang__) && __clang_major__ == 3 && __clang_minor__ == 7
  22. asm volatile("" ::
  23. : "memory");
  24. #endif
  25. RelaxedStore(&TotalSamples, RelaxedLoad(&TotalSamples) + inc);
  26. RelaxedStore(&Buckets[ind], RelaxedLoad(&Buckets[ind]) + inc);
  27. }
  28. void Aggregate(const TLogHistogram& other) {
  29. const ui64 inc = RelaxedLoad(&other.TotalSamples);
  30. RelaxedStore(&TotalSamples, RelaxedLoad(&TotalSamples) + inc);
  31. for (size_t i = 0; i < Y_ARRAY_SIZE(Buckets); ++i) {
  32. Buckets[i] += RelaxedLoad(&other.Buckets[i]);
  33. }
  34. }
  35. // IHistogramSnapshot
  36. ui32 Count() const override {
  37. return Y_ARRAY_SIZE(Buckets);
  38. }
  39. NMonitoring::TBucketBound UpperBound(ui32 index) const override {
  40. Y_ASSERT(index < Y_ARRAY_SIZE(Buckets));
  41. if (index == 0) {
  42. return 1;
  43. }
  44. return NMonitoring::TBucketBound(1ull << (index - 1)) * 2.0;
  45. }
  46. NMonitoring::TBucketValue Value(ui32 index) const override {
  47. Y_ASSERT(index < Y_ARRAY_SIZE(Buckets));
  48. return Buckets[index];
  49. }
  50. ui64 TotalSamples = 0;
  51. ui64 Buckets[65];
  52. };
  53. struct TExecutorPoolStats {
  54. ui64 MaxUtilizationTime = 0;
  55. ui64 IncreasingThreadsByNeedyState = 0;
  56. ui64 IncreasingThreadsByExchange = 0;
  57. ui64 DecreasingThreadsByStarvedState = 0;
  58. ui64 DecreasingThreadsByHoggishState = 0;
  59. ui64 DecreasingThreadsByExchange = 0;
  60. i64 MaxConsumedCpuUs = 0;
  61. i64 MinConsumedCpuUs = 0;
  62. i64 MaxBookedCpuUs = 0;
  63. i64 MinBookedCpuUs = 0;
  64. i16 WrongWakenedThreadCount = 0;
  65. i16 CurrentThreadCount = 0;
  66. i16 PotentialMaxThreadCount = 0;
  67. i16 DefaultThreadCount = 0;
  68. i16 MaxThreadCount = 0;
  69. bool IsNeedy = false;
  70. bool IsStarved = false;
  71. bool IsHoggish = false;
  72. };
  73. struct TExecutorThreadStats {
  74. ui64 SentEvents = 0;
  75. ui64 ReceivedEvents = 0;
  76. ui64 PreemptedEvents = 0; // Number of events experienced hard preemption
  77. ui64 NonDeliveredEvents = 0;
  78. ui64 EmptyMailboxActivation = 0;
  79. ui64 CpuUs = 0; // microseconds thread was executing on CPU (accounts for preemtion)
  80. ui64 SafeElapsedTicks = 0;
  81. ui64 WorstActivationTimeUs = 0;
  82. NHPTimer::STime ElapsedTicks = 0;
  83. NHPTimer::STime ParkedTicks = 0;
  84. NHPTimer::STime BlockedTicks = 0;
  85. TLogHistogram ActivationTimeHistogram;
  86. TLogHistogram EventDeliveryTimeHistogram;
  87. TLogHistogram EventProcessingCountHistogram;
  88. TLogHistogram EventProcessingTimeHistogram;
  89. TVector<NHPTimer::STime> ElapsedTicksByActivity;
  90. TVector<ui64> ReceivedEventsByActivity;
  91. TVector<i64> ActorsAliveByActivity; // the sum should be positive, but per-thread might be negative
  92. TVector<ui64> ScheduledEventsByActivity;
  93. TVector<ui64> StuckActorsByActivity;
  94. TVector<std::array<ui64, 10>> UsageByActivity;
  95. ui64 PoolActorRegistrations = 0;
  96. ui64 PoolDestroyedActors = 0;
  97. ui64 PoolAllocatedMailboxes = 0;
  98. ui64 MailboxPushedOutByTailSending = 0;
  99. ui64 MailboxPushedOutBySoftPreemption = 0;
  100. ui64 MailboxPushedOutByTime = 0;
  101. ui64 MailboxPushedOutByEventCount = 0;
  102. ui64 NotEnoughCpuExecutions = 0;
  103. TExecutorThreadStats() // must be not empty as 0 used as default
  104. : ElapsedTicksByActivity(TLocalProcessKeyStateIndexLimiter::GetMaxKeysCount())
  105. , ReceivedEventsByActivity(TLocalProcessKeyStateIndexLimiter::GetMaxKeysCount())
  106. , ActorsAliveByActivity(TLocalProcessKeyStateIndexLimiter::GetMaxKeysCount())
  107. , ScheduledEventsByActivity(TLocalProcessKeyStateIndexLimiter::GetMaxKeysCount())
  108. , StuckActorsByActivity(TLocalProcessKeyStateIndexLimiter::GetMaxKeysCount())
  109. , UsageByActivity(TLocalProcessKeyStateIndexLimiter::GetMaxKeysCount())
  110. {}
  111. template <typename T>
  112. static void AggregateOne(TVector<T>& self, const TVector<T>& other) {
  113. const size_t selfSize = self.size();
  114. const size_t otherSize = other.size();
  115. if (selfSize < otherSize)
  116. self.resize(otherSize);
  117. for (size_t at = 0; at < otherSize; ++at)
  118. self[at] += RelaxedLoad(&other[at]);
  119. }
  120. void Aggregate(const TExecutorThreadStats& other) {
  121. SentEvents += RelaxedLoad(&other.SentEvents);
  122. ReceivedEvents += RelaxedLoad(&other.ReceivedEvents);
  123. PreemptedEvents += RelaxedLoad(&other.PreemptedEvents);
  124. NonDeliveredEvents += RelaxedLoad(&other.NonDeliveredEvents);
  125. EmptyMailboxActivation += RelaxedLoad(&other.EmptyMailboxActivation);
  126. CpuUs += RelaxedLoad(&other.CpuUs);
  127. SafeElapsedTicks += RelaxedLoad(&other.SafeElapsedTicks);
  128. RelaxedStore(
  129. &WorstActivationTimeUs,
  130. std::max(RelaxedLoad(&WorstActivationTimeUs), RelaxedLoad(&other.WorstActivationTimeUs)));
  131. ElapsedTicks += RelaxedLoad(&other.ElapsedTicks);
  132. ParkedTicks += RelaxedLoad(&other.ParkedTicks);
  133. BlockedTicks += RelaxedLoad(&other.BlockedTicks);
  134. MailboxPushedOutByTailSending += RelaxedLoad(&other.MailboxPushedOutByTailSending);
  135. MailboxPushedOutBySoftPreemption += RelaxedLoad(&other.MailboxPushedOutBySoftPreemption);
  136. MailboxPushedOutByTime += RelaxedLoad(&other.MailboxPushedOutByTime);
  137. MailboxPushedOutByEventCount += RelaxedLoad(&other.MailboxPushedOutByEventCount);
  138. NotEnoughCpuExecutions += RelaxedLoad(&other.NotEnoughCpuExecutions);
  139. ActivationTimeHistogram.Aggregate(other.ActivationTimeHistogram);
  140. EventDeliveryTimeHistogram.Aggregate(other.EventDeliveryTimeHistogram);
  141. EventProcessingCountHistogram.Aggregate(other.EventProcessingCountHistogram);
  142. EventProcessingTimeHistogram.Aggregate(other.EventProcessingTimeHistogram);
  143. AggregateOne(ElapsedTicksByActivity, other.ElapsedTicksByActivity);
  144. AggregateOne(ReceivedEventsByActivity, other.ReceivedEventsByActivity);
  145. AggregateOne(ActorsAliveByActivity, other.ActorsAliveByActivity);
  146. AggregateOne(ScheduledEventsByActivity, other.ScheduledEventsByActivity);
  147. AggregateOne(StuckActorsByActivity, other.StuckActorsByActivity);
  148. if (UsageByActivity.size() < other.UsageByActivity.size()) {
  149. UsageByActivity.resize(other.UsageByActivity.size());
  150. }
  151. for (size_t i = 0; i < UsageByActivity.size(); ++i) {
  152. for (size_t j = 0; j < 10; ++j) {
  153. UsageByActivity[i][j] += RelaxedLoad(&other.UsageByActivity[i][j]);
  154. }
  155. }
  156. RelaxedStore(
  157. &PoolActorRegistrations,
  158. std::max(RelaxedLoad(&PoolActorRegistrations), RelaxedLoad(&other.PoolActorRegistrations)));
  159. RelaxedStore(
  160. &PoolDestroyedActors,
  161. std::max(RelaxedLoad(&PoolDestroyedActors), RelaxedLoad(&other.PoolDestroyedActors)));
  162. RelaxedStore(
  163. &PoolAllocatedMailboxes,
  164. std::max(RelaxedLoad(&PoolAllocatedMailboxes), RelaxedLoad(&other.PoolAllocatedMailboxes)));
  165. }
  166. size_t MaxActivityType() const {
  167. return ActorsAliveByActivity.size();
  168. }
  169. };
  170. }