<metaname="Description" content="Traceview is a graphical viewer for execution logs that you create by using the Debug class to log tracing information in your code. Traceview can help you debug your application and profile its performance. When you have a trace log file (generated by…">
<divclass="nav-section-header"><ahref="http://developer.android.com/tools/devices/index.html"><spanclass="en">Setting Up Virtual Devices</span></a></div>
<li><ahref="debugging-tracing.html#knownissues">Traceview Known Issues</a></li>
</ol>
</div>
</div>
<p>Traceview is a graphical viewer for execution logs that you create by using the <code><ahref="../../reference/android/os/Debug.html">Debug</a></code> class to log tracing information in your code. Traceview can help you debug
your application and profile its performance.</p>
<h2id="traceviewLayout">Traceview Layout</h2>
<p>When you have a trace log file (generated by adding tracing code to your application or by DDMS),
you can load the log files in Traceview, which displays the log data in two panels:</p>
<ul>
<li>A <ahref="debugging-tracing.html#timelinepanel">timeline panel</a> -- describes when each thread and method
started and stopped</li>
<li>A <ahref="debugging-tracing.html#timelinepanel">profile panel</a> -- provides a summary of what happened inside
a method</li>
</ul>
<p>The sections below provide addition information about the traceview output panes.</p>
<h3id="timelinepanel">Timeline Panel</h3>
<p>Figure 1 shows a close up of the timeline panel. Each thread’s execution is shown
in its own row, with time increasing to the right. Each method is shown in another color (colors
are reused in a round-robin fashion starting with the methods that have the most inclusive time).
The thin lines underneath the first row show the extent (entry to exit) of all the calls to the
selected method.</p>
<imgsrc="../../images/traceview_timeline.png"
alt="Traceview timeline panel"
width="893"
height="284" />
<pclass="img-caption"><strong>Figure 1.</strong> The Traceview Timeline Panel</p>
<h3id="profilepanel">Profile Panel</h3>
<p>Figure 2 shows the profile pane, a summary of all the time spent
in a method. The table shows both the inclusive and exclusive times (as well as the percentage of
the total time). Exclusive time is the time spent in the method. Inclusive time is the time spent
in the method plus the time spent in any called functions. We refer to calling methods as
"parents" and called methods as "children." When a method is selected (by clicking on it), it
expands to show the parents and children. Parents are shown with a purple background and children
with a yellow background. The last column in the table shows the number of calls to this method
plus the number of recursive calls. The last column shows the number of calls out of the total
number of calls made to that method. In this view, we can see that there were 14 calls to
<code>LoadListener.nativeFinished();</code> looking at the timeline panel shows that one of those calls took
an unusually long time.</p>
<imgsrc="../../images/traceview_profile.png"
alt="Traceview profile panel."
width="892"
height="630" />
<pclass="img-caption"><strong>Figure 2.</strong> The Traceview Profile Panel</p>
<p>To use Traceview, you need to generate log files containing the trace information you want to
analyze.</p>
<p>There are two ways to generate trace logs:</p>
<ul>
<li>Include the <code><ahref="../../reference/android/os/Debug.html">Debug</a></code> class in your code and call its
methods such as <code><ahref="../../reference/android/os/Debug.html#startMethodTracing()">startMethodTracing()</a></code> and <code><ahref="../../reference/android/os/Debug.html#stopMethodTracing()">stopMethodTracing()</a></code>, to start and stop logging of trace information to disk.
This option is very precise because
you can specify exactly where to start and stop logging trace data in your code.</li>
<li>Use the method profiling feature of DDMS to generate trace logs. This option is less
precise because you do not modify code, but rather specify when to start and stop logging with
DDMS. Although you have less control on exactly where logging starts and stops,
this option is useful if you don't have access to the application's code, or if you do
not need precise log timing.
</li>
</ul>
<p>Before you start generating trace logs, be aware of the following restrictions:</p>
<ul>
<li>If you are using the <code><ahref="../../reference/android/os/Debug.html">Debug</a></code> class,
your application must have permission to write to external storage
have an SD card present and your application must have permission to write to the SD card.
<li>Android 2.2 and later devices do not need an SD card. The trace log files are
streamed directly to your development machine.</li>
</ul>
</li>
</ul>
<p>This document focuses on using the <code><ahref="../../reference/android/os/Debug.html">Debug</a></code> class to generate trace data. For more information on using DDMS
to generate trace data, see <ahref="ddms.html#profiling">Using the Dalvik Debug Monitor Server.</a>
</p>
<p>To create the trace files, include the <code><ahref="../../reference/android/os/Debug.html">Debug</a></code> class and call one of the
<code><ahref="../../reference/android/os/Debug.html#startMethodTracing()">startMethodTracing()</a></code> methods. In the call, you
specify a base name for the trace files that the system generates. To stop tracing, call <code><ahref="../../reference/android/os/Debug.html#stopMethodTracing()">stopMethodTracing()</a></code>. These methods start and stop method
tracing across the entire virtual machine. For example, you could call
<code><ahref="../../reference/android/os/Debug.html#startMethodTracing()">startMethodTracing()</a></code> in
your activity's <code><ahref="http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)">onCreate()</a></code> method, and call
<code><ahref="../../reference/android/os/Debug.html#stopMethodTracing()">stopMethodTracing()</a></code> in that activity's
<p>When your application calls <code><ahref="../../reference/android/os/Debug.html#startMethodTracing()">startMethodTracing()</a></code>,
the system creates a file called
<code><trace-base-name>.trace</code>. This contains the binary method trace data and a
mapping table with thread and method names.</p>
<p>The system then begins buffering the generated trace data, until your application calls
<code><ahref="../../reference/android/os/Debug.html#stopMethodTracing()">stopMethodTracing()</a></code>, at which time it writes
the buffered data to the output file. If the system
reaches the maximum buffer size before you call <code><ahref="../../reference/android/os/Debug.html#stopMethodTracing()">stopMethodTracing()</a></code>, the system stops tracing
and sends a notification to the console.</p>
<p>Interpreted code runs more slowly when profiling is enabled. Don't try to generate
absolute timings from the profiler results (such as, "function X takes 2.5 seconds to run"). The
times are only useful in relation to other profile output, so you can see if changes have made
the code faster or slower relative to a previous profiling run.</p>
<h2id="copyingfiles">Copying Trace Files to a Host Machine</h2>
<p>After your application has run and the system has created your trace files
<code><trace-base-name>.trace</code> on a device or emulator, you must copy those files to
your development computer. You can use <code>adb pull</code> to copy the files. Here's an example
that shows how to copy an example file, calc.trace, from the default location on the emulator to
the /tmp directory on the emulator host machine:</p>
<pre>
adb pull /sdcard/calc.trace /tmp
</pre>
<h2id="runningtraceview">Viewing Trace Files in Traceview</h2>
<p>To run Traceview and view the trace files, enter <code>traceview
<trace-base-name></code>. For example, to run Traceview on the example files copied in the