Linux CPU数量判断,经过/proc/cpuinfo

其实只要
html

#include <unistd.h>
long num = sysconf(_SC_NPROCESSORS_ONLN);linux

即可以得到当前CPU的数量。。。
api

判断依据:
1.具备相同core id的cpu是同一个core的超线程。
2.具备相同physical id的cpu是同一颗cpu封装的线程或者cores。缓存

英文版:
1.Physical id and core id are not necessarily consecutive but they are unique. Any cpu with the same core id are hyperthreads in the same core.
2.Any cpu with the same physical id are threads or cores in the same physical socket.socket

实例:ide

LunarPages的CPU信息:ui

processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) CPU 3.00GHz
stepping : 3
cpu MHz : 3000.881
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe lm constant_tsc pni monitor ds_cpl cid xtpr
bogomips : 6006.73spa

 

processor : 1
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) CPU 3.00GHz
stepping : 3
cpu MHz : 3000.881
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe lm constant_tsc pni monitor ds_cpl cid xtpr
bogomips : 5999.40操作系统

 

processor : 2
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) CPU 3.00GHz
stepping : 3
cpu MHz : 3000.881
cache size : 2048 KB
physical id : 3
siblings : 2
core id : 3
cpu cores : 1
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe lm constant_tsc pni monitor ds_cpl cid xtpr
bogomips : 5999.08线程

 

processor : 3
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) CPU 3.00GHz
stepping : 3
cpu MHz : 3000.881
cache size : 2048 KB
physical id : 3
siblings : 2
core id : 3
cpu cores : 1
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe lm constant_tsc pni monitor ds_cpl cid xtpr
bogomips : 5999.55

显示4个逻辑CPU,经过physical id,前面两个逻辑cpu的相同,后面两个的相同,因此有两个物理CPU。前面两个的 core id相同,后面的两个core ID相同,说明这两个CPU都是单核。也就是说两个单核cpu,启用了超线程技术。
经过intel的cpu的参数能够初步判断 使用的是两个 Xeon奔腾4CPU ,有点差。。。。
如何得到CPU的详细信息:

linux命令:cat /proc/cpuinfo

用命令判断几个物理CPU,几个核等:

逻辑CPU个数:
# cat /proc/cpuinfo | grep 'processor' | wc -l

物理CPU个数:
# cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l

每一个物理CPU中Core的个数:
# cat /proc/cpuinfo | grep 'cpu cores' | wc -l

是否为超线程?
若是有两个逻辑CPU具备相同的”core id”,那么超线程是打开的。

每一个物理CPU中逻辑CPU(多是core, threads或both)的个数:
# cat /proc/cpuinfo | grep 'siblings'
其余特征:

目前intel新的多核心cpu都会在后面显示具体的型号数字,例如:

model name : Intel(R) Xeon(R) CPU          X3230  @ 2.66GHz

说明是 Xeon 3230的cpu,而不显示型号的具体数字的,大部分都是奔腾的CPU

不少主机商都骗人,用奔腾的cpu,却说是多核心的CPU。

探针看到的数据:

类型:Intel(R) Xeon(TM) CPU 2.80GHz 缓存:1024 KB
类型:Intel(R) Xeon(TM) CPU 2.80GHz 缓存:1024 KB
类型:Intel(R) Xeon(TM) CPU 2.80GHz 缓存:1024 KB
类型:Intel(R) Xeon(TM) CPU 2.80GHz 缓存:1024 KB

没有具体的型号,缓存1M,通常都是奔腾系列的cpu,或者是intel假双核的cpu,具体要根据上面说的去判断。新的多核心cpu都能看到具体的型号。
另外多核心的xeon的CPU,通常主频都不高,达到2.8和3.0的只有不多的几个高端CPU型号,通常主机商不会用这么好的。


一些操做系统的最新版本已经更新了 /proc/cpuinfo 文件,以支持多路平台。若是您的系统中的 /proc/cpuinfo 文件可以正确地反映出处理器信息,那么就不须要执行上述步骤。反之,可采用本文中的信息进行解释。

/proc/cpuinfo 文件包含系统上每一个处理器的数据段落。/proc/cpuinfo 描述中有 6 个条目适用于多内核和超线程(HT)技术检查:processor, vendor id, physical id, siblings, core id 和 cpu cores。

processor 条目包括这一逻辑处理器的惟一标识符。
physical id 条目包括每一个物理封装的惟一标识符。
core id 条目保存每一个内核的惟一标识符。
siblings 条目列出了位于相同物理封装中的逻辑处理器的数量。
cpu cores 条目包含位于相同物理封装中的内核数量。

若是处理器为英特尔处理器,则 vendor id 条目中的字符串是 GenuineIntel。

1.拥有相同 physical id 的全部逻辑处理器共享同一个物理插座。每一个 physical id 表明一个惟一的物理封装。

2.Siblings 表示位于这一物理封装上的逻辑处理器的数量。它们可能支持也可能不支持超线程(HT)技术。

3.每一个 core id 均表明一个惟一的处理器内核。全部带有相同 core id 的逻辑处理器均位于同一个处理器内核上。

4.若是有一个以上逻辑处理器拥有相同的 core id 和 physical id,则说明系统支持超线程(HT)技术。

5.若是有两个或两个以上的逻辑处理器拥有相同的 physical id,可是 core id 不一样,则说明这是一个多内核处理器。cpu cores 条目也能够表示是否支持多内核。

例如,若是系统包含两个物理封装,每一个封装中又包含两个支持超线程(HT)技术的处理器内核,则 /proc/cpuinfo 文件将包含此数据。(注:数据并不在表格中。)


转载:http://blog.sina.com.cn/s/blog_4a6151550100iowl.html

相关文章
相关标签/搜索