18 Power Consumption Optimization Part1 Analyzing Power Consumption Through Progression of Power Optimization

18 Power Consumption Optimization (Part 1): Analyzing Power Consumption from the Perspective of Battery Optimization #

There was a joke that said, “A man who uses an Android phone must be a good man because he has to go home and charge it every day, sometimes even twice a day.”

We cannot live or work without our phones nowadays, yet it is difficult to find a phone that we can completely rely on and use throughout the day. In the feature phone era ten years ago, Nokia phones could last for more than ten days on standby. However, in the smartphone era, with CPUs at 7nm, 8GB of RAM, and 512GB of flash storage, the hardware has been rapidly evolving. Why hasn’t battery technology made significant breakthroughs and kept up with Moore’s Law?

Power consumption has always been a major concern for phone manufacturers. OPPO even directly advertises “5 minutes of charging, 2 hours of talk time.” Power optimization is also a topic covered every year at Google I/O. So, what efforts has the Android system made in power efficiency? How can we measure an app’s power consumption?

18 Power Consumption Optimization Part1 Analyzing Power Consumption through Progression of Power Optimization #

Let’s review the previous content of the column. I have already talked about memory, CPU, storage, and network. LPDDR5 memory, 7nm CPU, UFS 3.0 flash storage, and 5G chips. Hardware has been evolving rapidly with the goal of “faster and smaller”.

However, there is one important component on smartphones that has not had a revolutionary breakthrough for many years and is the most criticized – the battery. The development of smartphones, just like the barrel principle, ultimately hinges on the battery.

What are the important criteria for battery technology? What progress has been made in battery technology in recent years? Now let’s talk about the knowledge of smartphone batteries together.

1. Battery Technology

Let’s first look at the performance of the latest flagship phones from two giants, Apple and Huawei. Apple’s iPhone XS Max has a built-in lithium-ion rechargeable battery with a capacity of 3174mAh, which can charge up to 50% in 30 minutes.

Huawei Mate 20 Pro has upgraded to a highly-capacity lithium-ion battery of 4200mAh and is equipped with Huawei’s 40W SuperCharge technology for the first time. It can charge up to 70% in 30 minutes. It also has 15W high-power wireless fast charging and reverse wireless charging “black technology”. Mate 20 X even upgraded the battery capacity to 5000mAh and creatively applied graphene technology to smartphones.

From the introduction of the batteries in these two flagship phones, we can discover some key indicators of smartphone batteries.

  • Battery capacity. A larger battery capacity means longer battery life. We can achieve this by increasing the battery’s volume or density. In the case of the Huawei Mate 20, the battery occupies 48% of all internal components, and the battery capacity limits the phone’s progress towards being lighter and thinner.

  • Charging time. If it is not easy to break through the battery capacity, the only option is to consider how to fully charge the battery in a shorter amount of time. This requires fast charging technology. OPPO’s “5 minutes of charging, 2 hours of talk time” refers to VOOC flash charging technology. Fast charging technology basically involves increasing the current or voltage, and currently there are mainly two solutions: one is high voltage and low current fast charging, and the other is low voltage and high current fast charging. For an overview of fast charging technologies, you can refer to this article.

  • Lifespan. Battery lifespan is generally measured by charging cycles. One charging cycle means fully charging the battery, but it does not require completion in one go. For example, if the battery was previously charged to 25% and then charged for another 75%, the combination of these two charges would count as one charging cycle. Last year, Apple faced multiple lawsuits due to the “batterygate” scandal, which involved slowing down processors to solve the problem of insufficient battery life. According to official data from Apple, after 500 charging cycles, the remaining capacity of an iPhone battery is 80% of its original capacity.

  • Safety. As smartphones are items that users carry with them at all times, safety is the primary concern. Especially since the Samsung Note 7 explosion incident, major smartphone manufacturers have become more conservative in terms of battery capacity. Therefore, whether it is battery density or fast charging technology, safety is our top priority.

From the historically well-established nickel-chromium and nickel-hydrogen batteries, to the widely used lithium-ion batteries, and even to the graphene battery which is considered a revolutionary technology, although it doesn’t satisfy Moore’s Law, battery technology is constantly evolving. Interested students can refer to this article on the progress of smartphone battery technology. In fact, the Mate 20 X only uses graphene technology for its cooling system and not for the battery itself. According to the latest research, using graphene materials can increase battery capacity by 45%, accelerate charging speed by 5 times, and extend the cycle life to around 3500 cycles. In the future, it may be possible to fully charge our phone battery in just 12 minutes, which would be a significant milestone in the history of battery development.

