CPU Hyperthreading: Is it ON or OFF


Sometime back a customer had asked a question on how to find out the different between hyperthreaded CPUs and multi-core CPUs using Powershell. There are multiple utilities available on the web which provide this information readily along with code samples i.e. if you have an affinity for coding.

However, my requirement was to get this information without the use of an executable. Such an endeavor seemed worthwhile for me since such scripts can be used for auditing and inventory related purposes.

The powershell script below can help you identify if hyperthreading is enabled on the server or not and gives you information about the number of logical and physical processors on the server/machine. The powershell script below makes use of Win32_Processor WMI class. The script can be easily adapted to using VBScript as well.

Script:

 # This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.</pre>
# Author: Amit Banerjee

# Purpose: Helps identify the number of physical processors, logical processors and hyperthreading on the server.

# Provide the computer information

$vComputerName = "."

$vLogicalCPUs = 0

$vPhysicalCPUs = 0

$vCPUCores = 0

$vSocketDesignation = 0

$vIsHyperThreaded = -1

# Get the Processor information from the WMI object

$vProcessors = [object[]]$(get-WMIObject Win32_Processor -ComputerName $vComputerName)

# To account for older machines

if ($vProcessors[0].NumberOfCores -eq $null)

{

$vSocketDesignation = new-object hashtable

$vProcessors |%{$vSocketDesignation[$_.SocketDesignation] = 1}

$vPhysicalCPUs = $vSocketDesignation.count

$vLogicalCPUs = $vProcessors.count

}

# If the necessary hotfixes are installed as mentioned below, then the NumberOfCores and NumberOfLogicalProcessors can be fetched correctly

else

{

# For any machine of Windows Server 2008 or above

# For Windows Server 2003, KB932370 needs to be installed

# For Windows XP, KB936235 needs to be installed

$vCores = $vProcessors.count

$vLogicalCPUs = $($vProcessors|measure-object NumberOfLogicalProcessors -sum).Sum

$vPhysicalCPUs = $($vProcessors|measure-object NumberOfCores -sum).Sum

}

# Additional code can be written here to input the data below into a database

"Logical CPUs: {0}; Physical CPUs: {1}; Number of Cores: {2}" -f $vLogicalCPUs,$vPhysicalCPUs,$vCores

if ($vLogicalCPUs -gt $vPhysicalCPUs)

{

"Hyperthreading: Active"

}

else

{

"Hyperthreading: Inactive"

}

The output of the above script from my Windows 8 laptop is:

Logical CPUs: 8; Physical CPUs: 4; Number of Cores: 1
Hyperthreading: Active

Let us compare the output and find out if it is accurate based on what CoreInfo utility tells me from Sysinternals.

Snippet of Output of CoreInfo:

Logical to Physical Processor Map:
**——  Physical Processor 0 (Hyperthreaded)
–**—-  Physical Processor 1 (Hyperthreaded)
—-**–  Physical Processor 2 (Hyperthreaded)
——**  Physical Processor 3 (Hyperthreaded)

So far I have tested this script on multiple Intel processors with multi-core processors with and without hyperthreading enabled. If you do see a scenario where the above script is providing an incorrect output, please feel free to leave a comment on this post. I am always looking forward to suggestions.

Advertisements

16 thoughts on “CPU Hyperthreading: Is it ON or OFF

  1. Great Amit,I tested out in some of the servers and it worked well.

    Is it easy for you to include the hyper thread ratio too?

  2. I will see if that can be added. I believe you are referring to the ratio of logical to physical processors or am I misunderstanding your ask?

  3. Pingback: Something for the Weekend - SQL Server Links 29/06/12

  4. Thanks! I reduced it to one (long) line because I don’t need to support unhotfixed versions.

    Get-WmiObject Win32_Processor | Measure -Property NumberOfCores, NumberOfLogicalProcessors -Sum | Select Property, Sum

    The output is a table.

    Property Sum
    ——– —
    NumberOfCores 16
    NumberOfLogicalProcessors 16

    If the numbers are equal, hyperthreading is off.

  5. Pingback: Is Hyperthreading Enabled | darrylcauldwell.com

  6. Great Script.
    But I believe something is wrong with the final IF statement.
    Shouldn’t the IF statement be

    if ($vLogicalCPUs -gt $vCores)
    {
    “Hyperthreading: Active”
    }

  7. Your core might have more than one physical CPU. Eg. a dual core or octa core processor. Example from a machine: Logical CPUs: 8; Physical CPUs: 4; Number of Cores: 1. This is a quad core machine with hyperthreading enabled. If hyperthreading is disabled, then Logical CPUs = 4 will be greater than number of cores which is 1. But that would not mean hyperthreading is enabled. Hope this clarifies your question.

  8. wonder why the final IF statement doesnt consider cores, for example if I have Nr of Cores: 2, Logical CPUs: 16, Physical CPUs: 8. So the hyperthreading Disabled, but the IF Statement output Enabled.

  9. Pingback: How To Check If Hyperthreading Is Enabled Windows 2003 | CamHR

It is always good to hear from you! :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s