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
How to know the CPU and memory usage of a process with WMI?
提问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_Process或 Win32_PerfRawData_PerfProc_Process
E.g. their properties PercentProcessorTimeand WorkingSet
例如它们的属性PercentProcessorTime和WorkingSet
Note that the Perf classes take a bit effort to understand.
请注意,要理解 Perf 类需要付出一些努力。
- 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 WMIfor a starting place.
- 格式化版本存在问题(请参阅嘿,脚本专家!文章为什么我的性能监控脚本不断返回相同的错误值?)
- 并且原始数据类需要小心地将“ CounterType”转换为正确的数学公式,以用于根据原始数据计算值。看看我的驾驶怎么样?以 WMI为起点监视性能。
But those WMI classes should give you all the info you're looking for.
但是那些 WMI 类应该为您提供您正在寻找的所有信息。
回答by Mitch Wheat
回答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