namespace ThreadPilot.Platforms.Windows { using System; using System.Collections.Generic; using System.Linq; using ThreadPilot.Models; internal sealed class CpuSetMapping { private readonly IReadOnlyDictionary cpuSetIdsByProcessor; private readonly IReadOnlyDictionary processorsByCpuSetId; private CpuSetMapping( IReadOnlyDictionary cpuSetIdsByProcessor, IReadOnlyDictionary processorsByCpuSetId) { this.cpuSetIdsByProcessor = cpuSetIdsByProcessor; this.processorsByCpuSetId = processorsByCpuSetId; } public static CpuSetMapping Empty { get; } = new( new Dictionary(), new Dictionary()); public bool IsEmpty => this.cpuSetIdsByProcessor.Count == 0; public static CpuSetMapping Create(IReadOnlyDictionary cpuSetIdsByProcessor) { ArgumentNullException.ThrowIfNull(cpuSetIdsByProcessor); var forwardMap = cpuSetIdsByProcessor .OrderBy(kvp => kvp.Key.GlobalIndex) .ThenBy(kvp => kvp.Key.Group) .ThenBy(kvp => kvp.Key.LogicalProcessorNumber) .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); var inverseMap = forwardMap .GroupBy(kvp => kvp.Value) .ToDictionary( group => group.Key, group => group .Select(kvp => kvp.Key) .OrderBy(processor => processor.GlobalIndex) .ThenBy(processor => processor.Group) .ThenBy(processor => processor.LogicalProcessorNumber) .First()); return new CpuSetMapping(forwardMap, inverseMap); } public static ProcessorRef CreateProcessorRef(ushort group, byte logicalProcessorNumber) { return new ProcessorRef(group, logicalProcessorNumber, (group * 64) + logicalProcessorNumber); } public bool TryGetCpuSetId(ProcessorRef processor, out uint cpuSetId) { return this.cpuSetIdsByProcessor.TryGetValue(processor, out cpuSetId); } public bool TryGetProcessorRef(uint cpuSetId, out ProcessorRef processor) { return this.processorsByCpuSetId.TryGetValue(cpuSetId, out processor); } public IReadOnlyList ResolveCpuSetIds(CpuSelection selection) { ArgumentNullException.ThrowIfNull(selection); // Logical processor coordinates survive a topology change; the raw CPU Set IDs stored // with a saved rule do not - after a reboot or a CPU change the same opaque ID can name // a different processor. Resolve coordinates against the current mapping whenever we // have them, and require every one to resolve so a stale selection surfaces as an // invalid topology instead of silently pinning a subset of the wrong CPUs. if (selection.LogicalProcessors.Count > 0) { var resolved = new List(selection.LogicalProcessors.Count); foreach (var processor in selection.LogicalProcessors) { if (!this.TryGetCpuSetId(processor, out var cpuSetId)) { return []; } resolved.Add(cpuSetId); } return resolved .Distinct() .OrderBy(cpuSetId => cpuSetId) .ToList(); } return selection.CpuSetIds .Distinct() .OrderBy(cpuSetId => cpuSetId) .ToList(); } public IReadOnlyList ResolveLegacyAffinityMask(long affinityMask, int logicalProcessorCount) { var unsignedMask = unchecked((ulong)affinityMask); var maxLegacyBits = Math.Min(Math.Max(logicalProcessorCount, 0), 64); var cpuSetIds = new List(); for (var bit = 0; bit < maxLegacyBits; bit++) { if ((unsignedMask & (1UL << bit)) == 0) { continue; } var processor = CreateProcessorRef(0, (byte)bit); if (this.TryGetCpuSetId(processor, out var cpuSetId)) { cpuSetIds.Add(cpuSetId); } } return cpuSetIds .Distinct() .OrderBy(cpuSetId => cpuSetId) .ToList(); } } }