Commits

Antonio Cuni committed 4dcb563

add the -o option to memusage.py

Comments (0)

Files changed (1)

pypy/tool/memusage/memusage.py

 #! /usr/bin/env python
 """
+Usage: memusage.py [-o filename] command [args...]
+
 Runs a subprocess, and measure its RSS (resident set size) every second.
 At the end, print the maximum RSS measured, and some statistics.
-Also writes 'memusage.log', reporting every second the RSS.
+
+Also writes "filename", reporting every second the RSS.  If filename is not
+given, the output is written to "memusage.log"
 """
 
 import sys, os, re, time
 
-args = sys.argv[1:]
-if not args:
-    print >> sys.stderr, __doc__
+def parse_args():
+    args = sys.argv[1:]
+    if args[0] == '-o':
+        args.pop(0)
+        outname = args.pop(0)
+    else:
+        outname = 'memusage.log'
+    args[0] # make sure there is at least one argument left
+    return outname, args
+
+try:
+    outname, args = parse_args()
+except IndexError:
+    print >> sys.stderr, __doc__.strip()
     sys.exit(2)
+
 childpid = os.fork()
 if childpid == 0:
     os.execvp(args[0], args)
 rss_sum = 0
 rss_count = 0
 
-f = open('memusage.log', 'w', 0)
+f = open(outname, 'w', 0)
 while os.waitpid(childpid, os.WNOHANG)[0] == 0:
     g = open(filename)
     s = g.read()