2017-02-10 06:17:37 +00:00
|
|
|
# This tool lists processes that lock memory pages from swapping to disk.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
2017-09-10 02:51:10 +02:00
|
|
|
LCK_SUMMARY_REGEX = re.compile(
|
|
|
|
"^VmLck:\s+(?P<locked>[\d]+)\s+kB", re.MULTILINE)
|
2017-02-10 06:17:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
2017-03-28 19:37:39 +11:00
|
|
|
print(_get_report())
|
2017-02-10 06:17:37 +00:00
|
|
|
except Exception as e:
|
2017-03-28 19:37:39 +11:00
|
|
|
print("Failure listing processes locking memory: %s" % str(e))
|
|
|
|
raise
|
2017-02-10 06:17:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_report():
|
|
|
|
mlock_users = []
|
|
|
|
for proc in psutil.process_iter():
|
|
|
|
# sadly psutil does not expose locked pages info, that's why we
|
2017-09-10 02:51:10 +02:00
|
|
|
# iterate over the /proc/%pid/status files manually
|
2017-02-10 06:17:37 +00:00
|
|
|
try:
|
2017-09-10 02:51:10 +02:00
|
|
|
s = open("%s/%d/status" % (psutil.PROCFS_PATH, proc.pid), 'r')
|
2018-11-27 12:59:04 +11:00
|
|
|
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})
|
|
|
|
except OSError:
|
|
|
|
# pids can disappear, we're ok with that
|
2017-09-10 02:51:10 +02:00
|
|
|
continue
|
2018-11-27 12:59:04 +11:00
|
|
|
|
2017-02-10 06:17:37 +00:00
|
|
|
|
|
|
|
# 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()
|