python how to find out whether hyperthreading is enabled
Asked Answered
R

4

13

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!

Ridiculous answered 15/10, 2012 at 18:45 Comment(2)
possible duplicate of Python: Cross-platform solution to detect physical non-HT CPUs?Autobiographical
are you on linux or windows? The answer will be different, I don't think there exists a platform independent solutionEnlighten
A
3

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

Note that method is linux-specific.

Afterbirth answered 15/10, 2012 at 19:25 Comment(1)
OK that's helpful. Thank you!Ridiculous
C
8

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().

Collotype answered 1/3, 2013 at 2:0 Comment(1)
lscpu -p gives comma separated outputArioso
A
3

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

Note that method is linux-specific.

Afterbirth answered 15/10, 2012 at 19:25 Comment(1)
OK that's helpful. Thank you!Ridiculous
K
2

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.

Kreegar answered 27/9, 2013 at 19:1 Comment(0)
B
0

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

Wrong idea! On my Core2Due (without any hyperthrating) it gives same output:

<on "CPU0", hyperthreading is active>

I think you must change string in code to:

if cpu.NumberOfCores < cpu.NumberOfLogicalProcessors

(no LowerOrEqual just Lower)

Bernettabernette answered 1/12, 2015 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.