Mercurial > hg
comparison contrib/memory.py @ 27795:3e0d27d298b7
with: use context manager for file I/O in memusage
author | Bryan O'Sullivan <bryano@fb.com> |
---|---|
date | Fri, 15 Jan 2016 13:14:45 -0800 |
parents | 08a0f04b56bd |
children | ade330deb39a |
comparison
equal
deleted
inserted
replaced
27794:9a1f3f9bac5d | 27795:3e0d27d298b7 |
---|---|
13 | 13 |
14 import atexit | 14 import atexit |
15 | 15 |
16 def memusage(ui): | 16 def memusage(ui): |
17 """Report memory usage of the current process.""" | 17 """Report memory usage of the current process.""" |
18 status = None | |
19 result = {'peak': 0, 'rss': 0} | 18 result = {'peak': 0, 'rss': 0} |
20 try: | 19 with open('/proc/self/status', 'r') as status: |
21 # This will only work on systems with a /proc file system | 20 # This will only work on systems with a /proc file system |
22 # (like Linux). | 21 # (like Linux). |
23 status = open('/proc/self/status', 'r') | |
24 for line in status: | 22 for line in status: |
25 parts = line.split() | 23 parts = line.split() |
26 key = parts[0][2:-1].lower() | 24 key = parts[0][2:-1].lower() |
27 if key in result: | 25 if key in result: |
28 result[key] = int(parts[1]) | 26 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) | 27 ui.write_err(", ".join(["%s: %.1f MiB" % (key, value / 1024.0) |
33 for key, value in result.iteritems()]) + "\n") | 28 for key, value in result.iteritems()]) + "\n") |
34 | 29 |
35 def extsetup(ui): | 30 def extsetup(ui): |
36 atexit.register(memusage, ui) | 31 atexit.register(memusage, ui) |