binutils-gdb/gdbsupport/ptid.h
Simon Marchi 888bdb2b74 gdb: change regcache list to be a map
One regcache object is created for each stopped thread and is stored in
the regcache::regcaches linked list.  Looking up a regcache for a given
thread is therefore in O(number of threads).  Stopping all threads then
becomes O((number of threads) ^ 2).  Same goes for resuming a thread
(need to delete the regcache of a given ptid) and resuming all threads.
It becomes noticeable when debugging thousands of threads, which is
typical with GPU targets.  This patch replaces the linked list with some
maps to reduce that complexity.

The first design was using an std::unordered_map with (target, ptid,
arch) as the key, because that's how lookups are done (in
get_thread_arch_aspace_regcache).  However, the registers_changed_ptid
function, also somewhat on the hot path (it is used when resuming
threads), needs to delete all regcaches associated to a given (target,
ptid) tuple.  If the key of the map is (target, ptid, arch), we have to
walk all items of the map, not good.

The second design was therefore using an std::unordered_multimap with
(target, ptid) as the key.  One key could be associated to multiple
regcaches, all with different gdbarches.  When looking up, we would have
to walk all these regcaches.  This would be ok, because there will
usually be actually one matching regcache.  In the exceptional
multi-arch thread cases, there will be maybe two.  However, in
registers_changed_ptid, we sometimes need to remove all regcaches
matching a given target.  We would then have to talk all items of the
map again, not good.

The design as implemented in this patch therefore uses two levels of
map.  One std::unordered_map uses the target as the key.  The value type
is an std::unordered_multimap that itself uses the ptid as the key.  The
values of the multimap are the regcaches themselves.  Again, we expect
to have one or very few regcaches per (target, ptid).

So, in summary:

* The lookups (in get_thread_arch_aspace_regcache), become faster when
  the number of threads grows, compared to the linked list.  With a
  small number of threads, it will probably be a bit slower to do map
  lookups than to walk a few linked list nodes, but I don't think it
  will be noticeable in practice.

* The function registers_changed_ptid deletes all regcaches related to a
  given (target, ptid).  It must now handle the different cases separately:

    - NULL target and minus_one_ptid: we delete all the entries
    - NULL target and non-minus_one_ptid: invalid (checked by assert)
    - non-NULL target and non-minus_one_ptid: we delete all the entries
      associated to that tuple
    - a non-NULL target and minus_one_ptid: we delete all the entries
      associated to that target

* The function regcache_thread_ptid_changed is called when a thread
  changes ptid.  It is implemented efficiently using the map, although
  that's not very important: it is not called often, mostly when
  creating an inferior, on some specific platforms.

This patch is a tiny bit from ROCm GDB [1] we would like to merge
upstream.  Laurent Morichetti gave be these performance numbers:

The benchmark used is:

  time ./gdb --data-directory=data-directory /extra/lmoriche/hip/samples/0_Intro/bit_extract/bit_extract -ex "set pagination off" -ex "set breakpoint pending on" -ex "b bit_extract_kernel if \$_thread == 5" -ex run -ex c -batch

It measures the time it takes to continue from a conditional breakpoint with
2048 threads at that breakpoint, one of them reporting the breakpoint.

baseline:
real    0m10.227s
real    0m10.177s
real    0m10.362s

with patch:
real    0m8.356s
real    0m8.424s
real    0m8.494s

[1] https://github.com/ROCm-Developer-Tools/ROCgdb

gdb/ChangeLog:

	* regcache.c (ptid_regcache_map): New type.
	(target_ptid_regcache_map): New type.
	(regcaches): Change type to target_ptid_regcache_map.
	(get_thread_arch_aspace_regcache): Update to regcaches' new
	type.
	(regcache_thread_ptid_changed): Likewise.
	(registers_changed_ptid): Likewise.
	(regcaches_size): Likewise.
	(regcaches_test): Update.
	(regcache_thread_ptid_changed): Update.
	* regcache.h (regcache_up): New type.
	* gdbsupport/ptid.h (hash_ptid): New struct.

Change-Id: Iabb0a1111707936ca111ddb13f3b09efa83d3402
2020-08-07 11:29:00 -04:00

