18

I want to change the default governor on my pi from ondemand to performance. Is there a way of doing this?

2
  • The rpi-cpu.gov script aims to change the governor, and re-apply this change at every boot. That would seem to accomplish setting your "governor of choice" as the default.
    – Seamus
    Jul 5, 2021 at 21:51
  • You want to change because it reboots your Raspberry if you don't press Caps?
    – Sandburg
    May 23, 2022 at 11:53

7 Answers 7

26

I don't think you can change the default without recompiling the kernel. By that I mean, the one the kernel chooses when booted up.

However, you can tell it to use a different governor while running, so if you want to effectively set the default without recompiling, you can add something to /etc/rc.local or some other start-up script.

The /sys directory is not on disk; it's an in RAM fake filesystem that is actually an interface for getting information from and sending commands to the kernel. This is all relative to /sys/devices/system/cpu, so:

> cd /sys/devices/system/cpu

You also need to be root to do the writes (echo blahblah >). Anyway, relative to the above directory, to see the available governors:

> cat cpu0/cpufreq/scaling_available_governors
conservative ondemand userspace powersave performance

Apparently yours is ondemand (but read "Gotcha" below about this); to check:

> cat cpu0/cpufreq/scaling_governor
ondemand

To change it:

echo performance > cpu0/cpufreq/scaling_governor

Note this either has to be done root, or like this

sudo sh -c "echo performance > cpu0/cpufreq/scaling_governor"

Just plain sudo will not work because there is a redirect, >.

You'll now notice the /sys/devices/system/cpu/cpufreq directory, (NOT the same as /sys/devices/system/cpu/cpu0/cpufreq!), which formerly contained filehandles for configuring the ondemand governor, is empty.


Gotcha...

On Raspbian there is an init script, the confusingly named raspi-config (no, it has nothing to do with the other one, which is run on first boot by the also confusingly named apply_noobs_os_config.sh), which uses this technique to set the governor to ondemand unless the shift key is held down (the stock Raspbian kernel has powersave compiled in). It doesn't do anything else, so it is safe to disable this (systemctl disable raspi-config), or you could edit it as per Gorkamorka's answer, but then you run the risk of having it un-edited by a system update, etc. -- who knows what anyone plans to do with that.

You could try adding the non-sudo version above to /etc/rc.local. This may happen after the raspi-config script has exited.

6
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – goldilocks
    May 27, 2017 at 14:40
  • 3
    Rather then require switching to root, just use tee: echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor.
    – Fake Name
    Jun 12, 2017 at 22:58
  • 2
    Other way : sudo sh -c "echo performance > cpu0/cpufreq/scaling_governor"
    – Moonchild
    Mar 19, 2018 at 22:17
  • Since Raspbian is based on Debian, you should be able to find the packages cpufrequtils and sysfsutils. After installing them, you can set the governor and other parameters from /etc/default/cpufrequtils and /etc/sysfs.conf - see here: wiki.debian.org/HowTo/CpuFrequencyScaling
    – mbirth
    Nov 3, 2018 at 12:59
  • Very good answer, but worth noting that the cpufrequtils apps are now available to relieve us of all (at least most) of the sysfs gymnastics.
    – Seamus
    Jul 5, 2021 at 17:07
4

similar question on RPi stack exchange here.

to paraphrase it, the on demand speed bumps up to max when cpu is at 95% busy. You can lower the amount of busy time to a lower percentage (meaning it'll be clocked higher when not as busy) by:

"writing an integer value to a file (you can put the following for example in the /etc/rc.local startup file): echo 60 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold"

so, theoretically...you could put this percentage at something incredibly low like 10 or 5, which would mean it would always be running at max.

Hope this helps. Be sure to up score the person who posted this in the linked article!

2
  • 3
    That's not actually an answer to the question, "how do I change the governor from ondemand to performance (they are two different governors).
    – goldilocks
    Aug 16, 2013 at 6:30
  • 3
    @goldilocks I thinks this answer still adds something useful to the quesiton since it explains how to make the one behave like the other. Sep 8, 2016 at 15:08
4

I made a small programm to change it and let it be changed every boot by automatically creating an init.d script.

Link to my project

3

An alternative solution (if you are running Raspbian) could be to modify the init.d script in /etc/init.d/raspi-config to always disable the scaling governor. It's default behavior is to only enable it at startup if one of the shift-keys is being pressed down:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          raspi-config
# Required-Start: udev mountkernfs $remote_fs
# Required-Stop:
# Default-Start: S
# Default-Stop:
# Short-Description: Switch to ondemand cpu governor (unless shift key is pressed)
# Description:
### END INIT INFO

. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Checking if shift key is held down"
    timeout 1 thd --dump /dev/input/event* | grep -q "LEFTSHIFT\|RIGHTSHIFT"
    if [ $? -eq 0 ]; then
      printf " Yes. Not enabling ondemand scaling governor"
      log_end_msg 0
    else
      printf " No. Switching to ondemand scaling governor"
      SYS_CPUFREQ_GOVERNOR=/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor      if [ -e $SYS_CPUFREQ_GOVERNOR ]; t
hen
        echo "ondemand" > $SYS_CPUFREQ_GOVERNOR
        echo 50 > /sys/devices/system/cpu/cpufreq/ondemand/up_threshold
        echo 100000 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_rate
        echo 50 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
        echo 1 | sudo tee /sys/devices/system/cpu/cpufreq/ondemand/io_is_busy
      fi
      log_end_msg 0
    fi
    ;;
  *)
    echo "Usage: $0 start" >&2
    exit 3
    ;;
