blob: 41685f17e959d6ba10272a425c932be01ee74421 (
plain)
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
54
55
56
57
58
59
60
|
#!/usr/bin/env python
"""find-max-ram - Returns the maximum amount of memory used by a program.
Every half second, run ps with the appropriate commands, getting the
size of the program. Return max value.
"""
import os, sys, time
def get_val(cmdstr):
"""Runs ps and gets sum rss for processes making cmdstr
Returns None if process not found.
"""
cmd = ("ps -Ao cmd -o rss | grep '%s' | grep -v grep" % cmdstr)
# print "Running ", cmd
fp = os.popen(cmd)
lines = fp.readlines()
fp.close()
if not lines: return None
else: return reduce(lambda x,y: x+y, map(read_ps_line, lines))
def read_ps_line(psline):
"""Given a specially formatted line by ps, return rss value"""
l = psline.split()
assert len(l) >= 2 # first few are name, last one is rss
return int(l[-1])
def main(cmdstr):
while get_val(cmdstr) is None: time.sleep(0.5)
current_max = 0
while 1:
rss = get_val(cmdstr)
print rss
if rss is None: break
current_max = max(current_max, rss)
time.sleep(0.5)
print current_max
if __name__=="__main__":
if len(sys.argv) != 2:
print """Usage: find-max-ram [command string]
It will then run ps twice a second and keep totalling how much RSS
(resident set size) the process(es) whose ps command name contain the
given string use up. When there are no more processes found, it will
print the number and exit.
"""
sys.exit(1)
else: main(sys.argv[1])
|