From 6a68d7cb5af8f1985478754346df813939f1ce9c Mon Sep 17 00:00:00 2001 From: Katerina Kubecova Date: Wed, 18 Dec 2024 14:24:25 +0100 Subject: [PATCH] Threads: assigning IDs from hmap For certain upcoming data structures, we actually need to use thread IDs as a functional information to index things, not just a logging token. Thus, we need them to be dense, not just flying around as they were until now. To achieve this, we assign the IDs from a global hmap when the threads are started, and properly return them when the threads are finished. This way, the IDs of stopping threads are expected to be recycled, whereas until now it wasn't expected to happen. You may need to take care about this in your log reading apparatus. Also there is now a maximum thread count hard limit because unlimited thread count is too crazy to handle. But the limit is still ridiculously high and nobody is ever expected to hit it anyway. --- lib/birdlib.h | 2 +- lib/io-loop.h | 2 ++ sysdep/unix/io-loop.c | 40 ++++++++++++++++++++++++++-------------- sysdep/unix/io-loop.h | 1 + sysdep/unix/main.c | 1 + 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/birdlib.h b/lib/birdlib.h index 5a004d9a2..db3a83574 100644 --- a/lib/birdlib.h +++ b/lib/birdlib.h @@ -234,7 +234,7 @@ void debug_safe(const char *msg); /* Printf to debug output, async-safe */ /* Internal thread ID, useful for logging */ extern _Atomic uint max_thread_id; extern _Thread_local uint this_thread_id; -#define THIS_THREAD_ID (this_thread_id ?: (this_thread_id = atomic_fetch_add_explicit(&max_thread_id, 1, memory_order_acq_rel))) +#define THIS_THREAD_ID this_thread_id /* Debugging */ diff --git a/lib/io-loop.h b/lib/io-loop.h index d19d3fe5a..8c2a6cfbd 100644 --- a/lib/io-loop.h +++ b/lib/io-loop.h @@ -17,6 +17,8 @@ extern struct birdloop main_birdloop; +#define MAX_THREADS 256 + /* Currently running birdloop */ extern _Thread_local struct birdloop *this_birdloop; diff --git a/sysdep/unix/io-loop.c b/sysdep/unix/io-loop.c index 82f9782d5..deda2b40c 100644 --- a/sysdep/unix/io-loop.c +++ b/sysdep/unix/io-loop.c @@ -24,6 +24,7 @@ #include "lib/event.h" #include "lib/timer.h" #include "lib/socket.h" +#include "lib/bitmap.h" #include "lib/io-loop.h" #include "sysdep/unix/io-loop.h" @@ -937,10 +938,13 @@ bird_thread_busy_set(struct thread_group_private *group, int val) ASSERT_DIE(group->thread_busy_count <= group->thread_count); } +struct hmap hmap_thread_ids; + static void * bird_thread_main(void *arg) { struct bird_thread *thr = this_thread = arg; + this_thread_id = thr->id; rcu_thread_start(); @@ -1161,21 +1165,9 @@ bird_thread_cleanup(void *_thr) if (leftover_loops) /* Happens only with the last thread */ bird_thread_group_done(gpub, leftover_loops); -} -static void bird_thread_start(thread_group *); -static void -bird_thread_start_event(void *_data) -{ - thread_group *group = _data; - bird_thread_start(group); -} - -static void -bird_thread_start_indirect(struct thread_group_private *group) -{ - group->thread_dropper.event.hook = bird_thread_start_event; - ev_send(&global_event_list, &group->thread_dropper.event); + /* Return the thread ID */ + hmap_clear(&hmap_thread_ids, thr->id); } static void @@ -1205,6 +1197,10 @@ bird_thread_start(thread_group *gpub) thr->meta = meta; thr->meta->thread = thr; + thr->id = hmap_first_zero(&hmap_thread_ids); + hmap_set(&hmap_thread_ids, thr->id); + thr->id++; + wakeup_init(thr); ev_init_list(&thr->priority_events, NULL, "Thread direct event list"); @@ -1234,6 +1230,20 @@ bird_thread_start(thread_group *gpub) } } +static void +bird_thread_start_event(void *_data) +{ + thread_group *group = _data; + bird_thread_start(group); +} + +static void +bird_thread_start_indirect(struct thread_group_private *group) +{ + group->thread_dropper.event.hook = bird_thread_start_event; + ev_send(&global_event_list, &group->thread_dropper.event); +} + static void bird_thread_dropper_free(void *data) { @@ -1780,6 +1790,8 @@ birdloop_init(void) this_birdloop = &main_birdloop; this_thread = &main_thread; + hmap_init(&hmap_thread_ids, &root_pool, 1024); + defer_init(lp_new(&root_pool)); } diff --git a/sysdep/unix/io-loop.h b/sysdep/unix/io-loop.h index 6681f4d6d..51418b90f 100644 --- a/sysdep/unix/io-loop.h +++ b/sysdep/unix/io-loop.h @@ -90,6 +90,7 @@ struct bird_thread #define TLIST_WANT_WALK #define TLIST_WANT_ADD_TAIL TLIST_DEFAULT_NODE; + uint id; struct pipe wakeup; event_list priority_events; diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 2e5987f97..971ff8fca 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -41,6 +41,7 @@ #include "conf/conf.h" #include "filter/filter.h" #include "filter/data.h" +#include "lib/io-loop.h" #include "unix.h" #include "krt.h" -- 2.47.2