[libunwind] 881aba7 - [libunwind] Optimize dl_iterate_phdr's findUnwindSectionsByPhdr

Ryan Prichard via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 23 15:42:30 PDT 2020


Author: Ryan Prichard
Date: 2020-09-23T15:40:39-07:00
New Revision: 881aba7071c6e4cc2417e875ca5027ec7c0a92a3

URL: https://github.com/llvm/llvm-project/commit/881aba7071c6e4cc2417e875ca5027ec7c0a92a3
DIFF: https://github.com/llvm/llvm-project/commit/881aba7071c6e4cc2417e875ca5027ec7c0a92a3.diff

LOG: [libunwind] Optimize dl_iterate_phdr's findUnwindSectionsByPhdr

Currently, findUnwindSectionsByPhdr is slightly micro-optimized for the
case where the first callback has the target address, and is otherwise
very inefficient -- it decodes .eh_frame_hdr even when no PT_LOAD
matches the PC. (If the FrameHeaderCache is enabled, then the
micro-optimization only helps the first time unwind info is looked up.)

Instead, it makes more sense to optimize for the case where the
callback *doesn't* find the target address, so search for a PT_LOAD
segment first, and only look for the unwind info section if a matching
PT_LOAD is found.

This change helps on an Android benchmark with 100 shared objects,
where the DSO at the end of the dl_iterate_phdr list throws 10000
exceptions. Assuming the frame cache is disabled, this change cuts
about 30-40% off the benchmark's runtime.

Reviewed By: compnerd, saugustine, #libunwind

Differential Revision: https://reviews.llvm.org/D87881

Added: 
    

Modified: 
    libunwind/src/AddressSpace.hpp

Removed: 
    


################################################################################
diff  --git a/libunwind/src/AddressSpace.hpp b/libunwind/src/AddressSpace.hpp
index 7a926ec0be55..171318ff6370 100644
--- a/libunwind/src/AddressSpace.hpp
+++ b/libunwind/src/AddressSpace.hpp
@@ -462,29 +462,37 @@ static int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo,
 #endif
 
   Elf_Addr image_base = calculateImageBase(pinfo);
-  bool found_text = false;
-  bool found_unwind = false;
 
-  // Third phdr is usually the executable phdr.
-  if (pinfo->dlpi_phnum > 2)
-    found_text = checkAddrInSegment(&pinfo->dlpi_phdr[2], image_base, cbdata);
+  // Most shared objects seen in this callback function likely don't contain the
+  // target address, so optimize for that. Scan for a matching PT_LOAD segment
+  // first and bail when it isn't found.
+  bool found_text = false;
+  for (Elf_Half i = 0; i < pinfo->dlpi_phnum; ++i) {
+    if (checkAddrInSegment(&pinfo->dlpi_phdr[i], image_base, cbdata)) {
+      found_text = true;
+      break;
+    }
+  }
+  if (!found_text)
+    return 0;
 
   // PT_GNU_EH_FRAME and PT_ARM_EXIDX are usually near the end. Iterate
-  // backward. We already know that there is one or more phdrs.
+  // backward.
+  bool found_unwind = false;
   for (Elf_Half i = pinfo->dlpi_phnum; i > 0; i--) {
     const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i - 1];
-    if (!found_unwind && checkForUnwindInfoSegment(phdr, image_base, cbdata))
+    if (checkForUnwindInfoSegment(phdr, image_base, cbdata)) {
       found_unwind = true;
-    else if (!found_text && checkAddrInSegment(phdr, image_base, cbdata))
-      found_text = true;
-    if (found_text && found_unwind) {
-#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
-      TheFrameHeaderCache.add(cbdata->sects);
-#endif
-      return 1;
+      break;
     }
   }
-  return 0;
+  if (!found_unwind)
+    return 0;
+
+#if defined(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
+  TheFrameHeaderCache.add(cbdata->sects);
+#endif
+  return 1;
 }
 
 #endif  // defined(_LIBUNWIND_USE_DL_ITERATE_PHDR)


        


More information about the cfe-commits mailing list