How do I set the CPU frequency scaling governor for all cores at once?
I’d like to set the CPU frequency scaling governor for all cores at once instead of doing it individually for each core. Is there a way to do this? (I know it would be easy to echo the governor to /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor , but I’m not looking for a custom solution.)
With «custom solution» you mean that a shell script is unacceptable, it needs to be a built-in GUI button?
I’m just wondering if there already exists a solution in a standard installation (doesn’t need to have a GUI).
Check this page: idebian.wordpress.com/2008/06/22/cpu-frequency-scaling-in-linux under «debian implementation»: It is apparently possible to change the boot default, and there is a userspace tool called powernowd. (This is a comment rather than reply because the post is from 2008, and I haven’t tested if it still works. )
8 Answers 8
I googled a lot and I think it’s just not possible, so I added the following one-liner to my .bashrc :
Now I can run something like setgov ondemand and all cores will switch to the ondemand governor.
This doesn’t work as the file scaling_governor is owned by root and has restricted permissions. Changing the permissions is not always recommended.
I’m still a linux noob but don’t you think cpufrequtils lets u do it by using (its not bundled in the Ubuntu OS but is there in the repository)
sudo apt-get install cpufrequtils sudo cpufreq-set -r -g performance
The -r related option doesn’t do it for all the cores. We have to specify the cpu number with option -c
Exactly, @Sri is right. cpufreq-set in fact lacks of ultimately simple but needed functionality. Lots of people as this vote counters clearly shows are just not aware of it because its description is simply misleading to top it off.
Who gives these +1’s without testing. This does not work at all. I posted the simplest working solution I could find.
the shortest command to change governor of all cores is the following:
You could add it to .bashrc like htorque mentioned setgov performance :
function setgov () < for i in ; do sudo cpufreq-set -c $i -g $1; # run cpufreq-set with root done >
You CANNOT run a bash function with sudo. You can instead stick that into /usr/local/bin/setgov, and even set it as setuid so you can run it without root privileges.
You can do this for all cores at once by running
sudo cpupower frequency-set --governor performance
The cpupower command is available on 18.04 with the appropriate linux-tools package for the running kernel, according to user299889
Might as well add bash code completion, while we’re at it:
function setgovernor () < echo "$1" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >complete -W "$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)" setgovernor
The way I set this to stick since the governor defaults back to powersave after about 30 seconds now as of Ubuntu 16.04 or more recent (I’m on Ubuntu Mate);
- Put this (one line of) code (thank you, courtesy of switch87 in the previous answer)
sleep 60 && bash -c 'for i in ; do cpufreq-set -c $i -g performance; done'
Mine is a line above «exit 0» and uncommented, under the commented ones.
«rc.local» for those who do not know, it runs the command as sudo. Any command it seems, to change the governor, needs to be run as sudo.
It lets the governor reset back to powersave and runs the code as sudo after 60 seconds to change it back to performance.
Change the «60» (in the code you copy) to whatever time (in seconds. 60 = 60 seconds= 1 minute) you need to delay the command and «performance» (in the command part) to what governor you want it to change to.
Out of my hours and hours of searching I have not found a more permanent fix for this than this. I figure what’s a couple of minutes that it’s on powersave if this is the best fix I’ve found, right? Right.
Not the best fix, but it makes it somewhat permanent after it does it’s little switch to powersave thing. If you want to boot right up and jump into a game or something you’re going to have to wait a minute for the code you just put in to switch it back from powersave or lower the timing on it (depending on how long it takes everything to start up so it’ll switch back to performance correctly).
And, as always, to revert back to default (I’ve seen some issues with people’s PCs overheating which is why they might have defaulted it to powersave in the first place) just remove the code from rc.local and reboot or switch it back manually with your cpu icon indicator switcher or run;
sudo /etc/init.d/cpufrequtils restart
in the terminal and or reboot.
CPU Frequency Scaling On Linux With cpupower
CPU frequency scaling enable OS to scale the frequency of the CPU for several purpose, including power saving and for bench-marking purpose. This tutorial shows how to scale the frequency of CPU manually using cpupower .
- This is tutorial is written using Ubuntu, but also tested in Arch, should works well with other linux distro too.
- Most of the commands used below requires root permission.
Installing cpupower
Enter the command below to install the needed tools
$ sudo apt install linux-tools-common linux-tools-generic linux-tools-`uname -r`
Steps before configuring the frequency
Changing your CPU driver
Run the command to attain CPU information:
$ cpupower frequency-info | grep driver
It will show up something like this:
which indicates that the current driver is called intel_pstate . For more information regarding the driver, please refer to the document at kernel.org. This driver provides 2 choices of CPU governor which are performance and powersave . However, it doesn’t have the governor for us user to make customization to the frequency.
Hence, we have to replace the driver with other alternatives.
First, open the file /etc/default/grub and add the phrase intel_pstate=disable to the line GRUB_CMDLINE_LINUX_DEFAULT . After adding the phrase it should looks like
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash intel_pstate=disable"
Upon the next boot, check again the driver in use. If it shows acpi-cpufreq as result then we have successfully replaced the driver. To switch it back simply remove the phrase intel_pstate=disable from the line and run update-grub again.
Changing CPU governor
$ cpupower -c 0 frequency-info
The result are shown as below:
analyzing CPU 0: driver: acpi-cpufreq CPUs which run at the same hardware frequency: 0 CPUs which need to have their frequency coordinated by software: 0 maximum transition latency: 10.0 us hardware limits: 400 MHz - 1.80 GHz available frequency steps: 1.80 GHz, 1.80 GHz, 1.70 GHz, 1.60 GHz, 1.50 GHz, 1.40 GHz, 1.30 GHz, 1.20 GHz, 1.10 GHz, 1000 MHz, 900 MHz, 800 MHz, 700 MHz, 600 MHz, 500 MHz, 400 MHz available cpufreq governors: userspace performance schedutil current policy: frequency should be within 400 MHz and 1.80 GHz. The governor "userspace" may decide which speed to use within this range. current CPU frequency: Unable to call hardware current CPU frequency: 1.40 GHz (asserted by call to kernel) boost state support: Supported: yes Active: yes
Within the options at available cpufreq governors , we are going to use the governor userspace which allows us to do modification. If you don’t have userspace in your list then run the command
modprobe cpufreq_userspace
To choose userspace as the CPU governor and scale the frequency, run the following commands:
$ sudo cpupower frequency-set -g userspace $ sudo cpupower frequency-set -f 1400MHz
The input unit for the frequency can be Hz, MHz and GHz.
Your CPU should now running at your desired speed.
$ grep MHz /proc/cpuinfo cpu MHz : 1400.082 cpu MHz : 1400.100 cpu MHz : 1400.009 cpu MHz : 1400.000 cpu MHz : 1400.026 cpu MHz : 1400.123 cpu MHz : 1400.026 cpu MHz : 1400.002
Изменение CPU Scaling Governor в Linux
Благодаря «Scaling Governor», можно выбрать оптимальную схему управления частотой процессора, что влияет на потребление электроэнергии и производительность.
На высоко нагруженных серверах доступа у знакомых были случаи зависания сервера при «ondemand», хотя у меня таких случаев не было, но все же рекомендую изменить на «performance».
На ноутбуках и слабо нагруженных серверах можно оставить «ondemand» для экономии электроэнергии.
На серверах HPE в BIOS обязательно нужно включить Collaborative Power Control (CPC) иначе из операционной системы нельзя будет управлять производительностью процессора и не будет этих файлов:
find / -name scaling_governor find / -name scaling_max_freq
Посмотрим текущее значение всех ядер процессора:
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ls /sys/devices/system/cpu/
В моем случает, для всех восьми ядер отобразилось значение по умолчанию — «ondemand».
Посмотрим возможные схемы:
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
Переключимся на root пользователя:
И укажем для всех ядер «performance»:
echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu5/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu6/cpufreq/scaling_governor echo performance > /sys/devices/system/cpu/cpu7/cpufreq/scaling_governor
apt install cpufrequtils for i in 0 1 2 3 4 5 6 7 8 9 10 11; do cpufreq-set -c $i -g performance; done
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor egrep '(MHz)' /proc/cpuinfo
Если предыдущие способы не помогли, то для Intel CPU можно также указать «intel_idle.max_cstate» в grub:
nano /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT=". intel_idle.max_cstate=1" update-grub reboot dmesg -T | grep intel_idle
Либо можно полностью отключить энергосберегающие функции CPU и PCIe:
intel_idle.max_cstate=0 processor.max_cstate=1 pcie_aspm=off quiet mitigations=off
В крайнем случае с idle=poll:
intel_idle.max_cstate=0 processor.max_cstate=1 pcie_aspm=off quiet mitigations=off idle=poll
Замечу, что если указать idle=poll, то в BIOS или iLo например, может отображаться что CPU используется на 100%, хотя это не так.
Также отключим уже ненужную службу энергосбережения:
systemctl is-enabled ondemand systemctl disable ondemand