percentile_lg.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include <util/generic/bitops.h>
  3. #include <cmath>
  4. #include "percentile_base.h"
  5. namespace NMonitoring {
  6. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. // Percentile tracker for monitoring
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. template <size_t BASE_BITS, size_t EXP_BITS, size_t FRAME_COUNT>
  10. struct TPercentileTrackerLg : public TPercentileBase {
  11. static constexpr size_t BUCKET_COUNT = size_t(1) << EXP_BITS;
  12. static constexpr size_t BUCKET_SIZE = size_t(1) << BASE_BITS;
  13. static constexpr size_t ITEMS_COUNT = BUCKET_COUNT * BUCKET_SIZE;
  14. static constexpr size_t TRACKER_LIMIT = BUCKET_SIZE * ((size_t(1) << BUCKET_COUNT) - 1)
  15. - (size_t(1) << (BUCKET_COUNT - 1));
  16. static constexpr size_t MAX_GRANULARITY = size_t(1) << (BUCKET_COUNT - 1);
  17. size_t Borders[BUCKET_COUNT];
  18. std::atomic<size_t> Items[ITEMS_COUNT];
  19. size_t Frame[FRAME_COUNT][ITEMS_COUNT];
  20. size_t CurrentFrame;
  21. TPercentileTrackerLg()
  22. : CurrentFrame(0)
  23. {
  24. Borders[0] = 0;
  25. for (size_t i = 1; i < BUCKET_COUNT; ++i) {
  26. Borders[i] = Borders[i-1] + (BUCKET_SIZE << (i - 1));
  27. }
  28. for (size_t i = 0; i < ITEMS_COUNT; ++i) {
  29. Items[i].store(0);
  30. }
  31. for (size_t frame = 0; frame < FRAME_COUNT; ++frame) {
  32. for (size_t bucket = 0; bucket < ITEMS_COUNT; ++bucket) {
  33. Frame[frame][bucket] = 0;
  34. }
  35. }
  36. }
  37. size_t inline BucketIdxIf(size_t value) {
  38. static_assert(BASE_BITS == 5, "if-based bucket calculation cannot be used if BASE_BITS != 5");
  39. size_t bucket_idx;
  40. if (value < 8160) {
  41. if (value < 480) {
  42. if (value < 96) {
  43. if (value < 32) {
  44. bucket_idx = 0;
  45. } else {
  46. bucket_idx = 1;
  47. }
  48. } else {
  49. if (value < 224) {
  50. bucket_idx = 2;
  51. } else {
  52. bucket_idx = 3;
  53. }
  54. }
  55. } else {
  56. if (value < 2016) {
  57. if (value < 992) {
  58. bucket_idx = 4;
  59. } else {
  60. bucket_idx = 5;
  61. }
  62. } else {
  63. if (value < 4064) {
  64. bucket_idx = 6;
  65. } else {
  66. bucket_idx = 7;
  67. }
  68. }
  69. }
  70. } else {
  71. if (value < 131040) {
  72. if (value < 32736) {
  73. if (value < 16352) {
  74. bucket_idx = 8;
  75. } else {
  76. bucket_idx = 9;
  77. }
  78. } else {
  79. if (value < 65504) {
  80. bucket_idx = 10;
  81. } else {
  82. bucket_idx = 11;
  83. }
  84. }
  85. } else {
  86. if (value < 524256) {
  87. if (value < 262112) {
  88. bucket_idx = 12;
  89. } else {
  90. bucket_idx = 13;
  91. }
  92. } else {
  93. if (value < 1048544) {
  94. bucket_idx = 14;
  95. } else {
  96. bucket_idx = 15;
  97. }
  98. }
  99. }
  100. }
  101. return Min(bucket_idx, BUCKET_COUNT - 1);
  102. }
  103. size_t inline BucketIdxBinarySearch(size_t value) {
  104. size_t l = 0;
  105. size_t r = BUCKET_COUNT;
  106. while (l < r - 1) {
  107. size_t mid = (l + r) / 2;
  108. if (value < Borders[mid]) {
  109. r = mid;
  110. } else {
  111. l = mid;
  112. }
  113. }
  114. return l;
  115. }
  116. size_t inline BucketIdxMostSignificantBit(size_t value) {
  117. size_t bucket_idx = MostSignificantBit(value + BUCKET_SIZE) - BASE_BITS;
  118. return Min(bucket_idx, BUCKET_COUNT - 1);
  119. }
  120. void Increment(size_t value) {
  121. size_t bucket_idx = BucketIdxMostSignificantBit(value);
  122. size_t inside_bucket_idx = (value - Borders[bucket_idx] + (1 << bucket_idx) - 1) >> bucket_idx;
  123. size_t idx = bucket_idx * BUCKET_SIZE + inside_bucket_idx;
  124. Items[Min(idx, ITEMS_COUNT - 1)].fetch_add(1, std::memory_order_relaxed);
  125. }
  126. // Needed only for tests
  127. size_t GetPercentile(float threshold) {
  128. std::array<size_t, ITEMS_COUNT> totals;
  129. size_t total = 0;
  130. for (size_t i = 0; i < ITEMS_COUNT; ++i) {
  131. total += Items[i].load();
  132. totals[i] = total;
  133. }
  134. size_t item_threshold = std::llround(threshold * (float)total);
  135. item_threshold = Min(item_threshold, total);
  136. auto it = LowerBound(totals.begin(), totals.end(), item_threshold);
  137. size_t index = it - totals.begin();
  138. size_t bucket_idx = index / BUCKET_SIZE;
  139. return Borders[bucket_idx] + ((index % BUCKET_SIZE) << bucket_idx);
  140. }
  141. // shift frame (call periodically)
  142. void Update() {
  143. std::array<size_t, ITEMS_COUNT> totals;
  144. size_t total = 0;
  145. for (size_t i = 0; i < ITEMS_COUNT; ++i) {
  146. size_t item = Items[i].load(std::memory_order_relaxed);
  147. size_t prevItem = Frame[CurrentFrame][i];
  148. Frame[CurrentFrame][i] = item;
  149. total += item - prevItem;
  150. totals[i] = total;
  151. }
  152. for (size_t i = 0; i < Percentiles.size(); ++i) {
  153. TPercentile &percentile = Percentiles[i];
  154. size_t threshold = std::llround(percentile.first * (float)total);
  155. threshold = Min(threshold, total);
  156. auto it = LowerBound(totals.begin(), totals.end(), threshold);
  157. size_t index = it - totals.begin();
  158. size_t bucket_idx = index / BUCKET_SIZE;
  159. (*percentile.second) = Borders[bucket_idx] + ((index % BUCKET_SIZE) << bucket_idx);
  160. }
  161. CurrentFrame = (CurrentFrame + 1) % FRAME_COUNT;
  162. }
  163. };
  164. } // NMonitoring