views:

175

answers:

4

I can already correctly detect the number of logical processors correctly on all three of these platforms.

To be able to detect the number of physical processors/cores correctly I'll have to detect if hyperthreading is supported AND active (or enabled if you prefer) and if so divide the number of logical processors by 2 to determine the number of physical processors.

Perphaps I should provide an example:

A quad core Intel CPU's with hyperthreading enabled has 4 physical cores, yet 8 logical processors (hyperthreading creates 4 more logical processors). So my current function would detect 8 instead of the desired 4.

My question therefore is if there is a way to detect whether hyperthreading is supported AND ENABLED?

+4  A: 

Linux:

Number of physical CPUs:

grep -i "physical id" /proc/cpuinfo | sort -u | wc -l

Number of logical CPUs:

grep -i "processor" /proc/cpuinfo | sort -u | wc -l
Artelius
guess I'll have to you use grep.cpp from boost library in my program to use this. My question was specific to c/c++/assemler as noted in the subject and tags
HTASSCPP
+2  A: 

The CPUID instruction (when you pass function 1H in EAX) returns they hyper threading feature flag in bit 28 of the EDX register. I think that multi-core processors report that they are hyperthreading enabled even though each individual core can run only one thread.

It also returns the number of logical processors per physical processor in bits 23-16 of EBX. I think that you'd have to query each processor individually in order to hit all of the processors on your system.

Ken Bloom
+3  A: 

On Windows 2003 Server and Windows XP SP3 and later, you can determine this information using the GetLogicalProcessorInformation system call.

Ken Bloom
Does this work on lets say Windows 7 or Windows Server 2008 RC2? Or should I use GetLogicalProcessorInformation then?
HTASSCPP
+1  A: 

On OS X:

#include <sys/sysctl.h>

int physicalCores;
sysctlbyname("hw.physicalcpu", &physicalCores, sizeof(physicalCores), NULL, 0);

See the header or manpage for more information. (Note that you can get the number of logical cpus in the same way, using the "hw.logicalcpu" string)

Stephen Canon
Works pefectly on all machines I've tested this on, thanks!
HTASSCPP