Alibaba Cloud Linux 2, starting from kernel version 4.19.91-18.al7, and Alibaba Cloud Linux 3, starting from kernel version 5.10.60-9.al8, support the memory subsystem (memcg) out-of-memory (OOM) priority policy feature. This topic describes the interfaces for the memcg OOM priority policy feature and provides a configuration example.
Background information
In existing kernel systems, when memory pressure is high, the kernel traverses cgroups to reclaim memory from the ones that consume the most. If reclamation fails, the kernel may trigger an out-of-memory (OOM) event and terminate all tasks in that cgroup. This can severely impact critical services running in the cgroup. Therefore, it is crucial to protect the memory of cgroups that contain critical services from being reclaimed, or to ensure that the tasks within these cgroups can survive OOM events. This allows the kernel to select other cgroups for memory reclamation or OOM killing.
To solve this problem, Alibaba Cloud Linux includes a memcg out-of-memory (OOM) priority configuration feature. During an OOM operation, this feature determines cgroup priorities and selects a low-priority cgroup for the operation.
Functionality of the memcg OOM priority interface
Interface | Description |
| Specifies whether to enable the memcg OOM priority policy feature. Valid values are 0 and 1. This setting is not inherited by child cgroups. The default value is 0.
|
| Sets the memcg priority to support business of varying importance. There are 13 priority levels. Valid values range from 0 to 12. A larger value indicates a higher priority. This setting is not inherited by child cgroups. The default value is 0.
|
Interface configuration example
This example shows how to create six cgroup child nodes: A, B, C, D, E, and F. You can enable the memory.use_priority_oom interface and set the OOM priorities using the memory.priority interface.
root
/ \
A B
/ \ / \
C D E FThe interface values for each level are mapped as follows:
A: 10
B: 8
C: 5
D: 6
E: 7
F: 8
You can remotely connect to the ECS instance.
For more information, see Connect to a Linux instance using Workbench.
You can run the following commands to create two cgroup nodes, A and B, and enable the memcg OOM priority policy feature.
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_oomYou can run the following commands to set the memcg OOM priority for node A to 10 and for node B to 8.
cd /sys/fs/cgroup/memory/A echo 10 > memory.priority cd /sys/fs/cgroup/memory/B echo 8 > memory.priorityYou can run the following commands to create child nodes C and D under node A, create child nodes E and F under node B, and enable the memcg OOM priority policy feature.
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_oomYou can run the following commands to set the memcg OOM priorities for child nodes C, D, E, and F to 5, 6, 7, and 8 respectively.
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.