~ubuntu-branches/debian/sid/meliae/sid

« back to all changes in this revision

Viewing changes to track_memory.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2009-12-19 18:23:37 UTC
  • Revision ID: james.westby@ubuntu.com-20091219182337-t09txw6ca1yfysn9
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
2
 
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.
 
6
 
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.
 
11
 
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/>.
 
14
 
 
15
"""A program to spawn a subprocess and track how memory is consumed."""
 
16
 
 
17
import subprocess
 
18
import time
 
19
 
 
20
def spawn_and_track(opts, args):
 
21
    from meliae import perf_counter
 
22
    timer = perf_counter.perf_counter.get_timer()
 
23
    start = 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)
 
29
    last = start
 
30
    last_print = 0
 
31
    mem_secs = 0
 
32
    while p.poll() is None:
 
33
        now = timer()
 
34
        cur_mem, peak_mem = perf_counter.perf_counter.get_memory(p)
 
35
        mem_secs += cur_mem * (now - last)
 
36
        last = now
 
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),
 
41
            last_print = now
 
42
    print '%8.3fs %6.1fMB %6.1fMB %8.1fMB*s          ' % (now - start,
 
43
        cur_mem*mb, peak_mem*mb, mem_secs*mb)
 
44
 
 
45
 
 
46
def main(args):
 
47
    import optparse
 
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)
 
58
 
 
59
 
 
60
if __name__ == '__main__':
 
61
    import sys
 
62
    sys.exit(main(sys.argv[1:]))