691ad2da351d108f912a969d5d8a926cc01ce841
[folly.git] / folly / detail / Stats.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <chrono>
20 #include <cstdint>
21 #include <type_traits>
22
23 namespace folly { namespace detail {
24
25 /*
26  * Helper function to compute the average, given a specified input type and
27  * return type.
28  */
29
30 // If the input is long double, divide using long double to avoid losing
31 // precision.
32 template <typename ReturnType>
33 ReturnType avgHelper(long double sum, uint64_t count) {
34   if (count == 0) { return ReturnType(0); }
35   const long double countf = count;
36   return static_cast<ReturnType>(sum / countf);
37 }
38
39 // In all other cases divide using double precision.
40 // This should be relatively fast, and accurate enough for most use cases.
41 template <typename ReturnType, typename ValueType>
42 typename std::enable_if<!std::is_same<typename std::remove_cv<ValueType>::type,
43                                       long double>::value,
44                         ReturnType>::type
45 avgHelper(ValueType sum, uint64_t count) {
46   if (count == 0) { return ReturnType(0); }
47   const double sumf = double(sum);
48   const double countf = double(count);
49   return static_cast<ReturnType>(sumf / countf);
50 }
51
52 /*
53  * Helper function to compute the rate per Interval,
54  * given the specified count recorded over the elapsed time period.
55  */
56 template <
57     typename ReturnType = double,
58     typename Duration = std::chrono::seconds,
59     typename Interval = Duration>
60 ReturnType rateHelper(ReturnType count, Duration elapsed) {
61   if (elapsed == Duration(0)) {
62     return 0;
63   }
64
65   // Use std::chrono::duration_cast to convert between the native
66   // duration and the desired interval.  However, convert the rates,
67   // rather than just converting the elapsed duration.  Converting the
68   // elapsed time first may collapse it down to 0 if the elapsed interval
69   // is less than the desired interval, which will incorrectly result in
70   // an infinite rate.
71   typedef std::chrono::duration<
72       ReturnType,
73       std::ratio<Duration::period::den, Duration::period::num>>
74       NativeRate;
75   typedef std::chrono::duration<
76       ReturnType, std::ratio<Interval::period::den,
77                              Interval::period::num>> DesiredRate;
78
79   NativeRate native(count / elapsed.count());
80   DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
81   return desired.count();
82 }
83
84
85 template<typename T>
86 struct Bucket {
87  public:
88   typedef T ValueType;
89
90   Bucket()
91     : sum(ValueType()),
92       count(0) {}
93
94   void clear() {
95     sum = ValueType();
96     count = 0;
97   }
98
99   void add(const ValueType& s, uint64_t c) {
100     // TODO: It would be nice to handle overflow here.
101     sum += s;
102     count += c;
103   }
104
105   Bucket& operator+=(const Bucket& o) {
106     add(o.sum, o.count);
107     return *this;
108   }
109
110   Bucket& operator-=(const Bucket& o) {
111     // TODO: It would be nice to handle overflow here.
112     sum -= o.sum;
113     count -= o.count;
114     return *this;
115   }
116
117   template <typename ReturnType>
118   ReturnType avg() const {
119     return avgHelper<ReturnType>(sum, count);
120   }
121
122   ValueType sum;
123   uint64_t count;
124 };
125
126 }} // folly::detail