Skip to content

Instantly share code, notes, and snippets.

@Kumquatum
Last active June 29, 2021 14:04
Show Gist options
  • Save Kumquatum/5f13a27eceec11acafb5171e3e855aed to your computer and use it in GitHub Desktop.
Save Kumquatum/5f13a27eceec11acafb5171e3e855aed to your computer and use it in GitHub Desktop.

Personnal cgroup config

Based on paranoids blog article cgroup ubuntu 18.04 howto

Installing packages

sudo apt install cgroup-bin cgroup-lite cgroup-tools cgroupfs-mount libcgroup1

Config files

Files defining the rules for cgroups.

cgred: a service that moves tasks into cgroups according to parameters set in the /etc/cgrules.conf file

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

cgconfig: a service installed with the libcgroup package which provides a convenient way to create hierarchies, attach subsystems to hierarchies, and manage cgroups within those hierarchies.

sudo vim /etc/cgconfig.conf
group limitcpu {
    cpu {
        cpu.shares = 400;
    }
}

group rstudio_ide{
    memory {
        memory.limit_in_bytes = 5G;
    }
}

group browser{
    cpu {
        cpu.shares = 100;
    }
    memory {
        memory.limit_in_bytes = 2G;
    }
}

group ms_teams{
    cpu {
        cpu.shares = 50;
    }
    memory {
        memory.limit_in_bytes = 200M;
    }
}

cgrules: a configuration file used by libcgroups to define the control groups to which the process belongs to.

sudo vim /etc/cgrules.conf
#<user:process>         <controllers>           <group>
*:firefox               cpu,memory              browser
*:chromium-browser      cpu,memory              browser
*:rstudio               memory                  rstudio_ide
*:teams                 cpu,memory              ms_teams

Systemd for automation

Creating files that will launch services on boot

cgconfigparser.service

sudo vim /etc/systemd/system/cgconfigparser.service
[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

cgrulesgend.service

sudo vim /etc/systemd/system/cgrulesgend.service
[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

Enabling services

sudo systemctl daemon-reload
sudo systemctl enable cgconfigparser
sudo systemctl enable cgrulesgend
sudo systemctl start cgconfigparser
sudo systemctl start cgrulesgend

Aliases to put into .bashrc

alias cg_start='sudo systemctl start cgrulesgend.service'
alias cg_stop='sudo systemctl stop cgrulesgend.service'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment