[android] in Layman's Android app power consumption statistics

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Reference to:http://www.cnblogs.com/hyddd/p/4402621.html</strong></p></p><p><p><strong>Objective</strong></p></p><p><p>In Android Statistics app power consumption is more troublesome, until Android 4.4, it still does not disclose "battery statistics" API or document ... well, yes, It's not public, It's Not. Data you see on your Phone's "set-charge"</p></p><p><p></p></p><p><p>is the statistical result of the system invoking the internal Api.</p></p><p><p></p></p><p><p><strong>Basic concepts</strong></p></p>1. The mobile phone consists of a number of "parts", so-called "parts" means: Cpu,wifi,gps .... so, Android<strong><strong>App</strong></strong><strong><strong>total power Consumption is the total amount of power consumed by each component during the App's Run. </strong></strong>2. Assume that running the app causes the CPU to run, time: t,cpu unit time consumption: w, the App's CPU power consumption is: w = w*t, and there is a physical formula W = u*i*t (U: voltage value, I: Current value), in the phone, the general U constant, so you can separate<strong><strong>by Q (capacitance, unit: mAh) = I * t indicates battery charge</strong></strong>。<strong><strong>System Source Analysis</strong></strong><p><p><strong>Core Source</strong> :/packages/apps/settings/src/com/android/settings/fuelgauge/powerusagesummary.java</p></p><p><p><strong>Core class</strong> :</p></p><p><p>-batterystatsimpl: provides running time for each part of the app.</p></p><p><p>-powerprofile: provides the component current Value.</p></p><p><p><strong>Questions</strong> :</p></p><p><p>-how does Android store and read app power consumption information (i.e.: How does the Batterystatsimpl data come from?) )</p></p><p><p>-how does Android store the component current values (i.e.: How did the Powerprofile data come from?) )</p></p><p><p>-android Specific power consumption calculation method</p></p><p><p></p></p><strong><strong>1. How Android stores and reads app power consumption information</strong></strong><p><p>(1) First Look at the Powerusagesummary.java how to get batterystatsimpl?</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Visible Batterystatsimpl is obtained through the system service "batteryinfo".</p></p><p><p></p></p><p><p>(2) What is the system service "batteryinfo"? (see: Batterystatsservice.java)</p></p><p><p></p></p><p><p>System service "batteryinfo" is actually batterystatsservice, and Batterystatsservice "unique" constructor provides a very important message: filename!</p></p><p><p></p></p><p><p>(3) where is Batterystatsservice created? What is filename? (see: Activitymanagerservice.java)</p></p><p><p></p></p><p><p>FileName file is:/data/system/batterystats.bin, about batterystats.bin, before many articles say it is used as battery correction, but Android engineer Dianne Hackborn in Google + On Clear:</p></p><p><p></p></p><p><p>The Betterystats.bin file is just a file that records the amount of power used by different apps.</p></p><p><p></p></p><p><p>(4) look again at the Batterystatsimpl (String Filename) constructor (see: Batterystatsimpl.java)</p></p><p><p></p></p><p><p>Only some basic initialization is done here. The Betterystats.bin data is actually loaded in (activitymanagerservice.java) mbatterystatsservice.getactivestatistics (). readLocked ();</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>At this point, Android how to store and read the app power consumption information Analysis Ended.</p></p><p><p><strong>Summarize:</strong></p></p><p><p><strong>(1) Activitymanagerservice Create and initialize batterystatsservice, and incoming power consumption record file batterystats.bin;</strong></p></p><p><p><strong>(2) Batterystatsservice Create Batterystatsimpl instance internally and pass in power consumption</strong> <strong>Volume</strong> <strong>Record file batterystats.bin;</strong></p></p><p><p><strong>(3) Activitymanagerservice executes mbatterystatsservice.getactivestatistics (). readlocked (); causes batterystatsservice Batterystatsimpl Loading Batterystats.bin data;</strong></p></p><p><p><strong>(4) when Powerusagesummary calculates the app power consumption, Powerusagesummary obtains Batterystatsimpl instances from the Batterystatsservice to obtain data about the app.</strong></p></p><p><p></p></p><p><p><strong>2. How Android stores component current values</strong></p></p><p><p>(1) relatively simple, see Powerprofile.java</p></p><p><p></p></p><p><p>Powerprofile reads the resource com.android.internal.r.xml.power_profile and loads the data into the SPOWERMAP.</p></p><p><p></p></p><p><p>(2) where is com.android.internal.r.xml.power_profile?</p></p><p><p>In the official document "Power Profiles for Android" clear Power_profile.xml location: device///frameworks/base/core/res/res/xml/power_ Profile.xml.</p></p><p><p>The following is a Samsung Power_profile.xml:</p></p><p><p></p></p><p><p>field meaning see "Power Profiles for Android".</p></p><p><p></p></p><p><p>(3) Each OEM has its own independent power_profile.xml configuration</p></p><p><p>Official documentation indicates that OEMs should have their own power_profile.xml, because parts such as: cpu, wifi ... The power consumption should be related to the specific hardware, this only OEM manufacturer clear ...</p></p><p><p></p></p><p><p></p></p><p><p>(4) powerprofile Key Api:</p></p><p><p>-public Double Getaveragepower (String type): returns the current value (mA) of the type that represents a keyword in power_profile.xml (for example: gps.on).</p></p><p><p>-public Double getaveragepower (String type, Int. level): The current value (mA) of the type is returned, and level represents the first value of the array in the XML.</p></p><p><p></p></p><p><p>At this point, Android how to store part current numerical analysis ends.</p></p><p><p><strong>Summarize:</strong></p></p><p><p><strong>(1) Android</strong> <strong>Parts</strong> <strong>current information stored in: power_profile.xml</strong></p></p><p><p><strong>(2) Each OEM has a private power_profile.xml</strong></p></p><p><p><strong>(2) Powerprofile read power_profile.xml, and provide API access</strong> <strong>Parts</strong> <strong>Current Value. </strong></p></p><p><p></p></p><p><p><strong>3. How to calculate the specific power consumption of Android</strong></p></p><p><p>App power consumption statistics: processappusage ()</p></p><p><p>Hardware power consumption statistics: processmiscusage ()</p></p><p><p></p></p><p><p><strong>processappusage () Analysis</strong></p></p><p><p><strong>What is the time period for the "1" processappusage power consumption statistics? </strong></p></p><p><p></p></p><p><p></p></p><p><p>For the time period of statistics, Batterystats has 4 options:</p></p><p><p></p></p><p><p>As you can see, Processappusage is the App's power consumption statistics from the last time the device was Unplugged.</p></p><p><p></p></p><p><p><strong>"2" Processappusage's stats are really apps? </strong></p></p><p><p></p></p><p><p>The specific statistical process is in the for loop, the amount ... So the true statistical granularity of Processappusage is the UID.</p></p><p><p>UID and App Relationship: 2 app signatures and Shareduserid are the same, they have the same UID at run Time. That is to say, processappusage statistics may be multiple app power consumption data, for ordinary apps, this is less likely to happen, and for Android system applications are more Common.</p></p><p><p></p></p><p><p><strong>"3" Power consumption calculation Formula-part 1: calculates the power consumption data for each process under the UID and sums it up. </strong></p></p><p><p><strong>uid_power1 = (process1_power + ... + processn_power);</strong></p></p><p><p><strong>process_power = (cpuspeed_time * power_cpu_active</strong> <strong>);</strong></p></p><p><p><strong>           </strong></p></p><p><p></p></p><p><p><strong>"4" Power consumption calculation Formula-part 2: calculate UID for Wake lock power consumption</strong></p></p><p><p>here, Android only calculates the power consumption of the partial wake Lock.</p></p><p><p><strong>uid_power2 = partialwakelock_time * Power_cpu_wake</strong></p></p><p><p><strong>           </strong></p></p><p><p></p></p><p><p><strong>"5" Power consumption calculation formula-part 3: calculate UID Data Flow (traffic) power consumption</strong></p></p><p><p><strong>Uid_power3 = (tcpbytesreceived + Tcpbytessent) * averagecostperbyte</strong></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>"6" Power consumption calculation formula-part 4: calculate UID WiFi power Consumption. </strong></p></p><p><p><strong>uid_power4 = wifirunningtimems * power_wifi_on</strong></p></p><p><p></p></p><p><p></p></p><p><p><strong>"7" Power consumption calculation Formula-part 5: calculate UID other sensor power Consumption. </strong></p></p><p><p><strong>Uid_power5 = (sensor1_power + ... + sensorn_power)</strong></p></p><p><p><strong>sensor_power = sensor_time * Power_sensor</strong></p></p><p><p></p></p><p><p>At this point, the app power consumption calculation method analysis Ended. Hardware power consumption Statistics (processmiscusage ()) are also similar.</p></p><p><p><strong>Summarize app power consumption calculation Formula:</strong></p></p><p><p><strong>Uid_power (app Power consumption, unit: mAh) = uid_power1 + uid_power2 + uid_power3 + uid_power4 + uid_power5</strong></p></p><p><p><strong>Uid_power1 = (process1_power + ... + processn_power);</strong></p></p><p><p><strong>-process_power = (cpuspeed_time * power_cpu_active);</strong></p></p><p><p><strong>Uid_power2 = Partialwakelock_time * Power_cpu_wake</strong></p></p><p><p><strong>Uid_power3 = (tcpbytesreceived + Tcpbytessent) * averagecostperbyte</strong></p></p><p><p><strong>Uid_power4 = Wifirunningtimems * power_wifi_on</strong></p></p><p><p><strong>Uid_power5 = (sensor1_power + ... + sensorn_power)</strong></p></p><p><p><strong>-sensor_power = Sensor_time * power_sensor</strong></p></p>Say so much, come ... no, to a statistical power consumption of the app bar, in fact, someone has already put this part of the Android system code out, made an app, you can download here. --------------i'm a split Line--------------there's a good news: after android5.0, getting power data doesn't have to be so painful, Dumpsys batterystats Data. Contains: Estimated power use (mAh):, the following is the consumption of each uid, as long as the app all the UID consumption added up!<p><p>[android] in Layman's Android app power consumption statistics</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.