Skip to content

Improvements to memory use

Ian Rogers linux-perf-users lkml 5 versions – Applied

Status from this message: Thanks, applied to perf-tools-next.

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest50 patches 44 repliesApplied
  2. v453 patches 29 repliesApplied
  3. v350 patches 11 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v213 patches14 repliesSuperseded
v13 patches0 repliesSuperseded

Patches in v5 Applied

#Subject and filesSizeRepliesApplied
1/50perf comm: Use regular mutextools/perf/util/comm.c+5 -55 replies
2/50libperf: Lazily allocate/size mmap event copytools/lib/perf/include/internal/mmap.h tools/lib/perf/mmap.c+20 -44 replies
3/50perf mmap: Lazily initialize zstd streamstools/perf/builtin-record.c tools/perf/util/compress.h tools/perf/util/mmap.c +2 more+58 -431 reply
4/50tools api fs: Switch filename__read_str to use io.htools/lib/api/fs/fs.c tools/lib/api/io.h+22 -451 reply
5/50tools api fs: Avoid reading whole file for a 1 byte booltools/lib/api/fs/fs.c+15 -91 reply
6/50tools lib api: Add io_dir an allocation free readdir alternativetools/lib/api/Makefile tools/lib/api/io_dir.h+76 -15 replies
7/50perf maps: Switch modules tree walk to io_dir__readdirtools/perf/util/machine.c+8 -111 reply
8/50perf record: Be lazier in allocating lost samples buffertools/perf/builtin-record.c+19 -104 replies
9/50perf pmu: Switch to io_dir__readdirtools/perf/util/pmu.c tools/perf/util/pmus.c+30 -481 reply
10/50perf header: Switch mem topology to io_dir__readdirtools/perf/util/header.c+16 -151 reply
11/50perf events: Remove scandir in thread synthesistools/perf/util/synthetic-events.c+12 -101 reply
12/50perf map: Simplify map_ip/unmap_ip and make map size smallertools/perf/util/machine.c tools/perf/util/map.c tools/perf/util/map.h +2 more+50 -683 replies
13/50perf maps: Move symbol maps functions to maps.ctools/perf/util/maps.c tools/perf/util/maps.h tools/perf/util/symbol.c +1 more+250 -2492 replies
14/50perf thread: Add missing RC_CHK_EQUALtools/perf/util/thread.c+1 -12 replies
15/50perf maps: Add maps__for_each_map to call a function on each entrytools/perf/arch/x86/util/event.c tools/perf/builtin-report.c tools/perf/tests/maps.c +10 more+508 -3502 replies
16/50perf maps: Add remove maps function to remove a map based on callbacktools/perf/util/maps.c tools/perf/util/maps.h tools/perf/util/symbol.c+38 -162 replies
17/50perf debug: Expose debug filetools/perf/util/debug.c tools/perf/util/debug.h+16 -71 reply
18/50perf maps: Refactor maps__fixup_overlappingstools/perf/util/maps.c tools/perf/util/maps.h tools/perf/util/thread.c+39 -282 replies
19/50perf maps: Do simple merge if given map doesn't overlaptools/perf/util/maps.c+11 -02 replies
20/50perf maps: Rename clone to copy fromtools/perf/util/machine.c tools/perf/util/maps.c tools/perf/util/maps.h +1 more+4 -91 reply
21/50perf maps: Add maps__load_firsttools/perf/util/bpf_lock_contention.c tools/perf/util/maps.c tools/perf/util/maps.h+16 -10 replies
22/50perf maps: Add find next entry to give entry after the given maptools/perf/util/machine.c tools/perf/util/maps.c tools/perf/util/maps.h+16 -40 replies
23/50perf maps: Reduce scope of map_rb_node and maps internalstools/perf/util/maps.c tools/perf/util/maps.h+55 -580 replies
24/50perf maps: Fix up overlaps during fixup_endtools/perf/util/maps.c+1 -10 replies
25/50perf maps: Switch from rbtree to lazily sorted array for addressestools/perf/tests/maps.c tools/perf/util/map.c tools/perf/util/maps.c +1 more+757 -4840 replies
26/50perf maps: Get map before returning in maps__findtools/perf/arch/x86/tests/dwarf-unwind.c tools/perf/tests/vmlinux-kallsyms.c tools/perf/util/bpf-event.c +4 more+26 -270 replies
27/50perf maps: Get map before returning in maps__find_by_nametools/perf/tests/vmlinux-kallsyms.c tools/perf/util/machine.c tools/perf/util/maps.c +3 more+26 -160 replies
28/50perf maps: Get map before returning in maps__find_next_entrytools/perf/util/machine.c tools/perf/util/maps.c+4 -20 replies
29/50perf maps: Hide maps internalstools/perf/tests/thread-maps-share.c tools/perf/util/callchain.c tools/perf/util/maps.c +5 more+123 -1010 replies
30/50perf maps: Locking tidy up of nr_mapstools/perf/util/machine.c tools/perf/util/maps.c tools/perf/util/maps.h+11 -50 replies
31/50perf dso: Reorder variables to save space in struct dsotools/perf/util/dso.h+42 -420 replies
32/50perf report: Sort child tasks by tidtools/perf/builtin-report.c tools/perf/util/machine.c tools/perf/util/machine.h+155 -880 replies
33/50perf trace: Ignore thread hashing in summarytools/perf/builtin-trace.c tools/perf/util/rb_resort.h+23 -230 replies
34/50perf machine: Move fprintf to for_each loop and a callbacktools/perf/util/machine.c+27 -160 replies
35/50perf threads: Move threads to its own filestools/perf/util/Build tools/perf/util/bpf_lock_contention.c tools/perf/util/machine.c +5 more+325 -2780 replies
36/50perf threads: Switch from rbtree to hashmaptools/perf/util/threads.c tools/perf/util/threads.h+47 -1050 replies
37/50perf threads: Reduce table size from 256 to 8tools/perf/util/threads.h+1 -10 replies
38/50perf dsos: Attempt to better abstract dsos internalstools/perf/builtin-inject.c tools/perf/builtin-record.c tools/perf/util/build-id.c +8 more+97 -860 replies
39/50perf dsos: Tidy reference counting and lockingtools/perf/util/dsos.c tools/perf/util/dsos.h tools/perf/util/machine.c +2 more+97 -990 replies
40/50perf dsos: Add dsos__for_each_dsotools/perf/builtin-inject.c tools/perf/util/build-id.c tools/perf/util/dsos.c +2 more+100 -650 replies
41/50perf dso: Move dso functions out of dsostools/perf/util/dso.c tools/perf/util/dso.h tools/perf/util/dsos.c+65 -610 replies
42/50perf dsos: Switch more loops to dsos__for_each_dsotools/perf/util/build-id.c tools/perf/util/dsos.c tools/perf/util/dsos.h +1 more+174 -1020 replies
43/50perf dsos: Switch backing storage to array from rbtree/listtools/perf/util/dso.c tools/perf/util/dso.h tools/perf/util/dsos.c +1 more+177 -1090 replies
44/50perf dsos: Remove __dsos__addnewtools/perf/util/dsos.c tools/perf/util/dsos.h+0 -60 replies
45/50perf dsos: Remove __dsos__findnew_link_by_longname_idtools/perf/util/dsos.c tools/perf/util/dsos.h+10 -470 replies
46/50perf dsos: Switch hand code to bsearchtools/perf/util/dsos.c+27 -190 replies
47/50perf dso: Add reference count checking and accessor functionstools/perf/builtin-annotate.c tools/perf/builtin-buildid-cache.c tools/perf/builtin-buildid-list.c +48 more+1126 -7070 replies
48/50perf dso: Reference counting related fixestools/perf/util/machine.c tools/perf/util/map.c tools/perf/util/symbol-elf.c+21 -220 replies
49/50perf dso: Use container_of to avoid a pointer in dso_datatools/perf/tests/dso-data.c tools/perf/util/dso.c tools/perf/util/dso.h+46 -320 replies
50/50perf env: Avoid recursively taking env->bpf_progs.locktools/perf/util/bpf-event.c tools/perf/util/bpf-event.h tools/perf/util/env.c +2 more+51 -340 replies

Reviews trailers per patch

16 of 50 patches have a Reviewed-by or Acked-by.

PatchNamhyung Kim
1/50
2/50A-b
3/50
4/50A-b
5/50A-b
6/50A-b
7/50A-b
8/50A-b
9/50A-b
10/50A-b
11/50A-b
12/50A-b
13/50A-b
14/50A-b
15/50A-b
16/50A-b
17/50A-b
18/50
19/50
20/50A-b
21/50
22/50
23/50
24/50
25/50
26/50
27/50
28/50
29/50
30/50
31/50
32/50
33/50
34/50
35/50
36/50
37/50
38/50
39/50
40/50
41/50
42/50
43/50
44/50
45/50
46/50
47/50
48/50
49/50
50/50

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v5 thread

Cover letter [PATCH v5 00/50] Improvements to memory use

Fix memory leaks detected by address/leak sanitizer affecting LBR call-graphs, perf mem and BPF offcpu.

Make branch_type_stat in callchain_list optional as it is large and not always necessary - in particular it isn't used by perf top.

Make the allocations of zstd streams, kernel symbols and event copies lazier in order to save memory in cases like perf record.

Handle the thread exit event and have it remove the thread from the threads set in machine. Don't do this for perf report as it causes a regression for task lists, which assume threads are never removed from the machine's set, and offcpu events, that may sythensize samples for threads that have exited.

