Files
Reims-Go/internal/app/affinity_flow.go
T
Nwflower 59aaf36eb5 refactor(app): key VM runtime state by VMRef
Qualify transitions, pending changes, reboot watchers, disk/process caches, typing queues, thumbnail locks, and VM log sources so same-named VMs do not share mutable state.

Read driver-qualified transition keys in the frontend and add --start-driver to CLI/window handoff. Legacy window start rejects other drivers explicitly until lifecycle routing lands.
2026-11-30 07:51:10 -07:00

161 lines
4.9 KiB
Go

package app
import (
"context"
"fmt"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/macos/reims"
"github.com/Nwflower/reims-go/internal/vmstart"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// 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 {
for _, vm := range vms {
if vm.Name == name && vm.State == "running" && vm.PID != nil && *vm.PID > 0 {
return *vm.PID
}
}
return 0
}
}
a.mu.Lock()
defer a.mu.Unlock()
for _, vm := range a.lastVMs {
if vm.Name == name && vm.State == "running" && vm.PID != nil && *vm.PID > 0 {
return *vm.PID
}
}
return 0
}
func buildVMAffinityStatus(detail reims.VMDetail, 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(name string) (VMAffinityStatus, error) {
detail, err := a.GetVMDetail(name)
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" {
pid = a.findRunningVMPID(a.baseCtx(), name)
}
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(name string, req VMAffinityUpdate) (VMAffinityStatus, error) {
detail, err := a.GetVMDetail(name)
if err != nil {
return VMAffinityStatus{}, err
}
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" {
pid = a.findRunningVMPID(a.baseCtx(), name)
}
if pid > 0 {
line, applyErr := vmstart.ApplyVMAffinity(loc, rep, detail.Directory, globalAutoPin, pid, detail.CPUs, true)
if line != "" {
a.emitVMLog(name, line)
}
if applyErr != nil {
return buildVMAffinityStatus(detail, rep, globalAutoPin, pid), applyErr
}
}
if a.ctx != nil {
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
}
return buildVMAffinityStatus(detail, rep, globalAutoPin, pid), nil
}