Pay-As-You-Go VPS – Only pay for what you use, with flexible billing and no long-term commitment

Limit CPU Usage with cgroups: Optimize Your System Now

July 30, 2025

 

 

Managing resources effectively is a critical aspect of system administration, especially when running multiple applications or services on a single server. One of the powerful tools available for this purpose in Linux environments is cgroups, or control groups. This kernel feature allows you to limit, prioritize, and monitor resource usage—such as CPU, memory, disk I/O, and network bandwidth—of processes. In this article, we’ll explore how to limit CPU usage using cgroups, providing a practical approach for system administrators and developers alike.

Understanding cgroups

Control groups are a fundamental part of the Linux kernel that enable the organization of processes into hierarchical groups. With cgroups, you can allocate resources, enforce limits, and track consumption for process groups. This means you can ensure that no single application monopolizes the CPU, leading to a more stable and fair resource distribution among running applications.

Setting Up cgroups

To utilize cgroups for limiting CPU usage, you first need to ensure that you have the necessary tools installed. On most Linux distributions, cgroup-tools is a package that can help manage cgroups. You can install it using your package manager. For example:

sudo apt-get install cgroup-tools

Once installed, you can start creating and managing cgroups.

Creating a cgroup

Creating a cgroup to limit CPU usage is straightforward. You’ll follow these general steps:

  1. Create a cgroup directory:
    You can create a new cgroup by making a directory under /sys/fs/cgroup/cpu/. For example:

    sudo mkdir /sys/fs/cgroup/cpu/mygroup
    
  2. Limit CPU usage:
    To restrict the CPU usage, you can adjust the cpu.cfs_quota_us parameter. This parameter is used to set CPU usage limits in microseconds. For instance, if you want to limit a process to use 50% of a CPU core, you would set it like this:

    echo 50000 | sudo tee /sys/fs/cgroup/cpu/mygroup/cpu.cfs_quota_us
    
  3. Assign processes to the cgroup:
    You can add a process to your cgroup by echoing its PID into the cgroup.procs file. For instance, if the PID of your process is 1234, you would run:

    echo 1234 | sudo tee /sys/fs/cgroup/cpu/mygroup/cgroup.procs
    

Monitoring Resource Usage

After setting limits, you might want to monitor the resource usage of your cgroup. You can check the cpu.stat file to get insights on how your cgroup is performing:

cat /sys/fs/cgroup/cpu/mygroup/cpu.stat

This file contains information about CPU times and can help identify if your limits are effective.

Practical Examples

To illustrate how to effectively use cgroups, consider a scenario where you are running a web server and a database server on the same machine. You can create separate cgroups for each service, allowing the web server to consume up to 80% of a CPU core while limiting the database to 20%. This ensures that your web server remains responsive even under high load. The commands would look something like this:

# For the web server group
sudo mkdir /sys/fs/cgroup/cpu/webserver
echo 80000 | sudo tee /sys/fs/cgroup/cpu/webserver/cpu.cfs_quota_us
echo <PID_webserver> | sudo tee /sys/fs/cgroup/cpu/webserver/cgroup.procs

For the database group

sudo mkdir /sys/fs/cgroup/cpu/database echo 20000 | sudo tee /sys/fs/cgroup/cpu/database/cpu.cfs_quota_us echo <PID_database> | sudo tee /sys/fs/cgroup/cpu/database/cgroup.procs

Advanced Configurations

For more refined control, cgroups allow advanced configurations such as setting CPU shares, which dictate the relative weight of CPU time allocated among groups. This can provide finer granularity than outright limiting CPU usage, and can be useful in mixed-load environments.

Conclusion

Using cgroups to limit CPU usage provides numerous benefits, from preventing resource hogging to ensuring system stability under heavy workloads. By setting up cgroups, administrators can efficiently manage processes, streamline resource allocation, and maintain optimal performance across applications. Embracing this powerful Linux feature can significantly enhance the management of server resources, making it an essential skill for anyone involved in system administration or development.

VirtVPS