From a205a91bb3daa8a84fa6b429c13d39563add950f Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 21 Nov 2024 14:14:49 +0100 Subject: [PATCH] MINOR: tools: resolve main() only once in resolve_sym_name() resolv_sym_name() calls dladdr(main) for each symbol in order to compare the first address with other symbols. But this is pointless and quite expensive in outputs to "show profiling" for example. Let's just keep a local copy and have a variable indicating if the resolution is needed/ in progress/done to save the value for subsequent calls. --- src/tools.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/tools.c b/src/tools.c index 224c5df97..968b32485 100644 --- a/src/tools.c +++ b/src/tools.c @@ -5536,7 +5536,9 @@ const void *resolve_sym_name(struct buffer *buf, const char *pfx, const void *ad }; #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) - Dl_info dli, dli_main; + static Dl_info dli_main; + static int dli_main_done; // 0 = not resolved, 1 = resolve in progress, 2 = done + Dl_info dli; size_t size; const char *fname, *p; #endif @@ -5561,8 +5563,21 @@ const void *resolve_sym_name(struct buffer *buf, const char *pfx, const void *ad * that contains the main function. The name is picked between last '/' * and first following '.'. */ - if (!dladdr(main, &dli_main)) - dli_main.dli_fbase = NULL; + + /* let's check main only once, no need to do it all the time */ + + i = HA_ATOMIC_LOAD(&dli_main_done); + while (i < 2) { + i = 0; + if (HA_ATOMIC_CAS(&dli_main_done, &i, 1)) { + /* we're the first ones, resolve it */ + if (!dladdr(main, &dli_main)) + dli_main.dli_fbase = NULL; + HA_ATOMIC_STORE(&dli_main_done, 2); // done + break; + } + ha_thread_relax(); + } if (dli_main.dli_fbase != dli.dli_fbase) { fname = dli.dli_fname;