proxygen
folly::Histogram< T > Class Template Reference

#include <Histogram.h>

Classes

struct  AvgFromBucket
 
struct  CountFromBucket
 

Public Types

typedef T ValueType
 
typedef detail::Bucket< TBucket
 

Public Member Functions

 Histogram (ValueType bucketSize, ValueType min, ValueType max)
 
void addValue (ValueType value)
 
void addRepeatedValue (ValueType value, uint64_t nSamples)
 
void removeValue (ValueType value)
 
void removeRepeatedValue (ValueType value, uint64_t nSamples)
 
void clear ()
 
void subtract (const Histogram &hist)
 
void merge (const Histogram &hist)
 
void copy (const Histogram &hist)
 
ValueType getBucketSize () const
 
ValueType getMin () const
 
ValueType getMax () const
 
size_t getNumBuckets () const
 
const BucketgetBucketByIndex (size_t idx) const
 
ValueType getBucketMin (size_t idx) const
 
ValueType getBucketMax (size_t idx) const
 
uint64_t computeTotalCount () const
 
size_t getPercentileBucketIdx (double pct, double *lowPct=nullptr, double *highPct=nullptr) const
 
ValueType getPercentileEstimate (double pct) const
 
std::string debugString () const
 
void toTSV (std::ostream &out, bool skipEmptyBuckets=true) const
 

Static Private Member Functions

template<typename S , typename = _t<std::enable_if<std::is_integral<S>::value>>>
static constexpr _t< std::make_unsigned< S > > to_unsigned (S s)
 
template<typename S , typename = _t<std::enable_if<!std::is_integral<S>::value>>>
static constexpr S to_unsigned (S s)
 

Private Attributes

detail::HistogramBuckets< ValueType, Bucketbuckets_
 

Detailed Description

template<typename T>
class folly::Histogram< T >

Definition at line 243 of file Histogram.h.

Member Typedef Documentation

template<typename T>
typedef detail::Bucket<T> folly::Histogram< T >::Bucket

Definition at line 246 of file Histogram.h.

template<typename T>
typedef T folly::Histogram< T >::ValueType

Definition at line 245 of file Histogram.h.

Constructor & Destructor Documentation

template<typename T>
folly::Histogram< T >::Histogram ( ValueType  bucketSize,
ValueType  min,
ValueType  max 
)
inline

Definition at line 248 of file Histogram.h.

249  : buckets_(bucketSize, min, max, Bucket()) {}
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
detail::Bucket< T > Bucket
Definition: Histogram.h:246
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488

Member Function Documentation

template<typename T>
void folly::Histogram< T >::addRepeatedValue ( ValueType  value,
uint64_t  nSamples 
)
inline

Definition at line 263 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::detail::Bucket< T >::count, folly::detail::Bucket< T >::sum, and folly::to_unsigned().

263  {
264  Bucket& bucket = buckets_.getByValue(value);
265  // NOTE: Overflow is handled elsewhere and tests check this
266  // behavior (see HistogramTest.cpp TestOverflow* tests).
267  // TODO: It would be nice to handle overflow here and redesign this class.
268  auto const addend = to_unsigned(value) * nSamples;
269  bucket.sum = static_cast<ValueType>(to_unsigned(bucket.sum) + addend);
270  bucket.count += nSamples;
271  }
static constexpr _t< std::make_unsigned< S > > to_unsigned(S s)
Definition: Histogram.h:478
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::addValue ( ValueType  value)
inline

Definition at line 252 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::detail::Bucket< T >::count, folly::detail::Bucket< T >::sum, and folly::to_unsigned().

Referenced by addValue(), and TEST().

