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.

Comments

16 responses to “CPU Hyperthreading: Is it ON or OFF”

  1. Marcos Freccia Avatar

    Great script..

    Congrats!!

    Like

  2. Amit Banerjee Avatar

    Thanks. 🙂

    Like

  3. Anup Avatar

    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?

    Like

  4. Amit Banerjee Avatar

    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?

    Like

  5. […] CPU Hyperthreading: Is it ON or OFF – A handy helping of PowerShell magic from Amit Banerjee (Blog|Twitter). […]

    Like

  6. Anup Avatar

    Yes,ratio of logical to physical.

    Like

  7. Iain Elder Avatar

    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.

    Like

  8. Amit Banerjee Avatar

    Yup. If the unsupported machines are not required, then the pipe command is extremely helpful. 🙂 Thanks for contributing to the script.

    Like

  9. […] asked to check if Hyperthreading was enabled on a running server today,  rather pleasingly I then came across this short PowerShell script.  Save into a text file and run it on any server with PowerShell […]

    Like

  10. JC Abourjaily Avatar
    JC Abourjaily

    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”
    }

    Like

  11. Amit Banerjee Avatar

    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.

    Like

  12. Anton Avatar
    Anton

    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.

    Like

  13. Amit Banerjee Avatar

    What is the version of the OS?

    Like

  14. […] CPU Hyperthreading: Is it ON or OFF | … – Sep 09, 2015  · 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; … […]

    Like

  15. Blake Avatar
    Blake

    Awesome script! Any way of running it against multiple servers or a list of servers?

    Like

  16. Amit Banerjee Avatar

    You can use a string array and loop through the servers using a while loop.

    Like