2b4735f1 |
# This tool lists processes that lock memory pages from swapping to disk.
import re
import psutil
|
02f9e8bb |
LCK_SUMMARY_REGEX = re.compile(
"^VmLck:\s+(?P<locked>[\d]+)\s+kB", re.MULTILINE) |
2b4735f1 |
def main():
try: |
9573edb4 |
print(_get_report()) |
2b4735f1 |
except Exception as e: |
9573edb4 |
print("Failure listing processes locking memory: %s" % str(e))
raise |
2b4735f1 |
def _get_report():
mlock_users = []
for proc in psutil.process_iter():
# sadly psutil does not expose locked pages info, that's why we |
02f9e8bb |
# iterate over the /proc/%pid/status files manually |
2b4735f1 |
try: |
02f9e8bb |
s = open("%s/%d/status" % (psutil.PROCFS_PATH, proc.pid), 'r')
except EnvironmentError:
continue
with s:
for line in s:
result = LCK_SUMMARY_REGEX.search(line)
if result:
locked = int(result.group('locked'))
if locked:
mlock_users.append({'name': proc.name(),
'pid': proc.pid,
'locked': locked}) |
2b4735f1 |
# produce a single line log message with per process mlock stats
if mlock_users:
return "; ".join(
"[%(name)s (pid:%(pid)s)]=%(locked)dKB" % args
# log heavy users first
for args in sorted(mlock_users, key=lambda d: d['locked'])
)
else:
return "no locked memory"
if __name__ == "__main__":
main() |