X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Fdetail%2FStats.h;h=4729eeed4716b136bee7ede199520733dfbdc0c9;hp=3e5ef06ffc5b7aa2bf1e1c5dc42aad4885c1277b;hb=26b90449dd62f5d9d33c697799892eb203c21b51;hpb=e9bf39f0e3d57672e03d01a788f097c76405362e diff --git a/folly/detail/Stats.h b/folly/detail/Stats.h index 3e5ef06f..4729eeed 100644 --- a/folly/detail/Stats.h +++ b/folly/detail/Stats.h @@ -17,6 +17,7 @@ #ifndef FOLLY_DETAIL_STATS_H_ #define FOLLY_DETAIL_STATS_H_ +#include #include #include @@ -49,6 +50,36 @@ avgHelper(ValueType sum, uint64_t count) { return static_cast(sumf / countf); } +/* + * Helper function to compute the rate per Interval, + * given the specified count recorded over the elapsed time period. + */ +template +ReturnType rateHelper(ReturnType count, TimeType elapsed) { + if (elapsed == TimeType(0)) { + return 0; + } + + // Use std::chrono::duration_cast to convert between the native + // duration and the desired interval. However, convert the rates, + // rather than just converting the elapsed duration. Converting the + // elapsed time first may collapse it down to 0 if the elapsed interval + // is less than the desired interval, which will incorrectly result in + // an infinite rate. + typedef std::chrono::duration< + ReturnType, std::ratio> NativeRate; + typedef std::chrono::duration< + ReturnType, std::ratio> DesiredRate; + + NativeRate native(count / elapsed.count()); + DesiredRate desired = std::chrono::duration_cast(native); + return desired.count(); +} + template struct Bucket {