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
|
The tightest cgroup v2 |
|
The first of the deployment's core sets ( |
|
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. |
|
The first |
|
The idle state of |
|
The plan for a deployment whose workers' devices sit at PCI |
|
The PCI address of each worker's device ( |
|
|
|
|
|
Hold a PM QoS CPU latency request of |
|
The host's GPUs in PCI address order: the functions with the vendor and class of the device at |
|
Make |
|
The CPUs sharing |
|
The CPUs in a list such as |
|
This process's plan: the one |
The CPUs this process may run on; every CPU where affinity is unknown. |
|
|
The hardware threads of |
CPUs this process may run on (its affinity, else |
Classes
|
See the module docstring. |
|
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). |
|
An idle state a CPU may enter, as sysfs names it, with its exit latency. |
|
The CPUs one worker's main, GPU and plan threads share, and how they were chosen, for the log. |
Exceptions
A |
- class mstar.utils.cpus.CpuPlan(mask, usable, workers=(), result=None, why='step_thread_core off', declined=False, sets=1, slot=0, locked='')[source]#
Bases:
objectSee the module docstring.
workershas 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.
- property deflate_threads: int#
Threads of the deflate pool every PNG of the process shares (
image_io): enough forencode_workersimages finishing together to deflate all their blocks at once.
- property encode_workers: int#
Outputs the data worker encodes at once. A PNG deflates
png_threadsblocks 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
- prepare_workers(threads)[source]#
Requests the data worker prepares at once, for a model that declares
PREPROCESS_PARALLEL, when each runs onthreadsintra-op threads: at most 8, and the pool times the threads stays within the CPUs left.
- 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 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.
- exception mstar.utils.cpus.CpuPlanError[source]#
Bases:
ValueErrorA
step_thread_corevalue the process mask can’t honour.
- class mstar.utils.cpus.HostGpu(address, node, local)[source]#
Bases:
NamedTupleOne 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).
- class mstar.utils.cpus.IdleState(name, latency_us)[source]#
Bases:
NamedTupleAn idle state a CPU may enter, as sysfs names it, with its exit latency.
- class mstar.utils.cpus.StepCpus(cpus, why)[source]#
Bases:
NamedTupleThe CPUs one worker’s main, GPU and plan threads share, and how they were chosen, for the log.
- mstar.utils.cpus.cgroup_cpu_quota(root=_CGROUP_ROOT, proc_cgroup='/proc/self/cgroup')[source]#
The tightest cgroup v2
cpu.maxquota over this process’s cgroup and its ancestors, in CPUs; None when no level sets one (or cgroup v2 is absent).
- mstar.utils.cpus.claim_cores(first, build, siblings=None)[source]#
The first of the deployment’s core sets (
firstis 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
siblingsincluded, so no two servers that share_GPU_LOCK_DIRpin threads on one core. The kernel drops anflockhowever 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.
- 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
flockhowever 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.
- mstar.utils.cpus.cpu_vendor(cpuinfo=None)[source]#
The first
vendor_id/proc/cpuinfolists (GenuineIntel,AuthenticAMD); None where it lists none, as on Arm, or can’t be read.
- mstar.utils.cpus.deepest_idle(cpus, root=_CPU_ROOT)[source]#
The idle state of
cpuswith the longest exit latency, leaving out states disabled in sysfs; None where sysfs lists none (a VM that exposes no cpuidle).
- 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; onlyalwaysandsiblingsread them),reachablethe GPUs this container can open,ownthe ones this process was given, andslotthe core set to take when the mask holds more than one (claim_cores).offpins nothing, andautopins 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, raisesCpuPlanError.alwaysandsiblingsplace by device rank (module docstring), and pin nothing, saying why inCpuPlan.why, when the mask is too small. At least one CPU of the mask stays unpinned.- Parameters:
mask (Collection[int] | None)
usable (int | None)
pci_root (Path)
cpu_root (Path)
reachable (Collection[str])
own (Collection[str])
slot (int)
cpuinfo (Path | None)
idle_hold (str)
- Return type:
- 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.
- mstar.utils.cpus.devices_plan(setting, devices, idle_hold='off', slot=None)[source]#
deployment_planfor 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).
- mstar.utils.cpus.hold_idle(device=_PM_QOS_DEVICE)[source]#
Hold a PM QoS CPU latency request of
IDLE_HOLD_USfor 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.
- 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.
- mstar.utils.cpus.install(new)[source]#
Make
newthis process’s plan (None: back to nothing pinned).- Parameters:
new (CpuPlan | None)
- Return type:
None
- mstar.utils.cpus.parse_cpu_list(text)[source]#
The CPUs in a list such as
0-3,8,10-11(as sysfs andtasksetwrite them); empty for an empty string.
- mstar.utils.cpus.plan()[source]#
This process’s plan: the one
installset, else nothing pinned.- Return type:
- mstar.utils.cpus.process_mask()[source]#
The CPUs this process may run on; every CPU where affinity is unknown.