Skip to content

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

Namhyung Kim lkml 1 version Applied

Status from this message: Thanks, applied after rewriting the changelog to read as:

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest42 patches 23 repliesApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/42perf tools: Support to read compressed module from build-id cachetools/perf/util/symbol-elf.c+8 -52 replies
2/42perf tools: Do not use __perf_session__process_events() directlytools/perf/builtin-record.c tools/perf/util/session.c tools/perf/util/session.h+6 -100 replies
3/42perf record: Show precise number of samplestools/perf/builtin-record.c+39 -120 replies
4/42perf header: Set header version correctlytools/perf/util/header.c+1 -10 replies
5/42perf tools: Set attr.task bit for a tracking eventtools/perf/util/evsel.c+1 -00 replies
6/42perf 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
7/42perf tools: Use perf_data_file__fd() consistentlytools/perf/builtin-inject.c tools/perf/builtin-record.c+10 -90 replies
8/42perf tools: Add rm_rf() utility functiontools/perf/util/util.c tools/perf/util/util.h+44 -01 reply
9/42perf tools: Introduce copyfile_offset() functiontools/perf/util/util.c tools/perf/util/util.h+30 -90 replies
10/42perf tools: Create separate mmap for dummy tracking eventtools/perf/builtin-record.c tools/perf/util/evlist.c tools/perf/util/evlist.h+117 -250 replies
11/42perf tools: Introduce perf_evlist__mmap_track()tools/perf/builtin-record.c tools/perf/util/evlist.c tools/perf/util/evlist.h+19 -90 replies
12/42perf tools: Add HEADER_DATA_INDEX featuretools/perf/builtin-data.c tools/perf/builtin-record.c tools/perf/util/header.c +1 more+66 -00 replies
13/42perf tools: Handle indexed data file properlytools/perf/util/session.c tools/perf/util/session.h+41 -110 replies
14/42perf record: Add --index option for building index tabletools/perf/Documentation/perf-record.txt tools/perf/builtin-data.c tools/perf/builtin-record.c +2 more+161 -1010 replies
15/42perf report: Skip dummy tracking eventtools/perf/builtin-report.c tools/perf/ui/browsers/hists.c tools/perf/ui/gtk/hists.c+30 -60 replies
16/42perf tools: Pass session arg to perf_event__preprocess_sample()tools/perf/builtin-annotate.c tools/perf/builtin-diff.c tools/perf/builtin-mem.c +10 more+48 -270 replies
17/42perf script: Pass session arg to ->process_event callbacktools/perf/builtin-script.c tools/perf/util/db-export.c tools/perf/util/db-export.h +6 more+33 -200 replies
18/42perf tools: Introduce thread__comm_time() helperstools/perf/util/thread.c tools/perf/util/thread.h+34 -10 replies
19/42perf tools: Add a test case for thread comm handlingtools/perf/Makefile.perf tools/perf/tests/builtin-test.c tools/perf/tests/tests.h +1 more+53 -00 replies
20/42perf tools: Use thread__comm_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
21/42perf tools: Convert dead thread list into rbtreetools/perf/util/machine.c tools/perf/util/machine.h tools/perf/util/thread.c +1 more+68 -160 replies
22/42perf tools: Introduce machine__find*_thread_time()tools/perf/builtin-script.c tools/perf/tests/dwarf-unwind.c tools/perf/tests/hists_common.c +6 more+138 -150 replies
23/42perf tools: Add a test case for timed thread handlingtools/perf/Makefile.perf tools/perf/tests/builtin-test.c tools/perf/tests/tests.h +1 more+180 -00 replies
24/42perf 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+93 -40 replies
25/42perf tools: Introduce thread__find_addr_location_time() and friendstools/perf/util/event.c tools/perf/util/machine.c tools/perf/util/thread.c +3 more+146 -450 replies
26/42perf tools: Add a test case for timed map groups handlingtools/perf/Makefile.perf tools/perf/tests/builtin-test.c tools/perf/tests/tests.h +1 more+94 -00 replies
27/42perf tools: Protect dso symbol loading using a mutextools/perf/util/dso.c tools/perf/util/dso.h tools/perf/util/symbol.c+27 -102 replies
28/42perf tools: Protect dso cache tree using dso->locktools/perf/util/dso.c+27 -70 replies
29/42perf tools: Protect dso cache fd with a mutextools/perf/tests/dso-data.c tools/perf/util/dso.c+94 -474 replies
30/42perf session: Pass struct events stats to event processing functionstools/perf/util/ordered-events.c tools/perf/util/session.c tools/perf/util/session.h+49 -370 replies
31/42perf hists: Pass hists struct to hist_entry_iter functionstools/perf/builtin-report.c tools/perf/builtin-top.c tools/perf/tests/hists_cumulate.c +4 more+25 -260 replies
32/42perf 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/42perf report: Parallelize perf report using multi-threadtools/perf/util/hist.c tools/perf/util/hist.h tools/perf/util/session.c +2 more+220 -130 replies
34/42perf tools: Add missing_threads rb treetools/perf/tests/thread-lookup-time.c tools/perf/util/build-id.c tools/perf/util/machine.c +3 more+101 -560 replies
35/42perf record: Synthesize COMM event for a command line workloadtools/perf/builtin-record.c tools/perf/util/event.c tools/perf/util/event.h+26 -50 replies
36/42perf tools: Fix progress ui to support multi threadtools/perf/util/hist.c tools/perf/util/hist.h tools/perf/util/session.c +1 more+58 -160 replies
37/42perf report: Add --multi-thread option and config itemtools/perf/Documentation/perf-report.txt tools/perf/builtin-report.c tools/perf/util/session.c+61 -90 replies
38/42perf session: Handle index files generallytools/perf/util/session.c tools/perf/util/tool.h+50 -190 replies
39/42perf tools: Convert lseek + read to preadtools/perf/util/dso.c+1 -40 replies
40/42perf callchain: Save eh/debug frame offset for dwarf unwindtools/perf/util/dso.h tools/perf/util/unwind-libunwind.c+21 -113 replies
41/42perf tools: Add new perf data commandtools/perf/Documentation/perf-data.txt tools/perf/Makefile.perf tools/perf/builtin-data.c +3 more+94 -00 replies
42/42perf data: Implement 'index' subcommandtools/perf/Documentation/perf-data.txt tools/perf/builtin-data.c+382 -00 replies

