Skip to content

Commit b4696ac

Browse files
authored
Remove printf usage from emmalloc.s and sbrk.s. NFC (emscripten-core#26497)
I noticed some code size issues while working on emscripten-core#26489 and gemini tracked them down to usage of the `printf` symbol. It turns out that just referencing the printf (even if the referencing function is not used) in a used object file can lead code bloat due to the weak symbol mechanism used in musl to track stdio usage (see __stdout_used, etc).
1 parent 0a196a1 commit b4696ac

File tree

4 files changed

+95
-104
lines changed

4 files changed

+95
-104
lines changed

system/lib/emmalloc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <stdio.h>
6161
#include <emscripten/heap.h>
6262
#include <emscripten/threading.h>
63+
#include <emscripten/console.h>
6364

6465
void *_sbrk64(int64_t numBytes);
6566

@@ -1421,9 +1422,9 @@ size_t emmalloc_compute_free_dynamic_memory_fragmentation_map(size_t freeMemoryS
14211422
void emmalloc_dump_free_dynamic_memory_fragmentation_map() {
14221423
size_t freeMemorySizeMap[32];
14231424
size_t numFreeMemoryRegions = emmalloc_compute_free_dynamic_memory_fragmentation_map(freeMemorySizeMap);
1424-
printf("numFreeMemoryRegions: %zu\n", numFreeMemoryRegions);
1425+
emscripten_errf("numFreeMemoryRegions: %zu", numFreeMemoryRegions);
14251426
for (int i = 0; i < 32; ++i) {
1426-
printf("Free memory regions of size [%llu,%llu[ bytes: %zu regions\n", 1ull<<i, 1ull<<(i+1), freeMemorySizeMap[i]);
1427+
emscripten_errf("Free memory regions of size [%llu,%llu[ bytes: %zu regions", 1ull<<i, 1ull<<(i+1), freeMemorySizeMap[i]);
14271428
}
14281429
}
14291430

system/lib/libc/sbrk.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#include <limits.h>
1717
#include <stddef.h>
1818
#include <stdint.h>
19-
#ifdef __EMSCRIPTEN_SHARED_MEMORY__ // for error handling, see below
20-
#include <stdio.h>
21-
#include <stdlib.h>
19+
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
20+
#include <stdlib.h> // for abort
2221
#endif
2322

23+
#include <emscripten/console.h>
2424
#include <emscripten/heap.h>
2525
#include <emscripten/trace.h>
2626

@@ -116,7 +116,9 @@ void *sbrk(intptr_t increment_) {
116116
int brk(void* ptr) {
117117
#ifdef __EMSCRIPTEN_SHARED_MEMORY__
118118
// FIXME
119-
printf("brk() is not threadsafe yet, https://github.com/emscripten-core/emscripten/issues/10006");
119+
#ifndef NDEBUG
120+
emscripten_err("brk() is not threadsafe yet, https://github.com/emscripten-core/emscripten/issues/10006");
121+
#endif
120122
abort();
121123
#else
122124
uintptr_t last = (uintptr_t)sbrk(0);

test/codesize/test_codesize_minimal_pthreads.json

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,64 @@
11
{
2-
"a.out.js": 7866,
3-
"a.out.js.gz": 3852,
4-
"a.out.nodebug.wasm": 19707,
5-
"a.out.nodebug.wasm.gz": 9122,
6-
"total": 27573,
7-
"total_gz": 12974,
2+
"a.out.js": 7364,
3+
"a.out.js.gz": 3607,
4+
"a.out.nodebug.wasm": 19201,
5+
"a.out.nodebug.wasm.gz": 8856,
6+
"total": 26565,
7+
"total_gz": 12463,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",
1111
"c (exit)",
1212
"d (_emscripten_thread_set_strongref)",
1313
"e (_emscripten_receive_on_main_thread_js)",
14-
"f (fd_write)",
15-
"g (emscripten_runtime_keepalive_check)",
16-
"h (emscripten_resize_heap)",
17-
"i (emscripten_exit_with_live_runtime)",
18-
"j (emscripten_check_blocking_allowed)",
19-
"k (_emscripten_thread_mailbox_await)",
20-
"l (_emscripten_thread_cleanup)",
21-
"m (_emscripten_notify_mailbox_postmessage)",
22-
"n (_emscripten_init_main_thread_js)",
23-
"o (__pthread_create_js)"
14+
"f (emscripten_runtime_keepalive_check)",
15+
"g (emscripten_resize_heap)",
16+
"h (emscripten_exit_with_live_runtime)",
17+
"i (emscripten_check_blocking_allowed)",
18+
"j (_emscripten_thread_mailbox_await)",
19+
"k (_emscripten_thread_cleanup)",
20+
"l (_emscripten_notify_mailbox_postmessage)",
21+
"m (_emscripten_init_main_thread_js)",
22+
"n (__pthread_create_js)"
2423
],
2524
"imports": [
2625
"a (memory)",
2726
"b (emscripten_get_now)",
2827
"c (exit)",
2928
"d (_emscripten_thread_set_strongref)",
3029
"e (_emscripten_receive_on_main_thread_js)",
31-
"f (fd_write)",
32-
"g (emscripten_runtime_keepalive_check)",
33-
"h (emscripten_resize_heap)",
34-
"i (emscripten_exit_with_live_runtime)",
35-
"j (emscripten_check_blocking_allowed)",
36-
"k (_emscripten_thread_mailbox_await)",
37-
"l (_emscripten_thread_cleanup)",
38-
"m (_emscripten_notify_mailbox_postmessage)",
39-
"n (_emscripten_init_main_thread_js)",
40-
"o (__pthread_create_js)"
30+
"f (emscripten_runtime_keepalive_check)",
31+
"g (emscripten_resize_heap)",
32+
"h (emscripten_exit_with_live_runtime)",
33+
"i (emscripten_check_blocking_allowed)",
34+
"j (_emscripten_thread_mailbox_await)",
35+
"k (_emscripten_thread_cleanup)",
36+
"l (_emscripten_notify_mailbox_postmessage)",
37+
"m (_emscripten_init_main_thread_js)",
38+
"n (__pthread_create_js)"
4139
],
4240
"exports": [
43-
"A (_emscripten_run_js_on_main_thread)",
44-
"B (_emscripten_thread_free_data)",
45-
"C (_emscripten_thread_exit)",
46-
"D (_emscripten_check_mailbox)",
47-
"E (emscripten_stack_set_limits)",
48-
"F (_emscripten_stack_restore)",
49-
"G (_emscripten_stack_alloc)",
50-
"H (emscripten_stack_get_current)",
51-
"p (__wasm_call_ctors)",
52-
"q (add)",
53-
"r (main)",
54-
"s (global_val)",
55-
"t (__indirect_function_table)",
56-
"u (_emscripten_tls_init)",
57-
"v (pthread_self)",
58-
"w (_emscripten_proxy_main)",
59-
"x (_emscripten_thread_init)",
60-
"y (_emscripten_thread_crashed)",
61-
"z (_emscripten_run_js_on_main_thread_done)"
41+
"A (_emscripten_thread_free_data)",
42+
"B (_emscripten_thread_exit)",
43+
"C (_emscripten_check_mailbox)",
44+
"D (emscripten_stack_set_limits)",
45+
"E (_emscripten_stack_restore)",
46+
"F (_emscripten_stack_alloc)",
47+
"G (emscripten_stack_get_current)",
48+
"o (__wasm_call_ctors)",
49+
"p (add)",
50+
"q (main)",
51+
"r (global_val)",
52+
"s (__indirect_function_table)",
53+
"t (_emscripten_tls_init)",
54+
"u (pthread_self)",
55+
"v (_emscripten_proxy_main)",
56+
"w (_emscripten_thread_init)",
57+
"x (_emscripten_thread_crashed)",
58+
"y (_emscripten_run_js_on_main_thread_done)",
59+
"z (_emscripten_run_js_on_main_thread)"
6260
],
6361
"funcs": [
64-
"$__emscripten_stdout_seek",
6562
"$__errno_location",
6663
"$__memcpy",
6764
"$__memset",
@@ -76,7 +73,6 @@
7673
"$__pthread_self_internal",
7774
"$__pthread_setcancelstate",
7875
"$__set_thread_state",
79-
"$__stdio_write",
8076
"$__timedwait",
8177
"$__timedwait_cp",
8278
"$__tl_lock",
@@ -86,7 +82,6 @@
8682
"$__wait",
8783
"$__wake",
8884
"$__wake",
89-
"$__wasi_syscall_ret",
9085
"$__wasm_call_ctors",
9186
"$__wasm_init_memory",
9287
"$__wasm_init_tls",
@@ -146,7 +141,6 @@
146141
"$lock",
147142
"$main",
148143
"$nodtor",
149-
"$pthread_attr_destroy",
150144
"$pthread_cond_signal",
151145
"$pthread_mutex_destroy",
152146
"$pthread_setspecific",

test/codesize/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,64 @@
11
{
2-
"a.out.js": 8288,
3-
"a.out.js.gz": 4052,
4-
"a.out.nodebug.wasm": 19708,
5-
"a.out.nodebug.wasm.gz": 9123,
6-
"total": 27996,
7-
"total_gz": 13175,
2+
"a.out.js": 7766,
3+
"a.out.js.gz": 3812,
4+
"a.out.nodebug.wasm": 19202,
5+
"a.out.nodebug.wasm.gz": 8856,
6+
"total": 26968,
7+
"total_gz": 12668,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",
1111
"c (exit)",
1212
"d (_emscripten_thread_set_strongref)",
1313
"e (_emscripten_receive_on_main_thread_js)",
14-
"f (fd_write)",
15-
"g (emscripten_runtime_keepalive_check)",
16-
"h (emscripten_resize_heap)",
17-
"i (emscripten_exit_with_live_runtime)",
18-
"j (emscripten_check_blocking_allowed)",
19-
"k (_emscripten_thread_mailbox_await)",
20-
"l (_emscripten_thread_cleanup)",
21-
"m (_emscripten_notify_mailbox_postmessage)",
22-
"n (_emscripten_init_main_thread_js)",
23-
"o (__pthread_create_js)"
14+
"f (emscripten_runtime_keepalive_check)",
15+
"g (emscripten_resize_heap)",
16+
"h (emscripten_exit_with_live_runtime)",
17+
"i (emscripten_check_blocking_allowed)",
18+
"j (_emscripten_thread_mailbox_await)",
19+
"k (_emscripten_thread_cleanup)",
20+
"l (_emscripten_notify_mailbox_postmessage)",
21+
"m (_emscripten_init_main_thread_js)",
22+
"n (__pthread_create_js)"
2423
],
2524
"imports": [
2625
"a (memory)",
2726
"b (emscripten_get_now)",
2827
"c (exit)",
2928
"d (_emscripten_thread_set_strongref)",
3029
"e (_emscripten_receive_on_main_thread_js)",
31-
"f (fd_write)",
32-
"g (emscripten_runtime_keepalive_check)",
33-
"h (emscripten_resize_heap)",
34-
"i (emscripten_exit_with_live_runtime)",
35-
"j (emscripten_check_blocking_allowed)",
36-
"k (_emscripten_thread_mailbox_await)",
37-
"l (_emscripten_thread_cleanup)",
38-
"m (_emscripten_notify_mailbox_postmessage)",
39-
"n (_emscripten_init_main_thread_js)",
40-
"o (__pthread_create_js)"
30+
"f (emscripten_runtime_keepalive_check)",
31+
"g (emscripten_resize_heap)",
32+
"h (emscripten_exit_with_live_runtime)",
33+
"i (emscripten_check_blocking_allowed)",
34+
"j (_emscripten_thread_mailbox_await)",
35+
"k (_emscripten_thread_cleanup)",
36+
"l (_emscripten_notify_mailbox_postmessage)",
37+
"m (_emscripten_init_main_thread_js)",
38+
"n (__pthread_create_js)"
4139
],
4240
"exports": [
43-
"A (_emscripten_run_js_on_main_thread)",
44-
"B (_emscripten_thread_free_data)",
45-
"C (_emscripten_thread_exit)",
46-
"D (_emscripten_check_mailbox)",
47-
"E (emscripten_stack_set_limits)",
48-
"F (_emscripten_stack_restore)",
49-
"G (_emscripten_stack_alloc)",
50-
"H (emscripten_stack_get_current)",
51-
"p (__wasm_call_ctors)",
52-
"q (add)",
53-
"r (main)",
54-
"s (global_val)",
55-
"t (__indirect_function_table)",
56-
"u (_emscripten_tls_init)",
57-
"v (pthread_self)",
58-
"w (_emscripten_proxy_main)",
59-
"x (_emscripten_thread_init)",
60-
"y (_emscripten_thread_crashed)",
61-
"z (_emscripten_run_js_on_main_thread_done)"
41+
"A (_emscripten_thread_free_data)",
42+
"B (_emscripten_thread_exit)",
43+
"C (_emscripten_check_mailbox)",
44+
"D (emscripten_stack_set_limits)",
45+
"E (_emscripten_stack_restore)",
46+
"F (_emscripten_stack_alloc)",
47+
"G (emscripten_stack_get_current)",
48+
"o (__wasm_call_ctors)",
49+
"p (add)",
50+
"q (main)",
51+
"r (global_val)",
52+
"s (__indirect_function_table)",
53+
"t (_emscripten_tls_init)",
54+
"u (pthread_self)",
55+
"v (_emscripten_proxy_main)",
56+
"w (_emscripten_thread_init)",
57+
"x (_emscripten_thread_crashed)",
58+
"y (_emscripten_run_js_on_main_thread_done)",
59+
"z (_emscripten_run_js_on_main_thread)"
6260
],
6361
"funcs": [
64-
"$__emscripten_stdout_seek",
6562
"$__errno_location",
6663
"$__memcpy",
6764
"$__memset",
@@ -76,7 +73,6 @@
7673
"$__pthread_self_internal",
7774
"$__pthread_setcancelstate",
7875
"$__set_thread_state",
79-
"$__stdio_write",
8076
"$__timedwait",
8177
"$__timedwait_cp",
8278
"$__tl_lock",
@@ -86,7 +82,6 @@
8682
"$__wait",
8783
"$__wake",
8884
"$__wake",
89-
"$__wasi_syscall_ret",
9085
"$__wasm_call_ctors",
9186
"$__wasm_init_memory",
9287
"$__wasm_init_tls",
@@ -146,7 +141,6 @@
146141
"$lock",
147142
"$main",
148143
"$nodtor",
149-
"$pthread_attr_destroy",
150144
"$pthread_cond_signal",
151145
"$pthread_mutex_destroy",
152146
"$pthread_setspecific",

0 commit comments

Comments
 (0)