239 lines
6.9 KiB
Go
239 lines
6.9 KiB
Go
package host
|
|
|
|
import (
|
|
"fmt"
|
|
"math/bits"
|
|
"strings"
|
|
)
|
|
|
|
// Optimization summarizes the probed host availability and recommendation for
|
|
// one hardware/runtime optimization toggle.
|
|
type Optimization struct {
|
|
ID string `json:"id"`
|
|
Status string `json:"status"` // recommended | available | experimental | auto | unavailable
|
|
Supported bool `json:"supported"`
|
|
Recommended bool `json:"recommended"`
|
|
Detail string `json:"detail,omitempty"`
|
|
}
|
|
|
|
// CanSafelyPinPCores reports whether pinning a QEMU process with vmCPUs vCPUs
|
|
// exclusively to the host's P-cores is safe without oversubscribing logical
|
|
// CPUs. On non-SMT hybrid CPUs such as Intel Core Ultra 200 (Arrow Lake, e.g.
|
|
// 6P/6T + 8E), pinning an 8-vCPU VM onto 6 logical CPUs causes XNU spinlock
|
|
// lock-holder preemption stalls. Requires at least vmCPUs+1 P-core logical CPUs
|
|
// so QEMU I/O and vGPU threads have headroom alongside the vCPUs.
|
|
func CanSafelyPinPCores(rep Report, vmCPUs int) (bool, int) {
|
|
pThreads := len(rep.PCoreLogicalCPUs)
|
|
if pThreads == 0 {
|
|
pThreads = rep.PCoreCount
|
|
}
|
|
if !rep.HasHybridCores || rep.PCoreMask == 0 || pThreads <= 0 || vmCPUs <= 0 {
|
|
return false, pThreads
|
|
}
|
|
return pThreads >= vmCPUs+1, pThreads
|
|
}
|
|
|
|
// AffinityPlan describes the safest CPU affinity mask to apply to a running
|
|
// QEMU process on a hybrid host without oversubscribing logical CPUs.
|
|
type AffinityPlan struct {
|
|
Mode string // "pcores" | "non_lpe" | "all"
|
|
Mask uint64
|
|
MaskHex string
|
|
CoreCount int
|
|
ThreadCount int
|
|
PThreads int
|
|
}
|
|
|
|
// SelectSafeAffinity chooses P-core-only pinning when P-core logical CPUs have
|
|
// headroom (>= vmCPUs+1). On 3-tier hybrid CPUs (such as Intel Core Ultra 200
|
|
// Arrow Lake: 6P/6T + 8E + 2LP-E) where vmCPUs exceeds P-core threads, it falls
|
|
// back to pinning P+E cores (excluding only the slow SoC LP-E cores) instead of
|
|
// oversubscribing 6 cores or exposing guest vCPUs to LP-E cores.
|
|
func SelectSafeAffinity(rep Report, vmCPUs int) AffinityPlan {
|
|
ok, pThreads := CanSafelyPinPCores(rep, vmCPUs)
|
|
if ok {
|
|
return AffinityPlan{
|
|
Mode: "pcores",
|
|
Mask: rep.PCoreMask,
|
|
MaskHex: rep.PCoreMaskHex,
|
|
CoreCount: rep.PCoreCount,
|
|
ThreadCount: pThreads,
|
|
PThreads: pThreads,
|
|
}
|
|
}
|
|
if rep.LPECoreCount > 0 && rep.NonLPEMask > 0 && vmCPUs > 0 {
|
|
nonLPEThreads := len(rep.NonLPELogicalCPUs)
|
|
if nonLPEThreads == 0 {
|
|
nonLPEThreads = bits.OnesCount64(rep.NonLPEMask)
|
|
}
|
|
if nonLPEThreads >= vmCPUs+1 {
|
|
hex := rep.NonLPEMaskHex
|
|
if hex == "" {
|
|
hex = fmt.Sprintf("0x%X", rep.NonLPEMask)
|
|
}
|
|
return AffinityPlan{
|
|
Mode: "non_lpe",
|
|
Mask: rep.NonLPEMask,
|
|
MaskHex: hex,
|
|
CoreCount: rep.PCoreCount + rep.ECoreCount,
|
|
ThreadCount: nonLPEThreads,
|
|
PThreads: pThreads,
|
|
}
|
|
}
|
|
}
|
|
return AffinityPlan{
|
|
Mode: "all",
|
|
PThreads: pThreads,
|
|
}
|
|
}
|
|
|
|
// EvaluateOptimizations combines host capability probes with the active
|
|
// runtime's patch list so the UI can show accurate, self-explanatory guidance
|
|
// for every optimization toggle without guessing.
|
|
func EvaluateOptimizations(rep Report, runtimePatches []string) []Optimization {
|
|
hasPatch := func(substr string) bool {
|
|
for _, p := range runtimePatches {
|
|
if strings.Contains(strings.ToLower(p), strings.ToLower(substr)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var out []Optimization
|
|
|
|
// 1. P-Core affinity pinning (hybrid Intel CPUs)
|
|
if rep.HasHybridCores {
|
|
detail := fmt.Sprintf("%dP+%dE", rep.PCoreCount, rep.ECoreCount)
|
|
if len(rep.PCoreLogicalCPUs) > 0 {
|
|
detail = fmt.Sprintf("%dP(%dT)+%dE", rep.PCoreCount, len(rep.PCoreLogicalCPUs), rep.ECoreCount)
|
|
}
|
|
if rep.LPECoreCount > 0 {
|
|
detail += fmt.Sprintf("+%dLPE", rep.LPECoreCount)
|
|
}
|
|
out = append(out, Optimization{
|
|
ID: "pcore_pin",
|
|
Status: "recommended",
|
|
Supported: true,
|
|
Recommended: true,
|
|
Detail: detail,
|
|
})
|
|
} else {
|
|
out = append(out, Optimization{
|
|
ID: "pcore_pin",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
})
|
|
}
|
|
|
|
// 2. WHPX kernel-irqchip (APIC emulation in Hyper-V)
|
|
switch {
|
|
case rep.WHPX != WHPXReady || !rep.WHPXCaps.KernelIrqchipAvailable:
|
|
out = append(out, Optimization{
|
|
ID: "kernel_irqchip",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
})
|
|
case !rep.NeedsColdRebootWorkaround:
|
|
// Windows 11 / Server 2022+: QEMU enables kernel-irqchip by default,
|
|
// unless NestedVirtualization property probe fails (e.g. hr=0x80370302)
|
|
// on an unpatched runtime.
|
|
if rep.WHPXCaps.NestedVirtProperty {
|
|
out = append(out, Optimization{
|
|
ID: "kernel_irqchip",
|
|
Status: "recommended",
|
|
Supported: true,
|
|
Recommended: true,
|
|
})
|
|
} else if hasPatch("nested-virt-nonfatal") {
|
|
out = append(out, Optimization{
|
|
ID: "kernel_irqchip",
|
|
Status: "recommended",
|
|
Supported: true,
|
|
Recommended: true,
|
|
Detail: "win11-nested-virt-nonfatal",
|
|
})
|
|
} else {
|
|
out = append(out, Optimization{
|
|
ID: "kernel_irqchip",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
Detail: rep.WHPXCaps.NestedVirtProbeError,
|
|
})
|
|
}
|
|
default:
|
|
// Windows 10: requires startup-ipis patch in runtime for multi-core boot.
|
|
if hasPatch("startup-ipis") {
|
|
out = append(out, Optimization{
|
|
ID: "kernel_irqchip",
|
|
Status: "experimental",
|
|
Supported: true,
|
|
Detail: "win10-startup-ipis",
|
|
})
|
|
} else {
|
|
out = append(out, Optimization{
|
|
ID: "kernel_irqchip",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
Detail: "win10-needs-patch",
|
|
})
|
|
}
|
|
}
|
|
|
|
// 3. Win10 cold-reboot compensation
|
|
if rep.NeedsColdRebootWorkaround {
|
|
out = append(out, Optimization{
|
|
ID: "cold_reboot",
|
|
Status: "auto",
|
|
Supported: true,
|
|
Recommended: true,
|
|
})
|
|
} else {
|
|
out = append(out, Optimization{
|
|
ID: "cold_reboot",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
})
|
|
}
|
|
|
|
// 4. Discrete Vulkan GPU acceleration
|
|
if rep.RecommendedGPU != nil && rep.RecommendedGPU.Compat == GPUCompatUnsupported {
|
|
out = append(out, Optimization{
|
|
ID: "discrete_gpu",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
Detail: rep.RecommendedGPU.Name,
|
|
})
|
|
} else if rep.RecommendedGPU != nil && rep.RecommendedGPU.Compat == GPUCompatWarn {
|
|
out = append(out, Optimization{
|
|
ID: "discrete_gpu",
|
|
Status: "experimental",
|
|
Supported: true,
|
|
Detail: rep.RecommendedGPU.Name,
|
|
})
|
|
} else if rep.RecommendedGPU != nil && rep.RecommendedGPU.Kind == GPUDiscrete {
|
|
out = append(out, Optimization{
|
|
ID: "discrete_gpu",
|
|
Status: "recommended",
|
|
Supported: true,
|
|
Recommended: true,
|
|
Detail: rep.RecommendedGPU.Name,
|
|
})
|
|
} else if rep.RecommendedGPU != nil {
|
|
out = append(out, Optimization{
|
|
ID: "discrete_gpu",
|
|
Status: "available",
|
|
Supported: true,
|
|
Detail: rep.RecommendedGPU.Name,
|
|
})
|
|
} else {
|
|
out = append(out, Optimization{
|
|
ID: "discrete_gpu",
|
|
Status: "unavailable",
|
|
Supported: false,
|
|
})
|
|
}
|
|
|
|
return out
|
|
}
|