devstack/tools/mlock_report.py
Dirk Mueller 02f9e8bbdd Replace pmap shellout with pure python implementation
Without this patch, the pmap -XX call fails on openSUSE Leap
distributions as those have a fairly ancient procps version that
does not support the -XX parameter. A pure python implementation
is more portable, faster and even shorter than the subprocess
call.

Closes-Bug: #1716066
Change-Id: I2fdb457e65359a1c9d40452c922cfdca0e6e74dc
2017-09-10 17:50:00 -06:00

54 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python
# This tool lists processes that lock memory pages from swapping to disk.
import re
import psutil
LCK_SUMMARY_REGEX = re.compile(
"^VmLck:\s+(?P<locked>[\d]+)\s+kB", re.MULTILINE)
def main():
try:
print(_get_report())
except Exception as e:
print("Failure listing processes locking memory: %s" % str(e))
raise
def _get_report():
mlock_users = []
for proc in psutil.process_iter():
# sadly psutil does not expose locked pages info, that's why we
# iterate over the /proc/%pid/status files manually
try:
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})
# 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()