blob: 0e3bc8e1b4200c7aed469a36cc0cf93a19b35c68 [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 */
Brian Carlstromfc0e3212013-07-17 14:40:12 -070016#ifndef ART_RUNTIME_BASE_HISTOGRAM_H_
17#define ART_RUNTIME_BASE_HISTOGRAM_H_
Sameer Abu Asala8439542013-02-14 16:06:42 -080018
19#include <vector>
20#include <string>
21
22#include "base/logging.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080023
24namespace 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
31template <class Value> class Histogram {
Sameer Abu Asala8439542013-02-14 16:06:42 -080032 const double kAdjust;
Sameer Abu Asala8439542013-02-14 16:06:42 -080033 const size_t kInitialBucketCount;
34
35 public:
Mathieu Chartiere5426c92013-08-01 13:55:42 -070036 class CumulativeData {
37 friend class Histogram<Value>;
38 std::vector<uint64_t> freq_;
39 std::vector<double> perc_;
40 };
41
Mathieu Chartier7410f292013-11-24 13:17:35 -080042 // Used by the cumulative timing logger to search the histogram set using for an existing split
43 // with the same name using CumulativeLogger::HistogramComparator.
Mathieu Chartier19b0a912013-11-20 14:07:54 -080044 explicit Histogram(const char* name);
Mathieu Chartier7410f292013-11-24 13:17:35 -080045 // This is the expected constructor when creating new Histograms.
Mathieu Chartiere5426c92013-08-01 13:55:42 -070046 Histogram(const char* name, Value initial_bucket_width, size_t max_buckets = 100);
Sameer Abu Asala8439542013-02-14 16:06:42 -080047 void AddValue(Value);
Mathieu Chartier70a596d2014-12-17 14:56:47 -080048 void AdjustAndAddValue(Value); // Add a value after dividing it by kAdjust.
Mathieu Chartiere5426c92013-08-01 13:55:42 -070049 // Builds the cumulative distribution function from the frequency data.
50 // Accumulative summation of frequencies.
51 // cumulative_freq[i] = sum(frequency[j] : 0 < j < i )
52 // Accumulative summation of percentiles; which is the frequency / SampleSize
53 // cumulative_perc[i] = sum(frequency[j] / SampleSize : 0 < j < i )
Mathieu Chartierb2f99362013-11-20 17:26:00 -080054 void CreateHistogram(CumulativeData* data) const;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070055 // Reset the cumulative values, next time CreateHistogram is called it will recreate the cache.
Sameer Abu Asala8439542013-02-14 16:06:42 -080056 void Reset();
57 double Mean() const;
58 double Variance() const;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070059 double Percentile(double per, const CumulativeData& data) const;
60 void PrintConfidenceIntervals(std::ostream& os, double interval,
61 const CumulativeData& data) const;
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000062 void PrintMemoryUse(std::ostream& os) const;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070063 void PrintBins(std::ostream& os, const CumulativeData& data) const;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -070064 void DumpBins(std::ostream& os) const;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070065 Value GetRange(size_t bucket_idx) const;
66 size_t GetBucketCount() const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080067
68 uint64_t SampleSize() const {
69 return sample_size_;
70 }
71
72 Value Sum() const {
73 return sum_;
74 }
75
Mathieu Chartier19d46b42014-06-17 15:04:40 -070076 Value AdjustedSum() const {
77 return sum_ * kAdjust;
78 }
79
Sameer Abu Asala8439542013-02-14 16:06:42 -080080 Value Min() const {
81 return min_value_added_;
82 }
83
84 Value Max() const {
85 return max_value_added_;
86 }
87
Mathieu Chartier0dce75d2016-05-11 11:35:39 -070088 Value BucketWidth() const {
89 return bucket_width_;
90 }
91
Mathieu Chartiere5426c92013-08-01 13:55:42 -070092 const std::string& Name() const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080093 return name_;
94 }
95
Sameer Abu Asala8439542013-02-14 16:06:42 -080096 private:
Sameer Abu Asala8439542013-02-14 16:06:42 -080097 void Initialize();
Mathieu Chartiere5426c92013-08-01 13:55:42 -070098 size_t FindBucket(Value val) const;
99 void BucketiseValue(Value val);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800100 // Add more buckets to the histogram to fill in a new value that exceeded
101 // the max_read_value_.
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700102 void GrowBuckets(Value val);
Sameer Abu Asala8439542013-02-14 16:06:42 -0800103 std::string name_;
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700104 // Maximum number of buckets.
105 const size_t max_buckets_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800106 // Number of samples placed in histogram.
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700107 size_t sample_size_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800108 // Width of the bucket range. The lower the value is the more accurate
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700109 // histogram percentiles are. Grows adaptively when we hit max buckets.
Sameer Abu Asala8439542013-02-14 16:06:42 -0800110 Value bucket_width_;
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700111 // How many occurrences of values fall within a bucket at index i where i covers the range
112 // starting at min_ + i * bucket_width_ with size bucket_size_.
113 std::vector<uint32_t> frequency_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800114 // Summation of all the elements inputed by the user.
115 Value sum_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800116 // Minimum value that can fit in the histogram. Fixed to zero for now.
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700117 Value min_;
118 // Maximum value that can fit in the histogram, grows adaptively.
Sameer Abu Asala8439542013-02-14 16:06:42 -0800119 Value max_;
120 // Summation of the values entered. Used to calculate variance.
121 Value sum_of_squares_;
122 // Maximum value entered in the histogram.
123 Value min_value_added_;
124 // Minimum value entered in the histogram.
125 Value max_value_added_;
126
127 DISALLOW_COPY_AND_ASSIGN(Histogram);
128};
Mathieu Chartier02e25112013-08-14 16:14:24 -0700129} // namespace art
Sameer Abu Asala8439542013-02-14 16:06:42 -0800130
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700131#endif // ART_RUNTIME_BASE_HISTOGRAM_H_