| 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; | 5 package org.chromium.base; |
| 6 | 6 |
| 7 import android.os.Looper; | 7 import android.os.Looper; |
| 8 import android.os.MessageQueue; | 8 import android.os.MessageQueue; |
| 9 import android.os.SystemClock; | 9 import android.os.SystemClock; |
| 10 import android.util.Log; | 10 import android.util.Log; |
| 11 import android.util.Printer; | 11 import android.util.Printer; |
| 12 /** | 12 /** |
| 13 * Java mirror of Chrome trace event API. See base/trace_event/trace_event.h. Un
like the native | 13 * Java mirror of Chrome trace event API. See base/trace_event/trace_event.h. Un
like the native |
| 14 * version, Java does not have stack objects, so a TRACE_EVENT() which does both
TRACE_EVENT_BEGIN() | 14 * version, Java does not have stack objects, so a TRACE_EVENT() which does both
TRACE_EVENT_BEGIN() |
| 15 * and TRACE_EVENT_END() in ctor/dtor is not possible. | 15 * and TRACE_EVENT_END() in ctor/dtor is not possible. |
| 16 * It is OK to use tracing before the native library has loaded, but such traces
will | 16 * It is OK to use tracing before the native library has loaded, but such traces
will |
| 17 * be ignored. (Perhaps we could devise to buffer them up in future?). | 17 * be ignored. (Perhaps we could devise to buffer them up in future?). |
| 18 */ | 18 */ |
| 19 @JNINamespace("base::android") | 19 @JNINamespace("base::android") |
| 20 public class TraceEvent { | 20 public class TraceEvent { |
| 21 | 21 |
| 22 private static volatile boolean sEnabled = false; | 22 private static volatile boolean sEnabled = false; |
| 23 private static volatile boolean sATraceEnabled = false; // True when taking
an Android systrace. |
| 23 | 24 |
| 24 private static class BasicLooperMonitor implements Printer { | 25 private static class BasicLooperMonitor implements Printer { |
| 25 @Override | 26 @Override |
| 26 public void println(final String line) { | 27 public void println(final String line) { |
| 27 if (line.startsWith(">")) { | 28 if (line.startsWith(">")) { |
| 28 beginHandling(line); | 29 beginHandling(line); |
| 29 } else { | 30 } else { |
| 30 assert line.startsWith("<"); | 31 assert line.startsWith("<"); |
| 31 endHandling(line); | 32 endHandling(line); |
| 32 } | 33 } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 public static void registerNativeEnabledObserver() { | 170 public static void registerNativeEnabledObserver() { |
| 170 nativeRegisterEnabledObserver(); | 171 nativeRegisterEnabledObserver(); |
| 171 } | 172 } |
| 172 | 173 |
| 173 /** | 174 /** |
| 174 * Notification from native that tracing is enabled/disabled. | 175 * Notification from native that tracing is enabled/disabled. |
| 175 */ | 176 */ |
| 176 @CalledByNative | 177 @CalledByNative |
| 177 public static void setEnabled(boolean enabled) { | 178 public static void setEnabled(boolean enabled) { |
| 178 sEnabled = enabled; | 179 sEnabled = enabled; |
| 180 // Android M+ systrace logs this on its own. Only log it if not writing
to Android systrace. |
| 181 if (sATraceEnabled) return; |
| 179 ThreadUtils.getUiThreadLooper().setMessageLogging( | 182 ThreadUtils.getUiThreadLooper().setMessageLogging( |
| 180 enabled ? LooperMonitorHolder.sInstance : null); | 183 enabled ? LooperMonitorHolder.sInstance : null); |
| 181 } | 184 } |
| 182 | 185 |
| 183 /** | 186 /** |
| 184 * Enables or disabled Android systrace path of Chrome tracing. If enabled,
all Chrome | 187 * Enables or disabled Android systrace path of Chrome tracing. If enabled,
all Chrome |
| 185 * traces will be also output to Android systrace. Because of the overhead o
f Android | 188 * traces will be also output to Android systrace. Because of the overhead o
f Android |
| 186 * systrace, this is for WebView only. | 189 * systrace, this is for WebView only. |
| 187 */ | 190 */ |
| 188 public static void setATraceEnabled(boolean enabled) { | 191 public static void setATraceEnabled(boolean enabled) { |
| 189 if (sEnabled == enabled) return; | 192 if (sATraceEnabled == enabled) return; |
| 193 sATraceEnabled = enabled; |
| 190 if (enabled) { | 194 if (enabled) { |
| 195 // Calls TraceEvent.setEnabled(true) via |
| 196 // TraceLog::EnabledStateObserver::OnTraceLogEnabled |
| 191 nativeStartATrace(); | 197 nativeStartATrace(); |
| 192 } else { | 198 } else { |
| 199 // Calls TraceEvent.setEnabled(false) via |
| 200 // TraceLog::EnabledStateObserver::OnTraceLogDisabled |
| 193 nativeStopATrace(); | 201 nativeStopATrace(); |
| 194 } | 202 } |
| 195 } | 203 } |
| 196 | 204 |
| 197 /** | 205 /** |
| 198 * @return True if tracing is enabled, false otherwise. | 206 * @return True if tracing is enabled, false otherwise. |
| 199 * It is safe to call trace methods without checking if TraceEvent | 207 * It is safe to call trace methods without checking if TraceEvent |
| 200 * is enabled. | 208 * is enabled. |
| 201 */ | 209 */ |
| 202 public static boolean enabled() { | 210 public static boolean enabled() { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 private static native void nativeStartATrace(); | 284 private static native void nativeStartATrace(); |
| 277 private static native void nativeStopATrace(); | 285 private static native void nativeStopATrace(); |
| 278 private static native void nativeInstant(String name, String arg); | 286 private static native void nativeInstant(String name, String arg); |
| 279 private static native void nativeBegin(String name, String arg); | 287 private static native void nativeBegin(String name, String arg); |
| 280 private static native void nativeEnd(String name, String arg); | 288 private static native void nativeEnd(String name, String arg); |
| 281 private static native void nativeBeginToplevel(); | 289 private static native void nativeBeginToplevel(); |
| 282 private static native void nativeEndToplevel(); | 290 private static native void nativeEndToplevel(); |
| 283 private static native void nativeStartAsync(String name, long id); | 291 private static native void nativeStartAsync(String name, long id); |
| 284 private static native void nativeFinishAsync(String name, long id); | 292 private static native void nativeFinishAsync(String name, long id); |
| 285 } | 293 } |
| OLD | NEW |