Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
41be170
gh-109595: Add -Xcpu_count=<n> cmdline for container users
corona10 Sep 21, 2023
f7a7428
Check style
corona10 Sep 21, 2023
7009bbe
Add help
corona10 Sep 21, 2023
d1f91d8
Apply suggestions from code review
corona10 Sep 21, 2023
8c92ed6
Apply suggestions from code review
corona10 Sep 21, 2023
c27bdfc
Check style
corona10 Sep 21, 2023
b13e5ee
Address code review
corona10 Sep 21, 2023
45fce16
Address code review
corona10 Sep 21, 2023
49a48e4
Address code review
corona10 Sep 21, 2023
829a8e8
Address code review
corona10 Sep 21, 2023
cfb33a4
Fix test
corona10 Sep 21, 2023
95a2173
Update NEWS.d
corona10 Sep 21, 2023
0394d1d
Address code review
corona10 Sep 21, 2023
f4a3f01
Update
corona10 Sep 21, 2023
60a28fe
Update
corona10 Sep 21, 2023
7e5595c
nit
corona10 Sep 21, 2023
8678012
Update
corona10 Sep 21, 2023
cbc5484
Add PYTHONCPUCOUNT
corona10 Sep 21, 2023
4622b60
Add PYTHONCPUCOUNT
corona10 Sep 21, 2023
50f0178
Apply suggestions from code review
corona10 Sep 21, 2023
5c3ac68
Update Python/initconfig.c
corona10 Sep 21, 2023
a276bfd
Fix
corona10 Sep 21, 2023
9c582be
Update whatsnew
corona10 Sep 21, 2023
35b952f
nit
corona10 Sep 21, 2023
f04ea58
Check style
corona10 Sep 21, 2023
7ecf705
Fix docs
corona10 Sep 21, 2023
18dcd44
Update doc
corona10 Sep 21, 2023
b344f4f
nit
corona10 Sep 21, 2023
8069d21
Update
corona10 Sep 21, 2023
c70bc82
Update
corona10 Sep 21, 2023
c8abc29
Update
corona10 Sep 21, 2023
2ac6901
Address code review
corona10 Sep 21, 2023
f0a3ebf
Address code review
corona10 Sep 21, 2023
89d8bb2
Address Victor's suggestion
corona10 Sep 24, 2023
66c617f
nit
corona10 Sep 24, 2023
4a72ed4
Address Erlend's review
corona10 Sep 26, 2023
0426e3e
fix
corona10 Sep 26, 2023
ac5329b
Merge remote-tracking branch 'upstream/main' into gh-109595
corona10 Sep 30, 2023
e50e678
fix
corona10 Sep 30, 2023
24fe0e4
Add space
corona10 Sep 30, 2023
32843ed
Update os.py
corona10 Sep 30, 2023
a954f1c
nit
corona10 Sep 30, 2023
3ab2bc4
Add test code
corona10 Sep 30, 2023
7231697
Update docs
corona10 Sep 30, 2023
b18da0d
fix
corona10 Oct 1, 2023
3579fc4
Address code reivew
corona10 Oct 1, 2023
134ed9e
Address code review
corona10 Oct 1, 2023
2bec7f4
Add test
corona10 Oct 1, 2023
9f7cb5e
fix
corona10 Oct 1, 2023
1217ab5
fix
corona10 Oct 1, 2023
64da2f9
fix
corona10 Oct 1, 2023
64c7329
fix
corona10 Oct 1, 2023
cc54afb
Update
corona10 Oct 1, 2023
ba421c7
Address code review
corona10 Oct 1, 2023
5f20bf6
Update NEWS.d
corona10 Oct 1, 2023
c11789b
Update
corona10 Oct 1, 2023
936c182
Update
corona10 Oct 1, 2023
2f0dc1c
Address code review
corona10 Oct 1, 2023
a7b2c88
Rename to PYTHON_CPU_COUNT
corona10 Oct 1, 2023
551c76d
Hidden overrided cpu count
corona10 Oct 2, 2023
75021be
Use overridden
corona10 Oct 2, 2023
57dd53b
Minor refactoring
corona10 Oct 2, 2023
3f9da50
Revert to PYTHONCPUCOUNT
corona10 Oct 2, 2023
57e82c5
fix
corona10 Oct 2, 2023
c37c8d0
Use PYTHON_CPU_COUNT
corona10 Oct 3, 2023
c7726e5
Address Greg's review
corona10 Oct 3, 2023
de8bf53
Address Greg's code review
corona10 Oct 3, 2023
633914b
nit
corona10 Oct 3, 2023
37fbdfe
Include multiprocessing in docs, reword.
gpshead Oct 3, 2023
a0cfb21
Merge remote-tracking branch 'upstream/main' into gh-109595
corona10 Oct 3, 2023
8daa3b7
Merge remote-tracking branch 'upstream/main' into gh-109595
corona10 Oct 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5164,6 +5164,10 @@ Miscellaneous System Information

