Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Cpu usage of each process from wmi

I found many sources to get the cpu usage of each process. in general there are many ways to get the cpu usage of process .

  1. percentprocessortime from win32_perfformatteddata_perfproc_process
  2. performancecounter class in system.diagnostics
  3. by manual calculation
  4. Process class (by process.getcurrentprocess().totalprocessortime;) as said in here.

FirstWay:

For the remote process monitoring(my scenario is remote monitoring), the percentprocessortime always shows value 0 to 100+. this 100+ happens because of multiple processors in a system. it can be calculated by using percentprocessortime/ processorcount.

Question in firstway:

i can read the percentprocessortime in wmi explorer, it shows all the values are 0 or 100 only not other than this value. is this value is correct? or is it useful for monitoring the value?

Second Way:

for PerformanceCounter class monitoring, it can be done for local only. so i cannot use this. is it possible to use this for remote?

Third Way:

(biggest confusion happening here in terms of which formula to use.) this calculation is made either by a PerformanceCounter class or win32_process class from wmi. some says to calculate the performance counter by using the follwing

consider single CPU and

(processor\%processor time) = 10%

(processor\%user time) = 8%

(processor\% privilege time) = 2%

(process\% processor time\your application) = 80%

You application is using 80% of the (processor\% user time) which is (8*.8)=6.4% of the CPU.

for more refer here.

by calculating the usermodetime and kernelmodetime from win32_process by using the following formulae

DateTime firstSample, secondSample;
firstSample = DateTime.Now;
queryObj.Get();
//get cpu usage
ulong u_oldCPU = (ulong)queryObj.Properties["UserModeTime"].Value 
                +(ulong)queryObj.Properties["KernelModeTime"].Value;
//sleep to create interval
System.Threading.Thread.Sleep(1000);
//refresh object
secondSample = DateTime.Now;
queryObj.Get();
//get new usage
ulong u_newCPU = (ulong)queryObj.Properties["UserModeTime"].Value
               + (ulong)queryObj.Properties["KernelModeTime"].Value;
decimal msPassed = Convert.ToDecimal(
                             (secondSample - firstSample).TotalMilliseconds);

//formula to get CPU ussage
if (u_newCPU > u_oldCPU)
    PercentProcessorTime = (decimal)((u_newCPU - u_oldCPU) / 
                               (msPassed * 100 * Environment.ProcessorCount));

Console.WriteLine("Process name " + queryObj.Properties["name"].value);                       
Console.WriteLine("processor time " + PercentProcessorTime);

the above code results output in 85.999 and sometimes 135.89888. i was so confused which way can i calculate the cpu usage of process.

Note: Its a duplicate. I cannot come to the conclusion from the existing sources. and i was confused. so only i asked a question.

like image 304
Gomathipriya Avatar asked Mar 05 '14 10:03

Gomathipriya


People also ask

How do I get CPU usage in PowerShell?

In Windows PowerShell there is no exclusive cmdlet to find out the CPU and memory utilization rates. You can use the get-wmi object cmdlet along with required parameters to fetch the results.

Why does WMI use so much CPU?

WMI Provider Host shouldn't normally use much CPU, as it shouldn't normally be doing anything. It may occasionally use some CPU when another piece of software or script on your PC asks for information via WMI, and that's normal. High CPU usage is likely just a sign that another application is requesting data via WMI.


1 Answers

You can use WMI to query this. I think you are looking for Win32_PerfFormattedData_PerfProc_Process class.

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
  public class MyWMIQuery
  {
    public static void Main()
    {
        try
        {
            ManagementObjectSearcher searcher = 
                new ManagementObjectSearcher("root\\CIMV2", 
                "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"); 

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
                Console.WriteLine("PercentProcessorTime: {0}", queryObj["PercentProcessorTime"]);
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }
    }
  }
}

Output:- Output when I run on my machine

like image 107
Sundeep Avatar answered Oct 23 '22 19:10

Sundeep