]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
IO Loop: provide information about current loop and task time limit
authorMaria Matejka <mq@ucw.cz>
Wed, 3 Apr 2024 10:05:02 +0000 (12:05 +0200)
committerMaria Matejka <mq@ucw.cz>
Mon, 13 May 2024 06:52:48 +0000 (08:52 +0200)
lib/io-loop.h
lib/timer.h
sysdep/unix/io-loop.c

index ecae429d8c25018fc0c1d0173961543816735fa3..4b93919e38fd2b3b713cd8afec03b0dcb47d1a1b 100644 (file)
 
 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, ...);
 
index 34b53aa6f158d71d75d48b52c0dd54f5a57a02ba..7cdc5768cdd0c053efcc2f0d574bf8642c1dd00e 100644 (file)
@@ -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;
index c21d95031ebe417718dd3f507a8f581b4599db78..5f464a251696bbc308cf47647671fb7faa473e69 100644 (file)
@@ -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);
 }