-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmlock_report.py
More file actions
53 lines (41 loc) · 1.53 KB
/
mlock_report.py
File metadata and controls
53 lines (41 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# This tool lists processes that lock memory pages from swapping to disk.
import re
import psutil
LCK_SUMMARY_REGEX = re.compile(
r"^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')
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
continue
# 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()