[email protected] | 0704a97 | 2011-03-24 00:30:27 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 17 | #include "base/debug/leak_annotations.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | #include "base/logging.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] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 77 | // Collect the number of histograms created. |
| 78 | static uint32 number_of_histograms_ = 0; |
| 79 | // Collect the number of vectors saved because of caching ranges. |
| 80 | static uint32 number_of_vectors_saved_ = 0; |
| 81 | // Collect the number of ranges_ elements saved because of caching ranges. |
| 82 | static size_t saved_ranges_size_ = 0; |
| 83 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 84 | Histogram* Histogram::FactoryGet(const std::string& name, |
| 85 | Sample minimum, |
| 86 | Sample maximum, |
| 87 | size_t bucket_count, |
| 88 | Flags flags) { |
| 89 | Histogram* histogram(NULL); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 90 | |
| 91 | // Defensive code. |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 92 | if (minimum < 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 93 | minimum = 1; |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 94 | if (maximum > kSampleType_MAX - 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 95 | maximum = kSampleType_MAX - 1; |
| 96 | |
[email protected] | f2bc0cb5 | 2010-06-25 15:55:15 | [diff] [blame] | 97 | if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 98 | // Extra variable is not needed... but this keeps this section basically |
| 99 | // identical to other derived classes in this file (and compiler will |
| 100 | // optimize away the extra variable. |
| 101 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 102 | Histogram* tentative_histogram = |
| 103 | new Histogram(name, minimum, maximum, bucket_count); |
| 104 | tentative_histogram->InitializeBucketRange(); |
| 105 | tentative_histogram->SetFlags(flags); |
| 106 | histogram = |
| 107 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 110 | DCHECK_EQ(HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 111 | DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 112 | return histogram; |
| 113 | } |
| 114 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 115 | Histogram* Histogram::FactoryTimeGet(const std::string& name, |
| 116 | TimeDelta minimum, |
| 117 | TimeDelta maximum, |
| 118 | size_t bucket_count, |
| 119 | Flags flags) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 120 | return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
| 121 | bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 122 | } |
| 123 | |
[email protected] | b404b1f | 2011-10-16 07:29:17 | [diff] [blame] | 124 | TimeTicks Histogram::DebugNow() { |
| 125 | #ifndef NDEBUG |
| 126 | return TimeTicks::Now(); |
| 127 | #else |
| 128 | return TimeTicks(); |
| 129 | #endif |
| 130 | } |
| 131 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 132 | void Histogram::Add(int value) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 133 | if (value > kSampleType_MAX - 1) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | value = kSampleType_MAX - 1; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 135 | if (value < 0) |
| 136 | value = 0; |
| 137 | size_t index = BucketIndex(value); |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 138 | DCHECK_GE(value, ranges(index)); |
| 139 | DCHECK_LT(value, ranges(index + 1)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | Accumulate(value, 1, index); |
| 141 | } |
| 142 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 143 | void Histogram::AddBoolean(bool value) { |
| 144 | DCHECK(false); |
| 145 | } |
| 146 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 147 | void Histogram::AddSampleSet(const SampleSet& sample) { |
| 148 | sample_.Add(sample); |
| 149 | } |
| 150 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 151 | void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) { |
| 152 | DCHECK(false); |
| 153 | } |
| 154 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 155 | // The following methods provide a graphical histogram display. |
| 156 | void Histogram::WriteHTMLGraph(std::string* output) const { |
| 157 | // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. |
| 158 | output->append("<PRE>"); |
| 159 | WriteAscii(true, "<br>", output); |
| 160 | output->append("</PRE>"); |
| 161 | } |
| 162 | |
| 163 | void Histogram::WriteAscii(bool graph_it, const std::string& newline, |
| 164 | std::string* output) const { |
| 165 | // Get local (stack) copies of all effectively volatile class data so that we |
| 166 | // are consistent across our output activities. |
| 167 | SampleSet snapshot; |
| 168 | SnapshotSample(&snapshot); |
| 169 | Count sample_count = snapshot.TotalCount(); |
| 170 | |
| 171 | WriteAsciiHeader(snapshot, sample_count, output); |
| 172 | output->append(newline); |
| 173 | |
| 174 | // Prepare to normalize graphical rendering of bucket contents. |
| 175 | double max_size = 0; |
| 176 | if (graph_it) |
| 177 | max_size = GetPeakBucketSize(snapshot); |
| 178 | |
| 179 | // Calculate space needed to print bucket range numbers. Leave room to print |
| 180 | // nearly the largest bucket range without sliding over the histogram. |
[email protected] | e2951cf | 2008-09-24 23:51:25 | [diff] [blame] | 181 | size_t largest_non_empty_bucket = bucket_count() - 1; |
| 182 | while (0 == snapshot.counts(largest_non_empty_bucket)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 183 | if (0 == largest_non_empty_bucket) |
| 184 | break; // All buckets are empty. |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 185 | --largest_non_empty_bucket; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Calculate largest print width needed for any of our bucket range displays. |
| 189 | size_t print_width = 1; |
[email protected] | e2951cf | 2008-09-24 23:51:25 | [diff] [blame] | 190 | for (size_t i = 0; i < bucket_count(); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 191 | if (snapshot.counts(i)) { |
| 192 | size_t width = GetAsciiBucketRange(i).size() + 1; |
| 193 | if (width > print_width) |
| 194 | print_width = width; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | int64 remaining = sample_count; |
| 199 | int64 past = 0; |
| 200 | // Output the actual histogram graph. |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 201 | for (size_t i = 0; i < bucket_count(); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 202 | Count current = snapshot.counts(i); |
| 203 | if (!current && !PrintEmptyBucket(i)) |
| 204 | continue; |
| 205 | remaining -= current; |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 206 | std::string range = GetAsciiBucketRange(i); |
| 207 | output->append(range); |
| 208 | for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
| 209 | output->push_back(' '); |
[email protected] | e2951cf | 2008-09-24 23:51:25 | [diff] [blame] | 210 | if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) { |
| 211 | while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 212 | ++i; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | output->append("... "); |
| 214 | output->append(newline); |
| 215 | continue; // No reason to plot emptiness. |
| 216 | } |
| 217 | double current_size = GetBucketSize(current, i); |
| 218 | if (graph_it) |
| 219 | WriteAsciiBucketGraph(current_size, max_size, output); |
| 220 | WriteAsciiBucketContext(past, current, remaining, i, output); |
| 221 | output->append(newline); |
| 222 | past += current; |
| 223 | } |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 224 | DCHECK_EQ(sample_count, past); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 227 | // static |
| 228 | std::string Histogram::SerializeHistogramInfo(const Histogram& histogram, |
| 229 | const SampleSet& snapshot) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 230 | DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 231 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 232 | Pickle pickle; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 233 | pickle.WriteString(histogram.histogram_name()); |
| 234 | pickle.WriteInt(histogram.declared_min()); |
| 235 | pickle.WriteInt(histogram.declared_max()); |
| 236 | pickle.WriteSize(histogram.bucket_count()); |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 237 | pickle.WriteUInt32(histogram.range_checksum()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 238 | pickle.WriteInt(histogram.histogram_type()); |
[email protected] | 1f4fc8e8c | 2010-01-02 00:46:41 | [diff] [blame] | 239 | pickle.WriteInt(histogram.flags()); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 240 | |
| 241 | snapshot.Serialize(&pickle); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame^] | 242 | |
| 243 | histogram.SerializeRanges(&pickle); |
| 244 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 245 | return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
| 246 | } |
| 247 | |
| 248 | // static |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 249 | bool Histogram::DeserializeHistogramInfo(const std::string& histogram_info) { |
| 250 | if (histogram_info.empty()) { |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame^] | 251 | return false; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | Pickle pickle(histogram_info.data(), |
| 255 | static_cast<int>(histogram_info.size())); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 256 | std::string histogram_name; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 257 | int declared_min; |
| 258 | int declared_max; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 259 | size_t bucket_count; |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 260 | uint32 range_checksum; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 261 | int histogram_type; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 262 | int pickle_flags; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 263 | SampleSet sample; |
| 264 | |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 265 | void* iter = NULL; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 266 | if (!pickle.ReadString(&iter, &histogram_name) || |
| 267 | !pickle.ReadInt(&iter, &declared_min) || |
| 268 | !pickle.ReadInt(&iter, &declared_max) || |
| 269 | !pickle.ReadSize(&iter, &bucket_count) || |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 270 | !pickle.ReadUInt32(&iter, &range_checksum) || |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 271 | !pickle.ReadInt(&iter, &histogram_type) || |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 272 | !pickle.ReadInt(&iter, &pickle_flags) || |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 273 | !sample.Histogram::SampleSet::Deserialize(&iter, pickle)) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 274 | DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 275 | return false; |
| 276 | } |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame^] | 277 | |
[email protected] | 1f4fc8e8c | 2010-01-02 00:46:41 | [diff] [blame] | 278 | DCHECK(pickle_flags & kIPCSerializationSourceFlag); |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 279 | // Since these fields may have come from an untrusted renderer, do additional |
| 280 | // checks above and beyond those in Histogram::Initialize() |
| 281 | if (declared_max <= 0 || declared_min <= 0 || declared_max < declared_min || |
| 282 | INT_MAX / sizeof(Count) <= bucket_count || bucket_count < 2) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 283 | DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 287 | Flags flags = static_cast<Flags>(pickle_flags & ~kIPCSerializationSourceFlag); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 288 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 289 | DCHECK_NE(NOT_VALID_IN_RENDERER, histogram_type); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 290 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 291 | Histogram* render_histogram(NULL); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 292 | |
[email protected] | a764bf5e | 2010-06-02 21:31:44 | [diff] [blame] | 293 | if (histogram_type == HISTOGRAM) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 294 | render_histogram = Histogram::FactoryGet( |
| 295 | histogram_name, declared_min, declared_max, bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 296 | } else if (histogram_type == LINEAR_HISTOGRAM) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 297 | render_histogram = LinearHistogram::FactoryGet( |
| 298 | histogram_name, declared_min, declared_max, bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 299 | } else if (histogram_type == BOOLEAN_HISTOGRAM) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 300 | render_histogram = BooleanHistogram::FactoryGet(histogram_name, flags); |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame^] | 301 | } else if (histogram_type == CUSTOM_HISTOGRAM) { |
| 302 | std::vector<Histogram::Sample> sample_ranges(bucket_count); |
| 303 | if (!CustomHistogram::DeserializeRanges(&iter, pickle, &sample_ranges)) { |
| 304 | DLOG(ERROR) << "Pickle error decoding ranges: " << histogram_name; |
| 305 | return false; |
| 306 | } |
| 307 | render_histogram = |
| 308 | CustomHistogram::FactoryGet(histogram_name, sample_ranges, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 309 | } else { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 310 | DLOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: " |
| 311 | << histogram_type; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 312 | return false; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 315 | DCHECK_EQ(render_histogram->declared_min(), declared_min); |
| 316 | DCHECK_EQ(render_histogram->declared_max(), declared_max); |
| 317 | DCHECK_EQ(render_histogram->bucket_count(), bucket_count); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 318 | DCHECK_EQ(render_histogram->range_checksum(), range_checksum); |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 319 | DCHECK_EQ(render_histogram->histogram_type(), histogram_type); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 320 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 321 | if (render_histogram->flags() & kIPCSerializationSourceFlag) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 322 | DVLOG(1) << "Single process mode, histogram observed and not copied: " |
| 323 | << histogram_name; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 324 | } else { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 325 | DCHECK_EQ(flags & render_histogram->flags(), flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 326 | render_histogram->AddSampleSet(sample); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 332 | //------------------------------------------------------------------------------ |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 333 | // Methods for the validating a sample and a related histogram. |
| 334 | //------------------------------------------------------------------------------ |
| 335 | |
| 336 | Histogram::Inconsistencies Histogram::FindCorruption( |
| 337 | const SampleSet& snapshot) const { |
| 338 | int inconsistencies = NO_INCONSISTENCIES; |
| 339 | Sample previous_range = -1; // Bottom range is always 0. |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 340 | int64 count = 0; |
| 341 | for (size_t index = 0; index < bucket_count(); ++index) { |
| 342 | count += snapshot.counts(index); |
| 343 | int new_range = ranges(index); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 344 | if (previous_range >= new_range) |
| 345 | inconsistencies |= BUCKET_ORDER_ERROR; |
| 346 | previous_range = new_range; |
| 347 | } |
| 348 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 349 | if (!HasValidRangeChecksum()) |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 350 | inconsistencies |= RANGE_CHECKSUM_ERROR; |
| 351 | |
| 352 | int64 delta64 = snapshot.redundant_count() - count; |
| 353 | if (delta64 != 0) { |
| 354 | int delta = static_cast<int>(delta64); |
| 355 | if (delta != delta64) |
| 356 | delta = INT_MAX; // Flag all giant errors as INT_MAX. |
| 357 | // Since snapshots of histograms are taken asynchronously relative to |
| 358 | // sampling (and snapped from different threads), it is pretty likely that |
| 359 | // we'll catch a redundant count that doesn't match the sample count. We |
| 360 | // allow for a certain amount of slop before flagging this as an |
| 361 | // inconsistency. Even with an inconsistency, we'll snapshot it again (for |
| 362 | // UMA in about a half hour, so we'll eventually get the data, if it was |
| 363 | // not the result of a corruption. If histograms show that 1 is "too tight" |
| 364 | // then we may try to use 2 or 3 for this slop value. |
| 365 | const int kCommonRaceBasedCountMismatch = 1; |
| 366 | if (delta > 0) { |
| 367 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta); |
| 368 | if (delta > kCommonRaceBasedCountMismatch) |
| 369 | inconsistencies |= COUNT_HIGH_ERROR; |
| 370 | } else { |
| 371 | DCHECK_GT(0, delta); |
| 372 | UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta); |
| 373 | if (-delta > kCommonRaceBasedCountMismatch) |
| 374 | inconsistencies |= COUNT_LOW_ERROR; |
| 375 | } |
| 376 | } |
| 377 | return static_cast<Inconsistencies>(inconsistencies); |
| 378 | } |
| 379 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 380 | Histogram::ClassType Histogram::histogram_type() const { |
| 381 | return HISTOGRAM; |
| 382 | } |
| 383 | |
| 384 | Histogram::Sample Histogram::ranges(size_t i) const { |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 385 | return cached_ranges_->ranges(i); |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | size_t Histogram::bucket_count() const { |
| 389 | return bucket_count_; |
| 390 | } |
| 391 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 392 | // Do a safe atomic snapshot of sample data. |
| 393 | // This implementation assumes we are on a safe single thread. |
| 394 | void Histogram::SnapshotSample(SampleSet* sample) const { |
| 395 | // Note locking not done in this version!!! |
| 396 | *sample = sample_; |
| 397 | } |
| 398 | |
| 399 | bool Histogram::HasConstructorArguments(Sample minimum, |
| 400 | Sample maximum, |
| 401 | size_t bucket_count) { |
| 402 | return ((minimum == declared_min_) && (maximum == declared_max_) && |
| 403 | (bucket_count == bucket_count_)); |
| 404 | } |
| 405 | |
| 406 | bool Histogram::HasConstructorTimeDeltaArguments(TimeDelta minimum, |
| 407 | TimeDelta maximum, |
| 408 | size_t bucket_count) { |
| 409 | return ((minimum.InMilliseconds() == declared_min_) && |
| 410 | (maximum.InMilliseconds() == declared_max_) && |
| 411 | (bucket_count == bucket_count_)); |
| 412 | } |
| 413 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 414 | bool Histogram::HasValidRangeChecksum() const { |
| 415 | return CalculateRangeChecksum() == range_checksum_; |
| 416 | } |
| 417 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 418 | Histogram::Histogram(const std::string& name, Sample minimum, |
| 419 | Sample maximum, size_t bucket_count) |
| 420 | : histogram_name_(name), |
| 421 | declared_min_(minimum), |
| 422 | declared_max_(maximum), |
| 423 | bucket_count_(bucket_count), |
| 424 | flags_(kNoFlags), |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 425 | cached_ranges_(new CachedRanges(bucket_count + 1, 0)), |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 426 | range_checksum_(0), |
| 427 | sample_() { |
| 428 | Initialize(); |
| 429 | } |
| 430 | |
| 431 | Histogram::Histogram(const std::string& name, TimeDelta minimum, |
| 432 | TimeDelta maximum, size_t bucket_count) |
| 433 | : histogram_name_(name), |
| 434 | declared_min_(static_cast<int> (minimum.InMilliseconds())), |
| 435 | declared_max_(static_cast<int> (maximum.InMilliseconds())), |
| 436 | bucket_count_(bucket_count), |
| 437 | flags_(kNoFlags), |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 438 | cached_ranges_(new CachedRanges(bucket_count + 1, 0)), |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 439 | range_checksum_(0), |
| 440 | sample_() { |
| 441 | Initialize(); |
| 442 | } |
| 443 | |
| 444 | Histogram::~Histogram() { |
| 445 | if (StatisticsRecorder::dump_on_exit()) { |
| 446 | std::string output; |
| 447 | WriteAscii(true, "\n", &output); |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 448 | DLOG(INFO) << output; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Just to make sure most derived class did this properly... |
| 452 | DCHECK(ValidateBucketRanges()); |
[email protected] | 7767bdb | 2011-03-02 23:38:00 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame^] | 455 | bool Histogram::SerializeRanges(Pickle* pickle) const { |
| 456 | return true; |
| 457 | } |
| 458 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 459 | // Calculate what range of values are held in each bucket. |
| 460 | // We have to be careful that we don't pick a ratio between starting points in |
| 461 | // consecutive buckets that is sooo small, that the integer bounds are the same |
| 462 | // (effectively making one bucket get no values). We need to avoid: |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 463 | // ranges(i) == ranges(i + 1) |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 464 | // To avoid that, we just do a fine-grained bucket width as far as we need to |
| 465 | // until we get a ratio that moves us along at least 2 units at a time. From |
| 466 | // that bucket onward we do use the exponential growth of buckets. |
| 467 | void Histogram::InitializeBucketRange() { |
| 468 | double log_max = log(static_cast<double>(declared_max())); |
| 469 | double log_ratio; |
| 470 | double log_next; |
| 471 | size_t bucket_index = 1; |
| 472 | Sample current = declared_min(); |
| 473 | SetBucketRange(bucket_index, current); |
| 474 | while (bucket_count() > ++bucket_index) { |
| 475 | double log_current; |
| 476 | log_current = log(static_cast<double>(current)); |
| 477 | // Calculate the count'th root of the range. |
| 478 | log_ratio = (log_max - log_current) / (bucket_count() - bucket_index); |
| 479 | // See where the next bucket would start. |
| 480 | log_next = log_current + log_ratio; |
| 481 | int next; |
| 482 | next = static_cast<int>(floor(exp(log_next) + 0.5)); |
| 483 | if (next > current) |
| 484 | current = next; |
| 485 | else |
| 486 | ++current; // Just do a narrow bucket, and keep trying. |
| 487 | SetBucketRange(bucket_index, current); |
| 488 | } |
| 489 | ResetRangeChecksum(); |
| 490 | |
| 491 | DCHECK_EQ(bucket_count(), bucket_index); |
| 492 | } |
| 493 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 494 | bool Histogram::PrintEmptyBucket(size_t index) const { |
| 495 | return true; |
| 496 | } |
| 497 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 498 | size_t Histogram::BucketIndex(Sample value) const { |
| 499 | // Use simple binary search. This is very general, but there are better |
| 500 | // approaches if we knew that the buckets were linearly distributed. |
| 501 | DCHECK_LE(ranges(0), value); |
| 502 | DCHECK_GT(ranges(bucket_count()), value); |
| 503 | size_t under = 0; |
| 504 | size_t over = bucket_count(); |
| 505 | size_t mid; |
| 506 | |
| 507 | do { |
| 508 | DCHECK_GE(over, under); |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 509 | mid = under + (over - under)/2; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 510 | if (mid == under) |
| 511 | break; |
| 512 | if (ranges(mid) <= value) |
| 513 | under = mid; |
| 514 | else |
| 515 | over = mid; |
| 516 | } while (true); |
| 517 | |
| 518 | DCHECK_LE(ranges(mid), value); |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 519 | CHECK_GT(ranges(mid+1), value); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 520 | return mid; |
| 521 | } |
| 522 | |
| 523 | // Use the actual bucket widths (like a linear histogram) until the widths get |
| 524 | // over some transition value, and then use that transition width. Exponentials |
| 525 | // get so big so fast (and we don't expect to see a lot of entries in the large |
| 526 | // buckets), so we need this to make it possible to see what is going on and |
| 527 | // not have 0-graphical-height buckets. |
| 528 | double Histogram::GetBucketSize(Count current, size_t i) const { |
| 529 | DCHECK_GT(ranges(i + 1), ranges(i)); |
| 530 | static const double kTransitionWidth = 5; |
| 531 | double denominator = ranges(i + 1) - ranges(i); |
| 532 | if (denominator > kTransitionWidth) |
| 533 | denominator = kTransitionWidth; // Stop trying to normalize. |
| 534 | return current/denominator; |
| 535 | } |
| 536 | |
| 537 | void Histogram::ResetRangeChecksum() { |
| 538 | range_checksum_ = CalculateRangeChecksum(); |
| 539 | } |
| 540 | |
| 541 | const std::string Histogram::GetAsciiBucketRange(size_t i) const { |
| 542 | std::string result; |
| 543 | if (kHexRangePrintingFlag & flags_) |
| 544 | StringAppendF(&result, "%#x", ranges(i)); |
| 545 | else |
| 546 | StringAppendF(&result, "%d", ranges(i)); |
| 547 | return result; |
| 548 | } |
| 549 | |
| 550 | // Update histogram data with new sample. |
| 551 | void Histogram::Accumulate(Sample value, Count count, size_t index) { |
| 552 | // Note locking not done in this version!!! |
| 553 | sample_.Accumulate(value, count, index); |
| 554 | } |
| 555 | |
| 556 | void Histogram::SetBucketRange(size_t i, Sample value) { |
| 557 | DCHECK_GT(bucket_count_, i); |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 558 | DCHECK_GE(value, 0); |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 559 | cached_ranges_->SetBucketRange(i, value); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | bool Histogram::ValidateBucketRanges() const { |
| 563 | // Standard assertions that all bucket ranges should satisfy. |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 564 | DCHECK_EQ(bucket_count_ + 1, cached_ranges_->size()); |
| 565 | DCHECK_EQ(0, ranges(0)); |
| 566 | DCHECK_EQ(declared_min(), ranges(1)); |
| 567 | DCHECK_EQ(declared_max(), ranges(bucket_count_ - 1)); |
| 568 | DCHECK_EQ(kSampleType_MAX, ranges(bucket_count_)); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 569 | return true; |
| 570 | } |
| 571 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 572 | uint32 Histogram::CalculateRangeChecksum() const { |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 573 | DCHECK_EQ(cached_ranges_->size(), bucket_count() + 1); |
| 574 | // Seed checksum. |
| 575 | uint32 checksum = static_cast<uint32>(cached_ranges_->size()); |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 576 | for (size_t index = 0; index < bucket_count(); ++index) |
| 577 | checksum = Crc32(checksum, ranges(index)); |
| 578 | return checksum; |
| 579 | } |
| 580 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 581 | void Histogram::Initialize() { |
| 582 | sample_.Resize(*this); |
| 583 | if (declared_min_ < 1) |
| 584 | declared_min_ = 1; |
| 585 | if (declared_max_ > kSampleType_MAX - 1) |
| 586 | declared_max_ = kSampleType_MAX - 1; |
| 587 | DCHECK_LE(declared_min_, declared_max_); |
| 588 | DCHECK_GT(bucket_count_, 1u); |
[email protected] | b122c0c | 2011-02-23 22:31:18 | [diff] [blame] | 589 | CHECK_LT(bucket_count_, kBucketCount_MAX); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 590 | size_t maximal_bucket_count = declared_max_ - declared_min_ + 2; |
| 591 | DCHECK_LE(bucket_count_, maximal_bucket_count); |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 592 | DCHECK_EQ(0, ranges(0)); |
| 593 | cached_ranges_->SetBucketRange(bucket_count_, kSampleType_MAX); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 594 | } |
| 595 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 596 | // We generate the CRC-32 using the low order bits to select whether to XOR in |
| 597 | // the reversed polynomial 0xedb88320L. This is nice and simple, and allows us |
| 598 | // to keep the quotient in a uint32. Since we're not concerned about the nature |
| 599 | // of corruptions (i.e., we don't care about bit sequencing, since we are |
| 600 | // handling memory changes, which are more grotesque) so we don't bother to |
| 601 | // get the CRC correct for big-endian vs little-ending calculations. All we |
| 602 | // need is a nice hash, that tends to depend on all the bits of the sample, with |
| 603 | // very little chance of changes in one place impacting changes in another |
| 604 | // place. |
| 605 | uint32 Histogram::Crc32(uint32 sum, Histogram::Sample range) { |
| 606 | const bool kUseRealCrc = true; // TODO(jar): Switch to false and watch stats. |
| 607 | if (kUseRealCrc) { |
| 608 | union { |
| 609 | Histogram::Sample range; |
| 610 | unsigned char bytes[sizeof(Histogram::Sample)]; |
| 611 | } converter; |
| 612 | converter.range = range; |
| 613 | for (size_t i = 0; i < sizeof(converter); ++i) |
| 614 | sum = kCrcTable[(sum & 0xff) ^ converter.bytes[i]] ^ (sum >> 8); |
| 615 | } else { |
| 616 | // Use hash techniques provided in ReallyFastHash, except we don't care |
| 617 | // about "avalanching" (which would worsten the hash, and add collisions), |
| 618 | // and we don't care about edge cases since we have an even number of bytes. |
| 619 | union { |
| 620 | Histogram::Sample range; |
| 621 | uint16 ints[sizeof(Histogram::Sample) / 2]; |
| 622 | } converter; |
| 623 | DCHECK_EQ(sizeof(Histogram::Sample), sizeof(converter)); |
| 624 | converter.range = range; |
| 625 | sum += converter.ints[0]; |
| 626 | sum = (sum << 16) ^ sum ^ (static_cast<uint32>(converter.ints[1]) << 11); |
| 627 | sum += sum >> 11; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 628 | } |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 629 | return sum; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | //------------------------------------------------------------------------------ |
| 633 | // Private methods |
| 634 | |
| 635 | double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const { |
| 636 | double max = 0; |
| 637 | for (size_t i = 0; i < bucket_count() ; ++i) { |
| 638 | double current_size = GetBucketSize(snapshot.counts(i), i); |
| 639 | if (current_size > max) |
| 640 | max = current_size; |
| 641 | } |
| 642 | return max; |
| 643 | } |
| 644 | |
| 645 | void Histogram::WriteAsciiHeader(const SampleSet& snapshot, |
| 646 | Count sample_count, |
| 647 | std::string* output) const { |
| 648 | StringAppendF(output, |
| 649 | "Histogram: %s recorded %d samples", |
| 650 | histogram_name().c_str(), |
| 651 | sample_count); |
| 652 | if (0 == sample_count) { |
| 653 | DCHECK_EQ(snapshot.sum(), 0); |
| 654 | } else { |
| 655 | double average = static_cast<float>(snapshot.sum()) / sample_count; |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 656 | |
[email protected] | 0704a97 | 2011-03-24 00:30:27 | [diff] [blame] | 657 | StringAppendF(output, ", average = %.1f", average); |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 658 | } |
| 659 | if (flags_ & ~kHexRangePrintingFlag) |
| 660 | StringAppendF(output, " (flags = 0x%x)", flags_ & ~kHexRangePrintingFlag); |
| 661 | } |
| 662 | |
| 663 | void Histogram::WriteAsciiBucketContext(const int64 past, |
| 664 | const Count current, |
| 665 | const int64 remaining, |
| 666 | const size_t i, |
| 667 | std::string* output) const { |
| 668 | double scaled_sum = (past + current + remaining) / 100.0; |
| 669 | WriteAsciiBucketValue(current, scaled_sum, output); |
| 670 | if (0 < i) { |
| 671 | double percentage = past / scaled_sum; |
| 672 | StringAppendF(output, " {%3.1f%%}", percentage); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | void Histogram::WriteAsciiBucketValue(Count current, double scaled_sum, |
| 677 | std::string* output) const { |
| 678 | StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); |
| 679 | } |
| 680 | |
| 681 | void Histogram::WriteAsciiBucketGraph(double current_size, double max_size, |
| 682 | std::string* output) const { |
| 683 | const int k_line_length = 72; // Maximal horizontal width of graph. |
| 684 | int x_count = static_cast<int>(k_line_length * (current_size / max_size) |
| 685 | + 0.5); |
| 686 | int x_remainder = k_line_length - x_count; |
| 687 | |
| 688 | while (0 < x_count--) |
| 689 | output->append("-"); |
| 690 | output->append("O"); |
| 691 | while (0 < x_remainder--) |
| 692 | output->append(" "); |
| 693 | } |
| 694 | |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 695 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 696 | // Methods for the Histogram::SampleSet class |
| 697 | //------------------------------------------------------------------------------ |
| 698 | |
| 699 | Histogram::SampleSet::SampleSet() |
| 700 | : counts_(), |
| 701 | sum_(0), |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 702 | redundant_count_(0) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 703 | } |
| 704 | |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 705 | Histogram::SampleSet::~SampleSet() { |
| 706 | } |
| 707 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 708 | void Histogram::SampleSet::Resize(const Histogram& histogram) { |
| 709 | counts_.resize(histogram.bucket_count(), 0); |
| 710 | } |
| 711 | |
| 712 | void Histogram::SampleSet::CheckSize(const Histogram& histogram) const { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 713 | DCHECK_EQ(histogram.bucket_count(), counts_.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | |
| 717 | void Histogram::SampleSet::Accumulate(Sample value, Count count, |
| 718 | size_t index) { |
| 719 | DCHECK(count == 1 || count == -1); |
| 720 | counts_[index] += count; |
| 721 | sum_ += count * value; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 722 | redundant_count_ += count; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 723 | DCHECK_GE(counts_[index], 0); |
| 724 | DCHECK_GE(sum_, 0); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 725 | DCHECK_GE(redundant_count_, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | Count Histogram::SampleSet::TotalCount() const { |
| 729 | Count total = 0; |
| 730 | for (Counts::const_iterator it = counts_.begin(); |
| 731 | it != counts_.end(); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 732 | ++it) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 733 | total += *it; |
| 734 | } |
| 735 | return total; |
| 736 | } |
| 737 | |
| 738 | void Histogram::SampleSet::Add(const SampleSet& other) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 739 | DCHECK_EQ(counts_.size(), other.counts_.size()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 740 | sum_ += other.sum_; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 741 | redundant_count_ += other.redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 742 | for (size_t index = 0; index < counts_.size(); ++index) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 743 | counts_[index] += other.counts_[index]; |
| 744 | } |
| 745 | |
| 746 | void Histogram::SampleSet::Subtract(const SampleSet& other) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 747 | DCHECK_EQ(counts_.size(), other.counts_.size()); |
[email protected] | 0704a97 | 2011-03-24 00:30:27 | [diff] [blame] | 748 | // Note: Race conditions in snapshotting a sum may lead to (temporary) |
| 749 | // negative values when snapshots are later combined (and deltas calculated). |
| 750 | // As a result, we don't currently CHCEK() for positive values. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 751 | sum_ -= other.sum_; |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 752 | redundant_count_ -= other.redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 753 | for (size_t index = 0; index < counts_.size(); ++index) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 754 | counts_[index] -= other.counts_[index]; |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 755 | DCHECK_GE(counts_[index], 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 759 | bool Histogram::SampleSet::Serialize(Pickle* pickle) const { |
| 760 | pickle->WriteInt64(sum_); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 761 | pickle->WriteInt64(redundant_count_); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 762 | pickle->WriteSize(counts_.size()); |
| 763 | |
| 764 | for (size_t index = 0; index < counts_.size(); ++index) { |
| 765 | pickle->WriteInt(counts_[index]); |
| 766 | } |
| 767 | |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | bool Histogram::SampleSet::Deserialize(void** iter, const Pickle& pickle) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 772 | DCHECK_EQ(counts_.size(), 0u); |
| 773 | DCHECK_EQ(sum_, 0); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 774 | DCHECK_EQ(redundant_count_, 0); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 775 | |
| 776 | size_t counts_size; |
| 777 | |
| 778 | if (!pickle.ReadInt64(iter, &sum_) || |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 779 | !pickle.ReadInt64(iter, &redundant_count_) || |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 780 | !pickle.ReadSize(iter, &counts_size)) { |
| 781 | return false; |
| 782 | } |
| 783 | |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 784 | if (counts_size == 0) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 785 | return false; |
| 786 | |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 787 | int count = 0; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 788 | for (size_t index = 0; index < counts_size; ++index) { |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 789 | int i; |
| 790 | if (!pickle.ReadInt(iter, &i)) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 791 | return false; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 792 | counts_.push_back(i); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 793 | count += i; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 794 | } |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 795 | DCHECK_EQ(count, redundant_count_); |
| 796 | return count == redundant_count_; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 797 | } |
| 798 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 799 | //------------------------------------------------------------------------------ |
| 800 | // LinearHistogram: This histogram uses a traditional set of evenly spaced |
| 801 | // buckets. |
| 802 | //------------------------------------------------------------------------------ |
| 803 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 804 | LinearHistogram::~LinearHistogram() { |
| 805 | } |
| 806 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 807 | Histogram* LinearHistogram::FactoryGet(const std::string& name, |
| 808 | Sample minimum, |
| 809 | Sample maximum, |
| 810 | size_t bucket_count, |
| 811 | Flags flags) { |
| 812 | Histogram* histogram(NULL); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 813 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 814 | if (minimum < 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 815 | minimum = 1; |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 816 | if (maximum > kSampleType_MAX - 1) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 817 | maximum = kSampleType_MAX - 1; |
| 818 | |
[email protected] | f2bc0cb5 | 2010-06-25 15:55:15 | [diff] [blame] | 819 | if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 820 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 821 | LinearHistogram* tentative_histogram = |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 822 | new LinearHistogram(name, minimum, maximum, bucket_count); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 823 | tentative_histogram->InitializeBucketRange(); |
| 824 | tentative_histogram->SetFlags(flags); |
| 825 | histogram = |
| 826 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 827 | } |
| 828 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 829 | DCHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 830 | DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 831 | return histogram; |
| 832 | } |
| 833 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 834 | Histogram* LinearHistogram::FactoryTimeGet(const std::string& name, |
| 835 | TimeDelta minimum, |
| 836 | TimeDelta maximum, |
| 837 | size_t bucket_count, |
| 838 | Flags flags) { |
[email protected] | 2753b39 | 2009-12-28 06:59:52 | [diff] [blame] | 839 | return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
| 840 | bucket_count, flags); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 841 | } |
| 842 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 843 | Histogram::ClassType LinearHistogram::histogram_type() const { |
| 844 | return LINEAR_HISTOGRAM; |
| 845 | } |
| 846 | |
| 847 | void LinearHistogram::SetRangeDescriptions( |
| 848 | const DescriptionPair descriptions[]) { |
| 849 | for (int i =0; descriptions[i].description; ++i) { |
| 850 | bucket_description_[descriptions[i].sample] = descriptions[i].description; |
| 851 | } |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 852 | } |
| 853 | |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 854 | LinearHistogram::LinearHistogram(const std::string& name, |
| 855 | Sample minimum, |
| 856 | Sample maximum, |
| 857 | size_t bucket_count) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 858 | : Histogram(name, minimum >= 1 ? minimum : 1, maximum, bucket_count) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 859 | } |
| 860 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 861 | LinearHistogram::LinearHistogram(const std::string& name, |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 862 | TimeDelta minimum, |
| 863 | TimeDelta maximum, |
| 864 | size_t bucket_count) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 865 | : Histogram(name, minimum >= TimeDelta::FromMilliseconds(1) ? |
| 866 | minimum : TimeDelta::FromMilliseconds(1), |
| 867 | maximum, bucket_count) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 868 | } |
| 869 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 870 | void LinearHistogram::InitializeBucketRange() { |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 871 | DCHECK_GT(declared_min(), 0); // 0 is the underflow bucket here. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 872 | double min = declared_min(); |
| 873 | double max = declared_max(); |
| 874 | size_t i; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 875 | for (i = 1; i < bucket_count(); ++i) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 876 | double linear_range = (min * (bucket_count() -1 - i) + max * (i - 1)) / |
| 877 | (bucket_count() - 2); |
| 878 | SetBucketRange(i, static_cast<int> (linear_range + 0.5)); |
| 879 | } |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 880 | ResetRangeChecksum(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 881 | } |
| 882 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 883 | double LinearHistogram::GetBucketSize(Count current, size_t i) const { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 884 | DCHECK_GT(ranges(i + 1), ranges(i)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 885 | // Adjacent buckets with different widths would have "surprisingly" many (few) |
| 886 | // samples in a histogram if we didn't normalize this way. |
| 887 | double denominator = ranges(i + 1) - ranges(i); |
| 888 | return current/denominator; |
| 889 | } |
| 890 | |
[email protected] | b7d0820 | 2011-01-25 17:29:39 | [diff] [blame] | 891 | const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const { |
| 892 | int range = ranges(i); |
| 893 | BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
| 894 | if (it == bucket_description_.end()) |
| 895 | return Histogram::GetAsciiBucketRange(i); |
| 896 | return it->second; |
| 897 | } |
| 898 | |
| 899 | bool LinearHistogram::PrintEmptyBucket(size_t index) const { |
| 900 | return bucket_description_.find(ranges(index)) == bucket_description_.end(); |
| 901 | } |
| 902 | |
| 903 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 904 | //------------------------------------------------------------------------------ |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 905 | // This section provides implementation for BooleanHistogram. |
| 906 | //------------------------------------------------------------------------------ |
| 907 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 908 | Histogram* BooleanHistogram::FactoryGet(const std::string& name, Flags flags) { |
| 909 | Histogram* histogram(NULL); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 910 | |
[email protected] | f2bc0cb5 | 2010-06-25 15:55:15 | [diff] [blame] | 911 | if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 912 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 913 | BooleanHistogram* tentative_histogram = new BooleanHistogram(name); |
| 914 | tentative_histogram->InitializeBucketRange(); |
| 915 | tentative_histogram->SetFlags(flags); |
| 916 | histogram = |
| 917 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 918 | } |
| 919 | |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 920 | DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type()); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 921 | return histogram; |
| 922 | } |
| 923 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 924 | Histogram::ClassType BooleanHistogram::histogram_type() const { |
| 925 | return BOOLEAN_HISTOGRAM; |
| 926 | } |
| 927 | |
| 928 | void BooleanHistogram::AddBoolean(bool value) { |
| 929 | Add(value ? 1 : 0); |
| 930 | } |
| 931 | |
| 932 | BooleanHistogram::BooleanHistogram(const std::string& name) |
| 933 | : LinearHistogram(name, 1, 2, 3) { |
| 934 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 935 | |
| 936 | //------------------------------------------------------------------------------ |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 937 | // CustomHistogram: |
| 938 | //------------------------------------------------------------------------------ |
| 939 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 940 | Histogram* CustomHistogram::FactoryGet(const std::string& name, |
| 941 | const std::vector<Sample>& custom_ranges, |
| 942 | Flags flags) { |
| 943 | Histogram* histogram(NULL); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 944 | |
| 945 | // Remove the duplicates in the custom ranges array. |
| 946 | std::vector<int> ranges = custom_ranges; |
| 947 | ranges.push_back(0); // Ensure we have a zero value. |
| 948 | std::sort(ranges.begin(), ranges.end()); |
| 949 | ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end()); |
| 950 | if (ranges.size() <= 1) { |
| 951 | DCHECK(false); |
| 952 | // Note that we pushed a 0 in above, so for defensive code.... |
| 953 | ranges.push_back(1); // Put in some data so we can index to [1]. |
| 954 | } |
| 955 | |
| 956 | DCHECK_LT(ranges.back(), kSampleType_MAX); |
| 957 | |
[email protected] | f2bc0cb5 | 2010-06-25 15:55:15 | [diff] [blame] | 958 | if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 959 | // To avoid racy destruction at shutdown, the following will be leaked. |
| 960 | CustomHistogram* tentative_histogram = new CustomHistogram(name, ranges); |
| 961 | tentative_histogram->InitializedCustomBucketRange(ranges); |
| 962 | tentative_histogram->SetFlags(flags); |
| 963 | histogram = |
| 964 | StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | DCHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM); |
| 968 | DCHECK(histogram->HasConstructorArguments(ranges[1], ranges.back(), |
| 969 | ranges.size())); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 970 | return histogram; |
| 971 | } |
| 972 | |
[email protected] | 5d91c9e | 2010-07-28 17:25:28 | [diff] [blame] | 973 | Histogram::ClassType CustomHistogram::histogram_type() const { |
| 974 | return CUSTOM_HISTOGRAM; |
| 975 | } |
| 976 | |
[email protected] | 961fefb | 2011-05-24 13:59:58 | [diff] [blame] | 977 | // static |
| 978 | std::vector<Histogram::Sample> CustomHistogram::ArrayToCustomRanges( |
| 979 | const Sample* values, size_t num_values) { |
| 980 | std::vector<Sample> all_values; |
| 981 | for (size_t i = 0; i < num_values; ++i) { |
| 982 | Sample value = values[i]; |
| 983 | all_values.push_back(value); |
| 984 | |
| 985 | // Ensure that a guard bucket is added. If we end up with duplicate |
| 986 | // values, FactoryGet will take care of removing them. |
| 987 | all_values.push_back(value + 1); |
| 988 | } |
| 989 | return all_values; |
| 990 | } |
| 991 | |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 992 | CustomHistogram::CustomHistogram(const std::string& name, |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 993 | const std::vector<Sample>& custom_ranges) |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 994 | : Histogram(name, custom_ranges[1], custom_ranges.back(), |
| 995 | custom_ranges.size()) { |
| 996 | DCHECK_GT(custom_ranges.size(), 1u); |
| 997 | DCHECK_EQ(custom_ranges[0], 0); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 998 | } |
| 999 | |
[email protected] | cd56dff | 2011-11-13 04:19:15 | [diff] [blame^] | 1000 | bool CustomHistogram::SerializeRanges(Pickle* pickle) const { |
| 1001 | for (size_t i = 0; i < cached_ranges()->size(); ++i) { |
| 1002 | if (!pickle->WriteInt(cached_ranges()->ranges(i))) |
| 1003 | return false; |
| 1004 | } |
| 1005 | return true; |
| 1006 | } |
| 1007 | |
| 1008 | // static |
| 1009 | bool CustomHistogram::DeserializeRanges( |
| 1010 | void** iter, const Pickle& pickle, std::vector<Histogram::Sample>* ranges) { |
| 1011 | for (size_t i = 0; i < ranges->size(); ++i) { |
| 1012 | if (!pickle.ReadInt(iter, &(*ranges)[i])) |
| 1013 | return false; |
| 1014 | } |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 1018 | void CustomHistogram::InitializedCustomBucketRange( |
| 1019 | const std::vector<Sample>& custom_ranges) { |
| 1020 | DCHECK_GT(custom_ranges.size(), 1u); |
| 1021 | DCHECK_EQ(custom_ranges[0], 0); |
| 1022 | DCHECK_LE(custom_ranges.size(), bucket_count()); |
| 1023 | for (size_t index = 0; index < custom_ranges.size(); ++index) |
| 1024 | SetBucketRange(index, custom_ranges[index]); |
[email protected] | 93a41d7 | 2010-11-03 23:36:24 | [diff] [blame] | 1025 | ResetRangeChecksum(); |
[email protected] | 70cc56e4 | 2010-04-29 22:39:55 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | double CustomHistogram::GetBucketSize(Count current, size_t i) const { |
| 1029 | return 1; |
| 1030 | } |
| 1031 | |
| 1032 | //------------------------------------------------------------------------------ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1033 | // The next section handles global (central) support for all histograms, as well |
| 1034 | // as startup/teardown of this service. |
| 1035 | //------------------------------------------------------------------------------ |
| 1036 | |
| 1037 | // This singleton instance should be started during the single threaded portion |
| 1038 | // of main(), and hence it is not thread safe. It initializes globals to |
| 1039 | // provide support for all future calls. |
| 1040 | StatisticsRecorder::StatisticsRecorder() { |
| 1041 | DCHECK(!histograms_); |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1042 | if (lock_ == NULL) { |
| 1043 | // This will leak on purpose. It's the only way to make sure we won't race |
| 1044 | // against the static uninitialization of the module while one of our |
| 1045 | // static methods relying on the lock get called at an inappropriate time |
| 1046 | // during the termination phase. Since it's a static data member, we will |
| 1047 | // leak one per process, which would be similar to the instance allocated |
| 1048 | // during static initialization and released only on process termination. |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1049 | lock_ = new base::Lock; |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1050 | } |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1051 | base::AutoLock auto_lock(*lock_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1052 | histograms_ = new HistogramMap; |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1053 | ranges_ = new RangesMap; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | StatisticsRecorder::~StatisticsRecorder() { |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1057 | DCHECK(histograms_ && lock_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1058 | |
| 1059 | if (dump_on_exit_) { |
| 1060 | std::string output; |
| 1061 | WriteGraph("", &output); |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 1062 | DLOG(INFO) << output; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1063 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1064 | // Clean up. |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1065 | HistogramMap* histograms = NULL; |
| 1066 | { |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1067 | base::AutoLock auto_lock(*lock_); |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1068 | histograms = histograms_; |
| 1069 | histograms_ = NULL; |
| 1070 | } |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1071 | RangesMap* ranges = NULL; |
| 1072 | { |
| 1073 | base::AutoLock auto_lock(*lock_); |
| 1074 | ranges = ranges_; |
| 1075 | ranges_ = NULL; |
| 1076 | } |
| 1077 | // We are going to leak the histograms and the ranges. |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1078 | delete histograms; |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1079 | delete ranges; |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1080 | // We don't delete lock_ on purpose to avoid having to properly protect |
| 1081 | // against it going away after we checked for NULL in the static methods. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | // static |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1085 | bool StatisticsRecorder::IsActive() { |
| 1086 | if (lock_ == NULL) |
| 1087 | return false; |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1088 | base::AutoLock auto_lock(*lock_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1089 | return NULL != histograms_; |
| 1090 | } |
| 1091 | |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1092 | Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) { |
[email protected] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 1093 | // As per crbug.com/79322 the histograms are intentionally leaked, so we need |
| 1094 | // to annotate them. Because ANNOTATE_LEAKING_OBJECT_PTR may be used only once |
| 1095 | // for an object, the duplicates should not be annotated. |
| 1096 | // Callers are responsible for not calling RegisterOrDeleteDuplicate(ptr) |
| 1097 | // twice if (lock_ == NULL) || (!histograms_). |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1098 | DCHECK(histogram->HasValidRangeChecksum()); |
[email protected] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 1099 | if (lock_ == NULL) { |
| 1100 | ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1101 | return histogram; |
[email protected] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 1102 | } |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1103 | base::AutoLock auto_lock(*lock_); |
[email protected] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 1104 | if (!histograms_) { |
| 1105 | ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1106 | return histogram; |
[email protected] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 1107 | } |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1108 | const std::string name = histogram->histogram_name(); |
[email protected] | 98359ce | 2011-03-05 06:22:24 | [diff] [blame] | 1109 | HistogramMap::iterator it = histograms_->find(name); |
[email protected] | cc82864b | 2010-08-17 19:46:51 | [diff] [blame] | 1110 | // Avoid overwriting a previous registration. |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1111 | if (histograms_->end() == it) { |
| 1112 | (*histograms_)[name] = histogram; |
[email protected] | 9b3bbd6 | 2011-05-31 07:37:09 | [diff] [blame] | 1113 | ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1114 | RegisterOrDeleteDuplicateRanges(histogram); |
| 1115 | ++number_of_histograms_; |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1116 | } else { |
| 1117 | delete histogram; // We already have one by this name. |
| 1118 | histogram = it->second; |
| 1119 | } |
| 1120 | return histogram; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | // static |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1124 | void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) { |
| 1125 | DCHECK(histogram); |
| 1126 | CachedRanges* histogram_ranges = histogram->cached_ranges(); |
| 1127 | DCHECK(histogram_ranges); |
| 1128 | uint32 checksum = histogram->range_checksum(); |
| 1129 | histogram_ranges->SetRangeChecksum(checksum); |
| 1130 | |
| 1131 | RangesMap::iterator ranges_it = ranges_->find(checksum); |
| 1132 | if (ranges_->end() == ranges_it) { |
| 1133 | // Register the new CachedRanges. |
| 1134 | std::list<CachedRanges*>* checksum_matching_list( |
| 1135 | new std::list<CachedRanges*>()); |
| 1136 | checksum_matching_list->push_front(histogram_ranges); |
| 1137 | (*ranges_)[checksum] = checksum_matching_list; |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | // Use the registered CachedRanges if the registered CachedRanges has same |
| 1142 | // ranges_ as |histogram|'s CachedRanges. |
| 1143 | std::list<CachedRanges*>* checksum_matching_list = ranges_it->second; |
| 1144 | std::list<CachedRanges*>::iterator checksum_matching_list_it; |
| 1145 | for (checksum_matching_list_it = checksum_matching_list->begin(); |
| 1146 | checksum_matching_list_it != checksum_matching_list->end(); |
| 1147 | ++checksum_matching_list_it) { |
| 1148 | CachedRanges* existing_histogram_ranges = *checksum_matching_list_it; |
| 1149 | DCHECK(existing_histogram_ranges); |
| 1150 | if (existing_histogram_ranges->Equals(histogram_ranges)) { |
| 1151 | histogram->set_cached_ranges(existing_histogram_ranges); |
| 1152 | ++number_of_vectors_saved_; |
| 1153 | saved_ranges_size_ += histogram_ranges->size(); |
| 1154 | delete histogram_ranges; |
| 1155 | return; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // We haven't found a CachedRanges which has the same ranges. Register the |
| 1160 | // new CachedRanges. |
| 1161 | DCHECK(checksum_matching_list_it == checksum_matching_list->end()); |
| 1162 | checksum_matching_list->push_front(histogram_ranges); |
| 1163 | } |
| 1164 | |
| 1165 | // static |
| 1166 | void StatisticsRecorder::CollectHistogramStats(const std::string& suffix) { |
| 1167 | static int uma_upload_attempt = 0; |
| 1168 | ++uma_upload_attempt; |
| 1169 | if (uma_upload_attempt == 1) { |
| 1170 | UMA_HISTOGRAM_COUNTS_10000( |
| 1171 | "Histogram.SharedRange.Count.FirstUpload." + suffix, |
| 1172 | number_of_histograms_); |
| 1173 | UMA_HISTOGRAM_COUNTS_10000( |
| 1174 | "Histogram.SharedRange.RangesSaved.FirstUpload." + suffix, |
| 1175 | number_of_vectors_saved_); |
| 1176 | UMA_HISTOGRAM_COUNTS( |
| 1177 | "Histogram.SharedRange.ElementsSaved.FirstUpload." + suffix, |
| 1178 | static_cast<int>(saved_ranges_size_)); |
| 1179 | number_of_histograms_ = 0; |
| 1180 | number_of_vectors_saved_ = 0; |
| 1181 | saved_ranges_size_ = 0; |
| 1182 | return; |
| 1183 | } |
| 1184 | if (uma_upload_attempt == 2) { |
| 1185 | UMA_HISTOGRAM_COUNTS_10000( |
| 1186 | "Histogram.SharedRange.Count.SecondUpload." + suffix, |
| 1187 | number_of_histograms_); |
| 1188 | UMA_HISTOGRAM_COUNTS_10000( |
| 1189 | "Histogram.SharedRange.RangesSaved.SecondUpload." + suffix, |
| 1190 | number_of_vectors_saved_); |
| 1191 | UMA_HISTOGRAM_COUNTS( |
| 1192 | "Histogram.SharedRange.ElementsSaved.SecondUpload." + suffix, |
| 1193 | static_cast<int>(saved_ranges_size_)); |
| 1194 | number_of_histograms_ = 0; |
| 1195 | number_of_vectors_saved_ = 0; |
| 1196 | saved_ranges_size_ = 0; |
| 1197 | return; |
| 1198 | } |
| 1199 | UMA_HISTOGRAM_COUNTS_10000( |
| 1200 | "Histogram.SharedRange.Count.RestOfUploads." + suffix, |
| 1201 | number_of_histograms_); |
| 1202 | UMA_HISTOGRAM_COUNTS_10000( |
| 1203 | "Histogram.SharedRange.RangesSaved.RestOfUploads." + suffix, |
| 1204 | number_of_vectors_saved_); |
| 1205 | UMA_HISTOGRAM_COUNTS( |
| 1206 | "Histogram.SharedRange.ElementsSaved.RestOfUploads." + suffix, |
| 1207 | static_cast<int>(saved_ranges_size_)); |
| 1208 | } |
| 1209 | |
| 1210 | // static |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1211 | void StatisticsRecorder::WriteHTMLGraph(const std::string& query, |
| 1212 | std::string* output) { |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1213 | if (!IsActive()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1214 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1215 | |
| 1216 | Histograms snapshot; |
| 1217 | GetSnapshot(query, &snapshot); |
| 1218 | for (Histograms::iterator it = snapshot.begin(); |
| 1219 | it != snapshot.end(); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1220 | ++it) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1221 | (*it)->WriteHTMLGraph(output); |
| 1222 | output->append("<br><hr><br>"); |
| 1223 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | // static |
| 1227 | void StatisticsRecorder::WriteGraph(const std::string& query, |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1228 | std::string* output) { |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1229 | if (!IsActive()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1230 | return; |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 1231 | if (query.length()) |
| 1232 | StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); |
| 1233 | else |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1234 | output->append("Collections of all histograms\n"); |
| 1235 | |
| 1236 | Histograms snapshot; |
| 1237 | GetSnapshot(query, &snapshot); |
| 1238 | for (Histograms::iterator it = snapshot.begin(); |
| 1239 | it != snapshot.end(); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1240 | ++it) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1241 | (*it)->WriteAscii(true, "\n", output); |
| 1242 | output->append("\n"); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | // static |
| 1247 | void StatisticsRecorder::GetHistograms(Histograms* output) { |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1248 | if (lock_ == NULL) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1249 | return; |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1250 | base::AutoLock auto_lock(*lock_); |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1251 | if (!histograms_) |
| 1252 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1253 | for (HistogramMap::iterator it = histograms_->begin(); |
| 1254 | histograms_->end() != it; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1255 | ++it) { |
[email protected] | 2ef3748f | 2010-10-19 17:33:28 | [diff] [blame] | 1256 | DCHECK_EQ(it->first, it->second->histogram_name()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1257 | output->push_back(it->second); |
| 1258 | } |
| 1259 | } |
| 1260 | |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1261 | bool StatisticsRecorder::FindHistogram(const std::string& name, |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 1262 | Histogram** histogram) { |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1263 | if (lock_ == NULL) |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1264 | return false; |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1265 | base::AutoLock auto_lock(*lock_); |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1266 | if (!histograms_) |
| 1267 | return false; |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 1268 | HistogramMap::iterator it = histograms_->find(name); |
| 1269 | if (histograms_->end() == it) |
| 1270 | return false; |
| 1271 | *histogram = it->second; |
| 1272 | return true; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1273 | } |
| 1274 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1275 | // private static |
| 1276 | void StatisticsRecorder::GetSnapshot(const std::string& query, |
| 1277 | Histograms* snapshot) { |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1278 | if (lock_ == NULL) |
| 1279 | return; |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1280 | base::AutoLock auto_lock(*lock_); |
[email protected] | d1442554 | 2010-12-23 14:40:10 | [diff] [blame] | 1281 | if (!histograms_) |
| 1282 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1283 | for (HistogramMap::iterator it = histograms_->begin(); |
| 1284 | histograms_->end() != it; |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1285 | ++it) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1286 | if (it->first.find(query) != std::string::npos) |
| 1287 | snapshot->push_back(it->second); |
| 1288 | } |
| 1289 | } |
| 1290 | |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1291 | CachedRanges::CachedRanges(size_t bucket_count, int initial_value) |
| 1292 | : ranges_(bucket_count, initial_value), |
| 1293 | range_checksum_(0) { |
| 1294 | } |
| 1295 | |
| 1296 | CachedRanges::~CachedRanges() { |
| 1297 | } |
| 1298 | |
| 1299 | void CachedRanges::SetBucketRange(size_t i, Histogram::Sample value) { |
| 1300 | DCHECK_LT(i, ranges_.size()); |
| 1301 | DCHECK_GE(value, 0); |
| 1302 | ranges_[i] = value; |
| 1303 | } |
| 1304 | |
| 1305 | bool CachedRanges::Equals(CachedRanges* other) const { |
| 1306 | if (range_checksum_ != other->range_checksum_) |
| 1307 | return false; |
| 1308 | if (ranges_.size() != other->ranges_.size()) |
| 1309 | return false; |
| 1310 | for (size_t index = 0; index < ranges_.size(); ++index) { |
| 1311 | if (ranges_[index] != other->ranges_[index]) |
| 1312 | return false; |
| 1313 | } |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1317 | // static |
| 1318 | StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
| 1319 | // static |
[email protected] | 908de52 | 2011-10-20 00:55:00 | [diff] [blame] | 1320 | StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
| 1321 | // static |
[email protected] | bc581a68 | 2011-01-01 23:16:20 | [diff] [blame] | 1322 | base::Lock* StatisticsRecorder::lock_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1323 | // static |
| 1324 | bool StatisticsRecorder::dump_on_exit_ = false; |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 1325 | } // namespace base |