Avoid using 8kb buffers for filename__read_str which is excessive for reading CPU maps. Add io_dir as an allocation free readdir replacement, opendir allocating 32kb by default and the code uses it recursively.

Shrink perf map using a two value byte to replace two function pointers. Modify the implementation of maps to not use an rbtree as the container for maps, instead use a sorted array. Improve locking and reference counting issues.

Similar to maps separate out and reimplement threads to use a hashmap for lower memory consumption and faster look up. The fixes a regression in memory usage where reference count checking switched to using non-invasive tree nodes. Reduce its default size by 32 times and improve locking discipline. Also, fix regressions where tids had become unordered to make perf report --tasks and
perf trace --summary output easier to read.

Better encapsulate the dsos abstraction. Remove the linked list and rbtree used for faster iteration and log(n) lookup to a sorted array for similar performance but half the memory usage per dso. Improve reference counting and locking discipline, adding reference count checking to dso. Experimented with, but abandoned, a hashmap implementation due to the need for extra storage and the keys not being stable.

The overall effect is to reduce memory consumption significantly for perf top - with call graphs enabled running longer before 1GB of memory is consumed. For a perf record of 'true', the memory consumption goes from 39912kb max resident to 20096kb max resident - nearly halved. perf inject with -b of a system wide perf record of 'true' reduces the max resident by roughly 4.5% (3.4% in v4 due to branch_type_stat changes being merged). This is while improving correctness with locking discipline and reference count checking.

Patch organization (v5):
- 50 patches is a lot, the patches aren't divided as they merge conflict and later patches, for example in dsos, rely on the changes and fixes to maps.
- the dso reference count checking patch is larger due to switch use of dso to be by accessors, to encapsulate the reference count checker macros. The reference count checking changes within this largely mechanical change amount to a few lines and so weren't separated.
- the first patch contains a build fix if the rwsem error checking is enabled missed from v3.
- the next patches are an assortment of memory size fixes.
- the next patches are the refactoring of maps.
- the next patches are the refactoring of threads.
- the next patches are the refactoring of dsos.
- finally reference count checking is added to dso and some lock/reference count issues are resolved. This is done after changing the data structures, for example, as the single pointer on an array is easier to add reference count checking to compared to the 5 previous pointers.

v5: 3 patches were merged. 2nd patch addressed feedback from
[email protected] and Guilherme Amadio <[email protected]>. 4th patch rename function to getdelim as suggested by [email protected]. 5 patch adds the missing sysfs mountpoint as suggested by [email protected]. 49th patch fix a missed put in the dso_data tests.
v4: Rebased as 11 changes moved to perf-tools-next. Address comments
from v3 such as error checking on zstd streams. Improve the dsos/dso in ways similar to threads and maps, with the addition of reference count checking on dso.
v3: Additional memory/speed improvements, in particular for maps and
threads. Address review comments from [email protected] and [email protected].
v2: Add additional memory fixes on top of initial LBR and rc check
fixes.

