pahole: faster reproducible BTF encoding
v3 cover pahole: faster reproducible BTF encoding
This is a v3 of the patchset aiming to speed up parallel reproducible BTF encoding.
In comparison to v2:
- removed patch v2 03 adding pre_load_module hook
- removed patch v2 05 making use of the hook
- since we will have a single btf_encoder, there is no need to collect ELF tables before encoders are created
- removed patch v2 07 adding btf_encoder_context
- patch v3 04 is a rewritten patch v2 06
- each btf_encoder now maintains it's own list of function tables per ELF
- patch v3 07 is an updated patch v2 10
- dwarf_loader multithreading is adjusted attempting to minimize blocking on locks
- new patch v3 08 increases the cu->obstack chunk size
- new patch v3 09 cleans up global list of encoders in btf_encoder.c
Testing:
- ./tests/tests pass on vmlinux built from bpf-next
- bpftool dump of reproducible BTF is identical to v1.28
Sample perf runs on 6.9 kernel with a production-like config, on a machine with nproc=176:
This patchset:
Performance counter stats for '/home/isolodrai/dwarves/build/pahole -J -j --btf_features=encode_force,var,float,enum64,decl_tag,type_tag,optimized_func,consistent_func,decl_tag_kfuncs --lang_exclude=rust --btf_encode_detached=/dev/null .tmp_vmlinux.btf' (13 runs):
17,911.11 msec cpu-clock # 4.412 CPUs utilized ( +- 0.46% )
4.0600 +- 0.0116 seconds time elapsed ( +- 0.29% )
pahole/next (v1.28):
Performance counter stats for '/home/isolodrai/dwarves/build/pahole -J -j --btf_features=encode_force,var,float,enum64,decl_tag,type_tag,optimized_func,consistent_func,decl_tag_kfuncs --lang_exclude=rust --btf_encode_detached=/dev/null .tmp_vmlinux.btf' (13 runs):
82,289.12 msec cpu-clock # 17.427 CPUs utilized ( +- 0.54% )
4.7219 +- 0.0270 seconds time elapsed ( +- 0.57% )v2: https://lore.kernel.org/dwarves/[email protected]/
v1 RFC: https://lore.kernel.org/dwarves/[email protected]/
Alan Maguire (2):
btf_encoder: simplify function encoding
btf_encoder: separate elf function, saved function representations
Ihor Solodrai (6):
btf_encoder: introduce elf_functions struct type
btf_encoder: introduce elf_functions_list
btf_encoder: remove skip_encoding_inconsistent_proto
dwarf_loader: introduce cu->id
dwarf_loader: multithreading with a job/worker model
btf_encoder: clean up global encoders list
btf_encoder.c | 643 +++++++++++++++++++----------------- btf_encoder.h | 7 +- btf_loader.c | 2 +- ctf_loader.c | 2 +- dwarf_loader.c | 335 +++++++++++++------ dwarves.c | 44 --- dwarves.h | 21 +- pahole.c | 230 ++----------- pdwtags.c | 3 +- pfunct.c | 3 +- tests/reproducible_build.sh | 5 +- 11 files changed, 605 insertions(+), 690 deletions(-)
Signature · 1 line
2.47.1
PATCH v3 1/8 btf_encoder: simplify function encoding
From: Alan Maguire <[email protected]>
Currently we have two modes of function encoding; one adds functions based upon the first instance found and ignores inconsistent representations. The second saves function representations and later finds inconsistencies. The mode chosen is determined by conf_load->skip_encoding_btf_inconsistent_proto.
The knock-on effect is that we need to support two modes in btf_encoder__add_func(); one for each case. Simplify by using the "save function" approach for both cases; only difference is that we allow inconsistent representations if skip_encoding_btf_inconsistent_proto is not set (it is set by default for upstream kernels and has been for a while).
--- btf_encoder.c | 79 +++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 53 deletions(-)
btf_encoder.c +26 -53
@@ -88,7 +88,6 @@ struct btf_encoder_func_state {struct elf_function {const char *name;char *alias; -bool generated;size_t prefixlen;struct btf_encoder_func_state state; };
@@ -120,6 +119,7 @@ struct btf_encoder { force, gen_floats, skip_encoding_decl_tag, + skip_encoding_inconsistent_proto, tag_kfuncs, gen_distilled_base;uint32_t array_index_id;
@@ -1165,18 +1165,18 @@ out:return err; } -static int32_t btf_encoder__add_func (struct btf_encoder *encoder,struct function *fn, +static int32_t btf_encoder__add_func (struct btf_encoder *encoder,struct elf_function *func) { +struct btf_encoder_func_state *state = &func->state;int btf_fnproto_id, btf_fn_id, tag_type_id =0 ;int16_t component_idx =-1 ;const char *name;const char *value;char tmp_value[KSYM_NAME_LEN ]; +uint16_t idx; -assert (fn !=NULL || func !=NULL ); - - btf_fnproto_id =btf_encoder__add_func_proto (encoder, fn ? &fn->proto :NULL , func); + btf_fnproto_id =btf_encoder__add_func_proto (encoder,NULL , func); name = func->alias ?: func->name;if (btf_fnproto_id >=0 ) btf_fn_id = btf_encoder__add_ref_type(encoder, BTF_KIND_FUNC, btf_fnproto_id,
@@ -1186,40 +1186,23 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder, struct functio name, btf_fnproto_id < 0 ? "proto" : "func");return -1 ; } -if (!fn) { -struct btf_encoder_func_state *state = &func->state; -uint16_t idx; - -if (state->nr_annots ==0 ) -return 0 ; +if (state->nr_annots ==0 ) +return 0 ; -for (idx =0 ; idx < state->nr_annots; idx++) { -struct btf_encoder_func_annot *a = &state->annots[idx]; +for (idx =0 ; idx < state->nr_annots; idx++) { +struct btf_encoder_func_annot *a = &state->annots[idx]; - value =btf__str_by_offset (encoder->btf, a->value); -/* adding BTF data may result in a mode of the -* value string memory, so make a temporary copy. -*/ -strncpy (tmp_value, value,sizeof (tmp_value) -1 ); - component_idx = a->component_idx; - - tag_type_id =btf_encoder__add_decl_tag (encoder, tmp_value, - btf_fn_id, component_idx); -if (tag_type_id <0 ) -break ; - } - }else { -struct llvm_annotation *annot; - -list_for_each_entry (annot, &fn->annots, node) { - value = annot->value; - component_idx = annot->component_idx; + value =btf__str_by_offset (encoder->btf, a->value); +/* adding BTF data may result in a mode of the +* value string memory, so make a temporary copy. +*/ +strncpy (tmp_value, value,sizeof (tmp_value) -1 ); + component_idx = a->component_idx; - tag_type_id =btf_encoder__add_decl_tag (encoder, value, btf_fn_id, - component_idx); -if (tag_type_id <0 ) -break ; - } + tag_type_id =btf_encoder__add_decl_tag (encoder, tmp_value, + btf_fn_id, component_idx); +if (tag_type_id <0 ) +break ; }if (tag_type_id <0 ) { fprintf(stderr,
@@ -1277,8 +1260,9 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder)* just do not _use_ them. Only exclude functions with * unexpected register use or multiple inconsistent prototypes. */ -if (!state->unexpected_reg && !state->inconsistent_proto) { -if (btf_encoder__add_func (encoder,NULL , func)) +if (!encoder->skip_encoding_inconsistent_proto || + (!state->unexpected_reg && !state->inconsistent_proto)) { +if (btf_encoder__add_func (encoder, func))return -1 ; } state->processed =1 ;
@@ -2353,6 +2337,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->force = conf_load->btf_encode_force; encoder->gen_floats = conf_load->btf_gen_floats; encoder->skip_encoding_decl_tag = conf_load->skip_encoding_btf_decl_tag; + encoder->skip_encoding_inconsistent_proto = conf_load->skip_encoding_btf_inconsistent_proto; encoder->tag_kfuncs = conf_load->btf_decl_tag_kfuncs; encoder->gen_distilled_base = conf_load->btf_gen_distilled_base; encoder->verbose = verbose;
@@ -2558,7 +2543,6 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct cocu__for_each_function (cu, core_id, fn) {struct elf_function *func =NULL ; -bool save = false;/* * Skip functions that:
@@ -2580,15 +2564,8 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co/* prefer exact function name match... */ func =btf_encoder__find_function (encoder, name,0 ); -if (func) { -if (func->generated) -continue ; -if (conf_load->skip_encoding_btf_inconsistent_proto) - save = true; -else - func->generated = true; - }else if (encoder->functions.suffix_cnt && - conf_load->btf_gen_optimized) { +if (!func && encoder->functions.suffix_cnt && + conf_load->btf_gen_optimized) {/* falling back to name.isra.0 match if no exact * match is found; only bother if we found any * .suffix function names. The function
@@ -2599,7 +2576,6 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co func =btf_encoder__find_function (encoder, name,strlen (name));if (func) { - save = true;if (encoder->verbose) printf("matched function '%s' with '%s'%s\n", name, func->name,
@@ -2617,10 +2593,7 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct coif (!func)continue ; -if (save) - err =btf_encoder__save_func (encoder, fn, func); -else - err =btf_encoder__add_func (encoder, fn, func); + err =btf_encoder__save_func (encoder, fn, func);if (err) goto out; }
Signature · 1 line
2.47.1
PATCH v3 2/8 btf_encoder: separate elf function, saved function representations
In reply to Ihor SolodraiFrom: Alan Maguire <[email protected]>
Have saved function representation point back at immutable ELF function table. This will make sharing the ELF function table across encoders easier. Simply accumulate saved functions for each encoder, and on completion combine them into a name-sorted list. Then carry out comparisons to check for inconsistent representations, skipping functions that are inconsistent in their representation.
/usr/bin/time samples with this change:
jobs 1, mem 837844 Kb, time 6.40 sec
jobs 2, mem 936204 Kb, time 3.88 sec
jobs 4, mem 1023120 Kb, time 2.75 sec
jobs 8, mem 1163824 Kb, time 2.31 sec
jobs 16, mem 1190588 Kb, time 2.08 sec
jobs 32, mem 1341180 Kb, time 2.36 sec
/usr/bin/time samples on next (3ddadc1):
jobs 1, mem 834100 Kb, time 6.20 sec
jobs 2, mem 925048 Kb, time 3.81 sec
jobs 4, mem 1025424 Kb, time 2.88 sec
jobs 8, mem 1178480 Kb, time 2.21 sec
jobs 16, mem 1241780 Kb, time 2.07 sec
jobs 32, mem 1442316 Kb, time 2.33 sec
--- btf_encoder.c | 293 +++++++++++++++++++++++++++----------------------- btf_encoder.h | 1 + pahole.c | 2 + 3 files changed, 161 insertions(+), 135 deletions(-)
btf_encoder.c +158 -135
@@ -72,14 +72,15 @@ struct btf_encoder_func_annot {/* state used to do later encoding of saved functions */ struct btf_encoder_func_state { +struct list_head node; +struct btf_encoder *encoder; +struct elf_function *elf;uint32_t type_id_off;uint16_t nr_parms;uint16_t nr_annots; -uint8_t initialized:1 ;uint8_t optimized_parms:1 ;uint8_t unexpected_reg:1 ;uint8_t inconsistent_proto:1 ; -uint8_t processed:1 ;int ret_type_id;struct btf_encoder_func_parm *parms;struct btf_encoder_func_annot *annots;
@@ -89,7 +90,6 @@ struct elf_function {const char *name;char *alias;size_t prefixlen; -struct btf_encoder_func_state state; };struct elf_secinfo {
@@ -126,6 +126,7 @@ struct btf_encoder {struct elf_secinfo *secinfo;size_t seccnt;int encode_vars; +struct list_head func_states;struct {struct elf_function *entries;int allocated;
@@ -148,8 +149,6 @@ struct btf_kfunc_set_range {static LIST_HEAD (encoders);static pthread_mutex_t encoders__lock =PTHREAD_MUTEX_INITIALIZER ; -static int btf_encoder__add_saved_funcs (struct btf_encoder *encoder); -/* mutex only needed for add/delete, as this can happen in multiple encoding * threads. Traversal of the list is currently confined to thread collection. */
@@ -693,25 +692,26 @@ static int32_t btf_encoder__tag_type(struct btf_encoder *encoder, uint32_t tag_t }static int32_t btf_encoder__add_func_proto (struct btf_encoder *encoder,struct ftype *ftype, -struct elf_function *func) +struct btf_encoder_func_state *state) { -struct btf *btf = encoder->btf;const struct btf_type *t; +struct btf *btf;struct parameter *param;uint16_t nr_params, param_idx;int32_t id, type_id;char tmp_name[KSYM_NAME_LEN ];const char *name; -struct btf_encoder_func_state *state; -assert (ftype !=NULL || func !=NULL ); +assert (ftype !=NULL || state !=NULL );/* add btf_type for func_proto */ if (ftype) { + btf = encoder->btf; nr_params = ftype->nr_parms + (ftype->unspec_parms ?1 :0 ); type_id =btf_encoder__tag_type (encoder, ftype->tag.type); - }else if (func) { - state = &func->state; + }else if (state) { + encoder = state->encoder; + btf = state->encoder->btf; nr_params = state->nr_parms; type_id = state->ret_type_id; }else {
@@ -801,8 +801,6 @@ int32_t btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoderif (encoder == other)return 0 ; -btf_encoder__add_saved_funcs (other); -for (shndx =1 ; shndx < other->seccnt; shndx++) {struct gobuffer *var_secinfo_buf = &other->secinfo[shndx].secinfo;size_t sz =gobuffer__size (var_secinfo_buf);
@@ -1031,10 +1029,13 @@ static bool types__match(struct btf_encoder *encoder,return false; } -static bool funcs__match (struct btf_encoder *encoder,struct elf_function *func, -struct btf *btf1,struct btf_encoder_func_state *s1, -struct btf *btf2,struct btf_encoder_func_state *s2) +static bool funcs__match (struct btf_encoder_func_state *s1, +struct btf_encoder_func_state *s2) { +struct btf_encoder *encoder = s1->encoder; +struct elf_function *func = s1->elf; +struct btf *btf1 = s1->encoder->btf; +struct btf *btf2 = s2->encoder->btf;uint8_t i;if (s1->nr_parms != s2->nr_parms) {
@@ -1072,8 +1073,7 @@ static bool funcs__match(struct btf_encoder *encoder, struct elf_function *func,static int32_t btf_encoder__save_func (struct btf_encoder *encoder,struct function *fn,struct elf_function *func) { -struct btf_encoder_func_state *existing = &func->state; -struct btf_encoder_func_state state = {0 }; +struct btf_encoder_func_state *state =zalloc (sizeof (*state));struct ftype *ftype = &fn->proto;struct btf *btf = encoder->btf;struct llvm_annotation *annot;
@@ -1081,22 +1081,23 @@ static int32_t btf_encoder__save_func(struct btf_encoder *encoder, struct functiuint8_t param_idx =0 ;int str_off, err =0 ; -/* if already skipping this function, no need to proceed. */ -if (existing->unexpected_reg || existing->inconsistent_proto) -return 0 ; +if (!state) +return -ENOMEM ; - state.nr_parms = ftype->nr_parms + (ftype->unspec_parms ?1 :0 ); - state.ret_type_id = ftype->tag.type ==0 ?0 : encoder->type_id_off + ftype->tag.type; -if (state.nr_parms >0 ) { - state.parms =zalloc (state.nr_parms *sizeof (*state.parms)); -if (!state.parms) { + state->encoder = encoder; + state->elf = func; + state->nr_parms = ftype->nr_parms + (ftype->unspec_parms ?1 :0 ); + state->ret_type_id = ftype->tag.type ==0 ?0 : encoder->type_id_off + ftype->tag.type; +if (state->nr_parms >0 ) { + state->parms =zalloc (state->nr_parms *sizeof (*state->parms)); +if (!state->parms) { err = -ENOMEM ; goto out; } } - state.inconsistent_proto = ftype->inconsistent_proto; - state.unexpected_reg = ftype->unexpected_reg; - state.optimized_parms = ftype->optimized_parms; + state->inconsistent_proto = ftype->inconsistent_proto; + state->unexpected_reg = ftype->unexpected_reg; + state->optimized_parms = ftype->optimized_parms;ftype__for_each_parameter (ftype, param) {const char *name =parameter__name (param) ?:"" ;
@@ -1105,21 +1106,21 @@ static int32_t btf_encoder__save_func(struct btf_encoder *encoder, struct functi err = str_off; goto out; } - state.parms[param_idx].name_off = str_off; - state.parms[param_idx].type_id = param->tag.type ==0 ?0 : - encoder->type_id_off + param->tag.type; + state->parms[param_idx].name_off = str_off; + state->parms[param_idx].type_id = param->tag.type ==0 ?0 : + encoder->type_id_off + param->tag.type; param_idx++; }if (ftype->unspec_parms) - state.parms[param_idx].type_id =0 ; + state->parms[param_idx].type_id =0 ;list_for_each_entry (annot, &fn->annots, node) - state.nr_annots++; -if (state.nr_annots) { + state->nr_annots++; +if (state->nr_annots) {uint8_t idx =0 ; - state.annots =zalloc (state.nr_annots *sizeof (*state.annots)); -if (!state.annots) { + state->annots =zalloc (state->nr_annots *sizeof (*state->annots)); +if (!state->annots) { err = -ENOMEM ; goto out; }
@@ -1129,46 +1130,24 @@ static int32_t btf_encoder__save_func(struct btf_encoder *encoder, struct functi err = str_off; goto out; } - state.annots[idx].value = str_off; - state.annots[idx].component_idx = annot->component_idx; + state->annots[idx].value = str_off; + state->annots[idx].component_idx = annot->component_idx; idx++; } } - state.initialized =1 ; - -if (state.unexpected_reg) -btf_encoder__log_func_skip (encoder, func, -"unexpected register used for parameter\n" ); -if (!existing->initialized) { -memcpy (existing, &state,sizeof (*existing)); -return 0 ; - } - -/* If saving and we find an existing entry, we want to merge -* observations across both functions, checking that the -* "seen optimized parameters", "inconsistent prototype" -* and "unexpected register" status is reflected in the -* func entry. -* If the entry is new, record encoder state required -* to add the local function later (encoder + type_id_off) -* such that we can add the function later. -*/ - existing->optimized_parms |= state.optimized_parms; - existing->unexpected_reg |= state.unexpected_reg; -if (!existing->unexpected_reg && - !funcs__match (encoder, func, encoder->btf, &state, - encoder->btf, existing)) - existing->inconsistent_proto =1 ; +list_add_tail (&state->node, &encoder->func_states); +return 0 ; out: -zfree (&state.annots); -zfree (&state.parms); +zfree (&state->annots); +zfree (&state->parms); +free (state);return err; }static int32_t btf_encoder__add_func (struct btf_encoder *encoder, -struct elf_function *func) +struct btf_encoder_func_state *state) { -struct btf_encoder_func_state *state = &func->state; +struct elf_function *func = state->elf;int btf_fnproto_id, btf_fn_id, tag_type_id =0 ;int16_t component_idx =-1 ;const char *name;
@@ -1176,7 +1155,7 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,char tmp_value[KSYM_NAME_LEN ];uint16_t idx; - btf_fnproto_id =btf_encoder__add_func_proto (encoder,NULL , func); + btf_fnproto_id =btf_encoder__add_func_proto (encoder,NULL , state); name = func->alias ?: func->name;if (btf_fnproto_id >=0 ) btf_fn_id = btf_encoder__add_ref_type(encoder, BTF_KIND_FUNC, btf_fnproto_id,
@@ -1214,62 +1193,6 @@ static int32_t btf_encoder__add_func(struct btf_encoder *encoder,return 0 ; } -static int btf_encoder__add_saved_funcs (struct btf_encoder *encoder) -{ -int i; - -for (i =0 ; i < encoder->functions.cnt; i++) { -struct elf_function *func = &encoder->functions.entries[i]; -struct btf_encoder_func_state *state = &func->state; -struct btf_encoder *other_encoder =NULL ; - -if (!state->initialized || state->processed) -continue ; -/* merge optimized-out status across encoders; since each -* encoder has the same elf symbol table we can use the -* same index to access the same elf symbol. -*/ -btf_encoders__for_each_encoder (other_encoder) { -struct elf_function *other_func; -struct btf_encoder_func_state *other_state; -uint8_t optimized, unexpected, inconsistent; - -if (other_encoder == encoder) -continue ; - - other_func = &other_encoder->functions.entries[i]; - other_state = &other_func->state; -if (!other_state->initialized) -continue ; - optimized = state->optimized_parms | other_state->optimized_parms; - unexpected = state->unexpected_reg | other_state->unexpected_reg; - inconsistent = state->inconsistent_proto | other_state->inconsistent_proto; -if (!unexpected && !inconsistent && - !funcs__match (encoder, func, - encoder->btf, state, - other_encoder->btf, other_state)) - inconsistent =1 ; - state->optimized_parms = other_state->optimized_parms = optimized; - state->unexpected_reg = other_state->unexpected_reg = unexpected; - state->inconsistent_proto = other_state->inconsistent_proto = inconsistent; - - other_state->processed =1 ; - } -/* do not exclude functions with optimized-out parameters; they -* may still be _called_ with the right parameter values, they -* just do not _use_ them. Only exclude functions with -* unexpected register use or multiple inconsistent prototypes. -*/ -if (!encoder->skip_encoding_inconsistent_proto || - (!state->unexpected_reg && !state->inconsistent_proto)) { -if (btf_encoder__add_func (encoder, func)) -return -1 ; - } - state->processed =1 ; - } -return 0 ; -} -static int functions_cmp (const void *_a,const void *_b) {const struct elf_function *a = _a;
@@ -1297,6 +1220,110 @@ static void *reallocarray_grow(void *ptr, int *nmemb, size_t size)return new; } +static int saved_functions_cmp (const void *_a,const void *_b) +{ +struct btf_encoder_func_state *const *a = _a; +struct btf_encoder_func_state *const *b = _b; + +return functions_cmp ((*a)->elf, (*b)->elf); +} + +static int saved_functions_combine (void *_a,void *_b) +{ +uint8_t optimized, unexpected, inconsistent; +struct btf_encoder_func_state *a = _a; +struct btf_encoder_func_state *b = _b; +int ret; + + ret =strncmp (a->elf->name, b->elf->name, +max (a->elf->prefixlen, b->elf->prefixlen)); +if (ret !=0 ) +return ret; + optimized = a->optimized_parms | b->optimized_parms; + unexpected = a->unexpected_reg | b->unexpected_reg; + inconsistent = a->inconsistent_proto | b->inconsistent_proto; +if (!unexpected && !inconsistent && !funcs__match (a, b)) + inconsistent =1 ; + a->optimized_parms = b->optimized_parms = optimized; + a->unexpected_reg = b->unexpected_reg = unexpected; + a->inconsistent_proto = b->inconsistent_proto = inconsistent; + +return 0 ; +} + +static void btf_encoder__delete_saved_funcs (struct btf_encoder *encoder) +{ +struct btf_encoder_func_state *pos, *s; + +list_for_each_entry_safe (pos, s, &encoder->func_states, node) { +list_del (&pos->node); +free (pos->parms); +free (pos->annots); +free (pos); + } + +for (int i =0 ; i < encoder->functions.cnt; i++) +free (encoder->functions.entries[i].alias); +} + +int btf_encoder__add_saved_funcs (struct btf_encoder *encoder) +{ +struct btf_encoder_func_state **saved_fns, *s; +struct btf_encoder *e =NULL ; +int i =0 , j, nr_saved_fns =0 ; + +/* Retrieve function states from each encoder, combine them +* and sort by name, addr. +*/ +btf_encoders__for_each_encoder (e) { +list_for_each_entry (s, &e->func_states, node) + nr_saved_fns++; + } + +if (nr_saved_fns ==0 ) +return 0 ; + + saved_fns =calloc (nr_saved_fns,sizeof (*saved_fns)); +btf_encoders__for_each_encoder (e) { +list_for_each_entry (s, &e->func_states, node) + saved_fns[i++] = s; + } +qsort (saved_fns, nr_saved_fns,sizeof (*saved_fns), saved_functions_cmp); + +for (i =0 ; i < nr_saved_fns; i = j) { +struct btf_encoder_func_state *state = saved_fns[i]; + +/* Compare across sorted functions that match by name/prefix; +* share inconsistent/unexpected reg state between them. +*/ + j = i +1 ; + +while (j < nr_saved_fns &&saved_functions_combine (saved_fns[i], saved_fns[j]) ==0 ) + j++; + +/* do not exclude functions with optimized-out parameters; they +* may still be _called_ with the right parameter values, they +* just do not _use_ them. Only exclude functions with +* unexpected register use or multiple inconsistent prototypes. +*/ +if (!encoder->skip_encoding_inconsistent_proto || + (!state->unexpected_reg && !state->inconsistent_proto)) { +if (btf_encoder__add_func (state->encoder, state)) { +free (saved_fns); +return -1 ; + } + } + } + +/* Now that we are done with function states, free them. */ +free (saved_fns); +btf_encoders__for_each_encoder (e) { +btf_encoder__delete_saved_funcs (e); + } + +return 0 ; +} +static int btf_encoder__collect_function (struct btf_encoder *encoder,GElf_Sym *sym) {struct elf_function *new;
@@ -1330,6 +1357,8 @@ static int btf_encoder__collect_function(struct btf_encoder *encoder, GElf_Sym * encoder->functions.suffix_cnt++; encoder->functions.entries[encoder->functions.cnt].prefixlen = suffix - name; + }else { + encoder->functions.entries[encoder->functions.cnt].prefixlen =strlen (name); } encoder->functions.cnt++;return 0 ;
@@ -2345,6 +2374,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->need_index_type = false; encoder->array_index_id =0 ; encoder->encode_vars =0 ; +INIT_LIST_HEAD (&encoder->func_states);if (!conf_load->skip_encoding_btf_vars) encoder->encode_vars |=BTF_VAR_PERCPU ;if (conf_load->encode_btf_global_vars)
@@ -2429,16 +2459,8 @@ out_delete:return NULL ; } -void btf_encoder__delete_func (struct elf_function *func) -{ -free (func->alias); -zfree (&func->state.annots); -zfree (&func->state.parms); -} -void btf_encoder__delete (struct btf_encoder *encoder) { -int i;size_t shndx;if (encoder ==NULL )
@@ -2447,18 +2469,19 @@ void btf_encoder__delete(struct btf_encoder *encoder)btf_encoders__delete (encoder);for (shndx =0 ; shndx < encoder->seccnt; shndx++)__gobuffer__delete (&encoder->secinfo[shndx].secinfo); +free (encoder->secinfo);zfree (&encoder->filename);zfree (&encoder->source_filename);btf__free (encoder->btf); encoder->btf =NULL ;elf_symtab__delete (encoder->symtab); -for (i =0 ; i < encoder->functions.cnt; i++) -btf_encoder__delete_func (&encoder->functions.entries[i]); encoder->functions.allocated = encoder->functions.cnt =0 ;free (encoder->functions.entries); encoder->functions.entries =NULL ; +btf_encoder__delete_saved_funcs (encoder); +free (encoder); }
btf_encoder.h +1 -0
@@ -33,5 +33,6 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct costruct btf *btf_encoder__btf (struct btf_encoder *encoder);int btf_encoder__add_encoder (struct btf_encoder *encoder,struct btf_encoder *other); +int btf_encoder__add_saved_funcs (struct btf_encoder *encoder);#endif /* _BTF_ENCODER_H_ */
pahole.c +2 -0
@@ -3185,6 +3185,7 @@ static int pahole_threads_collect(struct conf_load *conf, int nr_threads, void *if (error) goto out; +btf_encoder__add_saved_funcs (btf_encoder);for (i =0 ; i < nr_threads; i++) {/* * Merge content of the btf instances of worker threads to the btf
@@ -3843,6 +3844,7 @@ try_sole_arg_as_class_names: } err =btf_encoder__encode (btf_encoder); +btf_encoder__delete (btf_encoder);if (err) {fputs ("Failed to encode BTF\n" , stderr); goto out_cus_delete;
Signature · 1 line
2.47.1
Re: [PATCH dwarves v3 2/8] btf_encoder: separate elf function, saved function representations
On Sat, Dec 21, 2024 at 01:23:01AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 9 lines
btf_encoder.c +9
-0+static int saved_functions_combine (void *_a,void *_b) +{ +uint8_t optimized, unexpected, inconsistent; +struct btf_encoder_func_state *a = _a; +struct btf_encoder_func_state *b = _b; +int ret; + + ret =strncmp (a->elf->name, b->elf->name, +max (a->elf->prefixlen, b->elf->prefixlen));
btf_encoder.c static void *reallocarray_grow(void *ptr, int *nmemb, size_t size) · line 1249
Quoted 10 lines
btf_encoder.c +10
-0+if (ret !=0 ) +return ret; + optimized = a->optimized_parms | b->optimized_parms; + unexpected = a->unexpected_reg | b->unexpected_reg; + inconsistent = a->inconsistent_proto | b->inconsistent_proto; +if (!unexpected && !inconsistent && !funcs__match (a, b)) + inconsistent =1 ; + a->optimized_parms = b->optimized_parms = optimized; + a->unexpected_reg = b->unexpected_reg = unexpected; + a->inconsistent_proto = b->inconsistent_proto = inconsistent;
do we still need to update the 'b' state object?
Quoted 22 lines
btf_encoder.c +22
-0+ +return 0 ; +} + +static void btf_encoder__delete_saved_funcs (struct btf_encoder *encoder) +{ +struct btf_encoder_func_state *pos, *s; + +list_for_each_entry_safe (pos, s, &encoder->func_states, node) { +list_del (&pos->node); +free (pos->parms); +free (pos->annots); +free (pos); + } + +for (int i =0 ; i < encoder->functions.cnt; i++) +free (encoder->functions.entries[i].alias); +} + +int btf_encoder__add_saved_funcs (struct btf_encoder *encoder) +{ +struct btf_encoder_func_state **saved_fns, *s;
btf_encoder.c static void *reallocarray_grow(void *ptr, int *nmemb, size_t size) · line 1281
Quoted 10 lines
btf_encoder.c +10
-0+struct btf_encoder *e =NULL ; +int i =0 , j, nr_saved_fns =0 ; + +/* Retrieve function states from each encoder, combine them +* and sort by name, addr. +*/ +btf_encoders__for_each_encoder (e) { +list_for_each_entry (s, &e->func_states, node) + nr_saved_fns++; + }
the encoder loop goes eventualy away, but still would it make to store func_states count instead of the loop?
now when there's just single place that stores 'state' it seems like it should be straighforward
SNIP
btf_encoder.c void btf_encoder__delete(struct btf_encoder *encoder) · line 2472
Quoted 11 lines
void
btf_encoder__delete(struct btf_encoder *encoder){btf_encoder.c +1
-1-int i;size_t shndx;if (encoder ==NULL )@@ -2447,18 +2469,19 @@ void btf_encoder__delete(struct btf_encoder *encoder)btf_encoders__delete (encoder);for (shndx =0 ; shndx < encoder->seccnt; shndx++)__gobuffer__delete (&encoder->secinfo[shndx].secinfo); +free (encoder->secinfo);
nit, this seems unrelated to this change, should be in separate fix?
thanks,
jirka
Quoted 17 lines
btf_encoder.c +2
-2zfree (&encoder->filename);zfree (&encoder->source_filename);btf__free (encoder->btf); encoder->btf =NULL ;elf_symtab__delete (encoder->symtab); -for (i =0 ; i < encoder->functions.cnt; i++) -btf_encoder__delete_func (&encoder->functions.entries[i]); encoder->functions.allocated = encoder->functions.cnt =0 ;free (encoder->functions.entries); encoder->functions.entries =NULL ; +btf_encoder__delete_saved_funcs (encoder); +free (encoder); }
SNIP
Re: [PATCH dwarves v3 2/8] btf_encoder: separate elf function, saved function representations
Quoted 20 lines · Jiri Olsa
On Sat, Dec 21, 2024 at 01:23:01AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 14 lines
btf_encoder.c +14
-0+static int saved_functions_combine (void *_a,void *_b) +{ +uint8_t optimized, unexpected, inconsistent; +struct btf_encoder_func_state *a = _a; +struct btf_encoder_func_state *b = _b; +int ret; + + ret =strncmp (a->elf->name, b->elf->name, +max (a->elf->prefixlen, b->elf->prefixlen)); +if (ret !=0 ) +return ret; + optimized = a->optimized_parms | b->optimized_parms; + unexpected = a->unexpected_reg | b->unexpected_reg; + inconsistent = a->inconsistent_proto | b->inconsistent_proto;
Quoted 8 lines
Quoted 5 lines
+ if (!unexpected && !inconsistent && !funcs__match(a, b))
+ inconsistent = 1;
+a->optimized_parms=b->optimized_parms= optimized;
+a->unexpected_reg=b->unexpected_reg= unexpected;
+a->inconsistent_proto=b->inconsistent_proto= inconsistent;do we still need to update the 'b' state object?
Hi Jiri, thanks for review.
Given how this function is used, we don't need to. But this is a kind of a "merge", and IMO having both states equal on the way out makes sense.
while (j < nr_saved_fns && saved_functions_combine(saved_fns[i], saved_fns[j]) == 0) j++;
Quoted 38 lines
Quoted 23 lines
btf_encoder.c +22
-0+ +return 0 ; +} + +static void btf_encoder__delete_saved_funcs (struct btf_encoder *encoder) +{ +struct btf_encoder_func_state *pos, *s; + +list_for_each_entry_safe (pos, s, &encoder->func_states, node) { +list_del (&pos->node); +free (pos->parms); +free (pos->annots); +free (pos); + } + +for (int i =0 ; i < encoder->functions.cnt; i++) +free (encoder->functions.entries[i].alias); +} + +int btf_encoder__add_saved_funcs (struct btf_encoder *encoder) +{ +struct btf_encoder_func_state **saved_fns, *s;btf_encoder.c static void *reallocarray_grow(void *ptr, int *nmemb, size_t size) · line 1281
Quoted 10 lines
btf_encoder.c +10
-0+struct btf_encoder e = NULL; +int i = 0, j, nr_saved_fns = 0; + +/ Retrieve function states from each encoder, combine them +* and sort by name, addr. +*/ +btf_encoders__for_each_encoder (e) { +list_for_each_entry (s, &e->func_states, node) + nr_saved_fns++; + }the encoder loop goes eventualy away, but still would it make to store
func_statescount instead of the loop?
Quoted 2 lines
now when there's just single place that stores 'state' it seems like it should be straighforward
Yeah.
Quoted 8 lines
SNIP
Quoted 5 lines
void
btf_encoder__delete(struct btf_encoder *encoder){
- int i;size_tshndx;
Quoted 9 lines
btf_encoder.c void btf_encoder__delete(struct btf_encoder *encoder) · line 2472
Quoted 6 lines
if (encoder == NULL)
btf_encoder.c +1
-0@@ -2447,18 +2469,19 @@ void btf_encoder__delete(struct btf_encoder *encoder)btf_encoders__delete (encoder);for (shndx =0 ; shndx < encoder->seccnt; shndx++)__gobuffer__delete (&encoder->secinfo[shndx].secinfo); +free (encoder->secinfo);nit, this seems unrelated to this change, should be in separate fix?
IIRC this was also caught by the sanitizer. I'll sliver this into a separate patch in the next iteration.
Quoted 23 lines
thanks,
jirkaQuoted 16 lines
btf_encoder.c +2
-2zfree (&encoder->filename);zfree (&encoder->source_filename);btf__free (encoder->btf); encoder->btf =NULL ;elf_symtab__delete (encoder->symtab); -for (i =0 ; i < encoder->functions.cnt; i++) -btf_encoder__delete_func (&encoder->functions.entries[i]); encoder->functions.allocated = encoder->functions.cnt =0 ;free (encoder->functions.entries); encoder->functions.entries =NULL ; +btf_encoder__delete_saved_funcs (encoder); +free (encoder); }SNIP
PATCH v3 3/8 btf_encoder: introduce elf_functions struct type
In reply to Ihor SolodraiExtract 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.
--- btf_encoder.c | 120 +++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 56 deletions(-)
btf_encoder.c +64 -56
@@ -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;
@@ -1324,44 +1316,29 @@ int btf_encoder__add_saved_funcs(struct btf_encoder *encoder)return 0 ; } -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,
@@ -2126,26 +2103,56 @@ 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; -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; + } + + functions->cnt =0 ; +elf_symtab__for_each_symbol_index (functions->symtab, core_id, sym, sym_sec_idx) { +elf_functions__collect_function (functions, &sym); } -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), 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)
@@ -2406,6 +2413,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamprintf ("%s: '%s' doesn't have symtab.\n" , __func__, cu->filename); goto out; } + encoder->functions.symtab = encoder->symtab;/* index the ELF sections for later lookup */
@@ -2444,7 +2452,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamif (!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)
@@ -2476,7 +2484,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 ;
Signature · 1 line
2.47.1
Re: [PATCH dwarves v3 3/8] btf_encoder: introduce elf_functions struct type
On Sat, Dec 21, 2024 at 01:23:10AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 29 lines
btf_encoder.c +5
-19-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)
btf_encoder.c int btf_encoder__add_saved_funcs(struct btf_encoder *encoder) · line 1357
Quoted 10 lines
btf_encoder.c +3
-4+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,'.' ); -
nit, let's keep that new line after declaration
Quoted 31 lines
btf_encoder.c +10
-8- 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,@@ -2126,26 +2103,56 @@ 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;
btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder) · line 2121
Quoted 10 lines
btf_encoder.c +7
-3-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;
you could just return -ENOMEM here
Quoted 4 lines
btf_encoder.c +4
-0+ } + + functions->cnt =0 ; +elf_symtab__for_each_symbol_index (functions->symtab, core_id, sym, sym_sec_idx) {
btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder) · line 2133
Quoted 10 lines
btf_encoder.c +5
-2+elf_functions__collect_function (functions, &sym); } -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), functions_cmp);
nit, why not keep the single line?
Quoted 28 lines
btf_encoder.c +19
-2-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)
btf_encoder.c struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam · line 2416
Quoted 4 lines
btf_encoder.c +1
-0@@ -2406,6 +2413,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamprintf ("%s: '%s' doesn't have symtab.\n" , __func__, cu->filename); goto out; } + encoder->functions.symtab = encoder->symtab;
I was wondering if we need to keep both symtab pointers, but it's sorted out in the next patch ;-)
thanks,
jirka
Quoted 26 lines
btf_encoder.c +2
-2/* index the ELF sections for later lookup */ @@ -2444,7 +2452,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamif (!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)@@ -2476,7 +2484,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 ;Signature · 1 line
2.47.1
Re: [PATCH dwarves v3 3/8] btf_encoder: introduce elf_functions struct type
Quoted 36 lines · Jiri Olsa
On Sat, Dec 21, 2024 at 01:23:10AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 31 lines
btf_encoder.c +1
-1-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){
- structelf_function*new;
+ structelf_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_functionsis called
- * inbtf_encoder__encode_cuerror path.
- */
- return -1;
- }
-encoder->functions.entries= new;
- }
+ name =elf_sym__name(sym, functions->symtab);
+ if (!name)
+ return;
Quoted 11 lines
Quoted 8 lines
- 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, '.');
-nit, let's keep that new line after declaration
ok
Quoted 32 lines
Quoted 32 lines
- 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 structbtf_encoder*encoder,btf_encoder.c +5
-3@@ -2126,26 +2103,56 @@ 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;
Quoted 13 lines
btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder) · line 2121
Quoted 10 lines
btf_encoder.c +7
-3-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;you could just return
-ENOMEMhere
I am trying to adhere to the kernel style, although not very strictly. It's recommended [1] to have a single exit from a function when there is cleanup work.
I usually check my patches with a script [2] before submitting.
[1] https://www.kernel.org/doc/html/v4.10/process/coding-style.html#centralized-exiting-of-functions
[2] https://github.com/torvalds/linux/blob/master/scripts/checkpatch.pl
Quoted 17 lines
Quoted 5 lines
btf_encoder.c +4
-0+ } + + functions->cnt =0 ; +elf_symtab__for_each_symbol_index (functions->symtab, core_id, sym, sym_sec_idx) {btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder) · line 2133
Quoted 10 lines
btf_encoder.c +5
-2+elf_functions__collect_function (functions, &sym); } -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), functions_cmp);
Quoted 1 line
nit, why not keep the single line?
How many chars in a line is too many? :)
Quoted 42 lines
Quoted 29 lines
btf_encoder.c +19
-2-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)btf_encoder.c struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam · line 2416
Quoted 4 lines
btf_encoder.c +1
-0@@ -2406,6 +2413,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamprintf ("%s: '%s' doesn't have symtab.\n" , func, cu->filename); goto out; } + encoder->functions.symtab = encoder->symtab;I was wondering if we need to keep both symtab pointers, but it's sorted out in the next patch ;-)
thanks,
jirka[...]
PATCH v3 4/8 btf_encoder: introduce elf_functions_list
In reply to Ihor SolodraiIn case of processing of multiple DWARF modules, multiple ELFs are read. This requires maintaining and elf_functions table per ELF.
Replace btf_encoder.functions with btf_encoder.elf_functions_list, that contains all necessary elf_functions tables.
The list is initialized when btf_encoder is created. When a new CU is assigned to the encoder in btf_encoder__encode_cu, an elf_functions table will be created if the CU is coming from an unknown Elf.
This patch is a variant of [1], following a discussion at [2].
[1] https://lore.kernel.org/bpf/[email protected]/
[2] https://lore.kernel.org/bpf/C82bYTvJaV4bfT15o25EsBiUvFsj5eTlm17933Hvva76CXjIcu3gvpaOCWPgeZ8g3cZ-RMa8Vp0y1o_QMR2LhPB-LEUYfZCGuCfR_HvkIP8=@pm.me/
--- btf_encoder.c | 138 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 115 insertions(+), 23 deletions(-)
btf_encoder.c +115 -23
@@ -102,6 +102,8 @@ struct elf_secinfo { };struct elf_functions { +struct list_head node;/* for elf_functions_list */ +Elf *elf;/* source ELF */ struct elf_symtab *symtab;struct elf_function *entries;int cnt;
@@ -134,7 +136,11 @@ struct btf_encoder {size_t seccnt;int encode_vars;struct list_head func_states; -struct elf_functions functions; +/* This is a list of elf_functions tables, one per ELF. +* Multiple ELF modules can be processed in one pahole run, +* so we have to store elf_functions tables per ELF. +*/ +struct list_head elf_functions_list; };struct btf_func {
@@ -148,6 +154,72 @@ struct btf_kfunc_set_range {uint64_t end; }; +static inline void elf_functions__delete (struct elf_functions *funcs) +{ +for (int i =0 ; i < funcs->cnt; i++) +free (funcs->entries[i].alias); +free (funcs->entries); +elf_symtab__delete (funcs->symtab); +list_del (&funcs->node); +free (funcs); +} + +static int elf_functions__collect (struct elf_functions *functions); + +struct elf_functions *elf_functions__new (Elf *elf) +{ +struct elf_functions *funcs; +int err; + + funcs =calloc (1 ,sizeof (*funcs)); +if (!funcs) { + err = -ENOMEM ; + goto out_delete; + } + + funcs->symtab =elf_symtab__new (NULL , elf); +if (!funcs->symtab) { + err =-1 ; + goto out_delete; + } + + funcs->elf = elf; + err =elf_functions__collect (funcs); +if (err <0 ) + goto out_delete; + +return funcs; + +out_delete: +elf_functions__delete (funcs); +return NULL ; +} + +static inline void elf_functions_list__clear (struct list_head *elf_functions_list) +{ +struct elf_functions *funcs; +struct list_head *pos, *tmp; + +list_for_each_safe (pos, tmp, elf_functions_list) { + funcs = list_entry(pos, struct elf_functions, node); +elf_functions__delete (funcs); + } +} + +static struct elf_functions *elf_functions__find (const Elf *elf,const struct list_head *elf_functions_list) +{ +struct elf_functions *funcs; +struct list_head *pos; + +list_for_each (pos, elf_functions_list) { + funcs = list_entry(pos, struct elf_functions, node); +if (funcs->elf == elf) +return funcs; + } +return NULL ; +} + +static LIST_HEAD (encoders);static pthread_mutex_t encoders__lock =PTHREAD_MUTEX_INITIALIZER ;
@@ -1253,9 +1325,6 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder)free (pos->annots);free (pos); } - -for (int i =0 ; i < encoder->functions.cnt; i++) -free (encoder->functions.entries[i].alias); }int btf_encoder__add_saved_funcs (struct btf_encoder *encoder)
@@ -1341,12 +1410,30 @@ static void elf_functions__collect_function(struct elf_functions *functions, GEl functions->cnt++; } +static struct elf_functions *btf_encoder__elf_functions (struct btf_encoder *encoder) +{ +struct elf_functions *funcs =NULL ; + +if (!encoder->cu || !encoder->cu->elf) +return NULL ; + + funcs =elf_functions__find (encoder->cu->elf, &encoder->elf_functions_list); +if (!funcs) { + funcs =elf_functions__new (encoder->cu->elf); +if (funcs) +list_add (&funcs->node, &encoder->elf_functions_list); + } + +return funcs; +} +static struct elf_function *btf_encoder__find_function (const struct btf_encoder *encoder,const char *name,size_t prefixlen) { +struct elf_functions *funcs =elf_functions__find (encoder->cu->elf, &encoder->elf_functions_list);struct elf_function key = { .name = name, .prefixlen = prefixlen }; -return bsearch (&key, encoder->functions.entries, encoder->functions.cnt,sizeof (key), functions_cmp); +return bsearch (&key, funcs->entries, funcs->cnt,sizeof (key), functions_cmp); }static bool btf_name_char_ok (char c,bool first)
@@ -2100,6 +2187,8 @@ int btf_encoder__encode(struct btf_encoder *encoder)#endif err =btf_encoder__write_elf (encoder, encoder->btf,BTF_ELF_SEC ); } + +elf_functions_list__clear (&encoder->elf_functions_list);return err; }
@@ -2358,8 +2447,10 @@ out:struct btf_encoder *btf_encoder__new (struct cu *cu,const char *detached_filename,struct btf *base_btf,bool verbose,struct conf_load *conf_load) {struct btf_encoder *encoder =zalloc (sizeof (*encoder)); +struct elf_functions *funcs =NULL ;if (encoder) { + encoder->cu = cu; encoder->raw_output = detached_filename !=NULL ; encoder->source_filename =strdup (cu->filename); encoder->filename =strdup (encoder->raw_output ? detached_filename : cu->filename);
@@ -2387,6 +2478,13 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamif (conf_load->encode_btf_global_vars) encoder->encode_vars |=BTF_VAR_GLOBAL ; +INIT_LIST_HEAD (&encoder->elf_functions_list); + funcs =btf_encoder__elf_functions (encoder); +if (!funcs) + goto out_delete; + + encoder->symtab = funcs->symtab; +GElf_Ehdr ehdr;if (gelf_getehdr (cu->elf, &ehdr) ==NULL ) {
@@ -2407,14 +2505,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam goto out_delete; } - encoder->symtab =elf_symtab__new (NULL , cu->elf); -if (!encoder->symtab) { -if (encoder->verbose) -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 */ GElf_Shdr shdr;
@@ -2452,14 +2542,11 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamif (!found_percpu && encoder->verbose)printf ("%s: '%s' doesn't have '%s' section\n" , __func__, cu->filename,PERCPU_SECTION ); -if (elf_functions__collect (&encoder->functions)) - goto out_delete; -if (encoder->verbose)printf ("File %s:\n" , cu->filename);btf_encoders__add (encoder); } -out: +return encoder; out_delete:
@@ -2482,11 +2569,8 @@ void btf_encoder__delete(struct btf_encoder *encoder)zfree (&encoder->source_filename);btf__free (encoder->btf); encoder->btf =NULL ; -elf_symtab__delete (encoder->symtab); - encoder->functions.cnt =0 ; -free (encoder->functions.entries); - encoder->functions.entries =NULL ; +elf_functions_list__clear (&encoder->elf_functions_list);btf_encoder__delete_saved_funcs (encoder);
@@ -2497,12 +2581,20 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co {struct llvm_annotation *annot;int btf_type_id, tag_type_id, skipped_types =0 ; +struct elf_functions *funcs;uint32_t core_id;struct function *fn;struct tag *pos;int err =0 ; encoder->cu = cu; + funcs =btf_encoder__elf_functions (encoder); +if (!funcs) { + err =-1 ; + goto out; + } + encoder->symtab = funcs->symtab; + encoder->type_id_off =btf__type_cnt (encoder->btf) -1 ;if (!encoder->has_index_type) {
@@ -2586,7 +2678,7 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct cocontinue ;if (!ftype__has_arg_names (&fn->proto))continue ; -if (encoder->functions.cnt) { +if (funcs->cnt) {const char *name; name =function__name (fn);
@@ -2595,7 +2687,7 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co/* prefer exact function name match... */ func =btf_encoder__find_function (encoder, name,0 ); -if (!func && encoder->functions.suffix_cnt && +if (!func && funcs->suffix_cnt && conf_load->btf_gen_optimized) {/* falling back to name.isra.0 match if no exact * match is found; only bother if we found any
Signature · 1 line
2.47.1
PATCH v3 5/8 btf_encoder: remove skip_encoding_inconsistent_proto
In reply to Ihor SolodraiThis flag is needed only for btf_encoder__add_saved_funcs(), so there is no reason to keep it in each btf_encoder.
--- btf_encoder.c | 10 ++++------ btf_encoder.h | 4 ++-- pahole.c | 7 +++++-- 3 files changed, 11 insertions(+), 10 deletions(-)
btf_encoder.c +4 -6
@@ -128,7 +128,6 @@ struct btf_encoder { force, gen_floats, skip_encoding_decl_tag, - skip_encoding_inconsistent_proto, tag_kfuncs, gen_distilled_base;uint32_t array_index_id;
@@ -1327,7 +1326,7 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder) } } -int btf_encoder__add_saved_funcs (struct btf_encoder *encoder) +int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) {struct btf_encoder_func_state **saved_fns, *s;struct btf_encoder *e =NULL ;
@@ -1367,7 +1366,7 @@ int btf_encoder__add_saved_funcs(struct btf_encoder *encoder)* just do not _use_ them. Only exclude functions with * unexpected register use or multiple inconsistent prototypes. */ -if (!encoder->skip_encoding_inconsistent_proto || +if (!skip_encoding_inconsistent_proto || (!state->unexpected_reg && !state->inconsistent_proto)) {if (btf_encoder__add_func (state->encoder, state)) {free (saved_fns);
@@ -2129,14 +2128,14 @@ out:return err; } -int btf_encoder__encode (struct btf_encoder *encoder) +int btf_encoder__encode (struct btf_encoder *encoder,struct conf_load *conf) {bool should_tag_kfuncs;int err;size_t shndx;/* for single-threaded case, saved funcs are added here */ -btf_encoder__add_saved_funcs (encoder); +btf_encoder__add_saved_funcs (conf->skip_encoding_btf_inconsistent_proto);for (shndx =1 ; shndx < encoder->seccnt; shndx++)if (gobuffer__size (&encoder->secinfo[shndx].secinfo))
@@ -2464,7 +2463,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->force = conf_load->btf_encode_force; encoder->gen_floats = conf_load->btf_gen_floats; encoder->skip_encoding_decl_tag = conf_load->skip_encoding_btf_decl_tag; - encoder->skip_encoding_inconsistent_proto = conf_load->skip_encoding_btf_inconsistent_proto; encoder->tag_kfuncs = conf_load->btf_decl_tag_kfuncs; encoder->gen_distilled_base = conf_load->btf_gen_distilled_base; encoder->verbose = verbose;
btf_encoder.h +2 -2
@@ -26,13 +26,13 @@ enum btf_var_option {struct btf_encoder *btf_encoder__new (struct cu *cu,const char *detached_filename,struct btf *base_btf,bool verbose,struct conf_load *conf_load);void btf_encoder__delete (struct btf_encoder *encoder); -int btf_encoder__encode (struct btf_encoder *encoder); +int btf_encoder__encode (struct btf_encoder *encoder,struct conf_load *conf);int btf_encoder__encode_cu (struct btf_encoder *encoder,struct cu *cu,struct conf_load *conf_load);struct btf *btf_encoder__btf (struct btf_encoder *encoder);int btf_encoder__add_encoder (struct btf_encoder *encoder,struct btf_encoder *other); -int btf_encoder__add_saved_funcs (struct btf_encoder *encoder); +int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto);#endif /* _BTF_ENCODER_H_ */
pahole.c +5 -2
@@ -3185,7 +3185,10 @@ static int pahole_threads_collect(struct conf_load *conf, int nr_threads, void *if (error) goto out; -btf_encoder__add_saved_funcs (btf_encoder); + err =btf_encoder__add_saved_funcs (conf_load.skip_encoding_btf_inconsistent_proto); +if (err <0 ) + goto out; +for (i =0 ; i < nr_threads; i++) {/* * Merge content of the btf instances of worker threads to the btf
@@ -3843,7 +3846,7 @@ try_sole_arg_as_class_names:exit (1 ); } - err =btf_encoder__encode (btf_encoder); + err =btf_encoder__encode (btf_encoder, &conf_load);btf_encoder__delete (btf_encoder);if (err) {fputs ("Failed to encode BTF\n" , stderr);
Signature · 1 line
2.47.1
PATCH v3 6/8 dwarf_loader: introduce cu->id
In reply to Ihor SolodraiAdd an id member to the struct cu.
An id is an index of a CU, in order they are created in dwarf_loader.c This allows for an easy identification of a CU, particularly when they need to be processed in order.
--- dwarf_loader.c | 4 ++++ dwarves.h | 1 + 2 files changed, 5 insertions(+)
dwarf_loader.c +4 -0
@@ -3440,6 +3440,7 @@ struct dwarf_cus {int build_id_len;int error;struct dwarf_cu *type_dcu; +uint32_t nr_cus_created; };struct dwarf_thread {
@@ -3472,6 +3473,9 @@ static struct dwarf_cu *dwarf_cus__create_cu(struct dwarf_cus *dcus, Dwarf_Die * cu->priv = dcu; cu->dfops = &dwarf__ops; + cu->id = dcus->nr_cus_created; + dcus->nr_cus_created++; +return dcu; }
dwarves.h +1 -0
@@ -290,6 +290,7 @@ struct cu {struct ptr_table functions_table;struct ptr_table tags_table;struct rb_root functions; +uint32_t id;const char *name;char *filename;void *priv;
Signature · 1 line
2.47.1
PATCH v3 7/8 dwarf_loader: multithreading with a job/worker model
In reply to Ihor SolodraiMultithreading is now contained in dwarf_loader.c, and is implemented using a jobs queue and a pool of worker threads. As a consequence, multithreading-related code is removed from pahole.c.
A single-thread special case is removed: queueing setup works fine with a single worker, which will switch between jobs as appropriate.
Code supporting previous version of the multithreading, such as cu_state, thread_data and related functions, is also removed.
reproducible_build flag is now moot: the BTF encoding is always reproducible with these changes.
The goal outlined in the RFC [1] - making parallel reproducible BTF generation as fast as non-reproducible - is achieved by implementing the requirement of ordered CU encoding (stealing) directly in the job queue in dwarf_loader.c
The synchronization in the queue is implemented by a mutex (which ensures consistency of queue state) and a job_added condition variable. Motivation behind using condition variables is a classic
one: we want to avoid the threads checking the state of the queue in a busy loop, competing for a single mutex.
In comparison to the previous version of this patch [2], job_taken condition variable is removed. The number of decoded CUs in memory is now limited by initial JOB_DECODE jobs. The enqueue/dequeue interface is changed aiming to reduce locking. See relevant discussion [3].
[1] https://lore.kernel.org/dwarves/[email protected]/
[2] https://lore.kernel.org/dwarves/[email protected]/
[3] https://lore.kernel.org/dwarves/[email protected]/
--- btf_encoder.c | 3 +- btf_encoder.h | 2 - btf_loader.c | 2 +- ctf_loader.c | 2 +- dwarf_loader.c | 331 +++++++++++++++++++++++++----------- dwarves.c | 44 ----- dwarves.h | 20 +-- pahole.c | 231 +++---------------------- pdwtags.c | 3 +- pfunct.c | 3 +- tests/reproducible_build.sh | 5 +- 11 files changed, 266 insertions(+), 380 deletions(-)
btf_encoder.c +1 -2
@@ -1326,7 +1326,7 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder) } } -int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) {struct btf_encoder_func_state **saved_fns, *s;struct btf_encoder *e =NULL ;
@@ -2134,7 +2134,6 @@ int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf)int err;size_t shndx; -/* for single-threaded case, saved funcs are added here */ btf_encoder__add_saved_funcs (conf->skip_encoding_btf_inconsistent_proto);for (shndx =1 ; shndx < encoder->seccnt; shndx++)
btf_encoder.h +0 -2
@@ -33,6 +33,4 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct costruct btf *btf_encoder__btf (struct btf_encoder *encoder);int btf_encoder__add_encoder (struct btf_encoder *encoder,struct btf_encoder *other); -int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto); -#endif /* _BTF_ENCODER_H_ */
btf_loader.c +1 -1
@@ -730,7 +730,7 @@ static int cus__load_btf(struct cus *cus, struct conf_load *conf, const char *fi* The app stole this cu, possibly deleting it, * so forget about it */ -if (conf && conf->steal && conf->steal (cu, conf,NULL )) +if (conf && conf->steal && conf->steal (cu, conf))return 0 ;cus__add (cus, cu);
ctf_loader.c +1 -1
@@ -728,7 +728,7 @@ int ctf__load_file(struct cus *cus, struct conf_load *conf,* The app stole this cu, possibly deleting it, * so forget about it */ -if (conf && conf->steal && conf->steal (cu, conf,NULL )) +if (conf && conf->steal && conf->steal (cu, conf))return 0 ;cus__add (cus, cu);
dwarf_loader.c +230 -101
418 lines not on this page · raw message
dwarves.c +0 -44
56 lines not on this page · raw message
dwarves.h +2 -18
@@ -44,12 +44,6 @@ enum load_steal_kind { LSK__STOP_LOADING, }; -enum cu_state { -CU__UNPROCESSED , -CU__LOADED , -CU__PROCESSING , -}; -/* * BTF combines all the types into one big CU using btf_dedup(), so for something * like a allyesconfig vmlinux kernel we can get over 65535 types.
@@ -60,7 +54,6 @@ struct btf;struct conf_fprintf ;/** struct conf_load - load configuration -* @thread_exit - called at the end of a thread, 1st user: BTF encoder dedup * @extra_dbg_info - keep original debugging format extra info * (e.g. DWARF's decl_{line,file}, id, etc) * @fixup_silly_bitfields - Fixup silly things such as "int foo:32;"
@@ -70,11 +63,9 @@ struct conf_fprintf;* @skip_missing - skip missing types rather than bailing out. */ struct conf_load { -enum load_steal_kind (*steal)(struct cu *cu, -struct conf_load *conf, -void *thr_data); +enum load_steal_kind (*steal)(struct cu *cu,struct conf_load *conf);struct cu * (*early_cu_filter)(struct cu *cu); -int (*thread_exit)(struct conf_load *conf,void *thr_data); +int (*pre_load_module)(Dwfl_Module *mod,Elf *elf);void *cookie;char *format_path;int nr_jobs;
@@ -105,8 +96,6 @@ struct conf_load {const char *kabi_prefix;struct btf *base_btf;struct conf_fprintf *conf_fprintf; -int (*threads_prepare)(struct conf_load *conf,int nr_threads,void **thr_data); -int (*threads_collect)(struct conf_load *conf,int nr_threads,void **thr_data,int error); };/** struct conf_fprintf - hints to the __fprintf routines
@@ -188,10 +177,6 @@ void cus__add(struct cus *cus, struct cu *cu);void __cus__remove (struct cus *cus,struct cu *cu);void cus__remove (struct cus *cus,struct cu *cu); -struct cu *cus__get_next_processable_cu (struct cus *cus); - -void cus__set_cu_state (struct cus *cus,struct cu *cu,enum cu_state state); -void cus__print_error_msg (const char *progname,const struct cus *cus,const char *filename,const int err);struct cu *cus__find_pair (struct cus *cus,const char *name);
@@ -308,7 +293,6 @@ struct cu {uint8_t nr_register_params;int register_params[ARCH_MAX_REGISTER_PARAMS ];int functions_saved; -enum cu_state state;uint16_t language;unsigned long nr_inline_expansions;size_t size_inline_expansions;
pahole.c +28 -203
267 lines not on this page · raw message
pdwtags.c +1 -2
@@ -91,8 +91,7 @@ static int cu__emit_tags(struct cu *cu) }static enum load_steal_kind pdwtags_stealer (struct cu *cu, -struct conf_load *conf_load__maybe_unused , -void *thr_data__maybe_unused ) +struct conf_load *conf_load__maybe_unused ) {cu__emit_tags (cu);return LSK__DELETE ;
pfunct.c +1 -2
@@ -510,8 +510,7 @@ int elf_symtabs__show(char *filenames[]) }static enum load_steal_kind pfunct_stealer (struct cu *cu, -struct conf_load *conf_load__maybe_unused , -void *thr_data__maybe_unused ) +struct conf_load *conf_load__maybe_unused ) {if (function_name) {
tests/reproducible_build.sh +1 -4
@@ -37,10 +37,7 @@ for threads in $(seq $nr_proc) ; do sleep 0.3s # PID part to remove ps output headers nr_threads_started=$(ps -L -C pahole | grep -v PID | wc -l) - - if [ $threads -gt 1 ] ; then - ((nr_threads_started -= 1)) - fi + ((nr_threads_started -= 1)) # main thread doesn't count, it waits to join if [ $threads != $nr_threads_started ] ; then echo "ERROR: pahole asked to start $threads encoding threads, started $nr_threads_started"
Signature · 1 line
2.47.1
Re: [PATCH dwarves v3 7/8] dwarf_loader: multithreading with a job/worker model
Quoted 24 lines · Ihor Solodrai
Multithreading is now contained in
dwarf_loader.c, and is implemented using a jobs queue and a pool of worker threads. As a consequence, multithreading-related code is removed frompahole.c.A single-thread special case is removed: queueing setup works fine with a single worker, which will switch between jobs as appropriate.
Code supporting previous version of the multithreading, such as
cu_state,thread_dataand related functions, is also removed.
reproducible_buildflag is now moot: the BTF encoding is always reproducible with these changes.The goal outlined in the RFC [1] - making parallel reproducible BTF generation as fast as non-reproducible - is achieved by implementing the requirement of ordered CU encoding (stealing) directly in the job queue in
dwarf_loader.cThe synchronization in the queue is implemented by a mutex (which ensures consistency of queue state) and a
job_addedcondition variable. Motivation behind using condition variables is a classic
one: we want to avoid the threads checking the state of the queue in a busy loop, competing for a single mutex.
Quoted 4 lines
In comparison to the previous version of this patch [2],
job_takencondition variable is removed. The number of decoded CUs in memory is now limited by initialJOB_DECODEjobs. The enqueue/dequeue interface is changed aiming to reduce locking. See relevant discussion [3].
So I'm running tests/tests after each of these patches and at this point I got:
root@number:/home/acme/git/pahole# tests/tests 1: Validation of BTF encoding of functions; this may take some time: grep: /tmp/btf_functions.sh.OgxoO4/dwarf.funcs: binary file matches ERROR: mismatch : BTF 'bool evtchn_fifo_is_pending(evtchn_port_t);' not found; DWARF '' Test data is in /tmp/btf_functions.sh.OgxoO4 2: Default BTF on a system without BTF: Ok 3: Flexible arrays accounting: Ok 4: Check that pfunct can print btf_decl_tags read from BTF: Ok 5: Pretty printing of files using DWARF type information: Ok 6: Parallel reproducible DWARF Loading/Serial BTF encoding: Ok /home/acme/git/pahole root@number:/home/acme/git/pahole#
root@number:/home/acme/git/pahole# grep evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/dwarf.funcs bool evtchn_fifo_is_pending(evtchn_port_t); root@number:/home/acme/git/pahole# grep evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/btf.funcs bool evtchn_fifo_is_pending(evtchn_port_t); root@number:/home/acme/git/pahole#
Which seems like a test artifact:
root@number:/home/acme/git/pahole# pfunct -f evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/vmlinux.btf bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole# pfunct -F btf -f evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/vmlinux.btf bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#
root@number:/home/acme/git/pahole# pfunct -F btf -f evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole# pfunct -F dwarf evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux bool evtchn_fifo_is_pending(evtchn_port_t port); bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#
Maybe because DWARF has two such functions? Alan?
But:
root@number:/home/acme/git/pahole# readelf -wi /lib/modules/6.13.0-rc2/build/vmlinux | grep -B2 -A12 evtchn_fifo_is_pending
<2><946502d>: Abbrev Number: 0
<1><946502e>: Abbrev Number: 128 (DW_TAG_subprogram)
<9465030> DW_AT_name : (indirect string, offset: 0x39779e): evtchn_fifo_is_pending
<9465034> DW_AT_decl_file : 1
<9465034> DW_AT_decl_line : 206
<9465035> DW_AT_decl_column : 13
<9465036> DW_AT_prototyped : 1
<9465036> DW_AT_type : <0x9456abf>
<946503a> DW_AT_low_pc : 0xffffffff81b6d330
<9465042> DW_AT_high_pc : 0x65
<946504a> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)
<946504c> DW_AT_call_all_calls: 1
<946504c> DW_AT_sibling : <0x946517a>
<2><9465050>: Abbrev Number: 81 (DW_TAG_formal_parameter)
<9465051> DW_AT_name : (indirect string, offset: 0x3abde1): port
root@number:/home/acme/git/pahole#I.e. there is just one, but when pfunct prints it must be seeing some other usage that refers back to 946502e...
root@number:/home/acme/git/pahole# readelf -wi /lib/modules/6.13.0-rc2/build/vmlinux | grep -w -B2 -A12 0x946502e
<4><9464825>: Abbrev Number: 60 (DW_TAG_call_site)
<9464826> DW_AT_call_return_pc: 0xffffffff81b6d4f5
<946482e> DW_AT_call_origin : <0x946502e>
<9464832> DW_AT_sibling : <0x946483d>
<5><9464836>: Abbrev Number: 13 (DW_TAG_call_site_parameter)
<9464837> DW_AT_location : 1 byte block: 55 (DW_OP_reg5 (rdi))
<9464839> DW_AT_call_value : 2 byte block: 7f 0 (DW_OP_breg15 (r15): 0)
<5><946483c>: Abbrev Number: 0
<4><946483d>: Abbrev Number: 60 (DW_TAG_call_site)
<946483e> DW_AT_call_return_pc: 0xffffffff81b6d590
<9464846> DW_AT_call_origin : <0x9463caa>
<946484a> DW_AT_sibling : <0x946485d>
<5><946484e>: Abbrev Number: 13 (DW_TAG_call_site_parameter)
<946484f> DW_AT_location : 1 byte block: 55 (DW_OP_reg5 (rdi))
<9464851> DW_AT_call_value : 2 byte block: 7f 0 (DW_OP_breg15 (r15): 0)
--
<9464f6b> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa)
<9464f6d> DW_AT_call_all_calls: 1
<9464f6d> DW_AT_sibling : <0x946502e>
<2><9464f71>: Abbrev Number: 81 (DW_TAG_formal_parameter)
<9464f72> DW_AT_name : (indirect string, offset: 0x3abde1): port
<9464f76> DW_AT_decl_file : 1
<9464f76> DW_AT_decl_line : 212
<9464f77> DW_AT_decl_column : 44
<9464f78> DW_AT_type : <0x9463263>
<9464f7c> DW_AT_location : 0x181ea81 (location list)
<9464f80> DW_AT_GNU_locviews: 0x181ea79
<2><9464f84>: Abbrev Number: 64 (DW_TAG_variable)
<9464f85> DW_AT_name : (indirect string, offset: 0x9f2cd): word
<9464f89> DW_AT_decl_file : 1
<9464f89> DW_AT_decl_line : 214
root@number:/home/acme/git/pahole#Not really :-\
root@number:/home/acme/git/pahole# pfunct --decl_info -F dwarf evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */ /* <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */ /* <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#
So far I couldn't find an explanation for this oddity... Lets see if after applying all patches we get past this.
- Arnaldo
Re: [PATCH dwarves v3 7/8] dwarf_loader: multithreading with a job/worker model
Quoted 136 lines · Arnaldo Carvalho de Melo
Quoted 24 lines · Ihor Solodrai
Multithreading is now contained in
dwarf_loader.c, and is implemented using a jobs queue and a pool of worker threads. As a consequence, multithreading-related code is removed frompahole.c.A single-thread special case is removed: queueing setup works fine with a single worker, which will switch between jobs as appropriate.
Code supporting previous version of the multithreading, such as
cu_state,thread_dataand related functions, is also removed.
reproducible_buildflag is now moot: the BTF encoding is always reproducible with these changes.The goal outlined in the RFC [1] - making parallel reproducible BTF generation as fast as non-reproducible - is achieved by implementing the requirement of ordered CU encoding (stealing) directly in the job queue in
dwarf_loader.cThe synchronization in the queue is implemented by a mutex (which ensures consistency of queue state) and a
job_addedcondition variable. Motivation behind using condition variables is a classic
one: we want to avoid the threads checking the state of the queue in a busy loop, competing for a single mutex.Quoted 4 lines
In comparison to the previous version of this patch [2],
job_takencondition variable is removed. The number of decoded CUs in memory is now limited by initialJOB_DECODEjobs. The enqueue/dequeue interface is changed aiming to reduce locking. See relevant discussion [3].So I'm running tests/tests after each of these patches and at this point I got:
root@number:/home/acme/git/pahole# tests/tests 1: Validation of BTF encoding of functions; this may take some time: grep: /tmp/btf_functions.sh.OgxoO4/dwarf.funcs: binary file matches ERROR: mismatch : BTF 'bool evtchn_fifo_is_pending(evtchn_port_t);' not found; DWARF '' Test data is in /tmp/btf_functions.sh.OgxoO4 2: Default BTF on a system without BTF: Ok 3: Flexible arrays accounting: Ok 4: Check that pfunct can print btf_decl_tags read from BTF: Ok 5: Pretty printing of files using DWARF type information: Ok 6: Parallel reproducible DWARF Loading/Serial BTF encoding: Ok /home/acme/git/pahole root@number:/home/acme/git/pahole#root@number:/home/acme/git/pahole# grep evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/dwarf.funcs bool evtchn_fifo_is_pending(evtchn_port_t); root@number:/home/acme/git/pahole# grep evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/btf.funcs bool evtchn_fifo_is_pending(evtchn_port_t); root@number:/home/acme/git/pahole#Which seems like a test artifact:
root@number:/home/acme/git/pahole# pfunct -f evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/vmlinux.btf bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole# pfunct -F btf -f evtchn_fifo_is_pending /tmp/btf_functions.sh.OgxoO4/vmlinux.btf bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#root@number:/home/acme/git/pahole# pfunct -F btf -f evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole# pfunct -F dwarf evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux bool evtchn_fifo_is_pending(evtchn_port_t port); bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#Maybe because DWARF has two such functions? Alan?
But:
root@number:/home/acme/git/pahole# readelf -wi /lib/modules/6.13.0-rc2/build/vmlinux | grep -B2 -A12 evtchn_fifo_is_pending <2><946502d>: Abbrev Number: 0 <1><946502e>: Abbrev Number: 128 (DW_TAG_subprogram) <9465030> DW_AT_name : (indirect string, offset: 0x39779e): evtchn_fifo_is_pending <9465034> DW_AT_decl_file : 1 <9465034> DW_AT_decl_line : 206 <9465035> DW_AT_decl_column : 13 <9465036> DW_AT_prototyped : 1 <9465036> DW_AT_type : <0x9456abf> <946503a> DW_AT_low_pc : 0xffffffff81b6d330 <9465042> DW_AT_high_pc : 0x65 <946504a> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa) <946504c> DW_AT_call_all_calls: 1 <946504c> DW_AT_sibling : <0x946517a> <2><9465050>: Abbrev Number: 81 (DW_TAG_formal_parameter) <9465051> DW_AT_name : (indirect string, offset: 0x3abde1): port root@number:/home/acme/git/pahole#I.e. there is just one, but when pfunct prints it must be seeing some other usage that refers back to 946502e...
root@number:/home/acme/git/pahole# readelf -wi /lib/modules/6.13.0-rc2/build/vmlinux | grep -w -B2 -A12 0x946502e <4><9464825>: Abbrev Number: 60 (DW_TAG_call_site) <9464826> DW_AT_call_return_pc: 0xffffffff81b6d4f5 <946482e> DW_AT_call_origin : <0x946502e> <9464832> DW_AT_sibling : <0x946483d> <5><9464836>: Abbrev Number: 13 (DW_TAG_call_site_parameter) <9464837> DW_AT_location : 1 byte block: 55 (DW_OP_reg5 (rdi)) <9464839> DW_AT_call_value : 2 byte block: 7f 0 (DW_OP_breg15 (r15): 0) <5><946483c>: Abbrev Number: 0 <4><946483d>: Abbrev Number: 60 (DW_TAG_call_site) <946483e> DW_AT_call_return_pc: 0xffffffff81b6d590 <9464846> DW_AT_call_origin : <0x9463caa> <946484a> DW_AT_sibling : <0x946485d> <5><946484e>: Abbrev Number: 13 (DW_TAG_call_site_parameter) <946484f> DW_AT_location : 1 byte block: 55 (DW_OP_reg5 (rdi)) <9464851> DW_AT_call_value : 2 byte block: 7f 0 (DW_OP_breg15 (r15): 0) -- <9464f6b> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa) <9464f6d> DW_AT_call_all_calls: 1 <9464f6d> DW_AT_sibling : <0x946502e> <2><9464f71>: Abbrev Number: 81 (DW_TAG_formal_parameter) <9464f72> DW_AT_name : (indirect string, offset: 0x3abde1): port <9464f76> DW_AT_decl_file : 1 <9464f76> DW_AT_decl_line : 212 <9464f77> DW_AT_decl_column : 44 <9464f78> DW_AT_type : <0x9463263> <9464f7c> DW_AT_location : 0x181ea81 (location list) <9464f80> DW_AT_GNU_locviews: 0x181ea79 <2><9464f84>: Abbrev Number: 64 (DW_TAG_variable) <9464f85> DW_AT_name : (indirect string, offset: 0x9f2cd): word <9464f89> DW_AT_decl_file : 1 <9464f89> DW_AT_decl_line : 214 root@number:/home/acme/git/pahole#Not really :-\
root@number:/home/acme/git/pahole# pfunct --decl_info -F dwarf evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */ /* <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */ /* <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#
Quoted 2 lines
So far I couldn't find an explanation for this oddity... Lets see if after applying all patches we get past this.
Its not related to this patch series, before we get two outputs for these (and other functions in /home/acme/git/linux/drivers/xen/events/events_fifo.c).
Still investigating.
- Arnaldo
Re: [PATCH dwarves v3 7/8] dwarf_loader: multithreading with a job/worker model
Quoted 11 lines · Arnaldo Carvalho de Melo
Quoted 11 lines
Not really :-\
root@number:/home/acme/git/pahole# pfunct --decl_info -F dwarf evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */ /* <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */ /* <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#
Quoted 8 lines
Quoted 2 lines
So far I couldn't find an explanation for this oddity... Lets see if after applying all patches we get past this.
Its not related to this patch series, before we get two outputs for these (and other functions in
/home/acme/git/linux/drivers/xen/events/events_fifo.c).Still investigating.
root@number:/home/acme/git/pahole# perf probe -x ~/bin/pfunct function__show Added new event: probe_pfunct:function_show (on function__show in /home/acme/git/pahole/build/pfunct)
You can now use it in all perf tools, such as:
perf record -e probe_pfunct:function_show -aR sleep 1
root@number:/home/acme/git/pahole# perf trace -e probe_pfunct:function_show --call-graph dwarf pfunct --decl_info -F dwarf evtchn_fifo_set_pending /lib/modules/6.13.0-rc2/build/vmlinux
/* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */
/* <946517a> /home/acme/git/linux/drivers/xen/events/events_fifo.c:200 */
void evtchn_fifo_set_pending(evtchn_port_t port);
/* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c */
/* <946517a> /home/acme/git/linux/drivers/xen/events/events_fifo.c:200 */
void evtchn_fifo_set_pending(evtchn_port_t port);
0.000 pfunct/2006089 probe_pfunct:function_show(__probe_ip: 4208235)
function__show (/home/acme/git/pahole/build/pfunct)
pfunct_stealer (/home/acme/git/pahole/build/pfunct)
cus__steal_now (/home/acme/git/pahole/build/libdwarves.so.1.0.0)
dwarf_loader__worker_thread (/home/acme/git/pahole/build/libdwarves.so.1.0.0)
start_thread (/usr/lib64/libc.so.6)
clone3 (/usr/lib64/libc.so.6)
0.134 pfunct/2006088 probe_pfunct:function_show(__probe_ip: 4208235)
function__show (/home/acme/git/pahole/build/pfunct)
cu_function_iterator (/home/acme/git/pahole/build/pfunct)
cus__for_each_cu (/home/acme/git/pahole/build/libdwarves.so.1.0.0)
main (/home/acme/git/pahole/build/pfunct)
__libc_start_call_main (/usr/lib64/libc.so.6)
__libc_start_main@@GLIBC_2.34 (/usr/lib64/libc.so.6)
_start (/home/acme/git/pahole/build/pfunct)
root@number:/home/acme/git/pahole#With the following patch we get just one output for this case, but that isn't the right solution... I'll look on removing the cu_function_iterator() based printing, otherwise when printing all matches we'll still duplicate the printings.
Anyway, doesn't seem related to the problem that tests/tests was catching, that I'm not being able to reproduce anymore after having the whole series applied, probably some race?
- Arnaldo
pfunct.c +7 -1
@@ -518,7 +518,13 @@ static enum load_steal_kind pfunct_stealer(struct cu *cu,if (tag) {function__show (tag__function (tag), cu); -return show_all_matches ?LSK__DELETE :LSK__STOP_LOADING ; +if (!show_all_matches) { +// Expedite exit, since we already did what was requested: +// print the first occurrence of a given function +exit (0 ); + } + +return LSK__DELETE ; } }else if (class_name) {cu_class_iterator (cu, class_name);
Re: [PATCH dwarves v3 7/8] dwarf_loader: multithreading with a job/worker model
Quoted 59 lines · Arnaldo Carvalho de Melo
Quoted 11 lines · Arnaldo Carvalho de Melo
Quoted 11 lines
Not really :-\
root@number:/home/acme/git/pahole# pfunct --decl_info -F dwarf evtchn_fifo_is_pending /lib/modules/6.13.0-rc2/build/vmlinux /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c / / <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 / bool evtchn_fifo_is_pending(evtchn_port_t port); / Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c / / <946502e> /home/acme/git/linux/drivers/xen/events/events_fifo.c:206 */ bool evtchn_fifo_is_pending(evtchn_port_t port); root@number:/home/acme/git/pahole#Quoted 8 lines
Quoted 2 lines
So far I couldn't find an explanation for this oddity... Lets see if after applying all patches we get past this.
Its not related to this patch series, before we get two outputs for these (and other functions in
/home/acme/git/linux/drivers/xen/events/events_fifo.c).Still investigating.
root@number:/home/acme/git/pahole# perf probe -x ~/bin/pfunct function__show Added new event: probe_pfunct:function_show (on function__show in /home/acme/git/pahole/build/pfunct)You can now use it in all perf tools, such as:
perf record -e probe_pfunct:function_show -aR sleep 1
root@number:/home/acme/git/pahole# perf trace -e probe_pfunct:function_show --call-graph dwarf pfunct --decl_info -F dwarf evtchn_fifo_set_pending /lib/modules/6.13.0-rc2/build/vmlinux /* Used at: /home/acme/git/linux/drivers/xen/events/events_fifo.c / / <946517a> /home/acme/git/linux/drivers/xen/events/events_fifo.c:200 */void
evtchn_fifo_set_pending(evtchn_port_t port);
/* Used at:/home/acme/git/linux/drivers/xen/events/events_fifo.c/ / <946517a>/home/acme/git/linux/drivers/xen/events/events_fifo.c:200*/void
evtchn_fifo_set_pending(evtchn_port_t port);
0.000 pfunct/2006089 probe_pfunct:function_show(__probe_ip: 4208235)function__show(/home/acme/git/pahole/build/pfunct)pfunct_stealer(/home/acme/git/pahole/build/pfunct)cus__steal_now(/home/acme/git/pahole/build/libdwarves.so.1.0.0)dwarf_loader__worker_thread(/home/acme/git/pahole/build/libdwarves.so.1.0.0)start_thread(/usr/lib64/libc.so.6)
clone3 (/usr/lib64/libc.so.6)
0.134 pfunct/2006088 probe_pfunct:function_show(__probe_ip: 4208235)function__show(/home/acme/git/pahole/build/pfunct)cu_function_iterator(/home/acme/git/pahole/build/pfunct)cus__for_each_cu(/home/acme/git/pahole/build/libdwarves.so.1.0.0) main (/home/acme/git/pahole/build/pfunct)__libc_start_call_main(/usr/lib64/libc.so.6) __libc_start_main@@GLIBC_2.34 (/usr/lib64/libc.so.6)_start(/home/acme/git/pahole/build/pfunct) root@number:/home/acme/git/pahole#
Quoted 8 lines
With the following patch we get just one output for this case, but that isn't the right solution... I'll look on removing the
cu_function_iterator()based printing, otherwise when printing all matches we'll still duplicate the printings.Anyway, doesn't seem related to the problem that tests/tests was catching, that I'm not being able to reproduce anymore after having the whole series applied, probably some race?
Hi Arnaldo, thank you for testing.
I couldn't reproduce the mismatch error that you saw:
root@number:/home/acme/git/pahole# tests/tests
1: Validation of BTF encoding of functions; this may take some time: grep: /tmp/btf_functions.sh.OgxoO4/dwarf.funcs: binary file matches
ERROR: mismatch : BTF 'bool evtchn_fifo_is_pending(evtchn_port_t);' not found; DWARF ''
Test data is in /tmp/btf_functions.sh.OgxoO4I've tried a couple of kernels:
* 6.12 with selftests/bpf config
* Fedora 6.12.6-100.fc40.x86_64 (my workstation)
* 6.13-rc2 with Fedora-like config
I saw warnings that don't seem to be related to this series:
theihor@qube:~/dev/dwarves$ PATH=$(realpath build):$PATH vmlinux=~/git/kernel.org/linux-for-pahole/vmlinux ./tests/tests
1: Validation of BTF encoding of functions; this may take some time: Ok
2: Default BTF on a system without BTF: Ok
3: Flexible arrays accounting: WARNING: still unsuported BTF_KIND_DECL_TAG(bpf_fastcall) for bpf_cast_to_kern_ctx already with attribute (bpf_kfunc), ignoring
WARNING: still unsuported BTF_KIND_DECL_TAG(bpf_fastcall) for bpf_rdonly_cast already with attribute (bpf_kfunc), ignoring
Ok
4: Pretty printing of files using DWARF type information: Ok
5: Parallel reproducible DWARF Loading/Serial BTF encoding: OkAs for potential race, since btf_encoder is sequential and a unit of work is a CU, I don't see how a single function could have been missed. Other declarations in events_fifo.c would've been affected I think.
If you see this again, please let me know.
Quoted 22 lines
- Arnaldo
pfunct.c +7
-1@@ -518,7 +518,13 @@ static enum load_steal_kind pfunct_stealer(struct cu *cu,if (tag) {function__show (tag__function (tag), cu); -return show_all_matches ?LSK__DELETE :LSK__STOP_LOADING ; +if (!show_all_matches) { +// Expedite exit, since we already did what was requested: +// print the first occurrence of a given function +exit (0 ); + } + +return LSK__DELETE ; } }else if (class_name) {cu_class_iterator (cu, class_name);
Re: [PATCH dwarves v3 7/8] dwarf_loader: multithreading with a job/worker model
In reply to Ihor SolodraiOn Sat, Dec 21, 2024 at 01:23:38AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 14 lines
btf_encoder.c +1
-1@@ -1326,7 +1326,7 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder) } } -int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) {struct btf_encoder_func_state **saved_fns, *s;struct btf_encoder *e =NULL ;
btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf) · line 2137
Quoted 5 lines
btf_encoder.c +0
-1@@ -2134,7 +2134,6 @@ int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf)int err;size_t shndx; -/* for single-threaded case, saved funcs are added here */ btf_encoder__add_saved_funcs (conf->skip_encoding_btf_inconsistent_proto);
should we check the return value in here? now it's the only caller
SNIP
Quoted 17 lines
btf_encoder.c +14
-3-struct dwarf_thread { -struct dwarf_cus *dcus; -void *data; +/* Multithreading is implemented using a job/worker model. +* cus_processing_queue represents a collection of jobs to be +* completed by workers. +* dwarf_loader__worker_thread is the worker loop, taking jobs from +* the queue, executing them and re-enqueuing new jobs as necessary. +* Implementation of this queue ensures two constraints: +* * JOB_STEAL jobs for a CU are executed in the order of cu->id, as +* a consequence JOB_STEAL jobs always run one at a time. +* * Initial number of JOB_DECODE jobs in the queue is effectively a +* limit on how many decoded CUs can be held in memory. +* See dwarf_loader__decoded_cus_limit() +*/ +static struct { + pthread_mutex_t mutex;
btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf)
Quoted 10 lines
btf_encoder.c +10
-0+pthread_cond_t job_added; +/* next_cu_id determines the next CU ready to be stealed +* This enforces the order of CU stealing. +*/ +uint32_t next_cu_id; +struct list_head jobs; +} cus_processing_queue; + +enum job_type { +JOB_NONE =0 ,
nit, no need for JOB_NONE?
SNIP
Quoted 13 lines
btf_encoder.c +13
-0+static struct cu_processing_job *cus_queue__try_dequeue (void ) +{ +struct cu_processing_job *job, *dequeued_job =NULL ; +struct list_head *pos, *tmp; + +list_for_each_safe (pos, tmp, &cus_processing_queue.jobs) { + job = list_entry(pos, struct cu_processing_job, node); +if (job->type ==JOB_STEAL && job->cu->id == cus_processing_queue.next_cu_id) { + dequeued_job = job; +break ; + } +if (job->type ==JOB_DECODE ) { +/* all JOB_STEALs are added to the head, so no viable JOB_STEAL available */
btf_encoder.c
Quoted 10 lines
btf_encoder.c +10
-0+ dequeued_job = job; +break ; + } + } +/* No jobs or only steals out of order */ +if (!dequeued_job) +return NULL ; + +list_del (&dequeued_job->node); +return job;
IIUC job == dequeued_job at this point, but I think we should return dequeued_job to be clear
SNIP
Quoted 15 lines
btf_encoder.c +5
-6-static void *dwarf_cus__process_cu_thread (void *arg) +static struct cu *dwarf_loader__decode_next_cu (struct dwarf_cus *dcus) { -struct dwarf_thread *dthr = arg; -struct dwarf_cus *dcus = dthr->dcus;uint8_t pointer_size, offset_size; +struct dwarf_cu *dcu =NULL ;Dwarf_Die die_mem, *cu_die; -struct dwarf_cu *dcu; +int err; -while (dwarf_cus__nextcu (dcus, &dcu, &die_mem, &cu_die, &pointer_size, &offset_size) ==0 ) { -if (cu_die ==NULL ) + err =dwarf_cus__nextcu (dcus, &dcu, &die_mem, &cu_die, &pointer_size, &offset_size); +
btf_encoder.c
Quoted 10 lines
btf_encoder.c +10
-0+if (err <0 ) + goto out_error; +else if (err ==1 )/* no more CUs */ +return NULL ; + + err =die__process_and_recode (cu_die, dcu->cu, dcus->conf); +if (err) + goto out_error; +if (cu_die ==NULL ) +return NULL ;
should this check be placed before calling die__process_and_recode ?
SNIP
Quoted 32 lines
btf_encoder.c +4
-10-static int dwarf_cus__process_cus (struct dwarf_cus *dcus) -{ -if (dcus->conf->nr_jobs >1 ) -return dwarf_cus__threaded_process_cus (dcus); - -return __dwarf_cus__process_cus (dcus); -} - static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf, Dwfl_Module *mod, Dwarf *dw, Elf *elf, const char *filename,@@ -3733,7 +3859,8 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,if (cu__resolve_func_ret_types_optimized (cu) !=LSK__KEEPIT ) goto out_abort; -if (cus__finalize (cus, cu, conf,NULL ) ==LSK__STOP_LOADING ) +cu__finalize (cu, cus, conf); +if (cus__steal_now (cus, cu, conf) ==LSK__STOP_LOADING ) goto out_abort;return 0 ;@@ -3765,7 +3892,8 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, }if (type_cu !=NULL ) { - type_lsk =cu__finalize (type_cu, cus, conf,NULL ); +cu__finalize (type_cu, cus, conf); + type_lsk =cus__steal_now (cus, type_cu, conf);if (type_lsk ==LSK__DELETE ) {cus__remove (cus, type_cu); }
btf_encoder.c static int cus__load_module(struct cus *cus, struct conf_load *conf, · line 3918
Quoted 4 lines
btf_encoder.c +1
-0@@ -3787,6 +3915,7 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, .type_dcu = type_cu ? &type_dcu : NULL, .build_id = build_id, .build_id_len = build_id_len, + .nr_cus_created = 0,
should go to the previous patch? if needed at all..
thanks,
jirka
Quoted 11 lines
btf_encoder.c +0
-0}; res =dwarf_cus__process_cus (&dcus); }dwarves.c +0
-0@@ -530,48 +530,6 @@ void cus__unlock(struct cus *cus)pthread_mutex_unlock (&cus->mutex); }
SNIP
Re: [PATCH dwarves v3 7/8] dwarf_loader: multithreading with a job/worker model
Quoted 23 lines · Jiri Olsa
On Sat, Dec 21, 2024 at 01:23:38AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 17 lines
btf_encoder.c +1
-1@@ -1326,7 +1326,7 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder) } } -int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) {struct btf_encoder_func_state **saved_fns, *s;struct btf_encoder *e =NULL ;@@ -2134,7 +2134,6 @@ int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf)int err;size_t shndx;
Quoted 5 lines
btf_encoder.c int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf) · line 2137
Quoted 2 lines
btf_encoder.c +0
-1-/* for single-threaded case, saved funcs are added here */ btf_encoder__add_saved_funcs (conf->skip_encoding_btf_inconsistent_proto);should we check the return value in here? now it's the only caller
Yes, thanks.
Quoted 25 lines
SNIP
Quoted 22 lines
btf_encoder.c +19
-3-struct dwarf_thread { -struct dwarf_cus *dcus; -void data; +/ Multithreading is implemented using a job/worker model. +* cus_processing_queue represents a collection of jobs to be +* completed by workers. +* dwarf_loader__worker_thread is the worker loop, taking jobs from +* the queue, executing them and re-enqueuing new jobs as necessary. +* Implementation of this queue ensures two constraints: +* * JOB_STEAL jobs for a CU are executed in the order of cu->id, as +* a consequence JOB_STEAL jobs always run one at a time. +* * Initial number of JOB_DECODE jobs in the queue is effectively a +* limit on how many decoded CUs can be held in memory. +* See dwarf_loader__decoded_cus_limit() +/ +static struct { +pthread_mutex_t mutex; +pthread_cond_t job_added; +/ next_cu_id determines the next CU ready to be stealed +* This enforces the order of CU stealing. +*/ +uint32_t next_cu_id;
Quoted 8 lines
btf_encoder.c
Quoted 5 lines
+ struct
list_headjobs;btf_encoder.c +4
-0+} cus_processing_queue; + +enum job_type { +JOB_NONE =0 ,nit, no need for
JOB_NONE?
I find it useful for the default value to not be a valid "type". This helps to avoid bugs when an object is initialized, but the type has not been set explicitly (even though it should be).
Quoted 22 lines
SNIP
Quoted 19 lines
btf_encoder.c +19
-0+static struct cu_processing_job *cus_queue__try_dequeue(void) +{ +struct cu_processing_job *job, *dequeued_job = NULL; +struct list_head *pos, tmp; + +list_for_each_safe(pos, tmp, &cus_processing_queue.jobs) { +job = list_entry(pos, struct cu_processing_job, node); +if (job->type == JOB_STEAL && job->cu->id == cus_processing_queue.next_cu_id) { +dequeued_job = job; +break; +} +if (job->type == JOB_DECODE) { +/ all JOB_STEALs are added to the head, so no viable JOB_STEAL available / +dequeued_job = job; +break; +} +} +/ No jobs or only steals out of order */ +if (!dequeued_job)
Quoted 8 lines
Quoted 4 lines
+ return NULL;
+
+list_del(&dequeued_job->node);
+ return job;IIUC job ==
dequeued_jobat this point, but I think we should returndequeued_jobto be clear
Agree.
Quoted 23 lines
SNIP
Quoted 20 lines
btf_encoder.c +1
-1-static void *dwarf_cus__process_cu_thread (void *arg) +static struct cu *dwarf_loader__decode_next_cu (struct dwarf_cus *dcus){
- structdwarf_thread*dthr = arg;
- structdwarf_cus*dcus =dthr->dcus;uint8_tpointer_size,offset_size;
+ structdwarf_cu*dcu = NULL;Dwarf_Diedie_mem,*cu_die;
- structdwarf_cu*dcu;
+ int err;- while (
dwarf_cus__nextcu(dcus, &dcu, &die_mem, &cu_die, &pointer_size, &offset_size)== 0) {
- if (cu_die== NULL)
+ err =dwarf_cus__nextcu(dcus, &dcu, &die_mem, &cu_die, &pointer_size, &offset_size);btf_encoder.c +6
-0+ +if (err <0 ) + goto out_error; +else if (err ==1 )/* no more CUs */ +return NULL ; +
Quoted 8 lines
Quoted 5 lines
+ err =
die__process_and_recode(cu_die, dcu->cu, dcus->conf);
+ if (err)
+ gotoout_error;
+ if (cu_die== NULL)
+ return NULL;should this check be placed before calling
die__process_and_recode?
Yes, but actually this check is redundant. If dwarf_cus__nextcu returns 0, then the cu_die is valid. There are null checks within dwarf_cus__nextcu:
if (ret == 0 && *cu_die != NULL) {
*dcu = dwarf_cus__create_cu(dcus, *cu_die, *pointer_size);
if (*dcu == NULL) {
dcus->error = ENOMEM;
ret = -1;
goto out_unlock;
}
// Do it here to keep all CUs in cus->cus in the same
// order as in the DWARF file being loaded (e.g. vmlinux)
__cus__add(dcus->cus, (*dcu)->cu);
}Quoted 28 lines
SNIP
Quoted 24 lines
btf_encoder.c +0
-8-static int dwarf_cus__process_cus (struct dwarf_cus *dcus) -{ -if (dcus->conf->nr_jobs >1 ) -return dwarf_cus__threaded_process_cus (dcus); - -return __dwarf_cus__process_cus (dcus); -} -static int cus__merge_and_process_cu(struct cus *cus, struct
conf_load*conf,Dwfl_Module*mod, Dwarf *dw, Elf *elf,
const char *filename,btf_encoder.c +2
-1@@ -3733,7 +3859,8 @@ static int cus__merge_and_process_cu(struct cus *cus, struct conf_load *conf,if (cu__resolve_func_ret_types_optimized (cu) !=LSK__KEEPIT ) goto out_abort; -if (cus__finalize (cus, cu, conf,NULL ) ==LSK__STOP_LOADING ) +cu__finalize (cu, cus, conf); +if (cus__steal_now (cus, cu, conf) ==LSK__STOP_LOADING ) goto out_abort;return 0 ;@@ -3765,7 +3892,8 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, }
Quoted 15 lines
btf_encoder.c static int cus__load_module(struct cus *cus, struct conf_load *conf, · line 3918
Quoted 12 lines
btf_encoder.c +3
-1if (type_cu !=NULL ) { - type_lsk =cu__finalize (type_cu, cus, conf,NULL ); +cu__finalize (type_cu, cus, conf); + type_lsk =cus__steal_now (cus, type_cu, conf);if (type_lsk ==LSK__DELETE ) {cus__remove (cus, type_cu); }@@ -3787,6 +3915,7 @@ static int cus__load_module(struct cus *cus, struct conf_load *conf, .type_dcu = type_cu ? &type_dcu : NULL, .build_id = build_id, .build_id_len = build_id_len, + .nr_cus_created = 0,should go to the previous patch? if needed at all..
Yeah. I think it's better to have an explicit assignment.
Quoted 17 lines
thanks,
jirkaQuoted 10 lines
btf_encoder.c +0
-0}; res =dwarf_cus__process_cus (&dcus); }dwarves.c +0
-0@@ -530,48 +530,6 @@ void cus__unlock(struct cus *cus)pthread_mutex_unlock (&cus->mutex); }SNIP
PATCH v3 8/8 btf_encoder: clean up global encoders list
In reply to Ihor SolodraiWith multithreading moved entirely to the dwarf_loader, now there is only one btf_encoder. Hence there is no need to maintain a global list of encoders anymore.
--- btf_encoder.c | 106 ++++++++------------------------------------------ btf_encoder.h | 4 -- 2 files changed, 17 insertions(+), 93 deletions(-)
btf_encoder.c +17 -89
@@ -218,39 +218,6 @@ static struct elf_functions *elf_functions__find(const Elf *elf, const struct lireturn NULL ; } - -static LIST_HEAD (encoders); -static pthread_mutex_t encoders__lock =PTHREAD_MUTEX_INITIALIZER ; - -/* mutex only needed for add/delete, as this can happen in multiple encoding -* threads. Traversal of the list is currently confined to thread collection. -*/ - -#define btf_encoders__for_each_encoder (encoder) \ - list_for_each_entry(encoder, &encoders, node) - -static void btf_encoders__add (struct btf_encoder *encoder) -{ -pthread_mutex_lock (&encoders__lock); -list_add_tail (&encoder->node, &encoders); -pthread_mutex_unlock (&encoders__lock); -} - -static void btf_encoders__delete (struct btf_encoder *encoder) -{ -struct btf_encoder *existing =NULL ; - -pthread_mutex_lock (&encoders__lock); -/* encoder may not have been added to list yet; check. */ -btf_encoders__for_each_encoder (existing) { -if (encoder == existing) -break ; - } -if (encoder == existing) -list_del (&encoder->node); -pthread_mutex_unlock (&encoders__lock); -} -#define PERCPU_SECTION ".data..percpu"/*
@@ -868,39 +835,6 @@ static int32_t btf_encoder__add_var_secinfo(struct btf_encoder *encoder, size_treturn gobuffer__add (&encoder->secinfo[shndx].secinfo, &si,sizeof (si)); } -int32_t btf_encoder__add_encoder (struct btf_encoder *encoder,struct btf_encoder *other) -{ -size_t shndx; -if (encoder == other) -return 0 ; - -for (shndx =1 ; shndx < other->seccnt; shndx++) { -struct gobuffer *var_secinfo_buf = &other->secinfo[shndx].secinfo; -size_t sz =gobuffer__size (var_secinfo_buf); -uint16_t nr_var_secinfo = sz /sizeof (struct btf_var_secinfo ); -uint32_t type_id; -uint32_t next_type_id =btf__type_cnt (encoder->btf); -int32_t i, id; -struct btf_var_secinfo *vsi; - -if (strcmp (encoder->secinfo[shndx].name, other->secinfo[shndx].name)) { -fprintf (stderr,"mismatched ELF sections at index %zu: \"%s\", \"%s\"\n" , - shndx, encoder->secinfo[shndx].name, other->secinfo[shndx].name); -return -1 ; - } - -for (i =0 ; i < nr_var_secinfo; i++) { - vsi = (struct btf_var_secinfo *)var_secinfo_buf->entries + i; - type_id = next_type_id + vsi->type -1 ;/* Type ID starts from 1 */ - id =btf_encoder__add_var_secinfo (encoder, shndx, type_id, vsi->offset, vsi->size); -if (id <0 ) -return id; - } - } - -return btf__add_btf (encoder->btf, other->btf); -} -static int32_t btf_encoder__add_datasec (struct btf_encoder *encoder,size_t shndx) {struct gobuffer *var_secinfo_buf = &encoder->secinfo[shndx].secinfo;
@@ -1326,27 +1260,29 @@ static void btf_encoder__delete_saved_funcs(struct btf_encoder *encoder) } } -static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (struct btf_encoder *encoder,bool skip_encoding_inconsistent_proto) {struct btf_encoder_func_state **saved_fns, *s; -struct btf_encoder *e =NULL ; -int i =0 , j, nr_saved_fns =0 ; +int err =0 , i =0 , j, nr_saved_fns =0 ; -/* Retrieve function states from each encoder, combine them +/* Retrieve function states from the encoder, combine them * and sort by name, addr. */ -btf_encoders__for_each_encoder (e) { -list_for_each_entry (s, &e->func_states, node) - nr_saved_fns++; +list_for_each_entry (s, &encoder->func_states, node) { + nr_saved_fns++; }if (nr_saved_fns ==0 ) -return 0 ; + goto out; saved_fns =calloc (nr_saved_fns,sizeof (*saved_fns)); -btf_encoders__for_each_encoder (e) { -list_for_each_entry (s, &e->func_states, node) - saved_fns[i++] = s; +if (!saved_fns) { + err = -ENOMEM ; + goto out; + } + +list_for_each_entry (s, &encoder->func_states, node) { + saved_fns[i++] = s; }qsort (saved_fns, nr_saved_fns,sizeof (*saved_fns), saved_functions_cmp);
@@ -1377,11 +1313,10 @@ static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto)/* Now that we are done with function states, free them. */ free (saved_fns); -btf_encoders__for_each_encoder (e) { -btf_encoder__delete_saved_funcs (e); - } +btf_encoder__delete_saved_funcs (encoder); -return 0 ; +out: +return err; }static void elf_functions__collect_function (struct elf_functions *functions,GElf_Sym *sym)
@@ -2134,7 +2069,7 @@ int btf_encoder__encode(struct btf_encoder *encoder, struct conf_load *conf)int err;size_t shndx; -btf_encoder__add_saved_funcs (conf->skip_encoding_btf_inconsistent_proto); +btf_encoder__add_saved_funcs (encoder, conf->skip_encoding_btf_inconsistent_proto);for (shndx =1 ; shndx < encoder->seccnt; shndx++)if (gobuffer__size (&encoder->secinfo[shndx].secinfo))
@@ -2541,7 +2476,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamif (encoder->verbose)printf ("File %s:\n" , cu->filename); -btf_encoders__add (encoder); }return encoder;
@@ -2558,7 +2492,6 @@ void btf_encoder__delete(struct btf_encoder *encoder)if (encoder ==NULL )return ; -btf_encoders__delete (encoder);for (shndx =0 ; shndx < encoder->seccnt; shndx++)__gobuffer__delete (&encoder->secinfo[shndx].secinfo);free (encoder->secinfo);
@@ -2727,8 +2660,3 @@ out: encoder->cu =NULL ;return err; } - -struct btf *btf_encoder__btf (struct btf_encoder *encoder) -{ -return encoder->btf; -}
btf_encoder.h +0 -4
@@ -27,10 +27,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenamvoid btf_encoder__delete (struct btf_encoder *encoder);int btf_encoder__encode (struct btf_encoder *encoder,struct conf_load *conf); -int btf_encoder__encode_cu (struct btf_encoder *encoder,struct cu *cu,struct conf_load *conf_load); -struct btf *btf_encoder__btf (struct btf_encoder *encoder); - -int btf_encoder__add_encoder (struct btf_encoder *encoder,struct btf_encoder *other);#endif /* _BTF_ENCODER_H_ */
Signature · 1 line
2.47.1
Re: [PATCH dwarves v3 8/8] btf_encoder: clean up global encoders list
On Sat, Dec 21, 2024 at 01:23:45AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 38 lines
btf_encoder.c +13
-11-static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (struct btf_encoder *encoder,bool skip_encoding_inconsistent_proto) {struct btf_encoder_func_state **saved_fns, *s; -struct btf_encoder *e =NULL ; -int i =0 , j, nr_saved_fns =0 ; +int err =0 , i =0 , j, nr_saved_fns =0 ; -/* Retrieve function states from each encoder, combine them +/* Retrieve function states from the encoder, combine them * and sort by name, addr. */ -btf_encoders__for_each_encoder (e) { -list_for_each_entry (s, &e->func_states, node) - nr_saved_fns++; +list_for_each_entry (s, &encoder->func_states, node) { + nr_saved_fns++; }if (nr_saved_fns ==0 ) -return 0 ; + goto out; saved_fns =calloc (nr_saved_fns,sizeof (*saved_fns)); -btf_encoders__for_each_encoder (e) { -list_for_each_entry (s, &e->func_states, node) - saved_fns[i++] = s; +if (!saved_fns) { + err = -ENOMEM ; + goto out; + } + +list_for_each_entry (s, &encoder->func_states, node) { + saved_fns[i++] = s; }qsort (saved_fns, nr_saved_fns,sizeof (*saved_fns), saved_functions_cmp);
btf_encoder.c static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto) · line 1316
Quoted 7 lines
btf_encoder.c +1
-3@@ -1377,11 +1313,10 @@ static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto)/* Now that we are done with function states, free them. */ free (saved_fns); -btf_encoders__for_each_encoder (e) { -btf_encoder__delete_saved_funcs (e); - } +btf_encoder__delete_saved_funcs (encoder);
is this call necessary? there's btf_encoder__delete call right after same for elf_functions_list__clear in btf_encoder__encode
thanks,
jirka
Quoted 6 lines
btf_encoder.c +2
-1-return 0 ; +out: +return err; }
SNIP
Re: [PATCH dwarves v3 8/8] btf_encoder: clean up global encoders list
Quoted 44 lines · Jiri Olsa
On Sat, Dec 21, 2024 at 01:23:45AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 39 lines
btf_encoder.c +1
-1-static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (struct btf_encoder *encoder,bool skip_encoding_inconsistent_proto){
structbtf_encoder_func_state**saved_fns, *s;
- structbtf_encoder*e = NULL;
- int i = 0, j,nr_saved_fns= 0;
+ int err = 0, i = 0, j,nr_saved_fns= 0;- /* Retrieve function states from each encoder, combine them
+ /* Retrieve function states from the encoder, combine them
* and sort by name, addr.
*/
-btf_encoders__for_each_encoder(e){
-list_for_each_entry(s, &e->func_states, node)
- nr_saved_fns++;
+list_for_each_entry(s, &encoder->func_states, node){
+ nr_saved_fns++;
}if (
nr_saved_fns== 0)
- return 0;
+ goto out;
saved_fns=calloc(nr_saved_fns, sizeof(*saved_fns));
-btf_encoders__for_each_encoder(e){
-list_for_each_entry(s, &e->func_states, node)
- saved_fns[i++] = s;
+ if (!saved_fns) {
+ err =-ENOMEM;
+ goto out;
+ }
+
+list_for_each_entry(s, &encoder->func_states, node){
+ saved_fns[i++] = s;
}qsort(saved_fns, nr_saved_fns, sizeof(*saved_fns), saved_functions_cmp);(quoted diff) +0
-0@@ -1377,11 +1313,10 @@ static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto)
Quoted 10 lines
btf_encoder.c static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto) · line 1316
Quoted 6 lines
btf_encoder.c +1
-3/* Now that we are done with function states, free them. */ free (saved_fns); -btf_encoders__for_each_encoder (e) { -btf_encoder__delete_saved_funcs (e); - } +btf_encoder__delete_saved_funcs (encoder);is this call necessary? there's
btf_encoder__deletecall right after same forelf_functions_list__clearinbtf_encoder__encode
At this point we know that the function information is no longer needed, so we can free up some memory.
I remember when I wrote a "context" patch [1] (now discarded), there was a significant difference in MAX RSS between freeing this right away and delaying until encoding is finished. Now it might not be as significant, but still there is no reason to keep the stuff we know is not used later.
[1] https://lore.kernel.org/dwarves/[email protected]/
Quoted 11 lines
thanks,
jirkaQuoted 4 lines
btf_encoder.c +2
-1-return 0 ; +out: +return err; }SNIP
Re: [PATCH dwarves v3 8/8] btf_encoder: clean up global encoders list
Quoted 57 lines · Ihor Solodrai
Quoted 44 lines · Jiri Olsa
On Sat, Dec 21, 2024 at 01:23:45AM +0000, Ihor Solodrai wrote:
SNIP
Quoted 39 lines
btf_encoder.c +1
-1-static int btf_encoder__add_saved_funcs (bool skip_encoding_inconsistent_proto) +static int btf_encoder__add_saved_funcs (struct btf_encoder *encoder,bool skip_encoding_inconsistent_proto){
structbtf_encoder_func_state**saved_fns, *s;
- structbtf_encoder*e = NULL;
- int i = 0, j,nr_saved_fns= 0;
+ int err = 0, i = 0, j,nr_saved_fns= 0;- /* Retrieve function states from each encoder, combine them
+ /* Retrieve function states from the encoder, combine them
* and sort by name, addr.
*/
-btf_encoders__for_each_encoder(e){
-list_for_each_entry(s, &e->func_states, node)
- nr_saved_fns++;
+list_for_each_entry(s, &encoder->func_states, node){
+ nr_saved_fns++;
}if (
nr_saved_fns== 0)
- return 0;
+ goto out;
saved_fns=calloc(nr_saved_fns, sizeof(*saved_fns));
-btf_encoders__for_each_encoder(e){
-list_for_each_entry(s, &e->func_states, node)
- saved_fns[i++] = s;
+ if (!saved_fns) {
+ err =-ENOMEM;
+ goto out;
+ }
+
+list_for_each_entry(s, &encoder->func_states, node){
+ saved_fns[i++] = s;
}qsort(saved_fns, nr_saved_fns, sizeof(*saved_fns), saved_functions_cmp);(quoted diff) +0
-0@@ -1377,11 +1313,10 @@ static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto)Quoted 10 lines
btf_encoder.c static int btf_encoder__add_saved_funcs(bool skip_encoding_inconsistent_proto) · line 1316
Quoted 6 lines
btf_encoder.c +1
-3/* Now that we are done with function states, free them. */ free (saved_fns); -btf_encoders__for_each_encoder (e) { -btf_encoder__delete_saved_funcs (e); - } +btf_encoder__delete_saved_funcs (encoder);is this call necessary? there's
btf_encoder__deletecall right after same forelf_functions_list__clearinbtf_encoder__encode
Quoted 8 lines
At this point we know that the function information is no longer needed, so we can free up some memory.
I remember when I wrote a "context" patch [1] (now discarded), there was a significant difference in MAX RSS between freeing this right away and delaying until encoding is finished. Now it might not be as significant, but still there is no reason to keep the stuff we know is not used later.
ok, makes sense
jirka
Quoted 15 lines
[1] https://lore.kernel.org/dwarves/[email protected]/
Quoted 11 lines
thanks,
jirkaQuoted 4 lines
btf_encoder.c +2
-1-return 0 ; +out: +return err; }SNIP