Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/fmt: use fflush(); stdio_write() instead of fwrite() #19250

Merged
merged 2 commits into from
Feb 8, 2023

Conversation

kaspar030
Copy link
Contributor

Contribution description

Currently, fmt's print() is using libc's fwrite() in order to mitigate both
libc and fmt output clashing wrt. libc stdio buffers. That causes print() to
use quite a bit more stack, we had to remove the lower stack case in
print_stack_usage_metrics().

This PR changes print() to use fflush(); stdio_write() instead, bypassing
a lot of libc's FILE machinery.

A quick test on nrf52840dk shows that print_stack_usage_metrics() on an
otherwise empty thread uses 124b stack without the fflush(), 160b with the
fflush() (this PR's state). So another commit re-adds the MIN_SIZE fmt
case.

Testing procedure

Issues/PRs references

@kaspar030 kaspar030 added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR labels Feb 6, 2023
@github-actions github-actions bot added the Area: sys Area: System label Feb 6, 2023
@riot-ci
Copy link

riot-ci commented Feb 6, 2023

Murdock results

✔️ PASSED

c77b104 sys/test_utils/print_stack_usage: reduce MIN_SIZE for fmt

Success Failures Total Runtime
6853 0 6853 11m:19s

Artifacts

@github-actions github-actions bot added the Area: tools Area: Supplementary tools label Feb 6, 2023
sys/fmt/fmt.c Outdated Show resolved Hide resolved
@github-actions github-actions bot removed the Area: tools Area: Supplementary tools label Feb 6, 2023
@kaspar030 kaspar030 force-pushed the fmt_fwrite_to_fflush branch 2 times, most recently from 9e06684 to bfcf2e5 Compare February 6, 2023 19:57
@benpicco
Copy link
Contributor

benpicco commented Feb 7, 2023

I think the failed test is just a fluke, what do you think?

@kaspar030
Copy link
Contributor Author

I think the failed test is just a fluke, what do you think?

Unfortunately not. Somehow the if condition caused printf to be used, which fails with the small stacks in those tests.
(apparently defined(FOO) in the RHS of a define is not generally supported).

I've changed print() to be two full functions, selected by preprocessor.

@kaspar030 kaspar030 force-pushed the fmt_fwrite_to_fflush branch 2 times, most recently from 7e8be4c to 2fb98a9 Compare February 7, 2023 20:21
@kaspar030
Copy link
Contributor Author

I've also added a bit more commentary.
@benpicco Maybe quickly re-ACK?

Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please squash

@kaspar030 kaspar030 force-pushed the fmt_fwrite_to_fflush branch from ba8a886 to c77b104 Compare February 7, 2023 21:42
@benpicco
Copy link
Contributor

benpicco commented Feb 7, 2023

bors merge

@bors
Copy link
Contributor

bors bot commented Feb 8, 2023

Build succeeded:

@bors bors bot merged commit 5bdf2d3 into RIOT-OS:master Feb 8, 2023
@kaspar030 kaspar030 deleted the fmt_fwrite_to_fflush branch February 8, 2023 10:08
@kaspar030
Copy link
Contributor Author

Thanks for the review!

Comment on lines +510 to +514
/* 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain to me why this is needed? I fail to understand the reason on my own. E.g. if I remove the guarded code, the unit test still pass for native. What am I misunderstanding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants