From e77f410fec91047a1e5a31be2926aa7ddf7116ca Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Wed, 29 May 2024 16:30:28 -0400 Subject: [PATCH] Fix reporting of "Own Memory" in TUI reporters The `live` and `summary` reporters include a column for the number of bytes directly allocated by a particular function (called "Own Bytes" or "Own Memory", with a corresponding column for the percentage of memory directly allocated by that function). A bug in our summation led to us undercounting and misreporting these columns. Signed-off-by: Matt Wozniski --- news/617.bugfix.rst | 1 + src/memray/reporters/tui.py | 14 +- .../unit/__snapshots__/test_tui_reporter.ambr | 558 +++++++++--------- tests/unit/test_summary_reporter.py | 30 +- 4 files changed, 299 insertions(+), 304 deletions(-) create mode 100644 news/617.bugfix.rst diff --git a/news/617.bugfix.rst b/news/617.bugfix.rst new file mode 100644 index 0000000000..348e3ee8d8 --- /dev/null +++ b/news/617.bugfix.rst @@ -0,0 +1 @@ +Fix reporting of "Own Memory" in the ``live`` and ``summary`` reporters. A bug in our summation led to us undercounting and misreporting the allocations directly attributed to a particular function. diff --git a/src/memray/reporters/tui.py b/src/memray/reporters/tui.py index 48c574b211..82c3a4178d 100644 --- a/src/memray/reporters/tui.py +++ b/src/memray/reporters/tui.py @@ -227,23 +227,17 @@ def aggregate_allocations( frame.n_allocations += allocation.n_allocations frame.thread_ids.add(allocation.tid) continue - (function, file_name, _), *caller_frames = stack_trace - location = Location(function=function, file=file_name) - processed_allocations[location] = AllocationEntry( - own_memory=allocation.size, - total_memory=allocation.size, - n_allocations=allocation.n_allocations, - thread_ids={allocation.tid}, - ) # Walk upwards and sum totals - visited = {location} - for function, file_name, _ in caller_frames: + visited = set() + for i, (function, file_name, _) in enumerate(stack_trace): location = Location(function=function, file=file_name) frame = processed_allocations[location] if location in visited: continue visited.add(location) + if i == 0: + frame.own_memory += allocation.size frame.total_memory += allocation.size frame.n_allocations += allocation.n_allocations frame.thread_ids.add(allocation.tid) diff --git a/tests/unit/__snapshots__/test_tui_reporter.ambr b/tests/unit/__snapshots__/test_tui_reporter.ambr index 3418730ea7..8d128385aa 100644 --- a/tests/unit/__snapshots__/test_tui_reporter.ambr +++ b/tests/unit/__snapshots__/test_tui_reporter.ambr @@ -22,145 +22,145 @@ font-weight: 700; } - .terminal-1423419425-matrix { + .terminal-619948215-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-1423419425-title { + .terminal-619948215-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-1423419425-r1 { fill: #e1e1e1;font-weight: bold } - .terminal-1423419425-r2 { fill: #e1e1e1 } - .terminal-1423419425-r3 { fill: #c5c8c6 } - .terminal-1423419425-r4 { fill: #f4005f } - .terminal-1423419425-r5 { fill: #64d484 } - .terminal-1423419425-r6 { fill: #dde6ed;font-weight: bold } - .terminal-1423419425-r7 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } - .terminal-1423419425-r8 { fill: #e1e2e3 } - .terminal-1423419425-r9 { fill: #cd5c5c } - .terminal-1423419425-r10 { fill: #61c12c } - .terminal-1423419425-r11 { fill: #fb8e03 } - .terminal-1423419425-r12 { fill: #ee960d } - .terminal-1423419425-r13 { fill: #8eb322 } - .terminal-1423419425-r14 { fill: #fea62b;font-weight: bold } - .terminal-1423419425-r15 { fill: #a7a9ab } + .terminal-619948215-r1 { fill: #e1e1e1;font-weight: bold } + .terminal-619948215-r2 { fill: #e1e1e1 } + .terminal-619948215-r3 { fill: #c5c8c6 } + .terminal-619948215-r4 { fill: #f4005f } + .terminal-619948215-r5 { fill: #64d484 } + .terminal-619948215-r6 { fill: #dde6ed;font-weight: bold } + .terminal-619948215-r7 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } + .terminal-619948215-r8 { fill: #e1e2e3 } + .terminal-619948215-r9 { fill: #cd5c5c } + .terminal-619948215-r10 { fill: #61c12c } + .terminal-619948215-r11 { fill: #fb8e03 } + .terminal-619948215-r12 { fill: #ee960d } + .terminal-619948215-r13 { fill: #8eb322 } + .terminal-619948215-r14 { fill: #fea62b;font-weight: bold } + .terminal-619948215-r15 { fill: #a7a9ab } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - MockApp + MockApp - + - - Memray live tracking      Fri Oct 13 12:00:00 2023 - PID: ???CMD: ??? - TID: 0x1Thread 1 of 1 - Samples: 2Duration: 42.0 seconds - StatusRemote has disconnected - -  Heap Usage ───────────────────────────────────── -                                                  ▐ -                                                  ▐ -                                                  █ -                                                  █ - ────────────────── 2.325KB (100% of 2.325KB max)  -                       Location                      Total Bytes% TotalO -  parent                                                 2.325KB100.00% -  grandparent                                            2.325KB100.00% -  f2                                                     1.325KB 56.99% -  f1                                                     1.000KB 43.01% -  malloc                                                333.000B 13.99% -  something0                                            333.000B 13.99% -  something1                                            333.000B 13.99% -  something2                                            333.000B 13.99% -  something3                                            333.000B 13.99% -  something4                                            333.000B 13.99% -  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own aS + + Memray live tracking      Fri Oct 13 12:00:00 2023 + PID: ???CMD: ??? + TID: 0x1Thread 1 of 1 + Samples: 2Duration: 42.0 seconds + StatusRemote has disconnected + +  Heap Usage ───────────────────────────────────── +                                                  ▐ +                                                  ▐ +                                                  █ +                                                  █ + ────────────────── 2.325KB (100% of 2.325KB max)  +                       Location                      Total Bytes% TotalO +  malloc                                                 2.325KB100.00% +  parent                                                 2.325KB100.00% +  grandparent                                            2.325KB100.00% +  f2                                                     1.325KB 56.99% +  f1                                                     1.000KB 43.01% +  something0                                            333.000B 13.99% +  something1                                            333.000B 13.99% +  something2                                            333.000B 13.99% +  something3                                            333.000B 13.99% +  something4                                            333.000B 13.99% +  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own aS @@ -190,142 +190,142 @@ font-weight: 700; } - .terminal-1874643093-matrix { + .terminal-3456420154-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-1874643093-title { + .terminal-3456420154-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-1874643093-r1 { fill: #dde6ed;font-weight: bold } - .terminal-1874643093-r2 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } - .terminal-1874643093-r3 { fill: #e1e1e1 } - .terminal-1874643093-r4 { fill: #c5c8c6 } - .terminal-1874643093-r5 { fill: #e1e2e3 } - .terminal-1874643093-r6 { fill: #cd5c5c } - .terminal-1874643093-r7 { fill: #61c12c } - .terminal-1874643093-r8 { fill: #f7840d } - .terminal-1874643093-r9 { fill: #d39f14 } - .terminal-1874643093-r10 { fill: #23568b } - .terminal-1874643093-r11 { fill: #fea62b;font-weight: bold } - .terminal-1874643093-r12 { fill: #a7a9ab } + .terminal-3456420154-r1 { fill: #dde6ed;font-weight: bold } + .terminal-3456420154-r2 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } + .terminal-3456420154-r3 { fill: #e1e1e1 } + .terminal-3456420154-r4 { fill: #c5c8c6 } + .terminal-3456420154-r5 { fill: #e1e2e3 } + .terminal-3456420154-r6 { fill: #cd5c5c } + .terminal-3456420154-r7 { fill: #61c12c } + .terminal-3456420154-r8 { fill: #f7840d } + .terminal-3456420154-r9 { fill: #d39f14 } + .terminal-3456420154-r10 { fill: #23568b } + .terminal-3456420154-r11 { fill: #fea62b;font-weight: bold } + .terminal-3456420154-r12 { fill: #a7a9ab } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - MockApp + MockApp - + - -                       Location                      Total Bytes% TotalOwn -  parent                                                 3.000KB100.00% -  grandparent                                            3.000KB100.00% -  f2                                                     2.000KB 66.67% -  malloc                                                 1.000KB 33.33%  1 -  f1                                                     1.000KB 33.33% - - - - - - - - - - - - - - - - - -  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own a S + +                       Location                      Total Bytes% TotalOwn +  malloc                                                 3.000KB100.00%  3 +  parent                                                 3.000KB100.00% +  grandparent                                            3.000KB100.00% +  f2                                                     2.000KB 66.67% +  f1                                                     1.000KB 33.33% + + + + + + + + + + + + + + + + + +  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own a S @@ -355,149 +355,149 @@ font-weight: 700; } - .terminal-506144247-matrix { + .terminal-1586243422-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-506144247-title { + .terminal-1586243422-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-506144247-r1 { fill: #e1e1e1;font-weight: bold } - .terminal-506144247-r2 { fill: #e1e1e1 } - .terminal-506144247-r3 { fill: #c5c8c6 } - .terminal-506144247-r4 { fill: #00ff00 } - .terminal-506144247-r5 { fill: #64d484 } - .terminal-506144247-r6 { fill: #f4005f } - .terminal-506144247-r7 { fill: #dde6ed;font-weight: bold } - .terminal-506144247-r8 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } - .terminal-506144247-r9 { fill: #dde6ed } - .terminal-506144247-r10 { fill: #e1e2e3 } - .terminal-506144247-r11 { fill: #cd5c5c } - .terminal-506144247-r12 { fill: #61c12c } - .terminal-506144247-r13 { fill: #c9a217 } - .terminal-506144247-r14 { fill: #f7840d } - .terminal-506144247-r15 { fill: #a6ac1e } - .terminal-506144247-r16 { fill: #d39f14 } - .terminal-506144247-r17 { fill: #83b625 } - .terminal-506144247-r18 { fill: #fea62b;font-weight: bold } - .terminal-506144247-r19 { fill: #a7a9ab } + .terminal-1586243422-r1 { fill: #e1e1e1;font-weight: bold } + .terminal-1586243422-r2 { fill: #e1e1e1 } + .terminal-1586243422-r3 { fill: #c5c8c6 } + .terminal-1586243422-r4 { fill: #00ff00 } + .terminal-1586243422-r5 { fill: #64d484 } + .terminal-1586243422-r6 { fill: #f4005f } + .terminal-1586243422-r7 { fill: #dde6ed;font-weight: bold } + .terminal-1586243422-r8 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } + .terminal-1586243422-r9 { fill: #dde6ed } + .terminal-1586243422-r10 { fill: #e1e2e3 } + .terminal-1586243422-r11 { fill: #cd5c5c } + .terminal-1586243422-r12 { fill: #b7a71a } + .terminal-1586243422-r13 { fill: #61c12c } + .terminal-1586243422-r14 { fill: #f7840d } + .terminal-1586243422-r15 { fill: #98b020 } + .terminal-1586243422-r16 { fill: #d39f14 } + .terminal-1586243422-r17 { fill: #7cb826 } + .terminal-1586243422-r18 { fill: #fea62b;font-weight: bold } + .terminal-1586243422-r19 { fill: #a7a9ab } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - MockApp + MockApp - + - - Memray live tracking      Fri Oct 13 12:00:00 2023 -  (∩`-´)⊃━☆゚.*・。゚  Heap Usage ───────────────────────────────────── - PID: ???CMD: ???                                                 ▐ - TID: 0x1Thread 1 of 1                                                 ▐ - Samples: 2Duration: 42.0 seconds                                                 ▟ - StatusRemote has disconnected                                                 █ - ────────────────── 3.000KB (100% of 3.000KB max)  - -                       Location                      Total Bytes% TotalOwn Bytes% OwnAllocations File/Module  -  parent                                                 3.000KB100.00%   0.000B  0.00%          3 fun.py       -  grandparent                                            3.000KB100.00%   0.000B  0.00%          3 fun.py       -  f2                                                     2.000KB 66.67%   0.000B  0.00%          2 f.py         -  malloc                                                 1.000KB 33.33%  1.000KB 33.33%          1 malloc.c     -  f1                                                     1.000KB 33.33%   0.000B  0.00%          1 f.py         - - - - - - - - - -  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own a Sort by Allocations + + Memray live tracking      Fri Oct 13 12:00:00 2023 +  (∩`-´)⊃━☆゚.*・。゚  Heap Usage ───────────────────────────────────── + PID: ???CMD: ???                                                 ▐ + TID: 0x1Thread 1 of 1                                                 ▐ + Samples: 2Duration: 42.0 seconds                                                 ▟ + StatusRemote has disconnected                                                 █ + ────────────────── 3.000KB (100% of 3.000KB max)  + +                       Location                      Total Bytes% TotalOwn Bytes% OwnAllocations File/Module  +  malloc                                                 3.000KB100.00%  3.000KB100.00%          3 malloc.c     +  parent                                                 3.000KB100.00%   0.000B  0.00%          3 fun.py       +  grandparent                                            3.000KB100.00%   0.000B  0.00%          3 fun.py       +  f2                                                     2.000KB 66.67%   0.000B  0.00%          2 f.py         +  f1                                                     1.000KB 33.33%   0.000B  0.00%          1 f.py         + + + + + + + + + +  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own a Sort by Allocations @@ -527,151 +527,151 @@ font-weight: 700; } - .terminal-627263092-matrix { + .terminal-2758690543-matrix { font-family: Fira Code, monospace; font-size: 20px; line-height: 24.4px; font-variant-east-asian: full-width; } - .terminal-627263092-title { + .terminal-2758690543-title { font-size: 18px; font-weight: bold; font-family: arial; } - .terminal-627263092-r1 { fill: #e1e1e1;font-weight: bold } - .terminal-627263092-r2 { fill: #e1e1e1 } - .terminal-627263092-r3 { fill: #c5c8c6 } - .terminal-627263092-r4 { fill: #00ff00 } - .terminal-627263092-r5 { fill: #64d484 } - .terminal-627263092-r6 { fill: #f4005f } - .terminal-627263092-r7 { fill: #dde6ed;font-weight: bold } - .terminal-627263092-r8 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } - .terminal-627263092-r9 { fill: #e1e2e3 } - .terminal-627263092-r10 { fill: #cd5c5c } - .terminal-627263092-r11 { fill: #61c12c } - .terminal-627263092-r12 { fill: #83b625 } - .terminal-627263092-r13 { fill: #fb8e03 } - .terminal-627263092-r14 { fill: #75ba27 } - .terminal-627263092-r15 { fill: #ee960d } - .terminal-627263092-r16 { fill: #6bbd29 } - .terminal-627263092-r17 { fill: #8eb322 } - .terminal-627263092-r18 { fill: #14191f } - .terminal-627263092-r19 { fill: #23568b } - .terminal-627263092-r20 { fill: #fea62b;font-weight: bold } - .terminal-627263092-r21 { fill: #a7a9ab } + .terminal-2758690543-r1 { fill: #e1e1e1;font-weight: bold } + .terminal-2758690543-r2 { fill: #e1e1e1 } + .terminal-2758690543-r3 { fill: #c5c8c6 } + .terminal-2758690543-r4 { fill: #00ff00 } + .terminal-2758690543-r5 { fill: #64d484 } + .terminal-2758690543-r6 { fill: #f4005f } + .terminal-2758690543-r7 { fill: #dde6ed;font-weight: bold } + .terminal-2758690543-r8 { fill: #dde6ed;font-weight: bold;text-decoration: underline; } + .terminal-2758690543-r9 { fill: #e1e2e3 } + .terminal-2758690543-r10 { fill: #cd5c5c } + .terminal-2758690543-r11 { fill: #80b725 } + .terminal-2758690543-r12 { fill: #61c12c } + .terminal-2758690543-r13 { fill: #fb8e03 } + .terminal-2758690543-r14 { fill: #75ba27 } + .terminal-2758690543-r15 { fill: #ee960d } + .terminal-2758690543-r16 { fill: #6bbd29 } + .terminal-2758690543-r17 { fill: #8eb322 } + .terminal-2758690543-r18 { fill: #14191f } + .terminal-2758690543-r19 { fill: #23568b } + .terminal-2758690543-r20 { fill: #fea62b;font-weight: bold } + .terminal-2758690543-r21 { fill: #a7a9ab } - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - MockApp + MockApp - + - - Memray live tracking      Fri Oct 13 12:00:00 2023 -  (∩`-´)⊃━☆゚.*・。゚  Heap Usage ───────────────────── - PID: ???CMD: ???                                 ▐ - TID: 0x1Thread 1 of 1                                 ▐ - Samples: 2Duration: 42.0 seconds                                 █ - StatusRemote has disconnected                                 █ - ── 2.325KB (100% of 2.325KB max)  - -                       Location                      Total Bytes% TotalOwn Bytes% OwnAllocations File/Modul -  parent                                                 2.325KB100.00%   0.000B  0.00%          3 fun.py     -  grandparent                                            2.325KB100.00%   0.000B  0.00%          3 fun.py     -  f2                                                     1.325KB 56.99%   0.000B  0.00%          2 f.py       -  f1                                                     1.000KB 43.01%   0.000B  0.00%          1 f.py       -  malloc                                                333.000B 13.99% 333.000B 13.99%          1 malloc.c   -  something0                                            333.000B 13.99%   0.000B  0.00%          1 something. -  something1                                            333.000B 13.99%   0.000B  0.00%          1 something.▃▃ -  something2                                            333.000B 13.99%   0.000B  0.00%          1 something. -  something3                                            333.000B 13.99%   0.000B  0.00%          1 something. -  something4                                            333.000B 13.99%   0.000B  0.00%          1 something. -  something5                                            333.000B 13.99%   0.000B  0.00%          1 something. -  something6                                            333.000B 13.99%   0.000B  0.00%          1 something. -  something7                                            333.000B 13.99%   0.000B  0.00%          1 something. - -  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own a Sort by Allocations + + Memray live tracking      Fri Oct 13 12:00:00 2023 +  (∩`-´)⊃━☆゚.*・。゚  Heap Usage ───────────────────── + PID: ???CMD: ???                                 ▐ + TID: 0x1Thread 1 of 1                                 ▐ + Samples: 2Duration: 42.0 seconds                                 █ + StatusRemote has disconnected                                 █ + ── 2.325KB (100% of 2.325KB max)  + +                       Location                      Total Bytes% TotalOwn Bytes% OwnAllocations File/Modul +  malloc                                                 2.325KB100.00%  2.325KB100.00%          3 malloc.c   +  parent                                                 2.325KB100.00%   0.000B  0.00%          3 fun.py     +  grandparent                                            2.325KB100.00%   0.000B  0.00%          3 fun.py     +  f2                                                     1.325KB 56.99%   0.000B  0.00%          2 f.py       +  f1                                                     1.000KB 43.01%   0.000B  0.00%          1 f.py       +  something0                                            333.000B 13.99%   0.000B  0.00%          1 something. +  something1                                            333.000B 13.99%   0.000B  0.00%          1 something.▃▃ +  something2                                            333.000B 13.99%   0.000B  0.00%          1 something. +  something3                                            333.000B 13.99%   0.000B  0.00%          1 something. +  something4                                            333.000B 13.99%   0.000B  0.00%          1 something. +  something5                                            333.000B 13.99%   0.000B  0.00%          1 something. +  something6                                            333.000B 13.99%   0.000B  0.00%          1 something. +  something7                                            333.000B 13.99%   0.000B  0.00%          1 something. + +  q Quit < Previous Thread > Next Thread t Sort by Total o Sort by Own a Sort by Allocations diff --git a/tests/unit/test_summary_reporter.py b/tests/unit/test_summary_reporter.py index 83e36d50d0..2ffa7e9cba 100644 --- a/tests/unit/test_summary_reporter.py +++ b/tests/unit/test_summary_reporter.py @@ -36,11 +36,11 @@ def test_with_multiple_allocations(): "┃ ┃