Skip to content

Commit 3977ff6

Browse files
authored
Merge pull request #17891 from kaspar030/print_stack_usage_fmt
sys/test_utils/print_stack_usage: work with small stacks
2 parents d6115e3 + 480ed47 commit 3977ff6

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

sys/test_utils/print_stack_usage/print_stack_usage.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,37 @@
2020
#include "thread.h"
2121
#include "log.h"
2222

23+
#if MODULE_FMT
24+
#include "fmt.h"
25+
#endif
26+
27+
#if MODULE_FMT
28+
/* fmt's `print_str()` needs very little stack. ~200 total was fine on Cortex-M. */
29+
# define MIN_SIZE (THREAD_STACKSIZE_TINY)
30+
#else
31+
# define MIN_SIZE (THREAD_STACKSIZE_TINY + THREAD_EXTRA_STACKSIZE_PRINTF)
32+
#endif
33+
2334
void print_stack_usage_metric(const char *name, void *stack, unsigned max_size)
2435
{
2536
unsigned free = thread_measure_stack_free(stack);
26-
LOG_INFO("{ \"threads\": [{ \"name\": \"%s\", \"stack_size\": %u, \"stack_used\": %u }]}\n",
27-
name, max_size, max_size - free);
37+
38+
if ((LOG_LEVEL >= LOG_INFO) &&
39+
(thread_get_stacksize(thread_get_active()) >= MIN_SIZE)) {
40+
#if MODULE_FMT
41+
print_str("{ \"threads\": [{ \"name\": \"");
42+
print_str(name);
43+
print_str(", \"stack_size\": ");
44+
print_u32_dec(max_size);
45+
print_str(", \"stack_used\": ");
46+
print_u32_dec(max_size - free);
47+
print_str("}]}\n");
48+
#else
49+
printf(
50+
"{ \"threads\": [{ \"name\": \"%s\", \"stack_size\": %u, \"stack_used\": %u }]}\n",
51+
name, max_size, max_size - free);
52+
#endif
53+
}
2854
}
2955

3056
#ifdef DEVELHELP

tests/pthread_flood/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ USEMODULE += posix_headers
44
USEMODULE += pthread
55
CFLAGS += -DMAXTHREADS=8
66

7+
# include "fmt" so print_stack_usage needs less stack
8+
USEMODULE += fmt
9+
710
include $(RIOTBASE)/Makefile.include

0 commit comments

Comments
 (0)