From 73adde26e9f803ed87dbbc62a8be521ea45c71a1 Mon Sep 17 00:00:00 2001 From: Christopher Wilcox Date: Fri, 28 Jul 2017 09:42:07 -0700 Subject: [PATCH 1/3] Fixes #30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count() --- Modules/posixmodule.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index f4a21679d0b202..419c1ac47b2042 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -11128,9 +11128,22 @@ os_cpu_count_impl(PyObject *module) { int ncpu = 0; #ifdef MS_WINDOWS - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - ncpu = sysinfo.dwNumberOfProcessors; + /* Vista is supported and the GetMaximumProcessorCount API is Win7+ + Need to fallback to Vista behavior if this call isn't present */ + HINSTANCE hKernel32; + hKernel32 = GetModuleHandleW(L"KERNEL32"); + + static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL; + *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32, + "GetMaximumProcessorCount"); + if (_GetMaximumProcessorCount != NULL) { + ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS); + } + else { + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + ncpu = sysinfo.dwNumberOfProcessors; + } #elif defined(__hpux) ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) From ef1b70469a53fd705fbe8f030d83800bd3d48f93 Mon Sep 17 00:00:00 2001 From: Christopher Wilcox Date: Fri, 4 Aug 2017 10:06:07 -0700 Subject: [PATCH 2/3] Add NEWS.d entry for bpo-30581, os.cpu_count on Windows. --- .../next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst diff --git a/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst b/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst new file mode 100644 index 00000000000000..928a2623793457 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst @@ -0,0 +1,2 @@ +The behavior of os.cpu_count() has changed on Windows. Windows will never +return, nor have access to, more than 32 CPUs on a 32 bit build of Python. From 780ea3cc96bf35ca016fc62e5d4fccbc7ca8b004 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 30 Aug 2017 10:44:24 +0200 Subject: [PATCH 3/3] Tweak NEWS entry --- .../next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst b/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst index 928a2623793457..6591fa0df00267 100644 --- a/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst +++ b/Misc/NEWS.d/next/Windows/2017-08-04-10-05-19.bpo-30581.OQhR7l.rst @@ -1,2 +1,2 @@ -The behavior of os.cpu_count() has changed on Windows. Windows will never -return, nor have access to, more than 32 CPUs on a 32 bit build of Python. +os.cpu_count() now returns the correct number of processors on Windows +when the number of logical processors is greater than 64.