2 * Copyright 2017 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.
27 #include <folly/CPortability.h>
28 #include <folly/stats/detail/Bucket.h>
35 * A helper class to manage a set of histogram buckets.
37 template <typename T, typename BucketT>
38 class HistogramBuckets {
41 typedef BucketT BucketType;
44 * Create a set of histogram buckets.
46 * One bucket will be created for each bucketSize interval of values within
47 * the specified range. Additionally, one bucket will be created to track
48 * all values that fall below the specified minimum, and one bucket will be
49 * created for all values above the specified maximum.
51 * If (max - min) is not a multiple of bucketSize, the last bucket will cover
52 * a smaller range of values than the other buckets.
54 * (max - min) must be larger than or equal to bucketSize.
60 const BucketType& defaultBucket);
62 /* Returns the bucket size of each bucket in the histogram. */
63 ValueType getBucketSize() const {
67 /* Returns the min value at which bucketing begins. */
68 ValueType getMin() const {
72 /* Returns the max value at which bucketing ends. */
73 ValueType getMax() const {
78 * Returns the number of buckets.
80 * This includes the total number of buckets for the [min, max) range,
81 * plus 2 extra buckets, one for handling values less than min, and one for
82 * values greater than max.
84 size_t getNumBuckets() const {
85 return buckets_.size();
88 /* Returns the bucket index into which the given value would fall. */
89 size_t getBucketIdx(ValueType value) const;
91 /* Returns the bucket for the specified value */
92 BucketType& getByValue(ValueType value) {
93 return buckets_[getBucketIdx(value)];
96 /* Returns the bucket for the specified value */
97 const BucketType& getByValue(ValueType value) const {
98 return buckets_[getBucketIdx(value)];
102 * Returns the bucket at the specified index.
104 * Note that index 0 is the bucket for all values less than the specified
105 * minimum. Index 1 is the first bucket in the specified bucket range.
107 BucketType& getByIndex(size_t idx) {
108 return buckets_[idx];
111 /* Returns the bucket at the specified index. */
112 const BucketType& getByIndex(size_t idx) const {
113 return buckets_[idx];
117 * Returns the minimum threshold for the bucket at the given index.
119 * The bucket at the specified index will store values in the range
120 * [bucketMin, bucketMin + bucketSize), or [bucketMin, max), if the overall
121 * max is smaller than bucketMin + bucketSize.
123 ValueType getBucketMin(size_t idx) const {
125 return std::numeric_limits<ValueType>::min();
127 if (idx == buckets_.size() - 1) {
131 return ValueType(min_ + ((idx - 1) * bucketSize_));
135 * Returns the maximum threshold for the bucket at the given index.
137 * The bucket at the specified index will store values in the range
138 * [bucketMin, bucketMin + bucketSize), or [bucketMin, max), if the overall
139 * max is smaller than bucketMin + bucketSize.
141 ValueType getBucketMax(size_t idx) const {
142 if (idx == buckets_.size() - 1) {
143 return std::numeric_limits<ValueType>::max();
146 return ValueType(min_ + (idx * bucketSize_));
150 * Computes the total number of values stored across all buckets.
152 * Runs in O(numBuckets)
154 * @param countFn A function that takes a const BucketType&, and returns the
155 * number of values in that bucket
156 * @return Returns the total number of values stored across all buckets
158 template <typename CountFn>
159 uint64_t computeTotalCount(CountFn countFromBucket) const;
162 * Determine which bucket the specified percentile falls into.
164 * Looks for the bucket that contains the Nth percentile data point.
166 * @param pct The desired percentile to find, as a value from 0.0 to 1.0.
167 * @param countFn A function that takes a const BucketType&, and returns the
168 * number of values in that bucket.
169 * @param lowPct The lowest percentile stored in the selected bucket will be
170 * returned via this parameter.
171 * @param highPct The highest percentile stored in the selected bucket will
172 * be returned via this parameter.
174 * @return Returns the index of the bucket that contains the Nth percentile
177 template <typename CountFn>
178 size_t getPercentileBucketIdx(
180 CountFn countFromBucket,
181 double* lowPct = nullptr,
182 double* highPct = nullptr) const;
185 * Estimate the value at the specified percentile.
187 * @param pct The desired percentile to find, as a value from 0.0 to 1.0.
188 * @param countFn A function that takes a const BucketType&, and returns the
189 * number of values in that bucket.
190 * @param avgFn A function that takes a const BucketType&, and returns the
191 * average of all the values in that bucket.
193 * @return Returns an estimate for N, where N is the number where exactly pct
194 * percentage of the data points in the histogram are less than N.
196 template <typename CountFn, typename AvgFn>
197 ValueType getPercentileEstimate(
199 CountFn countFromBucket,
200 AvgFn avgFromBucket) const;
203 * Iterator access to the buckets.
205 * Note that the first bucket is for all values less than min, and the last
206 * bucket is for all values greater than max. The buckets tracking values in
207 * the [min, max) actually start at the second bucket.
209 typename std::vector<BucketType>::const_iterator begin() const {
210 return buckets_.begin();
212 typename std::vector<BucketType>::iterator begin() {
213 return buckets_.begin();
215 typename std::vector<BucketType>::const_iterator end() const {
216 return buckets_.end();
218 typename std::vector<BucketType>::iterator end() {
219 return buckets_.end();
223 ValueType bucketSize_;
226 std::vector<BucketType> buckets_;
229 } // namespace detail
232 * A basic histogram class.
234 * Groups data points into equally-sized buckets, and stores the overall sum of
235 * the data points in each bucket, as well as the number of data points in the
238 * The caller must specify the minimum and maximum data points to expect ahead
239 * of time, as well as the bucket width.
241 template <typename T>
245 typedef detail::Bucket<T> Bucket;
247 Histogram(ValueType bucketSize, ValueType min, ValueType max)
248 : buckets_(bucketSize, min, max, Bucket()) {}
250 /* Add a data point to the histogram */
251 void addValue(ValueType value) FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(
252 "signed-integer-overflow",
253 "unsigned-integer-overflow") {
254 Bucket& bucket = buckets_.getByValue(value);
255 // NOTE: Overflow is handled elsewhere and tests check this
256 // behavior (see HistogramTest.cpp TestOverflow* tests).
257 // TODO: It would be nice to handle overflow here and redesign this class.
262 /* Add multiple same data points to the histogram */
263 void addRepeatedValue(ValueType value, uint64_t nSamples)
264 FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(
265 "signed-integer-overflow",
266 "unsigned-integer-overflow") {
267 Bucket& bucket = buckets_.getByValue(value);
268 // NOTE: Overflow is handled elsewhere and tests check this
269 // behavior (see HistogramTest.cpp TestOverflow* tests).
270 // TODO: It would be nice to handle overflow here and redesign this class.
271 bucket.sum += value * nSamples;
272 bucket.count += nSamples;
276 * Remove a data point to the histogram
278 * Note that this method does not actually verify that this exact data point
279 * had previously been added to the histogram; it merely subtracts the
280 * requested value from the appropriate bucket's sum.
282 void removeValue(ValueType value) FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER(
283 "signed-integer-overflow",
284 "unsigned-integer-overflow") {
285 Bucket& bucket = buckets_.getByValue(value);
286 // NOTE: Overflow is handled elsewhere and tests check this
287 // behavior (see HistogramTest.cpp TestOverflow* tests).
288 // TODO: It would be nice to handle overflow here and redesign this class.
289 if (bucket.count > 0) {
293 bucket.sum = ValueType();
298 /* Remove multiple same data points from the histogram */
299 void removeRepeatedValue(ValueType value, uint64_t nSamples) {
300 Bucket& bucket = buckets_.getByValue(value);
301 // TODO: It would be nice to handle overflow here.
302 if (bucket.count >= nSamples) {
303 bucket.sum -= value * nSamples;
304 bucket.count -= nSamples;
306 bucket.sum = ValueType();
311 /* Remove all data points from the histogram */
313 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
314 buckets_.getByIndex(i).clear();
318 /* Subtract another histogram data from the histogram */
319 void subtract(const Histogram& hist) {
320 // the two histogram bucket definitions must match to support
322 if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
323 getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
324 throw std::invalid_argument("Cannot subtract input histogram.");
327 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
328 buckets_.getByIndex(i) -= hist.buckets_.getByIndex(i);
332 /* Merge two histogram data together */
333 void merge(const Histogram& hist) {
334 // the two histogram bucket definitions must match to support
336 if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
337 getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
338 throw std::invalid_argument("Cannot merge from input histogram.");
341 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
342 buckets_.getByIndex(i) += hist.buckets_.getByIndex(i);
346 /* Copy bucket values from another histogram */
347 void copy(const Histogram& hist) {
348 // the two histogram bucket definition must match
349 if (getBucketSize() != hist.getBucketSize() || getMin() != hist.getMin() ||
350 getMax() != hist.getMax() || getNumBuckets() != hist.getNumBuckets()) {
351 throw std::invalid_argument("Cannot copy from input histogram.");
354 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
355 buckets_.getByIndex(i) = hist.buckets_.getByIndex(i);
359 /* Returns the bucket size of each bucket in the histogram. */
360 ValueType getBucketSize() const {
361 return buckets_.getBucketSize();
363 /* Returns the min value at which bucketing begins. */
364 ValueType getMin() const {
365 return buckets_.getMin();
367 /* Returns the max value at which bucketing ends. */
368 ValueType getMax() const {
369 return buckets_.getMax();
371 /* Returns the number of buckets */
372 size_t getNumBuckets() const {
373 return buckets_.getNumBuckets();
376 /* Returns the specified bucket (for reading only!) */
377 const Bucket& getBucketByIndex(size_t idx) const {
378 return buckets_.getByIndex(idx);
382 * Returns the minimum threshold for the bucket at the given index.
384 * The bucket at the specified index will store values in the range
385 * [bucketMin, bucketMin + bucketSize), or [bucketMin, max), if the overall
386 * max is smaller than bucketMin + bucketSize.
388 ValueType getBucketMin(size_t idx) const {
389 return buckets_.getBucketMin(idx);
393 * Returns the maximum threshold for the bucket at the given index.
395 * The bucket at the specified index will store values in the range
396 * [bucketMin, bucketMin + bucketSize), or [bucketMin, max), if the overall
397 * max is smaller than bucketMin + bucketSize.
399 ValueType getBucketMax(size_t idx) const {
400 return buckets_.getBucketMax(idx);
404 * Computes the total number of values stored across all buckets.
406 * Runs in O(numBuckets)
408 uint64_t computeTotalCount() const {
409 CountFromBucket countFn;
410 return buckets_.computeTotalCount(countFn);
414 * Get the bucket that the specified percentile falls into
416 * The lowest and highest percentile data points in returned bucket will be
417 * returned in the lowPct and highPct arguments, if they are not nullptr.
419 size_t getPercentileBucketIdx(
421 double* lowPct = nullptr,
422 double* highPct = nullptr) const {
423 // We unfortunately can't use lambdas here yet;
424 // Some users of this code are still built with gcc-4.4.
425 CountFromBucket countFn;
426 return buckets_.getPercentileBucketIdx(pct, countFn, lowPct, highPct);
430 * Estimate the value at the specified percentile.
432 * @param pct The desired percentile to find, as a value from 0.0 to 1.0.
434 * @return Returns an estimate for N, where N is the number where exactly pct
435 * percentage of the data points in the histogram are less than N.
437 ValueType getPercentileEstimate(double pct) const {
438 CountFromBucket countFn;
440 return buckets_.getPercentileEstimate(pct, countFn, avgFn);
444 * Get a human-readable string describing the histogram contents
446 std::string debugString() const;
449 * Write the histogram contents in tab-separated values (TSV) format.
450 * Format is "min max count sum".
452 void toTSV(std::ostream& out, bool skipEmptyBuckets = true) const;
454 struct CountFromBucket {
455 uint64_t operator()(const Bucket& bucket) const {
459 struct AvgFromBucket {
460 ValueType operator()(const Bucket& bucket) const {
461 if (bucket.count == 0) {
464 // Cast bucket.count to a signed integer type. This ensures that we
465 // perform division properly here: If bucket.sum is a signed integer
466 // type but we divide by an unsigned number, unsigned division will be
467 // performed and bucket.sum will be converted to unsigned first.
468 // If bucket.sum is unsigned, the code will still do unsigned division
471 // The only downside is if bucket.count is large enough to be negative
472 // when treated as signed. That should be extremely unlikely, though.
473 return bucket.sum / static_cast<int64_t>(bucket.count);
478 detail::HistogramBuckets<ValueType, Bucket> buckets_;
483 // MSVC 2017 Update 3 has an issue with explicitly instantiating templated
484 // functions with default arguments inside templated classes when compiled
485 // with /permissive- (the default for the CMake build), so we directly include
486 // the -defs as if it were -inl, and don't provide the explicit instantiations.
487 // https://developercommunity.visualstudio.com/content/problem/81223/incorrect-error-c5037-with-permissive.html
488 #if defined(_MSC_VER) && _MSC_FULL_VER >= 191125506 && _MSC_FULL_VER < 191125542
489 #define FOLLY_MSVC_USE_WORKAROUND_FOR_C5037 1
491 #define FOLLY_MSVC_USE_WORKAROUND_FOR_C5037 0
494 #if FOLLY_MSVC_USE_WORKAROUND_FOR_C5037
495 #include <folly/stats/Histogram-defs.h>