Limiting process resource usage in AL2027 using cgroups
AL2027 Preview
AL2027 is currently available for preview. It is intended for evaluation and testing only and is not recommended for production workloads.
While it is recommended to use Resource control with systemd, this section covers basic usage of the base
libcgroup-tools utilities to limit CPU and memory usage of processes. Both methods are alternatives to
using the cpulimit utility.
The following example covers running the stress-ng stress test (from the stress-ng
package) while limiting its CPU and memory usage using utilities from the libcgroup-tools package, and
the tunables in sysfs.
Use libcgroup-tools on the command line to limit resource usage
-
Install the
libcgroup-toolspackage.sudo dnf installlibcgroup-tools -
Create a
cgroupwith thememoryandcpucontrollers, and give it a name (our-example-limits). Using the-aand-toptions to allow theec2-useruser to control the tunables of thecgroup.sudo cgcreate -aec2-user-tec2-user-g memory,cpu:our-example-limitsThere is now a
/sys/fs/cgroup/our-example-limits/directory that contains files that can be used to control each tunable. -
Limit memory usage of all processes in our
cgroupto 100 million bytes.echo100000000> /sys/fs/cgroup/our-example-limits/memory.max -
Limit CPU usage of all processes in our
cgroupto 10%. The format of thecpu.maxfile is$MAX $PERIOD, limiting the group to consuming$MAXfor every$PERIOD.echo10000 100000> /sys/fs/cgroup/our-example-limits/cpu.max -
The following example runs
stress-ng(which can be installed by runningdnf install -y stress-ng) in theour-example-limitscgroup. While thestress-ngcommand is running, you can observe usingtopthat it is limited to 10% of CPU time.sudo cgexec -g memory,cpu:our-example-limitsstress-ng --cpu 1 -
Clean up by removing the cgroup.
sudo cgdelete -g memory,cpu:our-example-limits
The Linux Kernel documentation
for cgroup-v2