Skip to content

Commit d26b7d4

Browse files
dcpleungnashif
authored andcommitted
debug: thread_analyzer: display privileged stack usage
This adds the bits to display privileged stack usage for architectures that support obtaining this information. Signed-off-by: Daniel Leung <[email protected]>
1 parent aa65842 commit d26b7d4

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

include/zephyr/debug/thread_analyzer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ struct thread_analyzer_info {
3939
k_thread_runtime_stats_t usage;
4040
#endif
4141
#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
4250
};
4351

4452
/** @brief Thread analyzer stack size callback function

subsys/debug/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ config THREAD_ANALYZER_ISR_STACK_USAGE
4545
bool "Analyze interrupt stacks usage"
4646
default y
4747

48+
config THREAD_ANALYZER_PRIV_STACK_USAGE
49+
bool "Analyze privileged stacks usage"
50+
depends on USERSPACE
51+
depends on ARCH_HAS_THREAD_PRIV_STACK_SPACE_GET
52+
help
53+
Print privileged stack usage for user threads.
54+
4855
config THREAD_ANALYZER_RUN_UNLOCKED
4956
bool "Run analysis with interrupts unlocked"
5057
default y

subsys/debug/thread_analyzer.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ static void thread_print_cb(struct thread_analyzer_info *info)
4848
info->stack_size, pcnt,
4949
info->utilization);
5050

51+
#ifdef CONFIG_THREAD_ANALYZER_PRIV_STACK_USAGE
52+
if (info->priv_stack_size > 0) {
53+
pcnt = (info->priv_stack_used * 100U) / info->priv_stack_size;
54+
55+
THREAD_ANALYZER_PRINT(
56+
THREAD_ANALYZER_FMT(
57+
" %-20s: PRIV_STACK: unused %zu usage %zu / %zu (%zu %%)"),
58+
" ", info->priv_stack_size - info->priv_stack_used, info->priv_stack_used,
59+
info->priv_stack_size, pcnt);
60+
}
61+
#endif
62+
5163
#ifdef CONFIG_SCHED_THREAD_USAGE
5264
THREAD_ANALYZER_PRINT(
5365
THREAD_ANALYZER_FMT(" %-20s: Total CPU cycles used: %llu"),
@@ -82,7 +94,6 @@ static void thread_analyze_cb(const struct k_thread *cthread, void *user_data)
8294
struct k_thread *thread = (struct k_thread *)cthread;
8395
#ifdef CONFIG_THREAD_RUNTIME_STATS
8496
k_thread_runtime_stats_t rt_stats_all;
85-
int ret;
8697
#endif
8798
size_t size = thread->stack_info.size;
8899
struct ta_cb_user_data *ud = user_data;
@@ -93,6 +104,7 @@ static void thread_analyze_cb(const struct k_thread *cthread, void *user_data)
93104
const char *name;
94105
size_t unused;
95106
int err;
107+
int ret;
96108

97109

98110

@@ -116,6 +128,16 @@ static void thread_analyze_cb(const struct k_thread *cthread, void *user_data)
116128
info.stack_size = size;
117129
info.stack_used = size - unused;
118130

131+
#ifdef CONFIG_THREAD_ANALYZER_PRIV_STACK_USAGE
132+
ret = arch_thread_priv_stack_space_get(cthread, &size, &unused);
133+
if (ret == 0) {
134+
info.priv_stack_size = size;
135+
info.priv_stack_used = size - unused;
136+
} else {
137+
info.priv_stack_size = 0;
138+
}
139+
#endif
140+
119141
#ifdef CONFIG_THREAD_RUNTIME_STATS
120142
ret = 0;
121143

@@ -138,6 +160,9 @@ static void thread_analyze_cb(const struct k_thread *cthread, void *user_data)
138160
rt_stats_all.execution_cycles;
139161
}
140162
#endif
163+
164+
ARG_UNUSED(ret);
165+
141166
cb(&info);
142167
}
143168

0 commit comments

Comments
 (0)