From f31d5a93b517a5b233871e93811cb7da0a099d2d Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 6 Feb 2023 10:37:29 +0100 Subject: [PATCH 1/3] sys/fmt: `print()`: use `fflush(); stdio_write()` vs. `fwrite()` --- sys/fmt/fmt.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/sys/fmt/fmt.c b/sys/fmt/fmt.c index 11eddf3cfa..c1d6386550 100644 --- a/sys/fmt/fmt.c +++ b/sys/fmt/fmt.c @@ -27,6 +27,7 @@ #include "kernel_defines.h" #include "fmt.h" +#include "stdio_base.h" static const char _hex_chars[16] = "0123456789ABCDEF"; @@ -506,25 +507,24 @@ uint32_t scn_u32_hex(const char *str, size_t n) return res; } +/* native gets special treatment as native's stdio code is ... special. + * And when not building for RIOT, there's no `stdio_write()`. + * In those cases, just defer to `printf()`. + */ +#if IS_USED(MODULE_STDIO_NATIVE) || !defined(RIOT_VERSION) void print(const char *s, size_t n) { - if (IS_USED(MODULE_STDIO_NATIVE)) { - /* native gets special treatment as native's stdio code is ... - * special */ - printf("%.*s", (int)n, s); - return; - } - - while (n > 0) { - ssize_t written; - written = fwrite(s, 1, n, stdout); - if (written < 0) { - break; - } - n -= written; - s += written; - } + printf("%.*s", (int)n, s); } +#else +void print(const char *s, size_t n) +{ + /* flush the libc's output buffer so output is not intermingled. */ + fflush(stdout); + + stdio_write(s, n); +} +#endif void print_u32_dec(uint32_t val) { From c77b104c2dca40a0ca21b1fc66aa7312c2552f73 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Mon, 6 Feb 2023 10:38:18 +0100 Subject: [PATCH 2/3] sys/test_utils/print_stack_usage: reduce `MIN_SIZE` for fmt --- sys/test_utils/print_stack_usage/print_stack_usage.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sys/test_utils/print_stack_usage/print_stack_usage.c b/sys/test_utils/print_stack_usage/print_stack_usage.c index 6b67d6d1ac..b4e8f7c776 100644 --- a/sys/test_utils/print_stack_usage/print_stack_usage.c +++ b/sys/test_utils/print_stack_usage/print_stack_usage.c @@ -26,7 +26,11 @@ #include #endif +#if MODULE_FMT +# define MIN_SIZE (THREAD_STACKSIZE_TINY) +#else # define MIN_SIZE (THREAD_STACKSIZE_TINY + THREAD_EXTRA_STACKSIZE_PRINTF) +#endif void print_stack_usage_metric(const char *name, void *stack, unsigned max_size) { @@ -37,7 +41,7 @@ void print_stack_usage_metric(const char *name, void *stack, unsigned max_size) #if MODULE_FMT print_str("{ \"threads\": [{ \"name\": \""); print_str(name); - print_str(", \"stack_size\": "); + print_str("\", \"stack_size\": "); print_u32_dec(max_size); print_str(", \"stack_used\": "); print_u32_dec(max_size - free); From c27010c63046870c30d7bc7b8fb98ddd5be0aefb Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 7 Feb 2023 22:40:38 +0100 Subject: [PATCH 3/3] sys/stdio: mark stdio_tinyusb_cdc_acm as buffered stdio_tinyusb_cdc_acm benefits from stdio buffering (not transferring individual bytes) just like stdio_cdc_acm. --- makefiles/stdio.inc.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefiles/stdio.inc.mk b/makefiles/stdio.inc.mk index e641523145..9749c34177 100644 --- a/makefiles/stdio.inc.mk +++ b/makefiles/stdio.inc.mk @@ -82,7 +82,7 @@ endif # enable stdout buffering for modules that benefit from sending out buffers in larger chunks ifneq (,$(filter picolibc,$(USEMODULE))) - ifneq (,$(filter stdio_cdc_acm stdio_ethos slipdev_stdio stdio_semihosting,$(USEMODULE))) + ifneq (,$(filter stdio_cdc_acm stdio_ethos slipdev_stdio stdio_semihosting stdio_tinyusb_cdc_acm,$(USEMODULE))) USEMODULE += picolibc_stdout_buffered endif endif