Reviews trailers per patch

2 of 42 patches have a Reviewed-by or Acked-by.

PatchJiri Olsa
1/42
2/42
3/42R-b
4/42
5/42
6/42
7/42
8/42A-b
9/42
10/42
11/42
12/42
13/42
14/42
15/42
16/42
17/42
18/42
19/42
20/42
21/42
22/42
23/42
24/42
25/42
26/42
27/42
28/42
29/42
30/42
31/42
32/42
33/42
34/42
35/42
36/42
37/42
38/42
39/42
40/42
41/42
42/42

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/42] perf tools: Speed-up perf report by using multi thread (v2)

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.

The main change in this version is using single data file with an index table rather than using multiple files. It seems that single thread performance was improved by this than previous version but multi thread performance remains almost same.

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 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-4 are independent fixes and cleans. The patch 5-14 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.

The patch 15-26 are to manage machine and thread state using timestamp so that it can be searched when processing samples. The patch 27-40 are to implement parallel report. And finally I implemented 'perf data index' command to build an index table for a given data file.

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'.

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           285.708340593   94.317412961      36.707232978  
 with index        253.322717665   77.079748639      24.892021523
 + --multi-thread  174.037760271   44.717308080       8.300466711

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

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

 current           247.070444039  196.393820003       5.068489333
 with index        149.456483830  108.917644447       3.642109876
 + --multi-thread   43.990095636   28.342798882       1.829218561

I guess the speedup of indexed data file came from skipping ordered event layer.

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

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

 current           465.661321115  496.153153039       4.629841428
 with index        445.712762188  462.146612217       3.535147499
 + --multi-thread  215.264706814   29.279996335       1.938137940

On my archlinux system, callchain unwind using libdw is much slower than libunwind. I'm using elfutils version 0.160. 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.

You can get it from 'perf/threaded-v2' 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

Jiri Olsa (1):
perf tools: Add new perf data command

