Skip to content

Commit 1e62856

Browse files
Eric Saint-Etienneacmel
authored andcommitted
perf symbols: Fix slowness due to -ffunction-section
Perf can take minutes to parse an image when -ffunction-section is used. This is especially true with the kernel image when it is compiled this way, which is the arm64 default since the patcheset "Enable deadcode elimination at link time". Perf organize maps using a rbtree. Whenever perf finds a new symbols, it first searches this rbtree for the map it belongs to, by strcmp()'aring section names. When it finds the map with the right name, it uses it to add the symbol. With a usual image there aren't so many maps but when using -ffunction-section there's basically one map per function. With the kernel image that's north of 40,000 maps. For most symbols perf has to parses the entire rbtree to eventually create a new map and add it. Consequently perf spends most of the time browsing a rbtree that keeps getting larger. This performance fix introduces a secondary rbtree that indexes maps based on the section name. Signed-off-by: Eric Saint-Etienne <[email protected]> Reviewed-by: Dave Kleikamp <[email protected]> Reviewed-by: David Aldridge <[email protected]> Reviewed-by: Rob Gardner <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[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 dd1d004 commit 1e62856

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

tools/perf/util/map.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "unwind.h"
2222

2323
static void __maps__insert(struct maps *maps, struct map *map);
24+
static void __maps__insert_name(struct maps *maps, struct map *map);
2425

2526
static inline int is_anon_memory(const char *filename, u32 flags)
2627
{
@@ -496,6 +497,7 @@ u64 map__objdump_2mem(struct map *map, u64 ip)
496497
static void maps__init(struct maps *maps)
497498
{
498499
maps->entries = RB_ROOT;
500+
maps->names = RB_ROOT;
499501
init_rwsem(&maps->lock);
500502
}
501503

@@ -664,6 +666,7 @@ size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
664666
static void __map_groups__insert(struct map_groups *mg, struct map *map)
665667
{
666668
__maps__insert(&mg->maps, map);
669+
__maps__insert_name(&mg->maps, map);
667670
map->groups = mg;
668671
}
669672

@@ -824,10 +827,34 @@ static void __maps__insert(struct maps *maps, struct map *map)
824827
map__get(map);
825828
}
826829

830+
static void __maps__insert_name(struct maps *maps, struct map *map)
831+
{
832+
struct rb_node **p = &maps->names.rb_node;
833+
struct rb_node *parent = NULL;
834+
struct map *m;
835+
int rc;
836+
837+
while (*p != NULL) {
838+
parent = *p;
839+
m = rb_entry(parent, struct map, rb_node_name);
840+
rc = strcmp(m->dso->short_name, map->dso->short_name);
841+
if (rc < 0)
842+
p = &(*p)->rb_left;
843+
else if (rc > 0)
844+
p = &(*p)->rb_right;
845+
else
846+
return;
847+
}
848+
rb_link_node(&map->rb_node_name, parent, p);
849+
rb_insert_color(&map->rb_node_name, &maps->names);
850+
map__get(map);
851+
}
852+
827853
void maps__insert(struct maps *maps, struct map *map)
828854
{
829855
down_write(&maps->lock);
830856
__maps__insert(maps, map);
857+
__maps__insert_name(maps, map);
831858
up_write(&maps->lock);
832859
}
833860

tools/perf/util/map.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct map {
2525
struct rb_node rb_node;
2626
struct list_head node;
2727
};
28+
struct rb_node rb_node_name;
2829
u64 start;
2930
u64 end;
3031
bool erange_warned;
@@ -57,6 +58,7 @@ struct kmap {
5758

5859
struct maps {
5960
struct rb_root entries;
61+
struct rb_root names;
6062
struct rw_semaphore lock;
6163
};
6264

tools/perf/util/symbol.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,11 +1680,22 @@ struct map *map_groups__find_by_name(struct map_groups *mg, const char *name)
16801680
{
16811681
struct maps *maps = &mg->maps;
16821682
struct map *map;
1683+
struct rb_node *node;
16831684

16841685
down_read(&maps->lock);
16851686

1686-
for (map = maps__first(maps); map; map = map__next(map)) {
1687-
if (map->dso && strcmp(map->dso->short_name, name) == 0)
1687+
for (node = maps->names.rb_node; node; ) {
1688+
int rc;
1689+
1690+
map = rb_entry(node, struct map, rb_node_name);
1691+
1692+
rc = strcmp(map->dso->short_name, name);
1693+
if (rc < 0)
1694+
node = node->rb_left;
1695+
else if (rc > 0)
1696+
node = node->rb_right;
1697+
else
1698+
16881699
goto out_unlock;
16891700
}
16901701

0 commit comments

Comments
 (0)