mstar.utils.cpus#

The CPUs this process may use, and the plan every process of a deployment reads.

os.cpu_count() reports the host’s CPUs. In a container the cgroup CPU quota can be far smaller, and a thread pool sized to the host then runs more threads than the quota lets run, so a fork-join op waits on whichever thread the scheduler parked.

CpuPlan holds the process mask, how many of its CPUs the quota lets run, the CPUs the step_thread_core knob pins (each worker’s step threads and the API server’s result threads, see mstar.utils.threads), and the pool sizes, taken from the CPUs left after those. The API server and every worker build it with deployment_plan from the same inputs (the knob, the mask, the deployment’s devices), so they agree on it without talking.

Placement under always and siblings is by the device’s rank among the host’s GPUs on its NUMA node, as sysfs lists them (every GPU of the host, not only the ones this process was given), so servers on different GPUs pick different CPUs without knowing about each other. The API server takes the core at its first device’s rank from the top of the node’s allowed CPUs, and a worker the step unit at its device’s rank from the bottom of the cores below the API servers’. Under always a unit is STEP_CORES cores, one hardware thread of each, so the cores a GPU picks from must hold four per GPU, and no two pinned threads share a core; siblings (_siblings) puts a worker on one core’s hardware threads instead. A mask too small across the host may still do for the GPUs this container can open, which are the only ones that can share it, so those rank instead. Failing that, the GPUs this process was given rank, and the mask is cut into as many core sets as it holds for them. Servers on one GPU can’t be told apart by rank, so the first holds a lock on it (claim_gpus) and the next pins nothing. Under the last rule servers on different GPUs can’t either, so every server that pins also locks its cores (claim_cores), and one that finds a set held takes the next.

Pinning can pay where a thread woken on an idle core waits for that core to leave a deep idle state, and costs throughput where it doesn’t, since the threads only queue on their CPUs. Which layout pays depends on the host: on a deep-idle Xeon one core’s hardware threads beat no pin and separate cores lost to it, while one core’s hardware threads lost on two H200 hosts. So auto pins nothing and logs why (_auto_declines): the reason names the idle hold, a CPU vendor other than Intel, idle states under DEEP_IDLE_US (deepest_idle), or what the separate cores measured.

Functions

cgroup_cpu_quota([root, proc_cgroup])

The tightest cgroup v2 cpu.max quota over this process's cgroup and its ancestors, in CPUs; None when no level sets one (or cgroup v2 is absent).

claim_cores(first, build[, siblings])

The first of the deployment's core sets (first is set 0's plan, build(slot) gives the others) whose physical cores this process can lock, holding those locks for its life; else a plan that pins nothing and says who holds them.

claim_gpus(addresses)

Hold a lock on each GPU for the life of this process; if one can't be had, hold none of them and say why.

cpu_vendor([cpuinfo])

The first vendor_id /proc/cpuinfo lists (GenuineIntel, AuthenticAMD); None where it lists none, as on Arm, or can't be read.

deepest_idle(cpus[, root])

The idle state of cpus with the longest exit latency, leaving out states disabled in sysfs; None where sysfs lists none (a VM that exposes no cpuidle).

deployment_plan(setting, addresses, *[, ...])

The plan for a deployment whose workers' devices sit at PCI addresses, in the conductor's worker order (None for a device sysfs doesn't place, such as a CPU worker; only always and siblings read them), reachable the GPUs this container can open, own the ones this process was given, and slot the core set to take when the mask holds more than one (claim_cores).

device_addresses(devices)

The PCI address of each worker's device (cuda:1); None for a CPU worker, or where the platform doesn't report one.

devices_plan(setting, devices[, idle_hold, slot])

deployment_plan for the workers' device strings, in the conductor's worker order: the same call in the API server and in every worker.

format_cpu_list(cpus)

cpus as ranges, the inverse of parse_cpu_list.

hold_idle([device])

Hold a PM QoS CPU latency request of IDLE_HOLD_US for the life of this process, and say what happened, for the log.

host_gpus(like[, root])

The host's GPUs in PCI address order: the functions with the vendor and class of the device at like (0000:40:00.0).

install(new)

Make new this process's plan (None: back to nothing pinned).

l2_cluster(cpu)

The CPUs sharing cpu's L2 cluster, cpu included.

parse_cpu_list(text)

The CPUs in a list such as 0-3,8,10-11 (as sysfs and taskset write them); empty for an empty string.

plan()

This process's plan: the one install set, else nothing pinned.

process_mask()

The CPUs this process may run on; every CPU where affinity is unknown.

smt_siblings(cpu)

The hardware threads of cpu's core, cpu included.

usable_cpus()

CPUs this process may run on (its affinity, else os.cpu_count()), capped by the cgroup quota rounded down.

Classes

CpuPlan(mask, usable[, workers, result, ...])

See the module docstring.

HostGpu(address, node, local)

One of the host's GPUs as sysfs lists it: its PCI address, NUMA node and the node's CPUs (None where sysfs doesn't say).

IdleState(name, latency_us)

An idle state a CPU may enter, as sysfs names it, with its exit latency.

StepCpus(cpus, why)

The CPUs one worker's main, GPU and plan threads share, and how they were chosen, for the log.

Exceptions

CpuPlanError

A step_thread_core value the process mask can't honour.

class mstar.utils.cpus.CpuPlan(mask, usable, workers=(), result=None, why='step_thread_core off', declined=False, sets=1, slot=0, locked='')[source]#

Bases: object

See the module docstring. workers has one entry per worker of the deployment, in the conductor’s worker order (empty, or all None, when nothing is pinned).

Parameters:
property copy_workers: int#

Threads a large output read copies over (host_copy.HostCopier): one encode’s share of the CPUs left. Reads run one at a time on the data worker’s thread, each ahead of its own encode.

declined: bool = False#
property deflate_threads: int#

Threads of the deflate pool every PNG of the process shares (image_io): enough for encode_workers images finishing together to deflate all their blocks at once.

property encode_workers: int#

Outputs the data worker encodes at once. A PNG deflates png_threads blocks at once, so the pool times that stays within the CPUs left.

property left: int#

what the pools are sized from.

Type:

The CPUs the quota lets run, less the pinned ones

locked: str = ''#
mask: frozenset[int]#
property pinned: frozenset[int]#
property png_threads: int#

Blocks one PNG deflates in parallel (image_io.uint8_to_png).

prepare_workers(threads)[source]#

Requests the data worker prepares at once, for a model that declares PREPROCESS_PARALLEL, when each runs on threads intra-op threads: at most 8, and the pool times the threads stays within the CPUs left.

Parameters:

threads (int)

Return type:

int

property preprocess_threads: int#

The API server’s intra-op threads when neither the config nor the model sets them. A quarter of the CPUs left, at most 4: the process shares them with the engine workers, and its per-request ops are a few MB.

property preprocess_workers: int#

prepare_workers at the default thread count.

result: int | None = None#
sets: int = 1#
slot: int = 0#
usable: int#
property video_chunks: int#

Pieces one H.264 clip encodes in parallel (video_encode.encode_mp4_chunked). Each piece restarts x264’s reference chain, which lowers quality for a few frames after it; 8 pieces were about 20 ms faster on a 121-frame clip.

property video_encode_threads: int#

enough for encode_workers clips finishing together to encode all their pieces at once, as deflate_threads is for PNGs.

Type:

Workers of the pool every chunked clip of the process shares

why: str = 'step_thread_core off'#
workers: tuple[StepCpus | None, ...] = ()#
exception mstar.utils.cpus.CpuPlanError[source]#

Bases: ValueError

A step_thread_core value the process mask can’t honour.

class mstar.utils.cpus.HostGpu(address, node, local)[source]#

Bases: NamedTuple

One of the host’s GPUs as sysfs lists it: its PCI address, NUMA node and the node’s CPUs (None where sysfs doesn’t say).

Parameters:
address: str#

Alias for field number 0

local: frozenset[int] | None#

Alias for field number 2

node: str | None#

Alias for field number 1

class mstar.utils.cpus.IdleState(name, latency_us)[source]#

Bases: NamedTuple

An idle state a CPU may enter, as sysfs names it, with its exit latency.

Parameters:
latency_us: int#

Alias for field number 1

name: str#

Alias for field number 0

class mstar.utils.cpus.StepCpus(cpus, why)[source]#

Bases: NamedTuple

The CPUs one worker’s main, GPU and plan threads share, and how they were chosen, for the log.

Parameters:
cpus: frozenset[int]#

Alias for field number 0

why: str#

Alias for field number 1

mstar.utils.cpus.cgroup_cpu_quota(root=_CGROUP_ROOT, proc_cgroup='/proc/self/cgroup')[source]#

The tightest cgroup v2 cpu.max quota over this process’s cgroup and its ancestors, in CPUs; None when no level sets one (or cgroup v2 is absent).

Parameters:
Return type:

float | None

mstar.utils.cpus.claim_cores(first, build, siblings=None)[source]#

The first of the deployment’s core sets (first is set 0’s plan, build(slot) gives the others) whose physical cores this process can lock, holding those locks for its life; else a plan that pins nothing and says who holds them.

Every plan that pins goes through this, a CPU number and siblings included, so no two servers that share _GPU_LOCK_DIR pin threads on one core. The kernel drops an flock however the process exits, and the pid in the file is only for the next server’s log: a file naming a process that has exited is taken over. Like the GPU lock, it doesn’t reach a server in another container with its own /tmp.

Parameters:
Return type:

CpuPlan

mstar.utils.cpus.claim_gpus(addresses)[source]#

Hold a lock on each GPU for the life of this process; if one can’t be had, hold none of them and say why.

Placement depends only on the PCI address and the mask, so two servers on one GPU (two MIG instances report one address) would take the same CPUs. The kernel drops an flock however the process exits, so a dead server leaves nothing to clean up. The file is never unlinked: another process may already hold an fd on this inode, and removing it would let a third lock a different one. The lock only reaches servers that share _GPU_LOCK_DIR.

Parameters:

addresses (Iterable[str | None])

Return type:

str | None

mstar.utils.cpus.cpu_vendor(cpuinfo=None)[source]#

The first vendor_id /proc/cpuinfo lists (GenuineIntel, AuthenticAMD); None where it lists none, as on Arm, or can’t be read.

Parameters:

cpuinfo (Path | None)

Return type:

str | None

mstar.utils.cpus.deepest_idle(cpus, root=_CPU_ROOT)[source]#

The idle state of cpus with the longest exit latency, leaving out states disabled in sysfs; None where sysfs lists none (a VM that exposes no cpuidle).

Parameters:
Return type:

IdleState | None

mstar.utils.cpus.deployment_plan(setting, addresses, *, mask=None, usable=None, pci_root=_PCI_DEVICES, siblings=smt_siblings, cluster=l2_cluster, cpu_root=_CPU_ROOT, reachable=(), own=(), slot=0, cpuinfo=None, idle_hold='off')[source]#

The plan for a deployment whose workers’ devices sit at PCI addresses, in the conductor’s worker order (None for a device sysfs doesn’t place, such as a CPU worker; only always and siblings read them), reachable the GPUs this container can open, own the ones this process was given, and slot the core set to take when the mask holds more than one (claim_cores).

off pins nothing, and auto pins nothing and says why. A CPU number is the first worker’s core; the next workers take the next cores above it, and the API server the last core of the mask; a number outside the mask, or too near its top for every worker, raises CpuPlanError. always and siblings place by device rank (module docstring), and pin nothing, saying why in CpuPlan.why, when the mask is too small. At least one CPU of the mask stays unpinned.

Parameters:
Return type:

CpuPlan

mstar.utils.cpus.device_addresses(devices)[source]#

The PCI address of each worker’s device (cuda:1); None for a CPU worker, or where the platform doesn’t report one.

Parameters:

devices (Sequence[str])

Return type:

list[str | None]

mstar.utils.cpus.devices_plan(setting, devices, idle_hold='off', slot=None)[source]#

deployment_plan for the workers’ device strings, in the conductor’s worker order: the same call in the API server and in every worker. A worker takes the core set its API server locked (CORE_SET_ENV).

Parameters:
Return type:

CpuPlan

mstar.utils.cpus.format_cpu_list(cpus)[source]#

cpus as ranges, the inverse of parse_cpu_list.

Parameters:

cpus (Iterable[int])

Return type:

str

mstar.utils.cpus.hold_idle(device=_PM_QOS_DEVICE)[source]#

Hold a PM QoS CPU latency request of IDLE_HOLD_US for the life of this process, and say what happened, for the log. The kernel keeps every CPU of the host out of idle states slower than that while the fd stays open, and drops the request however the process exits. Failing to get it costs only the hold, so this never raises.

Parameters:

device (Path)

Return type:

str

mstar.utils.cpus.host_gpus(like, root=_PCI_DEVICES)[source]#

The host’s GPUs in PCI address order: the functions with the vendor and class of the device at like (0000:40:00.0). Every process on the host sees the same list, whatever GPUs it was given; empty without sysfs.

Parameters:
Return type:

list[HostGpu]

mstar.utils.cpus.install(new)[source]#

Make new this process’s plan (None: back to nothing pinned).

Parameters:

new (CpuPlan | None)

Return type:

None

mstar.utils.cpus.l2_cluster(cpu)[source]#

The CPUs sharing cpu’s L2 cluster, cpu included.

Parameters:

cpu (int)

Return type:

frozenset[int]

mstar.utils.cpus.parse_cpu_list(text)[source]#

The CPUs in a list such as 0-3,8,10-11 (as sysfs and taskset write them); empty for an empty string.

Parameters:

text (str)

Return type:

frozenset[int]

mstar.utils.cpus.plan()[source]#

This process’s plan: the one install set, else nothing pinned.

Return type:

CpuPlan

mstar.utils.cpus.process_mask()[source]#

The CPUs this process may run on; every CPU where affinity is unknown.

Return type:

frozenset[int]

mstar.utils.cpus.smt_siblings(cpu)[source]#

The hardware threads of cpu’s core, cpu included.

Parameters:

cpu (int)

Return type:

frozenset[int]

mstar.utils.cpus.usable_cpus()[source]#

CPUs this process may run on (its affinity, else os.cpu_count()), capped by the cgroup quota rounded down. At least 1.

Return type:

int