Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Number of physical CPUs?

429 views
Skip to first unread message

Nathan Evans

unread,
Apr 23, 2005, 8:23:12 AM4/23/05
to
GetSystemInfo() returns a count of all the "logical" CPUs.

How can I retrieve a count of just the number of "physical" CPUs?

Thanks!

Jochen Kalmbach [MVP]

unread,
Apr 23, 2005, 9:59:27 AM4/23/05
to
Hi Nathan!

> 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/

Jochen Kalmbach [MVP]

unread,
Apr 23, 2005, 10:40:33 AM4/23/05
to Nathan Evans
Hi Nathan!

> 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

Nathan Evans

unread,
Apr 23, 2005, 12:07:22 PM4/23/05
to
Thanks for those URLs. It seems Win2003 is the only OS capable of
natively doing this. So after reading a couple articles on the subject
I improvised and came up with this:-

// 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.

Severian [MVP]

unread,
Apr 23, 2005, 1:51:13 PM4/23/05
to
On 23 Apr 2005 09:07:22 -0700, "Nathan Evans" <nbe...@gmail.com>
wrote:

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

unread,
Apr 24, 2005, 2:24:02 AM4/24/05
to
Get the NUMBER_OF_PROCESSORS environment variable.

Mark.

Jochen Kalmbach [MVP]

unread,
Apr 24, 2005, 3:27:38 AM4/24/05
to
Hi 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).

Nathan Evans

unread,
Apr 24, 2005, 10:19:23 AM4/24/05
to
Hmm... that suggests to me that AMD's don't support this cpuid
parameter because your processor should have returned 1 :~(

Nathan Evans

unread,
Apr 24, 2005, 12:33:33 PM4/24/05
to
OK I've done some reading up of the AMD and Intel manuals and have now

came up with this:-

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 :~)

Severian [MVP]

unread,
Apr 24, 2005, 1:30:00 PM4/24/05
to
On 24 Apr 2005 09:33:33 -0700, "Nathan Evans" <nbe...@gmail.com>
wrote:

>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.

Jochen Kalmbach [MVP]

unread,
Apr 24, 2005, 1:35:31 PM4/24/05
to
hI Severian!

> 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

unread,
Apr 25, 2005, 10:10:04 AM4/25/05
to
Opps!

Mark.

0 new messages