252  {
253  Bucket& bucket = buckets_.getByValue(value);
254  // NOTE: Overflow is handled elsewhere and tests check this
255  // behavior (see HistogramTest.cpp TestOverflow* tests).
256  // TODO: It would be nice to handle overflow here and redesign this class.
257  auto const addend = to_unsigned(value);
258  bucket.sum = static_cast<ValueType>(to_unsigned(bucket.sum) + addend);
259  bucket.count += 1;
260  }
static constexpr _t< std::make_unsigned< S > > to_unsigned(S s)
Definition: Histogram.h:478
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::clear ( )
inline

Definition at line 309 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, and i.

309  {
310  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
311  buckets_.getByIndex(i).clear();
312  }
313  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
uint64_t folly::Histogram< T >::computeTotalCount ( ) const
inline

Computes the total number of values stored across all buckets.

Runs in O(numBuckets)

Definition at line 405 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by TEST().

405  {
406  CountFromBucket countFn;
407  return buckets_.computeTotalCount(countFn);
408  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::copy ( const Histogram< T > &  hist)
inline

Definition at line 344 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::Histogram< T >::buckets_, folly::detail::HistogramBuckets< T, BucketT >::getBucketSize(), folly::Histogram< T >::getBucketSize(), folly::detail::HistogramBuckets< T, BucketT >::getMax(), folly::Histogram< T >::getMax(), folly::detail::HistogramBuckets< T, BucketT >::getMin(), folly::Histogram< T >::getMin(), folly::detail::HistogramBuckets< T, BucketT >::getNumBuckets(), folly::Histogram< T >::getNumBuckets(), and i.

344  {
345  // the two histogram bucket definition must match
346  if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
347  getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
348  throw std::invalid_argument("Cannot copy from input histogram.");
349  }
350 
351  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
352  buckets_.getByIndex(i) = hist.buckets_.getByIndex(i);
353  }
354  }
ValueType getBucketSize() const
Definition: Histogram.h:357
ValueType getMin() const
Definition: Histogram.h:361
size_t getNumBuckets() const
Definition: Histogram.h:369
ValueType getMax() const
Definition: Histogram.h:365
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T >
std::string folly::Histogram< T >::debugString ( ) const

Definition at line 249 of file Histogram-defs.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, i, string, and folly::toAppend().

249  {
250  std::string ret = folly::to<std::string>(
251  "num buckets: ",
252  buckets_.getNumBuckets(),
253  ", bucketSize: ",
254  buckets_.getBucketSize(),
255  ", min: ",
256  buckets_.getMin(),
257  ", max: ",
258  buckets_.getMax(),
259  "\n");
260 
261  for (size_t i = 0; i < buckets_.getNumBuckets(); ++i) {
263  " ",
264  buckets_.getBucketMin(i),
265  ": ",
266  buckets_.getByIndex(i).count,
267  "\n",
268  &ret);
269  }
270 
271  return ret;
272 }
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
const char * string
Definition: Conv.cpp:212
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
const Bucket& folly::Histogram< T >::getBucketByIndex ( size_t  idx) const
inline

Definition at line 374 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by folly::TimeseriesHistogram< T, CT, C >::addValues(), and TEST().

