|
|
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 )
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?
Last edited by bartonc : April 5th, 2007 at 02:49 AM.
Reason: added [code][/code] tags
|
|
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 )
>>> import wmi >>> wm = wmi.WMI() >>> for j in wm.Win32_PerfFormattedData_PerfOS_Memory(): print j
another eg:
Code: ( text )
>>> import wmi >>> wm = wmi.WMI () >>> for j in wm.Win32_Processor (): print j
|
|
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 )
mempath = win32pdh.MakeCounterPath((None, "Memory", None, \
None, -1, "Available MBytes")) query = win32pdh.OpenQuery(None, 0) counter = win32pdh.AddCounter(query, mempath, 0) win32pdh.CollectQueryData(query) status, value = win32pdh.GetFormattedCounterValue(counter, 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
|
|
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 )
>>> import wmi >>> wm = wmi.WMI() >>> for j in wm.Win32_PerfFormattedData_PerfOS_Memory(): print j
another eg:
Code: ( text )
>>> import wmi >>> wm = wmi.WMI () >>> 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.
|