blob: aba376287ee797f38ee082bff055f18b1592bc0a [file] [log] [blame]
Sameer Abu Asala8439542013-02-14 16:06:42 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_HISTOGRAM_INL_H_
18#define ART_RUNTIME_BASE_HISTOGRAM_INL_H_
Sameer Abu Asala8439542013-02-14 16:06:42 -080019
Sameer Abu Asala8439542013-02-14 16:06:42 -080020#include <algorithm>
21#include <cmath>
22#include <limits>
23#include <ostream>
24
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "histogram.h"
26
27#include "base/bit_utils.h"
28#include "base/time_utils.h"
29
Sameer Abu Asala8439542013-02-14 16:06:42 -080030namespace art {
31
32template <class Value> inline void Histogram<Value>::AddValue(Value value) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -070033 CHECK_GE(value, static_cast<Value>(0));
Sameer Abu Asala8439542013-02-14 16:06:42 -080034 if (value >= max_) {
35 Value new_max = ((value + 1) / bucket_width_ + 1) * bucket_width_;
36 DCHECK_GT(new_max, max_);
37 GrowBuckets(new_max);
38 }
Sameer Abu Asala8439542013-02-14 16:06:42 -080039 BucketiseValue(value);
Sameer Abu Asala8439542013-02-14 16:06:42 -080040}
41
Mathieu Chartier70a596d2014-12-17 14:56:47 -080042template <class Value> inline void Histogram<Value>::AdjustAndAddValue(Value value) {
43 AddValue(value / kAdjust);
44}
45
Mathieu Chartier19b0a912013-11-20 14:07:54 -080046template <class Value> inline Histogram<Value>::Histogram(const char* name)
47 : kAdjust(0),
48 kInitialBucketCount(0),
49 name_(name),
50 max_buckets_(0) {
51}
52
Sameer Abu Asala8439542013-02-14 16:06:42 -080053template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -070054inline Histogram<Value>::Histogram(const char* name, Value initial_bucket_width,
55 size_t max_buckets)
Sameer Abu Asala8439542013-02-14 16:06:42 -080056 : kAdjust(1000),
Mathieu Chartiere5426c92013-08-01 13:55:42 -070057 kInitialBucketCount(8),
58 name_(name),
59 max_buckets_(max_buckets),
60 bucket_width_(initial_bucket_width) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080061 Reset();
62}
63
64template <class Value>
65inline void Histogram<Value>::GrowBuckets(Value new_max) {
66 while (max_ < new_max) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -070067 // If we have reached the maximum number of buckets, merge buckets together.
68 if (frequency_.size() >= max_buckets_) {
69 CHECK(IsAligned<2>(frequency_.size()));
70 // We double the width of each bucket to reduce the number of buckets by a factor of 2.
71 bucket_width_ *= 2;
72 const size_t limit = frequency_.size() / 2;
73 // Merge the frequencies by adding each adjacent two together.
74 for (size_t i = 0; i < limit; ++i) {
75 frequency_[i] = frequency_[i * 2] + frequency_[i * 2 + 1];
76 }
77 // Remove frequencies in the second half of the array which were added to the first half.
78 while (frequency_.size() > limit) {
79 frequency_.pop_back();
80 }
81 }
Sameer Abu Asala8439542013-02-14 16:06:42 -080082 max_ += bucket_width_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080083 frequency_.push_back(0);
Sameer Abu Asala8439542013-02-14 16:06:42 -080084 }
85}
86
Mathieu Chartiere5426c92013-08-01 13:55:42 -070087template <class Value> inline size_t Histogram<Value>::FindBucket(Value val) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080088 // Since this is only a linear histogram, bucket index can be found simply with
89 // dividing the value by the bucket width.
90 DCHECK_GE(val, min_);
91 DCHECK_LE(val, max_);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070092 const size_t bucket_idx = static_cast<size_t>((val - min_) / bucket_width_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080093 DCHECK_GE(bucket_idx, 0ul);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070094 DCHECK_LE(bucket_idx, GetBucketCount());
Sameer Abu Asala8439542013-02-14 16:06:42 -080095 return bucket_idx;
96}
97
98template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -070099inline void Histogram<Value>::BucketiseValue(Value val) {
100 CHECK_LT(val, max_);
101 sum_ += val;
102 sum_of_squares_ += val * val;
103 ++sample_size_;
104 ++frequency_[FindBucket(val)];
105 max_value_added_ = std::max(val, max_value_added_);
106 min_value_added_ = std::min(val, min_value_added_);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800107}
108
109template <class Value> inline void Histogram<Value>::Initialize() {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700110 for (size_t idx = 0; idx < kInitialBucketCount; idx++) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800111 frequency_.push_back(0);
112 }
113 // Cumulative frequency and ranges has a length of 1 over frequency.
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700114 max_ = bucket_width_ * GetBucketCount();
115}
116
117template <class Value> inline size_t Histogram<Value>::GetBucketCount() const {
118 return frequency_.size();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800119}
120
121template <class Value> inline void Histogram<Value>::Reset() {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800122 sum_of_squares_ = 0;
123 sample_size_ = 0;
124 min_ = 0;
125 sum_ = 0;
126 min_value_added_ = std::numeric_limits<Value>::max();
127 max_value_added_ = std::numeric_limits<Value>::min();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800128 frequency_.clear();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800129 Initialize();
130}
131
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700132template <class Value> inline Value Histogram<Value>::GetRange(size_t bucket_idx) const {
133 DCHECK_LE(bucket_idx, GetBucketCount());
134 return min_ + bucket_idx * bucket_width_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800135}
136
137template <class Value> inline double Histogram<Value>::Mean() const {
138 DCHECK_GT(sample_size_, 0ull);
139 return static_cast<double>(sum_) / static_cast<double>(sample_size_);
140}
141
142template <class Value> inline double Histogram<Value>::Variance() const {
143 DCHECK_GT(sample_size_, 0ull);
144 // Using algorithms for calculating variance over a population:
145 // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
146 Value sum_squared = sum_ * sum_;
147 double sum_squared_by_n_squared =
148 static_cast<double>(sum_squared) /
149 static_cast<double>(sample_size_ * sample_size_);
150 double sum_of_squares_by_n =
151 static_cast<double>(sum_of_squares_) / static_cast<double>(sample_size_);
152 return sum_of_squares_by_n - sum_squared_by_n_squared;
153}
154
155template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700156inline void Histogram<Value>::PrintBins(std::ostream& os, const CumulativeData& data) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800157 DCHECK_GT(sample_size_, 0ull);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700158 for (size_t bin_idx = 0; bin_idx < data.freq_.size(); ++bin_idx) {
159 if (bin_idx > 0 && data.perc_[bin_idx] == data.perc_[bin_idx - 1]) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800160 bin_idx++;
161 continue;
162 }
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700163 os << GetRange(bin_idx) << ": " << data.freq_[bin_idx] << "\t"
164 << data.perc_[bin_idx] * 100.0 << "%\n";
Sameer Abu Asala8439542013-02-14 16:06:42 -0800165 }
166}
167
168template <class Value>
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700169inline void Histogram<Value>::DumpBins(std::ostream& os) const {
170 DCHECK_GT(sample_size_, 0ull);
171 bool dumped_one = false;
172 for (size_t bin_idx = 0; bin_idx < frequency_.size(); ++bin_idx) {
173 if (frequency_[bin_idx] != 0U) {
174 if (dumped_one) {
175 // Prepend a comma if not the first bin.
176 os << ",";
177 } else {
178 dumped_one = true;
179 }
180 os << GetRange(bin_idx) << ":" << frequency_[bin_idx];
181 }
182 }
183}
184
185template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700186inline void Histogram<Value>::PrintConfidenceIntervals(std::ostream &os, double interval,
187 const CumulativeData& data) const {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700188 static constexpr size_t kFractionalDigits = 3;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800189 DCHECK_GT(interval, 0);
190 DCHECK_LT(interval, 1.0);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700191 const double per_0 = (1.0 - interval) / 2.0;
192 const double per_1 = per_0 + interval;
193 const TimeUnit unit = GetAppropriateTimeUnit(Mean() * kAdjust);
194 os << Name() << ":\tSum: " << PrettyDuration(Sum() * kAdjust) << " "
195 << (interval * 100) << "% C.I. " << FormatDuration(Percentile(per_0, data) * kAdjust, unit,
196 kFractionalDigits)
197 << "-" << FormatDuration(Percentile(per_1, data) * kAdjust, unit, kFractionalDigits) << " "
198 << "Avg: " << FormatDuration(Mean() * kAdjust, unit, kFractionalDigits) << " Max: "
199 << FormatDuration(Max() * kAdjust, unit, kFractionalDigits) << "\n";
Sameer Abu Asala8439542013-02-14 16:06:42 -0800200}
201
Mathieu Chartierb2f99362013-11-20 17:26:00 -0800202template <class Value>
203inline void Histogram<Value>::CreateHistogram(CumulativeData* out_data) const {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700204 DCHECK_GT(sample_size_, 0ull);
Ian Rogers500793f2013-11-14 17:49:12 -0800205 out_data->freq_.clear();
206 out_data->perc_.clear();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800207 uint64_t accumulated = 0;
Ian Rogers500793f2013-11-14 17:49:12 -0800208 out_data->freq_.push_back(accumulated);
209 out_data->perc_.push_back(0.0);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800210 for (size_t idx = 0; idx < frequency_.size(); idx++) {
211 accumulated += frequency_[idx];
Ian Rogers500793f2013-11-14 17:49:12 -0800212 out_data->freq_.push_back(accumulated);
213 out_data->perc_.push_back(static_cast<double>(accumulated) / static_cast<double>(sample_size_));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800214 }
Ian Rogers500793f2013-11-14 17:49:12 -0800215 DCHECK_EQ(out_data->freq_.back(), sample_size_);
216 DCHECK_LE(std::abs(out_data->perc_.back() - 1.0), 0.001);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800217}
Sameer Abu Asala8439542013-02-14 16:06:42 -0800218
Ian Rogers647b1a82014-10-10 11:02:11 -0700219#if defined(__clang__)
220#pragma clang diagnostic push
221#pragma clang diagnostic ignored "-Wfloat-equal"
222#endif
223
Sameer Abu Asala8439542013-02-14 16:06:42 -0800224template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700225inline double Histogram<Value>::Percentile(double per, const CumulativeData& data) const {
226 DCHECK_GT(data.perc_.size(), 0ull);
227 size_t upper_idx = 0, lower_idx = 0;
228 for (size_t idx = 0; idx < data.perc_.size(); idx++) {
229 if (per <= data.perc_[idx]) {
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800230 upper_idx = idx;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800231 break;
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800232 }
233
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700234 if (per >= data.perc_[idx] && idx != 0 && data.perc_[idx] != data.perc_[idx - 1]) {
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800235 lower_idx = idx;
236 }
Sameer Abu Asala8439542013-02-14 16:06:42 -0800237 }
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800238
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700239 const double lower_perc = data.perc_[lower_idx];
240 const double lower_value = static_cast<double>(GetRange(lower_idx));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800241 if (per == lower_perc) {
242 return lower_value;
243 }
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700244
245 const double upper_perc = data.perc_[upper_idx];
246 const double upper_value = static_cast<double>(GetRange(upper_idx));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800247 if (per == upper_perc) {
248 return upper_value;
249 }
250 DCHECK_GT(upper_perc, lower_perc);
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800251
Sameer Abu Asala8439542013-02-14 16:06:42 -0800252 double value = lower_value + (upper_value - lower_value) *
253 (per - lower_perc) / (upper_perc - lower_perc);
Sameer Abu Asal857a0782013-02-21 11:00:20 -0800254
255 if (value < min_value_added_) {
256 value = min_value_added_;
257 } else if (value > max_value_added_) {
258 value = max_value_added_;
259 }
260
Sameer Abu Asala8439542013-02-14 16:06:42 -0800261 return value;
262}
263
Ian Rogers647b1a82014-10-10 11:02:11 -0700264#if defined(__clang__)
265#pragma clang diagnostic pop
266#endif
267
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800268} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700269#endif // ART_RUNTIME_BASE_HISTOGRAM_INL_H_