Sources

These instructions adapt material from:

NOTE: I have tested what follows only superficially.+ I did so on computers running Linux Mint 20 Cinnamon. For other versions of Linux, one may need to do things a little differently. For one thing, this method of using cgroups uses systemd.

Install required packages

sudo apt install cgroup-tools

which also installs libcgroup1

Set up a configuration file

I believe that one needs to do the following. Take the template for the global configuration file and turn it into an actual global configuration file (though we will not need to alter the resulting file). To do that:

sudo cp /usr/share/doc/cgroup-tools/examples/cgred.conf /etc/

Create two other files

There are two files we will need to edit. Those files are respectively the file that specifies one’s 'groups' and the file that allocates programs to groups. We need first of all to create those files. Do that as follows.

# File - call it the 'groups file' - that specifies groups.
sudo touch /etc/cgconfig.conf
# File - call it the 'rules file' - that allocates programs to groups.
sudo touch /etc/cgrules.conf

One can edit each file with one’s favourite editor, so long as the editor can write to a root-owned file. (So, one needs to run the editor as root, or the editor needs the capacity to prompt for a password.)

Create some group(s) by editing the groups file (/etc/cgconfig.conf)

group app/indexer {
  cpu {
    cpu.shares = 300;
  }
}

Note (and I think one can insert notes into cgconfig.conf, if one starts the note with a hash symbol): the maximum value for cpu.shares is 1000,; so e.g. a value of 300 limits the cpu usage for the group in question to 30%. That is, the cpu limit per group is ( value / 10 )

Allocate some programme(s) to groups by editing the rules file

(/etc/cgrules.conf)

*:angrysearch_update_database.py	cpu		app/indexer/
*:chrome							cpu		app/browser/
*:conky								cpu		app/monitor/
*:firefox							cpu		app/browser/
*:iridium-browser					cpu		app/browser/
*:recoll							cpu		app/indexer/

Apply the rules

sudo cgconfigparser -l /etc/cgconfig.conf && sudo cgrulesengd

Check the rules

One can check thusly that a group’s cpu-limiting is set up as intended.

sudo cgget -g cpu:<full group name>

E.g.:

sudo cgget -g cpu:app/indexer

Get the rules applied at boot

Next one needs to ensure that the rules are parsed and applied on every boot. One way to do that is with systemd. To do that, one needs to create two service files, give the files some content, save the files, and then tell tell systemd to take it away. One does all that as follows.

sudo touch /etc/systemd/system/cgconfigparser.service

Add the following content to that file (and save).

[Unit]
Description=cgroup config parser
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/sbin/cgconfigparser -l /etc/cgconfig.conf
Type=oneshot

[Install]
WantedBy=multi-user.target
sudo touch /etc/systemd/system/cgrulesgend.service

Add the following content to that file (and save).

[Unit]
Description=cgroup rules generator
After=network.target cgconfigparser.service

[Service]
User=root
Group=root
Type=forking
EnvironmentFile=-/etc/cgred.conf
ExecStart=/usr/sbin/cgrulesengd
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then run:

sudo systemctl daemon-reload && sudo systemctl enable cgconfigparser --now && sudo systemctl enable cgrulesgend --now

Reapplying rules after editing them

If one changes any rules (either of the two .conf files), then, to apply the new rules without rebooting, I think one must do the following.

sudo systemctl restart cgconfigparser && sudo systemctl restart cgrulesgend