如何使用 WMI 了解进程的 CPU 和内存使用情况? [英] How to know the CPU and memory usage of a process with WMI?
问题描述
我使用 wmi 和 python 来跟踪在我的机器上运行的进程的行为.
<前>从 win32com.client 导入 GetObjectwmi = GetObject('winmgmts:')进程 = wmi.InstancesOf('Win32_Process')对于进程中的进程:打印 process.ProcessId、process.NameWin32_Process 有一个大量信息 但我没有看到任何用于跟踪 CPU 消耗的内容.窗口任务监视器正在显示此信息,因此我认为可以获取它.
我认为 WorkingSetSize 属性给出了进程的内存消耗,但我可以看到与 TaskMonitor 给出的值不同的值.
如何获取给定进程的这 2 个值?
更新:任务监视器显示 PrivateWorkingSetSize,它似乎不适用于 Win32_Process.WorkingSetSize 和 PrivateWorkingSetSize 有什么区别?
我很确定你想要 WMI perf 类Win32_PerfFormattedData_PerfProc_Process 或Win32_PerfRawData_PerfProc_Process
例如它们的属性 PercentProcessorTime 和 WorkingSet
请注意,要理解 Perf 类需要付出一些努力.
- 格式化版本存在问题(请参阅嘿,脚本专家!文章为什么我的性能监控脚本不断返回相同的错误值?)
- 原始数据类需要小心翻译CounterType";输入正确的数学公式,用于根据原始数据计算值.请参阅我的驾驶情况如何?使用 WMI 监控性能作为起点.
但是那些 WMI 类应该为您提供您正在寻找的所有信息.
I am using a wmi and python in order to track the behavior of the process running on my machine.
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 information but 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.
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.
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?
I'm pretty sure you want the WMI perf classes Win32_PerfFormattedData_PerfProc_Process or Win32_PerfRawData_PerfProc_Process
E.g. their properties PercentProcessorTime and WorkingSet
Note that the Perf classes take a bit effort to understand.
- There are gotchas with the Formatted versions (See Hey, Scripting Guy! article Why Does My Performance Monitoring Script Keep Returning the Same Incorrect Values?)
- And the Raw Data classes need care to translate the "CounterType" into the right math formula to use to calculate the values based on the raw data. See How's My Driving? Monitoring Performance Using WMI for a starting place.
But those WMI classes should give you all the info you're looking for.
这篇关于如何使用 WMI 了解进程的 CPU 和内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!