Skip to content

Commit

Permalink
refactor heap locking assert functions
Browse files Browse the repository at this point in the history
  • Loading branch information
LizBing committed Dec 23, 2023
1 parent 84c2379 commit 02e74bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
72 changes: 8 additions & 64 deletions src/hotspot/share/gc/g1/g1CollectedHeap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 56 additions & 0 deletions src/hotspot/share/gc/g1/g1CollectedHeap.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 02e74bf

Please sign in to comment.