我意识到我可以通过引用Environment.ProcessorCount
来获取逻辑处理器的数量,但是有没有办法获取使用.NET core 2.2的计算机上的物理处理器/核心数量?在.NET Framework中,我将使用类似以下的内容,但由于跨操作系统的支持,我知道该功能在内核中不可用。
var physicalCPUs = 0;
List<string> sockets = new List<string>();
System.Management.ManagementClass mc = new System.Management.ManagementClass("Win32_Processor");
System.Management.ManagementObjectCollection moc = mc.GetInstances();
// Iterate through logical processors
foreach (System.Management.ManagementObject mo in moc)
{
string socketDesignation = mo.Properties["SocketDesignation"].Value.ToString();
// We will count the unique SocketDesignations to find the number of physical CPUs in the system.
if (!sockets.Contains(socketDesignation))
sockets.Add(socketDesignation);
}
physicalCPUs = sockets.Count;