Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base.metrics; | 5 package org.chromium.base.metrics; |
| 6 | 6 |
| 7 import org.chromium.base.ChromiumBuildConfig; | |
| 8 import org.chromium.base.Log; | |
| 7 import org.chromium.base.VisibleForTesting; | 9 import org.chromium.base.VisibleForTesting; |
| 8 import org.chromium.base.annotations.JNINamespace; | 10 import org.chromium.base.annotations.JNINamespace; |
| 11 import org.chromium.base.annotations.RemovableInRelease; | |
| 9 | 12 |
| 10 import java.util.concurrent.TimeUnit; | 13 import java.util.concurrent.TimeUnit; |
| 11 | 14 |
| 12 /** | 15 /** |
| 13 * Java API for recording UMA histograms. Internally, the histogram will be cach ed by | 16 * Java API for recording UMA histograms. Internally, the histogram will be cach ed by |
| 14 * System.identityHashCode(name). | 17 * System.identityHashCode(name). |
| 15 * | 18 * |
| 16 * Note: the JNI calls are relatively costly - avoid calling these methods in pe rformance-critical | 19 * Note: the JNI calls are relatively costly - avoid calling these methods in pe rformance-critical |
| 17 * code. | 20 * code. |
| 18 */ | 21 */ |
| 19 @JNINamespace("base::android") | 22 @JNINamespace("base::android") |
| 20 public class RecordHistogram { | 23 public class RecordHistogram { |
| 24 private static final String TAG = "RecordHistogram"; | |
| 25 | |
| 26 private static boolean sNativeInitialized = false; | |
| 27 | |
| 28 @RemovableInRelease | |
| 29 private static void raiseExceptionInDebug(String name) { | |
|
jbudorick
2016/01/21 01:51:15
This no longer appears to be called.
| |
| 30 throw new RuntimeException("Calling RecordHistogram for " + name + " bef ore initialized."); | |
| 31 } | |
| 32 | |
| 33 /** | |
| 34 * Ensures that no jni exception is thrown in release builds when histograms are used before | |
| 35 * initialization. Just log a warning instead. | |
| 36 * @param name Name of the histogram. | |
| 37 * @return Whether histograms have been initialized. | |
| 38 */ | |
| 39 private static boolean isNativeInitialized(String name) { | |
| 40 if (!sNativeInitialized) { | |
| 41 if (!ChromiumBuildConfig.sIsDebug) { | |
| 42 Log.w(TAG, "Calling RecordHistogram for %s before initialized.", name); | |
| 43 } | |
| 44 } | |
| 45 return sNativeInitialized; | |
| 46 } | |
| 47 | |
| 21 /** | 48 /** |
| 22 * Records a sample in a boolean UMA histogram of the given name. Boolean hi stogram has two | 49 * Records a sample in a boolean UMA histogram of the given name. Boolean hi stogram has two |
| 23 * buckets, corresponding to success (true) and failure (false). This is the Java equivalent of | 50 * buckets, corresponding to success (true) and failure (false). This is the Java equivalent of |
| 24 * the UMA_HISTOGRAM_BOOLEAN C++ macro. | 51 * the UMA_HISTOGRAM_BOOLEAN C++ macro. |
| 25 * @param name name of the histogram | 52 * @param name name of the histogram |
| 26 * @param sample sample to be recorded, either true or false | 53 * @param sample sample to be recorded, either true or false |
| 27 */ | 54 */ |
| 28 public static void recordBooleanHistogram(String name, boolean sample) { | 55 public static void recordBooleanHistogram(String name, boolean sample) { |
| 29 nativeRecordBooleanHistogram(name, System.identityHashCode(name), sample ); | 56 if (isNativeInitialized(name)) { |
| 57 nativeRecordBooleanHistogram(name, System.identityHashCode(name), sa mple); | |
| 58 } | |
| 30 } | 59 } |
| 31 | 60 |
| 32 /** | 61 /** |
| 33 * Records a sample in an enumerated histogram of the given name and boundar y. Note that | 62 * Records a sample in an enumerated histogram of the given name and boundar y. Note that |
| 34 * |boundary| identifies the histogram - it should be the same at every invo cation. This is the | 63 * |boundary| identifies the histogram - it should be the same at every invo cation. This is the |
| 35 * Java equivalent of the UMA_HISTOGRAM_ENUMERATION C++ macro. | 64 * Java equivalent of the UMA_HISTOGRAM_ENUMERATION C++ macro. |
| 36 * @param name name of the histogram | 65 * @param name name of the histogram |
| 37 * @param sample sample to be recorded, at least 0 and at most |boundary| - 1 | 66 * @param sample sample to be recorded, at least 0 and at most |boundary| - 1 |
| 38 * @param boundary upper bound for legal sample values - all sample values h ave to be strictly | 67 * @param boundary upper bound for legal sample values - all sample values h ave to be strictly |
| 39 * lower than |boundary| | 68 * lower than |boundary| |
| 40 */ | 69 */ |
| 41 public static void recordEnumeratedHistogram(String name, int sample, int bo undary) { | 70 public static void recordEnumeratedHistogram(String name, int sample, int bo undary) { |
| 42 nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sam ple, boundary); | 71 if (isNativeInitialized(name)) { |
| 72 nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, boundary); | |
| 73 } | |
| 43 } | 74 } |
| 44 | 75 |
| 45 /** | 76 /** |
| 46 * Records a sample in a count histogram. This is the Java equivalent of the | 77 * Records a sample in a count histogram. This is the Java equivalent of the |
| 47 * UMA_HISTOGRAM_COUNTS C++ macro. | 78 * UMA_HISTOGRAM_COUNTS C++ macro. |
| 48 * @param name name of the histogram | 79 * @param name name of the histogram |
| 49 * @param sample sample to be recorded, at least 1 and at most 999999 | 80 * @param sample sample to be recorded, at least 1 and at most 999999 |
| 50 */ | 81 */ |
| 51 public static void recordCountHistogram(String name, int sample) { | 82 public static void recordCountHistogram(String name, int sample) { |
| 52 recordCustomCountHistogram(name, sample, 1, 1000000, 50); | 83 recordCustomCountHistogram(name, sample, 1, 1000000, 50); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 76 * Records a sample in a count histogram. This is the Java equivalent of the | 107 * Records a sample in a count histogram. This is the Java equivalent of the |
| 77 * UMA_HISTOGRAM_CUSTOM_COUNTS C++ macro. | 108 * UMA_HISTOGRAM_CUSTOM_COUNTS C++ macro. |
| 78 * @param name name of the histogram | 109 * @param name name of the histogram |
| 79 * @param sample sample to be recorded, at least |min| and at most |max| - 1 | 110 * @param sample sample to be recorded, at least |min| and at most |max| - 1 |
| 80 * @param min lower bound for expected sample values | 111 * @param min lower bound for expected sample values |
| 81 * @param max upper bounds for expected sample values | 112 * @param max upper bounds for expected sample values |
| 82 * @param numBuckets the number of buckets | 113 * @param numBuckets the number of buckets |
| 83 */ | 114 */ |
| 84 public static void recordCustomCountHistogram( | 115 public static void recordCustomCountHistogram( |
| 85 String name, int sample, int min, int max, int numBuckets) { | 116 String name, int sample, int min, int max, int numBuckets) { |
| 86 nativeRecordCustomCountHistogram( | 117 if (isNativeInitialized(name)) { |
| 87 name, System.identityHashCode(name), sample, min, max, numBucket s); | 118 nativeRecordCustomCountHistogram( |
| 119 name, System.identityHashCode(name), sample, min, max, numBu ckets); | |
| 120 } | |
| 88 } | 121 } |
| 89 | 122 |
| 90 /** | 123 /** |
| 91 * Records a sample in a linear histogram. This is the Java equivalent for u sing | 124 * Records a sample in a linear histogram. This is the Java equivalent for u sing |
| 92 * base::LinearHistogram. | 125 * base::LinearHistogram. |
| 93 * @param name name of the histogram | 126 * @param name name of the histogram |
| 94 * @param sample sample to be recorded, at least |min| and at most |max| - 1 . | 127 * @param sample sample to be recorded, at least |min| and at most |max| - 1 . |
| 95 * @param min lower bound for expected sample values, should be at least 1. | 128 * @param min lower bound for expected sample values, should be at least 1. |
| 96 * @param max upper bounds for expected sample values | 129 * @param max upper bounds for expected sample values |
| 97 * @param numBuckets the number of buckets | 130 * @param numBuckets the number of buckets |
| 98 */ | 131 */ |
| 99 public static void recordLinearCountHistogram( | 132 public static void recordLinearCountHistogram( |
| 100 String name, int sample, int min, int max, int numBuckets) { | 133 String name, int sample, int min, int max, int numBuckets) { |
| 101 nativeRecordLinearCountHistogram( | 134 if (isNativeInitialized(name)) { |
| 102 name, System.identityHashCode(name), sample, min, max, numBucket s); | 135 nativeRecordLinearCountHistogram( |
| 136 name, System.identityHashCode(name), sample, min, max, numBu ckets); | |
| 137 } | |
| 103 } | 138 } |
| 104 | 139 |
| 105 /** | 140 /** |
| 106 * Records a sample in a percentage histogram. This is the Java equivalent o f the | 141 * Records a sample in a percentage histogram. This is the Java equivalent o f the |
| 107 * UMA_HISTOGRAM_PERCENTAGE C++ macro. | 142 * UMA_HISTOGRAM_PERCENTAGE C++ macro. |
| 108 * @param name name of the histogram | 143 * @param name name of the histogram |
| 109 * @param sample sample to be recorded, at least 0 and at most 100. | 144 * @param sample sample to be recorded, at least 0 and at most 100. |
| 110 */ | 145 */ |
| 111 public static void recordPercentageHistogram(String name, int sample) { | 146 public static void recordPercentageHistogram(String name, int sample) { |
| 112 nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sam ple, 101); | 147 if (isNativeInitialized(name)) { |
| 148 nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, 101); | |
| 149 } | |
| 113 } | 150 } |
| 114 | 151 |
| 115 /** | 152 /** |
| 116 * Records a sparse histogram. This is the Java equivalent of UMA_HISTOGRAM_S PARSE_SLOWLY. | 153 * Records a sparse histogram. This is the Java equivalent of UMA_HISTOGRAM_S PARSE_SLOWLY. |
| 117 * @param name name of the histogram | 154 * @param name name of the histogram |
| 118 * @param sample sample to be recorded. All values of |sample| are valid, inc luding negative | 155 * @param sample sample to be recorded. All values of |sample| are valid, inc luding negative |
| 119 * values. | 156 * values. |
| 120 */ | 157 */ |
| 121 public static void recordSparseSlowlyHistogram(String name, int sample) { | 158 public static void recordSparseSlowlyHistogram(String name, int sample) { |
| 122 nativeRecordSparseHistogram(name, System.identityHashCode(name), sample) ; | 159 if (isNativeInitialized(name)) { |
| 160 nativeRecordSparseHistogram(name, System.identityHashCode(name), sam ple); | |
| 161 } | |
| 123 } | 162 } |
| 124 | 163 |
| 125 /** | 164 /** |
| 126 * Records a sample in a histogram of times. Useful for recording short dura tions. This is the | 165 * Records a sample in a histogram of times. Useful for recording short dura tions. This is the |
| 127 * Java equivalent of the UMA_HISTOGRAM_TIMES C++ macro. | 166 * Java equivalent of the UMA_HISTOGRAM_TIMES C++ macro. |
| 128 * @param name name of the histogram | 167 * @param name name of the histogram |
| 129 * @param duration duration to be recorded | 168 * @param duration duration to be recorded |
| 130 * @param timeUnit the unit of the duration argument | 169 * @param timeUnit the unit of the duration argument |
| 131 */ | 170 */ |
| 132 public static void recordTimesHistogram(String name, long duration, TimeUnit timeUnit) { | 171 public static void recordTimesHistogram(String name, long duration, TimeUnit timeUnit) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 * @param numBuckets the number of buckets | 208 * @param numBuckets the number of buckets |
| 170 */ | 209 */ |
| 171 public static void recordCustomTimesHistogram( | 210 public static void recordCustomTimesHistogram( |
| 172 String name, long duration, long min, long max, TimeUnit timeUnit, i nt numBuckets) { | 211 String name, long duration, long min, long max, TimeUnit timeUnit, i nt numBuckets) { |
| 173 recordCustomTimesHistogramMilliseconds(name, timeUnit.toMillis(duration) , | 212 recordCustomTimesHistogramMilliseconds(name, timeUnit.toMillis(duration) , |
| 174 timeUnit.toMillis(min), timeUnit.toMillis(max), numBuckets); | 213 timeUnit.toMillis(min), timeUnit.toMillis(max), numBuckets); |
| 175 } | 214 } |
| 176 | 215 |
| 177 private static void recordCustomTimesHistogramMilliseconds( | 216 private static void recordCustomTimesHistogramMilliseconds( |
| 178 String name, long duration, long min, long max, int numBuckets) { | 217 String name, long duration, long min, long max, int numBuckets) { |
| 179 nativeRecordCustomTimesHistogramMilliseconds( | 218 if (isNativeInitialized(name)) { |
| 180 name, System.identityHashCode(name), duration, min, max, numBuck ets); | 219 nativeRecordCustomTimesHistogramMilliseconds( |
| 220 name, System.identityHashCode(name), duration, min, max, num Buckets); | |
| 221 } | |
| 181 } | 222 } |
| 182 | 223 |
| 183 /** | 224 /** |
| 184 * Returns the number of samples recorded in the given bucket of the given h istogram. | 225 * Returns the number of samples recorded in the given bucket of the given h istogram. |
| 185 * @param name name of the histogram to look up | 226 * @param name name of the histogram to look up |
| 186 * @param sample the bucket containing this sample value will be looked up | 227 * @param sample the bucket containing this sample value will be looked up |
| 187 */ | 228 */ |
| 188 @VisibleForTesting | 229 @VisibleForTesting |
| 189 public static int getHistogramValueCountForTesting(String name, int sample) { | 230 public static int getHistogramValueCountForTesting(String name, int sample) { |
| 231 // Should fail if native is not loaded since this is testing-only. | |
| 190 return nativeGetHistogramValueCountForTesting(name, sample); | 232 return nativeGetHistogramValueCountForTesting(name, sample); |
| 191 } | 233 } |
| 192 | 234 |
| 193 /** | 235 /** |
| 194 * Initializes the metrics system. | 236 * Initializes the metrics system. |
| 195 */ | 237 */ |
| 196 public static void initialize() { | 238 public static void initialize() { |
| 197 nativeInitialize(); | 239 nativeInitialize(); |
| 240 sNativeInitialized = true; | |
| 198 } | 241 } |
| 199 | 242 |
| 200 private static native void nativeRecordCustomTimesHistogramMilliseconds( | 243 private static native void nativeRecordCustomTimesHistogramMilliseconds( |
| 201 String name, int key, long duration, long min, long max, int numBuck ets); | 244 String name, int key, long duration, long min, long max, int numBuck ets); |
| 202 | 245 |
| 203 private static native void nativeRecordBooleanHistogram(String name, int key , boolean sample); | 246 private static native void nativeRecordBooleanHistogram(String name, int key , boolean sample); |
| 204 private static native void nativeRecordEnumeratedHistogram( | 247 private static native void nativeRecordEnumeratedHistogram( |
| 205 String name, int key, int sample, int boundary); | 248 String name, int key, int sample, int boundary); |
| 206 private static native void nativeRecordCustomCountHistogram( | 249 private static native void nativeRecordCustomCountHistogram( |
| 207 String name, int key, int sample, int min, int max, int numBuckets); | 250 String name, int key, int sample, int min, int max, int numBuckets); |
| 208 private static native void nativeRecordLinearCountHistogram( | 251 private static native void nativeRecordLinearCountHistogram( |
| 209 String name, int key, int sample, int min, int max, int numBuckets); | 252 String name, int key, int sample, int min, int max, int numBuckets); |
| 210 private static native void nativeRecordSparseHistogram(String name, int key, int sample); | 253 private static native void nativeRecordSparseHistogram(String name, int key, int sample); |
| 211 | 254 |
| 212 private static native int nativeGetHistogramValueCountForTesting(String name , int sample); | 255 private static native int nativeGetHistogramValueCountForTesting(String name , int sample); |
| 213 private static native void nativeInitialize(); | 256 private static native void nativeInitialize(); |
| 214 } | 257 } |
| OLD | NEW |