Ian Rogers (50):
perf comm: Use regular mutex
libperf: Lazily allocate/size mmap event copy
perf mmap: Lazily initialize zstd streams
tools api fs: Switch filename__read_str to use io.h
tools api fs: Avoid reading whole file for a 1 byte bool tools lib api: Add io_dir an allocation free readdir alternative perf maps: Switch modules tree walk to io_dir__readdir perf record: Be lazier in allocating lost samples buffer perf pmu: Switch to io_dir__readdir
perf header: Switch mem topology to io_dir__readdir
perf events: Remove scandir in thread synthesis
perf map: Simplify map_ip/unmap_ip and make map size smaller perf maps: Move symbol maps functions to maps.c
perf thread: Add missing RC_CHK_EQUAL
perf maps: Add maps__for_each_map to call a function on each entry perf maps: Add remove maps function to remove a map based on callback perf debug: Expose debug file
perf maps: Refactor maps__fixup_overlappings
perf maps: Do simple merge if given map doesn't overlap perf maps: Rename clone to copy from
perf maps: Add maps__load_first
perf maps: Add find next entry to give entry after the given map perf maps: Reduce scope of map_rb_node and maps internals perf maps: Fix up overlaps during fixup_end
perf maps: Switch from rbtree to lazily sorted array for addresses perf maps: Get map before returning in maps__find
perf maps: Get map before returning in maps__find_by_name perf maps: Get map before returning in maps__find_next_entry perf maps: Hide maps internals
perf maps: Locking tidy up of nr_maps
perf dso: Reorder variables to save space in struct dso perf report: Sort child tasks by tid
perf trace: Ignore thread hashing in summary
perf machine: Move fprintf to for_each loop and a callback perf threads: Move threads to its own files
perf threads: Switch from rbtree to hashmap
perf threads: Reduce table size from 256 to 8
perf dsos: Attempt to better abstract dsos internals
perf dsos: Tidy reference counting and locking
perf dsos: Add dsos__for_each_dso
perf dso: Move dso functions out of dsos
perf dsos: Switch more loops to dsos__for_each_dso
perf dsos: Switch backing storage to array from rbtree/list perf dsos: Remove __dsos__addnew
perf dsos: Remove __dsos__findnew_link_by_longname_id
perf dsos: Switch hand code to bsearch
perf dso: Add reference count checking and accessor functions perf dso: Reference counting related fixes
perf dso: Use container_of to avoid a pointer in dso_data perf env: Avoid recursively taking env->bpf_progs.lock

 tools/lib/api/Makefile                        |    2 +-
 tools/lib/api/fs/fs.c                         |   80 +-
 tools/lib/api/io.h                            |   11 +-
 tools/lib/api/io_dir.h                        |   75 +
 tools/lib/perf/include/internal/mmap.h        |    3 +-
 tools/lib/perf/mmap.c                         |   21 +-
 tools/perf/arch/x86/tests/dwarf-unwind.c      |    1 +
 tools/perf/arch/x86/util/event.c              |  103 +-
 tools/perf/builtin-annotate.c                 |    6 +-
 tools/perf/builtin-buildid-cache.c            |    2 +-
 tools/perf/builtin-buildid-list.c             |   18 +-
 tools/perf/builtin-inject.c                   |   96 +-
 tools/perf/builtin-kallsyms.c                 |    2 +-
 tools/perf/builtin-mem.c                      |    4 +-
 tools/perf/builtin-record.c                   |   57 +-
 tools/perf/builtin-report.c                   |  243 ++--
 tools/perf/builtin-script.c                   |    8 +-
 tools/perf/builtin-top.c                      |    4 +-
 tools/perf/builtin-trace.c                    |   41 +-
 tools/perf/tests/code-reading.c               |    8 +-
 tools/perf/tests/dso-data.c                   |   67 +-
 tools/perf/tests/hists_common.c               |    6 +-
 tools/perf/tests/hists_cumulate.c             |    4 +-
 tools/perf/tests/hists_output.c               |    2 +-
 tools/perf/tests/maps.c                       |   64 +-
 tools/perf/tests/symbols.c                    |    2 +-
 tools/perf/tests/thread-maps-share.c          |    8 +-
 tools/perf/tests/vmlinux-kallsyms.c           |  181 +--
 tools/perf/ui/browsers/annotate.c             |    6 +-
 tools/perf/ui/browsers/hists.c                |    8 +-
 tools/perf/ui/browsers/map.c                  |    4 +-
 tools/perf/util/Build                         |    1 +
 tools/perf/util/annotate.c                    |   44 +-
 tools/perf/util/auxtrace.c                    |    2 +-
 tools/perf/util/block-info.c                  |    2 +-
 tools/perf/util/bpf-event.c                   |   17 +-
 tools/perf/util/bpf-event.h                   |   12 +-
 tools/perf/util/bpf_lock_contention.c         |   10 +-
 tools/perf/util/build-id.c                    |  136 +-
 tools/perf/util/build-id.h                    |    2 -
 tools/perf/util/callchain.c                   |    4 +-
 tools/perf/util/comm.c                        |   10 +-
 tools/perf/util/compress.h                    |    6 +-
 tools/perf/util/data-convert-json.c           |    2 +-
 tools/perf/util/db-export.c                   |    6 +-
 tools/perf/util/debug.c                       |   22 +-
 tools/perf/util/debug.h                       |    1 +
 tools/perf/util/dlfilter.c                    |   12 +-
 tools/perf/util/dso.c                         |  468 ++++---
 tools/perf/util/dso.h                         |  544 ++++++--
 tools/perf/util/dsos.c                        |  529 ++++---
 tools/perf/util/dsos.h                        |   40 +-
 tools/perf/util/env.c                         |   53 +-
 tools/perf/util/env.h                         |    4 +
 tools/perf/util/event.c                       |   12 +-
 tools/perf/util/header.c                      |   47 +-
 tools/perf/util/hist.c                        |    4 +-
 tools/perf/util/intel-pt.c                    |   22 +-
 tools/perf/util/machine.c                     |  652 +++------
 tools/perf/util/machine.h                     |   32 +-
 tools/perf/util/map.c                         |   93 +-
 tools/perf/util/map.h                         |   83 +-
 tools/perf/util/maps.c                        | 1239 +++++++++++++----
 tools/perf/util/maps.h                        |   95 +-
 tools/perf/util/mmap.c                        |    5 +-
 tools/perf/util/mmap.h                        |    1 -
 tools/perf/util/pmu.c                         |   48 +-
 tools/perf/util/pmus.c                        |   30 +-
 tools/perf/util/probe-event.c                 |   62 +-
 tools/perf/util/rb_resort.h                   |    5 -
 .../scripting-engines/trace-event-python.c    |   21 +-
 tools/perf/util/session.c                     |   21 +
 tools/perf/util/session.h                     |    2 +
 tools/perf/util/sort.c                        |   19 +-
 tools/perf/util/srcline.c                     |   65 +-
 tools/perf/util/symbol-elf.c                  |  138 +-
 tools/perf/util/symbol.c                      |  521 ++-----
 tools/perf/util/symbol.h                      |    1 -
 tools/perf/util/symbol_fprintf.c              |    4 +-
 tools/perf/util/synthetic-events.c            |  156 ++-
 tools/perf/util/thread.c                      |   48 +-
 tools/perf/util/thread.h                      |    6 -
 tools/perf/util/threads.c                     |  186 +++
 tools/perf/util/threads.h                     |   35 +
 tools/perf/util/unwind-libunwind-local.c      |   50 +-
 tools/perf/util/unwind-libunwind.c            |    9 +-
 tools/perf/util/vdso.c                        |   89 +-
 tools/perf/util/zstd.c                        |   63 +-
 88 files changed, 4101 insertions(+), 2827 deletions(-)
 create mode 100644 tools/lib/api/io_dir.h
 create mode 100644 tools/perf/util/threads.c
 create mode 100644 tools/perf/util/threads.h
Signature · 1 line
2.43.0.rc1.413.gea7ed67945-goog