Files
Reims-Go/internal/app/affinity_flow.go
T
NwflowerandClaude Opus 5.5 165f26351d refactor(app): split the VM poll loop into steps
The poll loop was one 290-line function that did everything that follows the
VM list. Its steps are now separate and recording a listing has a test. The
install guide part moved next to the rest of the guide code. Running process
IDs and capabilities are read through one method each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 21:26:54 -07:00

167 lines
5.1 KiB
Go

package app
import (
"context"
"fmt"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/vmstart"
)
// VMAffinityUpdate is the payload passed from the UI to save and live-apply a
// VM's CPU affinity mapping and EcoQoS power-throttling policy.
type VMAffinityUpdate struct {
Mode string `json:"mode"`
CPUs []int `json:"cpus,omitempty"`
DisableEcoQoS bool `json:"disable_eco_qos"`
}
// VMAffinityStatus describes both the saved per-VM affinity policy and the live
// QEMU process affinity state (when the VM is running).
type VMAffinityStatus struct {
Mode string `json:"mode"`
CPUs []int `json:"cpus"`
DisableEcoQoS bool `json:"disable_eco_qos"`
EffectiveMode string `json:"effective_mode"`
EffectiveCPUs []int `json:"effective_cpus"`
EffectiveMaskHex string `json:"effective_mask_hex"`
VCPUs int `json:"vcpus"`
Oversubscribed bool `json:"oversubscribed"`
Running bool `json:"running"`
PID int `json:"pid,omitempty"`
LiveCPUs []int `json:"live_cpus,omitempty"`
LiveMaskHex string `json:"live_mask_hex,omitempty"`
LiveReadError string `json:"live_read_error,omitempty"`
}
func (a *App) findRunningVMPID(ctx context.Context, name string) int {
if c, err := a.client(); err == nil {
if vms, lerr := c.List(ctx); lerr == nil {
return runningPIDByName(vms, name)
}
}
return a.cachedVMPID(name)
}
func buildVMAffinityStatus(detail driver.VMInfo, rep host.Report, globalAutoPin bool, pid int) VMAffinityStatus {
cfg := vmstart.ReadVMAffinity(detail.Directory)
res := vmstart.ResolveVMAffinity(cfg, globalAutoPin, rep, detail.CPUs)
savedCPUs := cfg.CPUs
if savedCPUs == nil {
savedCPUs = []int{}
}
effCPUs := res.CPUs
if effCPUs == nil {
effCPUs = []int{}
}
st := VMAffinityStatus{
Mode: string(res.ConfigMode),
CPUs: savedCPUs,
DisableEcoQoS: res.DisableEcoQoS,
EffectiveMode: string(res.EffectiveMode),
EffectiveCPUs: effCPUs,
EffectiveMaskHex: res.MaskHex,
VCPUs: detail.CPUs,
Oversubscribed: detail.CPUs > 0 && len(effCPUs) > 0 && len(effCPUs) < detail.CPUs,
Running: detail.State == "running" || pid > 0,
PID: pid,
}
if pid > 0 {
if procMask, _, err := host.GetProcessAffinityMask(uint32(pid)); err == nil {
limit := rep.LogicalCPUs
if limit <= 0 || limit > 64 {
limit = 64
}
st.LiveCPUs = host.MaskToCPUs(procMask, limit)
st.LiveMaskHex = fmt.Sprintf("0x%X", procMask)
} else {
st.LiveReadError = err.Error()
}
}
return st
}
// GetVMAffinity returns the per-VM processor affinity configuration, resolved
// effective CPU set, and live QEMU process affinity mask when running.
func (a *App) GetVMAffinity(driverID, name string) (VMAffinityStatus, error) {
vm, err := a.resolveVMInfo(driverID, name)
if err != nil {
return VMAffinityStatus{}, err
}
detail := vm
if err != nil {
return VMAffinityStatus{}, err
}
a.mu.Lock()
root := a.cfg.DataRoot
globalAutoPin := a.cfg.AutoPinPCores
a.mu.Unlock()
rep := host.Inspect(root)
pid := 0
if detail.State == "running" {
if listed, ok := a.polledVMForDriver(vm.DriverID, name); ok && listed.PID != nil {
pid = *listed.PID
}
if vm.PID != nil {
pid = *vm.PID
}
}
return buildVMAffinityStatus(detail, rep, globalAutoPin, pid), nil
}
// SetVMAffinity saves a VM's processor affinity mapping and immediately applies
// it (plus EcoQoS power-throttling control) to the running QEMU process if active.
func (a *App) SetVMAffinity(driverID, name string, req VMAffinityUpdate) (VMAffinityStatus, error) {
vm, err := a.resolveVMInfo(driverID, name)
if err != nil {
return VMAffinityStatus{}, err
}
detail := vm
if err != nil {
return VMAffinityStatus{}, err
}
if driver.IsHosted(vm.Capabilities) || !driver.SupportsCapability(vm.Capabilities, driver.CapAffinity) {
return VMAffinityStatus{}, fmt.Errorf("CPU affinity is not available for %s", name)
}
if detail.Directory == "" {
return VMAffinityStatus{}, fmt.Errorf("VM %s has no directory", name)
}
a.mu.Lock()
root := a.cfg.DataRoot
globalAutoPin := a.cfg.AutoPinPCores
loc := a.cfg.Locale
a.mu.Unlock()
rep := host.Inspect(root)
eco := req.DisableEcoQoS
if _, err := vmstart.SaveVMAffinity(detail.Directory, vmstart.VMAffinityConfig{
Mode: vmstart.AffinityMode(req.Mode),
CPUs: req.CPUs,
DisableEcoQoS: &eco,
}, rep.LogicalCPUs); err != nil {
return VMAffinityStatus{}, err
}
pid := 0
if detail.State == "running" {
if listed, ok := a.polledVMForDriver(vm.DriverID, name); ok && listed.PID != nil {
pid = *listed.PID
}
if vm.PID != nil {
pid = *vm.PID
}
}
if pid > 0 || a.brokerRunsVM(vm.DriverID, name) {
line, applyErr := a.applyVMAffinity(loc, rep, vm.Ref(), detail.Directory, globalAutoPin, pid, detail.CPUs, true)
if line != "" {
a.emitVMLogForDriver(driverID, name, line)
}
if applyErr != nil {
return buildVMAffinityStatus(detail, rep, globalAutoPin, pid), applyErr
}
}
a.emitStateChanged()
return buildVMAffinityStatus(detail, rep, globalAutoPin, pid), nil
}