kallsyms: Improve the performance of kallsyms_lookup_name()
authorZhen Lei <thunder.leizhen@huawei.com>
Wed, 2 Nov 2022 08:49:14 +0000 (16:49 +0800)
committerLuis Chamberlain <mcgrof@kernel.org>
Sun, 13 Nov 2022 02:47:36 +0000 (18:47 -0800)
commit60443c88f3a89fd303a9e8c0e84895910675c316
treebd16233e13ac0b294ae4225102f6e58e9a5d18a1
parentfcdf7197cf23ef452c30f376d31d73bdf7946de8
kallsyms: Improve the performance of kallsyms_lookup_name()

Currently, to search for a symbol, we need to expand the symbols in
'kallsyms_names' one by one, and then use the expanded string for
comparison. It's O(n).

If we sort names in ascending order like addresses, we can also use
binary search. It's O(log(n)).

In order not to change the implementation of "/proc/kallsyms", the table
kallsyms_names[] is still stored in a one-to-one correspondence with the
address in ascending order.

Add array kallsyms_seqs_of_names[], it's indexed by the sequence number
of the sorted names, and the corresponding content is the sequence number
of the sorted addresses. For example:
Assume that the index of NameX in array kallsyms_seqs_of_names[] is 'i',
the content of kallsyms_seqs_of_names[i] is 'k', then the corresponding
address of NameX is kallsyms_addresses[k]. The offset in kallsyms_names[]
is get_symbol_offset(k).

Note that the memory usage will increase by (4 * kallsyms_num_syms)
bytes, the next two patches will reduce (1 * kallsyms_num_syms) bytes
and properly handle the case CONFIG_LTO_CLANG=y.

Performance test results: (x86)
Before:
min=234, max=10364402, avg=5206926
min=267, max=11168517, avg=5207587
After:
min=1016, max=90894, avg=7272
min=1014, max=93470, avg=7293

The average lookup performance of kallsyms_lookup_name() improved 715x.

Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
kernel/kallsyms.c
kernel/kallsyms_internal.h
scripts/kallsyms.c