diff options
author | Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com> | 2014-06-25 11:49:03 -0400 |
---|---|---|
committer | Jiri Olsa <jolsa@kernel.org> | 2014-06-27 05:14:51 -0400 |
commit | a60335ba32981db5bc057b35782644e9e2436407 (patch) | |
tree | e6d63c2427f4c15a628cbbfd955d1b9b91c22704 | |
parent | 21da83fb6c3ff67e969b8770bbb33138c9ede88e (diff) |
perf tools powerpc: Adjust callchain based on DWARF debug info
When saving the callchain on Power, the kernel conservatively saves excess
entries in the callchain. A few of these entries are needed in some cases
but not others. We should use the DWARF debug information to determine
when the entries are needed.
Eg: the value in the link register (LR) is needed only when it holds the
return address of a function. At other times it must be ignored.
If the unnecessary entries are not ignored, we end up with duplicate arcs
in the call-graphs.
Use the DWARF debug information to determine if any callchain entries
should be ignored when building call-graphs.
Callgraph before the patch:
14.67% 2234 sprintft libc-2.18.so [.] __random
|
--- __random
|
|--61.12%-- __random
| |
| |--97.15%-- rand
| | do_my_sprintf
| | main
| | generic_start_main.isra.0
| | __libc_start_main
| | 0x0
| |
| --2.85%-- do_my_sprintf
| main
| generic_start_main.isra.0
| __libc_start_main
| 0x0
|
--38.88%-- rand
|
|--94.01%-- rand
| do_my_sprintf
| main
| generic_start_main.isra.0
| __libc_start_main
| 0x0
|
--5.99%-- do_my_sprintf
main
generic_start_main.isra.0
__libc_start_main
0x0
Callgraph after the patch:
14.67% 2234 sprintft libc-2.18.so [.] __random
|
--- __random
|
|--95.93%-- rand
| do_my_sprintf
| main
| generic_start_main.isra.0
| __libc_start_main
| 0x0
|
--4.07%-- do_my_sprintf
main
generic_start_main.isra.0
__libc_start_main
0x0
TODO: For split-debug info objects like glibc, we can only determine
the call-frame-address only when both .eh_frame and .debug_info
sections are available. We should be able to determin the CFA
even without the .eh_frame section.
Fix suggested by Anton Blanchard.
Thanks to valuable input on DWARF debug information from Ulrich Weigand.
Reported-by: Maynard Johnson <maynard@us.ibm.com>
Tested-by: Maynard Johnson <maynard@us.ibm.com>
Signed-off-by: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/20140625154903.GA29607@us.ibm.com
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
-rw-r--r-- | tools/perf/arch/powerpc/Makefile | 1 | ||||
-rw-r--r-- | tools/perf/arch/powerpc/util/skip-callchain-idx.c | 266 | ||||
-rw-r--r-- | tools/perf/config/Makefile | 4 | ||||
-rw-r--r-- | tools/perf/util/callchain.h | 13 | ||||
-rw-r--r-- | tools/perf/util/machine.c | 18 |
5 files changed, 300 insertions, 2 deletions
diff --git a/tools/perf/arch/powerpc/Makefile b/tools/perf/arch/powerpc/Makefile index 744e629797be..b92219b1900d 100644 --- a/tools/perf/arch/powerpc/Makefile +++ b/tools/perf/arch/powerpc/Makefile | |||
@@ -3,3 +3,4 @@ PERF_HAVE_DWARF_REGS := 1 | |||
3 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o | 3 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/dwarf-regs.o |
4 | endif | 4 | endif |
5 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o | 5 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o |
6 | LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/skip-callchain-idx.o | ||
diff --git a/tools/perf/arch/powerpc/util/skip-callchain-idx.c b/tools/perf/arch/powerpc/util/skip-callchain-idx.c new file mode 100644 index 000000000000..a7c23a4b3778 --- /dev/null +++ b/tools/perf/arch/powerpc/util/skip-callchain-idx.c | |||
@@ -0,0 +1,266 @@ | |||
1 | /* | ||
2 | * Use DWARF Debug information to skip unnecessary callchain entries. | ||
3 | * | ||
4 | * Copyright (C) 2014 Sukadev Bhattiprolu, IBM Corporation. | ||
5 | * Copyright (C) 2014 Ulrich Weigand, IBM Corporation. | ||
6 | * | ||
7 | * This program is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU General Public License | ||
9 | * as published by the Free Software Foundation; either version | ||
10 | * 2 of the License, or (at your option) any later version. | ||
11 | */ | ||
12 | #include <inttypes.h> | ||
13 | #include <dwarf.h> | ||
14 | #include <elfutils/libdwfl.h> | ||
15 | |||
16 | #include "util/thread.h" | ||
17 | #include "util/callchain.h" | ||
18 | |||
19 | /* | ||
20 | * When saving the callchain on Power, the kernel conservatively saves | ||
21 | * excess entries in the callchain. A few of these entries are needed | ||
22 | * in some cases but not others. If the unnecessary entries are not | ||
23 | * ignored, we end up with duplicate arcs in the call-graphs. Use | ||
24 | * DWARF debug information to skip over any unnecessary callchain | ||
25 | * entries. | ||
26 | * | ||
27 | * See function header for arch_adjust_callchain() below for more details. | ||
28 | * | ||
29 | * The libdwfl code in this file is based on code from elfutils | ||
30 | * (libdwfl/argp-std.c, libdwfl/tests/addrcfi.c, etc). | ||
31 | */ | ||
32 | static char *debuginfo_path; | ||
33 | |||
34 | static const Dwfl_Callbacks offline_callbacks = { | ||
35 | .debuginfo_path = &debuginfo_path, | ||
36 | .find_debuginfo = dwfl_standard_find_debuginfo, | ||
37 | .section_address = dwfl_offline_section_address, | ||
38 | }; | ||
39 | |||
40 | |||
41 | /* | ||
42 | * Use the DWARF expression for the Call-frame-address and determine | ||
43 | * if return address is in LR and if a new frame was allocated. | ||
44 | */ | ||
45 | static int check_return_reg(int ra_regno, Dwarf_Frame *frame) | ||
46 | { | ||
47 | Dwarf_Op ops_mem[2]; | ||
48 | Dwarf_Op dummy; | ||
49 | Dwarf_Op *ops = &dummy; | ||
50 | size_t nops; | ||
51 | int result; | ||
52 | |||
53 | result = dwarf_frame_register(frame, ra_regno, ops_mem, &ops, &nops); | ||
54 | if (result < 0) { | ||
55 | pr_debug("dwarf_frame_register() %s\n", dwarf_errmsg(-1)); | ||
56 | return -1; | ||
57 | } | ||
58 | |||
59 | /* | ||
60 | * Check if return address is on the stack. | ||
61 | */ | ||
62 | if (nops != 0 || ops != NULL) | ||
63 | return 0; | ||
64 | |||
65 | /* | ||
66 | * Return address is in LR. Check if a frame was allocated | ||
67 | * but not-yet used. | ||
68 | */ | ||
69 | result = dwarf_frame_cfa(frame, &ops, &nops); | ||
70 | if (result < 0) { | ||
71 | pr_debug("dwarf_frame_cfa() returns %d, %s\n", result, | ||
72 | dwarf_errmsg(-1)); | ||
73 | return -1; | ||
74 | } | ||
75 | |||
76 | /* | ||
77 | * If call frame address is in r1, no new frame was allocated. | ||
78 | */ | ||
79 | if (nops == 1 && ops[0].atom == DW_OP_bregx && ops[0].number == 1 && | ||
80 | ops[0].number2 == 0) | ||
81 | return 1; | ||
82 | |||
83 | /* | ||
84 | * A new frame was allocated but has not yet been used. | ||
85 | */ | ||
86 | return 2; | ||
87 | } | ||
88 | |||
89 | /* | ||
90 | * Get the DWARF frame from the .eh_frame section. | ||
91 | */ | ||
92 | static Dwarf_Frame *get_eh_frame(Dwfl_Module *mod, Dwarf_Addr pc) | ||
93 | { | ||
94 | int result; | ||
95 | Dwarf_Addr bias; | ||
96 | Dwarf_CFI *cfi; | ||
97 | Dwarf_Frame *frame; | ||
98 | |||
99 | cfi = dwfl_module_eh_cfi(mod, &bias); | ||
100 | if (!cfi) { | ||
101 | pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1)); | ||
102 | return NULL; | ||
103 | } | ||
104 | |||
105 | result = dwarf_cfi_addrframe(cfi, pc, &frame); | ||
106 | if (result) { | ||
107 | pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1)); | ||
108 | return NULL; | ||
109 | } | ||
110 | |||
111 | return frame; | ||
112 | } | ||
113 | |||
114 | /* | ||
115 | * Get the DWARF frame from the .debug_frame section. | ||
116 | */ | ||
117 | static Dwarf_Frame *get_dwarf_frame(Dwfl_Module *mod, Dwarf_Addr pc) | ||
118 | { | ||
119 | Dwarf_CFI *cfi; | ||
120 | Dwarf_Addr bias; | ||
121 | Dwarf_Frame *frame; | ||
122 | int result; | ||
123 | |||
124 | cfi = dwfl_module_dwarf_cfi(mod, &bias); | ||
125 | if (!cfi) { | ||
126 | pr_debug("%s(): no CFI - %s\n", __func__, dwfl_errmsg(-1)); | ||
127 | return NULL; | ||
128 | } | ||
129 | |||
130 | result = dwarf_cfi_addrframe(cfi, pc, &frame); | ||
131 | if (result) { | ||
132 | pr_debug("%s(): %s\n", __func__, dwfl_errmsg(-1)); | ||
133 | return NULL; | ||
134 | } | ||
135 | |||
136 | return frame; | ||
137 | } | ||
138 | |||
139 | /* | ||
140 | * Return: | ||
141 | * 0 if return address for the program counter @pc is on stack | ||
142 | * 1 if return address is in LR and no new stack frame was allocated | ||
143 | * 2 if return address is in LR and a new frame was allocated (but not | ||
144 | * yet used) | ||
145 | * -1 in case of errors | ||
146 | */ | ||
147 | static int check_return_addr(const char *exec_file, Dwarf_Addr pc) | ||
148 | { | ||
149 | int rc = -1; | ||
150 | Dwfl *dwfl; | ||
151 | Dwfl_Module *mod; | ||
152 | Dwarf_Frame *frame; | ||
153 | int ra_regno; | ||
154 | Dwarf_Addr start = pc; | ||
155 | Dwarf_Addr end = pc; | ||
156 | bool signalp; | ||
157 | |||
158 | dwfl = dwfl_begin(&offline_callbacks); | ||
159 | if (!dwfl) { | ||
160 | pr_debug("dwfl_begin() failed: %s\n", dwarf_errmsg(-1)); | ||
161 | return -1; | ||
162 | } | ||
163 | |||
164 | if (dwfl_report_offline(dwfl, "", exec_file, -1) == NULL) { | ||
165 | pr_debug("dwfl_report_offline() failed %s\n", dwarf_errmsg(-1)); | ||
166 | goto out; | ||
167 | } | ||
168 | |||
169 | mod = dwfl_addrmodule(dwfl, pc); | ||
170 | if (!mod) { | ||
171 | pr_debug("dwfl_addrmodule() failed, %s\n", dwarf_errmsg(-1)); | ||
172 | goto out; | ||
173 | } | ||
174 | |||
175 | /* | ||
176 | * To work with split debug info files (eg: glibc), check both | ||
177 | * .eh_frame and .debug_frame sections of the ELF header. | ||
178 | */ | ||
179 | frame = get_eh_frame(mod, pc); | ||
180 | if (!frame) { | ||
181 | frame = get_dwarf_frame(mod, pc); | ||
182 | if (!frame) | ||
183 | goto out; | ||
184 | } | ||
185 | |||
186 | ra_regno = dwarf_frame_info(frame, &start, &end, &signalp); | ||
187 | if (ra_regno < 0) { | ||
188 | pr_debug("Return address register unavailable: %s\n", | ||
189 | dwarf_errmsg(-1)); | ||
190 | goto out; | ||
191 | } | ||
192 | |||
193 | rc = check_return_reg(ra_regno, frame); | ||
194 | |||
195 | out: | ||
196 | dwfl_end(dwfl); | ||
197 | return rc; | ||
198 | } | ||
199 | |||
200 | /* | ||
201 | * The callchain saved by the kernel always includes the link register (LR). | ||
202 | * | ||
203 | * 0: PERF_CONTEXT_USER | ||
204 | * 1: Program counter (Next instruction pointer) | ||
205 | * 2: LR value | ||
206 | * 3: Caller's caller | ||
207 | * 4: ... | ||
208 | * | ||
209 | * The value in LR is only needed when it holds a return address. If the | ||
210 | * return address is on the stack, we should ignore the LR value. | ||
211 | * | ||
212 | * Further, when the return address is in the LR, if a new frame was just | ||
213 | * allocated but the LR was not saved into it, then the LR contains the | ||
214 | * caller, slot 4: contains the caller's caller and the contents of slot 3: | ||
215 | * (chain->ips[3]) is undefined and must be ignored. | ||
216 | * | ||
217 | * Use DWARF debug information to determine if any entries need to be skipped. | ||
218 | * | ||
219 | * Return: | ||
220 | * index: of callchain entry that needs to be ignored (if any) | ||
221 | * -1 if no entry needs to be ignored or in case of errors | ||
222 | */ | ||
223 | int arch_skip_callchain_idx(struct machine *machine, struct thread *thread, | ||
224 | struct ip_callchain *chain) | ||
225 | { | ||
226 | struct addr_location al; | ||
227 | struct dso *dso = NULL; | ||
228 | int rc; | ||
229 | u64 ip; | ||
230 | u64 skip_slot = -1; | ||
231 | |||
232 | if (chain->nr < 3) | ||
233 | return skip_slot; | ||
234 | |||
235 | ip = chain->ips[2]; | ||
236 | |||
237 | thread__find_addr_location(thread, machine, PERF_RECORD_MISC_USER, | ||
238 | MAP__FUNCTION, ip, &al); | ||
239 | |||
240 | if (al.map) | ||
241 | dso = al.map->dso; | ||
242 | |||
243 | if (!dso) { | ||
244 | pr_debug("%" PRIx64 " dso is NULL\n", ip); | ||
245 | return skip_slot; | ||
246 | } | ||
247 | |||
248 | rc = check_return_addr(dso->long_name, ip); | ||
249 | |||
250 | pr_debug("DSO %s, nr %" PRIx64 ", ip 0x%" PRIx64 "rc %d\n", | ||
251 | dso->long_name, chain->nr, ip, rc); | ||
252 | |||
253 | if (rc == 0) { | ||
254 | /* | ||
255 | * Return address on stack. Ignore LR value in callchain | ||
256 | */ | ||
257 | skip_slot = 2; | ||
258 | } else if (rc == 2) { | ||
259 | /* | ||
260 | * New frame allocated but return address still in LR. | ||
261 | * Ignore the caller's caller entry in callchain. | ||
262 | */ | ||
263 | skip_slot = 3; | ||
264 | } | ||
265 | return skip_slot; | ||
266 | } | ||
diff --git a/tools/perf/config/Makefile b/tools/perf/config/Makefile index f30ac5e5d271..346bdb617544 100644 --- a/tools/perf/config/Makefile +++ b/tools/perf/config/Makefile | |||
@@ -48,6 +48,10 @@ ifneq ($(ARCH),$(filter $(ARCH),x86 arm)) | |||
48 | NO_LIBDW_DWARF_UNWIND := 1 | 48 | NO_LIBDW_DWARF_UNWIND := 1 |
49 | endif | 49 | endif |
50 | 50 | ||
51 | ifeq ($(ARCH),powerpc) | ||
52 | CFLAGS += -DHAVE_SKIP_CALLCHAIN_IDX | ||
53 | endif | ||
54 | |||
51 | ifeq ($(LIBUNWIND_LIBS),) | 55 | ifeq ($(LIBUNWIND_LIBS),) |
52 | NO_LIBUNWIND := 1 | 56 | NO_LIBUNWIND := 1 |
53 | else | 57 | else |
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h index 8f84423a75da..da43619d6173 100644 --- a/tools/perf/util/callchain.h +++ b/tools/perf/util/callchain.h | |||
@@ -176,4 +176,17 @@ static inline void callchain_cursor_snapshot(struct callchain_cursor *dest, | |||
176 | dest->first = src->curr; | 176 | dest->first = src->curr; |
177 | dest->nr -= src->pos; | 177 | dest->nr -= src->pos; |
178 | } | 178 | } |
179 | |||
180 | #ifdef HAVE_SKIP_CALLCHAIN_IDX | ||
181 | extern int arch_skip_callchain_idx(struct machine *machine, | ||
182 | struct thread *thread, struct ip_callchain *chain); | ||
183 | #else | ||
184 | static inline int arch_skip_callchain_idx(struct machine *machine __maybe_unused, | ||
185 | struct thread *thread __maybe_unused, | ||
186 | struct ip_callchain *chain __maybe_unused) | ||
187 | { | ||
188 | return -1; | ||
189 | } | ||
190 | #endif | ||
191 | |||
179 | #endif /* __PERF_CALLCHAIN_H */ | 192 | #endif /* __PERF_CALLCHAIN_H */ |
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index c73e1fc12e53..e9b943acaa5e 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c | |||
@@ -1281,7 +1281,9 @@ static int machine__resolve_callchain_sample(struct machine *machine, | |||
1281 | u8 cpumode = PERF_RECORD_MISC_USER; | 1281 | u8 cpumode = PERF_RECORD_MISC_USER; |
1282 | int chain_nr = min(max_stack, (int)chain->nr); | 1282 | int chain_nr = min(max_stack, (int)chain->nr); |
1283 | int i; | 1283 | int i; |
1284 | int j; | ||
1284 | int err; | 1285 | int err; |
1286 | int skip_idx __maybe_unused; | ||
1285 | 1287 | ||
1286 | callchain_cursor_reset(&callchain_cursor); | 1288 | callchain_cursor_reset(&callchain_cursor); |
1287 | 1289 | ||
@@ -1290,14 +1292,26 @@ static int machine__resolve_callchain_sample(struct machine *machine, | |||
1290 | return 0; | 1292 | return 0; |
1291 | } | 1293 | } |
1292 | 1294 | ||
1295 | /* | ||
1296 | * Based on DWARF debug information, some architectures skip | ||
1297 | * a callchain entry saved by the kernel. | ||
1298 | */ | ||
1299 | skip_idx = arch_skip_callchain_idx(machine, thread, chain); | ||
1300 | |||
1293 | for (i = 0; i < chain_nr; i++) { | 1301 | for (i = 0; i < chain_nr; i++) { |
1294 | u64 ip; | 1302 | u64 ip; |
1295 | struct addr_location al; | 1303 | struct addr_location al; |
1296 | 1304 | ||
1297 | if (callchain_param.order == ORDER_CALLEE) | 1305 | if (callchain_param.order == ORDER_CALLEE) |
1298 | ip = chain->ips[i]; | 1306 | j = i; |
1299 | else | 1307 | else |
1300 | ip = chain->ips[chain->nr - i - 1]; | 1308 | j = chain->nr - i - 1; |
1309 | |||
1310 | #ifdef HAVE_SKIP_CALLCHAIN_IDX | ||
1311 | if (j == skip_idx) | ||
1312 | continue; | ||
1313 | #endif | ||
1314 | ip = chain->ips[j]; | ||
1301 | 1315 | ||
1302 | if (ip >= PERF_CONTEXT_MAX) { | 1316 | if (ip >= PERF_CONTEXT_MAX) { |
1303 | switch (ip) { | 1317 | switch (ip) { |