MemoryMonitor.py
Go to the documentation of this file.
1 import os
2 
3 def _proc_status(pid):
4  return '/proc/%d/status' % pid
5 
6 _scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
7  'KB': 1024.0, 'MB': 1024.0*1024.0}
8 
9 def _VmB(pid, VmKey):
10  '''Private.
11  '''
12  global _scale
13  proc_status = _proc_status(pid)
14  # get pseudo file /proc/<pid>/status
15  try:
16  t = open(proc_status)
17  v = t.read()
18  t.close()
19  except:
20  return 0.0 # non-Linux?
21  # get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
22  try:
23  i = v.index(VmKey)
24  except ValueError:
25  return 0.0
26  v = v[i:].split(None, 3) # whitespace
27  if len(v) < 3:
28  return 0.0 # invalid format?
29  # convert Vm value to bytes
30  return float(v[1]) * _scale[v[2]]
31 
32 
33 def memory(pid, since=0.0):
34  '''Return memory usage in bytes.
35  '''
36  return _VmB(pid, 'VmSize:') - since
37 
38 
39 def resident(pid, since=0.0):
40  '''Return resident memory usage in bytes.
41  '''
42  return _VmB(pid, 'VmRSS:') - since
43 
44 
45 def stacksize(pid, since=0.0):
46  '''Return stack size in bytes.
47  '''
48  return _VmB(pid, 'VmStk:') - since
void split(double tt, double *fr)
def memory(pid, since=0.0)
def stacksize(pid, since=0.0)
procfile open("FD_BRL_v0.txt")
def _VmB(pid, VmKey)
Definition: MemoryMonitor.py:9
def resident(pid, since=0.0)
def _proc_status(pid)
Definition: MemoryMonitor.py:3