From fa02b10065613f6e515c38a61e6f12d2f8e071d3 Mon Sep 17 00:00:00 2001 From: zoumujia Date: Wed, 9 Jul 2025 17:34:42 +0800 Subject: [PATCH] fix cmcgc jitfort issue Issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICL6O0 Signed-off-by: zoumujia --- .../base_runtime/base_runtime.cpp | 3 +++ .../heap/collector/collector_proxy.cpp | 3 ++- .../heap/collector/collector_resources.cpp | 19 +++++++++++++++++++ .../heap/collector/collector_resources.h | 10 +++++++--- .../heap/collector/trace_collector.cpp | 3 ++- .../heap/collector/trace_collector.h | 7 +++++++ common_components/heap/heap.cpp | 3 +++ common_components/heap/heap.h | 2 ++ ecmascript/ecma_vm.cpp | 10 ++++++++-- 9 files changed, 53 insertions(+), 7 deletions(-) diff --git a/common_components/base_runtime/base_runtime.cpp b/common_components/base_runtime/base_runtime.cpp index 516bb1a346..fda6b65fc8 100755 --- a/common_components/base_runtime/base_runtime.cpp +++ b/common_components/base_runtime/base_runtime.cpp @@ -209,6 +209,9 @@ void BaseRuntime::RequestGC(GcType type) void BaseRuntime::WaitForGCFinish() { Heap::GetHeap().WaitForGCFinish(); } +void BaseRuntime::MarkGCStart() { return Heap::GetHeap().MarkGCStart(); } +void BaseRuntime::MarkGCFinish() { return Heap::GetHeap().MarkGCFinish(); } + bool BaseRuntime::ForEachObj(HeapVisitor& visitor, bool safe) { return Heap::GetHeap().ForEachObject(visitor, safe); diff --git a/common_components/heap/collector/collector_proxy.cpp b/common_components/heap/collector/collector_proxy.cpp index 27476aab7c..4d6e18618e 100755 --- a/common_components/heap/collector/collector_proxy.cpp +++ b/common_components/heap/collector/collector_proxy.cpp @@ -42,7 +42,8 @@ void CollectorProxy::RunGarbageCollection(uint64_t gcIndex, GCReason reason) currentCollector_ = &wCollector_; break; } - currentCollector_->SetGcStarted(true); + currentCollector_->MarkGCStart(); + // currentCollector_->SetGcStarted(true); currentCollector_->RunGarbageCollection(gcIndex, reason); } } // namespace common diff --git a/common_components/heap/collector/collector_resources.cpp b/common_components/heap/collector/collector_resources.cpp index 34adfa649b..a19be0e872 100755 --- a/common_components/heap/collector/collector_resources.cpp +++ b/common_components/heap/collector/collector_resources.cpp @@ -218,6 +218,25 @@ void CollectorResources::NotifyGCFinished(uint64_t gcIndex) BroadcastGCFinished(); } +void CollectorResources::MarkGCStart() +{ + std::unique_lock lock(gcFinishedCondMutex_); + + // Wait for any existing GC to finish - inline the wait logic + std::function pred = [this] { + return !IsGcStarted(); + }; + gcFinishedCondVar_.wait(lock, pred); + + // Now claim GC ownership + SetGcStarted(true); +} + +void CollectorResources::MarkGCFinish(uint64_t gcIndex) +{ + NotifyGCFinished(gcIndex); +} + void CollectorResources::WaitForGCFinish() { uint64_t startTime = TimeUtil::MicroSeconds(); diff --git a/common_components/heap/collector/collector_resources.h b/common_components/heap/collector/collector_resources.h index 12e4a5c28f..a4d7cddb76 100755 --- a/common_components/heap/collector/collector_resources.h +++ b/common_components/heap/collector/collector_resources.h @@ -40,9 +40,6 @@ public: void WaitForGCFinish(); // gc main loop void RunTaskLoop(); - // Notify that GC has finished. - // Must be called by gc thread only - void NotifyGCFinished(uint64_t gcIndex); uint32_t GetGCThreadCount(const bool isConcurrent) const; Taskpool *GetThreadPool() const { return gcThreadPool_; } @@ -71,7 +68,14 @@ public: void StartRuntimeThreads(); void StopRuntimeThreads(); + void MarkGCStart(); + void MarkGCFinish(uint64_t gcIndex = 0); + private: + // Notify that GC has finished. + // Must be called by gc thread only + void NotifyGCFinished(uint64_t gcIndex); + void StartGCThreads(); void TerminateGCTask(); void StopGCThreads(); diff --git a/common_components/heap/collector/trace_collector.cpp b/common_components/heap/collector/trace_collector.cpp index aa9add28f7..90ff773209 100755 --- a/common_components/heap/collector/trace_collector.cpp +++ b/common_components/heap/collector/trace_collector.cpp @@ -569,7 +569,8 @@ void TraceCollector::PostGarbageCollection(uint64_t gcIndex) SatbBuffer::Instance().ReclaimALLPages(); // release pages in PagePool PagePool::Instance().Trim(); - collectorResources_.NotifyGCFinished(gcIndex); + // collectorResources_.NotifyGCFinished(gcIndex); + collectorResources_.MarkGCFinish(gcIndex); #if defined(GCINFO_DEBUG) && GCINFO_DEBUG DumpAfterGC(); diff --git a/common_components/heap/collector/trace_collector.h b/common_components/heap/collector/trace_collector.h index ca4336e178..ee659432b2 100755 --- a/common_components/heap/collector/trace_collector.h +++ b/common_components/heap/collector/trace_collector.h @@ -218,8 +218,15 @@ public: void SetHeapMarked(bool value) { collectorResources_.SetHeapMarked(value); } + // TODO: to be removed void SetGcStarted(bool val) { collectorResources_.SetGcStarted(val); } + void MarkGCStart() { collectorResources_.MarkGCStart(); } + void MarkGCFinish(uint64_t gcIndex) + { + collectorResources_.MarkGCFinish(gcIndex); + } + void RunGarbageCollection(uint64_t, GCReason) override; void ReclaimGarbageMemory(GCReason reason); diff --git a/common_components/heap/heap.cpp b/common_components/heap/heap.cpp index d883118cea..0b214bb826 100644 --- a/common_components/heap/heap.cpp +++ b/common_components/heap/heap.cpp @@ -75,6 +75,9 @@ public: void WaitForGCFinish() override { return collectorResources_.WaitForGCFinish(); } + void MarkGCStart() override { return collectorResources_.MarkGCStart(); } + void MarkGCFinish() override { return collectorResources_.MarkGCFinish(); } + bool IsGCEnabled() const override { return isGCEnabled_.load(); } void EnableGC(bool val) override { return isGCEnabled_.store(val); } diff --git a/common_components/heap/heap.h b/common_components/heap/heap.h index 6ad7dd1d13..3e0aceab74 100755 --- a/common_components/heap/heap.h +++ b/common_components/heap/heap.h @@ -82,6 +82,8 @@ public: virtual bool IsGcStarted() const = 0; virtual void WaitForGCFinish() = 0; + virtual void MarkGCStart() = 0; + virtual void MarkGCFinish() = 0; virtual bool IsGCEnabled() const = 0; virtual void EnableGC(bool val) = 0; diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index a5acbbb285..e3f1516bf4 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -402,8 +402,7 @@ EcmaVM::~EcmaVM() { if (g_isEnableCMCGC) { thread_->GetThreadHolder()->TransferToNative(); - common::BaseRuntime::WaitForGCFinish(); - thread_->GetThreadHolder()->TransferToRunning(); + common::BaseRuntime::MarkGCStart(); } if (isJitCompileVM_) { if (factory_ != nullptr) { @@ -412,6 +411,9 @@ EcmaVM::~EcmaVM() } stringTable_ = nullptr; thread_ = nullptr; + if (g_isEnableCMCGC) { + common::BaseRuntime::MarkGCFinish(); + } return; } #if ECMASCRIPT_ENABLE_THREAD_STATE_CHECK @@ -626,6 +628,10 @@ EcmaVM::~EcmaVM() delete thread_; thread_ = nullptr; } + + if (g_isEnableCMCGC) { + common::BaseRuntime::MarkGCFinish(); + } } void EcmaVM::InitializeEcmaScriptRunStat() -- Gitee