1 /*
2  * Copyright (c) 2019 - 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __STACK_SIZE_ANALYZER_H
8 #define __STACK_SIZE_ANALYZER_H
9 
10 #include <stddef.h>
11 #include <zephyr/kernel/thread.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /** @defgroup thread_analyzer Thread analyzer
18  *  @ingroup os_services
19  *  @brief Module for analyzing threads
20  *
21  *  This module implements functions and the configuration that simplifies
22  *  thread analysis.
23  *  @{
24  */
25 
26 struct thread_analyzer_info {
27 	/** The name of the thread or stringified address of the thread handle
28 	 * if name is not set.
29 	 */
30 	const char *name;
31 	/** The total size of the stack*/
32 	size_t stack_size;
33 	/** Stack size in used */
34 	size_t stack_used;
35 
36 #ifdef CONFIG_THREAD_RUNTIME_STATS
37 	unsigned int utilization;
38 #ifdef CONFIG_SCHED_THREAD_USAGE
39 	k_thread_runtime_stats_t  usage;
40 #endif
41 #endif
42 };
43 
44 /** @brief Thread analyzer stack size callback function
45  *
46  *  Callback function with thread analysis information.
47  *
48  *  @param info Thread analysis information.
49  */
50 typedef void (*thread_analyzer_cb)(struct thread_analyzer_info *info);
51 
52 /** @brief Run the thread analyzer and provide information to the callback
53  *
54  *  This function analyzes the current state for all threads and calls
55  *  a given callback on every thread found. In the special case when Kconfig
56  *  option THREAD_ANALYZER_AUTO_SEPARATE_CORES is set, the function analyzes
57  *  only the threads running on the specified cpu.
58  *
59  *  @param cb The callback function handler
60  *  @param cpu cpu to analyze, ignored if THREAD_ANALYZER_AUTO_SEPARATE_CORES=n
61  */
62 void thread_analyzer_run(thread_analyzer_cb cb, unsigned int cpu);
63 
64 /** @brief Run the thread analyzer and print stack size statistics.
65  *
66  *  This function runs the thread analyzer and prints the output in
67  *  standard form. In the special case when Kconfig option
68  *  THREAD_ANALYZER_AUTO_SEPARATE_CORES is set, the function analyzes
69  *  only the threads running on the specified cpu.
70  *
71  *  @param cpu cpu to analyze, ignored if THREAD_ANALYZER_AUTO_SEPARATE_CORES=n
72  */
73 void thread_analyzer_print(unsigned int cpu);
74 
75 /** @} */
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
81 #endif /* __STACK_SIZE_ANALYZER_H */
82