The 4.19.91-18.al7 kernel version for Alibaba Cloud Linux 2 and the 5.10.60-9.al8 kernel version for Alibaba Cloud Linux 3 support the memcg out of memory (OOM) priority policy feature. This topic describes the interfaces provided for the memcg OOM priority policy feature and how to use these interfaces.

Background information

In existing kernel systems, if memory is insufficient, the kernel traverses cgroups and reclaims memory from the cgroups that consume large amounts of memory. If memory cannot be reclaimed from a cgroup, an OOM error is reported and all tasks in the cgroup are stopped. This affects critical business in the cgroup. To prevent memory occupied by cgroups that contain critical business from being reclaimed and ensure that tasks in these cgroups can continue to run so that the kernel can reclaim memory from other cgroups or trigger an OOM error, Alibaba Cloud Linux provides the memcg OOM priority policy feature.

When this feature is enabled, the kernel system determines the priorities of cgroups and triggers OOM errors on low-priority cgroups.

Interfaces

Interface Description
memory.use_priority_oom This interface is used to specify whether to enable the memcg OOM priority policy feature. Valid values: 0 and 1. The priority of a parent cgroup is not inherited by its child cgroups. Default value: 0.
  • A value of 0 indicates that the memcg OOM priority policy feature is disabled.
  • A value of 1 indicates that the memcg OOM priority policy feature is enabled.
memory.priority This interface provides 13 memcg OOM priorities to support business of different degrees of importance. Valid values: 0 to 12. A larger value indicates a higher priority. The priority of a parent cgroup is not inherited by its child cgroups. Default value: 0.
  • This interface is used to implement memcg quality of service (QoS). The value of priority is not a global variable and can only be compared between child cgroups under the same parent cgroup.
  • The kernel system sorts the child cgroups with the same priority by their memory usage and triggers an OOM error on the child cgroup that consumes the largest amount of memory.

Interface configuration example

For an Elastic Compute Service (ECS) instance, create six child cgroups named A, B, C, D, E, and F, as shown in the following hierarchy. Enable the memory.use_priority_oom interface and set OOM priorities by specifying the memory.priority interface.
                root
                /  \
               A    B
              / \  /  \
             C   D E   F
The following section describes the memory.priority interface values that are set for the child cgroups:
  • A: 10
  • B: 8
  • C: 5
  • D: 6
  • E: 7
  • F: 8
  1. Connect to the ECS instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. To create child cgroups named A and B and enable the memcg OOM priority policy feature, run the following commands:
    mkdir /sys/fs/cgroup/memory/A
    cd /sys/fs/cgroup/memory/A
    echo 1 >memory.use_priority_oom
    mkdir /sys/fs/cgroup/memory/B
    cd /sys/fs/cgroup/memory/B
    echo 1 >memory.use_priority_oom
  3. To set the OOM priority of A to 10 and the OOM priority of B to 8, run the following commands:
    cd /sys/fs/cgroup/memory/A
    echo 10 > memory.priority
    cd /sys/fs/cgroup/memory/B
    echo 8 > memory.priority
  4. To create child cgroups named C and D under A, create child cgroups named E and F under B, and enable the memcg OOM priority policy feature, run the following commands:
    mkdir /sys/fs/cgroup/memory/A/C
    cd /sys/fs/cgroup/memory/A/C
    echo 1 >memory.use_priority_oom
    mkdir /sys/fs/cgroup/memory/A/D
    cd /sys/fs/cgroup/memory/A/D
    echo 1 >memory.use_priority_oom
    mkdir /sys/fs/cgroup/memory/B/E
    cd /sys/fs/cgroup/memory/B/E
    echo 1 >memory.use_priority_oom
    mkdir /sys/fs/cgroup/memory/B/F
    cd /sys/fs/cgroup/memory/B/F
    echo 1 >memory.use_priority_oom
  5. To set the OOM priority of C to 5, the OOM priority of D to 6, the OOM priority of E to 7, and the OOM priority of F to 8, run the following commands:
    cd /sys/fs/cgroup/memory/A/C
    echo 5 > memory.priority
    cd /sys/fs/cgroup/memory/A/D
    echo 6 > memory.priority
    cd /sys/fs/cgroup/memory/B/E
    echo 7 > memory.priority
    cd /sys/fs/cgroup/memory/B/F
    echo 8 > memory.priority

To trigger an OOM error when the root account is used, the system starts an iterative query from A and B. The priority of B is lower than that of A, so the system continues to query E and F under B. The system determines that E has the lowest priority among the child cgroups, and triggers an OOM error on E.