From: German Gomez <hidden> Date: 2021-12-17 15:45:33
(This cset applies on top of [1])
Call-graphs on ARM64 using the option "--call-grah fp" are missing the
callers of the leaf functions. See [PATCH 6/6] for a before and after
after example using this cset.
[1] https://lore.kernel.org/all/20211207180653.1147374-1-german.gomez@arm.com/
---
Changes since v4
- [PATCH 4/6] Apply Mark Rutland's comments.
- [PATCH 6/6] Rewrite commit log.
Changes since v3
- Only record LR register instead of all registers in [PATCH 1/6].
- Introduce [PATCH 5/6] to refactor the SAMPL_REG macro.
- Fix compilation issues on different platforms.
Alexandre Truong (5):
perf tools: record ARM64 LR register automatically
perf tools: add a mechanism to inject stack frames
perf tools: Refactor script__setup_sample_type()
perf tools: enable dwarf_callchain_users on arm64
perf tools: determine if LR is the return address
German Gomez (1):
perf tools: Refactor SMPL_REG macro in perf_regs.h
tools/perf/arch/arm64/util/machine.c | 7 +++
tools/perf/builtin-record.c | 8 +++
tools/perf/builtin-report.c | 4 +-
tools/perf/builtin-script.c | 13 +---
tools/perf/util/Build | 1 +
.../util/arm64-frame-pointer-unwind-support.c | 63 +++++++++++++++++++
.../util/arm64-frame-pointer-unwind-support.h | 10 +++
tools/perf/util/callchain.c | 14 ++++-
tools/perf/util/callchain.h | 4 +-
tools/perf/util/machine.c | 50 ++++++++++++++-
tools/perf/util/machine.h | 1 +
tools/perf/util/perf_regs.h | 7 ++-
12 files changed, 162 insertions(+), 20 deletions(-)
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.c
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.h
--
2.25.1
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: German Gomez <hidden> Date: 2021-12-17 15:45:36
From: Alexandre Truong <redacted>
On ARM64, automatically record the link register if the frame pointer
mode is on. It will be used to do a dwarf unwind to find the caller
of the leaf frame if the frame pointer was omitted.
Signed-off-by: Alexandre Truong <redacted>
Signed-off-by: German Gomez <redacted>
---
tools/perf/arch/arm64/util/machine.c | 7 +++++++
tools/perf/builtin-record.c | 8 ++++++++
tools/perf/util/callchain.h | 2 ++
3 files changed, 17 insertions(+)
@@ -5,6 +5,8 @@#include<string.h>#include"debug.h"#include"symbol.h"+#include"callchain.h"+#include"record.h"/* On arm64, kernel text segment starts at high memory address,*forexample0xffff00008xxxxxxx.Modulesstartatalowmemory
@@ -26,3 +28,8 @@ void arch__symbols__fixup_end(struct symbol *p, struct symbol *c)p->end=c->start;pr_debug4("%s sym:%s end:%#"PRIx64"\n",__func__,p->name,p->end);}++voidarch__add_leaf_frame_record_opts(structrecord_opts*opts)+{+opts->sample_user_regs|=sample_reg_masks[PERF_REG_ARM64_LR].mask;+}
From: German Gomez <hidden> Date: 2021-12-17 15:45:40
From: Alexandre Truong <redacted>
Add a mechanism for platforms to inject stack frames for the leaf
frame caller if there is enough information to determine a frame
is missing from dwarf or other post processing mechanisms.
Signed-off-by: Alexandre Truong <redacted>
Signed-off-by: German Gomez <redacted>
---
tools/perf/util/machine.c | 37 ++++++++++++++++++++++++++++++++++++-
1 file changed, 36 insertions(+), 1 deletion(-)
From: German Gomez <hidden> Date: 2021-12-17 15:45:49
From: Alexandre Truong <redacted>
On arm64, enable dwarf_callchain_users which will be needed
to do a dwarf unwind in order to get the caller of the leaf frame.
Signed-off-by: Alexandre Truong <redacted>
Signed-off-by: German Gomez <redacted>
---
tools/perf/builtin-report.c | 4 ++--
tools/perf/builtin-script.c | 4 ++--
tools/perf/util/callchain.c | 14 +++++++++++++-
tools/perf/util/callchain.h | 2 +-
4 files changed, 18 insertions(+), 6 deletions(-)
From: German Gomez <hidden> Date: 2021-12-17 15:45:53
Refactor the SAMPL_REG macro so that it can be used in a followup commit
to obtain the masks for ARM64 registers.
Signed-off-by: German Gomez <redacted>
---
tools/perf/util/perf_regs.h | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
From: German Gomez <hidden> Date: 2021-12-17 15:45:56
From: Alexandre Truong <redacted>
When unwinding using frame pointers on ARM64, the return address of the
current function may not have been pushed into the stack when a function
was interrupted, which makes perf show an incorrect call graph to the
user.
Consider the following example program:
void leaf() {
/* long computation */
}
void parent() {
// (1)
leaf();
// (2)
}
... could be compiled into (using gcc -fno-inline -fno-omit-frame-pointer):
leaf:
/* long computation */
nop
ret
parent:
// (1)
stp x29, x30, [sp, -16]!
mov x29, sp
bl parent
nop
ldp x29, x30, [sp], 16
// (2)
ret
If the program is interrupted at (1), (2), or any point in "leaf:", the
call graph will skip the callers of the current function. We can unwind
using the dwarf info and check if the return addr is the same as the LR
register, and inject the missing frame into the call graph.
Before this patch, the above example shows the following call-graph when
recording using "--call-graph fp" mode in ARM64:
# Children Self Command Shared Object Symbol
# ........ ........ ........ ................ ......................
#
99.86% 99.86% program3 program3 [.] leaf
|
---_start
__libc_start_main
main
leaf
As can be seen, the "parent" function is missing. This is specially
problematic in "leaf" because for leaf functions the compiler may always
omit pushing the return addr into the stack. After this patch, it shows
the correct graph:
# Children Self Command Shared Object Symbol
# ........ ........ ........ ................ ......................
#
99.86% 99.86% program3 program3 [.] leaf
|
---_start
__libc_start_main
main
parent
leaf
Signed-off-by: Alexandre Truong <redacted>
Signed-off-by: German Gomez <redacted>
---
tools/perf/util/Build | 1 +
.../util/arm64-frame-pointer-unwind-support.c | 63 +++++++++++++++++++
.../util/arm64-frame-pointer-unwind-support.h | 10 +++
tools/perf/util/machine.c | 19 ++++--
tools/perf/util/machine.h | 1 +
5 files changed, 89 insertions(+), 5 deletions(-)
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.c
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.h
From: James Clark <hidden> Date: 2021-12-17 16:01:43
On 17/12/2021 15:45, German Gomez wrote:
quoted hunk
From: Alexandre Truong <redacted>
When unwinding using frame pointers on ARM64, the return address of the
current function may not have been pushed into the stack when a function
was interrupted, which makes perf show an incorrect call graph to the
user.
Consider the following example program:
void leaf() {
/* long computation */
}
void parent() {
// (1)
leaf();
// (2)
}
... could be compiled into (using gcc -fno-inline -fno-omit-frame-pointer):
leaf:
/* long computation */
nop
ret
parent:
// (1)
stp x29, x30, [sp, -16]!
mov x29, sp
bl parent
nop
ldp x29, x30, [sp], 16
// (2)
ret
If the program is interrupted at (1), (2), or any point in "leaf:", the
call graph will skip the callers of the current function. We can unwind
using the dwarf info and check if the return addr is the same as the LR
register, and inject the missing frame into the call graph.
Before this patch, the above example shows the following call-graph when
recording using "--call-graph fp" mode in ARM64:
# Children Self Command Shared Object Symbol
# ........ ........ ........ ................ ......................
#
99.86% 99.86% program3 program3 [.] leaf
|
---_start
__libc_start_main
main
leaf
As can be seen, the "parent" function is missing. This is specially
problematic in "leaf" because for leaf functions the compiler may always
omit pushing the return addr into the stack. After this patch, it shows
the correct graph:
# Children Self Command Shared Object Symbol
# ........ ........ ........ ................ ......................
#
99.86% 99.86% program3 program3 [.] leaf
|
---_start
__libc_start_main
main
parent
leaf
Signed-off-by: Alexandre Truong <redacted>
Signed-off-by: German Gomez <redacted>
---
tools/perf/util/Build | 1 +
.../util/arm64-frame-pointer-unwind-support.c | 63 +++++++++++++++++++
.../util/arm64-frame-pointer-unwind-support.h | 10 +++
tools/perf/util/machine.c | 19 ++++--
tools/perf/util/machine.h | 1 +
5 files changed, 89 insertions(+), 5 deletions(-)
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.c
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.h
@@ -3114,14 +3118,19 @@ int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,}/*-*Comparestherawarchstring.N.B.seeinsteadperf_env__arch()ifa-*normalizedarchisneeded.+*Comparestherawarchstring.N.B.seeinsteadperf_env__arch()or+*machine__normalize_is()ifanormalizedarchisneeded.*/boolmachine__is(structmachine*machine,constchar*arch){returnmachine&&!strcmp(perf_env__raw_arch(machine->env),arch);}+boolmachine__normalize_is(structmachine*machine,constchar*arch)+{+returnmachine&&!strcmp(perf_env__arch(machine->env),arch);+}+
I think this function name would be clearer as something like "machine__normalized_is" or
"machine__normalized_arch_is". The tense is slightly off because it's a test rather than a
verb.
With that change, for the whole set:
Reviewed-by: James Clark <redacted>
From: Arnaldo Carvalho de Melo <acme@kernel.org> Date: 2021-12-18 11:35:12
Em Fri, Dec 17, 2021 at 04:01:38PM +0000, James Clark escreveu:
On 17/12/2021 15:45, German Gomez wrote:
quoted
From: Alexandre Truong <redacted>
When unwinding using frame pointers on ARM64, the return address of the
current function may not have been pushed into the stack when a function
was interrupted, which makes perf show an incorrect call graph to the
user.
Consider the following example program:
void leaf() {
/* long computation */
}
void parent() {
// (1)
leaf();
// (2)
}
... could be compiled into (using gcc -fno-inline -fno-omit-frame-pointer):
leaf:
/* long computation */
nop
ret
parent:
// (1)
stp x29, x30, [sp, -16]!
mov x29, sp
bl parent
nop
ldp x29, x30, [sp], 16
// (2)
ret
If the program is interrupted at (1), (2), or any point in "leaf:", the
call graph will skip the callers of the current function. We can unwind
using the dwarf info and check if the return addr is the same as the LR
register, and inject the missing frame into the call graph.
Before this patch, the above example shows the following call-graph when
recording using "--call-graph fp" mode in ARM64:
# Children Self Command Shared Object Symbol
# ........ ........ ........ ................ ......................
#
99.86% 99.86% program3 program3 [.] leaf
|
---_start
__libc_start_main
main
leaf
As can be seen, the "parent" function is missing. This is specially
problematic in "leaf" because for leaf functions the compiler may always
omit pushing the return addr into the stack. After this patch, it shows
the correct graph:
# Children Self Command Shared Object Symbol
# ........ ........ ........ ................ ......................
#
99.86% 99.86% program3 program3 [.] leaf
|
---_start
__libc_start_main
main
parent
leaf
Signed-off-by: Alexandre Truong <redacted>
Signed-off-by: German Gomez <redacted>
---
tools/perf/util/Build | 1 +
.../util/arm64-frame-pointer-unwind-support.c | 63 +++++++++++++++++++
.../util/arm64-frame-pointer-unwind-support.h | 10 +++
tools/perf/util/machine.c | 19 ++++--
tools/perf/util/machine.h | 1 +
5 files changed, 89 insertions(+), 5 deletions(-)
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.c
create mode 100644 tools/perf/util/arm64-frame-pointer-unwind-support.h
@@ -3114,14 +3118,19 @@ int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,}/*-*Comparestherawarchstring.N.B.seeinsteadperf_env__arch()ifa-*normalizedarchisneeded.+*Comparestherawarchstring.N.B.seeinsteadperf_env__arch()or+*machine__normalize_is()ifanormalizedarchisneeded.*/boolmachine__is(structmachine*machine,constchar*arch){returnmachine&&!strcmp(perf_env__raw_arch(machine->env),arch);}+boolmachine__normalize_is(structmachine*machine,constchar*arch)+{+returnmachine&&!strcmp(perf_env__arch(machine->env),arch);+}+
I think this function name would be clearer as something like "machine__normalized_is" or
"machine__normalized_arch_is". The tense is slightly off because it's a test rather than a
verb.
Agreed, its a question, not a command.
- Arnaldo
With that change, for the whole set:
Reviewed-by: James Clark <redacted>
From: Jiri Olsa <hidden> Date: 2021-12-21 09:32:57
On Fri, Dec 17, 2021 at 03:45:20PM +0000, German Gomez wrote:
SNIP
+}
+
+u64 get_leaf_frame_caller_aarch64(struct perf_sample *sample, struct thread *thread, int usr_idx)
+{
+ int ret;
+ struct entries entries = {};
+ struct regs_dump old_regs = sample->user_regs;
+
+ if (!get_leaf_frame_caller_enabled(sample))
+ return 0;
+
+ /*
+ * If PC and SP are not recorded, get the value of PC from the stack
+ * and set its mask. SP is not used when doing the unwinding but it
+ * still needs to be set to prevent failures.
+ */
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_PC))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_PC);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_PC] = sample->callchain->ips[usr_idx+1];
+ }
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_SP))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_SP);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_SP] = 0;
+ }
+
+ ret = unwind__get_entries(add_entry, &entries, thread, sample, 2);
just curious, did you try this with both unwinders libunwind/libdw?
any chance you could add arm specific test for this?
otherwise it looks good to me
Acked-by: Jiri Olsa <jolsa@kernel.org>
thanks,
jirka
From: Arnaldo Carvalho de Melo <acme@kernel.org> Date: 2021-12-21 14:56:40
Em Tue, Dec 21, 2021 at 10:32:50AM +0100, Jiri Olsa escreveu:
On Fri, Dec 17, 2021 at 03:45:20PM +0000, German Gomez wrote:
SNIP
quoted
+}
+
+u64 get_leaf_frame_caller_aarch64(struct perf_sample *sample, struct thread *thread, int usr_idx)
+{
+ int ret;
+ struct entries entries = {};
+ struct regs_dump old_regs = sample->user_regs;
+
+ if (!get_leaf_frame_caller_enabled(sample))
+ return 0;
+
+ /*
+ * If PC and SP are not recorded, get the value of PC from the stack
+ * and set its mask. SP is not used when doing the unwinding but it
+ * still needs to be set to prevent failures.
+ */
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_PC))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_PC);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_PC] = sample->callchain->ips[usr_idx+1];
+ }
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_SP))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_SP);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_SP] = 0;
+ }
+
+ ret = unwind__get_entries(add_entry, &entries, thread, sample, 2);
just curious, did you try this with both unwinders libunwind/libdw?
any chance you could add arm specific test for this?
otherwise it looks good to me
Whole patchkit?
Acked-by: Jiri Olsa <jolsa@kernel.org>
thanks,
jirka
From: Jiri Olsa <hidden> Date: 2021-12-21 15:06:23
On Tue, Dec 21, 2021 at 11:17:03AM -0300, Arnaldo Carvalho de Melo wrote:
Em Tue, Dec 21, 2021 at 10:32:50AM +0100, Jiri Olsa escreveu:
quoted
On Fri, Dec 17, 2021 at 03:45:20PM +0000, German Gomez wrote:
SNIP
quoted
+}
+
+u64 get_leaf_frame_caller_aarch64(struct perf_sample *sample, struct thread *thread, int usr_idx)
+{
+ int ret;
+ struct entries entries = {};
+ struct regs_dump old_regs = sample->user_regs;
+
+ if (!get_leaf_frame_caller_enabled(sample))
+ return 0;
+
+ /*
+ * If PC and SP are not recorded, get the value of PC from the stack
+ * and set its mask. SP is not used when doing the unwinding but it
+ * still needs to be set to prevent failures.
+ */
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_PC))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_PC);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_PC] = sample->callchain->ips[usr_idx+1];
+ }
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_SP))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_SP);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_SP] = 0;
+ }
+
+ ret = unwind__get_entries(add_entry, &entries, thread, sample, 2);
just curious, did you try this with both unwinders libunwind/libdw?
any chance you could add arm specific test for this?
otherwise it looks good to me
Whole patchkit?
yes, it's for the patchset
jirka
quoted
Acked-by: Jiri Olsa <jolsa@kernel.org>
thanks,
jirka
From: German Gomez <hidden> Date: 2022-01-10 10:49:15
Hi Jiri,
On 21/12/2021 09:32, Jiri Olsa wrote:
On Fri, Dec 17, 2021 at 03:45:20PM +0000, German Gomez wrote:
SNIP
quoted
+}
+
+u64 get_leaf_frame_caller_aarch64(struct perf_sample *sample, struct thread *thread, int usr_idx)
+{
+ int ret;
+ struct entries entries = {};
+ struct regs_dump old_regs = sample->user_regs;
+
+ if (!get_leaf_frame_caller_enabled(sample))
+ return 0;
+
+ /*
+ * If PC and SP are not recorded, get the value of PC from the stack
+ * and set its mask. SP is not used when doing the unwinding but it
+ * still needs to be set to prevent failures.
+ */
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_PC))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_PC);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_PC] = sample->callchain->ips[usr_idx+1];
+ }
+
+ if (!(sample->user_regs.mask & SMPL_REG_MASK(PERF_REG_ARM64_SP))) {
+ sample->user_regs.cache_mask |= SMPL_REG_MASK(PERF_REG_ARM64_SP);
+ sample->user_regs.cache_regs[PERF_REG_ARM64_SP] = 0;
+ }
+
+ ret = unwind__get_entries(add_entry, &entries, thread, sample, 2);
just curious, did you try this with both unwinders libunwind/libdw?
Yes I did,
This is the program I was using:
int a = 0;
void leaf() {
for (int i = 0; i < 10000000; i++)
a *= a;
}
void parent() {
leaf();
}
int main() {
parent();
}
... compiled with "gcc -O0 -fno-omit-frame-pointer -fno-inline program.c"
any chance you could add arm specific test for this?
I don't see a reason not to. I'll make a note for a separate patch.
otherwise it looks good to me
Acked-by: Jiri Olsa <jolsa@kernel.org>