From a132b7a39fc46b117b8a76d9fc0a661a04cb146a Mon Sep 17 00:00:00 2001 From: jinjiawei Date: Tue, 26 Aug 2025 15:07:21 +0800 Subject: [PATCH] add sharedgc in Sensitive Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICUU9W?from=project-issue Signed-off-by: jinjiawei Change-Id: Ib172399d270f628639d9ad70db21ee870c86d888 --- ecmascript/mem/heap.cpp | 35 +++++++++++++++++++++++++++++++++++ ecmascript/mem/heap.h | 31 ++++++++++++++++++------------- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/ecmascript/mem/heap.cpp b/ecmascript/mem/heap.cpp index 6ba62cfdd6..ac706bec8e 100644 --- a/ecmascript/mem/heap.cpp +++ b/ecmascript/mem/heap.cpp @@ -440,6 +440,10 @@ void SharedHeap::DaemonCollectGarbage([[maybe_unused]]TriggerGCType gcType, [[ma SharedHeapVerification(this, VerifyKind::VERIFY_POST_SHARED_GC).VerifyAll(); } CollectGarbageFinish(true, gcType); + // Update record sheap object size after gc if in sensitive status + if (GetSensitiveStatus() == AppSensitiveStatus::ENTER_HIGH_SENSITIVE) { + SetRecordHeapObjectSizeBeforeSensitive(GetHeapObjectSize()); + } } InvokeSharedNativePointerCallbacks(); // Don't process weak node nativeFinalizeCallback here. These callbacks would be called after localGC. @@ -710,6 +714,30 @@ bool SharedHeap::CheckIfNeedStopCollectionByStartup() return false; } +bool SharedHeap::CheckIfNeedStopCollectionByHighSensitive() +{ + AppSensitiveStatus sensitiveStatus = GetSensitiveStatus(); + if (sensitiveStatus != AppSensitiveStatus::ENTER_HIGH_SENSITIVE) { + return false; + } + + size_t objSize = GetHeapObjectSize(); + size_t recordSizeBeforeSensitive = GetRecordHeapObjectSizeBeforeSensitive(); + if (recordSizeBeforeSensitive == 0) { + recordSizeBeforeSensitive = objSize; + SetRecordHeapObjectSizeBeforeSensitive(recordSizeBeforeSensitive); + } + + if (objSize < recordSizeBeforeSensitive + config_.GetIncObjSizeThresholdInSensitive() + && !ObjectExceedMaxHeapSize()) { + return true; + } + + LOG_GC(INFO) << "SmartGC: sHeap obj size: " << GetHeapObjectSize() + << ", exceed sensitive gc threshold"; + return false; +} + bool SharedHeap::NeedStopCollection() { if (CheckIfNeedStopCollectionByStartup()) { @@ -723,6 +751,12 @@ bool SharedHeap::NeedStopCollection() if (!ObjectExceedMaxHeapSize()) { return true; } + + // check high sensitive before checking startup because we still need to record + // current heap object size when high sensitive happens in startup duration + if (CheckIfNeedStopCollectionByHighSensitive()) { + return true; + } return false; } @@ -2494,6 +2528,7 @@ bool Heap::HandleExitHighSensitiveEvent() && CASSensitiveStatus(status, AppSensitiveStatus::NORMAL_SCENE) && !OnStartupEvent()) { // Set record heap obj size 0 after exit high senstive SetRecordHeapObjectSizeBeforeSensitive(0); + SharedHeap::GetInstance()->SetRecordHeapObjectSizeBeforeSensitive(0); // set overshoot size to increase gc threashold larger 8MB than current heap size. TryIncreaseNewSpaceOvershootByConfigSize(); diff --git a/ecmascript/mem/heap.h b/ecmascript/mem/heap.h index dcffcaaad3..436e48ed65 100644 --- a/ecmascript/mem/heap.h +++ b/ecmascript/mem/heap.h @@ -177,6 +177,8 @@ public: */ virtual bool CheckOngoingConcurrentMarking() = 0; + virtual bool CheckIfNeedStopCollectionByHighSensitive() = 0; + virtual bool OldSpaceExceedCapacity(size_t size) const = 0; virtual bool OldSpaceExceedLimit() const = 0; @@ -334,6 +336,16 @@ public: return enablePageTagThreadId_; } + size_t GetRecordHeapObjectSizeBeforeSensitive() const + { + return recordObjSizeBeforeSensitive_; + } + + void SetRecordHeapObjectSizeBeforeSensitive(size_t objSize) + { + recordObjSizeBeforeSensitive_ = objSize; + } + void ThrowOutOfMemoryErrorForDefault(JSThread *thread, size_t size, std::string functionName, bool NonMovableObjNearOOM = false); @@ -451,6 +463,9 @@ protected: bool enablePageTagThreadId_ {false}; std::atomic_bool inGC_ {false}; int32_t recursionDepth_ {0}; + + // Record heap/sHeap object size before enter sensitive status + size_t recordObjSizeBeforeSensitive_ {0}; #ifndef NDEBUG bool triggerCollectionOnNewObject_ {true}; #endif @@ -596,6 +611,8 @@ public: bool CheckIfNeedStopCollectionByStartup(); + bool CheckIfNeedStopCollectionByHighSensitive() override; + void TryAdjustSpaceOvershootByConfigSize(); bool CheckAndTriggerSharedGC(JSThread *thread); @@ -1431,7 +1448,7 @@ public: bool CheckIfNeedStopCollectionByStartup(); - bool CheckIfNeedStopCollectionByHighSensitive(); + bool CheckIfNeedStopCollectionByHighSensitive() override; bool NeedStopCollection() override; @@ -1446,16 +1463,6 @@ public: return smartGCStats_.sensitiveStatus_.load(std::memory_order_acquire); } - void SetRecordHeapObjectSizeBeforeSensitive(size_t objSize) - { - recordObjSizeBeforeSensitive_ = objSize; - } - - size_t GetRecordHeapObjectSizeBeforeSensitive() const - { - return recordObjSizeBeforeSensitive_; - } - void SetNearGCInSensitive(bool flag) { nearGCInSensitive_ = flag; @@ -1936,8 +1943,6 @@ private: // Record memory before taskpool start, used to determine trigger GC or not after task finish. size_t recordObjectSize_ {0}; size_t recordNativeSize_ {0}; - // Record heap object size before enter sensitive status - size_t recordObjSizeBeforeSensitive_ {0}; bool nearGCInSensitive_ {false}; size_t pendingAsyncNativeCallbackSize_ {0}; -- Gitee