[PATCH v1 19/26] perf threads: Use generic hashmap for thread shards

From: Ian Rogers

Date: Fri Nov 13 2026 - 16:55:31 EST


Convert threads_table_entry::shard in util/threads.{c,h} to
DECLARE_HASHMAP(threads_map, pid_t, struct thread *) and replace
hashmap__for_each_entry{,_safe}() loops with hashmap__for_each{,_safe}().

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/threads.c | 16 ++++++++--------
tools/perf/util/threads.h | 4 +++-
2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c
index 98cf7edaf748..c2c675d064b3 100644
--- a/tools/perf/util/threads.c
+++ b/tools/perf/util/threads.c
@@ -136,15 +136,15 @@ void threads__remove_all_threads(struct threads *threads)
{
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
struct threads_table_entry *table = &threads->table[i];
- struct hashmap_entry *cur, *tmp;
- size_t bkt;
+ pid_t tid;
+ struct thread *val;

down_write(&table->lock);
__threads_table_entry__set_last_match(table, NULL);
- hashmap__for_each_entry_safe(&table->shard, cur, tmp, bkt) {
+ hashmap__for_each_safe(&table->shard, tid, val) {
struct thread *old_value = NULL;

- if (hashmap__delete(&table->shard, cur->key, /*old_key=*/NULL, &old_value))
+ if (hashmap__delete(&table->shard, tid, /*old_key=*/NULL, &old_value))
thread__put(old_value);
}
up_write(&table->lock);
@@ -171,12 +171,12 @@ int threads__for_each_thread(struct threads *threads,
{
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
struct threads_table_entry *table = &threads->table[i];
- struct hashmap_entry *cur;
- size_t bkt;
+ pid_t tid;
+ struct thread *th;

down_read(&table->lock);
- hashmap__for_each_entry(&table->shard, cur, bkt) {
- int rc = fn((struct thread *)cur->pvalue, data);
+ hashmap__for_each(&table->shard, tid, th) {
+ int rc = fn(th, data);

if (rc != 0) {
up_read(&table->lock);
diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h
index da68d2223f18..059d7a617e63 100644
--- a/tools/perf/util/threads.h
+++ b/tools/perf/util/threads.h
@@ -10,9 +10,11 @@ struct thread;
#define THREADS__TABLE_BITS 3
#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS)

+DECLARE_HASHMAP(threads_map, pid_t, struct thread *);
+
struct threads_table_entry {
/* Key is tid, value is struct thread. */
- struct hashmap shard;
+ struct threads_map shard;
struct rw_semaphore lock;
struct thread *last_match;
};
--
2.56.1.703.gf848ed807c-goog