histogram.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "histogram.h"
  2. #include <util/generic/cast.h>
  3. #include <util/generic/yexception.h>
  4. #include <contrib/libs/hdr_histogram/src/hdr_histogram.h>
  5. namespace NHdr {
  6. namespace {
  7. struct hdr_histogram* CreateHistogram(
  8. i64 lowestDiscernibleValue, i64 highestTrackableValue,
  9. i32 numberOfSignificantValueDigits, IAllocator* allocator) {
  10. struct hdr_histogram_bucket_config cfg;
  11. int r = hdr_calculate_bucket_config(
  12. lowestDiscernibleValue, highestTrackableValue,
  13. numberOfSignificantValueDigits, &cfg);
  14. if (r) {
  15. ythrow yexception() << "illegal arguments values";
  16. }
  17. size_t histogramSize = sizeof(struct hdr_histogram) +
  18. cfg.counts_len * sizeof(i64);
  19. IAllocator::TBlock mem = allocator->Allocate(histogramSize);
  20. struct hdr_histogram* histogram =
  21. reinterpret_cast<struct hdr_histogram*>(mem.Data);
  22. // memset will ensure that all of the function pointers are null
  23. memset(histogram, 0, histogramSize);
  24. hdr_init_preallocated(histogram, &cfg);
  25. return histogram;
  26. }
  27. }
  28. THistogram::THistogram(i64 lowestDiscernibleValue, i64 highestTrackableValue,
  29. i32 numberOfSignificantValueDigits, IAllocator* allocator)
  30. : Data_(CreateHistogram(
  31. lowestDiscernibleValue, highestTrackableValue,
  32. numberOfSignificantValueDigits, allocator))
  33. , Allocator_(allocator)
  34. {
  35. }
  36. THistogram::~THistogram() {
  37. if (Data_) {
  38. size_t size = GetMemorySize();
  39. Allocator_->Release({Data_.Release(), size});
  40. }
  41. }
  42. // Histogram structure querying support -----------------------------------
  43. i64 THistogram::GetLowestDiscernibleValue() const {
  44. return Data_->lowest_trackable_value;
  45. }
  46. i64 THistogram::GetHighestTrackableValue() const {
  47. return Data_->highest_trackable_value;
  48. }
  49. i32 THistogram::GetNumberOfSignificantValueDigits() const {
  50. return Data_->significant_figures;
  51. }
  52. size_t THistogram::GetMemorySize() const {
  53. return hdr_get_memory_size(Data_.Get());
  54. }
  55. i32 THistogram::GetCountsLen() const {
  56. return Data_->counts_len;
  57. }
  58. i64 THistogram::GetTotalCount() const {
  59. return Data_->total_count;
  60. }
  61. // Value recording support ------------------------------------------------
  62. bool THistogram::RecordValue(i64 value) {
  63. return hdr_record_value(Data_.Get(), value);
  64. }
  65. bool THistogram::RecordValues(i64 value, i64 count) {
  66. return hdr_record_values(Data_.Get(), value, count);
  67. }
  68. bool THistogram::RecordValueWithExpectedInterval(i64 value, i64 expectedInterval) {
  69. return hdr_record_corrected_value(Data_.Get(), value, expectedInterval);
  70. }
  71. bool THistogram::RecordValuesWithExpectedInterval(
  72. i64 value, i64 count, i64 expectedInterval) {
  73. return hdr_record_corrected_values(
  74. Data_.Get(), value, count, expectedInterval);
  75. }
  76. i64 THistogram::Add(const THistogram& rhs) {
  77. return hdr_add(Data_.Get(), rhs.Data_.Get());
  78. }
  79. i64 THistogram::AddWithExpectedInterval(const THistogram& rhs, i64 expectedInterval) {
  80. return hdr_add_while_correcting_for_coordinated_omission(
  81. Data_.Get(), rhs.Data_.Get(), expectedInterval);
  82. }
  83. // Histogram Data access support ------------------------------------------
  84. i64 THistogram::GetMin() const {
  85. return hdr_min(Data_.Get());
  86. }
  87. i64 THistogram::GetMax() const {
  88. return hdr_max(Data_.Get());
  89. }
  90. double THistogram::GetMean() const {
  91. return hdr_mean(Data_.Get());
  92. }
  93. double THistogram::GetStdDeviation() const {
  94. return hdr_stddev(Data_.Get());
  95. }
  96. i64 THistogram::GetValueAtPercentile(double percentile) const {
  97. return hdr_value_at_percentile(Data_.Get(), percentile);
  98. }
  99. i64 THistogram::GetCountAtValue(i64 value) const {
  100. return hdr_count_at_value(Data_.Get(), value);
  101. }
  102. bool THistogram::ValuesAreEqual(i64 v1, i64 v2) const {
  103. return hdr_values_are_equivalent(Data_.Get(), v1, v2);
  104. }
  105. i64 THistogram::GetLowestEquivalentValue(i64 value) const {
  106. return hdr_lowest_equivalent_value(Data_.Get(), value);
  107. }
  108. i64 THistogram::GetHighestEquivalentValue(i64 value) const {
  109. return hdr_next_non_equivalent_value(Data_.Get(), value) - 1;
  110. }
  111. i64 THistogram::GetMedianEquivalentValue(i64 value) const {
  112. return hdr_median_equivalent_value(Data_.Get(), value);
  113. }
  114. void THistogram::Reset() {
  115. hdr_reset(Data_.Get());
  116. }
  117. }