From 3ade7e7e33cc2c1ed98cd3077509a4c021ad768d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 2 Mar 2011 13:34:31 -0700 Subject: [PATCH] Add code to measure memory usage on windows --- src/calibre/utils/mem.py | 112 ++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/src/calibre/utils/mem.py b/src/calibre/utils/mem.py index 1f9bff8d63..930870abd1 100644 --- a/src/calibre/utils/mem.py +++ b/src/calibre/utils/mem.py @@ -5,53 +5,91 @@ __license__ = 'GPL v3' __copyright__ = '2010, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import gc +import gc, os -## {{{ http://code.activestate.com/recipes/286222/ (r1) -import os +from calibre.constants import iswindows, islinux -_proc_status = '/proc/%d/status' % os.getpid() +if islinux: + ## {{{ http://code.activestate.com/recipes/286222/ (r1) -_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0, - 'KB': 1024.0, 'MB': 1024.0*1024.0} + _proc_status = '/proc/%d/status' % os.getpid() -def _VmB(VmKey): - '''Private. - ''' - global _proc_status, _scale - # get pseudo file /proc//status - try: - t = open(_proc_status) - v = t.read() - t.close() - except: - return 0.0 # non-Linux? - # get VmKey line e.g. 'VmRSS: 9999 kB\n ...' - i = v.index(VmKey) - v = v[i:].split(None, 3) # whitespace - if len(v) < 3: - return 0.0 # invalid format? - # convert Vm value to bytes - return float(v[1]) * _scale[v[2]] + _scale = {'kB': 1024.0, 'mB': 1024.0*1024.0, + 'KB': 1024.0, 'MB': 1024.0*1024.0} + + def _VmB(VmKey): + '''Private. + ''' + global _proc_status, _scale + # get pseudo file /proc//status + try: + t = open(_proc_status) + v = t.read() + t.close() + except: + return 0.0 # non-Linux? + # get VmKey line e.g. 'VmRSS: 9999 kB\n ...' + i = v.index(VmKey) + v = v[i:].split(None, 3) # whitespace + if len(v) < 3: + return 0.0 # invalid format? + # convert Vm value to bytes + return float(v[1]) * _scale[v[2]] -def memory(since=0.0): - '''Return memory usage in bytes. - ''' - return _VmB('VmSize:') - since + def linux_memory(since=0.0): + '''Return memory usage in bytes. + ''' + return _VmB('VmSize:') - since -def resident(since=0.0): - '''Return resident memory usage in bytes. - ''' - return _VmB('VmRSS:') - since + def resident(since=0.0): + '''Return resident memory usage in bytes. + ''' + return _VmB('VmRSS:') - since -def stacksize(since=0.0): - '''Return stack size in bytes. - ''' - return _VmB('VmStk:') - since -## end of http://code.activestate.com/recipes/286222/ }}} + def stacksize(since=0.0): + '''Return stack size in bytes. + ''' + return _VmB('VmStk:') - since + ## end of http://code.activestate.com/recipes/286222/ }}} + memory = linux_memory +elif iswindows: + import win32process + import win32con + import win32api + + # See http://msdn.microsoft.com/en-us/library/ms684877.aspx + # for details on the info returned by get_meminfo + + def get_handle(pid): + return win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, 0, + pid) + + def listprocesses(self): + for process in win32process.EnumProcesses(): + try: + han = get_handle(process) + procmeminfo = meminfo(han) + procmemusage = procmeminfo["WorkingSetSize"] + yield process, procmemusage + except: + pass + + def get_meminfo(pid): + han = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, 0, + pid) + return meminfo(han) + + def meminfo(handle): + return win32process.GetProcessMemoryInfo(handle) + + def win_memory(since=0.0): + info = meminfo(get_handle(os.getpid())) + return info['WorkingSetSize'] - since + + memory = win_memory def gc_histogram():