1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License version 3 as
5
# published by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful, but
8
# WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
# General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
"""A program to spawn a subprocess and track how memory is consumed."""
20
def spawn_and_track(opts, args):
21
from meliae import perf_counter
22
timer = perf_counter.perf_counter.get_timer()
24
print 'spawning: %s' % (args,)
25
# We have to use shell=False, otherwise we end up tracking the 'cmd.exe' or
26
# 'sh' process, rather than the actual process we care about.
27
p = subprocess.Popen(args, shell=False)
28
mb = 1.0/(1024.0*1024)
32
while p.poll() is None:
34
cur_mem, peak_mem = perf_counter.perf_counter.get_memory(p)
35
mem_secs += cur_mem * (now - last)
37
time.sleep(opts.sleep_time)
38
if now - last_print > 3:
39
print '%8.3fs %6.1fMB %6.1fMB %8.1fMB*s \r' % (
40
now - start, cur_mem*mb, peak_mem*mb, mem_secs*mb),
42
print '%8.3fs %6.1fMB %6.1fMB %8.1fMB*s ' % (now - start,
43
cur_mem*mb, peak_mem*mb, mem_secs*mb)
48
p = optparse.OptionParser('%prog [local opts] command [opts]')
49
p.add_option('--trace-file', type=str, default=None,
50
help='Save the memory usage information to this file')
51
p.add_option('--sleep-time', type=float, default=0.1,
52
help='Check the status after this many seconds.')
53
# All options after the first 'command' are passed to the command, so don't
54
# try to process them ourselves.
55
p.disable_interspersed_args()
56
opts, args = p.parse_args(args)
57
spawn_and_track(opts, args)
60
if __name__ == '__main__':
62
sys.exit(main(sys.argv[1:]))