Disable tracing from Zygote

This change disables all atrace tracing in Zygote immediately after it is
initialized.  This is necessary because Zygote has no way to receive
notifications that the enabled trace tags have been changed.  Tracing is
re-enabled when other processes fork from Zygote.

Change-Id: If2983858fb0c4890ba9ab041849b1c4d98f66c13
This commit is contained in:
Jamie Gennis
2013-04-15 18:53:24 -07:00
committed by Android (Google) Code Review
parent d12a7645a3
commit 6ad0452e63
4 changed files with 34 additions and 4 deletions

View File

@@ -12,6 +12,7 @@
#include <utils/Log.h>
#include <cutils/process_name.h>
#include <cutils/memory.h>
#include <cutils/trace.h>
#include <android_runtime/AndroidRuntime.h>
#include <sys/personality.h>
@@ -95,6 +96,9 @@ public:
virtual void onZygoteInit()
{
// Re-enable tracing now that we're no longer in Zygote.
atrace_set_tracing_enabled(true);
sp<ProcessState> proc = ProcessState::self();
ALOGV("App process: starting thread pool.\n");
proc->startThreadPool();

View File

@@ -79,6 +79,7 @@ public final class Trace {
private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
private static native void nativeSetAppTracingAllowed(boolean allowed);
private static native void nativeSetTracingEnabled(boolean allowed);
static {
// We configure two separate change callbacks, one in Trace.cpp and one here. The
@@ -115,10 +116,6 @@ public final class Trace {
*/
private static long cacheEnabledTags() {
long tags = nativeGetEnabledTags();
if (tags == TRACE_TAG_NOT_READY) {
Log.w(TAG, "Unexpected value from nativeGetEnabledTags: " + tags);
// keep going
}
sEnabledTags = tags;
return tags;
}
@@ -168,6 +165,22 @@ public final class Trace {
cacheEnabledTags();
}
/**
* Set whether tracing is enabled in this process. Tracing is disabled shortly after Zygote
* initializes and re-enabled after processes fork from Zygote. This is done because Zygote
* has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
* caches the tracing tags, forked processes will inherit those stale tags.
*
* @hide
*/
public static void setTracingEnabled(boolean enabled) {
nativeSetTracingEnabled(enabled);
// Setting whether tracing is enabled may change the tags, so we update the cached tags
// here.
cacheEnabledTags();
}
/**
* Writes a trace message to indicate that a given section of code has
* begun. Must be followed by a call to {@link #traceEnd} using the same

View File

@@ -25,6 +25,7 @@ import android.net.LocalServerSocket;
import android.os.Debug;
import android.os.Process;
import android.os.SystemClock;
import android.os.Trace;
import android.util.EventLog;
import android.util.Log;
@@ -528,6 +529,10 @@ public class ZygoteInit {
// Do an initial gc to clean up after startup
gc();
// Disable tracing so that forked processes do not inherit stale tracing tags from
// Zygote.
Trace.setTracingEnabled(false);
// If requested, start system server directly from Zygote
if (argv.length != 2) {
throw new RuntimeException(argv[0] + USAGE_STRING);

View File

@@ -86,6 +86,11 @@ static void android_os_Trace_nativeSetAppTracingAllowed(JNIEnv* env,
atrace_set_debuggable(allowed);
}
static void android_os_Trace_nativeSetTracingEnabled(JNIEnv* env,
jclass clazz, jboolean enabled) {
atrace_set_tracing_enabled(enabled);
}
static JNINativeMethod gTraceMethods[] = {
/* name, signature, funcPtr */
{ "nativeGetEnabledTags",
@@ -109,6 +114,9 @@ static JNINativeMethod gTraceMethods[] = {
{ "nativeSetAppTracingAllowed",
"(Z)V",
(void*)android_os_Trace_nativeSetAppTracingAllowed },
{ "nativeSetTracingEnabled",
"(Z)V",
(void*)android_os_Trace_nativeSetTracingEnabled },
};
int register_android_os_Trace(JNIEnv* env) {