From 284870b81acafca64c4ead4fa0704d01d54db06c Mon Sep 17 00:00:00 2001 From: Simon Kuenzer Date: Mon, 22 May 2023 18:02:50 +0200 Subject: [PATCH] lib/uksched: Interface for returning idle thread With the intention for accessing metrics (like thread execution time), this commit introduces an interface to return a reference to the idle thread. Signed-off-by: Simon Kuenzer Reviewed-by: Michalis Pappas Reviewed-by: Sergiu Moga Approved-by: Razvan Deaconescu Tested-by: Unikraft CI GitHub-Closes: #965 --- lib/uksched/include/uk/sched.h | 23 +++++++++++++++++++++++ lib/uksched/include/uk/sched_impl.h | 3 ++- lib/ukschedcoop/schedcoop.c | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/uksched/include/uk/sched.h b/lib/uksched/include/uk/sched.h index d2aaa542a..7d1a05c69 100644 --- a/lib/uksched/include/uk/sched.h +++ b/lib/uksched/include/uk/sched.h @@ -72,6 +72,9 @@ typedef void (*uk_sched_thread_blocked_func_t) typedef void (*uk_sched_thread_woken_func_t) (struct uk_sched *s, struct uk_thread *t); +typedef const struct uk_thread * (*uk_sched_idle_thread_func_t) + (struct uk_sched *s, unsigned int proc_id); + typedef int (*uk_sched_start_t)(struct uk_sched *s, struct uk_thread *main); struct uk_sched { @@ -81,6 +84,7 @@ struct uk_sched { uk_sched_thread_remove_func_t thread_remove; uk_sched_thread_blocked_func_t thread_blocked; uk_sched_thread_woken_func_t thread_woken; + uk_sched_idle_thread_func_t idle_thread; uk_sched_start_t sched_start; @@ -136,6 +140,25 @@ static inline void uk_sched_thread_woken(struct uk_thread *t) s->thread_woken(s, t); } +/** + * Returns the reference to the idle thread that is responsible for + * the processing unit `proc_id`. Please note that `proc_id` is not + * a logical core ID; it is a linear increasing number over the managed + * logical cores by the scheduler instance `s`. + * It is possible that a scheduler implementation does not operate with idle + * threads and returns `NULL`. + */ +static inline const struct uk_thread *uk_sched_idle_thread(struct uk_sched *s, + unsigned int proc_id) +{ + UK_ASSERT(s); + + if (unlikely(!s->idle_thread)) + return NULL; + + return s->idle_thread(s, proc_id); +} + /** * Create a main thread from current context and call thread starter function */ diff --git a/lib/uksched/include/uk/sched_impl.h b/lib/uksched/include/uk/sched_impl.h index bf09b90a3..613f585f1 100644 --- a/lib/uksched/include/uk/sched_impl.h +++ b/lib/uksched/include/uk/sched_impl.h @@ -55,7 +55,7 @@ int uk_sched_register(struct uk_sched *s); #define uk_sched_init(s, start_func, yield_func, \ thread_add_func, thread_remove_func, \ thread_blocked_func, thread_woken_func, \ - def_allocator) \ + idle_thread_func, def_allocator) \ do { \ (s)->sched_start = start_func; \ (s)->yield = yield_func; \ @@ -63,6 +63,7 @@ int uk_sched_register(struct uk_sched *s); (s)->thread_remove = thread_remove_func; \ (s)->thread_blocked = thread_blocked_func; \ (s)->thread_woken = thread_woken_func; \ + (s)->idle_thread = idle_thread_func; \ uk_sched_register((s)); \ \ (s)->a = (def_allocator); \ diff --git a/lib/ukschedcoop/schedcoop.c b/lib/ukschedcoop/schedcoop.c index 56d94881e..afa37c3d5 100644 --- a/lib/ukschedcoop/schedcoop.c +++ b/lib/ukschedcoop/schedcoop.c @@ -307,6 +307,7 @@ struct uk_sched *uk_schedcoop_create(struct uk_alloc *a) schedcoop_thread_remove, schedcoop_thread_blocked, schedcoop_thread_woken, + NULL, a); /* Add idle thread to the scheduler's thread list */ -- 2.39.5