• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 The Chromium Authors
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)                         \
47   CACHE_HISTOGRAM_CUSTOM_TIMES(name, sample, base::Milliseconds(1), \
48                                base::Seconds(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_1M, 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_1M("DiskCache.2.MyName", 20);  // "2" is the CacheType.
88 //  UMA_HISTOGRAM_COUNTS_1M("DiskCache.2.MyExperiment_530", 55);
89 //
90 #define CACHE_UMA(type, name, experiment, sample)                    \
91   {                                                                  \
92     const std::string my_name =                                      \
93         CACHE_UMA_BACKEND_IMPL_OBJ->HistogramName(name, experiment); \
94     switch (CACHE_UMA_BACKEND_IMPL_OBJ->GetCacheType()) {            \
95       case net::REMOVED_MEDIA_CACHE:                                 \
96       default:                                                       \
97         NOTREACHED();                                                \
98         [[fallthrough]];                                                 \
99       case net::DISK_CACHE:                                          \
100       case net::APP_CACHE:                                           \
101       case net::SHADER_CACHE:                                        \
102       case net::PNACL_CACHE:                                         \
103         CACHE_HISTOGRAM_##type(my_name.data(), sample);              \
104         break;                                                       \
105       case net::GENERATED_BYTE_CODE_CACHE:                           \
106       case net::GENERATED_NATIVE_CODE_CACHE:                         \
107       case net::GENERATED_WEBUI_BYTE_CODE_CACHE:                     \
108         break;                                                       \
109     }                                                                \
110   }
111 
112 #endif  // NET_DISK_CACHE_BLOCKFILE_HISTOGRAM_MACROS_H_
113