proxygen
TimeseriesHistogram-defs.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #pragma once
18 
19 #include <folly/Conv.h>
24 
25 namespace folly {
26 
27 template <typename T, typename CT, typename C>
29  ValueType bucketSize,
30  ValueType min,
31  ValueType max,
32  const ContainerType& copyMe)
33  : buckets_(bucketSize, min, max, copyMe),
34  haveNotSeenValue_(true),
35  singleUniqueValue_(false) {}
36 
37 template <typename T, typename CT, typename C>
39  TimePoint now,
40  const ValueType& value) {
41  buckets_.getByValue(value).addValue(now, value);
43 }
44 
45 template <typename T, typename CT, typename C>
47  TimePoint now,
48  const ValueType& value,
49  uint64_t times) {
50  buckets_.getByValue(value).addValue(now, value, times);
52 }
53 
54 template <typename T, typename CT, typename C>
56  TimePoint now,
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());
62 
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);
68  }
69 
70  // We don't bother with the singleUniqueValue_ tracking.
71  haveNotSeenValue_ = false;
72  singleUniqueValue_ = false;
73 }
74 
75 template <typename T, typename CT, typename C>
77  const ValueType& value) {
78  if (haveNotSeenValue_) {
80  singleUniqueValue_ = true;
81  haveNotSeenValue_ = false;
82  } else if (singleUniqueValue_) {
83  if (value != firstValue_) {
84  singleUniqueValue_ = false;
85  }
86  }
87 }
88 
89 template <typename T, typename CT, typename C>
91  const {
92  if (singleUniqueValue_) {
93  return firstValue_;
94  }
95 
97  pct / 100.0, CountFromLevel(level), AvgFromLevel(level));
98 }
99 
100 template <typename T, typename CT, typename C>
102  double pct,
104  TimePoint end) const {
105  if (singleUniqueValue_) {
106  return firstValue_;
107  }
108 
110  pct / 100.0,
111  CountFromInterval(start, end),
112  AvgFromInterval<T>(start, end));
113 }
114 
115 template <typename T, typename CT, typename C>
117  double pct,
118  size_t level) const {
119  return buckets_.getPercentileBucketIdx(pct / 100.0, CountFromLevel(level));
120 }
121 
122 template <typename T, typename CT, typename C>
124  double pct,
126  TimePoint end) const {
128  pct / 100.0, CountFromInterval(start, end));
129 }
130 
131 template <typename T, typename CT, typename C>
133  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
134  buckets_.getByIndex(i).clear();
135  }
136 }
137 
138 template <typename T, typename CT, typename C>
140  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
141  buckets_.getByIndex(i).update(now);
142  }
143 }
144 
145 template <typename T, typename CT, typename C>
147  std::string result;
148 
149  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
150  if (i > 0) {
151  toAppend(",", &result);
152  }
153  const ContainerType& cont = buckets_.getByIndex(i);
154  toAppend(
156  ":",
157  cont.count(level),
158  ":",
159  cont.template avg<ValueType>(level),
160  &result);
161  }
162 
163  return result;
164 }
165 
166 template <typename T, typename CT, typename C>
169  TimePoint end) const {
170  std::string result;
171 
172  for (size_t i = 0; i < buckets_.getNumBuckets(); i++) {
173  if (i > 0) {
174  toAppend(",", &result);
175  }
176  const ContainerType& cont = buckets_.getByIndex(i);
177  toAppend(
179  ":",
180  cont.count(start, end),
181  ":",
182  cont.avg(start, end),
183  &result);
184  }
185 
186  return result;
187 }
188 
189 template <class T, class CT, class C>
191  ValueType* total,
192  uint64_t* nsamples,
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();
198  }
199 }
200 
201 template <class T, class CT, class C>
203  ValueType* total,
204  uint64_t* nsamples,
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);
211  }
212 }
213 
214 template <typename T, typename CT, typename C>
216  ValueType* total,
217  Duration* elapsed,
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());
223  }
224 }
225 
226 template <class T, class CT, class C>
228  ValueType* total,
229  Duration* elapsed,
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));
236  }
237 }
238 
239 } // namespace folly
ValueType getBucketSize() const
Definition: Histogram.h:357
folly::detail::HistogramBuckets< ValueType, ContainerType > buckets_
void computeAvgData(ValueType *total, uint64_t *nsamples, size_t level) const
ValueType getPercentileEstimate(double pct, CountFn countFromBucket, AvgFn avgFromBucket) const
char b
LogLevel max
Definition: LogLevel.cpp:31
const Bucket & getBucketByIndex(size_t idx) const
Definition: Histogram.h:374
std::string getString(size_t level) const
std::chrono::steady_clock::time_point now()
ValueType getMin() const
Definition: Histogram.h:361
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void addValues(TimePoint now, const folly::Histogram< ValueType > &values)
void maybeHandleSingleUniqueValue(const ValueType &value)
ValueType getBucketSize() const
BucketType & getByValue(ValueType value)
Definition: Histogram.h:93
LogLevel min
Definition: LogLevel.cpp:30
size_t getNumBuckets() const
Definition: Histogram.h:85
ValueType getBucketMin(size_t idx) const
Definition: Histogram.h:124
typename Clock::duration Duration
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
ValueType getPercentileEstimate(double pct, size_t level) const
size_t getNumBuckets() const
Definition: Histogram.h:369
BucketType & getByIndex(size_t idx)
Definition: Histogram.h:108
void toAppend(char value, Tgt *result)
Definition: Conv.h:406
auto start
size_t getPercentileBucketIdx(double pct, CountFn countFromBucket, double *lowPct=nullptr, double *highPct=nullptr) const
void computeRateData(ValueType *total, Duration *elapsed, size_t level) const
ValueType getMax() const
Definition: Histogram.h:365
const char * string
Definition: Conv.cpp:212
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
size_t getPercentileBucketIdx(double pct, size_t level) const
void addValue(TimePoint now, const ValueType &value)
Future< Unit > times(const int n, F &&thunk)
Definition: Future-inl.h:2348
typename Clock::time_point TimePoint
TimeseriesHistogram(ValueType bucketSize, ValueType min, ValueType max, const ContainerType &defaultContainer)