Skip to content

Commit 55f0160

Browse files
committed
Port the tree reporter to textual
Now that we are using Textual for the live mode, we can port the tree reporter to be a live Textual App. This has plenty of advantages over the static version as it offers interactive exploration of the tree, as well as the possibility of using different screens for showing detailed information about allocations such as the source and metadata. Signed-off-by: Pablo Galindo <[email protected]>
1 parent c4b35f3 commit 55f0160

File tree

10 files changed

+4476
-726
lines changed

10 files changed

+4476
-726
lines changed

docs/_static/images/tree_example.png

-1.49 MB
Loading

docs/tree.rst

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ the tracked process at the time when its memory usage was at its peak.
66

77
.. image:: _static/images/tree_example.png
88

9-
The tree reporter shows some statistics followed by a tree representation of
10-
the allocated memory. Several aspects are important when interpreting the tree
11-
representation:
9+
The tree reporter shows an interactive terminal applocation displaying a tree
10+
representation of the allocated memory. Several aspects are important when
11+
interpreting the tree representation:
1212

1313
* Only the 10 source locations responsible for the most allocated bytes are
1414
displayed. This is configurable with the ``--biggest-allocs`` command line
@@ -17,17 +17,24 @@ representation:
1717
calculated based only on the allocations that are shown. Since any allocation
1818
not big enough to be shown will not be included there, the reported total
1919
memory of the root node is normally less than the process's peak memory size.
20-
* Call chains of one node are collapsed for better readability. This means that
21-
branches in the tree where each node has only one child are collapsed and a
22-
special node is shown to reflect this. The hidden frames **never** correspond
23-
to frames that contained one of the source locations with the biggest
24-
allocations. The hidden frames are always callers of functions where the reported
25-
allocation happened.
2620
* The "📂" icon represents a frame that is a **caller** of a function where an
2721
allocation happened while the "📄" icon represents a frame that allocated
2822
memory.
2923
* Frames are colored based on their reported memory usage percentage, from red
3024
(most bytes allocated) to green (fewest).
25+
* You can interact with the application using the following keys:
26+
27+
* You can navigate the tree using the arrow keys. Pressing the up arrow key
28+
will move up one level in the tree, while pressing the down arrow key will
29+
move down one level. When a selected node is changed, the panel on the right
30+
will be updated to show the source code of the selected frame and some metadata
31+
about the allocations assigned to that frame.
32+
* Pressing the 'e' key will expand nodes and their children recursively until a node with
33+
more than one child is found. This can be used to quickly expand the tree.
34+
* Pressing the 'i' key will hide all nodes that belong to the import system and their
35+
children.
36+
* Presing the 'u' key will show all nodes that are marked as "uninteresting".
37+
3138

3239
Basic Usage
3340
-----------

news/499.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port the tree reporter to be an interactive Textual App.

src/memray/commands/tree.py

-13
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
from pathlib import Path
44
from textwrap import dedent
55

6-
from rich import print as rprint
7-
86
from memray import FileReader
97
from memray._errors import MemrayCommandError
10-
from memray._memray import size_fmt
118
from memray.commands.common import warn_if_not_enough_symbols
129
from memray.reporters.tree import TreeReporter
1310

@@ -80,14 +77,4 @@ def run(self, args: argparse.Namespace, parser: argparse.ArgumentParser) -> None
8077
f"Failed to parse allocation records in {result_path}\nReason: {e}",
8178
exit_code=1,
8279
)
83-
print()
84-
header = "Allocation metadata"
85-
rprint(f"{header}\n{'-'*len(header)}")
86-
rprint(f"Command line arguments: '{reader.metadata.command_line}'")
87-
rprint(f"Peak memory size: {size_fmt(reader.metadata.peak_memory)}")
88-
rprint(f"Number of allocations: {reader.metadata.total_allocations}")
89-
print()
90-
header = f"Biggest {args.biggest_allocs} allocations:"
91-
rprint(header)
92-
rprint("-" * len(header))
9380
reporter.render()

0 commit comments

Comments
 (0)