import { AlertTriangle, Check, Cpu } from 'lucide-react' import React, { useEffect, useMemo, useState } from 'react' import '../i18n/domains/settings.gen' import { I18nKey, parseAppError, t } from '../i18n' import { HostReport, Locale, LogicalCPUInfo, VMAffinityMode, VMAffinityStatus } from '../types' import { Button } from '../ui/controls' import { Modal } from '../ui/dialog' import { Segmented, Switch } from '../ui/settings' import { api } from '../wails' interface Props { locale: Locale vmName: string driverID: string vcpus: number host: HostReport globalAutoPin: boolean onClose: () => void onApplied: (status: VMAffinityStatus) => void } function buildTopology(host: HostReport): LogicalCPUInfo[] { if (host.cpu_topology && host.cpu_topology.length > 0) { return host.cpu_topology } const n = Math.min(Math.max(host.logical_cpus || 1, 1), 64) return Array.from({ length: n }, (_, i) => ({ index: i, core_index: i, tier: 'standard', smt: false, })) } function maskHexFromCPUs(cpus: number[]): string { let mask = 0n for (const idx of cpus) { if (idx >= 0 && idx < 64) { mask |= 1n << BigInt(idx) } } return '0x' + mask.toString(16).toUpperCase() } function resolveAutoPreview(topology: LogicalCPUInfo[], host: HostReport, globalAutoPin: boolean, vcpus: number): number[] { const all = topology.map((c) => c.index) if (!globalAutoPin || !host.has_hybrid_cores) return all const pCPUs = topology.filter((c) => c.tier === 'p').map((c) => c.index) if (pCPUs.length >= vcpus + 1 && pCPUs.length > 0) return pCPUs if ((host.lpe_core_count ?? 0) > 0) { const nonLPE = topology.filter((c) => c.tier !== 'lpe').map((c) => c.index) if (nonLPE.length >= vcpus + 1 && nonLPE.length > 0) return nonLPE } return all } export function formatAffinitySummary(locale: Locale, status: VMAffinityStatus | null): string { if (!status) return '—' const tr = (k: I18nKey, v?: Record) => t(locale, k, v) const count = status.effective_cpus?.length ?? 0 const mask = status.effective_mask_hex || '0x0' switch (status.mode) { case 'all': return tr('affinitySummaryAll', { count }) case 'pcores': return tr('affinitySummaryPCores', { count, mask }) case 'non_lpe': return tr('affinitySummaryNonLPE', { count, mask }) case 'custom': return tr('affinitySummaryCustom', { count, mask }) default: { const effLabel = status.effective_mode === 'pcores' ? tr('affinityModePCores') : status.effective_mode === 'non_lpe' ? tr('affinityModeNonLPE') : tr('affinityModeAll') return tr('affinitySummaryAuto', { eff: effLabel, count }) } } } export const AffinityDialog: React.FC = ({ locale, vmName, driverID, vcpus, host, globalAutoPin, onClose, onApplied, }) => { const tr = (k: I18nKey, v?: Record) => t(locale, k, v) const topology = useMemo(() => buildTopology(host), [host]) const allIndices = useMemo(() => topology.map((c) => c.index), [topology]) const hasSMT = useMemo(() => topology.some((c) => c.smt), [topology]) const [status, setStatus] = useState(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [err, setErr] = useState('') const [mode, setMode] = useState('auto') const [selected, setSelected] = useState(allIndices) const [disableEcoQoS, setDisableEcoQoS] = useState(true) useEffect(() => { let live = true setLoading(true) setErr('') api.GetVMAffinity(driverID, vmName) .then((st) => { if (!live) return setStatus(st) setMode(st.mode || 'auto') const initial = st.mode === 'custom' && st.cpus?.length > 0 ? st.cpus : (st.effective_cpus?.length > 0 ? st.effective_cpus : allIndices) setSelected([...initial].sort((a, b) => a - b)) setDisableEcoQoS(st.disable_eco_qos) }) .catch((e) => { if (live) setErr(parseAppError(e).summary) }) .finally(() => { if (live) setLoading(false) }) return () => { live = false } }, [vmName, allIndices]) const selectedSet = useMemo(() => new Set(selected), [selected]) const effectiveVCPUs = status?.vcpus || vcpus const maskHex = useMemo(() => maskHexFromCPUs(selected), [selected]) const applyPreset = (nextMode: VMAffinityMode) => { setMode(nextMode) setErr('') switch (nextMode) { case 'all': setSelected(allIndices) break case 'pcores': { const list = topology.filter((c) => c.tier === 'p').map((c) => c.index) setSelected(list.length > 0 ? list : allIndices) break } case 'non_lpe': { const list = topology.filter((c) => c.tier !== 'lpe').map((c) => c.index) setSelected(list.length > 0 ? list : allIndices) break } case 'auto': setSelected(resolveAutoPreview(topology, host, globalAutoPin, effectiveVCPUs)) break case 'custom': break } } const toggleCPU = (idx: number) => { setMode('custom') setErr('') setSelected((prev) => { const s = new Set(prev) if (s.has(idx)) s.delete(idx) else s.add(idx) return Array.from(s).sort((a, b) => a - b) }) } const pickCustomList = (list: number[]) => { setMode('custom') setErr('') setSelected([...new Set(list)].sort((a, b) => a - b)) } const physicalOnlyList = useMemo(() => { const seen = new Set() const out: number[] = [] for (const c of topology) { if (!seen.has(c.core_index)) { seen.add(c.core_index) out.push(c.index) } } return out }, [topology]) const modeOptions: { value: VMAffinityMode; label: string }[] = [ { value: 'auto', label: tr('affinityModeAuto') }, { value: 'all', label: tr('affinityModeAll') }, ...(host.has_hybrid_cores ? [{ value: 'pcores' as const, label: tr('affinityModePCores') }] : []), ...((host.lpe_core_count ?? 0) > 0 ? [{ value: 'non_lpe' as const, label: tr('affinityModeNonLPE') }] : []), { value: 'custom', label: tr('affinityModeCustom') }, ] const modeHintKey: Record = { auto: 'affinityModeAutoHint', all: 'affinityModeAllHint', pcores: 'affinityModePCoresHint', non_lpe: 'affinityModeNonLPEHint', custom: 'affinityModeCustomHint', } const emptyCustom = mode === 'custom' && selected.length === 0 const oversubscribed = effectiveVCPUs > 0 && selected.length > 0 && selected.length < effectiveVCPUs const allSelected = selected.length === allIndices.length && allIndices.length > 0 const handleSave = async () => { if (emptyCustom) { setErr(tr('affinityNeedOne')) return } setSaving(true) setErr('') try { const updated = await api.SetVMAffinity(driverID, vmName, { mode, cpus: mode === 'custom' ? selected : [], disable_eco_qos: disableEcoQoS, }) onApplied(updated) } catch (e) { setErr(parseAppError(e).summary) } finally { setSaving(false) } } const subtitle = status?.running && status.pid ? tr('affinitySubtitleRunning', { pid: status.pid, vcpus: effectiveVCPUs }) : tr('affinitySubtitleStopped', { vcpus: effectiveVCPUs }) const tierLabel = (tier: string) => { if (tier === 'p') return 'P' if (tier === 'e') return 'E' if (tier === 'lpe') return 'LP-E' return '' } return (
applyPreset(m as VMAffinityMode)} options={modeOptions} />

{tr(modeHintKey[mode])}

{hasSMT && ( )} {allIndices.length > 1 && ( )} {host.has_hybrid_cores && ( )} {(host.lpe_core_count ?? 0) > 0 && ( )}
{topology.map((cpu) => { const checked = selectedSet.has(cpu.index) const tLabel = tierLabel(cpu.tier) return ( ) })}
{status?.running && status.live_mask_hex && ( {tr('affinityLiveMask', { mask: status.live_mask_hex, count: status.live_cpus?.length ?? 0 })} )}
{oversubscribed && (
)}
{tr('affinityEcoQoS')}
{tr('affinityEcoQoSDesc')}
{err &&
{err}
}
) }