7

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?

HTASSCPP
  • 609
  • 1
  • 6
  • 4

4 Answers4

4

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
  • 57,498
  • 14
  • 111
  • 168
4

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

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
  • Does this work on lets say Windows 7 or Windows Server 2008 RC2? Or should I use GetLogicalProcessorInformation then? – HTASSCPP May 26 '10 at 16:26
  • The msdn page says of the SYSTEM_LOGICAL_PROCESSOR_INFORMATION::ProcessorCore.Flags field "If the value of this member is 1, the logical processors identified by the value of the ProcessorMask member share functional units, as in Hyperthreading or SMT. Otherwise, the identified logical processors do not share functional units." But it also says that the value is wrong before Windows Vista, and then only means that the logical processors are in the same physical package. There are more problems: it really isn't specified that the call will group the logical processors in a useful way. – Josh S Apr 26 '14 at 09:53
3

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
  • 48,337
  • 13
  • 89
  • 105
  • 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 May 26 '10 at 16:24
3

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
  • 103,815
  • 19
  • 183
  • 269