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" |
| 29 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 30 | namespace art { |
| 31 | |
| 32 | template <class Value> inline void Histogram<Value>::AddValue(Value value) { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 33 | CHECK_GE(value, static_cast<Value>(0)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 34 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 39 | BucketiseValue(value); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 42 | template <class Value> inline void Histogram<Value>::AdjustAndAddValue(Value value) { |
| 43 | AddValue(value / kAdjust); |
| 44 | } |
| 45 | |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 46 | template <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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 53 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 54 | inline Histogram<Value>::Histogram(const char* name, Value initial_bucket_width, |
| 55 | size_t max_buckets) |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 56 | : kAdjust(1000), |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 57 | kInitialBucketCount(8), |
| 58 | name_(name), |
| 59 | max_buckets_(max_buckets), |
| 60 | bucket_width_(initial_bucket_width) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 61 | Reset(); |
| 62 | } |
| 63 | |
| 64 | template <class Value> |
| 65 | inline void Histogram<Value>::GrowBuckets(Value new_max) { |
| 66 | while (max_ < new_max) { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 67 | // 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 82 | max_ += bucket_width_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 83 | frequency_.push_back(0); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 87 | 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] | 88 | // 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 Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 92 | 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] | 93 | DCHECK_GE(bucket_idx, 0ul); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 94 | DCHECK_LE(bucket_idx, GetBucketCount()); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 95 | return bucket_idx; |
| 96 | } |
| 97 | |
| 98 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 99 | inline 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | template <class Value> inline void Histogram<Value>::Initialize() { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 110 | for (size_t idx = 0; idx < kInitialBucketCount; idx++) { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 111 | frequency_.push_back(0); |
| 112 | } |
| 113 | // Cumulative frequency and ranges has a length of 1 over frequency. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 114 | max_ = bucket_width_ * GetBucketCount(); |
| 115 | } |
| 116 | |
| 117 | template <class Value> inline size_t Histogram<Value>::GetBucketCount() const { |
| 118 | return frequency_.size(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | template <class Value> inline void Histogram<Value>::Reset() { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 122 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 128 | frequency_.clear(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 129 | Initialize(); |
| 130 | } |
| 131 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 132 | template <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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | template <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 | |
| 142 | template <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 | |
| 155 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 156 | 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] | 157 | DCHECK_GT(sample_size_, 0ull); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 158 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 160 | bin_idx++; |
| 161 | continue; |
| 162 | } |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 163 | os << GetRange(bin_idx) << ": " << data.freq_[bin_idx] << "\t" |
| 164 | << data.perc_[bin_idx] * 100.0 << "%\n"; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | template <class Value> |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 169 | inline 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 | |
| 185 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 186 | inline void Histogram<Value>::PrintConfidenceIntervals(std::ostream &os, double interval, |
| 187 | const CumulativeData& data) const { |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 188 | static constexpr size_t kFractionalDigits = 3; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 189 | DCHECK_GT(interval, 0); |
| 190 | DCHECK_LT(interval, 1.0); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 191 | 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Mathieu Chartier | b2f9936 | 2013-11-20 17:26:00 -0800 | [diff] [blame] | 202 | template <class Value> |
| 203 | inline void Histogram<Value>::CreateHistogram(CumulativeData* out_data) const { |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 204 | DCHECK_GT(sample_size_, 0ull); |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 205 | out_data->freq_.clear(); |
| 206 | out_data->perc_.clear(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 207 | uint64_t accumulated = 0; |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 208 | out_data->freq_.push_back(accumulated); |
| 209 | out_data->perc_.push_back(0.0); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 210 | for (size_t idx = 0; idx < frequency_.size(); idx++) { |
| 211 | accumulated += frequency_[idx]; |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 212 | out_data->freq_.push_back(accumulated); |
| 213 | 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] | 214 | } |
Ian Rogers | 500793f | 2013-11-14 17:49:12 -0800 | [diff] [blame] | 215 | DCHECK_EQ(out_data->freq_.back(), sample_size_); |
| 216 | 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] | 217 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 218 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 219 | #if defined(__clang__) |
| 220 | #pragma clang diagnostic push |
| 221 | #pragma clang diagnostic ignored "-Wfloat-equal" |
| 222 | #endif |
| 223 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 224 | template <class Value> |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 225 | inline 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 Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 230 | upper_idx = idx; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 231 | break; |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 234 | 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] | 235 | lower_idx = idx; |
| 236 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 237 | } |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 238 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 239 | const double lower_perc = data.perc_[lower_idx]; |
| 240 | const double lower_value = static_cast<double>(GetRange(lower_idx)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 241 | if (per == lower_perc) { |
| 242 | return lower_value; |
| 243 | } |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 244 | |
| 245 | const double upper_perc = data.perc_[upper_idx]; |
| 246 | const double upper_value = static_cast<double>(GetRange(upper_idx)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 247 | if (per == upper_perc) { |
| 248 | return upper_value; |
| 249 | } |
| 250 | DCHECK_GT(upper_perc, lower_perc); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 251 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 252 | double value = lower_value + (upper_value - lower_value) * |
| 253 | (per - lower_perc) / (upper_perc - lower_perc); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 254 | |
| 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 Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 261 | return value; |
| 262 | } |
| 263 | |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 264 | #if defined(__clang__) |
| 265 | #pragma clang diagnostic pop |
| 266 | #endif |
| 267 | |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 268 | } // namespace art |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 269 | #endif // ART_RUNTIME_BASE_HISTOGRAM_INL_H_ |