提交 aeff5245 编写于 作者: T tonyp

7119027: G1: use atomics to update RS length / predict time of inc CSet

Summary: Make sure that the updates to the RS length and inc CSet predicted time are updated in an MT-safe way.
Reviewed-by: brutisso, iveresov
上级 0ed8fd00
...@@ -230,7 +230,9 @@ G1CollectorPolicy::G1CollectorPolicy() : ...@@ -230,7 +230,9 @@ G1CollectorPolicy::G1CollectorPolicy() :
_inc_cset_bytes_used_before(0), _inc_cset_bytes_used_before(0),
_inc_cset_max_finger(NULL), _inc_cset_max_finger(NULL),
_inc_cset_recorded_rs_lengths(0), _inc_cset_recorded_rs_lengths(0),
_inc_cset_recorded_rs_lengths_diffs(0),
_inc_cset_predicted_elapsed_time_ms(0.0), _inc_cset_predicted_elapsed_time_ms(0.0),
_inc_cset_predicted_elapsed_time_ms_diffs(0.0),
#ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
#pragma warning( disable:4355 ) // 'this' : used in base member initializer list #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
...@@ -1551,10 +1553,19 @@ void G1CollectorPolicy::record_collection_pause_end(int no_of_gc_threads) { ...@@ -1551,10 +1553,19 @@ void G1CollectorPolicy::record_collection_pause_end(int no_of_gc_threads) {
} }
} }
// It turns out that, sometimes, _max_rs_lengths can get smaller // This is defensive. For a while _max_rs_lengths could get
// than _recorded_rs_lengths which causes rs_length_diff to get // smaller than _recorded_rs_lengths which was causing
// very large and mess up the RSet length predictions. We'll be // rs_length_diff to get very large and mess up the RSet length
// defensive until we work out why this happens. // predictions. The reason was unsafe concurrent updates to the
// _inc_cset_recorded_rs_lengths field which the code below guards
// against (see CR 7118202). This bug has now been fixed (see CR
// 7119027). However, I'm still worried that
// _inc_cset_recorded_rs_lengths might still end up somewhat
// inaccurate. The concurrent refinement thread calculates an
// RSet's length concurrently with other CR threads updating it
// which might cause it to calculate the length incorrectly (if,
// say, it's in mid-coarsening). So I'll leave in the defensive
// conditional below just in case.
size_t rs_length_diff = 0; size_t rs_length_diff = 0;
if (_max_rs_lengths > _recorded_rs_lengths) { if (_max_rs_lengths > _recorded_rs_lengths) {
rs_length_diff = _max_rs_lengths - _recorded_rs_lengths; rs_length_diff = _max_rs_lengths - _recorded_rs_lengths;
...@@ -2436,10 +2447,45 @@ void G1CollectorPolicy::start_incremental_cset_building() { ...@@ -2436,10 +2447,45 @@ void G1CollectorPolicy::start_incremental_cset_building() {
_inc_cset_max_finger = 0; _inc_cset_max_finger = 0;
_inc_cset_recorded_rs_lengths = 0; _inc_cset_recorded_rs_lengths = 0;
_inc_cset_predicted_elapsed_time_ms = 0; _inc_cset_recorded_rs_lengths_diffs = 0;
_inc_cset_predicted_elapsed_time_ms = 0.0;
_inc_cset_predicted_elapsed_time_ms_diffs = 0.0;
_inc_cset_build_state = Active; _inc_cset_build_state = Active;
} }
void G1CollectorPolicy::finalize_incremental_cset_building() {
assert(_inc_cset_build_state == Active, "Precondition");
assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint");
// The two "main" fields, _inc_cset_recorded_rs_lengths and
// _inc_cset_predicted_elapsed_time_ms, are updated by the thread
// that adds a new region to the CSet. Further updates by the
// concurrent refinement thread that samples the young RSet lengths
// are accumulated in the *_diffs fields. Here we add the diffs to
// the "main" fields.
if (_inc_cset_recorded_rs_lengths_diffs >= 0) {
_inc_cset_recorded_rs_lengths += _inc_cset_recorded_rs_lengths_diffs;
} else {
// This is defensive. The diff should in theory be always positive
// as RSets can only grow between GCs. However, given that we
// sample their size concurrently with other threads updating them
// it's possible that we might get the wrong size back, which
// could make the calculations somewhat inaccurate.
size_t diffs = (size_t) (-_inc_cset_recorded_rs_lengths_diffs);
if (_inc_cset_recorded_rs_lengths >= diffs) {
_inc_cset_recorded_rs_lengths -= diffs;
} else {
_inc_cset_recorded_rs_lengths = 0;
}
}
_inc_cset_predicted_elapsed_time_ms +=
_inc_cset_predicted_elapsed_time_ms_diffs;
_inc_cset_recorded_rs_lengths_diffs = 0;
_inc_cset_predicted_elapsed_time_ms_diffs = 0.0;
}
void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length) { void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length) {
// This routine is used when: // This routine is used when:
// * adding survivor regions to the incremental cset at the end of an // * adding survivor regions to the incremental cset at the end of an
...@@ -2455,10 +2501,8 @@ void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_l ...@@ -2455,10 +2501,8 @@ void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_l
double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, true); double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, true);
size_t used_bytes = hr->used(); size_t used_bytes = hr->used();
_inc_cset_recorded_rs_lengths += rs_length; _inc_cset_recorded_rs_lengths += rs_length;
_inc_cset_predicted_elapsed_time_ms += region_elapsed_time_ms; _inc_cset_predicted_elapsed_time_ms += region_elapsed_time_ms;
_inc_cset_bytes_used_before += used_bytes; _inc_cset_bytes_used_before += used_bytes;
// Cache the values we have added to the aggregated informtion // Cache the values we have added to the aggregated informtion
...@@ -2469,37 +2513,33 @@ void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_l ...@@ -2469,37 +2513,33 @@ void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_l
hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms); hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms);
} }
void G1CollectorPolicy::remove_from_incremental_cset_info(HeapRegion* hr) { void G1CollectorPolicy::update_incremental_cset_info(HeapRegion* hr,
// This routine is currently only called as part of the updating of size_t new_rs_length) {
// existing policy information for regions in the incremental cset that // Update the CSet information that is dependent on the new RS length
// is performed by the concurrent refine thread(s) as part of young list assert(hr->is_young(), "Precondition");
// RSet sampling. Therefore we should not be at a safepoint. assert(!SafepointSynchronize::is_at_safepoint(),
"should not be at a safepoint");
assert(!SafepointSynchronize::is_at_safepoint(), "should not be at safepoint");
assert(hr->is_young(), "it should be"); // We could have updated _inc_cset_recorded_rs_lengths and
// _inc_cset_predicted_elapsed_time_ms directly but we'd need to do
// that atomically, as this code is executed by a concurrent
// refinement thread, potentially concurrently with a mutator thread
// allocating a new region and also updating the same fields. To
// avoid the atomic operations we accumulate these updates on two
// separate fields (*_diffs) and we'll just add them to the "main"
// fields at the start of a GC.
ssize_t old_rs_length = (ssize_t) hr->recorded_rs_length();
ssize_t rs_lengths_diff = (ssize_t) new_rs_length - old_rs_length;
_inc_cset_recorded_rs_lengths_diffs += rs_lengths_diff;
size_t used_bytes = hr->used();
size_t old_rs_length = hr->recorded_rs_length();
double old_elapsed_time_ms = hr->predicted_elapsed_time_ms(); double old_elapsed_time_ms = hr->predicted_elapsed_time_ms();
double new_region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, true);
double elapsed_ms_diff = new_region_elapsed_time_ms - old_elapsed_time_ms;
_inc_cset_predicted_elapsed_time_ms_diffs += elapsed_ms_diff;
// Subtract the old recorded/predicted policy information for hr->set_recorded_rs_length(new_rs_length);
// the given heap region from the collection set info. hr->set_predicted_elapsed_time_ms(new_region_elapsed_time_ms);
_inc_cset_recorded_rs_lengths -= old_rs_length;
_inc_cset_predicted_elapsed_time_ms -= old_elapsed_time_ms;
_inc_cset_bytes_used_before -= used_bytes;
// Clear the values cached in the heap region
hr->set_recorded_rs_length(0);
hr->set_predicted_elapsed_time_ms(0);
}
void G1CollectorPolicy::update_incremental_cset_info(HeapRegion* hr, size_t new_rs_length) {
// Update the collection set information that is dependent on the new RS length
assert(hr->is_young(), "Precondition");
remove_from_incremental_cset_info(hr);
add_to_incremental_cset_info(hr, new_rs_length);
} }
void G1CollectorPolicy::add_region_to_incremental_cset_common(HeapRegion* hr) { void G1CollectorPolicy::add_region_to_incremental_cset_common(HeapRegion* hr) {
...@@ -2591,6 +2631,7 @@ void G1CollectorPolicy::choose_collection_set(double target_pause_time_ms) { ...@@ -2591,6 +2631,7 @@ void G1CollectorPolicy::choose_collection_set(double target_pause_time_ms) {
double non_young_start_time_sec = os::elapsedTime(); double non_young_start_time_sec = os::elapsedTime();
YoungList* young_list = _g1->young_list(); YoungList* young_list = _g1->young_list();
finalize_incremental_cset_building();
guarantee(target_pause_time_ms > 0.0, guarantee(target_pause_time_ms > 0.0,
err_msg("target_pause_time_ms = %1.6lf should be positive", err_msg("target_pause_time_ms = %1.6lf should be positive",
......
...@@ -588,16 +588,29 @@ private: ...@@ -588,16 +588,29 @@ private:
// Used to record the highest end of heap region in collection set // Used to record the highest end of heap region in collection set
HeapWord* _inc_cset_max_finger; HeapWord* _inc_cset_max_finger;
// The RSet lengths recorded for regions in the collection set // The RSet lengths recorded for regions in the CSet. It is updated
// (updated by the periodic sampling of the regions in the // by the thread that adds a new region to the CSet. We assume that
// young list/collection set). // only one thread can be allocating a new CSet region (currently,
// it does so after taking the Heap_lock) hence no need to
// synchronize updates to this field.
size_t _inc_cset_recorded_rs_lengths; size_t _inc_cset_recorded_rs_lengths;
// The predicted elapsed time it will take to collect the regions // A concurrent refinement thread periodcially samples the young
// in the collection set (updated by the periodic sampling of the // region RSets and needs to update _inc_cset_recorded_rs_lengths as
// regions in the young list/collection set). // the RSets grow. Instead of having to syncronize updates to that
// field we accumulate them in this field and add it to
// _inc_cset_recorded_rs_lengths_diffs at the start of a GC.
ssize_t _inc_cset_recorded_rs_lengths_diffs;
// The predicted elapsed time it will take to collect the regions in
// the CSet. This is updated by the thread that adds a new region to
// the CSet. See the comment for _inc_cset_recorded_rs_lengths about
// MT-safety assumptions.
double _inc_cset_predicted_elapsed_time_ms; double _inc_cset_predicted_elapsed_time_ms;
// See the comment for _inc_cset_recorded_rs_lengths_diffs.
double _inc_cset_predicted_elapsed_time_ms_diffs;
// Stash a pointer to the g1 heap. // Stash a pointer to the g1 heap.
G1CollectedHeap* _g1; G1CollectedHeap* _g1;
...@@ -894,6 +907,10 @@ public: ...@@ -894,6 +907,10 @@ public:
// Initialize incremental collection set info. // Initialize incremental collection set info.
void start_incremental_cset_building(); void start_incremental_cset_building();
// Perform any final calculations on the incremental CSet fields
// before we can use them.
void finalize_incremental_cset_building();
void clear_incremental_cset() { void clear_incremental_cset() {
_inc_cset_head = NULL; _inc_cset_head = NULL;
_inc_cset_tail = NULL; _inc_cset_tail = NULL;
...@@ -902,10 +919,9 @@ public: ...@@ -902,10 +919,9 @@ public:
// Stop adding regions to the incremental collection set // Stop adding regions to the incremental collection set
void stop_incremental_cset_building() { _inc_cset_build_state = Inactive; } void stop_incremental_cset_building() { _inc_cset_build_state = Inactive; }
// Add/remove information about hr to the aggregated information // Add information about hr to the aggregated information for the
// for the incrementally built collection set. // incrementally built collection set.
void add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length); void add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length);
void remove_from_incremental_cset_info(HeapRegion* hr);
// Update information about hr in the aggregated information for // Update information about hr in the aggregated information for
// the incrementally built collection set. // the incrementally built collection set.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册