fix: P/E cores from the highest EfficiencyClass, AMD CCDs from real L3 groups
The legacy topology treated the lowest EfficiencyClass as the performance core, swapping P- and E-cores on hybrid Intel CPUs. AMD CCDs were guessed as 8 cores each, which split 6+6 parts (7900X3D, 9900X3D) into 8+4; they now come from the L3 groups Windows reports, with the guess kept only as a fallback. The topology snapshot also keeps each L3 group's size.
This commit is contained in:
+11
-3
@@ -59,6 +59,7 @@ namespace ThreadPilot.Models
|
||||
private readonly IReadOnlyDictionary<ProcessorRef, int> lastLevelCacheIndexesByProcessor;
|
||||
private readonly IReadOnlyDictionary<ProcessorRef, int> packageIndexesByProcessor;
|
||||
private readonly IReadOnlyDictionary<ProcessorRef, IReadOnlyList<int>> smtSiblingGlobalIndexesByProcessor;
|
||||
private readonly IReadOnlyDictionary<int, ulong> lastLevelCacheSizesByIndex;
|
||||
|
||||
private CpuTopologySnapshot(
|
||||
IReadOnlyList<ProcessorRef> logicalProcessors,
|
||||
@@ -70,7 +71,8 @@ namespace ThreadPilot.Models
|
||||
IReadOnlyDictionary<ProcessorRef, int> packageIndexesByProcessor,
|
||||
IReadOnlyDictionary<ProcessorRef, IReadOnlyList<int>> smtSiblingGlobalIndexesByProcessor,
|
||||
CpuTopologySignature signature,
|
||||
IReadOnlyList<int> reservedLogicalProcessorIndexes)
|
||||
IReadOnlyList<int> reservedLogicalProcessorIndexes,
|
||||
IReadOnlyDictionary<int, ulong> lastLevelCacheSizesByIndex)
|
||||
{
|
||||
this.LogicalProcessors = logicalProcessors;
|
||||
this.cpuSetIdsByProcessor = cpuSetIdsByProcessor;
|
||||
@@ -82,6 +84,7 @@ namespace ThreadPilot.Models
|
||||
this.smtSiblingGlobalIndexesByProcessor = smtSiblingGlobalIndexesByProcessor;
|
||||
this.Signature = signature;
|
||||
this.ReservedLogicalProcessorIndexes = reservedLogicalProcessorIndexes;
|
||||
this.lastLevelCacheSizesByIndex = lastLevelCacheSizesByIndex;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ProcessorRef> LogicalProcessors { get; }
|
||||
@@ -100,7 +103,8 @@ namespace ThreadPilot.Models
|
||||
IReadOnlyDictionary<ProcessorRef, int>? lastLevelCacheIndexes = null,
|
||||
IReadOnlyDictionary<ProcessorRef, int>? packageIndexes = null,
|
||||
IReadOnlyDictionary<ProcessorRef, IReadOnlyList<int>>? smtSiblingGlobalIndexes = null,
|
||||
IEnumerable<int>? reservedLogicalProcessorIndexes = null)
|
||||
IEnumerable<int>? reservedLogicalProcessorIndexes = null,
|
||||
IReadOnlyDictionary<int, ulong>? lastLevelCacheSizes = null)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(logicalProcessors);
|
||||
|
||||
@@ -177,7 +181,8 @@ namespace ThreadPilot.Models
|
||||
.Where(knownGlobalIndexes.Contains)
|
||||
.Distinct()
|
||||
.OrderBy(index => index)
|
||||
.ToList());
|
||||
.ToList(),
|
||||
lastLevelCacheSizes?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value) ?? new Dictionary<int, ulong>());
|
||||
}
|
||||
|
||||
public bool TryGetCpuSetId(ProcessorRef processor, out uint cpuSetId) =>
|
||||
@@ -195,6 +200,9 @@ namespace ThreadPilot.Models
|
||||
public bool TryGetLastLevelCacheIndex(ProcessorRef processor, out int lastLevelCacheIndex) =>
|
||||
this.lastLevelCacheIndexesByProcessor.TryGetValue(processor, out lastLevelCacheIndex);
|
||||
|
||||
public bool TryGetLastLevelCacheSize(int lastLevelCacheIndex, out ulong cacheSizeBytes) =>
|
||||
this.lastLevelCacheSizesByIndex.TryGetValue(lastLevelCacheIndex, out cacheSizeBytes);
|
||||
|
||||
public bool TryGetPackageIndex(ProcessorRef processor, out int packageIndex) =>
|
||||
this.packageIndexesByProcessor.TryGetValue(processor, out packageIndex);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace ThreadPilot.Services
|
||||
public class CpuTopologyService : ICpuTopologyService
|
||||
{
|
||||
private readonly ILogger<CpuTopologyService> logger;
|
||||
private readonly ICpuTopologyProvider? topologyProvider;
|
||||
private readonly SemaphoreSlim detectSemaphore = new(1, 1);
|
||||
private CpuTopologyModel? currentTopology;
|
||||
private DateTime topologyCachedAtUtc = DateTime.MinValue;
|
||||
@@ -72,9 +73,10 @@ namespace ThreadPilot.Services
|
||||
IntPtr buffer,
|
||||
ref int returnedLength);
|
||||
|
||||
public CpuTopologyService(ILogger<CpuTopologyService> logger)
|
||||
public CpuTopologyService(ILogger<CpuTopologyService> logger, ICpuTopologyProvider? topologyProvider = null)
|
||||
{
|
||||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
this.topologyProvider = topologyProvider;
|
||||
}
|
||||
|
||||
public async Task<CpuTopologyModel> DetectTopologyAsync()
|
||||
@@ -290,7 +292,7 @@ namespace ThreadPilot.Services
|
||||
}
|
||||
|
||||
this.ApplyHyperThreadingFromPhysicalMapping(topology);
|
||||
this.ApplyCoreTypeFromEfficiencyClass(topology, discovered);
|
||||
ApplyCoreTypeFromEfficiencyClass(topology, discovered);
|
||||
|
||||
this.logger.LogInformation(
|
||||
"Detected CPU topology via GetLogicalProcessorInformationEx: {LogicalCpuCount} logical CPUs, {PhysicalCpuCount} physical CPUs",
|
||||
@@ -345,13 +347,13 @@ namespace ThreadPilot.Services
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyCoreTypeFromEfficiencyClass(
|
||||
internal static void ApplyCoreTypeFromEfficiencyClass(
|
||||
CpuTopologyModel topology,
|
||||
List<(int PhysicalCpuId, int LogicalCpuId, byte EfficiencyClass)> discovered)
|
||||
{
|
||||
var byPhysical = discovered
|
||||
.GroupBy(d => d.PhysicalCpuId)
|
||||
.Select(g => new { PhysicalCpuId = g.Key, EfficiencyClass = g.Min(x => x.EfficiencyClass) })
|
||||
.Select(g => new { PhysicalCpuId = g.Key, EfficiencyClass = g.Max(x => x.EfficiencyClass) })
|
||||
.ToList();
|
||||
|
||||
var classes = byPhysical
|
||||
@@ -365,7 +367,8 @@ namespace ThreadPilot.Services
|
||||
return;
|
||||
}
|
||||
|
||||
byte performanceClass = classes.Min();
|
||||
// A higher EfficiencyClass is the more performant core (PROCESSOR_RELATIONSHIP docs).
|
||||
byte performanceClass = classes.Max();
|
||||
foreach (var logicalCpu in topology.LogicalCores)
|
||||
{
|
||||
byte classValue = byPhysical.First(x => x.PhysicalCpuId == logicalCpu.PhysicalCoreId).EfficiencyClass;
|
||||
@@ -525,6 +528,12 @@ namespace ThreadPilot.Services
|
||||
{
|
||||
if (topology.CpuBrand.Contains("AMD", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (await this.TryAssignCcdsFromL3GroupsAsync(topology))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback estimate, display only: Game Boost never uses it (it reads CpuTopologySnapshot).
|
||||
// AMD CCD detection - improved heuristic
|
||||
// Only assign CCD if we actually have multiple CCDs
|
||||
var totalPhysicalCores = topology.TotalPhysicalCores;
|
||||
@@ -562,6 +571,48 @@ namespace ThreadPilot.Services
|
||||
}
|
||||
}
|
||||
|
||||
// Each Zen CCD (CCX on Zen 2) has its own L3, so the real L3 groups replace the 8-cores-per-CCD guess,
|
||||
// which split 6+6 parts such as the 7900X3D into 8+4.
|
||||
internal async Task<bool> TryAssignCcdsFromL3GroupsAsync(CpuTopologyModel topology)
|
||||
{
|
||||
if (this.topologyProvider == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var snapshot = await this.topologyProvider.GetTopologySnapshotAsync();
|
||||
var l3ByLogicalCore = new Dictionary<int, int>();
|
||||
foreach (var processor in snapshot.LogicalProcessors)
|
||||
{
|
||||
if (snapshot.TryGetLastLevelCacheIndex(processor, out var l3Index))
|
||||
{
|
||||
l3ByLogicalCore[processor.GlobalIndex] = l3Index;
|
||||
}
|
||||
}
|
||||
|
||||
if (topology.LogicalCores.Count == 0 || topology.LogicalCores.Any(core => !l3ByLogicalCore.ContainsKey(core.LogicalCoreId)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Number the groups by their lowest CPU, so CCD 0 is always the one holding CPU 0.
|
||||
var ccdByL3 = topology.LogicalCores
|
||||
.GroupBy(core => l3ByLogicalCore[core.LogicalCoreId])
|
||||
.OrderBy(group => group.Min(core => core.LogicalCoreId))
|
||||
.Select((group, ccdId) => (group.Key, ccdId))
|
||||
.ToDictionary(item => item.Key, item => item.ccdId);
|
||||
var hasMultipleL3Groups = ccdByL3.Count > 1;
|
||||
|
||||
foreach (var core in topology.LogicalCores)
|
||||
{
|
||||
core.CcdId = hasMultipleL3Groups ? ccdByL3[l3ByLogicalCore[core.LogicalCoreId]] : null;
|
||||
core.CoreType = CpuCoreType.Zen3; // Default assumption, as in the estimate below
|
||||
}
|
||||
|
||||
this.logger.LogInformation("Detected AMD L3 groups from Windows topology: {L3GroupCount}", ccdByL3.Count);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void DetectHyperThreading(CpuTopologyModel topology)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -81,9 +81,13 @@ namespace ThreadPilot.Services
|
||||
return ReadProcessorsFromGroupMasks(relationshipPtr, ProcessorGroupMaskOffset, groupCount).ToList();
|
||||
}
|
||||
|
||||
public static bool TryReadL3CacheProcessors(IntPtr relationshipPtr, out IReadOnlyList<ProcessorRef> processors)
|
||||
public static bool TryReadL3CacheProcessors(IntPtr relationshipPtr, out IReadOnlyList<ProcessorRef> processors) =>
|
||||
TryReadL3CacheProcessors(relationshipPtr, out processors, out _);
|
||||
|
||||
public static bool TryReadL3CacheProcessors(IntPtr relationshipPtr, out IReadOnlyList<ProcessorRef> processors, out uint cacheSizeBytes)
|
||||
{
|
||||
var cache = Marshal.PtrToStructure<CacheRelationship>(relationshipPtr);
|
||||
cacheSizeBytes = cache.CacheSize;
|
||||
if (cache.Level != 3 || cache.GroupCount == 0)
|
||||
{
|
||||
processors = [];
|
||||
|
||||
@@ -91,6 +91,7 @@ namespace ThreadPilot.Services
|
||||
var coreIndexes = new Dictionary<ProcessorRef, int>();
|
||||
var numaNodeIndexes = new Dictionary<ProcessorRef, int>();
|
||||
var lastLevelCacheIndexes = new Dictionary<ProcessorRef, int>();
|
||||
var lastLevelCacheSizes = new Dictionary<int, ulong>();
|
||||
var packageIndexes = new Dictionary<ProcessorRef, int>();
|
||||
var smtSiblingGlobalIndexes = new Dictionary<ProcessorRef, IReadOnlyList<int>>();
|
||||
var reservedLogicalProcessorIndexes = new HashSet<int>();
|
||||
@@ -111,6 +112,7 @@ namespace ThreadPilot.Services
|
||||
coreIndexes,
|
||||
numaNodeIndexes,
|
||||
lastLevelCacheIndexes,
|
||||
lastLevelCacheSizes,
|
||||
packageIndexes,
|
||||
smtSiblingGlobalIndexes,
|
||||
cancellationToken);
|
||||
@@ -159,7 +161,8 @@ namespace ThreadPilot.Services
|
||||
lastLevelCacheIndexes,
|
||||
packageIndexes,
|
||||
smtSiblingGlobalIndexes,
|
||||
reservedLogicalProcessorIndexes);
|
||||
reservedLogicalProcessorIndexes,
|
||||
lastLevelCacheSizes);
|
||||
}
|
||||
|
||||
private void ReadCpuSetInformation(
|
||||
@@ -236,6 +239,7 @@ namespace ThreadPilot.Services
|
||||
IDictionary<ProcessorRef, int> coreIndexes,
|
||||
IDictionary<ProcessorRef, int> numaNodeIndexes,
|
||||
IDictionary<ProcessorRef, int> lastLevelCacheIndexes,
|
||||
IDictionary<int, ulong> lastLevelCacheSizes,
|
||||
IDictionary<ProcessorRef, int> packageIndexes,
|
||||
IDictionary<ProcessorRef, IReadOnlyList<int>> smtSiblingGlobalIndexes,
|
||||
CancellationToken cancellationToken)
|
||||
@@ -296,8 +300,9 @@ namespace ThreadPilot.Services
|
||||
packageIndexes);
|
||||
break;
|
||||
case LogicalProcessorRelationship.RelationCache:
|
||||
if (TryReadL3CacheProcessors(itemPtr, out var cacheProcessors))
|
||||
if (TryReadL3CacheProcessors(itemPtr, out var cacheProcessors, out var cacheSizeBytes))
|
||||
{
|
||||
lastLevelCacheSizes[lastLevelCacheIndex] = cacheSizeBytes;
|
||||
foreach (var processor in cacheProcessors)
|
||||
{
|
||||
logicalProcessors.Add(processor);
|
||||
@@ -365,9 +370,9 @@ namespace ThreadPilot.Services
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryReadL3CacheProcessors(IntPtr itemPtr, out IReadOnlyList<ProcessorRef> processors)
|
||||
private static bool TryReadL3CacheProcessors(IntPtr itemPtr, out IReadOnlyList<ProcessorRef> processors, out uint cacheSizeBytes)
|
||||
{
|
||||
return WindowsCpuTopologyNativeLayout.TryReadL3CacheProcessors(IntPtr.Add(itemPtr, 8), out processors);
|
||||
return WindowsCpuTopologyNativeLayout.TryReadL3CacheProcessors(IntPtr.Add(itemPtr, 8), out processors, out cacheSizeBytes);
|
||||
}
|
||||
|
||||
private void ReadNumaNodeRelationship(
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
namespace ThreadPilot.Core.Tests
|
||||
{
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using ThreadPilot.Models;
|
||||
using ThreadPilot.Services;
|
||||
|
||||
public sealed class CpuTopologyServiceTopologyFixTests
|
||||
{
|
||||
[Fact]
|
||||
public void EfficiencyClass_HigherClassIsThePerformanceCore()
|
||||
{
|
||||
var topology = new CpuTopologyModel();
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
topology.LogicalCores.Add(new CpuCoreModel { LogicalCoreId = i, PhysicalCoreId = i });
|
||||
}
|
||||
|
||||
CpuTopologyService.ApplyCoreTypeFromEfficiencyClass(
|
||||
topology,
|
||||
[(0, 0, (byte)1), (1, 1, (byte)1), (2, 2, (byte)0), (3, 3, (byte)0)]);
|
||||
|
||||
Assert.Equal(
|
||||
[CpuCoreType.PerformanceCore, CpuCoreType.PerformanceCore, CpuCoreType.EfficiencyCore, CpuCoreType.EfficiencyCore],
|
||||
topology.LogicalCores.Select(core => core.CoreType));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AmdCcds_ComeFromRealL3Groups_SixPlusSix()
|
||||
{
|
||||
var snapshot = TopologyFixtures.Amd([6, 6], cacheSizes: null);
|
||||
var service = new CpuTopologyService(NullLogger<CpuTopologyService>.Instance, new FixedTopologyProvider(snapshot));
|
||||
var topology = CreateLegacyTopology(24);
|
||||
|
||||
Assert.True(await service.TryAssignCcdsFromL3GroupsAsync(topology));
|
||||
|
||||
Assert.Equal(12, topology.LogicalCores.Count(core => core.CcdId == 0));
|
||||
Assert.Equal(12, topology.LogicalCores.Count(core => core.CcdId == 1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AmdCcds_SingleL3_HasNoCcdIds()
|
||||
{
|
||||
var snapshot = TopologyFixtures.Amd([8], cacheSizes: null);
|
||||
var service = new CpuTopologyService(NullLogger<CpuTopologyService>.Instance, new FixedTopologyProvider(snapshot));
|
||||
var topology = CreateLegacyTopology(16);
|
||||
|
||||
Assert.True(await service.TryAssignCcdsFromL3GroupsAsync(topology));
|
||||
|
||||
Assert.All(topology.LogicalCores, core => Assert.Null(core.CcdId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AmdCcds_WithoutL3Data_FallsBackToEstimate()
|
||||
{
|
||||
var snapshot = TopologyFixtures.Amd([8, 8], cacheSizes: null, withL3Indexes: false);
|
||||
var service = new CpuTopologyService(NullLogger<CpuTopologyService>.Instance, new FixedTopologyProvider(snapshot));
|
||||
|
||||
Assert.False(await service.TryAssignCcdsFromL3GroupsAsync(CreateLegacyTopology(32)));
|
||||
}
|
||||
|
||||
private static CpuTopologyModel CreateLegacyTopology(int logicalCount)
|
||||
{
|
||||
var topology = new CpuTopologyModel { CpuBrand = "AMD Ryzen" };
|
||||
for (var i = 0; i < logicalCount; i++)
|
||||
{
|
||||
topology.LogicalCores.Add(new CpuCoreModel { LogicalCoreId = i, PhysicalCoreId = i / 2 });
|
||||
}
|
||||
|
||||
return topology;
|
||||
}
|
||||
|
||||
private sealed class FixedTopologyProvider(CpuTopologySnapshot snapshot) : ICpuTopologyProvider
|
||||
{
|
||||
public Task<CpuTopologySnapshot> GetTopologySnapshotAsync(CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult(snapshot);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
namespace ThreadPilot.Core.Tests
|
||||
{
|
||||
using ThreadPilot.Models;
|
||||
|
||||
internal static class TopologyFixtures
|
||||
{
|
||||
/// <summary>AMD with SMT: each entry of coresPerL3 is one L3 group (CCD), two logical CPUs per core.</summary>
|
||||
public static CpuTopologySnapshot Amd(
|
||||
int[] coresPerL3,
|
||||
ulong[]? cacheSizes,
|
||||
bool withL3Indexes = true,
|
||||
bool withCpuSetIds = true,
|
||||
int[]? reserved = null)
|
||||
{
|
||||
var processors = new List<ProcessorRef>();
|
||||
var l3Indexes = new Dictionary<ProcessorRef, int>();
|
||||
var coreIndexes = new Dictionary<ProcessorRef, int>();
|
||||
var core = 0;
|
||||
for (var l3 = 0; l3 < coresPerL3.Length; l3++)
|
||||
{
|
||||
for (var c = 0; c < coresPerL3[l3]; c++, core++)
|
||||
{
|
||||
for (var thread = 0; thread < 2; thread++)
|
||||
{
|
||||
var index = (core * 2) + thread;
|
||||
var processor = new ProcessorRef(0, (byte)index, index);
|
||||
processors.Add(processor);
|
||||
coreIndexes[processor] = core;
|
||||
l3Indexes[processor] = l3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return CpuTopologySnapshot.Create(
|
||||
processors,
|
||||
cpuSetIds: withCpuSetIds ? processors.ToDictionary(p => p, p => (uint)(256 + p.GlobalIndex)) : null,
|
||||
coreIndexes: coreIndexes,
|
||||
lastLevelCacheIndexes: withL3Indexes ? l3Indexes : null,
|
||||
reservedLogicalProcessorIndexes: reserved,
|
||||
lastLevelCacheSizes: cacheSizes?.Select((size, index) => (size, index)).ToDictionary(x => x.index, x => x.size));
|
||||
}
|
||||
|
||||
/// <summary>Intel hybrid: P-cores with HT first (EfficiencyClass 1), then single-thread E-cores (class 0).</summary>
|
||||
public static CpuTopologySnapshot IntelHybrid(int performanceCores, int efficiencyCores, bool withL3Indexes = true)
|
||||
{
|
||||
var processors = new List<ProcessorRef>();
|
||||
var classes = new Dictionary<ProcessorRef, byte>();
|
||||
var coreIndexes = new Dictionary<ProcessorRef, int>();
|
||||
var index = 0;
|
||||
for (var core = 0; core < performanceCores + efficiencyCores; core++)
|
||||
{
|
||||
var isPerformance = core < performanceCores;
|
||||
for (var thread = 0; thread < (isPerformance ? 2 : 1); thread++, index++)
|
||||
{
|
||||
var processor = new ProcessorRef(0, (byte)index, index);
|
||||
processors.Add(processor);
|
||||
classes[processor] = isPerformance ? (byte)1 : (byte)0;
|
||||
coreIndexes[processor] = core;
|
||||
}
|
||||
}
|
||||
|
||||
return CpuTopologySnapshot.Create(
|
||||
processors,
|
||||
cpuSetIds: processors.ToDictionary(p => p, p => (uint)(256 + p.GlobalIndex)),
|
||||
efficiencyClasses: classes,
|
||||
coreIndexes: coreIndexes,
|
||||
lastLevelCacheIndexes: withL3Indexes ? processors.ToDictionary(p => p, _ => 0) : null);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user