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 #ifndef ART_RUNTIME_BASE_HISTOGRAM_H_
17 #define ART_RUNTIME_BASE_HISTOGRAM_H_
18 
19 #include <vector>
20 #include <string>
21 
22 #include "base/logging.h"
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 {
32   const double kAdjust;
33   const size_t kInitialBucketCount;
34 
35  public:
36   class CumulativeData {
37     friend class Histogram<Value>;
38     std::vector<uint64_t> freq_;
39     std::vector<double> perc_;
40   };
41 
42   // Used by the cumulative timing logger to search the histogram set using for an existing split
43   // with the same name using CumulativeLogger::HistogramComparator.
44   explicit Histogram(const char* name);
45   // This is the expected constructor when creating new Histograms.
46   Histogram(const char* name, Value initial_bucket_width, size_t max_buckets = 100);
47   void AddValue(Value);
48   void AdjustAndAddValue(Value);  // Add a value after dividing it by kAdjust.
49   // 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 )
54   void CreateHistogram(CumulativeData* data) const;
55   // Reset the cumulative values, next time CreateHistogram is called it will recreate the cache.
56   void Reset();
57   double Mean() const;
58   double Variance() const;
59   double Percentile(double per, const CumulativeData& data) const;
60   void PrintConfidenceIntervals(std::ostream& os, double interval,
61                                 const CumulativeData& data) const;
62   void PrintBins(std::ostream& os, const CumulativeData& data) const;
63   void DumpBins(std::ostream& os) const;
64   Value GetRange(size_t bucket_idx) const;
65   size_t GetBucketCount() const;
66 
SampleSize()67   uint64_t SampleSize() const {
68     return sample_size_;
69   }
70 
Sum()71   Value Sum() const {
72     return sum_;
73   }
74 
AdjustedSum()75   Value AdjustedSum() const {
76     return sum_ * kAdjust;
77   }
78 
Min()79   Value Min() const {
80     return min_value_added_;
81   }
82 
Max()83   Value Max() const {
84     return max_value_added_;
85   }
86 
Name()87   const std::string& Name() const {
88     return name_;
89   }
90 
91  private:
92   void Initialize();
93   size_t FindBucket(Value val) const;
94   void BucketiseValue(Value val);
95   // Add more buckets to the histogram to fill in a new value that exceeded
96   // the max_read_value_.
97   void GrowBuckets(Value val);
98   std::string name_;
99   // Maximum number of buckets.
100   const size_t max_buckets_;
101   // Number of samples placed in histogram.
102   size_t sample_size_;
103   // Width of the bucket range. The lower the value is the more accurate
104   // histogram percentiles are. Grows adaptively when we hit max buckets.
105   Value bucket_width_;
106   // How many occurrences of values fall within a bucket at index i where i covers the range
107   // starting at  min_ + i * bucket_width_ with size bucket_size_.
108   std::vector<uint32_t> frequency_;
109   // Summation of all the elements inputed by the user.
110   Value sum_;
111   // Minimum value that can fit in the histogram. Fixed to zero for now.
112   Value min_;
113   // Maximum value that can fit in the histogram, grows adaptively.
114   Value max_;
115   // Summation of the values entered. Used to calculate variance.
116   Value sum_of_squares_;
117   // Maximum value entered in the histogram.
118   Value min_value_added_;
119   // Minimum value entered in the histogram.
120   Value max_value_added_;
121 
122   DISALLOW_COPY_AND_ASSIGN(Histogram);
123 };
124 }  // namespace art
125 
126 #endif  // ART_RUNTIME_BASE_HISTOGRAM_H_
127