summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-09-30 22:12:51 (GMT)
committerGitHub <noreply@github.com>2023-09-30 22:12:51 (GMT)
commitc81521020d643b4a5183098470ef7e6470facefb (patch)
tree9679b712dd9f4bfa003d6c8a0b188d34cf015025 /Modules
parent2c234196ea30b9da370780204ed9068f1fb134c6 (diff)
downloadcpython-c81521020d643b4a5183098470ef7e6470facefb.zip
cpython-c81521020d643b4a5183098470ef7e6470facefb.tar.gz
cpython-c81521020d643b4a5183098470ef7e6470facefb.tar.bz2
gh-109649: Add os.process_cpu_count() function (#109907)
* 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.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/clinic/posixmodule.c.h8
-rw-r--r--Modules/posixmodule.c73
2 files changed, 45 insertions, 36 deletions
diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h
index e77a31b..fc39ab7 100644
--- a/Modules/clinic/posixmodule.c.h
+++ b/Modules/clinic/posixmodule.c.h
@@ -10425,11 +10425,9 @@ PyDoc_STRVAR(os_cpu_count__doc__,
"cpu_count($module, /)\n"
"--\n"
"\n"
-"Return the number of CPUs in the system; return None if indeterminable.\n"
+"Return the number of logical CPUs in the system.\n"
"\n"
-"This number is not equivalent to the number of CPUs the current process can\n"
-"use. The number of usable CPUs can be obtained with\n"
-"``len(os.sched_getaffinity(0))``");
+"Return None if indeterminable.");
#define OS_CPU_COUNT_METHODDEF \
{"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__},
@@ -11988,4 +11986,4 @@ exit:
#ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#define OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */
-/*[clinic end generated code: output=51aa26bc6a41e1da input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8b60de6ddb925bc3 input=a9049054013a1b77]*/
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 0b25209..d3c0aa6 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -8133,39 +8133,45 @@ static PyObject *
os_sched_getaffinity_impl(PyObject *module, pid_t pid)
/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
{
- int cpu, ncpus, count;
+ int ncpus = NCPUS_START;
size_t setsize;
- cpu_set_t *mask = NULL;
- PyObject *res = NULL;
+ cpu_set_t *mask;
- ncpus = NCPUS_START;
while (1) {
setsize = CPU_ALLOC_SIZE(ncpus);
mask = CPU_ALLOC(ncpus);
- if (mask == NULL)
+ if (mask == NULL) {
return PyErr_NoMemory();
- if (sched_getaffinity(pid, setsize, mask) == 0)
+ }
+ if (sched_getaffinity(pid, setsize, mask) == 0) {
break;
+ }
CPU_FREE(mask);
- if (errno != EINVAL)
+ if (errno != EINVAL) {
return posix_error();
+ }
if (ncpus > INT_MAX / 2) {
- PyErr_SetString(PyExc_OverflowError, "could not allocate "
- "a large enough CPU set");
+ PyErr_SetString(PyExc_OverflowError,
+ "could not allocate a large enough CPU set");
return NULL;
}
- ncpus = ncpus * 2;
+ ncpus *= 2;
}
- res = PySet_New(NULL);
- if (res == NULL)
+ PyObject *res = PySet_New(NULL);
+ if (res == NULL) {
goto error;
- for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
+ }
+
+ int cpu = 0;
+ int count = CPU_COUNT_S(setsize, mask);
+ for (; count; cpu++) {
if (CPU_ISSET_S(cpu, setsize, mask)) {
PyObject *cpu_num = PyLong_FromLong(cpu);
--count;
- if (cpu_num == NULL)
+ if (cpu_num == NULL) {
goto error;
+ }
if (PySet_Add(res, cpu_num)) {
Py_DECREF(cpu_num);
goto error;
@@ -8177,12 +8183,12 @@ os_sched_getaffinity_impl(PyObject *module, pid_t pid)
return res;
error:
- if (mask)
+ if (mask) {
CPU_FREE(mask);
+ }
Py_XDECREF(res);
return NULL;
}
-
#endif /* HAVE_SCHED_SETAFFINITY */
#endif /* HAVE_SCHED_H */
@@ -14333,44 +14339,49 @@ os_get_terminal_size_impl(PyObject *module, int fd)
/*[clinic input]
os.cpu_count
-Return the number of CPUs in the system; return None if indeterminable.
+Return the number of logical CPUs in the system.
-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))``
+Return None if indeterminable.
[clinic start generated code]*/
static PyObject *
os_cpu_count_impl(PyObject *module)
-/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
+/*[clinic end generated code: output=5fc29463c3936a9c input=ba2f6f8980a0e2eb]*/
{
- int ncpu = 0;
+ int ncpu;
#ifdef MS_WINDOWS
-#ifdef MS_WINDOWS_DESKTOP
+# ifdef MS_WINDOWS_DESKTOP
ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
-#endif
+# else
+ ncpu = 0;
+# endif
+
#elif defined(__hpux)
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
+
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
ncpu = sysconf(_SC_NPROCESSORS_ONLN);
+
#elif defined(__VXWORKS__)
ncpu = _Py_popcount32(vxCpuEnabledGet());
+
#elif defined(__DragonFly__) || \
defined(__OpenBSD__) || \
defined(__FreeBSD__) || \
defined(__NetBSD__) || \
defined(__APPLE__)
- int mib[2];
+ ncpu = 0;
size_t len = sizeof(ncpu);
- mib[0] = CTL_HW;
- mib[1] = HW_NCPU;
- if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
+ int mib[2] = {CTL_HW, HW_NCPU};
+ if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) {
ncpu = 0;
+ }
#endif
- if (ncpu >= 1)
- return PyLong_FromLong(ncpu);
- else
+
+ if (ncpu < 1) {
Py_RETURN_NONE;
+ }
+ return PyLong_FromLong(ncpu);
}