linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 00/50] Improvements to memory use
@ 2023-11-27 22:08 Ian Rogers
  2023-11-27 22:08 ` [PATCH v5 01/50] perf comm: Use regular mutex Ian Rogers
                   ` (50 more replies)
  0 siblings, 51 replies; 95+ messages in thread
From: Ian Rogers @ 2023-11-27 22:08 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Ian Rogers, Adrian Hunter, Nick Terrell, Kan Liang, Andi Kleen,
	Kajol Jain, Athira Rajeev, Huacai Chen, Masami Hiramatsu,
	Vincent Whitchurch, Steinar H. Gunderson, Liam Howlett,
	Miguel Ojeda, Colin Ian King, Dmitrii Dolgov, Yang Jihong,
	Ming Wang, James Clark, K Prateek Nayak, Sean Christopherson,
	Leo Yan, Ravi Bangoria, German Gomez, Changbin Du, Paolo Bonzini,
	Li Dong, Sandipan Das, liuwenyu, linux-kernel, linux-perf-users,
	Guilherme Amadio

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
    namhyung@kernel.org and Guilherme Amadio <amadio@gentoo.org>. 4th
    patch rename function to getdelim as suggested by
    namhyung@kernel.org. 5 patch adds the missing sysfs mountpoint as
    suggested by namhyung@kernel.org. 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 namhyung@kernel.org and
    adrian.hunter@intel.com.
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

-- 
2.43.0.rc1.413.gea7ed67945-goog


^ permalink raw reply	[flat|nested] 95+ messages in thread

end of thread, other threads:[~2024-02-06  3:04 UTC | newest]

