- GameBench Staff
- 17. December 2019
If you have ever wanted to measure the CPU Usage of a mobile app or game and you are stuck, then read on to find out how this can be achieved.
The fundamental goal of any app or game is to provide a great experience to the user. For games, frames per second is a good measure of this. When your game is not able to provide a good experience you are naturally curious about resource usage in your system. It could likely be that the game is bottlenecked by either the CPU/ GPU or memory bandwidth. These three fundamental areas are the first stop when looking to identify performance issues. Let’s look at how to measure CPU Usage in both Android and iOS devices.
What is CPU Load?
Before we can discuss methods of measuring the CPU Usage it’s important to know what we mean by the definition CPU Load. CPU load measures the amount of work done by a CPU between two sampling intervals. Modern devices have multi-core CPUs that can operate at different frequencies depending on the computational need of the software using the CPU. As a result, a CPU that’s fully loaded at a lower frequency can still accept more jobs from the operating system if the frequency is increased.
A key observation to make is the fact that CPU Load depends on the operating point of the CPU. A CPU that’s 100% loaded at 500 Mhz will only be 50% loaded if the frequency is increased to 1GHz. So there is a need to normalize the CPU Load to the maximum operating frequency of the CPU.
CPU Load can be obtained both for Android and iOS in a variety of ways. CPU Load can be more than 100%. For example in an iOS device with 4 cores, the CPU Load can go up to 400% (100% Load for each CPU core).
What is Normalized CPU Usage?
When the CPU Load metric as mentioned above is re-calculated using the Maximum operating point of each CPU Core we get normalized CPU Usage. I know this is a bit complex, so I have an example that explains this below,
Let’s consider the following scenario on a device that has a Quad-Core CPU
- CPU Load because of the app - 50%
- Number of cores that are online - 2 out of 4
- Maximum Operating point of the CPU Cores - 1 GHz
- Operating points of the two cores that were on - 500 MHz
The normalized CPU Usage is calculated as follows,
- Maximum Available CPU Cycles - 4 * 1 GHz - 4 GHz
- Available CPU Cycles when the app was running - 500 Mhz * 2 = 1 GHz
- Percentage of Total CPU Cycles Available during sampling - 1Ghz/4Ghz = 25% or 0.25
- Normalized CPU Usage - 50% * (0.25) = 12.5%
When you use any profiling tool, it is always useful to ask this question. Am I measuring Normalized CPU Usage? It’s important because when CPU Usage is normalized, it can be compared across two devices. CPU Load doesn’t lend itself to easy comparisons.
Now that we have understood the definitions of CPU Load, let’s look at some of the different ways in which we can measure this in Android and iOS
As a developer, there are many ways by which you can measure CPU Usage in Android for your app or game.
The easiest way to quickly get a handle on your apps CPU Usage is to use the “top” command. Android is based on Linux, it comes with some of the command line goodies also available in Linux.
If you have ADB access to the device, you could run a command like the following to look at CPU Usage,
The command ADB shell top -m 5 -d 1 produces a result like below:
Above, you can see that the package com.kiloo.subwa+ consumed 142% of CPU. It is worth noting again that this is CPU Load and is not normalized. Nevertheless, you can still use this number on the same device to get an understanding of CPU Usage. This method is also useful to get CPU usage on any app running on the device.
If you have access to the source code of the App, you can use the Android Studio profiler to get detailed CPU Usage metrics. You can follow the official android documentation here to use the CPU Profiler in Android Studio.
GameBench tools provide the easiest possible way to retrieve Normalised CPU Usage on an Android device for any app or game even if you are not the developer. In addition to the CPU Usage, the tools also report CPU Core States (Whether a CPU Core was online) and also their frequencies. For instance, an example chart for CPU Core Frequencies looks like below,
The above chart shows a mobile chipset with an 8 Core CPU and with a glance you can see that the CPU Frequency was clocked up for the first 25 seconds or so and then the Android OS decided to clock all the cores to lower frequencies. In this case, the profiled app was loading resources from disk and as a result, had to operate all the cores at a higher frequency for 25 seconds. If you would like to try out CPU Usage for your mobile device, sign up for a GameBench Pro trial below and have a quick go.
If you are an iOS developer, the easiest way to measure CPU Usage is by using the Xcode toolset.
We have found the following Apple tools to be particularly useful:
- Xcode Debug Gauges
- Time Profiler
- Activity Monitor
Xcode Debug Gauges
If you have access to the source code of your iOS app, then the Debug Gauges are a quick and easy way to get resource consumption of your app. Please find out more details on using the Debug Gauges here. All you need to do is launch the app on the connected device to get very useful information on resource usages. Please note that only CPU Load is reported in iOS (Not normalized).
Sometimes, you need access to more detailed CPU Profiling information and this is where Instruments can help. Instruments have a couple of different instruments that can help with measuring the performance of your app.
The time profiler instrument can perform low-overhead, time-based sampling of processes running on the system’s CPUs. There are a couple of different views in this instrument that can be used. The CPU strategy usage view can be used to compare core usage over given time periods. You could view the workload on each core of the device and spot any unexpected behavior. The threads strategy view can be used to examine your app’s use of threads when performing work. It is worth noting that source code is needed to understand the bottlenecks.
The activity monitor can provide a good health indicator of the app being profiled. This instrument can show CPU and Memory usage of all apps running on the device. This is useful if you would like to have a long-running test and measure performance parameters for the app/game.
Our tools allow you to measure the CPU Usage for your app even if you do not have access to the source code. This is especially useful if you would like to profile a release candidate without any debug information. Also, our tools allow you to measure CPU Usage from a windows PC which can be very useful as the above-mentioned ways of using Xcode and Instruments only work on a Mac that runs OSX.
One important point to note here is that the CPU Usage reported by GameBench varies based on the number of cores. Each core is allowed to use 100% and so for a six-core system, the maximum CPU Load can get to 600%.
What should you look for?
Sustained values of high CPU Load need to be looked at more carefully (Understand what was happening on screen using the screenshots for context) as this will most likely lead to increased battery drain. Because of the way modern CPU’s work, an increase in CPU load will usually trigger the CPU to go into the next higher operating frequency. The transition to the next operating point should result in a lower CPU load value. If an application is consistently loading the CPU, the OS continues to increase the operating point until it reaches the maximum value. This is not desirable behavior and should be investigated.
You should also look at CPU Usage values that are more than 25% of the maximum allowed. So in a 6 core system, any sustained CPU Load more than 150% usually points to an issue and you might want to use Time Profiler instrument to investigate this in more detail.
Short bursts of high CPU Load are fine.
This depends on the number of cores in the device used for testing. On a quad-core system, a value greater than 25% would mean that one core has been fully utilized for the application and as a result would be considered high CPU Usage although this is fine in short bursts. Consistent CPU Usage > 25% needs further investigation.
All the threads used in the app should be “named” to use the information provided in the best possible way. It’s always a good idea to check if any of the “light” threads are unexpectedly consuming more CPU Cycles.
If you are interested in learning more about the metrics gathered by the GameBench Pro tools, you can find several blog posts on the subject below.