54

I'm trying to set the CPU governor to performance in 18.04, the problem is that it never survives reboots.

I've tried following these steps in an answer to this question How to set "Performance" instead of "Powersave" as default? but when I reboot the computer the CPU governor switches back to powersaver.

I've also tried creating a file called /etc/rc.local with the line:

cpupower frequency-set --governor performance

That also doesn't survive reboots.

How can I do this?

Falc
  • 816
  • 3
    What procedure exactly did you follow? Is the typo in the filename /etc/rc.local only in this question, or did you make that same typo when creating it? This question may not be specific to 18.04 and probably shouldn't be a bug report rather than a question here. I think the question could be considered for reopening if you edit it to add more detail. – Zanna Apr 04 '18 at 10:30
  • The parameter --governor powersave should be --governor performance ... – WinEunuuchs2Unix Apr 04 '18 at 11:05
  • I followed this command list, but when I reboot the computer the cpu governor switches back to powersaver. I didn't realise there is a typo in the /etc/rc.local command, I got that from another askubuntu post here when I tried to make a script to have the command 'cpupower frequency-set --governor performance' run at startup. – Falc Apr 04 '18 at 18:38
  • 4
  • Yeah I've tried following that thread, I get this message: j@j-ncase:~$ sudo update-rc.d ondemand disable update-rc.d: error: cannot find a LSB script for ondemand – Falc Apr 04 '18 at 21:24
  • @DavidFoerster half-way duplicate... See my comment there with an Ubuntu 18.06 specific update: https://askubuntu.com/a/1049270/839002 . – n1ghtm4n4g3r Jun 24 '18 at 10:13
  • @Falc: Could you please [edit] your post when you want to add information? Especially file or program output listings (with the help of the {} button in the editor toolbar) will be much more readable there; alternatively you can use a pastie service for longer listings and include the link of your pastie in your question. Overall it’s best to have everything relevant in one place. Additionally, comments may be deleted for various reasons. Thanks. – David Foerster Jun 24 '18 at 10:17
  • @Thenightmanager: With your addition the linked question is not a full duplicate. ;-] – David Foerster Jun 24 '18 at 10:21
  • @Zanna is correct about reopening after an edit. I believe the question is relevant to all Intel processors that support frequency scaling which probably appeared a decade ago? – WinEunuuchs2Unix Jun 24 '18 at 15:52
  • Because of your question I updated my answer in your link first link: https://askubuntu.com/a/936488/307523 to make it work for your question. Thanks. – WinEunuuchs2Unix Oct 22 '18 at 00:16
  • Not one reference to cgroups (control groups)... looks like i'll have to answer this. cgroups limits cpu usage, and memory use for users in a group. – j0h Dec 12 '19 at 02:12

9 Answers9

49
sudo apt-get install cpufrequtils
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
sudo systemctl disable ondemand
11

Short Answer

In /etc/rc.local put in these commands:

sleep 120
cpupower frequency-set --governor performance

1 minute after boot automatic switch to Powersave

For whatever reasons my Skylake Intel CPU always starts up in Performance mode and then switches to Powersave mode at the 1 minute mark automatically.

If you set the mode to Performance on startup it will be overridden around the 1 minute Up Time mark to Powersave mode.

In the GIF below, the 3000+ MHz CPU speed at start up appears near the top. The up time appears near the bottom. When up time hits about 1 minute you see CPU MHz drop off. :

CPU goes powersave 1 minute 2.gif


Program to monitor exact second Powersave invoked

Create this script in /usr/local/bin/watch-gov.sh:

#! /bin/bash

# NAME: watch-gov.sh
# PATH: /usr/local/bin
# DESC: Set governnor to performance and watch for change
#       Ask Ubuntu question: https://askubuntu.com/questions/1021748/set-cpu-governor-to-performance-in-18-04/1084727#1084727
# CALL: called from `/etc/rc.local`
# DATE: Created Oct 18, 2018.

echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
last_gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
Uptime=$(uptime)
echo "watch-gov.sh: Set to performance at $Uptime " > /tmp/watch-gov.log

for ((i=0; i<300; i++)) ; do
    curr_gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
    if [ $last_gov != $curr_gov ] ; then
        last_gov=$curr_gov
        Uptime=$(uptime)
        echo "watch-gov.sh: Current governor: $last_gov Uptime: $Uptime" >> \
            /tmp/watch-gov.log
    fi
    sleep 1
done

Call the script in /etc/rc.local before the exit 0 command (explained in detail below).

One minute after logging in look at the output:

$ cat /tmp/watch-gov.log
watch-gov.sh: Set to performance at  17:50:09 up 0 min,  0 users,  load average: 0.00, 0.00, 0.00 
watch-gov.sh: Current governor: powersave Uptime:  17:51:09 up 1 min,  1 user,  load average: 1.89, 0.62, 0.22

Confirmation from this answer states this 1 minute force to powersave governor is controlled by /etc/init.d/ondemand.


Sleep 120 seconds before setting Performance Mode

The simplest way to stay in Performance mode is to edit /etc/rc.local and insert these lines before the last line containing exit 0:

sleep 120 # Give CPU startup routines time to settle.
echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

Save the file and reboot.

If you insert the new lines after exit 0 it will never be executed.

To setup /etc/rc.local in 18.04 see: How to Enable /etc/rc.local with Systemd


Caveats

Your machine will probably run 10 to 15 degrees C hotter.

You may need to remove other programs that change CPU frequency if they override your Performance settings in /etc/rc.local

  • Which package has cpupower and which GUI you have that animgif from? I'm trying to tune a Ryzen 7 with cpufreq-utils but it feels sluggish even with the performance governor – Csaba Toth Mar 01 '19 at 02:16
  • @CsabaToth I use Conky to display CPU, RAM, HDD, SDD, Network and other system details. You don't need cpupower but it is part of cpufrequtils package. If system is sluggish try Unity desktop instead of default Gnome desktop. – WinEunuuchs2Unix Mar 01 '19 at 10:44
  • @WinEunuchs2Unix thanks, I haven't ever heard of Conky, and it was installed onto my system. I'm running Devuan BTW with Xfce4. So Conky says my CPU frequency is 550 MHz (0.55 GHz), which explains the sluggishness and is what I expected, but cpufreq-info tells 3.2 GHz and I'm forcing performance governor. I need to open a Question for this. – Csaba Toth Mar 01 '19 at 16:51
  • /etc/rc.local & /etc/init.d/ondemand don't exist in 18.04.. – doug Jul 31 '20 at 07:11
  • @doug To setup /etc.rc.local in 18.04 see: How to Enable /etc/rc.local with Systemd – WinEunuuchs2Unix Jul 31 '20 at 22:25
  • This is a simple way to do it, thanks. I had to modify your sleep line to fork it in a separate process, as running rc.local was blocking startup. Just changed it to "$(sleep 60; cpupower frequency-set --governor performance) &". Boot runs at full speed, and then it ramps down for lower power steady state operation in an octoprint server. – naasking Oct 15 '23 at 17:33
8

Default Ubuntu kernel configurations are such that the performance CPU frequency scaling governor will be used during boot. The relevant section of the kernel configuration file ( /boot/config-4.15.0-36-generic , in this example) is:

#
# CPU Frequency scaling
#
CONFIG_CPU_FREQ=y
CONFIG_CPU_FREQ_GOV_ATTR_SET=y
CONFIG_CPU_FREQ_GOV_COMMON=y
CONFIG_CPU_FREQ_STAT=y
CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE=y
# CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE is not set
# CONFIG_CPU_FREQ_DEFAULT_GOV_SCHEDUTIL is not set
CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
CONFIG_CPU_FREQ_GOV_POWERSAVE=y
CONFIG_CPU_FREQ_GOV_USERSPACE=y
CONFIG_CPU_FREQ_GOV_ONDEMAND=y
CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
CONFIG_CPU_FREQ_GOV_SCHEDUTIL=y

