Skip to content

perf tools: Speed-up perf report by using multi thread (v4)

Namhyung Kim lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest40 patches 42 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/40perf tools: Use a software dummy event to track task/mmap eventstools/perf/util/evlist.c tools/perf/util/evlist.h tools/perf/util/evsel.h+46 -00 replies
2/40perf tools: Add rm_rf() utility functiontools/perf/util/util.c tools/perf/util/util.h+44 -00 replies
3/40perf tools: Introduce copyfile_offset() functiontools/perf/util/util.c tools/perf/util/util.h+30 -92 replies
4/40perf tools: Create separate mmap for dummy tracking eventtools/perf/builtin-record.c tools/perf/util/evlist.c tools/perf/util/evlist.h+123 -292 replies
5/40perf tools: Introduce perf_evlist__mmap_track()tools/perf/builtin-record.c tools/perf/util/evlist.c tools/perf/util/evlist.h+10 -72 replies
6/40perf tools: Add HEADER_DATA_INDEX featuretools/perf/builtin-record.c tools/perf/util/header.c tools/perf/util/header.h+67 -02 replies
7/40perf tools: Handle indexed data file properlytools/perf/perf.c tools/perf/perf.h tools/perf/util/session.c+49 -92 replies
8/40perf record: Add --index option for building index tabletools/perf/Documentation/perf-record.txt tools/perf/builtin-record.c tools/perf/perf.h +2 more+166 -140 replies
9/40perf report: Skip dummy tracking eventtools/perf/builtin-report.c tools/perf/ui/browsers/hists.c tools/perf/ui/gtk/hists.c+30 -60 replies
10/40perf tools: Introduce thread__comm(_str)_by_time() helperstools/perf/util/thread.c tools/perf/util/thread.h+34 -10 replies
11/40perf tools: Add a test case for thread comm handlingtools/perf/tests/Build tools/perf/tests/builtin-test.c tools/perf/tests/perf-targz-src-pkg +2 more+53 -212 replies
12/40perf tools: Use thread__comm_by_time() when adding hist entriestools/perf/builtin-annotate.c tools/perf/builtin-diff.c tools/perf/tests/hists_link.c +2 more+20 -180 replies
13/40perf tools: Convert dead thread list into rbtreetools/perf/util/machine.c tools/perf/util/machine.h tools/perf/util/thread.c +1 more+96 -182 replies
14/40perf tools: Introduce machine__find*_thread_by_time()tools/perf/tests/dwarf-unwind.c tools/perf/tests/hists_common.c tools/perf/tests/hists_link.c +5 more+148 -123 replies
15/40perf tools: Add a test case for timed thread handlingtools/perf/tests/Build tools/perf/tests/builtin-test.c tools/perf/tests/tests.h +1 more+185 -00 replies
16/40perf tools: Reducing arguments of hist_entry_iter__add()tools/perf/builtin-report.c tools/perf/builtin-top.c tools/perf/tests/hists_cumulate.c +4 more+22 -194 replies
17/40perf tools: Maintain map groups list in a leader threadtools/perf/tests/thread-mg-share.c tools/perf/util/event.c tools/perf/util/machine.c +4 more+124 -50 replies
18/40perf tools: Introduce thread__find_addr_location_by_time() and friendstools/perf/util/event.c tools/perf/util/machine.c tools/perf/util/session.h +2 more+106 -190 replies
19/40perf callchain: Use thread__find_addr_location_by_time() and friendstools/perf/util/machine.c tools/perf/util/scripting-engines/trace-event-python.c tools/perf/util/session.h +2 more+39 -300 replies
20/40perf tools: Add a test case for timed map groups handlingtools/perf/tests/Build tools/perf/tests/builtin-test.c tools/perf/tests/tests.h +1 more+99 -00 replies
21/40perf tools: Save timestamp of a map creationtools/perf/util/dso.c tools/perf/util/machine.c tools/perf/util/machine.h +5 more+35 -260 replies
22/40perf tools: Introduce map_groups__{insert,find}_by_time()tools/perf/util/map.c tools/perf/util/map.h+77 -00 replies
23/40perf tools: Use map_groups__find_addr_by_time()tools/perf/util/event.c tools/perf/util/thread.c+77 -120 replies
24/40perf tools: Add testcase for managing maps with timetools/perf/tests/Build tools/perf/tests/builtin-test.c tools/perf/tests/tests.h +1 more+96 -00 replies
25/40perf tools: Protect dso symbol loading using a mutextools/perf/util/dso.c tools/perf/util/dso.h tools/perf/util/symbol.c+27 -100 replies
26/40perf tools: Protect dso cache tree using dso->locktools/perf/util/dso.c+27 -70 replies
27/40perf tools: Protect dso cache fd with a mutextools/perf/util/dso.c+72 -260 replies
28/40perf callchain: Maintain libunwind's address space in map_groupstools/perf/tests/dwarf-unwind.c tools/perf/util/map.c tools/perf/util/map.h +3 more+27 -330 replies
29/40perf tools: Add dso__data_get/put_fd()tools/perf/util/dso.c tools/perf/util/dso.h tools/perf/util/unwind-libunwind.c+64 -270 replies
30/40perf session: Pass struct events stats to event processing functionstools/perf/util/session.c+45 -260 replies
31/40perf hists: Pass hists struct to hist_entry_iter structtools/perf/builtin-report.c tools/perf/builtin-top.c tools/perf/tests/hists_cumulate.c +4 more+13 -130 replies
32/40perf tools: Move BUILD_ID_SIZE definition to perf.htools/perf/perf.h tools/perf/util/build-id.h tools/perf/util/dso.h +1 more+2 -30 replies
33/40perf session: Separate struct machines from sessiontools/perf/builtin-annotate.c tools/perf/builtin-kmem.c tools/perf/builtin-kvm.c +7 more+48 -427 replies
34/40perf report: Parallelize perf report using multi-threadtools/perf/util/hist.c tools/perf/util/hist.h tools/perf/util/session.c +2 more+231 -132 replies
35/40perf record: Synthesize COMM event for a command line workloadtools/perf/builtin-record.c tools/perf/util/event.c tools/perf/util/event.h+23 -211 replies
36/40perf tools: Fix progress ui to support multi threadtools/perf/util/hist.c tools/perf/util/hist.h tools/perf/util/session.c +1 more+66 -160 replies
37/40perf report: Add --multi-thread option and config itemtools/perf/Documentation/perf-report.txt tools/perf/builtin-report.c+59 -90 replies
38/40perf session: Handle index files generallytools/perf/util/session.c tools/perf/util/tool.h+58 -223 replies
39/40perf data: Implement 'index' subcommandtools/perf/Documentation/perf-data.txt tools/perf/builtin-data.c+372 -40 replies
40/40perf tools: Disable thread refcount due to bugtools/perf/util/thread.c+1 -13 replies

