Skip to content

Commit 289fc5e

Browse files
authoredMar 8, 2023
Enhance/Fix sample socket-api and workload (bytecodealliance#2006)
- Add python script to test socket-api cases - Fix issues in socket-api send_recv wasm app - Fix issues in building samples/workload/meshoptimizer - Enhance build script of sample workload
1 parent a15a731 commit 289fc5e

File tree

10 files changed

+199
-34
lines changed

10 files changed

+199
-34
lines changed
 

‎samples/simple/sample_test_run.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@
1010
import sys
1111
import time
1212
import traceback
13+
import glob
1314

1415
WAMRC_CMD = "../../wamr-compiler/build/wamrc"
1516

17+
def compile_wasm_files_to_aot(wasm_apps_dir):
18+
wasm_files = glob.glob(wasm_apps_dir + "/*.wasm")
19+
print("Compile wasm app into aot files")
20+
for wasm_file in wasm_files:
21+
aot_file = wasm_file[0 : len(wasm_file) - 5] + ".aot";
22+
cmd = [ WAMRC_CMD, "-o", aot_file, wasm_file ]
23+
subprocess.check_call(cmd)
24+
1625
def start_server(cwd):
1726
"""
1827
Startup the 'simple' process works in TCP server mode
@@ -92,19 +101,8 @@ def main():
92101
print("Test with AOT mode")
93102
test_aot = True
94103
suffix = ".aot"
95-
wasm_files = [ "timer", "sensor", "connection",
96-
"event_publisher", "event_subscriber",
97-
"request_handler", "request_sender" ]
98-
work_dir = args.working_directory
99-
wasm_apps_dir = work_dir + "/wasm-apps"
100-
print("Compile wasm app into aot files")
101-
for wasm_file in wasm_files:
102-
CMD = []
103-
CMD.append(WAMRC_CMD)
104-
CMD.append("-o")
105-
CMD.append(wasm_apps_dir + "/" + wasm_file + ".aot")
106-
CMD.append(wasm_apps_dir + "/" + wasm_file + ".wasm")
107-
subprocess.check_call(CMD)
104+
wasm_apps_dir = args.working_directory + "/wasm-apps"
105+
compile_wasm_files_to_aot(wasm_apps_dir)
108106

109107
ret = 1
110108
app_server = None

‎samples/socket-api/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Say Hi from the Server
6868
send and receive data via 127.0.0.1:1234.
6969

7070
```bash
71-
$ ./iwasm --addr-pool=127.0.0.1/0 ./send_recv.wasm
71+
$ ./iwasm --addr-pool=127.0.0.1/0 ./send_recv.wasm
7272
```
7373

7474
The output is:
@@ -164,7 +164,7 @@ Datagram sent
164164

165165
`addr_resolve.wasm` demonstrates the usage of resolving a domain name
166166
```
167-
$ ./iwasm --allow-resolve=*.com addr_resolve.wasm github.com
167+
$ ./iwasm --allow-resolve=*.com addr_resolve.wasm github.com
168168
```
169169

170170
The command displays the host name and its corresponding IP address:

‎samples/socket-api/sample_test_run.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (C) 2023 Intel Corporation. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
import argparse
8+
import shlex
9+
import subprocess
10+
import sys
11+
import time
12+
import traceback
13+
import glob
14+
15+
WAMRC_CMD = "../../wamr-compiler/build/wamrc"
16+
17+
def compile_wasm_files_to_aot(wasm_apps_dir):
18+
wasm_files = glob.glob(wasm_apps_dir + "/*.wasm")
19+
print("Compile wasm app into aot files")
20+
for wasm_file in wasm_files:
21+
aot_file = wasm_file[0 : len(wasm_file) - 5] + ".aot";
22+
cmd = [ WAMRC_CMD, "-o", aot_file, wasm_file ]
23+
subprocess.check_call(cmd)
24+
25+
def start_server(cmd, cwd):
26+
app_server = subprocess.Popen(shlex.split(cmd), cwd=cwd)
27+
return app_server
28+
29+
def run_cmd(cmd, cwd):
30+
qry_prc = subprocess.run(
31+
shlex.split(cmd), cwd=cwd, check=False, capture_output=True
32+
)
33+
if (qry_prc.returncode != 0):
34+
print("Run {} failed, return {}".format(cmd), qry_prc.returncode)
35+
return
36+
print("return code: {}, output:\n{}".format(qry_prc.returncode,
37+
qry_prc.stdout.decode()))
38+
39+
def main():
40+
"""
41+
GO!GO!!GO!!!
42+
"""
43+
parser = argparse.ArgumentParser(description="run the sample and examine outputs")
44+
parser.add_argument("working_directory", type=str)
45+
parser.add_argument("--aot", action='store_true', help="Test with AOT")
46+
args = parser.parse_args()
47+
48+
test_aot = False
49+
suffix = ".wasm"
50+
if not args.aot:
51+
print("Test with interpreter mode")
52+
else:
53+
print("Test with AOT mode")
54+
test_aot = True
55+
suffix = ".aot"
56+
wasm_apps_dir = args.working_directory
57+
compile_wasm_files_to_aot(wasm_apps_dir)
58+
59+
ret = 1
60+
app_server = None
61+
try:
62+
print("\n================================")
63+
print("Test TCP server and client")
64+
cmd = "./iwasm --addr-pool=0.0.0.0/15 tcp_server" + suffix
65+
app_server = start_server(cmd, args.working_directory)
66+
# wait for a second
67+
time.sleep(1)
68+
cmd = "./iwasm --addr-pool=127.0.0.1/15 tcp_client" + suffix
69+
for i in range(5):
70+
run_cmd(cmd, args.working_directory)
71+
72+
print("\n================================")
73+
print("Test UDP server and client")
74+
cmd = "./iwasm --addr-pool=0.0.0.0/15 udp_server" + suffix
75+
app_server = start_server(cmd, args.working_directory)
76+
# wait for a second
77+
time.sleep(1)
78+
cmd = "./iwasm --addr-pool=127.0.0.1/15 udp_client" + suffix
79+
for i in range(5):
80+
run_cmd(cmd, args.working_directory)
81+
82+
print("\n=====================================================")
83+
print("Sleep 80 seconds to wait TCP server port actually close")
84+
time.sleep(80)
85+
86+
print("\n================================")
87+
print("Test send and receive")
88+
cmd = "./iwasm --addr-pool=127.0.0.1/0 ./send_recv" + suffix
89+
run_cmd(cmd, args.working_directory)
90+
91+
print("\n================================")
92+
print("Test socket options")
93+
cmd = "./iwasm socket_opts" + suffix
94+
run_cmd(cmd, args.working_directory)
95+
96+
print("\n================================")
97+
print("Test timeout server and client")
98+
cmd = "./iwasm --addr-pool=0.0.0.0/15 timeout_server" + suffix
99+
app_server = start_server(cmd, args.working_directory)
100+
# wait for a second
101+
time.sleep(1)
102+
cmd = "./iwasm --addr-pool=127.0.0.1/15 timeout_client" + suffix
103+
run_cmd(cmd, args.working_directory)
104+
105+
print("\n==========================================")
106+
print("Test multicast_client and multicast_server")
107+
cmd = "./iwasm --addr-pool=0.0.0.0/0,::/0 multicast_client.wasm 224.0.0.1"
108+
app_server = start_server(cmd, args.working_directory)
109+
# wait for a second
110+
time.sleep(1)
111+
cmd = "./multicast_server 224.0.0.1"
112+
run_cmd(cmd, args.working_directory)
113+
114+
cmd = "./iwasm --addr-pool=0.0.0.0/0,::/0 multicast_client.wasm FF02:113D:6FDD:2C17:A643:FFE2:1BD1:3CD2"
115+
app_server = start_server(cmd, args.working_directory)
116+
# wait for a second
117+
time.sleep(1)
118+
cmd = "./multicast_server FF02:113D:6FDD:2C17:A643:FFE2:1BD1:3CD2"
119+
run_cmd(cmd, args.working_directory)
120+
121+
print("\n================================")
122+
print("Test address resolving")
123+
cmd = "./iwasm --allow-resolve=*.com addr_resolve.wasm github.com"
124+
cmd = "./multicast_server FF02:113D:6FDD:2C17:A643:FFE2:1BD1:3CD2"
125+
run_cmd(cmd, args.working_directory)
126+
127+
# wait for a second
128+
time.sleep(1)
129+
130+
print("--> All pass")
131+
ret = 0
132+
except AssertionError:
133+
traceback.print_exc()
134+
finally:
135+
app_server.kill()
136+
137+
return ret
138+
139+
140+
if __name__ == "__main__":
141+
sys.exit(main())

‎samples/socket-api/wasm-src/send_recv.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
static pthread_mutex_t lock = { 0 };
2121
static pthread_cond_t cond = { 0 };
22+
static bool server_create_failed = false;
2223
static bool server_is_ready = false;
2324

2425
void *
@@ -46,13 +47,17 @@ run_as_server(void *arg)
4647
pthread_mutex_lock(&lock);
4748
sock = socket(AF_INET, SOCK_STREAM, 0);
4849
if (sock < 0) {
50+
server_create_failed = true;
51+
pthread_cond_signal(&cond);
4952
pthread_mutex_unlock(&lock);
5053
perror("Create a socket failed");
5154
return NULL;
5255
}
5356

5457
#ifndef __wasi__
5558
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
59+
server_create_failed = true;
60+
pthread_cond_signal(&cond);
5661
pthread_mutex_unlock(&lock);
5762
perror("Setsockopt failed");
5863
goto fail1;
@@ -66,12 +71,16 @@ run_as_server(void *arg)
6671

6772
addrlen = sizeof(addr);
6873
if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) {
74+
server_create_failed = true;
75+
pthread_cond_signal(&cond);
6976
pthread_mutex_unlock(&lock);
7077
perror("Bind failed");
7178
goto fail1;
7279
}
7380

7481
if (listen(sock, 0) < 0) {
82+
server_create_failed = true;
83+
pthread_cond_signal(&cond);
7584
pthread_mutex_unlock(&lock);
7685
perror("Listen failed");
7786
goto fail1;
@@ -117,11 +126,15 @@ run_as_client(void *arg)
117126
ssize_t recv_len = 0;
118127

119128
pthread_mutex_lock(&lock);
120-
while (false == server_is_ready) {
129+
while (!server_create_failed && !server_is_ready) {
121130
pthread_cond_wait(&cond, &lock);
122131
}
123132
pthread_mutex_unlock(&lock);
124133

134+
if (server_create_failed) {
135+
return NULL;
136+
}
137+
125138
printf("Client is running...\n");
126139
sock = socket(AF_INET, SOCK_STREAM, 0);
127140
if (sock < 0) {

‎samples/wasm-c-api-imports/wasm/send_recv.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
static pthread_mutex_t lock = { 0 };
2525
static pthread_cond_t cond = { 0 };
26+
static bool server_create_failed = false;
2627
static bool server_is_ready = false;
2728

2829
#ifdef __wasi__
@@ -71,13 +72,17 @@ run_as_server(void *arg)
7172
pthread_mutex_lock(&lock);
7273
sock = socket(AF_INET, SOCK_STREAM, 0);
7374
if (sock < 0) {
75+
server_create_failed = true;
76+
pthread_cond_signal(&cond);
7477
pthread_mutex_unlock(&lock);
7578
perror("Create a socket failed");
7679
return NULL;
7780
}
7881

7982
#ifndef __wasi__
8083
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))) {
84+
server_create_failed = true;
85+
pthread_cond_signal(&cond);
8186
pthread_mutex_unlock(&lock);
8287
perror("Setsockopt failed");
8388
goto fail1;
@@ -91,12 +96,16 @@ run_as_server(void *arg)
9196

9297
addrlen = sizeof(addr);
9398
if (bind(sock, (struct sockaddr *)&addr, addrlen) < 0) {
99+
server_create_failed = true;
100+
pthread_cond_signal(&cond);
94101
pthread_mutex_unlock(&lock);
95102
perror("Bind failed");
96103
goto fail1;
97104
}
98105

99106
if (listen(sock, 0) < 0) {
107+
server_create_failed = true;
108+
pthread_cond_signal(&cond);
100109
pthread_mutex_unlock(&lock);
101110
perror("Listen failed");
102111
goto fail1;
@@ -142,11 +151,15 @@ run_as_client(void *arg)
142151
ssize_t recv_len = 0;
143152

144153
pthread_mutex_lock(&lock);
145-
while (false == server_is_ready) {
154+
while (!server_create_failed && !server_is_ready) {
146155
pthread_cond_wait(&cond, &lock);
147156
}
148157
pthread_mutex_unlock(&lock);
149158

159+
if (server_create_failed) {
160+
return NULL;
161+
}
162+
150163
local_printf("Client is running...\n");
151164
sock = socket(AF_INET, SOCK_STREAM, 0);
152165
if (sock < 0) {

‎samples/workload/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ExternalProject_Add(iwasm
2424
CONFIGURE_COMMAND
2525
${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/../../product-mini/platforms/linux -B build -DWAMR_BUILD_LIBC_EMCC=1
2626
BUILD_COMMAND
27-
${CMAKE_COMMAND} --build build
27+
${CMAKE_COMMAND} --build build --parallel 4
2828
INSTALL_COMMAND
2929
# FIXME: replace with --install
3030
${CMAKE_COMMAND} -E copy_if_different
@@ -43,7 +43,7 @@ ExternalProject_Add(wamrc
4343
CONFIGURE_COMMAND
4444
${CMAKE_COMMAND} -S ${CMAKE_CURRENT_SOURCE_DIR}/../../wamr-compiler -B build
4545
BUILD_COMMAND
46-
${CMAKE_COMMAND} --build build
46+
${CMAKE_COMMAND} --build build --parallel 4
4747
INSTALL_COMMAND
4848
# FIXME: replace with --install
4949
${CMAKE_COMMAND} -E copy_if_different
@@ -113,4 +113,4 @@ add_test(
113113
./iwasm --dir=. testavx.aot ./wasm-av1/elephants_dream_480p24.ivf
114114
WORKING_DIRECTORY
115115
${CMAKE_CURRENT_BINARY_DIR}
116-
)
116+
)

‎samples/workload/bwa/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ExternalProject_Add(bwa
5050
-DCMAKE_SYSROOT=${WASISDK_SYSROOT}
5151
-DCMAKE_C_FLAGS=-isystem\ ${CMAKE_CURRENT_SOURCE_DIR}/../include/sse\ -isystem\ ${CMAKE_CURRENT_SOURCE_DIR}/../include/libc/musl
5252
${CMAKE_CURRENT_SOURCE_DIR}/bwa
53-
BUILD_COMMAND make bwa_wasm_opt
53+
BUILD_COMMAND make bwa_wasm_opt -j 4
5454
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different ./bwa.opt.wasm ${CMAKE_CURRENT_BINARY_DIR}/bwa.wasm
5555
)
5656

@@ -70,4 +70,4 @@ ExternalProject_Add(bwa-kit
7070
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
7171
${CMAKE_CURRENT_BINARY_DIR}/bwa-kit/src/bwa-kit/resource-GRCh38/hs38DH-extra.fa
7272
${CMAKE_CURRENT_BINARY_DIR}/hs38DH-extra.fa
73-
)
73+
)

‎samples/workload/meshoptimizer/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ include(ExternalProject)
2121
ExternalProject_Add(codecbench
2222
PREFIX codecbench
2323
GIT_REPOSITORY https://github.com/zeux/meshoptimizer.git
24-
GIT_TAG f926b288264522e1b331a41b07ba40167f396913
24+
GIT_TAG f734fd572aed5bf76e84d9ed62ca6f4f6c47d84e
2525
GIT_SHALLOW ON
2626
GIT_PROGRESS ON
2727
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/meshoptimizer
@@ -33,6 +33,6 @@ ExternalProject_Add(codecbench
3333
-DCMAKE_TOOLCHAIN_FILE=${WASISDK_TOOLCHAIN}
3434
-DCMAKE_SYSROOT=${WASISDK_SYSROOT}
3535
${CMAKE_CURRENT_SOURCE_DIR}/meshoptimizer
36-
BUILD_COMMAND make codecbench
36+
BUILD_COMMAND make codecbench -j 4
3737
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different ./codecbench.wasm ${CMAKE_CURRENT_BINARY_DIR}/codecbench.wasm
3838
)

‎samples/workload/meshoptimizer/codecbench.patch

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/CMakeLists.txt b/CMakeLists.txt
2-
index b13d946..4254003 100644
2+
index 612cf3b..22a365a 100644
33
--- a/CMakeLists.txt
44
+++ b/CMakeLists.txt
5-
@@ -149,3 +149,43 @@ install(FILES
5+
@@ -158,3 +158,43 @@ install(FILES
66
${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfigVersion.cmake
77
COMPONENT meshoptimizer
88
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/meshoptimizer)
@@ -47,10 +47,10 @@ index b13d946..4254003 100644
4747
+
4848
+add_dependencies(codecbench.opt codecbench)
4949
diff --git a/src/vertexcodec.cpp b/src/vertexcodec.cpp
50-
index 821c467..b7d30b1 100644
50+
index 4bd1112..257c258 100644
5151
--- a/src/vertexcodec.cpp
5252
+++ b/src/vertexcodec.cpp
53-
@@ -83,13 +83,13 @@
53+
@@ -89,13 +89,13 @@
5454
#endif
5555

5656
#ifdef SIMD_WASM
@@ -71,7 +71,7 @@ index 821c467..b7d30b1 100644
7171
#endif
7272

7373
namespace meshopt
74-
@@ -691,7 +691,7 @@ static v128_t decodeShuffleMask(unsigned char mask0, unsigned char mask1)
74+
@@ -757,7 +757,7 @@ static v128_t decodeShuffleMask(unsigned char mask0, unsigned char mask1)
7575
v128_t sm1 = wasm_v128_load(&kDecodeBytesGroupShuffle[mask1]);
7676

7777
v128_t sm1off = wasm_v128_load(&kDecodeBytesGroupCount[mask0]);
@@ -80,7 +80,7 @@ index 821c467..b7d30b1 100644
8080

8181
v128_t sm1r = wasm_i8x16_add(sm1, sm1off);
8282

83-
@@ -741,7 +741,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
83+
@@ -807,7 +807,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
8484

8585
v128_t shuf = decodeShuffleMask(mask0, mask1);
8686

@@ -89,7 +89,7 @@ index 821c467..b7d30b1 100644
8989

9090
wasm_v128_store(buffer, result);
9191

92-
@@ -763,7 +763,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
92+
@@ -829,7 +829,7 @@ static const unsigned char* decodeBytesGroupSimd(const unsigned char* data, unsi
9393

9494
v128_t shuf = decodeShuffleMask(mask0, mask1);
9595

@@ -99,7 +99,7 @@ index 821c467..b7d30b1 100644
9999
wasm_v128_store(buffer, result);
100100

101101
diff --git a/src/vertexfilter.cpp b/src/vertexfilter.cpp
102-
index 14a73b1..8f4b3c1 100644
102+
index 5c7589c..c79cad4 100644
103103
--- a/src/vertexfilter.cpp
104104
+++ b/src/vertexfilter.cpp
105105
@@ -57,10 +57,10 @@
@@ -116,4 +116,4 @@ index 14a73b1..8f4b3c1 100644
116116
+#define wasmx_unziphi_v32x4(a, b) wasm_i32x4_shuffle(a, b, 1, 3, 5, 7)
117117
#endif
118118

119-
namespace meshopt
119+
#ifndef __has_builtin

‎samples/workload/wasm-av1/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ExternalProject_Add(av1
3636
-DCMAKE_SYSROOT=${WASISDK_SYSROOT}
3737
-DCMAKE_C_FLAGS=-isystem\ ${CMAKE_CURRENT_SOURCE_DIR}/../include/sse\ -isystem\ ${CMAKE_CURRENT_SOURCE_DIR}/../include/libc/musl
3838
${CMAKE_CURRENT_SOURCE_DIR}/av1
39-
BUILD_COMMAND make testavx_opt
39+
BUILD_COMMAND make testavx_opt -j 4
4040
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_if_different
4141
testavx.opt.wasm
4242
${CMAKE_CURRENT_SOURCE_DIR}/av1/third_party/samples/elephants_dream_480p24.ivf

0 commit comments

Comments
 (0)
Please sign in to comment.