blob: 4af47d170a61fd9f9290dd556dfb18c3496202ac [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"
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000029#include "utils.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030
Sameer Abu Asala8439542013-02-14 16:06:42 -080031namespace art {
32
33template <class Value> inline void Histogram<Value>::AddValue(Value value) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -070034 CHECK_GE(value, static_cast<Value>(0));
Sameer Abu Asala8439542013-02-14 16:06:42 -080035 if (value >= max_) {
36 Value new_max = ((value + 1) / bucket_width_ + 1) * bucket_width_;
37 DCHECK_GT(new_max, max_);
38 GrowBuckets(new_max);
39 }
Sameer Abu Asala8439542013-02-14 16:06:42 -080040 BucketiseValue(value);
Sameer Abu Asala8439542013-02-14 16:06:42 -080041}
42
Mathieu Chartier70a596d2014-12-17 14:56:47 -080043template <class Value> inline void Histogram<Value>::AdjustAndAddValue(Value value) {
44 AddValue(value / kAdjust);
45}
46
Mathieu Chartier19b0a912013-11-20 14:07:54 -080047template <class Value> inline Histogram<Value>::Histogram(const char* name)
48 : kAdjust(0),
49 kInitialBucketCount(0),
50 name_(name),
51 max_buckets_(0) {
52}
53
Sameer Abu Asala8439542013-02-14 16:06:42 -080054template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -070055inline Histogram<Value>::Histogram(const char* name, Value initial_bucket_width,
56 size_t max_buckets)
Sameer Abu Asala8439542013-02-14 16:06:42 -080057 : kAdjust(1000),
Mathieu Chartiere5426c92013-08-01 13:55:42 -070058 kInitialBucketCount(8),
59 name_(name),
60 max_buckets_(max_buckets),
61 bucket_width_(initial_bucket_width) {
Sameer Abu Asala8439542013-02-14 16:06:42 -080062 Reset();
63}
64
65template <class Value>
66inline void Histogram<Value>::GrowBuckets(Value new_max) {
67 while (max_ < new_max) {
Mathieu Chartiere5426c92013-08-01 13:55:42 -070068 // If we have reached the maximum number of buckets, merge buckets together.
69 if (frequency_.size() >= max_buckets_) {
Roland Levillain14d90572015-07-16 10:52:26 +010070 CHECK_ALIGNED(frequency_.size(), 2);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070071 // We double the width of each bucket to reduce the number of buckets by a factor of 2.
72 bucket_width_ *= 2;
73 const size_t limit = frequency_.size() / 2;
74 // Merge the frequencies by adding each adjacent two together.
75 for (size_t i = 0; i < limit; ++i) {
76 frequency_[i] = frequency_[i * 2] + frequency_[i * 2 + 1];
77 }
78 // Remove frequencies in the second half of the array which were added to the first half.
79 while (frequency_.size() > limit) {
80 frequency_.pop_back();
81 }
82 }
Sameer Abu Asala8439542013-02-14 16:06:42 -080083 max_ += bucket_width_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080084 frequency_.push_back(0);
Sameer Abu Asala8439542013-02-14 16:06:42 -080085 }
86}
87
Mathieu Chartiere5426c92013-08-01 13:55:42 -070088template <class Value> inline size_t Histogram<Value>::FindBucket(Value val) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080089 // Since this is only a linear histogram, bucket index can be found simply with
90 // dividing the value by the bucket width.
91 DCHECK_GE(val, min_);
92 DCHECK_LE(val, max_);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070093 const size_t bucket_idx = static_cast<size_t>((val - min_) / bucket_width_);
Sameer Abu Asala8439542013-02-14 16:06:42 -080094 DCHECK_GE(bucket_idx, 0ul);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070095 DCHECK_LE(bucket_idx, GetBucketCount());
Sameer Abu Asala8439542013-02-14 16:06:42 -080096 return bucket_idx;
97}
98
99template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700100inline void Histogram<Value>::BucketiseValue(Value val) {
101 CHECK_LT(val, max_);
102 sum_ += val;
103 sum_of_squares_ += val * val;
104 ++sample_size_;
105 ++frequency_[FindBucket(val)];
106 max_value_added_ = std::max(val, max_value_added_);
107 min_value_added_ = std::min(val, min_value_added_);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800108}
109
110template <class Value> inline void Histogram<Value>::Initialize() {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700111 for (size_t idx = 0; idx < kInitialBucketCount; idx++) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800112 frequency_.push_back(0);
113 }
114 // Cumulative frequency and ranges has a length of 1 over frequency.
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700115 max_ = bucket_width_ * GetBucketCount();
116}
117
118template <class Value> inline size_t Histogram<Value>::GetBucketCount() const {
119 return frequency_.size();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800120}
121
122template <class Value> inline void Histogram<Value>::Reset() {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800123 sum_of_squares_ = 0;
124 sample_size_ = 0;
125 min_ = 0;
126 sum_ = 0;
127 min_value_added_ = std::numeric_limits<Value>::max();
128 max_value_added_ = std::numeric_limits<Value>::min();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800129 frequency_.clear();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800130 Initialize();
131}
132
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700133template <class Value> inline Value Histogram<Value>::GetRange(size_t bucket_idx) const {
134 DCHECK_LE(bucket_idx, GetBucketCount());
135 return min_ + bucket_idx * bucket_width_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800136}
137
138template <class Value> inline double Histogram<Value>::Mean() const {
139 DCHECK_GT(sample_size_, 0ull);
140 return static_cast<double>(sum_) / static_cast<double>(sample_size_);
141}
142
143template <class Value> inline double Histogram<Value>::Variance() const {
144 DCHECK_GT(sample_size_, 0ull);
145 // Using algorithms for calculating variance over a population:
146 // http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
147 Value sum_squared = sum_ * sum_;
148 double sum_squared_by_n_squared =
149 static_cast<double>(sum_squared) /
150 static_cast<double>(sample_size_ * sample_size_);
151 double sum_of_squares_by_n =
152 static_cast<double>(sum_of_squares_) / static_cast<double>(sample_size_);
153 return sum_of_squares_by_n - sum_squared_by_n_squared;
154}
155
156template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700157inline void Histogram<Value>::PrintBins(std::ostream& os, const CumulativeData& data) const {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800158 DCHECK_GT(sample_size_, 0ull);
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700159 for (size_t bin_idx = 0; bin_idx < data.freq_.size(); ++bin_idx) {
160 if (bin_idx > 0 && data.perc_[bin_idx] == data.perc_[bin_idx - 1]) {
Sameer Abu Asala8439542013-02-14 16:06:42 -0800161 bin_idx++;
162 continue;
163 }
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700164 os << GetRange(bin_idx) << ": " << data.freq_[bin_idx] << "\t"
165 << data.perc_[bin_idx] * 100.0 << "%\n";
Sameer Abu Asala8439542013-02-14 16:06:42 -0800166 }
167}
168
169template <class Value>
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700170inline void Histogram<Value>::DumpBins(std::ostream& os) const {
171 DCHECK_GT(sample_size_, 0ull);
172 bool dumped_one = false;
173 for (size_t bin_idx = 0; bin_idx < frequency_.size(); ++bin_idx) {
174 if (frequency_[bin_idx] != 0U) {
175 if (dumped_one) {
176 // Prepend a comma if not the first bin.
177 os << ",";
178 } else {
179 dumped_one = true;
180 }
181 os << GetRange(bin_idx) << ":" << frequency_[bin_idx];
182 }
183 }
184}
185
186template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700187inline void Histogram<Value>::PrintConfidenceIntervals(std::ostream &os, double interval,
188 const CumulativeData& data) const {
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700189 static constexpr size_t kFractionalDigits = 3;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800190 DCHECK_GT(interval, 0);
191 DCHECK_LT(interval, 1.0);
Mathieu Chartierf5997b42014-06-20 10:37:54 -0700192 const double per_0 = (1.0 - interval) / 2.0;
193 const double per_1 = per_0 + interval;
194 const TimeUnit unit = GetAppropriateTimeUnit(Mean() * kAdjust);
195 os << Name() << ":\tSum: " << PrettyDuration(Sum() * kAdjust) << " "
196 << (interval * 100) << "% C.I. " << FormatDuration(Percentile(per_0, data) * kAdjust, unit,
197 kFractionalDigits)
198 << "-" << FormatDuration(Percentile(per_1, data) * kAdjust, unit, kFractionalDigits) << " "
199 << "Avg: " << FormatDuration(Mean() * kAdjust, unit, kFractionalDigits) << " Max: "
200 << FormatDuration(Max() * kAdjust, unit, kFractionalDigits) << "\n";
Sameer Abu Asala8439542013-02-14 16:06:42 -0800201}
202
Mathieu Chartierb2f99362013-11-20 17:26:00 -0800203template <class Value>
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000204inline void Histogram<Value>::PrintMemoryUse(std::ostream &os) const {
Pavel Vyssotski3e80aeb2016-05-17 16:37:53 +0600205 os << Name();
206 if (sample_size_ != 0u) {
207 os << ": Avg: " << PrettySize(Mean()) << " Max: "
208 << PrettySize(Max()) << " Min: " << PrettySize(Min()) << "\n";
209 } else {
210 os << ": <no data>\n";
211 }
Nicolas Geoffraya4f81542016-03-08 16:57:48 +0000212}
213
214template <class Value>
Mathieu Chartierb2f99362013-11-20 17:26:00 -0800215inline void Histogram<Value>::CreateHistogram(CumulativeData* out_data) const {
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700216 DCHECK_GT(sample_size_, 0ull);
Ian Rogers500793f2013-11-14 17:49:12 -0800217 out_data->freq_.clear();
218 out_data->perc_.clear();
Sameer Abu Asala8439542013-02-14 16:06:42 -0800219 uint64_t accumulated = 0;
Ian Rogers500793f2013-11-14 17:49:12 -0800220 out_data->freq_.push_back(accumulated);
221 out_data->perc_.push_back(0.0);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800222 for (size_t idx = 0; idx < frequency_.size(); idx++) {
223 accumulated += frequency_[idx];
Ian Rogers500793f2013-11-14 17:49:12 -0800224 out_data->freq_.push_back(accumulated);
225 out_data->perc_.push_back(static_cast<double>(accumulated) / static_cast<double>(sample_size_));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800226 }
Ian Rogers500793f2013-11-14 17:49:12 -0800227 DCHECK_EQ(out_data->freq_.back(), sample_size_);
228 DCHECK_LE(std::abs(out_data->perc_.back() - 1.0), 0.001);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800229}
Sameer Abu Asala8439542013-02-14 16:06:42 -0800230
Ian Rogers647b1a82014-10-10 11:02:11 -0700231#if defined(__clang__)
232#pragma clang diagnostic push
233#pragma clang diagnostic ignored "-Wfloat-equal"
234#endif
235
Sameer Abu Asala8439542013-02-14 16:06:42 -0800236template <class Value>
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700237inline double Histogram<Value>::Percentile(double per, const CumulativeData& data) const {
238 DCHECK_GT(data.perc_.size(), 0ull);
239 size_t upper_idx = 0, lower_idx = 0;
240 for (size_t idx = 0; idx < data.perc_.size(); idx++) {
241 if (per <= data.perc_[idx]) {
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800242 upper_idx = idx;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800243 break;
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800244 }
245
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700246 if (per >= data.perc_[idx] && idx != 0 && data.perc_[idx] != data.perc_[idx - 1]) {
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800247 lower_idx = idx;
248 }
Sameer Abu Asala8439542013-02-14 16:06:42 -0800249 }
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800250
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700251 const double lower_perc = data.perc_[lower_idx];
252 const double lower_value = static_cast<double>(GetRange(lower_idx));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800253 if (per == lower_perc) {
254 return lower_value;
255 }
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700256
257 const double upper_perc = data.perc_[upper_idx];
258 const double upper_value = static_cast<double>(GetRange(upper_idx));
Sameer Abu Asala8439542013-02-14 16:06:42 -0800259 if (per == upper_perc) {
260 return upper_value;
261 }
262 DCHECK_GT(upper_perc, lower_perc);
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800263
Sameer Abu Asala8439542013-02-14 16:06:42 -0800264 double value = lower_value + (upper_value - lower_value) *
265 (per - lower_perc) / (upper_perc - lower_perc);
Sameer Abu Asal857a0782013-02-21 11:00:20 -0800266
267 if (value < min_value_added_) {
268 value = min_value_added_;
269 } else if (value > max_value_added_) {
270 value = max_value_added_;
271 }
272
Sameer Abu Asala8439542013-02-14 16:06:42 -0800273 return value;
274}
275
Ian Rogers647b1a82014-10-10 11:02:11 -0700276#if defined(__clang__)
277#pragma clang diagnostic pop
278#endif
279
Sameer Abu Asalc081e362013-02-20 16:45:38 -0800280} // namespace art
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700281#endif // ART_RUNTIME_BASE_HISTOGRAM_INL_H_