1// Copyright 2014 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_V3_H_
12#define NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_V3_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      base::HistogramBase* counter(NULL); \
23      if (!counter || name != counter->histogram_name()) \
24        counter = base::Histogram::FactoryGet( \
25            name, min, max, bucket_count, \
26            base::Histogram::kUmaTargetedHistogramFlag); \
27      counter->Add(sample); \
28    } while (0)
29
30#define CACHE_HISTOGRAM_COUNTS(name, sample) CACHE_HISTOGRAM_CUSTOM_COUNTS( \
31    name, sample, 1, 1000000, 50)
32
33#define CACHE_HISTOGRAM_COUNTS_10000(name, sample) \
34    CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 10000, 50)
35
36#define CACHE_HISTOGRAM_COUNTS_50000(name, sample) \
37    CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 50000000, 50)
38
39#define CACHE_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
40    do { \
41      base::HistogramBase* counter(NULL); \
42      if (!counter || name != counter->histogram_name()) \
43        counter = base::Histogram::FactoryTimeGet( \
44            name, min, max, bucket_count, \
45            base::Histogram::kUmaTargetedHistogramFlag); \
46      counter->AddTime(sample); \
47    } while (0)
48
49#define CACHE_HISTOGRAM_TIMES(name, sample) CACHE_HISTOGRAM_CUSTOM_TIMES( \
50    name, sample, base::TimeDelta::FromMilliseconds(1), \
51    base::TimeDelta::FromSeconds(10), 50)
52
53#define CACHE_HISTOGRAM_ENUMERATION(name, sample, boundary_value) do { \
54    base::HistogramBase* counter(NULL); \
55    if (!counter || name != counter->histogram_name()) \
56      counter = base::LinearHistogram::FactoryGet( \
57                    name, 1, boundary_value, boundary_value + 1, \
58                    base::Histogram::kUmaTargetedHistogramFlag); \
59    counter->Add(sample); \
60  } while (0)
61
62#define CACHE_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \
63    CACHE_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101)
64
65// -----------------------------------------------------------------------------
66
67// HISTOGRAM_HOURS will collect time related data with a granularity of hours
68// and normal values of a few months.
69#define CACHE_HISTOGRAM_HOURS CACHE_HISTOGRAM_COUNTS_10000
70
71// HISTOGRAM_AGE will collect time elapsed since |initial_time|, with a
72// granularity of hours and normal values of a few months.
73#define CACHE_HISTOGRAM_AGE(name, initial_time) \
74    CACHE_HISTOGRAM_COUNTS_10000(name, \
75                                 (base::Time::Now() - initial_time).InHours())
76
77// HISTOGRAM_AGE_MS will collect time elapsed since |initial_time|, with the
78// normal resolution of the UMA_HISTOGRAM_TIMES.
79#define CACHE_HISTOGRAM_AGE_MS(name, initial_time)\
80    CACHE_HISTOGRAM_TIMES(name, base::TimeTicks::Now() - initial_time)
81
82#define CACHE_HISTOGRAM_CACHE_ERROR(name, sample) \
83    CACHE_HISTOGRAM_ENUMERATION(name, sample, 50)
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", 20);
90// which may translate to:
91//  UMA_HISTOGRAM_COUNTS("DiskCache3.MyName_AppCache", 20);
92//
93#define CACHE_UMA(type, name, sample) {\
94    const std::string my_name =\
95        CACHE_UMA_BACKEND_IMPL_OBJ->HistogramName(name);\
96    switch (CACHE_UMA_BACKEND_IMPL_OBJ->cache_type()) {\
97      case net::DISK_CACHE:\
98      case net::MEDIA_CACHE:\
99      case net::APP_CACHE:\
100      case net::SHADER_CACHE:\
101      case net::PNACL_CACHE:\
102        CACHE_HISTOGRAM_##type(my_name.data(), sample);\
103        break;\
104      default:\
105        break;\
106    }\
107  }
108
109#endif  // NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_V3_H_
110