mercurial/utils/memorytop.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 11 Mar 2025 02:29:42 +0100
branchstable
changeset 53042 cdd7bf612c7b
parent 51860 1c5810ce737e
permissions -rw-r--r--
bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45797
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     1
# memorytop requires Python 3.4
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     2
#
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     3
# Usage: set PYTHONTRACEMALLOC=n in the environment of the hg invocation,
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     4
# where n>= is the number of frames to show in the backtrace. Put calls to
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     5
# memorytop in strategic places to show the current memory use by allocation
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     6
# site.
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     7
51860
1c5810ce737e typing: add `from __future__ import annotations` to remaining source files
Matt Harbison <matt_harbison@yahoo.com>
parents: 45797
diff changeset
     8
from __future__ import annotations
1c5810ce737e typing: add `from __future__ import annotations` to remaining source files
Matt Harbison <matt_harbison@yahoo.com>
parents: 45797
diff changeset
     9
45797
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    10
import gc
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    11
import tracemalloc
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    12
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    13
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    14
def memorytop(limit=10):
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    15
    gc.collect()
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    16
    snapshot = tracemalloc.take_snapshot()
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    17
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    18
    snapshot = snapshot.filter_traces(
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    19
        (
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    20
            tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    21
            tracemalloc.Filter(False, "<frozen importlib._bootstrap_external>"),
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    22
            tracemalloc.Filter(False, "<unknown>"),
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    23
        )
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    24
    )
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    25
    stats = snapshot.statistics('traceback')
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    26
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    27
    total = sum(stat.size for stat in stats)
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    28
    print("\nTotal allocated size: %.1f KiB\n" % (total / 1024))
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    29
    print("Lines with the biggest net allocations")
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    30
    for index, stat in enumerate(stats[:limit], 1):
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    31
        print(
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    32
            "#%d: %d objects using %.1f KiB"
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    33
            % (index, stat.count, stat.size / 1024)
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    34
        )
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    35
        for line in stat.traceback.format(most_recent_first=True):
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    36
            print('    ', line)
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    37
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    38
    other = stats[limit:]
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    39
    if other:
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    40
        size = sum(stat.size for stat in other)
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    41
        count = sum(stat.count for stat in other)
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    42
        print(
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    43
            "%s other: %d objects using %.1f KiB"
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    44
            % (len(other), count, size / 1024)
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    45
        )
5b6c0af021da utils: helper function to print top memory allocation site
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    46
    print()