From ab6bfbca217c379b5162cae54a26fd55fa0e7b49 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/config.Y | 1 + sysdep/unix/io-loop.c | 13 +++++++++++++ sysdep/unix/io-loop.h | 1 + sysdep/unix/main.c | 1 + 6 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/birdlib.h b/lib/birdlib.h index bc7c334cb..165dc9821 100644 --- a/lib/birdlib.h +++ b/lib/birdlib.h @@ -232,7 +232,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 ce9002057..1b70a6167 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/config.Y b/sysdep/unix/config.Y index 42d78c6f6..6e2452c42 100644 --- a/sysdep/unix/config.Y +++ b/sysdep/unix/config.Y @@ -147,6 +147,7 @@ cli_opts_block: conf: THREADS expr { if ($2 < 1) cf_error("Number of threads must be at least one."); + if ($2 > MAX_THREADS) cf_error("Number of threads must not be higher than %i.", MAX_THREADS); new_config->thread_count = $2; } diff --git a/sysdep/unix/io-loop.c b/sysdep/unix/io-loop.c index 75d863757..7e4e2a80a 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" @@ -840,10 +841,13 @@ bird_thread_busy_set(struct bird_thread *thr, int val) UNLOCK_DOMAIN(attrs, thr->group->domain); } +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(); @@ -1013,6 +1017,9 @@ bird_thread_cleanup(void *_thr) thr->meta->thread = NULL; thr->meta = NULL; birdloop_free(meta); + + /* Return the thread ID */ + hmap_clear(&hmap_thread_ids, thr->id); } static struct bird_thread * @@ -1034,6 +1041,10 @@ bird_thread_start(struct birdloop_pickup_group *group) 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"); @@ -1505,6 +1516,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 f7fde411b..f17f87b35 100644 --- a/sysdep/unix/io-loop.h +++ b/sysdep/unix/io-loop.h @@ -76,6 +76,7 @@ struct birdloop struct bird_thread { node n; + 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