1- #include < fc/log/logger_config.hpp> // set_thread_name
2-
31#include < eosio/chain/webassembly/eos-vm-oc/code_cache.hpp>
42#include < eosio/chain/webassembly/eos-vm-oc/config.hpp>
53#include < eosio/chain/webassembly/common.hpp>
97#include < eosio/chain/webassembly/eos-vm-oc/compile_monitor.hpp>
108#include < eosio/chain/exceptions.hpp>
119
10+ #include < fc/log/logger_config.hpp> // set_thread_name
11+
12+ #include < fstream>
1213#include < unistd.h>
13- #include < sys/syscall.h>
1414#include < sys/mman.h>
15- #include < linux/memfd.h>
16-
17- #include " IR/Module.h"
18- #include " IR/Validate.h"
19- #include " WASM/WASM.h"
20- #include " LLVMJIT.h"
21-
22- using namespace IR ;
2315
2416namespace eosio { namespace chain { namespace eosvmoc {
2517
@@ -81,7 +73,8 @@ void code_cache_async::wait_on_compile_monitor_message() {
8173 --_outstanding_compiles;
8274
8375 const auto & msg = std::get<wasm_compilation_result_message>(message);
84- _result_queue.push (msg);
76+ bool p = _result_queue.push (msg);
77+ assert (p);
8578
8679 _compile_complete_func (_ctx, msg.code .code_id , msg.queued_time );
8780
@@ -92,7 +85,7 @@ void code_cache_async::wait_on_compile_monitor_message() {
9285}
9386
9487// call with _mtx locked
95- void code_cache_async::write_message (const digest_type& code_id, const eosvmoc_message& message, const std::vector <wrapped_fd>& fds) {
88+ void code_cache_async::write_message (const digest_type& code_id, const eosvmoc_message& message, std::span <wrapped_fd> fds) {
9689 _outstanding_compiles_and_poison.emplace (code_id, false );
9790 ++_outstanding_compiles;
9891 if (!write_message_with_fds (_compile_monitor_write_socket, message, fds)) {
@@ -106,7 +99,8 @@ void code_cache_async::process_queued_compiles() {
10699 while (_outstanding_compiles < _threads && !_queued_compiles.empty ()) {
107100 auto nextup = _queued_compiles.begin ();
108101
109- write_message (nextup->code_id (), nextup->msg , nextup->fds_to_pass );
102+ auto fd = memfd_for_bytearray (nextup->code );
103+ write_message (nextup->code_id (), nextup->msg , std::span<wrapped_fd>{&fd, 1 });
110104
111105 _queued_compiles.erase (nextup);
112106 }
@@ -116,7 +110,9 @@ void code_cache_async::process_queued_compiles() {
116110// number processed, bytes available (only if number processed > 0)
117111std::tuple<size_t , size_t > code_cache_async::consume_compile_thread_queue () {
118112 std::unique_lock g (_mtx);
119- auto outstanding_compiles = _outstanding_compiles_and_poison; // will always be small, <= _threads
113+ // will be relatively small, ~ _threads. Can be larger than _threads if multiple compiles finish before
114+ // consume_compile_thread_queue() is called on the main thread
115+ auto outstanding_compiles = _outstanding_compiles_and_poison;
120116 g.unlock ();
121117
122118 std::vector<digest_type> erased;
@@ -142,8 +138,10 @@ std::tuple<size_t, size_t> code_cache_async::consume_compile_thread_queue() {
142138 });
143139
144140 g.lock ();
145- for (const auto & e : erased)
146- _outstanding_compiles_and_poison.erase (e);
141+ for (const auto & e : erased) {
142+ auto c = _outstanding_compiles_and_poison.erase (e);
143+ assert (c > 0 );
144+ }
147145 g.unlock ();
148146
149147 return {gotsome, bytes_remaining};
@@ -163,8 +161,7 @@ code_cache_async::get_descriptor_for_code(mode m, const digest_type& code_id, co
163161 }
164162
165163 // check for entry in cache
166- code_cache_index::index<by_hash>::type::iterator it = _cache_index.get <by_hash>().find (code_id);
167- if (it != _cache_index.get <by_hash>().end ()) {
164+ if (auto it = _cache_index.get <by_hash>().find (code_id); it != _cache_index.get <by_hash>().end ()) {
168165 if (m.write_window )
169166 _cache_index.relocate (_cache_index.begin (), _cache_index.project <0 >(it));
170167 return &*it;
@@ -205,20 +202,20 @@ code_cache_async::get_descriptor_for_code(mode m, const digest_type& code_id, co
205202 .queued_time = fc::time_point::now (),
206203 .limits = !m.whitelisted ? _eosvmoc_config.non_whitelisted_limits : std::optional<subjective_compile_limits>{}
207204 };
208- std::vector<wrapped_fd> fds_to_pass;
209- fds_to_pass.emplace_back (memfd_for_bytearray (codeobject->code ));
210205
211206 g.lock ();
212- if (_outstanding_compiles_and_poison.size () >= _threads) {
207+ if (_outstanding_compiles >= _threads) {
208+ std::vector<char > code{codeobject->code .begin (), codeobject->code .end ()};
213209 if (m.high_priority )
214- _queued_compiles.emplace_front (std::move (msg), std::move (fds_to_pass ));
210+ _queued_compiles.emplace_front (std::move (msg), std::move (code ));
215211 else
216- _queued_compiles.emplace_back (std::move (msg), std::move (fds_to_pass ));
212+ _queued_compiles.emplace_back (std::move (msg), std::move (code ));
217213 failure = get_cd_failure::temporary; // Compile might not be done yet
218214 return nullptr ;
219215 }
220216
221- write_message (code_id, msg, fds_to_pass);
217+ auto fd = memfd_for_bytearray (codeobject->code );
218+ write_message (code_id, msg, std::span<wrapped_fd>{&fd, 1 });
222219 failure = get_cd_failure::temporary; // Compile might not be done yet
223220 return nullptr ;
224221}
@@ -247,15 +244,13 @@ const code_descriptor* const code_cache_sync::get_descriptor_for_code_sync(mode
247244 if (!codeobject) // should be impossible right?
248245 return nullptr ;
249246
250- std::vector<wrapped_fd> fds_to_pass;
251- fds_to_pass.emplace_back (memfd_for_bytearray (codeobject->code ));
252-
253247 auto msg = compile_wasm_message{
254248 .code = { code_id, vm_version },
255249 .queued_time = fc::time_point{}, // could use now() if compile time measurement desired
256250 .limits = !m.whitelisted ? _eosvmoc_config.non_whitelisted_limits : std::optional<subjective_compile_limits>{}
257251 };
258- write_message_with_fds (_compile_monitor_write_socket, msg, fds_to_pass);
252+ auto fd = memfd_for_bytearray (codeobject->code );
253+ write_message_with_fds (_compile_monitor_write_socket, msg, std::span<wrapped_fd>{&fd, 1 });
259254 auto [success, message, fds] = read_message_with_fds (_compile_monitor_read_socket);
260255 EOS_ASSERT (success, wasm_execution_error, " failed to read response from monitor process" );
261256 EOS_ASSERT (std::holds_alternative<wasm_compilation_result_message>(message), wasm_execution_error, " unexpected response from monitor process" );
0 commit comments