From 1d8254acdde164948c109f86665513d00dcb6b69 Mon Sep 17 00:00:00 2001 From: Xavier Hallade Date: Tue, 17 Sep 2024 13:43:57 +0200 Subject: [PATCH 1/3] Fix #101873: wmic not found on insider builds of Windows 11 use powershell instead --- benchmark_script/info.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/benchmark_script/info.py b/benchmark_script/info.py index 2f90413..62d5934 100644 --- a/benchmark_script/info.py +++ b/benchmark_script/info.py @@ -138,26 +138,27 @@ def _get_cpu_topology() -> CPUTopology: sockets = int( subprocess.check_output( ( - 'wmic', - 'computersystem', - 'get', - 'NumberOfProcessors', - '/value', + 'powershell', + '-Command', + 'Get-CimInstance Win32_ComputerSystem | Select-Object -ExpandProperty NumberOfProcessors' ), 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 + int(core) for core in subprocess.check_output( + ( + 'powershell', + '-Command', + 'Get-CimInstance Win32_Processor | Select-Object -ExpandProperty NumberOfCores' + ), + text=True ) .strip() .split('\n') - if line.strip() + if core ) return CPUTopology(sockets=sockets, cores=cores, threads=multiprocessing.cpu_count()) -- 2.30.2 From 52ef1b8b51937edab4cbfe8fce2ac50ee853ed85 Mon Sep 17 00:00:00 2001 From: Xavier Hallade Date: Wed, 18 Sep 2024 08:26:39 +0200 Subject: [PATCH 2/3] readd wmic for Windows < 10 --- benchmark_script/info.py | 70 +++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/benchmark_script/info.py b/benchmark_script/info.py index 62d5934..14e49ea 100644 --- a/benchmark_script/info.py +++ b/benchmark_script/info.py @@ -135,31 +135,57 @@ def _get_cpu_topology() -> CPUTopology: sockets = cores_info.get_physical_processors_count() cores = cores_info.get_physical_cores_count() else: - sockets = int( - subprocess.check_output( - ( - 'powershell', - '-Command', - 'Get-CimInstance Win32_ComputerSystem | Select-Object -ExpandProperty NumberOfProcessors' - ), - text=True, + if sys.getwindowsversion().major >= 10: + sockets = int( + subprocess.check_output( + ( + 'powershell', + '-Command', + 'Get-CimInstance Win32_ComputerSystem | Select-Object -ExpandProperty NumberOfProcessors' + ), + text=True, + ) + .strip() ) - .strip() - ) - cores = sum( - int(core) for core in subprocess.check_output( - ( - 'powershell', - '-Command', - 'Get-CimInstance Win32_Processor | Select-Object -ExpandProperty NumberOfCores' - ), - text=True + 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: + 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() ) - .strip() - .split('\n') - if core - ) return CPUTopology(sockets=sockets, cores=cores, threads=multiprocessing.cpu_count()) -- 2.30.2 From 2db4040060709c9ef365581a1bda7b9cf078df63 Mon Sep 17 00:00:00 2001 From: Xavier Hallade Date: Wed, 18 Sep 2024 08:56:09 +0200 Subject: [PATCH 3/3] move windows cores count to cpu_cores module --- benchmark_script/info.py | 61 +--------------- benchmark_script/vendor/cpu_cores/__init__.py | 2 +- benchmark_script/vendor/cpu_cores/common.py | 3 + benchmark_script/vendor/cpu_cores/windows.py | 70 +++++++++++++++++++ 4 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 benchmark_script/vendor/cpu_cores/windows.py diff --git a/benchmark_script/info.py b/benchmark_script/info.py index 14e49ea..7844d5d 100644 --- a/benchmark_script/info.py +++ b/benchmark_script/info.py @@ -128,64 +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: - if sys.getwindowsversion().major >= 10: - sockets = 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: - 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 -- 2.30.2