Automatically trace messages on shared loopers.

There are a handful of looper threads in the system_process that
are shared by dozens of different internal services.  To help track
down what these operations are, tag the processing of each message
with a string that tries describing where it originated from: the
class name of the Handler, and the message number or class name of the
Runnable.

Bug: 28046299
Change-Id: I409ec43fea8daaae4fd70df05d4fed929a7249ae
This commit is contained in:
Jeff Sharkey
2016-04-06 17:40:54 -06:00
parent feef8b62ee
commit 74cd3de6f4
7 changed files with 41 additions and 3 deletions

View File

@@ -231,6 +231,18 @@ public class Handler {
mAsynchronous = async;
}
/** {@hide} */
public String getTraceName(Message message) {
final StringBuilder sb = new StringBuilder();
sb.append(getClass().getName()).append(": ");
if (message.callback != null) {
sb.append(message.callback.getClass().getName());
} else {
sb.append("#").append(message.what);
}
return sb.toString();
}
/**
* Returns a string representing the name of the specified message.
* The default implementation will either return the class name of the
@@ -739,8 +751,8 @@ public class Handler {
message.callback.run();
}
final MessageQueue mQueue;
final Looper mLooper;
final MessageQueue mQueue;
final Callback mCallback;
final boolean mAsynchronous;
IMessenger mMessenger;

View File

@@ -72,6 +72,7 @@ public final class Looper {
final Thread mThread;
private Printer mLogging;
private long mTraceTag;
/** Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
@@ -139,13 +140,23 @@ public final class Looper {
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
final long traceTag = me.mTraceTag;
if (traceTag != 0) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
try {
msg.target.dispatchMessage(msg);
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
@@ -208,6 +219,11 @@ public final class Looper {
mLogging = printer;
}
/** {@hide} */
public void setTraceTag(long traceTag) {
mTraceTag = traceTag;
}
/**
* Quits the looper.
* <p>

View File

@@ -18,6 +18,7 @@ package com.android.internal.os;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Trace;
/**
* Shared singleton background thread for each process.
@@ -34,6 +35,7 @@ public final class BackgroundThread extends HandlerThread {
if (sInstance == null) {
sInstance = new BackgroundThread();
sInstance.start();
sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
sHandler = new Handler(sInstance.getLooper());
}
}

View File

@@ -17,6 +17,7 @@
package com.android.server;
import android.os.Handler;
import android.os.Trace;
/**
* Shared singleton foreground thread for the system. This is a thread for
@@ -36,6 +37,7 @@ public final class DisplayThread extends ServiceThread {
if (sInstance == null) {
sInstance = new DisplayThread();
sInstance.start();
sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
sHandler = new Handler(sInstance.getLooper());
}
}

View File

@@ -17,6 +17,7 @@
package com.android.server;
import android.os.Handler;
import android.os.Trace;
/**
* Shared singleton foreground thread for the system. This is a thread for regular
@@ -38,6 +39,7 @@ public final class FgThread extends ServiceThread {
if (sInstance == null) {
sInstance = new FgThread();
sInstance.start();
sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
sHandler = new Handler(sInstance.getLooper());
}
}

View File

@@ -17,6 +17,7 @@
package com.android.server;
import android.os.Handler;
import android.os.Trace;
/**
* Shared singleton I/O thread for the system. This is a thread for non-background
@@ -35,6 +36,7 @@ public final class IoThread extends ServiceThread {
if (sInstance == null) {
sInstance = new IoThread();
sInstance.start();
sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
sHandler = new Handler(sInstance.getLooper());
}
}

View File

@@ -17,6 +17,7 @@
package com.android.server;
import android.os.Handler;
import android.os.Trace;
/**
* Shared singleton thread for showing UI. This is a foreground thread, and in
@@ -35,6 +36,7 @@ public final class UiThread extends ServiceThread {
if (sInstance == null) {
sInstance = new UiThread();
sInstance.start();
sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_ACTIVITY_MANAGER);
sHandler = new Handler(sInstance.getLooper());
}
}