Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance counter CPU usage for current process is more than 100

I want to display CPU usage for my multithread application (working over multicore processor). I want to receive numbers close to Task manager's. But I got numbers more than 100%. Even more than 500%. Yes, I know, than counter "% Processor Time" for category "Process" I need to divide into Environment.ProcessorCount or "NumberOfLogicalProcessors" (same for my configuration). And 500% is a result after this operation. I tested this example on different computers with different hardware (i7, i5, Core2) and software configurations (Windows 7 SP1 with all updates, Windows 2008 R2 SP1 with all updates) and got same problem.

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

and code to execute (you need to create windows forms application with two labeles, one BackgroundWorker and one button)

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

Please show me my error. And yes, I read this article and noticed that

“\Process(…)\% Processor Time” can go up to N*100 (where N is the number of CPUs) because it adds up the CPU usage of the requested process across all the CPUs.

like image 668
user809808 Avatar asked Feb 29 '12 15:02

user809808


People also ask

Can CPU utilization be more than 100?

CPUs are designed to run safely at 100% CPU utilization. However, you'll want to avoid these situations whenever they cause perceptible slowness in games.

Why my CPU usage is suddenly 100?

If a process is still using too much CPU, try updating your drivers. Drivers are programs that control particular devices connected to your motherboard. Updating your drivers may eliminate compatibility issues or bugs that cause increased CPU usage. Open the Start menu, then Settings.

What are CPU performance counters?

In computers, hardware performance counters (HPC), or hardware counters are a set of special-purpose registers built into modern microprocessors to store the counts of hardware-related activities within computer systems. Advanced users often rely on those counters to conduct low-level performance analysis or tuning.

Is 90% CPU usage normal?

If your CPU use temporarily spikes to 90% or 100%, that's normal if you're doing intensive tasks like high-end gaming or graphic design. So long as your CPU calms down after you're done, there's nothing to worry about.


1 Answers

This (somewhat related) question suggests using the System.Diagnostics.Process.TotalProcessorTime and System.Diagnostics.ProcessThread.TotalProcessorTime properties instead, for low overhead and easy implementation.

(Edit: Here's an article explaining how to use the properties, as well.)

Also, it looks like you're not waiting long enough between calls to "_processTimeCounter.NextValue()." As per the documentation, you're supposed to wait at least 1 second. Not sure if that would cause your strange numbers or not.

like image 145
BTownTKD Avatar answered Oct 06 '22 22:10

BTownTKD