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 | cca8ec1c9c491e23b18b1b24bbc910bba2ad8cf8 (patch) | |
tree | d02d12c0610110c68b84f49226e0f00a6e40011c /test/osinfo.py | |
parent | d8775b5f8dfbc4f47c78ac50f0947bafe4b77744 (diff) | |
download | python-coveragepy-git-cca8ec1c9c491e23b18b1b24bbc910bba2ad8cf8.tar.gz |
A Linux implementation of process_ram
Diffstat (limited to 'test/osinfo.py')
-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 5932c537..5dc3899e 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)""" |