Count number of allowed CPUs in a Docker container
You can use the nproc
shell script tool.
So it would be -j$(nproc)
in the make command line in question.
Related videos on Youtube
pmr
Updated on September 18, 2022Comments
-
pmr over 1 year
My specific scenario is the following. I launch a docker container with a specific cpuset:
docker run --cpuset-cpus="0-2" # ...
inside that container I run a shell script as the entry point and that shell script will run
make
at some point. I would like to figure out what a good number of jobs (-j
) would be. I could of course pass the the number of allocated CPUs through the environment, but an automatic way to detect it would be much preferred.I know I can use
taskset -c -p $$
orcat /proc/self/status | grep Cpus_allowed_list
to retrieve theCpus_allowed
for the current process, but I do not know how to retrieve the actual number of allowed CPUs. I would like to avoid parsing the output of those commands or fiddling with theCpus_allowed
mask, but will do it, when out of options.-
Brian about 9 years
nproc
- print the number of processing units available -
pmr about 9 years@Brian That was too easy.
-
-
Evan Benn over 5 years"docker run --rm --cpus 2 debian nproc" -> 8 ?