From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id CDAEF3857B96; Thu, 26 May 2022 13:49:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDAEF3857B96 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Finalize each cooked index separately X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: f420c9c84eb53fea7da452eab8ae0bd855537859 X-Git-Newrev: 20a26f4e018cd0cb40a3b81568be0d4f164eed0f Message-Id: <20220526134938.CDAEF3857B96@sourceware.org> Date: Thu, 26 May 2022 13:49:38 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 26 May 2022 13:49:38 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D20a26f4e018c= d0cb40a3b81568be0d4f164eed0f commit 20a26f4e018cd0cb40a3b81568be0d4f164eed0f Author: Tom Tromey Date: Fri May 13 13:46:52 2022 -0600 Finalize each cooked index separately =20 After DWARF has been scanned, the cooked index code does a "finalization" step in a worker thread. This step combines all the index entries into a single master list, canonicalizes C++ names, and splits Ada names to synthesize package names. =20 While this step is run in the background, gdb will wait for the results in some situations, and it turns out that this step can be slow. This is PR symtab/29105. =20 This can be sped up by parallelizing, at a small memory cost. Now each index is finalized on its own, in a worker thread. The cost comes from name canonicalization: if a given non-canonical name is referred to by multiple indices, there will be N canonical copies (one per index) rather than just one. =20 This requires changing the users of the index to iterate over multiple results. However, this is easily done by introducing a new "chained range" class. =20 When run on gdb itself, the memory cost seems rather low -- on my current machine, "maint space 1" reports no change due to the patch. =20 For performance testing, using "maint time 1" and "file" will not show correct results. That approach measures "time to next prompt", but because the patch only affects background work, this shouldn't (and doesn't) change. Instead, a simple way to make gdb wait for the results is to set a breakpoint. =20 Before: =20 $ /bin/time -f%e ~/gdb/install/bin/gdb -nx -q -batch \ -ex 'break main' /tmp/gdb Breakpoint 1 at 0x43ec30: file ../../binutils-gdb/gdb/gdb.c, line 2= 8. 2.00 =20 After: =20 $ /bin/time -f%e ./gdb/gdb -nx -q -batch \ -ex 'break main' /tmp/gdb Breakpoint 1 at 0x43ec30: file ../../binutils-gdb/gdb/gdb.c, line 2= 8. 0.65 =20 Regression tested on x86-64 Fedora 34. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29105 Diff: --- gdb/dwarf2/cooked-index.c | 267 +++++++++++++++++++++++-------------------= ---- gdb/dwarf2/cooked-index.h | 88 +++++++++------ gdbsupport/range-chain.h | 121 +++++++++++++++++++++ 3 files changed, 313 insertions(+), 163 deletions(-) diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index d8a1ab8d30e..3e34ad0e501 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -112,78 +112,22 @@ cooked_index::add (sect_offset die_offset, enum dwarf= _tag tag, return result; } =20 -cooked_index_vector::cooked_index_vector (vec_type &&vec) - : m_vector (std::move (vec)), - m_future (gdb::thread_pool::g_thread_pool->post_task - ([this] () - { - finalize (); - })) -{ -} - /* See cooked-index.h. */ =20 -dwarf2_per_cu_data * -cooked_index_vector::lookup (CORE_ADDR addr) +void +cooked_index::finalize () { - for (const auto &index : m_vector) + m_future =3D gdb::thread_pool::g_thread_pool->post_task ([this] () { - dwarf2_per_cu_data *result =3D index->lookup (addr); - if (result !=3D nullptr) - return result; - } - return nullptr; -} - -/* See cooked-index.h. */ - -std::vector -cooked_index_vector::get_addrmaps () -{ - std::vector result; - for (const auto &index : m_vector) - result.push_back (index->m_addrmap); - return result; -} - -/* See cooked-index.h. */ - -cooked_index_vector::range -cooked_index_vector::find (gdb::string_view name, bool completing) -{ - m_future.wait (); - - auto lower =3D std::lower_bound (m_entries.begin (), m_entries.end (), - name, - [=3D] (const cooked_index_entry *entry, - const gdb::string_view &n) - { - int cmp =3D strncasecmp (entry->canonical, n.data (), n.length ()); - if (cmp !=3D 0 || completing) - return cmp < 0; - return strlen (entry->canonical) < n.length (); - }); - - auto upper =3D std::upper_bound (m_entries.begin (), m_entries.end (), - name, - [=3D] (const gdb::string_view &n, - const cooked_index_entry *entry) - { - int cmp =3D strncasecmp (n.data (), entry->canonical, n.length ()); - if (cmp !=3D 0 || completing) - return cmp < 0; - return n.length () < strlen (entry->canonical); - }); - - return range (lower, upper); + do_finalize (); + }); } =20 /* See cooked-index.h. */ =20 gdb::unique_xmalloc_ptr -cooked_index_vector::handle_gnat_encoded_entry (cooked_index_entry *entry, - htab_t gnat_entries) +cooked_index::handle_gnat_encoded_entry (cooked_index_entry *entry, + htab_t gnat_entries) { std::string canonical =3D ada_decode (entry->name, false, false); if (canonical.empty ()) @@ -206,11 +150,9 @@ cooked_index_vector::handle_gnat_encoded_entry (cooked= _index_entry *entry, { gdb::unique_xmalloc_ptr new_name =3D make_unique_xstrndup (name.data (), name.length ()); - /* It doesn't matter which obstack we allocate this on, so - we pick the most convenient one. */ - last =3D m_vector[0]->create (entry->die_offset, DW_TAG_namespace, - 0, new_name.get (), parent, - entry->per_cu); + last =3D create (entry->die_offset, DW_TAG_namespace, + 0, new_name.get (), parent, + entry->per_cu); last->canonical =3D last->name; m_names.push_back (std::move (new_name)); *slot =3D last; @@ -225,28 +167,8 @@ cooked_index_vector::handle_gnat_encoded_entry (cooked= _index_entry *entry, =20 /* See cooked-index.h. */ =20 -const cooked_index_entry * -cooked_index_vector::get_main () const -{ - const cooked_index_entry *result =3D nullptr; - - for (const auto &index : m_vector) - { - const cooked_index_entry *entry =3D index->get_main (); - if (result =3D=3D nullptr - || ((result->flags & IS_MAIN) =3D=3D 0 - && entry !=3D nullptr - && (entry->flags & IS_MAIN) !=3D 0)) - result =3D entry; - } - - return result; -} - -/* See cooked-index.h. */ - void -cooked_index_vector::finalize () +cooked_index::do_finalize () { auto hash_name_ptr =3D [] (const void *p) { @@ -268,26 +190,38 @@ cooked_index_vector::finalize () htab_up seen_names (htab_create_alloc (10, hash_name_ptr, eq_name_ptr, nullptr, xcalloc, xfree)); =20 - for (auto &index : m_vector) - { - htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, - nullptr, xcalloc, xfree)); + htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry, + nullptr, xcalloc, xfree)); =20 - std::vector entries - =3D std::move (index->m_entries); - for (cooked_index_entry *entry : entries) + for (cooked_index_entry *entry : m_entries) + { + gdb_assert (entry->canonical =3D=3D nullptr); + if ((entry->per_cu->lang !=3D language_cplus + && entry->per_cu->lang !=3D language_ada) + || (entry->flags & IS_LINKAGE) !=3D 0) + entry->canonical =3D entry->name; + else { - gdb_assert (entry->canonical =3D=3D nullptr); - if ((entry->per_cu->lang !=3D language_cplus - && entry->per_cu->lang !=3D language_ada) - || (entry->flags & IS_LINKAGE) !=3D 0) - entry->canonical =3D entry->name; + if (entry->per_cu->lang =3D=3D language_ada) + { + gdb::unique_xmalloc_ptr canon_name + =3D handle_gnat_encoded_entry (entry, gnat_entries.get ()); + if (canon_name =3D=3D nullptr) + entry->canonical =3D entry->name; + else + { + entry->canonical =3D canon_name.get (); + m_names.push_back (std::move (canon_name)); + } + } else { - if (entry->per_cu->lang =3D=3D language_ada) + void **slot =3D htab_find_slot (seen_names.get (), entry, + INSERT); + if (*slot =3D=3D nullptr) { gdb::unique_xmalloc_ptr canon_name - =3D handle_gnat_encoded_entry (entry, gnat_entries.get ()); + =3D cp_canonicalize_string (entry->name); if (canon_name =3D=3D nullptr) entry->canonical =3D entry->name; else @@ -298,37 +232,12 @@ cooked_index_vector::finalize () } else { - void **slot =3D htab_find_slot (seen_names.get (), entry, - INSERT); - if (*slot =3D=3D nullptr) - { - gdb::unique_xmalloc_ptr canon_name - =3D cp_canonicalize_string (entry->name); - if (canon_name =3D=3D nullptr) - entry->canonical =3D entry->name; - else - { - entry->canonical =3D canon_name.get (); - m_names.push_back (std::move (canon_name)); - } - } - else - { - const cooked_index_entry *other - =3D (const cooked_index_entry *) *slot; - entry->canonical =3D other->canonical; - } + const cooked_index_entry *other + =3D (const cooked_index_entry *) *slot; + entry->canonical =3D other->canonical; } } } - - if (m_entries.empty ()) - m_entries =3D std::move (entries); - else - { - m_entries.reserve (m_entries.size () + entries.size ()); - m_entries.insert (m_entries.end (), entries.begin (), entries.end ()); - } } =20 m_names.shrink_to_fit (); @@ -339,3 +248,99 @@ cooked_index_vector::finalize () return *a < *b; }); } + +/* See cooked-index.h. */ + +cooked_index::range +cooked_index::find (gdb::string_view name, bool completing) +{ + wait (); + + auto lower =3D std::lower_bound (m_entries.begin (), m_entries.end (), + name, + [=3D] (const cooked_index_entry *entry, + const gdb::string_view &n) + { + int cmp =3D strncasecmp (entry->canonical, n.data (), n.length ()); + if (cmp !=3D 0 || completing) + return cmp < 0; + return strlen (entry->canonical) < n.length (); + }); + + auto upper =3D std::upper_bound (m_entries.begin (), m_entries.end (), + name, + [=3D] (const gdb::string_view &n, + const cooked_index_entry *entry) + { + int cmp =3D strncasecmp (n.data (), entry->canonical, n.length ()); + if (cmp !=3D 0 || completing) + return cmp < 0; + return n.length () < strlen (entry->canonical); + }); + + return range (lower, upper); +} + +cooked_index_vector::cooked_index_vector (vec_type &&vec) + : m_vector (std::move (vec)) +{ + for (auto &idx : m_vector) + idx->finalize (); +} + +/* See cooked-index.h. */ + +dwarf2_per_cu_data * +cooked_index_vector::lookup (CORE_ADDR addr) +{ + for (const auto &index : m_vector) + { + dwarf2_per_cu_data *result =3D index->lookup (addr); + if (result !=3D nullptr) + return result; + } + return nullptr; +} + +/* See cooked-index.h. */ + +std::vector +cooked_index_vector::get_addrmaps () +{ + std::vector result; + for (const auto &index : m_vector) + result.push_back (index->m_addrmap); + return result; +} + +/* See cooked-index.h. */ + +cooked_index_vector::range +cooked_index_vector::find (gdb::string_view name, bool completing) +{ + std::vector result_range; + result_range.reserve (m_vector.size ()); + for (auto &entry : m_vector) + result_range.push_back (entry->find (name, completing)); + return range (std::move (result_range)); +} + +/* See cooked-index.h. */ + +const cooked_index_entry * +cooked_index_vector::get_main () const +{ + const cooked_index_entry *result =3D nullptr; + + for (const auto &index : m_vector) + { + const cooked_index_entry *entry =3D index->get_main (); + if (result =3D=3D nullptr + || ((result->flags & IS_MAIN) =3D=3D 0 + && entry !=3D nullptr + && (entry->flags & IS_MAIN) !=3D 0)) + result =3D entry; + } + + return result; +} diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h index ad4de05ba52..4e65e9d0c61 100644 --- a/gdb/dwarf2/cooked-index.h +++ b/gdb/dwarf2/cooked-index.h @@ -33,6 +33,7 @@ #include "gdbsupport/thread-pool.h" #include "dwarf2/mapped-index.h" #include "dwarf2/tag.h" +#include "gdbsupport/range-chain.h" =20 struct dwarf2_per_cu_data; =20 @@ -190,8 +191,34 @@ public: m_addrmap =3D addrmap_create_fixed (map, &m_storage); } =20 + /* Finalize the index. This should be called a single time, when + the index has been fully populated. It enters all the entries + into the internal table. */ + void finalize (); + + /* Wait for this index's finalization to be complete. */ + void wait () + { + m_future.wait (); + } + friend class cooked_index_vector; =20 + /* A simple range over part of m_entries. */ + typedef iterator_range::iterator> rang= e; + + /* Return a range of all the entries. */ + range all_entries () + { + wait (); + return { m_entries.begin (), m_entries.end () }; + } + + /* Look up an entry by name. Returns a range of all matching + results. If COMPLETING is true, then a larger range, suitable + for completion, will be returned. */ + range find (gdb::string_view name, bool completing); + private: =20 /* Return the entry that is believed to represent the program's @@ -223,6 +250,17 @@ private: per_cu); } =20 + /* GNAT only emits mangled ("encoded") names in the DWARF, and does + not emit the module structure. However, we need this structure + to do lookups. This function recreates that structure for an + existing entry. It returns the base name (last element) of the + full decoded name. */ + gdb::unique_xmalloc_ptr handle_gnat_encoded_entry + (cooked_index_entry *entry, htab_t gnat_entries); + + /* A helper method that does the work of 'finalize'. */ + void do_finalize (); + /* Storage for the entries. */ auto_obstack m_storage; /* List of all entries. */ @@ -233,6 +271,12 @@ private: /* The addrmap. This maps address ranges to dwarf2_per_cu_data objects. */ addrmap *m_addrmap =3D nullptr; + /* Storage for canonical names. */ + std::vector> m_names; + /* A future that tracks when the 'finalize' method is done. Note + that the 'get' method is never called on this future, only + 'wait'. */ + gdb::future m_future; }; =20 /* The main index of DIEs. The parallel DIE indexers create @@ -253,17 +297,18 @@ public: =20 ~cooked_index_vector () { - /* The 'finalize' method may be run in a different thread. If - this object is destroyed before this completes, then the method - will end up writing to freed memory. Waiting for this to + /* The 'finalize' methods may be run in a different thread. If + this object is destroyed before these complete, then one will + end up writing to freed memory. Waiting for finalization to complete avoids this problem; and the cost seems ignorable because creating and immediately destroying the debug info is a relatively rare thing to do. */ - m_future.wait (); + for (auto &item : m_vector) + item->wait (); } =20 - /* A simple range over part of m_entries. */ - typedef iterator_range::iterator> rang= e; + /* A range over a vector of subranges. */ + typedef range_chain range; =20 /* Look up an entry by name. Returns a range of all matching results. If COMPLETING is true, then a larger range, suitable @@ -273,8 +318,11 @@ public: /* Return a range of all the entries. */ range all_entries () { - m_future.wait (); - return { m_entries.begin (), m_entries.end () }; + std::vector result_range; + result_range.reserve (m_vector.size ()); + for (auto &entry : m_vector) + result_range.push_back (entry->all_entries ()); + return range (std::move (result_range)); } =20 /* Look up ADDR in the address map, and return either the @@ -299,33 +347,9 @@ public: =20 private: =20 - /* GNAT only emits mangled ("encoded") names in the DWARF, and does - not emit the module structure. However, we need this structure - to do lookups. This function recreates that structure for an - existing entry. It returns the base name (last element) of the - full decoded name. */ - gdb::unique_xmalloc_ptr handle_gnat_encoded_entry - (cooked_index_entry *entry, htab_t gnat_entries); - - /* Finalize the index. This should be called a single time, when - the index has been fully populated. It enters all the entries - into the internal hash table. */ - void finalize (); - /* The vector of cooked_index objects. This is stored because the entries are stored on the obstacks in those objects. */ vec_type m_vector; - - /* List of all entries. This is sorted during finalization. */ - std::vector m_entries; - - /* Storage for canonical names. */ - std::vector> m_names; - - /* A future that tracks when the 'finalize' method is done. Note - that the 'get' method is never called on this future, only - 'wait'. */ - gdb::future m_future; }; =20 #endif /* GDB_DWARF2_COOKED_INDEX_H */ diff --git a/gdbsupport/range-chain.h b/gdbsupport/range-chain.h new file mode 100644 index 00000000000..57427370341 --- /dev/null +++ b/gdbsupport/range-chain.h @@ -0,0 +1,121 @@ +/* A range adapter that wraps multiple ranges + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . = */ + +#ifndef GDBSUPPORT_RANGE_CHAIN_H +#define GDBSUPPORT_RANGE_CHAIN_H + +/* A range adapter that presents a number of ranges as if it were a + single range. That is, iterating over a range_chain will iterate + over each sub-range in order. */ +template +struct range_chain +{ + /* The type of the iterator that is created by this range. */ + class iterator + { + public: + + iterator (const std::vector &ranges, size_t idx) + : m_index (idx), + m_ranges (ranges) + { + skip_empty (); + } + + bool operator=3D=3D (const iterator &other) const + { + if (m_index !=3D other.m_index || &m_ranges !=3D &other.m_ranges) + return false; + if (m_current.has_value () !=3D other.m_current.has_value ()) + return false; + if (m_current.has_value ()) + return *m_current =3D=3D *other.m_current; + return true; + } + + bool operator!=3D (const iterator &other) const + { + return !(*this =3D=3D other); + } + + iterator &operator++ () + { + ++*m_current; + if (*m_current =3D=3D m_ranges[m_index].end ()) + { + ++m_index; + skip_empty (); + } + return *this; + } + + typename Range::iterator::value_type operator* () const + { + return **m_current; + } + + private: + /* Skip empty sub-ranges. If this finds a valid sub-range, + m_current is updated to point to its start; otherwise, + m_current is reset. */ + void skip_empty () + { + for (; m_index < m_ranges.size (); ++m_index) + { + m_current =3D m_ranges[m_index].begin (); + if (*m_current !=3D m_ranges[m_index].end ()) + return; + } + m_current.reset (); + } + + /* Index into the vector indicating where the current iterator + comes from. */ + size_t m_index; + /* The current iterator into one of the vector ranges. If no + value then this (outer) iterator is at the end of the overall + range. */ + gdb::optional m_current; + /* Vector of ranges. */ + const std::vector &m_ranges; + }; + + /* Create a new range_chain. */ + template + range_chain (T &&ranges) + : m_ranges (std::forward (ranges)) + { + } + + iterator begin () const + { + return iterator (m_ranges, 0); + } + + iterator end () const + { + return iterator (m_ranges, m_ranges.size ()); + } + +private: + + /* The sub-ranges. */ + std::vector m_ranges; +}; + +#endif /* GDBSUPPORT_RANGE_CHAIN_H */