From: Maria Matejka Date: Wed, 3 Apr 2024 10:05:02 +0000 (+0200) Subject: IO Loop: provide information about current loop and task time limit X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=178ea41e84cb702681f3b28cfe613ec83bfd6790;p=thirdparty%2Fbird.git IO Loop: provide information about current loop and task time limit --- diff --git a/lib/io-loop.h b/lib/io-loop.h index ecae429d8..4b93919e3 100644 --- a/lib/io-loop.h +++ b/lib/io-loop.h @@ -16,6 +16,12 @@ extern struct birdloop main_birdloop; +/* Currently running birdloop */ +extern _Thread_local struct birdloop *this_birdloop; + +/* Check that the task has enough time to do a bit more */ +_Bool task_still_in_limit(void); + /* Start a new birdloop owned by given pool and domain */ struct birdloop *birdloop_new(pool *p, uint order, btime max_latency, const char *fmt, ...); diff --git a/lib/timer.h b/lib/timer.h index 34b53aa6f..7cdc5768c 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -117,6 +117,8 @@ void times_update(void); void timers_init(struct timeloop *loop, pool *p); void timers_fire(struct timeloop *loop, int io_log); +/* For extra fine precision */ +u64 ns_now(void); struct timeformat { const char *fmt1, *fmt2; diff --git a/sysdep/unix/io-loop.c b/sysdep/unix/io-loop.c index c21d95031..5f464a251 100644 --- a/sysdep/unix/io-loop.c +++ b/sysdep/unix/io-loop.c @@ -51,7 +51,7 @@ static void ns_init(void) #define NSEC_IN_SEC ((u64) (1000 * 1000 * 1000)) -static u64 ns_now(void) +u64 ns_now(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) @@ -789,6 +789,7 @@ bird_thread_main(void *arg) account_to(&thr->overhead); birdloop_enter(thr->meta); + this_birdloop = thr->meta; tmp_init(thr->pool, birdloop_domain(thr->meta)); init_list(&thr->loops); @@ -1334,6 +1335,11 @@ cmd_show_threads(int show_loops) bird_thread_sync_all(&tsd->sync, bird_thread_show, cmd_show_threads_done, "Show Threads"); } +_Bool task_still_in_limit(void) +{ + return ns_now() < account_last + this_thread->max_loop_time_ns; +} + /* * Birdloop @@ -1341,6 +1347,7 @@ cmd_show_threads(int show_loops) static struct bird_thread main_thread; struct birdloop main_birdloop = { .thread = &main_thread, }; +_Thread_local struct birdloop *this_birdloop; static void birdloop_enter_locked(struct birdloop *loop); @@ -1368,6 +1375,7 @@ birdloop_init(void) timers_init(&main_birdloop.time, &root_pool); birdloop_enter_locked(&main_birdloop); + this_birdloop = &main_birdloop; this_thread = &main_thread; } @@ -1414,6 +1422,7 @@ birdloop_stop_internal(struct birdloop *loop) ASSERT_DIE(!ev_active(&loop->event)); loop->ping_pending = 0; account_to(&this_thread->overhead); + this_birdloop = this_thread->meta; birdloop_leave(loop); /* Request local socket reload */ @@ -1434,6 +1443,7 @@ birdloop_run(void *_loop) struct birdloop *loop = _loop; account_to(&loop->locking); birdloop_enter(loop); + this_birdloop = loop; u64 dif = account_to(&loop->working); if (dif > this_thread->max_loop_time_ns) @@ -1459,7 +1469,7 @@ birdloop_run(void *_loop) repeat += ev_run_list(&loop->event_list); /* Check end time */ - } while (repeat && (ns_now() < account_last + this_thread->max_loop_time_ns)); + } while (repeat && task_still_in_limit()); /* Request meta timer */ timer *t = timers_first(&loop->time); @@ -1477,6 +1487,7 @@ birdloop_run(void *_loop) loop->sock_changed = 0; account_to(&this_thread->overhead); + this_birdloop = this_thread->meta; birdloop_leave(loop); }