Chromium Code Reviews| Index: base/android/java/src/org/chromium/base/TraceEvent.java |
| diff --git a/base/android/java/src/org/chromium/base/TraceEvent.java b/base/android/java/src/org/chromium/base/TraceEvent.java |
| index 3d3b11a04196cf40fe1179c3104eacd22c3285b7..ab097e29b737a3e19677e141b5cf137789692240 100644 |
| --- a/base/android/java/src/org/chromium/base/TraceEvent.java |
| +++ b/base/android/java/src/org/chromium/base/TraceEvent.java |
| @@ -9,16 +9,15 @@ import android.os.MessageQueue; |
| import android.os.SystemClock; |
| import android.util.Log; |
| import android.util.Printer; |
| + |
| /** |
| * Java mirror of Chrome trace event API. See base/trace_event/trace_event.h. Unlike the native |
| * version, Java does not have stack objects, so a TRACE_EVENT() which does both TRACE_EVENT_BEGIN() |
| * and TRACE_EVENT_END() in ctor/dtor is not possible. |
| - * It is OK to use tracing before the native library has loaded, but such traces will |
| - * be ignored. (Perhaps we could devise to buffer them up in future?). |
| + * Note that this class can also be used before the native library is loaded for early tracing. |
| */ |
| @JNINamespace("base::android") |
| public class TraceEvent { |
| - |
| private static volatile boolean sEnabled = false; |
| private static class BasicLooperMonitor implements Printer { |
| @@ -175,12 +174,26 @@ public class TraceEvent { |
| */ |
| @CalledByNative |
| public static void setEnabled(boolean enabled) { |
| + if (!enabled) EarlyTraceEvent.disable(); |
| + |
| sEnabled = enabled; |
| ThreadUtils.getUiThreadLooper().setMessageLogging( |
| enabled ? LooperMonitorHolder.sInstance : null); |
| } |
| /** |
| + * Can be called early during the application initialization so that TraceEvent can be used |
| + * before the native library is loaded. |
| + */ |
| + public static void enableEarlyTracing() { |
| + EarlyTraceEvent.enable(); |
| + } |
| + |
| + private static void disableEarlyTracing() { |
| + EarlyTraceEvent.disable(); |
| + } |
| + |
| + /** |
| * Enables or disabled Android systrace path of Chrome tracing. If enabled, all Chrome |
| * traces will be also output to Android systrace. Because of the overhead of Android |
| * systrace, this is for WebView only. |
| @@ -200,7 +213,7 @@ public class TraceEvent { |
| * is enabled. |
| */ |
| public static boolean enabled() { |
| - return sEnabled; |
| + return sEnabled || EarlyTraceEvent.isEnabled(); |
| } |
| /** |
| @@ -243,7 +256,7 @@ public class TraceEvent { |
| * @param name The name of the event. |
| */ |
| public static void begin(String name) { |
| - if (sEnabled) nativeBegin(name, null); |
| + if (enabled()) begin(name, null); |
| } |
| /** |
| @@ -252,7 +265,20 @@ public class TraceEvent { |
| * @param arg The arguments of the event. |
| */ |
| public static void begin(String name, String arg) { |
| - if (sEnabled) nativeBegin(name, arg); |
| + if (sEnabled) { |
| + nativeBegin(name, arg); |
| + return; |
| + } |
| + |
| + EarlyTraceEvent.begin(name, arg); |
| + } |
| + |
| + /** |
| + * Convenience wrapper around the versions of end() that take string parameters. |
| + * @see #begin() |
| + */ |
| + public static void end() { |
| + if (enabled()) end(getCallerName(), null); |
| } |
| /** |
| @@ -260,7 +286,7 @@ public class TraceEvent { |
| * @param name The name of the event. |
| */ |
| public static void end(String name) { |
| - if (sEnabled) nativeEnd(name, null); |
| + if (enabled()) end(name, null); |
| } |
| /** |
| @@ -269,9 +295,28 @@ public class TraceEvent { |
| * @param arg The arguments of the event. |
| */ |
| public static void end(String name, String arg) { |
| + if (!enabled()) return; |
| + |
| + if (EarlyTraceEvent.end(name, arg)) return; |
| + |
| if (sEnabled) nativeEnd(name, arg); |
| } |
| + static String getCallerName() { |
| + // This was measured to take about 1ms on Trygon device. |
|
dsinclair
2015/03/16 14:41:23
nit: No idea what Trygon is ....
And, what was 1m
Benoit L
2015/03/19 16:50:47
This code is not actually required, so it's gone.
|
| + StackTraceElement[] stack = java.lang.Thread.currentThread().getStackTrace(); |
| + |
| + // Commented out to avoid excess call overhead, but these lines can be useful to debug |
| + // exactly where the TraceEvent's client is on the callstack. |
| + // int index = 0; |
| + // while (!stack[index].getClassName().equals(TraceEvent.class.getName())) ++index; |
| + // while (stack[index].getClassName().equals(TraceEvent.class.getName())) ++index; |
| + // System.logW("TraceEvent caller is at stack index " + index); |
| + |
| + // '4' Was derived using the above commented out code snippet. |
| + return stack[4].getClassName() + "." + stack[4].getMethodName(); |
| + } |
| + |
| private static native void nativeRegisterEnabledObserver(); |
| private static native void nativeStartATrace(); |
| private static native void nativeStopATrace(); |