Thread overview: 95+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-27 22:08 [PATCH v5 00/50] Improvements to memory use Ian Rogers
2023-11-27 22:08 ` [PATCH v5 01/50] perf comm: Use regular mutex Ian Rogers
2023-11-30  0:55   ` Namhyung Kim
2023-11-30 18:27     ` Ian Rogers
2023-12-02 23:54       ` Namhyung Kim
2023-12-07  0:05         ` Ian Rogers
2024-02-06  3:04           ` Ian Rogers
2023-11-27 22:08 ` [PATCH v5 02/50] libperf: Lazily allocate/size mmap event copy Ian Rogers
2023-11-30  1:25   ` Namhyung Kim
2023-11-30 13:15   ` Arnaldo Carvalho de Melo
2023-11-30 14:19     ` Arnaldo Carvalho de Melo
2023-11-30 17:17       ` Arnaldo Carvalho de Melo
2023-11-27 22:08 ` [PATCH v5 03/50] perf mmap: Lazily initialize zstd streams Ian Rogers
2023-11-30  1:28   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 04/50] tools api fs: Switch filename__read_str to use io.h Ian Rogers
2023-11-30  1:36   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 05/50] tools api fs: Avoid reading whole file for a 1 byte bool Ian Rogers
2023-11-30  1:42   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 06/50] tools lib api: Add io_dir an allocation free readdir alternative Ian Rogers
2023-11-30  1:49   ` Namhyung Kim
2023-11-30 17:21   ` Arnaldo Carvalho de Melo
2023-11-30 17:56     ` Ian Rogers
2023-11-30 21:25       ` Arnaldo Carvalho de Melo
2023-12-07  5:13         ` Ian Rogers
2023-11-27 22:08 ` [PATCH v5 07/50] perf maps: Switch modules tree walk to io_dir__readdir Ian Rogers
2023-11-30  1:59   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 08/50] perf record: Be lazier in allocating lost samples buffer Ian Rogers
2023-11-30  2:09   ` Namhyung Kim
2023-11-30 18:29     ` Ian Rogers
2023-12-02 23:56       ` Namhyung Kim
2023-12-05 15:54         ` Arnaldo Carvalho de Melo
2023-11-27 22:08 ` [PATCH v5 09/50] perf pmu: Switch to io_dir__readdir Ian Rogers
2023-11-30  2:16   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 10/50] perf header: Switch mem topology " Ian Rogers
2023-11-30  4:17   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 11/50] perf events: Remove scandir in thread synthesis Ian Rogers
2023-11-30  4:22   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 12/50] perf map: Simplify map_ip/unmap_ip and make map size smaller Ian Rogers
2023-12-04 23:39   ` Namhyung Kim
2023-12-06 13:49     ` Arnaldo Carvalho de Melo
2023-12-06 16:19       ` Ian Rogers
2023-11-27 22:08 ` [PATCH v5 13/50] perf maps: Move symbol maps functions to maps.c Ian Rogers
2023-12-04 23:40   ` Namhyung Kim
2023-12-06 13:50     ` Arnaldo Carvalho de Melo
2023-11-27 22:08 ` [PATCH v5 14/50] perf thread: Add missing RC_CHK_EQUAL Ian Rogers
2023-12-04 23:41   ` Namhyung Kim
2023-12-06 13:51     ` Arnaldo Carvalho de Melo
2023-11-27 22:08 ` [PATCH v5 15/50] perf maps: Add maps__for_each_map to call a function on each entry Ian Rogers
2023-12-04 23:46   ` Namhyung Kim
2023-12-06 13:53     ` Arnaldo Carvalho de Melo
2023-11-27 22:08 ` [PATCH v5 16/50] perf maps: Add remove maps function to remove a map based on callback Ian Rogers
2023-12-04 23:49   ` Namhyung Kim
2023-12-06 23:28     ` Ian Rogers
2023-11-27 22:08 ` [PATCH v5 17/50] perf debug: Expose debug file Ian Rogers
2023-12-04 23:53   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 18/50] perf maps: Refactor maps__fixup_overlappings Ian Rogers
2023-12-04 23:59   ` Namhyung Kim
2023-12-06 23:39     ` Ian Rogers
2023-11-27 22:08 ` [PATCH v5 19/50] perf maps: Do simple merge if given map doesn't overlap Ian Rogers
2023-12-05  0:06   ` Namhyung Kim
2023-12-06 23:51     ` Ian Rogers
2023-11-27 22:08 ` [PATCH v5 20/50] perf maps: Rename clone to copy from Ian Rogers
2023-12-05  0:07   ` Namhyung Kim
2023-11-27 22:08 ` [PATCH v5 21/50] perf maps: Add maps__load_first Ian Rogers
2023-11-27 22:08 ` [PATCH v5 22/50] perf maps: Add find next entry to give entry after the given map Ian Rogers
2023-11-27 22:08 ` [PATCH v5 23/50] perf maps: Reduce scope of map_rb_node and maps internals Ian Rogers
2023-11-27 22:08 ` [PATCH v5 24/50] perf maps: Fix up overlaps during fixup_end Ian Rogers
2023-11-27 22:08 ` [PATCH v5 25/50] perf maps: Switch from rbtree to lazily sorted array for addresses Ian Rogers
2023-11-27 22:08 ` [PATCH v5 26/50] perf maps: Get map before returning in maps__find Ian Rogers
2023-11-27 22:08 ` [PATCH v5 27/50] perf maps: Get map before returning in maps__find_by_name Ian Rogers
2023-11-27 22:08 ` [PATCH v5 28/50] perf maps: Get map before returning in maps__find_next_entry Ian Rogers
2023-11-27 22:08 ` [PATCH v5 29/50] perf maps: Hide maps internals Ian Rogers
2023-11-27 22:08 ` [PATCH v5 30/50] perf maps: Locking tidy up of nr_maps Ian Rogers
2023-11-27 22:08 ` [PATCH v5 31/50] perf dso: Reorder variables to save space in struct dso Ian Rogers
2023-11-27 22:08 ` [PATCH v5 32/50] perf report: Sort child tasks by tid Ian Rogers
2023-11-27 22:08 ` [PATCH v5 33/50] perf trace: Ignore thread hashing in summary Ian Rogers
2023-11-27 22:08 ` [PATCH v5 34/50] perf machine: Move fprintf to for_each loop and a callback Ian Rogers
2023-11-27 22:08 ` [PATCH v5 35/50] perf threads: Move threads to its own files Ian Rogers
2023-11-27 22:08 ` [PATCH v5 36/50] perf threads: Switch from rbtree to hashmap Ian Rogers
2023-11-27 22:08 ` [PATCH v5 37/50] perf threads: Reduce table size from 256 to 8 Ian Rogers
2023-11-27 22:08 ` [PATCH v5 38/50] perf dsos: Attempt to better abstract dsos internals Ian Rogers
2023-11-27 22:08 ` [PATCH v5 39/50] perf dsos: Tidy reference counting and locking Ian Rogers
2023-11-27 22:08 ` [PATCH v5 40/50] perf dsos: Add dsos__for_each_dso Ian Rogers
2023-11-27 22:08 ` [PATCH v5 41/50] perf dso: Move dso functions out of dsos Ian Rogers
2023-11-27 22:08 ` [PATCH v5 42/50] perf dsos: Switch more loops to dsos__for_each_dso Ian Rogers
2023-11-27 22:08 ` [PATCH v5 43/50] perf dsos: Switch backing storage to array from rbtree/list Ian Rogers
2023-11-27 22:08 ` [PATCH v5 44/50] perf dsos: Remove __dsos__addnew Ian Rogers
2023-11-27 22:08 ` [PATCH v5 45/50] perf dsos: Remove __dsos__findnew_link_by_longname_id Ian Rogers
2023-11-27 22:08 ` [PATCH v5 46/50] perf dsos: Switch hand code to bsearch Ian Rogers
2023-11-27 22:08 ` [PATCH v5 47/50] perf dso: Add reference count checking and accessor functions Ian Rogers
2023-11-27 22:09 ` [PATCH v5 48/50] perf dso: Reference counting related fixes Ian Rogers
2023-11-27 22:09 ` [PATCH v5 49/50] perf dso: Use container_of to avoid a pointer in dso_data Ian Rogers
2023-11-27 22:09 ` [PATCH v5 50/50] perf env: Avoid recursively taking env->bpf_progs.lock Ian Rogers
2023-11-30  1:16 ` [PATCH v5 00/50] Improvements to memory use Namhyung Kim
2023-12-07  0:10   ` Ian Rogers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).