Commit d78235e3 authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Add separate accounting allocator to {WasmEngine}.

This adds an {AccountingAllocator} to the {WasmEngine}, separate from
the allocator used per Isolate. It is in preparation of being able to
share engines across multiple Isolates. For now we just add up the
stats from both allocators until a public API for the engine becomes
available.

R=ahaas@chromium.org
TEST=cctest/test-wasm-shared-engine/SharedEngineRunImported
BUG=v8:7424

Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Change-Id: Ia915a1ae4aa7ebed27073b7b6bd067e31717a6ea
Reviewed-on: https://chromium-review.googlesource.com/1127788
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54316}
parent 75ad46a4
......@@ -8521,10 +8521,15 @@ void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) {
heap_statistics->total_available_size_ = heap->Available();
heap_statistics->used_heap_size_ = heap->SizeOfObjects();
heap_statistics->heap_size_limit_ = heap->MaxReserved();
// TODO(7424): There is no public API for the {WasmEngine} yet. Once such an
// API becomes available we should report the malloced memory separately. For
// now we just add the values, thereby over-approximating the peak slightly.
heap_statistics->malloced_memory_ =
isolate->allocator()->GetCurrentMemoryUsage();
isolate->allocator()->GetCurrentMemoryUsage() +
isolate->wasm_engine()->allocator()->GetCurrentMemoryUsage();
heap_statistics->peak_malloced_memory_ =
isolate->allocator()->GetMaxMemoryUsage();
isolate->allocator()->GetMaxMemoryUsage() +
isolate->wasm_engine()->allocator()->GetMaxMemoryUsage();
heap_statistics->number_of_native_contexts_ = heap->NumberOfNativeContexts();
heap_statistics->number_of_detached_contexts_ =
heap->NumberOfDetachedContexts();
......
......@@ -121,7 +121,10 @@ class CompilationState {
: baseline_finish_units_;
}
// TODO(7423): Get rid of the Isolate field to make sure the CompilationState
// can be shared across multiple Isolates.
Isolate* const isolate_;
WasmEngine* const wasm_engine_;
// TODO(clemensh): Remove ModuleEnv, generate it when needed.
ModuleEnv module_env_;
const size_t max_memory_;
......@@ -2791,6 +2794,7 @@ ModuleEnv* GetModuleEnv(CompilationState* compilation_state) {
CompilationState::CompilationState(internal::Isolate* isolate,
const ModuleEnv& env)
: isolate_(isolate),
wasm_engine_(isolate->wasm_engine()),
module_env_(env),
max_memory_(GetMaxUsableMemorySize(isolate) / 2),
compile_mode_(FLAG_wasm_tier_up && env.module->origin == kWasmOrigin
......@@ -2804,15 +2808,15 @@ CompilationState::CompilationState(internal::Isolate* isolate,
v8::Platform* platform = V8::GetCurrentPlatform();
foreground_task_runner_ = platform->GetForegroundTaskRunner(v8_isolate);
// Register task manager for clean shutdown in case of an isolate shutdown.
isolate_->wasm_engine()->Register(&background_task_manager_);
isolate_->wasm_engine()->Register(&foreground_task_manager_);
// Register task manager for clean shutdown in case of an engine shutdown.
wasm_engine_->Register(&background_task_manager_);
wasm_engine_->Register(&foreground_task_manager_);
}
CompilationState::~CompilationState() {
CancelAndWait();
foreground_task_manager_.CancelAndWait();
isolate_->wasm_engine()->Unregister(&foreground_task_manager_);
wasm_engine_->Unregister(&foreground_task_manager_);
NotifyOnEvent(CompilationEvent::kDestroyed, nullptr);
}
......@@ -2949,7 +2953,7 @@ void CompilationState::ScheduleUnitForFinishing(
void CompilationState::CancelAndWait() {
background_task_manager_.CancelAndWait();
isolate_->wasm_engine()->Unregister(&background_task_manager_);
wasm_engine_->Unregister(&background_task_manager_);
}
void CompilationState::OnBackgroundTaskStopped() {
......
......@@ -291,8 +291,8 @@ class ModuleDecoderImpl : public Decoder {
void StartDecoding(Isolate* isolate) {
CHECK_NULL(module_);
SetCounters(isolate->counters());
module_.reset(new WasmModule(
base::make_unique<Zone>(isolate->allocator(), "signatures")));
module_.reset(new WasmModule(base::make_unique<Zone>(
isolate->wasm_engine()->allocator(), "signatures")));
module_->initial_pages = 0;
module_->maximum_pages = 0;
module_->mem_export = false;
......
......@@ -9,6 +9,7 @@
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-memory.h"
#include "src/zone/accounting-allocator.h"
namespace v8 {
namespace internal {
......@@ -89,9 +90,10 @@ class V8_EXPORT_PRIVATE WasmEngine {
WasmMemoryTracker* memory_tracker() { return &memory_tracker_; }
// We register and unregister CancelableTaskManagers that run
// isolate-dependent tasks. These tasks need to be shutdown if the isolate is
// shut down.
AccountingAllocator* allocator() { return &allocator_; }
// We register and unregister CancelableTaskManagers that run engine-dependent
// tasks. These tasks need to be shutdown if the engine is shut down.
void Register(CancelableTaskManager* task_manager);
void Unregister(CancelableTaskManager* task_manager);
......@@ -119,6 +121,7 @@ class V8_EXPORT_PRIVATE WasmEngine {
std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>> jobs_;
std::unique_ptr<WasmCodeManager> code_manager_;
WasmMemoryTracker memory_tracker_;
AccountingAllocator allocator_;
// Contains all CancelableTaskManagers that run tasks that are dependent
// on the isolate.
......
......@@ -157,8 +157,7 @@ struct V8_EXPORT_PRIVATE WasmModule {
mutable std::unique_ptr<std::unordered_map<uint32_t, WireBytesRef>>
function_names;
WasmModule() : WasmModule(nullptr) {}
WasmModule(std::unique_ptr<Zone> owned);
explicit WasmModule(std::unique_ptr<Zone> owned = nullptr);
WireBytesRef LookupFunctionName(const ModuleWireBytes& wire_bytes,
uint32_t function_index) const;
......
......@@ -177,19 +177,19 @@ TEST(SharedEngineRunImported) {
HandleScope scope(isolate.isolate());
ZoneBuffer* buffer = BuildReturnConstantModule(isolate.zone(), 23);
Handle<WasmInstanceObject> instance = isolate.CompileAndInstantiate(buffer);
// TODO(mstarzinger): Even just deferring the destruction of the native
// module doesn't work currently because of {WasmModule::signature_zone}.
// module = isolate.ExportInstance(instance);
module = isolate.ExportInstance(instance);
CHECK_EQ(23, isolate.Run(instance));
CHECK_EQ(2, module.use_count());
}
// TODO(mstarzinger): Tearing down the first Isolate still invalidates the
// compilation state and hence breaks tear down of the second Isolate.
// {
// SharedEngineIsolate isolate(&engine);
// HandleScope scope(isolate.isolate());
// Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
// CHECK_EQ(23, isolate.Run(instance));
// }
CHECK_EQ(1, module.use_count());
{
SharedEngineIsolate isolate(&engine);
HandleScope scope(isolate.isolate());
Handle<WasmInstanceObject> instance = isolate.ImportInstance(module);
CHECK_EQ(23, isolate.Run(instance));
CHECK_EQ(2, module.use_count());
}
CHECK_EQ(1, module.use_count());
}
} // namespace test_wasm_shared_engine
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment