用Shell脚本获取CPU拓扑关系

来源:Linuxeden 作者:zltjiangshi
  准确说这不是一个问题,因为地球人都知道cat /proc/cpuinfo获取CPU信息。但是,你能从那一大片输出里面立马看出来是多个物理CPU呢,还是多核呢,还是超线程之类的东东么?

本文给出一个脚本,通过分析proc来给出一个清晰的答案,本人写的脚本通用性可能比较差,大家根据情况进行修改,基本上在偶这里没啥问题。

先说点基础:
/proc/cpuinfo里面:
processor表示逻辑CPU号(核或线程)
core id表示CPU核号
physical id表示物理CPU号

脚本如下:
cpu_p=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l`
cpu_c=`cat /proc/cpuinfo | grep "core id" | uniq | wc -l`
cpu_l=`cat /proc/cpuinfo | grep "processor" | wc -l`

c_p=`expr $cpu_c / $cpu_p`
l_p=`expr $cpu_l / $cpu_p`
t_c=`expr $cpu_l / $cpu_c`

echo "Physical CPU Count: $cpu_p"
echo "CPU Core Count   : $cpu_c"
echo "Logical CPU Count: $cpu_l"

echo "$c_p cores per physical CPU"
echo "$l_p logical CPUs per physical CPU"
echo "$t_c threads per core"

if [ $c_p -eq 1 ]; then
  echo "Your CPU is unicore"
else
  echo "Your CPU is multicore"
fi

if [ $t_c -eq 1 ]; then
  echo "Your CPU isn\'t of HT"
else
  echo "Your CPU is of HT"
fi

echo "You have $cpu_p CPUs, each CPU has $c_p cores, each core has $t_c threads. In summary $cpu_c cores, $cpu_l threads."


下面是在俺这里的运行结果:

时间:2009-07-29 20:39 来源:Linuxeden 作者:zltjiangshi 原文链接

好文,顶一下
(173)
99.4%
文章真差,踩一下
(1)
0.6%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量