Debian Codegraph @

contrib/memory.py

raw
1 # memory.py - track memory usage
2 #
3 # Copyright 2009 Olivia Mackall <[email protected]> and others
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
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
15 def memusage(ui):
16 """Report memory usage of the current process."""
17 result = {'peak': 0, 'rss': 0}
18 with open('/proc/self/status') as status:
19 # This will only work on systems with a /proc file system
20 # (like Linux).
21 for line in status:
22 parts = line.split()
23 key = parts[0][2:-1].lower()
24 if key in result:
25 result[key] = int(parts[1])
26 ui.write_err(
27 ", ".join(
28 ["%s: %.1f MiB" % (k, v / 1024.0) for k, v in result.iteritems()]
29 )
30 + "\n"
31 )
32
33
34 def extsetup(ui):
35 ui.atexit(memusage, ui)