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
os.cpu_count() returns wrong number of processors on system with > 64 logical processors #74766
Comments
os.cpu_count() seems to report incorrect values on windows systems with >64 logical processors tried it on 2 similar systems, both running windows 7 / 10 with python 3.6.1 64bit (anaconda): platform1 - 2x Xeon E5-2698v4. 20 cores/CPU = total 80 logical cpus with hyperthreading os.cpu_count() reports 40 cores on platform1 and 56 on platform2 I would expect 80 and 56 respectively. I suppose this is because the windows api call used is not aware of processor groups, and reports only the number of processors in the current processor group ( eg GetSystemInfo vs GetMaximumProcessorCount ) |
On Windows, os.cpu_count() is currently implemented with: "GetSystemInfo(&sysinfo); return sysinfo.dwNumberOfProcessors;" https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx It seems to return the number of *logical* CPUs: """
Note: For information about the physical processors shared by logical processors, call GetLogicalProcessorInformationEx with the RelationshipType parameter set to RelationProcessorPackage (3). It seems like you have two physical CPU packages. Maybe the function only returns infos from the first package? |
yes, i believe its reporting the number of processors in the current group only, not across all groups. attached output of windows sysinternals/coreinfo showing 2 processor groups see giampaolo/psutil#771 for some further disucssion of this topic the maintainer of psutil asked me to raise this bug, also had a quick check on #python IRC. Its my first bug on bugs.python.org so if you need more info just let me know |
I am going to work on this if no one else has started. |
Nobody has AFAIK. |
MS documentation is not clear on what function should be used as there are many returning different values. Here it is being suggested to use GetLogicalProcessorInformationEx: |
I agree that the MS Docs for this are a bit confusing. I ended up reaching out to the guy who authored the GetMaximumProcessorCount function. I had also written an implementation that iterated over GetProcessorInformationEx and he advised against it. One of the things that makes this interesting is that in 32 bit processes (wow64) your processor is simulated to fit in the confines of that old system. This method will only report 32 cores under 32 bit as that is all the program can access in 32 bit mode. |
About GetMaximumProcessorCount, MS doc states that it returns the "maximum number of logical processors that a processor group or the system can have", so maybe it also includes "empty" CPU sockets. GetActiveProcessorCount, on the other hand, returns "the number of active processors in a processor group or in the system", which adds even more confusion. |
I was reviewing the docs for the os module and cpu_count should always return the number of cpus on the system, not the usable CPUs. GetMaximumProcessorCount returns a simulated count in WoW64. I have reached back out to the Windows API dev and will see if GetLogicalProcessorInformationEx will allow us to do this. He had thought that my solution that way had other limitations under WoW64. |
Fixed. Someone might backport this to 3.6 if they want. |
I reopen the issue to backport the bugfix to 3.6. |
Backport merged. Thanks Chris! |
One should be careful with this modification because of the Windows definition of process groups. For example, if multi-threaded code thinks that by reading the value of the new os.cpu_count() it can use all the cores returned, by default it cannot as in windows processes by default can run only in a single process group (how it worked before). We can see such code builtin python stdlib itself: cpython/Lib/concurrent/futures/thread.py Line 102 in bc61315
I think even .NET still uses the old way that python did until now: Although some of this stuff is used in code for python multiprocess code which that might actually get a boost (since different process can get scheduled to different groups) https://msdn.microsoft.com/en-us/library/windows/desktop/dd405503(v=vs.85).aspx |
os.cpu_count() is specified to return the total number of processors, not the number of usable processors. See e.g. https://bugs.python.org/issue26692 |
hi, Regarding https://bugs.python.org/issue30581#msg301150, I take your point that a lot of multiprocessing using the standard libraries may not benefit, as processes may be restricted to the processor group of the parent process (python). For my use case it works well: I launch a queue of blocking jobs, using a thread pool. Each thread launches 1 jobsubprocess.subprocess.run(), where the thread pool size is equal to number of processors reported by os.cpu_count(). Since the OS controls the scheduling in this case, it works perfectly well with 2 processor groups. thanks :-) |
Thanks for the heads up Rob! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: