Introduction to cgroups
I’ve found that managing resources on Linux systems can be a real challenge, especially when dealing with resource-intensive desktop apps. That’s where cgroups come in - a powerful tool that allows you to allocate and manage system resources such as CPU, memory, and I/O for a group of processes. In my experience, cgroups can be a game-changer for keeping your system running smoothly.
Creating a cgroup
To get started with cgroups, you’ll need to create a new group using the cgcreate command. This command not only creates the group but also specifies the subsystems that will be controlled. For example, to create a cgroup that controls CPU and memory usage, you can use the following command:
cgcreate -g cpu,memory:/resource_intensive
This command creates a new cgroup called resource_intensive that controls CPU and memory usage. Don’t bother with trying to create a cgroup without specifying the subsystems - it just won’t work.
Setting Resource Limits
Once you’ve created a cgroup, you can set resource limits for the group. I usually start with setting the CPU usage limit, as this can have a big impact on system performance. To limit the CPU usage of the resource_intensive cgroup to 50% of the total available CPU, you can use the following command:
cgset -r cpu.shares=512 /resource_intensive
Similarly, to limit the memory usage of the resource_intensive cgroup to 2GB, you can use the following command:
cgset -r memory.limit_in_bytes=2147483648 /resource_intensive
The real trick is finding the right balance between resource limits and system performance. If you set the limits too low, your apps may not have enough resources to run smoothly. On the other hand, if you set them too high, you may end up with a system that’s bogged down by resource-intensive apps.
Moving Processes to a cgroup
To move a process to a cgroup, you can use the cgclassify command. For example, to move the Firefox process to the resource_intensive cgroup, you can use the following command:
cgclassify -g cpu,memory:/resource_intensive $(pidof firefox)
This command moves the Firefox process to the resource_intensive cgroup, which will then be subject to the resource limits set for that group. I’ve seen this go wrong when the pidof command returns multiple PIDs - so make sure you’re only targeting the process you want to move.
Using systemd to Manage cgroups
If you’re using a systemd-based system, you can also use systemd to manage cgroups. systemd provides a systemd-cgtop command that allows you to view and manage cgroups. You can also use the systemd-run command to run a command in a specific cgroup. For more information, see the systemd.io documentation. In practice, I’ve found that using systemd to manage cgroups can be a lot easier than using the cgcreate and cgset commands directly.
Troubleshooting
If you encounter issues with cgroups, you can check the kernel logs for errors. You can also use the cgget command to view the current configuration of a cgroup. For more information, see the docs.kernel.org documentation on cgroups. This is where people usually get burned - not checking the kernel logs for errors. Don’t make the same mistake - take the time to troubleshoot your cgroup issues properly.
See also
- Taming My Terminal History: Customizing Bash History Behavior to Reduce Clutter
- Taming the Beast: How I Finally Got My Laptop's Power Consumption Under Control with Linux
- Taming the Beast: My Favorite Aliases and Functions for Taming Long Commands in Bash
- Taming Background Tasks with Nohup and Systemd: A Homelab Lesson Learned
- When Linux Boots Slowly - Uncovering the Causes Behind My Desktop's 5-Minute Startup Time