perf report: Get rid of report__inc_stat()

The report__inc_stat() is to collect number of hist entries in the
session in order to calculate the max size of progess bar.  It'd be
better if it does during addition of hist entries so that it can be
used by other places too.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf report: Show progress bar for output resorting

Sometimes it takes a time to resort hist entries for output in case of
a large data file.  Show a progress bar window and inform user.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf ui/tui: Print backtrace symbols when segfault occurred

The output will look like below.  (I added an error into ui__init()
for the test).

  $ perf report
  perf: Segmentation fault
  -------- backtrace --------
  perf[0x503781]
  /usr/lib/libc.so.6(+0x33b20)[0x7f1a14f04b20]
  perf(ui__init+0xd5)[0x503645]
  perf(setup_browser+0x97)[0x4ce4e7]
  perf(cmd_report+0xcea)[0x4392ba]
  perf[0x428493]
  perf(main+0x60a)[0x427c0a]
  /usr/lib/libc.so.6(__libc_start_main+0xf0)[0x7f1a14ef1040]
  perf[0x427d29]
  [0x0]

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Append callchains only when requested

The perf report --children can be called with callchain disabled so no
need to append callchains.  Actually the root of callchain tree is not
initialized properly in this case.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
perf tools: Set attr.task bit for a tracking event

The perf_event_attr.task bit is to track task (fork and exit) events
but it missed to be set by perf_evsel__config().  While it was not a
problem in practice since setting other bits (comm/mmap) ended up
being in same result, it'd be good to set it explicitly anyway.

Signed-off-by: Namhyung Kim <namhyung@gmail.com>
perf record: Use a software dummy event to track task/mmap events

Prepend a software dummy event into evlist to track task/comm/mmap
events separately.  This is a preparation of multi-file/thread support
which will come later.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Use perf_data_file__fd() consistently

Do not reference file->fd directly since we want hide the
implementation details from outside to support multi-file store.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Add multi file interface to perf_data_file

When multi file storage is enabled, the perf data files will be saved
in a directory (default: perf.data.dir) and it'll have a single header
file for metadata (task/comm/mmap events and file header) and multiple
data files (sample events) like below:

  $ tree perf.data.dir
  perf.data.dir
  |-- perf.data.0
  |-- perf.data.1
  |-- perf.data.2
  |-- perf.data.3
  `-- perf.header

  0 directories, 5 files

Existing data file interface supports multi files internally and add
new perf_data_file__prepare_write() and perf_data_file__write_multi()
functions in order to support multi-file record.  Note that multi read
interface is not needed since they're accessed via mmap.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Create separate mmap for dummy tracking event

When multi file support is enabled, a dummy tracking event will be
used to track metadata (like task, comm and mmap events) for a session
and actual samples will be recorded in separate files.

Provide separate mmap to the dummy tracking event.  The size is fixed
to 128KiB (+ 1 page) as the event rate will be lower than samples.  I
originally wanted to use a single mmap for this but cross-cpu sharing
is prohibited so it's per-cpu (or per-task) like normal mmaps.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Introduce perf_evlist__mmap_multi()

The perf_evlist__mmap_multi function creates data mmaps and optionally
tracking mmaps for events.  It'll be used for perf record to save
tracking events in a separate files.  Checking dummy tracking event in
perf_evlist__mmap() alone is not enough as users can specify the dummy
event at first (like in keep tracking testcase) without the multi-file
option.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: Namhyung Kim <namhyung@gmail.com>
perf tools: Do not use __perf_session__process_events() directly

It's only used for perf record to process build-id because its file
size it's not fixed at this time due to remaining header features.
However data offset and size is available so that we can use the
perf_session__process_events() once we set the file size as the
current offset like for now.

It turns out that we can staticize the function again as it's the only
user and add multi file support in a single place.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Handle multi-file session properly

When perf detects multi-file data directory, process header file first
and then rest data files in a row.  Note that the multi-file data is
recorded for each cpu/thread separately, it's already ordered with
respect to themselves so no need to use the ordered event queue
interface.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf record: Add -M/--multi option for multi file recording

The new -M/--multi option enables multi file storage recording.  Now
sample data in separate mmap are saved in different files and other
events will be recorded in the perf.header file.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf report: Skip dummy tracking event

The dummy tracking event is only for tracking task/comom/mmap events
and has no sample data for itself.  So no need to report, just skip it.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Introduce thread__comm_time() helpers

When multi-file data storage is enabled, it processes all task, comm
and mmap events first and then goes to the sample events.  So all it
sees is the last comm of a thread although it has information at the
time of sample.

Sort thread's comm by time so that it can find appropriate comm at the
sample time.  The thread__comm_time() will mostly work even if
PERF_SAMPLE_TIME bit is off since in that case, sample->time will be
-1 so it'll take the last comm anyway.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Add a test case for thread comm handling

The new test case checks various thread comm handling like overridding
and time sorting.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Use thread__comm_time() when adding hist entries

Now thread->comm can be handled with time properly, use it to find
correct comm when adding hist entries.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Convert dead thread list into rbtree

Currently perf maintains dead threads in a linked list but this can be
a problem if someone needs to search from it.  Convert it to a rbtree
like normal threads and it'll be used later with multi-file changes.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Introduce machine__find*_thread_time()

With multi-file data storage is enabled, it needs to search thread
based on sample time since sample processing is done after other
(task, comm and mmap) events are processed.  This can be a problem if
a session is very long and pid is recycled - in that case it'll only
see the last one.

So keep thread start time in it, and search thread based on the time.
This patch introduces machine__find{,new}_thread_time() function for
this.  It'll first search current thread rbtree and then dead thread
tree and list.  If it couldn't find anyone, it'll create a new thread.

The sample timestamp of 0 means that this is called from synthesized
event so just use current rbtree.  The timestamp will be -1 if sample
didn't record the timestamp so will see current threads automatically.

Dead threads are managed in a rbtree, and if there's more than one
thread has sample tid/pid, it'll be saved in a list.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
perf tools: Add a test case for timed thread handling

A test case for verifying live and dead thread tree management during
time change and new machine__find{,new}_thread_time().

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>