0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 01:31: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 b1b3c7aac2
commit 67f6ac1628
3 changed files with 21 additions and 2 deletions

View File

@ -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, ...);

View 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;

View 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))
@ -817,6 +817,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);
@ -1362,6 +1363,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
@ -1369,6 +1375,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);
@ -1396,6 +1403,7 @@ birdloop_init(void)
timers_init(&main_birdloop.time, &root_pool);
birdloop_enter_locked(&main_birdloop);
this_birdloop = &main_birdloop;
this_thread = &main_thread;
}
@ -1442,6 +1450,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 */
@ -1462,6 +1471,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)
@ -1490,7 +1500,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);
@ -1508,6 +1518,7 @@ birdloop_run(void *_loop)
loop->sock_changed = 0;
account_to(&this_thread->overhead);
this_birdloop = this_thread->meta;
birdloop_leave(loop);
}