using System; using System.Collections.Generic; using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; namespace ThreadPilot.Models { /// /// Represents a logical CPU core with topology information /// public partial class CpuCoreModel : ObservableObject { public int LogicalCoreId { get; set; } public int PhysicalCoreId { get; set; } public int SocketId { get; set; } public int? CcdId { get; set; } // Core Complex Die (AMD) public int? ClusterId { get; set; } // Intel Cluster public CpuCoreType CoreType { get; set; } = CpuCoreType.Unknown; public bool IsHyperThreaded { get; set; } public int? HyperThreadSibling { get; set; } public string Label { get; set; } = string.Empty; [ObservableProperty] private bool isEnabled = true; [ObservableProperty] private bool isSelected = false; /// /// Gets the affinity mask bit for this logical core /// public long AffinityMask => 1L << LogicalCoreId; } /// /// Types of CPU cores /// public enum CpuCoreType { Unknown, Standard, PerformanceCore, // Intel P-cores EfficiencyCore, // Intel E-cores Zen, // AMD Zen cores ZenPlus, // AMD Zen+ cores Zen2, // AMD Zen2 cores Zen3, // AMD Zen3 cores Zen4 // AMD Zen4 cores } /// /// Represents CPU topology information /// public class CpuTopologyModel { public List LogicalCores { get; set; } = new(); public int TotalLogicalCores => LogicalCores.Count; public int TotalPhysicalCores => LogicalCores.GroupBy(c => c.PhysicalCoreId).Count(); public int TotalSockets => LogicalCores.GroupBy(c => c.SocketId).Count(); public int SocketCount => TotalSockets; // Alias for TotalSockets public bool HasHyperThreading => LogicalCores.Any(c => c.IsHyperThreaded); public bool HasSmt => HasHyperThreading; // SMT is AMD's term for HyperThreading public bool HasIntelHybrid => LogicalCores.Any(c => c.CoreType == CpuCoreType.PerformanceCore || c.CoreType == CpuCoreType.EfficiencyCore); public bool HasHybridArchitecture => HasIntelHybrid; // Alias for HasIntelHybrid public bool HasAmdCcd => LogicalCores.Any(c => c.CcdId.HasValue); public int CcdCount => LogicalCores.Where(c => c.CcdId.HasValue).Select(c => c.CcdId!.Value).Distinct().Count(); public string Architecture => CpuArchitecture; // Alias for CpuArchitecture public string CpuArchitecture { get; set; } = "Unknown"; public string CpuBrand { get; set; } = "Unknown"; public bool TopologyDetectionSuccessful { get; set; } = false; /// /// Gets all CCDs (Core Complex Dies) available /// public IEnumerable AvailableCcds => LogicalCores .Where(c => c.CcdId.HasValue) .Select(c => c.CcdId!.Value) .Distinct() .OrderBy(id => id); /// /// Gets all performance cores (Intel P-cores) /// public IEnumerable PerformanceCores => LogicalCores .Where(c => c.CoreType == CpuCoreType.PerformanceCore); /// /// Gets all efficiency cores (Intel E-cores) /// public IEnumerable EfficiencyCores => LogicalCores .Where(c => c.CoreType == CpuCoreType.EfficiencyCore); /// /// Gets all physical cores (one logical core per physical core, excluding HT siblings) /// public IEnumerable PhysicalCores => LogicalCores .GroupBy(c => c.PhysicalCoreId) .Select(g => g.OrderBy(c => c.LogicalCoreId).First()); /// /// Gets cores by CCD ID /// public IEnumerable GetCoresByCcd(int ccdId) => LogicalCores .Where(c => c.CcdId == ccdId); /// /// Gets cores by socket ID /// public IEnumerable GetCoresBySocket(int socketId) => LogicalCores .Where(c => c.SocketId == socketId); /// /// Calculates affinity mask for selected cores /// public long CalculateAffinityMask(IEnumerable cores) { return cores.Aggregate(0L, (mask, core) => mask | core.AffinityMask); } /// /// Gets affinity mask for all physical cores (excluding HT siblings) /// public long GetPhysicalCoresAffinityMask() => CalculateAffinityMask(PhysicalCores); /// /// Gets affinity mask for performance cores /// public long GetPerformanceCoresAffinityMask() => CalculateAffinityMask(PerformanceCores); /// /// Gets affinity mask for efficiency cores /// public long GetEfficiencyCoresAffinityMask() => CalculateAffinityMask(EfficiencyCores); /// /// Gets affinity mask for a specific CCD /// public long GetCcdAffinityMask(int ccdId) => CalculateAffinityMask(GetCoresByCcd(ccdId)); } /// /// Quick selection preset for CPU affinity /// public class CpuAffinityPreset { public string Name { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; public long AffinityMask { get; set; } public bool IsAvailable { get; set; } = true; public string UnavailableReason { get; set; } = string.Empty; } }