2. Battery Capacity and Hardware

A feature phone with a 1000mAh battery can last for several days, so why do we need to charge a smartphone with a 5000mAh battery every day? This is because our smartphones today require video calling and popular games like “王者” (King of Glory) or “吃鸡” (PUBG), and the types and performance of the hardware devices have vastly improved.

However, these applications and games do not directly consume battery power. Instead, they consume power through the use of hardware modules. The image below shows some of the power-consuming hardware modules in a phone.

The CPU, screen, WiFi, data network, GPS, and audio-video calling are the major power consumers in our daily use. To be honest, the rapid advancement of smartphone hardware is mostly just marketing gimmicks by manufacturers. Most hardware components are already in a state of excess performance for us, but this excess performance also consumes more power.

Therefore, resource scheduling is the most important means of power optimization for manufacturers. For example, as I mentioned in the optimization for lag, CPU chips are designed with big and small core architectures and will flexibly allocate computational resources for different tasks. Modules like the mobile baseband and GPS will enter low-power or sleep modes when not in use to reduce power consumption.

Nowadays, more and more manufacturers are using local AI powered by deep learning to optimize resource scheduling, ensuring efficient allocation of resources such as the GPU and running memory to achieve comprehensive power reduction. Manufacturers need to find a balance between high performance and battery life. Some manufacturers may lean towards providing better performance, while others may prioritize longer battery life.

Power consumption is indeed very important. When I worked on pre-installed apps for phones, I found that manufacturers have very strict regulations on power consumption, which deepened my understanding of power consumption. However, to ensure a better user experience for popular applications, manufacturers are willing to allocate more resources to them. This is why Qualcomm has introduced CPU Boost, WeChat has hardcoded optimizations, and there are various cooperation channels between manufacturers and other apps.

But conversely, why do manufacturers only include WeChat and QQ in the background whitelist, but not other popular apps like Taobao, Alipay, or Douyin? I speculate that power consumption may be an important factor in this decision.

3. Battery Capacity and Applications

Each hardware module consumes power, and the power consumption varies for different hardware. So how do we evaluate the power consumption of different applications?

According to the principles of physics, the formula for electrical energy is:

Electrical Energy = Voltage * Current * Time

For mobile phones, the voltage generally does not change. For example, the constant voltage of the Huawei Mate 20 is 3.82V. So, assuming constant voltage, only measuring the current and time is needed to determine power consumption.

The power consumption of different modules can be calculated using the following formula:

Module Power (mAh) = Module Current (mA) * Module Time (h)

Module time is relatively easy to understand, but how do we obtain the module current? Android system requires different manufacturers to provide power configuration files for components in /frameworks/base/core/res/res/xml/power_profile.xml.

The power_profile.xml file defines the current consumption values for different modules and the approximate power consumption of the module over a certain period of time. You can also refer to the Android Developer documentation on Power Profile. Of course, the current value is also related to the state of the module, for example, the current of the screen at different brightness levels will definitely be different.

The Android system’s power calculation PowerProfile also reads values from power_profile.xml. The specific values differ for different manufacturers. We can obtain them using the following method:

  • Export the /system/framework/framework-res.apk file from the phone.
  • Use a decompiling tool (such as apktool) to decompile the exported framework-res.apk file.
  • Locate the power_profile.xml file at /res/xml/power_profile.xml in the decompiled framework-res directory.

For the power consumption of the system, we can export it using dumpsys batterystats.

adb shell dumpsys batterystats > battery.txt
// Total power consumption of each UID, estimated roughly.
Estimated power use (mAh):
    Capacity: 3450, Computed drain: 501, actual drain: 552-587
    ...
    Idle: 41.8
    Uid 0: 135 ( cpu=103 wake=31.5 wifi=0.346 )
    Uid u0a208: 17.8 ( cpu=17.7 wake=0.00460 wifi=0.0901 )
    Uid u0a65: 17.5 ( cpu=12.7 wake=4.11 wifi=0.436 gps=0.309 )
    ...
    
// Reset battery statistics.
adb shell dumpsys batterystats --reset

BatteryStatsService is the external power statistics service, but the specific statistics work is done by BatteryStatsImpl. BatteryStatsImpl uses PowerProfile internally. BatteryStatsImpl creates a UID instance for each application to monitor its use of system resources. The monitored system resources include the contents shown in the diagram below.

The use of power is also related to the environment. For example, in winter when the temperature is below zero, power consumption will be faster. The power measurement methods provided by the system only provide reference values. However, by using the above methods, we can successfully convert power measurement into module usage time or frequency.

Accurate measurement of power consumption is not that easy. In the article Dianping App’s Short Video Power Consumption Optimization Case Study, several power consumption testing methods are summarized below.

When testing or when someone reports power consumption issues, the best way to investigate is to use bug report combined with Battery Historian.

// For Android 7.0 and later
$ adb bugreport bugreport.zip
// For Android 6.0 and earlier:
$ adb bugreport > bugreport.txt
// Analyze the result using Battery Historian
python historian.py -a bugreport.txt > battery.html

The Evolution of Battery Drain in Android #

Although the iPhone XS Max has a battery capacity of only 3174mAh, which is much lower than most flagship Android phones, we often find that its battery life is better than that of most Android phones.

Upon closer inspection, we realize that Android is based on the Linux kernel, which is mainly used on servers and does not have strict power consumption optimization. Moreover, there are various “battery-saving hacks” in China, and many apps running in the background act as “power-hungry parasites”.

So what efforts has Android made to optimize battery usage? Every year at Google I/O, they specifically discuss battery optimization. Let’s take a look at the changes Android has made in terms of power consumption.

1. Wild Growth: Pre-Android 5.0

Before Android 5.0, the system was not as refined and had relatively few battery optimization features. There were no strict restrictions on background processes, resulting in the prevalence of multi-processes, forking native processes, and broadcast launches.

Users experienced rapid battery drain, and the following issues were noticeable:

  • Battery consumption is related to the number of installed applications. The more apps users install, regardless of whether they are opened or not, the faster the battery drains.

  • App battery consumption is unrelated to app usage time. Users expect app battery consumption to be proportional to its usage time, but some apps continue to consume a significant amount of battery even if they are rarely used.

  • Troubleshooting battery issues is complex. Both measuring battery usage and troubleshooting power consumption problems are extremely difficult.

Of course, before Android 5.0, attempts were made to optimize power consumption as well.

2. Gradual Restriction: Android 5.0 to Android 8.0

With Android 5.0, the Project Volta was launched to improve battery life. At the same time, tools like “dumpsys batteryst” were introduced to generate statistics on device battery usage.

Starting from Android 6.0, Google began cleaning up background apps and broadcasts to further optimize power consumption. However, several issues remained during this period:

  • Power saving mode was not effective enough. The Doze low power mode was not strictly enforced, permitting activities such as retrieving location or network permissions for background apps even when the screen was off.

  • Lack of user control over apps. Users lacked simple control over the battery consumption and background behavior of certain apps, though many domestic manufacturers had already implemented this feature.

  • Developers were not proactive in responding to Target API updates. To avoid some restrictions introduced in new versions, many domestic apps insisted on not upgrading their Target API to Oreo or later, rendering many power-saving features ineffective.

3. Strictest Limitation: Android 9.0

When Android 9.0 was released, I used my phone normally for a day and received a surprising notification in the status bar: WeChat is seriously draining battery in the background.

Despite several versions of optimization, the battery life issue in Android still hasn’t fundamentally improved. However, starting from Android 9.0, Google introduced stricter power management features.

With the app standby bucket feature, we can ensure that app battery consumption is proportional to their usage time, rather than the number of installed apps on the phone. Less frequently used apps are less likely to drain the battery excessively. With power-saving mode and app background restrictions, users can identify power-hungry apps and impose stricter limitations on them.

On the other hand, both Google Play and the Chinese Android Green Alliance require apps to update to the latest Target API within a year. Battery life has always been the lifeline of Android, and I believe this year’s Android Q will introduce more optimization measures.

Summary #

Today I talked about the relationship between applications, the Android system, phone hardware, and battery, and reviewed the evolution of Android battery optimization. But when it comes to specific work, how should we go about optimizing battery usage? We will address this question in the next installment.

When discussing knowledge about memory, CPU, storage, and network, I will also talk about some hardware-related knowledge. The main purpose is to help you establish a comprehensive understanding from the application layer to the operating system, and then to the hardware. When you have a complete knowledge map in your mind, you can better handle and solve some difficult problems, and then do a good job in corresponding performance optimization.

Homework #

Today’s homework is about the power consumption issues you have encountered in your everyday development process. When faced with these issues, what methods do you usually use to locate and fix them? Please leave a comment to discuss with me and other classmates.

Feel free to click on “Share with Friends” to share today’s content with your friends and invite them to learn together. Lastly, don’t forget to submit your homework in the comments section. I have also prepared a generous “Study Boost Package” for diligent students. Looking forward to improving together with you!