blob: 8724d2c582a2dfe4685814c2ba91f904ae55aa69 [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"
23#include "utils.h"
24
25namespace 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
32template <class Value> class Histogram {
33
34 const double kAdjust;
35 const Value kBucketWidth;
36 const size_t kInitialBucketCount;
37
38 public:
39 Histogram(std::string);
40 void AddValue(Value);
41 void CreateHistogram();
42 void Reset();
43 double Mean() const;
44 double Variance() const;
45 double Percentile(double) const;
46 void PrintConfidenceIntervals(std::ostream &, double) const;
47 void PrintBins(std::ostream &);
48
49 uint64_t SampleSize() const {
50 return sample_size_;
51 }
52
53 Value Sum() const {
54 return sum_;
55 }
56
57 Value Min() const {
58 return min_value_added_;
59 }
60
61 Value Max() const {
62 return max_value_added_;
63 }
64
65 const std::string &Name() const {
66 return name_;
67 }
68
69
70 private:
71 void BuildRanges();
72 void Initialize();
73 size_t FindBucket(Value);
74 void BucketiseValue(Value);
75 // Builds the cumulative distribution function from the frequency data.
76 // Must be called before using the percentile function.
77 void BuildCDF();
78 // Add more buckets to the histogram to fill in a new value that exceeded
79 // the max_read_value_.
80 void GrowBuckets(Value);
81 bool new_values_added_;
82 std::string name_;
83 // Number of samples placed in histogram.
84 uint64_t sample_size_;
85 // Width of the bucket range. The lower the value is the more accurate
86 // histogram percentiles are.
87 Value bucket_width_;
88 // Number of bucket to have in the histogram. this value will increase
89 // to accommodate for big values that don't fit in initial bucket ranges.
90 size_t bucket_count_;
91 // Represents the ranges of the histograms. Has SampleSize() + 1 elements
92 // e.g. 0,5,10,15 represents ranges 0-5, 5-10, 10-15
93 std::vector<Value> ranges_;
94 // How many occurrences of values fall within a corresponding range that is
95 // saved in the ranges_ vector.
96 std::vector<uint64_t> frequency_;
97 // Accumulative summation of frequencies.
98 // cumulative_freq_[i] = sum(cumulative_freq_[j] : 0 < j < i )
99 std::vector<uint64_t> cumulative_freq_;
100 // Accumulative summation of percentiles; which is the frequency / SampleSize
101 // cumulative_freq_[i] = sum(cumulative_freq_[j] : 0 < j < i )
102 std::vector<double> cumulative_perc_;
103 // Summation of all the elements inputed by the user.
104 Value sum_;
105 // Maximum value that can fit in the histogram, grows adaptively.
106 Value min_;
107 // Minimum value that can fit in the histogram. Fixed to zero for now.
108 Value max_;
109 // Summation of the values entered. Used to calculate variance.
110 Value sum_of_squares_;
111 // Maximum value entered in the histogram.
112 Value min_value_added_;
113 // Minimum value entered in the histogram.
114 Value max_value_added_;
115
116 DISALLOW_COPY_AND_ASSIGN(Histogram);
117};
118}
119
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700120#endif // ART_RUNTIME_BASE_HISTOGRAM_H_