Reviews trailers per patch

None of the 40 patches has a Reviewed-by or Acked-by.

PatchNamhyung Kim
1/40
2/40
3/40
4/40
5/40
6/40
7/40
8/40
9/40
10/40
11/40
12/40
13/40
14/40
15/40
16/40
17/40
18/40
19/40
20/40
21/40
22/40
23/40
24/40
25/40
26/40
27/40
28/40
29/40
30/40
31/40
32/40
33/40
34/40
35/40
36/40
37/40
38/40
39/40
40/40Not-signed-off-by

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

Read the v1 thread

Cover letter [RFC/PATCHSET 00/40] perf tools: Speed-up perf report by using multi thread (v4)

Hello,

This patchset converts perf report to use multiple threads in order to speed up the processing on large data files. I can see a minimum ~30% of speedup with this change. The code is still experimental and contains many rough edges. But I'd like to share and give some feedbacks.

* changes in v4)
- rename to *_find(new)_by_time()
- also sort struct map by time
- use 'perf_has_index' global variable
- fix an off-by-one bug in index generation
- rebased onto the recent atomic thread refcounting
- remove missing threads tree in favor of thread rwlock

* changes in v3)
- handle header (metadata) same as sample data (at index 0)
- maintain libunwind address space in map_groups instead of thread
- use *_time API only for indexed data file
- resolve callchain with the *_time API
- use dso__data_get/put_fd() to protect access to fd
- synthesize COMM event for command line workload

* changes in v2)
- rework with single indexed data file rather than multiple files in a directory

The perf report processes (sample) events like below:

1. preprocess sample to get matching thread/dso/symbol info
2. insert it to hists rbtree (with callchain tree) based on the info
3. optionally collapse hist entries that match given sort key(s)
4. resort hist entries (by overhead) for output
5. display the hist entries

The stage 1 is a preprocessing and mostly act like a read-only operation in that it doesn't change a machine state during the sample processing. Meta events like fork, comm and mmap can change the machine/thread state but symbols can be loaded during the processing (stage 2).

The stage 2 consumes most of the time especially with callchains and
--children option is enabled. And this work can be easily patitioned
as each sample is independent to others. But the resulting hists must be combined/collapsed to a single global hists before going to further steps.

The stage 3 is optional and only needed by certain sort keys - but with stage 2 paralellized, it needs to be done always.

The stage 4 and 5 works on whole hists so must be done serially.

So my approach is like this:

Partially do stage 1 first - but only for meta events that changes machine state. To do this I add a dummy tracking event to perf record and make it collect such meta events only. They are saved as normal data and processed before sample events at perf report time.

