Skip to content

Commit 5dfa210

Browse files
milianwacmel
authored andcommitted
perf report: Enable sorting by srcline as key
Often it is interesting to know how costly a given source line is in total. Previously, one had to build these sums manually based on all addresses that pointed to the same source line. This patch introduces srcline as a sort key, which will do the aggregation for us. Paired with the recent addition of showing inline frames, this makes perf report much more useful for many C++ work loads. The following shows the new feature in action. First, let's show the status quo output when we sort by address. The result contains many hist entries that generate the same output: ~~~~~~~~~~~~~~~~ $ perf report --stdio --inline -g address # Children Self Command Shared Object Symbol # ........ ........ ............ ................... ......................................... # 99.89% 35.34% cpp-inlining cpp-inlining [.] main | |--64.55%--main complex:655 | /home/milian/projects/kdab/rnd/hotspot/tests/test-clients/cpp-inlining/main.cpp:39 (inline) | /usr/include/c++/6.3.1/complex:664 (inline) | | | |--60.31%--hypot +20 | | | | | |--8.52%--__hypot_finite +273 | | | | | |--7.32%--__hypot_finite +411 ... --35.34%--_start +4194346 __libc_start_main +241 | |--6.65%--main random.tcc:3326 | /home/milian/projects/kdab/rnd/hotspot/tests/test-clients/cpp-inlining/main.cpp:39 (inline) | /usr/include/c++/6.3.1/bits/random.h:1809 (inline) | /usr/include/c++/6.3.1/bits/random.h:1818 (inline) | /usr/include/c++/6.3.1/bits/random.h:185 (inline) | |--2.70%--main random.tcc:3326 | /home/milian/projects/kdab/rnd/hotspot/tests/test-clients/cpp-inlining/main.cpp:39 (inline) | /usr/include/c++/6.3.1/bits/random.h:1809 (inline) | /usr/include/c++/6.3.1/bits/random.h:1818 (inline) | /usr/include/c++/6.3.1/bits/random.h:185 (inline) | |--1.69%--main random.tcc:3326 | /home/milian/projects/kdab/rnd/hotspot/tests/test-clients/cpp-inlining/main.cpp:39 (inline) | /usr/include/c++/6.3.1/bits/random.h:1809 (inline) | /usr/include/c++/6.3.1/bits/random.h:1818 (inline) | /usr/include/c++/6.3.1/bits/random.h:185 (inline) ... ~~~~~~~~~~~~~~~~ With this patch and `-g srcline` we instead get the following output: ~~~~~~~~~~~~~~~~ $ perf report --stdio --inline -g srcline # Children Self Command Shared Object Symbol # ........ ........ ............ ................... ......................................... # 99.89% 35.34% cpp-inlining cpp-inlining [.] main | |--64.55%--main complex:655 | /home/milian/projects/kdab/rnd/hotspot/tests/test-clients/cpp-inlining/main.cpp:39 (inline) | /usr/include/c++/6.3.1/complex:664 (inline) | | | |--64.02%--hypot | | | | | --59.81%--__hypot_finite | | | --0.53%--cabs | --35.34%--_start __libc_start_main | |--12.48%--main random.tcc:3326 | /home/milian/projects/kdab/rnd/hotspot/tests/test-clients/cpp-inlining/main.cpp:39 (inline) | /usr/include/c++/6.3.1/bits/random.h:1809 (inline) | /usr/include/c++/6.3.1/bits/random.h:1818 (inline) | /usr/include/c++/6.3.1/bits/random.h:185 (inline) ... ~~~~~~~~~~~~~~~~ Signed-off-by: Milian Wolff <milian.wolff@kdab.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Yao Jin <yao.jin@linux.intel.com> Link: http://lkml.kernel.org/r/20170318214928.9047-1-milian.wolff@kdab.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 0d3eb0b commit 5dfa210

10 files changed

Lines changed: 78 additions & 21 deletions

File tree

‎tools/perf/Documentation/perf-report.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ OPTIONS
235235
sort_key can be:
236236
- function: compare on functions (default)
237237
- address: compare on individual code addresses
238+
- srcline: compare on source filename and line number
238239

239240
branch can be:
240241
- branch: include last branch information in callgraph when available.