.. versionadded:: 3.4

.. versionchanged:: 3.13
if :samp:`-X cpu_count={n}` is given, :func:`cpu_count` will return the overrided
value *n*.


.. function:: getloadavg()

Expand Down
8 changes: 8 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ Miscellaneous options
report Python calls. This option is only available on some platforms and
will do nothing if is not supported on the current system. The default value
is "off". See also :envvar:`PYTHONPERFSUPPORT` and :ref:`perf_profiling`.
* :samp:`-X cpu_count={n}` will override the number of CPU count from system.
If this option is provided, :func:`os.cpu_count` will return the overrided value.
And *n* must be greater equal then 1.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* :samp:`-X cpu_count={n}` will override the number of CPU count from system.
If this option is provided, :func:`os.cpu_count` will return the overrided value.
And *n* must be greater equal then 1.
* :samp:`-X cpu_count={n}` overrides the number of CPU count from system:
:func:`os.cpu_count` returns *n*. The value *n* must be greater than
or equal to 1.

This option will be useful for users who need to limit CPU resources of a container system.
Comment thread
corona10 marked this conversation as resolved.
Outdated


It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.
Expand Down Expand Up @@ -593,6 +598,9 @@ Miscellaneous options
.. versionadded:: 3.12
The ``-X perf`` option.

.. versionadded:: 3.13
The ``-X cpu_count`` option.


Options you shouldn't use
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ typedef struct PyConfig {
int safe_path;
int int_max_str_digits;

// Python 3.13
int cpu_count;
Comment thread
vstinner marked this conversation as resolved.

/* --- Path configuration inputs ------------ */
int pathconfig_warnings;
wchar_t *program_name;
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,11 @@ def res2int(res):
)
self.assertEqual(res2int(res), (6000, 6000))

def test_cpu_count(self):
code = "import os; print(os.cpu_count())"
res = assert_python_ok('-X', 'cpu_count=4321', '-c', code)
self.assertEqual(res.out.strip().decode("utf-8"), '4321')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a lot of CPUs, it's exciting! Until recently, Linux was limited to 4096 CPUs! https://unix.stackexchange.com/questions/4507/how-many-cores-can-linux-kernel-handle



@unittest.skipIf(interpreter_requires_environment(),
'Cannot run -I tests when PYTHON env vars are required.')
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'use_hash_seed': 0,
'hash_seed': 0,
'int_max_str_digits': sys.int_info.default_max_str_digits,
'cpu_count': -1,
'faulthandler': 0,
'tracemalloc': 0,
'perf_profiling': 0,
Expand Down Expand Up @@ -894,6 +895,7 @@ def test_init_from_config(self):
'module_search_paths': self.IGNORE_CONFIG,
'safe_path': 1,
'int_max_str_digits': 31337,
'cpu_count': -1,

