Counting processors on your Linux box
Ever since the /proc file system first made an appearance on Unix systems, getting information on running processes became a whole lot easier. The useful, but too often insufficient information available in the output of ps commands was thoroughly upstaged by /proc which acts as an interface to kernel data structures. /proc provides interfaces to kernel data structures that can supply information on how memory is being used, what devices are mounted, which file descriptors are in use, and what command line arguments were used with running processes -- and that's just a start. On Linux systems, /proc also contains information on the system's processors. It sits in a file called cpuinfo.
To find out how many processors you have, for example, look through /proc/cpuinfo for lines containing the string "physical id". You can grab this with grep and then pass that information through a couple handy filters like this to get a count:
$ grep "physical id" /proc/cpuinfo | sort | uniq | wc -l 2
This tells you how many physical processors are on your system, but doesn't answer questions about how many cores or whether your system is using hyper-threading. Note that any particular physical id may appear in the file more than once, so you want to sort lines that contain that string (e.g., "physical id : 0") to be sure that each gets counted only once.
OK, in this example, the answer is 2. There are two physical CPUs on this particular system. Before dual, quad, octal ... processors, that's all we would have needed to know, but these days, that only tells us part of the story. We likely have a lot more processing power than the number of CPUs indicates.
If your processors are multi-core, you need to know how many virtual processors you have. You can count those by looking for lines that start with "processor".
$ grep "^processor" /proc/cpuinfo | wc -l 24
That sounds much more interesting! Even this, however, doesn't tell you the whole story. You might have more virtual processors than physical processors because your processors are mutli-core, because your processors are hyper-threaded, or both. The way to tell how may cores you have is to look for "cpu cores" in your /proc/cpuinfo file. This line will show up for each virtual processor. If the number of cores shown is less than the number of virtual processors, your system is multi-threading.
$ grep "cpu cores" /proc/cpuinfo | uniq cpu cores : 6
In the system we've been looking at above, we saw two physical processors, 24 virtual processors and six cores. If each processor has six cores, that would still only account for twelve virtual processors. The extra twelve tell us that multi-threading is also in use. The other clue is the number of "siblings" reported for each of the virtual processors:
$ grep siblings /proc/cpuinfo siblings : 12 siblings : 12 siblings : 12 ...
This tells you that each of the physical processors has essentially twelves cores but, again, only because hyper-threading tells the system that for every physical core, there are two cores. So, two processors can swing a lot more processing power than is immediately obvious and /proc/cpuinfo
The information provided on each of the virtual processors on this particular system looks like this. Keep in mind that you would see the same basic information repeated 24 times if I wanted this posting to go on for another couple of pages.
processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 44 model name : Intel(R) Xeon(R) CPU X5650 @ 2.67GHz stepping : 2 cpu MHz : 2660.128 cache size : 12288 KB physical id : 1 siblings : 12 core id : 0 cpu cores : 6 apicid : 32 fpu : yes fpu_exception : yes cpuid level : 11 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx pdpe1gb rdtscp lm constant_tsc ida pni monitor ds_cpl vmx smx est tm2 cx16 xtpr popcnt lahf_lm bogomips : 5323.84 clflush size : 64 cache_alignment : 64 address sizes : 40 bits physical, 48 bits virtual power management:
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.