Skip to Content

How to use cgroup (control group) to limit CPU usage on Linux?

Control groups (cgroups) are a feature of the Linux kernel that allow you to allocate resources — such as CPU time, system memory, network bandwidth, or combinations of these resources — among user-defined groups of processes.

Setting up cgroups involves creating the cgroup, adding processes to it, and setting the desired limits. Here's a basic example of how you can do it:

1. Install the libcgroup-tools package. This package provides the cgcreate, cgexec, and other utilities that are useful for working with cgroups.

sudo yum install libcgroup-tools
2. Create a new cgroup. Let's call it limited_group. We'll create it in the cpu (for CPU resources) and cpuset (for specific CPUs or cores) subsystems.
sudo cgcreate -g cpu,cpuset:/limited_group
3. Set the CPU limit for the cgroup. Here, we'll set it to 50% of one CPU core, represented as 50000 (since the value is in units of "CPU microseconds per scheduler period").
echo 50000 | sudo tee /sys/fs/cgroup/cpu/limited_group/cpu.cfs_quota_us
4. Assign specific CPUs (or cores) to the cgroup. If you want the processes to only run on specific cores, you can set that here. The following command makes the cgroup's processes eligible to run on cores 0 and 1:
echo 0,1 | sudo tee /sys/fs/cgroup/cpuset/limited_group/cpuset.cpus
5. Add the processes to the cgroup. You'll need to know their process IDs (PIDs). Once you have those, you can add them to the cgroup like this:
echo [PID] | sudo tee /sys/fs/cgroup/cpu/limited_group/tasks echo [PID] | sudo tee /sys/fs/cgroup/cpuset/limited_group/tasks
Remember to replace [PID] with the actual PID of the elasticsearch and logstash processes. You can find them using the pgrep command:
pgrep -f elasticsearch pgrep -f logstash
Start new processes in the cgroup. If you want to start a new process and have it immediately be part of the cgroup, you can use cgexec like this:
sudo cgexec -g cpu,cpuset:limited_group /path/to/program
Remember that these settings will not persist across reboots. If you want to make them permanent, you'll need to add the necessary commands to a startup script, or use a daemon like cgconfig (part of libcgroup-tools) to manage the cgroups for you. Please refer to the cgconfig documentation or man pages for more information.
Also, be sure to carefully test the impact of these changes. Improper use of cgroups can have unintended side effects, including degraded system performance or instability.

Powered by PHPKB Knowledge Base Software