15

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 $$ or cat /proc/self/status | grep Cpus_allowed_list to retrieve the Cpus_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 the Cpus_allowed mask, but will do it, when out of options.

2
  • 1
    nproc - print the number of processing units available
    – Brian
    Commented May 13, 2015 at 10:56
  • @Brian That was too easy.
    – pmr
    Commented May 13, 2015 at 11:18

1 Answer 1

22

You can use the nproc shell script tool.

So it would be -j$(nproc) in the make command line in question.

2
  • 1
    "docker run --rm --cpus 2 debian nproc" -> 8 ?
    – Evan Benn
    Commented Sep 18, 2018 at 2:28
  • 3
    @EvanBenn the --cpus="2" doesn't limit the number of processing units and is equivalent to -cpu-period="100000" and --cpu-quota="200000".
    – Brian
    Commented Jan 29, 2019 at 23:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .