Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to find out whether hyperthreading is enabled

I have Intel i7-2600K quadcore, with hyperthreading enabled on Ubuntu 12.04. I know that I can find out how many cores I have in Python with import multiprocessing; multiprocessing.cpu_count(), but that gives me 8 because I have hyperthreading enabled on 4 physical cores. I'm rather interested in finding out how many physical cores I have. Is there a way to do that in Python? Alternatively, is there a way of finding out in Python whether hyperthreading is enabled? Thank you in advance for your help!

like image 946
Ray Avatar asked Oct 15 '12 18:10

Ray


3 Answers

To get hyperthreading information on the mac, you can use:

os.popen('sysctl hw').readlines()[1:20]

and compare the 'hw.activecpu' value to the 'hw.physicalcpu' value, the former being the number of cpus including hyperthreading. Or you could just compare 'hw.physicalcpu' with the value returned from multiprocessing.cpu_count().

On linux you could do something similar using:

os.popen('lscpu').readlines()

and multiply the 'Socket(s)' value and the 'Core(s) per socket' value to get the number of physical cpus. Again, you could check this number against multiprocessing.cpu_count().

I dont use Windows, so I cant help you there, but it seems like others have some ideas in this direction

An example related to this can be found here hardware_info().

like image 79
Paul Nation Avatar answered Oct 23 '22 01:10

Paul Nation


According to http://archive.richweb.com/cpu_info, determining a cpu hyperthreading a bit complicated, but still useful.

Note that method is linux-specific.

like image 3
mechmind Avatar answered Oct 23 '22 02:10

mechmind


On windows, to see if hyperthreading is enbabled you can do some WMI magic with pywin32:

from __future__ import print_function
from win32com.client import GetObject

winmgmts_root = GetObject("winmgmts:root\cimv2")
cpus = winmgmts_root.ExecQuery("Select * from Win32_Processor")
for cpu in cpus:
    print('on "{}", hyperthreading is '.format(cpu.DeviceID), end='') 
    if cpu.NumberOfCores < cpu.NumberOfLogicalProcessors:
        print('active')
    else:
        print('inactive')

On my machine, the output is:

on "CPU0", hyperthreading is active

See more info at msdn describing what can be fetched from the Win32_Processor class.

like image 1
David Avatar answered Oct 23 '22 02:10

David