Download VS2005 Project and Binary
The WMI class, ‘Win32_Processor’, will provide you with numerous bits of information about the CPUs in your machine. It is not possible however, to query whether hyperthreading is enabled, or otherwise query whether or not a processor is physical or logical.
Using the SocketDesignation property of the ‘Win32_Processor’ WMI class we can identify which socket each CPU is in. A physical CPU will report the true socket, and any logical CPU will report the same socket as your first CPU.
I have created a small C# console application to demonstrate detecting hyperthreading, by comparing the count of physical and logical CPUs.
You will need to add a reference to System.Management.
static void Main(string[] args)
{
List<string> Sockets=new List<string>();
int PhysicalCPU=0;
int LogicalCPU=0;
//Create WMI Class
ManagementClass mc = new ManagementClass(“Win32_Processor”);
//Populate class with Processor objects
ManagementObjectCollection moc = mc.GetInstances();
//Iterate through logical processors
foreach (ManagementObject mo in moc)
{
LogicalCPU++;
string SocketDesignation = mo.Properties[“SocketDesignation”].Value.ToString();
//We will count the unique SocketDesignations to find
//the number of physical CPUs in the system.
if(!Sockets.Contains(SocketDesignation))
{
Sockets.Add(SocketDesignation);
}
}
PhysicalCPU = Sockets.Count;
Console.WriteLine(LogicalCPU + ” logical CPUs detected.”);
Console.WriteLine(PhysicalCPU + ” physical CPUs detected.”);
//Are there more logical than physical cpus?
//If so, obviously we are hyperthreading.
if (LogicalCPU > PhysicalCPU)
{
Console.WriteLine(“HyperThreading is enabled.”);
}
Console.ReadKey();
}
The sample code and available project are .net 2.0. If you want to use this in VS.net 2003, and .net 1.1, you will simply need to change our the List generic for an arraylist.
Does not work on Intel i7 (hyperthreading enabled).
Returns 1 physical and 1 logical processor.