Skip to content

Commit cca8ec1

Browse files
committed
A Linux implementation of process_ram
1 parent d8775b5 commit cca8ec1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/osinfo.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
3434
return 0
3535
return mem_struct.PrivateUsage
3636

37+
elif sys.platform == 'linux2':
38+
import os
39+
40+
_scale = {'kb': 1024, 'mb': 1024*1024}
41+
42+
def _VmB(key):
43+
"""Read the /proc/PID/status file to find memory use."""
44+
try:
45+
# get pseudo file /proc/<pid>/status
46+
t = open('/proc/%d/status' % os.getpid())
47+
v = t.read()
48+
t.close()
49+
except IOError:
50+
return 0 # non-Linux?
51+
# get VmKey line e.g. 'VmRSS: 9999 kB\n ...'
52+
i = v.index(key)
53+
v = v[i:].split(None, 3)
54+
if len(v) < 3:
55+
return 0 # invalid format?
56+
# convert Vm value to bytes
57+
return int(float(v[1]) * _scale[v[2].lower()])
58+
59+
def process_ram():
60+
"""How much RAM is this process using? (Linux implementation"""
61+
return _VmB('VmRSS')
62+
63+
3764
else:
3865
def process_ram():
3966
"""How much RAM is this process using? (no implementation)"""

0 commit comments

Comments
 (0)