0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00

IO Loop: provide information about current loop and task time limit

This commit is contained in:
Maria Matejka 2024-04-03 12:05:02 +02:00
parent 5ba5cdd9f9
commit 178ea41e84
3 changed files with 21 additions and 2 deletions

View File

@ -16,6 +16,12 @@
extern struct birdloop main_birdloop; 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 */ /* Start a new birdloop owned by given pool and domain */
struct birdloop *birdloop_new(pool *p, uint order, btime max_latency, const char *fmt, ...); struct birdloop *birdloop_new(pool *p, uint order, btime max_latency, const char *fmt, ...);

View File

@ -117,6 +117,8 @@ void times_update(void);
void timers_init(struct timeloop *loop, pool *p); void timers_init(struct timeloop *loop, pool *p);
void timers_fire(struct timeloop *loop, int io_log); void timers_fire(struct timeloop *loop, int io_log);
/* For extra fine precision */
u64 ns_now(void);
struct timeformat { struct timeformat {
const char *fmt1, *fmt2; const char *fmt1, *fmt2;

View File

@ -51,7 +51,7 @@ static void ns_init(void)
#define NSEC_IN_SEC ((u64) (1000 * 1000 * 1000)) #define NSEC_IN_SEC ((u64) (1000 * 1000 * 1000))
static u64 ns_now(void) u64 ns_now(void)
{ {
struct timespec ts; struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts)) if (clock_gettime(CLOCK_MONOTONIC, &ts))
@ -789,6 +789,7 @@ bird_thread_main(void *arg)
account_to(&thr->overhead); account_to(&thr->overhead);
birdloop_enter(thr->meta); birdloop_enter(thr->meta);
this_birdloop = thr->meta;
tmp_init(thr->pool, birdloop_domain(thr->meta)); tmp_init(thr->pool, birdloop_domain(thr->meta));
init_list(&thr->loops); 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"); 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 * Birdloop
@ -1341,6 +1347,7 @@ cmd_show_threads(int show_loops)
static struct bird_thread main_thread; static struct bird_thread main_thread;
struct birdloop main_birdloop = { .thread = &main_thread, }; struct birdloop main_birdloop = { .thread = &main_thread, };
_Thread_local struct birdloop *this_birdloop;
static void birdloop_enter_locked(struct birdloop *loop); static void birdloop_enter_locked(struct birdloop *loop);
@ -1368,6 +1375,7 @@ birdloop_init(void)
timers_init(&main_birdloop.time, &root_pool); timers_init(&main_birdloop.time, &root_pool);
birdloop_enter_locked(&main_birdloop); birdloop_enter_locked(&main_birdloop);
this_birdloop = &main_birdloop;
this_thread = &main_thread; this_thread = &main_thread;
} }
@ -1414,6 +1422,7 @@ birdloop_stop_internal(struct birdloop *loop)
ASSERT_DIE(!ev_active(&loop->event)); ASSERT_DIE(!ev_active(&loop->event));
loop->ping_pending = 0; loop->ping_pending = 0;
account_to(&this_thread->overhead); account_to(&this_thread->overhead);
this_birdloop = this_thread->meta;
birdloop_leave(loop); birdloop_leave(loop);
/* Request local socket reload */ /* Request local socket reload */
@ -1434,6 +1443,7 @@ birdloop_run(void *_loop)
struct birdloop *loop = _loop; struct birdloop *loop = _loop;
account_to(&loop->locking); account_to(&loop->locking);
birdloop_enter(loop); birdloop_enter(loop);
this_birdloop = loop;
u64 dif = account_to(&loop->working); u64 dif = account_to(&loop->working);
if (dif > this_thread->max_loop_time_ns) if (dif > this_thread->max_loop_time_ns)
@ -1459,7 +1469,7 @@ birdloop_run(void *_loop)
repeat += ev_run_list(&loop->event_list); repeat += ev_run_list(&loop->event_list);
/* Check end time */ /* Check end time */
} while (repeat && (ns_now() < account_last + this_thread->max_loop_time_ns)); } while (repeat && task_still_in_limit());
/* Request meta timer */ /* Request meta timer */
timer *t = timers_first(&loop->time); timer *t = timers_first(&loop->time);
@ -1477,6 +1487,7 @@ birdloop_run(void *_loop)
loop->sock_changed = 0; loop->sock_changed = 0;
account_to(&this_thread->overhead); account_to(&this_thread->overhead);
this_birdloop = this_thread->meta;
birdloop_leave(loop); birdloop_leave(loop);
} }