Get processor usage by every process from wmi

I found many sources to get processor usage by each process. In general, there are many ways to get processor use of a process.

  • percentprocessortime of win32_perfformatteddata_perfproc_process
  • class PerformanceCounter in system.diagnostics
  • manually calculated
  • The class of the process (process.getcurrentprocess (). Totalprocessortime;) as stated here .

Firstway:

For remote monitoring of processes (my scenario is remote monitoring), percentage performance always shows a value from 0 to 100+. this 100+ is due to several processors in the system. it can be calculated using percentage CPU time / processor.

The first question is:

I can read the percentage of CPU time in wmi explorer, it shows that all values ​​are 0 or 100, but not only this value. is this value correct? or is it useful for monitoring values?

The second way:

to control the PerformanceCounter class, this can only be done locally. therefore, I cannot use this. can this be used for remote?

Third way:

(the biggest confusion occurring here in terms of using the formula). This calculation is performed either by the PerformanceCounter class or by the win32_process class from wmi. some say to calculate a performance counter using the following command

consider one processor and

(processor \% processor time) = 10%

(processor \% user time) = 8%

(processor \% privileged time) = 2%

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

The application uses 80% (processor \% user time), which is (8 * .8) = 6.4% of the CPU.

for more details here .

computing usermodetime and kernelmodetime from win32_process using the following formulas

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 are displayed in 85.999, and sometimes in 135.89888. I was so confused how I can calculate the processor usage by processor.

Note: This is a duplicate. I cannot conclude from existing sources. and I was confused. so only i asked a question.

+6
source share
1 answer

You can use WMI to query. I think you are looking for the 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

+2
source

All Articles