2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_DETAIL_STATS_H_
18 #define FOLLY_DETAIL_STATS_H_
22 #include <type_traits>
24 namespace folly { namespace detail {
27 * Helper function to compute the average, given a specified input type and
31 // If the input is long double, divide using long double to avoid losing
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);
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,
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);
54 * Helper function to compute the rate per Interval,
55 * given the specified count recorded over the elapsed time period.
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)) {
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
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;
78 NativeRate native(count / elapsed.count());
79 DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
80 return desired.count();
98 void add(const ValueType& s, uint64_t c) {
99 // TODO: It would be nice to handle overflow here.
104 Bucket& operator+=(const Bucket& o) {
109 Bucket& operator-=(const Bucket& o) {
110 // TODO: It would be nice to handle overflow here.
116 template <typename ReturnType>
117 ReturnType avg() const {
118 return avgHelper<ReturnType>(sum, count);
127 #endif // FOLLY_DETAIL_STATS_H_