contrib/memory.py
changeset 10017 253d0da256b2
child 10264 d6512b3e9ac0
equal deleted inserted replaced
10016:8a0af23af5d1 10017:253d0da256b2
       
     1 # memory.py - track memory usage
       
     2 #
       
     3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
       
     4 #
       
     5 # This software may be used and distributed according to the terms of the
       
     6 # GNU General Public License version 2, incorporated herein by reference.
       
     7 
       
     8 '''helper extension to measure memory usage
       
     9 
       
    10 Reads current and peak memory usage from ``/proc/self/status`` and
       
    11 prints it to ``stderr`` on exit.
       
    12 '''
       
    13 
       
    14 import atexit
       
    15 
       
    16 def memusage(ui):
       
    17     """Report memory usage of the current process."""
       
    18     status = None
       
    19     result = {'peak': 0, 'rss': 0}
       
    20     try:
       
    21         # This will only work on systems with a /proc file system
       
    22         # (like Linux).
       
    23         status = open('/proc/self/status', 'r')
       
    24         for line in status:
       
    25             parts = line.split()
       
    26             key = parts[0][2:-1].lower()
       
    27             if key in result:
       
    28                 result[key] = int(parts[1])
       
    29     finally:
       
    30         if status is not None:
       
    31             status.close()
       
    32     ui.write_err(", ".join(["%s: %.1f MiB" % (key, value/1024.0)
       
    33                             for key, value in result.iteritems()]) + "\n")
       
    34 
       
    35 def extsetup(ui):
       
    36     atexit.register(memusage, ui)