blob: 8b4bded6acc6bc9d14b28b3b6f68c996780373e2 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.commit3f4a7322008-07-27 06:49:38 +09004
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 { \
dsh@google.com0f8dd262008-10-28 05:43:33 +090047 static Histogram counter((name), base::TimeDelta::FromMilliseconds(1), \
48 base::TimeDelta::FromSeconds(10), 50); \
initial.commit3f4a7322008-07-27 06:49:38 +090049 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
103static const int kUmaTargetedHistogramFlag = 0x1;
104
105#define UMA_HISTOGRAM_TIMES(name, sample) do { \
dsh@google.com0f8dd262008-10-28 05:43:33 +0900106 static Histogram counter((name), base::TimeDelta::FromMilliseconds(1), \
107 base::TimeDelta::FromSeconds(10), 50); \
initial.commit3f4a7322008-07-27 06:49:38 +0900108 counter.SetFlags(kUmaTargetedHistogramFlag); \
109 counter.AddTime(sample); \
110 } while (0)
111
jar@google.coma5c410d2008-11-22 10:40:22 +0900112#define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) do { \
113 static Histogram counter((name), base::TimeDelta::FromMilliseconds(10), \
114 base::TimeDelta::FromMinutes(3), 50); \
115 counter.SetFlags(kUmaTargetedHistogramFlag); \
116 counter.AddTime(sample); \
117 } while (0)
118
initial.commit3f4a7322008-07-27 06:49:38 +0900119// Use this macro when times can routinely be much longer than 10 seconds.
120#define UMA_HISTOGRAM_LONG_TIMES(name, sample) do { \
dsh@google.com0f8dd262008-10-28 05:43:33 +0900121 static Histogram counter((name), base::TimeDelta::FromMilliseconds(1), \
122 base::TimeDelta::FromHours(1), 50); \
initial.commit3f4a7322008-07-27 06:49:38 +0900123 counter.SetFlags(kUmaTargetedHistogramFlag); \
124 counter.AddTime(sample); \
125 } while (0)
126
127#define UMA_HISTOGRAM_COUNTS(name, sample) do { \
128 static Histogram counter((name), 1, 1000000, 50); \
129 counter.SetFlags(kUmaTargetedHistogramFlag); \
130 counter.Add(sample); \
131 } while (0)
132
133#define UMA_HISTOGRAM_COUNTS_100(name, sample) do { \
134 static Histogram counter((name), 1, 100, 50); \
135 counter.SetFlags(kUmaTargetedHistogramFlag); \
136 counter.Add(sample); \
137 } while (0)
138
139#define UMA_HISTOGRAM_MEMORY_KB(name, sample) do { \
140 static Histogram counter((name), 1000, 500000, 50); \
141 counter.SetFlags(kUmaTargetedHistogramFlag); \
142 counter.Add(sample); \
143 } while (0)
144
145#define UMA_HISTOGRAM_MEMORY_MB(name, sample) do { \
146 static Histogram counter((name), 1, 1000, 50); \
147 counter.SetFlags(kUmaTargetedHistogramFlag); \
148 counter.Add(sample); \
149 } while (0)
150
151//------------------------------------------------------------------------------
152
153class Histogram : public StatsRate {
154 public:
155 typedef int Sample; // Used for samples (and ranges of samples).
156 typedef int Count; // Used to count samples in a bucket.
157 static const Sample kSampleType_MAX = INT_MAX;
158
159 typedef std::vector<Count> Counts;
deanm@google.com18188472008-08-12 03:57:54 +0900160 typedef std::vector<Sample> Ranges;
initial.commit3f4a7322008-07-27 06:49:38 +0900161
darin@google.com12d40bb2008-08-20 03:36:23 +0900162 static const int kHexRangePrintingFlag;
163
initial.commit3f4a7322008-07-27 06:49:38 +0900164 //----------------------------------------------------------------------------
165 // Statistic values, developed over the life of the histogram.
166
167 class SampleSet {
168 public:
169 explicit SampleSet();
170 // Adjust size of counts_ for use with given histogram.
171 void Resize(const Histogram& histogram);
172 void CheckSize(const Histogram& histogram) const;
173
174 // Accessor for histogram to make routine additions.
175 void Accumulate(Sample value, Count count, size_t index);
176
177 // Accessor methods.
178 Count counts(size_t i) const { return counts_[i]; }
179 Count TotalCount() const ;
180 int64 sum() const { return sum_; }
181 int64 square_sum() const { return square_sum_; }
182
183 // Arithmetic manipulation of corresponding elements of the set.
184 void Add(const SampleSet& other);
185 void Subtract(const SampleSet& other);
186
rvargas@google.com644c9122008-09-25 08:51:25 +0900187 protected:
initial.commit3f4a7322008-07-27 06:49:38 +0900188 // Actual histogram data is stored in buckets, showing the count of values
189 // that fit into each bucket.
190 Counts counts_;
191
192 // Save simple stats locally. Note that this MIGHT get done in base class
193 // without shared memory at some point.
194 int64 sum_; // sum of samples.
195 int64 square_sum_; // sum of squares of samples.
196 };
197 //----------------------------------------------------------------------------
198
199 Histogram(const wchar_t* name, Sample minimum,
200 Sample maximum, size_t bucket_count);
dsh@google.com0f8dd262008-10-28 05:43:33 +0900201 Histogram(const wchar_t* name, base::TimeDelta minimum,
202 base::TimeDelta maximum, size_t bucket_count);
maruel@google.comfc903372008-10-01 05:50:51 +0900203 virtual ~Histogram();
initial.commit3f4a7322008-07-27 06:49:38 +0900204
205 // Hooks to override stats counter methods. This ensures that we gather all
206 // input the stats counter sees.
207 virtual void Add(int value);
208
jar@google.coma5c410d2008-11-22 10:40:22 +0900209 // The following methods provide graphical histogram displays.
initial.commit3f4a7322008-07-27 06:49:38 +0900210 void WriteHTMLGraph(std::string* output) const;
211 void WriteAscii(bool graph_it, const std::string& newline,
212 std::string* output) const;
213
214 // Support generic flagging of Histograms.
215 // 0x1 Currently used to mark this histogram to be recorded by UMA..
216 // 0x8000 means print ranges in hex.
217 void SetFlags(int flags) { flags_ |= flags; }
rvargas@google.com644c9122008-09-25 08:51:25 +0900218 void ClearFlags(int flags) { flags_ &= ~flags; }
initial.commit3f4a7322008-07-27 06:49:38 +0900219 int flags() const { return flags_; }
220
221 //----------------------------------------------------------------------------
222 // Accessors for serialization and testing.
223 //----------------------------------------------------------------------------
224 const std::string histogram_name() const { return histogram_name_; }
225 Sample declared_min() const { return declared_min_; }
226 Sample declared_max() const { return declared_max_; }
rvargas@google.com644c9122008-09-25 08:51:25 +0900227 virtual Sample ranges(size_t i) const { return ranges_[i];}
228 virtual size_t bucket_count() const { return bucket_count_; }
initial.commit3f4a7322008-07-27 06:49:38 +0900229 // Snapshot the current complete set of sample data.
230 // Override with atomic/locked snapshot if needed.
231 virtual void SnapshotSample(SampleSet* sample) const;
232
233 protected:
234 // Method to override to skip the display of the i'th bucket if it's empty.
235 virtual bool PrintEmptyBucket(size_t index) const { return true; }
236
237 //----------------------------------------------------------------------------
238 // Methods to override to create histogram with different bucket widths.
239 //----------------------------------------------------------------------------
240 // Initialize ranges_ mapping.
241 virtual void InitializeBucketRange();
242 // Find bucket to increment for sample value.
243 virtual size_t BucketIndex(Sample value) const;
244 // Get normalized size, relative to the ranges_[i].
245 virtual double GetBucketSize(Count current, size_t i) const;
246
247 // Return a string description of what goes in a given bucket.
248 // Most commonly this is the numeric value, but in derived classes it may
249 // be a name (or string description) given to the bucket.
250 virtual const std::string GetAsciiBucketRange(size_t it) const;
251
252 //----------------------------------------------------------------------------
253 // Methods to override to create thread safe histogram.
254 //----------------------------------------------------------------------------
255 // Update all our internal data, including histogram
256 virtual void Accumulate(Sample value, Count count, size_t index);
257
258 //----------------------------------------------------------------------------
259 // Accessors for derived classes.
260 //----------------------------------------------------------------------------
261 void SetBucketRange(size_t i, Sample value);
262
263 // Validate that ranges_ was created sensibly (top and bottom range
264 // values relate properly to the declared_min_ and declared_max_)..
265 bool ValidateBucketRanges() const;
266
267 private:
268 // Post constructor initialization.
269 void Initialize();
270
271 //----------------------------------------------------------------------------
272 // Helpers for emitting Ascii graphic. Each method appends data to output.
273
274 // Find out how large the (graphically) the largest bucket will appear to be.
275 double GetPeakBucketSize(const SampleSet& snapshot) const;
276
277 // Write a common header message describing this histogram.
278 void WriteAsciiHeader(const SampleSet& snapshot,
279 Count sample_count, std::string* output) const ;
280
281 // Write information about previous, current, and next buckets.
282 // Information such as cumulative percentage, etc.
283 void WriteAsciiBucketContext(const int64 past, const Count current,
284 const int64 remaining, const size_t i,
285 std::string* output) const;
286
287 // Write textual description of the bucket contents (relative to histogram).
288 // Output is the count in the buckets, as well as the percentage.
289 void WriteAsciiBucketValue(Count current, double scaled_sum,
290 std::string* output) const;
291
292 // Produce actual graph (set of blank vs non blank char's) for a bucket.
293 void WriteAsciiBucketGraph(double current_size, double max_size,
294 std::string* output) const;
295
296 //----------------------------------------------------------------------------
297 // Invariant values set at/near construction time
298
299 // ASCII version of original name given to the constructor. All identically
300 // named instances will be coalesced cross-project TODO(jar).
301 // If a user needs one histogram name to be called by several places in a
302 // single process, a central function should be defined by teh user, which
303 // defins the single declared instance of the named histogram.
304 const std::string histogram_name_;
305 Sample declared_min_; // Less than this goes into counts_[0]
306 Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1].
307 size_t bucket_count_; // Dimension of counts_[].
308
309 // Flag the histogram for recording by UMA via metric_services.h.
310 int flags_;
311
312 // For each index, show the least value that can be stored in the
313 // corresponding bucket. We also append one extra element in this array,
314 // containing kSampleType_MAX, to make calculations easy.
315 // The dimension of ranges_ is bucket_count + 1.
316 Ranges ranges_;
317
318 // Finally, provide the state that changes with the addition of each new
319 // sample.
320 SampleSet sample_;
321
322 // Indicate if successfully registered.
323 bool registered_;
324
325 DISALLOW_EVIL_CONSTRUCTORS(Histogram);
326};
327
328//------------------------------------------------------------------------------
329
330// LinearHistogram is a more traditional histogram, with evenly spaced
331// buckets.
332class LinearHistogram : public Histogram {
333 public:
334 struct DescriptionPair {
335 Sample sample;
evanm@google.com1b2d3e02008-08-21 07:56:30 +0900336 const char* description; // Null means end of a list of pairs.
initial.commit3f4a7322008-07-27 06:49:38 +0900337 };
338 LinearHistogram(const wchar_t* name, Sample minimum,
339 Sample maximum, size_t bucket_count);
dsh@google.com0f8dd262008-10-28 05:43:33 +0900340 LinearHistogram(const wchar_t* name, base::TimeDelta minimum,
341 base::TimeDelta maximum, size_t bucket_count);
initial.commit3f4a7322008-07-27 06:49:38 +0900342 ~LinearHistogram() {}
343
344 // Store a list of number/text values for use in rendering the histogram.
345 // The last element in the array has a null in its "description" slot.
346 void SetRangeDescriptions(const DescriptionPair descriptions[]);
347
348 protected:
349 // Initialize ranges_ mapping.
350 virtual void InitializeBucketRange();
351 // Find bucket to increment for sample value.
352 virtual size_t BucketIndex(Sample value) const;
mmoss@google.comb16d6e02008-08-14 04:38:25 +0900353 virtual double GetBucketSize(Count current, size_t i) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900354
355 // If we have a description for a bucket, then return that. Otherwise
356 // let parent class provide a (numeric) description.
357 virtual const std::string GetAsciiBucketRange(size_t i) const;
358
359 // Skip printing of name for numeric range if we have a name (and if this is
360 // an empty bucket).
361 virtual bool PrintEmptyBucket(size_t index) const;
362
363 private:
364 // For some ranges, we store a printable description of a bucket range.
365 // If there is no desciption, then GetAsciiBucketRange() uses parent class
366 // to provide a description.
367 typedef std::map<Sample, std::string> BucketDescriptionMap;
368 BucketDescriptionMap bucket_description_;
369
370 DISALLOW_EVIL_CONSTRUCTORS(LinearHistogram);
371};
372
abarth@chromium.org7ed364e2009-01-18 04:15:36 +0900373//------------------------------------------------------------------------------
374
375// BooleanHistogram is a histogram for booleans.
376class BooleanHistogram : public LinearHistogram {
377 public:
378 BooleanHistogram(const wchar_t* name) : LinearHistogram(name, 0, 2, 3) {}
379
380 virtual void AddBoolean(bool value) { Add(value ? 1 : 0); }
381
382 private:
383 DISALLOW_EVIL_CONSTRUCTORS(BooleanHistogram);
384};
initial.commit3f4a7322008-07-27 06:49:38 +0900385
386//------------------------------------------------------------------------------
387// This section provides implementation for ThreadSafeHistogram.
388//------------------------------------------------------------------------------
389
390class ThreadSafeHistogram : public Histogram {
391 public:
392 ThreadSafeHistogram(const wchar_t* name, Sample minimum,
393 Sample maximum, size_t bucket_count);
394
395 // Provide the analog to Add()
396 void Remove(int value);
397
398 protected:
399 // Provide locked versions to get precise counts.
400 virtual void Accumulate(Sample value, Count count, size_t index);
401
402 virtual void SnapshotSample(SampleSet* sample);
403
404 private:
405 Lock lock_;
406
407 DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeHistogram);
408};
409
410//------------------------------------------------------------------------------
411// StatisticsRecorder handles all histograms in the system. It provides a
412// general place for histograms to register, and supports a global API for
413// accessing (i.e., dumping, or graphing) the data in all the histograms.
414
415class StatisticsRecorder {
416 public:
417 typedef std::vector<const Histogram*> Histograms;
418
419 StatisticsRecorder();
420
421 ~StatisticsRecorder();
422
423 // Find out if histograms can now be registered into our list.
424 static bool WasStarted();
425
426 // Register, or add a new histogram to the collection of statistics.
427 // Return true if registered.
428 static bool Register(const Histogram& histogram);
429 // Unregister, or remove, a histogram from the collection of statistics.
430 static void UnRegister(const Histogram& histogram);
431
432 // Methods for printing histograms. Only histograms which have query as
433 // a substring are written to output (an empty string will process all
434 // registered histograms).
435 static void WriteHTMLGraph(const std::string& query, std::string* output);
436 static void WriteGraph(const std::string& query, std::string* output);
437
438 // Method for extracting histograms which were marked for use by UMA.
439 static void GetHistograms(Histograms* output);
440
441 static void set_dump_on_exit(bool enable) { dump_on_exit_ = enable; }
442
443 private:
444 typedef std::map<std::string, const Histogram*> HistogramMap;
445 // We keep all registered histograms in a map, from name to histogram.
446
447 // GetSnapshot copies some of the pointers to registered histograms into the
448 // caller supplied vector (Histograms). Only histograms with names matching
449 // query are returned. The query must be a substring of histogram name for its
450 // pointer to be copied.
451 static void GetSnapshot(const std::string& query, Histograms* snapshot);
452
453 static HistogramMap* histograms_;
454 // lock protects access to the above map.
455 static Lock* lock_;
456
457 // Dump all known histograms to log.
458 static bool dump_on_exit_;
459
460 DISALLOW_EVIL_CONSTRUCTORS(StatisticsRecorder);
461};
462
463#endif // BASE_HISTOGRAM_H__
license.botf003cfe2008-08-24 09:55:55 +0900464