2 * Copyright 2013-present 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>
20 #include <folly/stats/BucketedTimeSeries-defs.h>
21 #include <folly/stats/Histogram-defs.h>
22 #include <folly/stats/MultiLevelTimeSeries-defs.h>
23 #include <folly/stats/TimeseriesHistogram.h>
27 template <typename T, typename CT, typename C>
28 TimeseriesHistogram<T, CT, C>::TimeseriesHistogram(
32 const ContainerType& copyMe)
33 : buckets_(bucketSize, min, max, copyMe),
34 haveNotSeenValue_(true),
35 singleUniqueValue_(false) {}
37 template <typename T, typename CT, typename C>
38 void TimeseriesHistogram<T, CT, C>::addValue(
40 const ValueType& value) {
41 buckets_.getByValue(value).addValue(now, value);
42 maybeHandleSingleUniqueValue(value);
45 template <typename T, typename CT, typename C>
46 void TimeseriesHistogram<T, CT, C>::addValue(
48 const ValueType& value,
50 buckets_.getByValue(value).addValue(now, value, times);
51 maybeHandleSingleUniqueValue(value);
54 template <typename T, typename CT, typename C>
55 void TimeseriesHistogram<T, CT, C>::addValues(
57 const folly::Histogram<ValueType>& hist) {
58 CHECK_EQ(hist.getMin(), getMin());
59 CHECK_EQ(hist.getMax(), getMax());
60 CHECK_EQ(hist.getBucketSize(), getBucketSize());
61 CHECK_EQ(hist.getNumBuckets(), getNumBuckets());
63 for (size_t n = 0; n < hist.getNumBuckets(); ++n) {
64 const typename folly::Histogram<ValueType>::Bucket& histBucket =
65 hist.getBucketByIndex(n);
66 Bucket& myBucket = buckets_.getByIndex(n);
67 myBucket.addValueAggregated(now, histBucket.sum, histBucket.count);
70 // We don't bother with the singleUniqueValue_ tracking.
71 haveNotSeenValue_ = false;
72 singleUniqueValue_ = false;
75 template <typename T, typename CT, typename C>
76 void TimeseriesHistogram<T, CT, C>::maybeHandleSingleUniqueValue(
77 const ValueType& value) {
78 if (haveNotSeenValue_) {
80 singleUniqueValue_ = true;
81 haveNotSeenValue_ = false;
82 } else if (singleUniqueValue_) {
83 if (value != firstValue_) {
84 singleUniqueValue_ = false;
89 template <typename T, typename CT, typename C>
90 T TimeseriesHistogram<T, CT, C>::getPercentileEstimate(double pct, size_t level)
92 if (singleUniqueValue_) {
96 return buckets_.getPercentileEstimate(
97 pct / 100.0, CountFromLevel(level), AvgFromLevel(level));
100 template <typename T, typename CT, typename C>
101 T TimeseriesHistogram<T, CT, C>::getPercentileEstimate(
104 TimePoint end) const {
105 if (singleUniqueValue_) {
109 return buckets_.getPercentileEstimate(
111 CountFromInterval(start, end),
112 AvgFromInterval<T>(start, end));
115 template <typename T, typename CT, typename C>
116 size_t TimeseriesHistogram<T, CT, C>::getPercentileBucketIdx(
118 size_t level) const {
119 return buckets_.getPercentileBucketIdx(pct / 100.0, CountFromLevel(level));
122 template <typename T, typename CT, typename C>
123 size_t TimeseriesHistogram<T, CT, C>::getPercentileBucketIdx(
126 TimePoint end) const {
127 return buckets_.getPercentileBucketIdx(
128 pct / 100.0, CountFromInterval(start, end));
131 template <typename T, typename CT, typename C>
132 void TimeseriesHistogram<T, CT, C>::clear() {
133 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
134 buckets_.getByIndex(i).clear();
138 template <typename T, typename CT, typename C>
139 void TimeseriesHistogram<T, CT, C>::update(TimePoint now) {
140 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
141 buckets_.getByIndex(i).update(now);
145 template <typename T, typename CT, typename C>
146 std::string TimeseriesHistogram<T, CT, C>::getString(size_t level) const {
149 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
151 toAppend(",", &result);
153 const ContainerType& cont = buckets_.getByIndex(i);
155 buckets_.getBucketMin(i),
159 cont.template avg<ValueType>(level),
166 template <typename T, typename CT, typename C>
167 std::string TimeseriesHistogram<T, CT, C>::getString(
169 TimePoint end) const {
172 for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
174 toAppend(",", &result);
176 const ContainerType& cont = buckets_.getByIndex(i);
178 buckets_.getBucketMin(i),
180 cont.count(start, end),
182 cont.avg(start, end),
189 template <class T, class CT, class C>
190 void TimeseriesHistogram<T, CT, C>::computeAvgData(
193 size_t level) const {
194 for (size_t b = 0; b < buckets_.getNumBuckets(); ++b) {
195 const auto& levelObj = buckets_.getByIndex(b).getLevel(level);
196 *total += levelObj.sum();
197 *nsamples += levelObj.count();
201 template <class T, class CT, class C>
202 void TimeseriesHistogram<T, CT, C>::computeAvgData(
206 TimePoint end) const {
207 for (size_t b = 0; b < buckets_.getNumBuckets(); ++b) {
208 const auto& levelObj = buckets_.getByIndex(b).getLevel(start);
209 *total += levelObj.sum(start, end);
210 *nsamples += levelObj.count(start, end);
214 template <typename T, typename CT, typename C>
215 void TimeseriesHistogram<T, CT, C>::computeRateData(
218 size_t level) const {
219 for (size_t b = 0; b < buckets_.getNumBuckets(); ++b) {
220 const auto& levelObj = buckets_.getByIndex(b).getLevel(level);
221 *total += levelObj.sum();
222 *elapsed = std::max(*elapsed, levelObj.elapsed());
226 template <class T, class CT, class C>
227 void TimeseriesHistogram<T, CT, C>::computeRateData(
231 TimePoint end) const {
232 for (size_t b = 0; b < buckets_.getNumBuckets(); ++b) {
233 const auto& level = buckets_.getByIndex(b).getLevel(start);
234 *total += level.sum(start, end);
235 *elapsed = std::max(*elapsed, level.elapsed(start, end));