Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_HISTOGRAM_INL_H_ |
| 18 | #define ART_RUNTIME_BASE_HISTOGRAM_INL_H_ |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 19 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <cmath> |
| 22 | #include <limits> |
| 23 | #include <ostream> |
| 24 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "histogram.h" |
| 26 | |
| 27 | #include "base/bit_utils.h" |
| 28 | #include "base/time_utils.h" |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 29 | #include "utils.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 31 | namespace art { |
| 32 | |
| 33 | template <class Value> inline void Histogram<Value>::AddValue(Value value) { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 34 | CHECK_GE(value, static_cast<Value>(0)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 35 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 40 | BucketiseValue(value); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 43 | template <class Value> inline void Histogram<Value>::AdjustAndAddValue(Value value) { |
| 44 | AddValue(value / kAdjust); |
| 45 | } |
| 46 | |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 47 | template <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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 54 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 55 | inline Histogram<Value>::Histogram(const char* name, Value initial_bucket_width, |
| 56 | size_t max_buckets) |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 57 | : kAdjust(1000), |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 58 | kInitialBucketCount(8), |
| 59 | name_(name), |
| 60 | max_buckets_(max_buckets), |
| 61 | bucket_width_(initial_bucket_width) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 62 | Reset(); |
| 63 | } |
| 64 | |
| 65 | template <class Value> |
| 66 | inline void Histogram<Value>::GrowBuckets(Value new_max) { |
| 67 | while (max_ < new_max) { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 68 | // If we have reached the maximum number of buckets, merge buckets together. |
| 69 | if (frequency_.size() >= max_buckets_) { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 70 | CHECK_ALIGNED(frequency_.size(), 2); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 71 | // 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 83 | max_ += bucket_width_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 84 | frequency_.push_back(0); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 88 | template <class Value> inline size_t Histogram<Value>::FindBucket(Value val) const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 89 | // 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 Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 93 | const size_t bucket_idx = static_cast<size_t>((val - min_) / bucket_width_); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 94 | DCHECK_GE(bucket_idx, 0ul); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 95 | DCHECK_LE(bucket_idx, GetBucketCount()); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 96 | return bucket_idx; |
| 97 | } |
| 98 | |
| 99 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 100 | inline 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | template <class Value> inline void Histogram<Value>::Initialize() { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 111 | for (size_t idx = 0; idx < kInitialBucketCount; idx++) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 112 | frequency_.push_back(0); |
| 113 | } |
| 114 | // Cumulative frequency and ranges has a length of 1 over frequency. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 115 | max_ = bucket_width_ * GetBucketCount(); |
| 116 | } |
| 117 | |
| 118 | template <class Value> inline size_t Histogram<Value>::GetBucketCount() const { |
| 119 | return frequency_.size(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | template <class Value> inline void Histogram<Value>::Reset() { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 123 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 129 | frequency_.clear(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 130 | Initialize(); |
| 131 | } |
| 132 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 133 | template <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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | template <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 | |
| 143 | template <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 | |
| 156 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 157 | inline void Histogram<Value>::PrintBins(std::ostream& os, const CumulativeData& data) const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 158 | DCHECK_GT(sample_size_, 0ull); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 159 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 161 | bin_idx++; |
| 162 | continue; |
| 163 | } |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 164 | os << GetRange(bin_idx) << ": " << data.freq_[bin_idx] << "\t" |
| 165 | << data.perc_[bin_idx] * 100.0 << "%\n"; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | template <class Value> |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 170 | inline 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 | |
| 186 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 187 | inline void Histogram<Value>::PrintConfidenceIntervals(std::ostream &os, double interval, |
| 188 | const CumulativeData& data) const { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 189 | static constexpr size_t kFractionalDigits = 3; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 190 | DCHECK_GT(interval, 0); |
| 191 | DCHECK_LT(interval, 1.0); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 192 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Mathieu Chartier | b2f9936 | 2013-11-20 17:26:00 -0800 | [diff] [blame] | 203 | template <class Value> |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 204 | inline void Histogram<Value>::PrintMemoryUse(std::ostream &os) const { |
Pavel Vyssotski | 3e80aeb | 2016-05-17 16:37:53 +0600 | [diff] [blame^] | 205 | 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 Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | template <class Value> |
Mathieu Chartier | b2f9936 | 2013-11-20 17:26:00 -0800 | [diff] [blame] | 215 | inline void Histogram<Value>::CreateHistogram(CumulativeData* out_data) const { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 216 | DCHECK_GT(sample_size_, 0ull); |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 217 | out_data->freq_.clear(); |
| 218 | out_data->perc_.clear(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 219 | uint64_t accumulated = 0; |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 220 | out_data->freq_.push_back(accumulated); |
| 221 | out_data->perc_.push_back(0.0); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 222 | for (size_t idx = 0; idx < frequency_.size(); idx++) { |
| 223 | accumulated += frequency_[idx]; |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 224 | out_data->freq_.push_back(accumulated); |
| 225 | out_data->perc_.push_back(static_cast<double>(accumulated) / static_cast<double>(sample_size_)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 226 | } |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 227 | DCHECK_EQ(out_data->freq_.back(), sample_size_); |
| 228 | DCHECK_LE(std::abs(out_data->perc_.back() - 1.0), 0.001); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 229 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 230 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 231 | #if defined(__clang__) |
| 232 | #pragma clang diagnostic push |
| 233 | #pragma clang diagnostic ignored "-Wfloat-equal" |
| 234 | #endif |
| 235 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 236 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 237 | inline 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 Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 242 | upper_idx = idx; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 243 | break; |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 246 | if (per >= data.perc_[idx] && idx != 0 && data.perc_[idx] != data.perc_[idx - 1]) { |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 247 | lower_idx = idx; |
| 248 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 249 | } |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 250 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 251 | const double lower_perc = data.perc_[lower_idx]; |
| 252 | const double lower_value = static_cast<double>(GetRange(lower_idx)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 253 | if (per == lower_perc) { |
| 254 | return lower_value; |
| 255 | } |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 256 | |
| 257 | const double upper_perc = data.perc_[upper_idx]; |
| 258 | const double upper_value = static_cast<double>(GetRange(upper_idx)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 259 | if (per == upper_perc) { |
| 260 | return upper_value; |
| 261 | } |
| 262 | DCHECK_GT(upper_perc, lower_perc); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 263 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 264 | double value = lower_value + (upper_value - lower_value) * |
| 265 | (per - lower_perc) / (upper_perc - lower_perc); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 266 | |
| 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 273 | return value; |
| 274 | } |
| 275 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 276 | #if defined(__clang__) |
| 277 | #pragma clang diagnostic pop |
| 278 | #endif |
| 279 | |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 280 | } // namespace art |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 281 | #endif // ART_RUNTIME_BASE_HISTOGRAM_INL_H_ |