Commit 69546692 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Store WasmEngine in NativeModule

The {CompilationState} currently stores the {WasmEngine}, while the
{NativeModule} only stores the {WasmCodeManager}. From a high-level
view, this does not make much sense. The {NativeModule} belongs to
exactly one {WasmEngine}, so that link should be stored there. We can
then get to the {WasmCodeManager} from the {WasmEngine}.

This change requires a refactoring of the {WasmCodeManagerTest} which
created {WasmCodeManager}s independent of the {Isolate} and the
{WasmEngine}. This is not supported any more.
Note that in production, each {WasmEngine} owns exactly one
{WasmCodeManager} and one {WasmMemoryTracker}, so testing that a
{WasmMemoryTracker} can be shared by several {WasmCodeManager}s didn't
make sense in the first place.

R=mstarzinger@chromium.org

Bug: v8:8217
Change-Id: I582e698be35f97dbd38bf6e12eb7f8ee4fc1f0f2
Reviewed-on: https://chromium-review.googlesource.com/c/1297960
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56992}
parent 5ed7e71f
...@@ -119,7 +119,7 @@ class CompilationState { ...@@ -119,7 +119,7 @@ class CompilationState {
bool has_outstanding_units() const { return outstanding_units_ > 0; } bool has_outstanding_units() const { return outstanding_units_ > 0; }
WasmEngine* wasm_engine() const { return wasm_engine_; } WasmEngine* wasm_engine() const { return native_module_->wasm_engine(); }
CompileMode compile_mode() const { return compile_mode_; } CompileMode compile_mode() const { return compile_mode_; }
WasmFeatures* detected_features() { return &detected_features_; } WasmFeatures* detected_features() { return &detected_features_; }
...@@ -131,10 +131,9 @@ class CompilationState { ...@@ -131,10 +131,9 @@ class CompilationState {
: baseline_finish_units_; : baseline_finish_units_;
} }
// TODO(7423): Get rid of the Isolate field to make sure the CompilationState // TODO(mstarzinger): Get rid of the Isolate field to make sure the
// can be shared across multiple Isolates. // CompilationState can be shared across multiple Isolates.
Isolate* const isolate_; Isolate* const isolate_;
WasmEngine* const wasm_engine_;
NativeModule* const native_module_; NativeModule* const native_module_;
const CompileMode compile_mode_; const CompileMode compile_mode_;
bool baseline_compilation_finished_ = false; bool baseline_compilation_finished_ = false;
...@@ -2848,7 +2847,6 @@ std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState( ...@@ -2848,7 +2847,6 @@ std::unique_ptr<CompilationState, CompilationStateDeleter> NewCompilationState(
CompilationState::CompilationState(internal::Isolate* isolate, CompilationState::CompilationState(internal::Isolate* isolate,
NativeModule* native_module) NativeModule* native_module)
: isolate_(isolate), : isolate_(isolate),
wasm_engine_(isolate->wasm_engine()),
native_module_(native_module), native_module_(native_module),
compile_mode_(FLAG_wasm_tier_up && compile_mode_(FLAG_wasm_tier_up &&
native_module->module()->origin == kWasmOrigin native_module->module()->origin == kWasmOrigin
......
...@@ -337,7 +337,7 @@ WasmCode::~WasmCode() { ...@@ -337,7 +337,7 @@ WasmCode::~WasmCode() {
NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled, NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled,
bool can_request_more, VirtualMemory code_space, bool can_request_more, VirtualMemory code_space,
WasmCodeManager* code_manager, WasmEngine* wasm_engine,
std::shared_ptr<const WasmModule> module) std::shared_ptr<const WasmModule> module)
: enabled_features_(enabled), : enabled_features_(enabled),
module_(std::move(module)), module_(std::move(module)),
...@@ -345,7 +345,7 @@ NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled, ...@@ -345,7 +345,7 @@ NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled,
import_wrapper_cache_(std::unique_ptr<WasmImportWrapperCache>( import_wrapper_cache_(std::unique_ptr<WasmImportWrapperCache>(
new WasmImportWrapperCache(this))), new WasmImportWrapperCache(this))),
free_code_space_(code_space.region()), free_code_space_(code_space.region()),
wasm_code_manager_(code_manager), wasm_engine_(wasm_engine),
can_request_more_memory_(can_request_more), can_request_more_memory_(can_request_more),
use_trap_handler_(trap_handler::IsTrapHandlerEnabled() ? kUseTrapHandler use_trap_handler_(trap_handler::IsTrapHandlerEnabled() ? kUseTrapHandler
: kNoTrapHandler) { : kNoTrapHandler) {
...@@ -680,14 +680,15 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) { ...@@ -680,14 +680,15 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) {
Address hint = owned_code_space_.empty() ? kNullAddress Address hint = owned_code_space_.empty() ? kNullAddress
: owned_code_space_.back().end(); : owned_code_space_.back().end();
VirtualMemory new_mem = VirtualMemory new_mem = wasm_engine_->code_manager()->TryAllocate(
wasm_code_manager_->TryAllocate(size, reinterpret_cast<void*>(hint)); size, reinterpret_cast<void*>(hint));
if (!new_mem.IsReserved()) { if (!new_mem.IsReserved()) {
V8::FatalProcessOutOfMemory(nullptr, V8::FatalProcessOutOfMemory(nullptr,
"NativeModule::AllocateForCode reservation"); "NativeModule::AllocateForCode reservation");
UNREACHABLE(); UNREACHABLE();
} }
wasm_code_manager_->AssignRanges(new_mem.address(), new_mem.end(), this); wasm_engine_->code_manager()->AssignRanges(new_mem.address(), new_mem.end(),
this);
free_code_space_.Merge(new_mem.region()); free_code_space_.Merge(new_mem.region());
owned_code_space_.emplace_back(std::move(new_mem)); owned_code_space_.emplace_back(std::move(new_mem));
...@@ -720,7 +721,7 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) { ...@@ -720,7 +721,7 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) {
Address start = std::max(commit_start, vmem.address()); Address start = std::max(commit_start, vmem.address());
Address end = std::min(commit_end, vmem.end()); Address end = std::min(commit_end, vmem.end());
size_t commit_size = static_cast<size_t>(end - start); size_t commit_size = static_cast<size_t>(end - start);
if (!wasm_code_manager_->Commit(start, commit_size)) { if (!wasm_engine_->code_manager()->Commit(start, commit_size)) {
V8::FatalProcessOutOfMemory(nullptr, V8::FatalProcessOutOfMemory(nullptr,
"NativeModule::AllocateForCode commit"); "NativeModule::AllocateForCode commit");
UNREACHABLE(); UNREACHABLE();
...@@ -732,7 +733,8 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) { ...@@ -732,7 +733,8 @@ Vector<byte> NativeModule::AllocateForCode(size_t size) {
if (commit_start >= commit_end) break; if (commit_start >= commit_end) break;
} }
#else #else
if (!wasm_code_manager_->Commit(commit_start, commit_end - commit_start)) { if (!wasm_engine_->code_manager()->Commit(commit_start,
commit_end - commit_start)) {
V8::FatalProcessOutOfMemory(nullptr, V8::FatalProcessOutOfMemory(nullptr,
"NativeModule::AllocateForCode commit"); "NativeModule::AllocateForCode commit");
UNREACHABLE(); UNREACHABLE();
...@@ -800,7 +802,7 @@ NativeModule::~NativeModule() { ...@@ -800,7 +802,7 @@ NativeModule::~NativeModule() {
// Cancel all background compilation before resetting any field of the // Cancel all background compilation before resetting any field of the
// NativeModule or freeing anything. // NativeModule or freeing anything.
CancelAndWaitCompilationState(compilation_state_.get()); CancelAndWaitCompilationState(compilation_state_.get());
wasm_code_manager_->FreeNativeModule(this); wasm_engine_->code_manager()->FreeNativeModule(this);
} }
WasmCodeManager::WasmCodeManager(WasmMemoryTracker* memory_tracker, WasmCodeManager::WasmCodeManager(WasmMemoryTracker* memory_tracker,
...@@ -886,6 +888,10 @@ void WasmCodeManager::SampleModuleSizes(Isolate* isolate) const { ...@@ -886,6 +888,10 @@ void WasmCodeManager::SampleModuleSizes(Isolate* isolate) const {
} }
} }
void WasmCodeManager::SetMaxCommittedMemoryForTesting(size_t limit) {
remaining_uncommitted_code_space_.store(limit);
}
namespace { namespace {
void ModuleSamplingCallback(v8::Isolate* v8_isolate, v8::GCType type, void ModuleSamplingCallback(v8::Isolate* v8_isolate, v8::GCType type,
...@@ -938,6 +944,7 @@ bool WasmCodeManager::ShouldForceCriticalMemoryPressureNotification() { ...@@ -938,6 +944,7 @@ bool WasmCodeManager::ShouldForceCriticalMemoryPressureNotification() {
std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule( std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule(
Isolate* isolate, const WasmFeatures& enabled, size_t code_size_estimate, Isolate* isolate, const WasmFeatures& enabled, size_t code_size_estimate,
bool can_request_more, std::shared_ptr<const WasmModule> module) { bool can_request_more, std::shared_ptr<const WasmModule> module) {
DCHECK_EQ(this, isolate->wasm_engine()->code_manager());
if (ShouldForceCriticalMemoryPressureNotification()) { if (ShouldForceCriticalMemoryPressureNotification()) {
(reinterpret_cast<v8::Isolate*>(isolate)) (reinterpret_cast<v8::Isolate*>(isolate))
->MemoryPressureNotification(MemoryPressureLevel::kCritical); ->MemoryPressureNotification(MemoryPressureLevel::kCritical);
...@@ -966,9 +973,9 @@ std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule( ...@@ -966,9 +973,9 @@ std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule(
Address start = code_space.address(); Address start = code_space.address();
size_t size = code_space.size(); size_t size = code_space.size();
Address end = code_space.end(); Address end = code_space.end();
std::unique_ptr<NativeModule> ret( std::unique_ptr<NativeModule> ret(new NativeModule(
new NativeModule(isolate, enabled, can_request_more, isolate, enabled, can_request_more, std::move(code_space),
std::move(code_space), this, std::move(module))); isolate->wasm_engine(), std::move(module)));
TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start, TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start,
size); size);
AssignRangesAndAddModule(start, end, ret.get()); AssignRangesAndAddModule(start, end, ret.get());
......
...@@ -30,6 +30,7 @@ namespace wasm { ...@@ -30,6 +30,7 @@ namespace wasm {
class NativeModule; class NativeModule;
class WasmCodeManager; class WasmCodeManager;
class WasmEngine;
class WasmMemoryTracker; class WasmMemoryTracker;
class WasmImportWrapperCache; class WasmImportWrapperCache;
struct WasmModule; struct WasmModule;
...@@ -332,7 +333,7 @@ class V8_EXPORT_PRIVATE NativeModule final { ...@@ -332,7 +333,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
wire_bytes_ = std::move(wire_bytes); wire_bytes_ = std::move(wire_bytes);
} }
const WasmModule* module() const { return module_.get(); } const WasmModule* module() const { return module_.get(); }
WasmCodeManager* code_manager() const { return wasm_code_manager_; } WasmEngine* wasm_engine() const { return wasm_engine_; }
WasmCode* Lookup(Address) const; WasmCode* Lookup(Address) const;
...@@ -352,7 +353,7 @@ class V8_EXPORT_PRIVATE NativeModule final { ...@@ -352,7 +353,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
NativeModule(Isolate* isolate, const WasmFeatures& enabled_features, NativeModule(Isolate* isolate, const WasmFeatures& enabled_features,
bool can_request_more, VirtualMemory code_space, bool can_request_more, VirtualMemory code_space,
WasmCodeManager* code_manager, WasmEngine* wasm_engine,
std::shared_ptr<const WasmModule> module); std::shared_ptr<const WasmModule> module);
WasmCode* AddAnonymousCode(Handle<Code>, WasmCode::Kind kind, WasmCode* AddAnonymousCode(Handle<Code>, WasmCode::Kind kind,
...@@ -453,7 +454,7 @@ class V8_EXPORT_PRIVATE NativeModule final { ...@@ -453,7 +454,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
// End of fields protected by {allocation_mutex_}. // End of fields protected by {allocation_mutex_}.
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
WasmCodeManager* wasm_code_manager_; WasmEngine* const wasm_engine_;
std::atomic<size_t> committed_code_space_{0}; std::atomic<size_t> committed_code_space_{0};
int modification_scope_depth_ = 0; int modification_scope_depth_ = 0;
bool can_request_more_memory_; bool can_request_more_memory_;
...@@ -486,6 +487,8 @@ class V8_EXPORT_PRIVATE WasmCodeManager final { ...@@ -486,6 +487,8 @@ class V8_EXPORT_PRIVATE WasmCodeManager final {
// Add a sample of all module sizes. // Add a sample of all module sizes.
void SampleModuleSizes(Isolate* isolate) const; void SampleModuleSizes(Isolate* isolate) const;
void SetMaxCommittedMemoryForTesting(size_t limit);
// TODO(v8:7424): For now we sample module sizes in a GC callback. This will // TODO(v8:7424): For now we sample module sizes in a GC callback. This will
// bias samples towards apps with high memory pressure. We should switch to // bias samples towards apps with high memory pressure. We should switch to
// using sampling based on regular intervals independent of the GC. // using sampling based on regular intervals independent of the GC.
......
...@@ -188,7 +188,7 @@ std::shared_ptr<NativeModule> WasmEngine::ExportNativeModule( ...@@ -188,7 +188,7 @@ std::shared_ptr<NativeModule> WasmEngine::ExportNativeModule(
Handle<WasmModuleObject> WasmEngine::ImportNativeModule( Handle<WasmModuleObject> WasmEngine::ImportNativeModule(
Isolate* isolate, std::shared_ptr<NativeModule> shared_module) { Isolate* isolate, std::shared_ptr<NativeModule> shared_module) {
CHECK_EQ(code_manager(), shared_module->code_manager()); CHECK_EQ(this, shared_module->wasm_engine());
Vector<const byte> wire_bytes = shared_module->wire_bytes(); Vector<const byte> wire_bytes = shared_module->wire_bytes();
const WasmModule* module = shared_module->module(); const WasmModule* module = shared_module->module();
Handle<Script> script = Handle<Script> script =
......
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