15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Histogram is an object that aggregates statistics, and can summarize them in
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// various forms, including ASCII graphical, HTML, and numerically (as a
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// vector of numbers corresponding to each of the aggregating buckets).
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It supports calls to accumulate either time intervals (which are processed
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// as integral number of milliseconds), or arbitrary integral units.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For Histogram(exponential histogram), LinearHistogram and CustomHistogram,
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the minimum for a declared range is 1 (instead of 0), while the maximum is
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (HistogramBase::kSampleType_MAX - 1). Currently you can declare histograms
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with ranges exceeding those limits (e.g. 0 as minimal or
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HistogramBase::kSampleType_MAX as maximal), but those excesses will be
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// silently clamped to those limits (for backwards compatibility with existing
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// code). Best practice is to not exceed the limits.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Each use of a histogram with the same name will reference the same underlying
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// data, so it is safe to record to the same histogram from multiple locations
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the code. It is a runtime error if all uses of the same histogram do not
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// agree exactly in type, bucket size and range.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For Histogram and LinearHistogram, the maximum for a declared range should
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// always be larger (not equal) than minimal range. Zero and
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HistogramBase::kSampleType_MAX are implicitly added as first and last ranges,
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// so the smallest legal bucket_count is 3. However CustomHistogram can have
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bucket count as 2 (when you give a custom ranges vector containing only 1
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// range).
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For these 3 kinds of histograms, the max bucket count is always
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (Histogram::kBucketCount_MAX - 1).
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The buckets layout of class Histogram is exponential. For example, buckets
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// might contain (sequentially) the count of values in the following intervals:
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// [0,1), [1,2), [2,4), [4,8), [8,16), [16,32), [32,64), [64,infinity)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// That bucket allocation would actually result from construction of a histogram
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for values between 1 and 64, with 8 buckets, such as:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Histogram count("some name", 1, 64, 8);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that the underflow bucket [0,1) and the overflow bucket [64,infinity)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are also counted by the constructor in the user supplied "bucket_count"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// argument.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The above example has an exponential ratio of 2 (doubling the bucket width
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in each consecutive bucket.  The Histogram class automatically calculates
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the smallest ratio that it can use to construct the number of buckets
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// selected in the constructor.  An another example, if you had 50 buckets,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and millisecond time values from 1 to 10000, then the ratio between
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// consecutive bucket widths will be approximately somewhere around the 50th
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// root of 10000.  This approach provides very fine grain (narrow) buckets
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// at the low end of the histogram scale, but allows the histogram to cover a
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// gigantic range with the addition of very few buckets.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Usually we use macros to define and use a histogram. These macros use a
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pattern involving a function static variable, that is a pointer to a
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// histogram.  This static is explicitly initialized on any thread
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that detects a uninitialized (NULL) pointer.  The potentially racy
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// initialization is not a problem as it is always set to point to the same
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// value (i.e., the FactoryGet always returns the same value).  FactoryGet
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is also completely thread safe, which results in a completely thread safe,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and relatively fast, set of counters.  To avoid races at shutdown, the static
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// pointer is NOT deleted, and we leak the histograms at process termination.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef BASE_METRICS_HISTOGRAM_H_
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define BASE_METRICS_HISTOGRAM_H_
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/atomicops.h"
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/base_export.h"
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/bucket_ranges.h"
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram_base.h"
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram_samples.h"
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Pickle;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PickleIterator;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Lock;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Histograms are often put in areas where they are called many many times, and
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// performance is critical.  As a result, they are designed to have a very low
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// recurring cost of executing (adding additional samples).  Toward that end,
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the macros declare a static pointer to the histogram in question, and only
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// take a "slow path" to construct (or find) the histogram on the first run
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// through the macro.  We leak the histograms at shutdown time so that we don't
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// have to validate using the pointers at any time during the running of the
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// process.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following code is generally what a thread-safe static pointer
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// initialization looks like for a histogram (after a macro is expanded).  This
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sample is an expansion (with comments) of the code for
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// LOCAL_HISTOGRAM_CUSTOM_COUNTS().
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  do {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The pointer's presence indicates the initialization is complete.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Initialization is idempotent, so it can safely be atomically repeated.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static base::subtle::AtomicWord atomic_histogram_pointer = 0;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Acquire_Load() ensures that we acquire visibility to the pointed-to data
110f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // in the histogram.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Histogram* histogram_pointer(reinterpret_cast<base::Histogram*>(
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::subtle::Acquire_Load(&atomic_histogram_pointer)));
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!histogram_pointer) {
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // This is the slow path, which will construct OR find the matching
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // histogram.  FactoryGet includes locks on a global histogram name map
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // and is completely thread safe.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      histogram_pointer = base::Histogram::FactoryGet(
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          name, min, max, bucket_count, base::HistogramBase::kNoFlags);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Use Release_Store to ensure that the histogram data is made available
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // globally before we make the pointer visible.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Several threads may perform this store, but the same value will be
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // stored in all cases (for a given named/spec'ed histogram).
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // We could do this without any barrier, since FactoryGet entered and
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // exited a lock after construction, but this barrier makes things clear.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::subtle::Release_Store(&atomic_histogram_pointer,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer));
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Ensure calling contract is upheld, and the name does NOT vary.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(histogram_pointer->histogram_name() == constant_histogram_name);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    histogram_pointer->Add(sample);
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } while (0);
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The above pattern is repeated in several macros.  The only elements that
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// vary are the invocation of the Add(sample) vs AddTime(sample), and the choice
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of which FactoryGet method to use.  The different FactoryGet methods have
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// various argument lists, so the function with its argument list is provided as
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a macro argument here.  The name is only used in a DCHECK, to assure that
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// callers don't try to vary the name of the histogram (which would tend to be
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ignored by the one-time initialization of the histogtram_pointer).
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define STATIC_HISTOGRAM_POINTER_BLOCK(constant_histogram_name, \
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       histogram_add_method_invocation, \
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       histogram_factory_get_invocation) \
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  do { \
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static base::subtle::AtomicWord atomic_histogram_pointer = 0; \
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::HistogramBase* histogram_pointer( \
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        reinterpret_cast<base::HistogramBase*>( \
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::subtle::Acquire_Load(&atomic_histogram_pointer))); \
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!histogram_pointer) { \
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      histogram_pointer = histogram_factory_get_invocation; \
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::subtle::Release_Store(&atomic_histogram_pointer, \
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); \
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } \
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (DCHECK_IS_ON) \
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      histogram_pointer->CheckName(constant_histogram_name); \
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    histogram_pointer->histogram_add_method_invocation; \
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } while (0)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Provide easy general purpose histogram in a macro, just like stats counters.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The first four macros use 50 buckets.
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_TIMES(name, sample) LOCAL_HISTOGRAM_CUSTOM_TIMES( \
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, base::TimeDelta::FromMilliseconds(1), \
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeDelta::FromSeconds(10), 50)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// For folks that need real specific times, use this to select a precise range
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// of times you want plotted, and the number of buckets you want used.
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        base::HistogramBase::kNoFlags))
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_COUNTS(name, sample) LOCAL_HISTOGRAM_CUSTOM_COUNTS( \
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, 1, 1000000, 50)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_COUNTS_100(name, sample) \
1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 100, 50)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_COUNTS_10000(name, sample) \
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Histogram::FactoryGet(name, min, max, bucket_count, \
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    base::HistogramBase::kNoFlags))
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This is a helper macro used by other macros and shouldn't be used directly.
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, \
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            flag))
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    LOCAL_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_BOOLEAN(name, sample) \
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::BooleanHistogram::FactoryGet(name, base::Histogram::kNoFlags))
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Support histograming of an enumerated value.  The samples should always be
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// strictly less than |boundary_value| -- this prevents you from running into
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// problems down the line if you add additional buckets to the histogram.  Note
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// also that, despite explicitly setting the minimum bucket value to |1| below,
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it is fine for enumerated histograms to be 0-indexed -- this is because
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// enumerated histograms should never have underflow.
2121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::LinearHistogram::FactoryGet(name, 1, boundary_value, \
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            boundary_value + 1, base::HistogramBase::kNoFlags))
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Support histograming of an enumerated value. Samples should be one of the
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// std::vector<int> list provided via |custom_ranges|. See comments above
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CustomRanges::FactoryGet about the requirement of |custom_ranges|.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You can use the helper function CustomHistogram::ArrayToCustomRanges to
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// transform a C-style array of valid sample values to a std::vector<int>.
2221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::CustomHistogram::FactoryGet(name, custom_ranges, \
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          base::HistogramBase::kNoFlags))
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LOCAL_HISTOGRAM_MEMORY_KB(name, sample) LOCAL_HISTOGRAM_CUSTOM_COUNTS( \
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    name, sample, 1000, 500000, 50)
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The following macros provide typical usage scenarios for callers that wish
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to record histogram data, and have the data submitted/uploaded via UMA.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Not all systems support such UMA, but if they do, the following macros
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should work with the service.
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, base::TimeDelta::FromMilliseconds(1), \
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeDelta::FromSeconds(10), 50)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, base::TimeDelta::FromMilliseconds(10), \
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeDelta::FromMinutes(3), 50)
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use this macro when times can routinely be much longer than 10 seconds.
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, base::TimeDelta::FromMilliseconds(1), \
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeDelta::FromHours(1), 50)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
249d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use this macro when times can routinely be much longer than 10 seconds and
250d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// you want 100 buckets.
251d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \
252d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    name, sample, base::TimeDelta::FromMilliseconds(1), \
253d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    base::TimeDelta::FromHours(1), 100)
254d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::HistogramBase::kUmaTargetedHistogramFlag))
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, 1, 1000000, 50)
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, 1, 100, 50)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, 1, 10000, 50)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::Histogram::FactoryGet(name, min, max, bucket_count, \
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::HistogramBase::kUmaTargetedHistogramFlag))
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_MEMORY_KB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, 1000, 500000, 50)
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_MEMORY_MB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    name, sample, 1, 1000, 50)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UMA_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_BOOLEAN(name, sample) \
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::BooleanHistogram::FactoryGet(name, \
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::HistogramBase::kUmaTargetedHistogramFlag))
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The samples should always be strictly less than |boundary_value|.  For more
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// details, see the comment for the |HISTOGRAM_ENUMERATION| macro, above.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary_value, \
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::HistogramBase::kUmaTargetedHistogramFlag)
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Similar to UMA_HISTOGRAM_ENUMERATION, but used for recording stability
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// histograms.  Use this if recording a histogram that should be part of the
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// initial stability log.
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary_value, \
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::HistogramBase::kUmaStabilityHistogramFlag)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::CustomHistogram::FactoryGet(name, custom_ranges, \
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            base::HistogramBase::kUmaTargetedHistogramFlag))
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BucketRanges;
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SampleVector;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BooleanHistogram;
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CustomHistogram;
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Histogram;
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LinearHistogram;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT Histogram : public HistogramBase {
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize maximum number of buckets in histograms as 16,384.
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const size_t kBucketCount_MAX;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::vector<Count> Counts;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //----------------------------------------------------------------------------
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For a valid histogram, input should follow these restrictions:
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // minimum > 0 (if a minimum below 1 is specified, it will implicitly be
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //              normalized up to 1)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // maximum > minimum
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // buckets > 2 [minimum buckets needed: underflow, overflow and the range]
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Additionally,
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // buckets <= (maximum - minimum + 2) - this is to ensure that we don't have
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // more buckets than the range of numbers; having more buckets than 1 per
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // value in the range would be nonsensical.
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryGet(const std::string& name,
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   Sample minimum,
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   Sample maximum,
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   size_t bucket_count,
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int32 flags);
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryTimeGet(const std::string& name,
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       base::TimeDelta minimum,
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       base::TimeDelta maximum,
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       size_t bucket_count,
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       int32 flags);
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void InitializeBucketRanges(Sample minimum,
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     Sample maximum,
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     BucketRanges* ranges);
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This constant if for FindCorruption. Since snapshots of histograms are
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // taken asynchronously relative to sampling, and our counting code currently
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // does not prevent race conditions, it is pretty likely that we'll catch a
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // redundant count that doesn't match the sample count.  We allow for a
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // certain amount of slop before flagging this as an inconsistency. Even with
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an inconsistency, we'll snapshot it again (for UMA in about a half hour),
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so we'll eventually get the data, if it was not the result of a corruption.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int kCommonRaceBasedCountMismatch;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check to see if bucket ranges, counts and tallies in the snapshot are
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // consistent with the bucket ranges and checksums in our histogram.  This can
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // produce a false-alarm if a race occurred in the reading of the data during
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a SnapShot process, but should otherwise be false at all times (unless we
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // have memory over-writes, or DRAM failures).
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual int FindCorruption(const HistogramSamples& samples) const OVERRIDE;
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //----------------------------------------------------------------------------
365f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Accessors for factory construction, serialization and testing.
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //----------------------------------------------------------------------------
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Sample declared_min() const { return declared_min_; }
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Sample declared_max() const { return declared_max_; }
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual Sample ranges(size_t i) const;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual size_t bucket_count() const;
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const BucketRanges* bucket_ranges() const { return bucket_ranges_; }
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function validates histogram construction arguments. It returns false
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if some of the arguments are totally bad.
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note. Currently it allow some bad input, e.g. 0 as minimum, but silently
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // converts it to good input: 1.
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(kaiwang): Be more restrict and return false for any bad input, and
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // make this a readonly validating function.
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool InspectConstructionArguments(const std::string& name,
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           Sample* minimum,
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           Sample* maximum,
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           size_t* bucket_count);
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HistogramBase implementation:
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual HistogramType GetHistogramType() const OVERRIDE;
386eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual bool HasConstructionArguments(
387eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Sample expected_minimum,
388eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      Sample expected_maximum,
389eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      size_t expected_bucket_count) const OVERRIDE;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Add(Sample value) OVERRIDE;
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual scoped_ptr<HistogramSamples> SnapshotSamples() const OVERRIDE;
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void AddSamples(const HistogramSamples& samples) OVERRIDE;
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool AddSamplesFromPickle(PickleIterator* iter) OVERRIDE;
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void WriteHTMLGraph(std::string* output) const OVERRIDE;
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void WriteAscii(std::string* output) const OVERRIDE;
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
398eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // |ranges| should contain the underflow and overflow buckets. See top
399eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // comments for example.
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Histogram(const std::string& name,
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            Sample minimum,
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            Sample maximum,
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            const BucketRanges* ranges);
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~Histogram();
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // HistogramBase implementation:
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Method to override to skip the display of the i'th bucket if it's empty.
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool PrintEmptyBucket(size_t index) const;
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get normalized size, relative to the ranges(i).
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual double GetBucketSize(Count current, size_t i) const;
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return a string description of what goes in a given bucket.
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Most commonly this is the numeric value, but in derived classes it may
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be a name (or string description) given to the bucket.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string GetAsciiBucketRange(size_t it) const;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Allow tests to corrupt our innards for testing purposes.
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest);
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest);
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds);
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts);
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest);
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class StatisticsRecorder;  // To allow it to delete duplicates.
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class StatisticsRecorderTest;
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PickleIterator* iter);
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* DeserializeInfoImpl(PickleIterator* iter);
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Implementation of SnapshotSamples function.
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SampleVector> SnapshotSampleVector() const;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //----------------------------------------------------------------------------
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helpers for emitting Ascii graphic.  Each method appends data to output.
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void WriteAsciiImpl(bool graph_it,
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      const std::string& newline,
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      std::string* output) const;
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Find out how large (graphically) the largest bucket will appear to be.
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  double GetPeakBucketSize(const SampleVector& samples) const;
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Write a common header message describing this histogram.
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void WriteAsciiHeader(const SampleVector& samples,
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        Count sample_count,
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        std::string* output) const;
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Write information about previous, current, and next buckets.
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Information such as cumulative percentage, etc.
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void WriteAsciiBucketContext(const int64 past, const Count current,
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const int64 remaining, const size_t i,
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               std::string* output) const;
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // WriteJSON calls these.
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetParameters(DictionaryValue* params) const OVERRIDE;
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetCountAndBucketData(Count* count,
464b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                     int64* sum,
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     ListValue* buckets) const OVERRIDE;
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Does not own this object. Should get from StatisticsRecorder.
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const BucketRanges* bucket_ranges_;
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
470eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  Sample declared_min_;  // Less than this goes into the first bucket.
471eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  Sample declared_max_;  // Over this goes into the last bucket.
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Finally, provide the state that changes with the addition of each new
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sample.
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SampleVector> samples_;
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Histogram);
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LinearHistogram is a more traditional histogram, with evenly spaced
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// buckets.
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT LinearHistogram : public Histogram {
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~LinearHistogram();
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     default underflow bucket. */
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryGet(const std::string& name,
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   Sample minimum,
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   Sample maximum,
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   size_t bucket_count,
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   int32 flags);
4952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryTimeGet(const std::string& name,
4962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       TimeDelta minimum,
4972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       TimeDelta maximum,
4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       size_t bucket_count,
4992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       int32 flags);
5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct DescriptionPair {
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Sample sample;
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const char* description;  // Null means end of a list of pairs.
5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create a LinearHistogram and store a list of number/text values for use in
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // writing the histogram graph.
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |descriptions| can be NULL, which means no special descriptions to set. If
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it's not NULL, the last element in the array must has a NULL in its
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "description" field.
5112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryGetWithRangeDescription(
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& name,
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Sample minimum,
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Sample maximum,
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      size_t bucket_count,
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int32 flags,
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const DescriptionPair descriptions[]);
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void InitializeBucketRanges(Sample minimum,
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     Sample maximum,
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     BucketRanges* ranges);
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Histogram:
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual HistogramType GetHistogramType() const OVERRIDE;
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LinearHistogram(const std::string& name,
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  Sample minimum,
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  Sample maximum,
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  const BucketRanges* ranges);
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual double GetBucketSize(Count current, size_t i) const OVERRIDE;
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If we have a description for a bucket, then return that.  Otherwise
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // let parent class provide a (numeric) description.
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string GetAsciiBucketRange(size_t i) const OVERRIDE;
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Skip printing of name for numeric range if we have a name (and if this is
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an empty bucket).
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool PrintEmptyBucket(size_t index) const OVERRIDE;
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PickleIterator* iter);
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* DeserializeInfoImpl(PickleIterator* iter);
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // For some ranges, we store a printable description of a bucket range.
548f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If there is no description, then GetAsciiBucketRange() uses parent class
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to provide a description.
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<Sample, std::string> BucketDescriptionMap;
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BucketDescriptionMap bucket_description_;
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(LinearHistogram);
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BooleanHistogram is a histogram for booleans.
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT BooleanHistogram : public LinearHistogram {
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryGet(const std::string& name, int32 flags);
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual HistogramType GetHistogramType() const OVERRIDE;
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BooleanHistogram(const std::string& name, const BucketRanges* ranges);
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PickleIterator* iter);
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* DeserializeInfoImpl(PickleIterator* iter);
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(BooleanHistogram);
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//------------------------------------------------------------------------------
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CustomHistogram is a histogram for a set of custom integers.
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BASE_EXPORT CustomHistogram : public Histogram {
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |custom_ranges| contains a vector of limits on ranges. Each limit should be
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // compatibility). The limits can be unordered or contain duplication, but
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // client should not depend on this.
5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* FactoryGet(const std::string& name,
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   const std::vector<Sample>& custom_ranges,
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   int32 flags);
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Overridden from Histogram:
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual HistogramType GetHistogramType() const OVERRIDE;
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper method for transforming an array of valid enumeration values
5921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION.
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function ensures that a guard bucket exists right after any
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // valid sample value (unless the next higher sample is also a valid value),
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so that invalid samples never fall into the same bucket as valid samples.
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(kaiwang): Change name to ArrayToCustomEnumRanges.
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static std::vector<Sample> ArrayToCustomRanges(const Sample* values,
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 size_t num_values);
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CustomHistogram(const std::string& name,
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  const BucketRanges* ranges);
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // HistogramBase implementation:
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE;
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual double GetBucketSize(Count current, size_t i) const OVERRIDE;
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo(
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      PickleIterator* iter);
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static HistogramBase* DeserializeInfoImpl(PickleIterator* iter);
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges);
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static BucketRanges* CreateBucketRangesFromCustomRanges(
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<Sample>& custom_ranges);
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CustomHistogram);
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // BASE_METRICS_HISTOGRAM_H_
623