histogram_macros.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2009 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_HISTOGRAM_MACROS_H_
12#define NET_DISK_CACHE_HISTOGRAM_MACROS_H_
13
14// -----------------------------------------------------------------------------
15
16// These histograms follow the definition of UMA_HISTOGRAMN_XXX except that
17// whenever the name changes (the experiment group changes), the histrogram
18// object is re-created.
19
20#define CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
21    do { \
22      static scoped_refptr<Histogram> counter; \
23      if (!counter || name != counter->histogram_name()) \
24        counter = Histogram::FactoryGet(name, min, max, bucket_count, \
25                                        Histogram::kUmaTargetedHistogramFlag); \
26      counter->Add(sample); \
27    } while (0)
28
29#define CACHE_HISTOGRAM_COUNTS(name, sample) CACHE_HISTOGRAM_CUSTOM_COUNTS( \
30    name, sample, 1, 1000000, 50)
31
32#define CACHE_HISTOGRAM_COUNTS_10000(name, sample) \
33    CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50)
34
35#define CACHE_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
36    do { \
37      static scoped_refptr<Histogram> counter; \
38      if (!counter || name != counter->histogram_name()) \
39        counter = Histogram::FactoryTimeGet(name, min, max, bucket_count, \
40                      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    static scoped_refptr<Histogram> counter; \
50    if (!counter || name != counter->histogram_name()) \
51      counter = LinearHistogram::FactoryGet( \
52                    name, 1, boundary_value, boundary_value + 1, \
53                    Histogram::kUmaTargetedHistogramFlag); \
54    counter->Add(sample); \
55  } while (0)
56
57#define CACHE_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
58    CACHE_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
59
60// -----------------------------------------------------------------------------
61
62// HISTOGRAM_HOURS will collect time related data with a granularity of hours
63// and normal values of a few months.
64#define CACHE_HISTOGRAM_HOURS CACHE_HISTOGRAM_COUNTS_10000
65
66// HISTOGRAM_AGE will collect time elapsed since |initial_time|, with a
67// granularity of hours and normal values of a few months.
68#define CACHE_HISTOGRAM_AGE(name, initial_time) \
69    CACHE_HISTOGRAM_COUNTS_10000(name, (Time::Now() - initial_time).InHours())
70
71// HISTOGRAM_AGE_MS will collect time elapsed since |initial_time|, with the
72// normal resolution of the UMA_HISTOGRAM_TIMES.
73#define CACHE_HISTOGRAM_AGE_MS(name, initial_time)\
74    CACHE_HISTOGRAM_TIMES(name, TimeTicks::Now() - initial_time)
75
76#define CACHE_HISTOGRAM_CACHE_ERROR(name, sample) \
77    CACHE_HISTOGRAM_ENUMERATION(name, sample, 50)
78
79#ifdef NET_DISK_CACHE_BACKEND_IMPL_CC_
80#define BACKEND_OBJ this
81#else
82#define BACKEND_OBJ backend_
83#endif
84
85// Generates a UMA histogram of the given type, generating the proper name for
86// it (asking backend_->HistogramName), and adding the provided sample.
87// For example, to generate a regualar UMA_HISTOGRAM_COUNTS, this macro would
88// be used as:
89//  CACHE_UMA(COUNTS, "MyName", 0, 20);
90//  CACHE_UMA(COUNTS, "MyExperiment", 530, 55);
91// which roughly translates to:
92//  UMA_HISTOGRAM_COUNTS("DiskCache.2.MyName", 20);  // "2" is the CacheType.
93//  UMA_HISTOGRAM_COUNTS("DiskCache.2.MyExperiment_530", 55);
94//
95#define CACHE_UMA(type, name, experiment, sample) {\
96    const std::string my_name = BACKEND_OBJ->HistogramName(name, experiment);\
97    switch (BACKEND_OBJ->cache_type()) {\
98      case net::DISK_CACHE:\
99        CACHE_HISTOGRAM_##type(my_name.data(), sample);\
100        break;\
101      case net::MEDIA_CACHE:\
102        CACHE_HISTOGRAM_##type(my_name.data(), sample);\
103        break;\
104      case net::APP_CACHE:\
105        CACHE_HISTOGRAM_##type(my_name.data(), sample);\
106        break;\
107      default:\
108        NOTREACHED();\
109        break;\
110    }\
111  }
112
113#endif  // NET_DISK_CACHE_HISTOGRAM_MACROS_H_
114