Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning CPU usage in WMI using C#

Tags:

c#

wmi

To return the CPU usage by using WMI, do I return the PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor?

If not, which class should I look towards? Thanks.

like image 549
jpavlov Avatar asked Mar 19 '12 20:03

jpavlov


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 do I report CPU usage?

1. Click Activity > CPU Utilization to display the CPU Utilization report page. 2.To view a graph of data points over time, keep the page open. Data points are graphed every ten seconds.

How do I check CPU usage on box?

Check CPU Usage with vmstat Command The vmstat command will display the information about system processes, memory, swap, I/O, and CPU performance. It will display the average details since the last reboot.


1 Answers

It seems like the info is also available in WMI here:

select LoadPercentage from Win32_Processor

  • https://msdn.microsoft.com/en-us/library/aa394373%28v=vs.85%29.aspx

"Load capacity of each processor, averaged to the last second. Processor loading refers to the total computing burden for each processor at one time."

OR:

select LoadPercentage from CIM_Processor

  • https://msdn.microsoft.com/en-us/library/aa387978(v=vs.85).aspx

"Loading of the processor, averaged over the last minute, in a percentage."

OR:

select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor also seems to work.


Note: these often return multiple results per CPU core and have to be summed/averaged to get the total CPU usage for the system as a whole, so look for that.

This question and answer really has more to do with WMI since getting info from WMI with C# is really a different question and should be very similar for any WMI query in C#.

like image 106
jgstew Avatar answered Sep 28 '22 14:09

jgstew