| Last Modified On : | May 7, 2008 1:06 AM PDT |
Rate |
|
#pragma once
typedef struct cpuid_args_s {
DWORD eax;
DWORD ebx;
DWORD ecx;
DWORD edx;
} CPUID_ARGS;
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _M_X64 // For 64-bit apps
unsigned __int64 __rdtsc(void);
#pragma intrinsic(__rdtsc)
#define _RDTSC __rdtsc
void cpuid64(CPUID_ARGS* p);
#define _CPUID cpuid64
#else // For 32-bit apps
#define _RDTSC_STACK(ts)
__asm rdtsc
__asm mov DWORD PTR [ts], eax
__asm mov DWORD PTR [ts+4], edx
__inline unsigned __int64 _inl_rdtsc32() {
unsigned __int64 t;
_RDTSC_STACK(t);
return t;
}
#define _RDTSC _inl_rdtsc32
void cpuid32(CPUID_ARGS* p);
#define _CPUID cpuid32
#endif
// Our 32/64-bit example function
int GetCoresPerPackage();
#ifdef __cplusplus
}
#endif
|
|
|
; call cpuid with args in eax, ecx
; store eax, ebx, ecx, edx to p
PUBLIC cpuid64
.CODE
ALIGN 8
cpuid64 PROC FRAME
; void cpuid64(CPUID_ARGS* p);
; rcx <= p
sub rsp, 32
.allocstack 32
push rbx
.pushreg rbx
.endprolog
mov r8, rcx
mov eax, DWORD PTR [r8+0]
mov ecx, DWORD PTR [r8+8]
cpuid
mov DWORD PTR [r8+0], eax
mov DWORD PTR [r8+4], ebx
mov DWORD PTR [r8+8], ecx
mov DWORD PTR [r8+12], edx
pop rbx
add rsp, 32
ret
ALIGN 8
cpuid64 ENDP
_TEXT ENDS
|
| April 1, 2008 12:00 PM PDT
Eric Palmer | You cannot detect the number of physical processors without some use of OS APIs. When you issue the CPUID instruction, it runs on the processor on which the OS has scheduled your thread. The enumeration algorithms in the samples below work by forcing the current thread to each of the processors in the system and then reading the APIC-related fields from the CPUID instruction. This requires that the OS provide the total number of logical processors and a way to force the current thread to run on each (set affinity). See http://softwarecommunity.intel.com/articles/eng/2728.htm and/or http://softwarecommunity.intel.com/articles/eng/2728.htm. The first has the most up-to-date detection code. |
| April 1, 2008 12:01 PM PDT
Eric Palmer | The 2nd link below should have been http://softwarecommunity.intel.com/articles/eng/1855.htm |
Eric Palmer (Intel)
|
Heath
Is it possible to detect the number of physical processors using this technique? The sample works perfectly but I need a method to query the processor(s) directly to get a count of physical processors in the system without using Windows API.
Many thanks.