Mercurial > hg
annotate contrib/memory.py @ 47767:66ad7e32011f stable
fix: use scmutil.movedirstate() instead of reimplementing
I wrote this patch 2 years ago as a little cleanup. I wanted to
generally used `scmutil.movedirstate()` instead of manually updating
the dirstate because that is easy to get wrong. I didn't know until
today that the current code had a bug. So I added the test case two
patches before this one and dusted off this one patch. This is a
little slower than the previous code, as it diffs two
manifests. However, it fixes the bug and I don't think it's going to
be noticeably slower anyway.
Differential Revision: https://phab.mercurial-scm.org/D11210
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 11 Mar 2019 10:59:35 -0700 |
parents | d4ba4d51f85f |
children | 6000f5b25c9b |
rev | line source |
---|---|
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
1 # memory.py - track memory usage |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
2 # |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents:
43076
diff
changeset
|
3 # Copyright 2009 Olivia Mackall <olivia@selenic.com> and others |
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
4 # |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10264 | 6 # GNU General Public License version 2 or any later version. |
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
7 |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
8 '''helper extension to measure memory usage |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
9 |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
10 Reads current and peak memory usage from ``/proc/self/status`` and |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
11 prints it to ``stderr`` on exit. |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
12 ''' |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
13 |
28510
ade330deb39a
contrib: make memory.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27795
diff
changeset
|
14 from __future__ import absolute_import |
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
15 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
16 |
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
17 def memusage(ui): |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
18 """Report memory usage of the current process.""" |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
19 result = {'peak': 0, 'rss': 0} |
27795
3e0d27d298b7
with: use context manager for file I/O in memusage
Bryan O'Sullivan <bryano@fb.com>
parents:
10282
diff
changeset
|
20 with open('/proc/self/status', 'r') as status: |
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
21 # This will only work on systems with a /proc file system |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
22 # (like Linux). |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
23 for line in status: |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
24 parts = line.split() |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
25 key = parts[0][2:-1].lower() |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
26 if key in result: |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
27 result[key] = int(parts[1]) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
28 ui.write_err( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
29 ", ".join( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
30 ["%s: %.1f MiB" % (k, v / 1024.0) for k, v in result.iteritems()] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
31 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
32 + "\n" |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
33 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
31958
diff
changeset
|
34 |
10017
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
35 |
253d0da256b2
contrib: helper extension to track memory usage
Martin Geisler <mg@lazybytes.net>
parents:
diff
changeset
|
36 def extsetup(ui): |
31958
de5c9d0e02ea
atexit: switch to home-grown implementation
Bryan O'Sullivan <bryano@fb.com>
parents:
30386
diff
changeset
|
37 ui.atexit(memusage, ui) |