From: Ihor Solodrai <ihor.solodrai@pm.me>
To: dwarves@vger.kernel.org
Cc: bpf@vger.kernel.org, acme@kernel.org, alan.maguire@oracle.com,
eddyz87@gmail.com, andrii@kernel.org, mykolal@fb.com,
olsajiri@gmail.com
Subject: [PATCH dwarves v4 04/10] btf_encoder: introduce elf_functions struct type
Date: Tue, 07 Jan 2025 19:09:21 +0000 [thread overview]
Message-ID: <20250107190855.2312210-5-ihor.solodrai@pm.me> (raw)
In-Reply-To: <20250107190855.2312210-1-ihor.solodrai@pm.me>
Extract elf_functions struct type from btf_encoder.
Replace routines operating on functions table in btf_encoder by
routines operating on elf_functions:
- btf_encoder__collect_function -> elf_functions__collect_function
- btf_encoder__collect_symbols -> elf_functions__collect
Now these functions do not depend on btf_encoder being passed to them
as a parameter.
Link: https://lore.kernel.org/dwarves/3MqWfdjBO9srtpr8kjweJgCkdwYKV6JC_-SN27S8Y9_J1SzssIgZs4Ptc5tEqpZ7w2vbSmTQ35J5CX35Yb4KMbw8wsTrB2IAf2SWU-k4Xi4=@pm.me/
Link: https://lore.kernel.org/dwarves/20241128012341.4081072-6-ihor.solodrai@pm.me/
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
---
btf_encoder.c | 118 ++++++++++++++++++++++++++------------------------
1 file changed, 62 insertions(+), 56 deletions(-)
diff --git a/btf_encoder.c b/btf_encoder.c
index b1e80ca..0df9296 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -101,6 +101,13 @@ struct elf_secinfo {
struct gobuffer secinfo;
};
+struct elf_functions {
+ struct elf_symtab *symtab;
+ struct elf_function *entries;
+ int cnt;
+ int suffix_cnt; /* number of .isra, .part etc */
+};
+
/*
* cu: cu being processed.
*/
@@ -127,12 +134,7 @@ struct btf_encoder {
size_t seccnt;
int encode_vars;
struct list_head func_states;
- struct {
- struct elf_function *entries;
- int allocated;
- int cnt;
- int suffix_cnt; /* number of .isra, .part etc */
- } functions;
+ struct elf_functions functions;
};
struct btf_func {
@@ -1210,16 +1212,6 @@ static int functions_cmp(const void *_a, const void *_b)
#define max(x, y) ((x) < (y) ? (y) : (x))
#endif
-static void *reallocarray_grow(void *ptr, int *nmemb, size_t size)
-{
- int new_nmemb = max(1000, *nmemb * 3 / 2);
- void *new = realloc(ptr, new_nmemb * size);
-
- if (new)
- *nmemb = new_nmemb;
- return new;
-}
-
static int saved_functions_cmp(const void *_a, const void *_b)
{
struct btf_encoder_func_state * const *a = _a;
@@ -1330,44 +1322,30 @@ out:
return err;
}
-static int btf_encoder__collect_function(struct btf_encoder *encoder, GElf_Sym *sym)
+static void elf_functions__collect_function(struct elf_functions *functions, GElf_Sym *sym)
{
- struct elf_function *new;
+ struct elf_function *func;
const char *name;
if (elf_sym__type(sym) != STT_FUNC)
- return 0;
- name = elf_sym__name(sym, encoder->symtab);
- if (!name)
- return 0;
+ return;
- if (encoder->functions.cnt == encoder->functions.allocated) {
- new = reallocarray_grow(encoder->functions.entries,
- &encoder->functions.allocated,
- sizeof(*encoder->functions.entries));
- if (!new) {
- /*
- * The cleanup - delete_functions is called
- * in btf_encoder__encode_cu error path.
- */
- return -1;
- }
- encoder->functions.entries = new;
- }
+ name = elf_sym__name(sym, functions->symtab);
+ if (!name)
+ return;
- memset(&encoder->functions.entries[encoder->functions.cnt], 0,
- sizeof(*new));
- encoder->functions.entries[encoder->functions.cnt].name = name;
+ func = &functions->entries[functions->cnt];
+ func->name = name;
if (strchr(name, '.')) {
const char *suffix = strchr(name, '.');
- encoder->functions.suffix_cnt++;
- encoder->functions.entries[encoder->functions.cnt].prefixlen = suffix - name;
+ functions->suffix_cnt++;
+ func->prefixlen = suffix - name;
} else {
- encoder->functions.entries[encoder->functions.cnt].prefixlen = strlen(name);
+ func->prefixlen = strlen(name);
}
- encoder->functions.cnt++;
- return 0;
+
+ functions->cnt++;
}
static struct elf_function *btf_encoder__find_function(const struct btf_encoder *encoder,
@@ -2139,26 +2117,53 @@ int btf_encoder__encode(struct btf_encoder *encoder)
return err;
}
-
-static int btf_encoder__collect_symbols(struct btf_encoder *encoder)
+static int elf_functions__collect(struct elf_functions *functions)
{
- uint32_t sym_sec_idx;
+ uint32_t nr_symbols = elf_symtab__nr_symbols(functions->symtab);
+ struct elf_function *tmp;
+ Elf32_Word sym_sec_idx;
uint32_t core_id;
GElf_Sym sym;
+ int err = 0;
- elf_symtab__for_each_symbol_index(encoder->symtab, core_id, sym, sym_sec_idx) {
- if (btf_encoder__collect_function(encoder, &sym))
- return -1;
+ /* We know that number of functions is less than number of symbols,
+ * so we can overallocate temporarily.
+ */
+ functions->entries = calloc(nr_symbols, sizeof(*functions->entries));
+ if (!functions->entries) {
+ err = -ENOMEM;
+ goto out_free;
}
- if (encoder->functions.cnt) {
- qsort(encoder->functions.entries, encoder->functions.cnt, sizeof(encoder->functions.entries[0]),
- functions_cmp);
- if (encoder->verbose)
- printf("Found %d functions!\n", encoder->functions.cnt);
+ functions->cnt = 0;
+ elf_symtab__for_each_symbol_index(functions->symtab, core_id, sym, sym_sec_idx) {
+ elf_functions__collect_function(functions, &sym);
+ }
+
+ if (functions->cnt) {
+ qsort(functions->entries, functions->cnt, sizeof(*functions->entries), functions_cmp);
+ } else {
+ err = 0;
+ goto out_free;
+ }
+
+ /* Reallocate to the exact size */
+ tmp = realloc(functions->entries, functions->cnt * sizeof(struct elf_function));
+ if (tmp) {
+ functions->entries = tmp;
+ } else {
+ fprintf(stderr, "could not reallocate memory for elf_functions table\n");
+ err = -ENOMEM;
+ goto out_free;
}
return 0;
+
+out_free:
+ free(functions->entries);
+ functions->entries = NULL;
+ functions->cnt = 0;
+ return err;
}
static bool ftype__has_arg_names(const struct ftype *ftype)
@@ -2419,6 +2424,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
printf("%s: '%s' doesn't have symtab.\n", __func__, cu->filename);
goto out;
}
+ encoder->functions.symtab = encoder->symtab;
/* index the ELF sections for later lookup */
@@ -2457,7 +2463,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
if (!found_percpu && encoder->verbose)
printf("%s: '%s' doesn't have '%s' section\n", __func__, cu->filename, PERCPU_SECTION);
- if (btf_encoder__collect_symbols(encoder))
+ if (elf_functions__collect(&encoder->functions))
goto out_delete;
if (encoder->verbose)
@@ -2489,7 +2495,7 @@ void btf_encoder__delete(struct btf_encoder *encoder)
encoder->btf = NULL;
elf_symtab__delete(encoder->symtab);
- encoder->functions.allocated = encoder->functions.cnt = 0;
+ encoder->functions.cnt = 0;
free(encoder->functions.entries);
encoder->functions.entries = NULL;
--
2.47.1
next prev parent reply other threads:[~2025-01-07 19:09 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-07 19:08 [PATCH dwarves v4 00/10] pahole: faster reproducible BTF encoding Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4 01/10] btf_encoder: simplify function encoding Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4 02/10] btf_encoder: free encoder->secinfo in btf_encoder__delete Ihor Solodrai
2025-01-09 16:54 ` Alan Maguire
2025-01-09 21:36 ` Arnaldo Carvalho de Melo
2025-01-07 19:09 ` [PATCH dwarves v4 03/10] btf_encoder: separate elf function, saved function representations Ihor Solodrai
2025-01-07 19:09 ` Ihor Solodrai [this message]
2025-01-07 19:09 ` [PATCH dwarves v4 05/10] btf_encoder: introduce elf_functions_list Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4 06/10] btf_encoder: remove skip_encoding_inconsistent_proto Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4 07/10] dwarf_loader: introduce cu->id Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4] dwarf_loader: multithreading with a job/worker model Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4 09/10] btf_encoder: clean up global encoders list Ihor Solodrai
2025-01-07 19:09 ` [PATCH dwarves v4] btf_encoder: switch func_states from a list to an array Ihor Solodrai
2025-01-07 20:35 ` [PATCH dwarves v4 00/10] pahole: faster reproducible BTF encoding Ihor Solodrai
2025-01-09 18:32 ` Arnaldo Carvalho de Melo
2025-01-09 18:38 ` Ihor Solodrai
2025-01-09 21:21 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250107190855.2312210-5-ihor.solodrai@pm.me \
--to=ihor.solodrai@pm.me \
--cc=acme@kernel.org \
--cc=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=dwarves@vger.kernel.org \
--cc=eddyz87@gmail.com \
--cc=mykolal@fb.com \
--cc=olsajiri@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox