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

os.process_cpu_count(): get the number CPUs usable by the current process #109649

Closed
vstinner opened this issue Sep 21, 2023 · 3 comments
Closed
Labels
type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Sep 21, 2023

Feature or enhancement

os.cpu_count() returns the total number of CPUs that the current machine has. On Unix, sched_setaffinity() can reduce the number of CPU "usable" by a process. Currently, os.cpu_count() docstring contains a recipe how to get the number of usable CPUs:

>>> import os; help(os.cpu_count)
cpu_count()
    Return the number of CPUs in the system; return None if indeterminable.
    
    This number is not equivalent to the number of CPUs the current process can
    use.  The number of usable CPUs can be obtained with
    ``len(os.sched_getaffinity(0))``

I propose to add an affinity optional keyword-only parameter to os.cpu_count() which would return len(os.sched_getaffinity(0)).st

UPDATE: Rename usable parameter to affinity.

Linked PRs

@vstinner vstinner added the type-feature A feature request or enhancement label Sep 21, 2023
@vstinner
Copy link
Member Author

vstinner commented Sep 21, 2023

os.cpu_count() issues (open and closed):

Previously, the function was known as multiprocessing.cpu_count():

vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
when the test completes!
@vstinner
Copy link
Member Author

On Windows, maybe it can be implemented with GetProcessGroupAffinity(), GetProcessAffinityMask(), GetActiveProcessorGroupCount() and GetActiveProcessorCount().

GetProcessGroupAffinity():
https://learn.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-getprocessgroupaffinity

@vstinner
Copy link
Member Author

vstinner commented Sep 21, 2023

On Windows, a CPU affinity can be run with start /affinity command. Example to only run Python on the firstCPU:

start /affinity 0x0000000000000001 python.exe

vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
Implement cpu_count(affinity=True) with sched_getaffinity() on Unix
and GetProcessAffinityMask() on Windows.

Changes:

* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs and mention
  that Linux cgroups are ignored.
* _Py_popcount32() uses UINT32_C() for M1, M2 and M4 constants.
* Add _Py_popcount64(). Add tests on _Py_popcount64().
@vstinner vstinner changed the title os.cpu_count(): add "usable" parameter to get the number of "usable" CPUs os.cpu_count(): add "affinity" parameter to get the number of logical CPUs usable by the current process Sep 21, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
Implement cpu_count(affinity=True) with sched_getaffinity() on Unix
and GetProcessAffinityMask() on Windows.

Changes:

* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs and mention
  that Linux cgroups are ignored.
* _Py_popcount32() uses UINT32_C() for M1, M2 and M4 constants.
* Add _Py_popcount64(). Add tests on _Py_popcount64().
vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
Implement cpu_count(affinity=True) with sched_getaffinity() on Unix
and GetProcessAffinityMask() on Windows.

Changes:

* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs and mention
  that Linux cgroups are ignored.
* _Py_popcount32() uses UINT32_C() for M1, M2 and M4 constants.
* Add _Py_popcount64(). Add tests on _Py_popcount64().
vstinner added a commit to vstinner/cpython that referenced this issue Sep 21, 2023
Implement cpu_count(affinity=True) with sched_getaffinity().

Changes:

* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs. Mention
  that Linux cgroups are ignored.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 26, 2023
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 26, 2023
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 26, 2023
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 29, 2023
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 30, 2023
* Refactor os_sched_getaffinity_impl(): move variable definitions to
  their first assignment.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
vstinner added a commit that referenced this issue Sep 30, 2023
* Refactor os_sched_getaffinity_impl(): move variable definitions to
  their first assignment.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
@vstinner vstinner changed the title os.cpu_count(): add "affinity" parameter to get the number of logical CPUs usable by the current process os.process_cpu_count(): get the number CPUs usable by the current process Sep 30, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Sep 30, 2023
Replace os.cpu_count() with os.process_cpu_count() in modules:

* compileall
* concurrent.futures
* multiprocessing

Replace os.cpu_count() with os.process_cpu_count() in programs:

* _decimal deccheck.py test
* freeze.py
* multissltests.py
* python -m test (regrtest)
* wasm_build.py

Other changes:

* test.pythoninfo logs os.process_cpu_count()
* regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 30, 2023
Replace os.cpu_count() with os.process_cpu_count() in modules:

* compileall
* concurrent.futures
* multiprocessing

Replace os.cpu_count() with os.process_cpu_count() in programs:

* _decimal deccheck.py test
* freeze.py
* multissltests.py
* python -m test (regrtest)
* wasm_build.py

Other changes:

* test.pythoninfo logs os.process_cpu_count()
* regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
vstinner added a commit to vstinner/cpython that referenced this issue Sep 30, 2023
Replace os.cpu_count() with os.process_cpu_count() in modules:

* compileall
* concurrent.futures
* multiprocessing

Replace os.cpu_count() with os.process_cpu_count() in programs:

* _decimal deccheck.py test
* freeze.py
* multissltests.py
* python -m test (regrtest)
* wasm_build.py

Other changes:

* test.pythoninfo logs os.process_cpu_count().
* regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
vstinner added a commit to vstinner/cpython that referenced this issue Oct 1, 2023
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
vstinner added a commit to vstinner/cpython that referenced this issue Oct 1, 2023
* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!
vstinner added a commit that referenced this issue Oct 1, 2023
Replace os.cpu_count() with os.process_cpu_count() in modules:

* compileall
* concurrent.futures
* multiprocessing

Replace os.cpu_count() with os.process_cpu_count() in programs:

* _decimal deccheck.py test
* freeze.py
* multissltests.py
* python -m test (regrtest)
* wasm_build.py

Other changes:

* test.pythoninfo logs os.process_cpu_count().
* regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
Yhg1s pushed a commit that referenced this issue Oct 2, 2023
* gh-109649: Enhance os.cpu_count() documentation

* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!

* Restore removed text
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 2, 2023
…H-110169)

* pythongh-109649: Enhance os.cpu_count() documentation

* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!

* Restore removed text
(cherry picked from commit 5245b97)

Co-authored-by: Victor Stinner <vstinner@python.org>
gpshead pushed a commit that referenced this issue Oct 3, 2023
) (#110226)

[3.12] gh-109649: Enhance os.cpu_count() documentation (GH-110169)

* gh-109649: Enhance os.cpu_count() documentation

* Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Doc: Specify that os.sched_getaffinity(0) is related to the calling
  thread.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask
  when the test completes!

* Restore removed text
(cherry picked from commit 5245b97)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit to vstinner/cpython that referenced this issue Nov 3, 2023
When CPUs are isolated on Linux, os.process_cpu_count() is smaller
than os.cpu_count(). Fix the test for this case.

Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux
command line to isolated two logical CPUs:

$ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())'
10 / 12
vstinner added a commit that referenced this issue Nov 3, 2023
When CPUs are isolated on Linux, os.process_cpu_count() is smaller
than os.cpu_count(). Fix the test for this case.

Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux
command line to isolated two logical CPUs:

$ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())'
10 / 12
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…n#111689)

When CPUs are isolated on Linux, os.process_cpu_count() is smaller
than os.cpu_count(). Fix the test for this case.

Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux
command line to isolated two logical CPUs:

$ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())'
10 / 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant