blob: 4e5d29a2977d20128aceba1a55d031b4adcf7b64 [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 {
Sameer Abu Asala8439542013-02-14 16:06:42 -080033 const double kAdjust;
Sameer Abu Asala8439542013-02-14 16:06:42 -080034 const size_t kInitialBucketCount;
35
36 public:
Mathieu Chartiere5426c92013-08-01 13:55:42 -070037 class CumulativeData {
38 friend class Histogram<Value>;
39 std::vector<uint64_t> freq_;
40 std::vector<double> perc_;
41 };
42
Mathieu Chartier19b0a912013-11-20 14:07:54 -080043 // Used for name based comparators in the timing loggers.
44 explicit Histogram(const char* name);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070045 Histogram(const char* name, Value initial_bucket_width, size_t max_buckets = 100);
Sameer Abu Asala8439542013-02-14 16:06:42 -080046 void AddValue(Value);
Mathieu Chartiere5426c92013-08-01 13:55:42 -070047 // Builds the cumulative distribution function from the frequency data.
48 // Accumulative summation of frequencies.
49 // cumulative_freq[i] = sum(frequency[j] : 0 < j < i )
50 // Accumulative summation of percentiles; which is the frequency / SampleSize
51 // cumulative_perc[i] = sum(frequency[j] / SampleSize : 0 < j < i )
Mathieu Chartierb2f99362013-11-20 17:26:00 -080052 void CreateHistogram(CumulativeData* data) const;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070053 // Reset the cumulative values, next time CreateHistogram is called it will recreate the cache.
Sameer Abu Asala8439542013-02-14 16:06:42 -080054 void Reset();
55 double Mean() const;
56 double Variance() const;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070057 double Percentile(double per, const CumulativeData& data) const;
58 void PrintConfidenceIntervals(std::ostream& os, double interval,
59 const CumulativeData& data) const;
60 void PrintBins(std::ostream& os, const CumulativeData& data) const;
61 Value GetRange(size_t bucket_idx) const;
62 size_t GetBucketCount() const;
Sameer Abu Asala8439542013-02-14 16:06:42 -080063
64 uint64_t SampleSize() const {
65 return sample_size_;
66 }
67
68 Value Sum() const {
69 return sum_;
70 }
71
72 Value Min() const {
73 return min_value_added_;
74 }
75
76 Value Max() const {
77 return max_value_added_;
78 }
79
Mathieu Chartiere5426c92013-08-01 13:55:42 -070080 const std::string& Name() const {
Sameer Abu Asala8439542013-02-14 16:06:42 -080081 return name_;
82 }
83
Sameer Abu Asala8439542013-02-14 16:06:42 -080084 private:
Sameer Abu Asala8439542013-02-14 16:06:42 -080085 void Initialize();
Mathieu Chartiere5426c92013-08-01 13:55:42 -070086 size_t FindBucket(Value val) const;
87 void BucketiseValue(Value val);
Sameer Abu Asala8439542013-02-14 16:06:42 -080088 // Add more buckets to the histogram to fill in a new value that exceeded
89 // the max_read_value_.
Mathieu Chartiere5426c92013-08-01 13:55:42 -070090 void GrowBuckets(Value val);
Sameer Abu Asala8439542013-02-14 16:06:42 -080091 std::string name_;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070092 // Maximum number of buckets.
93 const size_t max_buckets_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080094 // Number of samples placed in histogram.
Mathieu Chartiere5426c92013-08-01 13:55:42 -070095 size_t sample_size_;
Sameer Abu Asala8439542013-02-14 16:06:42 -080096 // Width of the bucket range. The lower the value is the more accurate
Mathieu Chartiere5426c92013-08-01 13:55:42 -070097 // histogram percentiles are. Grows adaptively when we hit max buckets.
Sameer Abu Asala8439542013-02-14 16:06:42 -080098 Value bucket_width_;
Mathieu Chartiere5426c92013-08-01 13:55:42 -070099 // How many occurrences of values fall within a bucket at index i where i covers the range
100 // starting at min_ + i * bucket_width_ with size bucket_size_.
101 std::vector<uint32_t> frequency_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800102 // Summation of all the elements inputed by the user.
103 Value sum_;
Sameer Abu Asala8439542013-02-14 16:06:42 -0800104 // Minimum value that can fit in the histogram. Fixed to zero for now.
Mathieu Chartiere5426c92013-08-01 13:55:42 -0700105 Value min_;
106 // Maximum value that can fit in the histogram, grows adaptively.
Sameer Abu Asala8439542013-02-14 16:06:42 -0800107 Value max_;
108 // Summation of the values entered. Used to calculate variance.
109 Value sum_of_squares_;
110 // Maximum value entered in the histogram.
111 Value min_value_added_;
112 // Minimum value entered in the histogram.
113 Value max_value_added_;
114
115 DISALLOW_COPY_AND_ASSIGN(Histogram);
116};
Mathieu Chartier02e25112013-08-14 16:14:24 -0700117} // namespace art
Sameer Abu Asala8439542013-02-14 16:06:42 -0800118
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700119#endif // ART_RUNTIME_BASE_HISTOGRAM_H_