FileDocCategorySizeDatePackage
Trace.javaAPI DocAndroid 5.1 API11368Thu Mar 12 22:22:10 GMT 2015android.os

Trace

public final class Trace extends Object
Writes trace events to the system trace buffer. These trace events can be collected and visualized using the Systrace tool.

This tracing mechanism is independent of the method tracing mechanism offered by {@link Debug#startMethodTracing}. In particular, it enables tracing of events that occur across multiple processes.

For information about using the Systrace tool, read Analyzing Display and Performance with Systrace.

Fields Summary
private static final String
TAG
public static final long
TRACE_TAG_NEVER
public static final long
TRACE_TAG_ALWAYS
public static final long
TRACE_TAG_GRAPHICS
public static final long
TRACE_TAG_INPUT
public static final long
TRACE_TAG_VIEW
public static final long
TRACE_TAG_WEBVIEW
public static final long
TRACE_TAG_WINDOW_MANAGER
public static final long
TRACE_TAG_ACTIVITY_MANAGER
public static final long
TRACE_TAG_SYNC_MANAGER
public static final long
TRACE_TAG_AUDIO
public static final long
TRACE_TAG_VIDEO
public static final long
TRACE_TAG_CAMERA
public static final long
TRACE_TAG_HAL
public static final long
TRACE_TAG_APP
public static final long
TRACE_TAG_RESOURCES
public static final long
TRACE_TAG_DALVIK
public static final long
TRACE_TAG_RS
public static final long
TRACE_TAG_BIONIC
public static final long
TRACE_TAG_POWER
private static final long
TRACE_TAG_NOT_READY
private static final int
MAX_SECTION_NAME_LEN
private static volatile long
sEnabledTags
Constructors Summary
private Trace()


        
             
           
         
             
             
         
         

     
        // We configure two separate change callbacks, one in Trace.cpp and one here.  The
        // native callback reads the tags from the system property, and this callback
        // reads the value that the native code retrieved.  It's essential that the native
        // callback executes first.
        //
        // The system provides ordering through a priority level.  Callbacks made through
        // SystemProperties.addChangeCallback currently have a negative priority, while
        // our native code is using a priority of zero.
        SystemProperties.addChangeCallback(new Runnable() {
            @Override public void run() {
                cacheEnabledTags();
            }
        });
    
    
Methods Summary
public static voidasyncTraceBegin(long traceTag, java.lang.String methodName, int cookie)
Writes a trace message to indicate that a given section of code has begun. Must be followed by a call to {@link #asyncTraceEnd} using the same tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)}, asynchronous events do not need to be nested. The name and cookie used to begin an event must be used to end it.

param
traceTag The trace tag.
param
methodName The method name to appear in the trace.
param
cookie Unique identifier for distinguishing simultaneous events
hide

        if (isTagEnabled(traceTag)) {
            nativeAsyncTraceBegin(traceTag, methodName, cookie);
        }
    
public static voidasyncTraceEnd(long traceTag, java.lang.String methodName, int cookie)
Writes a trace message to indicate that the current method has ended. Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)} using the same tag, name and cookie.

param
traceTag The trace tag.
param
methodName The method name to appear in the trace.
param
cookie Unique identifier for distinguishing simultaneous events
hide

        if (isTagEnabled(traceTag)) {
            nativeAsyncTraceEnd(traceTag, methodName, cookie);
        }
    
public static voidbeginSection(java.lang.String sectionName)
Writes a trace message to indicate that a given section of code has begun. This call must be followed by a corresponding call to {@link #endSection()} on the same thread.

At this time the vertical bar character '|', newline character '\n', and null character '\0' are used internally by the tracing mechanism. If sectionName contains these characters they will be replaced with a space character in the trace.

param
sectionName The name of the code section to appear in the trace. This may be at most 127 Unicode code units long.

        if (isTagEnabled(TRACE_TAG_APP)) {
            if (sectionName.length() > MAX_SECTION_NAME_LEN) {
                throw new IllegalArgumentException("sectionName is too long");
            }
            nativeTraceBegin(TRACE_TAG_APP, sectionName);
        }
    
private static longcacheEnabledTags()
Caches a copy of the enabled-tag bits. The "master" copy is held by the native code, and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.

If the native code hasn't yet read the property, we will cause it to do one-time initialization. We don't want to do this during class init, because this class is preloaded, so all apps would be stuck with whatever the zygote saw. (The zygote doesn't see the system-property update broadcasts.)

We want to defer initialization until the first use by an app, post-zygote.

We're okay if multiple threads call here simultaneously -- the native state is synchronized, and sEnabledTags is volatile (prevents word tearing).

        long tags = nativeGetEnabledTags();
        sEnabledTags = tags;
        return tags;
    
public static voidendSection()
Writes a trace message to indicate that a given section of code has ended. This call must be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method will mark the end of the most recently begun section of code, so care must be taken to ensure that beginSection / endSection pairs are properly nested and called from the same thread.

        if (isTagEnabled(TRACE_TAG_APP)) {
            nativeTraceEnd(TRACE_TAG_APP);
        }
    
public static booleanisTagEnabled(long traceTag)
Returns true if a trace tag is enabled.

param
traceTag The trace tag to check.
return
True if the trace tag is valid.
hide

        long tags = sEnabledTags;
        if (tags == TRACE_TAG_NOT_READY) {
            tags = cacheEnabledTags();
        }
        return (tags & traceTag) != 0;
    
private static native voidnativeAsyncTraceBegin(long tag, java.lang.String name, int cookie)

private static native voidnativeAsyncTraceEnd(long tag, java.lang.String name, int cookie)

private static native longnativeGetEnabledTags()

private static native voidnativeSetAppTracingAllowed(boolean allowed)

private static native voidnativeSetTracingEnabled(boolean allowed)

private static native voidnativeTraceBegin(long tag, java.lang.String name)

private static native voidnativeTraceCounter(long tag, java.lang.String name, int value)

private static native voidnativeTraceEnd(long tag)

public static voidsetAppTracingAllowed(boolean allowed)
Set whether application tracing is allowed for this process. This is intended to be set once at application start-up time based on whether the application is debuggable.

hide

        nativeSetAppTracingAllowed(allowed);

        // Setting whether app tracing is allowed may change the tags, so we update the cached
        // tags here.
        cacheEnabledTags();
    
public static voidsetTracingEnabled(boolean enabled)
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

        nativeSetTracingEnabled(enabled);

        // Setting whether tracing is enabled may change the tags, so we update the cached tags
        // here.
        cacheEnabledTags();
    
public static voidtraceBegin(long traceTag, java.lang.String methodName)
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 tag.

param
traceTag The trace tag.
param
methodName The method name to appear in the trace.
hide

        if (isTagEnabled(traceTag)) {
            nativeTraceBegin(traceTag, methodName);
        }
    
public static voidtraceCounter(long traceTag, java.lang.String counterName, int counterValue)
Writes trace message to indicate the value of a given counter.

param
traceTag The trace tag.
param
counterName The counter name to appear in the trace.
param
counterValue The counter value.
hide

        if (isTagEnabled(traceTag)) {
            nativeTraceCounter(traceTag, counterName, counterValue);
        }
    
public static voidtraceEnd(long traceTag)
Writes a trace message to indicate that the current method has ended. Must be called exactly once for each call to {@link #traceBegin} using the same tag.

param
traceTag The trace tag.
hide

        if (isTagEnabled(traceTag)) {
            nativeTraceEnd(traceTag);
        }