Detailed battery usage - iOS

My current project is a permanent presence application (I think, Tinder or Foursquare), and battery consumption is through the roof. We believe that the main power needs are GPS and WiFi antennas. We would like to be able to measure the energy consumption of an application in several different configurations.

But how to do that? We need a process that:

  • It can be used with a phone disconnected from the computer (so we know that we use a battery, not power supply via USB),
  • It has sufficient detail to allow us to correlate energy surges with application events (application launch, location update, sending analytics information to Mixpanel, etc.),
  • You can run all night without a nanny,
  • It can be exported as CSV or whatever for statistical analysis.

Enough list, I know.

These are the requirements, and here are the options that I know of:

1. Inclusion of non-loose energy diagnostics. Register on your iOS device and export to Tools

This is an obvious answer, but it has one gigantic flaw.

Pros:

  • Uses battery instead of USB power.
  • Excellent grit (1 s time series data, 20 levels of discrete energy consumption),
  • Matches other device events, such as using a GPS antenna, etc.

Minuses:

2. Monitoring the connected phone using tools

Pros:

  • The same excellent granularity and correlation with other events of the device.
  • The battery cannot work.

Minuses:

  • Do not use a battery, so energy consumption is not comparable to use in the real world.
  • Tools do not even reliably show energy consumption. Sometimes it's just empty.
  • Unable to export to CSV.

3. Using public Cocoa APIs to record energy consumption in our application - [UIDevice.currentDevice batteryLevel]

This is the most common answer to SO. I looked at Estimated battery life on iOS , iphone: calculating battery life and about a dozen others.

Pros:

  • Arbitrary time between measurements.
  • It can be saved even if the battery dies, writing to the disk somehow (CoreData, by default, a network, whatever).
  • You can select any data format, for example CSV.

Minuses:

  • Much more work than others.
  • The open API gives you a battery level of up to 5% accuracy. This is essentially the time integral of the instantaneous energy consumption data that we get from approaches 1 and 2. Not granular enough to correlate with other device events (but, perhaps, enough to get a general estimate of the device’s battery life).

4. Using Cocoa Private APIs to Record Power Consumption

As we will only do this during development, it does not matter if Apple rejects the application to use the private API. Presumably there is a private API for this, since Apple can record data using Untethered Energy Diagnostics.

Pros:

  • Arbitrary detailing, arbitrary file format, arbitrary persistence.

Minuses:

  • Waaaaay has more work to figure out how to use it. Perhaps not even possible.

5. The combined approach

We could use raw diagnostics to quantify the marginal cost of energy for each action. "Well, a GPS rotating antenna occupies 150 mW • H. An estimated position takes 50 mW • H. Sending a Mixpanel event takes 25 mW • H unless we made another network call within the previous 30 seconds, in which case it takes 15 MW • H. " (all numbers are thought up on the spot.) Then we can use tethered monitoring to record when each of these actions happens, connect to a linear equation and estimate the amount of energy that it had to take.

Pros:

  • Flexible. Arbitrary everything.

Minuses:

  • Mathematics.
  • It is very easy to skip nonlinear contributions. Getting something like E = k0 / (gps polling interval) + k1 * (number of analytics calls) is easy, but what if the cost of deploying both antennas is less than the sum of deploying them separately?
  • Ignores any caching strategies that Apple can do internally in location services.
  • Even remotely close to real-time use.

In any case, I got quite mad. Has anyone done this before? How?

+6
source share
3 answers

Not sure if this applies to this particular use case, but I developed a library called UIDeviceListener that allows you to easily (basically, with one line of code) retrieve data from the operating system for energy consumption and many other battery related data points / charging: https://github.com/eldoogy/PowerData

