Professional Developer Community
Help | Site Map
Hello! Sign in or register.

Need Python Forum help? Post your Question here. It's Free!

soulofstar's Avatar
soulofstar
Newbie
3 Posts
April 5th, 2007
02:13 AM
#1

How to monitor CPU and RAM usage using Python
I want to monitor the CPU and RAM usage using Python,

Now, I know how to monitor RAM usage by Python

Code: ( text )
  1. from ctypes import *
  2. from ctypes.wintypes import *
  3.  
  4. class MEMORYSTATUS(Structure):
  5.     _fields_ = [
  6.     ('dwLength', DWORD),
  7.     ('dwMemoryLoad', DWORD),
  8.     ('dwTotalPhys', DWORD),
  9.     ('dwAvailPhys', DWORD),
  10.     ('dwTotalPageFile', DWORD),
  11.     ('dwAvailPageFile', DWORD),
  12.     ('dwTotalVirtual', DWORD),
  13.     ('dwAvailVirtual', DWORD),
  14.     ]
  15. def winmem():
  16.     m = MEMORYSTATUS()
  17.     windll.kernel32.GlobalMemoryStatus(byref(m))
  18.     return m
  19. m = winmem()
  20. print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)



But I don't know how to monitor CPU realtime usage, please help me.

And how to monitor CPU and RAM used by one program?

Last edited by bartonc : April 5th, 2007 at 02:49 AM. Reason: added [code][/code] tags
Reply
ghostdog74's Avatar
ghostdog74
Expert
345 Posts
April 5th, 2007
02:54 AM
#2

Re: How to monitor CPU and RAM usage using Python
Quote:
Originally Posted by soulofstar
I want to monitor the CPU and RAM usage using Python,

Now, I know how to monitor RAM usage by Python
from ctypes import *
from ctypes.wintypes import *

class MEMORYSTATUS(Structure):
_fields_ = [
('dwLength', DWORD),
('dwMemoryLoad', DWORD),
('dwTotalPhys', DWORD),
('dwAvailPhys', DWORD),
('dwTotalPageFile', DWORD),
('dwAvailPageFile', DWORD),
('dwTotalVirtual', DWORD),
('dwAvailVirtual', DWORD),
]
def winmem():
m = MEMORYSTATUS()
windll.kernel32.GlobalMemoryStatus(byref(m))
return m
m = winmem()
print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)


But I don't know how to monitor CPU realtime usage, please help me.

And how to monitor CPU and RAM used by one program?



while i am not much into ctypes, there's another way, using WMI
check MSDN and here for more.

one example :
Code: ( text )
  1. >>> import wmi
  2. >>> wm = wmi.WMI()
  3. >>> for j in wm.Win32_PerfFormattedData_PerfOS_Memory(): print j


another eg:
Code: ( text )
  1. >>> import wmi
  2. >>> wm = wmi.WMI ()
  3. >>> for j in wm.Win32_Processor (): print j

Reply
soulofstar's Avatar
soulofstar
Newbie
3 Posts
April 5th, 2007
07:04 AM
#3

Re: How to monitor CPU and RAM usage using Python
Now I knew how to monitor CPU and Memory usage via win32pdh, while I don't know how to monitor memory usage of one application.

snippet of code:
Code: ( text )
  1. mempath = win32pdh.MakeCounterPath((None, "Memory", None, \
  2.                                             None, -1, "Available MBytes"))
  3. query = win32pdh.OpenQuery(None, 0)
  4. counter = win32pdh.AddCounter(query, mempath, 0)
  5.  
  6. win32pdh.CollectQueryData(query)
  7. status, value = win32pdh.GetFormattedCounterValue(counter,
  8.                 win32pdh.PDH_FMT_LONG)

These code only can monitor the whole availabe memory, I do not know how to change the CounterPath to monitor one certain application's memory usage.

anyone can help me, thanks for your reply

Last edited by bartonc : April 5th, 2007 at 07:07 AM. Reason: added [code][/code] tags
Reply
bartonc's Avatar
bartonc
Moderator
3,601 Posts
April 5th, 2007
07:19 AM
#4

Re: How to monitor CPU and RAM usage using Python
Quote:
Originally Posted by ghostdog74
while i am not much into ctypes, there's another way, using WMI
check MSDN and here for more.

one example :
Code: ( text )
  1. >>> import wmi
  2. >>> wm = wmi.WMI()
  3. >>> for j in wm.Win32_PerfFormattedData_PerfOS_Memory(): print j


another eg:
Code: ( text )
  1. >>> import wmi
  2. >>> wm = wmi.WMI ()
  3. >>> for j in wm.Win32_Processor (): print j

Great link GD! Would you consider doing an article or code post for one of the sub forums. This is something that I had not heard of. Thanks for the info.

Reply
Reply

TOP CONTRIBUTORS