This also requires to handle multiple sample data concurrently and to find a corresponding machine state when processing samples. On a large profiling session, many tasks were created and exited so pid might be recycled (even more than once!). To deal with it, I managed to have thread, map_groups, map and comm in time sorted. The only remaining thing is symbol loading as it's done lazily when sample requires it.

With that being done, the stage 2 can be done by multiple threads. I also save each sample data (per-cpu or per-thread) in separate files during record and then merge them into a single data file with an index table. On perf report time, each region of sample data will be processed by each thread. And symbol loading is protected by a mutex lock.

For DWARF post-unwinding, dso cache data also needs to be protected by a lock and this caused a huge contention. I made it to search the rbtree speculatively first and then, if it didn't find one, search it again under the dso lock. Please take a look at it if it's acceptable.

The patch 1-8 are to support indexing for data file. With --index option, perf record will create a intermediate directory and then save meta events and sample data to separate files. And finally it'll build an index table and concatenate the data files (and also remove the intermediate direcotry).

The patch 9-24 are to manage machine and thread state using timestamp so that it can be searched when processing samples. The patch 25-38 are to implement parallel report. And finally I implemented 'perf data index' command to build an index table for a given data file. The last patch is just to prevent segfault due to a bug in thread refcounting and not inteneded to be merged.

This patchset didn't change perf record to use multi-thread. But I think it can be easily done later if needed.

Note that output has a slight difference to original version when compared using indexed data file. But they're mostly unresolved symbols for callchains.

Here is the result:

This is just elapsed time measured by 'perf stat -r 5'. Note that overall performance in v4 slows down than v3 by ~30% possibly due to atomic thread refcounting and rwlock acquisition.

The data file was recorded during kernel build with fp callchain and size is 2.1GB. The machine has 6 core with hyper-threading enabled and I got a similar result on my laptop too.

perf report --children --no-children + --call-graph none
------------- ------------- -------------------

 current           368.290435503  122.786548856      51.269541556
 with index        339.618194440  101.383647394      35.880577271
 + --multi-thread  219.711277265   70.448753814      29.324704738

This result is with 7.7GB data file using libunwind for callchain.

perf report --children --no-children + --call-graph none
------------- ------------- -------------------

 current           213.799598463  155.297229724       6.235268506
 with index        216.429553998  156.337434110       4.771545122
 + --multi-thread   54.892308166   35.090129366       3.415513290

This result is with same file but using libdw for callchain unwind.

perf report --children --no-children + --call-graph none
------------- ------------- -------------------

 current           401.405331379  399.054998021       6.015400857
 with index        383.643272700  372.205715392       5.050819167
 + --multi-thread  176.419091057  139.776442715       3.356360850

On my archlinux system, callchain unwind using libdw is much slower than libunwind. I'm using elfutils version 0.161. Also I don't know why --children takes less time than --no-children. Anyway we can see the --multi-thread performance is much better for each case.

This patch is based on acme/perf/core - commit 70923bd26c73 ("perf
tools: Make flex/bison calls honour V=1"). I've just found that it's updated to apply atomic refcounting to other structs. But as it requires another round of rebase, fix and testing cycles on my side, I'd like to send out the current version before doing that.

You can get it from 'perf/threaded-v4' branch on my tree at:

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Please take a look and play with it. Any comments are welcome! :)

Thanks,
Namhyung

