histogram_windowing.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include "rocksdb/env.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. class HistogramWindowingImpl : public Histogram
  14. {
  15. public:
  16. HistogramWindowingImpl();
  17. HistogramWindowingImpl(uint64_t num_windows,
  18. uint64_t micros_per_window,
  19. uint64_t min_num_per_window);
  20. HistogramWindowingImpl(const HistogramWindowingImpl&) = delete;
  21. HistogramWindowingImpl& operator=(const HistogramWindowingImpl&) = delete;
  22. ~HistogramWindowingImpl();
  23. virtual void Clear() override;
  24. virtual bool Empty() const override;
  25. virtual void Add(uint64_t value) override;
  26. virtual void Merge(const Histogram& other) override;
  27. void Merge(const HistogramWindowingImpl& other);
  28. virtual std::string ToString() const override;
  29. virtual const char* Name() const override { return "HistogramWindowingImpl"; }
  30. virtual uint64_t min() const override { return stats_.min(); }
  31. virtual uint64_t max() const override { return stats_.max(); }
  32. virtual uint64_t num() const override { return stats_.num(); }
  33. virtual double Median() const override;
  34. virtual double Percentile(double p) const override;
  35. virtual double Average() const override;
  36. virtual double StandardDeviation() const override;
  37. virtual void Data(HistogramData* const data) const override;
  38. private:
  39. void TimerTick();
  40. void SwapHistoryBucket();
  41. inline uint64_t current_window() const {
  42. return current_window_.load(std::memory_order_relaxed);
  43. }
  44. inline uint64_t last_swap_time() const{
  45. return last_swap_time_.load(std::memory_order_relaxed);
  46. }
  47. Env* env_;
  48. std::mutex mutex_;
  49. // Aggregated stats over windows_stats_, all the computation is done
  50. // upon aggregated values
  51. HistogramStat stats_;
  52. // This is a circular array representing the latest N time-windows.
  53. // Each entry stores a time-window of data. Expiration is done
  54. // on window-based.
  55. std::unique_ptr<HistogramStat[]> window_stats_;
  56. std::atomic_uint_fast64_t current_window_;
  57. std::atomic_uint_fast64_t last_swap_time_;
  58. // Following parameters are configuable
  59. uint64_t num_windows_ = 5;
  60. uint64_t micros_per_window_ = 60000000;
  61. // By default, don't care about the number of values in current window
  62. // when decide whether to swap windows or not.
  63. uint64_t min_num_per_window_ = 0;
  64. };
  65. } // namespace ROCKSDB_NAMESPACE