CPU Tweaks


Back to the homepage


Tweaking your CPU can be useful (plus it's a lot of fun for us nerds). Below, you'll learn a couple of ways to do that.

Contents of this page:


How to toggle the performance mode of your CPU

1. There's a nifty tool for toggling the performance mode of your CPU. This is how to install and use it:

a. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

b. Copy/paste the following command line into the terminal:

sudo apt-get install cpupower-gui

Press Enter. Type your password when prompted. In Ubuntu this remains entirely invisible, not even dots will show when you type it, that's normal. In Mint this has changed: you'll see asterisks when you type. Press Enter again.

c. Then launch cpupower-gui from the menu and tweak to your heart's delight. It lets you set the scaling governor either individually for each CPU core (why?), or for all of them at once (which is of course what you always want, so make sure you flip that switch for the "All CPUs" option).

The default governor will probably be either schedutil or powersave, both of which are a good compromise for most users. You can set it to performance, which will ensure that your CPU will always run at maximal speed (and at maximal power consumption!).

This sizeable increase in power consumption is of course not a small thing: that's why I only recommend to select performance when you really need it (e.g. for gaming). And only for the duration of that need, so temporarily.

To check what your CPU cores are currently set to, utilize this terminal command (copy/paste it into the terminal and press Enter):

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

Limiting CPU usage for a particular application

2. It's possible to limit the CPU usage for a particular application, by means of taskset, cpulimit or systemd-run. Below, I'll describe all three methods.

Setting a CPU limit for an application with taskset

2.1. The terminal application taskset should be installed by default. It can be used when your CPU has more than one core, which is customary in modern computers.

With taskset you can determine which CPU core(s) a specified application may use, thus leaving the other CPU cores free to do other tasks. This might come in handy when your computer is running a big intensive background job.

Running taskset with the parameter -c allows you to set the CPU cores by number. Because taskset is a hard-core nerdy terminal application, it starts counting at 0 instead of 1 (something that nerds absolutely love), so for taskset a CPU with eight cores has cores 0, 1, 2, 3, 4, 5, 6, 7.

Some examples make things easier. Let's suppose that you want Timeshift to make a snapshot of your system. But in the background, so in the meanwhile you'll have enough computing power to do other things. You can then launch Timeshift with a restriction to use only the first core of your eight-core CPU, like this:

taskset -c 0 timeshift

Only the first three CPU cores:

taskset -c 0,1,2 timeshift

Only the middle two CPU cores (yes, I'm pleasing my inner nerd here!)

taskset -c 3,4 timeshift

You can also specify a range, for example in order to use only the last four cores:

taskset -c 4-7 timeshift

Setting a CPU limit for an application with cpulimit

2.2. The terminal application cpulimit is not installed by default, so first install it:

sudo apt-get install cpulimit

With cpulimit you can determine the maximum percentage that an application may use of your CPU. If your CPU has only one core, the maximum percentage is 100. But if your CPU has four cores, then the maximum percentage is 400.

And it becomes even more complicated when your CPU has more than one thread per core: for a CPU with four cores and eight threads, the maximum percentage becomes 800.

An example makes things easier. Let's suppose that you want Timeshift to make a snapshot of your system. But in the background, so in the meanwhile you'll have enough computing power to do other things. You can then launch Timeshift with a restriction to use only 25 % of your two-core single-thread CPU, like this:

cpulimit -e timeshift -l 50

(50, because the CPU in the example has two single-thread cores, which means that cpulimit counts up to 200 %, so 50 is the true 25 % here)

To disable cpulimit:

pkill cpulimit

Setting both a CPU limit and a RAM limit for an application with systemd-run

2.3. You can use systemd-run for limiting both RAM usage and CPU usage of a particular application.

An example makes it easiest, so below you'll find an example command in which text editor xed is capped to 20 % CPU and 2 GB RAM (modify those parameters at will):

a. Launch a terminal window.
(You can launch a terminal window like this: *Click*)

b. Copy/paste the following command line into the terminal (it's one long line, don't chop it up):

systemd-run --scope -p CPUQuota=20% -p MemoryMax=2048M -p MemoryHigh=1940M --user xed

Press Enter and enjoy.

Note that the last parameter (in the example command: xed) should be the name of the executable launcher of your application as it's called in your file system in /usr/bin (launch your file manager and check its name there, or simply check the properties of the menu entry of your application).

Explanation: This command uses Cgroups under the hood, but provides an easier and simpler way to utilize that.

The parameter MemoryMax is a hard cap, so there's also the MemoryHigh parameter in the oneliner (here arbitrarily set at 95% of MemoryMax). This works as a last line of defense, in order to handle memory limits more gracefully.

Based on this fine explanation on Askubuntu.

To the content of this website applies a Creative Commons license.

Back to the home page

Disclaimer