perf report: fix off-by-one for non-activation frames
PATCH 1/1 perf report: fix off-by-one for non-activation frames
As the documentation for dwfl_frame_pc says, frames that are no activation frames need to have their program counter decremented by one to properly find the function of the caller.
This fixes many cases where perf report currently attributes the cost to the next line. I.e. I have code like this:
~~~~~~~~~~~~~~~
#include <thread>
#include <chrono>
using namespace std;
int main()
{
this_thread::sleep_for(chrono::milliseconds(1000));
this_thread::sleep_for(chrono::milliseconds(100));
this_thread::sleep_for(chrono::milliseconds(10));return 0;
}
~~~~~~~~~~~~~~~
Now compile and record it:
~~~~~~~~~~~~~~~
g++ -std=c++11 -g -O2 test.cpp
echo 1 | sudo tee /proc/sys/kernel/sched_schedstats
perf record \
--event sched:sched_stat_sleep \
--event sched:sched_process_exit \
--event sched:sched_switch --call-graph=dwarf \
--output perf.data.raw \
./a.outecho 0 | sudo tee /proc/sys/kernel/sched_schedstats perf inject --sched-stat --input perf.data.raw --output perf.data
~~~~~~~~~~~~~~~
Before this patch, the report clearly shows the off-by-one issue. Most notably, the last sleep invocation is incorrectly attributed to the "return 0;" line:
~~~~~~~~~~~~~~~
Overhead Source:Line
........ ...........
100.00% core.c:0
|
---__schedule core.c:0
schedule
do_nanosleep hrtimer.c:0
hrtimer_nanosleep
sys_nanosleep
entry_SYSCALL_64_fastpath .tmp_entry_64.o:0
__nanosleep_nocancel .:0
std::this_thread::sleep_for<long, std::ratio<1l, 1000l> > thread:323
|
|--90.08%--main test.cpp:9
| __libc_start_main
| _start
|
|--9.01%--main test.cpp:10
| __libc_start_main
| _start
|
--0.91%--main test.cpp:13
__libc_start_main
_start~~~~~~~~~~~~~~~
When compiling perf using libdwfl for unwinding instead of libunwind and having this patch here applied, the issue is fixed. The report becomes much more usable:
~~~~~~~~~~~~~~~
Overhead Source:Line
........ ...........
100.00% core.c:0
|
---__schedule core.c:0
schedule
do_nanosleep hrtimer.c:0
hrtimer_nanosleep
sys_nanosleep
entry_SYSCALL_64_fastpath .tmp_entry_64.o:0
__nanosleep_nocancel .:0
std::this_thread::sleep_for<long, std::ratio<1l, 1000l> > thread:323
|
|--90.08%--main test.cpp:8
| __libc_start_main
| _start
|
|--9.01%--main test.cpp:9
| __libc_start_main
| _start
|
--0.91%--main test.cpp:10
__libc_start_main
_start~~~~~~~~~~~~~~~
--- tools/perf/util/unwind-libdw.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/util/unwind-libdw.c +5 -1
@@ -168,12 +168,16 @@ frame_callback(Dwfl_Frame *state, void *arg) {struct unwind_info *ui = arg;Dwarf_Addr pc; +bool isactivation; -if (!dwfl_frame_pc (state, &pc,NULL )) { +if (!dwfl_frame_pc (state, &pc, &isactivation)) {pr_err ("%s" ,dwfl_errmsg (-1 ));return DWARF_CB_ABORT ; } +if (!isactivation) + --pc; +return entry (pc, ui) || !(--ui->max_stack) ?DWARF_CB_ABORT :DWARF_CB_OK ; }
Signature · 1 line
2.13.0
Quoted 3 lines · CEST Milian Wolff
As the documentation for
dwfl_frame_pcsays, frames that are no activation frames need to have their program counter decremented by one to properly find the function of the caller.
Note that this leaves the perf build against libunwind in the current, broken state. I do not know how to detect the activation property there. Does anyone else? See elfutils source code for what it is doing:
https://sourceware.org/git/?p=elfutils.git;a=blob;f=libdwfl/
dwfl_frame_pc.c;h=296c815b9c73f42d79ac1778d2a0c420b89ee4eb;hb=HEAD
Cheers
Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt Experts
Namhyung Kim
maintainerQuoted 11 lines · Milian Wolff
Quoted 3 lines · CEST Milian Wolff
As the documentation for
dwfl_frame_pcsays, frames that are no activation frames need to have their program counter decremented by one to properly find the function of the caller.Note that this leaves the perf build against libunwind in the current, broken state. I do not know how to detect the activation property there. Does anyone else? See elfutils source code for what it is doing:
https://sourceware.org/git/?p=elfutils.git;a=blob;f=libdwfl/
dwfl_frame_pc.c;h=296c815b9c73f42d79ac1778d2a0c420b89ee4eb;hb=HEAD
It seems that you can use unw_is_signal_frame().
Thanks,
Namhyung
Quoted 6 lines · On Dienstag, 16. Mai 2017 03:57:53 CEST Namhyung Kim wrote:
Quoted 5 lines · Milian Wolff
Quoted 3 lines · CEST Milian Wolff
As the documentation for
dwfl_frame_pcsays, frames that are no activation frames need to have their program counter decremented by one to properly find the function of the caller.
Quoted 8 lines
Quoted 6 lines
Note that this leaves the perf build against libunwind in the current, broken state. I do not know how to detect the activation property there. Does anyone else? See elfutils source code for what it is doing:
https://sourceware.org/git/?p=elfutils.git;a=blob;f=libdwfl/
dwfl_frame_pc.c;h=296c815b9c73f42d79ac1778d2a0c420b89ee4eb;hb=HEADIt seems that you can use
unw_is_signal_frame().
Thank you! The v2 patch I just sent now uses that and it seems to work reliably in my quick test.
Cheers
Signature · 4 lines
Milian Wolff | [email protected] | Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt Experts
On Mon, 15 May 2017 17:04:44 +0200, Milian Wolff wrote:
commit 1982ad48fc82c284a5cc55697a012d3357e84d01
Author: Milian Wolff <[email protected]> Date: Wed May 24 15:21:25 2017 +0900
Quoted 3 lines
tools/perf/util/unwind-libdw.c +0
-0@@ -168,12 +168,16 @@ frame_callback(Dwfl_Frame *state, void *arg)
...
tools/perf/util/unwind-libdw.c frame_callback(Dwfl_Frame *state, void *arg) · line 180
Quoted 3 lines
tools/perf/util/unwind-libdw.c +3
-0+if (!isactivation) + --pc; +
FYI I find it as a regression a bit:
perf-4.11.4-200.fc25.x86_64
30c563 gdb_main (/usr/libexec/gdb)
fae48 main (/usr/libexec/gdb)
0x000055555564ee43 <+51>: callq 0x55555585f340 <gdb_main(captured_main_args*)> 0x000055555564ee48 <+56>: mov 0x18(%rsp),%rcx
perf-4.12.0-0.rc5.git0.1.fc27.x86_64
39e32e gdb_main (/usr/libexec/gdb)
10b6fa main (/usr/libexec/gdb)
0x000055555565f6f6 <+54>: callq 0x5555558f17a0 <gdb_main(captured_main_args*)>
0x000055555565f6fb <+59>: mov 0x18(%rsp),%rcxIn backtraces it is correct to show the source line of the calling line - as perf does now after your fix - but one still should report PC address of the start of the next instruction. At least this is what debuggers are used to do:
#9 gdb_main (args=0x7fffffffe2e0) at ../../gdb/main.c:1257
#10 0x000055555565f6fb in main (argc=<optimized out>, argv=<optimized out>) at ../../gdb/gdb.c:40
0x000055555565f6f6 <+54>: callq 0x5555558f17a0 <gdb_main(captured_main_args*)>
=> 0x000055555565f6fb <+59>: mov 0x18(%rsp),%rcx
Line 40 of "../../gdb/gdb.c" starts at address 0x55555565f6f6 <main(int, char**)+54> and ends at 0x55555565f6fb <main(int, char**)+59>. Line 41 of "../../gdb/gdb.c" starts at address 0x55555565f6fb <main(int, char**)+59> and ends at 0x55555565f715.
You see "gdb.c:40" and 0x000055555565f6fb in the backtrace despite 0x55555565f6fb is already line 41.
This is also why elfutils reports separately PC and 'isactivation' flag. Instead of just reporting decreased PC.
Jan
Quoted 47 lines · On Freitag, 16. Juni 2017 08:14:56 CEST Jan Kratochvil wrote:
On Mon, 15 May 2017 17:04:44 +0200, Milian Wolff wrote:
commit
1982ad48fc82c284a5cc55697a012d3357e84d01Author: Milian Wolff <[email protected]> Date: Wed May 24 15:21:25 2017 +0900Quoted 3 lines
tools/perf/util/unwind-libdw.c +0
-0@@ -168,12 +168,16 @@ frame_callback(Dwfl_Frame *state, void *arg)...
tools/perf/util/unwind-libdw.c frame_callback(Dwfl_Frame *state, void *arg) · line 180
Quoted 3 lines
tools/perf/util/unwind-libdw.c +3
-0+if (!isactivation) + --pc; +FYI I find it as a regression a bit:
perf-4.11.4-200.fc25.x86_64
30c563gdb_main(/usr/libexec/gdb)
fae48 main (/usr/libexec/gdb)
0x000055555564ee43<+51>: callq0x55555585f340
<gdb_main(captured_main_args*)>0x000055555564ee48<+56>: mov
0x18(%rsp),%rcxperf-4.12.0-0.rc5.git0.1.fc27.x86_64
39e32e gdb_main (/usr/libexec/gdb) 10b6fa main (/usr/libexec/gdb)
0x000055555565f6f6<+54>: callq0x5555558f17a0
<gdb_main(captured_main_args*)>0x000055555565f6fb<+59>: mov
0x18(%rsp),%rcxIn backtraces it is correct to show the source line of the calling line - as perf does now after your fix - but one still should report PC address of the start of the next instruction. At least this is what debuggers are used to do:
#9
gdb_main(args=0x7fffffffe2e0) at../../gdb/main.c:1257
#100x000055555565f6fbin main (argc=<optimized out>, argv=<optimized out>)
at../../gdb/gdb.c:400x000055555565f6f6<+54>: callq0x5555558f17a0
<gdb_main(captured_main_args*)> =>0x000055555565f6fb<+59>: mov
0x18(%rsp),%rcx
Line 40 of "../../gdb/gdb.c" starts at address0x55555565f6f6<main(int, char**)+54> and ends at0x55555565f6fb<main(int, char**)+59>. Line 41 of "../../gdb/gdb.c" starts at address0x55555565f6fb<main(int, char**)+59> and ends at0x55555565f715.
Quoted 5 lines
You see "gdb.c:40" and
0x000055555565f6fbin the backtrace despite0x55555565f6fbis already line 41.This is also why elfutils reports separately PC and 'isactivation' flag. Instead of just reporting decreased PC.
Excuse me, but I'm having trouble following you. The non-GDB backtraces you are pasting do not show srcline information. So what exactly is broken? Can you show me the differences a bit more clearly? Maybe paste the perf output you get now and highlight what you'd expect instead? Best would be an accompanying test case that I can use to improve the situation, if possible?
Thanks
Signature · 4 lines
Milian Wolff | [email protected] | Senior Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt Experts
Quoted 5 lines · Milian Wolff
Quoted 5 lines
perf-4.12.0-0.rc5.git0.1.fc27.x86_64
39e32e gdb_main (/usr/libexec/gdb) 10b6fa main (/usr/libexec/gdb) 0x000055555565f6f6 <+54>: callq 0x5555558f17a0 <gdb_main(captured_main_args*) 0x000055555565f6fb <+59>: mov 0x18(%rsp),%rcx
[...]
Quoted 2 lines
Excuse me, but I'm having trouble following you. The non-GDB backtraces you are pasting do not show srcline information. So what exactly is broken?
There is broken that perf now reports address 10b6fa (corresponding to relocated address 0x000055555565f6fa) but there is no instruction on address 0x000055555565f6fa. If you 'objdump -d' it you cannot find any instruction on adress 0x000055555565f6fa (or on address 0x10b6fa). There is instruction on address 0x000055555565f6fb.
Quoted 2 lines
Maybe paste the perf output you get now and highlight what you'd expect instead?
Actual:
39e32e gdb_main (/usr/libexec/gdb)
10b6fa main (/usr/libexec/gdb)
Expected:
39e32f gdb_main (/usr/libexec/gdb)
10b6fb main (/usr/libexec/gdb)I agree perf needs to calculate with 39e32e and 10b6fa. But it should display to user 39e32f and 10b6fb.
Jan
Quoted 26 lines · On Freitag, 16. Juni 2017 13:57:44 CEST Jan Kratochvil wrote:
Quoted 8 lines · Milian Wolff
Quoted 8 lines
perf-4.12.0-0.rc5.git0.1.fc27.x86_64
39e32e gdb_main (/usr/libexec/gdb) 10b6fa main (/usr/libexec/gdb)0x000055555565f6f6 <+54>: callq 0x5555558f17a0 <gdb_main(captured_main_args*) 0x000055555565f6fb <+59>: mov 0x18(%rsp),%rcx[...]
Quoted 3 lines
Excuse me, but I'm having trouble following you. The non-GDB backtraces you
are pasting do not show srcline information. So what exactly is broken?There is broken that perf now reports address 10b6fa (corresponding to relocated address
0x000055555565f6fa) but there is no instruction on address0x000055555565f6fa. If you 'objdump -d' it you cannot find any instruction on adress0x000055555565f6fa(or on address0x10b6fa). There is instruction on address0x000055555565f6fb.Quoted 2 lines
Maybe paste the perf output you get now and highlight what you'd expect instead?
Actual:
Quoted 8 lines
39e32e gdb_main (/usr/libexec/gdb) 10b6fa main (/usr/libexec/gdb) Expected: 39e32f gdb_main (/usr/libexec/gdb) 10b6fb main (/usr/libexec/gdb)I agree perf needs to calculate with 39e32e and 10b6fa. But it should display to user 39e32f and 10b6fb.
Hmmm this will require some more changes throughout the stack then. I.e. we'll have to remember the "isactivation" flag along with the original IP, and only apply the offset then when we query for inliners or srcline information. Maybe I can pull that off somehow in the patch series I'm working on currently, which refactors the whole inline/srcline/callchain logic anyways.
I don't see an easy way to fix the behavior. Does anyone else? So how do we deal with this situation in the interim? I'd prefer we keep the current "broken" state, as I consider it less broken than what we had before... I guess some of the core perf developers should decide how to handle this.
Thanks
Signature · 4 lines
Milian Wolff | [email protected] | Senior Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt Experts
Namhyung Kim
maintainerQuoted 42 lines · Milian Wolff
Quoted 26 lines · On Freitag, 16. Juni 2017 13:57:44 CEST Jan Kratochvil wrote:
Quoted 8 lines · Milian Wolff
Quoted 8 lines
perf-4.12.0-0.rc5.git0.1.fc27.x86_64
39e32e gdb_main (/usr/libexec/gdb) 10b6fa main (/usr/libexec/gdb)0x000055555565f6f6 <+54>: callq 0x5555558f17a0 <gdb_main(captured_main_args*) 0x000055555565f6fb <+59>: mov 0x18(%rsp),%rcx[...]
Quoted 3 lines
Excuse me, but I'm having trouble following you. The non-GDB backtraces you
are pasting do not show srcline information. So what exactly is broken?There is broken that perf now reports address 10b6fa (corresponding to relocated address
0x000055555565f6fa) but there is no instruction on address0x000055555565f6fa. If you 'objdump -d' it you cannot find any instruction on adress0x000055555565f6fa(or on address0x10b6fa). There is instruction on address0x000055555565f6fb.Quoted 2 lines
Maybe paste the perf output you get now and highlight what you'd expect instead?
Actual:
Quoted 8 lines
39e32e gdb_main (/usr/libexec/gdb) 10b6fa main (/usr/libexec/gdb) Expected: 39e32f gdb_main (/usr/libexec/gdb) 10b6fb main (/usr/libexec/gdb)I agree perf needs to calculate with 39e32e and 10b6fa. But it should display to user 39e32f and 10b6fb.
Hmmm this will require some more changes throughout the stack then. I.e. we'll have to remember the "isactivation" flag along with the original IP, and only apply the offset then when we query for inliners or srcline information. Maybe I can pull that off somehow in the patch series I'm working on currently, which refactors the whole inline/srcline/callchain logic anyways.
Quoted 4 lines
I don't see an easy way to fix the behavior. Does anyone else? So how do we deal with this situation in the interim? I'd prefer we keep the current "broken" state, as I consider it less broken than what we had before... I guess some of the core perf developers should decide how to handle this.
Not sure whether it needs be fixed or not. If we fix it, srcline and address would not match so it can give its own confusion to users. Ideally it should display an addressof the instruction before the address IMHO.
Thanks,
Namhyung
Quoted 4 lines · Namhyung Kim
Not sure whether it needs be fixed or not. If we fix it, srcline and address would not match so it can give its own confusion to users. Ideally it should display an addressof the instruction before the address IMHO.
One can figure million ways how it can behave and each one has its pros and cons. I was just describing the current behavior of GDB and LLDB which people are used to already.
Jan
Quoted 9 lines · On Samstag, 17. Juni 2017 10:04:02 CEST Jan Kratochvil wrote:
Quoted 4 lines · Namhyung Kim
Not sure whether it needs be fixed or not. If we fix it, srcline and address would not match so it can give its own confusion to users. Ideally it should display an addressof the instruction before the address IMHO.
One can figure million ways how it can behave and each one has its pros and cons. I was just describing the current behavior of GDB and LLDB which people are used to already.
Personally, I agree with Jan that we should mimick existing tool's behavior. I just fear that it's not trivial to do it with the current code base...
Signature · 4 lines
Milian Wolff | [email protected] | Senior Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt Experts
Em Sat, Jun 17, 2017 at 01:13:11PM +0200, Milian Wolff escreveu:
Quoted 7 lines · On Samstag, 17. Juni 2017 10:04:02 CEST Jan Kratochvil wrote:
Quoted 6 lines · Namhyung Kim
Quoted 4 lines
Not sure whether it needs be fixed or not. If we fix it, srcline and address would not match so it can give its own confusion to users. Ideally it should display an addressof the instruction before the address IMHO.
Quoted 6 lines
Quoted 3 lines
One can figure million ways how it can behave and each one has its pros and cons. I was just describing the current behavior of GDB and LLDB which people are used to already.
Personally, I agree with Jan that we should mimick existing tool's behavior. I just fear that it's not trivial to do it with the current code base...
But we agree it is a worthwhile change (have backtraces in perf match what gdb, etc show), right?
If you can, please try to do this, your attempt will help us understand more the extent of the changes needed and perhaps someonw can come up with simplifications...
- Arnaldo
Quoted 16 lines · CEST Arnaldo Carvalho de Melo
Em Sat, Jun 17, 2017 at 01:13:11PM +0200, Milian Wolff escreveu:
Quoted 7 lines · On Samstag, 17. Juni 2017 10:04:02 CEST Jan Kratochvil wrote:
Quoted 6 lines · Namhyung Kim
Quoted 4 lines
Not sure whether it needs be fixed or not. If we fix it, srcline and address would not match so it can give its own confusion to users. Ideally it should display an addressof the instruction before the address IMHO.
Quoted 8 lines
Quoted 4 lines
One can figure million ways how it can behave and each one has its pros and
cons. I was just describing the current behavior of GDB and LLDB which people are used to already.Personally, I agree with Jan that we should mimick existing tool's behavior. I just fear that it's not trivial to do it with the current code base...
Quoted 6 lines
But we agree it is a worthwhile change (have backtraces in perf match what gdb, etc show), right?
If you can, please try to do this, your attempt will help us understand more the extent of the changes needed and perhaps someonw can come up with simplifications...
Sorry for the (long) delay, but I'm sadly busy on other things right now. I have this on my radar and will try to find time to look into it. But it probably won't happen before end of July. If this is urgent, maybe someone else needs to look into it before me.
Cheers
Signature · 4 lines
Milian Wolff | [email protected] | Senior Software Engineer KDAB (Deutschland) GmbH&Co KG, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt Experts
Quoted 20 lines · Milian Wolff
Quoted 19 lines · CEST Arnaldo Carvalho de Melo
Em Sat, Jun 17, 2017 at 01:13:11PM +0200, Milian Wolff escreveu:
Quoted 7 lines · On Samstag, 17. Juni 2017 10:04:02 CEST Jan Kratochvil wrote:
Quoted 6 lines · Namhyung Kim
Quoted 4 lines
Not sure whether it needs be fixed or not. If we fix it, srcline and address would not match so it can give its own confusion to users. Ideally it should display an addressof the instruction before the address IMHO.
Quoted 8 lines
Quoted 4 lines
One can figure million ways how it can behave and each one has its pros and
cons. I was just describing the current behavior of GDB and LLDB which people are used to already.Personally, I agree with Jan that we should mimick existing tool's behavior. I just fear that it's not trivial to do it with the current code base...
But we agree it is a worthwhile change (have backtraces in perf match what gdb, etc show), right?
Quoted 8 lines
Quoted 3 lines
If you can, please try to do this, your attempt will help us understand more the extent of the changes needed and perhaps someonw can come up with simplifications...
Sorry for the (long) delay, but I'm sadly busy on other things right now. I have this on my radar and will try to find time to look into it. But it probably won't happen before end of July. If this is urgent, maybe someone else needs to look into it before me.
I ran into the odd "decremented by one" IPs (and function offsets) on s390, which requires instructions to be aligned on 2 byte boundary, and ended up debugging the code to learn that this is actually on purpose and why.
If it cannot easily be "fixed" in the output, could it at least be documented?
Thanks and regards,
Jens
Signature · 9 lines
Jens Remus Linux on Z Development (D3303) and z/VSE Support +49-7031-16-1128 Office [email protected] IBM IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Böblingen; Registergericht: Amtsgericht Stuttgart, HRB 243294 IBM Data Privacy Statement: https://www.ibm.com/privacy/