# python mem.py `pgrep docker`
Last active
October 18, 2021 21:47
-
-
Save takenoko-str/895fbfdd4547b484715fa544ce48c2a7 to your computer and use it in GitHub Desktop.
memory checker script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import division | |
import re | |
import sys | |
for pid in sys.argv[1:]: | |
mem = {'Rss:': 0, | |
'Pss:': 0, | |
'Shared_Clean:': 0, | |
'Shared_Dirty:': 0, | |
'Private_Clean:': 0, | |
'Private_Dirty:': 0, | |
'Swap:': 0} | |
for line in open('/proc/%s/smaps' % pid, 'r'): | |
line = line.rstrip('\n') | |
parts = re.split('\s+', line) | |
for k, v in mem.iteritems(): | |
if k in parts: | |
val = int(parts[1]) | |
if parts[2] == 'kB': | |
val = val * 1024 | |
elif parts[2] == 'mB': | |
val = val * 1024 ** 2 | |
mem[k] = v + val | |
shared_ratio = (mem['Shared_Clean:'] + | |
mem['Shared_Dirty:']) * 100.0 / mem['Rss:'] | |
swap = float(mem['Swap:']) / (1024 ** 2) | |
print 'PID %s shared %.2f%% %.2f[MB]' % (pid, shared_ratio, swap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment