歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> SHELL編程

用Shell腳本獲取CPU拓撲關系

准確說這不是一個問題,因為地球人都知道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."

下面是在俺這裡的運行結果:
用Shell腳本獲取CPU拓撲關系 

Copyright © Linux教程網 All Rights Reserved