move rateHelper() to detail/Stats.h
[folly.git] / folly / detail / Stats.h
1 /*
2  * Copyright 2013 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 #ifndef FOLLY_DETAIL_STATS_H_
18 #define FOLLY_DETAIL_STATS_H_
19
20 #include <chrono>
21 #include <cstdint>
22 #include <type_traits>
23
24 namespace folly { namespace detail {
25
26 /*
27  * Helper function to compute the average, given a specified input type and
28  * return type.
29  */
30
31 // If the input is long double, divide using long double to avoid losing
32 // precision.
33 template <typename ReturnType>
34 ReturnType avgHelper(long double sum, uint64_t count) {
35   if (count == 0) { return ReturnType(0); }
36   const long double countf = count;
37   return static_cast<ReturnType>(sum / countf);
38 }
39
40 // In all other cases divide using double precision.
41 // This should be relatively fast, and accurate enough for most use cases.
42 template <typename ReturnType, typename ValueType>
43 typename std::enable_if<!std::is_same<typename std::remove_cv<ValueType>::type,
44                                       long double>::value,
45                         ReturnType>::type
46 avgHelper(ValueType sum, uint64_t count) {
47   if (count == 0) { return ReturnType(0); }
48   const double sumf = sum;
49   const double countf = count;
50   return static_cast<ReturnType>(sumf / countf);
51 }
52
53 /*
54  * Helper function to compute the rate per Interval,
55  * given the specified count recorded over the elapsed time period.
56  */
57 template <typename ReturnType=double,
58           typename TimeType=std::chrono::seconds,
59           typename Interval=TimeType>
60 ReturnType rateHelper(ReturnType count, TimeType elapsed) {
61   if (elapsed == TimeType(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, std::ratio<TimeType::period::den,
73                              TimeType::period::num>> NativeRate;
74   typedef std::chrono::duration<
75       ReturnType, std::ratio<Interval::period::den,
76                              Interval::period::num>> DesiredRate;
77
78   NativeRate native(count / elapsed.count());
79   DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
80   return desired.count();
81 }
82
83
84 template<typename T>
85 struct Bucket {
86  public:
87   typedef T ValueType;
88
89   Bucket()
90     : sum(ValueType()),
91       count(0) {}
92
93   void clear() {
94     sum = ValueType();
95     count = 0;
96   }
97
98   void add(const ValueType& s, uint64_t c) {
99     // TODO: It would be nice to handle overflow here.
100     sum += s;
101     count += c;
102   }
103
104   Bucket& operator+=(const Bucket& o) {
105     add(o.sum, o.count);
106     return *this;
107   }
108
109   Bucket& operator-=(const Bucket& o) {
110     // TODO: It would be nice to handle overflow here.
111     sum -= o.sum;
112     count -= o.count;
113     return *this;
114   }
115
116   template <typename ReturnType>
117   ReturnType avg() const {
118     return avgHelper<ReturnType>(sum, count);
119   }
120
121   ValueType sum;
122   uint64_t count;
123 };
124
125 }} // folly::detail
126
127 #endif // FOLLY_DETAIL_STATS_H_