Files
Reims-Go/internal/app/affinity_flow.go
T
NwflowerandClaude Opus 5.5 c0f08be124 feat: per-VM CPU affinity, disk snapshots, SSH helpers and VM window actions
Backend
- Per-VM CPU affinity (vmstart/affinity.go, affinity_flow.go): modes auto /
  all / P-cores / non-LP-E / custom, saved in the VM folder, applied to the
  QEMU process on start (GUI and CLI) and live via SetVMAffinity; copied on
  clone. Start summary logs the resolved mapping. Host report gains CPU
  topology helpers (mask <-> CPU list).
- Offline qcow2 restore points via qemu-img (driver/reims/snapshot.go,
  snapshot_flow.go): list / create / restore / delete.
- SSH helpers (ssh_flow.go): remembered user, open a terminal, write an
  ~/.ssh/config entry, open VS Code Remote-SSH.
- FocusVMWindow brings the QEMU window forward (restoring it if minimized);
  CopyVMScreenshot copies a full-resolution frame to the clipboard.
- DeliverPathsToGuest copies dropped host files into the shared folder;
  main.go enables Wails file drop on elements marked --wails-drop-target.
- VM note (.reims-go-note) and a storage breakdown (extras_flow.go).

Frontend
- VM page: running state's "Show Window" and the screen preview use
  FocusVMWindow; camera button and More-menu item copy a screenshot;
  editable note under the name; SSH row shows the saved user with
  Terminal / config / VS Code actions; restore-point list; install guide
  steps can be ticked off; drop files onto the page to send them.
- Sidebar right-click menu per VM (start, show window, screenshot, stop,
  restart, settings, folders, clone, delete); double-click and Ctrl+Enter
  still run the main action.
- AffinityDialog opened from the VM menu and VM settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 04:00:48 -07:00

161 lines
4.9 KiB
Go

package app
import (
"context"
"fmt"
"github.com/Nwflower/reims-go/internal/driver/reims"
"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 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.emitLog(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
}