| 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 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
| 15 import org.chromium.base.annotations.MainDex; | 15 import org.chromium.base.annotations.MainDex; |
| 16 /** | 16 /** |
| 17 * Java mirror of Chrome trace event API. See base/trace_event/trace_event.h. Un
like the native | 17 * Java mirror of Chrome trace event API. See base/trace_event/trace_event.h. Un
like the native |
| 18 * version, Java does not have stack objects, so a TRACE_EVENT() which does both
TRACE_EVENT_BEGIN() | 18 * version, Java does not have stack objects, so a TRACE_EVENT() which does both
TRACE_EVENT_BEGIN() |
| 19 * and TRACE_EVENT_END() in ctor/dtor is not possible. | 19 * and TRACE_EVENT_END() in ctor/dtor is not possible. |
| 20 * It is OK to use tracing before the native library has loaded, in a slightly r
estricted fashion. | 20 * It is OK to use tracing before the native library has loaded, in a slightly r
estricted fashion. |
| 21 * @see EarlyTraceEvent for details. | 21 * @see EarlyTraceEvent for details. |
| 22 */ | 22 */ |
| 23 @JNINamespace("base::android") | 23 @JNINamespace("base::android") |
| 24 @MainDex | 24 @MainDex |
| 25 public class TraceEvent { | 25 public class TraceEvent { |
| 26 | 26 private static volatile boolean sEnabled; |
| 27 private static volatile boolean sEnabled = false; | 27 private static volatile boolean sATraceEnabled; // True when taking an Andro
id systrace. |
| 28 private static volatile boolean sATraceEnabled = false; // True when taking
an Android systrace. | |
| 29 | 28 |
| 30 private static class BasicLooperMonitor implements Printer { | 29 private static class BasicLooperMonitor implements Printer { |
| 31 @Override | 30 @Override |
| 32 public void println(final String line) { | 31 public void println(final String line) { |
| 33 if (line.startsWith(">")) { | 32 if (line.startsWith(">")) { |
| 34 beginHandling(line); | 33 beginHandling(line); |
| 35 } else { | 34 } else { |
| 36 assert line.startsWith("<"); | 35 assert line.startsWith("<"); |
| 37 endHandling(line); | 36 endHandling(line); |
| 38 } | 37 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 // Calculation constants | 76 // Calculation constants |
| 78 private static final long FRAME_DURATION_MILLIS = 1000L / 60L; // 60 FPS | 77 private static final long FRAME_DURATION_MILLIS = 1000L / 60L; // 60 FPS |
| 79 // A reasonable threshold for defining a Looper event as "long running" | 78 // A reasonable threshold for defining a Looper event as "long running" |
| 80 private static final long MIN_INTERESTING_DURATION_MILLIS = | 79 private static final long MIN_INTERESTING_DURATION_MILLIS = |
| 81 FRAME_DURATION_MILLIS; | 80 FRAME_DURATION_MILLIS; |
| 82 // A reasonable threshold for a "burst" of tasks on the Looper | 81 // A reasonable threshold for a "burst" of tasks on the Looper |
| 83 private static final long MIN_INTERESTING_BURST_DURATION_MILLIS = | 82 private static final long MIN_INTERESTING_BURST_DURATION_MILLIS = |
| 84 MIN_INTERESTING_DURATION_MILLIS * 3; | 83 MIN_INTERESTING_DURATION_MILLIS * 3; |
| 85 | 84 |
| 86 // Stats tracking | 85 // Stats tracking |
| 87 private long mLastIdleStartedAt = 0L; | 86 private long mLastIdleStartedAt; |
| 88 private long mLastWorkStartedAt = 0L; | 87 private long mLastWorkStartedAt; |
| 89 private int mNumTasksSeen = 0; | 88 private int mNumTasksSeen; |
| 90 private int mNumIdlesSeen = 0; | 89 private int mNumIdlesSeen; |
| 91 private int mNumTasksSinceLastIdle = 0; | 90 private int mNumTasksSinceLastIdle; |
| 92 | 91 |
| 93 // State | 92 // State |
| 94 private boolean mIdleMonitorAttached = false; | 93 private boolean mIdleMonitorAttached; |
| 95 | 94 |
| 96 // Called from within the begin/end methods only. | 95 // Called from within the begin/end methods only. |
| 97 // This method can only execute on the looper thread, because that is | 96 // This method can only execute on the looper thread, because that is |
| 98 // the only thread that is permitted to call Looper.myqueue(). | 97 // the only thread that is permitted to call Looper.myqueue(). |
| 99 private final void syncIdleMonitoring() { | 98 private final void syncIdleMonitoring() { |
| 100 if (sEnabled && !mIdleMonitorAttached) { | 99 if (sEnabled && !mIdleMonitorAttached) { |
| 101 // approximate start time for computational purposes | 100 // approximate start time for computational purposes |
| 102 mLastIdleStartedAt = SystemClock.elapsedRealtime(); | 101 mLastIdleStartedAt = SystemClock.elapsedRealtime(); |
| 103 Looper.myQueue().addIdleHandler(this); | 102 Looper.myQueue().addIdleHandler(this); |
| 104 mIdleMonitorAttached = true; | 103 mIdleMonitorAttached = true; |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 private static native void nativeStartATrace(); | 305 private static native void nativeStartATrace(); |
| 307 private static native void nativeStopATrace(); | 306 private static native void nativeStopATrace(); |
| 308 private static native void nativeInstant(String name, String arg); | 307 private static native void nativeInstant(String name, String arg); |
| 309 private static native void nativeBegin(String name, String arg); | 308 private static native void nativeBegin(String name, String arg); |
| 310 private static native void nativeEnd(String name, String arg); | 309 private static native void nativeEnd(String name, String arg); |
| 311 private static native void nativeBeginToplevel(); | 310 private static native void nativeBeginToplevel(); |
| 312 private static native void nativeEndToplevel(); | 311 private static native void nativeEndToplevel(); |
| 313 private static native void nativeStartAsync(String name, long id); | 312 private static native void nativeStartAsync(String name, long id); |
| 314 private static native void nativeFinishAsync(String name, long id); | 313 private static native void nativeFinishAsync(String name, long id); |
| 315 } | 314 } |
| OLD | NEW |