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 | */ |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 16 | #ifndef ART_LIBARTBASE_BASE_HISTOGRAM_H_ |
| 17 | #define ART_LIBARTBASE_BASE_HISTOGRAM_H_ |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 18 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 19 | #include <string> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <vector> |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 21 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include <android-base/macros.h> |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | // Creates a data histogram for a better understanding of statistical data. |
| 27 | // Histogram analysis goes beyond simple mean and standard deviation to provide |
| 28 | // percentiles values, describing where the $% of the input data lies. |
| 29 | // Designed to be simple and used with timing logger in art. |
| 30 | |
| 31 | template <class Value> class Histogram { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 32 | const double kAdjust; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 33 | const size_t kInitialBucketCount; |
| 34 | |
| 35 | public: |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 36 | class CumulativeData { |
| 37 | friend class Histogram<Value>; |
| 38 | std::vector<uint64_t> freq_; |
| 39 | std::vector<double> perc_; |
| 40 | }; |
| 41 | |
Hans Boehm | 1973687 | 2019-04-22 10:20:22 -0700 | [diff] [blame] | 42 | // Minimum and initial number of allocated buckets in histogram. |
| 43 | static constexpr size_t kMinBuckets = 8; |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 44 | // Used by the cumulative timing logger to search the histogram set using for an existing split |
| 45 | // with the same name using CumulativeLogger::HistogramComparator. |
Mathieu Chartier | 19b0a91 | 2013-11-20 14:07:54 -0800 | [diff] [blame] | 46 | explicit Histogram(const char* name); |
Hans Boehm | 1973687 | 2019-04-22 10:20:22 -0700 | [diff] [blame] | 47 | // This is the expected constructor when creating new Histograms. Max_buckets must be even. |
| 48 | // Max_buckets, if specified, must be at least kMinBuckets. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 49 | Histogram(const char* name, Value initial_bucket_width, size_t max_buckets = 100); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 50 | void AddValue(Value); |
Mathieu Chartier | 70a596d | 2014-12-17 14:56:47 -0800 | [diff] [blame] | 51 | void AdjustAndAddValue(Value); // Add a value after dividing it by kAdjust. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 52 | // Builds the cumulative distribution function from the frequency data. |
| 53 | // Accumulative summation of frequencies. |
| 54 | // cumulative_freq[i] = sum(frequency[j] : 0 < j < i ) |
| 55 | // Accumulative summation of percentiles; which is the frequency / SampleSize |
| 56 | // cumulative_perc[i] = sum(frequency[j] / SampleSize : 0 < j < i ) |
Mathieu Chartier | b2f9936 | 2013-11-20 17:26:00 -0800 | [diff] [blame] | 57 | void CreateHistogram(CumulativeData* data) const; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 58 | // Reset the cumulative values, next time CreateHistogram is called it will recreate the cache. |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 59 | void Reset(); |
| 60 | double Mean() const; |
| 61 | double Variance() const; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 62 | double Percentile(double per, const CumulativeData& data) const; |
| 63 | void PrintConfidenceIntervals(std::ostream& os, double interval, |
| 64 | const CumulativeData& data) const; |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 65 | void PrintMemoryUse(std::ostream& os) const; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 66 | void PrintBins(std::ostream& os, const CumulativeData& data) const; |
Hiroshi Yamauchi | a1c9f01 | 2015-04-02 10:18:12 -0700 | [diff] [blame] | 67 | void DumpBins(std::ostream& os) const; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 68 | Value GetRange(size_t bucket_idx) const; |
| 69 | size_t GetBucketCount() const; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 70 | |
| 71 | uint64_t SampleSize() const { |
| 72 | return sample_size_; |
| 73 | } |
| 74 | |
| 75 | Value Sum() const { |
| 76 | return sum_; |
| 77 | } |
| 78 | |
Mathieu Chartier | 19d46b4 | 2014-06-17 15:04:40 -0700 | [diff] [blame] | 79 | Value AdjustedSum() const { |
| 80 | return sum_ * kAdjust; |
| 81 | } |
| 82 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 83 | Value Min() const { |
| 84 | return min_value_added_; |
| 85 | } |
| 86 | |
| 87 | Value Max() const { |
| 88 | return max_value_added_; |
| 89 | } |
| 90 | |
Mathieu Chartier | 0dce75d | 2016-05-11 11:35:39 -0700 | [diff] [blame] | 91 | Value BucketWidth() const { |
| 92 | return bucket_width_; |
| 93 | } |
| 94 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 95 | const std::string& Name() const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 96 | return name_; |
| 97 | } |
| 98 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 99 | private: |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 100 | void Initialize(); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 101 | size_t FindBucket(Value val) const; |
| 102 | void BucketiseValue(Value val); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 103 | // Add more buckets to the histogram to fill in a new value that exceeded |
| 104 | // the max_read_value_. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 105 | void GrowBuckets(Value val); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 106 | std::string name_; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 107 | // Maximum number of buckets. |
| 108 | const size_t max_buckets_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 109 | // Number of samples placed in histogram. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 110 | size_t sample_size_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 111 | // Width of the bucket range. The lower the value is the more accurate |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 112 | // histogram percentiles are. Grows adaptively when we hit max buckets. |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 113 | Value bucket_width_; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 114 | // How many occurrences of values fall within a bucket at index i where i covers the range |
| 115 | // starting at min_ + i * bucket_width_ with size bucket_size_. |
| 116 | std::vector<uint32_t> frequency_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 117 | // Summation of all the elements inputed by the user. |
| 118 | Value sum_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 119 | // Minimum value that can fit in the histogram. Fixed to zero for now. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 120 | Value min_; |
| 121 | // Maximum value that can fit in the histogram, grows adaptively. |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 122 | Value max_; |
| 123 | // Summation of the values entered. Used to calculate variance. |
| 124 | Value sum_of_squares_; |
| 125 | // Maximum value entered in the histogram. |
| 126 | Value min_value_added_; |
| 127 | // Minimum value entered in the histogram. |
| 128 | Value max_value_added_; |
| 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(Histogram); |
| 131 | }; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 132 | } // namespace art |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 133 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 134 | #endif // ART_LIBARTBASE_BASE_HISTOGRAM_H_ |