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. 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.
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.
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.
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.
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.
In the cpu0/cpufreq/ directory, information related to the frequency of CPU0 is stored. The file structure is as follows.
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.
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.
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.
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. 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.
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.
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
|
<<: 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)
On March 2, Apple introduced a peer group benchma...
Brand building is a long process. Although it is ...
What does a 70-story high bridge pier look like? ...
According to the current epidemic situation, Wuxi...
When it comes to bidding time periods, many compa...
Skin is more than just a barrier Skin is the larg...
When it comes to human flesh searches, people oft...
On January 2, although restoring the factory sett...
As network technology becomes increasingly mature...
1. Background The main process of Baidu's sea...
How much is the quotation for the development of ...
Recent advances in artificial intelligence are re...
Maybe you are a novice designer, or maybe you are...
When we do marketing promotion, we can choose cha...