You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
nproc doesn't report correct number of usable CPU cores in Docker / CGroups #108
nproc is used and recommended to figure out how many CPU cores are available and how algorithms, scripts or tools can scale on a given platform.
A common usage is make -j$(nproc).
Unfortunately, this number is not correct if executed within a Docker (or more in general: in a cgroups limited environment). Docker does not adjust the data source were nproc gets its data from. So far I couldn't figure out how nproc gets its information, because nproc.h is not part of the coreutils repository.
--cpus=<value>
Specify how much of the available CPU resources a container can use. For instance, if the host machine has two CPUs and you set --cpus="1.5", the container is guaranteed at most one and a half of the CPUs. This is the equivalent of setting --cpu-period="100000" and --cpu-quota="150000".
Obviously, not all tools use nproc to get the CPU count (or better: scaling factor), but many scripts could be helped if nproc could report the "cpu count" even in resource limited Docker containers.
My proposal:
If nproc is executed within a cgroups environment, report the rounded up result of dividing the scheduler ration reported by /sys/fs/cgroup/cpu.max.
I would have to look into this a bit more to know what the correct solution is. But you said:
So far I couldn't figure out how nproc gets its information, because nproc.h is not part of the coreutils repository.
The nproc.{c,h} files, among others used by Coreutils, are provided by Gnulib. You can see the sources on Savannah [1] or mirrored under the same organization as this one [2].
/* Return the total number of processors. Here QUERY must be one of
NPROC_ALL, NPROC_CURRENT. The result is guaranteed to be at least 1. */
static unsigned long int
num_processors_ignoring_omp (enum nproc_query query)
{
/* On systems with a modern affinity mask system call, we have
sysconf (_SC_NPROCESSORS_CONF)
>= sysconf (_SC_NPROCESSORS_ONLN)
>= num_processors_via_affinity_mask ()
The first number is the number of CPUs configured in the system.
The second number is the number of CPUs available to the scheduler.
The third number is the number of CPUs available to the current process.
Note! On Linux systems with glibc, the first and second number come from
the /sys and /proc file systems (see
glibc/sysdeps/unix/sysv/linux/getsysstats.c).
In some situations these file systems are not mounted, and the sysconf call
returns 1 or 2 (<https://sourceware.org/bugzilla/show_bug.cgi?id=21542>),
which does not reflect the reality. */
Especially this one: The second number is the number of CPUs available to the scheduler.
Does it mean, the nproc program, which uses nproc.c from gnulib should already use information from scheduler configuration? If so, does it mean it accesses wrong information inside of Docker, or does Docker not modify the /sys and /proc filesystems as needed?
Does it mean, the nproc program, which uses nproc.c from gnulib should already use information from scheduler configuration? If so, does it mean it accesses wrong information inside of Docker, or does Docker not modify the /sys and /proc filesystems as needed?
On GNU/Linux calling nproc should just use sched_getaffinity, unless I am remembering incorrectly.
I was able to see what you mean using a docker container, though. So I guess sched_getaffinity doesn't care about cgroups.
Can you share your proposal on [email protected]? That will get more attention than this mirror gets. You can just copy-paste your original message with the steps to reproduce. Thanks!
FYI, we are discussing implementing this in Gnulib (then we can discuss implementing it in Coreutils). I figured I would link you the mailing list in case you want to provide any input there [1].
Thanks for forwarding the problem. I couldn't make time to register to GNU and it's mailing list(s). Also thanks for providing the link for traceability.
Meanwhile I did some further tests and I think a similar problem exists for the memory limits. All programs always see the full amount of system memory in:
Thanks for forwarding the problem. I couldn't make time to register to GNU and it's mailing list(s). Also thanks for providing the link for traceability.
No problem. If you feel the need to post in the future you don't need to subscribe to the list.
Meanwhile I did some further tests and I think a similar problem exists for the memory limits. > All programs always see the full amount of system memory in:
/proc/meminfo
free
...
Coreutils uses physmem from Gnulib in wc and sort to guess a sane buffer size. I don't think it should be too much of a problem here.
Activity
collinfunk commentedon Jul 17, 2025
I would have to look into this a bit more to know what the correct solution is. But you said:
The
nproc.{c,h}files, among others used by Coreutils, are provided by Gnulib. You can see the sources on Savannah [1] or mirrored under the same organization as this one [2].[1] https://savannah.gnu.org/projects/gnulib
[2] https://github.com/coreutils/gnulib
Paebbels commentedon Jul 18, 2025
Checking these code lines:
Source: https://github.com/coreutils/gnulib/blob/master/lib/nproc.c#L244
Especially this one:
The second number is the number of CPUs available to the scheduler.Does it mean, the
nprocprogram, which usesnproc.cfrom gnulib should already use information from scheduler configuration? If so, does it mean it accesses wrong information inside of Docker, or does Docker not modify the/sysand/procfilesystems as needed?collinfunk commentedon Jul 19, 2025
On GNU/Linux calling
nprocshould just usesched_getaffinity, unless I am remembering incorrectly.I was able to see what you mean using a docker container, though. So I guess
sched_getaffinitydoesn't care about cgroups.Can you share your proposal on [email protected]? That will get more attention than this mirror gets. You can just copy-paste your original message with the steps to reproduce. Thanks!
collinfunk commentedon Aug 3, 2025
FYI, we are discussing implementing this in Gnulib (then we can discuss implementing it in Coreutils). I figured I would link you the mailing list in case you want to provide any input there [1].
Thanks again for reporting this.
[1] https://lists.gnu.org/archive/html/bug-gnulib/2025-08/msg00000.html
Paebbels commentedon Aug 3, 2025
Thanks for forwarding the problem. I couldn't make time to register to GNU and it's mailing list(s). Also thanks for providing the link for traceability.
Meanwhile I did some further tests and I think a similar problem exists for the memory limits. All programs always see the full amount of system memory in:
/proc/meminfofreeAny idea were to report this one?
My research brought me to a dead SourceForge project: https://sourceforge.net/projects/procps/ No updates since 12 years. And a dead mirror on GitHub: https://github.com/mmalecki/procps
Arch Linux refers to
procps-ng, which looks to be hosted here: https://gitlab.com/procps-ng/procpscollinfunk commentedon Aug 4, 2025
No problem. If you feel the need to post in the future you don't need to subscribe to the list.
Coreutils uses
physmemfrom Gnulib inwcandsortto guess a sane buffer size. I don't think it should be too much of a problem here.I don't know the full history here, but my Fedora machine and a Debian container I see (with differing versions):
So the gitlab link you sent seems correct.
Paebbels commentedon Aug 4, 2025
I opened an issue: https://gitlab.com/procps-ng/procps/-/issues/389 and asked if
freecould report memory limits.pixelb commentedon Aug 19, 2025
Closing this, since nproc now honors cgroups quotas: coreutils/gnulib@9b07115f4a
Paebbels commentedon Aug 19, 2025
Thanks for the quick fix.
Now we need to convince procps-ng of a similar fix for
free:).oech3 commentedon Dec 24, 2025
Why is
nprocunder 399% quota 3 instead of 4?pixelb commentedon Dec 24, 2025
How are you reproducing that? On my system:
The code does
MAX (1, (int)round(quota/period)), so perhaps what ever is setting the % is not setting cpu.max appropriatelyoech3 commentedon Dec 24, 2025
Oh... Sorry. Your
nprocis correct:This is prefer more than definition, But people sometimes prefer
make -j@@NOM_OF_CORE+1@@to mask cost of producing new process. I probably prefer 4.pixelb commentedon Dec 24, 2025
Note we do test for that rounding behavior on multicore systems:
coreutils/tests/nproc/nproc-quota.sh
Lines 140 to 145 in 95044cb
oech3 commentedon Dec 24, 2025
oech3 commentedon Mar 2, 2026
Is it able to get quota under masked
/procor/sys?pixelb commentedon Mar 2, 2026
No. It won't fail, but will ignore any quotaas