Managing cgroups v2 Using sysfs

This section shows you how to create and configure cgroups to manage the distribution of resources amongst processes running on the system by using the sysfs interface.

Important:

We recommend that you use systemd to handle all resource management. See Oracle Linux 8: Managing the System With systemd for more information. The examples provided here provide the context for actions that systemd performs on a system and show the functionality outside of systemd. The information provided can be helpful when debugging issues with cgroups.

The example procedure involves allocating CPU time between cgroups that each have different application PIDs assigned to them. The CPU time and application PID values are set in each group's cpu.weight and cgroup.procs files.

The example also includes the steps required to ensure the cpu controller and its associated files, including the cpu.weight file, are available in the cgroups you need to create under /sys/fs/cgroup.

Preparing the Control Group for Distribution of CPU Time

This procedure describes how to manually prepare a control group to manage the distribution of CPU time. Note that the recommended approach to configuring control groups is to use systemd.

  1. Verify that the cpu controller is available at the top of the hierarchy, in the root control group.

    Printing the contents of the /sys/fs/cgroup/cgroup.controllers file on the screen:

    sudo cat /sys/fs/cgroup/cgroup.controllers
    cpuset cpu io memory hugetlb pids rdma misc

    You can add any controllers listed in the cgroup.controllers file to the cgroup.subtree_control file in the same directory to make them available to the group's immediate child cgroups.

  2. Add the cpu controller to the cgroup.subtree_control file to make it available to immediate child cgroups of the root.

    By default, only the memory and pids controllers are in the file. To add the cpu controller, type:

    echo "+cpu" | sudo tee /sys/fs/cgroup/cgroup.subtree_control
    
  3. Optionally, verify that the cpu controller has been added as expected.
    sudo cat /sys/fs/cgroup/cgroup.subtree_control
    cpu memory pids
  4. Create a child group under the root control group to become the new control group for managing CPU resources on applications.
    sudo mkdir /sys/fs/cgroup/MyGroups
  5. Optionally, list the contents of the new subdirectory, or child group, and confirm that the cpu controller is present as expected.
    ls -l /sys/fs/cgroup/MyGroups
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 cgroup.controllers
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 cgroup.events
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cgroup.freeze
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cgroup.max.depth
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cgroup.max.descendants
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cgroup.procs
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 cgroup.stat
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cgroup.subtree_control
    …​
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 cpu.stat
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cpu.weight
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 cpu.weight.nice
    …​
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 memory.events.local
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 memory.high
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 memory.low
    …​
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 pids.current
    -r—​r—​r--. 1 root root 0 Jun  1 10:33 pids.events
    -rw-r—​r--. 1 root root 0 Jun  1 10:33 pids.max
  6. Enable the cpu controller in cgroup.subtree_control file in the MyGroups directory to make it available to its immediate child cgroups.
    echo "+cpu" | sudo tee /sys/fs/cgroup/MyGroups/cgroup.subtree_control
  7. Optionally, verify that the cpu controller is enabled for child groups under MyGroups.
    sudo cat /sys/fs/cgroup/MyGroups/cgroup.subtree_control
    cpu

Setting CPU Weight to Regulate Distribution of CPU Time

This procedure describes how to set CPU weight for three different processes by using a control group to manage the distribution of CPU time. Note that the recommended approach to configuring control groups is to use systemd.

This procedure is based on the following assumptions:

  • The application that's consuming CPU resources excessively is sha1sum, as shown in the following sample output of the top command:

    sudo top
    ...
    PID   USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    33301 root      20   0   18720   1756   1468 R  99.0   0.0   0:31.09 sha1sum
    33302 root      20   0   18720   1772   1480 R  99.0   0.0   0:30.54 sha1sum
    33303 root      20   0   18720   1772   1480 R  99.0   0.0   0:30.54 sha1sum
    1 root      20   0  109724  17196  11032 S   0.0   0.1   0:03.28 systemd                     
    2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd                    
    3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                      
    4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                  
              ...
  • The sha1sum processes have PIDs 33301, 33302, and 33303, as listed in the preceding sample output.

Important:

As a prerequisite to the following procedure, you must complete the preparations of cgroup-v2 as described in Preparing the Control Group for Distribution of CPU Time. If you skipped those preparations, you can't complete this procedure.

  1. Create 3 child groups in the MyGroups subdirectory.
    sudo mkdir /sys/fs/cgroup/MyGroups/g1
    sudo mkdir /sys/fs/cgroup/MyGroups/g2
    sudo mkdir /sys/fs/cgroup/MyGroups/g3
  2. Configure the CPU weight for each child group.
    echo "150" | sudo tee /sys/fs/cgroup/MyGroups/g1/cpu.weight
    echo "100" | sudo tee /sys/fs/cgroup/MyGroups/g2/cpu.weight
    echo "50" | sudo tee /sys/fs/cgroup/MyGroups/g3/cpu.weight
  3. Apply the application PIDs to their corresponding child groups.
    echo "33301" | sudo tee /sys/fs/cgroup/Example/g1/cgroup.procs
    echo "33302" | sudo tee /sys/fs/cgroup/Example/g2/cgroup.procs
    echo "33303" | sudo /sys/fs/cgroup/Example/g3/cgroup.procs

    These commands set the selected applications to become members of the MyGroups/g*/ control groups. The CPU time for each sha1sum process depends on the CPU time distribution as configured for each group.

    The weights of the g1, g2, and g3 groups that have running processes are summed up at the level of MyGroups, which is the parent control group.

    With this configuration, when all processes run at the same time, the kernel allocates to each of the sha1sum processes the proportionate CPU time based on their respective cgroup's cpu.weight file, as follows:

    Child group cpu.weight setting Percent of CPU time allocation
    g1 150 ~50% (150/300)
    g2 100 ~33% (100/300)
    g3 50 ~16% (50/300)

    If one child group has no running processes, then the CPU time allocation for running processes is recalculated based on the total weight of the remaining child groups with running processes. For example, if the g2 child group doesn't have any running processes, then the total weight becomes 200, which is the weight of g1+g3. In this case, the CPU time for g1 becomes 150/200 (~75%) and for g3, 50/200 (~25%)

  4. Check that the applications are running in the specified control groups.
    sudo cat /proc/33301/cgroup /proc/33302/cgroup /proc/33303/cgroup
    0::/MyGroups/g1
    0::/MyGroups/g2
    0::/MyGroups/g3
  5. Check the current CPU consumption after you have set the CPU weights.
    top
    ...
    PID   USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    33301 root      20   0   18720   1748   1460 R  49.5   0.0 415:05.87 sha1sum
    33302 root      20   0   18720   1756   1464 R  32.9   0.0 412:58.33 sha1sum
    33303 root      20   0   18720   1860   1568 R  16.3   0.0 411:03.12 sha1sum
    760 root      20   0  416620  28540  15296 S   0.3   0.7   0:10.23 tuned
    1 root      20   0  186328  14108   9484 S   0.0   0.4   0:02.00 systemd
    2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthread
    ...