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 | */ |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 16 | #ifndef ART_RUNTIME_BASE_HISTOGRAM_H_ |
| 17 | #define ART_RUNTIME_BASE_HISTOGRAM_H_ |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 18 | |
| 19 | #include <vector> |
| 20 | #include <string> |
| 21 | |
| 22 | #include "base/logging.h" |
| 23 | #include "utils.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | // Creates a data histogram for a better understanding of statistical data. |
| 28 | // Histogram analysis goes beyond simple mean and standard deviation to provide |
| 29 | // percentiles values, describing where the $% of the input data lies. |
| 30 | // Designed to be simple and used with timing logger in art. |
| 31 | |
| 32 | template <class Value> class Histogram { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 33 | const double kAdjust; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 34 | const size_t kInitialBucketCount; |
| 35 | |
| 36 | public: |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 37 | class CumulativeData { |
| 38 | friend class Histogram<Value>; |
| 39 | std::vector<uint64_t> freq_; |
| 40 | std::vector<double> perc_; |
| 41 | }; |
| 42 | |
| 43 | 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] | 44 | void AddValue(Value); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 45 | // Builds the cumulative distribution function from the frequency data. |
| 46 | // Accumulative summation of frequencies. |
| 47 | // cumulative_freq[i] = sum(frequency[j] : 0 < j < i ) |
| 48 | // Accumulative summation of percentiles; which is the frequency / SampleSize |
| 49 | // cumulative_perc[i] = sum(frequency[j] / SampleSize : 0 < j < i ) |
| 50 | void CreateHistogram(CumulativeData& data); |
| 51 | // 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] | 52 | void Reset(); |
| 53 | double Mean() const; |
| 54 | double Variance() const; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 55 | double Percentile(double per, const CumulativeData& data) const; |
| 56 | void PrintConfidenceIntervals(std::ostream& os, double interval, |
| 57 | const CumulativeData& data) const; |
| 58 | void PrintBins(std::ostream& os, const CumulativeData& data) const; |
| 59 | Value GetRange(size_t bucket_idx) const; |
| 60 | size_t GetBucketCount() const; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 61 | |
| 62 | uint64_t SampleSize() const { |
| 63 | return sample_size_; |
| 64 | } |
| 65 | |
| 66 | Value Sum() const { |
| 67 | return sum_; |
| 68 | } |
| 69 | |
| 70 | Value Min() const { |
| 71 | return min_value_added_; |
| 72 | } |
| 73 | |
| 74 | Value Max() const { |
| 75 | return max_value_added_; |
| 76 | } |
| 77 | |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 78 | const std::string& Name() const { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 79 | return name_; |
| 80 | } |
| 81 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 82 | private: |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 83 | void Initialize(); |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 84 | size_t FindBucket(Value val) const; |
| 85 | void BucketiseValue(Value val); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 86 | // Add more buckets to the histogram to fill in a new value that exceeded |
| 87 | // the max_read_value_. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 88 | void GrowBuckets(Value val); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 89 | std::string name_; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 90 | // Maximum number of buckets. |
| 91 | const size_t max_buckets_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 92 | // Number of samples placed in histogram. |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 93 | size_t sample_size_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 94 | // 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] | 95 | // histogram percentiles are. Grows adaptively when we hit max buckets. |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 96 | Value bucket_width_; |
Mathieu Chartier | e5426c9 | 2013-08-01 13:55:42 -0700 | [diff] [blame] | 97 | // How many occurrences of values fall within a bucket at index i where i covers the range |
| 98 | // starting at min_ + i * bucket_width_ with size bucket_size_. |
| 99 | std::vector<uint32_t> frequency_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 100 | // Summation of all the elements inputed by the user. |
| 101 | Value sum_; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 102 | // 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] | 103 | Value min_; |
| 104 | // Maximum value that can fit in the histogram, grows adaptively. |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 105 | Value max_; |
| 106 | // Summation of the values entered. Used to calculate variance. |
| 107 | Value sum_of_squares_; |
| 108 | // Maximum value entered in the histogram. |
| 109 | Value min_value_added_; |
| 110 | // Minimum value entered in the histogram. |
| 111 | Value max_value_added_; |
| 112 | |
| 113 | DISALLOW_COPY_AND_ASSIGN(Histogram); |
| 114 | }; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 115 | } // namespace art |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 116 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 117 | #endif // ART_RUNTIME_BASE_HISTOGRAM_H_ |