Skip to content

Commit

Permalink
push branch first
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Zhao committed Oct 28, 2023
1 parent 0656048 commit 7d8e300
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 77 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ add_definitions(-DMVVM_INTERP=1)
add_definitions(-DMVVM_DEBUG=1)
add_definitions(-DMVVM_WASI=1)
add_definitions(-DCXXOPTS_NO_RTTI=1)
add_subdirectory(test)
add_subdirectory(test)
4 changes: 2 additions & 2 deletions include/wamr_memory_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct WAMRMemoryInstance {

void dump_impl(WASMMemoryInstance *env) {
module_type = env->module_type;
is_shared = env->is_shared;
// is_shared = env->is_shared;
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 +48,7 @@ struct WAMRMemoryInstance {
};
void restore_impl(WASMMemoryInstance *env) {
env->module_type = module_type;
env->is_shared = is_shared;
// env->is_shared = is_shared;
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
28 changes: 14 additions & 14 deletions include/wamr_module_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ struct WAMRModuleInstance {
memories.push_back(local_mem);
}
global_data = std::vector<uint8>(env->global_data, env->global_data + env->global_data_size);
LOGV(DEBUG) << env->global_data_size;
for (int i = 0; i < env->global_data_size; i++) {
LOGV(DEBUG) << env->global_data[i];
}
// tables = std::vector<WASMTableInstance>(env->tables, env->tables + env->table_count);
LOGV(DEBUG) << env->global_data_size;
for (int i = 0; i < env->global_data_size; i++) {
LOGV(DEBUG) << env->global_data[i];
}
// tables = std::vector<WASMTableInstance>(env->tables, env->tables + env->table_count);
tables.reserve(env->table_count);
std::generate_n(
std::back_inserter(tables), env->table_count,
[i = 0, env]() mutable { return *(env->tables[i++]); } // or whatever your 'body' lambda would look like.
);
dump(&wasi_ctx, env->wasi_ctx);
// dump(&wasi_ctx, env->wasi_ctx);
dump(&extra, env->e);
aux_data_end_global_index = env->module->aux_data_end_global_index;
aux_data_end = env->module->aux_data_end;
Expand All @@ -156,13 +156,13 @@ struct WAMRModuleInstance {
}
memcpy(env->global_data, global_data.data(), global_data.size());
env->global_data_size = global_data.size();
// env->global_data = global_data.data();
// env->global_data_size = global_data.size() - 1;
LOGV(DEBUG) << env->global_data_size;
LOGV(DEBUG) << env->global_data;
for (int i = 0; i < env->global_data_size; i++) {
LOGV(DEBUG) << env->global_data[i];
}
// env->global_data = global_data.data();
// env->global_data_size = global_data.size() - 1;
LOGV(DEBUG) << env->global_data_size;
LOGV(DEBUG) << env->global_data;
for (int i = 0; i < env->global_data_size; i++) {
LOGV(DEBUG) << env->global_data[i];
}
env->table_count = tables.size();
for (int i = 0; i < env->table_count; i++) {
*env->tables[i] = tables[i];
Expand All @@ -176,7 +176,7 @@ struct WAMRModuleInstance {
env->module->aux_stack_bottom = aux_stack_bottom;
env->module->aux_stack_size = aux_stack_size;
restore(&global_table_data, env->global_table_data.memory_instances);
restore(&wasi_ctx, env->wasi_ctx);
// restore(&wasi_ctx, env->wasi_ctx);
};
};

Expand Down
2 changes: 1 addition & 1 deletion lib/wasm-micro-runtime
69 changes: 48 additions & 21 deletions src/checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "thread_manager.h"
#include "wamr.h"
#include <condition_variable>
#include <cstdio>
#include <cxxopts.hpp>
#include <fstream>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
Expand All @@ -20,11 +22,11 @@ std::vector<std::unique_ptr<WAMRExecEnv>> as;
std::mutex as_mtx;
/**fopen, fseek*/
void insert_fd(int fd, const char *path, int flags, int offset) {
printf("\n #insert_fd(fd,filename,flags, offset) %d %s %d %d \n\n",fd, path,flags, offset);
printf("\n #insert_fd(fd,filename,flags, offset) %d %s %d %d \n\n", fd, path, flags, offset);

if (wamr->fd_map_.find(fd) != wamr->fd_map_.end()) {
LOGV(ERROR) << "fd already exist" << fd;
if(offset == 0) {
if (offset == 0) {
// fOpen call
std::string curPath;
int curFlags;
Expand All @@ -39,7 +41,7 @@ void insert_fd(int fd, const char *path, int flags, int offset) {
std::tie(curPath, curFlags, curOffset) = wamr->fd_map_[fd];
wamr->fd_map_[fd] = std::make_tuple(curPath, curFlags, offset);
}

} else
wamr->fd_map_.insert(std::make_pair(fd, std::make_tuple(std::string(path), flags, offset)));
}
Expand All @@ -51,25 +53,26 @@ void remove_fd(int fd) {
if (wamr->fd_map_.find(fd) != wamr->fd_map_.end())
wamr->fd_map_.erase(fd);
else
LOGV(ERROR)<< "fd not found" << fd;
}
void insert_socket(int fd){

LOGV(ERROR) << "fd not found" << fd;
}
void dump_tls(WASMModule *module, WASMModuleInstanceExtra *instance);
void insert_socket(int fd) {}
void serialize_to_file(WASMExecEnv *instance) {
/** Sounds like AoT/JIT is in this?*/
// Note: insert fd
std::ifstream stdoutput; stdoutput.open("output.txt");
std:string current_str;
std::ifstream stdoutput;
stdoutput.open("output.txt");
std:
string current_str;
std::string fd_output;
std::string filename_output;
std::string flags_output;
if(stdoutput.is_open()) {
while(stdoutput.good()) {

if (stdoutput.is_open()) {
while (stdoutput.good()) {
stdoutput >> current_str;
if(current_str == "fopen_test(fd,filename,flags)") {
stdoutput >> fd_output;
if (current_str == "fopen_test(fd,filename,flags)") {
stdoutput >> fd_output;
stdoutput >> filename_output;
stdoutput >> flags_output;
insert_fd(std::stoi(fd_output), filename_output.c_str(), std::stoi(flags_output), 0);
Expand All @@ -79,7 +82,7 @@ void serialize_to_file(WASMExecEnv *instance) {
stdoutput.close();

//
std::cout<<"dasfasdfasf"<< re.str()<<"dasfasdfasf\n";
std::cout << "dasfasdfasf" << re.str() << "dasfasdfasf\n";
auto cluster = wasm_exec_env_get_cluster(instance);
auto all_count = bh_list_length(&cluster->exec_env_list);
int cur_count = 0;
Expand All @@ -94,16 +97,41 @@ void serialize_to_file(WASMExecEnv *instance) {
}
} // gets the element index
auto a = new WAMRExecEnv();
dump_tls(((WASMModuleInstance *)instance->module_inst)->module, ((WASMModuleInstance *)instance->module_inst)->e);
dump(a, instance);
as_mtx.lock();
std::unique_lock as_ul(as_mtx);
as.emplace_back(a);
as.back().get()->cur_count = cur_count;
if (as.size() == all_count) {
struct_pack::serialize_to(writer, as);
LOGV(INFO) << "serialize to file" << cur_count << " " << all_count << "\n";
exit(0);
}
as_mtx.unlock();
// Is there some better way to sleep until exit?
std::condition_variable as_cv;
as_cv.wait(as_ul);
}
// mini dumper
void dump_tls(WASMModule *module, WASMModuleInstanceExtra *instance) {
WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
WASMGlobal *aux_stack_top_global = NULL, *global;
uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
uint32 aux_data_end_global_index = (uint32)-1;
uint32 aux_heap_base_global_index = (uint32)-1;
/* Resolve aux stack top global */
for (int global_index = 0; global_index < instance->global_count; global_index++) {
auto global = module->globals + global_index;
if (global->is_mutable /* heap_base and data_end is
not mutable */
&& global->type == VALUE_TYPE_I32 && global->init_expr.init_expr_type == INIT_EXPR_TYPE_I32_CONST &&
(uint32)global->init_expr.u.i32 <= aux_heap_base) {
// LOGV(INFO) << "TLS" << global->init_expr << "\n";
// this is not the place been accessed, but initialized.
} else {
break;
}
}
}

#ifndef MVVM_DEBUG
Expand Down Expand Up @@ -145,8 +173,7 @@ int main(int argc, char *argv[]) {
options.add_options()("t,target", "The webassembly file to execute",
cxxopts::value<std::string>()->default_value("./test/counter.wasm"))(
"j,jit", "Whether the jit mode or interp mode", cxxopts::value<bool>()->default_value("false"))(
"d,dir", "The directory list exposed to WAMR",
cxxopts::value<std::vector<std::string>>()->default_value("./"))(
"d,dir", "The directory list exposed to WAMR", cxxopts::value<std::vector<std::string>>()->default_value("./"))(
"m,map_dir", "The mapped directory list exposed to WAMRe",
cxxopts::value<std::vector<std::string>>()->default_value(""))(
"e,env", "The environment list exposed to WAMR",
Expand All @@ -173,7 +200,7 @@ int main(int argc, char *argv[]) {
wamr = new WAMRInstance(target.c_str(), is_jit);
wamr->set_wasi_args(dir, map_dir, env, arg, addr, ns_pool);
wamr->instantiate();
freopen("output.txt","w",stdout);
freopen("output.txt", "w", stdout);

#ifndef MVVM_DEBUG
// Define the sigaction structure
Expand All @@ -197,4 +224,4 @@ int main(int argc, char *argv[]) {
// Main program loop
wamr->invoke_main();
return 0;
}
}
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enable_testing()
function(wamr_app input)
add_custom_target(${input}_wamr ALL COMMAND ${HOST_PREFIX}clang -Wno-implicit-function-declaration -Wno-int-conversion --target=wasm32-unknown-wasi -g -pthread -Wl,--allow-undefined-file=${WAMR_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt --sysroot=${HOST_PREFIX}/../share/wasi-sysroot ${CMAKE_CURRENT_SOURCE_DIR}/${input}.c -o ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm)
add_test(NAME ${input}_checkpoint COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_checkpoint --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm)
# add_test(NAME ${input}_restore COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_restore --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm)
add_test(NAME ${input}_restore COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../MVVM_restore --target ${CMAKE_CURRENT_BINARY_DIR}/${input}.wasm)
endfunction()

wamr_app(counter)
Expand Down
69 changes: 32 additions & 37 deletions test/multi-thread.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
/*
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <stdio.h>
#include <pthread.h>
#include <stdio.h>

#define MAX_NUM_THREADS 2
#define NUM_ITER 1000

int g_count = 0;

static void *
thread(void *arg)
{
for (int i = 0; i < NUM_ITER; i++) {
__atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST);
}
printf("%d\n", g_count);
return NULL;
static void *thread(void *arg) {
for (int i = 0; i < NUM_ITER; i++) {
__atomic_fetch_add(&g_count, 1, __ATOMIC_SEQ_CST);
}
printf("%d\n", g_count);
return NULL;
}

int
main(int argc, char **argv)
{
pthread_t tids[MAX_NUM_THREADS];

for (int i = 0; i < MAX_NUM_THREADS; i++) {
if (pthread_create(&tids[i], NULL, thread, NULL) != 0) {
printf("Thread creation failed\n");
}
}

for (int i = 0; i < MAX_NUM_THREADS; i++) {
if (pthread_join(tids[i], NULL) != 0) {
printf("Thread join failed\n");
}
}

printf("Value of counter after update: %d (expected=%d)\n", g_count,
MAX_NUM_THREADS * NUM_ITER);
if (g_count != MAX_NUM_THREADS * NUM_ITER) {
__builtin_trap();
}

return -1;
}
int main(int argc, char **argv) {
pthread_t tids[MAX_NUM_THREADS];

for (int i = 0; i < MAX_NUM_THREADS; i++) {
if (pthread_create(&tids[i], NULL, thread, NULL) != 0) {
printf("Thread creation failed\n");
}
}

for (int i = 0; i < MAX_NUM_THREADS; i++) {
if (pthread_join(tids[i], NULL) != 0) {
printf("Thread join failed\n");
}
}

printf("Value of counter after update: %d (expected=%d)\n", g_count, MAX_NUM_THREADS * NUM_ITER);
if (g_count != MAX_NUM_THREADS * NUM_ITER) {
__builtin_trap();
}

return -1;
}

0 comments on commit 7d8e300

Please sign in to comment.