windows 如何使用 WMI 了解进程的 CPU 和内存使用情况?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1476018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:45:45  来源:igfitidea点击:

How to know the CPU and memory usage of a process with WMI?

windowswmi

提问by luc

I am using a wmi and python in order to track the behavior of the process running on my machine.

我正在使用 wmi 和 python 来跟踪在我的机器上运行的进程的行为。

from win32com.client import GetObject
wmi = GetObject('winmgmts:')
processes = wmi.InstancesOf('Win32_Process')

for process in processes:
    print process.ProcessId, process.Name 

The Win32_Process has a lot of informationbut I don't see anything for tracking the CPU consumption. The window Task Monitor is showing this info so I think it is possible to get it.

Win32_Process 有很多信息,但我没有看到任何用于跟踪 CPU 消耗的信息。窗口任务监视器正在显示此信息,因此我认为可以获取它。

I thought that the WorkingSetSize property is giving the memory consumption of the process but I can see different value from what is given by TaskMonitor.

我认为 WorkingSetSize 属性给出了进程的内存消耗,但我可以看到与 TaskMonitor 给出的值不同的值。

How to get these 2 values for a given process?

如何获得给定过程的这两个值?

Update: Task Monitor shows the PrivateWorkingSetSize which seems to be not available with the Win32_Process. What is the difference betwen WorkingSetSize and PrivateWorkingSetSize?

更新:任务监视器显示 PrivateWorkingSetSize,它似乎不适用于 Win32_Process。WorkingSetSize 和 PrivateWorkingSetSize 有什么区别?

采纳答案by Daryn

I'm pretty sure you want the WMI perf classes Win32_PerfFormattedData_PerfProc_Processor Win32_PerfRawData_PerfProc_Process

我很确定你想要 WMI 性能类 Win32_PerfFormattedData_PerfProc_ProcessWin32_PerfRawData_PerfProc_Process

E.g. their properties PercentProcessorTimeand WorkingSet

例如它们的属性PercentProcessorTimeWorkingSet

Note that the Perf classes take a bit effort to understand.

请注意,要理解 Perf 类需要付出一些努力。

But those WMI classes should give you all the info you're looking for.

但是那些 WMI 类应该为您提供您正在寻找的所有信息。

回答by Matt Watson

Win32_Process also has UserModeTime and KernelModeTime which can be used to calculate CPU usage

Win32_Process 还有 UserModeTime 和 KernelModeTime 可以用来计算 CPU 使用率

Also look at http://technet.microsoft.com/en-us/library/ee176718.aspx

另请参阅 http://technet.microsoft.com/en-us/library/ee176718.aspx