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.)

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I googled a lot and I think it’s just not possible, so I added the following one-liner to my .bashrc:

function setgov ()
{
    echo "$1" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 
}

Now I can run something like setgov ondemand and all cores will switch to the ondemand governor.

Solution 2

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 flag is used to set the change for all (“all hardware related”) cores

Solution 3

the shortest command to change governor of all cores is the following:

sudo bash -c 'for ((i=0;i<$(nproc);i++)); do cpufreq-set -c $i -g performance; done'

You could add it to .bashrc like htorque mentioned setgov performance:

function setgov ()
{
     for i in {0..7}; 
     do 
         sudo  cpufreq-set -c $i -g $1; # run cpufreq-set with root
     done
}

Solution 4

You can do this for all cores at once by running

sudo cpupower frequency-set --governor performance

Solution 5

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

Solution 6

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);

  1. Put this (one line of) code (thank you, courtesy of switch87 in the previous answer)

    sleep 60 && bash -c 'for i in {0..7}; do cpufreq-set -c $i -g performance; done'
    
  2. into the file in the directory

    /etc/rc.local
    

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.

Solution 7

Basing on switch87’s answer, I made a simple script cpufreq-set-all, which will allow to do other cpufreq-set things with all CPUs:

   #!/bin/bash
   MAX_CPU=$((`nproc --all` - 1))
   for i in $(seq 0 $MAX_CPU); do
       echo "Changing CPU " $i " with parameter "[email protected];
       cpufreq-set -c $i [email protected] ;
   done

Now it can be invoked(after chmod +x, of course) like this:

cpufreq-set-all -g powersave

or

cpufreq-set-all -f 800Mhz

Solution 8

sometimes setting the governor isn’t enough. I had to set the MAX and MIN variables for frequency as well, because the scaling policies had them set to minimum, so what I do is
1. Get the min and max scaling frequencies from CPU 0
2. Divide them by a million to get the frequency in GHz
3. Pipe the result through bc so I can get the result in floating point(BASH can only handle integers) – scale=1 is how many decimals
4. Check if a scaling governor is supplied on the command line
5. If not, set ONDEMAND – save some money when idling
6. Run the cpufreq-set command in a loop on all CPU-s

#!/bin/bash
echo "Setting all CPUs to " "[email protected]"
CPUINFO_MIN_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq)
CPUINFO_MAX_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq)
MIN=$(echo "scale=1; $CPUINFO_MIN_FREQ / 1000000" | bc)"G"
MAX=$(echo "scale=1; $CPUINFO_MAX_FREQ / 1000000" | bc)"G"

echo "Minimum frequency: " $MIN ", Maximum frequency: " $MAX

[email protected]
if [ $# -eq 0 ]
    then
        echo "No arguments supplied, using ONDEMAND governor"
        GOVERNOR="ondemand"
    else
        echo "Governor " $1 " supplied"
        GOVERNOR=$1
fi

for ((i=0;i<$(nproc);i++));
    do cpufreq-set -c $i -r -g $GOVERNOR  --min $MIN --max $MAX;
done

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply