writing a simple script to get total number of cpus/cores in server

Hi,

I am very new to scripting and I wanted to write a unix shell script which can give me,

1)number of cpu's in a box

2)number of cores per cpu

3)total number of cores in abox (ie multiplying 1&2)

I am also trying to figure out how to check if hyper-threading is enabled in the server and also this check to script.

intially I am looking this to run on linux then wanted to implement it on other flavours of unix.

below is my script..

#!/bin/sh
if [ ! -r /proc/cpuinfo ]; then
  echo "Is this Linux? Cannot find or read /proc/cpuinfo"
  exit 1
fi
echo num_cpus = `grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
read num_cpu
echo num_cores_per_cpu=`grep 'core id' /proc/cpuinfo | sort -u | wc -l`
read num_cores_per_cpu
echo total_cores=`expr $num_cpus \\* num_cores_per_cpu` --this part is not working..

if [ $num_cores -eq 0 ]; then
# this box is either an old SMP or single-CPU box, so count the # of processors

  echo num_cores=`grep '^processor' /proc/cpuinfo | sort -u | wc -l`

fi
#echo $num_cores_per_cpu

any help is much appreciated.

thanks,
Steve.

Each unix flavor will require its own section within the script...not to mention different versions of the same unix flavor may use different commands to get the information that you are looking for...and /proc/cpuinfo is found only on linux. You need to learn some shell scripting before trying to write this sort of thing...

echo num_cpus = `grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
read num_cpu

should really be...

num_cpus=`grep 'physical id' /proc/cpuinfo | sort -u | wc -l`
echo $num_cpus

Hi,

Thanks for your reply.

yeah, I know that different unix flavors requires different commands but I want to start with linux.

what I am trying to do is display number of cpu and cores for each cpu and multiplying both to get total number of cores in the server.

when i change it to echo $num_cpus then where should i use the read command.

thanks,
steve.

Why bother? Each core shows up individually whether it's in a separate processor or not, just count them.

awk '/^processor/ { N++} END { print N }' /proc/cpuinfo

Besides, how do you know each CPU has the same number of cores?

To extract both:

awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^physical/ { N++ };  END { print N, P }' /proc/cpuinfo

Thanks corona,

then how do we know if Hyperthreading is enabled or not and how doo we check that?

Thanks,
Steve.

I don't have a linux computer with hyperthreading to check right now, but that also shows up in /proc/cpuinfo.

Hi Corona,

 
awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^physical/ { N++ };  END { print N, P }' /proc/cpuinfo
 

thanks for the above script. It shows the right information but how can we add comments to it so that we can understand what it is displaying?

eg output of above script.

16 4 -->script displays as this but how we add comments to this such that it will show

total cores =16 and cpus =4

thanks,
steve.

 awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^physical/ { N++ };  END { print "total cores = "N " and cpus = " P }' /proc/cpuinfo

Check for hyperthreading: look at the "flags" in /proc/cpuinfo and search for 'ht' flag:

if grep -q '^flags.* ht ' /proc/cpuinfo ; then 
   echo "HT supported"
fi

This will check if at least one processor has HT, but I think it is safe to assume if one has HT, all do. This, however only tells you that the processors are capable of hyperthreading, to really utilize it, the kernel has to be HT capable also. If the name of the Linux kernel has 'SMP', it is HT capable. So better check yet for linux:

if uname -a | grep -q SMP && grep -q '^flags.* ht ' /proc/cpuinfo ; then 
   echo "HT capable"
fi

To know if hyperthreading is enabled look for "ht" in the flags field of /proc/cpuinfo and the "siblings" field for each core shows how many contexts are available on a given core...

grep ht /proc/cpuinfo && grep siblings /proc/cpuinfo