diff --git a/core/include/sched.h b/core/include/sched.h index 7c94a5d6d4..9f89b7047b 100644 --- a/core/include/sched.h +++ b/core/include/sched.h @@ -203,29 +203,6 @@ extern clist_node_t sched_runqueues[SCHED_PRIO_LEVELS]; */ NORETURN void sched_task_exit(void); -#ifdef MODULE_SCHEDSTATISTICS -/** - * Scheduler statistics - */ -typedef struct { - uint32_t laststart; /**< Time stamp of the last time this thread was - scheduled to run */ - unsigned int schedules; /**< How often the thread was scheduled to run */ - uint64_t runtime_ticks; /**< The total runtime of this thread in ticks */ -} schedstat_t; - -/** - * Thread statistics table - */ -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); -#endif /* MODULE_SCHEDSTATISTICS */ - #ifdef MODULE_SCHED_CB /** * @brief Register a callback that will be called on every scheduler run diff --git a/core/sched.c b/core/sched.c index a4210d12a1..a751daa592 100644 --- a/core/sched.c +++ b/core/sched.c @@ -33,10 +33,6 @@ #include "mpu.h" #endif -#ifdef MODULE_SCHEDSTATISTICS -#include "xtimer.h" -#endif - #define ENABLE_DEBUG (0) #include "debug.h" @@ -77,9 +73,6 @@ const uint8_t _tcb_name_offset = offsetof(thread_t, name); #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 int __attribute__((used)) sched_run(void) { @@ -212,30 +205,3 @@ 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; - - /* 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/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index 26dc98a784..7b72be3b7e 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -71,7 +71,6 @@ PSEUDOMODULES += saul_default PSEUDOMODULES += saul_gpio PSEUDOMODULES += saul_nrf_temperature PSEUDOMODULES += scanf_float -PSEUDOMODULES += schedstatistics PSEUDOMODULES += sched_cb PSEUDOMODULES += semtech_loramac_rx PSEUDOMODULES += sock diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index 70fc45740a..66f80b3db7 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -93,7 +93,7 @@ #endif #ifdef MODULE_SCHEDSTATISTICS -#include "sched.h" +#include "schedstatistics.h" #endif #define ENABLE_DEBUG (0) diff --git a/sys/include/schedstatistics.h b/sys/include/schedstatistics.h new file mode 100644 index 0000000000..1437487a5b --- /dev/null +++ b/sys/include/schedstatistics.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin + * 2019 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup schedstatistics Schedstatistics + * @ingroup sys + * @brief When including this module scheduler statistics + * (@ref schedstat_t) for a thread will be updated on every + * @ref sched_run(). + * + * @note If auto_init is disabled `init_schedstatistics()` needs to be + * called as well as xtimer_init(). + * @{ + * + * @file + * @brief Scheduler statisctics + * + * @author Kaspar Schleiser + * @author Francisco Molina + * + */ + +#ifndef SCHEDSTATISTICS_H +#define SCHEDSTATISTICS_H + +#include +#include "kernel_types.h" + +#ifdef __cplusplus + extern "C" { +#endif + +/** + * Scheduler statistics + */ +typedef struct { + uint32_t laststart; /**< Time stamp of the last time this thread was + scheduled to run */ + unsigned int schedules; /**< How often the thread was scheduled to run */ + uint64_t runtime_ticks; /**< The total runtime of this thread in ticks */ +} schedstat_t; + +/** + * Thread statistics table + */ +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); + +#ifdef __cplusplus +} +#endif + +#endif /* SCHEDSTATISTICS_H */ +/** @} */ diff --git a/sys/ps/ps.c b/sys/ps/ps.c index 0542637188..7f57369258 100644 --- a/sys/ps/ps.c +++ b/sys/ps/ps.c @@ -22,6 +22,10 @@ #include "thread.h" #include "kernel_types.h" +#ifdef MODULE_SCHEDSTATISTICS +#include "schedstatistics.h" +#endif + #ifdef MODULE_TLSF_MALLOC #include "tlsf.h" #include "tlsf-malloc.h" diff --git a/sys/schedstatistics/Makefile b/sys/schedstatistics/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/sys/schedstatistics/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/schedstatistics/schedstatistics.c b/sys/schedstatistics/schedstatistics.c new file mode 100644 index 0000000000..fac0d181bf --- /dev/null +++ b/sys/schedstatistics/schedstatistics.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2014 Freie Universität Berlin + * 2019 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys + * @{ + * + * @file + * @brief Scheduler statistics implementation + * + * @author Kaspar Schleiser + * @author René Kijewski + * @author Francisco Molina + * + * @} + */ + +#include "sched.h" +#include "xtimer.h" +#include "schedstatistics.h" + +schedstat_t sched_pidlist[KERNEL_PID_LAST + 1]; + +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 allways 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); +}