#
# CPU frequency scaling drivers
#
CONFIG_X86_INTEL_PSTATE=y
CONFIG_X86_PCC_CPUFREQ=y
CONFIG_X86_ACPI_CPUFREQ=y
CONFIG_X86_ACPI_CPUFREQ_CPB=y
CONFIG_X86_POWERNOW_K8=y
CONFIG_X86_AMD_FREQ_SENSITIVITY=m
CONFIG_X86_SPEEDSTEP_CENTRINO=y
CONFIG_X86_P4_CLOCKMOD=m

But also by default during boot up the ondemand service is executed. It sleeps for 1 minutes and then changes the scaling governor to either interactive, ondemand or powersave, depending on availability. In turn availability depends on which CPU frequency scaling driver you are using. The code is (in multiple locations, search for ondemand):

#! /bin/sh
### BEGIN INIT INFO
# Provides:          ondemand
# Required-Start:    $remote_fs $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Set the CPU Frequency Scaling governor to "ondemand"
### END INIT INFO

# Don't run if we're going to start an Android LXC container:
[ ! -f /etc/init/lxc-android-config.conf ] || exit 0

PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

AVAILABLE="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
DOWN_FACTOR="/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor"

case "$1" in
    start)
        start-stop-daemon --start --background --exec /etc/init.d/ondemand -- background
        ;;
    background)
        sleep 60 # probably enough time for desktop login

        [ -f $AVAILABLE ] || exit 0
        read governors < $AVAILABLE
        case $governors in
                *interactive*)
                        GOVERNOR="interactive"
                        break
                        ;;
                *ondemand*)
                        GOVERNOR="ondemand"
                        case $(uname -m) in
                                ppc64*)
                                        SAMPLING=100
                                ;;
                        esac
                        break
                        ;;
                *powersave*)
                        GOVERNOR="powersave"
                        break
                        ;;
                *)
                        exit 0
                        ;;
        esac

        for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
        do
                [ -f $CPUFREQ ] || continue
                echo -n $GOVERNOR > $CPUFREQ
        done
        if [ -n "$SAMPLING" ] && [ -f $DOWN_FACTOR ]; then
                echo -n $SAMPLING > $DOWN_FACTOR
        fi
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

Why is it called "ondemand", but it sets other governors (for example with the intel_pstate driver it will set the powersave governor)? Because this tool pre-dates the intel_pstate driver, back to a time when, by far, the dominant frequency scaling driver was the acpi-cpufreq driver, and "ondemand" was the preferred Ubuntu default governor.

So, one way to boot and stay using the performance CPU frequency scaling governor is to disable the service that changes away from it (which was also mentioned in another answer):

Before:

~$ systemctl list-units --all --type=service | grep ondemand
  ondemand.service                                      loaded    inactive dead    Set the CPU Frequency Scaling governor
~$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
ondemand
ondemand

Disable the service:

~$ sudo systemctl disable ondemand
Removed /etc/systemd/system/multi-user.target.wants/ondemand.service.

re-boot, then check again (being sure to wait a minute after the re-boot):

doug@s17:~$ systemctl list-units --all --type=service | grep ondemand
doug@s17:~$
doug@s17:~$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
performance
performance

Note: the examples in this answer are from a computer that uses the acpi-cpufreq CPU frequency scaling driver. If you are using the intel_pstate driver, with no ondemand governor, the powersave governor will be used by default.

Anticipated question: Why do my CPU frequencies scale, even when using the performance governor?

Answer: Modern processors scale the CPU frequency, even in performance mode and as a function of the depth of the idle state they go into. If you really want to lock the CPU frequency then disable all idle states deeper than 0. However, note that it will cost a huge huge amount of power.

Personally, and as mentioned in another answer, I use the performance governor or the powersave governor as a function of whatever work I am doing. My scripts are a little different:

$ cat set_cpu_performance
#! /bin/bash
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "performance" > $file; done

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

and:

$ cat set_cpu_powersave
#! /bin/bash
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "powersave" > $file; done

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

example usage (on a computer using the intel_pstate driver):

$ sudo ./set_cpu_performance
powersave
powersave
powersave
powersave
powersave
powersave
powersave
powersave
performance
performance
performance
performance
performance
performance
performance
performance
Doug Smythies
  • 15,448
  • 5
  • 44
  • 61
  • 1
    There are so many exotic and complicated solutions out there, but systemctl disable ondemand is by far the simplest option that I've seen anywhere, and it works great. I also had to do echo 'GOVERNOR="performance"' > /etc/default/cpufrequtils to persist between reboots, as mentioned above. – Stefan Lasiewski Oct 01 '20 at 02:04
  • Thanks for this great summary. On my machine (Pop_OS 21.04), Kernel 5.13, the ondemand service does not appear to exist? sudo systemctl status ondemand says: Unit ondemand.service could not be found. – mxk Nov 17 '21 at 08:38
  • @Matthias : Yes, as far as I know the ondemand service is an Ubuntu thing. – Doug Smythies Nov 17 '21 at 15:09
  • @DougSmythies I researched it a bit, and it turns out that I was using the intel_pstate scaling driver. It only supports two governors: performance and powersave. I don't think it's Ubuntu related but rather based on the scaling driver. When I switch to intel_cpufreq, I see more, including ondemand. This is a pretty good summary: https://www.kernel.org/doc/html/v4.12/admin-guide/pm/cpufreq.html – mxk Nov 19 '21 at 06:59
  • You are confusing the ondemand governor with the Ubuntu on demand service. The two are not related at all. The ubuntu service sets the governor after boot. It used to wait one minute, but does't anymore. The service is Named "ondemand" because way back that was all it did, was set the governor to "ondemand", as at the time the intel_pstate driver did not exist. @Matthias . – Doug Smythies Nov 19 '21 at 15:16
  • Ah yes, makes sense -- thanks for clarifying! – mxk Nov 21 '21 at 13:11
4

You'll first need to install cpufrequtils;

sudo apt install cpufrequtils

then, perform a restart and set cpufrequency to performance,

sudo cpufreq-set -g performance

Note: If you ran the above command without restarting, then you'd have to edit edit the file in /etc/init.d/cpufrequtils and change the line that says

GOVERNOR=ondemand

to

GOVERNOR=performance

then run;

sudo sh /etc/init.d/cpufrequtils start

it'd probably prompt you to restart your systemctl daemon, so do it like this;

sudo systemctl daemon-reload

this should do the work. then view your cpufrequency with;

cat /proc/cpuinfo | grep -i mhz
2

What I did was use the file /etc/rc.local

To help you find your paths, use:

find / -name scaling_governor
find / -name scaling_max_freq

This works for my setup, but you just need to edit it for your setup

I added to /etc/rc.local using nano:

# Set CPU Governor and Freq at boot up
 echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
 echo 1500000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
 echo 2000000 > /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq
 echo "performance" > /sys/devices/system/cpu/cpu4/cpufreq/scaling_governor

directly under the shebang line. Close nano with Ctrl-X and Y to save

Then for 18.04 (may not work on 16.04) on the command line run:

systemctl disable ondemand

Then on the command line - read /etc/rc.local and then reboot:

/etc/rc.local
reboot

if /etc/rc.local chokes and errors then make sure it's chmod +x /etc/rc.local

WesZ
  • 21
  • One other thing that I do is to edit /etc/init.d/ondemand or ondemand needs to be moved to ondemandzzz to disable it. There is a sleep 60 in /etc/init.d/ondemand before it sets the governor. Even though rc.local will change it to performance at boot time, 60 seconds later it gets changed by /etc/init.d/ondemand. In /etc/init.d/ondemand change the line: echo -n $GOVERNOR > $CPUFREQ to: echo -n "performance" > $CPUFREQ if you decide to keep /etc/init.d/ondemand working... – WesZ Nov 08 '18 at 12:45
2

I am using this bash script to set the performance governor:

#!/bin/bash

sudo cpufreq-set --cpu 0 --governor performance
sudo cpufreq-set --cpu 1 --governor performance
sudo cpufreq-set --cpu 2 --governor performance
sudo cpufreq-set --cpu 3 --governor performance
sudo cpufreq-set --cpu 4 --governor performance
sudo cpufreq-set --cpu 5 --governor performance
sudo cpufreq-set --cpu 6 --governor performance
sudo cpufreq-set --cpu 7 --governor performance

cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

Make is executable chmod +x cpu.sh and run at any time You need (ever right after every computer start/reboot).

  • 1
    instead of modifying each cpu core separately you can specifiy the -r option "modify all hardware-related CPUs at the same time" – Willi Mentzel Dec 25 '20 at 09:00
1

I had a similar problem. In my case I wanted to set the governor to 'schedutil', but after a reboot I would always find the governor to be 'ondemand'. I read and did try most solutions presented here to no avail.

What did the trick for me, and I am using Ubuntu Mate 18.04.2 was finding the file:

/lib/systemd/set-cpufreq

editing it to insert the lines:

    *schedutil*)
            GOVERNOR="schedutil"
            break
            ;;

so now the file reads:

    #! /bin/sh
    # Set the CPU Frequency Scaling governor to "ondemand"/"powersave"      where available
    set -eu

    FIRSTCPU=`cut -f1 -d- /sys/devices/system/cpu/online`
    AVAILABLE="/sys/devices/system/cpu/cpu$FIRSTCPU/cpufreq/scaling_available_governors"
    DOWN_FACTOR="/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor"

    [ -f $AVAILABLE ] || exit 0

    read governors < $AVAILABLE
    case $governors in
            *interactive*)
                   GOVERNOR="interactive"
                   break
                   ;;
            *schedutil*)
                   GOVERNOR="schedutil"
                   break
                   ;;
            *ondemand*)
                   GOVERNOR="ondemand"
                   case $(uname -m) in
                           ppc64*)
                           SAMPLING=100
                   ;;
                   esac
                   break
                   ;;
            *powersave*)
                   GOVERNOR="powersave"
                   break
                   ;;
            *)
                   exit 0
                   ;;
    esac

    [ -n "${GOVERNOR:-}" ] || exit 0

    echo "Setting $GOVERNOR scheduler for all CPUs"

    for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
    do
            [ -f $CPUFREQ ] || continue
            echo -n $GOVERNOR > $CPUFREQ
    done
    if [ -n "${SAMPLING:-}" ] && [ -f $DOWN_FACTOR ]; then
            echo -n $SAMPLING > $DOWN_FACTOR
    fi.

and now when I boot my computer it starts with the 'schedutil' governor!

Vulto
  • 11
0

A simple script to switch between powersave and performance mode. You can try with any of the 'wrmsr' commenting out one in below script. I don't know why, but writing 0 to register worked well for me.

#!/bin/bash

argument: powersave or performance

if [ $# != 1 ] then echo "You should specify CPU driver governor as argument!" exit 1 fi

totalCPUs=$(lscpu | grep '^CPU(s):' | awk '{print $2}')

for (( cpuIndex=0; cpuIndex<$totalCPUs; cpuIndex++ )) do echo "Optimizing CPU $cpuIndex" cpufreq-set -c $cpuIndex -g $1 done

if [ $1 == "performance" ] then modprobe msr controlRegVal=$(rdmsr 0x1FC) #wrmsr 0x1FC $controlRegVal wrmsr 0x1FC 0 fi

exit 0

0

On Ubuntu 22.04 LTS, after setting GOVERNOR="performance" and disabling ondemand, I also had to restart cpufrequtils service to actually make it work:

sudo systemctl restart cpufrequtils.service