| 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 4721b254d422b69a44a4b6404b6f7fac8724f1d6..934b40982444ba48df139dccb7ea9e122ff7138c 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/debug/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();
|
| }
|
|
|
| /**
|
| @@ -239,11 +252,24 @@ public class TraceEvent {
|
| }
|
|
|
| /**
|
| + * Convenience wrapper around the versions of begin() that take string parameters.
|
| + * The name of the event will be derived from the class and function name that call this.
|
| + * IMPORTANT: if using this version, ensure end() (no parameters) is always called from the
|
| + * same calling context.
|
| + *
|
| + * Note that the overhead is at ms or sub-ms order. Don't use this when millisecond accuracy
|
| + * is desired.
|
| + */
|
| + public static void begin() {
|
| + if (enabled()) begin(getCallerName(), null);
|
| + }
|
| +
|
| + /**
|
| * Triggers the 'begin' native trace event with no arguments.
|
| * @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 +278,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 +299,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 +308,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.
|
| + 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();
|
|
|