[Suggestion/Problems] perf annoate: Some problems related to the source code view and Improvement of it with line numbers
From: Taeung Song
Date: Wed Feb 15 2017 - 07:35:07 EST
Hi all :)
I found some problems about showing line numbers of perf-annotate.
and I have a suggestion about perf-annotate.
Sure, I have a plan to send the patchset about that.
But I'd like to know other opinions about that before sending the
patchset. :)
If the user do perf-annotate,
# perf record ./a.out
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.043 MB perf.data (799 samples) ]
# perf annotate
(In TUI, if using a 'k' option to see line numbers)
â3 Disassembly of section .text:
â
â5 0000000000400966 <get_cond_maxprice>:
â6 get_cond_maxprice():
â7 };
â
â9 unsigned int limited_wgt;
â
â11 unsigned int get_cond_maxprice(int wgt, struct jewelry
*jewelry)
â12 {
But I think it has some problems and seems a bit deficient..
There are several reasons as below.
1) I think we don't need the line numbers except for source codes
â3 Disassembly of section .text:
â
â5 0000000000400966 <get_cond_maxprice>:
â6 get_cond_maxprice():
I think that '3', '5' and '6' seems like needless.
2) The line numbers aren't correct
(But they are correct if perf-annotate work with --stdio)
If we check the actual source file as below,
we can find many wrong line numbers on perf-annotate.
(e.g. A actual line number of get_cond_amxprice() is 25,
but the line number of the function on perf-annotate is 11.)
...
18 struct jewelry {
19 unsigned int wgt;
20 unsigned int price;
21 };
22
23 unsigned int limited_wgt;
24
25 unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
26 {
27 /* Get maximum price based on a specific weight
28 * following a specific jewelry.
29 */
30 int i;
31 unsigned int nr_cases = wgt/jewelry->wgt;
32 unsigned int maxprice = 0;
...
Sure, the correct line numbers partially exist as well
but there are more wrong line numbers than them.
3) It is hard to read source code + assembly code of perf-annoate.
If the user want to see source code on perf-annotate,
In TUI the user can use 'k' option but the output is mixed with
both source code and assembly code so it's so confusing.. :-\
and the readability is not good.. :(
And the line numbers aren't even wrong..
So, I suggest first showing source code before assembly code
only when the target program has debug info.
If we do, perf-annotate provide good readable source code view
per function
and correct line numbers. :)
For example,
If there is 'get_cond_maxprice()' on a source file as below,
... (with line numbers) ...
25 unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
26 {
27 /* Get maximum price based on a specific weight
28 * following a specific jewelry.
29 */
30 int i;
31 unsigned int nr_cases = wgt/jewelry->wgt;
32 unsigned int maxprice = 0;
33
34 for (i = 1; i <= nr_cases; i++) {
35 unsigned int price, rest_wgt;
36
37 rest_wgt = wgt - (i * jewelry->wgt);
38 price = (i * jewelry->price) +
knapsack_list[rest_wgt].maxprice;
39 if (maxprice < price)
40 maxprice = price;
41 }
42
43 return maxprice;
44 }
...
And if the target program(e.g. a.out) compiled with the source file has
debug info,
we can first show source code before assembly code for good readability
as below.
(If we do, the user don't be confusing due to mixed parts of code any more)
# perf record ./a.out
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.043 MB perf.data (799 samples) ]
# perf annoate (using 'k' option in TUI)
...
â25 unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
2.21 â26 {
â27 /* Get maximum price based on a specific weight
â28 * following a specific jewelry.
â29 */
â30 int i;
4.43 â31 unsigned int nr_cases = wgt/jewelry->wgt;
â32 unsigned int maxprice = 0;
â33
7.49 â34 for (i = 1; i <= nr_cases; i++) {
â35 unsigned int price, rest_wgt;
â36
29.12 â37 rest_wgt = wgt - (i * jewelry->wgt);
15.83 â38 price = (i * jewelry->price) +
knapsack_list[rest_wgt].maxprice;
17.38 â39 if (maxprice < price)
0.34 â40 maxprice = price;
â41 }
â42
1.19 â43 return maxprice;
1.19 â44 }
And if the user want to see not only source code but also assembly code,
we can show as below.
(This output is similar to current annotate view
but it sequentially show full code of the function, not confusing mixed
parts of code
And this output show correct line numbers unlike current annotate view)
â25 unsigned int get_cond_maxprice(int wgt, struct jewelry *jewelry)
â26 {
0.17 â push %rbp
â mov %rsp,%rbp
â sub $0x30,%rsp
0.51 â â callq mcount@plt
â mov %edi,-0x24(%rbp)
1.53 â mov %rsi,-0x30(%rbp)
â27 /* Get maximum price based on a specific weight
â28 * following a specific jewelry.
â29 */
â30 int i;
â31 unsigned int nr_cases = wgt/jewelry->wgt;
â mov -0x24(%rbp),%eax
â mov -0x30(%rbp),%rdx
0.34 â mov (%rdx),%esi
1.36 â mov $0x0,%edx
2.73 â div %esi
â mov %eax,-0xc(%rbp)
â32 unsigned int maxprice = 0;
â movl $0x0,-0x10(%rbp)
â33
â34 for (i = 1; i <= nr_cases; i++) {
â movl $0x1,-0x14(%rbp)
â â jmp <get_cond_maxprice+0x84>
â35 unsigned int price, rest_wgt;
â36
â37 rest_wgt = wgt - (i * jewelry->wgt);
1.53 â mov -0x24(%rbp),%edx
â mov -0x30(%rbp),%rax
0.51 â mov (%rax),%ecx
10.56 â mov -0x14(%rbp),%eax
1.70 â imul %ecx,%eax
0.34 â sub %eax,%edx
1.36 â mov %edx,%eax
13.12 â mov %eax,-0x8(%rbp)
â38 price = (i * jewelry->price) +
knapsack_list[rest_wgt].maxprice;
1.87 â mov -0x30(%rbp),%rax
0.34 â mov 0x4(%rax),%edx
0.85 â mov -0x14(%rbp),%eax
10.90 â imul %eax,%edx
1.87 â mov knapsack_list,%rax
â mov -0x8(%rbp),%ecx
0.68 â shl $0x2,%rcx
11.24 â add %rcx,%rax
3.58 â mov (%rax),%eax
2.39 â add %edx,%eax
2.90 â mov %eax,-0x4(%rbp)
â39 if (maxprice < price)
7.67 â mov -0x10(%rbp),%eax
â cmp -0x4(%rbp),%eax
9.71 â â jae <get_cond_maxprice+0x80>
â40 maxprice = price;
â mov -0x4(%rbp),%eax
0.34 â mov %eax,-0x10(%rbp)
â
3.58 â addl $0x1,-0x14(%rbp)
1.36 â mov -0x14(%rbp),%eax
0.34 â cmp -0xc(%rbp),%eax
2.21 â â jbe <get_cond_maxprice+0x37>
â41 }
â42
â43 return maxprice;
1.19 â mov -0x10(%rbp),%eax
â44 }
0.68 â leaveq
0.51 â â retq
Sure, we can show only assembly code as below (this is current feature)
(Of course, the target program hasn't debug info, perf-annotate show
assembly code as below)
# perf annotate
0.17 â push %rbp
â mov %rsp,%rbp
â sub $0x30,%rsp
0.51 â â callq mcount@plt
â mov %edi,-0x24(%rbp)
1.53 â mov %rsi,-0x30(%rbp)
â mov -0x24(%rbp),%eax
â mov -0x30(%rbp),%rdx
0.34 â mov (%rdx),%esi
1.36 â mov $0x0,%edx
2.73 â div %esi
â mov %eax,-0xc(%rbp)
â movl $0x0,-0x10(%rbp)
â movl $0x1,-0x14(%rbp)
â â jmp 84
1.53 â37: mov -0x24(%rbp),%edx
â mov -0x30(%rbp),%rax
0.51 â mov (%rax),%ecx
10.56 â mov -0x14(%rbp),%eax
1.70 â imul %ecx,%eax
0.34 â sub %eax,%edx
1.36 â mov %edx,%eax
13.12 â mov %eax,-0x8(%rbp)
1.87 â mov -0x30(%rbp),%rax
0.34 â mov 0x4(%rax),%edx
0.85 â mov -0x14(%rbp),%eax
10.90 â imul %eax,%edx
1.87 â mov knapsack_list,%rax
â mov -0x8(%rbp),%ecx
0.68 â shl $0x2,%rcx
11.24 â add %rcx,%rax
3.58 â mov (%rax),%eax
2.39 â add %edx,%eax
2.90 â mov %eax,-0x4(%rbp)
7.67 â mov -0x10(%rbp),%eax
â cmp -0x4(%rbp),%eax
9.71 â â jae 80
â mov -0x4(%rbp),%eax
0.34 â mov %eax,-0x10(%rbp)
3.58 â80: addl $0x1,-0x14(%rbp)
1.36 â84: mov -0x14(%rbp),%eax
0.34 â cmp -0xc(%rbp),%eax
2.21 â â jbe 37
1.19 â mov -0x10(%rbp),%eax
0.68 â leaveq
0.51 â â retq
What do you think about the suggestion and some problems ?
Just send patchset for this problems and the my suggestion ?
I think the lack comes from the output of objdump command.
perf internally handle source codes and line numbers
by output of objdump with 'd' and 'S' as below.
# cat util/annotate.c
...
1436 "%s %s%s --start-address=0x%016" PRIx64
1437 " --stop-address=0x%016" PRIx64
1438 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
1439 objdump_path ? objdump_path : "objdump",
1440 disassembler_style ? "-M " : "",
1441 disassembler_style ? disassembler_style : "",
1442 map__rip_2objdump(map, sym->start),
1443 map__rip_2objdump(map, sym->end),
1444 symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
1445 symbol_conf.annotate_src ? "-S" : "",
1446 symfs_filename, symfs_filename);
...
So if we don't rely on a feature '-S' of objdump and
if we read the actual source file,
I think perf-annotate can provide more readable source code view per
function
and more precise line numbers.
Thanks,
Taeung