Skip to content

Commit

Permalink
Merge #19250 #19260
Browse files Browse the repository at this point in the history
19250: sys/fmt: use `fflush(); stdio_write()` instead of `fwrite()` r=benpicco a=kaspar030



19260: sys/stdio: mark stdio_tinyusb_cdc_acm as buffered r=benpicco a=benpicco





Co-authored-by: Kaspar Schleiser <[email protected]>
Co-authored-by: Benjamin Valentin <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2023
3 parents f341ad6 + c77b104 + c27010c commit 5bdf2d3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion makefiles/stdio.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 15 additions & 15 deletions sys/fmt/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "kernel_defines.h"
#include "fmt.h"
#include "stdio_base.h"

static const char _hex_chars[16] = "0123456789ABCDEF";

Expand Down Expand Up @@ -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;
}
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);

while (n > 0) {
ssize_t written;
written = fwrite(s, 1, n, stdout);
if (written < 0) {
break;
}
n -= written;
s += written;
}
stdio_write(s, n);
}
#endif

void print_u32_dec(uint32_t val)
{
Expand Down
6 changes: 5 additions & 1 deletion sys/test_utils/print_stack_usage/print_stack_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#include <stdio.h>
#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)
{
Expand All @@ -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);
Expand Down

0 comments on commit 5bdf2d3

Please sign in to comment.