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