Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
code_cache.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/lockfree/spsc_queue.hpp>
4
7#include <boost/multi_index_container.hpp>
8#include <boost/multi_index/hashed_index.hpp>
9#include <boost/multi_index/sequenced_index.hpp>
10#include <boost/multi_index/composite_key.hpp>
11#include <boost/multi_index/key_extractors.hpp>
12
13#include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
14#include <boost/asio/local/datagram_protocol.hpp>
15
16
17#include <thread>
18
19namespace std {
20 template<> struct hash<sysio::chain::eosvmoc::code_tuple> {
21 size_t operator()(const sysio::chain::eosvmoc::code_tuple& ct) const noexcept {
22 return ct.code_id._hash[0];
23 }
24 };
25}
26
27namespace sysio { namespace chain { namespace eosvmoc {
28
29using namespace boost::multi_index;
30using namespace boost::asio;
31
32namespace bip = boost::interprocess;
33namespace bfs = boost::filesystem;
34
35using allocator_t = bip::rbtree_best_fit<bip::null_mutex_family, bip::offset_ptr<void>, alignof(std::max_align_t)>;
36
37struct config;
38
40 public:
41 code_cache_base(const bfs::path data_dir, const eosvmoc::config& eosvmoc_config, const chainbase::database& db);
43
44 const int& fd() const { return _cache_fd; }
45
46 void free_code(const digest_type& code_id, const uint8_t& vm_version);
47
48 protected:
49 struct by_hash;
50
51 typedef boost::multi_index_container<
53 indexed_by<
54 sequenced<>,
55 hashed_unique<tag<by_hash>,
56 composite_key< code_descriptor,
57 member<code_descriptor, digest_type, &code_descriptor::code_hash>,
58 member<code_descriptor, uint8_t, &code_descriptor::vm_version>
59 >
60 >
61 >
64
66
69
70 io_context _ctx;
71 local::datagram_protocol::socket _compile_monitor_write_socket{_ctx};
72 local::datagram_protocol::socket _compile_monitor_read_socket{_ctx};
73
74 //these are really only useful to the async code cache, but keep them here so
75 //free_code can be shared
76 std::unordered_set<code_tuple> _queued_compiles;
77 std::unordered_map<code_tuple, bool> _outstanding_compiles_and_poison;
78
80 void check_eviction_threshold(size_t free_bytes);
81 void run_eviction_round();
82
83 void set_on_disk_region_dirty(bool);
84
85 template <typename T>
87};
88
90 public:
91 code_cache_async(const bfs::path data_dir, const eosvmoc::config& eosvmoc_config, const chainbase::database& db);
93
94 //If code is in cache: returns pointer & bumps to front of MRU list
95 //If code is not in cache, and not blacklisted, and not currently compiling: return nullptr and kick off compile
96 //otherwise: return nullptr
97 const code_descriptor* const get_descriptor_for_code(const digest_type& code_id, const uint8_t& vm_version);
98
99 private:
100 std::thread _monitor_reply_thread;
101 boost::lockfree::spsc_queue<wasm_compilation_result_message> _result_queue;
102 void wait_on_compile_monitor_message();
103 std::tuple<size_t, size_t> consume_compile_thread_queue();
104 std::unordered_set<code_tuple> _blacklist;
105 size_t _threads;
106};
107
109 public:
112
113 //Can still fail and return nullptr if, for example, there is an expected instantiation failure
114 const code_descriptor* const get_descriptor_for_code_sync(const digest_type& code_id, const uint8_t& vm_version);
115};
116
117}}}
code_cache_async(const bfs::path data_dir, const eosvmoc::config &eosvmoc_config, const chainbase::database &db)
const code_descriptor *const get_descriptor_for_code(const digest_type &code_id, const uint8_t &vm_version)
void serialize_cache_index(fc::datastream< T > &ds)
local::datagram_protocol::socket _compile_monitor_write_socket
void check_eviction_threshold(size_t free_bytes)
boost::multi_index_container< code_descriptor, indexed_by< sequenced<>, hashed_unique< tag< by_hash >, composite_key< code_descriptor, member< code_descriptor, digest_type, &code_descriptor::code_hash >, member< code_descriptor, uint8_t, &code_descriptor::vm_version > > > > > code_cache_index
local::datagram_protocol::socket _compile_monitor_read_socket
std::unordered_set< code_tuple > _queued_compiles
std::unordered_map< code_tuple, bool > _outstanding_compiles_and_poison
void free_code(const digest_type &code_id, const uint8_t &vm_version)
code_cache_base(const bfs::path data_dir, const eosvmoc::config &eosvmoc_config, const chainbase::database &db)
const chainbase::database & _db
const code_descriptor *const get_descriptor_for_code_sync(const digest_type &code_id, const uint8_t &vm_version)
Definition name.hpp:106
bip::rbtree_best_fit< bip::null_mutex_family, bip::offset_ptr< void >, alignof(std::max_align_t)> allocator_t
unsigned char uint8_t
Definition stdint.h:124
size_t operator()(const sysio::chain::eosvmoc::code_tuple &ct) const noexcept