Finalize each cooked index separately

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.

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.

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.

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.

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.

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.

Before:

    $ /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 28.
    2.00

After:

    $ /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 28.
    0.65

Regression tested on x86-64 Fedora 34.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29105
This commit is contained in:
Tom Tromey
2022-05-13 13:46:52 -06:00
parent f420c9c84e
commit 20a26f4e01
3 changed files with 355 additions and 205 deletions

View File

@@ -112,14 +112,180 @@ cooked_index::add (sect_offset die_offset, enum dwarf_tag tag,
return result;
}
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. */
void
cooked_index::finalize ()
{
m_future = gdb::thread_pool::g_thread_pool->post_task ([this] ()
{
do_finalize ();
});
}
/* See cooked-index.h. */
gdb::unique_xmalloc_ptr<char>
cooked_index::handle_gnat_encoded_entry (cooked_index_entry *entry,
htab_t gnat_entries)
{
std::string canonical = ada_decode (entry->name, false, false);
if (canonical.empty ())
return {};
std::vector<gdb::string_view> names = split_name (canonical.c_str (),
split_style::DOT);
gdb::string_view tail = names.back ();
names.pop_back ();
const cooked_index_entry *parent = nullptr;
for (const auto &name : names)
{
uint32_t hashval = dwarf5_djb_hash (name);
void **slot = htab_find_slot_with_hash (gnat_entries, &name,
hashval, INSERT);
/* CUs are processed in order, so we only need to check the most
recent entry. */
cooked_index_entry *last = (cooked_index_entry *) *slot;
if (last == nullptr || last->per_cu != entry->per_cu)
{
gdb::unique_xmalloc_ptr<char> new_name
= make_unique_xstrndup (name.data (), name.length ());
last = create (entry->die_offset, DW_TAG_namespace,
0, new_name.get (), parent,
entry->per_cu);
last->canonical = last->name;
m_names.push_back (std::move (new_name));
*slot = last;
}
parent = last;
}
entry->parent_entry = parent;
return make_unique_xstrndup (tail.data (), tail.length ());
}
/* See cooked-index.h. */
void
cooked_index::do_finalize ()
{
auto hash_name_ptr = [] (const void *p)
{
const cooked_index_entry *entry = (const cooked_index_entry *) p;
return htab_hash_pointer (entry->name);
};
auto eq_name_ptr = [] (const void *a, const void *b) -> int
{
const cooked_index_entry *ea = (const cooked_index_entry *) a;
const cooked_index_entry *eb = (const cooked_index_entry *) b;
return ea->name == eb->name;
};
/* We can use pointer equality here because names come from
.debug_str, which will normally be unique-ified by the linker.
Also, duplicates are relatively harmless -- they just mean a bit
of extra memory is used. */
htab_up seen_names (htab_create_alloc (10, hash_name_ptr, eq_name_ptr,
nullptr, xcalloc, xfree));
htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry,
nullptr, xcalloc, xfree));
for (cooked_index_entry *entry : m_entries)
{
gdb_assert (entry->canonical == nullptr);
if ((entry->per_cu->lang != language_cplus
&& entry->per_cu->lang != language_ada)
|| (entry->flags & IS_LINKAGE) != 0)
entry->canonical = entry->name;
else
{
if (entry->per_cu->lang == language_ada)
{
gdb::unique_xmalloc_ptr<char> canon_name
= handle_gnat_encoded_entry (entry, gnat_entries.get ());
if (canon_name == nullptr)
entry->canonical = entry->name;
else
{
entry->canonical = canon_name.get ();
m_names.push_back (std::move (canon_name));
}
}
else
{
void **slot = htab_find_slot (seen_names.get (), entry,
INSERT);
if (*slot == nullptr)
{
gdb::unique_xmalloc_ptr<char> canon_name
= cp_canonicalize_string (entry->name);
if (canon_name == nullptr)
entry->canonical = entry->name;
else
{
entry->canonical = canon_name.get ();
m_names.push_back (std::move (canon_name));
}
}
else
{
const cooked_index_entry *other
= (const cooked_index_entry *) *slot;
entry->canonical = other->canonical;
}
}
}
}
m_names.shrink_to_fit ();
m_entries.shrink_to_fit ();
std::sort (m_entries.begin (), m_entries.end (),
[] (const cooked_index_entry *a, const cooked_index_entry *b)
{
return *a < *b;
});
}
/* See cooked-index.h. */
cooked_index::range
cooked_index::find (gdb::string_view name, bool completing)
{
wait ();
auto lower = std::lower_bound (m_entries.begin (), m_entries.end (),
name,
[=] (const cooked_index_entry *entry,
const gdb::string_view &n)
{
int cmp = strncasecmp (entry->canonical, n.data (), n.length ());
if (cmp != 0 || completing)
return cmp < 0;
return strlen (entry->canonical) < n.length ();
});
auto upper = std::upper_bound (m_entries.begin (), m_entries.end (),
name,
[=] (const gdb::string_view &n,
const cooked_index_entry *entry)
{
int cmp = strncasecmp (n.data (), entry->canonical, n.length ());
if (cmp != 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. */
@@ -152,75 +318,11 @@ cooked_index_vector::get_addrmaps ()
cooked_index_vector::range
cooked_index_vector::find (gdb::string_view name, bool completing)
{
m_future.wait ();
auto lower = std::lower_bound (m_entries.begin (), m_entries.end (),
name,
[=] (const cooked_index_entry *entry,
const gdb::string_view &n)
{
int cmp = strncasecmp (entry->canonical, n.data (), n.length ());
if (cmp != 0 || completing)
return cmp < 0;
return strlen (entry->canonical) < n.length ();
});
auto upper = std::upper_bound (m_entries.begin (), m_entries.end (),
name,
[=] (const gdb::string_view &n,
const cooked_index_entry *entry)
{
int cmp = strncasecmp (n.data (), entry->canonical, n.length ());
if (cmp != 0 || completing)
return cmp < 0;
return n.length () < strlen (entry->canonical);
});
return range (lower, upper);
}
/* See cooked-index.h. */
gdb::unique_xmalloc_ptr<char>
cooked_index_vector::handle_gnat_encoded_entry (cooked_index_entry *entry,
htab_t gnat_entries)
{
std::string canonical = ada_decode (entry->name, false, false);
if (canonical.empty ())
return {};
std::vector<gdb::string_view> names = split_name (canonical.c_str (),
split_style::DOT);
gdb::string_view tail = names.back ();
names.pop_back ();
const cooked_index_entry *parent = nullptr;
for (const auto &name : names)
{
uint32_t hashval = dwarf5_djb_hash (name);
void **slot = htab_find_slot_with_hash (gnat_entries, &name,
hashval, INSERT);
/* CUs are processed in order, so we only need to check the most
recent entry. */
cooked_index_entry *last = (cooked_index_entry *) *slot;
if (last == nullptr || last->per_cu != entry->per_cu)
{
gdb::unique_xmalloc_ptr<char> new_name
= 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 = m_vector[0]->create (entry->die_offset, DW_TAG_namespace,
0, new_name.get (), parent,
entry->per_cu);
last->canonical = last->name;
m_names.push_back (std::move (new_name));
*slot = last;
}
parent = last;
}
entry->parent_entry = parent;
return make_unique_xstrndup (tail.data (), tail.length ());
std::vector<cooked_index::range> 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. */
@@ -242,100 +344,3 @@ cooked_index_vector::get_main () const
return result;
}
/* See cooked-index.h. */
void
cooked_index_vector::finalize ()
{
auto hash_name_ptr = [] (const void *p)
{
const cooked_index_entry *entry = (const cooked_index_entry *) p;
return htab_hash_pointer (entry->name);
};
auto eq_name_ptr = [] (const void *a, const void *b) -> int
{
const cooked_index_entry *ea = (const cooked_index_entry *) a;
const cooked_index_entry *eb = (const cooked_index_entry *) b;
return ea->name == eb->name;
};
/* We can use pointer equality here because names come from
.debug_str, which will normally be unique-ified by the linker.
Also, duplicates are relatively harmless -- they just mean a bit
of extra memory is used. */
htab_up seen_names (htab_create_alloc (10, hash_name_ptr, eq_name_ptr,
nullptr, xcalloc, xfree));
for (auto &index : m_vector)
{
htab_up gnat_entries (htab_create_alloc (10, hash_entry, eq_entry,
nullptr, xcalloc, xfree));
std::vector<cooked_index_entry *> entries
= std::move (index->m_entries);
for (cooked_index_entry *entry : entries)
{
gdb_assert (entry->canonical == nullptr);
if ((entry->per_cu->lang != language_cplus
&& entry->per_cu->lang != language_ada)
|| (entry->flags & IS_LINKAGE) != 0)
entry->canonical = entry->name;
else
{
if (entry->per_cu->lang == language_ada)
{
gdb::unique_xmalloc_ptr<char> canon_name
= handle_gnat_encoded_entry (entry, gnat_entries.get ());
if (canon_name == nullptr)
entry->canonical = entry->name;
else
{
entry->canonical = canon_name.get ();
m_names.push_back (std::move (canon_name));
}
}
else
{
void **slot = htab_find_slot (seen_names.get (), entry,
INSERT);
if (*slot == nullptr)
{
gdb::unique_xmalloc_ptr<char> canon_name
= cp_canonicalize_string (entry->name);
if (canon_name == nullptr)
entry->canonical = entry->name;
else
{
entry->canonical = canon_name.get ();
m_names.push_back (std::move (canon_name));
}
}
else
{
const cooked_index_entry *other
= (const cooked_index_entry *) *slot;
entry->canonical = other->canonical;
}
}
}
}
if (m_entries.empty ())
m_entries = std::move (entries);
else
{
m_entries.reserve (m_entries.size () + entries.size ());
m_entries.insert (m_entries.end (), entries.begin (), entries.end ());
}
}
m_names.shrink_to_fit ();
m_entries.shrink_to_fit ();
std::sort (m_entries.begin (), m_entries.end (),
[] (const cooked_index_entry *a, const cooked_index_entry *b)
{
return *a < *b;
});
}

View File

@@ -33,6 +33,7 @@
#include "gdbsupport/thread-pool.h"
#include "dwarf2/mapped-index.h"
#include "dwarf2/tag.h"
#include "gdbsupport/range-chain.h"
struct dwarf2_per_cu_data;
@@ -190,8 +191,34 @@ public:
m_addrmap = addrmap_create_fixed (map, &m_storage);
}
/* 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;
/* A simple range over part of m_entries. */
typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
/* 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:
/* Return the entry that is believed to represent the program's
@@ -223,6 +250,17 @@ private:
per_cu);
}
/* 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<char> 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 = nullptr;
/* Storage for canonical names. */
std::vector<gdb::unique_xmalloc_ptr<char>> 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<void> m_future;
};
/* The main index of DIEs. The parallel DIE indexers create
@@ -253,17 +297,18 @@ public:
~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 ();
}
/* A simple range over part of m_entries. */
typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
/* A range over a vector of subranges. */
typedef range_chain<cooked_index::range> range;
/* 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<cooked_index::range> 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));
}
/* Look up ADDR in the address map, and return either the
@@ -299,33 +347,9 @@ public:
private:
/* 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<char> 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<cooked_index_entry *> m_entries;
/* Storage for canonical names. */
std::vector<gdb::unique_xmalloc_ptr<char>> 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<void> m_future;
};
#endif /* GDB_DWARF2_COOKED_INDEX_H */

121
gdbsupport/range-chain.h Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>. */
#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<typename Range>
struct range_chain
{
/* The type of the iterator that is created by this range. */
class iterator
{
public:
iterator (const std::vector<Range> &ranges, size_t idx)
: m_index (idx),
m_ranges (ranges)
{
skip_empty ();
}
bool operator== (const iterator &other) const
{
if (m_index != other.m_index || &m_ranges != &other.m_ranges)
return false;
if (m_current.has_value () != other.m_current.has_value ())
return false;
if (m_current.has_value ())
return *m_current == *other.m_current;
return true;
}
bool operator!= (const iterator &other) const
{
return !(*this == other);
}
iterator &operator++ ()
{
++*m_current;
if (*m_current == 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 = m_ranges[m_index].begin ();
if (*m_current != 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<typename Range::iterator> m_current;
/* Vector of ranges. */
const std::vector<Range> &m_ranges;
};
/* Create a new range_chain. */
template<typename T>
range_chain (T &&ranges)
: m_ranges (std::forward<T> (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<Range> m_ranges;
};
#endif /* GDBSUPPORT_RANGE_CHAIN_H */