flat assembler
Message board for the users of flat assembler.

Index > Main > How to get number of physical cores?

Author
Thread Post new topic Reply to topic
SergeASM



Joined: 13 Nov 2015
Posts: 21
SergeASM 05 Apr 2016, 12:26
Hello all,

please give an example of code to get number of physical cores (processors) through cpuid.
I want to determine how many threads can work strong parallel and each thread should have separate CPU cashe (hyperthreading not interested), thanks.

_________________
Serge
Post 05 Apr 2016, 12:26
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20301
Location: In your JS exploiting you and your system
revolution 06 Apr 2016, 07:16
It might be more fruitful to call the OS API to get the number of available cores for the program. The number physical and available cores are not always the same.

And to ignore HT you need to map from the OSes available cores to the physically implemented threads. And to make things more interesting different OSes map these in different orders. The only really sure way to ensure that you are not sharing a core with two threads is to disable HT in the BIOS. For that you need to convince the user to check and/or alter BIOS settings which might be difficult depending upon the exact usage situation.
Post 06 Apr 2016, 07:16
View user's profile Send private message Visit poster's website Reply with quote
Feryno



Joined: 23 Mar 2005
Posts: 509
Location: Czech republic, Slovak republic
Feryno 06 Apr 2016, 11:11
Hi SergeASM, as revolution pointed you, e.g. ms win OS may start with /singlecpu boot switch and in that case CPUID reports something else than running OS is using.

64-ia-32-architectures-software-developer-vol-2a-manual.pdf

The CPUID leaf 04H also reports data that can be used to derive the topology of processor cores in a physical package. This information is constant for all valid index values. Software can query the raw data reported by executing CPUID with EAX=04H and ECX=0 and use it as part of the topology enumeration algorithm described in Chapter 8, “Multiple-Processor Management,” in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A.


64-ia-32-architectures-software-developer-system-programming-manual-325384.pdf

8.6 DETECTING HARDWARE MULTI-THREADING SUPPORT AND TOPOLOGY
Use the CPUID instruction to detect the presence of hardware multi-threading support in a physical processor.
Hardware multi-threading can support several varieties of multigrade and/or Intel Hyper-Threading Technology.
CPUID instruction provides several sets of parameter information to aid software enumerating topology information.
The relevant topology enumeration parameters provided by CPUID include:
• Hardware Multi-Threading feature flag (CPUID.1:EDX[28] = 1) — Indicates when set that the physical
package is capable of supporting Intel Hyper-Threading Technology and/or multiple cores.
• Processor topology enumeration parameters for 8-bit APIC ID:
— Addressable IDs for Logical processors in the same Package (CPUID.1:EBX[23:16]) — Indicates
the maximum number of addressable ID for logical processors in a physical package. Within a physical
package, there may be addressable IDs that are not occupied by any logical processors. This parameter
does not represents the hardware capability of the physical processor.
Addressable IDs for processor cores in the same Package6 (CPUID.(EAX=4, ECX=07):EAX[31:26] +
1 = Y) — Indicates the maximum number of addressable IDs attributable to processor cores (Y) in the physical
package.
• Extended Processor Topology Enumeration parameters for 32-bit APIC ID: Intel 64 processors
supporting CPUID leaf 0BH will assign unique APIC IDs to each logical processor in the system. CPUID leaf 0BH
reports the 32-bit APIC ID and provide topology enumeration parameters. See CPUID instruction reference
pages in Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 2A.
The CPUID feature flag may indicate support for hardware multi-threading when only one logical processor available
in the package. In this case, the decimal value represented by bits 16 through 23 in the EBX register will have
a value of 1.
Software should note that the number of logical processors enabled by system software may be less than the value
of “Addressable IDs for Logical processors”. Similarly, the number of cores enabled by system software may be less
than the value of “Addressable IDs for processor cores”.
Software can detect the availability of the CPUID extended topology enumeration leaf (0BH) by performing two
steps:
• Check maximum input value for basic CPUID information by executing CPUID with EAX= 0. If CPUID.0H:EAX is
greater than or equal or 11 (0BH), then proceed to next step,
• Check CPUID.EAX=0BH, ECX=0H:EBX is non-zero.
If both of the above conditions are true, extended topology enumeration leaf is available. Note the presence of
CPUID leaf 0BH in a processor does not guarantee support that the local APIC supports x2APIC. If
CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for basic CPUID information is greater
than 0BH, then CPUID.0BH leaf is not supported on that processor.
Post 06 Apr 2016, 11:11
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
SergeASM



Joined: 13 Nov 2015
Posts: 21
SergeASM 06 Apr 2016, 12:29
Thanks to all, but I think, some programmers already solve this hard
mind-breaker? Smile

May be WIN API GetSystemInfo, GetLogicalProcessorInformationEx will help?
Post 06 Apr 2016, 12:29
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20301
Location: In your JS exploiting you and your system
revolution 06 Apr 2016, 14:28
Since you now say you are using windows then look at GetProcessAffinityMask. However that won't tell you anything about HT.
Post 06 Apr 2016, 14:28
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.