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