| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 1 | page.title=Analyzing Display and Performance |
| 2 | page.tags=systrace,speed |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 3 | parent.title=Debugging |
| 4 | parent.link=index.html |
| 5 | @jd:body |
| 6 | |
| 7 | <div id="qv-wrapper"> |
| 8 | <div id="qv"> |
| 9 | <h2>In this document</h2> |
| 10 | <ol> |
| 11 | <li><a href="#overview">Overview</a> |
| 12 | </li> |
| 13 | <li><a href="#generate">Generating Traces</a> |
| 14 | <ol> |
| 15 | <li><a href="#limit-trace">Limiting trace data</a></li> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 16 | <li><a href="#running-4.3">Tracing on Android 4.3 and higher</a> |
| 17 | <li><a href="#running-4.2">Tracing on Android 4.2 and lower</a></li> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 18 | </ol> |
| 19 | </li> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 20 | <li><a href="#app-trace">Tracing Application Code</a></li> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 21 | <li><a href="#analysis">Analyzing Traces</a> |
| 22 | <ol> |
| 23 | <li><a href="#long-processes">Long running processes</a></li> |
| 24 | <li><a href="#display-interupts">Interruptions in display execution</a></li> |
| 25 | </ol> |
| 26 | </li> |
| 27 | </ol> |
| 28 | <h2>See also</h2> |
| 29 | <ol> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 30 | <li><a href="{@docRoot}tools/help/systrace.html">Systrace</a></li> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 31 | </ol> |
| 32 | </div> |
| 33 | </div> |
| 34 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 35 | <p>After building features, eliminating bugs, and cleaning up your code, you should spend some |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 36 | time looking at the performance of your application. The speed and smoothness with which your |
| 37 | application draws pixels and performs operations has an significant impact on your users' |
| 38 | experience.</p> |
| 39 | |
| 40 | <p>Android applications operate within a shared resource environment, and the performance of |
| 41 | your application can be impacted by how efficiently it interacts with those resources in |
| 42 | the larger system. Applications also operate in a multithreaded environment, competing with other |
| 43 | threaded processes for resources, which can cause performance problems that are hard to diagnose. |
| 44 | </p> |
| 45 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 46 | <p>The Systrace tool allows you to collect and review code execution data for your |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 47 | application and the Android system. You can use this data to diagnose execution problems and |
| 48 | improve the performance of your application.</p> |
| 49 | |
| 50 | |
| 51 | <h2 id="overview">Overview</h2> |
| 52 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 53 | <p>Systrace helps you analyze how the execution of your application fits into the larger |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 54 | Android environment, letting you see system and applications process execution on a common |
| 55 | timeline. The tool allows you to generate highly detailed, interactive reports from devices |
| 56 | running Android 4.1 and higher, such as the report in figure 1.</p> |
| 57 | |
| 58 | <img src="{@docRoot}images/systrace/report.png" alt="Systrace example report" id="figure1" /> |
| 59 | <p class="img-caption"> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 60 | <strong>Figure 1.</strong> An example Systrace report on 5 seconds of process execution |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 61 | for a running application and related Android system processes. |
| 62 | </p> |
| 63 | |
| 64 | |
| 65 | <h2 id="generate">Generating Traces</h2> |
| 66 | |
| 67 | <p>In order to create a trace of your application, you must perform a few setup steps. First, you |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 68 | must have a device running Android 4.1 or higher. Set up the device for |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 69 | <a href="{@docRoot}tools/device.html#setting-up">debugging</a>, connect it to your development |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 70 | system, and install your application. Some types of trace information, specifically disk activity |
| 71 | and kernel work queues, require that you have root access to the device. However, most Systrace |
| 72 | log data only requires that the device be enabled for developer debugging.</p> |
| 73 | |
| 74 | <p>Systrace traces can be run either from a |
| 75 | <a href="{@docRoot}tools/help/systrace.html#options">command line</a> or from a |
| 76 | <a href="{@docRoot}tools/help/systrace.html#gui">graphical user interface</a>. This guide |
| 77 | focuses on using the command line options.</p> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 78 | |
| 79 | |
| 80 | <h3 id="limit-trace">Limiting trace data</h3> |
| 81 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 82 | <p>The Systrace tool can generate a potentially huge amount of data from applications |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 83 | and system sources. To limit the amount of data the tool collects and make the data more relevant |
| 84 | to your analysis, use the following options:</p> |
| 85 | |
| 86 | <ul> |
| 87 | <li>Limit the amount of time covered by the trace with the {@code -t, --time} option. The default |
| 88 | length of a trace is 5 seconds.</li> |
| 89 | <li>Limit the size of the data collected by the trace with the {@code -b, --buf-size} option.</li> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 90 | <li>Specify what types of processes are traced. The types of processes that can be traced depends |
| 91 | on the version of Android you are running: |
| 92 | <ul> |
| 93 | <li>Android 4.2 and lower devices: Use the {@code --set-tags} option and the {@code --disk}, |
| 94 | {@code --cpu-freq}, {@code --cpu-idle}, {@code --cpu-load} options.</li> |
| 95 | <li>Android 4.3 and higher devices: Use the {@code --list-categories} option to see what |
| 96 | categories are available on your test device.</li> |
| 97 | </ul> |
| 98 | </li> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 99 | </ul> |
| 100 | |
| 101 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 102 | <h3 id="running-4.3">Tracing on Android 4.3 and higher</h3> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 103 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 104 | <p>To run a trace on Android 4.3 and higher devices:</p> |
| 105 | |
| 106 | <ol> |
| 107 | <li>Make sure the device is connected through a USB cable and is |
| 108 | <a href="{@docRoot}tools/device.html#setting-up">enabled for debugging</a>.</li> |
| 109 | <li>Run the trace with the options you want, for example: |
| 110 | <pre> |
| 111 | $ cd android-sdk/platform-tools/systrace |
| 112 | $ python systrace.py --time=10 -o mynewtrace.html sched gfx view wm |
| 113 | </pre> |
| 114 | </li> |
| 115 | <li>On the device, execute any user actions you want be included in the trace.</li> |
| 116 | </ol> |
| 117 | |
| 118 | <p>For more information on the available options for running Systrace, see the |
| 119 | <a href="{@docRoot}tools/help/systrace.html#options-4.3">Systrace</a> help page.</p> |
| 120 | |
| 121 | |
| 122 | <h3 id="running-4.2">Tracing on Android 4.2 and lower</h3> |
| 123 | |
| 124 | <p>To use Systrace effectively with devices running Android 4.2 and lower, |
| 125 | you must configure the types of processes you want to trace before running a trace. |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 126 | The tool can gather the following types of process information:</p> |
| 127 | |
| 128 | <ul> |
| 129 | <li>General system processes such as graphics, audio and input processes (selected using trace |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 130 | <a href="{@docRoot}tools/help/systrace.html#tags">category tags</a>).</li> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 131 | <li>Low level system information such as CPU, kernel and disk activity (selected using |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 132 | <a href="{@docRoot}tools/help/systrace.html#options">options</a>).</li> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 133 | </ul> |
| 134 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 135 | <p>To set trace tags for Systrace using the command-line:</p> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 136 | |
| 137 | <ol> |
| 138 | <li>Use the {@code --set-tags} option: |
| 139 | <pre> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 140 | $ cd android-sdk/platform-tools/systrace |
| 141 | $ python systrace.py --set-tags=gfx,view,wm |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 142 | </pre> |
| 143 | </li> |
| 144 | <li>Stop and restart the {@code adb} shell to enable tracing of these processes. |
| 145 | <pre> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 146 | $ adb shell stop |
| 147 | $ adb shell start |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 148 | </pre></li> |
| 149 | </ol> |
| 150 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 151 | <p>To set trace tags for Systrace using the device user interface:</p> |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 152 | |
| 153 | <ol> |
| 154 | <li>On the device connected for tracing, navigate to: <strong>Settings > |
| 155 | Developer options > Monitoring > Enable traces</strong>.</li> |
| 156 | <li>Select the categories of processes to be traced and click <strong>OK</strong>.</li> |
| 157 | </ol> |
| 158 | |
| 159 | <p class="note"> |
| 160 | <strong>Note:</strong> The {@code adb} shell does not have to be stopped and restarted when |
| 161 | selecting trace tags using this method. |
| 162 | </p> |
| 163 | |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 164 | <p>After you have configured the category tags for your trace, you can start collecting |
| 165 | information for analysis.</p> |
| 166 | |
| 167 | <p>To run a trace using the current trace tag settings:</p> |
| 168 | |
| 169 | <ol> |
| 170 | <li>Make sure the device is connected through a USB cable and is |
| 171 | <a href="{@docRoot}tools/device.html#setting-up">enabled for debugging</a>.</li> |
| 172 | <li>Run the trace with the low-level system trace options and limits you want, for example: |
| 173 | <pre> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 174 | $ python systrace.py --cpu-freq --cpu-load --time=10 -o mytracefile.html |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 175 | </pre> |
| 176 | </li> |
| 177 | <li>On the device, execute any user actions you want be included in the trace.</li> |
| 178 | </ol> |
| 179 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 180 | <p>For more information on the available options for running Systrace, see the |
| 181 | <a href="{@docRoot}tools/help/systrace.html#options-pre-4.3">Systrace</a> help page.</p> |
| 182 | |
| 183 | |
| 184 | <h2 id="app-trace">Tracing Application Code</h2> |
| 185 | |
| 186 | <p>The Systrace tool can trace the execution of code within your application. In Android |
| 187 | 4.3 (API level 18) and higher, you can use the methods of the {@link android.os.Trace} class to |
| 188 | add instrumentation to your application code and see the results in a Systrace report.</p> |
| 189 | |
| 190 | <p>The following code example shows how to use the {@link android.os.Trace} class to track |
| 191 | execution of an application method, including two nested code blocks within that method.</p> |
| 192 | |
| 193 | <pre> |
| 194 | public void ProcessPeople() { |
| 195 | Trace.beginSection("ProcessPeople"); |
| 196 | try { |
| 197 | Trace.beginSection("Processing Jane"); |
| 198 | try { |
| 199 | // code for Jane task... |
| 200 | } finally { |
| 201 | Trace.endSection(); // ends "Processing Jane" |
| 202 | } |
| 203 | |
| 204 | Trace.beginSection("Processing John"); |
| 205 | try { |
| 206 | // code for John task... |
| 207 | } finally { |
| 208 | Trace.endSection(); // ends "Processing John" |
| 209 | } |
| 210 | } finally { |
| 211 | Trace.endSection(); // ends "ProcessPeople" |
| 212 | } |
| 213 | } |
| 214 | </pre> |
| 215 | <p class="note"> |
| 216 | <strong>Note:</strong> When you nest trace calls within each other, the |
| 217 | {@link android.os.Trace#endSection} method ends the most recently called |
| 218 | {@link android.os.Trace#beginSection} method. This means that a trace started within another |
| 219 | trace cannot extend beyond the end of the enclosing trace, so make sure your beginning and |
| 220 | ending method calls are properly matched to measure your applications processing. |
| 221 | </p> |
| 222 | |
| 223 | <p class="note"> |
| 224 | <strong>Note:</strong> Traces must begin and end on the same thread. Do not call |
| 225 | {@link android.os.Trace#beginSection} on one thread of execution and then attempt to end the |
| 226 | trace with a call to {@link android.os.Trace#endSection} on another thread. |
| 227 | </p> |
| 228 | |
| 229 | <p>When using application-level tracing with Systrace, you must specify the package name of your |
| 230 | application in the user interface or specify the {@code -a} or {@code --app=} options on the |
| 231 | command line. For more information, see the |
| 232 | <a href="{@docRoot}tools/help/systrace.html">Systrace</a> help page.</p> |
| 233 | |
| 234 | <!-- todo: add ndk coverage --> |
| 235 | |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 236 | |
| 237 | <h2 id="analysis">Analyzing Traces</h2> |
| 238 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 239 | <p>After you have generated a trace using Systrace, it lists the location of the output |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 240 | file and you can open the report using a web browser. |
| 241 | How you use the trace data depends on the performance issues you are investigating. However, |
| 242 | this section provides some general instructions on how to analyze a trace.</p> |
| 243 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 244 | <p>The reports generated by Systrace are interactive, allowing you to zoom into and out of |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 245 | the process execution details. Use the <em>W</em> key to zoom in, the <em>S</em> |
| 246 | key to zoom out, the <em>A</em> key to pan left and the <em>D</em> key to pan |
| 247 | right. Select a task in timeline using your mouse to get more information about the task. |
| 248 | For more information about the using the keyboard navigation shortcuts and navigation, see the |
| 249 | <a href="{@docRoot}tools/help/systrace.html#viewing-options">Systrace</a> reference |
| 250 | documentation.</p> |
| 251 | |
| 252 | <h3 id="long-processes">Long running processes</h3> |
| 253 | |
| 254 | <p>A well-behaved application executes many small operations quickly and with a regular rhythm, |
| 255 | with individual operations completing within few milliseconds, depending on the device |
| 256 | and the processes being performed, as shown in figure 2:</p> |
| 257 | |
| 258 | <img src="{@docRoot}images/systrace/process-rhythm.png" alt="Systrace exerpt of app processing" |
| 259 | id="figure2" /> |
| 260 | <p class="img-caption"> |
| 261 | <strong>Figure 2.</strong> Excerpt from a trace of a smoothly running application with a regular |
| 262 | execution rhythm. |
| 263 | </p> |
| 264 | |
| 265 | <p>The trace excerpt in figure 2 shows a well-behaved application with |
| 266 | a regular process rhythm (1). The lower section of figure 2 shows a magnified section of |
| 267 | the trace indicated by the dotted outline, which reveals some irregularity in the process |
| 268 | execution. In particular, one of the wider task bars, indicated by (2), is taking slightly |
| 269 | longer (14 milliseconds) than other, similar tasks on this thread, which are averaging between |
| 270 | 9 and 12 milliseconds to complete. This particular task execution length is likely not noticeable |
| 271 | to a user, unless it impacts another process with specific timing, such as a screen update.</p> |
| 272 | |
| 273 | <p>Long running processes show up as thicker than usual execution bars in a trace. These thicker |
| 274 | bars can indicate a problem in your application performance. When they show up in your |
| 275 | trace, zoom in on the process using the |
| 276 | <a href="{@docRoot}tools/help/systrace.html#viewing-options">keyboard navigation</a> shortcuts to |
| 277 | identify the task causing the problem, and click on the task to get more information. You should |
| 278 | also look at other processes running at the same time, looking for a thread in one process that is |
| 279 | being blocked by another process.</p> |
| 280 | |
| 281 | |
| 282 | <h3 id="display-interupts">Interruptions in display execution</h3> |
| 283 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 284 | <p>The Systrace tool is particularly useful in analyzing application display slowness, |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 285 | or pauses in animations, because it shows you the execution of your application across multiple |
| 286 | system processes. With display execution, drawing screen frames with a regular rhythm is essential |
| 287 | for good performance. Having a regular rhythm for display ensures that animations and motion are |
| 288 | smooth on screen. If an application drops out of this rhythm, the display can become jerky or slow |
| 289 | from the users perspective.</p> |
| 290 | |
| 291 | <p>If you are analyzing an application for this type of problem, examine the |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 292 | <strong>SurfaceFlinger</strong> process in the Systrace report where your application is |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 293 | also executing to look for places where it drops out of its regular rhythm.</p> |
| 294 | |
| 295 | <img src="{@docRoot}images/systrace/display-rhythm.png" alt="Systrace exerpt of display processing" |
| 296 | id="figure3" /> |
| 297 | <p class="img-caption"> |
| 298 | <strong>Figure 3.</strong> Excerpt from a trace of an application showing interruptions in |
| 299 | display processing. |
| 300 | </p> |
| 301 | |
| 302 | <p>The trace excerpt in figure 3 shows an section of a trace that indicates an interruption in the |
| 303 | device display. The section of the <strong>SurfaceFlinger</strong> process in top excerpt, |
| 304 | indicated by (1), shows that display frames are being missed. These |
| 305 | dropped frames are potentially causing the display to stutter or halt. Zooming into this problem |
| 306 | area in the lower trace, shows that a memory operation (image buffer dequeuing and allocation) in |
| 307 | the <strong>surfaceflinger</strong> secondary thread is taking a long time (2). This delay |
| 308 | causes the application to miss the display update window, indicated by the dotted |
| 309 | line. As the developer of this application, you should investigate other threads in your |
| 310 | application that may also be trying to allocate memory at the same time or otherwise blocking |
| 311 | memory allocation with another request or task.</p> |
| 312 | |
| 313 | <p>Regular, rhythmic execution of the <strong>SurfaceFlinger</strong> process is essential to smooth |
| 314 | display of screen content, particularly for animations and motion. Interruptions in the regular |
| 315 | execution pattern of this thread is not always an indication of a display problem with your |
| 316 | application. Further testing is required to determine if this is actually a performance problem |
| 317 | from a user perspective. Being able to identify display execution patterns like the example above |
| 318 | can help you detect display problems and build a smooth-running, high-performance application. |
| 319 | </p> |
| 320 | |
| 321 | <p class="note"> |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 322 | <strong>Note:</strong> When using Systrace to analyze display problems, make sure |
| Joe Fernandez | 5e3a1c0 | 2012-08-24 12:34:43 -0700 | [diff] [blame] | 323 | you activate the tracing tags for <strong>Graphics</strong> and <strong>Views</strong>. |
| 324 | </p> |
| 325 | |
| Joe Fernandez | 25ef208 | 2013-09-16 11:31:25 -0700 | [diff] [blame] | 326 | <p>For more information on the command line options and keyboard controls for Systrace, |
| 327 | see the <a href="{@docRoot}tools/help/systrace.html">Systrace</a> help page.</p> |