diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-11-23 06:24:09 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-11-23 06:24:09 -0500 |
commit | a6792ad4c43d2b4c689161bfbbc5ce5cc3399e7f (patch) | |
tree | dd578f91f475f4ed6dbfd45d877a1a4401275b7d /test | |
parent | e1b2f88ede6938da498a765144f96a3e1156abb9 (diff) | |
download | python-coveragepy-a6792ad4c43d2b4c689161bfbbc5ce5cc3399e7f.tar.gz |
A Linux implementation of process_ram
Diffstat (limited to 'test')
-rw-r--r-- | test/osinfo.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/test/osinfo.py b/test/osinfo.py index 5932c53..5dc3899 100644 --- a/test/osinfo.py +++ b/test/osinfo.py @@ -34,6 +34,33 @@ if sys.hexversion >= 0x02050000 and sys.platform == 'win32': return 0 return mem_struct.PrivateUsage +elif sys.platform == 'linux2': + import os + + _scale = {'kb': 1024, 'mb': 1024*1024} + + def _VmB(key): + """Read the /proc/PID/status file to find memory use.""" + try: + # get pseudo file /proc/<pid>/status + t = open('/proc/%d/status' % os.getpid()) + v = t.read() + t.close() + except IOError: + return 0 # non-Linux? + # get VmKey line e.g. 'VmRSS: 9999 kB\n ...' + i = v.index(key) + v = v[i:].split(None, 3) + if len(v) < 3: + return 0 # invalid format? + # convert Vm value to bytes + return int(float(v[1]) * _scale[v[2].lower()]) + + def process_ram(): + """How much RAM is this process using? (Linux implementation""" + return _VmB('VmRSS') + + else: def process_ram(): """How much RAM is this process using? (no implementation)""" |