1

[My Attempts]

Already went through

  1. How to get the CPU Usage in C#? But "_Total" Instance of Processor would give me total consumption of CPU as opposed to specifc application or 'process'

  2. In a C# Program, I am trying to get the CPU usage percentage of the application but it always shows 100

  3. What exactly is CPU Time in task manager? , explains it but does not say how to retrive this value.

After referring http://social.technet.microsoft.com/wiki/contents/articles/12984.understanding-processor-processor-time-and-process-processor-time.aspx

I got that

TotalProcessorTimeCounter = new PerformanceCounter("Process", "% Processor Time", processName);

has a baseline of (No.of Logical CPU*100) Basically, this does not give me a scale of 100% over CPU consumed.

Tried digging around task manager and found that Task manger->Processor-> CPU Usage is on a scale of 100.

Processor\% Processor Time object does not take process name as input. it only has '_Total' as an input.

[Question]

How do I get this data(CPU consumption) using performance counters over a scale of 100 for a particular process for a multi-core system?

jjj
  • 1,136
  • 3
  • 18
  • 28
Ronak Agrawal
  • 1,006
  • 1
  • 19
  • 48

3 Answers3

7

After reading this performance counter document https://social.technet.microsoft.com/wiki/contents/articles/12984.understanding-processor-processor-time-and-process-processor-time.aspx I realized that the correct approach to getting this value is actually to take 100 and subtract the idle time of all processors.

% Processor Time is the percentage of elapsed time that the processor spends to execute a non-Idle thread.

var allIdle = new PerformanceCounter(
    "Processor", 
    "% Idle Time", 
    "_Total"
);

int cpu = 100 - allIdle;

This gives a value very close to what Task Manager displays and perhaps is only different at some points due to rounding or the specific time of when the counter is polled.

J. Campbell
  • 680
  • 4
  • 5
7

If you use this, you can get the same result with the task manager :

cpuCounter = new PerformanceCounter(
        "Processor Information",
        "% Processor Utility",
        "_Total",
        true
    );
Seçkin Durgay
  • 2,758
  • 4
  • 27
  • 36
2

This gives me the exact figure you get on Task Manager (in the Details tab), is this what you want?

// Declare the counter somewhere
var process_cpu = new PerformanceCounter(
                                   "Process", 
                                   "% Processor Time", 
                                   Process.GetCurrentProcess().ProcessName
                                        );
// Read periodically
var processUsage = process_cpu.NextValue() / Environment.ProcessorCount;
Jcl
  • 27,696
  • 5
  • 61
  • 92
  • Sorry if I am being naive, Is this how task manager does it? Just curious – Ronak Agrawal Feb 09 '16 at 10:18
  • 1
    @RonakAgrawal I don't really know, I've never seen task manager's source code or tried to dissasemble it. All I can say is that (if rounding to no decimals) I get the same figure from this code than I get on task manager – Jcl Feb 09 '16 at 10:19
  • I've made a simple test project and made a [screenshot](http://s10.postimg.org/vkkcb1q2h/screenshot_26.png) . This is on an 8-core single CPU (i7 860). The `11.99063` is the value I get on `processUsage` – Jcl Feb 09 '16 at 10:23
  • I was trying the same. Seems like task manager is just rounding off the values. Thanks for your time. – Ronak Agrawal Feb 09 '16 at 10:27
  • Of course: if you want the exact `Task Manager` figure then you might need to round (with `Math.Round`, for example), and sync your updating timer to the timer of the task manager... this might prove harder to do :-) – Jcl Feb 09 '16 at 10:28
  • 1
    Here's a possibly irrelevant side note. There are the OS performance counters and the HW performance counters. PerformanceCounter returns the OS version. Sometimes the OS and HW counters are the same, and sometimes not. It all has to do with non-standard hardware implementations and OSs needing to run on different HW architectures. – Taylor Kidd Feb 09 '16 at 14:56