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