Repository navigation
SMT/NUMA-aware CPU placement for pinned applications - #6335
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #6335 +/- ##
==========================================
+ Coverage 29.30% 31.27% +1.96%
==========================================
Files 550 573 +23
Lines 99502 102817 +3315
==========================================
+ Hits 29155 32151 +2996
- Misses 68049 68292 +243
- Partials 2298 2374 +76 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
48c2750 to
88f40c7
Compare
2b47086 to
7125ffc
Compare
7125ffc to
2235f31
Compare
|
Claude finished @rene's task in 5m 24s —— View job Review: SMT/NUMA-aware CPU placement (#6335)
This is a large, unusually well-structured and well-documented change. I reviewed the new files directly and the modified files via their PR commits (the branch has no merge-base with FindingsThe design is sound. I could not find a correctness bug I can stand behind. The items worth noting:
Things I specifically checked and found correct
Observation (not blocking)Patch coverage is 74% with |
| if err != nil || n < 0 { | ||
| return 0, false | ||
| } | ||
| return uint(n), true |
There was a problem hiding this comment.
strconv.ParseInt(s, 10, 64) followed by uint(n) here (and the identical pattern at line 321 in readRequiredUint) is what CodeQL keeps flagging (alerts 2707/2708/2933). In practice this is not a real truncation bug on EVE — all targets (amd64/arm64/riscv64) are 64-bit, so uint is 64-bit and the n < 0 guard makes the conversion lossless. But since the parsed values (core_id, package_id, NUMA id) are small sysfs integers, parsing with ParseInt(s, 10, 32) (or ParseUint with the negative-sentinel handled separately) would silence the scanner at no cost and keep the PR's CodeQL gate clean.
| domain = &cacheDomain{ | ||
| Level: int(level), | ||
| Type: cacheType, | ||
| ID: uint32(id), |
There was a problem hiding this comment.
Same class of CodeQL finding (alerts 2709–2712): id/cpu/level come from readUintFile, which parses with ParseUint(..., 64), so uint32(id)/int(level) are flagged as unchecked narrowing. These are harmless in practice (cache ids, levels and CPU numbers are tiny), but having readUintFile parse with bit size 32 — or storing these as uint64 — would clear the scanner. Low priority, just to keep the CodeQL gate green.
d0ea3d3 to
7306eac
Compare
This comment has been minimized.
This comment has been minimized.
7306eac to
29ded71
Compare
This comment has been minimized.
This comment has been minimized.
CPU placement confines threads, not only processes. libvirt's layout for a QEMU domain -- a threaded subtree with one group per vCPU and one for every other thread -- needs a cgroup made threaded (cgroup.type), controllers handed to the children of the domain's group (cgroup.subtree_control) and threads moved one at a time (cgroup.threads). Keeping host processes off the dedicated CPUs needs the processes in a group listed and moved (cgroup.procs) and the controllers a group delegates read back. MakeThreaded, EnableControllers and AddThread serve the first; Processes, AddProcess and SubtreeControl the second. Each is a single read or write of the cgroup's own file, so the kernel's rules -- a kernel thread cannot be moved, a thread only within its process's threaded subtree -- stay the kernel's to enforce and come back as the write's error. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Introduces the device-internal representation of a workload's CPU placement intent (CPUPlacementPolicy, on VmConfig): the pool its CPUs come from (housekeeping, dedicated or isolated), whether its vCPUs are pinned, and the refinements of a placement on CPUs of its own -- full-pcpus-only, threads per core and NUMA policy, in the Kubernetes CPU and Topology Managers' terms, and IO placement. Each enum has an Unknown value for one a newer controller sends that this EVE does not know, so a workload asking for it can be refused naming the field (UnknownField) rather than read as unset. Intent is kept deliberately separate from the allocator's vocabulary. Intent says what a workload needs; the allocator decides which host CPUs it gets. Keeping them apart means the wire format never dictates the placement mechanism. ResolveLegacy applies the one rule for a controller that does not send the pool: pin_cpu=true alone means a dedicated workload, pinned unless it is a container; with neither, the housekeeping pool, so behaviour is unchanged for a controller that sets none of this. Anything else is kept as sent, so domainmgr refuses what it does not serve instead of guessing. HasOwnCPUs says whether a policy gives the workload CPUs of its own. A workload's placement is one CPUPlacementState, on DomainStatus and mirrored on AppInstanceStatus: domainmgr's verdict on the request (CPUPlacementVerdict: placed, placed worse than planned, or why it was refused), the CPUs it holds (CPUAssignment), the workloads it has to make room for in a repack (RepackFor) and a wait for the CPUs planned for it, held by workloads on their way off them (CPUWait: the holders, and since when). A placement worse than planned is a verdict rather than an error: the workload runs normally, and whether the improvement is worth a restart is a judgement for an operator. RepackFor is kept apart from the verdict, because a workload standing in another's way is not itself placed badly; the status a controller sees, waiting or part of a repack included, is derived from the whole state. CPUAssignment is what the workload holds, the CPU counterpart of its assigned adapters: its CPUs, in vCPU order when it is pinned and ascending otherwise, the siblings it parks, the CPUs its non-vCPU threads run on under io_placement=housekeeping (IOCPUs) and the guest topology (CPUTopology). The parked siblings are kept out of the domain's cpuset (DomainCPUs): they are held so that nothing shares the workload's cores, its own threads included. EmulatorCPUs is where the non-vCPU threads run, and StartCPUs the CPUs a domain starts on. CPUPoolUtilization, CPUPoolIssue and CPUPoolWarning describe the node's pools, numbered as eve-api numbers them, with the mistakes in how isolcpus defines the isolated pool; CPUDemandSet is the set of workloads placement must account for. Capabilities gains CPUTopologyPinning: the hypervisor can bind single vCPUs and show the guest a topology. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Replaces the CPU allocator with one that understands the machine's topology, so a workload asking for dedicated CPUs can be given whole physical cores in a NUMA-local, SMT-aware way rather than an arbitrary set of logical CPUs. A best-effort request no single NUMA node can hold spans as few nodes as can hold it, those with the most free cores first; best effort is also what a request that leaves NUMA unset gets. Placement is computed for the whole set of pinned workloads at once and ordered by how constrained each one is -- whole-core-SMT first, since it can only use a core that really has two hardware threads and on a hybrid or SMT-disabled machine most cores cannot, then one-per-core, then anything thread-granular. The result is therefore a function of the request set rather than of the order requests arrive in. Allocating incrementally meant whichever workload activated first won the scarce cores, so a flexible workload could take the only SMT-capable core and leave a workload that needs one unplaceable -- and the same set of workloads could land differently on each boot. Nothing is decided by side effect. Plan works on a scratch copy; Propose says where one request would go among the CPUs free now and changes nothing; Claim makes an assignment the workload's, every CPU of it or none, in one step, after checking it against the topology and the CPUs held, so two workloads placed at once cannot both take a CPU. That is what lets a workload that has not started yet, or starts late, still claim the CPUs set aside for it. A workload placed off its plan names the CPUs to avoid, those the plan sets aside for workloads not started yet. Thread-granular requests go through the same Propose as whole-core ones. Score ranks an assignment by what actually costs performance -- NUMA nodes spanned, then last-level caches -- and deliberately not by which CPU indices were used. Many assignments share the best score, so comparing indices would report a workload as mis-placed merely because its first-choice CPUs were taken, and demand a restart that changes nothing. A core is withheld when any of its siblings is reserved for EVE. That costs capacity, so the shortage message says as much: handing out a core whose sibling runs housekeeping would reintroduce exactly the interference whole-core placement is bought to remove. The shortage also carries how many cores were needed against how many were free, so a caller can explain the refusal without computing a second, differently-filtered count. A CPU reserved for EVE is never given out, so the housekeeping set, where the host's processes, every workload without CPUs of its own and the IO threads of io_placement=housekeeping workloads run, always holds them: the dedicated pool can grow to every CPU but these. Kernel-isolated CPUs are capacity with exactly one claimant. The placer holds the isolated set and withholds every core touching it from a request that did not ask for isolation, thread-granular ones included; a request that requires isolation is served only from wholly isolated cores. Not a preference either way: handing isolated cores to whoever asked first would spend the operator's isolation on a workload that never wanted it and leave the one that needs it unplaceable, and placing a workload that asked for isolation on an unshielded core would tell it it is protected when it is not. A core only partly isolated, as an isolcpus written for an SMT-off boot leaves each core, serves no placement at all. A shortage names what stands in the way so it is not read against CPUs that look idle: for an ordinary request the cores withheld for isolation, for a request for isolation the isolated cores other workloads hold and the cores only partly isolated -- not, say, EVE's core, which could never have served it. IsolationIssues names each mistake in an isolcpus that covers the CPUs reserved for EVE or isolates part of a core, for the caller to report; the operator writes isolcpus, so EVE warns rather than refuses. A request no core of the node could serve -- whole-core SMT where no core has two threads, anything on a topology EVE could not read -- is TopologyUnsupported, not a shortage; an assignment that fails the placer's own consistency check is an InternalError, a fault in EVE rather than in the request. The placer is the one place CPUs are classified: kernel-isolated, reserved for EVE, held, or housekeeping, the set every workload without CPUs of its own runs on. Pools reports the housekeeping, dedicated and isolated pools with their CPU sets and whole-core counts, derived from what is held. The pools partition the node, as the API defines them: ISOLATED is the kernel's isolated set, free or held; DEDICATED is the CPUs held outside it; HOUSEKEEPING is the rest. Free threads alone answer "will it fit?" wrongly: threads left on partially-owned cores cannot satisfy a request for whole cores. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Realizing a whole-core placement on QEMU/KVM has three parts. The guest is launched with an -smp topology computed from the assignment, so software inside it sees the real SMT structure and can place its own hot work on non-sibling cores. A poll-mode datapath deliberately runs a worker on each sibling; without a truthful topology it cannot tell which vCPUs share a core. Every QEMU domain's threads are arranged in a threaded subtree of its cgroup, the layout libvirt uses: a vcpu<i> group per vCPU thread and an emulator group for every other one -- main loop, iothreads, vhost and IO workers. A thread starts in the group of the thread that created it, so one QEMU or the kernel starts later is confined by the kernel, not by a pass that would have to find it. For a domain with CPUs of its own, vcpu<i> holds vCPU i's host CPU alone and emulator the IO CPUs under io_placement=housekeeping; a group whose CPUs are the domain's leaves its cpuset empty and follows the domain, as both groups of an unpinned domain do. It is arranged while QEMU is paused (-S), before cont: each group gets its CPUs before a thread joins it, and the domain's group, which starts on the non-vCPU CPUs (StartCPUs, also in the containerd OCI spec) so that nothing QEMU sets up while starting names a vCPU's core, gets all its CPUs last. A vCPU count mismatch or any failure fails the start: the guest is never released with a placement it does not have. The guest-vCPU-to-thread mapping comes from QMP query-cpus-fast, the only place it exists: QEMU does not name its vCPU threads unless started with debug-threads=on, and vhost_task helpers are user threads of the same process, indistinguishable from vCPU threads by name or flags. Under io_placement=housekeeping the non-vCPU threads run off the hot cores, so device emulation cannot steal cycles from a busy vCPU, and a virtio-blk iothread keeps disk IO off the main loop. Their CPUs follow the housekeeping set while the domain runs: SetIOCPUs writes the domain's group, then its emulator group, so a shrink reaches every thread at once. One thread appears only once the guest runs: on x86 the first vCPU to enter KVM_RUN creates KVM's NX huge-page recovery thread in its own group, so on its CPU, which for an isolated workload is a kernel-isolated one. After cont it is moved to the emulator group. The -object thread-context argument passed for a pinned VM is dropped. QEMU applies a thread context's cpu-affinity only to threads created in that context, and only a memory backend given prealloc-context= creates any; nothing references it, so it pinned nothing and only added an idle thread. Kubevirt reports that it cannot bind individual vCPUs, and gives KubeVirt's dedicated CPU placement to a workload with CPUs of its own (HasOwnCPUs), what pin_cpu asked for there. The capability is separate from plain cpuset confinement, because a hypervisor that can confine a domain to a set of CPUs may still be unable to bind one vCPU to one CPU or to advertise the resulting topology -- and accepting a whole-core request it cannot apply would report the workload as optimally placed while nothing was pinned. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
zedagent marked its AppInstanceConfig publication restarted at startup, before it had parsed any configuration, as its own comment said: "XXX defer this until we have some config from cloud or saved copy". Restarted tells a subscriber it has every item, so a subscriber acting on it would act before any app config existed. Signal it once, after the first configuration -- from the controller or saved -- has been parsed and its app configs published. A subscriber gets the signal after every item published before it, so restarted again means "you have them all". A failed signal is retried with the next configuration. The subscribers down the chain (zedmanager, zedrouter, zedmanager again, domainmgr) only pass the signal on, so the later signal changes nothing else, on any hypervisor. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Maps the VmConfig CPU placement fields onto the device-internal intent. The pool (cpu_policy) and the pinning choice (pin_cpu) are kept as sent, each with its own meaning: a combination this EVE does not serve is left to domainmgr to refuse, never reinterpreted here. The one exception is a controller that does not send cpu_policy: pin_cpu=true alone then means a dedicated workload, pinned on a VM and not on a container, which has no vCPUs. An enum value this EVE does not know, from a newer controller, is kept as Unknown, and domainmgr refuses the workload naming the field rather than place it on a guess; a controller is expected to gate on the capability below in any case. Sets OptionalCapabilities.cpu_placement when domainmgr's capabilities say this node places CPUs (CPUTopologyPinning: KVM, on any architecture, on a CPU topology EVE could read), and re-sends device info when they arrive; any other node, EVE-k included, leaves it unset. Until the node says so a controller has no way to know it applies the placement fields at all. Reports the node's CPU pool utilization on device info, per pool, with both the CPU sets and the whole-core counts, so a controller can answer "will this fit?" before a deploy and explain a shortage after one, and with the warnings about how the node defines a pool, a mistaken isolcpus. This is dynamic state, so it rides the change-driven message rather than the cached hardware inventory, and a pool change triggers that message when it happens, as a change to the assignable adapters does: the controller decides what still fits from exactly this report, so it cannot wait for the next periodic one. Reports where each application stands with its CPU request (ZInfoApp.cpu_placement_status, and for a refusal cpu_placement_error): domainmgr's verdict -- placed, placed worse than planned, or FAILED and why it was refused -- unless it waits for its planned CPUs (WAITING) or is placed well but holds CPUs the plan gives to another (RepackFor), which makes it part of that repack (REPACK_ADVISED) so the controller includes it in the set it restarts. A running workload in a repack and a waiting one also get a WARNING advisory in appErr with their own wording; it is built only at the wire, and never placed in the status error fields, because those are read as fatal in several places and a workload whose placement is merely improvable must not be torn down for it. A wait is timestamped when it began: it lasts until its holders leave, so the controller has to be able to tell it from a stuck workload. Reports, per application, the CPUs it holds (ZInfoApp.assigned_cpus), as the adapters assigned to it are: its CPUs (in vCPU order when pinned), the siblings it parks and the shared CPUs its IO threads run on, which follow the housekeeping set. The node's pool report says how much is held; this says by whom and where, which a controller cannot work out from the pools: a core held whole looks the same whether both of its threads run vCPUs or one is parked. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
The start moment of a delayed workload was computed from a base time set only when zedmanager processed a controller-status message, and the app config regularly won that race -- leaving a start moment derived from the zero time, which is always in the past, so the delay was silently dropped and never recomputed. The base time is now established on first use, so a workload created before that message arrives gets the same start moment as one created after, and a later message does not move it. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
CPU placement has to be a function of the configured set of pinned workloads, but domainmgr only ever sees a DomainConfig, and a DomainConfig cannot exist before a workload's volumes are resolved -- it carries the disk list. So during boot, or while images download at different rates, whichever workload was ready first was placed as though it were alone and took cores the full plan would have assigned elsewhere. The same set of workloads landed differently on each boot. zedmanager knows the whole picture much earlier: it holds every AppInstanceConfig, it owns the profile resolution that decides what is meant to run, and it is the component that withholds the DomainConfig in the first place. It now publishes that demand set -- one aggregate object naming every workload intended to run with its CPU intent, and whether it runs as a container, which has no vCPUs to pin -- as soon as the config is resolved, with no dependence on volumes. The set is published as a single object rather than one item per workload on purpose. Per-workload items would leave the consumer planning over whatever had arrived so far, which is the same ordering bug on a faster topic. The set says whether it is complete: zedmanager marks it so once its app config subscription has restarted, i.e. once it has been given the whole configuration, even when that is empty. Before that it names the apps processed so far. A workload that is configured but not activated is left out: its cores belong to the workloads that do run, exactly as an assigned PCI device returns to the pool when its workload stops. domainmgr's placement state for the domain -- its verdict, the CPUs it holds, its RepackFor and a wait for its planned CPUs -- is mirrored onto AppInstanceStatus as one value for zedagent to report, before zedmanager waits for the domain to boot -- a refused domain never boots -- and cleared when the workload is deactivated. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Placement is planned over every workload the controller intends to run -- those zedmanager's demand set names and those whose DomainConfig asks for activation, the DomainConfig's intent winning, since it is what the workload is created from -- rather than over whichever DomainConfigs have arrived. The result for a given set of workloads is therefore the same regardless of the order they were configured, started or delayed in, and the same across a reboot -- properties an operator depends on when a workload's performance was validated against a specific placement. Nothing depends on the order events arrive in. One component makes every placement decision under one lock and claims the planned CPUs atomically. A change to its inputs -- the demand set, a DomainConfig, a placement or a release -- republishes the pool report and wakes every workload's handler, which brings its workload in line from the state as it stands: a shared workload's cpuset, the IO threads of an io_placement=housekeeping one, a wait for planned CPUs, its part in a repack. It does the same on every tick, so a missed or overtaken wakeup costs a tick, never a wrong outcome. After a domainmgr restart the placer is reseeded from the running workloads' DomainStatus, through the same Claim, before any input is handled. A workload with CPUs of its own is placed only once the demand set is complete. Right after a boot, one app's DomainConfig can be handled before the demand-set update that names another, and a placement against the partial set takes the other's planned CPUs. Until the set is complete the workload waits (CPUWait.AppSet), retried as the set changes; the controller is not told of that wait. The plan is derived, never stored. Persisting an assignment would create a second source of truth that can disagree with the hardware after a CPU is offlined, a NUMA node changes or the config changes, and the failure mode of stale placement data is silent and hard to diagnose. Determinism comes from ordering the batch by how constrained each workload is and breaking ties on the workload's identity, so recomputation reproduces the same answer. A whole-core request consumes every thread of its cores. When only one thread per core is wanted, the sibling is parked -- held by that workload and offered to nobody. This is the point of asking for a whole core: a best-effort workload running on the parked sibling would evict the cache lines and contend for the execution units the request exists to protect. It stays idle: it is kept out of the workload's own cpuset as well, so the workload's emulator and IO threads do not run there either. Parked threads are reported as consumed in the pool utilization rather than as spare capacity, so a controller sees the true remaining headroom. A pinned VM has its vCPUs pinned one to one. Without whole cores -- a dedicated request without full_pcpus_only, or the legacy pin_cpu alone -- it gets one host CPU per vCPU from its own exclusive set, in vCPU order, so the hypervisor pins each vCPU before the guest runs: an exclusive set with the vCPUs floating inside it is not what anyone asking for pinning expects. Its guest topology stays flat, since CPUs that need not form whole cores cannot describe an SMT structure. A container is never pinned: it has no vCPUs, and its processes run anywhere on its own CPUs. Thread-granular workloads are planned and claim their planned CPUs like whole-core ones. A workload placed off its plan, because a running workload holds its planned CPUs, avoids the CPUs the plan sets aside for workloads not started yet. The old allocator (CPUAllocator) and VmConfig.CPUsPinned, which only it read, go: everything that needs to know asks the policy (HasOwnCPUs). Kernel CPU isolation is a resource. domainmgr reads the isolated set from /sys/devices/system/cpu/isolated at startup and hands it to the placer, so cpu_policy=isolated is served from those cores and everything else is kept off them. The housekeeping set -- the cpuset of the non-pinned workloads -- excludes the isolated CPUs too, because isolcpus keeps the load balancer off a core but honours an explicit affinity. An isolcpus that swallows every free CPU makes it fall back to the unfiltered set, loudly: that is a misconfiguration, and an empty cpuset would take down every non-pinned workload on the node. An isolcpus that covers the CPUs reserved for EVE or isolates only part of a core is, for the same reason, not refused: each mistake is a warning on the isolated pool of the pool report (CPUPoolIssueReservedCPUs, CPUPoolIssuePartialCore), so the operator who made it is told where they look at the node, and it is logged at startup. Placement failures are terminal. A workload that cannot be placed stops, its verdict (DomainStatus.Placement.Verdict) naming the cause -- a shortage, a shortage a repack would fix, or a request nothing could satisfy -- with an error saying what has to change, and stays stopped until an explicit action, a restart or a deactivate and activate, which is how EVE already treats an unavailable PCI device. Retrying would silently place the workload the moment some unrelated workload happened to release cores, at an arbitrary time, with no operator awareness that its performance envelope had changed. Refusals are explicit wherever a request would otherwise be quietly downgraded or reinterpreted. The pool and the pinning choice are served as sent or refused as an invalid policy: pinning on the housekeeping pool, an unpinned VM on CPUs of its own, a pinned container and the whole-core refinements without full_pcpus_only are not implemented, and an isolated request without full_pcpus_only cannot be, since isolated CPUs are given out as whole cores -- on a shared core the kernel still schedules the sibling. So is a field with a value this EVE does not know, named in the refusal. An isolated request on a node whose kernel isolates nothing is refused as isolation unavailable, naming isolcpus, since the node cannot acquire an isolated set while running. A node whose CPU topology could not be read gives no workload CPUs of its own, thread-granular and legacy pin_cpu included: that is an unsupported topology, as the inventory reports the topology unknown, and the node's capabilities then say it cannot place CPUs, so the controller is told not to ask. So is a whole-core-SMT request on a node with no two-thread core, a topology limit rather than a bad policy, whose retry condition names enabling SMT in firmware or threads_per_core=1. A fault in the placer itself is reported as an invalid policy too, the only code the API leaves for it, with a description and retry condition that say it is EVE's. A shortage is called a repack only when the plan shows one would work. The plan is computed from an empty slate over every configured workload; a workload the plan finds no CPUs for, NUMA-local or not, is short of CPUs, and so is one the plan fits only by leaving out a workload that is running -- that is an eviction, not a repack, and the refusal names the workload it would take the CPUs from. The device owns the plan, so the device says who moves. Besides the refused workload, every running workload standing on CPUs the plan gives to a member of the set is flagged as part of the repack, as a WARNING advisory naming the workload it makes room for (RepackFor), following chains: a member's planned slot may be held by a third workload, which then joins. Membership is re-derived in each workload's handler, since it changes when other workloads are refused, removed or repacked; the flags that drive it are kept by placement, from a refusal or a worse placement until the workload is placed as planned, stopped or deleted, and survive the workload's own restart. A controller's whole contract is then to restart every flagged workload at once, and the restarts may reach the device in any order: a workload whose planned CPUs are held only by workloads stopping or flagged for the same repack waits for them (CPUWait) rather than taking whatever is free, and takes them on release. A holder staying where it is means nothing will free them, so the workload is placed or refused at once, and a refused workload is never started later on its own. Under io_placement=housekeeping a workload's emulator and IO threads run on the housekeeping set as it stands once the workload holds its own CPUs: the CPUs reserved for EVE and every other CPU no workload holds, never a kernel-isolated one, so they never land on cores another workload holds. The set changes as other workloads take and release CPUs, and the threads follow it, as shared workloads do; it always holds the CPUs reserved for EVE. What can fail and be retried -- an adapter not free, a disk whose format is not yet the configured one -- is checked before the CPUs are placed, so a workload retried for it does not take CPUs and give them back on every retry. A workload that stops gives its CPUs back, and a refused one its refusal with the error. A workload whose domain fails to boot keeps its CPUs, as it keeps its adapters, so the boot retry is pinned like the first attempt. On KVM a restart that keeps the domain's name records the restart and purge counters it came back with, so a later stop is not mistaken for another restart. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
The workloads and EVE's services each have a cpuset: the services and containerd with its shims in /eve, on EVE's own CPUs, and each workload in its own group under /eve-user-apps. The host processes left in the root cgroup -- pid 1, udevd, the getty -- do not, and the root spans every CPU. Left there, they run on a dedicated workload's cores whenever the scheduler puts them there, so those cores would be exclusive among workloads but not against the host. The root's cpuset cannot be narrowed. So domainmgr keeps a top-level "housekeeping" group, named after the pool it holds, beside /eve, /eve-user-apps and /eve-memory-monitor. On every allocator change it sets the group's CPUs to the housekeeping CPUs -- before moving anything, so no process runs on a CPU that has just been dedicated -- and moves into it every user-space process still in the root. Processes they fork inherit it. Kernel threads are left where they are. The one way a process is started in the root later, eve exec, puts it into the group itself, so nothing has to sweep the root again. Only where EVE pins workloads (Capabilities.CPUTopologyPinning), since under EVE-k the kubelet owns the cgroups, and only when the root hands the cpuset controller to its children: otherwise a group of its own would not confine anything, and domainmgr says so instead of pretending. Verified on a 32-CPU QEMU node (cgroup v2) where seven of its eight workloads held 30 of its 32 CPUs: no user-space process is left in the root cgroup, none outside the workloads is allowed on a dedicated or isolated CPU, and every host thread that ran in a 10 s window ran on the housekeeping CPUs. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
A test cannot assert anything about CPU placement on a node whose CPU topology it does not control: with a flat single-thread-per-core VM every placement policy looks alike, and whole-core, one-per-core and parked-sibling behaviour are indistinguishable. CPU placement tests therefore ask for SMT siblings (ThreadsPerCore in the device requirements) and must state what they expect in terms of the node they got. Tests get the node facts CPU placement work needs: the host's socket/core/sibling topology, which CPUs are online, which the kernel isolated, and the kernel command line, so an expectation can be stated in terms of what the node actually is rather than hard-coded numbers. Per workload they get its cpuset and every QEMU thread with its thread group and the CPUs it may run on. Per-workload facts come from QMP over the existing SSH transport. The guest-vCPU-to-host-thread mapping is only available there -- QEMU does not name its vCPU threads, and a domain's thread group contains helper threads that cannot be told apart from vCPU threads by name. A QMP call is also a point-in-time question with a definite answer, which is what an assertion wants, where waiting for a log line is a race dressed up as a check. The call is bounded by closing the connection from a timer, since deadlines are not supported on SSH channels. The application config gains the CPU placement policy fields and a start delay. The delay exists to test the property that matters most here: that placement does not depend on the order workloads happen to start in. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Every test asserts against the host's real topology and the kernel's view of the workload, not against expected CPU numbers. - cpuplacement: the three placement shapes a controller can ask for. One per core: as many distinct physical cores as vCPUs, each vCPU pinned to one host CPU, no two sharing a core. Whole-core-SMT: both siblings of each core become vCPUs, and the guest's view of its topology matches how it was pinned -- a guest told it has siblings that are not siblings co-schedules work that then contends. Each vCPU thread is in a thread group of its own on its CPU and every other QEMU thread in the emulator group, which under io_placement=housekeeping keeps them off the vCPUs' cores, on the node's housekeeping pool; when a later app takes one of those CPUs they leave it, and come back once it is gone. Deployed together, a whole-core-SMT app, a one-per-core app and a housekeeping app land on disjoint CPUs and cores, with housekeeping CPUs left to the system: a test of one app alone would pass while the allocator handed the same core to two workloads. The node reports both capabilities, and each pinned app is reported to the controller as placed. - cpuplacementstability: a validated placement stays put across a reboot, a staggered start with one app delayed, and a restart in the reverse order; every vCPU lands on the same host CPU each time. Boot order depends on downloads, network readiness and start delays, so a placement derived from arrival order would be reproducible only by luck. - cpuparkedsiblings: the idle sibling of a one-thread-per-core workload's core is consumed, not spare. No workload has it in its cpuset, the owner's own threads included; the node does not advertise it as free; and nothing is seen running on it. - cpuneedsrepack: a node fragmented by thread-granular workloads has free threads but no free whole core. The refusal is reported as NEEDS_REPACK, not a plain shortage (INSUFFICIENT), and after a repack the workload really runs, so the advice is shown to be true. Along the way the thread-granular workloads' vCPUs are checked to be pinned one to one onto their own CPUs. - cpufailsclosed: each class of unsatisfiable request is refused with its own status, never approximated by a weaker placement. The workload never boots, the dedicated pool is unchanged afterwards, and the refusal persists until the config changes, as for an app whose PCI device is unavailable. - cpuisolatedpool: a device booted with isolcpus and nohz_full through /config/grub.cfg runs an isolated, a dedicated whole-core and a housekeeping workload. The isolated app's CPUs lie wholly in the kernel's isolated set and the others touch none of it, in what they hold or may run on; the node reports those CPUs in the isolated pool only. The isolated set is read from sysfs, so an isolcpus that isolates nothing fails the test instead of passing it vacuously, and its CPU list is checked to cover whole cores. Each reachable app needs its own forwarded edge-node port, since the port belongs to the node. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Adds docs/CPU-PLACEMENT.md: what a workload can ask for, how domainmgr chooses its CPUs and how the KVM backend arranges a domain's threads in a cgroup tree, where IO threads run, kernel CPU isolation with isolcpus, the failure codes and the repack, what the device reports to the controller, and where to look on a running device. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
Update all package hashes after a change to a package that others are built from. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
29ded71 to
47d5349
Compare
|
Evetest: Claude chose to skip 23 test/suite name(s) as unrelated to or redundant for this PR. Which ones and why: run summary. Comment |
| @@ -0,0 +1,396 @@ | |||
| # CPU Placement and Isolation | |||
There was a problem hiding this comment.
Would it be worth adding a short "Recommendations" section with a few example configurations mapped to workload types? For example: real-time or latency-sensitive (ISOLATED, full_pcpus_only, threads_per_core=1), throughput-oriented VMs (DEDICATED, full_pcpus_only, threads_per_core=2), and general-purpose apps (HOUSEKEEPING). The number of knobs and how they interact might make it hard for (non-advanced) users to work out the right combination from the reference tables alone.
Or is our help center a better place for this, with this doc staying focused on semantics and implementation?
Places the vCPUs of applications that ask for CPUs of their own on whole physical cores, deterministically, keeps EVE's own processes off those CPUs, and tells the controller what the node has, what it can do and what each workload got.
docs/CPU-PLACEMENT.mddescribes what eve-kvm does.The API is lf-edge/eve-api#155, merged, and EVE is already on it (#6925); the CPU topology this builds on is #6946, merged. evetest already runs Adam 0.0.85 (lf-edge/adam#163, #6798), which knows the new fields.
Upstream depenedencies
What a workload asks for, and gets
cpu_policypicks the pool its CPUs come from:HOUSEKEEPING(shared with EVE and every other workload there),DEDICATED(CPUs of its own) orISOLATED(CPUs of its own that the kernel isolates withisolcpus).pin_cpusays whether each vCPU gets a host CPU of its own.full_pcpus_only,threads_per_core,numa_policyandio_placementrefine CPUs of its own. A controller that sends onlypin_cpu=truegets a dedicated, pinned workload, as before.threads_per_core=2both siblings become vCPUs and the guest is told truthfully which vCPUs are siblings; with1the sibling is parked, held for the workload and idle, so nothing evicts its cache or contends for its execution units. This is an emulation of "Disable Hyperthreading" in BIOS but per-workload ratheer than globally.query-cpus-fast, the only place it exists./housekeepingcgroup on the housekeeping CPUs and moves into it the user-space processes left in the root cgroup (pid 1, udevd, the getty), whose cpuset cannot be narrowed.isolcpusdefines the isolated pool. A mistake in it (covering EVE's own CPUs, isolating part of a core) is reported as a warning on that pool, not refused: refusing would take down the workloads that do not use it.IO threads
A VM's non-vCPU threads (QEMU's main loop, IO threads, vhost and IO workers) run in an
emulatorgroup of the VM's cgroup and never on another workload's CPUs. Underio_placement=DEDICATED, the default, a VM with CPUs of its own keeps them on its own CPUs. Underio_placement=HOUSEKEEPINGthey run on the housekeeping pool, which always holds the CPUs reserved for EVE, and follow it as other workloads take and release CPUs; itsvirtio-blkdisks get an iothread there. The domain starts on those CPUs, so whatever QEMU sets up while starting gets them too.The disks use
aio=io_uringfor every VM, as before.Known issue, kernel fix pending: io_uring's kernel workers (io-wq) do not follow a cpuset change. A worker that exists is moved with its cgroup, but every worker started after the change runs on the old CPUs, which another workload may hold by then. io-wq starts workers on demand for the whole life of the VM (every flush needs one) and retires idle ones after 5 seconds. So in a VM whose CPUs move, a housekeeping-pool VM or one with
io_placement=HOUSEKEEPING, new disk workers can run on CPUs since given to aDEDICATEDorISOLATEDworkload. The fix is sent upstream (io_uring/io-wq: apply the current cpuset to new workers) and to the EVE kernel (lf-edge/eve-kernel#336); EVE's kernel pin follows once it is merged.Placement as a set
domainmgr plans over every workload the controller intends to run, which zedmanager publishes as soon as config is resolved, not over those that happened to activate first. The set says when it is complete; right after a boot a workload with CPUs of its own waits for the complete set, so one app's domain config handled before the update naming another cannot take the other's planned CPUs. The plan is derived, never stored: the same configured set lands on the same CPUs across a reboot, a staggered start and any restart order. A workload that is configured but not activated releases its cores, as an assigned PCI device returns to the pool.
Failing closed, and repack
A request the device cannot serve as sent is refused, never approximated, and the refusal stays until an explicit action (a restart, or a deactivate and activate), as for an unavailable PCI device. A shortage a repack would fix (
NEEDS_REPACK) is told apart from one nothing would fix (INSUFFICIENT). The device names the set to restart: the refused workload and every running workload in its way (REPACK_ADVISED). The restarts may arrive in any order; a workload whose planned CPUs are still held by members of the repack waits for them (WAITING).Reporting: two pieces, and state
KernelCPUIsolation(#6946)OptionalCapabilitiescpu_placement: KVM on any architecture, with a CPU topology EVE could readThe controller gates the placement fields on
cpu_placement; an EVE that predates them does not set it, so the advertisedAPICapabilityis unchanged.Beside them, state: what is free (
ZInfoDevice.cpu_pools, with the pool warnings) and what each workload got (ZInfoApp.cpu_placement_statusand, for a refusal,cpu_placement_error, both typed enums, andassigned_cpus). A refusal's text and what would change it stay inappErr.PR dependencies
Testing
On this revision (cgroup v2, QEMU 11, master of 2026-11-16):
hypervisorandtypespass in the kvm and k flavors; the other touched packages passed on the previous revision and are unchanged since.isolcpusset) against the controller and UI that implement this API: placement of every shape, repack and the isolated pool (23 scenarios, 210 checks); every pinned vCPU runs only on its planned host CPU, a refusal reaches the controller asneeds_repackorinsufficient; every pool with and without pinning (14 scenarios, 160 checks, every VM's disks onaio=io_uring);/housekeepingholds exactly the housekeeping pool, no user-space process is left in the root cgroup, and none outside the workloads may run on a dedicated or isolated CPU. All pass.tests/apps, KVM with a real SMT topology, stock Adam 0.0.85), the eight CPU placement tests on this revision's live image: placement shapes and disjointness, stability across reboot, staggered start and reverse restart order, parked siblings, repack (NEEDS_REPACK, then a real repack), every class of refusal failing closed, the isolated pool. 8/8 pass locally. CI runs them on the evetest brokers, which give the device the SMT topology the tests ask for (evetest: let a test ask for SMT sibling threads #7130); the SMT tests fail rather than skip on a device without SMT. CI also runsTestContainerdCPUsalongside the host cpuset.Before the rebase (cgroup v1), end to end: a reboot with apps configured (each back on its planned CPUs), a misconfigured
isolcpus, and every pool with and without pinning.Also fixes a pre-existing bug: zedmanager dropped an application's
start_delay_in_secondswhen its config arrived before the controller status that sets the delay's base time.