Skip to contentSkip to conversation

perf tools: Speed up dwarf callchain post-unwinding for libunwind (v3)

PATCH In review Namhyung Kim 2 participants 8 messages Oct 2, 2014 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf tools: Speed up dwarf callchain post-unwinding for libunwind (v3)

Hello,

This is v3 for libunwind callchain post processing speed up. It was able to reduce 50% of processing time by using global cache provided in libunwind. In this version, I decided to use the existing callchain_param.record_mode instead of adding a new field in the symbol_conf.

The patch 4 and 5 are just cleanups so that we can easily find out that which part of code uses the thread->priv.

You can also get it from 'perf/callchain-unwind-v3' branch on my tree:

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

Thanks,
Namhyung

Namhyung Kim (5):
perf report: Set callchain_param.record_mode for future use perf callchain: Create an address space per thread perf callchain: Use global caching provided by libunwind perf kvm: Use thread_{,_set}_priv helpers
perf trace: Use thread_{,_set}_priv helpers

 tools/perf/builtin-kvm.c           |  6 ++---
 tools/perf/builtin-report.c        |  6 +++++
 tools/perf/builtin-trace.c         | 16 ++++++-------
 tools/perf/tests/dwarf-unwind.c    |  3 +++
 tools/perf/util/callchain.h        |  2 ++
 tools/perf/util/hist.h             |  2 --
 tools/perf/util/thread.c           |  9 +++++++
 tools/perf/util/unwind-libunwind.c | 48 ++++++++++++++++++++++++++++++++++----
 tools/perf/util/unwind.h           | 20 ++++++++++++++++
 9 files changed, 94 insertions(+), 18 deletions(-)
Signature · 1 line
2.1.0
author maintainer
#1 lore raw

PATCH 1/5 perf report: Set callchain_param.record_mode for future use

Normally the callchain_param.record_mode is used only for record path. But as it might need to prepare something for dwarf unwinding, setup this info for perf report too.

Cc Jiri Olsa <[email protected]>
Cc David Ahern <[email protected]>
Cc Jean Pihet <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 tools/perf/builtin-report.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/perf/builtin-report.c +6 -0
index ac145fae0521..495168e73d4a 100644
@@ -257,6 +257,12 @@ static int report__setup_sample_type(struct report *rep)
 		}
 	}
 
+	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
+		if (sample_type & PERF_SAMPLE_REGS_USER)
+			callchain_param.record_mode = CALLCHAIN_DWARF;
+		else
+			callchain_param.record_mode = CALLCHAIN_FP;
+	}
 	return 0;
 }
 
Signature · 1 line
2.1.0
author maintainer
#2 lore raw

PATCH 1/5 perf report: Set callchain_param.record_mode for future use

Normally the callchain_param.record_mode is used only for record path. But as it might need to prepare something for dwarf unwinding, setup this info for perf report too.

Cc Jiri Olsa <[email protected]>
Cc David Ahern <[email protected]>
Cc Jean Pihet <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 tools/perf/builtin-report.c     | 6 ++++++
 tools/perf/tests/dwarf-unwind.c | 3 +++
 tools/perf/util/callchain.h     | 2 ++
 tools/perf/util/hist.h          | 2 --
 4 files changed, 11 insertions(+), 2 deletions(-)
tools/perf/builtin-report.c +6 -0
index ac145fae0521..495168e73d4a 100644
@@ -257,6 +257,12 @@ static int report__setup_sample_type(struct report *rep)
 		}
 	}
 
+	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
+		if (sample_type & PERF_SAMPLE_REGS_USER)
+			callchain_param.record_mode = CALLCHAIN_DWARF;
+		else
+			callchain_param.record_mode = CALLCHAIN_FP;
+	}
 	return 0;
 }
 
tools/perf/tests/dwarf-unwind.c +3 -0
index 96adb730b744..fc25e57f4a5d 100644
@@ -9,6 +9,7 @@
 #include "perf_regs.h"
 #include "map.h"
 #include "thread.h"
+#include "callchain.h"
 
 static int mmap_handler(struct perf_tool *tool __maybe_unused,
 			union perf_event *event,
@@ -120,6 +121,8 @@ int test__dwarf_unwind(void)
 		return -1;
 	}
 
