Files
Reims-Go/internal/vmstart/core.go
T
Nwflower aacaec535d Merge origin/main into feat/vmref-driver-start
Adapt Intel VM suspend/resume to driver.SuspendDriver and lifecycle.Service, move native VM close confirmation into internal/host, migrate vm-suspend legacy capability detection, drop moved runtime build tooling, and regenerate i18n and Wails bindings.
2026-12-01 03:47:20 -07:00

125 lines
3.5 KiB
Go

// Package vmstart holds driver-independent start helpers: commit-memory
// checks, process affinity and command-output line writing.
package vmstart
import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/Nwflower/reims-go/internal/host"
"github.com/Nwflower/reims-go/internal/i18n"
)
func parseSizeGB(raw string) (float64, bool) {
raw = strings.TrimSpace(raw)
if len(raw) < 2 {
return 0, false
}
v, err := strconv.ParseFloat(raw[:len(raw)-1], 64)
if err != nil || v <= 0 {
return 0, false
}
switch strings.ToUpper(raw[len(raw)-1:]) {
case "G":
return v, true
case "M":
return v / 1024, true
default:
return 0, false
}
}
// WaitForCommitMemory waits until guest RAM plus the host margin is available.
func WaitForCommitMemory(ctx context.Context, memory string, timeout time.Duration, onWait func(availGB, neededGB float64)) bool {
return waitForCommitMemory(ctx, memory, timeout, 250*time.Millisecond, host.CommitAvailGB, onWait)
}
func waitForCommitMemory(ctx context.Context, memory string, timeout, pollInterval time.Duration, probe func() (float64, bool), onWait func(availGB, neededGB float64)) bool {
memGB, ok := parseSizeGB(memory)
if !ok {
return true
}
needed := memGB + host.StartMarginGB
avail, ok := probe()
if !ok || needed <= avail {
return true
}
if onWait != nil {
onWait(avail, needed)
}
if timeout <= 0 {
return false
}
if pollInterval <= 0 {
pollInterval = 250 * time.Millisecond
}
timer := time.NewTimer(timeout)
defer timer.Stop()
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return false
case <-timer.C:
avail, ok := probe()
return !ok || needed <= avail
case <-ticker.C:
avail, ok := probe()
if !ok || needed <= avail {
return true
}
}
}
}
// PinQEMU applies the safe host affinity plan and returns a human log line.
func PinQEMU(loc i18n.Locale, rep host.Report, pid, vcpus int) string {
plan := host.SelectSafeAffinity(rep, vcpus)
if plan.Mode == "all" {
return i18n.M(loc, "vmstart.pin.skipped", i18n.Vars{"vcpus": vcpus, "pthreads": plan.PThreads})
}
err := host.PinProcessToPCores(uint32(pid), plan.Mask)
if plan.Mode == "non_lpe" {
if err != nil {
return fmt.Sprintf("[CPU] Warning: P+E core pinning failed for PID %d: %v", pid, err)
}
return i18n.M(loc, "vmstart.pin.non_lpe", i18n.Vars{"vcpus": vcpus, "pthreads": plan.PThreads, "pid": pid, "cores": plan.CoreCount, "threads": plan.ThreadCount, "mask": plan.MaskHex, "lpe": rep.LPECoreCount})
}
if err != nil {
return fmt.Sprintf("[CPU] Warning: P-core pinning failed for PID %d: %v", pid, err)
}
return i18n.M(loc, "vmstart.pin.pcores", i18n.Vars{"pid": pid, "cores": plan.CoreCount, "threads": plan.ThreadCount, "mask": plan.MaskHex, "excluded": rep.ECoreCount + rep.LPECoreCount})
}
// LineWriter turns command output into one log call per non-empty line.
type LineWriter func(string)
func (w LineWriter) Write(b []byte) (int, error) {
sc := bufio.NewScanner(strings.NewReader(string(b)))
for sc.Scan() {
if line := strings.TrimSpace(sc.Text()); line != "" {
w(line)
}
}
return len(b), nil
}
// CloseRequestEnv returns the environment variables that let a VM display
// window delegate close confirmation back to this desktop client executable.
func CloseRequestEnv(name string) []string {
executable, err := os.Executable()
if err != nil {
return nil
}
return []string{
"REIMS_GO_VM_CLOSE_EXE=" + executable,
"REIMS_GO_VM_CLOSE_NAME=" + name,
}
}