374  {
375  return buckets_.getByIndex(idx);
376  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
ValueType folly::Histogram< T >::getBucketMax ( size_t  idx) const
inline

Definition at line 396 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

396  {
397  return buckets_.getBucketMax(idx);
398  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
ValueType folly::Histogram< T >::getBucketMin ( size_t  idx) const
inline

Definition at line 385 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

385  {
386  return buckets_.getBucketMin(idx);
387  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
ValueType folly::Histogram< T >::getBucketSize ( ) const
inline

Definition at line 357 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by folly::TimeseriesHistogram< T, CT, C >::addValues(), folly::Histogram< T >::copy(), folly::Histogram< T >::merge(), and folly::Histogram< T >::subtract().

357  {
358  return buckets_.getBucketSize();
359  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
ValueType folly::Histogram< T >::getMax ( ) const
inline

Definition at line 365 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by folly::TimeseriesHistogram< T, CT, C >::addValues(), folly::Histogram< T >::copy(), folly::Histogram< T >::merge(), and folly::Histogram< T >::subtract().

365  {
366  return buckets_.getMax();
367  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
ValueType folly::Histogram< T >::getMin ( ) const
inline

Definition at line 361 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by folly::TimeseriesHistogram< T, CT, C >::addValues(), folly::Histogram< T >::copy(), folly::Histogram< T >::merge(), and folly::Histogram< T >::subtract().

361  {
362  return buckets_.getMin();
363  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
size_t folly::Histogram< T >::getNumBuckets ( ) const
inline

Definition at line 369 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by folly::TimeseriesHistogram< T, CT, C >::addValues(), folly::Histogram< T >::copy(), folly::Histogram< T >::merge(), folly::Histogram< T >::subtract(), and TEST().

369  {
370  return buckets_.getNumBuckets();
371  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
size_t folly::Histogram< T >::getPercentileBucketIdx ( double  pct,
double *  lowPct = nullptr,
double *  highPct = nullptr 
) const
inline

Definition at line 416 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_.

Referenced by TEST().

419  {
420  // We unfortunately can't use lambdas here yet;
421  // Some users of this code are still built with gcc-4.4.
422  CountFromBucket countFn;
423  return buckets_.getPercentileBucketIdx(pct, countFn, lowPct, highPct);
424  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
ValueType folly::Histogram< T >::getPercentileEstimate ( double  pct) const
inline

Estimate the value at the specified percentile.

Parameters
pctThe desired percentile to find, as a value from 0.0 to 1.0.
Returns
Returns an estimate for N, where N is the number where exactly pct percentage of the data points in the histogram are less than N.

Definition at line 434 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, and string.

Referenced by TEST().

434  {
435  CountFromBucket countFn;
436  AvgFromBucket avgFn;
437  return buckets_.getPercentileEstimate(pct, countFn, avgFn);
438  }
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::merge ( const Histogram< T > &  hist)
inline

Definition at line 330 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::Histogram< T >::buckets_, folly::detail::HistogramBuckets< T, BucketT >::getBucketSize(), folly::Histogram< T >::getBucketSize(), folly::detail::HistogramBuckets< T, BucketT >::getMax(), folly::Histogram< T >::getMax(), folly::detail::HistogramBuckets< T, BucketT >::getMin(), folly::Histogram< T >::getMin(), folly::detail::HistogramBuckets< T, BucketT >::getNumBuckets(), folly::Histogram< T >::getNumBuckets(), and i.

330  {
331  // the two histogram bucket definitions must match to support
332  // a merge.
333  if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
334  getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
335  throw std::invalid_argument("Cannot merge from input histogram.");
336  }
337 
338  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
339  buckets_.getByIndex(i) += hist.buckets_.getByIndex(i);
340  }
341  }
ValueType getBucketSize() const
Definition: Histogram.h:357
ValueType getMin() const
Definition: Histogram.h:361
size_t getNumBuckets() const
Definition: Histogram.h:369
ValueType getMax() const
Definition: Histogram.h:365
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::removeRepeatedValue ( ValueType  value,
uint64_t  nSamples 
)
inline

Definition at line 296 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::detail::Bucket< T >::count, and folly::detail::Bucket< T >::sum.

296  {
297  Bucket& bucket = buckets_.getByValue(value);
298  // TODO: It would be nice to handle overflow here.
299  if (bucket.count >= nSamples) {
300  bucket.sum -= value * nSamples;
301  bucket.count -= nSamples;
302  } else {
303  bucket.sum = ValueType();
304  bucket.count = 0;
305  }
306  }
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::removeValue ( ValueType  value)
inline

Definition at line 280 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::detail::Bucket< T >::count, folly::detail::Bucket< T >::sum, and folly::to_unsigned().

280  {
281  Bucket& bucket = buckets_.getByValue(value);
282  // NOTE: Overflow is handled elsewhere and tests check this
283  // behavior (see HistogramTest.cpp TestOverflow* tests).
284  // TODO: It would be nice to handle overflow here and redesign this class.
285  if (bucket.count > 0) {
286  auto const subtrahend = to_unsigned(value);
287  bucket.sum = static_cast<ValueType>(to_unsigned(bucket.sum) - subtrahend);
288  bucket.count -= 1;
289  } else {
290  bucket.sum = ValueType();
291  bucket.count = 0;
292  }
293  }
static constexpr _t< std::make_unsigned< S > > to_unsigned(S s)
Definition: Histogram.h:478
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
void folly::Histogram< T >::subtract ( const Histogram< T > &  hist)
inline

Definition at line 316 of file Histogram.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::Histogram< T >::buckets_, folly::detail::HistogramBuckets< T, BucketT >::getBucketSize(), folly::Histogram< T >::getBucketSize(), folly::detail::HistogramBuckets< T, BucketT >::getMax(), folly::Histogram< T >::getMax(), folly::detail::HistogramBuckets< T, BucketT >::getMin(), folly::Histogram< T >::getMin(), folly::detail::HistogramBuckets< T, BucketT >::getNumBuckets(), folly::Histogram< T >::getNumBuckets(), and i.

316  {
317  // the two histogram bucket definitions must match to support
318  // subtract.
319  if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
320  getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
321  throw std::invalid_argument("Cannot subtract input histogram.");
322  }
323 
324  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
325  buckets_.getByIndex(i) -= hist.buckets_.getByIndex(i);
326  }
327  }
ValueType getBucketSize() const
Definition: Histogram.h:357
ValueType getMin() const
Definition: Histogram.h:361
size_t getNumBuckets() const
Definition: Histogram.h:369
ValueType getMax() const
Definition: Histogram.h:365
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488
template<typename T>
template<typename S , typename = _t<std::enable_if<std::is_integral<S>::value>>>
static constexpr _t<std::make_unsigned<S> > folly::Histogram< T >::to_unsigned ( S  s)
inlinestaticprivate

Definition at line 478 of file Histogram.h.

References s.

478  {
479  return static_cast<_t<std::make_unsigned<S>>>(s);
480  }
static set< string > s
template<typename T>
template<typename S , typename = _t<std::enable_if<!std::is_integral<S>::value>>>
static constexpr S folly::Histogram< T >::to_unsigned ( S  s)
inlinestaticprivate

Definition at line 484 of file Histogram.h.

References s.

484  {
485  return s;
486  }
static set< string > s
template<typename T >
void folly::Histogram< T >::toTSV ( std::ostream &  out,
bool  skipEmptyBuckets = true 
) const

Definition at line 275 of file Histogram-defs.h.

References folly::detail::HistogramBuckets< T, BucketT >::buckets_, folly::detail::HistogramBuckets< T, BucketT >::getBucketMax(), folly::detail::HistogramBuckets< T, BucketT >::getBucketMin(), and i.

275  {
276  for (size_t i = 0; i < buckets_.getNumBuckets(); ++i) {
277  // Do not output empty buckets in order to reduce data file size.
278  if (skipEmptyBuckets && getBucketByIndex(i).count == 0) {
279  continue;
280  }
281  const auto& bucket = getBucketByIndex(i);
282  out << getBucketMin(i) << '\t' << getBucketMax(i) << '\t' << bucket.count
283  << '\t' << bucket.sum << '\n';
284  }
285 }
ValueType getBucketMax(size_t idx) const
Definition: Histogram.h:396
const Bucket & getBucketByIndex(size_t idx) const
Definition: Histogram.h:374
ValueType getBucketMin(size_t idx) const
Definition: Histogram.h:385
detail::HistogramBuckets< ValueType, Bucket > buckets_
Definition: Histogram.h:488

Member Data Documentation


The documentation for this class was generated from the following files: