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