I. /proc/cpuinfo, /proc/stat, sysconf()
1. cat /proc/cpuinfo
/proc/cpuinfo marks down the details of each logical cpu (e.g. cpu core or hyper-thread if hyper-thread is enabled).
By counting the number of lines which contain "processor", we can get the number of logical cpu:
cat /proc/cpuinfo | grep "processor" | wc -l
Any logical cpu shares the same Physical ID is in the same cpu socket, so we can get the number of physical cpu by counting the number of physical id which are unique:
cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
For the same physical cpu, the number of cores equals to the number of logical cpus if hyperthread is not enabled; If two logical cpus share the same core id, hyper-thread is enabled.
This is how to get the number of cores in each physical cpu:
cat /proc/cpuinfo | grep "cpu cores" | wc -l
This is how to get the number of logical cpus in each physical cpu:
cat /proc/cpuinfo | grep "siblings"
2. cat /proc/stat
/proc/stat gives the performance of each logical cpu (Not sure about this). We can get the number by counting the lines of "cpu[0-9]":
cat /proc/stat | grep "^cpu[0-9]" | wc -l
3. Programming in C: sysconf()
Examples of getting number of processors:
#include <unistd.h>
long num_processor_configured = sysconf (_SC_NPROCESSORS_CONF); => list the number of processors configured
long num_processor_available = sysconf(_SC_NPROCESSORS_ONLN); => list the number of processors currently online
sysconf() is a function from glibc providing values of a configurable system. It is said that sysconf(_SC_NPROCESSORS_ONLN) gets the number of processors available by counting the lines in /proc/stat.
Example 1 (6 cores, 1 cpu socket):
[root@localhost ~]# cat /proc/cpuinfo
processor : 0
model name : Six-Core AMD Opteron(tm) Processor 8431
physical id : 0
core id : 0
cpu cores : 6
processor : 1
model name : Six-Core AMD Opteron(tm) Processor 8431
physical id : 0
core id : 1
cpu cores : 6
processor : 2
model name : Six-Core AMD Opteron(tm) Processor 8431
physical id : 0
core id : 2
cpu cores : 6
processor : 3
model name : Six-Core AMD Opteron(tm) Processor 8431
physical id : 0
core id : 3
cpu cores : 6
processor : 4
model name : Six-Core AMD Opteron(tm) Processor 8431
physical id : 0
core id : 4
cpu cores : 6
processor : 5
model name : Six-Core AMD Opteron(tm) Processor 8431
physical id : 0
core id : 5
cpu cores : 6
[root@localhost ~]# cat /proc/stat
cpu 4162865 54802 17262096 2509381549 1710104 218653 187057 0
cpu0 697349 7150 2995004 418253009 209167 6 3501 0
cpu1 627300 4797 2856686 418320595 316591 8692 27166 0
cpu2 823480 6976 2878035 418024209 346533 49309 34083 0
cpu3 642511 6386 2882030 418295657 230548 52706 52881 0
cpu4 583546 10618 2829846 418364092 288908 49996 37702 0
cpu5 788677 18871 2820493 418123985 318355 57942 31722 0
printf("%ld\n", sysconf (_SC_NPROCESSORS_CONF)); => 6
printf("%ld\n", sysconf(_SC_NPROCESSORS_ONLN)); => 6
Example 2 (2 cpu sockets, 2 cores for each cpu socket):
linux-q55j:/home/cat # cat /proc/cpuinfo
processor : 0
model name : Intel(R) Xeon(R) CPU E5502 @ 1.87GHz
physical id : 0
core id : 0
cpu cores : 2
processor : 1
model name : Intel(R) Xeon(R) CPU E5502 @ 1.87GHz
physical id : 0
core id : 2
cpu cores : 2
processor : 2
model name : Intel(R) Xeon(R) CPU E5502 @ 1.87GHz
physical id : 1
core id : 0
cpu cores : 2
processor : 3
model name : Intel(R) Xeon(R) CPU E5502 @ 1.87GHz
physical id : 1
core id : 2
cpu cores : 2
linux-q55j:/home/cat # cat /proc/stat
cpu 597925 23710 191645 443151716 77080 2412 10769 0
cpu0 251568 8192 71852 110625120 54392 107 2444 0
cpu1 127831 7165 67476 110785569 18740 1994 5079 0
cpu2 115977 4925 17693 110872516 2652 2 87 0
cpu3 102548 3426 34623 110868509 1294 308 3158 0
printf("%ld\n", sysconf (_SC_NPROCESSORS_CONF)); => 4
printf("%ld\n", sysconf(_SC_NPROCESSORS_ONLN)); => 4
II. dmidecode
dmidecode collects information from System BIOS based on the SMBIOS/DMI standard, e.g. processor, memory. It is kind of decode of DMI.
This is how to get the information of CPU using dmidecode:
dmidecode -t processor
A simple explanation of DMI and SMBIO.
-------
| DMI |
-------
/\
||
\/
--------------------
|SMBIOS Extensions|
--------------------
--------------------
| Motherboard BIOS |
--------------------
SMBIOS: System Management BIOS
DMI: Desktop Management Interface
출처: http://heidydogdog.appspot.com/?p=105001