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
This commit is contained in:
Dirk Mueller 2017-09-10 02:51:10 +02:00
parent 6effdf370a
commit 02f9e8bbdd

View File

@ -3,12 +3,12 @@
# This tool lists processes that lock memory pages from swapping to disk. # This tool lists processes that lock memory pages from swapping to disk.
import re import re
import subprocess
import psutil import psutil
SUMMARY_REGEX = re.compile(b".*\s+(?P<locked>[\d]+)\s+KB") LCK_SUMMARY_REGEX = re.compile(
"^VmLck:\s+(?P<locked>[\d]+)\s+kB", re.MULTILINE)
def main(): def main():
@ -22,28 +22,21 @@ def main():
def _get_report(): def _get_report():
mlock_users = [] mlock_users = []
for proc in psutil.process_iter(): for proc in psutil.process_iter():
pid = proc.pid
# sadly psutil does not expose locked pages info, that's why we # sadly psutil does not expose locked pages info, that's why we
# call to pmap and parse the output here # iterate over the /proc/%pid/status files manually
try: try:
out = subprocess.check_output(['pmap', '-XX', str(pid)]) s = open("%s/%d/status" % (psutil.PROCFS_PATH, proc.pid), 'r')
except subprocess.CalledProcessError as e: except EnvironmentError:
# 42 means process just vanished, which is ok continue
if e.returncode == 42: with s:
continue for line in s:
raise result = LCK_SUMMARY_REGEX.search(line)
last_line = out.splitlines()[-1] if result:
locked = int(result.group('locked'))
# some processes don't provide a memory map, for example those if locked:
# running as kernel services, so we need to skip those that don't mlock_users.append({'name': proc.name(),
# match 'pid': proc.pid,
result = SUMMARY_REGEX.match(last_line) 'locked': locked})
if result:
locked = int(result.group('locked'))
if locked:
mlock_users.append({'name': proc.name(),
'pid': pid,
'locked': locked})
# produce a single line log message with per process mlock stats # produce a single line log message with per process mlock stats
if mlock_users: if mlock_users: