blob: a2672e0b2797c7d16989389a165546617850334e [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 { \
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
103static 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
146class 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.com18188472008-08-12 03:57:54 +0900153 typedef std::vector<Sample> Ranges;
initial.commit3f4a7322008-07-27 06:49:38 +0900154
darin@google.com12d40bb2008-08-20 03:36:23 +0900155 static const int kHexRangePrintingFlag;
156
initial.commit3f4a7322008-07-27 06:49:38 +0900157 //----------------------------------------------------------------------------
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
rvargas@google.com644c9122008-09-25 08:51:25 +0900180 protected:
initial.commit3f4a7322008-07-27 06:49:38 +0900181 // 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; }
rvargas@google.com644c9122008-09-25 08:51:25 +0900211 void ClearFlags(int flags) { flags_ &= ~flags; }
initial.commit3f4a7322008-07-27 06:49:38 +0900212 int flags() const { return flags_; }
213
214 //----------------------------------------------------------------------------
215 // Accessors for serialization and testing.
216 //----------------------------------------------------------------------------
217 const std::string histogram_name() const { return histogram_name_; }
218 Sample declared_min() const { return declared_min_; }
219 Sample declared_max() const { return declared_max_; }
rvargas@google.com644c9122008-09-25 08:51:25 +0900220 virtual Sample ranges(size_t i) const { return ranges_[i];}
221 virtual size_t bucket_count() const { return bucket_count_; }
initial.commit3f4a7322008-07-27 06:49:38 +0900222 // Snapshot the current complete set of sample data.
223 // Override with atomic/locked snapshot if needed.
224 virtual void SnapshotSample(SampleSet* sample) const;
225
226 protected:
227 // Method to override to skip the display of the i'th bucket if it's empty.
228 virtual bool PrintEmptyBucket(size_t index) const { return true; }
229
230 //----------------------------------------------------------------------------
231 // Methods to override to create histogram with different bucket widths.
232 //----------------------------------------------------------------------------
233 // Initialize ranges_ mapping.
234 virtual void InitializeBucketRange();
235 // Find bucket to increment for sample value.
236 virtual size_t BucketIndex(Sample value) const;
237 // Get normalized size, relative to the ranges_[i].
238 virtual double GetBucketSize(Count current, size_t i) const;
239
240 // Return a string description of what goes in a given bucket.
241 // Most commonly this is the numeric value, but in derived classes it may
242 // be a name (or string description) given to the bucket.
243 virtual const std::string GetAsciiBucketRange(size_t it) const;
244
245 //----------------------------------------------------------------------------
246 // Methods to override to create thread safe histogram.
247 //----------------------------------------------------------------------------
248 // Update all our internal data, including histogram
249 virtual void Accumulate(Sample value, Count count, size_t index);
250
251 //----------------------------------------------------------------------------
252 // Accessors for derived classes.
253 //----------------------------------------------------------------------------
254 void SetBucketRange(size_t i, Sample value);
255
256 // Validate that ranges_ was created sensibly (top and bottom range
257 // values relate properly to the declared_min_ and declared_max_)..
258 bool ValidateBucketRanges() const;
259
260 private:
261 // Post constructor initialization.
262 void Initialize();
263
264 //----------------------------------------------------------------------------
265 // Helpers for emitting Ascii graphic. Each method appends data to output.
266
267 // Find out how large the (graphically) the largest bucket will appear to be.
268 double GetPeakBucketSize(const SampleSet& snapshot) const;
269
270 // Write a common header message describing this histogram.
271 void WriteAsciiHeader(const SampleSet& snapshot,
272 Count sample_count, std::string* output) const ;
273
274 // Write information about previous, current, and next buckets.
275 // Information such as cumulative percentage, etc.
276 void WriteAsciiBucketContext(const int64 past, const Count current,
277 const int64 remaining, const size_t i,
278 std::string* output) const;
279
280 // Write textual description of the bucket contents (relative to histogram).
281 // Output is the count in the buckets, as well as the percentage.
282 void WriteAsciiBucketValue(Count current, double scaled_sum,
283 std::string* output) const;
284
285 // Produce actual graph (set of blank vs non blank char's) for a bucket.
286 void WriteAsciiBucketGraph(double current_size, double max_size,
287 std::string* output) const;
288
289 //----------------------------------------------------------------------------
290 // Invariant values set at/near construction time
291
292 // ASCII version of original name given to the constructor. All identically
293 // named instances will be coalesced cross-project TODO(jar).
294 // If a user needs one histogram name to be called by several places in a
295 // single process, a central function should be defined by teh user, which
296 // defins the single declared instance of the named histogram.
297 const std::string histogram_name_;
298 Sample declared_min_; // Less than this goes into counts_[0]
299 Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1].
300 size_t bucket_count_; // Dimension of counts_[].
301
302 // Flag the histogram for recording by UMA via metric_services.h.
303 int flags_;
304
305 // For each index, show the least value that can be stored in the
306 // corresponding bucket. We also append one extra element in this array,
307 // containing kSampleType_MAX, to make calculations easy.
308 // The dimension of ranges_ is bucket_count + 1.
309 Ranges ranges_;
310
311 // Finally, provide the state that changes with the addition of each new
312 // sample.
313 SampleSet sample_;
314
315 // Indicate if successfully registered.
316 bool registered_;
317
318 DISALLOW_EVIL_CONSTRUCTORS(Histogram);
319};
320
321//------------------------------------------------------------------------------
322
323// LinearHistogram is a more traditional histogram, with evenly spaced
324// buckets.
325class LinearHistogram : public Histogram {
326 public:
327 struct DescriptionPair {
328 Sample sample;
evanm@google.com1b2d3e02008-08-21 07:56:30 +0900329 const char* description; // Null means end of a list of pairs.
initial.commit3f4a7322008-07-27 06:49:38 +0900330 };
331 LinearHistogram(const wchar_t* name, Sample minimum,
332 Sample maximum, size_t bucket_count);
333 LinearHistogram(const wchar_t* name, TimeDelta minimum,
334 TimeDelta maximum, size_t bucket_count);
335 ~LinearHistogram() {}
336
337 // Store a list of number/text values for use in rendering the histogram.
338 // The last element in the array has a null in its "description" slot.
339 void SetRangeDescriptions(const DescriptionPair descriptions[]);
340
341 protected:
342 // Initialize ranges_ mapping.
343 virtual void InitializeBucketRange();
344 // Find bucket to increment for sample value.
345 virtual size_t BucketIndex(Sample value) const;
mmoss@google.comb16d6e02008-08-14 04:38:25 +0900346 virtual double GetBucketSize(Count current, size_t i) const;
initial.commit3f4a7322008-07-27 06:49:38 +0900347
348 // If we have a description for a bucket, then return that. Otherwise
349 // let parent class provide a (numeric) description.
350 virtual const std::string GetAsciiBucketRange(size_t i) const;
351
352 // Skip printing of name for numeric range if we have a name (and if this is
353 // an empty bucket).
354 virtual bool PrintEmptyBucket(size_t index) const;
355
356 private:
357 // For some ranges, we store a printable description of a bucket range.
358 // If there is no desciption, then GetAsciiBucketRange() uses parent class
359 // to provide a description.
360 typedef std::map<Sample, std::string> BucketDescriptionMap;
361 BucketDescriptionMap bucket_description_;
362
363 DISALLOW_EVIL_CONSTRUCTORS(LinearHistogram);
364};
365
366
367//------------------------------------------------------------------------------
368// This section provides implementation for ThreadSafeHistogram.
369//------------------------------------------------------------------------------
370
371class ThreadSafeHistogram : public Histogram {
372 public:
373 ThreadSafeHistogram(const wchar_t* name, Sample minimum,
374 Sample maximum, size_t bucket_count);
375
376 // Provide the analog to Add()
377 void Remove(int value);
378
379 protected:
380 // Provide locked versions to get precise counts.
381 virtual void Accumulate(Sample value, Count count, size_t index);
382
383 virtual void SnapshotSample(SampleSet* sample);
384
385 private:
386 Lock lock_;
387
388 DISALLOW_EVIL_CONSTRUCTORS(ThreadSafeHistogram);
389};
390
391//------------------------------------------------------------------------------
392// StatisticsRecorder handles all histograms in the system. It provides a
393// general place for histograms to register, and supports a global API for
394// accessing (i.e., dumping, or graphing) the data in all the histograms.
395
396class StatisticsRecorder {
397 public:
398 typedef std::vector<const Histogram*> Histograms;
399
400 StatisticsRecorder();
401
402 ~StatisticsRecorder();
403
404 // Find out if histograms can now be registered into our list.
405 static bool WasStarted();
406
407 // Register, or add a new histogram to the collection of statistics.
408 // Return true if registered.
409 static bool Register(const Histogram& histogram);
410 // Unregister, or remove, a histogram from the collection of statistics.
411 static void UnRegister(const Histogram& histogram);
412
413 // Methods for printing histograms. Only histograms which have query as
414 // a substring are written to output (an empty string will process all
415 // registered histograms).
416 static void WriteHTMLGraph(const std::string& query, std::string* output);
417 static void WriteGraph(const std::string& query, std::string* output);
418
419 // Method for extracting histograms which were marked for use by UMA.
420 static void GetHistograms(Histograms* output);
421
422 static void set_dump_on_exit(bool enable) { dump_on_exit_ = enable; }
423
424 private:
425 typedef std::map<std::string, const Histogram*> HistogramMap;
426 // We keep all registered histograms in a map, from name to histogram.
427
428 // GetSnapshot copies some of the pointers to registered histograms into the
429 // caller supplied vector (Histograms). Only histograms with names matching
430 // query are returned. The query must be a substring of histogram name for its
431 // pointer to be copied.
432 static void GetSnapshot(const std::string& query, Histograms* snapshot);
433
434 static HistogramMap* histograms_;
435 // lock protects access to the above map.
436 static Lock* lock_;
437
438 // Dump all known histograms to log.
439 static bool dump_on_exit_;
440
441 DISALLOW_EVIL_CONSTRUCTORS(StatisticsRecorder);
442};
443
444#endif // BASE_HISTOGRAM_H__
license.botf003cfe2008-08-24 09:55:55 +0900445