Namhyung Kim (41):
perf tools: Support to read compressed module from build-id cache perf tools: Do not use __perf_session__process_events() directly perf record: Show precise number of samples
perf header: Set header version correctly
perf tools: Set attr.task bit for a tracking event
perf tools: Use a software dummy event to track task/mmap events perf tools: Use perf_data_file__fd() consistently
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: Pass session arg to perf_event__preprocess_sample() perf script: Pass session arg to ->process_event callback perf tools: Introduce thread__comm_time() helpers
perf tools: Add a test case for thread comm handling
perf tools: Use thread__comm_time() when adding hist entries perf tools: Convert dead thread list into rbtree
perf tools: Introduce machine__find*_thread_time()
perf tools: Add a test case for timed thread handling perf tools: Maintain map groups list in a leader thread perf tools: Introduce thread__find_addr_location_time() and friends perf tools: Add a test case for timed map groups handling 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 session: Pass struct events stats to event processing functions perf hists: Pass hists struct to hist_entry_iter functions perf tools: Move BUILD_ID_SIZE definition to perf.h
perf report: Parallelize perf report using multi-thread perf tools: Add missing_threads rb tree
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 tools: Convert lseek + read to pread
perf callchain: Save eh/debug frame offset for dwarf unwind perf data: Implement 'index' subcommand

 tools/perf/Documentation/perf-data.txt             |  44 +++
 tools/perf/Documentation/perf-record.txt           |   4 +
 tools/perf/Documentation/perf-report.txt           |   3 +
 tools/perf/Makefile.perf                           |   4 +
 tools/perf/builtin-annotate.c                      |   8 +-
 tools/perf/builtin-data.c                          | 428 +++++++++++++++++++++
 tools/perf/builtin-diff.c                          |  21 +-
 tools/perf/builtin-inject.c                        |   5 +-
 tools/perf/builtin-mem.c                           |   6 +-
 tools/perf/builtin-record.c                        | 261 +++++++++++--
 tools/perf/builtin-report.c                        |  74 +++-
 tools/perf/builtin-script.c                        |  54 ++-
 tools/perf/builtin-timechart.c                     |  10 +-
 tools/perf/builtin-top.c                           |   7 +-
 tools/perf/builtin.h                               |   1 +
 tools/perf/command-list.txt                        |   1 +
 tools/perf/perf.c                                  |   1 +
 tools/perf/perf.h                                  |   2 +
 tools/perf/tests/builtin-test.c                    |  12 +
 tools/perf/tests/dso-data.c                        |   5 +
 tools/perf/tests/dwarf-unwind.c                    |   8 +-
 tools/perf/tests/hists_common.c                    |   3 +-
 tools/perf/tests/hists_cumulate.c                  |   6 +-
 tools/perf/tests/hists_filter.c                    |   5 +-
 tools/perf/tests/hists_link.c                      |  10 +-
 tools/perf/tests/hists_output.c                    |   6 +-
 tools/perf/tests/tests.h                           |   3 +
 tools/perf/tests/thread-comm.c                     |  47 +++
 tools/perf/tests/thread-lookup-time.c              | 180 +++++++++
 tools/perf/tests/thread-mg-share.c                 |   7 +-
 tools/perf/tests/thread-mg-time.c                  |  88 +++++
 tools/perf/ui/browsers/hists.c                     |  30 +-
 tools/perf/ui/gtk/hists.c                          |   3 +
 tools/perf/util/build-id.c                         |   9 +-
 tools/perf/util/build-id.h                         |   2 -
 tools/perf/util/db-export.c                        |   6 +-
 tools/perf/util/db-export.h                        |   4 +-
 tools/perf/util/dso.c                              | 159 +++++---
 tools/perf/util/dso.h                              |   3 +
 tools/perf/util/event.c                            | 106 ++++-
 tools/perf/util/event.h                            |  13 +-
 tools/perf/util/evlist.c                           | 161 ++++++--
 tools/perf/util/evlist.h                           |  22 +-
 tools/perf/util/evsel.c                            |   1 +
 tools/perf/util/evsel.h                            |  15 +
 tools/perf/util/header.c                           |  63 ++-
 tools/perf/util/header.h                           |   3 +
 tools/perf/util/hist.c                             | 121 ++++--
 tools/perf/util/hist.h                             |  12 +-
 tools/perf/util/machine.c                          | 258 +++++++++++--
 tools/perf/util/machine.h                          |  12 +-
 tools/perf/util/map.c                              |   1 +
 tools/perf/util/map.h                              |   2 +
 tools/perf/util/ordered-events.c                   |   4 +-
 .../perf/util/scripting-engines/trace-event-perl.c |   3 +-
 .../util/scripting-engines/trace-event-python.c    |   5 +-
 tools/perf/util/session.c                          | 356 ++++++++++++++---
 tools/perf/util/session.h                          |  11 +-
 tools/perf/util/symbol-elf.c                       |  13 +-
 tools/perf/util/symbol.c                           |  34 +-
 tools/perf/util/thread.c                           | 139 ++++++-
 tools/perf/util/thread.h                           |  28 +-
 tools/perf/util/tool.h                             |  14 +
 tools/perf/util/trace-event-scripting.c            |   3 +-
 tools/perf/util/trace-event.h                      |   3 +-
 tools/perf/util/unwind-libdw.c                     |  11 +-
 tools/perf/util/unwind-libunwind.c                 |  49 ++-
 tools/perf/util/util.c                             |  81 +++-
 tools/perf/util/util.h                             |   2 +
 69 files changed, 2662 insertions(+), 414 deletions(-)
 create mode 100644 tools/perf/Documentation/perf-data.txt
 create mode 100644 tools/perf/builtin-data.c
 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-mg-time.c
Signature · 1 line
2.2.2