[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 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | #include "base/logging.h" |
[email protected] | 567d30e | 2012-07-13 21:48:29 | [diff] [blame] | 18 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 3f38385 | 2009-04-03 18:18:55 | [diff] [blame] | 19 | #include "base/pickle.h" |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 20 | #include "base/stringprintf.h" |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 21 | #include "base/synchronization/lock.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 23 | namespace base { |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 24 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 25 | // Static table of checksums for all possible 8 bit bytes. |
| 26 | const uint32 Histogram::kCrcTable[256] = {0x0, 0x77073096L, 0xee0e612cL, |
| 27 | 0x990951baL, 0x76dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0xedb8832L, |
| 28 | 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x9b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, |
| 29 | 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL, |
| 30 | 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, |
| 31 | 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, |
| 32 | 0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, |
| 33 | 0xa50ab56bL, 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, |
| 34 | 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, 0xc8d75180L, |
| 35 | 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, 0x2802b89eL, |
| 36 | 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, |
| 37 | 0xb6662d3dL, 0x76dc4190L, 0x1db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, |
| 38 | 0x6b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0xf00f934L, 0x9609a88eL, |
| 39 | 0xe10e9818L, 0x7f6a0dbbL, 0x86d3d2dL, 0x91646c97L, 0xe6635c01L, 0x6b6b51f4L, |
| 40 | 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, |
| 41 | 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, |
| 42 | 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, |
| 43 | 0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, |
| 44 | 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, |
| 45 | 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L, |
| 46 | 0x206f85b3L, 0xb966d409L, 0xce61e49fL, 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, |
| 47 | 0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, |
| 48 | 0x9abfb3b6L, 0x3b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x4db2615L, |
| 49 | 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0xd6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL, |
| 50 | 0x9309ff9dL, 0xa00ae27L, 0x7d079eb1L, 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, |
| 51 | 0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, |
| 52 | 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, |
| 53 | 0x60b08ed5L, 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, |
| 54 | 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, |
| 55 | 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, 0xcb61b38cL, |
| 56 | 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, |
| 57 | 0x5505262fL, 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, |
| 58 | 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, |
| 59 | 0x26d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x5005713L, 0x95bf4a82L, |
| 60 | 0xe2b87a14L, 0x7bb12baeL, 0xcb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, |
| 61 | 0xbdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, |
| 62 | 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, |
| 63 | 0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, |
| 64 | 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL, |
| 65 | 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L, |
| 66 | 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, |
| 67 | 0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, |
| 68 | 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, |
| 69 | 0x2d02ef8dL, |
| 70 | }; |
| 71 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 72 | typedef Histogram::Count Count; |
| 73 | |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 74 | // static |
[email protected] | 9fce5f0 | 2011-03-02 08:04:57 | [diff] [blame] | 75 | const size_t Histogram::kBucketCount_MAX = 16384u; |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 76 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 77 | Histogram* Histogram::FactoryGet(const std::string& name, |
| 78 | Sample minimum, |
| 79 | Sample maximum, |
| 80 | size_t bucket_count, |
| 81 | Flags flags) { |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 82 | // Defensive code. |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 83 | if (minimum < 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 84 | minimum = 1; |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 85 | if (maximum > kSampleType_MAX - 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 86 | maximum = kSampleType_MAX - 1; |
| 87 | |
[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 88 | DCHECK_GT(maximum, minimum); |
| 89 | DCHECK_GT((Sample) bucket_count, 2); |
| 90 | DCHECK_LE((Sample) bucket_count, maximum - minimum + 2); |
| 91 | |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame^] | 92 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 93 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 94 | // Extra variable is not needed... but this keeps this section basically |
| 95 | // identical to other derived classes in this file (and compiler will |
| 96 | // optimize away the extra variable. |
| 97 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 98 | Histogram* tentative_histogram = |
| 99 | new Histogram(name, minimum, maximum, bucket_count); |
| 100 | tentative_histogram->InitializeBucketRange(); |
| 101 | tentative_histogram->SetFlags(flags); |
| 102 | histogram = |
| 103 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 106 | DCHECK_EQ(HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 107 | DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 108 | return histogram; |
| 109 | } |
| 110 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 111 | Histogram* Histogram::FactoryTimeGet(const std::string& name, |
| 112 | TimeDelta minimum, |
| 113 | TimeDelta maximum, |
| 114 | size_t bucket_count, |
| 115 | Flags flags) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 116 | return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
| 117 | bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | b404b1f | 2011-10-16 07:29:17 | [diff] [blame] | 120 | TimeTicks Histogram::DebugNow() { |
| 121 | #ifndef NDEBUG |
| 122 | return TimeTicks::Now(); |
| 123 | #else |
| 124 | return TimeTicks(); |
| 125 | #endif |
| 126 | } |
| 127 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | void Histogram::Add(int value) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 129 | if (value > kSampleType_MAX - 1) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | value = kSampleType_MAX - 1; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | if (value < 0) |
| 132 | value = 0; |
| 133 | size_t index = BucketIndex(value); |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 134 | DCHECK_GE(value, ranges(index)); |
| 135 | DCHECK_LT(value, ranges(index + 1)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | Accumulate(value, 1, index); |
| 137 | } |
| 138 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 139 | void Histogram::AddBoolean(bool value) { |
| 140 | DCHECK(false); |
| 141 | } |
| 142 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 143 | void Histogram::AddSampleSet(const SampleSet& sample) { |
| 144 | sample_.Add(sample); |
| 145 | } |
| 146 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 147 | void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) { |
| 148 | DCHECK(false); |
| 149 | } |
| 150 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | // The following methods provide a graphical histogram display. |
| 152 | void Histogram::WriteHTMLGraph(std::string* output) const { |
| 153 | // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. |
| 154 | output->append("<PRE>"); |
| 155 | WriteAscii(true, "<br>", output); |
| 156 | output->append("</PRE>"); |
| 157 | } |
| 158 | |
| 159 | void Histogram::WriteAscii(bool graph_it, const std::string& newline, |
| 160 | std::string* output) const { |
| 161 | // Get local (stack) copies of all effectively volatile class data so that we |
| 162 | // are consistent across our output activities. |
| 163 | SampleSet snapshot; |
| 164 | SnapshotSample(&snapshot); |
| 165 | Count sample_count = snapshot.TotalCount(); |
| 166 | |
| 167 | WriteAsciiHeader(snapshot, sample_count, output); |
| 168 | output->append(newline); |
| 169 | |
| 170 | // Prepare to normalize graphical rendering of bucket contents. |
| 171 | double max_size = 0; |
| 172 | if (graph_it) |
| 173 | max_size = GetPeakBucketSize(snapshot); |
| 174 | |
| 175 | // Calculate space needed to print bucket range numbers. Leave room to print |
| 176 | // nearly the largest bucket range without sliding over the histogram. |
[email protected] | e2951cf | 2008-09-24 23:51:25 | [diff] [blame] | 177 | size_t largest_non_empty_bucket = bucket_count() - 1; |
| 178 | while (0 == snapshot.counts(largest_non_empty_bucket)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | if (0 == largest_non_empty_bucket) |
| 180 | break; // All buckets are empty. |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 181 | --largest_non_empty_bucket; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Calculate largest print width needed for any of our bucket range displays. |
| 185 | size_t print_width = 1; |
[email protected] | e2951cf | 2008-09-24 23:51:25 | [diff] [blame] | 186 | for (size_t i = 0; i < bucket_count(); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 187 | if (snapshot.counts(i)) { |
| 188 | size_t width = GetAsciiBucketRange(i).size() + 1; |
| 189 | if (width > print_width) |
| 190 | print_width = width; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int64 remaining = sample_count; |
| 195 | int64 past = 0; |
| 196 | // Output the actual histogram graph. |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 197 | for (size_t i = 0; i < bucket_count(); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 198 | Count current = snapshot.counts(i); |
| 199 | if (!current && !PrintEmptyBucket(i)) |
| 200 | continue; |
| 201 | remaining -= current; |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 202 | std::string range = GetAsciiBucketRange(i); |
| 203 | output->append(range); |
| 204 | for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 205 | output->push_back(' '); |
[email protected] | e2951cf | 2008-09-24 23:51:25 | [diff] [blame] | 206 | if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) { |
| 207 | while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 208 | ++i; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | output->append("... "); |
| 210 | output->append(newline); |
| 211 | continue; // No reason to plot emptiness. |
| 212 | } |
| 213 | double current_size = GetBucketSize(current, i); |
| 214 | if (graph_it) |
| 215 | WriteAsciiBucketGraph(current_size, max_size, output); |
| 216 | WriteAsciiBucketContext(past, current, remaining, i, output); |
| 217 | output->append(newline); |
| 218 | past += current; |
| 219 | } |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 220 | DCHECK_EQ(sample_count, past); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 223 | // static |
| 224 | std::string Histogram::SerializeHistogramInfo(const Histogram& histogram, |
| 225 | const SampleSet& snapshot) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 226 | DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 227 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 228 | Pickle pickle; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 229 | pickle.WriteString(histogram.histogram_name()); |
| 230 | pickle.WriteInt(histogram.declared_min()); |
| 231 | pickle.WriteInt(histogram.declared_max()); |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 232 | pickle.WriteUInt64(histogram.bucket_count()); |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 233 | pickle.WriteUInt32(histogram.range_checksum()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 234 | pickle.WriteInt(histogram.histogram_type()); |
[email protected] | 1f4fc8e8c | 2010-01-02 00:46:41 | [diff] [blame] | 235 | pickle.WriteInt(histogram.flags()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 236 | |
| 237 | snapshot.Serialize(&pickle); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 238 | |
| 239 | histogram.SerializeRanges(&pickle); |
| 240 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 241 | return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
| 242 | } |
| 243 | |
| 244 | // static |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 245 | bool Histogram::DeserializeHistogramInfo(const std::string& histogram_info) { |
| 246 | if (histogram_info.empty()) { |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 247 | return false; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | Pickle pickle(histogram_info.data(), |
| 251 | static_cast<int>(histogram_info.size())); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 252 | std::string histogram_name; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 253 | int declared_min; |
| 254 | int declared_max; |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 255 | uint64 bucket_count; |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 256 | uint32 range_checksum; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 257 | int histogram_type; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 258 | int pickle_flags; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 259 | SampleSet sample; |
| 260 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 261 | PickleIterator iter(pickle); |
| 262 | if (!iter.ReadString(&histogram_name) || |
| 263 | !iter.ReadInt(&declared_min) || |
| 264 | !iter.ReadInt(&declared_max) || |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 265 | !iter.ReadUInt64(&bucket_count) || |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 266 | !iter.ReadUInt32(&range_checksum) || |
| 267 | !iter.ReadInt(&histogram_type) || |
| 268 | !iter.ReadInt(&pickle_flags) || |
| 269 | !sample.Histogram::SampleSet::Deserialize(&iter)) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 270 | DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 271 | return false; |
| 272 | } |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 273 | |
[email protected] | 1f4fc8e8c | 2010-01-02 00:46:41 | [diff] [blame] | 274 | DCHECK(pickle_flags & kIPCSerializationSourceFlag); |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 275 | // Since these fields may have come from an untrusted renderer, do additional |
| 276 | // checks above and beyond those in Histogram::Initialize() |
| 277 | if (declared_max <= 0 || declared_min <= 0 || declared_max < declared_min || |
| 278 | INT_MAX / sizeof(Count) <= bucket_count || bucket_count < 2) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 279 | DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 283 | Flags flags = static_cast<Flags>(pickle_flags & ~kIPCSerializationSourceFlag); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 284 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 285 | DCHECK_NE(NOT_VALID_IN_RENDERER, histogram_type); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 286 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 287 | Histogram* render_histogram(NULL); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 288 | |
[email protected] | a764bf5e | 2010-06-02 21:31:44 | [diff] [blame] | 289 | if (histogram_type == HISTOGRAM) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 290 | render_histogram = Histogram::FactoryGet( |
| 291 | histogram_name, declared_min, declared_max, bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 292 | } else if (histogram_type == LINEAR_HISTOGRAM) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 293 | render_histogram = LinearHistogram::FactoryGet( |
| 294 | histogram_name, declared_min, declared_max, bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 295 | } else if (histogram_type == BOOLEAN_HISTOGRAM) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 296 | render_histogram = BooleanHistogram::FactoryGet(histogram_name, flags); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 297 | } else if (histogram_type == CUSTOM_HISTOGRAM) { |
| 298 | std::vector<Histogram::Sample> sample_ranges(bucket_count); |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 299 | if (!CustomHistogram::DeserializeRanges(&iter, &sample_ranges)) { |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 300 | DLOG(ERROR) << "Pickle error decoding ranges: " << histogram_name; |
| 301 | return false; |
| 302 | } |
| 303 | render_histogram = |
| 304 | CustomHistogram::FactoryGet(histogram_name, sample_ranges, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 305 | } else { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 306 | DLOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: " |
| 307 | << histogram_type; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 308 | return false; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 311 | DCHECK_EQ(render_histogram->declared_min(), declared_min); |
| 312 | DCHECK_EQ(render_histogram->declared_max(), declared_max); |
| 313 | DCHECK_EQ(render_histogram->bucket_count(), bucket_count); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 314 | DCHECK_EQ(render_histogram->range_checksum(), range_checksum); |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 315 | DCHECK_EQ(render_histogram->histogram_type(), histogram_type); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 316 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 317 | if (render_histogram->flags() & kIPCSerializationSourceFlag) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 318 | DVLOG(1) << "Single process mode, histogram observed and not copied: " |
| 319 | << histogram_name; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 320 | } else { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 321 | DCHECK_EQ(flags & render_histogram->flags(), flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 322 | render_histogram->AddSampleSet(sample); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 328 | //------------------------------------------------------------------------------ |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 329 | // Methods for the validating a sample and a related histogram. |
| 330 | //------------------------------------------------------------------------------ |
| 331 | |
| 332 | Histogram::Inconsistencies Histogram::FindCorruption( |
| 333 | const SampleSet& snapshot) const { |
| 334 | int inconsistencies = NO_INCONSISTENCIES; |
| 335 | Sample previous_range = -1; // Bottom range is always 0. |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 336 | int64 count = 0; |
| 337 | for (size_t index = 0; index < bucket_count(); ++index) { |
| 338 | count += snapshot.counts(index); |
| 339 | int new_range = ranges(index); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 340 | if (previous_range >= new_range) |
| 341 | inconsistencies |= BUCKET_ORDER_ERROR; |
| 342 | previous_range = new_range; |
| 343 | } |
| 344 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 345 | if (!HasValidRangeChecksum()) |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 346 | inconsistencies |= RANGE_CHECKSUM_ERROR; |
| 347 | |
| 348 | int64 delta64 = snapshot.redundant_count() - count; |
| 349 | if (delta64 != 0) { |
| 350 | int delta = static_cast<int>(delta64); |
| 351 | if (delta != delta64) |
| 352 | delta = INT_MAX; // Flag all giant errors as INT_MAX. |
| 353 | // Since snapshots of histograms are taken asynchronously relative to |
| 354 | // sampling (and snapped from different threads), it is pretty likely that |
| 355 | // we'll catch a redundant count that doesn't match the sample count. We |
| 356 | // allow for a certain amount of slop before flagging this as an |
| 357 | // inconsistency. Even with an inconsistency, we'll snapshot it again (for |
| 358 | // UMA in about a half hour, so we'll eventually get the data, if it was |
| 359 | // not the result of a corruption. If histograms show that 1 is "too tight" |
| 360 | // then we may try to use 2 or 3 for this slop value. |
| 361 | const int kCommonRaceBasedCountMismatch = 1; |
| 362 | if (delta > 0) { |
| 363 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta); |
| 364 | if (delta > kCommonRaceBasedCountMismatch) |
| 365 | inconsistencies |= COUNT_HIGH_ERROR; |
| 366 | } else { |
| 367 | DCHECK_GT(0, delta); |
| 368 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta); |
| 369 | if (-delta > kCommonRaceBasedCountMismatch) |
| 370 | inconsistencies |= COUNT_LOW_ERROR; |
| 371 | } |
| 372 | } |
| 373 | return static_cast<Inconsistencies>(inconsistencies); |
| 374 | } |
| 375 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 376 | Histogram::ClassType Histogram::histogram_type() const { |
| 377 | return HISTOGRAM; |
| 378 | } |
| 379 | |
| 380 | Histogram::Sample Histogram::ranges(size_t i) const { |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 381 | return cached_ranges_->ranges(i); |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | size_t Histogram::bucket_count() const { |
| 385 | return bucket_count_; |
| 386 | } |
| 387 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 388 | // Do a safe atomic snapshot of sample data. |
| 389 | // This implementation assumes we are on a safe single thread. |
| 390 | void Histogram::SnapshotSample(SampleSet* sample) const { |
| 391 | // Note locking not done in this version!!! |
| 392 | *sample = sample_; |
| 393 | } |
| 394 | |
| 395 | bool Histogram::HasConstructorArguments(Sample minimum, |
| 396 | Sample maximum, |
| 397 | size_t bucket_count) { |
| 398 | return ((minimum == declared_min_) && (maximum == declared_max_) && |
| 399 | (bucket_count == bucket_count_)); |
| 400 | } |
| 401 | |
| 402 | bool Histogram::HasConstructorTimeDeltaArguments(TimeDelta minimum, |
| 403 | TimeDelta maximum, |
| 404 | size_t bucket_count) { |
| 405 | return ((minimum.InMilliseconds() == declared_min_) && |
| 406 | (maximum.InMilliseconds() == declared_max_) && |
| 407 | (bucket_count == bucket_count_)); |
| 408 | } |
| 409 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 410 | bool Histogram::HasValidRangeChecksum() const { |
| 411 | return CalculateRangeChecksum() == range_checksum_; |
| 412 | } |
| 413 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 414 | Histogram::Histogram(const std::string& name, Sample minimum, |
| 415 | Sample maximum, size_t bucket_count) |
| 416 | : histogram_name_(name), |
| 417 | declared_min_(minimum), |
| 418 | declared_max_(maximum), |
| 419 | bucket_count_(bucket_count), |
| 420 | flags_(kNoFlags), |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 421 | cached_ranges_(new CachedRanges(bucket_count + 1, 0)), |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 422 | range_checksum_(0), |
| 423 | sample_() { |
| 424 | Initialize(); |
| 425 | } |
| 426 | |
| 427 | Histogram::Histogram(const std::string& name, TimeDelta minimum, |
| 428 | TimeDelta maximum, size_t bucket_count) |
| 429 | : histogram_name_(name), |
| 430 | declared_min_(static_cast<int> (minimum.InMilliseconds())), |
| 431 | declared_max_(static_cast<int> (maximum.InMilliseconds())), |
| 432 | bucket_count_(bucket_count), |
| 433 | flags_(kNoFlags), |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 434 | cached_ranges_(new CachedRanges(bucket_count + 1, 0)), |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 435 | range_checksum_(0), |
| 436 | sample_() { |
| 437 | Initialize(); |
| 438 | } |
| 439 | |
| 440 | Histogram::~Histogram() { |
| 441 | if (StatisticsRecorder::dump_on_exit()) { |
| 442 | std::string output; |
| 443 | WriteAscii(true, "\n", &output); |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 444 | DLOG(INFO) << output; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Just to make sure most derived class did this properly... |
| 448 | DCHECK(ValidateBucketRanges()); |
[email protected] | 7767bdb | 2011-03-02 23:38:00 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 451 | bool Histogram::SerializeRanges(Pickle* pickle) const { |
| 452 | return true; |
| 453 | } |
| 454 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 455 | // Calculate what range of values are held in each bucket. |
| 456 | // We have to be careful that we don't pick a ratio between starting points in |
| 457 | // consecutive buckets that is sooo small, that the integer bounds are the same |
| 458 | // (effectively making one bucket get no values). We need to avoid: |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 459 | // ranges(i) == ranges(i + 1) |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 460 | // To avoid that, we just do a fine-grained bucket width as far as we need to |
| 461 | // until we get a ratio that moves us along at least 2 units at a time. From |
| 462 | // that bucket onward we do use the exponential growth of buckets. |
| 463 | void Histogram::InitializeBucketRange() { |
| 464 | double log_max = log(static_cast<double>(declared_max())); |
| 465 | double log_ratio; |
| 466 | double log_next; |
| 467 | size_t bucket_index = 1; |
| 468 | Sample current = declared_min(); |
| 469 | SetBucketRange(bucket_index, current); |
| 470 | while (bucket_count() > ++bucket_index) { |
| 471 | double log_current; |
| 472 | log_current = log(static_cast<double>(current)); |
| 473 | // Calculate the count'th root of the range. |
| 474 | log_ratio = (log_max - log_current) / (bucket_count() - bucket_index); |
| 475 | // See where the next bucket would start. |
| 476 | log_next = log_current + log_ratio; |
| 477 | int next; |
| 478 | next = static_cast<int>(floor(exp(log_next) + 0.5)); |
| 479 | if (next > current) |
| 480 | current = next; |
| 481 | else |
| 482 | ++current; // Just do a narrow bucket, and keep trying. |
| 483 | SetBucketRange(bucket_index, current); |
| 484 | } |
| 485 | ResetRangeChecksum(); |
| 486 | |
| 487 | DCHECK_EQ(bucket_count(), bucket_index); |
| 488 | } |
| 489 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 490 | bool Histogram::PrintEmptyBucket(size_t index) const { |
| 491 | return true; |
| 492 | } |
| 493 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 494 | size_t Histogram::BucketIndex(Sample value) const { |
| 495 | // Use simple binary search. This is very general, but there are better |
| 496 | // approaches if we knew that the buckets were linearly distributed. |
| 497 | DCHECK_LE(ranges(0), value); |
| 498 | DCHECK_GT(ranges(bucket_count()), value); |
| 499 | size_t under = 0; |
| 500 | size_t over = bucket_count(); |
| 501 | size_t mid; |
| 502 | |
| 503 | do { |
| 504 | DCHECK_GE(over, under); |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 505 | mid = under + (over - under)/2; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 506 | if (mid == under) |
| 507 | break; |
| 508 | if (ranges(mid) <= value) |
| 509 | under = mid; |
| 510 | else |
| 511 | over = mid; |
| 512 | } while (true); |
| 513 | |
| 514 | DCHECK_LE(ranges(mid), value); |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 515 | CHECK_GT(ranges(mid+1), value); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 516 | return mid; |
| 517 | } |
| 518 | |
| 519 | // Use the actual bucket widths (like a linear histogram) until the widths get |
| 520 | // over some transition value, and then use that transition width. Exponentials |
| 521 | // get so big so fast (and we don't expect to see a lot of entries in the large |
| 522 | // buckets), so we need this to make it possible to see what is going on and |
| 523 | // not have 0-graphical-height buckets. |
| 524 | double Histogram::GetBucketSize(Count current, size_t i) const { |
| 525 | DCHECK_GT(ranges(i + 1), ranges(i)); |
| 526 | static const double kTransitionWidth = 5; |
| 527 | double denominator = ranges(i + 1) - ranges(i); |
| 528 | if (denominator > kTransitionWidth) |
| 529 | denominator = kTransitionWidth; // Stop trying to normalize. |
| 530 | return current/denominator; |
| 531 | } |
| 532 | |
| 533 | void Histogram::ResetRangeChecksum() { |
| 534 | range_checksum_ = CalculateRangeChecksum(); |
| 535 | } |
| 536 | |
| 537 | const std::string Histogram::GetAsciiBucketRange(size_t i) const { |
| 538 | std::string result; |
| 539 | if (kHexRangePrintingFlag & flags_) |
| 540 | StringAppendF(&result, "%#x", ranges(i)); |
| 541 | else |
| 542 | StringAppendF(&result, "%d", ranges(i)); |
| 543 | return result; |
| 544 | } |
| 545 | |
| 546 | // Update histogram data with new sample. |
| 547 | void Histogram::Accumulate(Sample value, Count count, size_t index) { |
| 548 | // Note locking not done in this version!!! |
| 549 | sample_.Accumulate(value, count, index); |
| 550 | } |
| 551 | |
| 552 | void Histogram::SetBucketRange(size_t i, Sample value) { |
| 553 | DCHECK_GT(bucket_count_, i); |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 554 | DCHECK_GE(value, 0); |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 555 | cached_ranges_->SetBucketRange(i, value); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | bool Histogram::ValidateBucketRanges() const { |
| 559 | // Standard assertions that all bucket ranges should satisfy. |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 560 | DCHECK_EQ(bucket_count_ + 1, cached_ranges_->size()); |
| 561 | DCHECK_EQ(0, ranges(0)); |
| 562 | DCHECK_EQ(declared_min(), ranges(1)); |
| 563 | DCHECK_EQ(declared_max(), ranges(bucket_count_ - 1)); |
| 564 | DCHECK_EQ(kSampleType_MAX, ranges(bucket_count_)); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 568 | uint32 Histogram::CalculateRangeChecksum() const { |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 569 | DCHECK_EQ(cached_ranges_->size(), bucket_count() + 1); |
| 570 | // Seed checksum. |
| 571 | uint32 checksum = static_cast<uint32>(cached_ranges_->size()); |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 572 | for (size_t index = 0; index < bucket_count(); ++index) |
| 573 | checksum = Crc32(checksum, ranges(index)); |
| 574 | return checksum; |
| 575 | } |
| 576 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 577 | void Histogram::Initialize() { |
| 578 | sample_.Resize(*this); |
| 579 | if (declared_min_ < 1) |
| 580 | declared_min_ = 1; |
| 581 | if (declared_max_ > kSampleType_MAX - 1) |
| 582 | declared_max_ = kSampleType_MAX - 1; |
| 583 | DCHECK_LE(declared_min_, declared_max_); |
| 584 | DCHECK_GT(bucket_count_, 1u); |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 585 | CHECK_LT(bucket_count_, kBucketCount_MAX); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 586 | size_t maximal_bucket_count = declared_max_ - declared_min_ + 2; |
| 587 | DCHECK_LE(bucket_count_, maximal_bucket_count); |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 588 | DCHECK_EQ(0, ranges(0)); |
| 589 | cached_ranges_->SetBucketRange(bucket_count_, kSampleType_MAX); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 590 | } |
| 591 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 592 | // We generate the CRC-32 using the low order bits to select whether to XOR in |
| 593 | // the reversed polynomial 0xedb88320L. This is nice and simple, and allows us |
| 594 | // to keep the quotient in a uint32. Since we're not concerned about the nature |
| 595 | // of corruptions (i.e., we don't care about bit sequencing, since we are |
| 596 | // handling memory changes, which are more grotesque) so we don't bother to |
| 597 | // get the CRC correct for big-endian vs little-ending calculations. All we |
| 598 | // need is a nice hash, that tends to depend on all the bits of the sample, with |
| 599 | // very little chance of changes in one place impacting changes in another |
| 600 | // place. |
| 601 | uint32 Histogram::Crc32(uint32 sum, Histogram::Sample range) { |
| 602 | const bool kUseRealCrc = true; // TODO(jar): Switch to false and watch stats. |
| 603 | if (kUseRealCrc) { |
| 604 | union { |
| 605 | Histogram::Sample range; |
| 606 | unsigned char bytes[sizeof(Histogram::Sample)]; |
| 607 | } converter; |
| 608 | converter.range = range; |
| 609 | for (size_t i = 0; i < sizeof(converter); ++i) |
| 610 | sum = kCrcTable[(sum & 0xff) ^ converter.bytes[i]] ^ (sum >> 8); |
| 611 | } else { |
| 612 | // Use hash techniques provided in ReallyFastHash, except we don't care |
| 613 | // about "avalanching" (which would worsten the hash, and add collisions), |
| 614 | // and we don't care about edge cases since we have an even number of bytes. |
| 615 | union { |
| 616 | Histogram::Sample range; |
| 617 | uint16 ints[sizeof(Histogram::Sample) / 2]; |
| 618 | } converter; |
| 619 | DCHECK_EQ(sizeof(Histogram::Sample), sizeof(converter)); |
| 620 | converter.range = range; |
| 621 | sum += converter.ints[0]; |
| 622 | sum = (sum << 16) ^ sum ^ (static_cast<uint32>(converter.ints[1]) << 11); |
| 623 | sum += sum >> 11; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 624 | } |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 625 | return sum; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | //------------------------------------------------------------------------------ |
| 629 | // Private methods |
| 630 | |
| 631 | double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const { |
| 632 | double max = 0; |
| 633 | for (size_t i = 0; i < bucket_count() ; ++i) { |
| 634 | double current_size = GetBucketSize(snapshot.counts(i), i); |
| 635 | if (current_size > max) |
| 636 | max = current_size; |
| 637 | } |
| 638 | return max; |
| 639 | } |
| 640 | |
| 641 | void Histogram::WriteAsciiHeader(const SampleSet& snapshot, |
| 642 | Count sample_count, |
| 643 | std::string* output) const { |
| 644 | StringAppendF(output, |
| 645 | "Histogram: %s recorded %d samples", |
| 646 | histogram_name().c_str(), |
| 647 | sample_count); |
| 648 | if (0 == sample_count) { |
| 649 | DCHECK_EQ(snapshot.sum(), 0); |
| 650 | } else { |
| 651 | double average = static_cast<float>(snapshot.sum()) / sample_count; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 652 | |
[email protected] | 0704a97 | 2011-03-24 00:30:27 | [diff] [blame] | 653 | StringAppendF(output, ", average = %.1f", average); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 654 | } |
| 655 | if (flags_ & ~kHexRangePrintingFlag) |
| 656 | StringAppendF(output, " (flags = 0x%x)", flags_ & ~kHexRangePrintingFlag); |
| 657 | } |
| 658 | |
| 659 | void Histogram::WriteAsciiBucketContext(const int64 past, |
| 660 | const Count current, |
| 661 | const int64 remaining, |
| 662 | const size_t i, |
| 663 | std::string* output) const { |
| 664 | double scaled_sum = (past + current + remaining) / 100.0; |
| 665 | WriteAsciiBucketValue(current, scaled_sum, output); |
| 666 | if (0 < i) { |
| 667 | double percentage = past / scaled_sum; |
| 668 | StringAppendF(output, " {%3.1f%%}", percentage); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | void Histogram::WriteAsciiBucketValue(Count current, double scaled_sum, |
| 673 | std::string* output) const { |
| 674 | StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); |
| 675 | } |
| 676 | |
| 677 | void Histogram::WriteAsciiBucketGraph(double current_size, double max_size, |
| 678 | std::string* output) const { |
| 679 | const int k_line_length = 72; // Maximal horizontal width of graph. |
| 680 | int x_count = static_cast<int>(k_line_length * (current_size / max_size) |
| 681 | + 0.5); |
| 682 | int x_remainder = k_line_length - x_count; |
| 683 | |
| 684 | while (0 < x_count--) |
| 685 | output->append("-"); |
| 686 | output->append("O"); |
| 687 | while (0 < x_remainder--) |
| 688 | output->append(" "); |
| 689 | } |
| 690 | |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 691 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 692 | // Methods for the Histogram::SampleSet class |
| 693 | //------------------------------------------------------------------------------ |
| 694 | |
| 695 | Histogram::SampleSet::SampleSet() |
| 696 | : counts_(), |
| 697 | sum_(0), |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 698 | redundant_count_(0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 699 | } |
| 700 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 701 | Histogram::SampleSet::~SampleSet() { |
| 702 | } |
| 703 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 704 | void Histogram::SampleSet::Resize(const Histogram& histogram) { |
| 705 | counts_.resize(histogram.bucket_count(), 0); |
| 706 | } |
| 707 | |
| 708 | void Histogram::SampleSet::CheckSize(const Histogram& histogram) const { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 709 | DCHECK_EQ(histogram.bucket_count(), counts_.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
| 713 | void Histogram::SampleSet::Accumulate(Sample value, Count count, |
| 714 | size_t index) { |
| 715 | DCHECK(count == 1 || count == -1); |
| 716 | counts_[index] += count; |
| 717 | sum_ += count * value; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 718 | redundant_count_ += count; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 719 | DCHECK_GE(counts_[index], 0); |
| 720 | DCHECK_GE(sum_, 0); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 721 | DCHECK_GE(redundant_count_, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | Count Histogram::SampleSet::TotalCount() const { |
| 725 | Count total = 0; |
| 726 | for (Counts::const_iterator it = counts_.begin(); |
| 727 | it != counts_.end(); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 728 | ++it) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 729 | total += *it; |
| 730 | } |
| 731 | return total; |
| 732 | } |
| 733 | |
| 734 | void Histogram::SampleSet::Add(const SampleSet& other) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 735 | DCHECK_EQ(counts_.size(), other.counts_.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 736 | sum_ += other.sum_; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 737 | redundant_count_ += other.redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 738 | for (size_t index = 0; index < counts_.size(); ++index) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 739 | counts_[index] += other.counts_[index]; |
| 740 | } |
| 741 | |
| 742 | void Histogram::SampleSet::Subtract(const SampleSet& other) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 743 | DCHECK_EQ(counts_.size(), other.counts_.size()); |
[email protected] | 0704a97 | 2011-03-24 00:30:27 | [diff] [blame] | 744 | // Note: Race conditions in snapshotting a sum may lead to (temporary) |
| 745 | // negative values when snapshots are later combined (and deltas calculated). |
| 746 | // As a result, we don't currently CHCEK() for positive values. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 747 | sum_ -= other.sum_; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 748 | redundant_count_ -= other.redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 749 | for (size_t index = 0; index < counts_.size(); ++index) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 750 | counts_[index] -= other.counts_[index]; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 751 | DCHECK_GE(counts_[index], 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 755 | bool Histogram::SampleSet::Serialize(Pickle* pickle) const { |
| 756 | pickle->WriteInt64(sum_); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 757 | pickle->WriteInt64(redundant_count_); |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 758 | pickle->WriteUInt64(counts_.size()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 759 | |
| 760 | for (size_t index = 0; index < counts_.size(); ++index) { |
| 761 | pickle->WriteInt(counts_[index]); |
| 762 | } |
| 763 | |
| 764 | return true; |
| 765 | } |
| 766 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 767 | bool Histogram::SampleSet::Deserialize(PickleIterator* iter) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 768 | DCHECK_EQ(counts_.size(), 0u); |
| 769 | DCHECK_EQ(sum_, 0); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 770 | DCHECK_EQ(redundant_count_, 0); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 771 | |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 772 | uint64 counts_size; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 773 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 774 | if (!iter->ReadInt64(&sum_) || |
| 775 | !iter->ReadInt64(&redundant_count_) || |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 776 | !iter->ReadUInt64(&counts_size)) { |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 777 | return false; |
| 778 | } |
| 779 | |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 780 | if (counts_size == 0) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 781 | return false; |
| 782 | |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 783 | int count = 0; |
[email protected] | ee028ac | 2012-03-14 00:08:02 | [diff] [blame] | 784 | for (uint64 index = 0; index < counts_size; ++index) { |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 785 | int i; |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 786 | if (!iter->ReadInt(&i)) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 787 | return false; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 788 | counts_.push_back(i); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 789 | count += i; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 790 | } |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 791 | DCHECK_EQ(count, redundant_count_); |
| 792 | return count == redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 793 | } |
| 794 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 795 | //------------------------------------------------------------------------------ |
| 796 | // LinearHistogram: This histogram uses a traditional set of evenly spaced |
| 797 | // buckets. |
| 798 | //------------------------------------------------------------------------------ |
| 799 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 800 | LinearHistogram::~LinearHistogram() { |
| 801 | } |
| 802 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 803 | Histogram* LinearHistogram::FactoryGet(const std::string& name, |
| 804 | Sample minimum, |
| 805 | Sample maximum, |
| 806 | size_t bucket_count, |
| 807 | Flags flags) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 808 | if (minimum < 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 809 | minimum = 1; |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 810 | if (maximum > kSampleType_MAX - 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 811 | maximum = kSampleType_MAX - 1; |
| 812 | |
[email protected] | a93721e2 | 2012-01-06 02:13:28 | [diff] [blame] | 813 | DCHECK_GT(maximum, minimum); |
| 814 | DCHECK_GT((Sample) bucket_count, 2); |
| 815 | DCHECK_LE((Sample) bucket_count, maximum - minimum + 2); |
| 816 | |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame^] | 817 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 818 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 819 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 820 | LinearHistogram* tentative_histogram = |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 821 | new LinearHistogram(name, minimum, maximum, bucket_count); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 822 | tentative_histogram->InitializeBucketRange(); |
| 823 | tentative_histogram->SetFlags(flags); |
| 824 | histogram = |
| 825 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 826 | } |
| 827 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 828 | DCHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 829 | DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 830 | return histogram; |
| 831 | } |
| 832 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 833 | Histogram* LinearHistogram::FactoryTimeGet(const std::string& name, |
| 834 | TimeDelta minimum, |
| 835 | TimeDelta maximum, |
| 836 | size_t bucket_count, |
| 837 | Flags flags) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 838 | return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
| 839 | bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 840 | } |
| 841 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 842 | Histogram::ClassType LinearHistogram::histogram_type() const { |
| 843 | return LINEAR_HISTOGRAM; |
| 844 | } |
| 845 | |
| 846 | void LinearHistogram::SetRangeDescriptions( |
| 847 | const DescriptionPair descriptions[]) { |
| 848 | for (int i =0; descriptions[i].description; ++i) { |
| 849 | bucket_description_[descriptions[i].sample] = descriptions[i].description; |
| 850 | } |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 851 | } |
| 852 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 853 | LinearHistogram::LinearHistogram(const std::string& name, |
| 854 | Sample minimum, |
| 855 | Sample maximum, |
| 856 | size_t bucket_count) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 857 | : Histogram(name, minimum >= 1 ? minimum : 1, maximum, bucket_count) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 858 | } |
| 859 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 860 | LinearHistogram::LinearHistogram(const std::string& name, |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 861 | TimeDelta minimum, |
| 862 | TimeDelta maximum, |
| 863 | size_t bucket_count) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 864 | : Histogram(name, minimum >= TimeDelta::FromMilliseconds(1) ? |
| 865 | minimum : TimeDelta::FromMilliseconds(1), |
| 866 | maximum, bucket_count) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 867 | } |
| 868 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 869 | void LinearHistogram::InitializeBucketRange() { |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 870 | DCHECK_GT(declared_min(), 0); // 0 is the underflow bucket here. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 871 | double min = declared_min(); |
| 872 | double max = declared_max(); |
| 873 | size_t i; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 874 | for (i = 1; i < bucket_count(); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 875 | double linear_range = (min * (bucket_count() -1 - i) + max * (i - 1)) / |
| 876 | (bucket_count() - 2); |
| 877 | SetBucketRange(i, static_cast<int> (linear_range + 0.5)); |
| 878 | } |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 879 | ResetRangeChecksum(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 880 | } |
| 881 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 882 | double LinearHistogram::GetBucketSize(Count current, size_t i) const { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 883 | DCHECK_GT(ranges(i + 1), ranges(i)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 884 | // Adjacent buckets with different widths would have "surprisingly" many (few) |
| 885 | // samples in a histogram if we didn't normalize this way. |
| 886 | double denominator = ranges(i + 1) - ranges(i); |
| 887 | return current/denominator; |
| 888 | } |
| 889 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 890 | const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const { |
| 891 | int range = ranges(i); |
| 892 | BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
| 893 | if (it == bucket_description_.end()) |
| 894 | return Histogram::GetAsciiBucketRange(i); |
| 895 | return it->second; |
| 896 | } |
| 897 | |
| 898 | bool LinearHistogram::PrintEmptyBucket(size_t index) const { |
| 899 | return bucket_description_.find(ranges(index)) == bucket_description_.end(); |
| 900 | } |
| 901 | |
| 902 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 903 | //------------------------------------------------------------------------------ |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 904 | // This section provides implementation for BooleanHistogram. |
| 905 | //------------------------------------------------------------------------------ |
| 906 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 907 | Histogram* BooleanHistogram::FactoryGet(const std::string& name, Flags flags) { |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame^] | 908 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 909 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 910 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 911 | BooleanHistogram* tentative_histogram = new BooleanHistogram(name); |
| 912 | tentative_histogram->InitializeBucketRange(); |
| 913 | tentative_histogram->SetFlags(flags); |
| 914 | histogram = |
| 915 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 916 | } |
| 917 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 918 | DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 919 | return histogram; |
| 920 | } |
| 921 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 922 | Histogram::ClassType BooleanHistogram::histogram_type() const { |
| 923 | return BOOLEAN_HISTOGRAM; |
| 924 | } |
| 925 | |
| 926 | void BooleanHistogram::AddBoolean(bool value) { |
| 927 | Add(value ? 1 : 0); |
| 928 | } |
| 929 | |
| 930 | BooleanHistogram::BooleanHistogram(const std::string& name) |
| 931 | : LinearHistogram(name, 1, 2, 3) { |
| 932 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 933 | |
| 934 | //------------------------------------------------------------------------------ |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 935 | // CustomHistogram: |
| 936 | //------------------------------------------------------------------------------ |
| 937 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 938 | Histogram* CustomHistogram::FactoryGet(const std::string& name, |
| 939 | const std::vector<Sample>& custom_ranges, |
| 940 | Flags flags) { |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 941 | // Remove the duplicates in the custom ranges array. |
| 942 | std::vector<int> ranges = custom_ranges; |
| 943 | ranges.push_back(0); // Ensure we have a zero value. |
| 944 | std::sort(ranges.begin(), ranges.end()); |
| 945 | ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end()); |
| 946 | if (ranges.size() <= 1) { |
| 947 | DCHECK(false); |
| 948 | // Note that we pushed a 0 in above, so for defensive code.... |
| 949 | ranges.push_back(1); // Put in some data so we can index to [1]. |
| 950 | } |
| 951 | |
| 952 | DCHECK_LT(ranges.back(), kSampleType_MAX); |
| 953 | |
[email protected] | 993ae74 | 2012-07-18 18:06:30 | [diff] [blame^] | 954 | Histogram* histogram = StatisticsRecorder::FindHistogram(name); |
| 955 | if (!histogram) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 956 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 957 | CustomHistogram* tentative_histogram = new CustomHistogram(name, ranges); |
| 958 | tentative_histogram->InitializedCustomBucketRange(ranges); |
| 959 | tentative_histogram->SetFlags(flags); |
| 960 | histogram = |
| 961 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | DCHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM); |
| 965 | DCHECK(histogram->HasConstructorArguments(ranges[1], ranges.back(), |
| 966 | ranges.size())); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 967 | return histogram; |
| 968 | } |
| 969 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 970 | Histogram::ClassType CustomHistogram::histogram_type() const { |
| 971 | return CUSTOM_HISTOGRAM; |
| 972 | } |
| 973 | |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 974 | // static |
| 975 | std::vector<Histogram::Sample> CustomHistogram::ArrayToCustomRanges( |
| 976 | const Sample* values, size_t num_values) { |
| 977 | std::vector<Sample> all_values; |
| 978 | for (size_t i = 0; i < num_values; ++i) { |
| 979 | Sample value = values[i]; |
| 980 | all_values.push_back(value); |
| 981 | |
| 982 | // Ensure that a guard bucket is added. If we end up with duplicate |
| 983 | // values, FactoryGet will take care of removing them. |
| 984 | all_values.push_back(value + 1); |
| 985 | } |
| 986 | return all_values; |
| 987 | } |
| 988 | |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 989 | CustomHistogram::CustomHistogram(const std::string& name, |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 990 | const std::vector<Sample>& custom_ranges) |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 991 | : Histogram(name, custom_ranges[1], custom_ranges.back(), |
| 992 | custom_ranges.size()) { |
| 993 | DCHECK_GT(custom_ranges.size(), 1u); |
| 994 | DCHECK_EQ(custom_ranges[0], 0); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 995 | } |
| 996 | |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 997 | bool CustomHistogram::SerializeRanges(Pickle* pickle) const { |
| 998 | for (size_t i = 0; i < cached_ranges()->size(); ++i) { |
| 999 | if (!pickle->WriteInt(cached_ranges()->ranges(i))) |
| 1000 | return false; |
| 1001 | } |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | // static |
| 1006 | bool CustomHistogram::DeserializeRanges( |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1007 | PickleIterator* iter, std::vector<Histogram::Sample>* ranges) { |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 1008 | for (size_t i = 0; i < ranges->size(); ++i) { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1009 | if (!iter->ReadInt(&(*ranges)[i])) |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame] | 1010 | return false; |
| 1011 | } |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 1015 | void CustomHistogram::InitializedCustomBucketRange( |
| 1016 | const std::vector<Sample>& custom_ranges) { |
| 1017 | DCHECK_GT(custom_ranges.size(), 1u); |
| 1018 | DCHECK_EQ(custom_ranges[0], 0); |
| 1019 | DCHECK_LE(custom_ranges.size(), bucket_count()); |
| 1020 | for (size_t index = 0; index < custom_ranges.size(); ++index) |
| 1021 | SetBucketRange(index, custom_ranges[index]); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 1022 | ResetRangeChecksum(); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | double CustomHistogram::GetBucketSize(Count current, size_t i) const { |
| 1026 | return 1; |
| 1027 | } |
| 1028 | |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1029 | CachedRanges::CachedRanges(size_t bucket_count, int initial_value) |
| 1030 | : ranges_(bucket_count, initial_value), |
| 1031 | range_checksum_(0) { |
| 1032 | } |
| 1033 | |
| 1034 | CachedRanges::~CachedRanges() { |
| 1035 | } |
| 1036 | |
| 1037 | void CachedRanges::SetBucketRange(size_t i, Histogram::Sample value) { |
| 1038 | DCHECK_LT(i, ranges_.size()); |
| 1039 | DCHECK_GE(value, 0); |
| 1040 | ranges_[i] = value; |
| 1041 | } |
| 1042 | |
| 1043 | bool CachedRanges::Equals(CachedRanges* other) const { |
| 1044 | if (range_checksum_ != other->range_checksum_) |
| 1045 | return false; |
| 1046 | if (ranges_.size() != other->ranges_.size()) |
| 1047 | return false; |
| 1048 | for (size_t index = 0; index < ranges_.size(); ++index) { |
| 1049 | if (ranges_[index] != other->ranges_[index]) |
| 1050 | return false; |
| 1051 | } |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 1055 | } // namespace base |