【问题标题】:How to get CPU usage for more than 2 cores?如何获得超过 2 个内核的 CPU 使用率?
【发布时间】:2011-04-04 10:35:34
【问题描述】:

我尝试在我的程序中计算 CPU 使用率除以核心。现在我使用 PerformanceCounter 并在 0 和 1 之间更改 InstanceName 我有来自 2 个核心的数据。

PerformanceCounter pc0 = new PerformanceCounter("Processor", "% Processor Time", "0");
PerformanceCounter pc1 = new PerformanceCounter("Processor", "% Processor Time", "1");

如何获得第三、第四核心等的核心使用情况?

有人可以帮助我吗?

谢谢

【问题讨论】:

标签: c# cpu-usage performancecounter


【解决方案1】:

我怀疑您真正要问的是“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 并想读取逻辑处理器使用情况怎么办?
  • 查看另一个 StackOverflow 问题 (stackoverflow.com/questions/1542213/…) 的已接受答案。这提供了您可能想要阅读的所有不同内容的详细信息。
  • 我以前读过这个。我有关于逻辑 cpu 数量等的数据,但我需要读取逻辑 cpu 的 ProcessorTime。我不确定是否可以使用 PerformanceCounter 来做到这一点
  • 嗯。我认为处理器类别实际上是指逻辑处理器,困难在于生成每个核心处理器的使用情况。你需要做一些事情,比如识别哪个处理器对在单个内核上运行,然后取它们的平均使用量。我怀疑它比这更复杂......
  • NumberOfLogicalProcessors 是对的 NumberOfCore 返回核心物理核心,在我的机器(i7)中 numberOfCore = 4 但 NumberOfLogicalProcessors = 8 ;反正tnx
【解决方案2】:

我之前没有使用过 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 个内核,那么您的代码就可以了。
  • 正是我尝试在具有 2 个内核的机器上执行此操作,并收到有关“实例名称 2 不存在”的错误,所以我认为它不起作用。现在我使用 try-catch 异常并且在两种架构上都可以正常工作。谢谢
  • @Spychu 我的回答将防止你不得不捕获一个真正不应该是异常情况的异常......
  • this link 中找到核心数并循环!
【解决方案3】:

这可能是一个老问题,但对于寻找不同解决方案的其他人来说,为什么不使用 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;
}

编辑:我注意到这只会返回逻辑处理器的数量,而不是实际的核心数。

【讨论】:

  • 即使没有核心数量仍然是一个不错的答案。
【解决方案4】:

这样的事情也应该满足您的要求

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;
}

【讨论】:

    【解决方案5】:
    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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      相关资源
      最近更新 更多