cgroup ubuntu 18.04 howto

Install required packages

apt install cgroup-tools

copy cgred.conf from examples

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

/etc/cgconfig.conf

group web2 {
cpu {
cpu.cfs_quota_us=10000;
}
memory {
memory.limit_in_bytes = 1024m;
}
}

cpu.cfs_quota_us = 10000 equals to 10% cpu usage
memory.limit_in_bytes = 1024 equals to 1G of system memory


/etc/cgrules.conf

#<user>    <controllers>           <destination>
web2 cpu,memory web2

This will limit every process of the user web2 to 10% CPU and 1G of memory.


For testing use this commands:

/usr/sbin/cgconfigparser -l /etc/cgconfig.conf
/usr/sbin/cgrulesengd -vvv

check if cgroup’s are working properly

cat /sys/fs/cgroup/cpu/web2/tasks
cat /sys/fs/cgroup/memory/web2/tasks

Here my systemd service files, install them the usual systemd way

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

[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

reload systemd and start services

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

Have fun!

ubuntu cgroup howto

Hi

Need to limit php-cgi procs?
Here some nice howto:

aptitude install cgroup-bin
service cgconfig stop

here the content of my /etc/cgconfig.conf

#mounts the cgroupfilesystems
mount {
   cpu = /sys/fs/cgroup/cpu;
   cpuset = /sys/fs/cgroup/cpuset;
   memory = /sys/fs/cgroup/memory;
}

#defines a group trasuser which is pinned to cpu 3 of the quadcore system
group trashuser {
   cpuset {
      cpuset.mems = 0;
      cpuset.cpus = 3;
   }
}

#defines the group defaultphp where all php-cgi processes get 75% of the quadcore cpu and 3GB Ram
group defaultphp {
   cpu {
      cpu.shares = 750;
   }
   memory {
      memory.limit_in_bytes = 3G;
   }
}

here the content of my cgrules.conf

#the unix user webuser01 gets pinned in group trashuser
webuser01        cpuset        trashuser/
#pinnes all php-cgi procs to group defaultphp
*:php-cgi    cpu,memory    defaultphp/
#pinnes all other procs to the default profile
*        *        default/
service cgconfig start

Here a nice link with resources

Hint:
If “service cgconfig restart” not works try to unmount all mounted cgroupfilesystems. You will find them under “cat /proc/mounts”
After that it should start.

Have Fun!