[PATCH 7/8] scripts/kallsyms: decrease expand_symbol() / cleanup_symbol_name() calls

From: Masahiro Yamada
Date: Wed Mar 08 2023 - 06:53:41 EST


Currently, expand_symbol() is called many times to get the uncompressed
symbol names for sorting, and also for adding comments.

With the output order shuffled in the previous commit, the symbol data
are now written in the following order:

(1) kallsyms_num_syms
(2) kallsyms_names <-- need compressed names
(3) kallsyms_markers
(4) kallsyms_token_table
(5) kallsyms_token_index
(6) kallsyms_addressed / kallsyms_offsets <-- need uncompressed names (for commenting)
(7) kallsyms_relative_base
(8) kallsyms_seq_of_names <-- need uncompressed names (for sorting)

The compressed names are only needed by (2).

Call expand_symbol() between (2) and (3) to restore the original symbol
names. This requires just one expand_symbol() call for each symbol.

Call cleanup_symbol_name() between (7) and (8) instead of during sorting.
It is allowed to overwrite the ->sym field because (8) just outputs the
index instead of the name of each symbol. Again, this requires just one
cleanup_symbol_name() call for each symbol.

This refactoring makes it ~30% faster.

[Before]

$ time scripts/kallsyms --all-symbols --absolute-percpu --base-relative \
.tmp_vmlinux.kallsyms2.syms >/dev/null

real 0m1.027s
user 0m1.010s
sys 0m0.016s

$ time scripts/kallsyms --all-symbols --absolute-percpu --base-relative \
.tmp_vmlinux.kallsyms2.syms >/dev/null

real 0m0.717s
user 0m0.717s
sys 0m0.000s

Signed-off-by: Masahiro Yamada <masahiroy@xxxxxxxxxx>
---

scripts/kallsyms.c | 33 +++++++++++++++------------------
1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c
index 5996f1e61bcf..937900823fa8 100644
--- a/scripts/kallsyms.c
+++ b/scripts/kallsyms.c
@@ -335,19 +335,10 @@ static int symbol_absolute(const struct sym_entry *s)
return s->percpu_absolute;
}

-static char * s_name(char *buf)
-{
- /* Skip the symbol type */
- return buf + 1;
-}
-
static void cleanup_symbol_name(char *s)
{
char *p;

- if (!lto_clang)
- return;
-
/*
* ASCII[.] = 2e
* ASCII[0-9] = 30,39
@@ -366,16 +357,10 @@ static void cleanup_symbol_name(char *s)
static int compare_names(const void *a, const void *b)
{
int ret;
- char sa_namebuf[KSYM_NAME_LEN];
- char sb_namebuf[KSYM_NAME_LEN];
const struct sym_entry *sa = *(const struct sym_entry **)a;
const struct sym_entry *sb = *(const struct sym_entry **)b;

- expand_symbol(sa->sym, sa->len, sa_namebuf);
- expand_symbol(sb->sym, sb->len, sb_namebuf);
- cleanup_symbol_name(s_name(sa_namebuf));
- cleanup_symbol_name(s_name(sb_namebuf));
- ret = strcmp(s_name(sa_namebuf), s_name(sb_namebuf));
+ ret = strcmp(sym_name(sa), sym_name(sb));
if (!ret) {
if (sa->addr > sb->addr)
return 1;
@@ -464,6 +449,15 @@ static void write_src(void)
}
printf("\n");

+ /*
+ * Now that we wrote out the compressed symbol names, restore the
+ * original names, which are needed in some of the later steps.
+ */
+ for (i = 0; i < table_cnt; i++) {
+ expand_symbol(table[i]->sym, table[i]->len, buf);
+ strcpy((char *)table[i]->sym, buf);
+ }
+
output_label("kallsyms_markers");
for (i = 0; i < ((table_cnt + 255) >> 8); i++)
printf("\t.long\t%u\n", markers[i]);
@@ -520,8 +514,7 @@ static void write_src(void)
table[i]->addr);
exit(EXIT_FAILURE);
}
- expand_symbol(table[i]->sym, table[i]->len, buf);
- printf("\t.long\t%#x /* %s */\n", (int)offset, buf);
+ printf("\t.long\t%#x /* %s */\n", (int)offset, table[i]->sym);
} else if (!symbol_absolute(table[i])) {
output_address(table[i]->addr);
} else {
@@ -536,6 +529,10 @@ static void write_src(void)
printf("\n");
}

+ if (lto_clang)
+ for (i = 0; i < table_cnt; i++)
+ cleanup_symbol_name((char *)table[i]->sym);
+
sort_symbols_by_name();
output_label("kallsyms_seqs_of_names");
for (i = 0; i < table_cnt; i++)
--
2.34.1