Files
Reims-Go/internal/host/host.go
T

286 lines
9.7 KiB
Go

package host
import (
"strings"
)
type GPUKind string
const (
GPUDiscrete GPUKind = "discrete"
GPUIntegrated GPUKind = "integrated"
GPUVirtual GPUKind = "virtual"
GPUUnknown GPUKind = "unknown"
)
type GPUCompat string
const (
GPUCompatSupported GPUCompat = "supported"
GPUCompatWarn GPUCompat = "warn"
GPUCompatUnsupported GPUCompat = "unsupported"
GPUCompatUnknown GPUCompat = "unknown"
)
type GPUInfo struct {
Name string `json:"name"`
VulkanICD string `json:"vulkan_icd"`
Kind GPUKind `json:"kind"`
VendorID string `json:"vendor_id,omitempty"`
DeviceID string `json:"device_id,omitempty"`
DriverVersion string `json:"driver_version,omitempty"`
VulkanVersion string `json:"vulkan_version,omitempty"`
Compat GPUCompat `json:"compat,omitempty"`
CompatCode string `json:"compat_code,omitempty"`
CompatDetail string `json:"compat_detail,omitempty"`
}
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"`
NestedVMX bool `json:"nested_vmx"`
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 {
DefenderRealtimeActive bool `json:"defender_realtime_active,omitempty"`
OS string `json:"os"`
Arch string `json:"arch"`
Timezone string `json:"timezone,omitempty"`
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"`
MemorySpec *HostMemorySpec `json:"memory_spec,omitempty"`
// QEMU commits all guest RAM at start (CreateFileMapping), so a VM requires
// available Windows commit charge rather than physical free RAM alone. Both
// values include expandable paging-file headroom (CommitHeadroom).
CommitAvailGB float64 `json:"commit_avail_gb"`
CommitLimitGB float64 `json:"commit_limit_gb"`
PagingFileGrowthGB float64 `json:"paging_file_growth_gb,omitempty"` // part of both values above
DataRoot string `json:"data_root"`
DataRootDriveType string `json:"data_root_drive_type,omitempty"`
DeepBackingChains []BackingChainIssue `json:"deep_backing_chains,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"`
VulkanProbeError string `json:"vulkan_probe_error,omitempty"`
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
// from their Windows display adapter name when Vulkan device-type metadata is
// unavailable.
func ClassifyGPU(name string) GPUKind {
low := strings.ToLower(strings.TrimSpace(name))
for _, v := range []string{
"todesk", "gameviewer", "parsec", "virtual", "idd",
"microsoft basic", "microsoft remote", "spacedesk",
"llvmpipe", "lavapipe", "swiftshader",
} {
if strings.Contains(low, v) {
return GPUVirtual
}
}
// Integrated Intel Arc (Core Ultra Meteor/Arrow/Lunar Lake: "Intel(R) Arc(TM) Graphics",
// "Intel(R) Arc(TM) 140V/130V/140T/130T") must be checked before discrete "intel(r) arc".
if isIntegratedIntelArcName(low) {
return GPUIntegrated
}
for _, d := range []string{
"nvidia", "geforce", "rtx", "gtx", "quadro",
"radeon rx", "radeon pro", "radeon vii", "radeon r9", "radeon r7",
"intel(r) arc", "intel arc",
} {
if strings.Contains(low, d) {
return GPUDiscrete
}
}
for _, i := range []string{
"intel(r) uhd", "intel(r) hd", "intel(r) iris", "intel(r) graphics",
"intel uhd", "intel hd", "intel iris",
"radeon(tm) graphics", "radeon graphics",
"radeon(tm) vega", "radeon vega",
"radeon 610m", "radeon 660m", "radeon 680m",
"radeon 740m", "radeon 760m", "radeon 780m",
"radeon 880m", "radeon 890m",
} {
if strings.Contains(low, i) {
return GPUIntegrated
}
}
return GPUUnknown
}
func isIntegratedIntelArcName(low string) bool {
if !strings.Contains(low, "intel") || !strings.Contains(low, "arc") {
return false
}
// Discrete Arc cards carry an A/B/Pro series model designator (e.g. A380, A770, B580, Pro A60).
for _, token := range []string{" a3", " a5", " a7", " b5", " b7", " pro "} {
if strings.Contains(low, token) {
return false
}
}
return strings.Contains(low, "arc(tm) graphics") ||
strings.Contains(low, "arc graphics") ||
strings.Contains(low, "130v") ||
strings.Contains(low, "140v") ||
strings.Contains(low, "130t") ||
strings.Contains(low, "140t")
}
// PickRecommendedGPU selects the best Vulkan GPU, preferring verified-supported
// discrete GPUs over integrated ones, skipping virtual and unsupported GPUs,
// and falling back to warned GPUs only when no cleaner option exists.
func PickRecommendedGPU(gpus []GPUInfo) *GPUInfo {
compatRank := func(c GPUCompat) int {
switch c {
case GPUCompatSupported, "":
return 0
case GPUCompatUnknown:
return 1
case GPUCompatWarn:
return 2
default: // GPUCompatUnsupported
return 99
}
}
kindRank := func(k GPUKind) int {
switch k {
case GPUDiscrete:
return 0
case GPUIntegrated:
return 1
default:
return 2
}
}
bestIdx := -1
bestScore := 1000
for i := range gpus {
g := gpus[i]
if g.Kind == GPUVirtual || g.Compat == GPUCompatUnsupported {
continue
}
if g.VulkanICD == "" && g.VulkanVersion == "" {
continue
}
score := compatRank(g.Compat)*10 + kindRank(g.Kind)
if score < bestScore {
bestScore = score
bestIdx = i
}
}
if bestIdx < 0 {
return nil
}
cp := gpus[bestIdx]
return &cp
}
// 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
}