3

I been looking at PerformanceCounter Class for monitoring system performance. I have figured out how to call built in counters. What I'm struggling with is understanding the values that I get back and how compare those values to what I can see in Task Manager.

So far I have successfully been able to monitor available RAM in MB and it correctly corresponded to value in Task Manager.

first I created PerformanceCounter

 myMemoryCounter = new PerformanceCounter("Memory", "Available MBytes", true);

Then I put the following line inside a loop.

Console.WriteLine("Value" + myMemoryCounter.NextSample().RawValue.ToString());

When looking at counters for processor, I'm not able to make connection to % CPU Utilization that can be observed in Task Manager.

Do I need to have multiple counters and compute the value or is there easier way?

Update:

Looking at question What is the correct Performance Counter to get CPU and Memory Usage of a Process? it just describes which counter to use but not how to convert results to percentage?

NextSample().RawValue returns long data type and I need to know how to convert it to percentage.

Using the following code

 var _myCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
 Console.WriteLine("CPU " + _myCPUCounter.NextSample().RawValue.ToString("0.000"));

I get this value 1794607539062 how do I convert that to percentage?

Community
  • 1
  • 1
  • I have updated the question. The problem with duplicate there is nothing about how to get percentage value, only which counter to use. –  Jul 31 '14 at 19:17
  • This counter's result is already a percentage... – BartoszKP Jul 31 '14 at 20:04
  • `NextSample().RawValue` returns LONG data type, for example in my case one value was `1794607539062` how do I convert that to percentage –  Jul 31 '14 at 20:53
  • 1
    But wait, in your code you're using the `Available MBytes` counter - how is that related to CPU usage?? – BartoszKP Jul 31 '14 at 21:04
  • I was first trying to get it to work with `Available MByte` and it returns correct value. The problem is with `"% Processor Time"` that returns some value that I'm not sure how to convert to percentage. –  Jul 31 '14 at 23:38
  • Have you [checked this](http://stackoverflow.com/questions/4679962/what-is-the-correct-performance-counter-to-get-cpu-and-memory-usage-of-a-process)? – Marcel N. Jul 31 '14 at 23:40
  • I already linked in my question. That is where I got the code `("Processor", "% Processor Time", "_Total")` –  Jul 31 '14 at 23:52
  • 3
    You're retrieving the NextSample which gives you the "raw, or uncalculated, value". But a percentage is a calculated value. Why don't your use NextValue() instead of NextSample and let the counter do the calculation for you? – Frank Boyne Aug 01 '14 at 03:33
  • @FrankBoyne thanks, that is what I needed. Is there a formula for calculating that value out of raw value? –  Aug 01 '14 at 12:05

2 Answers2

2
// TRY THIS
public class MyCPU
{
    // This method that will be called when the thread is started
    public void Info()
    {
        /*
         * CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system
         */
        PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
        for (int i64 = 0; i64 < 100; ++i64)
        {
            Console.WriteLine("CPU: " + (cpuCounter.NextValue() / Environment.ProcessorCount) + " %");
        }
    }
};
  • Thanks Andy, your solution was just what I needed. I made a couple minor changes so I could get the average over a few seconds you can see here: https://stackoverflow.com/questions/40949924/nextvalue-returns-0 – Alan Jun 21 '18 at 15:01
0

According to the source, for the CPU counter, which is type Timer100NsInverse, the calculator method returns:

(1f - ((float)(newSample.RawValue - oldSample.RawValue))/(float)(newSample.TimeStamp - oldSample.TimeStamp)) * 100.0f;

Note it needs two samples.

See CounterSampleCalculator.cs which you can find in multiple places on the web. It can show you all the calculations for all the types. But you can just call the Calculate methods, or NextValue() as already described.

Siyual
  • 16,415
  • 8
  • 44
  • 58
Dave
  • 21
  • 2