[Win32utils-devel] pctcpu and pctmem for windows?

Heesob Park phasis at gmail.com
Fri Feb 11 22:06:21 EST 2011


Hi,

2011/2/12 Daniel Berger <djberg96 at gmail.com>:
> Hi,
>
> Given the fields currently available in sys-proctable (from
> Win32_Process), what's a good way to calculate %cpu and %mem for a
> given process?
>
I think that using WbemScripting.SWbemRefresher is more appropriate
for getting cpu usage.

Here is a sample code:

require 'win32ole'
computer = "."
intPasses = 5
intPause = 0.1
wmi = WIN32OLE.connect("winmgmts:{impersonationLevel=impersonate}!//#{computer}/root/cimv2")
refresher = WIN32OLE.new("WbemScripting.SWbemRefresher")
items = refresher.addEnum(wmi,
"Win32_PerfFormattedData_PerfProc_Process").objectSet
refresher.refresh
for i in (1..intPasses)
  sleep intPause
  puts "Pass #{i}"
  puts Time.now
  items.each do |item|
    refresher.refresh
    if item.IDProcess==4
      puts "Name: #{item.Name}"
      puts "  Process ID: #{item.IDProcess}"
      puts "  Percent Processor Time: #{item.PercentProcessorTime}"
    end
  end
end

Regards,
Park Heesob


More information about the win32utils-devel mailing list