2020-03-24 16:15:15

by Peter Zijlstra

[permalink] [raw]
Subject: [PATCH v3 13/26] objtool: Optimize find_symbol_by_name()

Perf showed that find_symbol_by_name() takes time; add a symbol name
hash.

This shaves another second off of objtool on vmlinux.o runtime, down
to 15 seconds.

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Josh Poimboeuf <[email protected]>
---
tools/objtool/elf.c | 10 +++++-----
tools/objtool/elf.h | 2 ++
2 files changed, 7 insertions(+), 5 deletions(-)

--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -203,13 +203,11 @@ struct symbol *find_func_containing(stru

struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
{
- struct section *sec;
struct symbol *sym;

- list_for_each_entry(sec, &elf->sections, list)
- list_for_each_entry(sym, &sec->symbol_list, list)
- if (!strcmp(sym->name, name))
- return sym;
+ hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
+ if (!strcmp(sym->name, name))
+ return sym;

return NULL;
}
@@ -386,6 +384,7 @@ static int read_symbols(struct elf *elf)
entry = &sym->sec->symbol_list;
list_add(&sym->list, entry);
hash_add(elf->symbol_hash, &sym->hash, sym->idx);
+ hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
}

if (stats)
@@ -524,6 +523,7 @@ struct elf *elf_read(const char *name, i
memset(elf, 0, sizeof(*elf));

hash_init(elf->symbol_hash);
+ hash_init(elf->symbol_name_hash);
hash_init(elf->section_hash);
hash_init(elf->section_name_hash);
INIT_LIST_HEAD(&elf->sections);
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -47,6 +47,7 @@ struct symbol {
struct list_head list;
struct rb_node node;
struct hlist_node hash;
+ struct hlist_node name_hash;
GElf_Sym sym;
struct section *sec;
char *name;
@@ -77,6 +78,7 @@ struct elf {
char *name;
struct list_head sections;
DECLARE_HASHTABLE(symbol_hash, 20);
+ DECLARE_HASHTABLE(symbol_name_hash, 20);
DECLARE_HASHTABLE(section_hash, 16);
DECLARE_HASHTABLE(section_name_hash, 16);
};



2020-03-25 10:26:01

by Miroslav Benes

[permalink] [raw]
Subject: Re: [PATCH v3 13/26] objtool: Optimize find_symbol_by_name()

On Tue, 24 Mar 2020, Peter Zijlstra wrote:

> Perf showed that find_symbol_by_name() takes time; add a symbol name
> hash.
>
> This shaves another second off of objtool on vmlinux.o runtime, down
> to 15 seconds.
>
> Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
> Acked-by: Josh Poimboeuf <[email protected]>

Reviewed-by: Miroslav Benes <[email protected]>

M

Subject: [tip: core/objtool] objtool: Optimize find_symbol_by_name()

The following commit has been merged into the core/objtool branch of tip:

Commit-ID: cdb3d057a17d56363a831e486ea39e4c389a6cf9
Gitweb: https://git.kernel.org/tip/cdb3d057a17d56363a831e486ea39e4c389a6cf9
Author: Peter Zijlstra <[email protected]>
AuthorDate: Thu, 12 Mar 2020 10:17:38 +01:00
Committer: Peter Zijlstra <[email protected]>
CommitterDate: Wed, 25 Mar 2020 18:28:30 +01:00

objtool: Optimize find_symbol_by_name()

Perf showed that find_symbol_by_name() takes time; add a symbol name
hash.

This shaves another second off of objtool on vmlinux.o runtime, down
to 15 seconds.

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Reviewed-by: Miroslav Benes <[email protected]>
Acked-by: Josh Poimboeuf <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
tools/objtool/elf.c | 10 +++++-----
tools/objtool/elf.h | 2 ++
2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 07db4df..43abae7 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -203,13 +203,11 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset)

struct symbol *find_symbol_by_name(struct elf *elf, const char *name)
{
- struct section *sec;
struct symbol *sym;

- list_for_each_entry(sec, &elf->sections, list)
- list_for_each_entry(sym, &sec->symbol_list, list)
- if (!strcmp(sym->name, name))
- return sym;
+ hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
+ if (!strcmp(sym->name, name))
+ return sym;

return NULL;
}
@@ -386,6 +384,7 @@ static int read_symbols(struct elf *elf)
entry = &sym->sec->symbol_list;
list_add(&sym->list, entry);
hash_add(elf->symbol_hash, &sym->hash, sym->idx);
+ hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
}

if (stats)
@@ -524,6 +523,7 @@ struct elf *elf_read(const char *name, int flags)
memset(elf, 0, sizeof(*elf));

hash_init(elf->symbol_hash);
+ hash_init(elf->symbol_name_hash);
hash_init(elf->section_hash);
hash_init(elf->section_name_hash);
INIT_LIST_HEAD(&elf->sections);
diff --git a/tools/objtool/elf.h b/tools/objtool/elf.h
index d18f466..3088d92 100644
--- a/tools/objtool/elf.h
+++ b/tools/objtool/elf.h
@@ -47,6 +47,7 @@ struct symbol {
struct list_head list;
struct rb_node node;
struct hlist_node hash;
+ struct hlist_node name_hash;
GElf_Sym sym;
struct section *sec;
char *name;
@@ -77,6 +78,7 @@ struct elf {
char *name;
struct list_head sections;
DECLARE_HASHTABLE(symbol_hash, 20);
+ DECLARE_HASHTABLE(symbol_name_hash, 20);
DECLARE_HASHTABLE(section_hash, 16);
DECLARE_HASHTABLE(section_name_hash, 16);
};