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 <stdbool.h>
17 
log_stack_usage(const struct k_thread * thread)18 static inline void log_stack_usage(const struct k_thread *thread)
19 {
20 #if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
21 	size_t unused, size = thread->stack_info.size;
22 
23 	LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
24 
25 	if (k_thread_stack_space_get(thread, &unused) == 0) {
26 		unsigned int pcnt = ((size - unused) * 100U) / size;
27 		const char *tname;
28 
29 		tname = k_thread_name_get((k_tid_t)thread);
30 		if (tname == NULL) {
31 			tname = "unknown";
32 		}
33 
34 		LOG_INF("%p (%s):\tunused %zu\tusage %zu / %zu (%u %%)",
35 			thread, tname, unused, size - unused, size,
36 			pcnt);
37 	}
38 #endif
39 }
40 #endif /* ZEPHYR_INCLUDE_DEBUG_STACK_H_ */
41