Alibaba Cloud Linux 2 supports the cgroup writeback feature for the cgroup v1 kernel interface in the 4.19.36-12.al7 kernel version and later. This feature allows you to limit the buffered I/O rate when you use the cgroup v1 kernel interface.
Background information
cgroups (abbreviated from control groups) are a Linux kernel mechanism that organizes processes hierarchically and distributes system resources along the hierarchy in a controlled and configurable manner. Two versions of cgroups are available: cgroup v1 and cgroup v2. For more information, visit What are control groups. This topic describes how to enable the cgroup writeback feature for cgroup v1 to limit the buffered I/O rate of processes.
Limits
After you enable the cgroup writeback feature, check whether the mappings between memory subsystems (memcgs) and I/O subsystems (blkcgs) conform to the following rule. If the mappings conform to the rule, limit the buffered I/O rate of processes.
The cgroup writeback feature requires cooperation between memcgs and blkcgs to limit the buffered I/O rate. However, by default, the control subsystems of cgroup v1 do not work together. Memcgs and blkcgs must be associated based on the rule that each memcg is mapped to a unique blkcg. The mappings from memcgs to blkcgs can be one-to-one or many-to-one, but cannot be one-to-many or many-to-many.
- When A and B belong to two different memcgs, each of the memcgs can be mapped to a
unique blkcg. For example, A belongs to
memcg1
andblkcg1
, and B belongs tomemcg2
andblkcg0
. - When A and B belong to two different memcgs, these memcgs can also be mapped to the
same blkcg. For example, A belongs to
memcg1
and B belongs tomemcg2
, and both A and B belong toblkcg2
. - When A and B belong to the same memcg, the memcg can be mapped only to one blkcg.
For example, both A and B belong to
memcg0
andblkcg3
.
cgroup.procs
interface for the corresponding blkcg, and write the ID of the process to the interface
to ensure that the memcgs mapped to this blkcg are not mapped to any other blkcgs.
You can also use a tool to view the mappings between memcgs and blkcgs. For more information,
see Verify the mappings from memcgs to blkcgs.
Enable cgroup writeback
By default, the cgroup writeback feature is disabled for the cgroup v1 interface. To enable this feature, perform the following steps:
Verify the mappings from memcgs to blkcgs
Before you limit the buffered I/O rate of processes, you can use one of the following methods to check whether the mappings from memcgs to blkcgs are one-to-one or many-to-one.
- Run the following command to view the mappings from memcgs to blkcgs:
The following sample response shows that the mapping from the memcg to the blkcg is one-to-one.sudo cat /sys/kernel/debug/bdi/bdi_wb_link
memory <---> blkio memcg1: 35 <---> blkcg1: 48
- Use the ftrace kernel monitoring tool.
- Use the ftrace tool.
sudo bash -c "echo 1 > /sys/kernel/debug/tracing/events/writeback/insert_memcg_blkcg_link/enable"
- View the output information.
The following sample response containssudo cat /sys/kernel/debug/tracing/trace_pipe
memcg_ino=35 blkcg_ino=48
, which indicates that the mapping from the memcg to the blkcg is one-to-one.<... >-1537 [006] .... 99.511327: insert_memcg_blkcg_link: memcg_ino=35 blkcg_ino=48 old_blkcg_ino=0
- Use the ftrace tool.
Check whether cgroup writeback takes effect
In this example, two processes that generate I/O are simulated to check whether the cgroup writeback feature takes effect.
- The
dd
command responds quickly and the screen rolls too fast to be viewed. Run theiostat
command to view the dd command output. - The
dd
command writes data in sequence. The system performs sequential I/O refreshing every time 1 MB of output data is generated. Therefore, you must set the threshold forblkio.throttle.write_bps_device
to a value of no less than 1 MB (1,048,576 bytes). If you set the threshold for blkio.throttle.write_bps_device to a value of less than 1 MB, I/O hangs may occur.