110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
// canScreendump reports whether the host can read a VM's screen over its QMP
|
|
// socket: the VM must declare `qmp` and must not be hosted. A VM with no
|
|
// declared capabilities is a legacy macOS VM.
|
|
func canScreendump(caps []string) bool {
|
|
if driver.IsHosted(caps) {
|
|
return false
|
|
}
|
|
return len(caps) == 0 || driver.HasCapability(caps, driver.CapQMP)
|
|
}
|
|
|
|
// readyToken and endpointSignature fold a VM's readiness and endpoints into
|
|
// the poll signature, so the UI refreshes when a driver flips them.
|
|
func readyToken(ready *bool) string {
|
|
switch {
|
|
case ready == nil:
|
|
return "?"
|
|
case *ready:
|
|
return "1"
|
|
default:
|
|
return "0"
|
|
}
|
|
}
|
|
|
|
func endpointSignature(eps []driver.VMEndpoint) string {
|
|
var b strings.Builder
|
|
for _, ep := range eps {
|
|
fmt.Fprintf(&b, "%s/%t,", ep.Kind, ep.Ready)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// stateTable returns the table of driver-reported VM states (nil-safe to use).
|
|
func (a *App) stateTable() *driver.StateTable {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
return a.vmStateTable
|
|
}
|
|
|
|
// broker returns the plugin broker, or nil before plugins start.
|
|
func (a *App) broker() *plugin.Broker {
|
|
a.mu.Lock()
|
|
mgr := a.pluginMgr
|
|
a.mu.Unlock()
|
|
if mgr == nil {
|
|
return nil
|
|
}
|
|
return mgr.Broker()
|
|
}
|
|
|
|
// brokerRunsVM reports whether the host spawned a still-running process tree
|
|
// for the VM on a driver's behalf; the broker's job watches that tree.
|
|
func (a *App) brokerRunsVM(driverID, name string) bool {
|
|
br := a.broker()
|
|
return br != nil && driverID != "" && br.IsManagedVMRunning(driverID, name)
|
|
}
|
|
|
|
// vmProcessPIDs lists every process of a VM: the whole job tree when the host
|
|
// spawned it for a driver, else only rootPID.
|
|
func (a *App) vmProcessPIDs(ref driver.VMRef, rootPID int) []uint32 {
|
|
if br := a.broker(); br != nil {
|
|
if pids := br.ManagedVMPIDs(ref.DriverID, ref.Name); len(pids) > 0 {
|
|
return pids
|
|
}
|
|
}
|
|
if rootPID > 0 {
|
|
return []uint32{uint32(rootPID)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// applyVMAffinity applies the VM's saved CPU affinity. A VM the host spawned in
|
|
// a per-VM job is limited through the job, so children the launcher starts
|
|
// later are covered; other VMs are pinned by PID.
|
|
func (a *App) applyVMAffinity(loc i18n.Locale, rep host.Report, ref driver.VMRef, vmDir string, globalAutoPin bool, pid, vcpus int, force bool) (string, error) {
|
|
br := a.broker()
|
|
if br == nil || !br.IsManagedVMRunning(ref.DriverID, ref.Name) {
|
|
return vmstart.ApplyVMAffinity(loc, rep, vmDir, globalAutoPin, pid, vcpus, force)
|
|
}
|
|
res := vmstart.ResolveVMAffinity(vmstart.ReadVMAffinity(vmDir), globalAutoPin, rep, vcpus)
|
|
if res.DisableEcoQoS {
|
|
for _, p := range br.ManagedVMPIDs(ref.DriverID, ref.Name) {
|
|
_ = host.DisableProcessPowerThrottling(p)
|
|
}
|
|
}
|
|
if !res.ShouldPin && !force {
|
|
return "", nil
|
|
}
|
|
mask := res.Mask
|
|
if res.EffectiveMode == vmstart.AffinityAll {
|
|
mask = 0 // lift the job's limit
|
|
}
|
|
if err := br.SetManagedVMAffinity(ref.DriverID, ref.Name, mask); err != nil {
|
|
return fmt.Sprintf("[CPU] Warning: CPU affinity update failed for %s (mask 0x%X): %v", ref.Name, mask, err), err
|
|
}
|
|
return fmt.Sprintf("[CPU] Limited %s and its child processes to %d host CPUs (mask %s)", ref.Name, len(res.CPUs), res.MaskHex), nil
|
|
}
|