Skip to content

nproc doesn't report correct number of usable CPU cores in Docker / CGroups #108

Closed

Description

@Paebbels

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.

Anyhow.
The problem can be reproduced like this:

echo "CPUs: $(nproc)"
24
docker container run --rm -it --cpus=6 alpine:latest echo "CPUs: $(nproc)"
24

Actually, the parameter --cpus=6 doesn't set the number of available CPUs, but a scheduler ration:

docker container run --rm -it --cpus=6 alpine:latest cat /sys/fs/cgroup/cpu.max
600000 100000

--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".

Source: https://docs.docker.com/engine/containers/resource_constraints/#configure-the-default-cfs-scheduler

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.

My current workaround is this Bash script:

mapfile -td " " scheduler < /sys/fs/cgroup/cpu.max
#    ceil(cpu-quota / cpu-period)
echo "$(((${scheduler[0]}+${scheduler[1]}-1) / ${scheduler[1]}))"

/cc @skoehler

Activity

  1. collinfunk commented on Jul 17, 2025

    @collinfunk
    Member

    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].

    [1] https://savannah.gnu.org/projects/gnulib
    [2] https://github.com/coreutils/gnulib

  2. Paebbels commented on Jul 18, 2025

    @Paebbels
    Author

    Checking these code lines:

    /* 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.  */
    

    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 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?

  3. collinfunk commented on Jul 19, 2025

    @collinfunk
    Member

    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!

  4. collinfunk commented on Aug 3, 2025

    @collinfunk
    Member

    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

  5. Paebbels commented on Aug 3, 2025

    @Paebbels
    Author

    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/meminfo
    • free
    • ...

    Any 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/procps

  6. collinfunk commented on Aug 4, 2025

    @collinfunk
    Member

    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.

    Any 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/procps

    I don't know the full history here, but my Fedora machine and a Debian container I see (with differing versions):

    $ free --version
    free from procps-ng 4.0.4
    

    So the gitlab link you sent seems correct.

  7. Paebbels commented on Aug 4, 2025

    @Paebbels
    Author
  8. pixelb commented on Aug 19, 2025

    @pixelb
    Member

    Closing this, since nproc now honors cgroups quotas: coreutils/gnulib@9b07115f4a

  9. Paebbels commented on Aug 19, 2025

    @Paebbels
    Author

    Thanks for the quick fix.


    Now we need to convince procps-ng of a similar fix for free :).

  10. oech3 commented on Dec 24, 2025

    @oech3
    Contributor

    Why is nproc under 399% quota 3 instead of 4?

  11. pixelb commented on Dec 24, 2025

    @pixelb
    Member

    Why is nproc under 399% quota 3 instead of 4?

    How are you reproducing that? On my system:

    $ src/nproc
    4
    $ nested_cgroup=/sys/fs/cgroup/$(dirname $(cut -d/ -f2- /proc/self/cgroup))
    $ echo "399000 100000" > $nested_cgroup/cpu.max
    $ src/nproc
    4
    $ echo "349000 100000" > $nested_cgroup/cpu.max
    $ src/nproc
    3
    

    The code does MAX (1, (int)round(quota/period)), so perhaps what ever is setting the % is not setting cpu.max appropriately

  12. oech3 commented on Dec 24, 2025

    @oech3
    Contributor

    Oh... Sorry. Your nproc is correct:

    $ systemd-run --user --scope -q --property="CPUQuota=399%" uu-nproc
    3
    $ systemd-run --user --scope -q --property="CPUQuota=399%" gnu-nproc
    4
    

    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.

  13. pixelb commented on Dec 24, 2025

    @pixelb
    Member

    Note we do test for that rounding behavior on multicore systems:

    if test $ncpus -gt 1; then
    echo '150000 100000' > $ROOT/sys/fs/cgroup/cpu.max &&
    test $(NPROC) -eq 2 || fail=1
    echo '150000 100000' > $ROOT/sys/fs/cgroup/cpu.max &&
    test $(OMP_THREAD_LIMIT=10 NPROC) -eq 2 || fail=1
    fi

  14. oech3 commented on Dec 24, 2025

    @oech3
  15. oech3 commented on Mar 2, 2026

    @oech3
    Contributor

    Is it able to get quota under masked /proc or /sys?

  16. pixelb commented on Mar 2, 2026

    @pixelb
    Member

    Is it able to get quota under masked /proc or /sys?

    No. It won't fail, but will ignore any quotaas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions