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.
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.
12 #include "monitoring/histogram.h"
13 #include "rocksdb/env.h"
17 class HistogramWindowingImpl
: public Histogram
20 HistogramWindowingImpl();
21 HistogramWindowingImpl(uint64_t num_windows
,
22 uint64_t micros_per_window
,
23 uint64_t min_num_per_window
);
25 HistogramWindowingImpl(const HistogramImpl
&) = delete;
26 HistogramWindowingImpl
& operator=(const HistogramImpl
&) = delete;
28 ~HistogramWindowingImpl();
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
);
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
;
49 void SwapHistoryBucket();
50 inline uint64_t current_window() const {
51 return current_window_
.load(std::memory_order_relaxed
);
53 inline uint64_t last_swap_time() const{
54 return last_swap_time_
.load(std::memory_order_relaxed
);
60 // Aggregated stats over windows_stats_, all the computation is done
61 // upon aggregated values
64 // This is a circular array representing the latest N time-windows.
65 // Each entry stores a time-window of data. Expiration is done
67 std::unique_ptr
<HistogramStat
[]> window_stats_
;
69 std::atomic_uint_fast64_t current_window_
;
70 std::atomic_uint_fast64_t last_swap_time_
;
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;
80 } // namespace rocksdb