Skip to content

Commit 4112796

Browse files
Namhyung Kimacmel
authored andcommitted
perf annotate: Merge same lines in summary view
The --print-line option of perf annotate command shows summary for each source line. But it didn't merge same lines so that it can appear multiple times. * before: Sorted summary for file /home/namhyung/bin/mcol ---------------------------------------------- 21.71 /home/namhyung/tmp/mcol.c:26 20.66 /home/namhyung/tmp/mcol.c:25 9.53 /home/namhyung/tmp/mcol.c:24 7.68 /home/namhyung/tmp/mcol.c:25 7.67 /home/namhyung/tmp/mcol.c:25 7.66 /home/namhyung/tmp/mcol.c:26 7.49 /home/namhyung/tmp/mcol.c:26 6.92 /home/namhyung/tmp/mcol.c:25 6.81 /home/namhyung/tmp/mcol.c:25 1.07 /home/namhyung/tmp/mcol.c:26 0.52 /home/namhyung/tmp/mcol.c:25 0.51 /home/namhyung/tmp/mcol.c:25 0.51 /home/namhyung/tmp/mcol.c:24 * after: Sorted summary for file /home/namhyung/bin/mcol ---------------------------------------------- 50.77 /home/namhyung/tmp/mcol.c:25 37.94 /home/namhyung/tmp/mcol.c:26 10.04 /home/namhyung/tmp/mcol.c:24 To do that, introduce percent_sum field so that the normal line-by-line output doesn't get changed. Signed-off-by: Namhyung Kim <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 32ae1ef commit 4112796

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

tools/perf/util/annotate.c

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,41 @@ static void insert_source_line(struct rb_root *root, struct source_line *src_lin
858858
struct source_line *iter;
859859
struct rb_node **p = &root->rb_node;
860860
struct rb_node *parent = NULL;
861+
int ret;
861862

862863
while (*p != NULL) {
863864
parent = *p;
864865
iter = rb_entry(parent, struct source_line, node);
865866

866-
if (src_line->percent > iter->percent)
867+
ret = strcmp(iter->path, src_line->path);
868+
if (ret == 0) {
869+
iter->percent_sum += src_line->percent;
870+
return;
871+
}
872+
873+
if (ret < 0)
874+
p = &(*p)->rb_left;
875+
else
876+
p = &(*p)->rb_right;
877+
}
878+
879+
src_line->percent_sum = src_line->percent;
880+
881+
rb_link_node(&src_line->node, parent, p);
882+
rb_insert_color(&src_line->node, root);
883+
}
884+
885+
static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
886+
{
887+
struct source_line *iter;
888+
struct rb_node **p = &root->rb_node;
889+
struct rb_node *parent = NULL;
890+
891+
while (*p != NULL) {
892+
parent = *p;
893+
iter = rb_entry(parent, struct source_line, node);
894+
895+
if (src_line->percent_sum > iter->percent_sum)
867896
p = &(*p)->rb_left;
868897
else
869898
p = &(*p)->rb_right;
@@ -873,6 +902,24 @@ static void insert_source_line(struct rb_root *root, struct source_line *src_lin
873902
rb_insert_color(&src_line->node, root);
874903
}
875904

905+
static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
906+
{
907+
struct source_line *src_line;
908+
struct rb_node *node;
909+
910+
node = rb_first(src_root);
911+
while (node) {
912+
struct rb_node *next;
913+
914+
src_line = rb_entry(node, struct source_line, node);
915+
next = rb_next(node);
916+
rb_erase(node, src_root);
917+
918+
__resort_source_line(dest_root, src_line);
919+
node = next;
920+
}
921+
}
922+
876923
static void symbol__free_source_line(struct symbol *sym, int len)
877924
{
878925
struct annotation *notes = symbol__annotation(sym);
@@ -897,6 +944,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
897944
struct source_line *src_line;
898945
struct annotation *notes = symbol__annotation(sym);
899946
struct sym_hist *h = annotation__histogram(notes, evidx);
947+
struct rb_root tmp_root = RB_ROOT;
900948

901949
if (!h->sum)
902950
return 0;
@@ -931,12 +979,13 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
931979
goto next;
932980

933981
strcpy(src_line[i].path, path);
934-
insert_source_line(root, &src_line[i]);
982+
insert_source_line(&tmp_root, &src_line[i]);
935983

936984
next:
937985
pclose(fp);
938986
}
939987

988+
resort_source_line(root, &tmp_root);
940989
return 0;
941990
}
942991

@@ -960,7 +1009,7 @@ static void print_summary(struct rb_root *root, const char *filename)
9601009
char *path;
9611010

9621011
src_line = rb_entry(node, struct source_line, node);
963-
percent = src_line->percent;
1012+
percent = src_line->percent_sum;
9641013
color = get_percent_color(percent);
9651014
path = src_line->path;
9661015

tools/perf/util/annotate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct sym_hist {
7676
struct source_line {
7777
struct rb_node node;
7878
double percent;
79+
double percent_sum;
7980
char *path;
8081
};
8182

0 commit comments

Comments
 (0)