public inbox for dwarves@vger.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Ihor Solodrai <ihor.solodrai@pm.me>,
	dwarves@vger.kernel.org,  acme@kernel.org
Cc: bpf@vger.kernel.org, alan.maguire@oracle.com, andrii@kernel.org,
	 mykolal@fb.com
Subject: Re: [RFC PATCH 5/9] btf_encoder: introduce elf_functions struct type
Date: Fri, 29 Nov 2024 13:50:18 -0800	[thread overview]
Message-ID: <0ea54d31ef9c473ee99f191bf00e451bb941710d.camel@gmail.com> (raw)
In-Reply-To: <20241128012341.4081072-6-ihor.solodrai@pm.me>

On Thu, 2024-11-28 at 01:24 +0000, Ihor Solodrai wrote:
> Extract elf_functions struct type from btf_encoder.
> 
> Replace methods operating functions table in btf_encoder by methods
> 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.
> 
> Signed-off-by: Ihor Solodrai <ihor.solodrai@pm.me>
> ---

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

[...]

> @@ -2132,31 +2110,59 @@ 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)
>  {
> -	bool base_addr_set = false;
> -	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;
> +
> +	/* We know that number of functions is less than number of symbols,
> +	 * so we can overallocate temporarily.
> +	 */
> +	functions->entries = calloc(nr_symbols, sizeof(struct elf_function));

Nit: use sizeof(*functions->entries) instead of sizeof(struct elf_function)
     here and elsewhere.

> +	if (!functions->entries) {
> +		err = -ENOMEM;
> +		goto out_free;
> +	}
>  
> -	elf_symtab__for_each_symbol_index(encoder->symtab, core_id, sym, sym_sec_idx) {
> -		if (!base_addr_set && sym_sec_idx && sym_sec_idx < encoder->seccnt) {
> -			encoder->functions.base_addr = encoder->secinfo[sym_sec_idx].addr;
> -			base_addr_set = true;
> +	functions->cnt = 0;
> +	elf_symtab__for_each_symbol_index(functions->symtab, core_id, sym, sym_sec_idx) {
> +		if (elf_functions__collect_function(functions, &sym)) {
> +			err = -1;
> +			goto out_free;

Nit: elf_functions__collect_function() never fails now (make it void?).

>  		}
> -		if (btf_encoder__collect_function(encoder, &sym))
> -			return -1;
>  	}
>  
> -	if (encoder->functions.cnt) {
> -		qsort(encoder->functions.entries, encoder->functions.cnt, sizeof(encoder->functions.entries[0]),
> +	if (functions->cnt) {
> +		qsort(functions->entries,
> +		      functions->cnt,
> +		      sizeof(functions->entries[0]),
>  		      functions_cmp);
> -		if (encoder->verbose)
> -			printf("Found %d functions!\n", encoder->functions.cnt);
> +	} 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)
> @@ -2417,6 +2423,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 */
>  
> @@ -2455,7 +2462,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)
> @@ -2486,7 +2493,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;

Nit: this cleanup code is repeated two times.




  reply	other threads:[~2024-11-29 21:50 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-28  1:23 [RFC PATCH 0/9] pahole: shared ELF and faster reproducible BTF encoding Ihor Solodrai
2024-11-28  1:23 ` [RFC PATCH 1/9] btf_encoder: simplify function encoding Ihor Solodrai
2024-11-29  8:16   ` Eduard Zingerman
2024-12-02 13:55   ` Jiri Olsa
2024-11-28  1:23 ` [RFC PATCH 2/9] btf_encoder: store,use section-relative addresses in ELF function representation Ihor Solodrai
2024-11-29  9:07   ` Eduard Zingerman
2024-12-02 14:34     ` Alan Maguire
2024-11-28  1:23 ` [RFC PATCH 3/9] btf_encoder: separate elf function, saved function representations Ihor Solodrai
2024-11-29 20:37   ` Eduard Zingerman
2024-12-09 21:19     ` Ihor Solodrai
2024-11-28  1:24 ` [RFC PATCH 4/9] dwarf_loader: introduce pre_load_module hook to conf_load Ihor Solodrai
2024-11-29 21:03   ` Eduard Zingerman
2024-11-28  1:24 ` [RFC PATCH 5/9] btf_encoder: introduce elf_functions struct type Ihor Solodrai
2024-11-29 21:50   ` Eduard Zingerman [this message]
2024-11-28  1:24 ` [RFC PATCH 6/9] btf_encoder: collect elf_functions in btf_encoder__pre_load_module Ihor Solodrai
2024-11-29 22:27   ` Eduard Zingerman
2024-11-28  1:24 ` [RFC PATCH 7/9] btf_encoder: switch to shared elf_functions table Ihor Solodrai
2024-11-29 22:35   ` Eduard Zingerman
2024-12-09 23:55     ` Ihor Solodrai
2024-11-28  1:24 ` [RFC PATCH 8/9] btf_encoder: introduce btf_encoding_context Ihor Solodrai
2024-11-29 23:12   ` Eduard Zingerman
2024-11-28  1:24 ` [RFC PATCH 9/9] pahole: faster reproducible BTF encoding Ihor Solodrai
2024-11-30  0:17   ` Eduard Zingerman
2024-12-02 13:55 ` [RFC PATCH 0/9] pahole: shared ELF and " Jiri Olsa
2024-12-06 18:19   ` Ihor Solodrai
2024-12-06 18:30     ` 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=0ea54d31ef9c473ee99f191bf00e451bb941710d.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=acme@kernel.org \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=dwarves@vger.kernel.org \
    --cc=ihor.solodrai@pm.me \
    --cc=mykolal@fb.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