]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2013, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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 | ||
10 | #pragma once | |
11 | ||
12 | #include "monitoring/histogram.h" | |
7c673cae | 13 | |
f67539c2 | 14 | namespace ROCKSDB_NAMESPACE { |
1e59de90 | 15 | class SystemClock; |
7c673cae | 16 | |
1e59de90 TL |
17 | class HistogramWindowingImpl : public Histogram { |
18 | public: | |
7c673cae | 19 | HistogramWindowingImpl(); |
1e59de90 | 20 | HistogramWindowingImpl(uint64_t num_windows, uint64_t micros_per_window, |
7c673cae FG |
21 | uint64_t min_num_per_window); |
22 | ||
11fdf7f2 TL |
23 | HistogramWindowingImpl(const HistogramWindowingImpl&) = delete; |
24 | HistogramWindowingImpl& operator=(const HistogramWindowingImpl&) = delete; | |
7c673cae FG |
25 | |
26 | ~HistogramWindowingImpl(); | |
27 | ||
28 | virtual void Clear() override; | |
29 | virtual bool Empty() const override; | |
30 | virtual void Add(uint64_t value) override; | |
31 | virtual void Merge(const Histogram& other) override; | |
32 | void Merge(const HistogramWindowingImpl& other); | |
33 | ||
34 | virtual std::string ToString() const override; | |
35 | virtual const char* Name() const override { return "HistogramWindowingImpl"; } | |
36 | virtual uint64_t min() const override { return stats_.min(); } | |
37 | virtual uint64_t max() const override { return stats_.max(); } | |
38 | virtual uint64_t num() const override { return stats_.num(); } | |
39 | virtual double Median() const override; | |
40 | virtual double Percentile(double p) const override; | |
41 | virtual double Average() const override; | |
42 | virtual double StandardDeviation() const override; | |
43 | virtual void Data(HistogramData* const data) const override; | |
44 | ||
1e59de90 TL |
45 | #ifndef NDEBUG |
46 | void TEST_UpdateClock(const std::shared_ptr<SystemClock>& clock) { | |
47 | clock_ = clock; | |
48 | } | |
49 | #endif // NDEBUG | |
50 | ||
51 | private: | |
7c673cae FG |
52 | void TimerTick(); |
53 | void SwapHistoryBucket(); | |
54 | inline uint64_t current_window() const { | |
55 | return current_window_.load(std::memory_order_relaxed); | |
56 | } | |
1e59de90 | 57 | inline uint64_t last_swap_time() const { |
7c673cae FG |
58 | return last_swap_time_.load(std::memory_order_relaxed); |
59 | } | |
60 | ||
1e59de90 | 61 | std::shared_ptr<SystemClock> clock_; |
7c673cae FG |
62 | std::mutex mutex_; |
63 | ||
64 | // Aggregated stats over windows_stats_, all the computation is done | |
65 | // upon aggregated values | |
66 | HistogramStat stats_; | |
67 | ||
68 | // This is a circular array representing the latest N time-windows. | |
69 | // Each entry stores a time-window of data. Expiration is done | |
70 | // on window-based. | |
71 | std::unique_ptr<HistogramStat[]> window_stats_; | |
72 | ||
73 | std::atomic_uint_fast64_t current_window_; | |
74 | std::atomic_uint_fast64_t last_swap_time_; | |
75 | ||
76 | // Following parameters are configuable | |
77 | uint64_t num_windows_ = 5; | |
78 | uint64_t micros_per_window_ = 60000000; | |
79 | // By default, don't care about the number of values in current window | |
80 | // when decide whether to swap windows or not. | |
81 | uint64_t min_num_per_window_ = 0; | |
82 | }; | |
83 | ||
f67539c2 | 84 | } // namespace ROCKSDB_NAMESPACE |