Lock the CPU frequency of Android devices

Lock the CPU frequency of Android devices

[[184787]]

This article introduces the method of checking the status of the Android device CPU and the method of locking the frequency in detail. What is the use of this? As a test engineer, you deserve to know it.

CPU frequency

First, let's talk about the CPU frequency. We all know that the higher the CPU operating frequency, the faster the calculation, but the higher the energy consumption. However, in many cases, the device does not need such high computing performance. At this time, we hope to reduce the CPU operating frequency and pursue lower energy consumption to achieve longer standby time.

Based on this demand, the CPU of current electronic devices has multiple operating frequencies and can automatically switch the CPU frequency according to the actual scenario to achieve the goal of balancing computing performance and energy consumption.

The purpose of frequency locking

So why do we need frequency locking?

For ordinary users, these scenarios may be familiar:

When playing games on a laptop at home, the computer is connected to the power supply, and you don't care about energy consumption. You just want the highest possible performance. At this time, you can choose the high-performance mode, that is, keep the CPU working at the maximum frequency.

When traveling with a laptop computer powered by a battery, you hope that the computer can be on standby for as long as possible. In this case, you can choose the power saving mode, which means that the CPU keeps running at the highest frequency.

As a test engineer, when we conduct software testing, in order to make the test results truly reflect the efficiency of the software itself, from the perspective of the controlled variable method, we hope that the test results will be as unaffected by the hardware itself as possible. At this time, we can try to lock the CPU frequency of the device, that is, to ensure that the CPU of the hardware device runs at a constant frequency during the test.

Here is a foreshadowing. In the official chromium test library, some test scenarios will adjust the frequency of all CPUs of the device to the maximum state when initializing the test environment. I will analyze the source code of that part in a separate blog later. For those who can't wait, you can go and look at the source code first. The source code path is pylib/perf/PerfControl.SetHighPerfMode.

Check CPU status information

Before modifying the CPU status, we need to check the CPU properties and status information first so that we can make correct settings in a targeted manner.

When it comes to the CPU status, we usually focus on two types of information: one is at the overall level, that is, the number of CPU cores running; the other is at the detailed level, that is, the working status of each CPU, including the working mode, frequency, etc.

In the Android system, CPU-related information is stored in files in the /sys/devices/system/cpu directory. We can obtain the CPU status information of the current device by reading specific files in this directory, or we can change the CPU frequency and other status information by writing values ​​to specific files in this directory.

This article uses Nexus 5 (system version 5.1.1) as an example, and all subsequent examples will use this device as an example. Please be aware that there may be some differences between different models and Android system versions.

In the /sys/devices/system/cpu directory, the file structure is as follows.

  1. shell@hammerhead:/sys/devices/system/cpu $ ll
  2.  
  3. drwxr-xr-x root root 2016-01-20 01:36 cpu0
  4.  
  5. drwxr-xr-x root root 2016-01-20 21:06 cpu1
  6.  
  7. drwxr-xr-x root root 2016-01-20 21:07 cpu2
  8.  
  9. drwxr-xr-x root root 2016-01-20 21:07 cpu3
  10.  
  11. -rw ------- root root 4096 1970-01-17 10:27 cpuctl  
  12.  
  13. drwxr-xr-x root root 1970-01-17 10:27 cpufreq
  14.  
  15. drwxr-xr-x root root 1970-01-17 10:27 cpuidle
  16.  
  17. -r --r--r-- root root 4096 1970-01-17 10:27 kernel_max  
  18.  
  19. -r --r--r-- root root 4096 1970-01-17 10:27 offline  
  20.  
  21. -r --r--r-- root root 4096 1970-01-17 10:27 online  
  22.  
  23. -r --r--r-- root root 4096 1970-01-17 10:27 possible  
  24.  
  25. drwxr-xr-x root root 1970-01-17 10:27 power
  26.  
  27. -r --r--r-- root root 4096 1970-01-17 10:27 present  
  28.  
  29. -rw-r --r-- root root 4096 1970-01-17 10:27 uevent  

1. View overall cpu info

The possible file stores the available CPUs of the current device in digital form. For example, 0-3 means that the current device has 4 cores, numbered 0, 1, 2, and 3.

  1. shell@hammerhead:/sys/devices/system/cpu $ cat possible
  2.  
  3. 0-3

The online file stores the CPUs currently running on the device. Because sometimes the device does not need very high performance, some CPUs can be turned off. However, it should be noted that CPU0 is always in operation at any time. The storage format of the online file is similar to possible. If only some CPUs are running and the CPU numbers are not continuous, they will be separated by commas; for example, 0,2 means that CPU0 and CPU2 are currently in operation.

  1. shell@hammerhead:/sys/devices/system/cpu $ cat online
  2.  
  3. 0,2

