Skip to content
Snippets Groups Projects
Commit 2798c194 authored by Mohammad Mahhouk's avatar Mohammad Mahhouk
Browse files

some more changes and extra benchmarking adjustments

parent 7604ebc9
No related branches found
No related tags found
No related merge requests found
......@@ -64,22 +64,20 @@ if (NOT EXISTS ${WASI_SDK_DIR})
)
endif()
if(NOT BENCH_ITERATIONS)
SET(BENCH_ITERATIONS 1000000)
endif()
if(NOT VARIANT)
SET(VARIANT 1)
endif()
SET(DEFAULT_BENCHMARK_ITERATIONS 1000)
if(BENCH_ITERATIONS)
add_custom_command(
OUTPUT wasm_apps
COMMAND cd wasm_apps && BENCH_ITERATIONS=${BENCH_ITERATIONS} make all && mv *.wasm ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
else()
add_custom_command(
OUTPUT wasm_apps
COMMAND cd wasm_apps && BENCH_ITERATIONS=${DEFAULT_BENCHMARK_ITERATIONS} make all && mv *.wasm ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
add_custom_command(
OUTPUT wasm_apps
COMMAND cd wasm_apps && BENCH_ITERATIONS=${BENCH_ITERATIONS} VARIANT=${VARIANT} make all && mv *.wasm ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
file(GLOB WASM_FILES
"${CMAKE_BINARY_DIR}/add.wasm"
......@@ -113,19 +111,28 @@ set(EXPORT_KEY_VALUE_SOURCE_FILES export_key_value/export_key_value.cpp)
add_executable(export_key_value ${EXPORT_KEY_VALUE_SOURCE_FILES})
target_include_directories(export_key_value PUBLIC ${WAMR_EMBEDDING_API_INCLUDE_PATH} ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(export_key_value PRIVATE vmlib pthread m)
if(BENCH_ITERATIONS)
target_compile_definitions(export_key_value PUBLIC -DBENCH_ITERATIONS=${BENCH_ITERATIONS})
else()
target_compile_definitions(export_key_value PUBLIC -DBENCH_ITERATIONS=${DEFAULT_BENCHMARK_ITERATIONS})
endif()
target_compile_definitions(export_key_value PUBLIC -DBENCH_ITERATIONS=${BENCH_ITERATIONS} -DVARIANT=${VARIANT})
set(EXPORT_KEY_VALUE_SHARED_MEM_SOURCE_FILES export_key_value_shared_mem/export_key_value_shared_mem.cpp)
add_executable(export_key_value_shared_mem ${EXPORT_KEY_VALUE_SHARED_MEM_SOURCE_FILES})
target_include_directories(export_key_value_shared_mem PUBLIC ${WAMR_EMBEDDING_API_INCLUDE_PATH} ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(export_key_value_shared_mem PRIVATE vmlib pthread m)
if(BENCH_ITERATIONS)
target_compile_definitions(export_key_value_shared_mem PUBLIC -DBENCH_ITERATIONS=${BENCH_ITERATIONS})
else()
target_compile_definitions(export_key_value_shared_mem PUBLIC -DBENCH_ITERATIONS=${DEFAULT_BENCHMARK_ITERATIONS})
endif()
target_compile_definitions(export_key_value_shared_mem PUBLIC -DBENCH_ITERATIONS=${BENCH_ITERATIONS} -DVARIANT=${VARIANT})
#if(BENCH_ITERATIONS)
# target_compile_definitions(export_key_value PUBLIC -DBENCH_ITERATIONS=${BENCH_ITERATIONS})
# target_compile_definitions(export_key_value_shared_mem PUBLIC -DBENCH_ITERATIONS=${BENCH_ITERATIONS})
#else()
# target_compile_definitions(export_key_value PUBLIC -DBENCH_ITERATIONS=${DEFAULT_BENCHMARK_ITERATIONS})
# target_compile_definitions(export_key_value_shared_mem PUBLIC -DBENCH_ITERATIONS=${DEFAULT_BENCHMARK_ITERATIONS})
#endif()
#
#if(VARIANT)
# target_compile_definitions(export_key_value PUBLIC -DVARIANT=${VARIANT})
# target_compile_definitions(export_key_value_shared_mem PUBLIC -DVARIANT=${VARIANT})
#else()
# target_compile_definitions(export_key_value PUBLIC -DVARIANT=${DEFAULT_VARIANT})
# target_compile_definitions(export_key_value_shared_mem PUBLIC -DVARIANT=${DEFAULT_VARIANT})
#endif()
\ No newline at end of file
#include <bits/stdint-uintn.h>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
......@@ -23,16 +24,34 @@ std::map<int, char *> int_string_map = {{}};
int get_key_value(wasm_exec_env_t exec_env, int index)
{
if(index >= int_string_map.size())
if(!int_string_map.count(index))
{
printf("Error(get_key_value): index out of bound \n");
exit(EXIT_FAILURE);
}
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasm_function_inst_t func = wasm_runtime_lookup_function(module_inst, "module_malloc", "(i)i");
#if defined VARIANT && VARIANT == 1
/* Variant 1 */
return wasm_runtime_addr_native_to_app(module_inst, int_string_map[index]);
#endif
#if defined VARIANT && VARIANT == 2
/* Variant 2 */
char *native_memory = NULL;
uint32_t wasm_buffer= 0;
uint32_t value_size = strlen(int_string_map[index]);
wasm_buffer = wasm_runtime_module_malloc(module_inst, value_size,(void **)&native_memory); // should be later free either from host or export wasm_runtime_module_free() as a native function
memcpy(native_memory, int_string_map[index], value_size);
return wasm_buffer;
#endif
#if defined VARIANT && VARIANT == 3
/* Variant 3 */
wasm_function_inst_t func = wasm_runtime_lookup_function(module_inst, "module_malloc", "(i)i");
uint32_t args[2] = {0};
args[0] = strlen(int_string_map[index]);
if (wasm_runtime_call_wasm(exec_env, func, 1, args) ) {
......@@ -47,6 +66,12 @@ int get_key_value(wasm_exec_env_t exec_env, int index)
printf("%s\n", wasm_runtime_get_exception(module_inst));
exit(EXIT_FAILURE);
}
#endif
}
void native_module_free(wasm_exec_env_t exec_env, uint32_t ptr)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
wasm_runtime_module_free(module_inst, ptr);
}
void set_key_value(wasm_exec_env_t exec_env, int index, char *value)
......@@ -106,16 +131,22 @@ int main(int argc, char *argv[])
static NativeSymbol native_symbols[] =
{
{
"set_key_value", // the name of WASM function name
(void *) set_key_value, // the native function pointer
"(i$)", // the function prototype signature
NULL, // no attachments
"set_key_value", // the name of WASM function name
(void *) set_key_value, // the native function pointer
"(i$)", // the function prototype signature
NULL, // no attachments
},
{
"get_key_value", // the name of WASM function name
(void *) get_key_value, // the native function pointer
"(i)i", // the function prototype signature
NULL, // no attachments
},
{
"get_key_value", // the name of WASM function name
(void *) get_key_value, // the native function pointer
"(i)i", // the function prototype signature
NULL, // no attachments
"native_module_free", // the name of WASM function name
(void *) native_module_free, // the native function pointer
"(i)", // the function prototype signature
NULL // no attachments
}
};
......
......@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
char *buffer, error_buf[128];
wasm_module_t module;
wasm_module_inst_t module_inst;
wasm_function_inst_t func;
wasm_function_inst_t func, addfunc;
wasm_exec_env_t exec_env;
uint32_t size, stack_size = 8092, heap_size = 8092;
......@@ -147,7 +147,7 @@ int main(int argc, char *argv[])
uint32_t args[4] = {0};
args[0] = 1;
args[1] = 1;
args[2] = 2;
args[2] = 1;
if (wasm_runtime_call_wasm(exec_env, func, 3, args) ) {
/* the return value is stored in argv[0] */
printf("mutladd function returned %d \n", args[0]);
......@@ -156,6 +156,38 @@ int main(int argc, char *argv[])
/* exception is thrown if call fails */
printf("%s\n", wasm_runtime_get_exception(module_inst));
}
args[0] = 1;
args[1] = 1;
args[2] = 1;
if (wasm_runtime_call_wasm(exec_env, func, 3, args) ) {
/* the return value is stored in argv[0] */
printf("mutladd function returned %d \n", args[0]);
}
else {
/* exception is thrown if call fails */
printf("%s\n", wasm_runtime_get_exception(module_inst));
}
addfunc = wasm_runtime_lookup_function(module_inst, "externalCheck", "()");
if (addfunc == NULL) {
printf("add function not found\n");
return -1;
}
char *add_args[1]= {0};
if (wasm_application_execute_func(module_inst,"$add.wasm$check", 0 , &add_args[0])) {
//if (wasm_runtime_call_wasm(exec_env, addfunc, 0, add_args) ) {
/* the return value is stored in argv[0] */
//printf("add function returned %d \n", add_args[0]);
}
else {
/* exception is thrown if call fails */
printf("%s\n", wasm_runtime_get_exception(module_inst));
}
wasm_runtime_destroy();
return 0;
......
BENCH_ITERATIONS?=1000
VARIANT?=VARIANT1
C := ../wasi-sdk/bin/clang
#CFLAGS := --sysroot=../wasi-sdk-12.0/share/wasi-sysroot -Wl,--no-entry,--allow-undefined,--export-all -O3
CFLAGS := --sysroot=../wasi-sdk/share/wasi-sysroot -Wl,--no-entry -O3 -DBENCH_ITERATIONS=$(BENCH_ITERATIONS)
CFLAGS := --sysroot=../wasi-sdk/share/wasi-sysroot -Wl,--no-entry -O3 -DBENCH_ITERATIONS=$(BENCH_ITERATIONS) -DVARIANT=$(VARIANT)
.PHONY: test add multadd key_value key_value_bench kv_shared_mem all
......@@ -19,4 +19,4 @@ clean:
rm -f *.wasm
%.wasm: %.c
$(C) $(CFLAGS) -o $@ $<
@echo "The benchmark iterations are set to $(BENCH_ITERATIONS)!"
\ No newline at end of file
@echo "The benchmark iterations are set to $(BENCH_ITERATIONS) and the variant is set to $(VARIANT)!"
\ No newline at end of file
#include <stdio.h>
#define export(name) __attribute__((export_name(name))) __attribute__((visibility("default")))
int sum=10;
export("addFunc")
int addFunc(int a, int b){
return a+b;
sum += a+b;
return sum;
}
export("check")
void check()
{
sum+= 2;
printf("sum is equal to %d\n", sum);
}
export("main")
int main()
{
int result = addFunc(1,1);
printf("1 + 1 is %d quick math\n", result);
printf("1 + 1 is %d quick quick quick math\n", result);
return result;
}
\ No newline at end of file
......@@ -12,6 +12,9 @@ extern char* get_key_value(int index);
import("set_key_value")
extern void set_key_value(int index, char *value);
import("native_module_free")
extern void native_module_free(void *ptr);
export("module_malloc")
char *module_malloc(int size){
if(size <= 0) return NULL;
......@@ -34,8 +37,20 @@ int main(){
printf("getting index 0 is %s\n", result);
printf("getting index 1 is %s\n", result2);
printf("getting index 2 is %s\n", result3);
free(result);
#if defined VARIANT && VARIANT == 2
/* Variant 2 */
native_module_free(result);
native_module_free(result2);
native_module_free(result3);
printf("getting index 0 after free is %s\n", result);
printf("getting index 1 after free is %s\n", result2);
printf("getting index 2 after free is %s\n", result3);
#endif
/* Variant 3 */
/*free(result);
free(result2);
free(result3);
free(result3);*/
return 0;
}
......@@ -5,6 +5,8 @@
#define export(name) __attribute__((export_name(name))) __attribute__((visibility("default")))
import("native_module_free")
extern void native_module_free(char *ptr);
import("get_key_value")
extern char* get_key_value(int index);
......@@ -26,7 +28,12 @@ int main(){
for(int i= 0; i<BENCH_ITERATIONS; i++){
result = get_key_value(0);
//printf("getting index 0 is %s\n", result);
#if defined VARIANT && VARIANT == 2
native_module_free(result); //breaks down if not free
#endif
#if defined VARIANT && VARIANT == 3
free(result);
#endif
}
return 0;
}
......@@ -5,6 +5,15 @@
__attribute__((import_module("add.wasm"))) import("addFunc")
extern int addFunc(int a, int b);
__attribute__((import_module("add.wasm"))) import("check")
extern void check();
export("externalCheck")
void externalCheck()
{
check();
}
export("multadd")
int multadd(int a, int b, int c)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment