1 /* 2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 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.inline.hpp" 27 #include "gc/g1/g1CollectionSet.hpp" 28 #include "gc/g1/g1CollectionSetCandidates.hpp" 29 #include "gc/g1/g1CollectorState.hpp" 30 #include "gc/g1/g1HotCardCache.hpp" 31 #include "gc/g1/g1ParScanThreadState.hpp" 32 #include "gc/g1/g1Policy.hpp" 33 #include "gc/g1/heapRegion.inline.hpp" 34 #include "gc/g1/heapRegionRemSet.hpp" 35 #include "gc/g1/heapRegionSet.hpp" 36 #include "logging/logStream.hpp" 37 #include "runtime/orderAccess.hpp" 38 #include "utilities/debug.hpp" 39 #include "utilities/globalDefinitions.hpp" 40 #include "utilities/quickSort.hpp" 41 42 G1CollectorState* G1CollectionSet::collector_state() const { 43 return _g1h->collector_state(); 44 } 45 46 G1GCPhaseTimes* G1CollectionSet::phase_times() { 47 return _policy->phase_times(); 48 } 49 50 double G1CollectionSet::predict_region_non_copy_time_ms(HeapRegion* hr) const { 51 return _policy->predict_region_non_copy_time_ms(hr, collector_state()->in_young_only_phase()); 52 } 53 54 G1CollectionSet::G1CollectionSet(G1CollectedHeap* g1h, G1Policy* policy) : 55 _g1h(g1h), 56 _policy(policy), 57 _candidates(NULL), 58 _eden_region_length(0), 59 _survivor_region_length(0), 60 _old_region_length(0), 61 _collection_set_regions(NULL), 62 _collection_set_cur_length(0), 63 _collection_set_max_length(0), 64 _num_optional_regions(0), 65 _bytes_used_before(0), 66 _recorded_rs_length(0), 67 _inc_build_state(Inactive), 68 _inc_part_start(0), 69 _inc_collection_set_stats(NULL), 70 _inc_bytes_used_before(0), 71 _inc_recorded_rs_length(0), 72 _inc_recorded_rs_length_diff(0), 73 _inc_predicted_non_copy_time_ms(0.0), 74 _inc_predicted_non_copy_time_ms_diff(0.0) { 75 } 76 77 G1CollectionSet::~G1CollectionSet() { 78 FREE_C_HEAP_ARRAY(uint, _collection_set_regions); 79 FREE_C_HEAP_ARRAY(IncCollectionSetRegionStat, _inc_collection_set_stats); 80 free_optional_regions(); 81 clear_candidates(); 82 } 83 84 void G1CollectionSet::init_region_lengths(uint eden_cset_region_length, 85 uint survivor_cset_region_length) { 86 assert_at_safepoint_on_vm_thread(); 87 88 _eden_region_length = eden_cset_region_length; 89 _survivor_region_length = survivor_cset_region_length; 90 91 assert((size_t) young_region_length() == _collection_set_cur_length, 92 "Young region length %u should match collection set length " SIZE_FORMAT, young_region_length(), _collection_set_cur_length); 93 94 _old_region_length = 0; 95 free_optional_regions(); 96 } 97 98 void G1CollectionSet::initialize(uint max_region_length) { 99 guarantee(_collection_set_regions == NULL, "Must only initialize once."); 100 _collection_set_max_length = max_region_length; 101 _collection_set_regions = NEW_C_HEAP_ARRAY(uint, max_region_length, mtGC); 102 _inc_collection_set_stats = NEW_C_HEAP_ARRAY(IncCollectionSetRegionStat, max_region_length, mtGC); 103 } 104 105 void G1CollectionSet::free_optional_regions() { 106 _num_optional_regions = 0; 107 } 108 109 void G1CollectionSet::clear_candidates() { 110 delete _candidates; 111 _candidates = NULL; 112 } 113 114 void G1CollectionSet::set_recorded_rs_length(size_t rs_length) { 115 _recorded_rs_length = rs_length; 116 } 117 118 // Add the heap region at the head of the non-incremental collection set 119 void G1CollectionSet::add_old_region(HeapRegion* hr) { 120 assert_at_safepoint_on_vm_thread(); 121 122 assert(_inc_build_state == Active, 123 "Precondition, actively building cset or adding optional later on"); 124 assert(hr->is_old(), "the region should be old"); 125 126 assert(!hr->in_collection_set(), "should not already be in the collection set"); 127 _g1h->register_old_region_with_region_attr(hr); 128 129 _collection_set_regions[_collection_set_cur_length++] = hr->hrm_index(); 130 assert(_collection_set_cur_length <= _collection_set_max_length, "Collection set now larger than maximum size."); 131 132 _bytes_used_before += hr->used(); 133 _recorded_rs_length += hr->rem_set()->occupied(); 134 _old_region_length++; 135 136 _g1h->old_set_remove(hr); 137 } 138 139 void G1CollectionSet::add_optional_region(HeapRegion* hr) { 140 assert(hr->is_old(), "the region should be old"); 141 assert(!hr->in_collection_set(), "should not already be in the CSet"); 142 143 _g1h->register_optional_region_with_region_attr(hr); 144 145 hr->set_index_in_opt_cset(_num_optional_regions++); 146 } 147 148 void G1CollectionSet::start_incremental_building() { 149 assert(_collection_set_cur_length == 0, "Collection set must be empty before starting a new collection set."); 150 assert(_inc_build_state == Inactive, "Precondition"); 151 #ifdef ASSERT 152 for (size_t i = 0; i < _collection_set_max_length; i++) { 153 _inc_collection_set_stats[i].reset(); 154 } 155 #endif 156 157 _inc_bytes_used_before = 0; 158 159 _inc_recorded_rs_length = 0; 160 _inc_recorded_rs_length_diff = 0; 161 _inc_predicted_non_copy_time_ms = 0.0; 162 _inc_predicted_non_copy_time_ms_diff = 0.0; 163 164 update_incremental_marker(); 165 } 166 167 void G1CollectionSet::finalize_incremental_building() { 168 assert(_inc_build_state == Active, "Precondition"); 169 assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint"); 170 171 // The two "main" fields, _inc_recorded_rs_length and 172 // _inc_predicted_non_copy_time_ms, are updated by the thread 173 // that adds a new region to the CSet. Further updates by the 174 // concurrent refinement thread that samples the young RSet lengths 175 // are accumulated in the *_diff fields. Here we add the diffs to 176 // the "main" fields. 177 178 _inc_recorded_rs_length += _inc_recorded_rs_length_diff; 179 _inc_predicted_non_copy_time_ms += _inc_predicted_non_copy_time_ms_diff; 180 181 _inc_recorded_rs_length_diff = 0; 182 _inc_predicted_non_copy_time_ms_diff = 0.0; 183 } 184 185 void G1CollectionSet::clear() { 186 assert_at_safepoint_on_vm_thread(); 187 _collection_set_cur_length = 0; 188 } 189 190 void G1CollectionSet::iterate(HeapRegionClosure* cl) const { 191 size_t len = _collection_set_cur_length; 192 OrderAccess::loadload(); 193 194 for (uint i = 0; i < len; i++) { 195 HeapRegion* r = _g1h->region_at(_collection_set_regions[i]); 196 bool result = cl->do_heap_region(r); 197 if (result) { 198 cl->set_incomplete(); 199 return; 200 } 201 } 202 } 203 204 void G1CollectionSet::iterate_optional(HeapRegionClosure* cl) const { 205 assert_at_safepoint(); 206 207 for (uint i = 0; i < _num_optional_regions; i++) { 208 HeapRegion* r = _candidates->at(i); 209 bool result = cl->do_heap_region(r); 210 guarantee(!result, "Must not cancel iteration"); 211 } 212 } 213 214 void G1CollectionSet::iterate_incremental_part_from(HeapRegionClosure* cl, 215 HeapRegionClaimer* hr_claimer, 216 uint worker_id, 217 uint total_workers) const { 218 assert_at_safepoint(); 219 220 size_t len = increment_length(); 221 if (len == 0) { 222 return; 223 } 224 225 size_t start_pos = (worker_id * len) / total_workers; 226 size_t cur_pos = start_pos; 227 228 do { 229 uint region_idx = _collection_set_regions[cur_pos + _inc_part_start]; 230 if (hr_claimer == NULL || hr_claimer->claim_region(region_idx)) { 231 HeapRegion* r = _g1h->region_at(region_idx); 232 bool result = cl->do_heap_region(r); 233 guarantee(!result, "Must not cancel iteration"); 234 } 235 236 cur_pos++; 237 if (cur_pos == len) { 238 cur_pos = 0; 239 } 240 } while (cur_pos != start_pos); 241 } 242 243 void G1CollectionSet::update_young_region_prediction(HeapRegion* hr, 244 size_t new_rs_length) { 245 // Update the CSet information that is dependent on the new RS length 246 assert(hr->is_young(), "Precondition"); 247 assert(!SafepointSynchronize::is_at_safepoint(), "should not be at a safepoint"); 248 249 IncCollectionSetRegionStat* stat = &_inc_collection_set_stats[hr->hrm_index()]; 250 251 size_t old_rs_length = stat->_rs_length; 252 assert(old_rs_length <= new_rs_length, 253 "Remembered set decreased (changed from " SIZE_FORMAT " to " SIZE_FORMAT " region %u type %s)", 254 old_rs_length, new_rs_length, hr->hrm_index(), hr->get_short_type_str()); 255 size_t rs_length_diff = new_rs_length - old_rs_length; 256 stat->_rs_length = new_rs_length; 257 _inc_recorded_rs_length_diff += rs_length_diff; 258 259 double old_non_copy_time = stat->_non_copy_time_ms; 260 assert(old_non_copy_time >= 0.0, "Non copy time for region %u not initialized yet, is %.3f", hr->hrm_index(), old_non_copy_time); 261 double new_non_copy_time = predict_region_non_copy_time_ms(hr); 262 double non_copy_time_ms_diff = new_non_copy_time - old_non_copy_time; 263 264 stat->_non_copy_time_ms = new_non_copy_time; 265 _inc_predicted_non_copy_time_ms_diff += non_copy_time_ms_diff; 266 } 267 268 void G1CollectionSet::add_young_region_common(HeapRegion* hr) { 269 assert(hr->is_young(), "invariant"); 270 assert(_inc_build_state == Active, "Precondition"); 271 272 // This routine is used when: 273 // * adding survivor regions to the incremental cset at the end of an 274 // evacuation pause or 275 // * adding the current allocation region to the incremental cset 276 // when it is retired. 277 // Therefore this routine may be called at a safepoint by the 278 // VM thread, or in-between safepoints by mutator threads (when 279 // retiring the current allocation region) 280 // We need to clear and set the cached recorded/cached collection set 281 // information in the heap region here (before the region gets added 282 // to the collection set). An individual heap region's cached values 283 // are calculated, aggregated with the policy collection set info, 284 // and cached in the heap region here (initially) and (subsequently) 285 // by the Young List sampling code. 286 // Ignore calls to this due to retirement during full gc. 287 288 if (!_g1h->collector_state()->in_full_gc()) { 289 size_t rs_length = hr->rem_set()->occupied(); 290 double non_copy_time = predict_region_non_copy_time_ms(hr); 291 292 // Cache the values we have added to the aggregated information 293 // in the heap region in case we have to remove this region from 294 // the incremental collection set, or it is updated by the 295 // rset sampling code 296 297 IncCollectionSetRegionStat* stat = &_inc_collection_set_stats[hr->hrm_index()]; 298 stat->_rs_length = rs_length; 299 stat->_non_copy_time_ms = non_copy_time; 300 301 _inc_recorded_rs_length += rs_length; 302 _inc_predicted_non_copy_time_ms += non_copy_time; 303 _inc_bytes_used_before += hr->used(); 304 } 305 306 assert(!hr->in_collection_set(), "invariant"); 307 _g1h->register_young_region_with_region_attr(hr); 308 309 // We use UINT_MAX as "invalid" marker in verification. 310 assert(_collection_set_cur_length < (UINT_MAX - 1), 311 "Collection set is too large with " SIZE_FORMAT " entries", _collection_set_cur_length); 312 hr->set_young_index_in_cset((uint)_collection_set_cur_length + 1); 313 314 _collection_set_regions[_collection_set_cur_length] = hr->hrm_index(); 315 // Concurrent readers must observe the store of the value in the array before an 316 // update to the length field. 317 OrderAccess::storestore(); 318 _collection_set_cur_length++; 319 assert(_collection_set_cur_length <= _collection_set_max_length, "Collection set larger than maximum allowed."); 320 } 321 322 void G1CollectionSet::add_survivor_regions(HeapRegion* hr) { 323 assert(hr->is_survivor(), "Must only add survivor regions, but is %s", hr->get_type_str()); 324 add_young_region_common(hr); 325 } 326 327 void G1CollectionSet::add_eden_region(HeapRegion* hr) { 328 assert(hr->is_eden(), "Must only add eden regions, but is %s", hr->get_type_str()); 329 add_young_region_common(hr); 330 } 331 332 #ifndef PRODUCT 333 class G1VerifyYoungAgesClosure : public HeapRegionClosure { 334 public: 335 bool _valid; 336 337 G1VerifyYoungAgesClosure() : HeapRegionClosure(), _valid(true) { } 338 339 virtual bool do_heap_region(HeapRegion* r) { 340 guarantee(r->is_young(), "Region must be young but is %s", r->get_type_str()); 341 342 if (!r->has_surv_rate_group()) { 343 log_error(gc, verify)("## encountered young region without surv_rate_group"); 344 _valid = false; 345 } 346 347 if (!r->has_valid_age_in_surv_rate()) { 348 log_error(gc, verify)("## encountered invalid age in young region"); 349 _valid = false; 350 } 351 352 return false; 353 } 354 355 bool valid() const { return _valid; } 356 }; 357 358 bool G1CollectionSet::verify_young_ages() { 359 assert_at_safepoint_on_vm_thread(); 360 361 G1VerifyYoungAgesClosure cl; 362 iterate(&cl); 363 364 if (!cl.valid()) { 365 LogStreamHandle(Error, gc, verify) log; 366 print(&log); 367 } 368 369 return cl.valid(); 370 } 371 372 class G1PrintCollectionSetDetailClosure : public HeapRegionClosure { 373 outputStream* _st; 374 public: 375 G1PrintCollectionSetDetailClosure(outputStream* st) : HeapRegionClosure(), _st(st) { } 376 377 virtual bool do_heap_region(HeapRegion* r) { 378 assert(r->in_collection_set(), "Region %u should be in collection set", r->hrm_index()); 379 _st->print_cr(" " HR_FORMAT ", P: " PTR_FORMAT "N: " PTR_FORMAT ", age: %4d", 380 HR_FORMAT_PARAMS(r), 381 p2i(r->prev_top_at_mark_start()), 382 p2i(r->next_top_at_mark_start()), 383 r->has_surv_rate_group() ? r->age_in_surv_rate_group() : -1); 384 return false; 385 } 386 }; 387 388 void G1CollectionSet::print(outputStream* st) { 389 st->print_cr("\nCollection_set:"); 390 391 G1PrintCollectionSetDetailClosure cl(st); 392 iterate(&cl); 393 } 394 #endif // !PRODUCT 395 396 double G1CollectionSet::finalize_young_part(double target_pause_time_ms, G1SurvivorRegions* survivors) { 397 Ticks start_time = Ticks::now(); 398 399 finalize_incremental_building(); 400 401 guarantee(target_pause_time_ms > 0.0, 402 "target_pause_time_ms = %1.6lf should be positive", target_pause_time_ms); 403 404 size_t pending_cards = _policy->pending_cards_at_gc_start() + _g1h->hot_card_cache()->num_entries(); 405 406 log_trace(gc, ergo, cset)("Start choosing CSet. Pending cards: " SIZE_FORMAT " target pause time: %1.2fms", 407 pending_cards, target_pause_time_ms); 408 409 // The young list is laid with the survivor regions from the previous 410 // pause are appended to the RHS of the young list, i.e. 411 // [Newly Young Regions ++ Survivors from last pause]. 412 413 uint eden_region_length = _g1h->eden_regions_count(); 414 uint survivor_region_length = survivors->length(); 415 init_region_lengths(eden_region_length, survivor_region_length); 416 417 verify_young_cset_indices(); 418 419 // Clear the fields that point to the survivor list - they are all young now. 420 survivors->convert_to_eden(); 421 422 _bytes_used_before = _inc_bytes_used_before; 423 424 // The number of recorded young regions is the incremental 425 // collection set's current size 426 set_recorded_rs_length(_inc_recorded_rs_length); 427 428 double predicted_base_time_ms = _policy->predict_base_elapsed_time_ms(pending_cards); 429 double predicted_eden_time = _inc_predicted_non_copy_time_ms + _policy->predict_eden_copy_time_ms(eden_region_length); 430 double remaining_time_ms = MAX2(target_pause_time_ms - (predicted_base_time_ms + predicted_eden_time), 0.0); 431 432 log_trace(gc, ergo, cset)("Added young regions to CSet. Eden: %u regions, Survivors: %u regions, " 433 "predicted eden time: %1.2fms, predicted base time: %1.2fms, target pause time: %1.2fms, remaining time: %1.2fms", 434 eden_region_length, survivor_region_length, 435 predicted_eden_time, predicted_base_time_ms, target_pause_time_ms, remaining_time_ms); 436 437 phase_times()->record_young_cset_choice_time_ms((Ticks::now() - start_time).seconds() * 1000.0); 438 439 return remaining_time_ms; 440 } 441 442 static int compare_region_idx(const uint a, const uint b) { 443 if (a > b) { 444 return 1; 445 } else if (a == b) { 446 return 0; 447 } else { 448 return -1; 449 } 450 } 451 452 void G1CollectionSet::finalize_old_part(double time_remaining_ms) { 453 double non_young_start_time_sec = os::elapsedTime(); 454 455 if (collector_state()->in_mixed_phase()) { 456 candidates()->verify(); 457 458 uint num_initial_old_regions; 459 uint num_optional_old_regions; 460 461 _policy->calculate_old_collection_set_regions(candidates(), 462 time_remaining_ms, 463 num_initial_old_regions, 464 num_optional_old_regions); 465 466 // Prepare initial old regions. 467 move_candidates_to_collection_set(num_initial_old_regions); 468 469 // Prepare optional old regions for evacuation. 470 uint candidate_idx = candidates()->cur_idx(); 471 for (uint i = 0; i < num_optional_old_regions; i++) { 472 add_optional_region(candidates()->at(candidate_idx + i)); 473 } 474 475 candidates()->verify(); 476 } 477 478 stop_incremental_building(); 479 480 double non_young_end_time_sec = os::elapsedTime(); 481 phase_times()->record_non_young_cset_choice_time_ms((non_young_end_time_sec - non_young_start_time_sec) * 1000.0); 482 483 QuickSort::sort(_collection_set_regions, _collection_set_cur_length, compare_region_idx, true); 484 } 485 486 void G1CollectionSet::move_candidates_to_collection_set(uint num_old_candidate_regions) { 487 if (num_old_candidate_regions == 0) { 488 return; 489 } 490 uint candidate_idx = candidates()->cur_idx(); 491 for (uint i = 0; i < num_old_candidate_regions; i++) { 492 HeapRegion* r = candidates()->at(candidate_idx + i); 493 // This potentially optional candidate region is going to be an actual collection 494 // set region. Clear cset marker. 495 _g1h->clear_region_attr(r); 496 add_old_region(r); 497 } 498 candidates()->remove(num_old_candidate_regions); 499 500 candidates()->verify(); 501 } 502 503 void G1CollectionSet::finalize_initial_collection_set(double target_pause_time_ms, G1SurvivorRegions* survivor) { 504 double time_remaining_ms = finalize_young_part(target_pause_time_ms, survivor); 505 finalize_old_part(time_remaining_ms); 506 } 507 508 bool G1CollectionSet::finalize_optional_for_evacuation(double remaining_pause_time) { 509 update_incremental_marker(); 510 511 uint num_selected_regions; 512 _policy->calculate_optional_collection_set_regions(candidates(), 513 _num_optional_regions, 514 remaining_pause_time, 515 num_selected_regions); 516 517 move_candidates_to_collection_set(num_selected_regions); 518 519 _num_optional_regions -= num_selected_regions; 520 521 stop_incremental_building(); 522 523 _g1h->verify_region_attr_remset_update(); 524 525 return num_selected_regions > 0; 526 } 527 528 void G1CollectionSet::abandon_optional_collection_set(G1ParScanThreadStateSet* pss) { 529 for (uint i = 0; i < _num_optional_regions; i++) { 530 HeapRegion* r = candidates()->at(candidates()->cur_idx() + i); 531 pss->record_unused_optional_region(r); 532 // Clear collection set marker and make sure that the remembered set information 533 // is correct as we still need it later. 534 _g1h->clear_region_attr(r); 535 _g1h->register_region_with_region_attr(r); 536 r->clear_index_in_opt_cset(); 537 } 538 free_optional_regions(); 539 540 _g1h->verify_region_attr_remset_update(); 541 } 542 543 #ifdef ASSERT 544 class G1VerifyYoungCSetIndicesClosure : public HeapRegionClosure { 545 private: 546 size_t _young_length; 547 uint* _heap_region_indices; 548 public: 549 G1VerifyYoungCSetIndicesClosure(size_t young_length) : HeapRegionClosure(), _young_length(young_length) { 550 _heap_region_indices = NEW_C_HEAP_ARRAY(uint, young_length + 1, mtGC); 551 for (size_t i = 0; i < young_length + 1; i++) { 552 _heap_region_indices[i] = UINT_MAX; 553 } 554 } 555 ~G1VerifyYoungCSetIndicesClosure() { 556 FREE_C_HEAP_ARRAY(int, _heap_region_indices); 557 } 558 559 virtual bool do_heap_region(HeapRegion* r) { 560 const uint idx = r->young_index_in_cset(); 561 562 assert(idx > 0, "Young index must be set for all regions in the incremental collection set but is not for region %u.", r->hrm_index()); 563 assert(idx <= _young_length, "Young cset index %u too large for region %u", idx, r->hrm_index()); 564 565 assert(_heap_region_indices[idx] == UINT_MAX, 566 "Index %d used by multiple regions, first use by region %u, second by region %u", 567 idx, _heap_region_indices[idx], r->hrm_index()); 568 569 _heap_region_indices[idx] = r->hrm_index(); 570 571 return false; 572 } 573 }; 574 575 void G1CollectionSet::verify_young_cset_indices() const { 576 assert_at_safepoint_on_vm_thread(); 577 578 G1VerifyYoungCSetIndicesClosure cl(_collection_set_cur_length); 579 iterate(&cl); 580 } 581 #endif