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 /** |
13 * Java mirror of Chrome trace event API. See base/trace_event/trace_event.h. Un like the native | 14 * 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() | 15 * 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. | 16 * 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 | 17 * Note that this class can also be used before the native library is loaded for early tracing. |
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 | |
22 private static volatile boolean sEnabled = false; | 21 private static volatile boolean sEnabled = false; |
23 | 22 |
24 private static class BasicLooperMonitor implements Printer { | 23 private static class BasicLooperMonitor implements Printer { |
25 @Override | 24 @Override |
26 public void println(final String line) { | 25 public void println(final String line) { |
27 if (line.startsWith(">")) { | 26 if (line.startsWith(">")) { |
28 beginHandling(line); | 27 beginHandling(line); |
29 } else { | 28 } else { |
30 assert line.startsWith("<"); | 29 assert line.startsWith("<"); |
31 endHandling(line); | 30 endHandling(line); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 */ | 167 */ |
169 public static void registerNativeEnabledObserver() { | 168 public static void registerNativeEnabledObserver() { |
170 nativeRegisterEnabledObserver(); | 169 nativeRegisterEnabledObserver(); |
171 } | 170 } |
172 | 171 |
173 /** | 172 /** |
174 * Notification from native that tracing is enabled/disabled. | 173 * Notification from native that tracing is enabled/disabled. |
175 */ | 174 */ |
176 @CalledByNative | 175 @CalledByNative |
177 public static void setEnabled(boolean enabled) { | 176 public static void setEnabled(boolean enabled) { |
177 if (!enabled) EarlyTraceEvent.disable(); | |
178 | |
178 sEnabled = enabled; | 179 sEnabled = enabled; |
179 ThreadUtils.getUiThreadLooper().setMessageLogging( | 180 ThreadUtils.getUiThreadLooper().setMessageLogging( |
180 enabled ? LooperMonitorHolder.sInstance : null); | 181 enabled ? LooperMonitorHolder.sInstance : null); |
181 } | 182 } |
182 | 183 |
183 /** | 184 /** |
185 * Can be called early during the application initialization so that TraceEv ent can be used | |
186 * before the native library is loaded. | |
187 */ | |
188 public static void enableEarlyTracing() { | |
189 EarlyTraceEvent.enable(); | |
190 } | |
191 | |
192 private static void disableEarlyTracing() { | |
193 EarlyTraceEvent.disable(); | |
194 } | |
195 | |
196 /** | |
184 * Enables or disabled Android systrace path of Chrome tracing. If enabled, all Chrome | 197 * 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 | 198 * traces will be also output to Android systrace. Because of the overhead o f Android |
186 * systrace, this is for WebView only. | 199 * systrace, this is for WebView only. |
187 */ | 200 */ |
188 public static void setATraceEnabled(boolean enabled) { | 201 public static void setATraceEnabled(boolean enabled) { |
189 if (sEnabled == enabled) return; | 202 if (sEnabled == enabled) return; |
190 if (enabled) { | 203 if (enabled) { |
191 nativeStartATrace(); | 204 nativeStartATrace(); |
192 } else { | 205 } else { |
193 nativeStopATrace(); | 206 nativeStopATrace(); |
194 } | 207 } |
195 } | 208 } |
196 | 209 |
197 /** | 210 /** |
198 * @return True if tracing is enabled, false otherwise. | 211 * @return True if tracing is enabled, false otherwise. |
199 * It is safe to call trace methods without checking if TraceEvent | 212 * It is safe to call trace methods without checking if TraceEvent |
200 * is enabled. | 213 * is enabled. |
201 */ | 214 */ |
202 public static boolean enabled() { | 215 public static boolean enabled() { |
203 return sEnabled; | 216 return sEnabled || EarlyTraceEvent.isEnabled(); |
204 } | 217 } |
205 | 218 |
206 /** | 219 /** |
207 * Triggers the 'instant' native trace event with no arguments. | 220 * Triggers the 'instant' native trace event with no arguments. |
208 * @param name The name of the event. | 221 * @param name The name of the event. |
209 */ | 222 */ |
210 public static void instant(String name) { | 223 public static void instant(String name) { |
211 if (sEnabled) nativeInstant(name, null); | 224 if (sEnabled) nativeInstant(name, null); |
212 } | 225 } |
213 | 226 |
(...skipping 22 matching lines...) Expand all Loading... | |
236 */ | 249 */ |
237 public static void finishAsync(String name, long id) { | 250 public static void finishAsync(String name, long id) { |
238 if (sEnabled) nativeFinishAsync(name, id); | 251 if (sEnabled) nativeFinishAsync(name, id); |
239 } | 252 } |
240 | 253 |
241 /** | 254 /** |
242 * Triggers the 'begin' native trace event with no arguments. | 255 * Triggers the 'begin' native trace event with no arguments. |
243 * @param name The name of the event. | 256 * @param name The name of the event. |
244 */ | 257 */ |
245 public static void begin(String name) { | 258 public static void begin(String name) { |
246 if (sEnabled) nativeBegin(name, null); | 259 if (enabled()) begin(name, null); |
247 } | 260 } |
248 | 261 |
249 /** | 262 /** |
250 * Triggers the 'begin' native trace event. | 263 * Triggers the 'begin' native trace event. |
251 * @param name The name of the event. | 264 * @param name The name of the event. |
252 * @param arg The arguments of the event. | 265 * @param arg The arguments of the event. |
253 */ | 266 */ |
254 public static void begin(String name, String arg) { | 267 public static void begin(String name, String arg) { |
255 if (sEnabled) nativeBegin(name, arg); | 268 if (sEnabled) { |
269 nativeBegin(name, arg); | |
270 return; | |
271 } | |
272 | |
273 EarlyTraceEvent.begin(name, arg); | |
256 } | 274 } |
257 | 275 |
258 /** | 276 /** |
277 * Convenience wrapper around the versions of end() that take string paramet ers. | |
278 * @see #begin() | |
279 */ | |
280 public static void end() { | |
281 if (enabled()) end(getCallerName(), null); | |
282 } | |
283 | |
284 /** | |
259 * Triggers the 'end' native trace event with no arguments. | 285 * Triggers the 'end' native trace event with no arguments. |
260 * @param name The name of the event. | 286 * @param name The name of the event. |
261 */ | 287 */ |
262 public static void end(String name) { | 288 public static void end(String name) { |
263 if (sEnabled) nativeEnd(name, null); | 289 if (enabled()) end(name, null); |
264 } | 290 } |
265 | 291 |
266 /** | 292 /** |
267 * Triggers the 'end' native trace event. | 293 * Triggers the 'end' native trace event. |
268 * @param name The name of the event. | 294 * @param name The name of the event. |
269 * @param arg The arguments of the event. | 295 * @param arg The arguments of the event. |
270 */ | 296 */ |
271 public static void end(String name, String arg) { | 297 public static void end(String name, String arg) { |
298 if (!enabled()) return; | |
299 | |
300 if (EarlyTraceEvent.end(name, arg)) return; | |
301 | |
272 if (sEnabled) nativeEnd(name, arg); | 302 if (sEnabled) nativeEnd(name, arg); |
273 } | 303 } |
274 | 304 |
305 static String getCallerName() { | |
306 // 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.
| |
307 StackTraceElement[] stack = java.lang.Thread.currentThread().getStackTra ce(); | |
308 | |
309 // Commented out to avoid excess call overhead, but these lines can be u seful to debug | |
310 // exactly where the TraceEvent's client is on the callstack. | |
311 // int index = 0; | |
312 // while (!stack[index].getClassName().equals(TraceEvent.class.getName( ))) ++index; | |
313 // while (stack[index].getClassName().equals(TraceEvent.class.getName() )) ++index; | |
314 // System.logW("TraceEvent caller is at stack index " + index); | |
315 | |
316 // '4' Was derived using the above commented out code snippet. | |
317 return stack[4].getClassName() + "." + stack[4].getMethodName(); | |
318 } | |
319 | |
275 private static native void nativeRegisterEnabledObserver(); | 320 private static native void nativeRegisterEnabledObserver(); |
276 private static native void nativeStartATrace(); | 321 private static native void nativeStartATrace(); |
277 private static native void nativeStopATrace(); | 322 private static native void nativeStopATrace(); |
278 private static native void nativeInstant(String name, String arg); | 323 private static native void nativeInstant(String name, String arg); |
279 private static native void nativeBegin(String name, String arg); | 324 private static native void nativeBegin(String name, String arg); |
280 private static native void nativeEnd(String name, String arg); | 325 private static native void nativeEnd(String name, String arg); |
281 private static native void nativeBeginToplevel(); | 326 private static native void nativeBeginToplevel(); |
282 private static native void nativeEndToplevel(); | 327 private static native void nativeEndToplevel(); |
283 private static native void nativeStartAsync(String name, long id); | 328 private static native void nativeStartAsync(String name, long id); |
284 private static native void nativeFinishAsync(String name, long id); | 329 private static native void nativeFinishAsync(String name, long id); |
285 } | 330 } |
OLD | NEW |