aboutsummaryrefslogtreecommitdiff
path: root/gdb/thread.c
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2021-06-11 18:29:33 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2021-07-12 20:46:53 -0400
commit922cc93d5da6a6dc422b7e7a09ee745414d67457 (patch)
tree4963b5066e4c34a8b7b2331195c977254e55973e /gdb/thread.c
parent71a2349005e74e0d64554f5c88e3632f3ace167a (diff)
downloadgdb-922cc93d5da6a6dc422b7e7a09ee745414d67457.zip
gdb-922cc93d5da6a6dc422b7e7a09ee745414d67457.tar.gz
gdb-922cc93d5da6a6dc422b7e7a09ee745414d67457.tar.bz2
gdb: maintain ptid -> thread map, optimize find_thread_ptid
When debugging a large number of threads (thousands), looking up a thread by ptid_t using the inferior::thread_list linked list can add up. Add inferior::thread_map, an std::unordered_map indexed by ptid_t, and change the find_thread_ptid function to look up a thread using std::unordered_map::find, instead of iterating on all of the inferior's threads. This should make it faster to look up a thread from its ptid. Change-Id: I3a8da0a839e18dee5bb98b8b7dbeb7f3dfa8ae1c Co-Authored-By: Pedro Alves <pedro@palves.net>
Diffstat (limited to 'gdb/thread.c')
-rw-r--r--gdb/thread.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/gdb/thread.c b/gdb/thread.c
index 74d6b90..8f0584e 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -206,6 +206,14 @@ set_thread_exited (thread_info *tp, bool silent)
/* Clear breakpoints, etc. associated with this thread. */
clear_thread_inferior_resources (tp);
+
+ /* Remove from the ptid_t map. We don't want for
+ find_thread_ptid to find exited threads. Also, the target
+ may reuse the ptid for a new thread, and there can only be
+ one value per key; adding a new thread with the same ptid_t
+ would overwrite the exited thread's ptid entry. */
+ size_t nr_deleted = tp->inf->ptid_thread_map.erase (tp->ptid);
+ gdb_assert (nr_deleted == 1);
}
}
@@ -228,6 +236,11 @@ new_thread (struct inferior *inf, ptid_t ptid)
inf->thread_list.push_back (*tp);
+ /* A thread with this ptid should not exist in the map yet. */
+ gdb_assert (inf->ptid_thread_map.find (ptid) == inf->ptid_thread_map.end ());
+
+ inf->ptid_thread_map[ptid] = tp;
+
return tp;
}
@@ -484,11 +497,11 @@ find_thread_ptid (inferior *inf, ptid_t ptid)
{
gdb_assert (inf != nullptr);
- for (thread_info *tp : inf->non_exited_threads ())
- if (tp->ptid == ptid)
- return tp;
-
- return NULL;
+ auto it = inf->ptid_thread_map.find (ptid);
+ if (it != inf->ptid_thread_map.end ())
+ return it->second;
+ else
+ return nullptr;
}
/* See gdbthread.h. */
@@ -758,7 +771,13 @@ thread_change_ptid (process_stratum_target *targ,
inf->pid = new_ptid.pid ();
tp = find_thread_ptid (inf, old_ptid);
+ gdb_assert (tp != nullptr);
+
+ int num_erased = inf->ptid_thread_map.erase (old_ptid);
+ gdb_assert (num_erased == 1);
+
tp->ptid = new_ptid;
+ inf->ptid_thread_map[new_ptid] = tp;
gdb::observers::thread_ptid_changed.notify (targ, old_ptid, new_ptid);
}