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" 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 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(name string) (VMAffinityStatus, error) { return a.GetVMAffinityWithDriver("", name) } func (a *App) GetVMAffinityWithDriver(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(name string, req VMAffinityUpdate) (VMAffinityStatus, error) { return a.SetVMAffinityWithDriver("", name, req) } func (a *App) SetVMAffinityWithDriver(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 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 { line, applyErr := vmstart.ApplyVMAffinity(loc, rep, detail.Directory, globalAutoPin, pid, detail.CPUs, true) if line != "" { a.emitVMLogForDriver(driverID, 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 }