MINOR: tools: resolve main() only once in resolve_sym_name()
authorWilly Tarreau <w@1wt.eu>
Thu, 21 Nov 2024 13:14:49 +0000 (14:14 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 20 Mar 2025 10:38:00 +0000 (11:38 +0100)
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.

(cherry picked from commit a205a91bb3daa8a84fa6b429c13d39563add950f)
Signed-off-by: Willy Tarreau <w@1wt.eu>

src/tools.c

index 474f6e9..10ad5ce 100644 (file)
@@ -5444,7 +5444,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
@@ -5469,8 +5471,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;