‎tools/perf/ui/browsers/hists.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,8 @@ static int hist_browser__show_inline(struct hist_browser *browser,
851851
if (ui_browser__is_current_entry(&browser->b, row))
852852
color = HE_COLORSET_SELECTED;
853853

854-
if (callchain_param.key == CCKEY_ADDRESS) {
854+
if (callchain_param.key == CCKEY_ADDRESS ||
855+
callchain_param.key == CCKEY_SRCLINE) {
855856
if (ilist->filename != NULL)
856857
scnprintf(buf, sizeof(buf),
857858
"%s:%d (inline)",

‎tools/perf/ui/stdio/hist.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ static size_t inline__fprintf(struct map *map, u64 ip, int left_margin,
5252
ret += fprintf(fp, " ");
5353
}
5454

55-
if (callchain_param.key == CCKEY_ADDRESS) {
55+
if (callchain_param.key == CCKEY_ADDRESS ||
56+
callchain_param.key == CCKEY_SRCLINE) {
5657
if (ilist->filename != NULL)
5758
ret += fprintf(fp, "%s:%d (inline)",
5859
ilist->filename,

‎tools/perf/util/annotate.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,8 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
16741674
goto next;
16751675

16761676
offset = start + i;
1677-
src_line->path = get_srcline(map->dso, offset, NULL, false);
1677+
src_line->path = get_srcline(map->dso, offset, NULL,
1678+
false, true);
16781679
insert_source_line(&tmp_root, src_line);
16791680

16801681
next:

‎tools/perf/util/callchain.c‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ static int parse_callchain_sort_key(const char *value)
8080
callchain_param.key = CCKEY_ADDRESS;
8181
return 0;
8282
}
83+
if (!strncmp(value, "srcline", strlen(value))) {
84+
callchain_param.key = CCKEY_SRCLINE;
85+
return 0;
86+
}
8387
if (!strncmp(value, "branch", strlen(value))) {
8488
callchain_param.branch_callstack = 1;
8589
return 0;
@@ -510,14 +514,51 @@ enum match_result {
510514
MATCH_GT,
511515
};
512516

517+
static enum match_result match_chain_srcline(struct callchain_cursor_node *node,
518+
struct callchain_list *cnode)
519+
{
520+
char *left = get_srcline(cnode->ms.map->dso,
521+
map__rip_2objdump(cnode->ms.map, cnode->ip),
522+
cnode->ms.sym, true, false);
523+
char *right = get_srcline(node->map->dso,
524+
map__rip_2objdump(node->map, node->ip),
525+
node->sym, true, false);
526+
enum match_result ret = MATCH_EQ;
527+
int cmp;
528+
529+
if (left && right)
530+
cmp = strcmp(left, right);
531+
else if (!left && right)
532+
cmp = 1;
533+
else if (left && !right)
534+
cmp = -1;
535+
else if (cnode->ip == node->ip)
536+
cmp = 0;
537+
else
538+
cmp = (cnode->ip < node->ip) ? -1 : 1;
539+
540+
if (cmp != 0)
541+
ret = cmp < 0 ? MATCH_LT : MATCH_GT;
542+
543+
free_srcline(left);
544+
free_srcline(right);
545+
return ret;
546+
}
547+
513548
static enum match_result match_chain(struct callchain_cursor_node *node,
514549
struct callchain_list *cnode)
515550
{
516551
struct symbol *sym = node->sym;
517552
u64 left, right;
518553

519-
if (cnode->ms.sym && sym &&
520-
callchain_param.key == CCKEY_FUNCTION) {
554+
if (callchain_param.key == CCKEY_SRCLINE) {
555+
enum match_result match = match_chain_srcline(node, cnode);
556+
557+
if (match != MATCH_ERROR)
558+
return match;
559+
}
560+
561+
if (cnode->ms.sym && sym && callchain_param.key == CCKEY_FUNCTION) {
521562
left = cnode->ms.sym->start;
522563
right = sym->start;
523564
} else {
@@ -911,15 +952,16 @@ int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *
911952
char *callchain_list__sym_name(struct callchain_list *cl,
912953
char *bf, size_t bfsize, bool show_dso)
913954
{
955+
bool show_addr = callchain_param.key == CCKEY_ADDRESS;
956+
bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
914957
int printed;
915958

916959
if (cl->ms.sym) {
917-
if (callchain_param.key == CCKEY_ADDRESS &&
918-
cl->ms.map && !cl->srcline)
960+
if (show_srcline && cl->ms.map && !cl->srcline)
919961
cl->srcline = get_srcline(cl->ms.map->dso,
920962
map__rip_2objdump(cl->ms.map,
921963
cl->ip),
922-
cl->ms.sym, false);
964+
cl->ms.sym, false, show_addr);
923965
if (cl->srcline)
924966
printed = scnprintf(bf, bfsize, "%s %s",
925967
cl->ms.sym->name, cl->srcline);

‎tools/perf/util/callchain.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
7777

7878
enum chain_key {
7979
CCKEY_FUNCTION,
80-
CCKEY_ADDRESS
80+
CCKEY_ADDRESS,
81+
CCKEY_SRCLINE
8182
};
8283

8384
enum chain_value {

‎tools/perf/util/map.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
405405

406406
if (map && map->dso) {
407407
srcline = get_srcline(map->dso,
408-
map__rip_2objdump(map, addr), NULL, true);
408+
map__rip_2objdump(map, addr), NULL,
409+
true, true);
409410
if (srcline != SRCLINE_UNKNOWN)
410411
ret = fprintf(fp, "%s%s", prefix, srcline);
411412
free_srcline(srcline);

‎tools/perf/util/sort.c‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ char *hist_entry__get_srcline(struct hist_entry *he)
323323
return SRCLINE_UNKNOWN;
324324

325325
return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
326-
he->ms.sym, true);
326+
he->ms.sym, true, true);
327327
}
328328

329329
static int64_t
@@ -366,7 +366,8 @@ sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
366366
left->branch_info->srcline_from = get_srcline(map->dso,
367367
map__rip_2objdump(map,
368368
left->branch_info->from.al_addr),
369-
left->branch_info->from.sym, true);
369+
left->branch_info->from.sym,
370+
true, true);
370371
}
371372
if (!right->branch_info->srcline_from) {
372373
struct map *map = right->branch_info->from.map;
@@ -376,7 +377,8 @@ sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
376377
right->branch_info->srcline_from = get_srcline(map->dso,
377378
map__rip_2objdump(map,
378379
right->branch_info->from.al_addr),
379-
right->branch_info->from.sym, true);
380+
right->branch_info->from.sym,
381+
true, true);
380382
}
381383
return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
382384
}
@@ -407,7 +409,8 @@ sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
407409
left->branch_info->srcline_to = get_srcline(map->dso,
408410
map__rip_2objdump(map,
409411
left->branch_info->to.al_addr),
410-
left->branch_info->from.sym, true);
412+
left->branch_info->from.sym,
413+
true, true);
411414
}
412415
if (!right->branch_info->srcline_to) {
413416
struct map *map = right->branch_info->to.map;
@@ -417,7 +420,8 @@ sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
417420
right->branch_info->srcline_to = get_srcline(map->dso,
418421
map__rip_2objdump(map,
419422
right->branch_info->to.al_addr),
420-
right->branch_info->to.sym, true);
423+
right->branch_info->to.sym,
424+
true, true);
421425
}
422426
return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
423427
}
@@ -448,7 +452,7 @@ static char *hist_entry__get_srcfile(struct hist_entry *e)
448452
return no_srcfile;
449453

450454
sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
451-
e->ms.sym, false, true);
455+
e->ms.sym, false, true, true);
452456
if (!strcmp(sf, SRCLINE_UNKNOWN))
453457
return no_srcfile;
454458
p = strchr(sf, ':');

