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