178 lines
5.3 KiB
Go
178 lines
5.3 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,
|
|
}
|
|
}
|
|
|
|
// EvaluateCoreOptimizations evaluates driver-independent host capabilities.
|
|
func EvaluateCoreOptimizations(rep Report) []Optimization {
|
|
|
|
var out []Optimization
|
|
|
|
// 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,
|
|
})
|
|
}
|
|
|
|
// 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,
|
|
})
|
|
}
|
|
|
|
// CoW backing chain depth advisory (depth >= 2 or broken parent)
|
|
if len(rep.DeepBackingChains) > 0 {
|
|
var parts []string
|
|
for _, c := range rep.DeepBackingChains {
|
|
if c.Broken {
|
|
parts = append(parts, fmt.Sprintf("%s(depth=%d,broken)", c.VMName, c.Depth))
|
|
} else {
|
|
parts = append(parts, fmt.Sprintf("%s(depth=%d)", c.VMName, c.Depth))
|
|
}
|
|
}
|
|
out = append(out, Optimization{
|
|
ID: "cow_backing_chain",
|
|
Status: "recommended",
|
|
Supported: true,
|
|
Recommended: true,
|
|
Detail: strings.Join(parts, ", "),
|
|
})
|
|
}
|
|
|
|
return out
|
|
}
|