2 # linux-time.py -- Print resourse usage of a command executing on GNU/Linux
4 # awaiting PC's linux-mm patch for getrusage
6 # source file of the GNU LilyPond music typesetter
8 # (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
12 version = '@TOPLEVEL_VERSION@'
13 if version == '@' + 'TOPLEVEL_VERSION' + '@':
14 version = '(unknown version)' # uGUHGUHGHGUGH
25 sys.stderr.write ("%s from LilyPond %s\n" % (name, version))
29 Usage: %s [OPTION]... COMMAND
31 Print resourse usage of a command executing on GNU/Linux.
35 -v, --version version information
39 print (r"""%s (GNU LilyPond) %s""" % (name, version))
41 (options, files) = getopt.getopt (sys.argv[1:], 'vh', ['help','version'])
46 if o== '--help' or o == '-h':
49 if o == '--version' or o == '-v':
63 #sys.system ("/usr/bin/time %s" % command)
73 # why doesn't it work when put in a function?
76 # #child_pid = os.fork ()
77 # child_pid = forkit ()
82 # # AARg, wait takes only 1 argument,
83 # # but this still compiled
84 # #(pid, status) = os.wait (child_pid, os.WNOHANG)
85 # (pid, status) = os.waitpid (child_pid, os.WNOHANG)
87 # time.sleep (INTERVAL)
94 # status = os.system ("/usr/bin/time %s" % string.join (files, " "))
99 # cute, but this doesn't work: we get resources of /usr/bin/time
100 command = "/usr/bin/time"
103 # we should do 'time' ourselves:
109 child_pid = os.fork ()
112 statm = "/proc/%d/statm" % child_pid
115 # AARg, wait takes only 1 argument,
116 # but this still compiled
117 #(pid, status) = os.wait (child_pid, os.WNOHANG)
118 (pid, status) = os.waitpid (child_pid, os.WNOHANG)
124 time.sleep (INTERVAL)
125 f = open (statm, "r")
126 stats = f.readline ()
128 (size, rss, drie, vier, vijf, zes, zeven) = string.split (stats, " ")
130 size = string.atoi (size)
131 max_size = max (size, max_size)
132 avg_size = avg_size + size
133 rss = string.atoi (rss)
134 max_rss = max (rss, max_rss)
135 avg_rss = avg_rss + rss
138 #status = os.system ("/usr/bin/time %s" % string.join (files, " "))
140 os.execvp (command, (command,) + tuple (args))
143 ms = max_size * 4.0/1024
144 mr = max_size * 4.0/1024
145 avg_size = avg_size / t
146 avg_rss = avg_rss / t
147 as = avg_size * 4.0/1024
148 ar = avg_rss * 4.0/1024
149 print ("MAXSIZE: %6.3fM(%d), MAXRSS: %6.3fM(%d)" % (ms, max_size, mr, max_rss))
150 print ("AVGSIZE: %6.3fM(%d), AVGRSS: %6.3fM(%d)" % (as, avg_size, ar, avg_rss))