From 3d62fa05b8fe4372c81966ded0f05cbb57987268 Mon Sep 17 00:00:00 2001 From: francisco Date: Wed, 3 Jul 2019 15:12:03 +0200 Subject: [PATCH 1/2] core/schedstatistics: refactor - add init_schedstatistics function to be called after auto_init, that way xtimer_is init is called before the first call to xtimer_now - register schedstatics code as a callback to be executed on each sched_run() --- core/include/sched.h | 8 +++++- core/kernel_init.c | 9 ------ core/sched.c | 60 ++++++++++++++++++++++++--------------- sys/auto_init/auto_init.c | 7 +++++ 4 files changed, 51 insertions(+), 33 deletions(-) diff --git a/core/include/sched.h b/core/include/sched.h index 906f3c46bd30..4b8c56b6ace9 100644 --- a/core/include/sched.h +++ b/core/include/sched.h @@ -219,12 +219,18 @@ typedef struct { */ extern schedstat_t sched_pidlist[KERNEL_PID_LAST + 1]; +/** + * @brief Registers the sched statistics callback and sets laststart for + * caller thread + */ +void init_schedstatistics(void); + /** * @brief Register a callback that will be called on every scheduler run * * @param[in] callback The callback functions the will be called */ -void sched_register_cb(void (*callback)(uint32_t, uint32_t)); +void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t)); #endif /* MODULE_SCHEDSTATISTICS */ #ifdef __cplusplus diff --git a/core/kernel_init.c b/core/kernel_init.c index 1b153b38ad27..cab06a3ec123 100644 --- a/core/kernel_init.c +++ b/core/kernel_init.c @@ -29,10 +29,6 @@ #include "periph/pm.h" -#ifdef MODULE_SCHEDSTATISTICS -#include "sched.h" -#endif - #define ENABLE_DEBUG (0) #include "debug.h" @@ -49,11 +45,6 @@ static void *main_trampoline(void *arg) auto_init(); #endif -#ifdef MODULE_SCHEDSTATISTICS - schedstat_t *ss = &sched_pidlist[thread_getpid()]; - ss->laststart = 0; -#endif - LOG_INFO("main(): This is RIOT! (Version: " RIOT_VERSION ")\n"); main(); diff --git a/core/sched.c b/core/sched.c index 40eded9eef72..0da844bb2b9d 100644 --- a/core/sched.c +++ b/core/sched.c @@ -75,7 +75,7 @@ const uint8_t _tcb_name_offset = offsetof(thread_t, name); #endif #ifdef MODULE_SCHEDSTATISTICS -static void (*sched_cb) (uint32_t timestamp, uint32_t value) = NULL; +static void (*sched_cb) (kernel_pid_t active_thread, kernel_pid_t next_thread) = NULL; schedstat_t sched_pidlist[KERNEL_PID_LAST + 1]; #endif @@ -100,10 +100,6 @@ int __attribute__((used)) sched_run(void) return 0; } -#ifdef MODULE_SCHEDSTATISTICS - uint32_t now = xtimer_now().ticks32; -#endif - if (active_thread) { if (active_thread->status == STATUS_RUNNING) { active_thread->status = STATUS_PENDING; @@ -114,21 +110,14 @@ int __attribute__((used)) sched_run(void) LOG_WARNING("scheduler(): stack overflow detected, pid=%" PRIkernel_pid "\n", active_thread->pid); } #endif - -#ifdef MODULE_SCHEDSTATISTICS - schedstat_t *active_stat = &sched_pidlist[active_thread->pid]; - if (active_stat->laststart) { - active_stat->runtime_ticks += now - active_stat->laststart; - } -#endif } #ifdef MODULE_SCHEDSTATISTICS - schedstat_t *next_stat = &sched_pidlist[next_thread->pid]; - next_stat->laststart = now; - next_stat->schedules++; if (sched_cb) { - sched_cb(now, next_thread->pid); + /* Use `sched_active_pid` instead of `active_thread` since after `sched_task_exit()` is + called `active_thread` is set to NULL while `sched_active_thread` isn't updated until + `next_thread` is scheduled*/ + sched_cb(sched_active_pid, next_thread->pid); } #endif @@ -151,13 +140,6 @@ int __attribute__((used)) sched_run(void) return 1; } -#ifdef MODULE_SCHEDSTATISTICS -void sched_register_cb(void (*callback)(uint32_t, uint32_t)) -{ - sched_cb = callback; -} -#endif - void sched_set_status(thread_t *process, thread_status_t status) { if (status >= STATUS_ON_RUNQUEUE) { @@ -221,3 +203,35 @@ NORETURN void sched_task_exit(void) sched_active_thread = NULL; cpu_switch_context_exit(); } + +#ifdef MODULE_SCHEDSTATISTICS +void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t)) +{ + sched_cb = callback; +} + +void sched_statistics_cb(kernel_pid_t active_thread, kernel_pid_t next_thread) +{ + uint32_t now = xtimer_now().ticks32; + + /* Update active thread runtime, there is always an active thread since + first sched_run happens when main_trampoline gets scheduled */ + schedstat_t *active_stat = &sched_pidlist[active_thread]; + active_stat->runtime_ticks += now - active_stat->laststart; + + /* Update next_thread stats */ + schedstat_t *next_stat = &sched_pidlist[next_thread]; + next_stat->laststart = now; + next_stat->schedules++; +} + +void init_schedstatistics(void) +{ + /* Init laststart for the thread starting schedstatistics since the callback + wasn't registered when it was first scheduled */ + schedstat_t *active_stat = &sched_pidlist[sched_active_pid]; + active_stat->laststart = xtimer_now().ticks32; + active_stat->schedules = 1; + sched_register_cb(sched_statistics_cb); +} +#endif diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 0d2af83aeb81..046f37cfa09e 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -92,6 +92,10 @@ #include "net/sock/dtls.h" #endif +#ifdef MODULE_SCHEDSTATISTICS +#include "sched.h" +#endif + #define ENABLE_DEBUG (0) #include "debug.h" @@ -105,6 +109,9 @@ void auto_init(void) DEBUG("Auto init xtimer module.\n"); xtimer_init(); #endif +#ifdef MODULE_SCHEDSTATISTICS + init_schedstatistics(); +#endif #ifdef MODULE_MCI DEBUG("Auto init mci module.\n"); mci_initialize(); From fc58ebbd97f0b5a2fa74efa9246a15927157da19 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Tue, 10 Sep 2019 17:04:31 +0200 Subject: [PATCH 2/2] core/sched: separate sched_cb from schedstatistics --- Makefile.dep | 1 + core/include/sched.h | 4 +++- core/sched.c | 10 +++++++--- makefiles/pseudomodules.inc.mk | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Makefile.dep b/Makefile.dep index a75565ca1692..8df111494116 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -610,6 +610,7 @@ endif ifneq (,$(filter schedstatistics,$(USEMODULE))) USEMODULE += xtimer + USEMODULE += sched_cb endif ifneq (,$(filter arduino,$(USEMODULE))) diff --git a/core/include/sched.h b/core/include/sched.h index 4b8c56b6ace9..7c94a5d6d483 100644 --- a/core/include/sched.h +++ b/core/include/sched.h @@ -224,14 +224,16 @@ extern schedstat_t sched_pidlist[KERNEL_PID_LAST + 1]; * caller thread */ void init_schedstatistics(void); +#endif /* MODULE_SCHEDSTATISTICS */ +#ifdef MODULE_SCHED_CB /** * @brief Register a callback that will be called on every scheduler run * * @param[in] callback The callback functions the will be called */ void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t)); -#endif /* MODULE_SCHEDSTATISTICS */ +#endif /* MODULE_SCHED_CB */ #ifdef __cplusplus } diff --git a/core/sched.c b/core/sched.c index 0da844bb2b9d..a4210d12a1f4 100644 --- a/core/sched.c +++ b/core/sched.c @@ -74,8 +74,10 @@ FORCE_USED_SECTION const uint8_t _tcb_name_offset = offsetof(thread_t, name); #endif -#ifdef MODULE_SCHEDSTATISTICS +#ifdef MODULE_SCHED_CB static void (*sched_cb) (kernel_pid_t active_thread, kernel_pid_t next_thread) = NULL; +#endif +#ifdef MODULE_SCHEDSTATISTICS schedstat_t sched_pidlist[KERNEL_PID_LAST + 1]; #endif @@ -112,7 +114,7 @@ int __attribute__((used)) sched_run(void) #endif } -#ifdef MODULE_SCHEDSTATISTICS +#ifdef MODULE_SCHED_CB if (sched_cb) { /* Use `sched_active_pid` instead of `active_thread` since after `sched_task_exit()` is called `active_thread` is set to NULL while `sched_active_thread` isn't updated until @@ -204,12 +206,14 @@ NORETURN void sched_task_exit(void) cpu_switch_context_exit(); } -#ifdef MODULE_SCHEDSTATISTICS +#ifdef MODULE_SCHED_CB void sched_register_cb(void (*callback)(kernel_pid_t, kernel_pid_t)) { sched_cb = callback; } +#endif +#ifdef MODULE_SCHEDSTATISTICS void sched_statistics_cb(kernel_pid_t active_thread, kernel_pid_t next_thread) { uint32_t now = xtimer_now().ticks32; diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index d110900ff1bf..b4f769214f0e 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -70,6 +70,7 @@ PSEUDOMODULES += saul_gpio PSEUDOMODULES += saul_nrf_temperature PSEUDOMODULES += scanf_float PSEUDOMODULES += schedstatistics +PSEUDOMODULES += sched_cb PSEUDOMODULES += semtech_loramac_rx PSEUDOMODULES += sock PSEUDOMODULES += sock_ip