Script Center > Scripting Forums > The Official Scripting Guys Forum! > How to find if hyperthreading is enabled using WMI
Ask a questionAsk a question
 

QuestionHow to find if hyperthreading is enabled using WMI

  • Friday, October 30, 2009 4:18 AMSachinNair Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,
    I want to use WMI to check if hyperthreading is enabled for a list of machines. I am not sure if WMI can be reliably used for this.
    This is the code i figured out after reading a lot of forums.


    private int IsHyperThreadingEnabled(string MachineName)
            {
                try
                {
                    Sockets = new List<string>();
                    int PhysicalCPU = 0;
                    int LogicalCPU = 0;
                    int returnValue = 0;
                    //Connection credentials to the remote computer - not needed if the logged in account has access
                    ConnectionOptions oConn = new ConnectionOptions();
                    //connect to remote machine
                    ManagementScope oMs = new ManagementScope(@"\\" + MachineName + @"\root\cimv2", oConn);
                    //query for remote machine
                    ObjectQuery oQuery = new ObjectQuery("Select * from Win32_Processor");
                    //Execute the query
                    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
                    //Get the results
                    ManagementObjectCollection oReturnCollection = oSearcher.Get();
                    //loop through found drives and write out info
                    foreach (ManagementObject mo in oReturnCollection)
                    {

                        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;
                    //Are there more logical than physical cpus?
                    //If so, obviously we are hyperthreading.
                    if (LogicalCPU > PhysicalCPU)
                    {
                        returnValue = 1;
                    }
                    return returnValue;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return 0;
                }
            }


    Will this code work for all kinds of processors, i mean Dual core,Core to duo, Quad core, machines with many processors etc.

    Thanks in advance.

All Replies

  • Friday, October 30, 2009 5:37 AMNickHunyady Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This has been an issue for some time with Server 2003 and below (can't speak for Vista/2008 yet...). I have used a tool called HTdump which will tell you physical versus logical CPU's and then parsed the output. WMI will output logical CPU's because thats what shows up in most all places, device manager, task manger, sysinfo, etc. One other issue I have seen with this is servers with multiple cores and hyperthreading. HTDump is the way to go, IMO, unless someone else can jump in.

  • Tuesday, November 03, 2009 8:58 AMSachinNair Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for your help Nick.

  • Tuesday, November 03, 2009 3:12 PMKarl Mitschke Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    In Server 2003 you can fix the issue:

    http://support.microsoft.com/kb/932370/

    Karl