Skip to content

Commit

Permalink
8272147: Consolidate preserved marks handling with other STW collectors
Browse files Browse the repository at this point in the history
Reviewed-by: tschatzl, ayang, rkennke
  • Loading branch information
lgxbslgx committed Jun 12, 2023
1 parent 6cd166a commit 0727507
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 61 deletions.
5 changes: 4 additions & 1 deletion src/hotspot/share/gc/serial/genMarkSweep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "gc/shared/genCollectedHeap.hpp"
#include "gc/shared/generation.hpp"
#include "gc/shared/modRefBarrierSet.hpp"
#include "gc/shared/preservedMarks.inline.hpp"
#include "gc/shared/referencePolicy.hpp"
#include "gc/shared/referenceProcessorPhaseTimes.hpp"
#include "gc/shared/space.hpp"
Expand Down Expand Up @@ -140,14 +141,16 @@ void GenMarkSweep::allocate_stacks() {

_preserved_marks = (PreservedMark*)scratch;
_preserved_count = 0;

_preserved_overflow_stack_set.init(1);
}


void GenMarkSweep::deallocate_stacks() {
GenCollectedHeap* gch = GenCollectedHeap::heap();
gch->release_scratch();

_preserved_overflow_stack.clear(true);
_preserved_overflow_stack_set.reclaim();
_marking_stack.clear();
_objarray_stack.clear(true);
}
Expand Down
29 changes: 7 additions & 22 deletions src/hotspot/share/gc/serial/markSweep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ uint MarkSweep::_total_invocations = 0;
Stack<oop, mtGC> MarkSweep::_marking_stack;
Stack<ObjArrayTask, mtGC> MarkSweep::_objarray_stack;

Stack<PreservedMark, mtGC> MarkSweep::_preserved_overflow_stack;
PreservedMarksSet MarkSweep::_preserved_overflow_stack_set(false /* in_c_heap */);
size_t MarkSweep::_preserved_count = 0;
size_t MarkSweep::_preserved_count_max = 0;
PreservedMark* MarkSweep::_preserved_marks = nullptr;
Expand Down Expand Up @@ -142,14 +142,6 @@ template <class T> void MarkSweep::follow_root(T* p) {
void MarkSweep::FollowRootClosure::do_oop(oop* p) { follow_root(p); }
void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); }

void PreservedMark::adjust_pointer() {
MarkSweep::adjust_pointer(&_obj);
}

void PreservedMark::restore() {
_obj->set_mark(_mark);
}

// We preserve the mark which should be replaced at the end and the location
// that it will go. Note that the object that this markWord belongs to isn't
// currently at that address but it will be after phase4
Expand All @@ -161,7 +153,7 @@ void MarkSweep::preserve_mark(oop obj, markWord mark) {
if (_preserved_count < _preserved_count_max) {
_preserved_marks[_preserved_count++] = PreservedMark(obj, mark);
} else {
_preserved_overflow_stack.push(PreservedMark(obj, mark));
_preserved_overflow_stack_set.get()->push_always(obj, mark);
}
}

Expand Down Expand Up @@ -205,30 +197,23 @@ AdjustPointerClosure MarkSweep::adjust_pointer_closure;
void MarkSweep::adjust_marks() {
// adjust the oops we saved earlier
for (size_t i = 0; i < _preserved_count; i++) {
_preserved_marks[i].adjust_pointer();
PreservedMarks::adjust_preserved_mark(_preserved_marks + i);
}

// deal with the overflow stack
StackIterator<PreservedMark, mtGC> iter(_preserved_overflow_stack);
while (!iter.is_empty()) {
PreservedMark* p = iter.next_addr();
p->adjust_pointer();
}
_preserved_overflow_stack_set.get()->adjust_during_full_gc();
}

void MarkSweep::restore_marks() {
log_trace(gc)("Restoring " SIZE_FORMAT " marks", _preserved_count + _preserved_overflow_stack.size());
log_trace(gc)("Restoring " SIZE_FORMAT " marks", _preserved_count + _preserved_overflow_stack_set.get()->size());

// restore the marks we saved earlier
for (size_t i = 0; i < _preserved_count; i++) {
_preserved_marks[i].restore();
_preserved_marks[i].set_mark();
}

// deal with the overflow
while (!_preserved_overflow_stack.is_empty()) {
PreservedMark p = _preserved_overflow_stack.pop();
p.restore();
}
_preserved_overflow_stack_set.restore(nullptr);
}

MarkSweep::IsAliveClosure MarkSweep::is_alive;
Expand Down
14 changes: 2 additions & 12 deletions src/hotspot/share/gc/serial/markSweep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define SHARE_GC_SERIAL_MARKSWEEP_HPP

#include "gc/shared/collectedHeap.hpp"
#include "gc/shared/preservedMarks.inline.hpp"
#include "gc/shared/referenceProcessor.hpp"
#include "gc/shared/stringdedup/stringDedup.hpp"
#include "gc/shared/taskqueue.hpp"
Expand Down Expand Up @@ -99,7 +100,7 @@ class MarkSweep : AllStatic {
static Stack<ObjArrayTask, mtGC> _objarray_stack;

// Space for storing/restoring mark word
static Stack<PreservedMark, mtGC> _preserved_overflow_stack;
static PreservedMarksSet _preserved_overflow_stack_set;
static size_t _preserved_count;
static size_t _preserved_count_max;
static PreservedMark* _preserved_marks;
Expand Down Expand Up @@ -186,15 +187,4 @@ class AdjustPointerClosure: public BasicOopIterateClosure {
virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
};

class PreservedMark {
private:
oop _obj;
markWord _mark;

public:
PreservedMark(oop obj, markWord mark) : _obj(obj), _mark(mark) {}
void adjust_pointer();
void restore();
};

#endif // SHARE_GC_SERIAL_MARKSWEEP_HPP
19 changes: 11 additions & 8 deletions src/hotspot/share/gc/shared/preservedMarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,24 @@

void PreservedMarks::restore() {
while (!_stack.is_empty()) {
const OopAndMarkWord elem = _stack.pop();
const PreservedMark elem = _stack.pop();
elem.set_mark();
}
assert_empty();
}

void PreservedMarks::adjust_preserved_mark(PreservedMark* elem) {
oop obj = elem->get_oop();
if (obj->is_forwarded()) {
elem->set_oop(obj->forwardee());
}
}

void PreservedMarks::adjust_during_full_gc() {
StackIterator<OopAndMarkWord, mtGC> iter(_stack);
StackIterator<PreservedMark, mtGC> iter(_stack);
while (!iter.is_empty()) {
OopAndMarkWord* elem = iter.next_addr();

oop obj = elem->get_oop();
if (obj->is_forwarded()) {
elem->set_oop(obj->forwardee());
}
PreservedMark* elem = iter.next_addr();
adjust_preserved_mark(elem);
}
}

Expand Down
34 changes: 20 additions & 14 deletions src/hotspot/share/gc/shared/preservedMarks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@ class WorkerTask;
class PreservedMarksSet;
class WorkerThreads;

class PreservedMarks {
private:
class OopAndMarkWord {
private:
oop _o;
markWord _m;
class PreservedMark {
private:
oop _o;
markWord _m;

public:
OopAndMarkWord(oop obj, markWord m) : _o(obj), _m(m) { }
public:
PreservedMark(oop obj, markWord m) : _o(obj), _m(m) { }

oop get_oop() { return _o; }
inline void set_mark() const;
void set_oop(oop obj) { _o = obj; }
};

oop get_oop() { return _o; }
inline void set_mark() const;
void set_oop(oop obj) { _o = obj; }
};
typedef Stack<OopAndMarkWord, mtGC> OopAndMarkWordStack;
class PreservedMarks {
private:
typedef Stack<PreservedMark, mtGC> PreservedMarkStack;

OopAndMarkWordStack _stack;
PreservedMarkStack _stack;

inline bool should_preserve_mark(oop obj, markWord m) const;

Expand All @@ -61,6 +62,11 @@ class PreservedMarks {
// Iterate over the stack, restore all preserved marks, and
// reclaim the memory taken up by the stack segments.
void restore();

// Adjust the preserved mark according to its
// forwarding location stored in the mark.
static void adjust_preserved_mark(PreservedMark* elem);

// Iterate over the stack, adjust all preserved marks according
// to their forwarding location stored in the mark.
void adjust_during_full_gc();
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/share/gc/shared/preservedMarks.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ inline bool PreservedMarks::should_preserve_mark(oop obj, markWord m) const {

inline void PreservedMarks::push_if_necessary(oop obj, markWord m) {
if (should_preserve_mark(obj, m)) {
OopAndMarkWord elem(obj, m);
PreservedMark elem(obj, m);
_stack.push(elem);
}
}

inline void PreservedMarks::push_always(oop obj, markWord m) {
assert(!m.is_marked(), "precondition");
OopAndMarkWord elem(obj, m);
PreservedMark elem(obj, m);
_stack.push(elem);
}

inline PreservedMarks::PreservedMarks()
: _stack(OopAndMarkWordStack::default_segment_size(),
: _stack(PreservedMarkStack::default_segment_size(),
// This stack should be used very infrequently so there's
// no point in caching stack segments (there will be a
// waste of space most of the time). So we set the max
// cache size to 0.
0 /* max_cache_size */) { }

void PreservedMarks::OopAndMarkWord::set_mark() const {
void PreservedMark::set_mark() const {
_o->set_mark(_m);
}

Expand Down

1 comment on commit 0727507

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.