2 * Copyright 2016 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.
19 #include <folly/Conv.h>
21 #include <glog/logging.h>
27 template <typename T, typename BucketT>
28 HistogramBuckets<T, BucketT>::HistogramBuckets(ValueType bucketSize,
31 const BucketType& defaultBucket)
32 : bucketSize_(bucketSize),
35 CHECK_GT(bucketSize_, ValueType(0));
38 // Deliberately make this a signed type, because we're about
39 // to compare it against max-min, which is nominally signed, too.
40 int numBuckets = (max - min) / bucketSize;
41 // Round up if the bucket size does not fit evenly
42 if (numBuckets * bucketSize < max - min) {
45 // Add 2 for the extra 'below min' and 'above max' buckets
47 buckets_.assign(numBuckets, defaultBucket);
50 template <typename T, typename BucketType>
51 unsigned int HistogramBuckets<T, BucketType>::getBucketIdx(
52 ValueType value) const {
55 } else if (value >= max_) {
56 return buckets_.size() - 1;
58 // the 1 is the below_min bucket
59 return ((value - min_) / bucketSize_) + 1;
63 template <typename T, typename BucketType>
64 template <typename CountFn>
65 uint64_t HistogramBuckets<T, BucketType>::computeTotalCount(
66 CountFn countFromBucket) const {
68 for (unsigned int n = 0; n < buckets_.size(); ++n) {
69 count += countFromBucket(const_cast<const BucketType&>(buckets_[n]));
74 template <typename T, typename BucketType>
75 template <typename CountFn>
76 unsigned int HistogramBuckets<T, BucketType>::getPercentileBucketIdx(
78 CountFn countFromBucket,
79 double* lowPct, double* highPct) const {
83 unsigned int numBuckets = buckets_.size();
85 // Compute the counts in each bucket
86 std::vector<uint64_t> counts(numBuckets);
87 uint64_t totalCount = 0;
88 for (unsigned int n = 0; n < numBuckets; ++n) {
89 uint64_t bucketCount =
90 countFromBucket(const_cast<const BucketType&>(buckets_[n]));
91 counts[n] = bucketCount;
92 totalCount += bucketCount;
95 // If there are no elements, just return the lowest bucket.
96 // Note that we return bucket 1, which is the first bucket in the
97 // histogram range; bucket 0 is for all values below min_.
98 if (totalCount == 0) {
99 // Set lowPct and highPct both to 0.
100 // getPercentileEstimate() will recognize this to mean that the histogram
111 // Loop through all the buckets, keeping track of each bucket's
112 // percentile range: [0,10], [10,17], [17,45], etc. When we find a range
113 // that includes our desired percentile, we return that bucket index.
114 double prevPct = 0.0;
116 uint64_t curCount = 0;
118 for (idx = 0; idx < numBuckets; ++idx) {
119 if (counts[idx] == 0) {
120 // skip empty buckets
125 curCount += counts[idx];
126 curPct = static_cast<double>(curCount) / totalCount;
128 // This is the desired bucket
142 template <typename T, typename BucketType>
143 template <typename CountFn, typename AvgFn>
144 T HistogramBuckets<T, BucketType>::getPercentileEstimate(
146 CountFn countFromBucket,
147 AvgFn avgFromBucket) const {
149 // Find the bucket where this percentile falls
152 unsigned int bucketIdx = getPercentileBucketIdx(pct, countFromBucket,
154 if (lowPct == 0.0 && highPct == 0.0) {
155 // Invalid range -- the buckets must all be empty
156 // Return the default value for ValueType.
159 if (lowPct == highPct) {
160 // Unlikely to have exact equality,
161 // but just return the bucket average in this case.
162 // We handle this here to avoid division by 0 below.
163 return avgFromBucket(buckets_[bucketIdx]);
166 CHECK_GE(pct, lowPct);
167 CHECK_LE(pct, highPct);
168 CHECK_LT(lowPct, highPct);
170 // Compute information about this bucket
171 ValueType avg = avgFromBucket(buckets_[bucketIdx]);
174 if (bucketIdx == 0) {
176 // This normally shouldn't happen. This bucket is only supposed to track
177 // values less than min_. Most likely this means that integer overflow
178 // occurred, and the code in avgFromBucket() returned a huge value
179 // instead of a small one. Just return the minimum possible value for
182 // (Note that if the counter keeps being decremented, eventually it will
183 // wrap and become small enough that we won't detect this any more, and
184 // we will return bogus information.)
185 LOG(ERROR) << "invalid average value in histogram minimum bucket: " <<
186 avg << " > " << min_ << ": possible integer overflow?";
187 return getBucketMin(bucketIdx);
189 // For the below-min bucket, just assume the lowest value ever seen is
190 // twice as far away from min_ as avg.
192 low = high - (2 * (high - avg));
193 // Adjust low in case it wrapped
195 low = std::numeric_limits<ValueType>::min();
197 } else if (bucketIdx == buckets_.size() - 1) {
199 // Most likely this means integer overflow occurred. See the comments
200 // above in the minimum case.
201 LOG(ERROR) << "invalid average value in histogram maximum bucket: " <<
202 avg << " < " << max_ << ": possible integer overflow?";
203 return getBucketMax(bucketIdx);
205 // Similarly for the above-max bucket, assume the highest value ever seen
206 // is twice as far away from max_ as avg.
208 high = low + (2 * (avg - low));
209 // Adjust high in case it wrapped
211 high = std::numeric_limits<ValueType>::max();
214 low = getBucketMin(bucketIdx);
215 high = getBucketMax(bucketIdx);
216 if (avg < low || avg > high) {
217 // Most likely this means an integer overflow occurred.
218 // See the comments above. Return the midpoint between low and high
219 // as a best guess, since avg is meaningless.
220 LOG(ERROR) << "invalid average value in histogram bucket: " <<
221 avg << " not in range [" << low << ", " << high <<
222 "]: possible integer overflow?";
223 return (low + high) / 2;
227 // Since we know the average value in this bucket, we can do slightly better
228 // than just assuming the data points in this bucket are uniformly
229 // distributed between low and high.
231 // Assume that the median value in this bucket is the same as the average
233 double medianPct = (lowPct + highPct) / 2.0;
234 if (pct < medianPct) {
235 // Assume that the data points lower than the median of this bucket
236 // are uniformly distributed between low and avg
237 double pctThroughSection = (pct - lowPct) / (medianPct - lowPct);
238 return low + ((avg - low) * pctThroughSection);
240 // Assume that the data points greater than the median of this bucket
241 // are uniformly distributed between avg and high
242 double pctThroughSection = (pct - medianPct) / (highPct - medianPct);
243 return avg + ((high - avg) * pctThroughSection);
250 template <typename T>
251 std::string Histogram<T>::debugString() const {
252 std::string ret = folly::to<std::string>(
253 "num buckets: ", buckets_.getNumBuckets(),
254 ", bucketSize: ", buckets_.getBucketSize(),
255 ", min: ", buckets_.getMin(), ", max: ", buckets_.getMax(), "\n");
257 for (unsigned int i = 0; i < buckets_.getNumBuckets(); ++i) {
258 folly::toAppend(" ", buckets_.getBucketMin(i), ": ",
259 buckets_.getByIndex(i).count, "\n",
266 template <typename T>
267 void Histogram<T>::toTSV(std::ostream& out, bool skipEmptyBuckets) const {
268 for (unsigned int i = 0; i < buckets_.getNumBuckets(); ++i) {
269 // Do not output empty buckets in order to reduce data file size.
270 if (skipEmptyBuckets && getBucketByIndex(i).count == 0) {
273 const auto& bucket = getBucketByIndex(i);
274 out << getBucketMin(i) << '\t' << getBucketMax(i) << '\t'
275 << bucket.count << '\t' << bucket.sum << '\n';