mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add code to measure memory usage on windows
This commit is contained in:
parent
8d5e009d61
commit
3ade7e7e33
@ -5,17 +5,19 @@ __license__ = 'GPL v3'
|
|||||||
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import gc
|
import gc, os
|
||||||
|
|
||||||
## {{{ http://code.activestate.com/recipes/286222/ (r1)
|
from calibre.constants import iswindows, islinux
|
||||||
import os
|
|
||||||
|
|
||||||
_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,
|
_proc_status = '/proc/%d/status' % os.getpid()
|
||||||
|
|
||||||
|
_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
|
||||||
'KB': 1024.0, 'MB': 1024.0*1024.0}
|
'KB': 1024.0, 'MB': 1024.0*1024.0}
|
||||||
|
|
||||||
def _VmB(VmKey):
|
def _VmB(VmKey):
|
||||||
'''Private.
|
'''Private.
|
||||||
'''
|
'''
|
||||||
global _proc_status, _scale
|
global _proc_status, _scale
|
||||||
@ -35,23 +37,59 @@ def _VmB(VmKey):
|
|||||||
return float(v[1]) * _scale[v[2]]
|
return float(v[1]) * _scale[v[2]]
|
||||||
|
|
||||||
|
|
||||||
def memory(since=0.0):
|
def linux_memory(since=0.0):
|
||||||
'''Return memory usage in bytes.
|
'''Return memory usage in bytes.
|
||||||
'''
|
'''
|
||||||
return _VmB('VmSize:') - since
|
return _VmB('VmSize:') - since
|
||||||
|
|
||||||
|
|
||||||
def resident(since=0.0):
|
def resident(since=0.0):
|
||||||
'''Return resident memory usage in bytes.
|
'''Return resident memory usage in bytes.
|
||||||
'''
|
'''
|
||||||
return _VmB('VmRSS:') - since
|
return _VmB('VmRSS:') - since
|
||||||
|
|
||||||
|
|
||||||
def stacksize(since=0.0):
|
def stacksize(since=0.0):
|
||||||
'''Return stack size in bytes.
|
'''Return stack size in bytes.
|
||||||
'''
|
'''
|
||||||
return _VmB('VmStk:') - since
|
return _VmB('VmStk:') - since
|
||||||
## end of http://code.activestate.com/recipes/286222/ }}}
|
## 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():
|
def gc_histogram():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user