+	callchain_param.record_mode = CALLCHAIN_DWARF;
+
 	if (init_live_machine(machine)) {
 		pr_err("Could not init machine\n");
 		goto out;
tools/perf/util/callchain.h +2 -0
index 2a1f5a46543a..94cfefddf4db 100644
@@ -65,6 +65,8 @@ struct callchain_param {
 	enum chain_key		key;
 };
 
+extern struct callchain_param callchain_param;
+
 struct callchain_list {
 	u64			ip;
 	struct map_symbol	ms;
tools/perf/util/hist.h +0 -2
index 8c9c70e18cbb..ac1ee82c6c7e 100644
@@ -8,8 +8,6 @@
 #include "color.h"
 #include "ui/progress.h"
 
-extern struct callchain_param callchain_param;
-
 struct hist_entry;
 struct addr_location;
 struct symbol;
Signature · 1 line
2.1.0
#3 lore raw

Re: [PATCH v3.1 1/5] perf report: Set callchain_param.record_mode for future use

Quoted 21 lines · Namhyung Kim

Normally the callchain_param.record_mode is used only for record path. But as it might need to prepare something for dwarf unwinding, setup this info for perf report too.

Cc Jiri Olsa <[email protected]>
Cc David Ahern <[email protected]>
Cc Jean Pihet <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 tools/perf/builtin-report.c     | 6 ++++++
 tools/perf/tests/dwarf-unwind.c | 3 +++
 tools/perf/util/callchain.h     | 2 ++
 tools/perf/util/hist.h          | 2 --
 4 files changed, 11 insertions(+), 2 deletions(-)
tools/perf/builtin-report.c +0 -0
index ac145fae0521..495168e73d4a 100644

tools/perf/builtin-report.c static int report__setup_sample_type(struct report *rep) · line 261

Quoted 5 lines
tools/perf/builtin-report.c +2 -0
@@ -257,6 +257,12 @@ static int report__setup_sample_type(struct report *rep)
 		}
 	}
 
+	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
+		if (sample_type & PERF_SAMPLE_REGS_USER)

should we check also for PERF_SAMPLE_STACK_USER?
as in machine__resolve_callchain

thanks,
jirka

author maintainer
#4 lore raw

PATCH 2/5 perf callchain: Create an address space per thread

In reply to Namhyung Kim

The unw_addr_space_t in libunwind represents an address space to be used for stack unwinding. It doesn't need to be create/destory everytime to unwind callchain (as in get_entries) and can have a same lifetime as thread (unless exec called).

So move the address space construction/destruction logic to the thread lifetime handling functions. This is a preparation to enable caching in the unwind library.

Note that it saves unw_addr_space_t object using thread__set_priv(). It seems currently only used by perf trace and perf kvm stat commands which don't use callchain.

Cc Jiri Olsa <[email protected]>
Cc Arun Sharma <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 tools/perf/util/thread.c           |  6 ++++++
 tools/perf/util/unwind-libunwind.c | 36 +++++++++++++++++++++++++++++++-----
 tools/perf/util/unwind.h           | 17 +++++++++++++++++
 3 files changed, 54 insertions(+), 5 deletions(-)
tools/perf/util/thread.c +6 -0
index a9df7f2c6dc9..2b7b2d91c016 100644
@@ -7,6 +7,7 @@
 #include "util.h"
 #include "debug.h"
 #include "comm.h"
+#include "unwind.h"
 
 int thread__init_map_groups(struct thread *thread, struct machine *machine)
 {
@@ -37,6 +38,9 @@ struct thread *thread__new(pid_t pid, pid_t tid)
 		thread->cpu = -1;
 		INIT_LIST_HEAD(&thread->comm_list);
 
+		if (unwind__prepare_access(thread) < 0)
+			goto err_thread;
+
 		comm_str = malloc(32);
 		if (!comm_str)
 			goto err_thread;
@@ -48,6 +52,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)
 			goto err_thread;
 
 		list_add(&comm->list, &thread->comm_list);
+
 	}
 
 	return thread;
@@ -69,6 +74,7 @@ void thread__delete(struct thread *thread)
 		list_del(&comm->list);
 		comm__free(comm);
 	}
+	unwind__finish_access(thread);
 
 	free(thread);
 }
tools/perf/util/unwind-libunwind.c +31 -5
index 92b56db52471..d9874465206b 100644
@@ -525,12 +525,12 @@ static unw_accessors_t accessors = {
 	.get_proc_name		= get_proc_name,
 };
 
