c# - 从wmi获取每个进程的Cpu使用率

标签 c# wmi performancecounter win32-process

我找到了很多资源来获取每个进程的 cpu 使用率。一般来说,有很多方法可以获取进程的 cpu 使用率。

  1. 来自 win32_perfformatteddata_perfproc_process 的处理器时间百分比
  2. system.diagnostics 中的性能计数器类
  3. 人工计算
  4. 进程类(process.getcurrentprocess().totalprocessortime;) 如 here 中所述.

第一方式:

对于远程进程监控(我的场景是远程监控),percentprocessortime 始终显示值 0 到 100+。这 100+ 的发生是因为系统中有多个处理器。它可以通过使用 percentprocessortime/processorcount 来计算。

第一个问题:

我可以在 wmi 资源管理器中读取 percentprocessortime,它显示所有值都是 0 或 100,仅此值除外。这个值是否正确?还是对监控值(value)有用?

第二种方式:

对于PerformanceCounter类的监控,只能在本地进行。所以我不能用这个。可以远程使用吗?

第三种方式:

(最大的混淆发生在使用哪个公式方面。)这个计算是由 wmi 的 PerformanceCounter 类或 win32_process 类进行的。有人说使用以下方法计算性能计数器

考虑单 CPU 和

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

(处理器\%用户时间)= 8%

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

(进程\% 处理器时间\你的应用程序)= 80%

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

更多引用here .

通过使用以下公式从 win32_process 计算 usermodetime 和 kernelmodetime

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 使用率。

注意: 它是重复的。我不能从现有的资料中得出结论。我很困惑。所以只有我问了一个问题。

最佳答案

您可以使用 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

关于c# - 从wmi获取每个进程的Cpu使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195277/

相关文章:

c# - 如何生成 .svc 文件?

c# - 如何使用 WMI 防止应用程序在权限检查期间挂起

.net - 以编程方式将 SSL 证书绑定(bind)到端口

windows - 如何通过 WMI 浏览服务?

c# - 跨越不同性能计数器的性能计数器实例

c# - 如何从 ViewModel(MVVM) 调用 UI Controls 方法?

c# - 如何在 ASP.NET MVC 3 中将 Razor View 呈现为字符串?

c# - 如何将 System.Web.Cache 传递到我的服务层

powershell - Set-WindowsAzureLog 的替代品是什么?

delphi - 在 Windows 7 中看不到性能对象