Objective
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"
is the statistical result of the system invoking the internal API.
Basic concepts
1. The mobile phone consists of a number of "parts", so-called "parts" means: Cpu,wifi,gps .... So, Android
App
total power Consumption is the total amount of power consumed by each component during the app's run. 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
by Q (capacitance, unit: mAh) = I * t indicates battery charge。
System Source Analysis
Core source :/packages/apps/settings/src/com/android/settings/fuelgauge/powerusagesummary.java
Core class :
-Batterystatsimpl: Provides running time for each part of the app.
-Powerprofile: Provides the component current value.
Questions :
-How does Android store and read app power consumption information (i.e.: How does the Batterystatsimpl data come from?) )
-How does Android store the component current values (i.e.: How did the Powerprofile data come from?) )
-Android Specific power consumption calculation method
1. How Android stores and reads app power consumption information
(1) First look at the Powerusagesummary.java how to get Batterystatsimpl?
Visible Batterystatsimpl is obtained through the system service "Batteryinfo".
(2) What is the system service "Batteryinfo"? (See: Batterystatsservice.java)
System service "Batteryinfo" is actually batterystatsservice, and Batterystatsservice "unique" constructor provides a very important message: filename!
(3) where is Batterystatsservice created? What is filename? (See: Activitymanagerservice.java)
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:
The Betterystats.bin file is just a file that records the amount of power used by different apps.
(4) Look again at the Batterystatsimpl (String filename) constructor (see: Batterystatsimpl.java)
Only some basic initialization is done here. The Betterystats.bin data is actually loaded in (Activitymanagerservice.java) Mbatterystatsservice.getactivestatistics (). readLocked ();
At this point, Android how to store and read the app power consumption information analysis ended.
Summarize:
(1) Activitymanagerservice Create and initialize Batterystatsservice, and incoming power consumption record file Batterystats.bin;
(2) Batterystatsservice Create Batterystatsimpl instance internally and pass in power consumption Volume record file Batterystats.bin;
(3) Activitymanagerservice executes Mbatterystatsservice.getactivestatistics (). readlocked (); causes batterystatsservice Batterystatsimpl loading Batterystats.bin data;
(4) When Powerusagesummary calculates the app power consumption, powerusagesummary obtains Batterystatsimpl instances from the Batterystatsservice to obtain data about the app.
2. How Android stores component current values
(1) Relatively simple, see Powerprofile.java
Powerprofile reads the resource com.android.internal.r.xml.power_profile and loads the data into the Spowermap.
(2) where is Com.android.internal.r.xml.power_profile?
In the official document "Power Profiles for Android" Clear Power_profile.xml Location: Device///frameworks/base/core/res/res/xml/power_ Profile.xml.
The following is a Samsung Power_profile.xml:
field meaning See "Power Profiles for Android".
(3) Each OEM has its own independent power_profile.xml configuration
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 ...
(4) Powerprofile key API:
-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).
-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.
At this point, Android how to store part current numerical analysis ends.
Summarize:
(1) Android Parts current information stored in: Power_profile.xml
(2) Each OEM has a private power_profile.xml
(2) powerprofile read Power_profile.xml, and provide API access Parts Current value.
3. How to calculate the specific power consumption of Android
App power consumption statistics: processappusage ()
Hardware power consumption Statistics: processmiscusage ()
processappusage () analysis
What is the time period for the "1" Processappusage power consumption statistics?
For the time period of statistics, Batterystats has 4 options:
As you can see, Processappusage is the app's power consumption statistics from the last time the device was unplugged.
"2" Processappusage's stats are really apps?
The specific statistical process is in the For loop, the amount ... So the true statistical granularity of Processappusage is the UID.
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.
"3" Power consumption calculation formula-Part 1: Calculates the power consumption data for each process under the UID and sums it up.
Uid_power1 = (process1_power + ... + processn_power);
Process_power = (Cpuspeed_time * power_cpu_active );
"4" Power consumption calculation formula-Part 2: Calculate UID for Wake lock power consumption
Here, Android only calculates the power consumption of the partial wake lock.
uid_power2 = partialwakelock_time * Power_cpu_wake
"5" Power consumption calculation Formula-Part 3: Calculate UID Data flow (traffic) power consumption
Uid_power3 = (tcpbytesreceived + tcpbytessent) * Averagecostperbyte
"6" Power consumption calculation Formula-Part 4: Calculate UID WiFi power consumption.
uid_power4 = wifirunningtimems * power_wifi_on
"7" Power consumption calculation formula-Part 5: Calculate UID other sensor power consumption.
Uid_power5 = (sensor1_power + ... + sensorn_power)
sensor_power = sensor_time * Power_sensor
At this point, the app power consumption calculation method analysis ended. Hardware power consumption statistics (processmiscusage ()) are also similar.
Summarize app power consumption calculation formula:
Uid_power (app power consumption, Unit: mAh) = uid_power1 + uid_power2 + uid_power3 + uid_power4 + uid_power5
Uid_power1 = (process1_power + ... + processn_power);
-Process_power = (Cpuspeed_time * power_cpu_active);
Uid_power2 = Partialwakelock_time * Power_cpu_wake
Uid_power3 = (tcpbytesreceived + tcpbytessent) * Averagecostperbyte
Uid_power4 = Wifirunningtimems * power_wifi_on
Uid_power5 = (sensor1_power + ... + sensorn_power)
-Sensor_power = Sensor_time * power_sensor
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 hyddd
Source: http://www.cnblogs.com/hyddd/
This article is copyrighted by the author, welcome reprint, deduction or for commercial purposes, but must explain the source of this article (including links).
Android app power consumption statistics in layman's