-static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
-		       void *arg, int max_stack)
+int unwind__prepare_access(struct thread *thread)
 {
 	unw_addr_space_t addr_space;
-	unw_cursor_t c;
-	int ret;
+
+	if (callchain_param.record_mode != CALLCHAIN_DWARF)
+		return 0;
 
 	addr_space = unw_create_addr_space(&accessors, 0);
 	if (!addr_space) {
@@ -538,6 +538,33 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
 		return -ENOMEM;
 	}
 
+	thread__set_priv(thread, addr_space);
+
+	return 0;
+}
+
+void unwind__finish_access(struct thread *thread)
+{
+	unw_addr_space_t addr_space;
+
+	if (callchain_param.record_mode != CALLCHAIN_DWARF)
+		return;
+
+	addr_space = thread__priv(thread);
+	unw_destroy_addr_space(addr_space);
+}
+
+static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
+		       void *arg, int max_stack)
+{
+	unw_addr_space_t addr_space;
+	unw_cursor_t c;
+	int ret;
+
+	addr_space = thread__priv(ui->thread);
+	if (addr_space == NULL)
+		return -1;
+
 	ret = unw_init_remote(&c, addr_space, ui);
 	if (ret)
 		display_error(ret);
@@ -549,7 +576,6 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,
 		ret = ip ? entry(ip, ui->thread, ui->machine, cb, arg) : 0;
 	}
 
-	unw_destroy_addr_space(addr_space);
 	return ret;
 }
 
tools/perf/util/unwind.h +17 -0
index f03061260b4e..4b99c6280c2a 100644
@@ -4,6 +4,7 @@
 #include <linux/types.h>
 #include "event.h"
 #include "symbol.h"
+#include "thread.h"
 
 struct unwind_entry {
 	struct map	*map;
@@ -21,6 +22,15 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
 /* libunwind specific */
 #ifdef HAVE_LIBUNWIND_SUPPORT
 int libunwind__arch_reg_id(int regnum);
+int unwind__prepare_access(struct thread *thread);
+void unwind__finish_access(struct thread *thread);
+#else
+static inline int unwind__prepare_access(struct thread *thread)
+{
+	return 0;
+}
+
+static inline void unwind__finish_access(struct thread *thread) {}
 #endif
 #else
 static inline int
@@ -33,5 +43,12 @@ unwind__get_entries(unwind_entry_cb_t cb __maybe_unused,
 {
 	return 0;
 }
+
+static inline int unwind__prepare_access(struct thread *thread)
+{
+	return 0;
+}
+
+static inline void unwind__finish_access(struct thread *thread) {}
 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
 #endif /* __UNWIND_H */
Signature · 1 line
2.1.0
author maintainer
#5 lore raw

PATCH 3/5 perf callchain: Use global caching provided by libunwind

In reply to Namhyung Kim

The libunwind provides two caching policy which are global and per-thread. As perf unwinds callchains in a single thread, it'd sufficient to use global caching.

This speeds up my perf report from 14s to 7s on a ~260MB data file. Although the output sometimes contains a slight difference (~0.01% in terms of number of lines printed) on callchains which were not resolved.

Cc Jiri Olsa <[email protected]>
Cc Arun Sharma <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 tools/perf/util/thread.c           |  3 +++
 tools/perf/util/unwind-libunwind.c | 12 ++++++++++++
 tools/perf/util/unwind.h           |  3 +++
 3 files changed, 18 insertions(+)
tools/perf/util/thread.c +3 -0
index 2b7b2d91c016..c41411726c7a 100644
@@ -117,6 +117,9 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
 		if (!new)
 			return -ENOMEM;
 		list_add(&new->list, &thread->comm_list);
+
+		if (exec)
+			unwind__flush_access(thread);
 	}
 
 	thread->comm_set = true;
tools/perf/util/unwind-libunwind.c +12 -0
index d9874465206b..2d52af98bc8d 100644
@@ -538,11 +538,23 @@ int unwind__prepare_access(struct thread *thread)
 		return -ENOMEM;
 	}
 
+	unw_set_caching_policy(addr_space, UNW_CACHE_GLOBAL);
 	thread__set_priv(thread, addr_space);
 
 	return 0;
 }
 
+void unwind__flush_access(struct thread *thread)
+{
+	unw_addr_space_t addr_space;
+
+	if (callchain_param.record_mode != CALLCHAIN_DWARF)
+		return;
+
+	addr_space = thread__priv(thread);
+	unw_flush_cache(addr_space, 0, 0);
+}
+
 void unwind__finish_access(struct thread *thread)
 {
 	unw_addr_space_t addr_space;
tools/perf/util/unwind.h +3 -0
index 4b99c6280c2a..d68f24d4f01b 100644
@@ -23,6 +23,7 @@ int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
 #ifdef HAVE_LIBUNWIND_SUPPORT
 int libunwind__arch_reg_id(int regnum);
 int unwind__prepare_access(struct thread *thread);
+void unwind__flush_access(struct thread *thread);
 void unwind__finish_access(struct thread *thread);
 #else
 static inline int unwind__prepare_access(struct thread *thread)
@@ -30,6 +31,7 @@ static inline int unwind__prepare_access(struct thread *thread)
 	return 0;
 }
 
+static inline void unwind__flush_access(struct thread *thread) {}
 static inline void unwind__finish_access(struct thread *thread) {}
 #endif
 #else
@@ -49,6 +51,7 @@ static inline int unwind__prepare_access(struct thread *thread)
 	return 0;
 }
 
+static inline void unwind__flush_access(struct thread *thread) {}
 static inline void unwind__finish_access(struct thread *thread) {}
 #endif /* HAVE_DWARF_UNWIND_SUPPORT */
 #endif /* __UNWIND_H */
Signature · 1 line
2.1.0
author maintainer
#6 lore raw

PATCH 4/5 perf kvm: Use thread_{,_set}_priv helpers

In reply to Namhyung Kim

This is mechanical changes only for accounting access to thread->priv properly in the source level.

---
 tools/perf/builtin-kvm.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/builtin-kvm.c +3 -3
index d8bf2271f4ea..d9b0743180db 100644
@@ -376,7 +376,7 @@ struct vcpu_event_record *per_vcpu_record(struct thread *thread,
 					  struct perf_sample *sample)
 {
 	/* Only kvm_entry records vcpu id. */
-	if (!thread->priv && kvm_entry_event(evsel)) {
+	if (!thread__priv(thread) && kvm_entry_event(evsel)) {
 		struct vcpu_event_record *vcpu_record;
 
 		vcpu_record = zalloc(sizeof(*vcpu_record));
@@ -386,10 +386,10 @@ struct vcpu_event_record *per_vcpu_record(struct thread *thread,
 		}
 
 		vcpu_record->vcpu_id = perf_evsel__intval(evsel, sample, VCPU_ID);
-		thread->priv = vcpu_record;
+		thread__set_priv(thread, vcpu_record);
 	}
 
-	return thread->priv;
+	return thread__priv(thread);
 }
 
 static bool handle_kvm_event(struct perf_kvm_stat *kvm,
Signature · 1 line
2.1.0
author maintainer
#7 lore raw

PATCH 5/5 perf trace: Use thread_{,_set}_priv helpers

In reply to Namhyung Kim

This is mechanical changes only for accounting access to thread->priv properly in the source level.

---
 tools/perf/builtin-trace.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/perf/builtin-trace.c +8 -8
index 09bcf2393910..fb126459b134 100644
@@ -1189,13 +1189,13 @@ static struct thread_trace *thread__trace(struct thread *thread, FILE *fp)
 	if (thread == NULL)
 		goto fail;
 
-	if (thread->priv == NULL)
-		thread->priv = thread_trace__new();
+	if (thread__priv(thread) == NULL)
+		thread__set_priv(thread, thread_trace__new());
 		
-	if (thread->priv == NULL)
+	if (thread__priv(thread) == NULL)
 		goto fail;
 
-	ttrace = thread->priv;
+	ttrace = thread__priv(thread);
 	++ttrace->nr_events;
 
 	return ttrace;
@@ -1248,7 +1248,7 @@ struct trace {
 
 static int trace__set_fd_pathname(struct thread *thread, int fd, const char *pathname)
 {
-	struct thread_trace *ttrace = thread->priv;
+	struct thread_trace *ttrace = thread__priv(thread);
 
 	if (fd > ttrace->paths.max) {
 		char **npath = realloc(ttrace->paths.table, (fd + 1) * sizeof(char *));
@@ -1301,7 +1301,7 @@ static int thread__read_fd_path(struct thread *thread, int fd)
 static const char *thread__fd_path(struct thread *thread, int fd,
 				   struct trace *trace)
 {
-	struct thread_trace *ttrace = thread->priv;
+	struct thread_trace *ttrace = thread__priv(thread);
 
 	if (ttrace == NULL)
 		return NULL;
@@ -1338,7 +1338,7 @@ static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
 {
 	int fd = arg->val;
 	size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
-	struct thread_trace *ttrace = arg->thread->priv;
+	struct thread_trace *ttrace = thread__priv(arg->thread);
 
 	if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
 		zfree(&ttrace->paths.table[fd]);
@@ -2381,7 +2381,7 @@ static int trace__fprintf_one_thread(struct thread *thread, void *priv)
 	FILE *fp = data->fp;
 	size_t printed = data->printed;
 	struct trace *trace = data->trace;
-	struct thread_trace *ttrace = thread->priv;
+	struct thread_trace *ttrace = thread__priv(thread);
 	double ratio;
 
 	if (ttrace == NULL)
Signature · 1 line
2.1.0