aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@redhat.com>2014-01-07 07:47:22 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2014-02-18 07:34:47 -0500
commitaa16b81fe916378ef6474530c59f719c36cd6ec4 (patch)
tree0cc4938360b9a9b9f95bcb71bf8ac1b5c7f59228 /tools
parent3c8b06f981091f91ee603768855e9739a8938296 (diff)
perf tests x86: Add dwarf unwind test
Adding dwarf unwind test, that setups live machine data over the perf test thread and does the remote unwind. At this moment this test fails due to bug in the max_stack processing in unwind__get_entries function. This is fixed in following patch. Need to use -fno-optimize-sibling-calls for test compilation, otherwise 'krava_*' function calls are optimized into jumps and ommited from the stack unwind. So far it's enabled only for x86. Signed-off-by: Jiri Olsa <jolsa@redhat.com> Acked-by: Jean Pihet <jean.pihet@linaro.org> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jean Pihet <jean.pihet@linaro.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1389098853-14466-6-git-send-email-jolsa@redhat.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/Makefile.perf8
-rw-r--r--tools/perf/arch/x86/Makefile1
-rw-r--r--tools/perf/arch/x86/include/perf_regs.h4
-rw-r--r--tools/perf/arch/x86/tests/dwarf-unwind.c58
-rw-r--r--tools/perf/tests/builtin-test.c8
-rw-r--r--tools/perf/tests/dwarf-unwind.c144
-rw-r--r--tools/perf/tests/tests.h9
7 files changed, 232 insertions, 0 deletions
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 7257e7e9e38a..10fcf331e0e3 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -408,6 +408,11 @@ endif
408LIB_OBJS += $(OUTPUT)tests/code-reading.o 408LIB_OBJS += $(OUTPUT)tests/code-reading.o
409LIB_OBJS += $(OUTPUT)tests/sample-parsing.o 409LIB_OBJS += $(OUTPUT)tests/sample-parsing.o
410LIB_OBJS += $(OUTPUT)tests/parse-no-sample-id-all.o 410LIB_OBJS += $(OUTPUT)tests/parse-no-sample-id-all.o
411ifndef NO_LIBUNWIND
412ifeq ($(ARCH),x86)
413LIB_OBJS += $(OUTPUT)tests/dwarf-unwind.o
414endif
415endif
411 416
412BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o 417BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
413BUILTIN_OBJS += $(OUTPUT)builtin-bench.o 418BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
@@ -655,6 +660,9 @@ $(OUTPUT)tests/python-use.o: tests/python-use.c $(OUTPUT)PERF-CFLAGS
655 -DPYTHON='"$(PYTHON_WORD)"' \ 660 -DPYTHON='"$(PYTHON_WORD)"' \
656 $< 661 $<
657 662
663$(OUTPUT)tests/dwarf-unwind.o: tests/dwarf-unwind.c
664 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -fno-optimize-sibling-calls $<
665
658$(OUTPUT)util/config.o: util/config.c $(OUTPUT)PERF-CFLAGS 666$(OUTPUT)util/config.o: util/config.c $(OUTPUT)PERF-CFLAGS
659 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $< 667 $(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
660 668
diff --git a/tools/perf/arch/x86/Makefile b/tools/perf/arch/x86/Makefile
index 1cbef7338f84..948ea6cfd681 100644
--- a/tools/perf/arch/x86/Makefile
+++ b/tools/perf/arch/x86/Makefile
@@ -5,6 +5,7 @@ endif
5ifndef NO_LIBUNWIND 5ifndef NO_LIBUNWIND
6LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind.o 6LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind.o
7LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/regs_load.o 7LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/regs_load.o
8LIB_OBJS += $(OUTPUT)arch/$(ARCH)/tests/dwarf-unwind.o
8endif 9endif
9LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o 10LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o
10LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/tsc.o 11LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/tsc.o
diff --git a/tools/perf/arch/x86/include/perf_regs.h b/tools/perf/arch/x86/include/perf_regs.h
index f3435d62aec1..fc819ca34a7e 100644
--- a/tools/perf/arch/x86/include/perf_regs.h
+++ b/tools/perf/arch/x86/include/perf_regs.h
@@ -9,12 +9,16 @@ void perf_regs_load(u64 *regs);
9 9
10#ifndef HAVE_ARCH_X86_64_SUPPORT 10#ifndef HAVE_ARCH_X86_64_SUPPORT
11#define PERF_REGS_MASK ((1ULL << PERF_REG_X86_32_MAX) - 1) 11#define PERF_REGS_MASK ((1ULL << PERF_REG_X86_32_MAX) - 1)
12#define PERF_REGS_MAX PERF_REG_X86_32_MAX
13#define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_32
12#else 14#else
13#define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \ 15#define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \
14 (1ULL << PERF_REG_X86_ES) | \ 16 (1ULL << PERF_REG_X86_ES) | \
15 (1ULL << PERF_REG_X86_FS) | \ 17 (1ULL << PERF_REG_X86_FS) | \
16 (1ULL << PERF_REG_X86_GS)) 18 (1ULL << PERF_REG_X86_GS))
17#define PERF_REGS_MASK (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~REG_NOSUPPORT) 19#define PERF_REGS_MASK (((1ULL << PERF_REG_X86_64_MAX) - 1) & ~REG_NOSUPPORT)
20#define PERF_REGS_MAX PERF_REG_X86_64_MAX
21#define PERF_SAMPLE_REGS_ABI PERF_SAMPLE_REGS_ABI_64
18#endif 22#endif
19#define PERF_REG_IP PERF_REG_X86_IP 23#define PERF_REG_IP PERF_REG_X86_IP
20#define PERF_REG_SP PERF_REG_X86_SP 24#define PERF_REG_SP PERF_REG_X86_SP
diff --git a/tools/perf/arch/x86/tests/dwarf-unwind.c b/tools/perf/arch/x86/tests/dwarf-unwind.c
new file mode 100644
index 000000000000..371f8493dcfd
--- /dev/null
+++ b/tools/perf/arch/x86/tests/dwarf-unwind.c
@@ -0,0 +1,58 @@
1#include <string.h>
2#include "perf_regs.h"
3#include "thread.h"
4#include "map.h"
5#include "event.h"
6#include "tests/tests.h"
7
8#define STACK_SIZE 8192
9
10static int sample_ustack(struct perf_sample *sample,
11 struct thread *thread, u64 *regs)
12{
13 struct stack_dump *stack = &sample->user_stack;
14 struct map *map;
15 unsigned long sp;
16 u64 stack_size, *buf;
17
18 buf = malloc(STACK_SIZE);
19 if (!buf) {
20 pr_debug("failed to allocate sample uregs data\n");
21 return -1;
22 }
23
24 sp = (unsigned long) regs[PERF_REG_X86_SP];
25
26 map = map_groups__find(&thread->mg, MAP__FUNCTION, (u64) sp);
27 if (!map) {
28 pr_debug("failed to get stack map\n");
29 return -1;
30 }
31
32 stack_size = map->end - sp;
33 stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
34
35 memcpy(buf, (void *) sp, stack_size);
36 stack->data = (char *) buf;
37 stack->size = stack_size;
38 return 0;
39}
40
41int test__arch_unwind_sample(struct perf_sample *sample,
42 struct thread *thread)
43{
44 struct regs_dump *regs = &sample->user_regs;
45 u64 *buf;
46
47 buf = malloc(sizeof(u64) * PERF_REGS_MAX);
48 if (!buf) {
49 pr_debug("failed to allocate sample uregs data\n");
50 return -1;
51 }
52
53 perf_regs_load(buf);
54 regs->abi = PERF_SAMPLE_REGS_ABI;
55 regs->regs = buf;
56
57 return sample_ustack(sample, thread, buf);
58}
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 1e67437fb4ca..f5a6ffba8a47 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -115,6 +115,14 @@ static struct test {
115 .desc = "Test parsing with no sample_id_all bit set", 115 .desc = "Test parsing with no sample_id_all bit set",
116 .func = test__parse_no_sample_id_all, 116 .func = test__parse_no_sample_id_all,
117 }, 117 },
118#if defined(__x86_64__) || defined(__i386__)
119#ifdef HAVE_LIBUNWIND_SUPPORT
120 {
121 .desc = "Test dwarf unwind",
122 .func = test__dwarf_unwind,
123 },
124#endif
125#endif
118 { 126 {
119 .func = NULL, 127 .func = NULL,
120 }, 128 },
diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c
new file mode 100644
index 000000000000..a203c0caaf8a
--- /dev/null
+++ b/tools/perf/tests/dwarf-unwind.c
@@ -0,0 +1,144 @@
1#include <linux/compiler.h>
2#include <sys/types.h>
3#include <unistd.h>
4#include "tests.h"
5#include "debug.h"
6#include "machine.h"
7#include "event.h"
8#include "unwind.h"
9#include "perf_regs.h"
10#include "map.h"
11#include "thread.h"
12
13static int mmap_handler(struct perf_tool *tool __maybe_unused,
14 union perf_event *event,
15 struct perf_sample *sample __maybe_unused,
16 struct machine *machine)
17{
18 return machine__process_mmap_event(machine, event, NULL);
19}
20
21static int init_live_machine(struct machine *machine)
22{
23 union perf_event event;
24 pid_t pid = getpid();
25
26 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
27 mmap_handler, machine, true);
28}
29
30#define MAX_STACK 6
31
32static int unwind_entry(struct unwind_entry *entry, void *arg)
33{
34 unsigned long *cnt = (unsigned long *) arg;
35 char *symbol = entry->sym ? entry->sym->name : NULL;
36 static const char *funcs[MAX_STACK] = {
37 "test__arch_unwind_sample",
38 "unwind_thread",
39 "krava_3",
40 "krava_2",
41 "krava_1",
42 "test__dwarf_unwind"
43 };
44
45 if (*cnt >= MAX_STACK) {
46 pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
47 return -1;
48 }
49
50 if (!symbol) {
51 pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
52 entry->ip);
53 return -1;
54 }
55
56 pr_debug("got: %s 0x%" PRIx64 "\n", symbol, entry->ip);
57 return strcmp((const char *) symbol, funcs[(*cnt)++]);
58}
59
60__attribute__ ((noinline))
61static int unwind_thread(struct thread *thread, struct machine *machine)
62{
63 struct perf_sample sample;
64 unsigned long cnt = 0;
65 int err = -1;
66
67 memset(&sample, 0, sizeof(sample));
68
69 if (test__arch_unwind_sample(&sample, thread)) {
70 pr_debug("failed to get unwind sample\n");
71 goto out;
72 }
73
74 err = unwind__get_entries(unwind_entry, &cnt, machine, thread,
75 PERF_REGS_MASK, &sample, MAX_STACK);
76 if (err)
77 pr_debug("unwind failed\n");
78 else if (cnt != MAX_STACK) {
79 pr_debug("got wrong number of stack entries %lu != %d\n",
80 cnt, MAX_STACK);
81 err = -1;
82 }
83
84 out:
85 free(sample.user_stack.data);
86 free(sample.user_regs.regs);
87 return err;
88}
89
90__attribute__ ((noinline))
91static int krava_3(struct thread *thread, struct machine *machine)
92{
93 return unwind_thread(thread, machine);
94}
95
96__attribute__ ((noinline))
97static int krava_2(struct thread *thread, struct machine *machine)
98{
99 return krava_3(thread, machine);
100}
101
102__attribute__ ((noinline))
103static int krava_1(struct thread *thread, struct machine *machine)
104{
105 return krava_2(thread, machine);
106}
107
108int test__dwarf_unwind(void)
109{
110 struct machines machines;
111 struct machine *machine;
112 struct thread *thread;
113 int err = -1;
114
115 machines__init(&machines);
116
117 machine = machines__find(&machines, HOST_KERNEL_ID);
118 if (!machine) {
119 pr_err("Could not get machine\n");
120 return -1;
121 }
122
123 if (init_live_machine(machine)) {
124 pr_err("Could not init machine\n");
125 goto out;
126 }
127
128 if (verbose > 1)
129 machine__fprintf(machine, stderr);
130
131 thread = machine__find_thread(machine, getpid());
132 if (!thread) {
133 pr_err("Could not get thread\n");
134 goto out;
135 }
136
137 err = krava_1(thread, machine);
138
139 out:
140 machine__delete_threads(machine);
141 machine__exit(machine);
142 machines__exit(&machines);
143 return err;
144}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index e0ac713857ba..8979309876a7 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -40,5 +40,14 @@ int test__code_reading(void);
40int test__sample_parsing(void); 40int test__sample_parsing(void);
41int test__keep_tracking(void); 41int test__keep_tracking(void);
42int test__parse_no_sample_id_all(void); 42int test__parse_no_sample_id_all(void);
43int test__dwarf_unwind(void);
43 44
45#if defined(__x86_64__) || defined(__i386__)
46#ifdef HAVE_LIBUNWIND_SUPPORT
47struct thread;
48struct perf_sample;
49int test__arch_unwind_sample(struct perf_sample *sample,
50 struct thread *thread);
51#endif
52#endif
44#endif /* TESTS_H */ 53#endif /* TESTS_H */