Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting CPU usage of a specific Service in C#

I want to know the CPU usage of a specific service in C#.

PerformanceCounter works fine with process:

PerformanceCounter counter = new PerformanceCounter("Process", "% Processor Time", "myprocess", true);  
double result = counter.NextValue();

but not with services:

   PerformanceCounter counter = new PerformanceCounter("Service", "% Processor Time", "myservice", true);  
    double result = counter.NextValue();
like image 678
flofreelance Avatar asked Sep 12 '15 17:09

flofreelance


People also ask

How do I get CPU usage in C?

To get the % CPU usage, you will need to divide it by the # of logical cores that the OS sees. To get get the CPU % usage you would need ( CPU time / # of cores / wall-clock time elapsed ), but otherwise correct.

How is CPU usage calculated for a program?

CPU time is the amount of time that the process is using the CPU, converting it to percentage is done by dividing it by the real amount of time passed. For example, if CPU time is 1 second of total execution time 2 seconds then he CPU utilization is ½ x 100 = 50%.

What command displays the CPU utilization for individual processes?

The mpstat command provides information about CPU performance and utilization by giving CPU statistics for the whole system and each available processor. Running the mpstat command on a Linux system will display an output like the one shown in figure 2.


1 Answers

The correct name for the performance counter would be

PerformanceCounter counter = new PerformanceCounter("Process", "% Processor Time", "ACService", true);
like image 175
Scott Chamberlain Avatar answered Sep 20 '22 23:09

Scott Chamberlain