summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:19:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:20:02 +0000
commit58daab21cd043e1dc37024a7f99b396788372918 (patch)
tree96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread
parentReleasing debian version 1.43.2-1. (diff)
downloadnetdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz
netdata-58daab21cd043e1dc37024a7f99b396788372918.zip
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread')
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/CMakeLists.txt79
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/src/main.c242
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/CMakeLists.txt38
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/sum.c16
4 files changed, 375 insertions, 0 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/CMakeLists.txt
new file mode 100644
index 00000000..7b76311d
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/CMakeLists.txt
@@ -0,0 +1,79 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 3.14)
+
+include(CheckPIESupported)
+
+project(spawn_thread)
+
+################ runtime settings ################
+string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
+if (APPLE)
+ add_definitions(-DBH_PLATFORM_DARWIN)
+endif ()
+
+# Resetdefault linker flags
+set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
+
+# WAMR features switch
+
+# Set WAMR_BUILD_TARGET, currently values supported:
+# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
+# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
+if (NOT DEFINED WAMR_BUILD_TARGET)
+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
+ set (WAMR_BUILD_TARGET "AARCH64")
+ elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
+ set (WAMR_BUILD_TARGET "RISCV64")
+ elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
+ # Build as X86_64 by default in 64-bit platform
+ set (WAMR_BUILD_TARGET "X86_64")
+ elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
+ # Build as X86_32 by default in 32-bit platform
+ set (WAMR_BUILD_TARGET "X86_32")
+ else ()
+ message(SEND_ERROR "Unsupported build target platform!")
+ endif ()
+endif ()
+
+if (NOT CMAKE_BUILD_TYPE)
+ set (CMAKE_BUILD_TYPE Release)
+endif ()
+
+set(WAMR_BUILD_INTERP 1)
+set(WAMR_BUILD_AOT 1)
+set(WAMR_BUILD_JIT 0)
+set(WAMR_BUILD_LIBC_BUILTIN 1)
+set(WAMR_BUILD_FAST_INTERP 1)
+set(WAMR_BUILD_LIB_PTHREAD 1)
+
+# compiling and linking flags
+if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
+ set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
+endif ()
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
+
+# build out vmlib
+set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
+
+add_library(vmlib ${WAMR_RUNTIME_LIB_SOURCE})
+################################################
+
+
+################ wasm application ################
+add_subdirectory(wasm-apps)
+
+################ wamr runtime ################
+include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
+
+set (RUNTIME_SOURCE_ALL
+ ${CMAKE_CURRENT_LIST_DIR}/src/main.c
+ ${UNCOMMON_SHARED_SOURCE}
+)
+add_executable (spawn_thread ${RUNTIME_SOURCE_ALL})
+check_pie_supported()
+set_target_properties (spawn_thread PROPERTIES POSITION_INDEPENDENT_CODE ON)
+target_link_libraries(spawn_thread vmlib -lpthread -lm)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/src/main.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/src/main.c
new file mode 100644
index 00000000..9501ae4c
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/src/main.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "wasm_export.h"
+#include "bh_read_file.h"
+#include "pthread.h"
+
+#define THREAD_NUM 10
+
+typedef struct ThreadArgs {
+ wasm_exec_env_t exec_env;
+ int start;
+ int length;
+} ThreadArgs;
+
+void *
+thread(void *arg)
+{
+ ThreadArgs *thread_arg = (ThreadArgs *)arg;
+ wasm_exec_env_t exec_env = thread_arg->exec_env;
+ wasm_module_inst_t module_inst = get_module_inst(exec_env);
+ wasm_function_inst_t func;
+ uint32 argv[2];
+
+ if (!wasm_runtime_init_thread_env()) {
+ printf("failed to initialize thread environment");
+ return NULL;
+ }
+
+ func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
+ if (!func) {
+ printf("failed to lookup function sum");
+ wasm_runtime_destroy_thread_env();
+ return NULL;
+ }
+ argv[0] = thread_arg->start;
+ argv[1] = thread_arg->length;
+
+ /* call the WASM function */
+ if (!wasm_runtime_call_wasm(exec_env, func, 2, argv)) {
+ printf("%s\n", wasm_runtime_get_exception(module_inst));
+ wasm_runtime_destroy_thread_env();
+ return NULL;
+ }
+
+ wasm_runtime_destroy_thread_env();
+ return (void *)(uintptr_t)argv[0];
+}
+
+void *
+wamr_thread_cb(wasm_exec_env_t exec_env, void *arg)
+{
+ ThreadArgs *thread_arg = (ThreadArgs *)arg;
+ wasm_module_inst_t module_inst = get_module_inst(exec_env);
+ wasm_function_inst_t func;
+ uint32 argv[2];
+
+ func = wasm_runtime_lookup_function(module_inst, "sum", NULL);
+ if (!func) {
+ printf("failed to lookup function sum");
+ return NULL;
+ }
+ argv[0] = thread_arg->start;
+ argv[1] = thread_arg->length;
+
+ /* call the WASM function */
+ if (!wasm_runtime_call_wasm(exec_env, func, 2, argv)) {
+ printf("%s\n", wasm_runtime_get_exception(module_inst));
+ return NULL;
+ }
+
+ return (void *)(uintptr_t)argv[0];
+}
+
+int
+main(int argc, char *argv[])
+{
+ char *wasm_file = "wasm-apps/test.wasm";
+ uint8 *wasm_file_buf = NULL;
+ uint32 wasm_file_size, wasm_argv[2], i, threads_created;
+ uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
+ wasm_module_t wasm_module = NULL;
+ wasm_module_inst_t wasm_module_inst = NULL;
+ wasm_exec_env_t exec_env = NULL;
+ RuntimeInitArgs init_args;
+ ThreadArgs thread_arg[THREAD_NUM];
+ pthread_t tid[THREAD_NUM];
+ wasm_thread_t wasm_tid[THREAD_NUM];
+ uint32 result[THREAD_NUM], sum;
+ wasm_function_inst_t func;
+ char error_buf[128] = { 0 };
+
+ memset(thread_arg, 0, sizeof(ThreadArgs) * THREAD_NUM);
+ memset(&init_args, 0, sizeof(RuntimeInitArgs));
+ init_args.mem_alloc_type = Alloc_With_Allocator;
+ init_args.mem_alloc_option.allocator.malloc_func = malloc;
+ init_args.mem_alloc_option.allocator.realloc_func = realloc;
+ init_args.mem_alloc_option.allocator.free_func = free;
+ init_args.max_thread_num = THREAD_NUM;
+
+ /* initialize runtime environment */
+ if (!wasm_runtime_full_init(&init_args)) {
+ printf("Init runtime environment failed.\n");
+ return -1;
+ }
+
+ /* load WASM byte buffer from WASM bin file */
+ if (!(wasm_file_buf =
+ (uint8 *)bh_read_file_to_buffer(wasm_file, &wasm_file_size)))
+ goto fail1;
+
+ /* load WASM module */
+ if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
+ error_buf, sizeof(error_buf)))) {
+ printf("%s\n", error_buf);
+ goto fail2;
+ }
+
+ /* instantiate the module */
+ if (!(wasm_module_inst =
+ wasm_runtime_instantiate(wasm_module, stack_size, heap_size,
+ error_buf, sizeof(error_buf)))) {
+ printf("%s\n", error_buf);
+ goto fail3;
+ }
+
+ /* Create the first exec_env */
+ if (!(exec_env =
+ wasm_runtime_create_exec_env(wasm_module_inst, stack_size))) {
+ printf("failed to create exec_env\n");
+ goto fail4;
+ }
+
+ func = wasm_runtime_lookup_function(wasm_module_inst, "sum", NULL);
+ if (!func) {
+ printf("failed to lookup function sum");
+ goto fail5;
+ }
+ wasm_argv[0] = 0;
+ wasm_argv[1] = THREAD_NUM * 10;
+
+ /*
+ * Execute the wasm function in current thread, get the expect result
+ */
+ if (!wasm_runtime_call_wasm(exec_env, func, 2, wasm_argv)) {
+ printf("%s\n", wasm_runtime_get_exception(wasm_module_inst));
+ }
+ printf("expect result: %d\n", wasm_argv[0]);
+
+ /*
+ * Run wasm function in multiple thread created by pthread_create
+ */
+ memset(thread_arg, 0, sizeof(ThreadArgs) * THREAD_NUM);
+ for (i = 0; i < THREAD_NUM; i++) {
+ wasm_exec_env_t new_exec_env;
+ thread_arg[i].start = 10 * i;
+ thread_arg[i].length = 10;
+
+ /* spawn a new exec_env to be executed in other threads */
+ new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
+ if (new_exec_env)
+ thread_arg[i].exec_env = new_exec_env;
+ else {
+ printf("failed to spawn exec_env\n");
+ break;
+ }
+
+ /* If we use:
+ thread_arg[i].exec_env = exec_env,
+ we may get wrong result */
+
+ if (0 != pthread_create(&tid[i], NULL, thread, &thread_arg[i])) {
+ printf("failed to create thread.\n");
+ wasm_runtime_destroy_spawned_exec_env(new_exec_env);
+ break;
+ }
+ }
+
+ threads_created = i;
+
+ sum = 0;
+ memset(result, 0, sizeof(uint32) * THREAD_NUM);
+ for (i = 0; i < threads_created; i++) {
+ pthread_join(tid[i], (void **)&result[i]);
+ sum += result[i];
+ /* destroy the spawned exec_env */
+ if (thread_arg[i].exec_env)
+ wasm_runtime_destroy_spawned_exec_env(thread_arg[i].exec_env);
+ }
+
+ printf("[pthread]sum result: %d\n", sum);
+
+ /*
+ * Run wasm function in multiple thread created by wamr spawn API
+ */
+ memset(thread_arg, 0, sizeof(ThreadArgs) * THREAD_NUM);
+ for (i = 0; i < THREAD_NUM; i++) {
+ thread_arg[i].start = 10 * i;
+ thread_arg[i].length = 10;
+
+ /* No need to spawn exec_env manually */
+ if (0
+ != wasm_runtime_spawn_thread(exec_env, &wasm_tid[i], wamr_thread_cb,
+ &thread_arg[i])) {
+ printf("failed to spawn thread.\n");
+ break;
+ }
+ }
+
+ threads_created = i;
+
+ sum = 0;
+ memset(result, 0, sizeof(uint32) * THREAD_NUM);
+ for (i = 0; i < threads_created; i++) {
+ wasm_runtime_join_thread(wasm_tid[i], (void **)&result[i]);
+ sum += result[i];
+ /* No need to destroy the spawned exec_env */
+ }
+ printf("[spwan_thread]sum result: %d\n", sum);
+
+fail5:
+ wasm_runtime_destroy_exec_env(exec_env);
+
+fail4:
+ /* destroy the module instance */
+ wasm_runtime_deinstantiate(wasm_module_inst);
+
+fail3:
+ /* unload the module */
+ wasm_runtime_unload(wasm_module);
+
+fail2:
+ /* free the file buffer */
+ wasm_runtime_free(wasm_file_buf);
+
+fail1:
+ /* destroy runtime environment */
+ wasm_runtime_destroy();
+ return 0;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/CMakeLists.txt b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/CMakeLists.txt
new file mode 100644
index 00000000..52ee7d75
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/CMakeLists.txt
@@ -0,0 +1,38 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+cmake_minimum_required(VERSION 2.8)
+project(wasm-apps)
+
+set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
+
+if (APPLE)
+ set (HAVE_FLAG_SEARCH_PATHS_FIRST 0)
+ set (CMAKE_C_LINK_FLAGS "")
+ set (CMAKE_CXX_LINK_FLAGS "")
+endif ()
+set(CMAKE_SYSTEM_PROCESSOR wasm32)
+set (CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
+
+if (NOT DEFINED WASI_SDK_DIR)
+ set (WASI_SDK_DIR "/opt/wasi-sdk")
+endif ()
+
+set (CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments")
+set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=32768")
+set (CMAKE_C_COMPILER_TARGET "wasm32")
+set (CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
+
+set (DEFINED_SYMBOLS
+"${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
+
+set (CMAKE_EXE_LINKER_FLAGS
+ "-Wl,--shared-memory,--max-memory=131072, \
+ -Wl,--no-entry,--strip-all,--export=sum, \
+ -Wl,--export=__heap_base,--export=__data_end \
+ -Wl,--export=__wasm_call_ctors \
+ -Wl,--allow-undefined-file=${DEFINED_SYMBOLS}"
+)
+
+add_executable(test.wasm sum.c)
+target_link_libraries(test.wasm)
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/sum.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/sum.c
new file mode 100644
index 00000000..0bcb8918
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/samples/spawn-thread/wasm-apps/sum.c
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+int
+sum(int start, int length)
+{
+ int sum = 0, i;
+
+ for (i = start; i < start + length; i++) {
+ sum += i;
+ }
+
+ return sum;
+} \ No newline at end of file