18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/g1/g1CollectedHeap.hpp"
27 #include "gc/g1/g1CollectionSet.hpp"
28 #include "gc/g1/g1CollectorState.hpp"
29 #include "gc/g1/g1Policy.hpp"
30 #include "gc/g1/heapRegion.inline.hpp"
31 #include "gc/g1/heapRegionRemSet.hpp"
32 #include "gc/g1/heapRegionSet.hpp"
33 #include "logging/logStream.hpp"
34 #include "utilities/debug.hpp"
35 #include "utilities/quickSort.hpp"
36
37 G1CollectorState* G1CollectionSet::collector_state() {
38 return _g1->collector_state();
39 }
40
41 G1GCPhaseTimes* G1CollectionSet::phase_times() {
42 return _policy->phase_times();
43 }
44
45 CollectionSetChooser* G1CollectionSet::cset_chooser() {
46 return _cset_chooser;
47 }
48
49 double G1CollectionSet::predict_region_elapsed_time_ms(HeapRegion* hr) {
50 return _policy->predict_region_elapsed_time_ms(hr, collector_state()->in_young_only_phase());
51 }
52
53 G1CollectionSet::G1CollectionSet(G1CollectedHeap* g1h, G1Policy* policy) :
54 _g1(g1h),
55 _policy(policy),
56 _cset_chooser(new CollectionSetChooser()),
57 _eden_region_length(0),
58 _survivor_region_length(0),
59 _old_region_length(0),
60 _bytes_used_before(0),
61 _recorded_rs_lengths(0),
62 _collection_set_regions(NULL),
63 _collection_set_cur_length(0),
64 _collection_set_max_length(0),
65 // Incremental CSet attributes
66 _inc_build_state(Inactive),
67 _inc_bytes_used_before(0),
68 _inc_recorded_rs_lengths(0),
69 _inc_recorded_rs_lengths_diffs(0),
70 _inc_predicted_elapsed_time_ms(0.0),
71 _inc_predicted_elapsed_time_ms_diffs(0.0) {
72 }
73
74 G1CollectionSet::~G1CollectionSet() {
92 }
93
94 void G1CollectionSet::initialize(uint max_region_length) {
95 guarantee(_collection_set_regions == NULL, "Must only initialize once.");
96 _collection_set_max_length = max_region_length;
97 _collection_set_regions = NEW_C_HEAP_ARRAY(uint, max_region_length, mtGC);
98 }
99
100 void G1CollectionSet::set_recorded_rs_lengths(size_t rs_lengths) {
101 _recorded_rs_lengths = rs_lengths;
102 }
103
104 // Add the heap region at the head of the non-incremental collection set
105 void G1CollectionSet::add_old_region(HeapRegion* hr) {
106 assert_at_safepoint_on_vm_thread();
107
108 assert(_inc_build_state == Active, "Precondition");
109 assert(hr->is_old(), "the region should be old");
110
111 assert(!hr->in_collection_set(), "should not already be in the CSet");
112 _g1->register_old_region_with_cset(hr);
113
114 _collection_set_regions[_collection_set_cur_length++] = hr->hrm_index();
115 assert(_collection_set_cur_length <= _collection_set_max_length, "Collection set now larger than maximum size.");
116
117 _bytes_used_before += hr->used();
118 size_t rs_length = hr->rem_set()->occupied();
119 _recorded_rs_lengths += rs_length;
120 _old_region_length += 1;
121 }
122
123 // Initialize the per-collection-set information
124 void G1CollectionSet::start_incremental_building() {
125 assert(_collection_set_cur_length == 0, "Collection set must be empty before starting a new collection set.");
126 assert(_inc_build_state == Inactive, "Precondition");
127
128 _inc_bytes_used_before = 0;
129
130 _inc_recorded_rs_lengths = 0;
131 _inc_recorded_rs_lengths_diffs = 0;
132 _inc_predicted_elapsed_time_ms = 0.0;
168
169 void G1CollectionSet::clear() {
170 assert_at_safepoint_on_vm_thread();
171 _collection_set_cur_length = 0;
172 }
173
174 void G1CollectionSet::iterate(HeapRegionClosure* cl) const {
175 iterate_from(cl, 0, 1);
176 }
177
178 void G1CollectionSet::iterate_from(HeapRegionClosure* cl, uint worker_id, uint total_workers) const {
179 size_t len = _collection_set_cur_length;
180 OrderAccess::loadload();
181 if (len == 0) {
182 return;
183 }
184 size_t start_pos = (worker_id * len) / total_workers;
185 size_t cur_pos = start_pos;
186
187 do {
188 HeapRegion* r = G1CollectedHeap::heap()->region_at(_collection_set_regions[cur_pos]);
189 bool result = cl->do_heap_region(r);
190 if (result) {
191 cl->set_incomplete();
192 return;
193 }
194 cur_pos++;
195 if (cur_pos == len) {
196 cur_pos = 0;
197 }
198 } while (cur_pos != start_pos);
199 }
200
201 void G1CollectionSet::update_young_region_prediction(HeapRegion* hr,
202 size_t new_rs_length) {
203 // Update the CSet information that is dependent on the new RS length
204 assert(hr->is_young(), "Precondition");
205 assert(!SafepointSynchronize::is_at_safepoint(), "should not be at a safepoint");
206
207 // We could have updated _inc_recorded_rs_lengths and
208 // _inc_predicted_elapsed_time_ms directly but we'd need to do
240 OrderAccess::storestore();
241 _collection_set_cur_length++;
242 assert(_collection_set_cur_length <= _collection_set_max_length, "Collection set larger than maximum allowed.");
243
244 // This routine is used when:
245 // * adding survivor regions to the incremental cset at the end of an
246 // evacuation pause or
247 // * adding the current allocation region to the incremental cset
248 // when it is retired.
249 // Therefore this routine may be called at a safepoint by the
250 // VM thread, or in-between safepoints by mutator threads (when
251 // retiring the current allocation region)
252 // We need to clear and set the cached recorded/cached collection set
253 // information in the heap region here (before the region gets added
254 // to the collection set). An individual heap region's cached values
255 // are calculated, aggregated with the policy collection set info,
256 // and cached in the heap region here (initially) and (subsequently)
257 // by the Young List sampling code.
258 // Ignore calls to this due to retirement during full gc.
259
260 if (!G1CollectedHeap::heap()->collector_state()->in_full_gc()) {
261 size_t rs_length = hr->rem_set()->occupied();
262 double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr);
263
264 // Cache the values we have added to the aggregated information
265 // in the heap region in case we have to remove this region from
266 // the incremental collection set, or it is updated by the
267 // rset sampling code
268 hr->set_recorded_rs_length(rs_length);
269 hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms);
270
271 _inc_recorded_rs_lengths += rs_length;
272 _inc_predicted_elapsed_time_ms += region_elapsed_time_ms;
273 _inc_bytes_used_before += hr->used();
274 }
275
276 assert(!hr->in_collection_set(), "invariant");
277 _g1->register_young_region_with_cset(hr);
278 }
279
280 void G1CollectionSet::add_survivor_regions(HeapRegion* hr) {
281 assert(hr->is_survivor(), "Must only add survivor regions, but is %s", hr->get_type_str());
282 add_young_region_common(hr);
283 }
284
285 void G1CollectionSet::add_eden_region(HeapRegion* hr) {
286 assert(hr->is_eden(), "Must only add eden regions, but is %s", hr->get_type_str());
287 add_young_region_common(hr);
288 }
289
290 #ifndef PRODUCT
291 class G1VerifyYoungAgesClosure : public HeapRegionClosure {
292 public:
293 bool _valid;
294 public:
295 G1VerifyYoungAgesClosure() : HeapRegionClosure(), _valid(true) { }
296
297 virtual bool do_heap_region(HeapRegion* r) {
356 double G1CollectionSet::finalize_young_part(double target_pause_time_ms, G1SurvivorRegions* survivors) {
357 double young_start_time_sec = os::elapsedTime();
358
359 finalize_incremental_building();
360
361 guarantee(target_pause_time_ms > 0.0,
362 "target_pause_time_ms = %1.6lf should be positive", target_pause_time_ms);
363
364 size_t pending_cards = _policy->pending_cards();
365 double base_time_ms = _policy->predict_base_elapsed_time_ms(pending_cards);
366 double time_remaining_ms = MAX2(target_pause_time_ms - base_time_ms, 0.0);
367
368 log_trace(gc, ergo, cset)("Start choosing CSet. pending cards: " SIZE_FORMAT " predicted base time: %1.2fms remaining time: %1.2fms target pause time: %1.2fms",
369 pending_cards, base_time_ms, time_remaining_ms, target_pause_time_ms);
370
371 // The young list is laid with the survivor regions from the previous
372 // pause are appended to the RHS of the young list, i.e.
373 // [Newly Young Regions ++ Survivors from last pause].
374
375 uint survivor_region_length = survivors->length();
376 uint eden_region_length = _g1->eden_regions_count();
377 init_region_lengths(eden_region_length, survivor_region_length);
378
379 verify_young_cset_indices();
380
381 // Clear the fields that point to the survivor list - they are all young now.
382 survivors->convert_to_eden();
383
384 _bytes_used_before = _inc_bytes_used_before;
385 time_remaining_ms = MAX2(time_remaining_ms - _inc_predicted_elapsed_time_ms, 0.0);
386
387 log_trace(gc, ergo, cset)("Add young regions to CSet. eden: %u regions, survivors: %u regions, predicted young region time: %1.2fms, target pause time: %1.2fms",
388 eden_region_length, survivor_region_length, _inc_predicted_elapsed_time_ms, target_pause_time_ms);
389
390 // The number of recorded young regions is the incremental
391 // collection set's current size
392 set_recorded_rs_lengths(_inc_recorded_rs_lengths);
393
394 double young_end_time_sec = os::elapsedTime();
395 phase_times()->record_young_cset_choice_time_ms((young_end_time_sec - young_start_time_sec) * 1000.0);
396
459
460 // We'll add it anyway given that we haven't reached the
461 // minimum number of old regions.
462 expensive_region_num += 1;
463 }
464 } else {
465 if (old_region_length() >= min_old_cset_length) {
466 // In the non-auto-tuning case, we'll finish adding regions
467 // to the CSet if we reach the minimum.
468
469 log_debug(gc, ergo, cset)("Finish adding old regions to CSet (old CSet region num reached min). old %u regions, min %u regions",
470 old_region_length(), min_old_cset_length);
471 break;
472 }
473 }
474
475 // We will add this region to the CSet.
476 time_remaining_ms = MAX2(time_remaining_ms - predicted_time_ms, 0.0);
477 predicted_old_time_ms += predicted_time_ms;
478 cset_chooser()->pop(); // already have region via peek()
479 _g1->old_set_remove(hr);
480 add_old_region(hr);
481
482 hr = cset_chooser()->peek();
483 }
484 if (hr == NULL) {
485 log_debug(gc, ergo, cset)("Finish adding old regions to CSet (candidate old regions not available)");
486 }
487
488 if (expensive_region_num > 0) {
489 // We print the information once here at the end, predicated on
490 // whether we added any apparently expensive regions or not, to
491 // avoid generating output per region.
492 log_debug(gc, ergo, cset)("Added expensive regions to CSet (old CSet region num not reached min)."
493 "old: %u regions, expensive: %u regions, min: %u regions, remaining time: %1.2fms",
494 old_region_length(), expensive_region_num, min_old_cset_length, time_remaining_ms);
495 }
496
497 cset_chooser()->verify();
498 }
499
|
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/g1/g1CollectedHeap.hpp"
27 #include "gc/g1/g1CollectionSet.hpp"
28 #include "gc/g1/g1CollectorState.hpp"
29 #include "gc/g1/g1Policy.hpp"
30 #include "gc/g1/heapRegion.inline.hpp"
31 #include "gc/g1/heapRegionRemSet.hpp"
32 #include "gc/g1/heapRegionSet.hpp"
33 #include "logging/logStream.hpp"
34 #include "utilities/debug.hpp"
35 #include "utilities/quickSort.hpp"
36
37 G1CollectorState* G1CollectionSet::collector_state() {
38 return _g1h->collector_state();
39 }
40
41 G1GCPhaseTimes* G1CollectionSet::phase_times() {
42 return _policy->phase_times();
43 }
44
45 CollectionSetChooser* G1CollectionSet::cset_chooser() {
46 return _cset_chooser;
47 }
48
49 double G1CollectionSet::predict_region_elapsed_time_ms(HeapRegion* hr) {
50 return _policy->predict_region_elapsed_time_ms(hr, collector_state()->in_young_only_phase());
51 }
52
53 G1CollectionSet::G1CollectionSet(G1CollectedHeap* g1h, G1Policy* policy) :
54 _g1h(g1h),
55 _policy(policy),
56 _cset_chooser(new CollectionSetChooser()),
57 _eden_region_length(0),
58 _survivor_region_length(0),
59 _old_region_length(0),
60 _bytes_used_before(0),
61 _recorded_rs_lengths(0),
62 _collection_set_regions(NULL),
63 _collection_set_cur_length(0),
64 _collection_set_max_length(0),
65 // Incremental CSet attributes
66 _inc_build_state(Inactive),
67 _inc_bytes_used_before(0),
68 _inc_recorded_rs_lengths(0),
69 _inc_recorded_rs_lengths_diffs(0),
70 _inc_predicted_elapsed_time_ms(0.0),
71 _inc_predicted_elapsed_time_ms_diffs(0.0) {
72 }
73
74 G1CollectionSet::~G1CollectionSet() {
92 }
93
94 void G1CollectionSet::initialize(uint max_region_length) {
95 guarantee(_collection_set_regions == NULL, "Must only initialize once.");
96 _collection_set_max_length = max_region_length;
97 _collection_set_regions = NEW_C_HEAP_ARRAY(uint, max_region_length, mtGC);
98 }
99
100 void G1CollectionSet::set_recorded_rs_lengths(size_t rs_lengths) {
101 _recorded_rs_lengths = rs_lengths;
102 }
103
104 // Add the heap region at the head of the non-incremental collection set
105 void G1CollectionSet::add_old_region(HeapRegion* hr) {
106 assert_at_safepoint_on_vm_thread();
107
108 assert(_inc_build_state == Active, "Precondition");
109 assert(hr->is_old(), "the region should be old");
110
111 assert(!hr->in_collection_set(), "should not already be in the CSet");
112 _g1h->register_old_region_with_cset(hr);
113
114 _collection_set_regions[_collection_set_cur_length++] = hr->hrm_index();
115 assert(_collection_set_cur_length <= _collection_set_max_length, "Collection set now larger than maximum size.");
116
117 _bytes_used_before += hr->used();
118 size_t rs_length = hr->rem_set()->occupied();
119 _recorded_rs_lengths += rs_length;
120 _old_region_length += 1;
121 }
122
123 // Initialize the per-collection-set information
124 void G1CollectionSet::start_incremental_building() {
125 assert(_collection_set_cur_length == 0, "Collection set must be empty before starting a new collection set.");
126 assert(_inc_build_state == Inactive, "Precondition");
127
128 _inc_bytes_used_before = 0;
129
130 _inc_recorded_rs_lengths = 0;
131 _inc_recorded_rs_lengths_diffs = 0;
132 _inc_predicted_elapsed_time_ms = 0.0;
168
169 void G1CollectionSet::clear() {
170 assert_at_safepoint_on_vm_thread();
171 _collection_set_cur_length = 0;
172 }
173
174 void G1CollectionSet::iterate(HeapRegionClosure* cl) const {
175 iterate_from(cl, 0, 1);
176 }
177
178 void G1CollectionSet::iterate_from(HeapRegionClosure* cl, uint worker_id, uint total_workers) const {
179 size_t len = _collection_set_cur_length;
180 OrderAccess::loadload();
181 if (len == 0) {
182 return;
183 }
184 size_t start_pos = (worker_id * len) / total_workers;
185 size_t cur_pos = start_pos;
186
187 do {
188 HeapRegion* r = _g1h->region_at(_collection_set_regions[cur_pos]);
189 bool result = cl->do_heap_region(r);
190 if (result) {
191 cl->set_incomplete();
192 return;
193 }
194 cur_pos++;
195 if (cur_pos == len) {
196 cur_pos = 0;
197 }
198 } while (cur_pos != start_pos);
199 }
200
201 void G1CollectionSet::update_young_region_prediction(HeapRegion* hr,
202 size_t new_rs_length) {
203 // Update the CSet information that is dependent on the new RS length
204 assert(hr->is_young(), "Precondition");
205 assert(!SafepointSynchronize::is_at_safepoint(), "should not be at a safepoint");
206
207 // We could have updated _inc_recorded_rs_lengths and
208 // _inc_predicted_elapsed_time_ms directly but we'd need to do
240 OrderAccess::storestore();
241 _collection_set_cur_length++;
242 assert(_collection_set_cur_length <= _collection_set_max_length, "Collection set larger than maximum allowed.");
243
244 // This routine is used when:
245 // * adding survivor regions to the incremental cset at the end of an
246 // evacuation pause or
247 // * adding the current allocation region to the incremental cset
248 // when it is retired.
249 // Therefore this routine may be called at a safepoint by the
250 // VM thread, or in-between safepoints by mutator threads (when
251 // retiring the current allocation region)
252 // We need to clear and set the cached recorded/cached collection set
253 // information in the heap region here (before the region gets added
254 // to the collection set). An individual heap region's cached values
255 // are calculated, aggregated with the policy collection set info,
256 // and cached in the heap region here (initially) and (subsequently)
257 // by the Young List sampling code.
258 // Ignore calls to this due to retirement during full gc.
259
260 if (!_g1h->collector_state()->in_full_gc()) {
261 size_t rs_length = hr->rem_set()->occupied();
262 double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr);
263
264 // Cache the values we have added to the aggregated information
265 // in the heap region in case we have to remove this region from
266 // the incremental collection set, or it is updated by the
267 // rset sampling code
268 hr->set_recorded_rs_length(rs_length);
269 hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms);
270
271 _inc_recorded_rs_lengths += rs_length;
272 _inc_predicted_elapsed_time_ms += region_elapsed_time_ms;
273 _inc_bytes_used_before += hr->used();
274 }
275
276 assert(!hr->in_collection_set(), "invariant");
277 _g1h->register_young_region_with_cset(hr);
278 }
279
280 void G1CollectionSet::add_survivor_regions(HeapRegion* hr) {
281 assert(hr->is_survivor(), "Must only add survivor regions, but is %s", hr->get_type_str());
282 add_young_region_common(hr);
283 }
284
285 void G1CollectionSet::add_eden_region(HeapRegion* hr) {
286 assert(hr->is_eden(), "Must only add eden regions, but is %s", hr->get_type_str());
287 add_young_region_common(hr);
288 }
289
290 #ifndef PRODUCT
291 class G1VerifyYoungAgesClosure : public HeapRegionClosure {
292 public:
293 bool _valid;
294 public:
295 G1VerifyYoungAgesClosure() : HeapRegionClosure(), _valid(true) { }
296
297 virtual bool do_heap_region(HeapRegion* r) {
356 double G1CollectionSet::finalize_young_part(double target_pause_time_ms, G1SurvivorRegions* survivors) {
357 double young_start_time_sec = os::elapsedTime();
358
359 finalize_incremental_building();
360
361 guarantee(target_pause_time_ms > 0.0,
362 "target_pause_time_ms = %1.6lf should be positive", target_pause_time_ms);
363
364 size_t pending_cards = _policy->pending_cards();
365 double base_time_ms = _policy->predict_base_elapsed_time_ms(pending_cards);
366 double time_remaining_ms = MAX2(target_pause_time_ms - base_time_ms, 0.0);
367
368 log_trace(gc, ergo, cset)("Start choosing CSet. pending cards: " SIZE_FORMAT " predicted base time: %1.2fms remaining time: %1.2fms target pause time: %1.2fms",
369 pending_cards, base_time_ms, time_remaining_ms, target_pause_time_ms);
370
371 // The young list is laid with the survivor regions from the previous
372 // pause are appended to the RHS of the young list, i.e.
373 // [Newly Young Regions ++ Survivors from last pause].
374
375 uint survivor_region_length = survivors->length();
376 uint eden_region_length = _g1h->eden_regions_count();
377 init_region_lengths(eden_region_length, survivor_region_length);
378
379 verify_young_cset_indices();
380
381 // Clear the fields that point to the survivor list - they are all young now.
382 survivors->convert_to_eden();
383
384 _bytes_used_before = _inc_bytes_used_before;
385 time_remaining_ms = MAX2(time_remaining_ms - _inc_predicted_elapsed_time_ms, 0.0);
386
387 log_trace(gc, ergo, cset)("Add young regions to CSet. eden: %u regions, survivors: %u regions, predicted young region time: %1.2fms, target pause time: %1.2fms",
388 eden_region_length, survivor_region_length, _inc_predicted_elapsed_time_ms, target_pause_time_ms);
389
390 // The number of recorded young regions is the incremental
391 // collection set's current size
392 set_recorded_rs_lengths(_inc_recorded_rs_lengths);
393
394 double young_end_time_sec = os::elapsedTime();
395 phase_times()->record_young_cset_choice_time_ms((young_end_time_sec - young_start_time_sec) * 1000.0);
396
459
460 // We'll add it anyway given that we haven't reached the
461 // minimum number of old regions.
462 expensive_region_num += 1;
463 }
464 } else {
465 if (old_region_length() >= min_old_cset_length) {
466 // In the non-auto-tuning case, we'll finish adding regions
467 // to the CSet if we reach the minimum.
468
469 log_debug(gc, ergo, cset)("Finish adding old regions to CSet (old CSet region num reached min). old %u regions, min %u regions",
470 old_region_length(), min_old_cset_length);
471 break;
472 }
473 }
474
475 // We will add this region to the CSet.
476 time_remaining_ms = MAX2(time_remaining_ms - predicted_time_ms, 0.0);
477 predicted_old_time_ms += predicted_time_ms;
478 cset_chooser()->pop(); // already have region via peek()
479 _g1h->old_set_remove(hr);
480 add_old_region(hr);
481
482 hr = cset_chooser()->peek();
483 }
484 if (hr == NULL) {
485 log_debug(gc, ergo, cset)("Finish adding old regions to CSet (candidate old regions not available)");
486 }
487
488 if (expensive_region_num > 0) {
489 // We print the information once here at the end, predicated on
490 // whether we added any apparently expensive regions or not, to
491 // avoid generating output per region.
492 log_debug(gc, ergo, cset)("Added expensive regions to CSet (old CSet region num not reached min)."
493 "old: %u regions, expensive: %u regions, min: %u regions, remaining time: %1.2fms",
494 old_region_length(), expensive_region_num, min_old_cset_length, time_remaining_ms);
495 }
496
497 cset_chooser()->verify();
498 }
499
|