bpo-30581: Windows: os.cpu_count() returns wrong number of processors… · python/cpython@c67bae0 (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +os.cpu_count() now returns the correct number of processors on Windows
2 +when the number of logical processors is greater than 64.
Original file line number Diff line number Diff line change
@@ -11174,9 +11174,22 @@ os_cpu_count_impl(PyObject *module)
11174 11174 {
11175 11175 int ncpu = 0;
11176 11176 #ifdef MS_WINDOWS
11177 -SYSTEM_INFO sysinfo;
11178 -GetSystemInfo(&sysinfo);
11179 -ncpu = sysinfo.dwNumberOfProcessors;
11177 +/* Vista is supported and the GetMaximumProcessorCount API is Win7+
11178 + Need to fallback to Vista behavior if this call isn't present */
11179 +HINSTANCE hKernel32;
11180 +hKernel32 = GetModuleHandleW(L"KERNEL32");
11181 +
11182 +static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11183 +*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11184 +"GetMaximumProcessorCount");
11185 +if (_GetMaximumProcessorCount != NULL) {
11186 +ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11187 + }
11188 +else {
11189 +SYSTEM_INFO sysinfo;
11190 +GetSystemInfo(&sysinfo);
11191 +ncpu = sysinfo.dwNumberOfProcessors;
11192 + }
11180 11193 #elif defined(__hpux)
11181 11194 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11182 11195 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)