From 02e74bfa613678115618ea592c03324d28dfec3a Mon Sep 17 00:00:00 2001 From: Lei Zaakjyu Date: Sat, 23 Dec 2023 09:15:40 +0800 Subject: [PATCH] refactor heap locking assert functions --- src/hotspot/share/gc/g1/g1CollectedHeap.hpp | 72 +++---------------- .../share/gc/g1/g1CollectedHeap.inline.hpp | 56 +++++++++++++++ 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/hotspot/share/gc/g1/g1CollectedHeap.hpp b/src/hotspot/share/gc/g1/g1CollectedHeap.hpp index 58ac36a734f7f..f1fe22541c21f 100644 --- a/src/hotspot/share/gc/g1/g1CollectedHeap.hpp +++ b/src/hotspot/share/gc/g1/g1CollectedHeap.hpp @@ -306,72 +306,16 @@ class G1CollectedHeap : public CollectedHeap { void trace_heap(GCWhen::Type when, const GCTracer* tracer) override; - // These are macros so that, if the assert fires, we get the correct + // These are assert functions so that, if the assert fires, we get the correct // line number, file, etc. -#define heap_locking_asserts_params(_extra_message_) \ - "%s : Heap_lock locked: %s, at safepoint: %s, is VM thread: %s", \ - (_extra_message_), \ - BOOL_TO_STR(Heap_lock->owned_by_self()), \ - BOOL_TO_STR(SafepointSynchronize::is_at_safepoint()), \ - BOOL_TO_STR(Thread::current()->is_VM_thread()) - -#define assert_heap_locked() \ - do { \ - assert(Heap_lock->owned_by_self(), \ - heap_locking_asserts_params("should be holding the Heap_lock")); \ - } while (0) - -#define assert_heap_locked_or_at_safepoint(_should_be_vm_thread_) \ - do { \ - assert(Heap_lock->owned_by_self() || \ - (SafepointSynchronize::is_at_safepoint() && \ - ((_should_be_vm_thread_) == Thread::current()->is_VM_thread())), \ - heap_locking_asserts_params("should be holding the Heap_lock or " \ - "should be at a safepoint")); \ - } while (0) - -#define assert_heap_locked_and_not_at_safepoint() \ - do { \ - assert(Heap_lock->owned_by_self() && \ - !SafepointSynchronize::is_at_safepoint(), \ - heap_locking_asserts_params("should be holding the Heap_lock and " \ - "should not be at a safepoint")); \ - } while (0) - -#define assert_heap_not_locked() \ - do { \ - assert(!Heap_lock->owned_by_self(), \ - heap_locking_asserts_params("should not be holding the Heap_lock")); \ - } while (0) - -#define assert_heap_not_locked_and_not_at_safepoint() \ - do { \ - assert(!Heap_lock->owned_by_self() && \ - !SafepointSynchronize::is_at_safepoint(), \ - heap_locking_asserts_params("should not be holding the Heap_lock and " \ - "should not be at a safepoint")); \ - } while (0) - -#define assert_at_safepoint_on_vm_thread() \ - do { \ - assert_at_safepoint(); \ - assert(Thread::current_or_null() != nullptr, "no current thread"); \ - assert(Thread::current()->is_VM_thread(), "current thread is not VM thread"); \ - } while (0) - -#ifdef ASSERT -#define assert_used_and_recalculate_used_equal(g1h) \ - do { \ - size_t cur_used_bytes = g1h->used(); \ - size_t recal_used_bytes = g1h->recalculate_used(); \ - assert(cur_used_bytes == recal_used_bytes, "Used(" SIZE_FORMAT ") is not" \ - " same as recalculated used(" SIZE_FORMAT ").", \ - cur_used_bytes, recal_used_bytes); \ - } while (0) -#else -#define assert_used_and_recalculate_used_equal(g1h) do {} while(0) -#endif + void assert_heap_locked() const NOT_DEBUG_RETURN; + void assert_heap_locked_or_at_safepoint(bool should_be_vm_thread) const NOT_DEBUG_RETURN; + void assert_heap_locked_and_not_at_safepoint() const NOT_DEBUG_RETURN; + void assert_heap_not_locked() const NOT_DEBUG_RETURN; + void assert_heap_not_locked_and_not_at_safepoint() const NOT_DEBUG_RETURN; + void assert_at_safepoint_on_vm_thread() const NOT_DEBUG_RETURN; + void assert_used_and_recalculate_used_equal() const NOT_DEBUG_RETURN; // The young region list. G1EdenRegions _eden; diff --git a/src/hotspot/share/gc/g1/g1CollectedHeap.inline.hpp b/src/hotspot/share/gc/g1/g1CollectedHeap.inline.hpp index 7b81a93abe177..051536966865e 100644 --- a/src/hotspot/share/gc/g1/g1CollectedHeap.inline.hpp +++ b/src/hotspot/share/gc/g1/g1CollectedHeap.inline.hpp @@ -313,4 +313,60 @@ inline bool G1CollectedHeap::is_collection_set_candidate(const HeapRegion* r) co return candidates->contains(r); } +#define heap_locking_asserts_params(_extra_message_) \ + "%s : Heap_lock locked: %s, at safepoint: %s, is VM thread: %s", \ + (_extra_message_), \ + BOOL_TO_STR(Heap_lock->owned_by_self()), \ + BOOL_TO_STR(SafepointSynchronize::is_at_safepoint()), \ + BOOL_TO_STR(Thread::current()->is_VM_thread()) + +inline void G1CollectedHeap::assert_heap_locked() const { + assert(Heap_lock->owned_by_self(), + heap_locking_asserts_params("should be holding the Heap_lock")); +} + +inline void G1CollectedHeap:: +assert_heap_locked_or_at_safepoint(bool should_be_vm_thread) const { + assert(Heap_lock->owned_by_self() || + (SafepointSynchronize::is_at_safepoint() && + ((should_be_vm_thread) == Thread::current()->is_VM_thread())), + heap_locking_asserts_params("should be holding the Heap_lock or " + "should be at a safepoint")); +} + +inline void G1CollectedHeap::assert_heap_locked_and_not_at_safepoint() const { + assert(Heap_lock->owned_by_self() && + !SafepointSynchronize::is_at_safepoint(), + heap_locking_asserts_params("should be holding the Heap_lock and " + "should not be at a safepoint")); +} + +inline void G1CollectedHeap::assert_heap_not_locked() const { + assert(!Heap_lock->owned_by_self(), + heap_locking_asserts_params("should not be holding the Heap_lock")); +} + +inline void G1CollectedHeap::assert_heap_not_locked_and_not_at_safepoint() const { + assert(!Heap_lock->owned_by_self() && + !SafepointSynchronize::is_at_safepoint(), + heap_locking_asserts_params("should not be holding the Heap_lock and " + "should not be at a safepoint")); +} + +inline void G1CollectedHeap::assert_at_safepoint_on_vm_thread() const { + assert_at_safepoint(); + assert(Thread::current_or_null() != nullptr, "no current thread"); + assert(Thread::current()->is_VM_thread(), "current thread is not VM thread"); +} + +inline void G1CollectedHeap::assert_used_and_recalculate_used_equal() const { +#if ASSERT + size_t cur_used_bytes = used(); + size_t recal_used_bytes = recalculate_used(); + assert(cur_used_bytes == recal_used_bytes, "Used(" SIZE_FORMAT ") is not" + " same as recalculated used(" SIZE_FORMAT ").", + cur_used_bytes, recal_used_bytes); +#endif +} + #endif // SHARE_GC_G1_G1COLLECTEDHEAP_INLINE_HPP