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