172 lines
4.8 KiB
C++

/* The ptid_t type and common functions operating on it.
Copyright (C) 1986-2020 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 COMMON_PTID_H
#define COMMON_PTID_H
/* The ptid struct is a collection of the various "ids" necessary for
identifying the inferior process/thread being debugged. This
consists of the process id (pid), lightweight process id (lwp) and
thread id (tid). When manipulating ptids, the constructors,
accessors, and predicates declared in this file should be used. Do
NOT access the struct ptid members directly.
process_stratum targets that handle threading themselves should
prefer using the ptid.lwp field, leaving the ptid.tid field for any
thread_stratum target that might want to sit on top.
*/
#include <functional>
class ptid_t
{
public:
/* Must have a trivial defaulted default constructor so that the
type remains POD. */
ptid_t () noexcept = default;
/* Make a ptid given the necessary PID, LWP, and TID components.
A ptid with only a PID (LWP and TID equal to zero) is usually used to
represent a whole process, including all its lwps/threads. */
explicit constexpr ptid_t (int pid, long lwp = 0, long tid = 0)
: m_pid (pid), m_lwp (lwp), m_tid (tid)
{}
/* Fetch the pid (process id) component from the ptid. */
constexpr int pid () const
{ return m_pid; }
/* Return true if the ptid's lwp member is non-zero. */
constexpr bool lwp_p () const
{ return m_lwp != 0; }
/* Fetch the lwp (lightweight process) component from the ptid. */
constexpr long lwp () const
{ return m_lwp; }
/* Return true if the ptid's tid member is non-zero. */
constexpr bool tid_p () const
{ return m_tid != 0; }
/* Fetch the tid (thread id) component from a ptid. */
constexpr long tid () const
{ return m_tid; }
/* Return true if the ptid represents a whole process, including all its
lwps/threads. Such ptids have the form of (pid, 0, 0), with
pid != -1. */
constexpr bool is_pid () const
{
return (*this != make_null ()
&& *this != make_minus_one ()
&& m_lwp == 0
&& m_tid == 0);
}
/* Compare two ptids to see if they are equal. */
constexpr bool operator== (const ptid_t &other) const
{
return (m_pid == other.m_pid
&& m_lwp == other.m_lwp
&& m_tid == other.m_tid);
}
/* Compare two ptids to see if they are different. */
constexpr bool operator!= (const ptid_t &other) const
{
return !(*this == other);
}
/* Return true if the ptid matches FILTER. FILTER can be the wild
card MINUS_ONE_PTID (all ptids match it); can be a ptid representing
a process (ptid.is_pid () returns true), in which case, all lwps and
threads of that given process match, lwps and threads of other
processes do not; or, it can represent a specific thread, in which
case, only that thread will match true. The ptid must represent a
specific LWP or THREAD, it can never be a wild card. */
constexpr bool matches (const ptid_t &filter) const
{
return (/* If filter represents any ptid, it's always a match. */
filter == make_minus_one ()
/* If filter is only a pid, any ptid with that pid
matches. */
|| (filter.is_pid () && m_pid == filter.pid ())
/* Otherwise, this ptid only matches if it's exactly equal
to filter. */
|| *this == filter);
}
/* Make a null ptid. */
static constexpr ptid_t make_null ()
{ return ptid_t (0, 0, 0); }
/* Make a minus one ptid. */
static constexpr ptid_t make_minus_one ()
{ return ptid_t (-1, 0, 0); }
private:
/* Process id. */
int m_pid;
/* Lightweight process id. */
long m_lwp;
/* Thread id. */
long m_tid;
};
/* Functor to hash a ptid. */
struct hash_ptid
{
size_t operator() (const ptid_t &ptid) const
{
std::hash<long> long_hash;
return (long_hash (ptid.pid ())
+ long_hash (ptid.lwp ())
+ long_hash (ptid.tid ()));
}
};
/* The null or zero ptid, often used to indicate no process. */
extern const ptid_t null_ptid;
/* The (-1,0,0) ptid, often used to indicate either an error condition
or a "don't care" condition, i.e, "run all threads." */
extern const ptid_t minus_one_ptid;
#endif /* COMMON_PTID_H */