获得从WMI每个进程的CPU使用率 [英] Get the Cpu usage of each process from wmi

查看:3496
本文介绍了获得从WMI每个进程的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现许多来源获得每个进程的CPU占用率。一般来说有很多方法可以获取进程的CPU占用率。

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. 从win32_perfformatteddata_perfproc_process percentprocessortime

  2. 在System.Diagnostics程序

  3. 通过手工计算

  4. Process类PerformanceCounter类(由process.getcurrentprocess()totalprocessortime;)
    作为所说在这里

  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:

有关远程过程监控(我的方案是远程监控),该percentprocessortime总是显示为0〜100+。这种100+是因为系统中的多个处理器。它可以通过使用percentprocessortime / processorcount来计算。

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.

问在firstway:

我可以读取WMI浏览器中的percentprocessortime,它显示了所有的值是0或100只不大于该值其他。是这个值是正确的?或者是监测值有用

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?

第二种方式:

有关PerformanceCounter类的监测,它只能为本地完成。所以我不能用这个。 ?是否有可能使用它进行远程

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

第三条道路:

(最大的困惑发生在这里无论在哪个公式来使用。)这种计算通过从一个WMI PerformanceCounter类或Win32_Process类的。也有人说,通过使用follwing

(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

计算性能计数器考虑单CPU以及

consider single CPU and

(processor\%处理器时间)= 10%

(processor\%processor time) = 10%

(processor\%用户时间)= 8%

(processor\%user time) = 8%

(processor\\ \\%特权时间)= 2%

(processor\% privilege time) = 2%

(process\%的处理器time\your应用程序)= 80%

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

您应用程序正在使用的(processor\%用户时间)80%,它是(8 * 0.8)= 6.4的CPU%。

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

更多参考的这里

通过使用下列公式

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);



以上代码的结果输出85.999,有时135.89888。我是如此迷茫哪种方式我可以计算过程中的CPU占用

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.

推荐答案

您可以使用WMI查询此。我认为你正在寻找的 Win32_PerfFormattedData_PerfProc_Process 的类

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:-

这篇关于获得从WMI每个进程的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