license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [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 | |
| 9 | // It supports calls to accumulate either time intervals (which are processed |
| 10 | // as integral number of milliseconds), or arbitrary integral units. |
| 11 | |
| 12 | // The default layout of buckets is exponential. For example, buckets might |
| 13 | // contain (sequentially) the count of values in the following intervals: |
| 14 | // [0,1), [1,2), [2,4), [4,8), [8,16), [16,32), [32,64), [64,infinity) |
| 15 | // That bucket allocation would actually result from construction of a histogram |
| 16 | // for values between 1 and 64, with 8 buckets, such as: |
| 17 | // Histogram count(L"some name", 1, 64, 8); |
| 18 | // Note that the underflow bucket [0,1) and the overflow bucket [64,infinity) |
| 19 | // are not counted by the constructor in the user supplied "bucket_count" |
| 20 | // argument. |
| 21 | // The above example has an exponential ratio of 2 (doubling the bucket width |
| 22 | // in each consecutive bucket. The Histogram class automatically calculates |
| 23 | // the smallest ratio that it can use to construct the number of buckets |
| 24 | // selected in the constructor. An another example, if you had 50 buckets, |
| 25 | // and millisecond time values from 1 to 10000, then the ratio between |
| 26 | // consecutive bucket widths will be approximately somewhere around the 50th |
| 27 | // root of 10000. This approach provides very fine grain (narrow) buckets |
| 28 | // at the low end of the histogram scale, but allows the histogram to cover a |
| 29 | // gigantic range with the addition of very few buckets. |
| 30 | |
| 31 | #ifndef BASE_HISTOGRAM_H__ |
| 32 | #define BASE_HISTOGRAM_H__ |
| 33 | |
| 34 | #include <map> |
| 35 | #include <string> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include "base/lock.h" |
| 39 | #include "base/scoped_ptr.h" |
| 40 | #include "base/stats_counters.h" |
| 41 | |
| 42 | //------------------------------------------------------------------------------ |
| 43 | // Provide easy general purpose histogram in a macro, just like stats counters. |
| 44 | // These macros all use 50 buckets. |
| 45 | |
| 46 | #define HISTOGRAM_TIMES(name, sample) do { \ |
| 47 | static Histogram counter((name), TimeDelta::FromMilliseconds(1), \ |
| 48 | TimeDelta::FromSeconds(10), 50); \ |
| 49 | counter.AddTime(sample); \ |
| 50 | } while (0) |
| 51 | |
| 52 | #define HISTOGRAM_COUNTS(name, sample) do { \ |
| 53 | static Histogram counter((name), 1, 1000000, 50); \ |
| 54 | counter.Add(sample); \ |
| 55 | } while (0) |
| 56 | |
| 57 | //------------------------------------------------------------------------------ |
| 58 | // This macro set is for a histogram that can support both addition and removal |
| 59 | // of samples. It should be used to render the accumulated asset allocation |
| 60 | // of some samples. For example, it can sample memory allocation sizes, and |
| 61 | // memory releases (as negative samples). |
| 62 | // To simplify the interface, only non-zero values can be sampled, with positive |
| 63 | // numbers indicating addition, and negative numbers implying dimunition |
| 64 | // (removal). |
| 65 | // Note that the underlying ThreadSafeHistogram() uses locking to ensure that |
| 66 | // counts are precise (no chance of losing an addition or removal event, due to |
| 67 | // multithread racing). This precision is required to prevent missed-counts from |
| 68 | // resulting in drift, as the calls to Remove() for a given value should always |
| 69 | // be equal in number or fewer than the corresponding calls to Add(). |
| 70 | |
| 71 | #define ASSET_HISTOGRAM_COUNTS(name, sample) do { \ |
| 72 | static ThreadSafeHistogram counter((name), 1, 1000000, 50); \ |
| 73 | if (0 == sample) break; \ |
| 74 | if (sample >= 0) \ |
| 75 | counter.Add(sample); \ |
| 76 | else\ |
| 77 | counter.Remove(-sample); \ |
| 78 | } while (0) |
| 79 | |
| 80 | //------------------------------------------------------------------------------ |
| 81 | // Define Debug vs non-debug flavors of macros. |
| 82 | #ifndef NDEBUG |
| 83 | |
| 84 | #define DHISTOGRAM_TIMES(name, sample) HISTOGRAM_TIMES(name, sample) |
| 85 | #define DHISTOGRAM_COUNTS(name, sample) HISTOGRAM_COUNTS(name, sample) |
| 86 | #define DASSET_HISTOGRAM_COUNTS(name, sample) ASSET_HISTOGRAM_COUNTS(name, \ |
| 87 | sample) |
| 88 | |
| 89 | #else // NDEBUG |
| 90 | |
| 91 | #define DHISTOGRAM_TIMES(name, sample) do {} while (0) |
| 92 | #define DHISTOGRAM_COUNTS(name, sample) do {} while (0) |
| 93 | #define DASSET_HISTOGRAM_COUNTS(name, sample) do {} while (0) |
| 94 | |
| 95 | #endif // NDEBUG |
| 96 | |
| 97 | //------------------------------------------------------------------------------ |
| 98 | // The following macros provide typical usage scenarios for callers that wish |
| 99 | // to record histogram data, and have the data submitted/uploaded via UMA. |
| 100 | // Not all systems support such UMA, but if they do, the following macros |
| 101 | // should work with the service. |
| 102 | |
| 103 | static const int kUmaTargetedHistogramFlag = 0x1; |
| 104 | |
| 105 | #define UMA_HISTOGRAM_TIMES(name, sample) do { \ |
| 106 | static Histogram counter((name), TimeDelta::FromMilliseconds(1), \ |
| 107 | TimeDelta::FromSeconds(10), 50); \ |
| 108 | counter.SetFlags(kUmaTargetedHistogramFlag); \ |
| 109 | counter.AddTime(sample); \ |
| 110 | } while (0) |
| 111 | |
| 112 | // Use this macro when times can routinely be much longer than 10 seconds. |
| 113 | #define UMA_HISTOGRAM_LONG_TIMES(name, sample) do { \ |
| 114 | static Histogram counter((name), TimeDelta::FromMilliseconds(1), \ |
| 115 | TimeDelta::FromHours(1), 50); \ |
| 116 | counter.SetFlags(kUmaTargetedHistogramFlag); \ |
| 117 | counter.AddTime(sample); \ |
| 118 | } while (0) |
| 119 | |
| 120 | #define UMA_HISTOGRAM_COUNTS(name, sample) do { \ |
| 121 | static Histogram counter((name), 1, 1000000, 50); \ |
| 122 | counter.SetFlags(kUmaTargetedHistogramFlag); \ |
| 123 | counter.Add(sample); \ |
| 124 | } while (0) |
| 125 | |
| 126 | #define UMA_HISTOGRAM_COUNTS_100(name, sample) do { \ |
| 127 | static Histogram counter((name), 1, 100, 50); \ |
| 128 | counter.SetFlags(kUmaTargetedHistogramFlag); \ |
| 129 | counter.Add(sample); \ |
| 130 | } while (0) |
| 131 | |
| 132 | #define UMA_HISTOGRAM_MEMORY_KB(name, sample) do { \ |
| 133 | static Histogram counter((name), 1000, 500000, 50); \ |
| 134 | counter.SetFlags(kUmaTargetedHistogramFlag); \ |
| 135 | counter.Add(sample); \ |
| 136 | } while (0) |
| 137 | |
| 138 | #define UMA_HISTOGRAM_MEMORY_MB(name, sample) do { \ |
| 139 | static Histogram counter((name), 1, 1000, 50); \ |
| 140 | counter.SetFlags(kUmaTargetedHistogramFlag); \ |
| 141 | counter.Add(sample); \ |
| 142 | } while (0) |
| 143 | |
| 144 | //------------------------------------------------------------------------------ |
| 145 | |
| 146 | class Histogram : public StatsRate { |
| 147 | public: |
| 148 | typedef int Sample; // Used for samples (and ranges of samples). |
| 149 | typedef int Count; // Used to count samples in a bucket. |
| 150 | static const Sample kSampleType_MAX = INT_MAX; |
| 151 | |
| 152 | typedef std::vector<Count> Counts; |
deanm@google.com | 1818847 | 2008-08-12 03:57:54 +0900 | [diff] [blame] | 153 | typedef std::vector<Sample> Ranges; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 154 | |
darin@google.com | 12d40bb | 2008-08-20 03:36:23 +0900 | [diff] [blame] | 155 | static const int kHexRangePrintingFlag; |
| 156 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 157 | //---------------------------------------------------------------------------- |
| 158 | // Statistic values, developed over the life of the histogram. |
| 159 | |
| 160 | class SampleSet { |
| 161 | public: |
| 162 | explicit SampleSet(); |
| 163 | // Adjust size of counts_ for use with given histogram. |
| 164 | void Resize(const Histogram& histogram); |
| 165 | void CheckSize(const Histogram& histogram) const; |
| 166 | |
| 167 | // Accessor for histogram to make routine additions. |
| 168 | void Accumulate(Sample value, Count count, size_t index); |
| 169 | |
| 170 | // Accessor methods. |
| 171 | Count counts(size_t i) const { return counts_[i]; } |
| 172 | Count TotalCount() const ; |
| 173 | int64 sum() const { return sum_; } |
| 174 | int64 square_sum() const { return square_sum_; } |
| 175 | |
| 176 | // Arithmetic manipulation of corresponding elements of the set. |
| 177 | void Add(const SampleSet& other); |
| 178 | void Subtract(const SampleSet& other); |
| 179 | |
| 180 | private: |
| 181 | // Actual histogram data is stored in buckets, showing the count of values |
| 182 | // that fit into each bucket. |
| 183 | Counts counts_; |
| 184 | |
| 185 | // Save simple stats locally. Note that this MIGHT get done in base class |
| 186 | // without shared memory at some point. |
| 187 | int64 sum_; // sum of samples. |
| 188 | int64 square_sum_; // sum of squares of samples. |
| 189 | }; |
| 190 | //---------------------------------------------------------------------------- |
| 191 | |
| 192 | Histogram(const wchar_t* name, Sample minimum, |
| 193 | Sample maximum, size_t bucket_count); |
| 194 | Histogram(const wchar_t* name, TimeDelta minimum, |
| 195 | TimeDelta maximum, size_t bucket_count); |
| 196 | ~Histogram(); |
| 197 | |
| 198 | // Hooks to override stats counter methods. This ensures that we gather all |
| 199 | // input the stats counter sees. |
| 200 | virtual void Add(int value); |
| 201 | |
| 202 | // The following methods provide a graphical histogram displays. |
| 203 | void WriteHTMLGraph(std::string* output) const; |
| 204 | void WriteAscii(bool graph_it, const std::string& newline, |
| 205 | std::string* output) const; |
| 206 | |
| 207 | // Support generic flagging of Histograms. |
| 208 | // 0x1 Currently used to mark this histogram to be recorded by UMA.. |
| 209 | // 0x8000 means print ranges in hex. |
| 210 | void SetFlags(int flags) { flags_ |= flags; } |
| 211 | int flags() const { return flags_; } |
| 212 | |
| 213 | //---------------------------------------------------------------------------- |
| 214 | // Accessors for serialization and testing. |
| 215 | //---------------------------------------------------------------------------- |
| 216 | const std::string histogram_name() const { return histogram_name_; } |
| 217 | Sample declared_min() const { return declared_min_; } |
| 218 | Sample declared_max() const { return declared_max_; } |
| 219 | Sample ranges(size_t i) const { return ranges_[i];} |
| 220 | size_t bucket_count() const { return bucket_count_; } |
| 221 | // Snapshot the current complete set of sample data. |
| 222 | // Override with atomic/locked snapshot if needed. |
| 223 | virtual void SnapshotSample(SampleSet* sample) const; |
| 224 | |
| 225 | protected: |
| 226 | // Method to override to skip the display of the i'th bucket if it's empty. |
| 227 | virtual bool PrintEmptyBucket(size_t index) const { return true; } |
| 228 | |
| 229 | //---------------------------------------------------------------------------- |
| 230 | // Methods to override to create histogram with different bucket widths. |
| 231 | //---------------------------------------------------------------------------- |
| 232 | // Initialize ranges_ mapping. |
| 233 | virtual void InitializeBucketRange(); |
| 234 | // Find bucket to increment for sample value. |
| 235 | virtual size_t BucketIndex(Sample value) const; |
| 236 | // Get normalized size, relative to the ranges_[i]. |
| 237 | virtual double GetBucketSize(Count current, size_t i) const; |
| 238 | |
| 239 | // Return a string description of what goes in a given bucket. |
| 240 | // Most commonly this is the numeric value, but in derived classes it may |
| 241 | // be a name (or string description) given to the bucket. |
| 242 | virtual const std::string GetAsciiBucketRange(size_t it) const; |
| 243 | |
| 244 | //---------------------------------------------------------------------------- |
| 245 | // Methods to override to create thread safe histogram. |
| 246 | //---------------------------------------------------------------------------- |
| 247 | // Update all our internal data, including histogram |
| 248 | virtual void Accumulate(Sample value, Count count, size_t index); |
| 249 | |
| 250 | //---------------------------------------------------------------------------- |
| 251 | // Accessors for derived classes. |
| 252 | //---------------------------------------------------------------------------- |
| 253 | void SetBucketRange(size_t i, Sample value); |
| 254 | |
| 255 | // Validate that ranges_ was created sensibly (top and bottom range |
| 256 | // values relate properly to the declared_min_ and declared_max_).. |
| 257 | bool ValidateBucketRanges() const; |
| 258 | |
| 259 | private: |
| 260 | // Post constructor initialization. |
| 261 | void Initialize(); |
| 262 | |
| 263 | //---------------------------------------------------------------------------- |
| 264 | // Helpers for emitting Ascii graphic. Each method appends data to output. |
| 265 | |
| 266 | // Find out how large the (graphically) the largest bucket will appear to be. |
| 267 | double GetPeakBucketSize(const SampleSet& snapshot) const; |
| 268 | |
| 269 | // Write a common header message describing this histogram. |
| 270 | void WriteAsciiHeader(const SampleSet& snapshot, |
| 271 | Count sample_count, std::string* output) const ; |
| 272 | |
| 273 | // Write information about previous, current, and next buckets. |
| 274 | // Information such as cumulative percentage, etc. |
| 275 | void WriteAsciiBucketContext(const int64 past, const Count current, |
| 276 | const int64 remaining, const size_t i, |
| 277 | std::string* output) const; |
| 278 | |
| 279 | // Write textual description of the bucket contents (relative to histogram). |
| 280 | // Output is the count in the buckets, as well as the percentage. |
| 281 | void WriteAsciiBucketValue(Count current, double scaled_sum, |
| 282 | std::string* output) const; |
| 283 | |
| 284 | // Produce actual graph (set of blank vs non blank char's) for a bucket. |
| 285 | void WriteAsciiBucketGraph(double current_size, double max_size, |
| 286 | std::string* output) const; |
| 287 | |
| 288 | //---------------------------------------------------------------------------- |
| 289 | // Invariant values set at/near construction time |
| 290 | |
| 291 | // ASCII version of original name given to the constructor. All identically |
| 292 | // named instances will be coalesced cross-project TODO(jar). |
| 293 | // If a user needs one histogram name to be called by several places in a |
| 294 | // single process, a central function should be defined by teh user, which |
| 295 | // defins the single declared instance of the named histogram. |
| 296 | const std::string histogram_name_; |
| 297 | Sample declared_min_; // Less than this goes into counts_[0] |
| 298 | Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1]. |
| 299 | size_t bucket_count_; // Dimension of counts_[]. |
| 300 | |
| 301 | // Flag the histogram for recording by UMA via metric_services.h. |
| 302 | int flags_; |
| 303 | |
| 304 | // For each index, show the least value that can be stored in the |
| 305 | // corresponding bucket. We also append one extra element in this array, |
| 306 | // containing kSampleType_MAX, to make calculations easy. |
| 307 | // The dimension of ranges_ is bucket_count + 1. |
| 308 | Ranges ranges_; |
| 309 | |
| 310 | // Finally, provide the state that changes with the addition of each new |
| 311 | // sample. |
| 312 | SampleSet sample_; |
| 313 | |
| 314 | // Indicate if successfully registered. |
| 315 | bool registered_; |
| 316 | |
| 317 | DISALLOW_EVIL_CONSTRUCTORS(Histogram); |
| 318 | }; |
| 319 | |
| 320 | //------------------------------------------------------------------------------ |
| 321 | |
| 322 | // LinearHistogram is a more traditional histogram, with evenly spaced |
| 323 | // buckets. |
| 324 | class LinearHistogram : public Histogram { |
| 325 | public: |
| 326 | struct DescriptionPair { |
| 327 | Sample sample; |
evanm@google.com | 1b2d3e0 | 2008-08-21 07:56:30 +0900 | [diff] [blame] | 328 | const char* description; // Null means end of a list of pairs. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 329 | }; |
| 330 | LinearHistogram(const wchar_t* name, Sample minimum, |
| 331 | Sample maximum, size_t bucket_count); |
| 332 | LinearHistogram(const wchar_t* name, TimeDelta minimum, |
| 333 | TimeDelta maximum, size_t bucket_count); |
| 334 | ~LinearHistogram() {} |
| 335 | |
| 336 | // Store a list of number/text values for use in rendering the histogram. |
| 337 | // The last element in the array has a null in its "description" slot. |
| 338 | void SetRangeDescriptions(const DescriptionPair descriptions[]); |
| 339 | |
| 340 | protected: |
| 341 | // Initialize ranges_ mapping. |
| 342 | virtual void InitializeBucketRange(); |
| 343 | // Find bucket to increment for sample value. |
| 344 | virtual size_t BucketIndex(Sample value) const; |
mmoss@google.com | b16d6e0 | 2008-08-14 04:38:25 +0900 | [diff] [blame] | 345 | virtual double GetBucketSize(Count current, size_t i) const; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 346 | |
| 347 | // If we have a description for a bucket, then return that. Otherwise |
| 348 | // let parent class provide a (numeric) description. |
| 349 | virtual const std::string GetAsciiBucketRange(size_t i) const; |
| 350 | |
| 351 | // Skip printing of name for numeric range if we have a name (and if this is |
| 352 | // an empty bucket). |
| 353 | virtual bool PrintEmptyBucket(size_t index) const; |
| 354 | |
| 355 | private: |
| 356 | // For some ranges, we store a printable description of a bucket range. |
| 357 | // If there is no desciption, then GetAsciiBucketRange() uses parent class |
| 358 | // to provide a description. |
| 359 | typedef std::map<Sample, std::string> BucketDescriptionMap; |
| 360 | BucketDescriptionMap bucket_description_; |
| 361 | |
| 362 | DISALLOW_EVIL_CONSTRUCTORS(LinearHistogram); |
| 363 | }; |
| 364 | |
| 365 | |
| 366 | //------------------------------------------------------------------------------ |
| 367 | // This section provides implementation for ThreadSafeHistogram. |
| 368 | //------------------------------------------------------------------------------ |
| 369 | |
| 370 | class ThreadSafeHistogram : public Histogram { |
| 371 | public: |
| 372 | ThreadSafeHistogram(const wchar_t* name, Sample minimum, |
| 373 | Sample maximum, size_t bucket_count); |
| 374 | |
| 375 | // Provide the analog to Add() |
| 376 | void Remove(int value); |
| 377 | |
| 378 | protected: |
| 379 | // Provide locked versions to get precise counts. |
| 380 | virtual void Accumulate(Sample value, Count count, size_t index); |
| 381 | |
| 382 | virtual void SnapshotSample(SampleSet* sample); |
| 383 | |
| 384 | private: |
| 385 | Lock lock_; |
| 386 | |
| 387 | DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeHistogram); |
| 388 | }; |
| 389 | |
| 390 | //------------------------------------------------------------------------------ |
| 391 | // StatisticsRecorder handles all histograms in the system. It provides a |
| 392 | // general place for histograms to register, and supports a global API for |
| 393 | // accessing (i.e., dumping, or graphing) the data in all the histograms. |
| 394 | |
| 395 | class StatisticsRecorder { |
| 396 | public: |
| 397 | typedef std::vector<const Histogram*> Histograms; |
| 398 | |
| 399 | StatisticsRecorder(); |
| 400 | |
| 401 | ~StatisticsRecorder(); |
| 402 | |
| 403 | // Find out if histograms can now be registered into our list. |
| 404 | static bool WasStarted(); |
| 405 | |
| 406 | // Register, or add a new histogram to the collection of statistics. |
| 407 | // Return true if registered. |
| 408 | static bool Register(const Histogram& histogram); |
| 409 | // Unregister, or remove, a histogram from the collection of statistics. |
| 410 | static void UnRegister(const Histogram& histogram); |
| 411 | |
| 412 | // Methods for printing histograms. Only histograms which have query as |
| 413 | // a substring are written to output (an empty string will process all |
| 414 | // registered histograms). |
| 415 | static void WriteHTMLGraph(const std::string& query, std::string* output); |
| 416 | static void WriteGraph(const std::string& query, std::string* output); |
| 417 | |
| 418 | // Method for extracting histograms which were marked for use by UMA. |
| 419 | static void GetHistograms(Histograms* output); |
| 420 | |
| 421 | static void set_dump_on_exit(bool enable) { dump_on_exit_ = enable; } |
| 422 | |
| 423 | private: |
| 424 | typedef std::map<std::string, const Histogram*> HistogramMap; |
| 425 | // We keep all registered histograms in a map, from name to histogram. |
| 426 | |
| 427 | // GetSnapshot copies some of the pointers to registered histograms into the |
| 428 | // caller supplied vector (Histograms). Only histograms with names matching |
| 429 | // query are returned. The query must be a substring of histogram name for its |
| 430 | // pointer to be copied. |
| 431 | static void GetSnapshot(const std::string& query, Histograms* snapshot); |
| 432 | |
| 433 | static HistogramMap* histograms_; |
| 434 | // lock protects access to the above map. |
| 435 | static Lock* lock_; |
| 436 | |
| 437 | // Dump all known histograms to log. |
| 438 | static bool dump_on_exit_; |
| 439 | |
| 440 | DISALLOW_EVIL_CONSTRUCTORS(StatisticsRecorder); |
| 441 | }; |
| 442 | |
| 443 | #endif // BASE_HISTOGRAM_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 444 | |