Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

windows

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?

like image 522
luc Avatar asked Sep 25 '09 08:09

luc


People also ask

How do I check memory and CPU usage?

Press CTRL + Shift + Esc to open Task Manager. Click the Performance tab. This tab displays your system's RAM, CPU, GPU, and disk usage, along with network info. To view RAM usage, select the Memory box.

How do I monitor CPU memory usage of a single process in Windows?

Go to the Performance Monitor. Right-click on the graph and select "Add Counters". In the "Available counters" list, open the "Process" section by clicking on the down arrow next to it. Select "% Processor Time" (and any other counter you want).

How do I find the CPU and RAM usage using PowerShell?

In Windows PowerShell there is no exclusive cmdlet to find out the CPU and memory utilization rates. You can use the get-wmi object cmdlet along with required parameters to fetch the results.


1 Answers

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.

like image 53
Daryn Avatar answered Sep 22 '22 21:09

Daryn