Namhyung Kim (40):
perf tools: Use a software dummy event to track task/mmap events perf tools: Add rm_rf() utility function
perf tools: Introduce copyfile_offset() function
perf tools: Create separate mmap for dummy tracking event perf tools: Introduce perf_evlist__mmap_track()
perf tools: Add HEADER_DATA_INDEX feature
perf tools: Handle indexed data file properly
perf record: Add --index option for building index table perf report: Skip dummy tracking event
perf tools: Introduce thread__comm(_str)_by_time() helpers perf tools: Add a test case for thread comm handling
perf tools: Use thread__comm_by_time() when adding hist entries perf tools: Convert dead thread list into rbtree
perf tools: Introduce machine__find*_thread_by_time()
perf tools: Add a test case for timed thread handling
perf tools: Reducing arguments of hist_entry_iter__add() perf tools: Maintain map groups list in a leader thread perf tools: Introduce thread__find_addr_location_by_time() and friends perf callchain: Use thread__find_addr_location_by_time() and friends perf tools: Add a test case for timed map groups handling perf tools: Save timestamp of a map creation
perf tools: Introduce map_groups__{insert,find}_by_time() perf tools: Use map_groups__find_addr_by_time()
perf tools: Add testcase for managing maps with time
perf tools: Protect dso symbol loading using a mutex
perf tools: Protect dso cache tree using dso->lock
perf tools: Protect dso cache fd with a mutex
perf callchain: Maintain libunwind's address space in map_groups perf tools: Add dso__data_get/put_fd()
perf session: Pass struct events stats to event processing functions perf hists: Pass hists struct to hist_entry_iter struct perf tools: Move BUILD_ID_SIZE definition to perf.h
perf session: Separate struct machines from session
perf report: Parallelize perf report using multi-thread perf record: Synthesize COMM event for a command line workload perf tools: Fix progress ui to support multi thread
perf report: Add --multi-thread option and config item perf session: Handle index files generally
perf data: Implement 'index' subcommand
perf tools: Disable thread refcount due to bug

 tools/perf/Documentation/perf-data.txt             |  25 +-
 tools/perf/Documentation/perf-record.txt           |   4 +
 tools/perf/Documentation/perf-report.txt           |   2 +
 tools/perf/builtin-annotate.c                      |   7 +-
 tools/perf/builtin-data.c                          | 351 +++++++++++++++++-
 tools/perf/builtin-diff.c                          |   8 +-
 tools/perf/builtin-kmem.c                          |  10 +-
 tools/perf/builtin-kvm.c                           |   2 +-
 tools/perf/builtin-record.c                        | 202 ++++++++++-
 tools/perf/builtin-report.c                        |  81 ++++-
 tools/perf/builtin-top.c                           |  16 +-
 tools/perf/builtin-trace.c                         |   2 +-
 tools/perf/perf.c                                  |   1 +
 tools/perf/perf.h                                  |   4 +
 tools/perf/tests/Build                             |   4 +
 tools/perf/tests/builtin-test.c                    |  16 +
 tools/perf/tests/dwarf-unwind.c                    |  12 +-
 tools/perf/tests/hists_common.c                    |   3 +-
 tools/perf/tests/hists_cumulate.c                  |   7 +-
 tools/perf/tests/hists_filter.c                    |   5 +-
 tools/perf/tests/hists_link.c                      |   6 +-
 tools/perf/tests/hists_output.c                    |   7 +-
 tools/perf/tests/perf-targz-src-pkg                |  21 --
 tools/perf/tests/tests.h                           |   4 +
 tools/perf/tests/thread-comm.c                     |  47 +++
 tools/perf/tests/thread-lookup-time.c              | 179 ++++++++++
 tools/perf/tests/thread-map-time.c                 |  90 +++++
 tools/perf/tests/thread-mg-share.c                 |   7 +-
 tools/perf/tests/thread-mg-time.c                  |  93 +++++
 tools/perf/ui/browsers/hists.c                     |  30 +-
 tools/perf/ui/gtk/hists.c                          |   3 +
 tools/perf/util/build-id.c                         |  16 +-
 tools/perf/util/build-id.h                         |   2 -
 tools/perf/util/dso.c                              | 174 ++++++---
 tools/perf/util/dso.h                              |  11 +-
 tools/perf/util/event.c                            | 142 +++++++-
 tools/perf/util/event.h                            |   6 +-
 tools/perf/util/evlist.c                           | 168 +++++++--
 tools/perf/util/evlist.h                           |  14 +-
 tools/perf/util/evsel.h                            |  15 +
 tools/perf/util/header.c                           |  64 ++++
 tools/perf/util/header.h                           |   3 +
 tools/perf/util/hist.c                             | 123 ++++---
 tools/perf/util/hist.h                             |   8 +-
 tools/perf/util/machine.c                          | 291 ++++++++++++---
 tools/perf/util/machine.h                          |  14 +-
 tools/perf/util/map.c                              |  72 +++-
 tools/perf/util/map.h                              |  37 +-
 tools/perf/util/probe-event.c                      |   2 +-
 .../util/scripting-engines/trace-event-python.c    |   4 +-
 tools/perf/util/session.c                          | 396 ++++++++++++++++++---
 tools/perf/util/session.h                          |   7 +-
 tools/perf/util/symbol-elf.c                       |   2 +-
 tools/perf/util/symbol.c                           |  38 +-
 tools/perf/util/thread.c                           | 207 ++++++++++-
 tools/perf/util/thread.h                           |  28 +-
 tools/perf/util/tool.h                             |  14 +
 tools/perf/util/unwind-libdw.c                     |  12 +-
 tools/perf/util/unwind-libunwind.c                 |  93 ++---
 tools/perf/util/unwind.h                           |  15 +-
 tools/perf/util/util.c                             |  81 ++++-
 tools/perf/util/util.h                             |   2 +
 62 files changed, 2856 insertions(+), 454 deletions(-)
 delete mode 100755 tools/perf/tests/perf-targz-src-pkg
 create mode 100644 tools/perf/tests/thread-comm.c
 create mode 100644 tools/perf/tests/thread-lookup-time.c
 create mode 100644 tools/perf/tests/thread-map-time.c
 create mode 100644 tools/perf/tests/thread-mg-time.c
Signature · 1 line
2.4.0