How can I retrieve a count of just the number of "physical" CPUs?
Thanks!
> How can I retrieve a count of just the number of "physical" CPUs?
See: Windows Support for Hyper-Threading Technology
http://www.microsoft.com/whdc/system/CEC/HT-Windows.mspx
<quote>
There is no API provided in Windows 2000 or Windows XP that enables
application software to identify the presence of HT or to identify which
of the bits in the affinity mask apply to which logical or physical
processor. Applications that require this information can use the CPUID
instruction identification mechanism.
</quote>
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
> How can I retrieve a count of just the number of "physical" CPUs?
See: GetLogicalProcessorInformation
http://msdn.microsoft.com/library/en-us/dllproc/base/getlogicalprocessorinformation.asp
// Get number of Logical CPUs Per Package
// For example:
// HyperThreading P4 CPU will return 2
// Regular P4 will return 1
// Dual Xeon with HT will return 2
// Dual Xeon w/o HT will return 1
// Dual-core Pentium-D will return 2
// Dual-core Pentium-D XE will return 4
// Dual-core Athlon 64 will return 2
UINT NumCpuPerPackage() {
UINT cpp; // unsigned int
_asm {
mov eax, 1
cpuid
mov eax, ebx
and eax, 0x00FF0000 // mask the "logical CPU count" byte
shr eax, 16 // shift byte to be least-significant
mov cpp, eax
}
return cpp;
}
Obviously it's then just a matter of using GetSystemInfo() and doing
dwNumberOfProcessors/NumCpuPerPackage() = Number of Physical CPUs.
I've tested this on a Non-HT P4 and a HT P4 and it seems to work fine.
If anyone can test it on more systems I'd be very grateful!
Also I'm not sure if the number returned will vary depending on whether
HyperThreading is enabled in the BIOS.
My AMD FX-53 reports 0 (cpuid results in 0x00000800 in ebx), so I
changed the last line to:
return cpp ? cpp : 1;
--
Phillip Crews aka Severian
Microsoft MVP, Windows SDK
Posting email address is real, but please post replies on the newsgroup.
Mark.
>>>How can I retrieve a count of just the number of "physical" CPUs?
>
> Get the NUMBER_OF_PROCESSORS environment variable.
This returns the number of "logical" processors (like GetSystemInfo).
int NumCpuPerPackage() {
// Number of Logical Cores per Physical Processor
int nCoreCount = 1;
// Initialize to 1 to support older processors.
_asm {
mov eax, 1
cpuid
// Test for HTT bit
test edx, 0x10000000
jz Unp
// Multi-core or Hyperthreading supported...
// Read the "# of Logical Processors per Physical Processor" field:
mov eax, ebx
and eax, 0x00FF0000 // Mask the "logical core counter" byte
shr eax, 16 // Shift byte to be least-significant
mov nCoreCount, eax
// Uniprocessor (i.e. Pentium III or any AMD CPU excluding their new
dual-core A64)
Unp:
// nCoreCount will contain 1.
}
return nCoreCount;
}
I am pretty darn certain this will work on almost anything now! Please
test on your systems for me though :~)
>OK I've done some reading up of the AMD and Intel manuals and have now
I will try to remember to test at the office tomorrow.
It would be good to know in multi-core siutations how many cores and
whether they are HT.
E.g:
Number of logical processors: 4
Number of cores: 2
Number of processors: 1
I believe there is a new Windows call in Win2k3 SP1 and XP64 that will
provide this information.
> I believe there is a new Windows call in Win2k3 SP1 and XP64 that will
> provide this information.
NUMA:
See: GetLogicalProcessorInformation
http://msdn.microsoft.com/library/en-us/dllproc/base/getlogicalprocessorinformation.asp
Mark.