1 page.title=Analyzing Power Use with Battery Historian 2 page.metaDescription=Improve network performance by optimizing image size. 3 4 meta.tags="power" 5 page.tags="power" 6 7 @jd:body 8 9 <div id="qv-wrapper"> 10 <div id="qv"> 11 12 <h2>In this document</h2> 13 <ol> 14 <li> 15 <a href="#sv">System-wide View</a> 16 </li> 17 <li> 18 <a href="#asd">App-Specific Data</a> 19 </li> 20 <li> 21 <a href="#usecases">Other Cases Where Battery Historian Can Help</a> 22 </li> 23 </ol> 24 <h2>See also</h2> 25 <ol> 26 <li> 27 <a href="https://github.com/google/battery-historian">Battery Historian 28 on GitHub</a> 29 </li> 30 31 <li> 32 <a href="https://developer.android.com/studio/profile/battery-historian.html"> 33 Batterystats and Battery Historian Walkthrough 34 </li> 35 36 <li> 37 <a href="https://youtu.be/VC2Hlb22mZM?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC&t=2063" 38 target="_blank"> 39 Battery Historian talk at Google I/O 2016</a> 40 </li> 41 </ol> 42 </div> 43 </div> 44 45 <p> 46 The Battery Historian tool provides insight into a devices battery consumption 47 over time. At a system-wide level, the tool visualizes power-related events from 48 the system logs in an HTML representation. At an app-specific level, the tool 49 provides a variety of data that can help you identify battery-draining app 50 behavior. 51 </p> 52 53 <p> 54 This document describes some of the ways you can use Battery Historian 55 to learn about battery-consumption patterns. The document begins by explaining 56 how to read the system-wide data that Battery Historian reports. Then, 57 it presents ways in which you can use Battery Historian to diagnose 58 and troubleshoot your own app's behavior related to battery consumption. 59 Last, it offers several tips on scenarios in which Battery Historian may be 60 particularly useful. 61 </p> 62 63 <h2 id="sv">System-wide View</h2> 64 65 <p> 66 The Battery Historian tool provides a system-wide visualization of various 67 app and system behaviors, along with their correlation against battery 68 consumption over time. This view, shown in Figure 1, can help you 69 diagnose and identify power use issues with your app. 70 </p> 71 72 <img src="{@docRoot}topic/performance/images/generic-timeline.png"> 73 <p class="img-caption"> 74 <strong>Figure 1.</strong> 75 Battery Historians display of system-wide events affecting power 76 consumption. 77 </p> 78 79 <p> 80 Of particular interest in this figure is the black, horizontal, downward trend 81 line representing Battery Level, measured on the y-axis. For example, at the 82 very beginning of the Battery Level line, at approximately 6:50 AM, the 83 visualization shows a relatively steep drop in battery level. 84 </p> 85 86 <p> 87 Figure 2 provides a close-up of that part of the display. 88 </p> 89 90 <img src="{@docRoot}topic/performance/images/s-generic-closeup.png"> 91 <p class="img-caption"> 92 <strong>Figure 2.</strong> 93 A close-up of the Battery Historian timeline from roughly 6:50 AM to 7:20 AM. 94 </p> 95 96 <p> 97 At the very beginning of the Battery Level line, as battery decline steeply, 98 the display shows three things happening: The CPU is running, an app has 99 acquired a wakelock, and the screen is on. In this way, Battery Historian helps 100 you understand what events are happening when battery consumption is high. You 101 can then target these behaviors in your app and investigate whether there are 102 related optimizations you can make. 103 </p> 104 105 <p> 106 The system-wide visualization can provide other clues, as well. For instance, if 107 it shows that the mobile radio is frequently being turned off and on, there may 108 be an opportunity to optimize this behavior through <a href=intelligent 109 scheduling page>intelligent scheduling APIs</a> such as JobScheduler or 110 Firebase Job Dispatcher. 111 </p> 112 113 <p> 114 The next section explains how to investigate behavior and events specific to 115 your own app. 116 </p> 117 118 <p> 119 <h2 id="asd">App-Specific Data</h2> 120 </p> 121 122 <p> 123 In addition to the macro-level data provided by the system-wide view, Battery 124 Historian also provides tables and some visualization of data specific to each 125 app running on your device. The tabular data includes: 126 </p> 127 128 <ul> 129 <li>The apps estimated power use on the device.</li> 130 <li>Network information.</li> 131 <li>Wakelocks.</li> 132 <li>Services.</li> 133 <li>Process info.</li> 134 </ul> 135 136 <p> 137 The tables provide two dimensions of data about your app. First, you can look 138 up where your apps power usage ranks compared to other apps. To do so, click 139 <em>Device Power Estimates</em> table under <em>Tables</em>. This example 140 examines a fictional app called Pug Power. 141 </p> 142 143 <img src="{@docRoot}topic/performance/images/app-rankings.png"> 144 <p class="img-caption"> 145 <strong>Figure 3.</strong> Investigating which apps consume the most power. 146 </p> 147 148 <p> 149 The table in Figure 3 reveals that Pug Power is the ninth biggest consumer of 150 battery power on this device, and the third biggest app that is not part of the 151 OS. This data suggests that this app bears deeper investigation. 152 </p> 153 154 <p> 155 To look up the data for a specific app, enter its package name into the lower 156 of the two dropdown menus under <em>App Selection</em>, located under the left 157 side of the visualization. 158 </p> 159 160 <img src="{@docRoot}topic/performance/images/dropdown.png"> 161 <p class="img-caption"> 162 <strong>Figure 4.</strong> Entering a specific app whose data to view. 163 </p> 164 165 <p> 166 When you select a specific app, the following data visualization categories 167 change to display app-specific data instead of system-wide data: 168 </p> 169 170 <ul> 171 <li>SyncManager.</li> 172 <li>Foreground process.</li> 173 <li>Userspace Wakelock.</li> 174 <li>Top app.</li> 175 <li>JobScheduler.</li> 176 <li>Activity Manager Proc.</li> 177 </ul> 178 179 The SyncManager and JobScheduler visualizations immediately make it obvious if 180 your app performs syncs and executes jobs more frequently than necessary. In 181 doing so, they can quickly reveal an opportunity to optimize your apps 182 behavior for improved battery performance. 183 184 <p> 185 You can also obtain one more piece of app-specific visualization data, 186 <em>Userspace Wakelock</em>. To include this information in the bug report, 187 enter the following command in your terminal window: 188 </p> 189 190 <pre> 191 $ adb shell dumpsys batterystats --enable full-wake-history 192 </pre> 193 194 <p class="note"> 195 <strong>Note:</strong> From Android 6.0 (API level 23), the platform includes 196 Doze functionality, which imposes certain optimizations on apps. For example, 197 Doze batches jobs to take place during brief maintenance windows, regardless of 198 how JobScheduler has scheduled them. 199 </p> 200 201 <p> 202 Figures 5 and 6 show data for Pug Power: Figure 5 203 shows the visualization of 204 the app-specific data, and Figure 6 shows the corresponding tabular data. 205 </p> 206 207 <img src="{@docRoot}topic/performance/images/pug-visualization.png"> 208 <p class="img-caption"> 209 <strong>Figure 5.</strong> Visualization of data for fictional app Pug Power. 210 </p> 211 212 <img src="{@docRoot}topic/performance/images/pugspecificdata.png"> 213 <p class="img-caption"> 214 <strong>Figure 6.</strong> Tabular data for the fictional Pug Power app. 215 </p> 216 217 <p> 218 A look at the visualization does not show anything immediately obvious. 219 The JobScheduler line shows that the app has no jobs scheduled. The SyncManager 220 line shows that the app has not performed any syncs. 221 </p> 222 223 <p> 224 However, examination of the <em>Wakelocks</em> segment of the tabular data 225 reveals that Pug Power acquires wakelocks totaling over an hour. This unusual 226 and costly behavior can account for the apps high level of power consumption. 227 This piece of information helps the developer target an area where optimization 228 is likely to greatly help. In this case, why does the app acquire so much 229 wakelock time, and how can the developer ameliorate this behavior? 230 </p> 231 232 <h2 id="usecases">Other Cases Where Battery Historian Can Help</h2> 233 234 <p> 235 There are many other cases in which Battery Historian can help you diagnose 236 opportunities for improving battery behavior. For example, Battery Historian 237 can tell you if your app is: 238 </p> 239 240 <ul> 241 <li>Firing wakeup alarms overly frequently (every 10 seconds or less).</li> 242 <li>Continuously holding a GPS lock.</li> 243 <li>Scheduling jobs every 30 seconds or less.</li> 244 <li>Scheduling syncs every 30 seconds or less.</li> 245 <li>Using the cellular radio more frequently than you expect.</li> 246 </ul> 247 248