从一本书上学到的,查看服务器CPU信息的工具
#!/bin/bash
#file: cpu-info.sh
#this script only works in a Linux system which has one or more identical physical CPU(s)
echo -n "logical CPU number in total:"
#逻辑CPU个数
cat /proc/cpuinfo | grep "processor" | wc -l
#
cat /proc/cpuinfo | grep -qi "core id"
if [ $? -ne 0 ]; then
echo "Warning. No multi-core or hyper-threading is enabled"
exit 0;
fi
echo -n "Physical CPU number in total: "
#物理CPU个数
cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
echo -n "core number in a physical CPU: "
#每个物理CPU上Core的个数(未计算超线程)
core_per_phy_cpu=$(cat /proc/cpuinfo | grep "core id" | sort | uniq | wc -l)
echo $core_per_phy_cpu
echo -n "logical CPU number in a physical CPU:"
#每个物理CPU中逻辑CPU(可能是core, threads或both)的个数
logical_cpu_per_phy_cpu=$(cat /proc/cpuinfo | grep "siblings" | sort | uniq | awk -F: '{print $2}')
echo $logical_cpu_per_phy_cpu
#是否打开有超线程
#如果在同一个物理CPU上两个逻辑CPU具有相同的"core id ",那么超线程是打开的
#此处根据前面的 core_per_phy_cpu 和 logical_cpu_per_phy_cpu 的比较来查看超线程
if [ $logical_cpu_per_phy_cpu -gt $core_per_phy_cpu ]; then
echo "Hyper threading is enabled"
elif [ $logical_cpu_per_phy_cpu -eq $core_per_phy_cpu ]; then
echo "Hyper threading is NOT enabled"
else
echo "Error, There is something wrong."
fi
运行结果:
另外还有一个查看CPU信息的地方
ls /sys/devices/system/cpu