2014-03-05 122 views
6

我发现了很多来源来获取每个进程的CPU使用情况。通常有很多方法可以获得进程的CPU使用情况。在System.Diagnostics程序 从wmi获取每个进程的CPU使用情况

  • 从win32_perfformatteddata_perfproc_process

    1. percentprocessortime
    2. PerformanceCounter类由手工计算
    3. Process类(通过process.getcurrentprocess()totalprocessortime) 在here作为所述。

    FirstWay:

    对于远程过程监视(我的方案是远程监控),则percentprocessortime总是显示值0至100+。这100多个因为系统中的多个处理器而发生。可以使用percentprocessortime/processorcount来计算。

    问题在firstway:

    我可以读取WMI浏览器中的percentprocessortime,它示出了所有的值是0或100仅比该值不是其他。这个数值是否正确?或者它对监视值有用吗?

    方式二:

    为PerformanceCounter类的监测,可以仅为本地完成。所以我不能使用这个。有可能使用这个遥控器?

    第三条道路:

    (最大的困惑在这里发生的无论在哪个式使用。)这个计算是通过从一个WMI PerformanceCounter类或Win32_Process类制成。一些说通过使用follwing

    考虑单个CPU和

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

    (处理器\%用户时间)= 8%来计算性能计数器

    (处理器\%特权时间)= 2%

    (过程\%处理器时间\应用程序)= 80%

    你的应用程序使用80% (处理器\%用户时间)是(8 * .8)= 6.4%的CPU。

    更多请参考here

    通过使用下面的公式

    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计算来自Win32_Process的所述usermodetime和kernelmodetime。我很困惑我可以计算过程的CPU使用率。

    注意: 其重复。我无法从现有资料中得出结论。我很困惑。所以只有我问了一个问题。

  • 回答

    2

    您可以使用WMI来查询。我想你正在寻找Win32_PerfFormattedData_PerfProc_Process类。

    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 when I run on my machine

    +0

    在我看来,CPU使用率[ “PercentProcessorTime”]是错误的。只有3个进程使用非0 PercentProcessorTime _Total,并且在100处空闲,然后下一个进程的PercentProcessorTime大约为5。 – aeroson

    相关问题