Chromium Code Reviews| Index: base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
| diff --git a/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java b/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
| index c1b04892e87362dfc459bcbacdb75d817f4fb975..fcff68ee07ce51c44524afbc2f294bee3bf5df00 100644 |
| --- a/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
| +++ b/base/android/java/src/org/chromium/base/metrics/RecordHistogram.java |
| @@ -4,8 +4,11 @@ |
| package org.chromium.base.metrics; |
| +import org.chromium.base.ChromiumBuildConfig; |
| +import org.chromium.base.Log; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.base.annotations.RemovableInRelease; |
| import java.util.concurrent.TimeUnit; |
| @@ -18,6 +21,30 @@ import java.util.concurrent.TimeUnit; |
| */ |
| @JNINamespace("base::android") |
| public class RecordHistogram { |
| + private static final String TAG = "RecordHistogram"; |
| + |
| + private static boolean sNativeInitialized = false; |
| + |
| + @RemovableInRelease |
| + private static void raiseExceptionInDebug(String name) { |
|
jbudorick
2016/01/21 01:51:15
This no longer appears to be called.
|
| + throw new RuntimeException("Calling RecordHistogram for " + name + " before initialized."); |
| + } |
| + |
| + /** |
| + * Ensures that no jni exception is thrown in release builds when histograms are used before |
| + * initialization. Just log a warning instead. |
| + * @param name Name of the histogram. |
| + * @return Whether histograms have been initialized. |
| + */ |
| + private static boolean isNativeInitialized(String name) { |
| + if (!sNativeInitialized) { |
| + if (!ChromiumBuildConfig.sIsDebug) { |
| + Log.w(TAG, "Calling RecordHistogram for %s before initialized.", name); |
| + } |
| + } |
| + return sNativeInitialized; |
| + } |
| + |
| /** |
| * Records a sample in a boolean UMA histogram of the given name. Boolean histogram has two |
| * buckets, corresponding to success (true) and failure (false). This is the Java equivalent of |
| @@ -26,7 +53,9 @@ public class RecordHistogram { |
| * @param sample sample to be recorded, either true or false |
| */ |
| public static void recordBooleanHistogram(String name, boolean sample) { |
| - nativeRecordBooleanHistogram(name, System.identityHashCode(name), sample); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordBooleanHistogram(name, System.identityHashCode(name), sample); |
| + } |
| } |
| /** |
| @@ -39,7 +68,9 @@ public class RecordHistogram { |
| * lower than |boundary| |
| */ |
| public static void recordEnumeratedHistogram(String name, int sample, int boundary) { |
| - nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, boundary); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, boundary); |
| + } |
| } |
| /** |
| @@ -83,8 +114,10 @@ public class RecordHistogram { |
| */ |
| public static void recordCustomCountHistogram( |
| String name, int sample, int min, int max, int numBuckets) { |
| - nativeRecordCustomCountHistogram( |
| - name, System.identityHashCode(name), sample, min, max, numBuckets); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordCustomCountHistogram( |
| + name, System.identityHashCode(name), sample, min, max, numBuckets); |
| + } |
| } |
| /** |
| @@ -98,8 +131,10 @@ public class RecordHistogram { |
| */ |
| public static void recordLinearCountHistogram( |
| String name, int sample, int min, int max, int numBuckets) { |
| - nativeRecordLinearCountHistogram( |
| - name, System.identityHashCode(name), sample, min, max, numBuckets); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordLinearCountHistogram( |
| + name, System.identityHashCode(name), sample, min, max, numBuckets); |
| + } |
| } |
| /** |
| @@ -109,7 +144,9 @@ public class RecordHistogram { |
| * @param sample sample to be recorded, at least 0 and at most 100. |
| */ |
| public static void recordPercentageHistogram(String name, int sample) { |
| - nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, 101); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordEnumeratedHistogram(name, System.identityHashCode(name), sample, 101); |
| + } |
| } |
| /** |
| @@ -119,7 +156,9 @@ public class RecordHistogram { |
| * values. |
| */ |
| public static void recordSparseSlowlyHistogram(String name, int sample) { |
| - nativeRecordSparseHistogram(name, System.identityHashCode(name), sample); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordSparseHistogram(name, System.identityHashCode(name), sample); |
| + } |
| } |
| /** |
| @@ -176,8 +215,10 @@ public class RecordHistogram { |
| private static void recordCustomTimesHistogramMilliseconds( |
| String name, long duration, long min, long max, int numBuckets) { |
| - nativeRecordCustomTimesHistogramMilliseconds( |
| - name, System.identityHashCode(name), duration, min, max, numBuckets); |
| + if (isNativeInitialized(name)) { |
| + nativeRecordCustomTimesHistogramMilliseconds( |
| + name, System.identityHashCode(name), duration, min, max, numBuckets); |
| + } |
| } |
| /** |
| @@ -187,6 +228,7 @@ public class RecordHistogram { |
| */ |
| @VisibleForTesting |
| public static int getHistogramValueCountForTesting(String name, int sample) { |
| + // Should fail if native is not loaded since this is testing-only. |
| return nativeGetHistogramValueCountForTesting(name, sample); |
| } |
| @@ -195,6 +237,7 @@ public class RecordHistogram { |
| */ |
| public static void initialize() { |
| nativeInitialize(); |
| + sNativeInitialized = true; |
| } |
| private static native void nativeRecordCustomTimesHistogramMilliseconds( |