‎tools/perf/util/srcline.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
429429
#define A2L_FAIL_LIMIT 123
430430

431431
char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
432-
bool show_sym, bool unwind_inlines)
432+
bool show_sym, bool show_addr, bool unwind_inlines)
433433
{
434434
char *file = NULL;
435435
unsigned line = 0;
@@ -463,6 +463,11 @@ char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
463463
dso->has_srcline = 0;
464464
dso__free_a2l(dso);
465465
}
466+
467+
if (!show_addr)
468+
return (show_sym && sym) ?
469+
strndup(sym->name, sym->namelen) : NULL;
470+
466471
if (sym) {
467472
if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
468473
addr - sym->start) < 0)
@@ -479,9 +484,9 @@ void free_srcline(char *srcline)
479484
}
480485

481486
char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
482-
bool show_sym)
487+
bool show_sym, bool show_addr)
483488
{
484-
return __get_srcline(dso, addr, sym, show_sym, false);
489+
return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
485490
}
486491

487492
struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)

‎tools/perf/util/util.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ struct symbol;
287287

288288
extern bool srcline_full_filename;
289289
char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
290-
bool show_sym);
290+
bool show_sym, bool show_addr);
291291
char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
292-
bool show_sym, bool unwind_inlines);
292+
bool show_sym, bool show_addr, bool unwind_inlines);
293293
void free_srcline(char *srcline);
294294

295295
int perf_event_paranoid(void);

0 commit comments

Comments
 (0)