如何获得超过2个核心的CPU使用率?
我试图让我的程序CPU使用率除以核心。现在我使用PerformanceCounter 并更改0和1之间的InstanceName我有来自2个核心的数据。如何获得超过2个核心的CPU使用率?
PerformanceCounter pc0 = new PerformanceCounter("Processor", "% Processor Time", "0");
PerformanceCounter pc1 = new PerformanceCounter("Processor", "% Processor Time", "1");
我如何获得第三,第四个核心等的核心使用?
有人能帮助我吗?
感谢
我还没有使用的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");
如果核心实际上不存在,你会得到一个错误。只要您尝试从计数器中取出一个值(例如'pc4.RawValue'),就会发生错误。但是,如果你知道至少有4个内核,那么你的代码将会很好。 – 2011-04-04 10:54:12
ahh当然:p – 2011-04-04 11:05:54
确切地说,我试图在2核心的机器上做这个,并且出现'instanceName 2不存在'的错误,所以我认为它不起作用。 现在我使用try-catch异常并且在两种架构上都能正常工作。 谢谢 – spychu 2011-04-04 11:38:14
我怀疑你真正要问的是“How do I count the number of cores?”。此代码将计算内核的数量,然后根据此数据创建性能计数器。
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);
}
如果我们支持HTT并希望读取逻辑处理器的使用情况,那么情况如何? – spychu 2011-04-05 08:20:35
请参阅其他*问题上的接受答案(http://*.com/questions/1542213/how-to-find-the-number-of-cpu-cores-via-net-c)。这给出了您可能想要阅读的所有不同内容的详细信息。 – 2011-04-05 08:21:58
我以前读过这个。我有关于逻辑CPU数量等数据,但我需要读取逻辑CPU的ProcessorTime。 我不确定我是否可以使用PerformanceCounter – spychu 2011-04-05 09:11:25
一些像这样的事情也应努力为您的要求
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;
}
这可能是一个老问题,但对于其他人寻找不同的解决方案,你为什么不使用统环境?
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;
}
编辑:我注意到这只是返回逻辑处理器的数量,而不是实际的核心数。
即使没有核心数量,仍然是一个很好的答案。 – 2012-10-02 14:09:46
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());
}
你试过''新的PerformanceCounter(“Processor”,“%Processor Time”,“2”);'? – Tokk 2011-04-04 10:42:48
另请参阅[SO:如何获得每个核心的CPU负载](http://*.com/questions/2938629/how-can-i-get-cpu-load-per-core-in-c) – 2011-04-04 10:49:57