Skip to content

Commit

Permalink
Merge branch 'mutex' of github.com:Multi-V-VM/MVVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Zhao committed Jan 23, 2024
2 parents 268eb45 + e0f4939 commit c56c44a
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 91 deletions.
8 changes: 4 additions & 4 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ExternalProject_Add(ftp
CONFIGURE_COMMAND ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=Release
-GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK}
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk-pthread.cmake
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake
-DCMAKE_EXE_LINKER_FLAGS="-Wl,--export-all -Wl,--allow-undefined-file=${WASI_SDK}/share/wasi-sysroot/share/wasm32-wasi-threads/undefined-symbols.txt"
-DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot -DCMAKE_C_COMPILER_WORK=1 -DCMAKE_CXX_COMPILER_WORK=1
${CMAKE_CURRENT_SOURCE_DIR}/ftp
Expand All @@ -97,7 +97,7 @@ ExternalProject_Add(nas
CONFIGURE_COMMAND ${CMAKE_COMMAND}
-DCMAKE_BUILD_TYPE=Release
-GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK}
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk-pthread.cmake
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake
-DCMAKE_EXE_LINKER_FLAGS="-Wl,--export-all -Wl,--allow-undefined-file=${WASI_SDK}/share/wasi-sysroot/share/wasm32-wasi-threads/undefined-symbols.txt"
-DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot -DCMAKE_C_COMPILER_WORK=1 -DCMAKE_CXX_COMPILER_WORK=1
${CMAKE_CURRENT_SOURCE_DIR}/nas
Expand Down Expand Up @@ -132,7 +132,7 @@ ExternalProject_Add(linpack
PATCH_COMMAND ""
CONFIGURE_COMMAND ${CMAKE_COMMAND}
-GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK}
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk-pthread.cmake
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_EXE_LINKER_FLAGS="-Wl,--export-all -Wl,--allow-undefined-file=${WASI_SDK}/share/wasi-sysroot/share/wasm32-wasi-threads/undefined-symbols.txt"
-DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot -DCMAKE_C_COMPILER_WORK=1 -DCMAKE_CXX_COMPILER_WORK=1
Expand All @@ -146,7 +146,7 @@ ExternalProject_Add(hdastar
PATCH_COMMAND ""
CONFIGURE_COMMAND ${CMAKE_COMMAND}
-GNinja -DWASI_SDK_DIR=${WASI_SDK} -DWASI_SDK_PREFIX=${WASI_SDK}
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk-pthread.cmake
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_EXE_LINKER_FLAGS="-Wl,--export-all -Wl,--allow-undefined-file=${WASI_SDK}/share/wasi-sysroot/share/wasm32-wasi-threads/undefined-symbols.txt"
-DCMAKE_SYSROOT=${WASI_SDK}/share/wasi-sysroot -DCMAKE_C_COMPILER_WORK=1 -DCMAKE_CXX_COMPILER_WORK=1
Expand Down
14 changes: 14 additions & 0 deletions include/wamr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "wamr_wasi_context.h"
#include "wasm_runtime.h"
#include <mutex>
#include <condition_variable>
#include <semaphore>
#include <iterator>
#include <ranges>
#include <tuple>

Expand All @@ -42,9 +45,20 @@ class WAMRInstance {
std::map<int, SocketMetaData> socket_fd_map_{};
SocketAddrPool local_addr{};
// lwcp is LightWeight CheckPoint
std::map<ssize_t, int> lwcp_list;
size_t ready = 0;
std::mutex as_mtx;

std::vector<struct sync_op_t> sync_ops;
void replay_sync_ops(bool, wasm_exec_env_t);
void register_tid_map();
std::vector<struct sync_op_t>::iterator sync_iter;
std::mutex sync_op_mutex;
std::condition_variable sync_op_cv;
std::map<ssize_t, ssize_t> tid_map;
ssize_t cur_thread;


bool is_jit;
bool is_aot{};
char error_buf[128]{};
Expand Down
2 changes: 1 addition & 1 deletion include/wamr_exec_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct WAMRExecEnv { // multiple
//
// /* Previous thread's exec env of a WASM module instance. */
// struct WASMExecEnv *prev;
uint8 cur_count{};
ssize_t cur_count{};

/* Note: field module_inst, argv_buf, native_stack_boundary,
susƒend_flags, aux_stack_boundary, aux_stack_bottom, and
Expand Down
15 changes: 14 additions & 1 deletion include/wamr_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ struct SocketAddrPool {
uint16 port;
};
enum fd_op { MVVM_FOPEN = 0, MVVM_FWRITE = 1, MVVM_FREAD = 2, MVVM_FSEEK = 3 };

enum sync_op{
SYNC_OP_MUTEX_LOCK,
SYNC_OP_MUTEX_UNLOCK,
};

struct sync_op_t {
ssize_t tid;
uint32 ref;
enum sync_op sync_op;
};

#if !defined(_WIN32)
void insert_sock_send_to_data(uint32_t, uint8 *, uint32, uint16_t, __wasi_addr_t *);
void insert_sock_open_data(uint32_t, int, int, uint32_t);
Expand All @@ -29,6 +41,7 @@ void init_gateway(struct SocketAddrPool *address);
void set_tcp();
void insert_lock(char const *, int);
void insert_sem(char const *, int);
void insert_sync_op(wasm_exec_env_t exec_env, uint32* mutex, enum sync_op locking);
void remove_lock(char const *);
void remove_sem(char const *);
void restart_execution(uint32 targs);
Expand All @@ -43,7 +56,7 @@ void remove_fd(int);
void rename_fd(int, char const *, int, char const *);
void lightweight_checkpoint(WASMExecEnv *);
void lightweight_uncheckpoint(WASMExecEnv *);
void wamr_wait();
void wamr_wait(wasm_exec_env_t);
void sigint_handler(int sig);
void register_sigtrap();
void sigtrap_handler(int sig);
Expand Down
8 changes: 5 additions & 3 deletions include/wamr_memory_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct WAMRMemoryInstance {
/* Module type */
uint32 module_type;
/* Shared memory flag */
bool is_shared;
uint16 ref_count;
/* Number bytes per page */
uint32 num_bytes_per_page;
/* Current page count */
Expand All @@ -33,7 +33,8 @@ struct WAMRMemoryInstance {

void dump_impl(WASMMemoryInstance *env) {
module_type = env->module_type;
is_shared = env->ref_count;
ref_count = env->ref_count;
LOGV(ERROR)<< "ref_count:" << ref_count;
num_bytes_per_page = env->num_bytes_per_page;
cur_page_count = env->cur_page_count;
max_page_count = env->max_page_count;
Expand All @@ -48,7 +49,8 @@ struct WAMRMemoryInstance {
};
void restore_impl(WASMMemoryInstance *env) {
env->module_type = module_type;
env->ref_count = is_shared;
env->ref_count = ref_count+1;
LOGV(ERROR)<< "ref_count:" << env->ref_count;
env->num_bytes_per_page = num_bytes_per_page;
env->cur_page_count = cur_page_count;
env->max_page_count = max_page_count;
Expand Down
1 change: 1 addition & 0 deletions include/wamr_wasi_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct SocketMetaData {
struct WAMRWASIContext {
std::map<int, std::tuple<std::string, std::vector<std::tuple<int, int, fd_op>>>> fd_map;
std::map<int, SocketMetaData> socket_fd_map;
std::vector<struct sync_op_t> sync_ops;
std::vector<std::string> dir;
std::vector<std::string> map_dir;
std::vector<std::string> arg;
Expand Down
77 changes: 43 additions & 34 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,45 +142,54 @@ void serialize_to_file(WASMExecEnv *instance) {
// Clean up
close(fd);
}
auto cluster = wasm_exec_env_get_cluster(instance);
// wasm_cluster_suspend_all_except_self(cluster, instance);
auto all_count = bh_list_length(&cluster->exec_env_list);
int cur_count = 0;
if (all_count > 1) {
auto elem = (WASMExecEnv *)bh_list_first_elem(&cluster->exec_env_list);
while (elem) {
if (elem == instance) {
break;
}
cur_count++;
elem = (WASMExecEnv *)bh_list_elem_next(elem);
}
} // gets the element index
#else
auto all_count = 1;
#endif
auto a = new WAMRExecEnv();
dump(a, instance);
a->cur_count = gettid();

std::unique_lock as_ul(as_mtx);
as.emplace_back(a);
if (as.size() == all_count - 1) {
// fill vector
#if !defined(_WIN32)
kill(getpid(), SIGINT);
#else
raise(SIGINT);
#endif
std::unique_lock as_ul(wamr->as_mtx);
auto cluster = wasm_exec_env_get_cluster(instance);
all_count = bh_list_length(&cluster->exec_env_list);
printf("get lock\n");
wamr->ready++;
wamr->lwcp_list[gettid()]++;
//If we're not all ready
printf("thread %d, with %ld ready out of %d total\n", gettid(), wamr->ready, all_count);
if(wamr->ready < all_count){
// Then wait for someone else to get here and finish the job
std::condition_variable as_cv;
as_cv.wait(as_ul);
}
if (as.size() == all_count) {
struct_pack::serialize_to(*writer, as);
exit(EXIT_SUCCESS);
// If we're all ready
// double check
{
auto ready_count = 0;
for(auto [k, v] : wamr->lwcp_list){
printf("%ld: %d\n", k, v);
if(v>0) ready_count++;
}
if(ready_count != all_count){
printf("we have a discrepancy between ready count and number of threads that say they are\n");
printf("ready: %d, all: %d\n", ready_count, all_count);
//not actually ready
std::condition_variable as_cv;
as_cv.wait(as_ul);
}
}
// wasm_cluster_suspend_all_except_self(cluster, instance);
auto elem = (WASMExecEnv *)bh_list_first_elem(&cluster->exec_env_list);
while (elem) {
instance = elem;
#endif // windows has no threads so only does it once
auto a = new WAMRExecEnv();
dump(a, instance);
as.emplace_back(a);
#if !defined(_WIN32)
// Is there some better way to sleep until exit?
std::condition_variable as_cv;
as_cv.wait(as_ul);
elem = (WASMExecEnv *)bh_list_elem_next(elem);
}
// finish filling vector
#endif

struct_pack::serialize_to(*writer, as);
exit(EXIT_SUCCESS);
}

int main(int argc, char *argv[]) {
Expand Down Expand Up @@ -253,7 +262,7 @@ int main(int argc, char *argv[]) {
wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool);
wamr->instantiate();
wamr->get_int3_addr();
wamr->replace_int3_with_nop();
//wamr->replace_int3_with_nop();

// freopen("output.txt", "w", stdout);
#if defined(_WIN32)
Expand Down
Loading

0 comments on commit c56c44a

Please sign in to comment.