esac
0
3

The simplest way to do that is using cpufrequtils package. Install it simply by:

sudo apt install cpufrequtils

cpufrequtils is a collection of three separate, but related, apps: cpufreq-aperf, cpufreq-info & cpufreq-set, each with its own reference manual.

And then you show available governors and more info on all CPUs using:

cpufreq-info

Finally, you can set a governor (performance for example) as follow:

sudo cpufreq-set -g performance

How to make it work after the reboot?

Quite easy. All you have to do is to create a bash script cpufreq.sh in root directory that will contain this:

#!/bin/bash
cpufreq-set -g performance

Then just type:

sudo crontab -e

Add this:

@reboot /root/cpufreq.sh

After reboot simple check

sudo systemctl status cron.service

Should show this:

pi@raspberrypi:~ $ sudo systemctl status cron.service
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-02-12 22:50:06 CET; 5min ago
       Docs: man:cron(8)
   Main PID: 477 (cron)
      Tasks: 1 (limit: 9039)
        CPU: 58ms
     CGroup: /system.slice/cron.service
             └─477 /usr/sbin/cron -f

Feb 12 22:50:06 raspberrypi systemd[1]: Started Regular background program processing daemon.
Feb 12 22:50:06 raspberrypi cron[477]: (CRON) INFO (pidfile fd = 3)
Feb 12 22:50:07 raspberrypi cron[477]: (CRON) INFO (Running @reboot jobs)
Feb 12 22:50:08 raspberrypi CRON[504]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Feb 12 22:50:08 raspberrypi CRON[642]: (root) CMD (/root/cpufreq.sh)
Feb 12 22:50:08 raspberrypi CRON[504]: pam_unix(cron:session): session closed for user root
1
  • This is part of a useful answer. If you added a method to invoke this at each bootup (e.g. @reboot in cron, or perhaps systemd), I feel you'd have a complete answer that addresses the OP's question.
    – Seamus
    Jul 5, 2021 at 16:46
0

While cpufrequtils is "new" and easy, I found that the best way to get the governor changed early in the boot sequence, was to modify the code in /etc/init.d/raspi-config as Gorkamorka suggests. The reason being, if cpufrequtils is installed, it changes the governor back to "ondemand" after your modified raspi-config code changed it to, say "conservative". This can be verified by looking through the daemon.log.

I created a "conservative" set of definitions and commented out the "default" governor = ondemand definition.

#CPU_DEFAULT_GOVERNOR="${CPU_DEFAULT_GOVERNOR:-ondemand}" CPU_ONDEMAND_UP_THRESHOLD="${CPU_ONDEMAND_UP_THRESHOLD:-50}" CPU_ONDEMAND_SAMPLING_RATE="${CPU_ONDEMAND_SAMPLING_RATE:-100000}" CPU_ONDEMAND_DOWN_SAMPLING_FACTOR="${CPU_ONDEMAND_DOWN_SAMPLING_FACTOR:-50}" CPU_ONDEMAND_INCLUDE_IO_CALC="${CPU_ONDEMAND_INCLUDE_IO_CALC:-0}"

CPU_DEFAULT_GOVERNOR="${CPU_DEFAULT_GOVERNOR:-conservative}" CPU_CONSERVATIVE_FREQ_STEP="${CPU_CONSERVATIVE_FREQ_STEP:-5}" CPU_CONSERVATIVE_UP_THRESHOLD="${CPU_CONSERVATIVE_UP_THRESHOLD:-50}" CPU_CONSERVATIVE_DOWN_THRESHOLD="${CPU_CONSERVATIVE_DOWN_THRESHOLD:-30}" CPU_CONSERVATIVE_SAMPLING_RATE="${CPU_CONSERVATIVE_SAMPLING_RATE:-100000}" CPU_CONSERVATIVE_DOWN_SAMPLING_FACTOR="${CPU_CONSERVATIVE_DOWN_SAMPLING_FACTOR:-50}"

and then I created an "elif" section to load conservative values

    if [ "$CPU_DEFAULT_GOVERNOR" = "ondemand" ]; then

    elif [ "$CPU_DEFAULT_GOVERNOR" = "conservative" ]; then
-3

echo "performance" |sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

1
  • While this may answer the question, good answers require more information than you have provided here. Also, your answer does not anything that the above answers have not already provided. Sep 8, 2016 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.