From 2ba85c1bc84ef4710e0701b0adfe18deeab4d5ce Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 6 Aug 2020 13:24:06 +0200 Subject: [PATCH] core: add support for test_utils_print_stack_usage - activate THREAD_CREATE_STACKTEST also if test_utils_print_stack_usage is used - make thread_measure_stack_free() available unconditionally - if DEVELHELP is active, call test_utils_print_stack_usage() on any thread exit - if DEVELHELP is active, call test_utils_print_stack_usage() after main for the idle thread, if that is used --- core/include/thread.h | 2 -- core/lib/init.c | 13 ++++++++++--- core/sched.c | 6 ++++++ core/thread.c | 5 ++--- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/core/include/thread.h b/core/include/thread.h index 1a67398424..dd1ee69040 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -438,7 +438,6 @@ void thread_add_to_list(list_node_t *list, thread_t *thread); */ const char *thread_getname(kernel_pid_t pid); -#ifdef DEVELHELP /** * @brief Measures the stack usage of a stack * @@ -449,7 +448,6 @@ const char *thread_getname(kernel_pid_t pid); * @return the amount of unused space of the thread's stack */ uintptr_t thread_measure_stack_free(const char *stack); -#endif /* DEVELHELP */ /** * @brief Get the number of bytes used on the ISR stack diff --git a/core/lib/init.c b/core/lib/init.c index 0e651fb835..840677c49e 100644 --- a/core/lib/init.c +++ b/core/lib/init.c @@ -40,6 +40,9 @@ extern int main(void); +static char main_stack[THREAD_STACKSIZE_MAIN]; +static char idle_stack[THREAD_STACKSIZE_IDLE]; + static void *main_trampoline(void *arg) { (void)arg; @@ -54,12 +57,16 @@ static void *main_trampoline(void *arg) main(); +#ifdef MODULE_TEST_UTILS_PRINT_STACK_USAGE + void print_stack_usage_metric(const char *name, void *stack, unsigned max_size); + if (IS_USED(MODULE_CORE_IDLE_THREAD)) { + print_stack_usage_metric("idle", idle_stack, THREAD_STACKSIZE_IDLE); + } +#endif + return NULL; } -static char main_stack[THREAD_STACKSIZE_MAIN]; -static char idle_stack[THREAD_STACKSIZE_IDLE]; - static void *idle_thread(void *arg) { (void)arg; diff --git a/core/sched.c b/core/sched.c index 8ff2ec1d6d..fb8dbc3d7c 100644 --- a/core/sched.c +++ b/core/sched.c @@ -304,6 +304,12 @@ NORETURN void sched_task_exit(void) DEBUG("sched_task_exit: ending thread %" PRIkernel_pid "...\n", thread_getpid()); +#if defined(MODULE_TEST_UTILS_PRINT_STACK_USAGE) && defined(DEVELHELP) + void print_stack_usage_metric(const char *name, void *stack, unsigned max_size); + thread_t *me = thread_get_active(); + print_stack_usage_metric(me->name, me->stack_start, me->stack_size); +#endif + (void)irq_disable(); sched_threads[thread_getpid()] = NULL; sched_num_threads--; diff --git a/core/thread.c b/core/thread.c index ff3dcc7687..430130d761 100644 --- a/core/thread.c +++ b/core/thread.c @@ -171,7 +171,6 @@ void thread_add_to_list(list_node_t *list, thread_t *thread) list->next = new_node; } -#ifdef DEVELHELP uintptr_t thread_measure_stack_free(const char *stack) { /* Alignment of stack has been fixed (if needed) by thread_create(), so @@ -188,7 +187,6 @@ uintptr_t thread_measure_stack_free(const char *stack) return space_free; } -#endif kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, int flags, thread_task_func_t function, void *arg, @@ -234,7 +232,8 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, _init_tls(thread->tls); #endif -#if defined(DEVELHELP) || IS_ACTIVE(SCHED_TEST_STACK) +#if defined(DEVELHELP) || defined(SCHED_TEST_STACK) \ + || defined(MODULE_TEST_UTILS_PRINT_STACK_USAGE) if (flags & THREAD_CREATE_STACKTEST) { /* assign each int of the stack the value of it's address. Alignment * has been handled above, so silence -Wcast-align */