Number of Logical Processors per Physical Processor

Submit New Article

Last Modified On :   July 16, 2007 1:07 PM PDT
Rate
 



Challenge

Identify the number of Hyper-Threading Technology enabled logical processors supported by a physical processor. To maximize performance from Hyper-Threading Technology, applications may set processor affinity on a per-thread basis. With knowledge of application thread-resource requirements, you can effectively schedule threads on logical processors based on their association to the physical processors.


Solution

Use the cpuid instruction to retrieve bits 16-23 in register ebx with eax equal to 1. If the physical processor supports Hyper-Threading Technology, then bits 16-23 in register ebx, using the cpuid instruction with eax equal to 1, identifies the number of logical processors the physical processor supports.

The following sample code identifies the number of logical processors per physical processor:

#define NUM_LOGICAL_BITS   0x00FF0000 //
EBX[23:16] indicate number of
// logical processors per package
// Returns the number of logical processors per physical
processors
unsigned char LogicalProcessorsPerPackage(void)
{
unsigned int reg_ebx = 0;
if (!HTSupported()) return (unsigned char) 1;
__asm {
mov    eax, 1 // call cpuid with eax = 1
cpuid
mov    reg_ebx, ebx // Has info on number of logical processors
}
return (unsigned char) ((reg_ebx
NUM_LOGICAL_BITS) >> 16);
}

 

Other issues related to the detection of support for Hyper-Threading Technology are covered in separate items:

 

The steps for determining whether Hyper-Threading Technology is supported and for retrieving the number of logical processors per physical processor can be combined, thus minimizing the number of calls to the cpuid instruction.

Note that, although a processor supports Hyper-Threading Technology and has more than one logical processor within the package, Hyper-Threading Technology may not be enabled by BIOS, and the operating system may not utilize the extra logical processors. If Hyper-Threading Technology is not supported, the default number of logical processors per physical processor is one.