stouputils.system module#

What this process is actually allowed to use, as opposed to what the host reports.

The kernel interfaces every tool reads describe the machine, not the container. A pod capped at 12 of the machine’s 192 cores therefore reports 6 % CPU while it is saturated, and a 96 GiB cap against 2.4 TiB of host RAM reads as 4 % while the pod is about to be killed. Cgroup v2 is where the real ceiling is stated.

Outside a capped container both fall back to the host, which is the right answer there. Cgroup v1 is not read, its interface having been superseded on every distribution still receiving updates.

CPU_COUNT and MEMORY_MEGABYTES are what the rest of the package reads these through.

CPU_MAX_PATH: str = '/sys/fs/cgroup/cpu.max'[source]#

Cgroup v2 file holding "<quota> <period>" in microseconds, or "max <period>" when uncapped.

MEMORY_MAX_PATH: str = '/sys/fs/cgroup/memory.max'[source]#

Cgroup v2 file holding the memory cap in bytes, or "max" when uncapped.

MEMINFO_PATH: str = '/proc/meminfo'[source]#

Kernel file whose MemTotal line is the host fallback when no cgroup cap is set.

MEGABYTE: int = 1048576[source]#

Bytes in the mebibyte memory limits are reported in.

cpu_limit() float[source]#

Cores this process may use, falling back to the host’s count outside a capped container.

Fractional on purpose: a pod may be given half a core, and rounding that up or down is wrong either way. This is the ceiling itself, not a worker count. StouputilsConfig.CPU_COUNT is the latter, and it stays overridable by the usual thread-count environment variables.

>>> cpu_limit() > 0
True
memory_limit_megabytes() float[source]#

Mebibytes this process may use, falling back to the host’s total outside a capped container.

Returns 0.0 when neither is readable, which a caller reads as “unknown” rather than as “none left”.

>>> memory_limit_megabytes() >= 0
True
parse_cpu_max(raw: str) float | None[source]#

Cores a cgroup v2 cpu.max line allows, None when it sets no quota.

Parameters:

raw – Content of CPU_MAX_PATH, empty when the file is absent.

>>> parse_cpu_max("1200000 100000")
12.0
>>> parse_cpu_max("50000 100000")
0.5
>>> parse_cpu_max("max 100000") is None
True
>>> parse_cpu_max("") is None
True
parse_memory_max(raw: str) float | None[source]#

Mebibytes a cgroup v2 memory.max line allows, None when it sets no cap.

Parameters:

raw – Content of MEMORY_MAX_PATH, empty when the file is absent.

>>> parse_memory_max("103079215104")
98304.0
>>> parse_memory_max("max") is None
True
parse_meminfo_total(raw: str) float[source]#

Mebibytes the MemTotal line of /proc/meminfo reports, 0.0 when there is none.

>>> parse_meminfo_total("MemTotal:       16302056 kB")
15919.9765625
>>> parse_meminfo_total("")
0.0
read_limit_file(path: str) str[source]#

The file’s content, empty when it does not exist or cannot be read.

>>> read_limit_file("/nonexistent/cgroup/file")
''