import android.os.LooperProto;
import android.util.Log;
import android.util.Printer;
+import android.util.Slog;
import android.util.proto.ProtoOutputStream;
/**
private Printer mLogging;
private long mTraceTag;
+ /* If set, the looper will show a warning log if a message dispatch takes longer than time. */
+ private long mSlowDispatchThresholdMs;
+
/** Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
* this looper, before actually starting the loop. Be sure to call
msg.callback + ": " + msg.what);
}
+ final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
+
final long traceTag = me.mTraceTag;
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
+ final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
+ final long end;
try {
msg.target.dispatchMessage(msg);
+ end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
+ if (slowDispatchThresholdMs > 0) {
+ final long time = end - start;
+ if (time > slowDispatchThresholdMs) {
+ Slog.w(TAG, "Dispatch took " + time + "ms on "
+ + Thread.currentThread().getName() + ", h=" +
+ msg.target + " cb=" + msg.callback + " msg=" + msg.what);
+ }
+ }
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
mTraceTag = traceTag;
}
+ /** {@hide} */
+ public void setSlowDispatchThresholdMs(long slowDispatchThresholdMs) {
+ mSlowDispatchThresholdMs = slowDispatchThresholdMs;
+ }
+
/**
* Quits the looper.
* <p>
package com.android.server;
import android.os.Handler;
+import android.os.Looper;
import android.os.Process;
import android.os.Trace;
* on it to avoid UI jank.
*/
public final class UiThread extends ServiceThread {
+ private static final long SLOW_DISPATCH_THRESHOLD_MS = 100;
private static UiThread sInstance;
private static Handler sHandler;
if (sInstance == null) {
sInstance = new UiThread();
sInstance.start();
- sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
+ final Looper looper = sInstance.getLooper();
+ looper.setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
+ looper.setSlowDispatchThresholdMs(SLOW_DISPATCH_THRESHOLD_MS);
sHandler = new Handler(sInstance.getLooper());
}
}