Detect CPU count from Shell
Recently I upgraded my CPU to a Dual Core 2 Duo and am now running the x86_64 version of Arch Linux in which the makepkg tool can take advantage of the multiple core CPU and run make using multiple threads (great stuff). This is accomplished through the -jX option of the make utility (where X is the CPU count: 2 in my case). One can see the tremendous benefits of this when, for example, compiling the kernel (for me the compilation time dropped by almost 45%; I checked!).So, following that example I wanted to make my own makefiles use this trick, but automatically, so that the makefile itself would detect the number if CPU’s on the running system. Here’s what I got:
Command to get the CPU count on the running system as a simple number:
cat /proc/cpuinfo | grep processor | wc -l
Then, to use it inside a makefile:
... CPU_COUNT := $(shell cat /proc/cpuinfo | grep processor | wc -l) ... make -j$(CPU_COUNT) ... ...
Enjoyed this post?, why not subscribe to the RSS feed!
February 12th, 2008 at 3:17 pm
thats for the update
April 3rd, 2008 at 11:43 am
Or you can just use:
grep -c processor /proc/cpuinfo
April 3rd, 2008 at 2:35 pm
Yap! That’s actually better since it only uses one external tool (i.e. grep). Good job!
May 8th, 2008 at 7:20 am
[…] and thus running much faster (or “make -j4″ if you have 4 cores and so on… - see this post on how to get the CPU […]