Greener Software Engineering: Why, What, and How?

This article was written by Hina Anwar, Lecturer of Software Engineering at the University of Tartu.

To physically measure software energy consumption, we can measure the power consumption of the hardware over time while it executes the software task. However, measuring the energy consumption of software is not trivial and involves a steep learning curve. This article helps software practitioners understand the Why, What, and How of the software energy measurement process, particularly mobile app development. We explain a typical software energy measurement process for mobile apps.

In the current era, software systems are integrated into our lives due to the wide usage of portable devices such as smartphones. Every year we see more and more software products and services focusing on mobile platforms, and as a result, the data created on the internet every minute is huge. Every activity on our mobile devices comes with a small energy cost and contributes to our CO2 footprint. Billions of people use mobile devices worldwide, and a small improvement in energy consumption of these devices could have a large impact collectively. Mobile apps that do not drain the battery get good user ratings usually. Developing greener software is essential for achieving the global carbon neutrality targets.

Software practitioners consider many non-functional aspects and related trade-offs to access software quality. It is important to create awareness among software practitioners that the energy consumption of software should be considered while making quality trade-offs. In contrast to other quality aspects such as execution time and performance, gathering energy data is not trivial and involves a steep learning curve.  

To physically measure software energy consumption, we can measure the power consumption of the hardware over time while it executes the software task. This measurement means that fundamentally we can measure the cost of energy consumption of software because we can measure the cost of electricity production and its environmental impacts. Often, developers use the reduction in execution time as a proxy for reducing energy consumption, which is not always true.

To improve app code for energy efficiency, developers could apply refactoring alternatives that are energy efficient. Existing refactoring guidelines cannot be generalized w.r.t energy efficiency, as there is not enough energy-related data for every context. In addition, the existing guidelines and tools mostly address the energy issues a posteriori, i.e., once they have already been introduced. We need more contextual guidelines, accurate data, tutorials, and global solutions to educate and support software practitioners toward greener software engineering.  

In the Software Engineering & Information Systems research group at the University of Tartu, we are researching to add data to the existing body of knowledge by evaluating alternative coding patterns and building support tools for app developers that recommend energy-efficient alternatives during coding. We have developed two prototypical tools: ARENA (Analyzing eneRgy Efficiency in aNdroid Apps) and REHAB  (Recommending Energy-efficient tHird-pArty liBraries) to help developers and researchers improve the energy efficiency of their apps. Both tools are open-source and developed as a plugin for Android Studio and IntelliJ IDEs.

Let us consider a simple scenario: We want to implement a particular functionality in a mobile app. We must choose between two alternative third-party libraries. We want to know if the app version with library A is more energy-efficient than the app version using library B.

Third-party libraries are reusable components available to implement various app functionalities and speed up development. Almost 60% of app code is the result of using third-party libraries. Choosing the right library is a time-consuming task. Many alternative libraries exist for implementing a particular functionality, and developers consider many factors such as performance, documentation, and community support. However, the energy consumption of third-party libraries is usually not in the mix.

Imagine doing all that hard work and ending up with an app that drains the battery. Maybe the included third-party library reduces the execution time via increased performance but uses the CPU in a high-power state, thus causing an increase in energy consumption.

Ideally, some support tool should be available within the IDEs to help developers make such informed choices. For this purpose, we have developed a prototypical recommender tool, REHAB, which makes it easier for developers to choose between alternative energy-efficient third-party network libraries.

At the moment, the scope of this tool is limited because the recommendations are dependent on the availability of reliable energy data. There are millions of unique libraries available in the Maven repository. Such a huge supply of third-party libraries is linked to the demands and needs of developers. Unfortunately, there is no easy way to measure and compare the energy consumption of these libraries quickly. The lack of reliable energy data leads to a lack of support tools for developers.

Usually, software practitioners are not aware of how to measure the energy consumption of apps and gather reliable energy data. Many factors could affect an energy measurement, and different approaches are available for recording energy data.

There is no one size fits all.

Software-based approaches for energy measurement are easier to implement and are based on the estimation of a set of features such as system calls, the number of packets transferred over the network, CPU cycles, etc. Software-based approaches are not very accurate. On the other hand, hardware-based approaches are difficult to set up and depend on a physical measurement device.   However, hardware-based approaches are more accurate than software-based approaches once the one-time setup is done. In this article, we discuss the hardware-based approach.  

A typical software energy measurement process involves :

  1. Setting up a measurement environment.

  2. Executing the app under test on a mobile device.

  3. Recording energy data via a hardware-based approach.

  4. Cleaning and aggregating data for analyses, reports, and visualizations.

We prepare app version(s) with test cases that could mimic user interaction in a given use case(s). Next, we execute the test scenario several times on the mobile phone, and energy measurements are captured via a physical measurement device such as a Monsoon power monitor. Then, energy data is collected, filtered, and aggregated. The captured energy data and related ADB logs are filtered by matching timestamps to identify the start and end of the test scenario. In the end, statistical analysis is performed on filtered data to identify significant changes in energy consumption.

Figure 1: Typical hardware-based energy measurement process

Here is a checklist to help software practitioners avoid common mistakes while taking energy measurements.

  • Close all unnecessary services and applications on the mobile device.

  • Disable notifications.

  • Disable Wi-Fi and mobile data (if not needed).

  • Keep the screen's brightness to a minimum and consistent throughout the measurements.

  • Warm up the device to avoid errors due to temperature changes in hardware.

  • Try to keep the room temperature consistent for all measurements.

  • Give short pauses between measurements to avoid errors due to residual energy consumption.

  • Repeat measurements several times to avoid bias and account for underlying variation due to background activities.

  • To make reliable, unbiased, and consistent measurements repeat measurements several times.

  • If executing several scenarios, execute them in random order to avoid unforeseen errors due to the order of executions.

We developed the support tool ARENA to help automate the typical energy measurement process and make it easier for researchers and developers to gather reliable energy data. ARENA is an open-source tool built for integration with IntelliJ IDEA and Android Studio IDE as a plugin.

Figure 2: An overview of the energy measurement and analysis process supported by ARENA.

A video demonstrating the usage of ARENA is available here.   

The energy data gathered via support tools such as ARENA is reliable and could be used to develop and train software-based energy measurement models. The more energy data we have, the more contextual guidelines and support tools can be built to help develop energy-efficient apps.

Further Reading

  1. Towards greener software engineering using software analytics   

  2. Should energy consumption influence the choice of Android third-party HTTP libraries?

Hina Anwar will be taking the stage at Digit 2022 on April 22 to talk about Greener Software Engineering - Process and Challenges.