性能计数器当前进程的CPU使用率超过100

时间:2012-02-29 15:14:08

标签: c# process cpu-usage performancecounter

我想显示多线程应用程序的CPU使用率(在多核处理器上工作)。我想收到任务经理附近的数字。但我得到的数字超过100%。甚至超过500%。是的,我知道,对于类别“处理器”“%Processor Time”,我需要分为 Environment.ProcessorCount “NumberOfLogicalProcessors”(我的配置相同)。 此操作后,结果为500%。我在具有不同硬件(i7,i5,Core2)和软件配置(带有所有更新的Windows 7 SP1,带有所有更新的Windows 2008 R2 SP1)的不同计算机上测试了此示例,并遇到了同样的问题。

public static class SystemInfo
{
    private static Process _thisProc;
    private static bool HasData = false;
    private static PerformanceCounter _processTimeCounter;

    private static void Init()
    {
        if (HasData)
            return;

        if (CheckForPerformanceCounterCategoryExist("Process"))
        {
            _processTimeCounter = new PerformanceCounter();
            _processTimeCounter.CategoryName = "Process";
            _processTimeCounter.CounterName = "% Processor Time";
            _processTimeCounter.InstanceName = FindInstanceName("Process");
            _processTimeCounter.NextValue();
        }

        MaximumCpuUsageForCurrentProcess = 0;
        HasData = true;
    }

    private static bool CheckForPerformanceCounterCategoryExist(string categoryName)
    {
        return PerformanceCounterCategory.Exists(categoryName);
    }

    public static string FindInstanceName(string categoryName)
    {
        string result = String.Empty;
        _thisProc = Process.GetCurrentProcess();

        if (!ReferenceEquals(_thisProc, null))
        {
            if (!String.IsNullOrEmpty(categoryName))
            {
                if (CheckForPerformanceCounterCategoryExist(categoryName))
                {
                    PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName);
                    string[] instances = category.GetInstanceNames();
                    string processName = _thisProc.ProcessName;

                    if (instances != null)
                    {
                        foreach (string instance in instances)
                        {
                            if (instance.ToLower().Equals(processName.ToLower()))
                            {
                                result = instance;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    public static int CpuUsageForCurrentProcess
    {
        get
        {
            Init();

            if (!ReferenceEquals(_processTimeCounter, null))
            {
                int result = (int) _processTimeCounter.NextValue();
                result /= Environment.ProcessorCount; //NumberOfLogicalProcessors //same for me

                if (MaximumCpuUsageForCurrentProcess < result)
                    MaximumCpuUsageForCurrentProcess = result;

                return result;
            }
            return 0;
        }
    }

    public static int MaximumCpuUsageForCurrentProcess { private set; get; }
}

和要执行的代码(您需要创建带有两个标签的Windows窗体应用程序,一个BackgroundWorker和一个按钮)

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        IList<Task> tasks = new List<Task>();
        for (int i = 0; i < 10; i++)
        {
            Task t = new Task(() =>
            {
                do {
                    if (backgroundWorker1.CancellationPending)
                        break;
                } while (true);
            });
            t.Start();
            tasks.Add(t);
        }

        Task displayProgress = new Task(() => { do {
                                                    if (backgroundWorker1.CancellationPending)
                                                        break;
                                                    backgroundWorker1.ReportProgress(1);
                                                    Thread.Sleep(10);
                                                } while (true); });
        displayProgress.Start();
        tasks.Add(displayProgress);

        Task.WaitAll(tasks.ToArray());
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Text = SystemInfo.CpuUsageForCurrentProcess.ToString();
        label2.Text = SystemInfo.MaximumCpuUsageForCurrentProcess.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = SystemInfo.CpuUsageForCurrentProcess.ToString();

        if (backgroundWorker1.IsBusy)
            backgroundWorker1.CancelAsync();
        else
            backgroundWorker1.RunWorkerAsync();
    }

请告诉我我的错误。是的,我读了this article并注意到了

  

“\ Process(...)\%Processor Time”可以达到N * 100(其中N是CPU的数量),因为它会在所有CPU中累加所请求进程的CPU使用率。

1 个答案:

答案 0 :(得分:5)

This(有点相关)问题建议使用System.Diagnostics.Process.TotalProcessorTimeSystem.Diagnostics.ProcessThread.TotalProcessorTime属性,以降低开销并轻松实现。

(编辑:Here's an article解释如何使用这些属性。)

此外,看起来你在“_processTimeCounter.NextValue()”调用之间没有等待足够长的时间。根据{{​​3}},您应该等待至少1秒钟。不确定这是否会导致您的奇怪数字。