diff --git a/benchmark_script/info.py b/benchmark_script/info.py index 2f90413..7844d5d 100644 --- a/benchmark_script/info.py +++ b/benchmark_script/info.py @@ -128,37 +128,9 @@ def _get_cpu_topology() -> CPUTopology: Get topology information (number of sockets, physical and logical threads) of the system CPUs. """ - sockets: int - cores: int - if not sys.platform.startswith('win'): - cores_info = cpu_cores.CPUCoresCounter.factory() # type: ignore - sockets = cores_info.get_physical_processors_count() - cores = cores_info.get_physical_cores_count() - else: - sockets = int( - subprocess.check_output( - ( - 'wmic', - 'computersystem', - 'get', - 'NumberOfProcessors', - '/value', - ), - text=True, - ) - .strip() - .split('=')[1] - ) - - cores = sum( - int(line.strip().split('=')[1]) - for line in subprocess.check_output( - ('wmic', 'cpu', 'get', 'NumberOfCores', '/value'), text=True - ) - .strip() - .split('\n') - if line.strip() - ) + cores_info = cpu_cores.CPUCoresCounter.factory() # type: ignore + sockets: int = cores_info.get_physical_processors_count() + cores: int = cores_info.get_physical_cores_count() return CPUTopology(sockets=sockets, cores=cores, threads=multiprocessing.cpu_count()) diff --git a/benchmark_script/vendor/cpu_cores/__init__.py b/benchmark_script/vendor/cpu_cores/__init__.py index be1ccc5..894daaa 100644 --- a/benchmark_script/vendor/cpu_cores/__init__.py +++ b/benchmark_script/vendor/cpu_cores/__init__.py @@ -1,7 +1,7 @@ # This file is part of cpu_cores released under the MIT license. # See the LICENSE file for more information. -version_info = (0, 1, 3) +version_info = (0, 1, 4) __version__ = ".".join([str(x) for x in version_info]) from .common import CPUCoresCounter diff --git a/benchmark_script/vendor/cpu_cores/common.py b/benchmark_script/vendor/cpu_cores/common.py index 6c81aee..25d2e1c 100644 --- a/benchmark_script/vendor/cpu_cores/common.py +++ b/benchmark_script/vendor/cpu_cores/common.py @@ -25,6 +25,9 @@ class CPUCoresCounter(object): elif cls.platform.startswith('linux'): from .linux import LinuxCPUCoresCounter return LinuxCPUCoresCounter() + elif cls.platform.startswith('win'): + from .windows import WindowsCPUCoresCounter + return WindowsCPUCoresCounter() else: raise NotImplementedError("unsupported platform type [%s]" % cls.platform) diff --git a/benchmark_script/vendor/cpu_cores/windows.py b/benchmark_script/vendor/cpu_cores/windows.py new file mode 100644 index 0000000..859f302 --- /dev/null +++ b/benchmark_script/vendor/cpu_cores/windows.py @@ -0,0 +1,70 @@ +# This file is part of cpu_cores released under the MIT license. +# See the LICENSE file for more information. + +import platform +import sys +import subprocess + +from .common import CPUCoresCounter + +class WindowsCPUCoresCounter(CPUCoresCounter): + + def _count(self, cpuinfo_filepath=None): + # powershell is available from Windows 10, wmic is optional from Windows 11 2024 + if sys.getwindowsversion().major >= 10: + processors = int( + subprocess.check_output( + ( + 'powershell', + '-Command', + 'Get-CimInstance Win32_ComputerSystem | Select-Object -ExpandProperty NumberOfProcessors' + ), + text=True, + ) + .strip() + ) + + cores = sum( + int(core) for core in subprocess.check_output( + ( + 'powershell', + '-Command', + 'Get-CimInstance Win32_Processor | Select-Object -ExpandProperty NumberOfCores' + ), + text=True + ) + .strip() + .split('\n') + if core + ) + else: + processors = int( + subprocess.check_output( + ( + 'wmic', + 'computersystem', + 'get', + 'NumberOfProcessors', + '/value', + ), + text=True, + ) + .strip() + .split('=')[1] + ) + + cores = sum( + int(line.strip().split('=')[1]) + for line in subprocess.check_output( + ('wmic', 'cpu', 'get', 'NumberOfCores', '/value'), text=True + ) + .strip() + .split('\n') + if line.strip() + ) + + if cores == 0 or processors == 0: + raise Exception("can't get the cpu cores count (windows)") + + self._physical_cores_count = cores + self._physical_processors_count = processors