1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2026-10-24 00:48:56 +03:00
Files
gnulib/tests/test-nproc.c
Pádraig Brady 9b07115f4a nproc: honor cgroupv2 CPU quotas
cgroupv1 CPU quotas are not considered,
as those are now legacy (RHEL7 era),
and are more complex/inefficient to parse.

Tested in coreutils on Fedora 42
as detailed in tests/test-nproc.c

* lib/nproc.c (cgroup2_mount): Identify the cgroup2 mount point,
first at the common location for efficiency,
resorting to searching mount points otherwise.
(get_cgroup2_cpu_quota): Walk cgroup2 quotas for the current process,
returning the lowest integer number of CPUs configured.
(cpu_quota): On Linux return the cgroupv2 CPU quota if the
currrent scheduler honors it.  Otherwise return ULONG_MAX.
(num_processors): Clamp the return to <= quota.
* m4/nproc.m4: Add a check for mntent.h.
* tests/test-nproc.c: Document how cgroup CPU quotas were tested.
2025-08-19 16:33:49 +01:00

66 lines
1.7 KiB
C

/*
nproc honors cgroup v2 CPU quotas
and was tested in coreutils on a Fedora 42 system as follows:
# Note we honor a limit anywhere in /proc/self/cgroup hierarchy
# so apply settings below in the parent cgroup of the current process
$ nested_cgroup=/sys/fs/cgroup/$(dirname $(cut -d/ -f2- /proc/self/cgroup))
$ echo $nested_cgroup
/sys/fs/cgroup/user.slice/user-1001.slice/user@1001.service/app.slice
# This test system has 4 CPUs
$ src/nproc
4
# Behave like MAX (1, (int)round(quota/period))
$ echo "100000 100000" > $nested_cgroup/cpu.max
$ src/nproc
1
$ echo "90000 100000" > $nested_cgroup/cpu.max
$ src/nproc
1
$ echo "140000 100000" > $nested_cgroup/cpu.max
$ src/nproc
1
$ echo "150000 100000" > $nested_cgroup/cpu.max
$ src/nproc
2
# Ensure NPROC_ALL takes precedence
$ echo "100000 100000" > $nested_cgroup/cpu.max
$ src/nproc --all
4
# Ensure OMP env vars have appropriate precedence
$ echo "200000 100000" > $nested_cgroup/cpu.max
$ OMP_NUM_THREADS=10 src/nproc
10
$ OMP_THREAD_LIMIT=10 src/nproc
2
# Ensure quota only reduces
$ echo "500000 100000" > $nested_cgroup/cpu.max
$ src/nproc
4
# Restore system to unlimited
$ echo "max 100000" > $nested_cgroup/cpu.max
# Test quota in root hierarchy
$ podman run --cpus=2 -i --rm fedora:latest /tmp/nproc
2
$ podman run --cpus=1.5 -i --rm fedora:latest /tmp/nproc
2
$ podman run --cpus=1.4 -i --rm fedora:latest /tmp/nproc
1
$ podman run --cpus=100 -i --rm fedora:latest /tmp/nproc
4
# Docker is similar to podman, but explicitly limits max allowable
$ docker run --cpus=1.4 -i --rm fedora:latest /tmp/nproc
1
$ docker run --cpus=100 -i --rm fedora:latest /tmp/nproc
docker: Error response from daemon:
range of CPUs is from 0.01 to 4.00, as there are only 4 CPUs
*/