how to monitor battery usage in android apps per activity? - android

I want to monitor battery usage on a very granular level. Like what is the battery usage of each individual activity, or even more granular detail like what is the battery usage in running a for loop of my app. Is there any android app or developer tool using which I can do that on an app whose code I already have?

No, because your phone is not capable of measuring "battery usage on a very granular level".
The closest you will get is with a Qualcomm MDP device and their battery measurement software. Even then, the battery usage by process is somewhat guesswork, as the contributors to power drain (CPU, screen, radios, etc.) are shared by all running processes that use them. A few other off-the-shelf devices may also work as well, though I suspect that the battery measurement software will work a lot better on an MDP, as it has dedicated hardware for this stuff.
Getting finer-grained detail than that will be impractical. At best, with a lot of testing, you might be able to draw some conclusions comparing two algorithms, but for most such algorithms, you would probably be just as well off measuring how much CPU time they took, and extrapolate battery usage from there.

I'm guessing that you want to use algorithms that are very efficient to minimize power usage by your program. If this is true, you are going about it the wrong way. The power consumption by computational algorithms is trivial compared to (a) the usage of external peripherals such as blue tooth and wireless, and (b) preventing the processor from dropping into an idle state. To maximize the power efficiency of your app, you want it to be idle (meaning asleep, not spinning) for as long as possible.

Related

Android Power consumption of a method

For a research project I need to measure the power consumption of some functionality. So I would like to measure the power consumption of a method. For example: method computeSomething() needed x microampere-hours. Is this possible? If yes, how?
I tried to measure the remaining battery capacity in microampere-hours and the remaining energy in nanowatt-hours before and after execution with the BatteryManager. But this isn't accurate enough. Is that even possible to do it?
this is what i tried:
mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
EDIT:
for sure it's the emulator which don't consume energy and that's why I get a consumption of 0. Is it possible to simulate power consumption with the android emulator or do I need a real phone?
Here's how I would measure the energy used (because that's what you want and not power).
Shutdown (or don't use) everything I can, including network, cell, USB, etc.
Disable any power saving features on the phone
Log any data you get on the phone's file system (vs through the USB or wireless)
Wrap the program in a large loop that makes its runtime at least a few seconds, perhaps a few minutes
Run the test program you just made many times (>=16 times for statistical significance)
Measure the phone at rest over the same amount of time and collect the energy used.
ANALYSIS: (and you need to do this)
Check on the statistical deviation (std dev) of both your function run and the "at rest" run. If the std dev is large, something else is going on that is using energy.
Find out the resolution of BatteryManager. Just because the field is labeled in nWatts doesn't mean the hardware measures it at that resolution (and it probably doesn't). The people who develop a data structure want that structure to be useful on a lot of hardware and for the foreseeable future. Thus they make the field measure in something very small (e.g. nWatts) even if most hardware can only measure at best, say, in tenth of watts.
If possible, use a hardware interposer between the battery and the phone to measure energy/power directly. This gets around the uncertainty in the implementation of BatteryManager.
Just to be safe, you might also find out how long of an integration window the BatteryManager uses for measuring power. These measurements usually involve a moving window for computing averages.
An emulator isn't going to give you any useful information. Emulators test functionality and little else. Even a rough measure of performance is very iffy.
I've developed a similar App before but is used to evaluate the power consumption of each Application. But as far as I know, the Google's battery static API evaluate power consumption according to each UID, both hardware and software. I think you can find more information by looking into the source code of Android, especially for BatterySipper.java and BatteryStatsHelper.java.
For emulator, Android system uses power_profile.xml as reference to calculate the power consumption. This file is usually modified by manufacturer. Maybe it's not included in emulator.

Application Battery Consumption Tool

Is there a tool that would allow you to measure the battery used in milli amps or milli watts by your application on an iPhone or an Android. Specifically, I want to run my application with a few test cases, then be able to see how much more battery was used by me running the application versus the phone sitting idle. I was able to find a tool for Android here from Intel, however, it does not offer a similar solution for IOS. Preferably, I would like to use the same tool for both IOS and Android..
PowerTutor is an application for Google phones that displays the power consumed by major system components such as CPU, network interface, display, and GPS receiver and different applications. The application allows software developers to see the impact of design changes on power efficiency. Application users can also use it to determine how their actions are impacting battery life. PowerTutor uses a power consumption model built by direct measurements during careful control of device power management states. This model generally provides power consumption estimates within 5% of actual values. A configurable display for power consumption history is provided. It also provides users with a text-file based output containing detailed results. You can use PowerTutor to monitor the power consumption of any application.
http://ziyang.eecs.umich.edu/projects/powertutor/

Energy consumption of smartphone components

