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#include "base/metrics/histogram.h"
15
16// -----------------------------------------------------------------------------
17
18// These histograms follow the definition of UMA_HISTOGRAMN_XXX except that
19// the counter is not cached locally.
20
21#define CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
22    do { \
23      base::HistogramBase* counter = base::Histogram::FactoryGet( \
24            name, min, max, bucket_count, \
25            base::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_COUNTS_50000(name, sample) \
36    CACHE_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 50000000, 50)
37
38#define CACHE_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
39    do { \
40      base::HistogramBase* counter = base::Histogram::FactoryTimeGet( \
41            name, min, max, bucket_count, \
42            base::Histogram::kUmaTargetedHistogramFlag); \
43      counter->AddTime(sample); \
44    } while (0)
45
46#define CACHE_HISTOGRAM_TIMES(name, sample) CACHE_HISTOGRAM_CUSTOM_TIMES( \
47    name, sample, base::TimeDelta::FromMilliseconds(1), \
48    base::TimeDelta::FromSeconds(10), 50)
49
50#define CACHE_HISTOGRAM_ENUMERATION(name, sample, boundary_value) do { \
51    base::HistogramBase* counter = base::LinearHistogram::FactoryGet( \
52                    name, 1, boundary_value, boundary_value + 1, \
53                    base::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, \
70                                 (base::Time::Now() - initial_time).InHours())
71
72// HISTOGRAM_AGE_MS will collect time elapsed since |initial_time|, with the
73// normal resolution of the UMA_HISTOGRAM_TIMES.
74#define CACHE_HISTOGRAM_AGE_MS(name, initial_time)\
75    CACHE_HISTOGRAM_TIMES(name, base::TimeTicks::Now() - initial_time)
76
77#define CACHE_HISTOGRAM_CACHE_ERROR(name, sample) \
78    CACHE_HISTOGRAM_ENUMERATION(name, sample, 50)
79
80// Generates a UMA histogram of the given type, generating the proper name for
81// it (asking backend_->HistogramName), and adding the provided sample.
82// For example, to generate a regualar UMA_HISTOGRAM_COUNTS, this macro would
83// be used as:
84//  CACHE_UMA(COUNTS, "MyName", 0, 20);
85//  CACHE_UMA(COUNTS, "MyExperiment", 530, 55);
86// which roughly translates to:
87//  UMA_HISTOGRAM_COUNTS("DiskCache.2.MyName", 20);  // "2" is the CacheType.
88//  UMA_HISTOGRAM_COUNTS("DiskCache.2.MyExperiment_530", 55);
89//
90#define CACHE_UMA(type, name, experiment, sample) {\
91    const std::string my_name =\
92        CACHE_UMA_BACKEND_IMPL_OBJ->HistogramName(name, experiment);\
93    switch (CACHE_UMA_BACKEND_IMPL_OBJ->cache_type()) {\
94      default:\
95        NOTREACHED();\
96        /* Fall-through. */\
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    }\
105  }
106
107#endif  // NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_H_
108