Debian Codegraph @

pypy/tool/memusage/memusage.py

raw symbols SCIP
1 #! /usr/bin/env python
2 """
3 Usage: memusage.py [-o filename] command [args...]
4
5 Runs a subprocess, and measure its RSS (resident set size) every second.
6 At the end, print the maximum RSS measured, and some statistics.
7
8 Also writes "filename", reporting every second the RSS. If filename is not
9 given, the output is written to "memusage.log"
10 """
11
12 import sys, os, re, time
13
14 def parse_args():
15 args = sys.argv[1:]
16 if args[0] == '-o':
17 args.pop(0)
18 outname = args.pop(0)
19 else:
20 outname = 'memusage.log'
21 args[0] # make sure there is at least one argument left
22 return outname, args
23
24 try:
25 outname, args = parse_args()
26 except IndexError:
27 print >> sys.stderr, __doc__.strip()
28 sys.exit(2)
29
30 childpid = os.fork()
31 if childpid == 0:
32 os.execvp(args[0], args)
33 sys.exit(1)
34
35 r = re.compile("VmRSS:\s*(\d+)")
36
37 filename = '/proc/%d/status' % childpid
38 rss_max = 0
39 rss_sum = 0
40 rss_count = 0
41
42 f = open(outname, 'w', 0)
43 while os.waitpid(childpid, os.WNOHANG)[0] == 0:
44 g = open(filename)
45 s = g.read()
46 g.close()
47 match = r.search(s)
48 if not match: # VmRSS is missing if the process just finished
49 break
50 rss = int(match.group(1))
51 print >> f, rss
52 if rss > rss_max: rss_max = rss
53 rss_sum += rss
54 rss_count += 1
55 time.sleep(1)
56 f.close()
57
58 if rss_count > 0:
59 print
60 print 'Memory usage:'
61 print '\tmaximum RSS: %10d kb' % rss_max
62 print '\tmean RSS: %10d kb' % (rss_sum / rss_count)
63 print '\trun time: %10d s' % rss_count