perf report: fix off-by-one for non-activation frames
PATCH v2 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~~~~~~~~~~~~~~~
With 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~~~~~~~~~~~~~~~
Note how this patch fixes this issue for both unwinding methods, i.e. both dwfl and libunwind. The former case is straight-forward thanks to dwfl_frame_pc. For libunwind, we replace the functionality via unw_is_signal_frame for any but the very first frame.
--- tools/perf/util/unwind-libdw.c | 6 +++++- tools/perf/util/unwind-libunwind-local.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
v2:
- use unw_is_signal_frame to also fix this issue for libunwind
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 ; }
tools/perf/util/unwind-libunwind-local.c +14 -0
@@ -690,8 +690,22 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,if (ret)display_error (ret); +bool previous_frame_was_signal = false;while (!ret && (unw_step (&c) >0 ) && i < max_stack) {unw_get_reg (&c,UNW_REG_IP , &ips[i]); + +/* +* Decrement the IP for any non-activation frames. +* this is required to properly find the srcline +* for caller frames. +* See also the documentation for dwfl_frame_pc, +* which this code tries to replicate. +*/ +bool frame_is_signal =unw_is_signal_frame (&c) >0 ; +if (!previous_frame_was_signal && !frame_is_signal) + --ips[i]; + previous_frame_was_signal = frame_is_signal; + ++i; }
Signature · 1 line
2.13.0
Namhyung Kim
maintainerQuoted 19 lines · 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.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));
Quoted 3 lines
return 0;}
~~~~~~~~~~~~~~~
It'd be nice if the test program has a signal frame for verification.
Quoted 133 lines
Now compile and record it:
~~~~~~~~~~~~~~~
g++ -std=c++11 -g -O2 test.cpp
echo 1 | sudo tee/proc/sys/kernel/sched_schedstatsperf 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_schedstatsperf 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~~~~~~~~~~~~~~~
With 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~~~~~~~~~~~~~~~
Note how this patch fixes this issue for both unwinding methods, i.e. both dwfl and libunwind. The former case is straight-forward thanks to
dwfl_frame_pc. For libunwind, we replace the functionality viaunw_is_signal_framefor any but the very first frame.Cc Arnaldo Carvalho de Melo <[email protected]>Cc David Ahern <[email protected]>Cc Namhyung Kim <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Yao Jin <[email protected]>Signed-off-by Milian Wolff <[email protected]>--- tools/perf/util/unwind-libdw.c | 6 +++++- tools/perf/util/unwind-libunwind-local.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)v2:
- useunw_is_signal_frameto also fix this issue for libunwindtools/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 ; }tools/perf/util/unwind-libunwind-local.c +3
-0@@ -690,8 +690,22 @@ static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb,if (ret)display_error (ret); +bool previous_frame_was_signal = false;while (!ret && (unw_step (&c) >0 ) && i < max_stack) {unw_get_reg (&c,UNW_REG_IP , &ips[i]); + +/*
tools/perf/util/unwind-libunwind-local.c static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, · line 707
Quoted 10 lines
tools/perf/util/unwind-libunwind-local.c +10
-0+* Decrement the IP for any non-activation frames. +* this is required to properly find the srcline +* for caller frames. +* See also the documentation for dwfl_frame_pc, +* which this code tries to replicate. +*/ +bool frame_is_signal =unw_is_signal_frame (&c) >0 ; +if (!previous_frame_was_signal && !frame_is_signal) + --ips[i]; + previous_frame_was_signal = frame_is_signal;
Does it need to check previous frame too?
Thanks,
Namhyung
Quoted 7 lines
tools/perf/util/unwind-libunwind-local.c +1
-0+ ++i; }Signature · 1 line
2.13.0
Quoted 22 lines · On Dienstag, 16. Mai 2017 16:38:29 CEST Namhyung Kim wrote:
Quoted 21 lines · 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.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));
Quoted 7 lines
Quoted 5 lines
return 0;}~~~~~~~~~~~~~~~
It'd be nice if the test program has a signal frame for verification.
I have pretty much zero experience about signals. Would it be enough to add a signal handler for, say, SIGUSR1 to my test application and then trigger a sleep when that signal is delivered? If that should be enough, I'll write and test it out.
<snip>
Quoted 17 lines
Quoted 17 lines
(quoted diff) +0
-0
b/tools/perf/util/unwind-libunwind-local.cindexf8455bed6e65..30ab26375c80100644tools/perf/util/unwind-libunwind-local.c +0
-0@@ -690,8 +690,22 @@ static int get_entries(struct unwind_info *ui,
unwind_entry_cb_tcb,>tools/perf/util/unwind-libunwind-local.c +1
-0if (ret)display_error (ret); +bool previous_frame_was_signal = false;while (!ret && (unw_step (&c) >0 ) && i < max_stack) {unw_get_reg (&c,UNW_REG_IP , &ips[i]);
Quoted 14 lines
tools/perf/util/unwind-libunwind-local.c static int get_entries(struct unwind_info *ui, unwind_entry_cb_t cb, · line 707
Quoted 12 lines
tools/perf/util/unwind-libunwind-local.c +12
-0+ +/* +* Decrement the IP for any non-activation frames. +* this is required to properly find the srcline +* for caller frames. +* See also the documentation for dwfl_frame_pc, +* which this code tries to replicate. +*/ +bool frame_is_signal =unw_is_signal_frame (&c) >0 ; +if (!previous_frame_was_signal && !frame_is_signal) + --ips[i]; + previous_frame_was_signal = frame_is_signal;Does it need to check previous frame too?
That's what dwfl_frame_pc does, if I'm not misunderstanding it's source code?
Bye
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
Quoted 27 lines · On Dienstag, 16. Mai 2017 18:17:26 CEST Milian Wolff wrote:
Quoted 26 lines · On Dienstag, 16. Mai 2017 16:38:29 CEST Namhyung Kim wrote:
Quoted 25 lines · 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.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;}
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
~~~~~~~~~~~~~~~
It'd be nice if the test program has a signal frame for verification.
I have pretty much zero experience about signals. Would it be enough to add a signal handler for, say, SIGUSR1 to my test application and then trigger a sleep when that signal is delivered? If that should be enough, I'll write and test it out.
~~~~~~~~~~~~~~~
#include <thread>
#include <chrono>
#include <signal.h>
using namespace std;
volatile bool run_loop = true;
void my_handler(int signum)
{
this_thread::sleep_for(chrono::milliseconds(1000));
this_thread::sleep_for(chrono::milliseconds(100));
this_thread::sleep_for(chrono::milliseconds(10));
run_loop = false;
}int main()
{
signal(SIGUSR1, my_handler);
while (run_loop) {}
return 0;
}~~~~~~~~~~~~~~~
This does not properly unwind neither before nor after this patch. I only ever get:
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
So... should this work? Please tell me how to test this properly.
Thanks
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 77 lines · Milian Wolff
Quoted 27 lines · On Dienstag, 16. Mai 2017 18:17:26 CEST Milian Wolff wrote:
Quoted 26 lines · On Dienstag, 16. Mai 2017 16:38:29 CEST Namhyung Kim wrote:
Quoted 25 lines · 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.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;}Quoted 8 lines
Quoted 3 lines
Quoted 1 line
~~~~~~~~~~~~~~~
It'd be nice if the test program has a signal frame for verification.
I have pretty much zero experience about signals. Would it be enough to add a signal handler for, say, SIGUSR1 to my test application and then trigger a sleep when that signal is delivered? If that should be enough, I'll write and test it out.
~~~~~~~~~~~~~~~
#include <thread>
#include <chrono>
#include <signal.h>using namespace std;
volatile bool
run_loop= true;void
my_handler(int signum){ this_thread::sleep_for(chrono::milliseconds(1000)); this_thread::sleep_for(chrono::milliseconds(100)); this_thread::sleep_for(chrono::milliseconds(10)); run_loop = false; }int
main(){ signal(SIGUSR1, my_handler); while (run_loop) {} return 0; }~~~~~~~~~~~~~~~
This does not properly unwind neither before nor after this patch. I only ever get:
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> >
Quoted 3 lines
thread:323
So... should this work? Please tell me how to test this properly.
How did you send the SIGUSR1 to the process?
Anyway it does nothing to do with inlining, right? I just wrote a test code below to burn a cpu with or without a signal frame.
$ nl -ba frame-addr.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4
5 #define __noinline __attribute__((noinline))
6
7 __noinline void bar(void)
8 {
9 volatile long cnt = 0;
10
11 for (cnt = 0; cnt < 100000000; cnt++);
12 }
13
14 __noinline void foo(void)
15 {
16 bar();
17 }
18
19 void sig_handler(int sig)
20 {
21 foo();
22 }
23
24 int main(void)
25 {
26 signal(SIGUSR1, sig_handler);
27 raise(SIGUSR1);
28
29 foo();
30 return 0;
31 }$ gcc -O2 -g -fno-optimize-sibling-calls -o frame-addr frame-addr.c
$ perf record --call-graph dwarf ./frame-addr
$ perf report -q -g srcline | head -15
99.88% frame-addr frame-addr [.] bar
|
---bar frame-addr.c:11
foo frame-addr.c:16
|
|--51.12%--main frame-addr.c:29
| __libc_start_main
| _start
|
--48.76%--sig_handler frame-addr.c:21
0x33a8f
raise .:0
main frame-addr.c:29 <--- bad
__libc_start_main
_startNote that 'raise' was called at line 27. It seems that simply checking current frame fixes it.
Thanks,
Namhyung
Quoted 151 lines · On Mittwoch, 17. Mai 2017 07:12:21 CEST Namhyung Kim wrote:
Quoted 86 lines · Milian Wolff
Quoted 31 lines · On Dienstag, 16. Mai 2017 18:17:26 CEST Milian Wolff wrote:
Quoted 22 lines · On Dienstag, 16. Mai 2017 16:38:29 CEST Namhyung Kim wrote:
Quoted 21 lines · 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.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));Quoted 7 lines
Quoted 5 lines
return 0;}~~~~~~~~~~~~~~~
It'd be nice if the test program has a signal frame for verification.
Quoted 7 lines
I have pretty much zero experience about signals. Would it be enough to add
a signal handler for, say, SIGUSR1 to my test application and then trigger
a sleep when that signal is delivered? If that should be enough, I'll write
and test it out.~~~~~~~~~~~~~~~
#include <thread>
#include <chrono>
#include <signal.h>using namespace std;
volatile bool
run_loop= true;void
my_handler(int signum)
{this_thread::sleep_for(chrono::milliseconds(1000)); this_thread::sleep_for(chrono::milliseconds(100)); this_thread::sleep_for(chrono::milliseconds(10)); run_loop = false;}
int
main()
{signal(SIGUSR1, my_handler); while (run_loop) {} return 0;}
~~~~~~~~~~~~~~~This does not properly unwind neither before nor after this patch. I only ever>
get: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> >Quoted 3 lines
thread:323
So... should this work? Please tell me how to test this properly.
How did you send the SIGUSR1 to the process?
Anyway it does nothing to do with inlining, right? I just wrote a test code below to burn a cpu with or without a signal frame.
$ nl -ba frame-addr.c 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <signal.h> 4 5 #define __noinline __attribute__((noinline)) 6 7 __noinline void bar(void) 8 { 9 volatile long cnt = 0; 10 11 for (cnt = 0; cnt < 100000000; cnt++); 12 } 13 14 __noinline void foo(void) 15 { 16 bar(); 17 } 18 19 void sig_handler(int sig) 20 { 21 foo(); 22 } 23 24 int main(void) 25 { 26 signal(SIGUSR1, sig_handler); 27 raise(SIGUSR1); 28 29 foo(); 30 return 0; 31 }$ gcc -O2 -g -fno-optimize-sibling-calls -o frame-addr frame-addr.c$ perf record --call-graph dwarf ./frame-addr$ perf report -q -g srcline | head -15 99.88% frame-addr frame-addr [.] bar---bar frame-addr.c:11 foo frame-addr.c:16 |--51.12%--main frame-addr.c:29 | | __libc_start_main | _start --48.76%--sig_handler frame-addr.c:21 0x33a8f raise .:0 main frame-addr.c:29 <--- bad __libc_start_main _start
Quoted 2 lines
Note that 'raise' was called at line 27. It seems that simply checking current frame fixes it.
Got it - thanks for your test case. I fixed it and will resend v3 of this patch shortly.
Thanks
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