Commit 19bad28d authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm][gc] Link Isolates from WasmEngine

First step towards GC of wasm code: Introduce a link to all Isolates
that use a WasmEngine.

R=mstarzinger@chromium.org

Bug: v8:8217
Change-Id: Ib7f4495e7c7e5cc9ad58293518c65738f23d664c
Reviewed-on: https://chromium-review.googlesource.com/1240335
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56204}
parent 47945fa7
...@@ -2300,6 +2300,13 @@ void Isolate::UnregisterManagedPtrDestructor(ManagedPtrDestructor* destructor) { ...@@ -2300,6 +2300,13 @@ void Isolate::UnregisterManagedPtrDestructor(ManagedPtrDestructor* destructor) {
destructor->next_ = nullptr; destructor->next_ = nullptr;
} }
void Isolate::SetWasmEngine(std::shared_ptr<wasm::WasmEngine> engine) {
DCHECK_NULL(wasm_engine_); // Only call once before {Init}.
wasm_engine_ = std::move(engine);
wasm_engine_->AddIsolate(this);
wasm::WasmCodeManager::InstallSamplingGCCallback(this);
}
// NOLINTNEXTLINE // NOLINTNEXTLINE
Isolate::PerIsolateThreadData::~PerIsolateThreadData() { Isolate::PerIsolateThreadData::~PerIsolateThreadData() {
#if defined(USE_SIMULATOR) #if defined(USE_SIMULATOR)
...@@ -2656,7 +2663,10 @@ void Isolate::Deinit() { ...@@ -2656,7 +2663,10 @@ void Isolate::Deinit() {
heap_.TearDown(); heap_.TearDown();
logger_->TearDown(); logger_->TearDown();
wasm_engine_.reset(); if (wasm_engine_) {
wasm_engine_->RemoveIsolate(this);
wasm_engine_.reset();
}
if (FLAG_embedded_builtins) { if (FLAG_embedded_builtins) {
if (DefaultEmbeddedBlob() == nullptr && embedded_blob() != nullptr) { if (DefaultEmbeddedBlob() == nullptr && embedded_blob() != nullptr) {
...@@ -2973,9 +2983,9 @@ bool Isolate::Init(StartupDeserializer* des) { ...@@ -2973,9 +2983,9 @@ bool Isolate::Init(StartupDeserializer* des) {
// Setup the wasm engine. // Setup the wasm engine.
if (wasm_engine_ == nullptr) { if (wasm_engine_ == nullptr) {
wasm_engine_ = wasm::WasmEngine::GetWasmEngine(); SetWasmEngine(wasm::WasmEngine::GetWasmEngine());
wasm::WasmCodeManager::InstallSamplingGCCallback(this);
} }
DCHECK_NOT_NULL(wasm_engine_);
deoptimizer_data_ = new DeoptimizerData(heap()); deoptimizer_data_ = new DeoptimizerData(heap());
......
...@@ -1509,10 +1509,7 @@ class Isolate : private HiddenFactory { ...@@ -1509,10 +1509,7 @@ class Isolate : private HiddenFactory {
} }
wasm::WasmEngine* wasm_engine() const { return wasm_engine_.get(); } wasm::WasmEngine* wasm_engine() const { return wasm_engine_.get(); }
void set_wasm_engine(std::shared_ptr<wasm::WasmEngine> engine) { void SetWasmEngine(std::shared_ptr<wasm::WasmEngine> engine);
DCHECK_NULL(wasm_engine_); // Only call once before {Init}.
wasm_engine_ = std::move(engine);
}
const v8::Context::BackupIncumbentScope* top_backup_incumbent_scope() const { const v8::Context::BackupIncumbentScope* top_backup_incumbent_scope() const {
return top_backup_incumbent_scope_; return top_backup_incumbent_scope_;
......
...@@ -24,6 +24,8 @@ WasmEngine::WasmEngine() ...@@ -24,6 +24,8 @@ WasmEngine::WasmEngine()
WasmEngine::~WasmEngine() { WasmEngine::~WasmEngine() {
// All AsyncCompileJobs have been canceled. // All AsyncCompileJobs have been canceled.
DCHECK(jobs_.empty()); DCHECK(jobs_.empty());
// All Isolates have been deregistered.
DCHECK(isolates_.empty());
} }
bool WasmEngine::SyncValidate(Isolate* isolate, const WasmFeatures& enabled, bool WasmEngine::SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
...@@ -251,6 +253,7 @@ std::unique_ptr<AsyncCompileJob> WasmEngine::RemoveCompileJob( ...@@ -251,6 +253,7 @@ std::unique_ptr<AsyncCompileJob> WasmEngine::RemoveCompileJob(
bool WasmEngine::HasRunningCompileJob(Isolate* isolate) { bool WasmEngine::HasRunningCompileJob(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_); base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
for (auto& entry : jobs_) { for (auto& entry : jobs_) {
if (entry.first->isolate() == isolate) return true; if (entry.first->isolate() == isolate) return true;
} }
...@@ -259,6 +262,7 @@ bool WasmEngine::HasRunningCompileJob(Isolate* isolate) { ...@@ -259,6 +262,7 @@ bool WasmEngine::HasRunningCompileJob(Isolate* isolate) {
void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) { void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_); base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
for (auto it = jobs_.begin(); it != jobs_.end();) { for (auto it = jobs_.begin(); it != jobs_.end();) {
if (it->first->isolate() == isolate) { if (it->first->isolate() == isolate) {
it = jobs_.erase(it); it = jobs_.erase(it);
...@@ -268,6 +272,18 @@ void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) { ...@@ -268,6 +272,18 @@ void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) {
} }
} }
void WasmEngine::AddIsolate(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(0, isolates_.count(isolate));
isolates_.insert(isolate);
}
void WasmEngine::RemoveIsolate(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
isolates_.erase(isolate);
}
namespace { namespace {
struct WasmEnginePointerConstructTrait final { struct WasmEnginePointerConstructTrait final {
...@@ -286,16 +302,19 @@ base::LazyStaticInstance<std::shared_ptr<WasmEngine>, ...@@ -286,16 +302,19 @@ base::LazyStaticInstance<std::shared_ptr<WasmEngine>,
} // namespace } // namespace
// static
void WasmEngine::InitializeOncePerProcess() { void WasmEngine::InitializeOncePerProcess() {
if (!FLAG_wasm_shared_engine) return; if (!FLAG_wasm_shared_engine) return;
global_wasm_engine.Pointer()->reset(new WasmEngine()); global_wasm_engine.Pointer()->reset(new WasmEngine());
} }
// static
void WasmEngine::GlobalTearDown() { void WasmEngine::GlobalTearDown() {
if (!FLAG_wasm_shared_engine) return; if (!FLAG_wasm_shared_engine) return;
global_wasm_engine.Pointer()->reset(); global_wasm_engine.Pointer()->reset();
} }
// static
std::shared_ptr<WasmEngine> WasmEngine::GetWasmEngine() { std::shared_ptr<WasmEngine> WasmEngine::GetWasmEngine() {
if (FLAG_wasm_shared_engine) return global_wasm_engine.Get(); if (FLAG_wasm_shared_engine) return global_wasm_engine.Get();
return std::shared_ptr<WasmEngine>(new WasmEngine()); return std::shared_ptr<WasmEngine>(new WasmEngine());
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define V8_WASM_WASM_ENGINE_H_ #define V8_WASM_WASM_ENGINE_H_
#include <memory> #include <memory>
#include <unordered_set>
#include "src/wasm/wasm-code-manager.h" #include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-memory.h" #include "src/wasm/wasm-memory.h"
...@@ -135,6 +136,10 @@ class V8_EXPORT_PRIVATE WasmEngine { ...@@ -135,6 +136,10 @@ class V8_EXPORT_PRIVATE WasmEngine {
// for tearing down an isolate, or to clean it up to be reused. // for tearing down an isolate, or to clean it up to be reused.
void DeleteCompileJobsOnIsolate(Isolate* isolate); void DeleteCompileJobsOnIsolate(Isolate* isolate);
// Manage the set of Isolates that use this WasmEngine.
void AddIsolate(Isolate* isolate);
void RemoveIsolate(Isolate* isolate);
// Call on process start and exit. // Call on process start and exit.
static void InitializeOncePerProcess(); static void InitializeOncePerProcess();
static void GlobalTearDown(); static void GlobalTearDown();
...@@ -168,6 +173,9 @@ class V8_EXPORT_PRIVATE WasmEngine { ...@@ -168,6 +173,9 @@ class V8_EXPORT_PRIVATE WasmEngine {
std::unique_ptr<CompilationStatistics> compilation_stats_; std::unique_ptr<CompilationStatistics> compilation_stats_;
std::unique_ptr<CodeTracer> code_tracer_; std::unique_ptr<CodeTracer> code_tracer_;
// Set of isolates which use this WasmEngine. Used for cross-isolate GCs.
std::unordered_set<Isolate*> isolates_;
// End of fields protected by {mutex_}. // End of fields protected by {mutex_}.
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
......
...@@ -56,7 +56,7 @@ class SharedEngineIsolate { ...@@ -56,7 +56,7 @@ class SharedEngineIsolate {
public: public:
explicit SharedEngineIsolate(SharedEngine* engine) explicit SharedEngineIsolate(SharedEngine* engine)
: isolate_(v8::Isolate::Allocate()) { : isolate_(v8::Isolate::Allocate()) {
isolate()->set_wasm_engine(engine->ExportEngineForSharing()); isolate()->SetWasmEngine(engine->ExportEngineForSharing());
v8::Isolate::CreateParams create_params; v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate::Initialize(isolate_, create_params); v8::Isolate::Initialize(isolate_, create_params);
......
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