Processor Supports of Hyper-Threading Technology

Submit New Article

Last Modified On :   March 20, 2008 6:40 PM PDT
Rate
 



Challenge

Detect the presence or absence of Hyper-Threading Technology-support in a processor. Hyper-Threading Technology-enabled processors contain multiple logical processors per physical processor package. The state information necessary to support each logical processor is replicated, while sharing the underlying physical processor resources. Given that processor resources are generally underutilized by most applications, Hyper-Threading Technology-enabled processors can improve overall application performance. Multiple threads running in parallel can achieve higher utilization and increased throughput.


Solution

Use the CPUID instruction to determine whether the physical processor supports Hyper-Threading Technology. First, ensure that the processor supports the cpuid instruction and is a genuine Intel® Pentium® 4 family processor or later. Then retrieve model-specific feature information about Hyper-Threading Technology using the cpuid instruction and setting the eax register to 1.

If the platform supports more than one physical processor, it does not matter on which processor you execute the cpuid instruction, since all physical processors present on the platform must support the same number of logical processors. After executing the cpuid instruction, if bit 28 in edx is set, then the physical processor supports Hyper-Threading Technology.

The following code fragment enables an application to determine whether Hyper-Threading Technology is supported on the processor:

    #define HT_BIT 0x10000000 // EDX[28] - Bit 28 set
indicates
// Hyper-Threading Technology is supported in hardware.
#define FAMILY_ID    0x0f00 // EAX[11:8] - Bit
11 thru 8 contains family
// processor id
#define EXT_FAMILY_ID 0x0f00000 // EAX[23:20] - Bit 23 thru 20
contains    
// extended family processor id
#define PENTIUM4_ID    0x0f00 // Pentium 4
family processor id      
// Returns non-zero if Hyper-Threading Technology is supported on
// the processors and zero if not. This does not mean that
// Hyper-Threading Technology is necessarily enabled.
unsigned int HTSupported(void)
{
   unsigned int reg_eax = 0;
   unsigned int reg_edx = 0;
   unsigned int vendor_id[3] = {0, 0, 0};
   __try {    // verify cpuid
instruction is supported
      __asm {
       xor    eax,
eax // call cpuid with eax = 0
       cpuid    //
get vendor id string
       mov   
vendor_id, ebx
       mov   
vendor_id + 4, edx
       mov   
vendor_id + 8, ecx
       mov    eax,
1    // call cpuid with eax = 1
       cpuid
       mov   
reg_eax, eax // eax contains cpu family type info
       mov   
reg_edx, edx // edx has info whether
// Hyper-Threading Technology is available
}
}
   __except (EXCEPTION_EXECUTE_HANDLER ) {
      return 0; // CPUID is not
supported and so
// Hyper-Threading Technology is not supported
   }
   
// Check to see if this is a Pentium 4 or later processor
if (((reg_eax <span>&amp;</span> FAMILY_ID) == PENTIUM4_ID) ||
(reg_eax <span>&amp;</span> EXT_FAMILY_ID))
      if (vendor_id[0] ==
'uneG')
         if
(vendor_id[1] == 'Ieni')
            if
(vendor_id[2] == 'letn')
return (reg_edx HT_BIT); // Genuine Intel
Processor with Hyper-Threading Technology
return 0;   // If we get here, must not be
genuine Intel processor.
}

 

Note that support for Hyper-Threading technology on the processor does not necessarily mean that the processor supports more than one logical processor, that the BIOS has d the feature, or that the operating system is utilizing the extra logical processors. Additional steps are required to determine the number of logical processors supported by the physical processor, as well as querying the operating system to determine the logical-to-physical processor mapping. Some of these issues are addressed in the following separate items:

 

 

Refer to Intel's Application Note AP-485 titled Intel Processor Identification and the CPUID Instruction for details on detecting model specific features available on all Intel processors.