[Lldb-commits] [PATCH] D40470: DWZ 03/07: Protect DWARFCompileUnit::m_die_array by a new mutex

Jan Kratochvil via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Apr 30 09:55:34 PDT 2018


jankratochvil updated this revision to Diff 144576.

https://reviews.llvm.org/D40470

Files:
  source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
  source/Plugins/SymbolFile/DWARF/DWARFUnit.h


Index: source/Plugins/SymbolFile/DWARF/DWARFUnit.h
===================================================================
--- source/Plugins/SymbolFile/DWARF/DWARFUnit.h
+++ source/Plugins/SymbolFile/DWARF/DWARFUnit.h
@@ -141,6 +141,8 @@
   void *m_user_data = nullptr;
   // The compile unit debug information entry item
   DWARFDebugInfoEntry::collection m_die_array;
+  // Prevent m_extractdies_mutex lock overhead for most cases.
+  std::atomic_size_t m_die_array_size_atomic { 0 };
   // A table similar to the .debug_aranges table, but this one points to the
   // exact DW_TAG_subprogram DIEs
   std::unique_ptr<DWARFDebugAranges> m_func_aranges_ap;
@@ -160,6 +162,7 @@
   // If this is a dwo compile unit this is the offset of the base compile unit
   // in the main object file
   dw_offset_t m_base_obj_offset = DW_INVALID_OFFSET;
+  std::mutex m_extractdies_mutex;
 
   static void
   IndexPrivate(DWARFUnit *dwarf_cu, const lldb::LanguageType cu_language,
Index: source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
===================================================================
--- source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
+++ source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
@@ -42,9 +42,19 @@
 // done.
 //----------------------------------------------------------------------
 size_t DWARFUnit::ExtractDIEsIfNeeded(bool cu_die_only) {
-  const size_t initial_die_array_size = m_die_array.size();
-  if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
-    return 0; // Already parsed
+  size_t initial_die_array_size;
+  auto already_parsed = [cu_die_only, &initial_die_array_size, this]() -> bool {
+    initial_die_array_size = m_die_array_size_atomic;
+    return (cu_die_only && initial_die_array_size > 0)
+        || initial_die_array_size > 1;
+  };
+  if (already_parsed())
+    return 0;
+  std::lock_guard<std::mutex> guard(m_extractdies_mutex);
+  if (already_parsed())
+    return 0;
+  std::shared_ptr<void> m_die_array_size_atomic_set(nullptr,
+      [&](void*){ m_die_array_size_atomic = m_die_array.size(); });
 
   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
   Timer scoped_timer(
@@ -307,6 +317,8 @@
 
 void DWARFUnit::ClearDIEs(bool keep_compile_unit_die) {
   if (m_die_array.size() > 1) {
+    std::lock_guard<std::mutex> guard(m_extractdies_mutex);
+
     // std::vectors never get any smaller when resized to a smaller size,
     // or when clear() or erase() are called, the size will report that it
     // is smaller, but the memory allocated remains intact (call capacity()
@@ -318,8 +330,11 @@
     // Save at least the compile unit DIE
     DWARFDebugInfoEntry::collection tmp_array;
     m_die_array.swap(tmp_array);
-    if (keep_compile_unit_die)
+    m_die_array_size_atomic = 0;
+    if (keep_compile_unit_die) {
       m_die_array.push_back(tmp_array.front());
+      ++m_die_array_size_atomic;
+    }
   }
 
   if (m_dwo_symbol_file)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40470.144576.patch
Type: text/x-patch
Size: 2922 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20180430/bc88efa7/attachment.bin>


More information about the lldb-commits mailing list