hcsoci,hcs,shim: honor CPU affinity for Argon containers#2764
Open
zylxjtu wants to merge 1 commit into
Open
Conversation
2aa0cf6 to
7f9757c
Compare
Process-isolated (Argon) WCOW containers run inside a server silo, which is a job object owned by HCS. HCS does not expose a CPU-affinity field on the container Processor schema (Count/Maximum/Weight), so the OCI spec.Windows.Resources.CPU.Affinity field was silently ignored for Argon containers at both create and update time. Honor it the same way HostProcess containers do: open the silo's job object (by its well-known \Container_<id> name, the same handle queryInProc already opens) and apply the affinity with SetInformationJobObject(JobObjectGroupInformationEx) via the existing SetCPUGroupAffinities helper. Create path: - Add (*hcs.System).SetSiloCPUGroupAffinities, which opens the silo job and sets the group affinities on it. - After CreateComputeSystem but before the container is started, apply affinity for Argon (HostingSystem == nil) via applyArgonCPUAffinity. This is race-free: the kernel records the property on the job, then applies it to the init process (and every descendant that later joins the silo) at the moment HCS calls AssignProcessToJobObject during Start. No container instruction ever runs on a forbidden processor, and the property lives on the kernel object so no per-PID walking or watchdog is needed. Update path (runtime update / UpdateContainerResources): - Loosen isValidWindowsCPUResources to accept an affinity-only update (no Count/Shares/Maximum), so affinity can be changed after the container starts. - updateWCOWContainerCPU now only sends the HCS Processor modify request when a rate control is set (avoiding an empty no-op request) and additionally applies affinity out of band when present. - New updateWCOWContainerCPUAffinity re-pins the silo for Argon via the same SetSiloCPUGroupAffinities mechanism (the kernel re-applies the mask to current and future silo members). Hypervisor-isolated (Xenon) containers return ErrNotImplemented rather than silently dropping the request, since they require a UVM-level CPU-group swap. Shared helpers: - Collect the container-kind-agnostic CPU affinity validators (ValidateCPUAffinity / ValidateCPUAffinityEntries), their sentinel errors, and the OCI -> jobobject.GroupAffinity converter (ToJobObjectAffinities) into a single kind-neutral cpuaffinity.go, shared by the HostProcess (internal/jobcontainers) and Argon paths. jobcontainers now reuses ToJobObjectAffinities instead of a duplicated conversion loop. Xenon (UVM-backed) containers are out of scope here and are skipped; they require UVM-level CPU groups and are handled separately. Tests: unit coverage for the converter, the affinity validators, the loosened isValidWindowsCPUResources (including affinity-only), and the affinity-update dispatch (no-op and Xenon not-implemented branches). Plus a functional create-path test (test/functional/container_affinity_test.go) that pins an Argon container at create and asserts, in-process via internal/jobobject, that the affinity landed on the silo job object (the real regression gate, both pre- and post-start) and that the init process inherited it. It needs no external tooling since the functional suite runs as SYSTEM and can open the silo job directly. Runtime-update (post-start) read-back over cri-containerd is deferred: it relies on jobobject-util, whose get-path is still single-group only. Signed-off-by: zylxjtu <zhang.yuanliang@hotmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Process-isolated (Argon) WCOW containers run inside a server silo, which is a job object owned by HCS. HCS does not expose a CPU-affinity field on the container Processor schema (Count/Maximum/Weight), so the OCI spec.Windows.Resources.CPU.Affinity field was silently ignored for Argon containers at both create and update time.
Honor it the same way HostProcess containers do: open the silo's job object (by its well-known \Container_ name, the same handle queryInProc already opens) and apply the affinity with SetInformationJobObject(JobObjectGroupInformationEx) via the existing SetCPUGroupAffinities helper.
Create path:
Update path (runtime update / UpdateContainerResources):
Shared helpers:
Xenon (UVM-backed) containers are out of scope here and are skipped; they require UVM-level CPU groups and are handled separately.
Tests: unit coverage for the converter, the affinity validators, the loosened isValidWindowsCPUResources (including affinity-only), and the affinity-update dispatch (no-op and Xenon not-implemented branches). End-to-end silo pinning is now covered by a Windows functional test (Test_Container_CPUAffinity_Argon in test/functional/container_affinity_test.go): it creates a process-isolated container with spec.Windows.Resources.CPU.Affinity and asserts the affinity is written to the silo's job object in the create→start window (and again once a member is running), plus a kernel-level check that the init process was constrained to the pinned group — skipped if the affinity can't be read, but a genuine mismatch fails the test.. It covers the single-group case on any host plus a multi-group case gated on a Server 2022+ host with more than one processor group.
Signed-off-by: zylxjtu (zhang.yuanliang@hotmail.com)