mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
WebAssembly.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
10#include <unordered_map>
11
12
13namespace m {
14
17{
19 static constexpr std::size_t WASM_PAGE_SIZE = 1UL << 16;
21 static constexpr std::size_t WASM_MAX_MEMORY = (1UL << 32) - (1UL << 16);
23 static constexpr std::size_t WASM_ALIGNMENT = 8;
24
27 {
28 enum config_t : uint64_t
29 {
31 };
32
33 private:
35
36 public:
37 unsigned id;
38 const MatchBase &plan;
40 std::unique_ptr<const storage::DataLayoutFactory> result_set_factory;
42 uint32_t heap = 0;
43 std::vector<std::reference_wrapper<const idx::IndexBase>> indexes;
44
45 WasmContext(uint32_t id, const MatchBase &plan, config_t configuration, std::size_t size);
46
47 bool config(config_t cfg) const { return bool(cfg & config_); }
48
52 uint32_t map_table(const Table &table);
53
56 void install_guard_page();
57
59 std::size_t add_index(const idx::IndexBase &index) {
60 indexes.emplace_back(index);
61 return indexes.size() - 1;
62 }
63 };
64
65 private:
67 static inline std::unordered_map<unsigned, std::unique_ptr<WasmContext>> contexts_;
68
69 public:
72 const MatchBase &plan,
74 std::size_t size = WASM_MAX_MEMORY)
75 {
76 auto wasm_context = std::make_unique<WasmContext>(id, plan, configuration, size);
77 auto [it, inserted] = contexts_.emplace(id, std::move(wasm_context));
78 M_insist(inserted, "WasmContext with that ID already exists");
79 return *it->second;
80 }
81
83 static std::pair<std::reference_wrapper<WasmContext>, bool>
85 const MatchBase &plan,
87 std::size_t size = WASM_MAX_MEMORY)
88 {
89 auto [it, inserted] = contexts_.try_emplace(id, lazy_construct(
90 [&](){ return std::make_unique<WasmContext>(id, plan, configuration, size); }
91 ));
92 return { std::ref(*it->second), inserted };
93 }
94
96 static void Dispose_Wasm_Context(unsigned id) {
97 auto res = contexts_.erase(id);
98 (void) res;
99 M_insist(res == 1, "There is no context with the given ID to erase");
100 }
101
103 static void Dispose_Wasm_Context(const WasmContext &ctx) { Dispose_Wasm_Context(ctx.id); }
104
106 static WasmContext & Get_Wasm_Context_By_ID(unsigned id) {
107 auto it = contexts_.find(id);
108 M_insist(it != contexts_.end(), "There is no context with the given ID");
109 return *it->second;
110 }
111
113 static bool Has_Wasm_Context(unsigned id) { return contexts_.find(id) != contexts_.end(); }
114
115 WasmEngine() = default;
116 virtual ~WasmEngine() { }
117 WasmEngine(const WasmEngine&) = delete;
118 WasmEngine(WasmEngine&&) = default;
119
121 virtual void compile(const MatchBase &plan) const = 0;
122
124 virtual void execute(const MatchBase &plan) = 0;
125};
126
129{
130 private:
131 std::unique_ptr<WasmEngine> engine_;
132
133 public:
134 WasmBackend(std::unique_ptr<WasmEngine> engine) : engine_(std::move(engine)) { }
135
137 const WasmEngine & engine() const { return *engine_; }
138
139 void register_operators(PhysicalOptimizer &phys_opt) const override;
140
141 void execute(const MatchBase &plan) const override;
142};
143
144}
#define M_insist(...)
Definition: macro.hpp:129
‍mutable namespace
Definition: Backend.hpp:10
STL namespace.
Defines the interface of all execution Backends.
Definition: Backend.hpp:17
The physical optimizer interface.
A table is a sorted set of attributes.
Definition: Schema.hpp:388
A Backend to execute a plan on a specific WasmEngine.
void register_operators(PhysicalOptimizer &phys_opt) const override
Registers all physical operators of this Backend in phys_opt.
WasmBackend(std::unique_ptr< WasmEngine > engine)
const WasmEngine & engine() const
Returns this backend's WasmEngine.
void execute(const MatchBase &plan) const override
Executes the already computed physical covering represented by plan using this Backend.
std::unique_ptr< WasmEngine > engine_
the WasmEngine of this backend
A WasmContext holds associated information of a WebAssembly module instance.
Definition: WebAssembly.hpp:27
const MatchBase & plan
current plan
Definition: WebAssembly.hpp:38
unsigned id
a unique ID
Definition: WebAssembly.hpp:37
void install_guard_page()
Installs a guard page at the current heap and increments heap to the next page.
Definition: WebAssembly.cpp:90
std::unique_ptr< const storage::DataLayoutFactory > result_set_factory
‍factory used to create the result set data layout
Definition: WebAssembly.hpp:40
uint32_t heap
beginning of the heap, encoded as offset from the beginning of the virtual address space
Definition: WebAssembly.hpp:42
@ TRAP_GUARD_PAGES
map guard pages with PROT_NONE to trap any accesses
Definition: WebAssembly.hpp:30
uint32_t map_table(const Table &table)
Maps a table at the current start of heap and advances heap past the mapped region.
Definition: WebAssembly.cpp:67
std::size_t add_index(const idx::IndexBase &index)
Adds an index to the WasmContext and returns its position in the vector as id.
Definition: WebAssembly.hpp:59
bool config(config_t cfg) const
Definition: WebAssembly.hpp:47
memory::AddressSpace vm
WebAssembly module instance's virtual address space aka. linear memory
Definition: WebAssembly.hpp:41
std::vector< std::reference_wrapper< const idx::IndexBase > > indexes
the indexes used in the query
Definition: WebAssembly.hpp:43
A WasmEngine provides an environment to compile and execute WebAssembly modules.
Definition: WebAssembly.hpp:17
WasmEngine(WasmEngine &&)=default
static WasmContext & Create_Wasm_Context_For_ID(unsigned id, const MatchBase &plan, WasmContext::config_t configuration=WasmContext::config_t(0x0), std::size_t size=WASM_MAX_MEMORY)
Creates a new WasmContext for ID id with size bytes of virtual address space.
Definition: WebAssembly.hpp:71
static std::pair< std::reference_wrapper< WasmContext >, bool > Ensure_Wasm_Context_For_ID(unsigned id, const MatchBase &plan, WasmContext::config_t configuration=WasmContext::config_t(0x0), std::size_t size=WASM_MAX_MEMORY)
If none exists, creates a new WasmContext for ID id with size bytes of virtual address space.
Definition: WebAssembly.hpp:84
static constexpr std::size_t WASM_ALIGNMENT
The alignment that is suitable for all built-in types.
Definition: WebAssembly.hpp:23
static bool Has_Wasm_Context(unsigned id)
Tests if the WasmContext with ID id exists.
static constexpr std::size_t WASM_PAGE_SIZE
the size of a WebAssembly memory page, 64 KiB.
Definition: WebAssembly.hpp:19
virtual void execute(const MatchBase &plan)=0
Executes the already computed physical covering represented by plan using this WasmEngine.
WasmEngine()=default
WasmEngine(const WasmEngine &)=delete
static void Dispose_Wasm_Context(const WasmContext &ctx)
Disposes the WasmContext ctx.
static std::unordered_map< unsigned, std::unique_ptr< WasmContext > > contexts_
‍maps unique IDs to WasmContext instances
Definition: WebAssembly.hpp:67
static WasmContext & Get_Wasm_Context_By_ID(unsigned id)
Returns a reference to the WasmContext with ID id.
virtual ~WasmEngine()
static constexpr std::size_t WASM_MAX_MEMORY
The maximum memory of a WebAssembly module: 2^32 - 2^16 bytes ≈ 4 GiB.
Definition: WebAssembly.hpp:21
virtual void compile(const MatchBase &plan) const =0
Compiles the already computed physical covering represented by plan using this WasmEngine.
static void Dispose_Wasm_Context(unsigned id)
Disposes the WasmContext with ID id.
Definition: WebAssembly.hpp:96
The base class for indexes.
Definition: Index.hpp:27
This class represents a reserved address space in virtual memory.
Definition: memory.hpp:45