'check_hash_pycs_mode': 'always',
'pathconfig_warnings': 0,
Expand Down Expand Up @@ -966,6 +968,7 @@ def test_init_python_env(self):
'module_search_paths': self.IGNORE_CONFIG,
'safe_path': 1,
'int_max_str_digits': 4567,
'cpu_count': -1,
}
if Py_STATS:
config['_pystats'] = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :samp:`-Xcpu_count={n}` command line option for overriding system CPU resources from
Python program. Patch by Donghee Na.
Comment thread
corona10 marked this conversation as resolved.
Outdated
4 changes: 4 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -14349,6 +14349,10 @@ static PyObject *
os_cpu_count_impl(PyObject *module)
/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
{
const PyConfig *config = _Py_GetConfig();
if (config->cpu_count > 0) {
return PyLong_FromLong(config->cpu_count);
}
int ncpu = 0;
Comment thread
corona10 marked this conversation as resolved.
#ifdef MS_WINDOWS
#ifdef MS_WINDOWS_DESKTOP
Expand Down
1 change: 1 addition & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ static int test_init_from_config(void)

putenv("PYTHONINTMAXSTRDIGITS=6666");
config.int_max_str_digits = 31337;
config.cpu_count = -1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please set a more interesting value like 1234?


init_from_config_clear(&config);

Expand Down
41 changes: 40 additions & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ The following implementation-specific options are available:\n\
\n\
-X int_max_str_digits=number: limit the size of int<->str conversions.\n\
This helps avoid denial of service attacks when parsing untrusted data.\n\
The default is sys.int_info.default_max_str_digits. 0 disables."
The default is sys.int_info.default_max_str_digits. 0 disables.\n\
\n\
-X cpu_count=n: override CPU count of os.cpu_count().\n\
Comment thread
corona10 marked this conversation as resolved.
Outdated
This helps for users who need to limit CPU resources of a container system."

#ifdef Py_STATS
"\n\
Expand Down Expand Up @@ -633,6 +636,8 @@ config_check_consistency(const PyConfig *config)
assert(config->_is_python_build >= 0);
assert(config->safe_path >= 0);
assert(config->int_max_str_digits >= 0);
// cpu_count can be -1 if the user doesn't override it.
assert(config->cpu_count != 0);
Comment thread
corona10 marked this conversation as resolved.
// config->use_frozen_modules is initialized later
// by _PyConfig_InitImportConfig().
#ifdef Py_STATS
Expand Down Expand Up @@ -732,6 +737,7 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->int_max_str_digits = -1;
config->_is_python_build = 0;
config->code_debug_ranges = 1;
config->cpu_count = -1;
}


Expand Down Expand Up @@ -959,6 +965,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_WSTRLIST(orig_argv);
COPY_ATTR(_is_python_build);
COPY_ATTR(int_max_str_digits);
COPY_ATTR(cpu_count);
Comment thread
corona10 marked this conversation as resolved.
Outdated
#ifdef Py_STATS
COPY_ATTR(_pystats);
#endif
Expand Down Expand Up @@ -1069,6 +1076,7 @@ _PyConfig_AsDict(const PyConfig *config)
SET_ITEM_INT(safe_path);
SET_ITEM_INT(_is_python_build);
SET_ITEM_INT(int_max_str_digits);
SET_ITEM_INT(cpu_count);
#ifdef Py_STATS
SET_ITEM_INT(_pystats);
#endif
Expand Down Expand Up @@ -1379,6 +1387,7 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
GET_UINT(safe_path);
GET_UINT(_is_python_build);
GET_INT(int_max_str_digits);
GET_INT(cpu_count);
#ifdef Py_STATS
GET_UINT(_pystats);
#endif
Expand Down Expand Up @@ -1678,6 +1687,29 @@ config_read_env_vars(PyConfig *config)
return _PyStatus_OK();
}

static PyStatus
config_init_cpu_count(PyConfig *config)
{
int cpu_count = -1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can move the variable declaration inside the "if (sep)" block. Does it have to be initialized?

If you are afraid of undefined behavior, maybe config_wstr_to_int() should set *result to 0 on error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now I added PYTHONCPUCOUNT envvar, so the current structure will be needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you want, you can move the variable declaration inside each "if (env)" block and duplicate the variable, to better show its scope. But well, that's just a personal preference. Feel free to ignore my coding style remark ;-)

const wchar_t *xoption = config_get_xoption(config, L"cpu_count");
Comment thread
corona10 marked this conversation as resolved.
if (xoption) {
const wchar_t *sep = wcschr(xoption, L'=');
if (sep) {
if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) {
goto error;
}
}
else {
goto error;
}
config->cpu_count = cpu_count;
}
return _PyStatus_OK();

error:
Comment thread
corona10 marked this conversation as resolved.
return _PyStatus_ERR("-X cpu_count=n option: n is missing or an invalid number");
Comment thread
corona10 marked this conversation as resolved.
Outdated
}

static PyStatus
config_init_perf_profiling(PyConfig *config)
{
Expand Down Expand Up @@ -1860,6 +1892,13 @@ config_read_complex_options(PyConfig *config)
}
}

if (config->cpu_count < 0) {
status = config_init_cpu_count(config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}

if (config->pycache_prefix == NULL) {
status = config_init_pycache_prefix(config);
if (_PyStatus_EXCEPTION(status)) {
Expand Down