Commit 344f49c4 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Apply clang-format to folly/stats/

Summary: [Folly] Apply clang-format to `folly/stats/`.

Reviewed By: Orvid

Differential Revision: D5366745

fbshipit-source-id: 3b7419d4ab4c6203693603722cd8e707741d3953
parent 1fd03592
......@@ -173,8 +173,8 @@ size_t BucketedTimeSeries<VT, CT>::updateBuckets(TimePoint now) {
size_t currentBucket;
TimePoint currentBucketStart;
TimePoint nextBucketStart;
getBucketInfo(latestTime_, &currentBucket,
&currentBucketStart, &nextBucketStart);
getBucketInfo(
latestTime_, &currentBucket, &currentBucketStart, &nextBucketStart);
// Update latestTime_
latestTime_ = now;
......@@ -246,8 +246,8 @@ typename CT::time_point BucketedTimeSeries<VT, CT>::getEarliestTimeNonEmpty()
size_t currentBucket;
TimePoint currentBucketStart;
TimePoint nextBucketStart;
getBucketInfo(latestTime_, &currentBucket,
&currentBucketStart, &nextBucketStart);
getBucketInfo(
latestTime_, &currentBucket, &currentBucketStart, &nextBucketStart);
// Subtract 1 duration from the start of the next bucket to find the
// earliest possible data point we could be tracking.
......@@ -500,7 +500,7 @@ VT BucketedTimeSeries<VT, CT>::rangeAdjust(
TimePoint intervalStart = std::max(start, bucketStart);
TimePoint intervalEnd = std::min(end, nextBucketStart);
return input * (intervalEnd - intervalStart) /
(nextBucketStart - bucketStart);
(nextBucketStart - bucketStart);
}
template <typename VT, typename CT>
......
......@@ -293,7 +293,7 @@ class BucketedTimeSeries {
* Note that you generally should call update() before calling avg(), to
* make sure you are not reading stale data.
*/
template <typename ReturnType=double>
template <typename ReturnType = double>
ReturnType avg() const {
return total_.template avg<ReturnType>();
}
......@@ -483,8 +483,8 @@ class BucketedTimeSeries {
TimePoint latestTime_; // time of last update()
Duration duration_; // total duration ("window length") of the time series
Bucket total_; // sum and count of everything in time series
std::vector<Bucket> buckets_; // actual buckets of values
Bucket total_; // sum and count of everything in time series
std::vector<Bucket> buckets_; // actual buckets of values
};
} // folly
......@@ -26,13 +26,12 @@ namespace folly {
namespace detail {
template <typename T, typename BucketT>
HistogramBuckets<T, BucketT>::HistogramBuckets(ValueType bucketSize,
ValueType min,
ValueType max,
const BucketType& defaultBucket)
: bucketSize_(bucketSize),
min_(min),
max_(max) {
HistogramBuckets<T, BucketT>::HistogramBuckets(
ValueType bucketSize,
ValueType min,
ValueType max,
const BucketType& defaultBucket)
: bucketSize_(bucketSize), min_(min), max_(max) {
CHECK_GT(bucketSize_, ValueType(0));
CHECK_LT(min_, max_);
......@@ -88,7 +87,7 @@ size_t HistogramBuckets<T, BucketType>::getPercentileBucketIdx(
uint64_t totalCount = 0;
for (size_t n = 0; n < numBuckets; ++n) {
uint64_t bucketCount =
countFromBucket(const_cast<const BucketType&>(buckets_[n]));
countFromBucket(const_cast<const BucketType&>(buckets_[n]));
counts[n] = bucketCount;
totalCount += bucketCount;
}
......@@ -146,7 +145,6 @@ T HistogramBuckets<T, BucketType>::getPercentileEstimate(
double pct,
CountFn countFromBucket,
AvgFn avgFromBucket) const {
// Find the bucket where this percentile falls
double lowPct;
double highPct;
......@@ -183,8 +181,8 @@ T HistogramBuckets<T, BucketType>::getPercentileEstimate(
// (Note that if the counter keeps being decremented, eventually it will
// wrap and become small enough that we won't detect this any more, and
// we will return bogus information.)
LOG(ERROR) << "invalid average value in histogram minimum bucket: " <<
avg << " > " << min_ << ": possible integer overflow?";
LOG(ERROR) << "invalid average value in histogram minimum bucket: " << avg
<< " > " << min_ << ": possible integer overflow?";
return getBucketMin(bucketIdx);
}
// For the below-min bucket, just assume the lowest value ever seen is
......@@ -199,8 +197,8 @@ T HistogramBuckets<T, BucketType>::getPercentileEstimate(
if (avg < max_) {
// Most likely this means integer overflow occurred. See the comments
// above in the minimum case.
LOG(ERROR) << "invalid average value in histogram maximum bucket: " <<
avg << " < " << max_ << ": possible integer overflow?";
LOG(ERROR) << "invalid average value in histogram maximum bucket: " << avg
<< " < " << max_ << ": possible integer overflow?";
return getBucketMax(bucketIdx);
}
// Similarly for the above-max bucket, assume the highest value ever seen
......@@ -218,9 +216,9 @@ T HistogramBuckets<T, BucketType>::getPercentileEstimate(
// Most likely this means an integer overflow occurred.
// See the comments above. Return the midpoint between low and high
// as a best guess, since avg is meaningless.
LOG(ERROR) << "invalid average value in histogram bucket: " <<
avg << " not in range [" << low << ", " << high <<
"]: possible integer overflow?";
LOG(ERROR) << "invalid average value in histogram bucket: " << avg
<< " not in range [" << low << ", " << high
<< "]: possible integer overflow?";
return (low + high) / 2;
}
}
......@@ -247,18 +245,27 @@ T HistogramBuckets<T, BucketType>::getPercentileEstimate(
} // detail
template <typename T>
std::string Histogram<T>::debugString() const {
std::string ret = folly::to<std::string>(
"num buckets: ", buckets_.getNumBuckets(),
", bucketSize: ", buckets_.getBucketSize(),
", min: ", buckets_.getMin(), ", max: ", buckets_.getMax(), "\n");
"num buckets: ",
buckets_.getNumBuckets(),
", bucketSize: ",
buckets_.getBucketSize(),
", min: ",
buckets_.getMin(),
", max: ",
buckets_.getMax(),
"\n");
for (size_t i = 0; i < buckets_.getNumBuckets(); ++i) {
folly::toAppend(" ", buckets_.getBucketMin(i), ": ",
buckets_.getByIndex(i).count, "\n",
&ret);
folly::toAppend(
" ",
buckets_.getBucketMin(i),
": ",
buckets_.getByIndex(i).count,
"\n",
&ret);
}
return ret;
......@@ -272,8 +279,8 @@ void Histogram<T>::toTSV(std::ostream& out, bool skipEmptyBuckets) const {
continue;
}
const auto& bucket = getBucketByIndex(i);
out << getBucketMin(i) << '\t' << getBucketMax(i) << '\t'
<< bucket.count << '\t' << bucket.sum << '\n';
out << getBucketMin(i) << '\t' << getBucketMax(i) << '\t' << bucket.count
<< '\t' << bucket.sum << '\n';
}
}
......
......@@ -40,14 +40,16 @@ template size_t detail::HistogramBuckets<int64_t, Histogram<int64_t>::Bucket>::
Histogram<int64_t>::CountFromBucket countFromBucket,
double* lowPct,
double* highPct) const;
template int64_t detail::HistogramBuckets<int64_t, Histogram<int64_t>::Bucket>
::getPercentileEstimate<Histogram<int64_t>::CountFromBucket,
Histogram<int64_t>::AvgFromBucket>(
double pct,
Histogram<int64_t>::CountFromBucket countFromBucket,
Histogram<int64_t>::AvgFromBucket avgFromBucket) const;
template uint64_t detail::HistogramBuckets<int64_t, Histogram<int64_t>::Bucket>
::computeTotalCount<Histogram<int64_t>::CountFromBucket>(
Histogram<int64_t>::CountFromBucket countFromBucket) const;
template int64_t detail::HistogramBuckets<int64_t, Histogram<int64_t>::Bucket>::
getPercentileEstimate<
Histogram<int64_t>::CountFromBucket,
Histogram<int64_t>::AvgFromBucket>(
double pct,
Histogram<int64_t>::CountFromBucket countFromBucket,
Histogram<int64_t>::AvgFromBucket avgFromBucket) const;
template uint64_t
detail::HistogramBuckets<int64_t, Histogram<int64_t>::Bucket>::
computeTotalCount<Histogram<int64_t>::CountFromBucket>(
Histogram<int64_t>::CountFromBucket countFromBucket) const;
} // folly
......@@ -53,8 +53,11 @@ class HistogramBuckets {
*
* (max - min) must be larger than or equal to bucketSize.
*/
HistogramBuckets(ValueType bucketSize, ValueType min, ValueType max,
const BucketType& defaultBucket);
HistogramBuckets(
ValueType bucketSize,
ValueType min,
ValueType max,
const BucketType& defaultBucket);
/* Returns the bucket size of each bucket in the histogram. */
ValueType getBucketSize() const {
......@@ -191,9 +194,10 @@ class HistogramBuckets {
* percentage of the data points in the histogram are less than N.
*/
template <typename CountFn, typename AvgFn>
ValueType getPercentileEstimate(double pct,
CountFn countFromBucket,
AvgFn avgFromBucket) const;
ValueType getPercentileEstimate(
double pct,
CountFn countFromBucket,
AvgFn avgFromBucket) const;
/*
* Iterator access to the buckets.
......@@ -224,7 +228,6 @@ class HistogramBuckets {
} // detail
/*
* A basic histogram class.
*
......@@ -242,7 +245,7 @@ class Histogram {
typedef detail::Bucket<T> Bucket;
Histogram(ValueType bucketSize, ValueType min, ValueType max)
: buckets_(bucketSize, min, max, Bucket()) {}
: buckets_(bucketSize, min, max, Bucket()) {}
/* Add a data point to the histogram */
void addValue(ValueType value) FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(
......@@ -313,13 +316,11 @@ class Histogram {
}
/* Subtract another histogram data from the histogram */
void subtract(const Histogram &hist) {
void subtract(const Histogram& hist) {
// the two histogram bucket definitions must match to support
// subtract.
if (getBucketSize() != hist.getBucketSize() ||
getMin() != hist.getMin() ||
getMax() != hist.getMax() ||
getNumBuckets() != hist.getNumBuckets() ) {
if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
throw std::invalid_argument("Cannot subtract input histogram.");
}
......@@ -329,13 +330,11 @@ class Histogram {
}
/* Merge two histogram data together */
void merge(const Histogram &hist) {
void merge(const Histogram& hist) {
// the two histogram bucket definitions must match to support
// a merge.
if (getBucketSize() != hist.getBucketSize() ||
getMin() != hist.getMin() ||
getMax() != hist.getMax() ||
getNumBuckets() != hist.getNumBuckets() ) {
if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
throw std::invalid_argument("Cannot merge from input histogram.");
}
......@@ -345,12 +344,10 @@ class Histogram {
}
/* Copy bucket values from another histogram */
void copy(const Histogram &hist) {
void copy(const Histogram& hist) {
// the two histogram bucket definition must match
if (getBucketSize() != hist.getBucketSize() ||
getMin() != hist.getMin() ||
getMax() != hist.getMax() ||
getNumBuckets() != hist.getNumBuckets() ) {
if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
throw std::invalid_argument("Cannot copy from input histogram.");
}
......
......@@ -113,7 +113,7 @@ void MultiLevelTimeSeries<VT, CT>::flush() {
template <typename VT, typename CT>
void MultiLevelTimeSeries<VT, CT>::clear() {
for (auto & level : levels_) {
for (auto& level : levels_) {
level.clear();
}
......@@ -122,4 +122,4 @@ void MultiLevelTimeSeries<VT, CT>::clear() {
cachedCount_ = 0;
}
} // folly
} // namespace folly
......@@ -89,7 +89,9 @@ class MultiLevelTimeSeries {
/*
* Return the number of levels tracked by MultiLevelTimeSeries.
*/
size_t numLevels() const { return levels_.size(); }
size_t numLevels() const {
return levels_.size();
}
/*
* Get the BucketedTimeSeries backing the specified level.
......
......@@ -62,7 +62,7 @@ void TimeseriesHistogram<T, CT, C>::addValues(
for (size_t n = 0; n < hist.getNumBuckets(); ++n) {
const typename folly::Histogram<ValueType>::Bucket& histBucket =
hist.getBucketByIndex(n);
hist.getBucketByIndex(n);
Bucket& myBucket = buckets_.getByIndex(n);
myBucket.addValueAggregated(now, histBucket.sum, histBucket.count);
}
......@@ -93,8 +93,8 @@ T TimeseriesHistogram<T, CT, C>::getPercentileEstimate(double pct, size_t level)
return firstValue_;
}
return buckets_.getPercentileEstimate(pct / 100.0, CountFromLevel(level),
AvgFromLevel(level));
return buckets_.getPercentileEstimate(
pct / 100.0, CountFromLevel(level), AvgFromLevel(level));
}
template <typename T, typename CT, typename C>
......@@ -106,9 +106,10 @@ T TimeseriesHistogram<T, CT, C>::getPercentileEstimate(
return firstValue_;
}
return buckets_.getPercentileEstimate(pct / 100.0,
CountFromInterval(start, end),
AvgFromInterval<T>(start, end));
return buckets_.getPercentileEstimate(
pct / 100.0,
CountFromInterval(start, end),
AvgFromInterval<T>(start, end));
}
template <typename T, typename CT, typename C>
......@@ -123,8 +124,8 @@ size_t TimeseriesHistogram<T, CT, C>::getPercentileBucketIdx(
double pct,
TimePoint start,
TimePoint end) const {
return buckets_.getPercentileBucketIdx(pct / 100.0,
CountFromInterval(start, end));
return buckets_.getPercentileBucketIdx(
pct / 100.0, CountFromInterval(start, end));
}
template <typename T, typename CT, typename C>
......@@ -150,9 +151,13 @@ std::string TimeseriesHistogram<T, CT, C>::getString(size_t level) const {
toAppend(",", &result);
}
const ContainerType& cont = buckets_.getByIndex(i);
toAppend(buckets_.getBucketMin(i),
":", cont.count(level),
":", cont.template avg<ValueType>(level), &result);
toAppend(
buckets_.getBucketMin(i),
":",
cont.count(level),
":",
cont.template avg<ValueType>(level),
&result);
}
return result;
......@@ -169,9 +174,13 @@ std::string TimeseriesHistogram<T, CT, C>::getString(
toAppend(",", &result);
}
const ContainerType& cont = buckets_.getByIndex(i);
toAppend(buckets_.getBucketMin(i),
":", cont.count(start, end),
":", cont.avg(start, end), &result);
toAppend(
buckets_.getBucketMin(i),
":",
cont.count(start, end),
":",
cont.avg(start, end),
&result);
}
return result;
......@@ -227,4 +236,4 @@ void TimeseriesHistogram<T, CT, C>::computeRateData(
}
}
} // namespace folly
} // namespace folly
......@@ -16,9 +16,9 @@
#pragma once
#include <string>
#include <folly/stats/Histogram.h>
#include <folly/stats/MultiLevelTimeSeries.h>
#include <string>
namespace folly {
......@@ -53,8 +53,8 @@ template <
class C = folly::MultiLevelTimeSeries<T, CT>>
class TimeseriesHistogram {
private:
// NOTE: T must be equivalent to _signed_ numeric type for our math.
static_assert(std::numeric_limits<T>::is_signed, "");
// NOTE: T must be equivalent to _signed_ numeric type for our math.
static_assert(std::numeric_limits<T>::is_signed, "");
public:
// Values to be inserted into container
......@@ -80,17 +80,26 @@ class TimeseriesHistogram {
* @param defaultContainer a pre-initialized timeseries with the desired
* number of levels and their durations.
*/
TimeseriesHistogram(ValueType bucketSize, ValueType min, ValueType max,
const ContainerType& defaultContainer);
TimeseriesHistogram(
ValueType bucketSize,
ValueType min,
ValueType max,
const ContainerType& defaultContainer);
/* Return the bucket size of each bucket in the histogram. */
ValueType getBucketSize() const { return buckets_.getBucketSize(); }
ValueType getBucketSize() const {
return buckets_.getBucketSize();
}
/* Return the min value at which bucketing begins. */
ValueType getMin() const { return buckets_.getMin(); }
ValueType getMin() const {
return buckets_.getMin();
}
/* Return the max value at which bucketing ends. */
ValueType getMax() const { return buckets_.getMax(); }
ValueType getMax() const {
return buckets_.getMax();
}
/* Return the number of levels of the Timeseries object in each bucket */
size_t getNumLevels() const {
......@@ -386,4 +395,4 @@ class TimeseriesHistogram {
bool singleUniqueValue_;
ValueType firstValue_;
};
} // folly
} // namespace folly
......@@ -20,7 +20,8 @@
#include <cstdint>
#include <type_traits>
namespace folly { namespace detail {
namespace folly {
namespace detail {
/*
* Helper function to compute the average, given a specified input type and
......@@ -31,7 +32,9 @@ namespace folly { namespace detail {
// precision.
template <typename ReturnType>
ReturnType avgHelper(long double sum, uint64_t count) {
if (count == 0) { return ReturnType(0); }
if (count == 0) {
return ReturnType(0);
}
const long double countf = count;
return static_cast<ReturnType>(sum / countf);
}
......@@ -39,11 +42,13 @@ ReturnType avgHelper(long double sum, uint64_t count) {
// In all other cases divide using double precision.
// This should be relatively fast, and accurate enough for most use cases.
template <typename ReturnType, typename ValueType>
typename std::enable_if<!std::is_same<typename std::remove_cv<ValueType>::type,
long double>::value,
ReturnType>::type
typename std::enable_if<
!std::is_same<typename std::remove_cv<ValueType>::type, long double>::value,
ReturnType>::type
avgHelper(ValueType sum, uint64_t count) {
if (count == 0) { return ReturnType(0); }
if (count == 0) {
return ReturnType(0);
}
const double sumf = double(sum);
const double countf = double(count);
return static_cast<ReturnType>(sumf / countf);
......@@ -73,23 +78,21 @@ ReturnType rateHelper(ReturnType count, Duration elapsed) {
std::ratio<Duration::period::den, Duration::period::num>>
NativeRate;
typedef std::chrono::duration<
ReturnType, std::ratio<Interval::period::den,
Interval::period::num>> DesiredRate;
ReturnType,
std::ratio<Interval::period::den, Interval::period::num>>
DesiredRate;
NativeRate native(count / elapsed.count());
DesiredRate desired = std::chrono::duration_cast<DesiredRate>(native);
return desired.count();
}
template<typename T>
template <typename T>
struct Bucket {
public:
typedef T ValueType;
Bucket()
: sum(ValueType()),
count(0) {}
Bucket() : sum(ValueType()), count(0) {}
void clear() {
sum = ValueType();
......@@ -122,5 +125,5 @@ struct Bucket {
ValueType sum;
uint64_t count;
};
}} // folly::detail
} // namespace detail
} // namespace folly
......@@ -13,20 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/stats/BucketedTimeSeries.h>
#include <folly/stats/BucketedTimeSeries-defs.h>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/stats/BucketedTimeSeries-defs.h>
using std::chrono::seconds;
using folly::BenchmarkSuspender;
using folly::BucketedTimeSeries;
void addValue(unsigned int iters,
seconds duration, size_t numBuckets,
size_t callsPerSecond) {
void addValue(
unsigned int iters,
seconds duration,
size_t numBuckets,
size_t callsPerSecond) {
BenchmarkSuspender suspend;
BucketedTimeSeries<int64_t> ts(numBuckets, duration);
suspend.dismiss();
......@@ -70,7 +73,7 @@ BENCHMARK_NAMED_PARAM(addValue, 100x10_100perSec, seconds(100), 10, 100);
BENCHMARK_NAMED_PARAM(addValue, 71x5_100perSec, seconds(71), 5, 100);
BENCHMARK_NAMED_PARAM(addValue, 1x1_100perSec, seconds(1), 1, 100);
int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
folly::runBenchmarks();
return 0;
......
......@@ -27,7 +27,9 @@ void addValue(unsigned int n, int64_t bucketSize, int64_t min, int64_t max) {
FOR_EACH_RANGE (i, 0, n) {
hist.addValue(num);
++num;
if (num > max) { num = min; }
if (num > max) {
num = min;
}
}
}
......@@ -35,7 +37,7 @@ BENCHMARK_NAMED_PARAM(addValue, 0_to_100, 1, 0, 100);
BENCHMARK_NAMED_PARAM(addValue, 0_to_1000, 10, 0, 1000);
BENCHMARK_NAMED_PARAM(addValue, 5k_to_20k, 250, 5000, 20000);
int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
folly::runBenchmarks();
return 0;
......
......@@ -15,9 +15,9 @@
*/
#include <folly/stats/Histogram.h>
#include <folly/stats/Histogram-defs.h>
#include <folly/portability/GTest.h>
#include <folly/stats/Histogram-defs.h>
using folly::Histogram;
......@@ -42,8 +42,8 @@ TEST(Histogram, Test100) {
if (n < 100) {
double lowPct = -1.0;
double highPct = -1.0;
unsigned int bucketIdx = h.getPercentileBucketIdx(pct + epsilon,
&lowPct, &highPct);
unsigned int bucketIdx =
h.getPercentileBucketIdx(pct + epsilon, &lowPct, &highPct);
EXPECT_EQ(n + 1, bucketIdx);
EXPECT_FLOAT_EQ(n / 100.0, lowPct);
EXPECT_FLOAT_EQ((n + 1) / 100.0, highPct);
......@@ -53,8 +53,8 @@ TEST(Histogram, Test100) {
if (n > 0) {
double lowPct = -1.0;
double highPct = -1.0;
unsigned int bucketIdx = h.getPercentileBucketIdx(pct - epsilon,
&lowPct, &highPct);
unsigned int bucketIdx =
h.getPercentileBucketIdx(pct - epsilon, &lowPct, &highPct);
EXPECT_EQ(n, bucketIdx);
EXPECT_FLOAT_EQ((n - 1) / 100.0, lowPct);
EXPECT_FLOAT_EQ(n / 100.0, highPct);
......@@ -212,7 +212,7 @@ TEST(Histogram, Counts) {
// Add one to each bucket, make sure the counts match
for (int32_t i = 0; i < 10; i++) {
h.addValue(i);
EXPECT_EQ(i+1, h.computeTotalCount());
EXPECT_EQ(i + 1, h.computeTotalCount());
}
// Add a lot to one bucket, make sure the counts still make sense
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment