[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" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 20 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 3f38385 | 2009-04-03 18:18:55 | [diff] [blame] | 21 | #include "base/pickle.h" |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 22 | #include "base/string_util.h" |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 24 | #include "base/synchronization/lock.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 26 | using std::string; |
| 27 | using std::vector; |
| 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] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 31 | typedef HistogramBase::Count Count; |
| 32 | typedef HistogramBase::Sample Sample; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 33 | |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 34 | // static |
[email protected] | 9fce5f0 | 2011-03-02 08:04:57 | [diff] [blame] | 35 | const size_t Histogram::kBucketCount_MAX = 16384u; |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 36 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 37 | Histogram::SampleSet::SampleSet(size_t size) |
| 38 | : counts_(size, 0), |
| 39 | sum_(0), |
| 40 | redundant_count_(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | |
| 42 | Histogram::SampleSet::SampleSet() |
| 43 | : counts_(), |
| 44 | sum_(0), |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 45 | redundant_count_(0) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 46 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 47 | Histogram::SampleSet::~SampleSet() {} |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 48 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 49 | void Histogram::SampleSet::Resize(size_t size) { |
| 50 | counts_.resize(size, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 51 | } |
| 52 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | void Histogram::SampleSet::Accumulate(Sample value, Count count, |
| 54 | size_t index) { |
| 55 | DCHECK(count == 1 || count == -1); |
| 56 | counts_[index] += count; |
| 57 | sum_ += count * value; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 58 | redundant_count_ += count; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 59 | DCHECK_GE(counts_[index], 0); |
| 60 | DCHECK_GE(sum_, 0); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 61 | DCHECK_GE(redundant_count_, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Count Histogram::SampleSet::TotalCount() const { |
| 65 | Count total = 0; |
| 66 | for (Counts::const_iterator it = counts_.begin(); |
| 67 | it != counts_.end(); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 68 | ++it) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 69 | total += *it; |
| 70 | } |
| 71 | return total; |
| 72 | } |
| 73 | |
| 74 | void Histogram::SampleSet::Add(const SampleSet& other) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 75 | DCHECK_EQ(counts_.size(), other.counts_.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | sum_ += other.sum_; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 77 | redundant_count_ += other.redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 78 | for (size_t index = 0; index < counts_.size(); ++index) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 79 | counts_[index] += other.counts_[index]; |
| 80 | } |
| 81 | |
| 82 | void Histogram::SampleSet::Subtract(const SampleSet& other) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 83 | DCHECK_EQ(counts_.size(), other.counts_.size()); |
[email protected] | 0704a97 | 2011-03-24 00:30:27 | [diff] [blame] | 84 | // Note: Race conditions in snapshotting a sum may lead to (temporary) |
| 85 | // negative values when snapshots are later combined (and deltas calculated). |
| 86 | // As a result, we don't currently CHCEK() for positive values. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | sum_ -= other.sum_; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 88 | redundant_count_ -= other.redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 89 | for (size_t index = 0; index < counts_.size(); ++index) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 90 | counts_[index] -= other.counts_[index]; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 91 | DCHECK_GE(counts_[index], 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 95 | bool Histogram::SampleSet::Serialize(Pickle* pickle) const { |
| 96 | pickle->WriteInt64(sum_); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 97 | pickle->WriteInt64(redundant_count_); |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 98 | pickle->WriteUInt64(counts_.size()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 99 | |
| 100 | for (size_t index = 0; index < counts_.size(); ++index) { |
| 101 | pickle->WriteInt(counts_[index]); |
| 102 | } |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 107 | bool Histogram::SampleSet::Deserialize(PickleIterator* iter) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 108 | DCHECK_EQ(counts_.size(), 0u); |
| 109 | DCHECK_EQ(sum_, 0); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 110 | DCHECK_EQ(redundant_count_, 0); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 111 | |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 112 | uint64 counts_size; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 113 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 114 | if (!iter->ReadInt64(&sum_) || |
| 115 | !iter->ReadInt64(&redundant_count_) || |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 116 | !iter->ReadUInt64(&counts_size)) { |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 120 | if (counts_size == 0) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 121 | return false; |
| 122 | |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 123 | int count = 0; |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 124 | for (uint64 index = 0; index < counts_size; ++index) { |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 125 | int i; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 126 | if (!iter->ReadInt(&i)) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 127 | return false; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 128 | counts_.push_back(i); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 129 | count += i; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 130 | } |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 131 | DCHECK_EQ(count, redundant_count_); |
| 132 | return count == redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 135 | // TODO(rtenneti): delete this code after debugging. |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 136 | void CheckCorruption(const Histogram& histogram, bool new_histogram) { |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 137 | const std::string& histogram_name = histogram.histogram_name(); |
| 138 | char histogram_name_buf[128]; |
| 139 | base::strlcpy(histogram_name_buf, |
| 140 | histogram_name.c_str(), |
| 141 | arraysize(histogram_name_buf)); |
| 142 | base::debug::Alias(histogram_name_buf); |
| 143 | |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 144 | bool debug_new_histogram[1]; |
| 145 | debug_new_histogram[0] = new_histogram; |
| 146 | base::debug::Alias(debug_new_histogram); |
| 147 | |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 148 | Sample previous_range = -1; // Bottom range is always 0. |
| 149 | for (size_t index = 0; index < histogram.bucket_count(); ++index) { |
| 150 | int new_range = histogram.ranges(index); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 151 | CHECK_LT(previous_range, new_range); |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 152 | previous_range = new_range; |
| 153 | } |
| 154 | |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 155 | CHECK(histogram.bucket_ranges()->HasValidChecksum()); |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 158 | Histogram* Histogram::FactoryGet(const string& name, |
| 159 | Sample minimum, |
| 160 | Sample maximum, |
| 161 | size_t bucket_count, |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 162 | int32 flags) { |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 163 | bool valid_arguments = |
| 164 | InspectConstructionArguments(name, &minimum, &maximum, &bucket_count); |
| 165 | DCHECK(valid_arguments); |
[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 166 | |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame] | 167 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 168 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 169 | // To avoid racy destruction at shutdown, the following will be leaked. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 170 | BucketRanges* ranges = new BucketRanges(bucket_count + 1); |
| 171 | InitializeBucketRanges(minimum, maximum, bucket_count, ranges); |
| 172 | const BucketRanges* registered_ranges = |
| 173 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 174 | |
| 175 | Histogram* tentative_histogram = |
| 176 | new Histogram(name, minimum, maximum, bucket_count, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 177 | CheckCorruption(*tentative_histogram, true); |
| 178 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 179 | tentative_histogram->SetFlags(flags); |
| 180 | histogram = |
| 181 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 182 | } |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 183 | // TODO(rtenneti): delete this code after debugging. |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 184 | CheckCorruption(*histogram, false); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 185 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 186 | CHECK_EQ(HISTOGRAM, histogram->histogram_type()); |
[email protected] | 065d170 | 2012-08-23 23:55:13 | [diff] [blame^] | 187 | CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 188 | return histogram; |
| 189 | } |
| 190 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 191 | Histogram* Histogram::FactoryTimeGet(const string& name, |
| 192 | TimeDelta minimum, |
| 193 | TimeDelta maximum, |
| 194 | size_t bucket_count, |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 195 | int32 flags) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 196 | return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
| 197 | bucket_count, flags); |
| 198 | } |
| 199 | |
| 200 | TimeTicks Histogram::DebugNow() { |
| 201 | #ifndef NDEBUG |
| 202 | return TimeTicks::Now(); |
| 203 | #else |
| 204 | return TimeTicks(); |
| 205 | #endif |
| 206 | } |
| 207 | |
| 208 | // Calculate what range of values are held in each bucket. |
| 209 | // We have to be careful that we don't pick a ratio between starting points in |
| 210 | // consecutive buckets that is sooo small, that the integer bounds are the same |
| 211 | // (effectively making one bucket get no values). We need to avoid: |
| 212 | // ranges(i) == ranges(i + 1) |
| 213 | // To avoid that, we just do a fine-grained bucket width as far as we need to |
| 214 | // until we get a ratio that moves us along at least 2 units at a time. From |
| 215 | // that bucket onward we do use the exponential growth of buckets. |
| 216 | // |
| 217 | // static |
| 218 | void Histogram::InitializeBucketRanges(Sample minimum, |
| 219 | Sample maximum, |
| 220 | size_t bucket_count, |
| 221 | BucketRanges* ranges) { |
| 222 | DCHECK_EQ(ranges->size(), bucket_count + 1); |
| 223 | double log_max = log(static_cast<double>(maximum)); |
| 224 | double log_ratio; |
| 225 | double log_next; |
| 226 | size_t bucket_index = 1; |
| 227 | Sample current = minimum; |
| 228 | ranges->set_range(bucket_index, current); |
| 229 | while (bucket_count > ++bucket_index) { |
| 230 | double log_current; |
| 231 | log_current = log(static_cast<double>(current)); |
| 232 | // Calculate the count'th root of the range. |
| 233 | log_ratio = (log_max - log_current) / (bucket_count - bucket_index); |
| 234 | // See where the next bucket would start. |
| 235 | log_next = log_current + log_ratio; |
| 236 | Sample next; |
| 237 | next = static_cast<int>(floor(exp(log_next) + 0.5)); |
| 238 | if (next > current) |
| 239 | current = next; |
| 240 | else |
| 241 | ++current; // Just do a narrow bucket, and keep trying. |
| 242 | ranges->set_range(bucket_index, current); |
| 243 | } |
| 244 | ranges->set_range(ranges->size() - 1, HistogramBase::kSampleType_MAX); |
| 245 | ranges->ResetChecksum(); |
| 246 | } |
| 247 | |
| 248 | // static |
| 249 | void Histogram::Add(int value) { |
| 250 | if (value > kSampleType_MAX - 1) |
| 251 | value = kSampleType_MAX - 1; |
| 252 | if (value < 0) |
| 253 | value = 0; |
| 254 | size_t index = BucketIndex(value); |
| 255 | DCHECK_GE(value, ranges(index)); |
| 256 | DCHECK_LT(value, ranges(index + 1)); |
| 257 | Accumulate(value, 1, index); |
| 258 | } |
| 259 | |
| 260 | void Histogram::AddBoolean(bool value) { |
| 261 | DCHECK(false); |
| 262 | } |
| 263 | |
| 264 | void Histogram::AddSampleSet(const SampleSet& sample) { |
| 265 | sample_.Add(sample); |
| 266 | } |
| 267 | |
| 268 | void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) { |
| 269 | DCHECK(false); |
| 270 | } |
| 271 | |
| 272 | // The following methods provide a graphical histogram display. |
| 273 | void Histogram::WriteHTMLGraph(string* output) const { |
| 274 | // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. |
| 275 | output->append("<PRE>"); |
| 276 | WriteAsciiImpl(true, "<br>", output); |
| 277 | output->append("</PRE>"); |
| 278 | } |
| 279 | |
| 280 | void Histogram::WriteAscii(string* output) const { |
| 281 | WriteAsciiImpl(true, "\n", output); |
| 282 | } |
| 283 | |
| 284 | // static |
| 285 | string Histogram::SerializeHistogramInfo(const Histogram& histogram, |
| 286 | const SampleSet& snapshot) { |
| 287 | DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type()); |
| 288 | DCHECK(histogram.bucket_ranges()->HasValidChecksum()); |
| 289 | |
| 290 | Pickle pickle; |
| 291 | pickle.WriteString(histogram.histogram_name()); |
| 292 | pickle.WriteInt(histogram.declared_min()); |
| 293 | pickle.WriteInt(histogram.declared_max()); |
| 294 | pickle.WriteUInt64(histogram.bucket_count()); |
| 295 | pickle.WriteUInt32(histogram.bucket_ranges()->checksum()); |
| 296 | pickle.WriteInt(histogram.histogram_type()); |
| 297 | pickle.WriteInt(histogram.flags()); |
| 298 | |
| 299 | snapshot.Serialize(&pickle); |
| 300 | |
| 301 | histogram.SerializeRanges(&pickle); |
| 302 | |
| 303 | return string(static_cast<const char*>(pickle.data()), pickle.size()); |
| 304 | } |
| 305 | |
| 306 | // static |
| 307 | bool Histogram::DeserializeHistogramInfo(const string& histogram_info) { |
| 308 | if (histogram_info.empty()) { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | Pickle pickle(histogram_info.data(), |
| 313 | static_cast<int>(histogram_info.size())); |
| 314 | string histogram_name; |
| 315 | int declared_min; |
| 316 | int declared_max; |
| 317 | uint64 bucket_count; |
| 318 | uint32 range_checksum; |
| 319 | int histogram_type; |
| 320 | int pickle_flags; |
| 321 | SampleSet sample; |
| 322 | |
| 323 | PickleIterator iter(pickle); |
| 324 | if (!iter.ReadString(&histogram_name) || |
| 325 | !iter.ReadInt(&declared_min) || |
| 326 | !iter.ReadInt(&declared_max) || |
| 327 | !iter.ReadUInt64(&bucket_count) || |
| 328 | !iter.ReadUInt32(&range_checksum) || |
| 329 | !iter.ReadInt(&histogram_type) || |
| 330 | !iter.ReadInt(&pickle_flags) || |
| 331 | !sample.Histogram::SampleSet::Deserialize(&iter)) { |
| 332 | DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | DCHECK(pickle_flags & kIPCSerializationSourceFlag); |
| 337 | // Since these fields may have come from an untrusted renderer, do additional |
| 338 | // checks above and beyond those in Histogram::Initialize() |
| 339 | if (declared_max <= 0 || declared_min <= 0 || declared_max < declared_min || |
| 340 | INT_MAX / sizeof(Count) <= bucket_count || bucket_count < 2) { |
| 341 | DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name; |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | Flags flags = static_cast<Flags>(pickle_flags & ~kIPCSerializationSourceFlag); |
| 346 | |
| 347 | DCHECK_NE(NOT_VALID_IN_RENDERER, histogram_type); |
| 348 | |
| 349 | Histogram* render_histogram(NULL); |
| 350 | |
| 351 | if (histogram_type == HISTOGRAM) { |
| 352 | render_histogram = Histogram::FactoryGet( |
| 353 | histogram_name, declared_min, declared_max, bucket_count, flags); |
| 354 | } else if (histogram_type == LINEAR_HISTOGRAM) { |
| 355 | render_histogram = LinearHistogram::FactoryGet( |
| 356 | histogram_name, declared_min, declared_max, bucket_count, flags); |
| 357 | } else if (histogram_type == BOOLEAN_HISTOGRAM) { |
| 358 | render_histogram = BooleanHistogram::FactoryGet(histogram_name, flags); |
| 359 | } else if (histogram_type == CUSTOM_HISTOGRAM) { |
| 360 | vector<Sample> sample_ranges(bucket_count); |
| 361 | if (!CustomHistogram::DeserializeRanges(&iter, &sample_ranges)) { |
| 362 | DLOG(ERROR) << "Pickle error decoding ranges: " << histogram_name; |
| 363 | return false; |
| 364 | } |
| 365 | render_histogram = |
| 366 | CustomHistogram::FactoryGet(histogram_name, sample_ranges, flags); |
| 367 | } else { |
| 368 | DLOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: " |
| 369 | << histogram_type; |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | DCHECK_EQ(render_histogram->declared_min(), declared_min); |
| 374 | DCHECK_EQ(render_histogram->declared_max(), declared_max); |
| 375 | DCHECK_EQ(render_histogram->bucket_count(), bucket_count); |
| 376 | DCHECK_EQ(render_histogram->histogram_type(), histogram_type); |
| 377 | |
| 378 | if (render_histogram->bucket_ranges()->checksum() != range_checksum) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if (render_histogram->flags() & kIPCSerializationSourceFlag) { |
| 383 | DVLOG(1) << "Single process mode, histogram observed and not copied: " |
| 384 | << histogram_name; |
| 385 | } else { |
| 386 | DCHECK_EQ(flags & render_histogram->flags(), flags); |
| 387 | render_histogram->AddSampleSet(sample); |
| 388 | } |
| 389 | |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | // Validate a sample and related histogram. |
| 395 | Histogram::Inconsistencies Histogram::FindCorruption( |
| 396 | const SampleSet& snapshot) const { |
| 397 | int inconsistencies = NO_INCONSISTENCIES; |
| 398 | Sample previous_range = -1; // Bottom range is always 0. |
| 399 | int64 count = 0; |
| 400 | for (size_t index = 0; index < bucket_count(); ++index) { |
| 401 | count += snapshot.counts(index); |
| 402 | int new_range = ranges(index); |
| 403 | if (previous_range >= new_range) |
| 404 | inconsistencies |= BUCKET_ORDER_ERROR; |
| 405 | previous_range = new_range; |
| 406 | } |
| 407 | |
| 408 | if (!bucket_ranges()->HasValidChecksum()) |
| 409 | inconsistencies |= RANGE_CHECKSUM_ERROR; |
| 410 | |
| 411 | int64 delta64 = snapshot.redundant_count() - count; |
| 412 | if (delta64 != 0) { |
| 413 | int delta = static_cast<int>(delta64); |
| 414 | if (delta != delta64) |
| 415 | delta = INT_MAX; // Flag all giant errors as INT_MAX. |
| 416 | // Since snapshots of histograms are taken asynchronously relative to |
| 417 | // sampling (and snapped from different threads), it is pretty likely that |
| 418 | // we'll catch a redundant count that doesn't match the sample count. We |
| 419 | // allow for a certain amount of slop before flagging this as an |
| 420 | // inconsistency. Even with an inconsistency, we'll snapshot it again (for |
| 421 | // UMA in about a half hour, so we'll eventually get the data, if it was |
| 422 | // not the result of a corruption. If histograms show that 1 is "too tight" |
| 423 | // then we may try to use 2 or 3 for this slop value. |
| 424 | const int kCommonRaceBasedCountMismatch = 5; |
| 425 | if (delta > 0) { |
| 426 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta); |
| 427 | if (delta > kCommonRaceBasedCountMismatch) |
| 428 | inconsistencies |= COUNT_HIGH_ERROR; |
| 429 | } else { |
| 430 | DCHECK_GT(0, delta); |
| 431 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta); |
| 432 | if (-delta > kCommonRaceBasedCountMismatch) |
| 433 | inconsistencies |= COUNT_LOW_ERROR; |
| 434 | } |
| 435 | } |
| 436 | return static_cast<Inconsistencies>(inconsistencies); |
| 437 | } |
| 438 | |
| 439 | Histogram::ClassType Histogram::histogram_type() const { |
| 440 | return HISTOGRAM; |
| 441 | } |
| 442 | |
| 443 | Sample Histogram::ranges(size_t i) const { |
| 444 | return bucket_ranges_->range(i); |
| 445 | } |
| 446 | |
| 447 | size_t Histogram::bucket_count() const { |
| 448 | return bucket_count_; |
| 449 | } |
| 450 | |
| 451 | // Do a safe atomic snapshot of sample data. |
| 452 | // This implementation assumes we are on a safe single thread. |
| 453 | void Histogram::SnapshotSample(SampleSet* sample) const { |
| 454 | // Note locking not done in this version!!! |
| 455 | *sample = sample_; |
| 456 | } |
| 457 | |
| 458 | bool Histogram::HasConstructionArguments(Sample minimum, |
| 459 | Sample maximum, |
| 460 | size_t bucket_count) { |
| 461 | return ((minimum == declared_min_) && (maximum == declared_max_) && |
| 462 | (bucket_count == bucket_count_)); |
| 463 | } |
| 464 | |
| 465 | Histogram::Histogram(const string& name, |
| 466 | Sample minimum, |
| 467 | Sample maximum, |
| 468 | size_t bucket_count, |
| 469 | const BucketRanges* ranges) |
| 470 | : HistogramBase(name), |
| 471 | bucket_ranges_(ranges), |
| 472 | declared_min_(minimum), |
| 473 | declared_max_(maximum), |
| 474 | bucket_count_(bucket_count), |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 475 | sample_(bucket_count) {} |
| 476 | |
| 477 | Histogram::~Histogram() { |
| 478 | if (StatisticsRecorder::dump_on_exit()) { |
| 479 | string output; |
| 480 | WriteAsciiImpl(true, "\n", &output); |
| 481 | DLOG(INFO) << output; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // static |
| 486 | bool Histogram::InspectConstructionArguments(const string& name, |
| 487 | Sample* minimum, |
| 488 | Sample* maximum, |
| 489 | size_t* bucket_count) { |
| 490 | // Defensive code for backward compatibility. |
| 491 | if (*minimum < 1) { |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 492 | DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 493 | *minimum = 1; |
| 494 | } |
| 495 | if (*maximum >= kSampleType_MAX) { |
[email protected] | a5c7bd79 | 2012-08-02 00:29:04 | [diff] [blame] | 496 | DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum; |
| 497 | *maximum = kSampleType_MAX - 1; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 498 | } |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 499 | if (*bucket_count >= kBucketCount_MAX) { |
| 500 | DVLOG(1) << "Histogram: " << name << " has bad bucket_count: " |
| 501 | << *bucket_count; |
| 502 | *bucket_count = kBucketCount_MAX - 1; |
| 503 | } |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 504 | |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 505 | if (*minimum >= *maximum) |
| 506 | return false; |
| 507 | if (*bucket_count < 3) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 508 | return false; |
| 509 | if (*bucket_count > static_cast<size_t>(*maximum - *minimum + 2)) |
| 510 | return false; |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | bool Histogram::SerializeRanges(Pickle* pickle) const { |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | bool Histogram::PrintEmptyBucket(size_t index) const { |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | size_t Histogram::BucketIndex(Sample value) const { |
| 523 | // Use simple binary search. This is very general, but there are better |
| 524 | // approaches if we knew that the buckets were linearly distributed. |
| 525 | DCHECK_LE(ranges(0), value); |
| 526 | DCHECK_GT(ranges(bucket_count()), value); |
| 527 | size_t under = 0; |
| 528 | size_t over = bucket_count(); |
| 529 | size_t mid; |
| 530 | |
| 531 | do { |
| 532 | DCHECK_GE(over, under); |
| 533 | mid = under + (over - under)/2; |
| 534 | if (mid == under) |
| 535 | break; |
| 536 | if (ranges(mid) <= value) |
| 537 | under = mid; |
| 538 | else |
| 539 | over = mid; |
| 540 | } while (true); |
| 541 | |
| 542 | DCHECK_LE(ranges(mid), value); |
| 543 | CHECK_GT(ranges(mid+1), value); |
| 544 | return mid; |
| 545 | } |
| 546 | |
| 547 | // Use the actual bucket widths (like a linear histogram) until the widths get |
| 548 | // over some transition value, and then use that transition width. Exponentials |
| 549 | // get so big so fast (and we don't expect to see a lot of entries in the large |
| 550 | // buckets), so we need this to make it possible to see what is going on and |
| 551 | // not have 0-graphical-height buckets. |
| 552 | double Histogram::GetBucketSize(Count current, size_t i) const { |
| 553 | DCHECK_GT(ranges(i + 1), ranges(i)); |
| 554 | static const double kTransitionWidth = 5; |
| 555 | double denominator = ranges(i + 1) - ranges(i); |
| 556 | if (denominator > kTransitionWidth) |
| 557 | denominator = kTransitionWidth; // Stop trying to normalize. |
| 558 | return current/denominator; |
| 559 | } |
| 560 | |
| 561 | const string Histogram::GetAsciiBucketRange(size_t i) const { |
| 562 | string result; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 563 | if (kHexRangePrintingFlag & flags()) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 564 | StringAppendF(&result, "%#x", ranges(i)); |
| 565 | else |
| 566 | StringAppendF(&result, "%d", ranges(i)); |
| 567 | return result; |
| 568 | } |
| 569 | |
| 570 | // Update histogram data with new sample. |
| 571 | void Histogram::Accumulate(Sample value, Count count, size_t index) { |
| 572 | // Note locking not done in this version!!! |
| 573 | sample_.Accumulate(value, count, index); |
| 574 | } |
| 575 | |
| 576 | //------------------------------------------------------------------------------ |
| 577 | // Private methods |
| 578 | |
| 579 | void Histogram::WriteAsciiImpl(bool graph_it, |
| 580 | const string& newline, |
| 581 | string* output) const { |
| 582 | // Get local (stack) copies of all effectively volatile class data so that we |
| 583 | // are consistent across our output activities. |
| 584 | SampleSet snapshot; |
| 585 | SnapshotSample(&snapshot); |
| 586 | Count sample_count = snapshot.TotalCount(); |
| 587 | |
| 588 | WriteAsciiHeader(snapshot, sample_count, output); |
| 589 | output->append(newline); |
| 590 | |
| 591 | // Prepare to normalize graphical rendering of bucket contents. |
| 592 | double max_size = 0; |
| 593 | if (graph_it) |
| 594 | max_size = GetPeakBucketSize(snapshot); |
| 595 | |
| 596 | // Calculate space needed to print bucket range numbers. Leave room to print |
| 597 | // nearly the largest bucket range without sliding over the histogram. |
| 598 | size_t largest_non_empty_bucket = bucket_count() - 1; |
| 599 | while (0 == snapshot.counts(largest_non_empty_bucket)) { |
| 600 | if (0 == largest_non_empty_bucket) |
| 601 | break; // All buckets are empty. |
| 602 | --largest_non_empty_bucket; |
| 603 | } |
| 604 | |
| 605 | // Calculate largest print width needed for any of our bucket range displays. |
| 606 | size_t print_width = 1; |
| 607 | for (size_t i = 0; i < bucket_count(); ++i) { |
| 608 | if (snapshot.counts(i)) { |
| 609 | size_t width = GetAsciiBucketRange(i).size() + 1; |
| 610 | if (width > print_width) |
| 611 | print_width = width; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | int64 remaining = sample_count; |
| 616 | int64 past = 0; |
| 617 | // Output the actual histogram graph. |
| 618 | for (size_t i = 0; i < bucket_count(); ++i) { |
| 619 | Count current = snapshot.counts(i); |
| 620 | if (!current && !PrintEmptyBucket(i)) |
| 621 | continue; |
| 622 | remaining -= current; |
| 623 | string range = GetAsciiBucketRange(i); |
| 624 | output->append(range); |
| 625 | for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 626 | output->push_back(' '); |
| 627 | if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) { |
| 628 | while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) |
| 629 | ++i; |
| 630 | output->append("... "); |
| 631 | output->append(newline); |
| 632 | continue; // No reason to plot emptiness. |
| 633 | } |
| 634 | double current_size = GetBucketSize(current, i); |
| 635 | if (graph_it) |
| 636 | WriteAsciiBucketGraph(current_size, max_size, output); |
| 637 | WriteAsciiBucketContext(past, current, remaining, i, output); |
| 638 | output->append(newline); |
| 639 | past += current; |
| 640 | } |
| 641 | DCHECK_EQ(sample_count, past); |
| 642 | } |
| 643 | |
| 644 | double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const { |
| 645 | double max = 0; |
| 646 | for (size_t i = 0; i < bucket_count() ; ++i) { |
| 647 | double current_size = GetBucketSize(snapshot.counts(i), i); |
| 648 | if (current_size > max) |
| 649 | max = current_size; |
| 650 | } |
| 651 | return max; |
| 652 | } |
| 653 | |
| 654 | void Histogram::WriteAsciiHeader(const SampleSet& snapshot, |
| 655 | Count sample_count, |
| 656 | string* output) const { |
| 657 | StringAppendF(output, |
| 658 | "Histogram: %s recorded %d samples", |
| 659 | histogram_name().c_str(), |
| 660 | sample_count); |
| 661 | if (0 == sample_count) { |
| 662 | DCHECK_EQ(snapshot.sum(), 0); |
| 663 | } else { |
| 664 | double average = static_cast<float>(snapshot.sum()) / sample_count; |
| 665 | |
| 666 | StringAppendF(output, ", average = %.1f", average); |
| 667 | } |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 668 | if (flags() & ~kHexRangePrintingFlag) |
| 669 | StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | void Histogram::WriteAsciiBucketContext(const int64 past, |
| 673 | const Count current, |
| 674 | const int64 remaining, |
| 675 | const size_t i, |
| 676 | string* output) const { |
| 677 | double scaled_sum = (past + current + remaining) / 100.0; |
| 678 | WriteAsciiBucketValue(current, scaled_sum, output); |
| 679 | if (0 < i) { |
| 680 | double percentage = past / scaled_sum; |
| 681 | StringAppendF(output, " {%3.1f%%}", percentage); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void Histogram::WriteAsciiBucketValue(Count current, |
| 686 | double scaled_sum, |
| 687 | string* output) const { |
| 688 | StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); |
| 689 | } |
| 690 | |
| 691 | void Histogram::WriteAsciiBucketGraph(double current_size, |
| 692 | double max_size, |
| 693 | string* output) const { |
| 694 | const int k_line_length = 72; // Maximal horizontal width of graph. |
| 695 | int x_count = static_cast<int>(k_line_length * (current_size / max_size) |
| 696 | + 0.5); |
| 697 | int x_remainder = k_line_length - x_count; |
| 698 | |
| 699 | while (0 < x_count--) |
| 700 | output->append("-"); |
| 701 | output->append("O"); |
| 702 | while (0 < x_remainder--) |
| 703 | output->append(" "); |
| 704 | } |
| 705 | |
| 706 | //------------------------------------------------------------------------------ |
| 707 | // LinearHistogram: This histogram uses a traditional set of evenly spaced |
| 708 | // buckets. |
| 709 | //------------------------------------------------------------------------------ |
| 710 | |
| 711 | LinearHistogram::~LinearHistogram() {} |
| 712 | |
| 713 | Histogram* LinearHistogram::FactoryGet(const string& name, |
| 714 | Sample minimum, |
| 715 | Sample maximum, |
| 716 | size_t bucket_count, |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 717 | int32 flags) { |
[email protected] | e184be90 | 2012-08-07 04:49:24 | [diff] [blame] | 718 | bool valid_arguments = Histogram::InspectConstructionArguments( |
| 719 | name, &minimum, &maximum, &bucket_count); |
| 720 | DCHECK(valid_arguments); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 721 | |
| 722 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 723 | if (!histogram) { |
| 724 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 725 | BucketRanges* ranges = new BucketRanges(bucket_count + 1); |
| 726 | InitializeBucketRanges(minimum, maximum, bucket_count, ranges); |
| 727 | const BucketRanges* registered_ranges = |
| 728 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 729 | |
| 730 | LinearHistogram* tentative_histogram = |
| 731 | new LinearHistogram(name, minimum, maximum, bucket_count, |
| 732 | registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 733 | CheckCorruption(*tentative_histogram, true); |
| 734 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 735 | tentative_histogram->SetFlags(flags); |
| 736 | histogram = |
| 737 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
| 738 | } |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 739 | // TODO(rtenneti): delete this code after debugging. |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 740 | CheckCorruption(*histogram, false); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 741 | |
| 742 | CHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type()); |
[email protected] | 065d170 | 2012-08-23 23:55:13 | [diff] [blame^] | 743 | CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count)); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 744 | return histogram; |
| 745 | } |
| 746 | |
| 747 | Histogram* LinearHistogram::FactoryTimeGet(const string& name, |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 748 | TimeDelta minimum, |
| 749 | TimeDelta maximum, |
| 750 | size_t bucket_count, |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 751 | int32 flags) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 752 | return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
| 753 | bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 754 | } |
| 755 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 756 | Histogram::ClassType LinearHistogram::histogram_type() const { |
| 757 | return LINEAR_HISTOGRAM; |
| 758 | } |
| 759 | |
| 760 | void LinearHistogram::SetRangeDescriptions( |
| 761 | const DescriptionPair descriptions[]) { |
| 762 | for (int i =0; descriptions[i].description; ++i) { |
| 763 | bucket_description_[descriptions[i].sample] = descriptions[i].description; |
| 764 | } |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 765 | } |
| 766 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 767 | LinearHistogram::LinearHistogram(const string& name, |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 768 | Sample minimum, |
| 769 | Sample maximum, |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 770 | size_t bucket_count, |
| 771 | const BucketRanges* ranges) |
| 772 | : Histogram(name, minimum, maximum, bucket_count, ranges) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 773 | } |
| 774 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 775 | double LinearHistogram::GetBucketSize(Count current, size_t i) const { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 776 | DCHECK_GT(ranges(i + 1), ranges(i)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 777 | // Adjacent buckets with different widths would have "surprisingly" many (few) |
| 778 | // samples in a histogram if we didn't normalize this way. |
| 779 | double denominator = ranges(i + 1) - ranges(i); |
| 780 | return current/denominator; |
| 781 | } |
| 782 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 783 | const string LinearHistogram::GetAsciiBucketRange(size_t i) const { |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 784 | int range = ranges(i); |
| 785 | BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
| 786 | if (it == bucket_description_.end()) |
| 787 | return Histogram::GetAsciiBucketRange(i); |
| 788 | return it->second; |
| 789 | } |
| 790 | |
| 791 | bool LinearHistogram::PrintEmptyBucket(size_t index) const { |
| 792 | return bucket_description_.find(ranges(index)) == bucket_description_.end(); |
| 793 | } |
| 794 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 795 | // static |
| 796 | void LinearHistogram::InitializeBucketRanges(Sample minimum, |
| 797 | Sample maximum, |
| 798 | size_t bucket_count, |
| 799 | BucketRanges* ranges) { |
| 800 | DCHECK_EQ(ranges->size(), bucket_count + 1); |
| 801 | double min = minimum; |
| 802 | double max = maximum; |
| 803 | size_t i; |
| 804 | for (i = 1; i < bucket_count; ++i) { |
| 805 | double linear_range = |
| 806 | (min * (bucket_count -1 - i) + max * (i - 1)) / (bucket_count - 2); |
| 807 | ranges->set_range(i, static_cast<Sample>(linear_range + 0.5)); |
| 808 | } |
| 809 | ranges->set_range(ranges->size() - 1, HistogramBase::kSampleType_MAX); |
| 810 | ranges->ResetChecksum(); |
| 811 | } |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 812 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 813 | //------------------------------------------------------------------------------ |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 814 | // This section provides implementation for BooleanHistogram. |
| 815 | //------------------------------------------------------------------------------ |
| 816 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 817 | Histogram* BooleanHistogram::FactoryGet(const string& name, int32 flags) { |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame] | 818 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 819 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 820 | // To avoid racy destruction at shutdown, the following will be leaked. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 821 | BucketRanges* ranges = new BucketRanges(4); |
| 822 | LinearHistogram::InitializeBucketRanges(1, 2, 3, ranges); |
| 823 | const BucketRanges* registered_ranges = |
| 824 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 825 | |
| 826 | BooleanHistogram* tentative_histogram = |
| 827 | new BooleanHistogram(name, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 828 | CheckCorruption(*tentative_histogram, true); |
| 829 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 830 | tentative_histogram->SetFlags(flags); |
| 831 | histogram = |
| 832 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 833 | } |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 834 | // TODO(rtenneti): delete this code after debugging. |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 835 | CheckCorruption(*histogram, false); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 836 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 837 | CHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 838 | return histogram; |
| 839 | } |
| 840 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 841 | Histogram::ClassType BooleanHistogram::histogram_type() const { |
| 842 | return BOOLEAN_HISTOGRAM; |
| 843 | } |
| 844 | |
| 845 | void BooleanHistogram::AddBoolean(bool value) { |
| 846 | Add(value ? 1 : 0); |
| 847 | } |
| 848 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 849 | BooleanHistogram::BooleanHistogram(const string& name, |
| 850 | const BucketRanges* ranges) |
| 851 | : LinearHistogram(name, 1, 2, 3, ranges) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 852 | |
| 853 | //------------------------------------------------------------------------------ |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 854 | // CustomHistogram: |
| 855 | //------------------------------------------------------------------------------ |
| 856 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 857 | Histogram* CustomHistogram::FactoryGet(const string& name, |
| 858 | const vector<Sample>& custom_ranges, |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 859 | int32 flags) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 860 | CHECK(ValidateCustomRanges(custom_ranges)); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 861 | |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame] | 862 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 863 | if (!histogram) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 864 | BucketRanges* ranges = CreateBucketRangesFromCustomRanges(custom_ranges); |
| 865 | const BucketRanges* registered_ranges = |
| 866 | StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges); |
| 867 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 868 | // To avoid racy destruction at shutdown, the following will be leaked. |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 869 | CustomHistogram* tentative_histogram = |
| 870 | new CustomHistogram(name, registered_ranges); |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 871 | CheckCorruption(*tentative_histogram, true); |
| 872 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 873 | tentative_histogram->SetFlags(flags); |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 874 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 875 | histogram = |
| 876 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 877 | } |
[email protected] | ec0c0aa | 2012-08-14 02:02:00 | [diff] [blame] | 878 | // TODO(rtenneti): delete this code after debugging. |
[email protected] | 263a17a | 2012-08-16 03:03:54 | [diff] [blame] | 879 | CheckCorruption(*histogram, false); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 880 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 881 | CHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 882 | return histogram; |
| 883 | } |
| 884 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 885 | Histogram::ClassType CustomHistogram::histogram_type() const { |
| 886 | return CUSTOM_HISTOGRAM; |
| 887 | } |
| 888 | |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 889 | // static |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 890 | vector<Sample> CustomHistogram::ArrayToCustomRanges( |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 891 | const Sample* values, size_t num_values) { |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 892 | vector<Sample> all_values; |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 893 | for (size_t i = 0; i < num_values; ++i) { |
| 894 | Sample value = values[i]; |
| 895 | all_values.push_back(value); |
| 896 | |
| 897 | // Ensure that a guard bucket is added. If we end up with duplicate |
| 898 | // values, FactoryGet will take care of removing them. |
| 899 | all_values.push_back(value + 1); |
| 900 | } |
| 901 | return all_values; |
| 902 | } |
| 903 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 904 | CustomHistogram::CustomHistogram(const string& name, |
| 905 | const BucketRanges* ranges) |
| 906 | : Histogram(name, |
| 907 | ranges->range(1), |
| 908 | ranges->range(ranges->size() - 2), |
| 909 | ranges->size() - 1, |
| 910 | ranges) {} |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 911 | |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 912 | bool CustomHistogram::SerializeRanges(Pickle* pickle) const { |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 913 | for (size_t i = 0; i < bucket_ranges()->size(); ++i) { |
| 914 | if (!pickle->WriteInt(bucket_ranges()->range(i))) |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | return true; |
| 918 | } |
| 919 | |
| 920 | // static |
| 921 | bool CustomHistogram::DeserializeRanges( |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 922 | PickleIterator* iter, vector<Sample>* ranges) { |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 923 | for (size_t i = 0; i < ranges->size(); ++i) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 924 | if (!iter->ReadInt(&(*ranges)[i])) |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 925 | return false; |
| 926 | } |
| 927 | return true; |
| 928 | } |
| 929 | |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 930 | double CustomHistogram::GetBucketSize(Count current, size_t i) const { |
| 931 | return 1; |
| 932 | } |
| 933 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 934 | // static |
| 935 | bool CustomHistogram::ValidateCustomRanges( |
| 936 | const vector<Sample>& custom_ranges) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 937 | bool has_valid_range = false; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 938 | for (size_t i = 0; i < custom_ranges.size(); i++) { |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 939 | Sample sample = custom_ranges[i]; |
| 940 | if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 941 | return false; |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 942 | if (sample != 0) |
| 943 | has_valid_range = true; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 944 | } |
[email protected] | 640d95ef | 2012-08-04 06:23:03 | [diff] [blame] | 945 | return has_valid_range; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | // static |
| 949 | BucketRanges* CustomHistogram::CreateBucketRangesFromCustomRanges( |
| 950 | const vector<Sample>& custom_ranges) { |
| 951 | // Remove the duplicates in the custom ranges array. |
| 952 | vector<int> ranges = custom_ranges; |
| 953 | ranges.push_back(0); // Ensure we have a zero value. |
| 954 | ranges.push_back(HistogramBase::kSampleType_MAX); |
| 955 | std::sort(ranges.begin(), ranges.end()); |
| 956 | ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end()); |
| 957 | |
| 958 | BucketRanges* bucket_ranges = new BucketRanges(ranges.size()); |
| 959 | for (size_t i = 0; i < ranges.size(); i++) { |
| 960 | bucket_ranges->set_range(i, ranges[i]); |
| 961 | } |
| 962 | bucket_ranges->ResetChecksum(); |
| 963 | return bucket_ranges; |
| 964 | } |
| 965 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 966 | } // namespace base |