Skip to content

Commit 2ce65d8

Browse files
committed
[WasmDSL][V8] Add support for printing memory consumption.
1 parent dd5c426 commit 2ce65d8

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/backend/V8Engine.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ void m::wasm::detail::print(const v8::FunctionCallbackInfo<v8::Value> &info)
277277
std::cout << std::endl;
278278
}
279279

280+
void m::wasm::detail::print_memory_consumption(const v8::FunctionCallbackInfo<v8::Value> &info)
281+
{
282+
M_insist(Options::Get().statistics);
283+
284+
auto alloc_total_mem = info[0].As<v8::Uint32>()->Value();
285+
auto alloc_peak_mem = info[1].As<v8::Uint32>()->Value();
286+
287+
std::cout << "Allocated memory overall consumption: " << alloc_total_mem / (1024.0 * 1024.0) << " MiB"<< std::endl;
288+
std::cout << "Allocated memory peak consumption: " << alloc_peak_mem / (1024.0 * 1024.0) << " MiB"<< std::endl;
289+
}
290+
280291
void m::wasm::detail::set_wasm_instance_raw_memory(const v8::FunctionCallbackInfo<v8::Value> &info)
281292
{
282293
v8::Local<v8::WasmModuleObject> wasm_instance = info[0].As<v8::WasmModuleObject>();
@@ -728,6 +739,7 @@ void V8Engine::compile(const m::MatchBase &plan) const
728739
#if 1
729740
/*----- Add print function. --------------------------------------------------------------------------------------*/
730741
Module::Get().emit_function_import<void(uint32_t)>("print");
742+
Module::Get().emit_function_import<void(uint32_t, uint32_t)>("print_memory_consumption");
731743
#endif
732744

733745
/*----- Emit code for run function which computes the last pipeline and calls other pipeline functions. ----------*/
@@ -743,6 +755,14 @@ void V8Engine::compile(const m::MatchBase &plan) const
743755
{
744756
auto S = CodeGenContext::Get().scoped_environment(); // create scoped environment for this function
745757
run(); // call run function
758+
if (Options::Get().statistics) {
759+
std::cout << "Pre-allocated memory overall consumption: "
760+
<< Module::Allocator().pre_allocated_memory_consumption() / (1024.0 * 1024.0)
761+
<< " MiB" << std::endl;
762+
Module::Get().emit_call<void>("print_memory_consumption",
763+
Module::Allocator().allocated_memory_consumption(),
764+
Module::Allocator().allocated_memory_peak());
765+
}
746766
main.emit_return(CodeGenContext::Get().num_tuples()); // return size of result set
747767
}
748768

@@ -1029,6 +1049,7 @@ v8::Local<v8::Object> m::wasm::detail::create_env(v8::Isolate &isolate, const m:
10291049
}
10301050
ADD_FUNC(insist)
10311051
ADD_FUNC(print)
1052+
ADD_FUNC(print_memory_consumption)
10321053
ADD_FUNC(read_result_set)
10331054
#undef ADD_FUNC
10341055
{

src/backend/V8Engine.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct V8InspectorClientImpl : v8_inspector::V8InspectorClient
8080
void insist(const v8::FunctionCallbackInfo<v8::Value> &info);
8181
void _throw(const v8::FunctionCallbackInfo<v8::Value> &info);
8282
void print(const v8::FunctionCallbackInfo<v8::Value> &info);
83+
void print_memory_consumption(const v8::FunctionCallbackInfo<v8::Value> &info);
8384
void set_wasm_instance_raw_memory(const v8::FunctionCallbackInfo<v8::Value> &info);
8485
void read_result_set(const v8::FunctionCallbackInfo<v8::Value> &info);
8586

src/backend/WasmDSL.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ struct LinearAllocator : Allocator
220220
uint32_t pre_alloc_addr_;
221221
///> runtime global size of the currently used memory, used as pointer to next free allocation
222222
Global<U32x1> alloc_addr_;
223+
///> compile-time total memory consumption
224+
uint32_t pre_alloc_total_mem_ = 0;
225+
///> runtime total memory consumption
226+
Global<U32x1> alloc_total_mem_;
227+
///> runtime peak memory consumption
228+
Global<U32x1> alloc_peak_mem_;
223229

224230
public:
225231
LinearAllocator(const memory::AddressSpace &memory, uint32_t start_addr)
@@ -229,6 +235,8 @@ struct LinearAllocator : Allocator
229235
M_insist(start_addr != 0, "memory address 0 is reserved as `nullptr`");
230236
#ifdef M_ENABLE_SANITY_FIELDS
231237
alloc_addr_.val().discard(); // artificial use of `alloc_addr_` to silence diagnostics if allocator is not used
238+
alloc_total_mem_.val().discard(); // artificial use of `alloc_total_mem_` to silence diagnostics if allocator is not used
239+
alloc_peak_mem_.val().discard(); // artificial use of `alloc_peak_mem_` to silence diagnostics if allocator is not used
232240
#endif
233241
}
234242

@@ -245,6 +253,7 @@ struct LinearAllocator : Allocator
245253
align_pre_memory(alignment);
246254
void *ptr = static_cast<uint8_t*>(memory_.addr()) + pre_alloc_addr_;
247255
pre_alloc_addr_ += bytes; // advance memory size by bytes
256+
pre_alloc_total_mem_ += bytes;
248257
M_insist(memory_.size() >= pre_alloc_addr_, "allocation must fit in memory");
249258
return ptr;
250259
}
@@ -257,6 +266,7 @@ struct LinearAllocator : Allocator
257266
align_pre_memory(alignment);
258267
Ptr<void> ptr(U32x1(pre_alloc_addr_).template to<void*>());
259268
pre_alloc_addr_ += bytes; // advance memory size by bytes
269+
pre_alloc_total_mem_ += bytes;
260270
M_insist(memory_.size() >= pre_alloc_addr_, "allocation must fit in memory");
261271
return ptr;
262272
}
@@ -266,7 +276,9 @@ struct LinearAllocator : Allocator
266276
if (alignment != 1U)
267277
align_memory(alignment);
268278
Var<Ptr<void>> ptr(alloc_addr_.template to<void*>());
269-
alloc_addr_ += bytes; // advance memory size by bytes
279+
alloc_addr_ += bytes.clone(); // advance memory size by bytes
280+
alloc_total_mem_ += bytes;
281+
alloc_peak_mem_ = Select(alloc_peak_mem_ > alloc_addr_, alloc_peak_mem_, alloc_addr_);
270282
Wasm_insist(memory_.size() >= alloc_addr_, "allocation must fit in memory");
271283
return ptr;
272284
}
@@ -285,6 +297,10 @@ struct LinearAllocator : Allocator
285297
pre_allocations_performed_ = true;
286298
}
287299

