[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | // Histogram is an object that aggregates statistics, and can summarize them in |
| 6 | // various forms, including ASCII graphical, HTML, and numerically (as a |
| 7 | // vector of numbers corresponding to each of the aggregating buckets). |
| 8 | // See header file for details and examples. |
| 9 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 12 | #include <limits.h> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | #include <math.h> |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 14 | |
| 15 | #include <algorithm> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | #include <string> |
| 17 | |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
| 19 | #include "base/debug/alias.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 20 | #include "base/logging.h" |
asvitkine | 454600f | 2015-06-16 16:34:50 | [diff] [blame] | 21 | #include "base/metrics/histogram_macros.h" |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 22 | #include "base/metrics/metrics_hashes.h" |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 23 | #include "base/metrics/sample_vector.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 24 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 3f38385 | 2009-04-03 18:18:55 | [diff] [blame] | 25 | #include "base/pickle.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
| 27 | #include "base/strings/stringprintf.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 28 | #include "base/synchronization/lock.h" |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 29 | #include "base/values.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 31 | namespace base { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 32 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | bool ReadHistogramArguments(PickleIterator* iter, |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 36 | std::string* histogram_name, |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 37 | int* flags, |
| 38 | int* declared_min, |
| 39 | int* declared_max, |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 40 | size_t* bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 41 | uint32_t* range_checksum) { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 42 | if (!iter->ReadString(histogram_name) || |
| 43 | !iter->ReadInt(flags) || |
| 44 | !iter->ReadInt(declared_min) || |
| 45 | !iter->ReadInt(declared_max) || |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 46 | !iter->ReadSizeT(bucket_count) || |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 47 | !iter->ReadUInt32(range_checksum)) { |
| 48 | DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name; |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | // Since these fields may have come from an untrusted renderer, do additional |
| 53 | // checks above and beyond those in Histogram::Initialize() |
| 54 | if (*declared_max <= 0 || |
| 55 | *declared_min <= 0 || |
| 56 | *declared_max < *declared_min || |
| 57 | INT_MAX / sizeof(HistogramBase::Count) <= *bucket_count || |
| 58 | *bucket_count < 2) { |
| 59 | DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // We use the arguments to find or create the local version of the histogram |
| 64 | // in this process, so we need to clear the IPC flag. |
| 65 | DCHECK(*flags & HistogramBase::kIPCSerializationSourceFlag); |
| 66 | *flags &= ~HistogramBase::kIPCSerializationSourceFlag; |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool ValidateRangeChecksum(const HistogramBase& histogram, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 72 | uint32_t range_checksum) { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 73 | const Histogram& casted_histogram = |
| 74 | static_cast<const Histogram&>(histogram); |
| 75 | |
| 76 | return casted_histogram.bucket_ranges()->checksum() == range_checksum; |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 81 | typedef HistogramBase::Count Count; |
| 82 | typedef HistogramBase::Sample Sample; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 83 | |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 84 | // static |
[email protected] | 9fce5f0 | 2011-03-02 08:04:57 | [diff] [blame] | 85 | const size_t Histogram::kBucketCount_MAX = 16384u; |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 86 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 87 | HistogramBase* Histogram::FactoryGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 88 | Sample minimum, |
| 89 | Sample maximum, |
| 90 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 91 | int32_t flags) { |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 92 | bool valid_arguments = |
| 93 | InspectConstructionArguments(name, &minimum, &maximum, &bucket_count); |
| 94 | DCHECK(valid_arguments); |
[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 95 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 96 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame] | 97 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 98 | // To avoid racy destruction at shutdown, the following will be leaked. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 99 | BucketRanges* ranges = new BucketRanges(bucket_count + 1); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 100 | InitializeBucketRanges(minimum, maximum, ranges); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 101 | const BucketRanges* registered_ranges = |
| 102 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 103 | |
| 104 | Histogram* tentative_histogram = |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 105 | new Histogram(name, minimum, maximum, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 106 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 107 | tentative_histogram->SetFlags(flags); |
| 108 | histogram = |
| 109 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 112 | DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType()); |
[email protected] | 65796dc | 2014-03-13 14:08:18 | [diff] [blame] | 113 | if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) { |
| 114 | // The construction arguments do not match the existing histogram. This can |
| 115 | // come about if an extension updates in the middle of a chrome run and has |
| 116 | // changed one of them, or simply by bad code within Chrome itself. We |
| 117 | // return NULL here with the expectation that bad code in Chrome will crash |
| 118 | // on dereference, but extension/Pepper APIs will guard against NULL and not |
| 119 | // crash. |
| 120 | DLOG(ERROR) << "Histogram " << name << " has bad construction arguments"; |
| 121 | return NULL; |
| 122 | } |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 123 | return histogram; |
| 124 | } |
| 125 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 126 | HistogramBase* Histogram::FactoryTimeGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 127 | TimeDelta minimum, |
| 128 | TimeDelta maximum, |
| 129 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 130 | int32_t flags) { |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 131 | return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()), |
| 132 | static_cast<Sample>(maximum.InMilliseconds()), bucket_count, |
| 133 | flags); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 134 | } |
| 135 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 136 | HistogramBase* Histogram::FactoryGet(const char* name, |
| 137 | Sample minimum, |
| 138 | Sample maximum, |
| 139 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 140 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 141 | return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags); |
| 142 | } |
| 143 | |
| 144 | HistogramBase* Histogram::FactoryTimeGet(const char* name, |
| 145 | TimeDelta minimum, |
| 146 | TimeDelta maximum, |
| 147 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 148 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 149 | return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count, |
| 150 | flags); |
| 151 | } |
| 152 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 153 | // Calculate what range of values are held in each bucket. |
| 154 | // We have to be careful that we don't pick a ratio between starting points in |
| 155 | // consecutive buckets that is sooo small, that the integer bounds are the same |
| 156 | // (effectively making one bucket get no values). We need to avoid: |
| 157 | // ranges(i) == ranges(i + 1) |
| 158 | // To avoid that, we just do a fine-grained bucket width as far as we need to |
| 159 | // until we get a ratio that moves us along at least 2 units at a time. From |
| 160 | // that bucket onward we do use the exponential growth of buckets. |
| 161 | // |
| 162 | // static |
| 163 | void Histogram::InitializeBucketRanges(Sample minimum, |
| 164 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 165 | BucketRanges* ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 166 | double log_max = log(static_cast<double>(maximum)); |
| 167 | double log_ratio; |
| 168 | double log_next; |
| 169 | size_t bucket_index = 1; |
| 170 | Sample current = minimum; |
| 171 | ranges->set_range(bucket_index, current); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 172 | size_t bucket_count = ranges->bucket_count(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 173 | while (bucket_count > ++bucket_index) { |
| 174 | double log_current; |
| 175 | log_current = log(static_cast<double>(current)); |
| 176 | // Calculate the count'th root of the range. |
| 177 | log_ratio = (log_max - log_current) / (bucket_count - bucket_index); |
| 178 | // See where the next bucket would start. |
| 179 | log_next = log_current + log_ratio; |
| 180 | Sample next; |
| 181 | next = static_cast<int>(floor(exp(log_next) + 0.5)); |
| 182 | if (next > current) |
| 183 | current = next; |
| 184 | else |
| 185 | ++current; // Just do a narrow bucket, and keep trying. |
| 186 | ranges->set_range(bucket_index, current); |
| 187 | } |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 188 | ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 189 | ranges->ResetChecksum(); |
| 190 | } |
| 191 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 192 | // static |
| 193 | const int Histogram::kCommonRaceBasedCountMismatch = 5; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 194 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 195 | int Histogram::FindCorruption(const HistogramSamples& samples) const { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 196 | int inconsistencies = NO_INCONSISTENCIES; |
| 197 | Sample previous_range = -1; // Bottom range is always 0. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 198 | for (size_t index = 0; index < bucket_count(); ++index) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 199 | int new_range = ranges(index); |
| 200 | if (previous_range >= new_range) |
| 201 | inconsistencies |= BUCKET_ORDER_ERROR; |
| 202 | previous_range = new_range; |
| 203 | } |
| 204 | |
| 205 | if (!bucket_ranges()->HasValidChecksum()) |
| 206 | inconsistencies |= RANGE_CHECKSUM_ERROR; |
| 207 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 208 | int64_t delta64 = samples.redundant_count() - samples.TotalCount(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 209 | if (delta64 != 0) { |
| 210 | int delta = static_cast<int>(delta64); |
| 211 | if (delta != delta64) |
| 212 | delta = INT_MAX; // Flag all giant errors as INT_MAX. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 213 | if (delta > 0) { |
| 214 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta); |
| 215 | if (delta > kCommonRaceBasedCountMismatch) |
| 216 | inconsistencies |= COUNT_HIGH_ERROR; |
| 217 | } else { |
| 218 | DCHECK_GT(0, delta); |
| 219 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta); |
| 220 | if (-delta > kCommonRaceBasedCountMismatch) |
| 221 | inconsistencies |= COUNT_LOW_ERROR; |
| 222 | } |
| 223 | } |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 224 | return inconsistencies; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 227 | Sample Histogram::ranges(size_t i) const { |
| 228 | return bucket_ranges_->range(i); |
| 229 | } |
| 230 | |
| 231 | size_t Histogram::bucket_count() const { |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 232 | return bucket_ranges_->bucket_count(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 235 | // static |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 236 | bool Histogram::InspectConstructionArguments(const std::string& name, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 237 | Sample* minimum, |
| 238 | Sample* maximum, |
| 239 | size_t* bucket_count) { |
| 240 | // Defensive code for backward compatibility. |
| 241 | if (*minimum < 1) { |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 242 | DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 243 | *minimum = 1; |
| 244 | } |
| 245 | if (*maximum >= kSampleType_MAX) { |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 246 | DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum; |
| 247 | *maximum = kSampleType_MAX - 1; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 248 | } |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 249 | if (*bucket_count >= kBucketCount_MAX) { |
| 250 | DVLOG(1) << "Histogram: " << name << " has bad bucket_count: " |
| 251 | << *bucket_count; |
| 252 | *bucket_count = kBucketCount_MAX - 1; |
| 253 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 254 | |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 255 | if (*minimum >= *maximum) |
| 256 | return false; |
| 257 | if (*bucket_count < 3) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 258 | return false; |
| 259 | if (*bucket_count > static_cast<size_t>(*maximum - *minimum + 2)) |
| 260 | return false; |
| 261 | return true; |
| 262 | } |
| 263 | |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 264 | uint64_t Histogram::name_hash() const { |
| 265 | return samples_->id(); |
| 266 | } |
| 267 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 268 | HistogramType Histogram::GetHistogramType() const { |
| 269 | return HISTOGRAM; |
| 270 | } |
| 271 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 272 | bool Histogram::HasConstructionArguments(Sample expected_minimum, |
| 273 | Sample expected_maximum, |
| 274 | size_t expected_bucket_count) const { |
| 275 | return ((expected_minimum == declared_min_) && |
| 276 | (expected_maximum == declared_max_) && |
| 277 | (expected_bucket_count == bucket_count())); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void Histogram::Add(int value) { |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 281 | AddCount(value, 1); |
| 282 | } |
| 283 | |
| 284 | void Histogram::AddCount(int value, int count) { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 285 | DCHECK_EQ(0, ranges(0)); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 286 | DCHECK_EQ(kSampleType_MAX, ranges(bucket_count())); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 287 | |
| 288 | if (value > kSampleType_MAX - 1) |
| 289 | value = kSampleType_MAX - 1; |
| 290 | if (value < 0) |
| 291 | value = 0; |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 292 | if (count <= 0) { |
| 293 | NOTREACHED(); |
| 294 | return; |
| 295 | } |
| 296 | samples_->Accumulate(value, count); |
simonhatch | df5a814 | 2015-07-15 22:22:57 | [diff] [blame] | 297 | |
| 298 | FindAndRunCallback(value); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | scoped_ptr<HistogramSamples> Histogram::SnapshotSamples() const { |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 302 | return SnapshotSampleVector(); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 305 | void Histogram::AddSamples(const HistogramSamples& samples) { |
| 306 | samples_->Add(samples); |
| 307 | } |
| 308 | |
| 309 | bool Histogram::AddSamplesFromPickle(PickleIterator* iter) { |
| 310 | return samples_->AddFromPickle(iter); |
| 311 | } |
| 312 | |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 313 | // The following methods provide a graphical histogram display. |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 314 | void Histogram::WriteHTMLGraph(std::string* output) const { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 315 | // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. |
| 316 | output->append("<PRE>"); |
| 317 | WriteAsciiImpl(true, "<br>", output); |
| 318 | output->append("</PRE>"); |
| 319 | } |
| 320 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 321 | void Histogram::WriteAscii(std::string* output) const { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 322 | WriteAsciiImpl(true, "\n", output); |
| 323 | } |
| 324 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 325 | bool Histogram::SerializeInfoImpl(Pickle* pickle) const { |
| 326 | DCHECK(bucket_ranges()->HasValidChecksum()); |
| 327 | return pickle->WriteString(histogram_name()) && |
| 328 | pickle->WriteInt(flags()) && |
| 329 | pickle->WriteInt(declared_min()) && |
| 330 | pickle->WriteInt(declared_max()) && |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 331 | pickle->WriteSizeT(bucket_count()) && |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 332 | pickle->WriteUInt32(bucket_ranges()->checksum()); |
| 333 | } |
| 334 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 335 | Histogram::Histogram(const std::string& name, |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 336 | Sample minimum, |
| 337 | Sample maximum, |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 338 | const BucketRanges* ranges) |
| 339 | : HistogramBase(name), |
| 340 | bucket_ranges_(ranges), |
| 341 | declared_min_(minimum), |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 342 | declared_max_(maximum) { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 343 | if (ranges) |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 344 | samples_.reset(new SampleVector(HashMetricName(name), ranges)); |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | Histogram::~Histogram() { |
[email protected] | abae9b02 | 2012-10-24 08:18:52 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 350 | bool Histogram::PrintEmptyBucket(size_t index) const { |
| 351 | return true; |
| 352 | } |
| 353 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 354 | // Use the actual bucket widths (like a linear histogram) until the widths get |
| 355 | // over some transition value, and then use that transition width. Exponentials |
| 356 | // get so big so fast (and we don't expect to see a lot of entries in the large |
| 357 | // buckets), so we need this to make it possible to see what is going on and |
| 358 | // not have 0-graphical-height buckets. |
| 359 | double Histogram::GetBucketSize(Count current, size_t i) const { |
| 360 | DCHECK_GT(ranges(i + 1), ranges(i)); |
| 361 | static const double kTransitionWidth = 5; |
| 362 | double denominator = ranges(i + 1) - ranges(i); |
| 363 | if (denominator > kTransitionWidth) |
| 364 | denominator = kTransitionWidth; // Stop trying to normalize. |
| 365 | return current/denominator; |
| 366 | } |
| 367 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 368 | const std::string Histogram::GetAsciiBucketRange(size_t i) const { |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 369 | return GetSimpleAsciiBucketRange(ranges(i)); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 372 | //------------------------------------------------------------------------------ |
| 373 | // Private methods |
| 374 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 375 | // static |
| 376 | HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 377 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 378 | int flags; |
| 379 | int declared_min; |
| 380 | int declared_max; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 381 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 382 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 383 | |
| 384 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 385 | &declared_max, &bucket_count, &range_checksum)) { |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | // Find or create the local version of the histogram in this process. |
| 390 | HistogramBase* histogram = Histogram::FactoryGet( |
| 391 | histogram_name, declared_min, declared_max, bucket_count, flags); |
| 392 | |
| 393 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 394 | // The serialized histogram might be corrupted. |
| 395 | return NULL; |
| 396 | } |
| 397 | return histogram; |
| 398 | } |
| 399 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 400 | scoped_ptr<SampleVector> Histogram::SnapshotSampleVector() const { |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 401 | scoped_ptr<SampleVector> samples( |
| 402 | new SampleVector(samples_->id(), bucket_ranges())); |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 403 | samples->Add(*samples_); |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 404 | return samples; |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 407 | void Histogram::WriteAsciiImpl(bool graph_it, |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 408 | const std::string& newline, |
| 409 | std::string* output) const { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 410 | // Get local (stack) copies of all effectively volatile class data so that we |
| 411 | // are consistent across our output activities. |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 412 | scoped_ptr<SampleVector> snapshot = SnapshotSampleVector(); |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 413 | Count sample_count = snapshot->TotalCount(); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 414 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 415 | WriteAsciiHeader(*snapshot, sample_count, output); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 416 | output->append(newline); |
| 417 | |
| 418 | // Prepare to normalize graphical rendering of bucket contents. |
| 419 | double max_size = 0; |
| 420 | if (graph_it) |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 421 | max_size = GetPeakBucketSize(*snapshot); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 422 | |
| 423 | // Calculate space needed to print bucket range numbers. Leave room to print |
| 424 | // nearly the largest bucket range without sliding over the histogram. |
| 425 | size_t largest_non_empty_bucket = bucket_count() - 1; |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 426 | while (0 == snapshot->GetCountAtIndex(largest_non_empty_bucket)) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 427 | if (0 == largest_non_empty_bucket) |
| 428 | break; // All buckets are empty. |
| 429 | --largest_non_empty_bucket; |
| 430 | } |
| 431 | |
| 432 | // Calculate largest print width needed for any of our bucket range displays. |
| 433 | size_t print_width = 1; |
| 434 | for (size_t i = 0; i < bucket_count(); ++i) { |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 435 | if (snapshot->GetCountAtIndex(i)) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 436 | size_t width = GetAsciiBucketRange(i).size() + 1; |
| 437 | if (width > print_width) |
| 438 | print_width = width; |
| 439 | } |
| 440 | } |
| 441 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 442 | int64_t remaining = sample_count; |
| 443 | int64_t past = 0; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 444 | // Output the actual histogram graph. |
| 445 | for (size_t i = 0; i < bucket_count(); ++i) { |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 446 | Count current = snapshot->GetCountAtIndex(i); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 447 | if (!current && !PrintEmptyBucket(i)) |
| 448 | continue; |
| 449 | remaining -= current; |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 450 | std::string range = GetAsciiBucketRange(i); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 451 | output->append(range); |
| 452 | for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 453 | output->push_back(' '); |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 454 | if (0 == current && i < bucket_count() - 1 && |
| 455 | 0 == snapshot->GetCountAtIndex(i + 1)) { |
| 456 | while (i < bucket_count() - 1 && |
| 457 | 0 == snapshot->GetCountAtIndex(i + 1)) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 458 | ++i; |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 459 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 460 | output->append("... "); |
| 461 | output->append(newline); |
| 462 | continue; // No reason to plot emptiness. |
| 463 | } |
| 464 | double current_size = GetBucketSize(current, i); |
| 465 | if (graph_it) |
| 466 | WriteAsciiBucketGraph(current_size, max_size, output); |
| 467 | WriteAsciiBucketContext(past, current, remaining, i, output); |
| 468 | output->append(newline); |
| 469 | past += current; |
| 470 | } |
| 471 | DCHECK_EQ(sample_count, past); |
| 472 | } |
| 473 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 474 | double Histogram::GetPeakBucketSize(const SampleVector& samples) const { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 475 | double max = 0; |
| 476 | for (size_t i = 0; i < bucket_count() ; ++i) { |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 477 | double current_size = GetBucketSize(samples.GetCountAtIndex(i), i); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 478 | if (current_size > max) |
| 479 | max = current_size; |
| 480 | } |
| 481 | return max; |
| 482 | } |
| 483 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 484 | void Histogram::WriteAsciiHeader(const SampleVector& samples, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 485 | Count sample_count, |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 486 | std::string* output) const { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 487 | StringAppendF(output, |
| 488 | "Histogram: %s recorded %d samples", |
| 489 | histogram_name().c_str(), |
| 490 | sample_count); |
| 491 | if (0 == sample_count) { |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 492 | DCHECK_EQ(samples.sum(), 0); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 493 | } else { |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 494 | double average = static_cast<float>(samples.sum()) / sample_count; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 495 | |
| 496 | StringAppendF(output, ", average = %.1f", average); |
| 497 | } |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 498 | if (flags() & ~kHexRangePrintingFlag) |
| 499 | StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 500 | } |
| 501 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 502 | void Histogram::WriteAsciiBucketContext(const int64_t past, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 503 | const Count current, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 504 | const int64_t remaining, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 505 | const size_t i, |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 506 | std::string* output) const { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 507 | double scaled_sum = (past + current + remaining) / 100.0; |
| 508 | WriteAsciiBucketValue(current, scaled_sum, output); |
| 509 | if (0 < i) { |
| 510 | double percentage = past / scaled_sum; |
| 511 | StringAppendF(output, " {%3.1f%%}", percentage); |
| 512 | } |
| 513 | } |
| 514 | |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 515 | void Histogram::GetParameters(DictionaryValue* params) const { |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 516 | params->SetString("type", HistogramTypeToString(GetHistogramType())); |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 517 | params->SetInteger("min", declared_min()); |
| 518 | params->SetInteger("max", declared_max()); |
| 519 | params->SetInteger("bucket_count", static_cast<int>(bucket_count())); |
| 520 | } |
| 521 | |
[email protected] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 522 | void Histogram::GetCountAndBucketData(Count* count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 523 | int64_t* sum, |
[email protected] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 524 | ListValue* buckets) const { |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 525 | scoped_ptr<SampleVector> snapshot = SnapshotSampleVector(); |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 526 | *count = snapshot->TotalCount(); |
[email protected] | cdd98fc | 2013-05-10 09:32:58 | [diff] [blame] | 527 | *sum = snapshot->sum(); |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 528 | size_t index = 0; |
| 529 | for (size_t i = 0; i < bucket_count(); ++i) { |
scottmg | dcc933d | 2015-01-27 21:37:55 | [diff] [blame] | 530 | Sample count_at_index = snapshot->GetCountAtIndex(i); |
| 531 | if (count_at_index > 0) { |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 532 | scoped_ptr<DictionaryValue> bucket_value(new DictionaryValue()); |
| 533 | bucket_value->SetInteger("low", ranges(i)); |
| 534 | if (i != bucket_count() - 1) |
| 535 | bucket_value->SetInteger("high", ranges(i + 1)); |
scottmg | dcc933d | 2015-01-27 21:37:55 | [diff] [blame] | 536 | bucket_value->SetInteger("count", count_at_index); |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 537 | buckets->Set(index, bucket_value.release()); |
| 538 | ++index; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 543 | //------------------------------------------------------------------------------ |
| 544 | // LinearHistogram: This histogram uses a traditional set of evenly spaced |
| 545 | // buckets. |
| 546 | //------------------------------------------------------------------------------ |
| 547 | |
| 548 | LinearHistogram::~LinearHistogram() {} |
| 549 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 550 | HistogramBase* LinearHistogram::FactoryGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 551 | Sample minimum, |
| 552 | Sample maximum, |
| 553 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 554 | int32_t flags) { |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 555 | return FactoryGetWithRangeDescription( |
| 556 | name, minimum, maximum, bucket_count, flags, NULL); |
| 557 | } |
| 558 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 559 | HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name, |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 560 | TimeDelta minimum, |
| 561 | TimeDelta maximum, |
| 562 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 563 | int32_t flags) { |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 564 | return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()), |
| 565 | static_cast<Sample>(maximum.InMilliseconds()), bucket_count, |
| 566 | flags); |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 567 | } |
| 568 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 569 | HistogramBase* LinearHistogram::FactoryGet(const char* name, |
| 570 | Sample minimum, |
| 571 | Sample maximum, |
| 572 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 573 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 574 | return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags); |
| 575 | } |
| 576 | |
| 577 | HistogramBase* LinearHistogram::FactoryTimeGet(const char* name, |
| 578 | TimeDelta minimum, |
| 579 | TimeDelta maximum, |
| 580 | size_t bucket_count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 581 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 582 | return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count, |
| 583 | flags); |
| 584 | } |
| 585 | |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 586 | HistogramBase* LinearHistogram::FactoryGetWithRangeDescription( |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 587 | const std::string& name, |
| 588 | Sample minimum, |
| 589 | Sample maximum, |
| 590 | size_t bucket_count, |
| 591 | int32_t flags, |
| 592 | const DescriptionPair descriptions[]) { |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 593 | bool valid_arguments = Histogram::InspectConstructionArguments( |
| 594 | name, &minimum, &maximum, &bucket_count); |
| 595 | DCHECK(valid_arguments); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 596 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 597 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 598 | if (!histogram) { |
| 599 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 600 | BucketRanges* ranges = new BucketRanges(bucket_count + 1); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 601 | InitializeBucketRanges(minimum, maximum, ranges); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 602 | const BucketRanges* registered_ranges = |
| 603 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 604 | |
| 605 | LinearHistogram* tentative_histogram = |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 606 | new LinearHistogram(name, minimum, maximum, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 607 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 608 | // Set range descriptions. |
| 609 | if (descriptions) { |
| 610 | for (int i = 0; descriptions[i].description; ++i) { |
| 611 | tentative_histogram->bucket_description_[descriptions[i].sample] = |
| 612 | descriptions[i].description; |
| 613 | } |
| 614 | } |
| 615 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 616 | tentative_histogram->SetFlags(flags); |
| 617 | histogram = |
| 618 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
| 619 | } |
| 620 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 621 | DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType()); |
[email protected] | 65796dc | 2014-03-13 14:08:18 | [diff] [blame] | 622 | if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) { |
| 623 | // The construction arguments do not match the existing histogram. This can |
| 624 | // come about if an extension updates in the middle of a chrome run and has |
| 625 | // changed one of them, or simply by bad code within Chrome itself. We |
| 626 | // return NULL here with the expectation that bad code in Chrome will crash |
| 627 | // on dereference, but extension/Pepper APIs will guard against NULL and not |
| 628 | // crash. |
| 629 | DLOG(ERROR) << "Histogram " << name << " has bad construction arguments"; |
| 630 | return NULL; |
| 631 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 632 | return histogram; |
| 633 | } |
| 634 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 635 | HistogramType LinearHistogram::GetHistogramType() const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 636 | return LINEAR_HISTOGRAM; |
| 637 | } |
| 638 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 639 | LinearHistogram::LinearHistogram(const std::string& name, |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 640 | Sample minimum, |
| 641 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 642 | const BucketRanges* ranges) |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 643 | : Histogram(name, minimum, maximum, ranges) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 644 | } |
| 645 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 646 | double LinearHistogram::GetBucketSize(Count current, size_t i) const { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 647 | DCHECK_GT(ranges(i + 1), ranges(i)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 648 | // Adjacent buckets with different widths would have "surprisingly" many (few) |
| 649 | // samples in a histogram if we didn't normalize this way. |
| 650 | double denominator = ranges(i + 1) - ranges(i); |
| 651 | return current/denominator; |
| 652 | } |
| 653 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 654 | const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 655 | int range = ranges(i); |
| 656 | BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
| 657 | if (it == bucket_description_.end()) |
| 658 | return Histogram::GetAsciiBucketRange(i); |
| 659 | return it->second; |
| 660 | } |
| 661 | |
| 662 | bool LinearHistogram::PrintEmptyBucket(size_t index) const { |
| 663 | return bucket_description_.find(ranges(index)) == bucket_description_.end(); |
| 664 | } |
| 665 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 666 | // static |
| 667 | void LinearHistogram::InitializeBucketRanges(Sample minimum, |
| 668 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 669 | BucketRanges* ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 670 | double min = minimum; |
| 671 | double max = maximum; |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 672 | size_t bucket_count = ranges->bucket_count(); |
| 673 | for (size_t i = 1; i < bucket_count; ++i) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 674 | double linear_range = |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 675 | (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 676 | ranges->set_range(i, static_cast<Sample>(linear_range + 0.5)); |
| 677 | } |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 678 | ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 679 | ranges->ResetChecksum(); |
| 680 | } |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 681 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 682 | // static |
| 683 | HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 684 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 685 | int flags; |
| 686 | int declared_min; |
| 687 | int declared_max; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 688 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 689 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 690 | |
| 691 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 692 | &declared_max, &bucket_count, &range_checksum)) { |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | HistogramBase* histogram = LinearHistogram::FactoryGet( |
| 697 | histogram_name, declared_min, declared_max, bucket_count, flags); |
| 698 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 699 | // The serialized histogram might be corrupted. |
| 700 | return NULL; |
| 701 | } |
| 702 | return histogram; |
| 703 | } |
| 704 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 705 | //------------------------------------------------------------------------------ |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 706 | // This section provides implementation for BooleanHistogram. |
| 707 | //------------------------------------------------------------------------------ |
| 708 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 709 | HistogramBase* BooleanHistogram::FactoryGet(const std::string& name, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 710 | int32_t flags) { |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 711 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame] | 712 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 713 | // To avoid racy destruction at shutdown, the following will be leaked. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 714 | BucketRanges* ranges = new BucketRanges(4); |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 715 | LinearHistogram::InitializeBucketRanges(1, 2, ranges); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 716 | const BucketRanges* registered_ranges = |
| 717 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 718 | |
| 719 | BooleanHistogram* tentative_histogram = |
| 720 | new BooleanHistogram(name, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 721 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 722 | tentative_histogram->SetFlags(flags); |
| 723 | histogram = |
| 724 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 725 | } |
| 726 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 727 | DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->GetHistogramType()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 728 | return histogram; |
| 729 | } |
| 730 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 731 | HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 732 | return FactoryGet(std::string(name), flags); |
| 733 | } |
| 734 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 735 | HistogramType BooleanHistogram::GetHistogramType() const { |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 736 | return BOOLEAN_HISTOGRAM; |
| 737 | } |
| 738 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 739 | BooleanHistogram::BooleanHistogram(const std::string& name, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 740 | const BucketRanges* ranges) |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 741 | : LinearHistogram(name, 1, 2, ranges) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 742 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 743 | HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 744 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 745 | int flags; |
| 746 | int declared_min; |
| 747 | int declared_max; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 748 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 749 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 750 | |
| 751 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 752 | &declared_max, &bucket_count, &range_checksum)) { |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | HistogramBase* histogram = BooleanHistogram::FactoryGet( |
| 757 | histogram_name, flags); |
| 758 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 759 | // The serialized histogram might be corrupted. |
| 760 | return NULL; |
| 761 | } |
| 762 | return histogram; |
| 763 | } |
| 764 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 765 | //------------------------------------------------------------------------------ |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 766 | // CustomHistogram: |
| 767 | //------------------------------------------------------------------------------ |
| 768 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 769 | HistogramBase* CustomHistogram::FactoryGet( |
| 770 | const std::string& name, |
| 771 | const std::vector<Sample>& custom_ranges, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 772 | int32_t flags) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 773 | CHECK(ValidateCustomRanges(custom_ranges)); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 774 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 775 | HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame] | 776 | if (!histogram) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 777 | BucketRanges* ranges = CreateBucketRangesFromCustomRanges(custom_ranges); |
| 778 | const BucketRanges* registered_ranges = |
| 779 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 780 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 781 | // To avoid racy destruction at shutdown, the following will be leaked. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 782 | CustomHistogram* tentative_histogram = |
| 783 | new CustomHistogram(name, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 784 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 785 | tentative_histogram->SetFlags(flags); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 786 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 787 | histogram = |
| 788 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 789 | } |
| 790 | |
[email protected] | cc7dec21 | 2013-03-01 03:53:25 | [diff] [blame] | 791 | DCHECK_EQ(histogram->GetHistogramType(), CUSTOM_HISTOGRAM); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 792 | return histogram; |
| 793 | } |
| 794 | |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 795 | HistogramBase* CustomHistogram::FactoryGet( |
| 796 | const char* name, |
| 797 | const std::vector<Sample>& custom_ranges, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 798 | int32_t flags) { |
asvitkine | 5c2d502 | 2015-06-19 00:37:50 | [diff] [blame] | 799 | return FactoryGet(std::string(name), custom_ranges, flags); |
| 800 | } |
| 801 | |
[email protected] | 07c0240 | 2012-10-31 06:20:25 | [diff] [blame] | 802 | HistogramType CustomHistogram::GetHistogramType() const { |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 803 | return CUSTOM_HISTOGRAM; |
| 804 | } |
| 805 | |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 806 | // static |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 807 | std::vector<Sample> CustomHistogram::ArrayToCustomRanges( |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 808 | const Sample* values, size_t num_values) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 809 | std::vector<Sample> all_values; |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 810 | for (size_t i = 0; i < num_values; ++i) { |
| 811 | Sample value = values[i]; |
| 812 | all_values.push_back(value); |
| 813 | |
| 814 | // Ensure that a guard bucket is added. If we end up with duplicate |
| 815 | // values, FactoryGet will take care of removing them. |
| 816 | all_values.push_back(value + 1); |
| 817 | } |
| 818 | return all_values; |
| 819 | } |
| 820 | |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 821 | CustomHistogram::CustomHistogram(const std::string& name, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 822 | const BucketRanges* ranges) |
| 823 | : Histogram(name, |
| 824 | ranges->range(1), |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 825 | ranges->range(ranges->bucket_count() - 1), |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 826 | ranges) {} |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 827 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 828 | bool CustomHistogram::SerializeInfoImpl(Pickle* pickle) const { |
| 829 | if (!Histogram::SerializeInfoImpl(pickle)) |
| 830 | return false; |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 831 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 832 | // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't |
| 833 | // write them. |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 834 | for (size_t i = 1; i < bucket_ranges()->bucket_count(); ++i) { |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 835 | if (!pickle->WriteInt(bucket_ranges()->range(i))) |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 836 | return false; |
| 837 | } |
| 838 | return true; |
| 839 | } |
| 840 | |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 841 | double CustomHistogram::GetBucketSize(Count current, size_t i) const { |
| 842 | return 1; |
| 843 | } |
| 844 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 845 | // static |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 846 | HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) { |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 847 | std::string histogram_name; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 848 | int flags; |
| 849 | int declared_min; |
| 850 | int declared_max; |
pkasting | 89a19f14 | 2014-10-02 03:01:04 | [diff] [blame] | 851 | size_t bucket_count; |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 852 | uint32_t range_checksum; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 853 | |
| 854 | if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min, |
| 855 | &declared_max, &bucket_count, &range_checksum)) { |
| 856 | return NULL; |
| 857 | } |
| 858 | |
| 859 | // First and last ranges are not serialized. |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 860 | std::vector<Sample> sample_ranges(bucket_count - 1); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 861 | |
| 862 | for (size_t i = 0; i < sample_ranges.size(); ++i) { |
| 863 | if (!iter->ReadInt(&sample_ranges[i])) |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | HistogramBase* histogram = CustomHistogram::FactoryGet( |
| 868 | histogram_name, sample_ranges, flags); |
| 869 | if (!ValidateRangeChecksum(*histogram, range_checksum)) { |
| 870 | // The serialized histogram might be corrupted. |
| 871 | return NULL; |
| 872 | } |
| 873 | return histogram; |
| 874 | } |
| 875 | |
| 876 | // static |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 877 | bool CustomHistogram::ValidateCustomRanges( |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 878 | const std::vector<Sample>& custom_ranges) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 879 | bool has_valid_range = false; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 880 | for (size_t i = 0; i < custom_ranges.size(); i++) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 881 | Sample sample = custom_ranges[i]; |
| 882 | if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 883 | return false; |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 884 | if (sample != 0) |
| 885 | has_valid_range = true; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 886 | } |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 887 | return has_valid_range; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // static |
| 891 | BucketRanges* CustomHistogram::CreateBucketRangesFromCustomRanges( |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 892 | const std::vector<Sample>& custom_ranges) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 893 | // Remove the duplicates in the custom ranges array. |
asvitkine | 24d3e9a | 2015-05-27 05:22:14 | [diff] [blame] | 894 | std::vector<int> ranges = custom_ranges; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 895 | ranges.push_back(0); // Ensure we have a zero value. |
| 896 | ranges.push_back(HistogramBase::kSampleType_MAX); |
| 897 | std::sort(ranges.begin(), ranges.end()); |
| 898 | ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end()); |
| 899 | |
| 900 | BucketRanges* bucket_ranges = new BucketRanges(ranges.size()); |
| 901 | for (size_t i = 0; i < ranges.size(); i++) { |
| 902 | bucket_ranges->set_range(i, ranges[i]); |
| 903 | } |
| 904 | bucket_ranges->ResetChecksum(); |
| 905 | return bucket_ranges; |
| 906 | } |
| 907 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 908 | } // namespace base |