[英]How to get CPU usage for more than 2 cores?
我嘗試將我的程序 CPU 使用率除以一個內核。 現在我使用 PerformanceCounter 並在 0 和 1 之間更改 InstanceName 我有來自 2 個內核的數據。
PerformanceCounter pc0 = new PerformanceCounter("Processor", "% Processor Time", "0");
PerformanceCounter pc1 = new PerformanceCounter("Processor", "% Processor Time", "1");
我如何獲得第 3、第 4 個核心等的核心使用量?
有沒有人可以幫助我?
謝謝
我懷疑您真正要問的是“ 我如何計算內核數?”。 此代碼將計算內核數,然后基於此創建性能計數器。
int coreCount = 0;
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
{
coreCount += int.Parse(item["NumberOfCores"].ToString());
}
PerformanceCounter[] pc = new PerformanceCounter[coreCount];
for (int i = 0; i < coreCount; i++)
{
pc[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
Console.WriteLine(pc[i].CounterName);
}
這可能是一個老問題,但對於其他尋求不同解決方案的人來說,為什么不使用 System.Environment?
public static List<System.Diagnostics.PerformanceCounter> GetPerformanceCounters()
{
List<System.Diagnostics.PerformanceCounter> performanceCounters = new List<System.Diagnostics.PerformanceCounter>();
int procCount = System.Environment.ProcessorCount;
for (int i = 0; i < procCount; i++)
{
System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", i.ToString());
performanceCounters.Add(pc);
}
return performanceCounters;
}
編輯:我注意到這僅返回邏輯處理器的數量,而不是實際的核心數。
我以前沒有使用過 PerformanceCounter,但這樣做有什么問題嗎?
PerformanceCounter pc0 = new PerformanceCounter("Processor", "% Processor Time", "0");
PerformanceCounter pc1 = new PerformanceCounter("Processor", "% Processor Time", "1");
PerformanceCounter pc2 = new PerformanceCounter("Processor", "% Processor Time", "2");
PerformanceCounter pc3 = new PerformanceCounter("Processor", "% Processor Time", "3");
像這樣的事情也應該滿足您的要求
public List<string> GetServerStatus()
{
List<string> cpuStatus = new List<string>();
ObjectQuery wmicpus = new WqlObjectQuery("SELECT * FROM Win32_Processor");
ManagementObjectSearcher cpus = new ManagementObjectSearcher(wmicpus);
try
{
int coreCount = 0;
int totusage = 0;
foreach (ManagementObject cpu in cpus.Get())
{
//cpuStatus.Add(cpu["DeviceId"] + " = " + cpu["LoadPercentage"]);
coreCount += 1;
totusage += Convert.ToInt32(cpu["LoadPercentage"]);
}
if (coreCount > 1)
{
double ActUtiFloat = totusage / coreCount;
int ActUti = Convert.ToInt32(Math.Round(ActUtiFloat));
//Utilisation = ActUti + "%";
cpuStatus.Add("CPU = " + ActUti);
}
else
{
cpuStatus.Add("CPU = " + totusage);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
cpus.Dispose();
}
return cpuStatus;
}
foreach (var item in new System.Management.ManagementObjectSearcher("Select NumberOfLogicalProcessors from Win32_Processor").Get())
coreCount += int.Parse(item["NumberOfLogicalProcessors"].ToString());
PerformanceCounter[] pc = new PerformanceCounter[coreCount];
for (int i = 0; i < coreCount; i++)
pc[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.