Skip to content

bpo-30581: Windows: os.cpu_count() returns wrong number of processors #2934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
os.cpu_count() now returns the correct number of processors on Windows
when the number of logical processors is greater than 64.
19 changes: 16 additions & 3 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the handle need to be closed after using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other places in posixmodule use this but on closer inspection they are all in methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, GetModuleHandle does not increase the refcount on the module (it also only works on already-loaded modules, but kernel32 is guaranteed to be there).


static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
"GetMaximumProcessorCount");
if (_GetMaximumProcessorCount != NULL) {
ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ALL_PROCESSOR_GROUPS always defined at compile-time, even before Windows 7?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be just windows 7 forward. I know the result needs to run on Pre-Windows 7, does it also need to compile?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Windows 10 SDK (which is required) the symbol is always defined. We filter APIs to those available on Vista, but that symbol is not excluded when you do that.

}
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)
Expand Down