[PATCH 2/2] perf script: Support callindent
From: Andi Kleen
Date: Fri Jun 10 2016 - 18:56:45 EST
From: Andi Kleen <ak@xxxxxxxxxxxxxxx>
When printing PT instruction traces with perf script
it is rather useful to see some indentation for the call tree. This
patch adds a new callindent field to perf script that prints
spaces for the function call stack depth.
We already have code to track the function call stack for PT,
previously used for the data base export. We can reuse that code
directly with minor modifications.
The resulting output is not quite as nice as ftrace yet, but
a lot better than what was there before.
Note there are some corner cases when the thread stack gets code confused
and prints incorrect indentation. Even with that it is fairly useful.
When displaying kernel code traces it is recommended to run as root, as
otherwise perf doesn't understand the kernel addresses properly, and may not
reset the call stack correctly on kernel boundaries.
Example output:
$ perf record -a -e intel_pt// sleep 1
$ perf script --ns -F callindent,time,comm,pid,sym,addr,event --itrace=cr
...
swapper 0 126327.044742970: return: => ffffffff810aa999 cpu_idle_loop
swapper 0 126327.044742970: branches: => ffffffff81525400 cpuidle_reflect
swapper 0 126327.044742970: return: => ffffffff81525400 cpuidle_reflect
swapper 0 126327.044742970: branches: => ffffffff810aa9ce cpu_idle_loop
swapper 0 126327.044742970: return: => ffffffff810aa9ce cpu_idle_loop
swapper 0 126327.044742970: branches: => ffffffff810cb0b0 rcu_idle_exit
swapper 0 126327.044742970: return: => ffffffff810cb0b0 rcu_idle_exit
swapper 0 126327.044742970: branches: => ffffffff810c99d0 rcu_eqs_exit_common.isra.43
swapper 0 126327.044742970: return: => ffffffff810c99d0 rcu_eqs_exit_common.isra.43
swapper 0 126327.044742970: branches: => ffffffff810cb124 rcu_idle_exit
swapper 0 126327.044742970: return: => ffffffff810cb124 rcu_idle_exit
swapper 0 126327.044742970: branches: => ffffffff810aa719 cpu_idle_loop
swapper 0 126327.044742970: return: => ffffffff810aa719 cpu_idle_loop
Cc: adrian.hunter@xxxxxxxxx
v2: Move get_main_thread in separate patch. Add thread__put.
Signed-off-by: Andi Kleen <ak@xxxxxxxxxxxxxxx>
---
tools/perf/builtin-script.c | 46 ++++++++++++++++++++++++++++++++++++++++++
tools/perf/util/thread-stack.c | 7 +++++++
tools/perf/util/thread-stack.h | 1 +
3 files changed, 54 insertions(+)
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index 46011235af5d..2973c097ec69 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -21,6 +21,7 @@
#include "util/cpumap.h"
#include "util/thread_map.h"
#include "util/stat.h"
+#include "util/thread-stack.h"
#include <linux/bitmap.h>
#include <linux/stringify.h>
#include "asm/bug.h"
@@ -63,6 +64,7 @@ enum perf_output_field {
PERF_OUTPUT_DATA_SRC = 1U << 17,
PERF_OUTPUT_WEIGHT = 1U << 18,
PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
+ PERF_OUTPUT_CALLINDENT = 1U << 20,
};
struct output_option {
@@ -89,6 +91,7 @@ struct output_option {
{.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
{.str = "weight", .field = PERF_OUTPUT_WEIGHT},
{.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
+ {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
};
/* default set to maintain compatibility with current format */
@@ -261,6 +264,11 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
PERF_OUTPUT_WEIGHT))
return -EINVAL;
+ if (PRINT_FIELD(CALLINDENT) && !PRINT_FIELD(ADDR)) {
+ pr_err("Display of callindent requested, but no branch address\n");
+ return -EINVAL;
+ }
+
if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
pr_err("Display of symbols requested but neither sample IP nor "
"sample address\nis selected. Hence, no addresses to convert "
@@ -562,6 +570,41 @@ static void print_sample_addr(struct perf_sample *sample,
}
}
+static int dummy_call_return(struct call_return *cr __maybe_unused,
+ void *arg __maybe_unused)
+{
+ return 0;
+}
+
+static void print_sample_callindent(struct perf_sample *sample,
+ struct perf_evsel *evsel,
+ struct thread *thread,
+ struct addr_location *al)
+{
+ struct perf_event_attr *attr = &evsel->attr;
+
+ if (sample_addr_correlates_sym(attr)) {
+ static struct call_return_processor *crp;
+ struct addr_location addr_al;
+ struct thread *main_thread;
+ struct comm *comm;
+
+ if (!crp)
+ crp = call_return_processor__new(dummy_call_return,
+ NULL);
+ thread__resolve(thread, &addr_al, sample);
+ main_thread = thread__main_thread(al->machine, thread);
+ if (main_thread) {
+ comm = machine__thread_exec_comm(al->machine,
+ main_thread);
+ thread__put(main_thread);
+ }
+ thread_stack__process(thread, comm, sample, al, &addr_al,
+ 0, crp);
+ }
+ thread_stack__print_indent(thread);
+}
+
static void print_sample_bts(struct perf_sample *sample,
struct perf_evsel *evsel,
struct thread *thread,
@@ -570,6 +613,9 @@ static void print_sample_bts(struct perf_sample *sample,
struct perf_event_attr *attr = &evsel->attr;
bool print_srcline_last = false;
+ if (PRINT_FIELD(CALLINDENT))
+ print_sample_callindent(sample, evsel, thread, al);
+
/* print branch_from information */
if (PRINT_FIELD(IP)) {
unsigned int print_opts = output[attr->type].print_ip_opts;
diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c
index 825086aa9a08..20e57f263c41 100644
--- a/tools/perf/util/thread-stack.c
+++ b/tools/perf/util/thread-stack.c
@@ -616,3 +616,10 @@ int thread_stack__process(struct thread *thread, struct comm *comm,
return err;
}
+
+void thread_stack__print_indent(struct thread *thread)
+{
+ if (!thread->ts)
+ return;
+ printf("%*s", (int)thread->ts->cnt * 4, "");
+}
diff --git a/tools/perf/util/thread-stack.h b/tools/perf/util/thread-stack.h
index ad44c7944b8e..7fad96415f88 100644
--- a/tools/perf/util/thread-stack.h
+++ b/tools/perf/util/thread-stack.h
@@ -87,6 +87,7 @@ void thread_stack__sample(struct thread *thread, struct ip_callchain *chain,
size_t sz, u64 ip);
int thread_stack__flush(struct thread *thread);
void thread_stack__free(struct thread *thread);
+void thread_stack__print_indent(struct thread *thread);
struct call_return_processor *
call_return_processor__new(int (*process)(struct call_return *cr, void *data),
--
2.5.5