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