Programming and Technology

By Robert Roos

I run Ubuntu on my laptop and Elementary OS on my stationary computer as my daily driver. Linux overall has come a very long way in the last couple of years, much due to the explosion in open-source applications available. But also due to companies working harder to make their programs and games run on Linux (Steam, Lutris and JetBrains Rider the real MVP).

We are getting closer to linux going mainstream I think, especially with user friendly distros such as Ubuntu, Elementary OS, Fedora and of course Manjaro.

But there are still quite a few things to work out - I think the lack of GUI:s for a lot of "under the hood" settings still scares off a lot of users. For example, I realized yesterday when running an older single-threaded game through Lutris that I had bad performance (AMD Ryzen CPU). I checked the CPU frequency and found out that the governor for the CPU was set to "ondemand", which is good for most standard work/multi-threaded games. But in this case forcing the CPU to work at a higher frequency was the only solution to get an acceptable framerate. This however needed some scary terminal tinkering... well not that scary at all to be honest, let me show you.

Start off by opening a fresh instance of the terminal and run:

sudo apt-get install cpufrequtils

This installs the cpu freq tools we will be using to set the governor to performance mode. But before we do that, run the following command in the terminal:

cpufreq-info

This should render information about your CPU, look for the governor settings line and make sure it's not already set to "performance". Usually Ubuntu and other Debian distros sets the governor to "ondemand", so that is what you should be seeing here.

Next off, open another terminal instance (keep your previous open) and run the following command:

watch -n1 "lscpu | grep MHz | awk '{print $1}'";

This will render your current CPU min, max and current frequency. If you want you can try running a game or larger video to see it go up and down, but overall trying to stay closer to the minimum frequency than the maximum. This is due to the governor beeing set to "ondemand", which is trying to save power and generate less heat.

Let's crank up the performance. Run the following in your first terminal instance:

sudo bash -c 'for i in {0..11}; do cpufreq-set -c $i -g performance; done'

NOTICE! Make sure you set the number 11 to the amount of cores your CPU has minus one (since the count starts at 0). In my case I run a 12 core Ryzen CPU, therefore I need to set it to 11. If you have an 8 core CPU, you set it to 7 and so on. Then hit enter to run the script.

Now run:

cpufreq-info

And look for the governor once again, your should now see it set to "performance" instead of "ondemand".

Open up the second terminal instance again and you should now see your overall cpu frequency staying at a higher number, closer to maximum than minimum. Try running a game or large video again and see the difference (and feel the difference with higher FPS in games).

To return the governor to "ondemand" simply run:

sudo bash -c 'for i in {0..11}; do cpufreq-set -c $i -g ondemand; done'

And that's it!

Now there are a lot of ways to work with this, for example the one I've just shown. Another is to add this script to your ".bashrc" for easier execution in the future. You could also add it to the startup scripts for your distro to have it set the CPU to performance mode permanently. I won't go into this since I think switching manually is better and ondemand is what you probably should be running most of the time when not doing heavy work, it saves some power and that is always good.

I will however show how you can make the execution of the script easier through ".bashrc".

Open a terminal instance and run:

sudo nano ~root/.bashrc

This will open a editor, on a new line paste the following (again change 11 to the number of cores you have minus one)

function setgov ()
{
     for i in {0..11}; 
     do 
         cpufreq-set -c $i -g $1; 
     done
}

Press CTRL+X and you will be asked if you want to save the changes, save the changes and the editor will close.

You can now change your CPU governor by running the following in a terminal instance:

sudo bash
sudo setgov performance

And to set it back to ondemand:

sudo bash
sudo setgov ondemand

That's it, you now have a very simple way to crank up the performance when gaming or whatever else needs that extra kick. Good luck!

Comments

Be the first to post a comment

Post a comment