histogram_windowing.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2013, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include "monitoring/histogram.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class SystemClock;
  13. class HistogramWindowingImpl : public Histogram {
  14. public:
  15. HistogramWindowingImpl();
  16. HistogramWindowingImpl(uint64_t num_windows, uint64_t micros_per_window,
  17. uint64_t min_num_per_window);
  18. HistogramWindowingImpl(const HistogramWindowingImpl&) = delete;
  19. HistogramWindowingImpl& operator=(const HistogramWindowingImpl&) = delete;
  20. ~HistogramWindowingImpl();
  21. void Clear() override;
  22. bool Empty() const override;
  23. void Add(uint64_t value) override;
  24. void Merge(const Histogram& other) override;
  25. void Merge(const HistogramWindowingImpl& other);
  26. std::string ToString() const override;
  27. const char* Name() const override { return "HistogramWindowingImpl"; }
  28. uint64_t min() const override { return stats_.min(); }
  29. uint64_t max() const override { return stats_.max(); }
  30. uint64_t num() const override { return stats_.num(); }
  31. double Median() const override;
  32. double Percentile(double p) const override;
  33. double Average() const override;
  34. double StandardDeviation() const override;
  35. void Data(HistogramData* const data) const override;
  36. #ifndef NDEBUG
  37. void TEST_UpdateClock(const std::shared_ptr<SystemClock>& clock) {
  38. clock_ = clock;
  39. }
  40. #endif // NDEBUG
  41. private:
  42. void TimerTick();
  43. void SwapHistoryBucket();
  44. inline uint64_t current_window() const {
  45. return current_window_.load(std::memory_order_relaxed);
  46. }
  47. inline uint64_t last_swap_time() const {
  48. return last_swap_time_.load(std::memory_order_relaxed);
  49. }
  50. std::shared_ptr<SystemClock> clock_;
  51. std::mutex mutex_;
  52. // Aggregated stats over windows_stats_, all the computation is done
  53. // upon aggregated values
  54. HistogramStat stats_;
  55. // This is a circular array representing the latest N time-windows.
  56. // Each entry stores a time-window of data. Expiration is done
  57. // on window-based.
  58. std::unique_ptr<HistogramStat[]> window_stats_;
  59. std::atomic_uint_fast64_t current_window_;
  60. std::atomic_uint_fast64_t last_swap_time_;
  61. // Following parameters are configuable
  62. uint64_t num_windows_ = 5;
  63. uint64_t micros_per_window_ = 60000000;
  64. // By default, don't care about the number of values in current window
  65. // when decide whether to swap windows or not.
  66. uint64_t min_num_per_window_ = 0;
  67. };
  68. } // namespace ROCKSDB_NAMESPACE