Correspondingly, the offline file indicates the CPU of the current device that is in the off state. This is complementary to the online file, and the union is just all the CPUs of the device, that is, the content in the possible file.

  1. shell@hammerhead:/sys/devices/system/cpu $ cat offline
  2.  
  3. 1,3

2. view specified cpu info

Next, if we want to obtain the information of a specific CPU, we need to enter the corresponding folder. For example, cpu0/ corresponds to the information of CPU0.

In the /sys/devices/system/cpu/cpu0 directory, the file structure is as follows.

  1. shell@hammerhead:/sys/devices/system/cpu $ ll cpu0
  2.  
  3. drwxr-xr-x root root 2016-01-20 01:37 cpufreq
  4.  
  5. drwxr-xr-x root root 1970-01-17 10:27 cpuidle
  6.  
  7. -r -------- root root 4096 1970-01-17 10:27 crash_notes  
  8.  
  9. -rw-r --r-- root root 4096 2016-01-20 01:36 online  
  10.  
  11. drwxr-xr-x root root 1970-01-17 10:27 power
  12.  
  13. drwxr-xr-x root root 1970-01-17 10:27 rq-stats
  14.  
  15. lrwxrwxrwx root root 1970-01-17 10:27 subsystem
  16.  
  17. drwxr-xr-x root root 1970-01-17 10:27 topology
  18.  
  19. -rw-r --r-- root root 4096 1970-01-17 10:27 uevent  

Among them, the content of the online file indicates whether the current CPU is in running state. If it is in running state, the content is 1, otherwise it is 0; this corresponds to the /sys/devices/system/cpu/online mentioned above.

  1. shell@hammerhead:/sys/devices/system/cpu $ cat cpu0/online
  2.  
  3. 1

In the cpu0/cpufreq/ directory, information related to the frequency of CPU0 is stored. The file structure is as follows.

  1. shell@hammerhead:/sys/devices/system/cpu $ ll cpu0/cpufreq/
  2.  
  3. -rw-r --r-- root root 4096 2016-01-20 01:57 UV_mV_table  
  4.  
  5. -r --r--r-- root root 4096 2016-01-20 01:57 affected_cpus  
  6.  
  7. -r --r--r-- root root 4096 2016-01-20 01:57 cpu_utilization  
  8.  
  9. -r -------- root root 4096 2016-01-20 01:57 cpuinfo_cur_freq  
  10.  
  11. -r --r--r-- root root 4096 2016-01-20 02:00 cpuinfo_max_freq  
  12.  
  13. -r --r--r-- root root 4096 2016-01-20 01:39 cpuinfo_min_freq  
  14.  
  15. -r --r--r-- root root 4096 2016-01-20 01:57 cpuinfo_transition_latency  
  16.  
  17. -r --r--r-- root root 4096 2016-01-20 01:57 related_cpus  
  18.  
  19. -r --r--r-- root root 4096 2016-01-20 01:39 scaling_available_frequencies  
  20.  
  21. -r --r--r-- root root 4096 2016-01-20 01:57 scaling_available_governors  
  22.  
  23. -r --r--r-- root root 4096 2016-01-20 01:50 scaling_cur_freq  
  24.  
  25. -r --r--r-- root root 4096 2016-01-20 01:57 scaling_driver  
  26.  
  27. -rw-r --r-- root root 4096 2016-01-20 01:50 scaling_governor  
  28.  
  29. -rw-r --r-- root root 4096 2016-01-20 08:29 scaling_max_freq  
  30.  
  31. -rw-r --r-- root root 4096 2016-01-20 08:29 scaling_min_freq  
  32.  
  33. -rw-r --r-- root root 4096 2016-01-20 02:52 scaling_setspeed  

In this directory, there are many files that we need to pay attention to.

First, there are scaling_available_governors and scaling_governor. The governor here can be understood as the working mode of the CPU. scaling_available_governors stores all the working modes supported by the current CPU, while scaling_governor stores the current working mode of the CPU.

  1. shell@hammerhead:/sys/devices/system/cpu $ cat cpu0/cpufreq/scaling_available_governors
  2.  
  3. impulse dancedance smartmax interactive conservative ondemand userspace powersave Lionheart bioshock performance
  4.  
  5.   
  6.  
  7. shell@hammerhead:/sys/devices/system/cpu $ cat cpu0/cpufreq/scaling_governor
  8.  
  9. performance

As you can see, Nexus 5 supports many working modes. Here we will only briefly explain several common modes.

performance: High performance mode, the CPU runs at high frequency even when the system load is very low.

Powersave: power saving mode, contrary to performance mode, the CPU always runs at maximum frequency.

