histogram_macros.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
1// Copyright (c) 2011 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.
4
5// This file contains macros to simplify histogram reporting from the disk
6// cache. The main issue is that we want to have separate histograms for each
7// type of cache (regular vs. media, etc), without adding the complexity of
8// keeping track of a potentially large number of histogram objects that have to
9// survive the backend object that created them.
10
11#ifndef NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_H_
12#define NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_H_
13
14// -----------------------------------------------------------------------------
15
16// These histograms follow the definition of UMA_HISTOGRAMN_XXX except that
17// the counter is not cached locally.
18
19#define CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
20    do { \
21      base::HistogramBase* counter = base::Histogram::FactoryGet( \
22            name, min, max, bucket_count, \
23            base::Histogram::kUmaTargetedHistogramFlag); \
24      counter->Add(sample); \
25    } while (0)
26
27#define CACHE_HISTOGRAM_COUNTS(name, sample) CACHE_HISTOGRAM_CUSTOM_COUNTS( \
28    name, sample, 1, 1000000, 50)
29
30#define CACHE_HISTOGRAM_COUNTS_10000(name, sample) \
31    CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50)
32
33#define CACHE_HISTOGRAM_COUNTS_50000(name, sample) \
34    CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 50000000, 50)
35
36#define CACHE_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
37    do { \
38      base::HistogramBase* counter = base::Histogram::FactoryTimeGet( \
39            name, min, max, bucket_count, \
40            base::Histogram::kUmaTargetedHistogramFlag); \
41      counter->AddTime(sample); \
42    } while (0)
43
44#define CACHE_HISTOGRAM_TIMES(name, sample) CACHE_HISTOGRAM_CUSTOM_TIMES( \
45    name, sample, base::TimeDelta::FromMilliseconds(1), \
46    base::TimeDelta::FromSeconds(10), 50)
47
48#define CACHE_HISTOGRAM_ENUMERATION(name, sample, boundary_value) do { \
49    base::HistogramBase* counter = base::LinearHistogram::FactoryGet( \
50                    name, 1, boundary_value, boundary_value + 1, \
51                    base::Histogram::kUmaTargetedHistogramFlag); \
52    counter->Add(sample); \
53  } while (0)
54
55#define CACHE_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
56    CACHE_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
57
58// -----------------------------------------------------------------------------
59
60// HISTOGRAM_HOURS will collect time related data with a granularity of hours
61// and normal values of a few months.
62#define CACHE_HISTOGRAM_HOURS CACHE_HISTOGRAM_COUNTS_10000
63
64// HISTOGRAM_AGE will collect time elapsed since |initial_time|, with a
65// granularity of hours and normal values of a few months.
66#define CACHE_HISTOGRAM_AGE(name, initial_time) \
67    CACHE_HISTOGRAM_COUNTS_10000(name, \
68                                 (base::Time::Now() - initial_time).InHours())
69
70// HISTOGRAM_AGE_MS will collect time elapsed since |initial_time|, with the
71// normal resolution of the UMA_HISTOGRAM_TIMES.
72#define CACHE_HISTOGRAM_AGE_MS(name, initial_time)\
73    CACHE_HISTOGRAM_TIMES(name, base::TimeTicks::Now() - initial_time)
74
75#define CACHE_HISTOGRAM_CACHE_ERROR(name, sample) \
76    CACHE_HISTOGRAM_ENUMERATION(name, sample, 50)
77
78// Generates a UMA histogram of the given type, generating the proper name for
79// it (asking backend_->HistogramName), and adding the provided sample.
80// For example, to generate a regualar UMA_HISTOGRAM_COUNTS, this macro would
81// be used as:
82//  CACHE_UMA(COUNTS, "MyName", 0, 20);
83//  CACHE_UMA(COUNTS, "MyExperiment", 530, 55);
84// which roughly translates to:
85//  UMA_HISTOGRAM_COUNTS("DiskCache.2.MyName", 20);  // "2" is the CacheType.
86//  UMA_HISTOGRAM_COUNTS("DiskCache.2.MyExperiment_530", 55);
87//
88#define CACHE_UMA(type, name, experiment, sample) {\
89    const std::string my_name =\
90        CACHE_UMA_BACKEND_IMPL_OBJ->HistogramName(name, experiment);\
91    switch (CACHE_UMA_BACKEND_IMPL_OBJ->cache_type()) {\
92      default:\
93        NOTREACHED();\
94        /* Fall-through. */\
95      case net::DISK_CACHE:\
96      case net::MEDIA_CACHE:\
97      case net::APP_CACHE:\
98      case net::SHADER_CACHE:\
99      case net::PNACL_CACHE:\
100        CACHE_HISTOGRAM_##type(my_name.data(), sample);\
101        break;\
102    }\
103  }
104
105#endif  // NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_H_
106