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 #ifdef CONFIG_THREAD_ANALYZER_PRIV_STACK_USAGE
44 	/** Total size of privileged stack */
45 	size_t priv_stack_size;
46 
47 	/** Privileged stack size in used */
48 	size_t priv_stack_used;
49 #endif
50 };
51 
52 /** @brief Thread analyzer stack size callback function
53  *
54  *  Callback function with thread analysis information.
55  *
56  *  @param info Thread analysis information.
57  */
58 typedef void (*thread_analyzer_cb)(struct thread_analyzer_info *info);
59 
60 /** @brief Run the thread analyzer and provide information to the callback
61  *
62  *  This function analyzes the current state for all threads and calls
63  *  a given callback on every thread found. In the special case when Kconfig
64  *  option THREAD_ANALYZER_AUTO_SEPARATE_CORES is set, the function analyzes
65  *  only the threads running on the specified cpu.
66  *
67  *  @param cb The callback function handler
68  *  @param cpu cpu to analyze, ignored if THREAD_ANALYZER_AUTO_SEPARATE_CORES=n
69  */
70 void thread_analyzer_run(thread_analyzer_cb cb, unsigned int cpu);
71 
72 /** @brief Run the thread analyzer and print stack size statistics.
73  *
74  *  This function runs the thread analyzer and prints the output in
75  *  standard form. In the special case when Kconfig option
76  *  THREAD_ANALYZER_AUTO_SEPARATE_CORES is set, the function analyzes
77  *  only the threads running on the specified cpu.
78  *
79  *  @param cpu cpu to analyze, ignored if THREAD_ANALYZER_AUTO_SEPARATE_CORES=n
80  */
81 void thread_analyzer_print(unsigned int cpu);
82 
83 /** @} */
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif /* __STACK_SIZE_ANALYZER_H */
90