From 714a8bd586f38ddabfc82952fb9c5dae97f09825 Mon Sep 17 00:00:00 2001 From: ZitongLi Date: Fri, 11 Jul 2025 15:34:01 +0800 Subject: [PATCH] Moveevent callback optimization Moveevent callback optimization Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICLIQQ Signed-off-by: zitongli --- .../heap/collector/trace_collector.cpp | 2 ++ .../heap/collector/trace_collector.h | 3 ++ .../heap/w_collector/w_collector.cpp | 28 ++++--------------- .../profiler/heap_profiler_listener.cpp | 17 +++++++---- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/common_components/heap/collector/trace_collector.cpp b/common_components/heap/collector/trace_collector.cpp index 953d2e6f98..cfd83be9f4 100755 --- a/common_components/heap/collector/trace_collector.cpp +++ b/common_components/heap/collector/trace_collector.cpp @@ -21,6 +21,7 @@ #include "common_components/heap/allocator/alloc_buffer.h" #include "common_components/heap/collector/heuristic_gc_policy.h" #include "common_interfaces/base/runtime_param.h" +#include "common_interfaces/profiler/heap_profiler_listener.h" #include namespace common { @@ -686,6 +687,7 @@ void TraceCollector::ReclaimGarbageMemory(GCReason reason) void TraceCollector::RunGarbageCollection(uint64_t gcIndex, GCReason reason, GCType gcType) { + isHeapProfiler_ = HeapProfilerListener::GetInstance().HasMoveEventCb(); gcReason_ = reason; gcType_ = gcType; auto gcReasonName = std::string(g_gcRequests[gcReason_].name); diff --git a/common_components/heap/collector/trace_collector.h b/common_components/heap/collector/trace_collector.h index f9d9779ac8..fddac4027d 100755 --- a/common_components/heap/collector/trace_collector.h +++ b/common_components/heap/collector/trace_collector.h @@ -271,6 +271,9 @@ protected: uint32_t snapshotFinalizerNum_ = 0; + // uint32 type for performance optimization + uint32_t isHeapProfiler_ {0}; + // reason for current GC. GCReason gcReason_ = GC_REASON_USER; diff --git a/common_components/heap/w_collector/w_collector.cpp b/common_components/heap/w_collector/w_collector.cpp index 92dfa1deb7..4ac56e7875 100755 --- a/common_components/heap/w_collector/w_collector.cpp +++ b/common_components/heap/w_collector/w_collector.cpp @@ -72,11 +72,6 @@ bool WCollector::TryUpdateRefFieldImpl(BaseObject* obj, RefField<>& field, BaseO if (IsFromObject(fromObj)) { //LCOV_EXCL_BR_LINE if (copy) { //LCOV_EXCL_BR_LINE toObj = const_cast(this)->TryForwardObject(fromObj); - if (toObj != nullptr) { //LCOV_EXCL_BR_LINE - HeapProfilerListener::GetInstance().OnMoveEvent(reinterpret_cast(fromObj), - reinterpret_cast(toObj), - toObj->GetSize()); - } } else { //LCOV_EXCL_BR_LINE toObj = FindToVersion(fromObj); } @@ -272,9 +267,6 @@ BaseObject* WCollector::ForwardUpdateRawRef(ObjectRef& root) if (IsFromObject(oldObj)) { BaseObject* toVersion = TryForwardObject(oldObj); CHECK_CC(toVersion != nullptr); - HeapProfilerListener::GetInstance().OnMoveEvent(reinterpret_cast(oldObj), - reinterpret_cast(toVersion), - toVersion->GetSize()); RefField<> newField(toVersion); // CAS failure means some mutator or gc thread writes a new ref (must be a to-object), no need to retry. if (refField.CompareExchange(oldField.GetFieldValue(), newField.GetFieldValue())) { @@ -302,9 +294,6 @@ public: Heap::throwOOM(); return; } - HeapProfilerListener::GetInstance().OnMoveEvent(reinterpret_cast(oldObj), - reinterpret_cast(toVersion), - toVersion->GetSize()); RefField<> newField(toVersion); // CAS failure means some mutator or gc thread writes a new ref (must be a to-object), no need to retry. if (refField.CompareExchange(oldField.GetFieldValue(), newField.GetFieldValue())) { @@ -429,9 +418,6 @@ void WCollector::PreforwardConcurrentRoots() if (IsFromObject(oldObj)) { BaseObject *toVersion = TryForwardObject(oldObj); ASSERT_LOGF(toVersion != nullptr, "TryForwardObject failed"); - HeapProfilerListener::GetInstance().OnMoveEvent(reinterpret_cast(oldObj), - reinterpret_cast(toVersion), - toVersion->GetSize()); RefField<> newField(toVersion); // CAS failure means some mutator or gc thread writes a new ref (must be a to-object), no need to retry. if (refField.CompareExchange(oldField.GetFieldValue(), newField.GetFieldValue())) { @@ -567,9 +553,6 @@ WeakRefFieldVisitor WCollector::GetWeakRefFieldVisitor() if (IsFromObject(oldObj)) { BaseObject *toVersion = TryForwardObject(oldObj); CHECK_CC(toVersion != nullptr); - HeapProfilerListener::GetInstance().OnMoveEvent(reinterpret_cast(oldObj), - reinterpret_cast(toVersion), - toVersion->GetSize()); RefField<> newField(toVersion); // CAS failure means some mutator or gc thread writes a new ref (must be // a to-object), no need to retry. @@ -840,17 +823,16 @@ void WCollector::ProcessFinalizers() BaseObject* WCollector::ForwardObject(BaseObject* obj) { BaseObject* to = TryForwardObject(obj); - if (to != nullptr) { - HeapProfilerListener::GetInstance().OnMoveEvent(reinterpret_cast(obj), - reinterpret_cast(to), - to->GetSize()); - } return (to != nullptr) ? to : obj; } BaseObject* WCollector::TryForwardObject(BaseObject* obj) { - return CopyObjectImpl(obj); + BaseObject *to = CopyObjectImpl(obj); + if (UNLIKELY(isHeapProfiler_)) { + HeapProfilerListener::GetInstance().OnMoveEvent(obj, to); + } + return to; } // ConcurrentGC diff --git a/common_components/profiler/heap_profiler_listener.cpp b/common_components/profiler/heap_profiler_listener.cpp index f1c588edec..8413cb795f 100644 --- a/common_components/profiler/heap_profiler_listener.cpp +++ b/common_components/profiler/heap_profiler_listener.cpp @@ -13,7 +13,6 @@ * limitations under the License. */ -#include "common_components/log/log.h" #include "common_interfaces/profiler/heap_profiler_listener.h" namespace common { @@ -36,12 +35,20 @@ void HeapProfilerListener::UnRegisterMoveEventCb(uint32_t key) moveEventCbs_.erase(key); } -void HeapProfilerListener::OnMoveEvent(uintptr_t fromObj, uintptr_t toObj, size_t size) +bool HeapProfilerListener::HasMoveEventCb() { + return !moveEventCbs_.empty(); +} + +void HeapProfilerListener::OnMoveEvent(BaseObject *fromObj, BaseObject *toObj) +{ + if (toObj == nullptr || toObj == fromObj) { + return; + } std::shared_lock lock(mutex_); - for (const auto &pair : moveEventCbs_) { - if (pair.second) { - pair.second(fromObj, toObj, size); + for (const auto &[id, cb] : moveEventCbs_) { + if (cb) { + cb(reinterpret_cast(fromObj), reinterpret_cast(toObj), toObj->GetSize()); } } } -- Gitee