Comments (6)
Hi, could you try defining macro BUILTIN_LIBC_BUFFERED_PRINTF and enlarging the buffer size with macro BUILTIN_LIBC_BUFFERED_PRINT_SIZE? It only outputs the buffer when \n
is met or the buffer is full:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c#L348-L386
from wasm-micro-runtime.
Hi, could you try defining macro BUILTIN_LIBC_BUFFERED_PRINTF and enlarging the buffer size with macro BUILTIN_LIBC_BUFFERED_PRINT_SIZE? It only outputs the buffer when
\n
is met or the buffer is full: https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c#L348-L386
Hi, thanks for bringing these macros to my attention. I have not seen it before.
I have tried it and it loses some text from last few calls to printf in the app. I think preferred approach would be to actually have 1 to 1 mapping between printf in the app and os_printf in the embedder.
Furthermore, by looking in the code, the buffer is not thread safe, and allows mixing text from multiple wamr instances too.
Perhaps, could the buffer be moved inside str_context?
from wasm-micro-runtime.
Yes, moving buffer inside str_context is good, and the text losing may be caused by some remaining characters without '\n' are not dumped. I tried to refactor the code, could you try the patch below:
diff --git a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c
index 24edd3c0..f4cf6b8e 100644
--- a/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c
+++ b/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c
@@ -321,10 +321,22 @@ fail:
return false;
}
+#ifndef BUILTIN_LIBC_BUFFERED_PRINTF
+#define BUILTIN_LIBC_BUFFERED_PRINTF 0
+#endif
+
+#ifndef BUILTIN_LIBC_BUFFERED_PRINT_SIZE
+#define BUILTIN_LIBC_BUFFERED_PRINT_SIZE 128
+#endif
+
struct str_context {
char *str;
uint32 max;
uint32 count;
+#if BUILTIN_LIBC_BUFFERED_PRINTF != 0
+ char print_buf[BUILTIN_LIBC_BUFFERED_PRINT_SIZE];
+ uint32 print_buf_size;
+#endif
};
static int
@@ -345,41 +357,23 @@ sprintf_out(int c, struct str_context *ctx)
return c;
}
-#ifndef BUILTIN_LIBC_BUFFERED_PRINTF
-#define BUILTIN_LIBC_BUFFERED_PRINTF 0
-#endif
-
-#ifndef BUILTIN_LIBC_BUFFERED_PRINT_SIZE
-#define BUILTIN_LIBC_BUFFERED_PRINT_SIZE 128
-#endif
-#ifndef BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
-#define BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
-#endif
-
#if BUILTIN_LIBC_BUFFERED_PRINTF != 0
-
-BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
-static char print_buf[BUILTIN_LIBC_BUFFERED_PRINT_SIZE] = { 0 };
-
-BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
-static int print_buf_size = 0;
-
static int
printf_out(int c, struct str_context *ctx)
{
if (c == '\n') {
- print_buf[print_buf_size] = '\0';
- os_printf("%s\n", print_buf);
- print_buf_size = 0;
+ ctx->print_buf[ctx->print_buf_size] = '\0';
+ os_printf("%s\n", ctx->print_buf);
+ ctx->print_buf_size = 0;
}
- else if (print_buf_size >= sizeof(print_buf) - 2) {
- print_buf[print_buf_size++] = (char)c;
- print_buf[print_buf_size] = '\0';
- os_printf("%s\n", print_buf);
- print_buf_size = 0;
+ else if (ctx->print_buf_size >= sizeof(ctx->print_buf) - 2) {
+ ctx->print_buf[ctx->print_buf_size++] = (char)c;
+ ctx->print_buf[ctx->print_buf_size] = '\0';
+ os_printf("%s\n", ctx->print_buf);
+ ctx->print_buf_size = 0;
}
else {
- print_buf[print_buf_size++] = (char)c;
+ ctx->print_buf[ctx->print_buf_size++] = (char)c;
}
ctx->count++;
return c;
@@ -398,7 +392,9 @@ static int
printf_wrapper(wasm_exec_env_t exec_env, const char *format, _va_list va_args)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
- struct str_context ctx = { NULL, 0, 0 };
+ struct str_context ctx = { 0 };
+
+ memset(&ctx, 0, sizeof(ctx));
/* format has been checked by runtime */
if (!validate_native_addr(va_args, (uint64)sizeof(int32)))
@@ -408,6 +404,11 @@ printf_wrapper(wasm_exec_env_t exec_env, const char *format, _va_list va_args)
module_inst))
return 0;
+#if BUILTIN_LIBC_BUFFERED_PRINTF != 0
+ if (ctx.print_buf_size > 0)
+ os_printf("%s", ctx.print_buf);
+#endif
+
return (int)ctx.count;
}
from wasm-micro-runtime.
This works for me.
Awesome.
Thanks ;)
from wasm-micro-runtime.
Welcome. #3460
from wasm-micro-runtime.
Thanks ;)
from wasm-micro-runtime.
Related Issues (20)
- aot_load_const_from_table is inefficient
- "duplicate export name" check is O(n^2) HOT 3
- Compilation errors when build iwasm with GC and EH support
- iwasm: WASM module load failed: struct or array as field is not supported in constant expr HOT 6
- iwasm: WASM module load failed: type mismatch: expect (ref null ht) but got other1 unknown type HOT 5
- exception handler intercepts c++ exceptions in the host on windows HOT 2
- WASM module instantiate failed on Mac HOT 1
- aot: xtensa xip triggers llvm assertion failure
- BH_DEBUG not set in multi-configuration cmake (eg. visual studio)
- Can AOT file built on windows run directly on other platforms? HOT 5
- How to load and run another wasm in a running wasm? HOT 5
- Exception Handling Support in Fast Interpreter HOT 1
- iwasm with aot file fail with SIGSEGV HOT 4
- Exception for Assemblyscript HOT 1
- How to cross compile to riscv64 HOT 2
- Unexpected behavior on fast interpreter mode
- A bug in validation
- Report some typo in wamr.gitbook HOT 1
- Calling "step-over" on the last line of the function failed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wasm-micro-runtime.