|
| 1 | +/* |
| 2 | + * ThreadPilot - Advanced Windows Process and Power Plan Manager |
| 3 | + * Copyright (C) 2025 Prime Build |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, version 3 only. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +namespace ThreadPilot.Platforms.Windows |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Collections.Generic; |
| 21 | + using System.Linq; |
| 22 | + using ThreadPilot.Models; |
| 23 | + |
| 24 | + internal sealed class CpuSetMapping |
| 25 | + { |
| 26 | + private readonly IReadOnlyDictionary<ProcessorRef, uint> cpuSetIdsByProcessor; |
| 27 | + private readonly IReadOnlyDictionary<uint, ProcessorRef> processorsByCpuSetId; |
| 28 | + |
| 29 | + private CpuSetMapping( |
| 30 | + IReadOnlyDictionary<ProcessorRef, uint> cpuSetIdsByProcessor, |
| 31 | + IReadOnlyDictionary<uint, ProcessorRef> processorsByCpuSetId) |
| 32 | + { |
| 33 | + this.cpuSetIdsByProcessor = cpuSetIdsByProcessor; |
| 34 | + this.processorsByCpuSetId = processorsByCpuSetId; |
| 35 | + } |
| 36 | + |
| 37 | + public static CpuSetMapping Empty { get; } = new( |
| 38 | + new Dictionary<ProcessorRef, uint>(), |
| 39 | + new Dictionary<uint, ProcessorRef>()); |
| 40 | + |
| 41 | + public bool IsEmpty => this.cpuSetIdsByProcessor.Count == 0; |
| 42 | + |
| 43 | + public static CpuSetMapping Create(IReadOnlyDictionary<ProcessorRef, uint> cpuSetIdsByProcessor) |
| 44 | + { |
| 45 | + ArgumentNullException.ThrowIfNull(cpuSetIdsByProcessor); |
| 46 | + |
| 47 | + var forwardMap = cpuSetIdsByProcessor |
| 48 | + .OrderBy(kvp => kvp.Key.GlobalIndex) |
| 49 | + .ThenBy(kvp => kvp.Key.Group) |
| 50 | + .ThenBy(kvp => kvp.Key.LogicalProcessorNumber) |
| 51 | + .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); |
| 52 | + |
| 53 | + var inverseMap = forwardMap |
| 54 | + .GroupBy(kvp => kvp.Value) |
| 55 | + .ToDictionary( |
| 56 | + group => group.Key, |
| 57 | + group => group |
| 58 | + .Select(kvp => kvp.Key) |
| 59 | + .OrderBy(processor => processor.GlobalIndex) |
| 60 | + .ThenBy(processor => processor.Group) |
| 61 | + .ThenBy(processor => processor.LogicalProcessorNumber) |
| 62 | + .First()); |
| 63 | + |
| 64 | + return new CpuSetMapping(forwardMap, inverseMap); |
| 65 | + } |
| 66 | + |
| 67 | + public static ProcessorRef CreateProcessorRef(ushort group, byte logicalProcessorNumber) |
| 68 | + { |
| 69 | + return new ProcessorRef(group, logicalProcessorNumber, (group * 64) + logicalProcessorNumber); |
| 70 | + } |
| 71 | + |
| 72 | + public bool TryGetCpuSetId(ProcessorRef processor, out uint cpuSetId) |
| 73 | + { |
| 74 | + return this.cpuSetIdsByProcessor.TryGetValue(processor, out cpuSetId); |
| 75 | + } |
| 76 | + |
| 77 | + public bool TryGetProcessorRef(uint cpuSetId, out ProcessorRef processor) |
| 78 | + { |
| 79 | + return this.processorsByCpuSetId.TryGetValue(cpuSetId, out processor); |
| 80 | + } |
| 81 | + |
| 82 | + public IReadOnlyList<uint> ResolveCpuSetIds(CpuSelection selection) |
| 83 | + { |
| 84 | + ArgumentNullException.ThrowIfNull(selection); |
| 85 | + |
| 86 | + if (selection.CpuSetIds.Count > 0) |
| 87 | + { |
| 88 | + return selection.CpuSetIds |
| 89 | + .Distinct() |
| 90 | + .OrderBy(cpuSetId => cpuSetId) |
| 91 | + .ToList(); |
| 92 | + } |
| 93 | + |
| 94 | + return selection.LogicalProcessors |
| 95 | + .Select(processor => this.TryGetCpuSetId(processor, out var cpuSetId) ? (uint?)cpuSetId : null) |
| 96 | + .Where(cpuSetId => cpuSetId.HasValue) |
| 97 | + .Select(cpuSetId => cpuSetId!.Value) |
| 98 | + .Distinct() |
| 99 | + .OrderBy(cpuSetId => cpuSetId) |
| 100 | + .ToList(); |
| 101 | + } |
| 102 | + |
| 103 | + public IReadOnlyList<uint> ResolveLegacyAffinityMask(long affinityMask, int logicalProcessorCount) |
| 104 | + { |
| 105 | + var unsignedMask = unchecked((ulong)affinityMask); |
| 106 | + var maxLegacyBits = Math.Min(Math.Max(logicalProcessorCount, 0), 64); |
| 107 | + var cpuSetIds = new List<uint>(); |
| 108 | + |
| 109 | + for (var bit = 0; bit < maxLegacyBits; bit++) |
| 110 | + { |
| 111 | + if ((unsignedMask & (1UL << bit)) == 0) |
| 112 | + { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + var processor = CreateProcessorRef(0, (byte)bit); |
| 117 | + if (this.TryGetCpuSetId(processor, out var cpuSetId)) |
| 118 | + { |
| 119 | + cpuSetIds.Add(cpuSetId); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return cpuSetIds |
| 124 | + .Distinct() |
| 125 | + .OrderBy(cpuSetId => cpuSetId) |
| 126 | + .ToList(); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments