Files
Reims-Go/internal/host/host.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

195 lines
6.7 KiB
Go

package host
import "strings"
type GPUKind string
const (
GPUDiscrete GPUKind = "discrete"
GPUIntegrated GPUKind = "integrated"
GPUVirtual GPUKind = "virtual"
GPUUnknown GPUKind = "unknown"
)
type GPUInfo struct {
Name string `json:"name"`
VulkanICD string `json:"vulkan_icd"`
Kind GPUKind `json:"kind"`
}
type WHPXState string
const (
WHPXReady WHPXState = "ready"
WHPXNoHypervisor WHPXState = "no_hypervisor"
WHPXMissing WHPXState = "missing"
WHPXUnsupported WHPXState = "unsupported"
)
type WHPXCapabilities struct {
ModernOS bool `json:"modern_os"`
APICEmulation bool `json:"apic_emulation"`
KernelIrqchipAvailable bool `json:"kernel_irqchip_available"`
NestedVirtProperty bool `json:"nested_virt_property"`
NestedVirtProbeError string `json:"nested_virt_probe_error,omitempty"`
}
type LogicalCPUInfo struct {
Index int `json:"index"`
CoreIndex int `json:"core_index"`
Tier string `json:"tier"` // "p" | "e" | "lpe" | "standard"
SMT bool `json:"smt"`
}
type Report struct {
OS string `json:"os"`
Arch string `json:"arch"`
LogicalCPUs int `json:"logical_cpus"`
PhysicalCores int `json:"physical_cores"`
HasHybridCores bool `json:"has_hybrid_cores"`
PCoreCount int `json:"p_core_count"`
ECoreCount int `json:"e_core_count"`
LPECoreCount int `json:"lpe_core_count,omitempty"`
PCoreMask uint64 `json:"p_core_mask"`
PCoreMaskHex string `json:"p_core_mask_hex"`
PCoreLogicalCPUs []int `json:"p_core_logical_cpus,omitempty"`
ECoreLogicalCPUs []int `json:"e_core_logical_cpus,omitempty"`
NonLPEMask uint64 `json:"non_lpe_mask,omitempty"`
NonLPEMaskHex string `json:"non_lpe_mask_hex,omitempty"`
NonLPELogicalCPUs []int `json:"non_lpe_logical_cpus,omitempty"`
CPUTopology []LogicalCPUInfo `json:"cpu_topology,omitempty"`
TotalRAMGB float64 `json:"total_ram_gb"`
AvailRAMGB float64 `json:"avail_ram_gb"`
MemoryLoadPct int `json:"memory_load_pct"`
// Commit memory: QEMU commits all guest RAM at start (CreateFileMapping), so
// a VM needs its memory free in the commit charge, not just in physical RAM.
// Measured 2026-11-28: 14.7 GiB commit free with 17.3 GiB physical free, and a
// 16G guest failed ("Cannot set up guest memory"). portable-runtime-spec.md §5.1.
CommitAvailGB float64 `json:"commit_avail_gb"`
CommitLimitGB float64 `json:"commit_limit_gb"`
DataRoot string `json:"data_root"`
DataRootDriveType string `json:"data_root_drive_type,omitempty"`
FreeDiskGB float64 `json:"free_disk_gb"`
WHPX WHPXState `json:"whpx"`
WHPXCaps WHPXCapabilities `json:"whpx_caps"`
NeedsColdRebootWorkaround bool `json:"needs_cold_reboot_workaround"`
GPUs []GPUInfo `json:"gpus"`
RecommendedGPU *GPUInfo `json:"recommended_gpu,omitempty"`
Displays []DisplayInfo `json:"displays,omitempty"`
PrimaryDisplay *DisplayInfo `json:"primary_display,omitempty"`
RecommendedResolution string `json:"recommended_resolution,omitempty"`
DisplayPresets []DisplayPreset `json:"display_presets,omitempty"`
HiDPIAdvice string `json:"hidpi_advice,omitempty"` // how to reach HiDPI when no preset does (display.go)
FullscreenHiDPI bool `json:"fullscreen_hidpi"` // a borderless window on the primary screen is HiDPI
ProcessorScheduling string `json:"processor_scheduling,omitempty"` // "programs" or "background" (scheduling.go)
Audio AudioInfo `json:"audio"`
}
// ClassifyGPU distinguishes discrete, integrated, and virtual display adapters.
func ClassifyGPU(name string) GPUKind {
low := strings.ToLower(name)
for _, v := range []string{"todesk", "gameviewer", "parsec", "virtual", "idd", "microsoft basic", "spacedesk"} {
if strings.Contains(low, v) {
return GPUVirtual
}
}
for _, d := range []string{"nvidia", "geforce", "rtx", "gtx", "quadro", "radeon rx", "radeon pro", "intel(r) arc"} {
if strings.Contains(low, d) {
return GPUDiscrete
}
}
for _, i := range []string{"intel(r) uhd", "intel(r) hd", "intel(r) iris", "radeon(tm) graphics"} {
if strings.Contains(low, i) {
return GPUIntegrated
}
}
return GPUUnknown
}
// PickRecommendedGPU selects the best Vulkan GPU (preferring discrete over integrated, skipping virtual).
func PickRecommendedGPU(gpus []GPUInfo) *GPUInfo {
for i := range gpus {
if gpus[i].Kind == GPUDiscrete && gpus[i].VulkanICD != "" {
cp := gpus[i]
return &cp
}
}
for i := range gpus {
if gpus[i].Kind == GPUIntegrated && gpus[i].VulkanICD != "" {
cp := gpus[i]
return &cp
}
}
for i := range gpus {
if gpus[i].Kind != GPUVirtual && gpus[i].VulkanICD != "" {
cp := gpus[i]
return &cp
}
}
return nil
}
// AllCPUsMask returns the 64-bit affinity mask covering logical CPUs 0..n-1
// (capped at 64 bits, matching Win32 SetProcessAffinityMask group 0).
func AllCPUsMask(logicalCPUs int) uint64 {
if logicalCPUs <= 0 {
return 0
}
if logicalCPUs >= 64 {
return ^uint64(0)
}
return (uint64(1) << uint(logicalCPUs)) - 1
}
// CPUsToMask encodes a list of logical CPU indices (0..maxCPUs-1, max 63) into
// an affinity bitmask and returns the normalized sorted CPU list alongside it.
func CPUsToMask(cpus []int, maxCPUs int) (uint64, []int) {
limit := maxCPUs
if limit <= 0 || limit > 64 {
limit = 64
}
var mask uint64
for _, idx := range cpus {
if idx >= 0 && idx < limit {
mask |= uint64(1) << uint(idx)
}
}
return mask, MaskToCPUs(mask, limit)
}
// MaskToCPUs decodes an affinity bitmask into sorted logical CPU indices.
func MaskToCPUs(mask uint64, maxCPUs int) []int {
limit := maxCPUs
if limit <= 0 || limit > 64 {
limit = 64
}
var out []int
for i := 0; i < limit; i++ {
if mask&(uint64(1)<<uint(i)) != 0 {
out = append(out, i)
}
}
return out
}
// DefaultCPUTopology constructs a homogeneous fallback topology when detailed
// OS processor-core probing is unavailable.
func DefaultCPUTopology(logicalCPUs int) []LogicalCPUInfo {
limit := logicalCPUs
if limit < 0 {
limit = 0
}
if limit > 64 {
limit = 64
}
out := make([]LogicalCPUInfo, limit)
for i := 0; i < limit; i++ {
out[i] = LogicalCPUInfo{
Index: i,
CoreIndex: i,
Tier: "standard",
}
}
return out
}