-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).