Skip to content
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

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

Merged
merged 3 commits into from
Aug 30, 2017

Conversation

crwilcox
Copy link
Contributor

@crwilcox crwilcox commented Jul 28, 2017

os.cpu_count() returns wrong number of processors on system with > 64 logical processors

https://bugs.python.org/issue30581

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

…ount API on Windows calls to os.cpu_count()
@crwilcox crwilcox force-pushed the bug/windows-processor-counts branch from 7e6a49c to 73adde2 Compare July 28, 2017 16:46
/* 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).

*(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.

Copy link
Member

@zooba zooba left a comment

Choose a reason for hiding this comment

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

This still requires a NEWS.d entry, and the documentation should be updated to indicate:

  • the behaviour of the function has changed on Windows
  • the fact that a 32-bit interpreter on 64-bit Windows will never return (nor have access to) more than 32 CPUs

I don't believe the latter point is a problem - the current behaviour of the API has the same issue. This falls into the same category as sys.getwindowsversion() reporting what the operating system provides, even when this is incorrect (e.g. Python 2.7 will always return 6.2 even on Windows 10, and the OS also shims certain APIs to behave like the previous version). Since the process is limited by the OS to only 32 CPUs, the os module API should reflect that limit.

@giampaolo
Copy link
Contributor

Before merging please consider my comment here:
http://bugs.python.org/issue30581#msg299477

@crwilcox
Copy link
Contributor Author

crwilcox commented Aug 4, 2017

@giampaolo, I had responded. Is there something I didn't address?

@pitrou
Copy link
Member

pitrou commented Aug 29, 2017

@giampaolo, do you have more concerns here?

@giampaolo
Copy link
Contributor

Sorry for the delay (I've been backpacking / travelling through China).
Considering os.cpu_count() is not supposed to return the number of usable CPUs (as I first erroneously thought) the patch LGTM.

@pitrou pitrou merged commit c67bae0 into python:master Aug 30, 2017
@pitrou
Copy link
Member

pitrou commented Aug 30, 2017

Thank you @crwilcox !

crwilcox added a commit to crwilcox/cpython that referenced this pull request Sep 1, 2017
…python#2934)

* Fixes python#30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count()

* Add NEWS.d entry for bpo-30581, os.cpu_count on Windows.

* Tweak NEWS entry
pitrou pushed a commit that referenced this pull request Sep 1, 2017
…#2934) (#3267)

* Fixes #30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count()

* Add NEWS.d entry for bpo-30581, os.cpu_count on Windows.

* Tweak NEWS entry
GadgetSteve pushed a commit to GadgetSteve/cpython that referenced this pull request Sep 10, 2017
…python#2934)

* Fixes python#30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count()

* Add NEWS.d entry for bpo-30581, os.cpu_count on Windows.

* Tweak NEWS entry
@arhadthedev arhadthedev changed the title bpo-30581: Windows: os.cpu_count() returns wrong number of processors empty Apr 25, 2023
@arhadthedev arhadthedev changed the title empty bpo-30581: Windows: os.cpu_count() returns wrong number of processors Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants