1 /**
2  * @file debug/stack.h
3  * Stack usage analysis helpers
4  */
5 
6 /*
7  * Copyright (c) 2015 Intel Corporation
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DEBUG_STACK_H_
13 #define ZEPHYR_INCLUDE_DEBUG_STACK_H_
14 
15 #include <zephyr/logging/log.h>
16 #include <zephyr/toolchain.h>
17 #include <stdbool.h>
18 
log_stack_usage(const struct k_thread * thread)19 static inline void log_stack_usage(const struct k_thread *thread)
20 {
21 #if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
22 	size_t unused, size = thread->stack_info.size;
23 
24 	TOOLCHAIN_IGNORE_WSHADOW_BEGIN;
25 	LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
26 	TOOLCHAIN_IGNORE_WSHADOW_END;
27 
28 	if (k_thread_stack_space_get(thread, &unused) == 0) {
29 		unsigned int pcnt = ((size - unused) * 100U) / size;
30 		const char *tname;
31 
32 		tname = k_thread_name_get((k_tid_t)thread);
33 		if (tname == NULL) {
34 			tname = "unknown";
35 		}
36 
37 		LOG_INF("%p (%s):\tunused %zu\tusage %zu / %zu (%u %%)",
38 			thread, tname, unused, size - unused, size,
39 			pcnt);
40 	}
41 #endif
42 }
43 #endif /* ZEPHYR_INCLUDE_DEBUG_STACK_H_ */
44