Here is a sample dictionary to give you an idea of ​​the information you can get. I will pay attention to the InstantAmperage key. This shows the real-time power consumption in mA for the entire device (while the device is disconnected from the network). This can help accomplish what you are looking for here.

 { AdapterDetails = { Amperage = 1000; Description = "usb host"; FamilyCode = "-536854528"; PMUConfiguration = 1000; Watts = 5; }; AdapterInfo = 16384; Amperage = 1000; AppleRawCurrentCapacity = 1279; AppleRawMaxCapacity = 1275; AtCriticalLevel = 0; AtWarnLevel = 0; BatteryData = { BatterySerialNumber = REDACTED; ChemID = 355; CycleCount = 524; DesignCapacity = 1420; Flags = 640; FullAvailableCapacity = 1325; ManufactureDate = REDACTED; MaxCapacity = 1273; MfgData = REDACTED; QmaxCell0 = 1350; StateOfCharge = 100; Voltage = 4194; }; BatteryInstalled = 1; BatteryKey = "0003-default"; BootBBCapacity = 52; BootCapacityEstimate = 2; BootVoltage = 3518; CFBundleIdentifier = "com.apple.driver.AppleD1815PMU"; ChargerConfiguration = 990; CurrentCapacity = 1275; CycleCount = 524; DesignCapacity = 1420; ExternalChargeCapable = 1; ExternalConnected = 1; FullyCharged = 1; IOClass = AppleD1815PMUPowerSource; IOFunctionParent64000000 = <>; IOGeneralInterest = "IOCommand is not serializable"; IOInterruptControllers = ( IOInterruptController34000000, IOInterruptController34000000, IOInterruptController34000000, IOInterruptController34000000 ); IOInterruptSpecifiers = ( <03000000>, <26000000>, <04000000>, <24000000> ); IOMatchCategory = AppleD1815PMUPowerSource; IOPowerManagement = { CurrentPowerState = 2; DevicePowerState = 2; MaxPowerState = 2; }; IOProbeScore = 0; IOProviderClass = AppleD1815PMU; InstantAmperage = 0; IsCharging = 0; Location = 0; Manufacturer = A; MaxCapacity = 1275; Model = "0003-A"; Serial = REDACTED; Temperature = 2590; TimeRemaining = 0; UpdateTime = 1461830702; Voltage = 4182; "battery-data" = { "0003-default" = <...>; "0004-default" = <...>; "0005-default" = <...}; "built-in" = 1; } 

UIDeviceListener supports regular, non-jailbroken iOS devices and does not call any private APIs.

+2
source

GPS requires a 10% battery per hour, so it will last about 10 hours with highly accurate location recording once per second. location polling interval has nothing to do with battery consumption. either the GPS chip is on or not. No gps low power mode! you can disable gps if you know that the user does not need it.

for permanent recording of location data you can save only the battery if you decide to go with low accuracy (1000 megahertz tower or wlan localization) instead of high (3-6 m = GPS)

Of course, during the measurement, you will kill all applications for third-party developers, especially for messaging applications, the most famous of them connects to the Internet every second!

0
source

I cannot make suggestions for measuring power in iOS, but I can indicate some possible sources of energy consumption, as well as indicate some problems that should be taken into account when taking measurements.

Common sources of excessive energy consumption:

  • Poll instead of using interrupts. this prevents the processor from moving to lower power levels. Low power consumption can reduce processor consumption by an order of magnitude

  • Use interrupt intervals that are too small. A common mistake is that a shorter polling interval, say 10 ms, leads to a faster response. If the ability of your application / user to respond to requests is 500 ms, then 10 ms only so that the processor does not sleep and does not chew energy.

  • All that is transmitted is pigs receiving less, but still bad (WiFi, GPS, Bluetooth, etc., as mentioned above). Minimize their use.

  • Peripheral devices also have low power states. Uses them sparingly to keep them in a lower energy state.

Power Measurement Comments:

  • Monitoring itself can lead to excessive energy consumption. The power of control is very complex. Monitoring uses interrupts or polls that support the processor / device and are in a high power state.

  • Most monitoring comes from the OS, which may or may not be from “real” HW measurements versus “SW guesses”.

  • Trying to be accurate really means more frequent measurements, which, you guessed it, make the processor and devices stay awake.

I suggest that you carefully familiarize yourself with what sampling interval you really need, and design the application to allow the processor and devices to relax as much as possible.

0
source

Source: https://habr.com/ru/post/972469/


All Articles