300+
uint32_t pre_allocated_memory_consumption() const override { return pre_alloc_total_mem_; }
301+
U32x1 allocated_memory_consumption() const override { return alloc_total_mem_; }
302+
U32x1 allocated_memory_peak() const override { return alloc_peak_mem_; }
303+
288304
private:
289305
/** Aligns the memory for pre-allocations with alignment requirement `align`. */
290306
void align_pre_memory(uint32_t alignment) {

src/backend/WasmDSL.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6490,6 +6490,13 @@ struct Allocator
64906490
* requested. */
64916491
virtual void perform_pre_allocations() = 0;
64926492

6493+
/** Returns the pre-allocated memory overall consumption. */
6494+
virtual uint32_t pre_allocated_memory_consumption() const = 0;
6495+
/** Returns the allocated memory overall consumption. */
6496+
virtual U32x1 allocated_memory_consumption() const = 0;
6497+
/** Returns the allocated memory peak consumption. */
6498+
virtual U32x1 allocated_memory_peak() const = 0;
6499+
64936500
Var<Ptr<void>> allocate(uint32_t bytes, uint32_t align = 1) { return allocate(U32x1(bytes), align); }
64946501
void deallocate(Ptr<void> ptr, uint32_t bytes) { return deallocate(ptr, U32x1(bytes)); }
64956502

0 commit comments

Comments
 (0)