I'm looking for a list of all the components and their power drainage on an up-to-date smart phone.
Accelerometer, gyroscope, magnetometer, etc.
Display
WiFi
Bluetooth
GPS
CPU
Camera
Microphone
etc.
Preferably in mA so it can be easily compared to the battery's capacity (usually specified in mAh).
The Sensor's power is actually available via the SDK and can also easily figured out for most devices on AndroidFragmentation. However what I'm looking for is comparable data for the other hardware components to consider their efficency.
Bonus: Will a request for less frequent updates of a Sensor decrease energy consumption of the Sensor, as it returns only one value for getPower()?
There are a couple of detailed studies that I am able to find on this.
A study from the USENIX meeting in 2010 which studies various components of a smartphone (except the camera)
Another study from the Hotmobile mobile computing workshop 2013 that has more information on cameras and displays.
Reference 1 especially seems a great starting point.
I'm looking for a list of all the components and their power drainage on an up-to-date smart phone.
That is impossible to answer.
First, different devices will use different varieties of these components, with different power characteristics.
Second, many, if not most, of those components will have no published power statistics, or the specific components themselves may not be knowable without a complete teardown of a device.
Will a request for less frequent updates of a Sensor decrease energy consumption of the Sensor, as it returns only one value for getPower()?
That will depend on the sensor. Some sensors are effectively always "on" (e.g., ambient light sensor), courtesy of the OS, in which case the only incremental power drain for your use of that sensor will be in passing that sensor data to your process. Other sensors might not be regularly used by the OS, meaning that your request for events from that sensor might turn it "on", resulting in power drain from the sensor itself in addition to supplying you with that data.
It would be truly wonderful if all Android devices were instrumented in the way the Qualcomm MDP is, so that we could get fine-grained power detail for our apps and their usage of various components.
There was a Google I/O session on this very subject a few years ago; you can see the video here and slides pdf here.
I know it's against the rules to plug your own startup, but what you're asking sounds exactly like what we're working on.
There's an Android performance monitoring tool called "Little Eye Labs". It shows real-time power consumption of an App as it runs on a phone. It currently only supports CPU, Display, GPS, Wifi and 3G, but you'll be able to get the instantaneous power consumed (in mW) by these components.
/end of plug
Note that there's no real way to get this information directly from a device, so the best you can do is model the phone, gather device resource consumption, and model power usage.
Display is the most power consuming part of the smartphones; accounting for up to 60% of total battery life (Can draw power up to 2W). There is a book called Green IT and its Applications; you can read it online in Google books.
On any modern Android, go to Settings > Battery (sometimes Settings > About > Battery). You should see a graph of power drain over time, as well as how much was used by what component.
Although consumption varies a lot based on usage patterns, in my experience the top consumers are display, radio, and CPU. I have not seen sensors rank high in energy use on any of my devices, in the absence of bugs. The link provided by Yusuf X places game sensors above CPU.
For more information about optimizing battery use on Android, see Reducing the battery impact of apps that downloads content over a smartphone radio and Optimizing Battery Life.
There is an app called PowerTutor that it does some battery consumption measurements for every phone component and for every process. The code is open so you can pick up some ideas from there. Note that this app was optimized for Google's phone, especially for the Nexus One.

Monitoring per Application power usage in android

Is there a way to get the average current/power consumption of a particular application in Android? I could only find private API PowerProfile.java and PowerUsageSummary.java which give some information, but I am hitting a dead end, can someone please help?
Is there a way to get the average current/power consumption of a particular application in Android?
No, because applications do not consume current/power. Hardware does. If six applications are using WiFi, it is very difficult to "assign blame" for the WiFi power consumption, for example.
Now, even getting hardware power information is difficult in Android, as there are no public APIs for it, and most hardware is not instrumented particularly well to indicate what is consuming power. The Qualcomm MDP has great instrumentation for this, along with Trepn software to help you collect it, but it is rather expensive.

Prevent battery "damages"

I'm currently developing with Android SDK, and because I'm building an application based on camera, I cannot use the emulator to test it.
This means that the following scenario happens quite frequently
I connect my phone to USB the battery is 50%
I disconnect it before it is fully charged, e.g. 80%
I have to leave, and I want to take my phone with me
I come back and connect again and so on...
I read that Lithium-ion batteries don't suffer the memory effect, but, on the other hand, I also read that one should wait until the battery is fully charged before disconnecting it from the USB.
So, I'm wondering if the way I'm using the phone now for debugging may damage or reduce the life of my battery
Any advice is appreciated
First of all, I'm not an expert in this subject.
Lithium-ion battery, as you say, does not suffer the charge memory effect but they have the "digital memory" (affects the accuracy % remaining battery) as reflected in this article (so you have to fully-discharge - fully-charge periodically the battery to avoid this):
Source: http://www.techrepublic.com/blog/five-tips/five-tips-for-extending-lithium-ion-battery-life/289
More info about it:
"Partial discharge on Li-ion is fine; there is no memory and the battery does not need periodic full discharge cycles other than to calibrate the fuel gauge on a smart battery"
So partial discharge is not a problem. The worst condition is keeping a fully charged battery at elevated temperatures
Source: http://batteryuniversity.com/learn/article/how_to_prolong_lithium_based_batteries
About the need to fully-charge in Lithium-ion batteries, I did find a source about it, but in Apple support you can find this:
"You can disconnect and use your iPod before the battery is fully charged. You don't have to wait until the battery is completely empty before charging it again. This is because lithium-ion batteries, such as those used in iPod, have none of the memory effects seen in nickel-based rechargeable batteries"
Source: http://support.apple.com/kb/ht1384
Conclusion: Your partial charges/discharges won't be noticeable. If you want to extend the battery life, follow the suggestions you can find in the first two articles (avoid hot, don't let fully discharge... etc).

Categories

Resources