ondemand: The CPU frequency changes according to the system load.

Userspace: It can be simply understood as a custom mode, in which the frequency can be set.

The meanings and strategies corresponding to various modes will not be elaborated here. Those who are interested can search on their own.

Then comes the CPU's operating frequency range, the corresponding files are cpuinfo_max_freq, cpuinfo_min_freq, scaling_max_freq, scaling_min_freq.

The prefix cpuinfo_ indicates the frequency range supported by the CPU hardware, which reflects the characteristics of the CPU itself and has nothing to do with the CPU's operating mode. The prefix scaling_ indicates the frequency range of the CPU in the current operating mode.

So, what is the current CPU working frequency, how can we check it?

Just check cpuinfo_cur_freq or scaling_cur_freq. cpuinfo_cur_freq represents the frequency value actually read by the hardware, while scaling_cur_freq is the current setting value of the software. In most cases, these two values ​​are consistent, but there may be slight differences due to hardware reasons.

  1. root@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq # cat cpuinfo_cur_freq
  2.  
  3. 1574400
  4.  
  5. root@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_cur_freq
  6.  
  7. 1574400

Change CPU status information

***Back to the topic of this article, how to set the CPU frequency?

This also corresponds to the CPU information viewing, which is divided into the setting of the overall CPU operation status and the setting of the specific CPU working mode.

There are two points that need special explanation here.

First of all, for Qualcomm CPU, there is a system service called mpdecision service. When this system service is running, we cannot change the CPU status information. Therefore, if we want to change the working mode of Qualcomm CPU, the first step is to terminate the mpdecision system service.

The operation is also very simple. Just execute the following command in the Android shell.

  1. stop mpdecision

The second point to note is that if we want to set the working status of a specific CPU, we must set scaling_governor to userspace. Only in this way can we set scaling_setspeed.

  1. root@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_setspeed
  2.  
  3. root@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq # echo userspace > scaling_governor
  4.  
  5. root@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_setspeed
  6.  
  7. 1574400

1. set overall cpu info

From a macro perspective, we can set the number of CPU cores to turn a specific CPU on and off. Of course, as we have said before, CPU0 is always running, so we cannot turn it off.

The setting method is very simple. Just write a value to the /sys/devices/system/cpu/cpu[i]/online file. When writing 1, the specified CPU is turned on, and when writing 0, the specified CPU is turned off.

  1. # turn off cpu1
  2.  
  3. root@hammerhead:/sys/devices/system/cpu/cpu1 # echo 0 > online
  4.  
  5. root@hammerhead:/sys/devices/system/cpu/cpu1 # cat online
  6.  
  7. 0

2. set specified cpu info

Before setting the frequency of a specific CPU, we need to know that the CPU cannot operate at any frequency. We can only set the CPU frequency to a value it supports.

By looking at scaling_available_frequencies, we can get the frequency values ​​supported by the current CPU.

  1. root@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq # cat scaling_available_frequencies
  2.  
  3. 300000 422400 652800 729600 883200 960000 1036800 1190400 1267200 1497600

Next, we can set the CPU operating frequency.

How do I set it? At first, I thought that writing a specific frequency value into scaling_setspeed or scaling_cur_freq would be enough, and this is also the method I found through Google search.

But after trying, I found that it is not feasible. Why is this the case? I haven't found the answer yet, and I hope friends who know the reason can tell me.

***After trying, I found that by setting scaling_max_freq and scaling_min_freq to the target frequency value at the same time, the CPU frequency can be successfully set. # before setting

  1.  
  2. shell@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq $ cat scaling_cur_freq
  3.  
  4. 1574400
  5.  
  6. # setting
  7.  
  8. shell@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq $ echo 1728000 > scaling_min_freq
  9.  
  10. shell@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq $ echo 1728000 > scaling_max_freq
  11.  
  12. # after setting
  13.  
  14. shell@hammerhead:/sys/devices/system/cpu/cpu0/cpufreq $ cat scaling_cur_freq
  15.  
  16. 1728000

<<:  VMplay Ai Qiwei: The era of free app downloads is coming

>>:  10 Practical Tips for iOS (There are always things you don’t know and things you will use)

Recommend

High! Really high!

What does a 70-story high bridge pier look like? ...

4 SEM promotion time strategies can increase your conversion rate by 5 times!

When it comes to bidding time periods, many compa...

Apple Pay reveals new issue: Cannot add bank cards after system restore

On January 2, although restoring the factory sett...

Baidu search promotion main process function upgrade

1. Background The main process of Baidu's sea...

How much does it cost to develop a baking utensils mini program in Zhangbei?

How much is the quotation for the development of ...

7 tips to quickly improve UI visual experience

Maybe you are a novice designer, or maybe you are...