[PATCH v2 dwarves 5/5] btf_encoder: switch to shared elf_functions table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Do not collect functions from ELF for each new btf_encoder, and
instead set a pointer to a shared elf_functions table, built
beforehand by btf_encoder__pre_cus__load_module().

Change the algorithm of btf_encoder__add_saved_funcs(): traverse
elf_functions table once, merge function state for each list of
elf_function, and add each function to the BTF of the owner of the
first elf_function of the list.

Do not call btf_encoder__add_saved_funcs() on every
btf_encoder__add_encoder(). Instead, introduce
btf_encoder__merge_encoders() function that does that once.

Signed-off-by: Ihor Solodrai <ihor.solodrai@xxxxx>
---
 btf_encoder.c | 113 +++++++++++++++++++++++++++-----------------------
 btf_encoder.h |   3 +-
 pahole.c      |  25 ++++++-----
 3 files changed, 78 insertions(+), 63 deletions(-)

diff --git a/btf_encoder.c b/btf_encoder.c
index f88c2e1..07d2031 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -140,7 +140,7 @@ struct btf_encoder {
 	struct elf_secinfo *secinfo;
 	size_t             seccnt;
 	int                encode_vars;
-	struct elf_functions functions;
+	struct elf_functions *functions;
 };
 
 struct btf_func {
@@ -243,8 +243,6 @@ int btf_encoder__pre_cus__load_module(struct process_dwflmod_parms *parms, Dwfl_
 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.
  */
@@ -890,14 +888,12 @@ static int32_t btf_encoder__add_var_secinfo(struct btf_encoder *encoder, size_t
 	return gobuffer__add(&encoder->secinfo[shndx].secinfo, &si, sizeof(si));
 }
 
-int32_t btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoder *other)
+static int32_t btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoder *other)
 {
 	size_t shndx;
 	if (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);
@@ -925,6 +921,25 @@ int32_t btf_encoder__add_encoder(struct btf_encoder *encoder, struct btf_encoder
 	return btf__add_btf(encoder->btf, other->btf);
 }
 
+int btf_encoder__merge_encoders(struct btf_encoder *base_encoder, struct btf_encoder **encoders, int nr_encoders)
+{
+	int err;
+
+	err = btf_encoder__add_saved_funcs(base_encoder);
+	if (err < 0)
+		return err;
+
+	for (int i = 0; i < nr_encoders; i++) {
+		if (base_encoder == encoders[i])
+			continue;
+		err = btf_encoder__add_encoder(base_encoder, encoders[i]);
+		if (err < 0)
+			return err;
+	}
+
+	return 0;
+}
+
 static int32_t btf_encoder__add_datasec(struct btf_encoder *encoder, size_t shndx)
 {
 	struct gobuffer *var_secinfo_buf = &encoder->secinfo[shndx].secinfo;
@@ -991,7 +1006,7 @@ static int32_t btf_encoder__add_decl_tag(struct btf_encoder *encoder, const char
 	return id;
 }
 
-static void btf_encoder__log_func_skip(struct btf_encoder *encoder, struct elf_function *func,
+static void btf_encoder__log_func_skip(const struct btf_encoder *encoder, struct elf_function *func,
 				       const char *fmt, ...)
 {
 	va_list ap;
@@ -1027,7 +1042,7 @@ static int fwd__kind(const struct btf_type *t)
 	return btf_kind(t);
 }
 
-static bool types__match(struct btf_encoder *encoder,
+static bool types__match(const struct btf_encoder *encoder,
 			 struct btf *btf1, int type_id1,
 			 struct btf *btf2, int type_id2)
 {
@@ -1126,7 +1141,7 @@ static bool types__match(struct btf_encoder *encoder,
 	return false;
 }
 
-static bool funcs__match(struct btf_encoder *encoder, struct elf_function *func,
+static bool funcs__match(const 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)
 {
@@ -1340,44 +1355,51 @@ static inline struct elf_function *find_elf_function_in_list(struct elf_function
 	return NULL;
 }
 
-static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder)
+
+// Each element of elf_functions->entries is a list of a variable
+// number of elf_function structs, because not all encoders saved a
+// particular function.  However each function saved by at least one
+// encoder needs to be added to BTF _somewhere_. Here we traverse the
+// entire elf_functions table, and for each list of elf_function
+// structs a function is added to the BTF of the encoder of the first
+// elf_function in the list.
+int btf_encoder__add_saved_funcs(struct btf_encoder *base_encoder)
 {
-	int i;
+	struct elf_functions *functions = base_encoder->functions;
 
-	for (i = 0; i < encoder->functions.cnt; i++) {
-		struct elf_function *func = find_elf_function_in_list(&encoder->functions.entries[i], encoder);
+	for (int i = 0; i < functions->cnt; i++) {
+		struct elf_function *list_head = &functions->entries[i];
+		struct btf_encoder_func_state *state;
+		struct elf_function *func;
 
-		if (!func)
+		// list_head without an owner means no encoder
+		// has saved a function with this name
+		if (!list_head->owner)
 			continue;
 
-		struct btf_encoder_func_state *state = &func->state;
-		struct btf_encoder *other_encoder = NULL;
+		func = list_head;
+		state = &func->state;
 
 		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;
-			other_func = find_elf_function_in_list(&other_encoder->functions.entries[i], other_encoder);
 
-			if (other_encoder == encoder || !other_func)
+		for_each_elf_function(other_func, list_head) {
+			if (func == other_func)
 				continue;
 
-			other_state = &other_func->state;
-			if (!other_state->initialized)
+			struct btf_encoder_func_state *other_state = &other_func->state;
+			uint8_t optimized, unexpected, inconsistent;
+
+			if (!other_func || !other_state->initialized || other_state->processed)
 				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))
+			    !funcs__match(func->owner, func,
+					  func->owner->btf, state,
+					  other_func->owner->btf, other_state))
 				inconsistent = 1;
 			state->optimized_parms = other_state->optimized_parms = optimized;
 			state->unexpected_reg = other_state->unexpected_reg = unexpected;
@@ -1391,7 +1413,7 @@ static int btf_encoder__add_saved_funcs(struct btf_encoder *encoder)
 		 * unexpected register use or multiple inconsistent prototypes.
 		 */
 		if (!state->unexpected_reg && !state->inconsistent_proto) {
-			if (btf_encoder__add_func(encoder, NULL, func))
+			if (btf_encoder__add_func((struct btf_encoder *)func->owner, NULL, func))
 				return -1;
 		}
 		state->processed = 1;
@@ -1442,8 +1464,8 @@ static struct elf_function *btf_encoder__find_function(const struct btf_encoder
 {
 	struct elf_function key = { .name = name, .prefixlen = prefixlen };
 	struct elf_function *head = bsearch(&key,
-					    encoder->functions.entries,
-					    encoder->functions.cnt,
+					    encoder->functions->entries,
+					    encoder->functions->cnt,
 					    sizeof(key),
 					    functions_cmp);
 	struct elf_function *func = NULL;
@@ -2169,9 +2191,6 @@ int btf_encoder__encode(struct btf_encoder *encoder)
 	int err;
 	size_t shndx;
 
-	/* for single-threaded case, saved funcs are added here */
-	btf_encoder__add_saved_funcs(encoder);
-
 	for (shndx = 1; shndx < encoder->seccnt; shndx++)
 		if (gobuffer__size(&encoder->secinfo[shndx].secinfo))
 			btf_encoder__add_datasec(encoder, shndx);
@@ -2222,11 +2241,8 @@ int btf_encoder__encode(struct btf_encoder *encoder)
 		err = btf_encoder__write_elf(encoder, encoder->btf, BTF_ELF_SEC);
 	}
 
-	// TODO: after moving encoders to shared elf_functions,
-	// replace elf_functions__get(encoder->functions.elf) here
-	// with encoder->functions
-	// The pointer to shared elf_functions will be set in btf_encoder__new
-	elf_functions__delete(elf_functions__get(encoder->functions.elf));
+	// Delete the shared elf_functions table, because we are done encoding
+	elf_functions__delete(encoder->functions);
 
 	return err;
 }
@@ -2525,8 +2541,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
 				printf("%s: '%s' doesn't have symtab.\n", __func__, cu->filename);
 			goto out;
 		}
-		encoder->functions.symtab = encoder->symtab;
-		encoder->functions.elf = cu->elf;
+		encoder->functions = elf_functions__get(cu->elf);
 
 		/* index the ELF sections for later lookup */
 
@@ -2565,9 +2580,6 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam
 		if (!found_percpu && encoder->verbose)
 			printf("%s: '%s' doesn't have '%s' section\n", __func__, cu->filename, PERCPU_SECTION);
 
-		if (elf_functions__collect(&encoder->functions))
-			goto out_delete;
-
 		if (encoder->verbose)
 			printf("File %s:\n", cu->filename);
 		btf_encoders__add(encoder);
@@ -2593,8 +2605,7 @@ void btf_encoder__delete(struct btf_encoder *encoder)
 	zfree(&encoder->source_filename);
 	btf__free(encoder->btf);
 	encoder->btf = NULL;
-
-	__elf_functions__delete(&encoder->functions);
+	elf_symtab__delete(encoder->symtab);
 
 
 	free(encoder);
@@ -2694,7 +2705,7 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co
 			continue;
 		if (!ftype__has_arg_names(&fn->proto))
 			continue;
-		if (encoder->functions.cnt) {
+		if (encoder->functions->cnt) {
 			const char *name;
 
 			name = function__name(fn);
@@ -2710,7 +2721,7 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co
 					save = true;
 				else
 					func->generated = true;
-			} else if (encoder->functions.suffix_cnt &&
+			} else if (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
diff --git a/btf_encoder.h b/btf_encoder.h
index 7bfc994..2fb4fc6 100644
--- a/btf_encoder.h
+++ b/btf_encoder.h
@@ -32,7 +32,8 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu, struct co
 
 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__merge_encoders(struct btf_encoder *base_encoder, struct btf_encoder **encoders, int nr_encoders);
 
 int btf_encoder__pre_cus__load_module(struct process_dwflmod_parms *parms, Dwfl_Module *mod, Dwarf *dw, Elf *elf);
 
diff --git a/pahole.c b/pahole.c
index 0ff6bc2..a45aa7a 100644
--- a/pahole.c
+++ b/pahole.c
@@ -3256,23 +3256,23 @@ static int pahole_threads_collect(struct conf_load *conf, int nr_threads, void *
 				  int error)
 {
 	struct thread_data **threads = (struct thread_data **)thr_data;
+	struct btf_encoder *encoders[nr_threads];
 	int i;
 	int err = 0;
 
 	if (error)
 		goto out;
 
-	for (i = 0; i < nr_threads; i++) {
-		/*
-		 * Merge content of the btf instances of worker threads to the btf
-		 * instance of the primary btf_encoder.
-                */
-		if (!threads[i]->btf)
-			continue;
-		err = btf_encoder__add_encoder(btf_encoder, threads[i]->encoder);
-		if (err < 0)
-			goto out;
-	}
+	for (i = 0; i < nr_threads; i++)
+		encoders[i] = threads[i]->encoder;
+
+	/*
+	 * Merge content of the btf instances of worker threads to the btf
+	 * instance of the primary btf_encoder.
+	 */
+	err = btf_encoder__merge_encoders(btf_encoder, encoders, nr_threads);
+	if (err < 0)
+		goto out;
 	err = 0;
 
 out:
@@ -3915,6 +3915,9 @@ try_sole_arg_as_class_names:
 			exit(1);
 		}
 
+		if (conf_load.nr_jobs <= 1 || conf_load.reproducible_build)
+			btf_encoder__add_saved_funcs(btf_encoder);
+
 		err = btf_encoder__encode(btf_encoder);
 		if (err) {
 			fputs("Failed to encode BTF\n", stderr);
-- 
2.43.0







[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux