OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #ifndef BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_ |
| 6 #define BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_ |
| 7 |
| 8 #include "base/atomicops.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/time/time.h" |
| 12 |
| 13 // This is for macros internal to base/metrics. They should not be used outside |
| 14 // of this directory. For writing to UMA histograms, see histogram_macros.h. |
| 15 |
| 16 // TODO(rkaplow): Improve commenting of these methods. |
| 17 |
| 18 //------------------------------------------------------------------------------ |
| 19 // Histograms are often put in areas where they are called many many times, and |
| 20 // performance is critical. As a result, they are designed to have a very low |
| 21 // recurring cost of executing (adding additional samples). Toward that end, |
| 22 // the macros declare a static pointer to the histogram in question, and only |
| 23 // take a "slow path" to construct (or find) the histogram on the first run |
| 24 // through the macro. We leak the histograms at shutdown time so that we don't |
| 25 // have to validate using the pointers at any time during the running of the |
| 26 // process. |
| 27 |
| 28 |
| 29 // In some cases (integration into 3rd party code), it's useful to separate the |
| 30 // definition of |atomic_histogram_pointer| from its use. To achieve this we |
| 31 // define HISTOGRAM_POINTER_USE, which uses an |atomic_histogram_pointer|, and |
| 32 // STATIC_HISTOGRAM_POINTER_BLOCK, which defines an |atomic_histogram_pointer| |
| 33 // and forwards to HISTOGRAM_POINTER_USE. |
| 34 #define HISTOGRAM_POINTER_USE(atomic_histogram_pointer, \ |
| 35 constant_histogram_name, \ |
| 36 histogram_add_method_invocation, \ |
| 37 histogram_factory_get_invocation) \ |
| 38 do { \ |
| 39 /* \ |
| 40 * Acquire_Load() ensures that we acquire visibility to the \ |
| 41 * pointed-to data in the histogram. \ |
| 42 */ \ |
| 43 base::HistogramBase* histogram_pointer( \ |
| 44 reinterpret_cast<base::HistogramBase*>( \ |
| 45 base::subtle::Acquire_Load(atomic_histogram_pointer))); \ |
| 46 if (!histogram_pointer) { \ |
| 47 /* \ |
| 48 * This is the slow path, which will construct OR find the \ |
| 49 * matching histogram. histogram_factory_get_invocation includes \ |
| 50 * locks on a global histogram name map and is completely thread \ |
| 51 * safe. \ |
| 52 */ \ |
| 53 histogram_pointer = histogram_factory_get_invocation; \ |
| 54 \ |
| 55 /* \ |
| 56 * Use Release_Store to ensure that the histogram data is made \ |
| 57 * available globally before we make the pointer visible. Several \ |
| 58 * threads may perform this store, but the same value will be \ |
| 59 * stored in all cases (for a given named/spec'ed histogram). \ |
| 60 * We could do this without any barrier, since FactoryGet entered \ |
| 61 * and exited a lock after construction, but this barrier makes \ |
| 62 * things clear. \ |
| 63 */ \ |
| 64 base::subtle::Release_Store( \ |
| 65 atomic_histogram_pointer, \ |
| 66 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); \ |
| 67 } \ |
| 68 if (DCHECK_IS_ON()) \ |
| 69 histogram_pointer->CheckName(constant_histogram_name); \ |
| 70 histogram_pointer->histogram_add_method_invocation; \ |
| 71 } while (0) |
| 72 |
| 73 // This is a helper macro used by other macros and shouldn't be used directly. |
| 74 // Defines the static |atomic_histogram_pointer| and forwards to |
| 75 // HISTOGRAM_POINTER_USE. |
| 76 #define STATIC_HISTOGRAM_POINTER_BLOCK(constant_histogram_name, \ |
| 77 histogram_add_method_invocation, \ |
| 78 histogram_factory_get_invocation) \ |
| 79 do { \ |
| 80 /* \ |
| 81 * The pointer's presence indicates that the initialization is complete. \ |
| 82 * Initialization is idempotent, so it can safely be atomically repeated. \ |
| 83 */ \ |
| 84 static base::subtle::AtomicWord atomic_histogram_pointer = 0; \ |
| 85 HISTOGRAM_POINTER_USE(&atomic_histogram_pointer, constant_histogram_name, \ |
| 86 histogram_add_method_invocation, \ |
| 87 histogram_factory_get_invocation); \ |
| 88 } while (0) |
| 89 |
| 90 // This is a helper macro used by other macros and shouldn't be used directly. |
| 91 #define INTERNAL_HISTOGRAM_CUSTOM_COUNTS_WITH_FLAG(name, sample, min, max, \ |
| 92 bucket_count, flag) \ |
| 93 STATIC_HISTOGRAM_POINTER_BLOCK( \ |
| 94 name, Add(sample), \ |
| 95 base::Histogram::FactoryGet(name, min, max, bucket_count, flag)) |
| 96 |
| 97 // This is a helper macro used by other macros and shouldn't be used directly. |
| 98 // One additional bucket is created in the LinearHistogram for the illegal |
| 99 // values >= boundary_value so that mistakes in calling the UMA enumeration |
| 100 // macros can be detected. |
| 101 #define INTERNAL_HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \ |
| 102 STATIC_HISTOGRAM_POINTER_BLOCK( \ |
| 103 name, Add(sample), \ |
| 104 base::LinearHistogram::FactoryGet( \ |
| 105 name, 1, boundary, boundary + 1, flag)) |
| 106 |
| 107 // This is a helper macro used by other macros and shouldn't be used directly. |
| 108 // This is necessary to expand __COUNTER__ to an actual value. |
| 109 #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, is_long, key) \ |
| 110 INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) |
| 111 |
| 112 // This is a helper macro used by other macros and shouldn't be used directly. |
| 113 #define INTERNAL_SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, is_long, key) \ |
| 114 class ScopedHistogramTimer##key { \ |
| 115 public: \ |
| 116 ScopedHistogramTimer##key() : constructed_(base::TimeTicks::Now()) {} \ |
| 117 ~ScopedHistogramTimer##key() { \ |
| 118 base::TimeDelta elapsed = base::TimeTicks::Now() - constructed_; \ |
| 119 if (is_long) { \ |
| 120 UMA_HISTOGRAM_LONG_TIMES_100(name, elapsed); \ |
| 121 } else { \ |
| 122 UMA_HISTOGRAM_TIMES(name, elapsed); \ |
| 123 } \ |
| 124 } \ |
| 125 private: \ |
| 126 base::TimeTicks constructed_; \ |
| 127 } scoped_histogram_timer_##key |
| 128 |
| 129 #endif // BASE_METRICS_HISTOGRAM_MACROS_INTERNAL_H_ |
OLD | NEW |