Skip to content

Commit f54f36e

Browse files
load module in parallel
1 parent 64af719 commit f54f36e

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

contracts/eosiolib_native/vm_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ int get_option(const char* option, char *result, int size);
347347
#define VM_TYPE_JAVA 12
348348
#define VM_TYPE_WABT 13
349349

350+
#define VM_MODULE_NOT_FOUND 1000
351+
350352
#ifdef __cplusplus
351353
}
352354
#endif

libraries/chain/include/eosio/chain/wasm_interface_private.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ namespace eosio { namespace chain {
5656
wasm_interface_impl(wasm_interface::vm_type vm) {
5757
#if defined(_WAVM)
5858
runtime_interface = std::make_unique<webassembly::wavm::wavm_runtime>();
59-
#elif defined(_BINARYEN)
60-
runtime_interface = std::make_unique<webassembly::binaryen::binaryen_runtime>();
6159
#elif defined(_WABT)
6260
runtime_interface = std::make_unique<webassembly::wabt_runtime::wabt_runtime>();
6361
#else
@@ -91,14 +89,14 @@ namespace eosio { namespace chain {
9189
char code_id[8*4];
9290

9391
{
94-
std::lock_guard<std::mutex> lock(m);
95-
9692
code = get_code( receiver, &size );
9793
get_code_id(receiver, code_id, sizeof(code_id));
9894
if (size <= 0) {
9995
EOS_ASSERT(false, asset_type_exception, "code size should not be zero");
10096
}
10197

98+
std::lock_guard<std::mutex> lock(m);
99+
102100
auto it = instantiation_cache.find(receiver);
103101
if (it != instantiation_cache.end()) {
104102
if (0 == memcmp(code_id, it->second->code_id, sizeof(code_id))) {
@@ -115,14 +113,24 @@ namespace eosio { namespace chain {
115113
if (!preload) {
116114
pause_billing_timer();
117115
}
116+
#ifdef _WAVM
117+
unload_module(receiver);
118+
load_module_async(receiver, code, size);
119+
{
120+
std::lock_guard<std::mutex> lock(m);
121+
return instantiation_cache.end()->second;
122+
}
123+
#elif defined(_WABT)
118124
return load_module(receiver, code, size);
125+
#endif
119126
}
120127

121128

122-
void load_module_async(uint64_t receiver, const char* code, size_t size) {
123-
load_module(receiver, code, size);
124-
//send a transaction to indicate that module is loaded by BP.
125-
}
129+
void load_module_async(uint64_t receiver, const char* code, size_t size) {
130+
boost::thread t( [receiver, &code, size, this]() {
131+
load_module(receiver, code, size);
132+
} );
133+
}
126134

127135
std::unique_ptr<wasm_instantiated_module_interface>& load_module(uint64_t receiver, const char* code, size_t size) {
128136
IR::Module module;
@@ -149,6 +157,7 @@ namespace eosio { namespace chain {
149157
} catch(const IR::ValidationException& e) {
150158
EOS_ASSERT(false, wasm_serialization_error, e.message.c_str());
151159
}
160+
152161
{
153162
std::lock_guard<std::mutex> lock(m);
154163
instantiation_cache[receiver] = runtime_interface->instantiate_module((const char*)bytes.data(), bytes.size(), parse_initial_memory(module));

libraries/vm/vm_wasm/wasm_interface.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,28 @@ namespace eosio { namespace chain {
6464
}
6565
validate(code, size);
6666

67-
// my->get_instantiated_module(account);
67+
my->get_instantiated_module(account);
6868
return 0;
6969
}
7070

7171
uint64_t wasm_interface::call(string& func, vector<uint64_t>& args) {
72-
return my->get_instantiated_module()->call(func, args);
72+
try {
73+
uint64_t receiver = get_vm_api()->current_receiver();
74+
auto& module = my->get_instantiated_module(receiver);
75+
if (!module.get()) {
76+
return VM_MODULE_NOT_FOUND;
77+
}
78+
module->call(func, args);
79+
} catch ( const wasm_exit& ){
80+
}
81+
return 1;
7382
}
7483

7584
int wasm_interface::apply( uint64_t receiver, uint64_t account, uint64_t act ) {
7685
try {
7786
auto& module = my->get_instantiated_module(receiver);
7887
if (!module.get()) {
79-
return 0;
88+
return VM_MODULE_NOT_FOUND;
8089
}
8190
module->apply(receiver, account, act);
8291
} catch ( const wasm_exit& ){

libraries/vm_manager/vm_manager.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ int vm_manager::setcode(int vm_type, uint64_t account) {
576576
}
577577

578578
int vm_manager::apply(int type, uint64_t receiver, uint64_t account, uint64_t act) {
579+
#if 0
579580
if (is_trusted_account(account)) {
580581

581582
} else {
@@ -584,6 +585,7 @@ int vm_manager::apply(int type, uint64_t receiver, uint64_t account, uint64_t ac
584585
type = VM_TYPE_IPC;
585586
}
586587
}
588+
#endif
587589
return local_apply(type, receiver, account, act);
588590
}
589591

@@ -605,6 +607,11 @@ int vm_manager::local_apply(int vm_type, uint64_t receiver, uint64_t account, ui
605607
}
606608
}
607609
}
610+
#if 0
611+
if (receiver == N(eosio.token) || receiver == N(eosio)) {
612+
vm_type = VM_TYPE_WAVM;
613+
}
614+
#endif
608615
if (vm_runtime == 0) {
609616
vm_type = VM_TYPE_WAVM;
610617
} else if (vm_runtime == 1) {
@@ -619,7 +626,12 @@ int vm_manager::local_apply(int vm_type, uint64_t receiver, uint64_t account, ui
619626
if (itr == vm_map.end()) {
620627
return 0;
621628
}
622-
itr->second->apply(receiver, account, act);
629+
int ret = itr->second->apply(receiver, account, act);
630+
if (VM_MODULE_NOT_FOUND == ret) {
631+
if (VM_TYPE_WAVM == vm_type) {
632+
vm_map[VM_TYPE_WABT]->apply(receiver, account, act);
633+
}
634+
}
623635
return 1;
624636
}
625637

0 commit comments

Comments
 (0)