1 /* 2 * Copyright (c) 2001, 2011, 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_implementation/g1/concurrentG1Refine.hpp" 27 #include "gc_implementation/g1/concurrentMark.hpp" 28 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp" 29 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 30 #include "gc_implementation/g1/g1CollectorPolicy.hpp" 31 #include "gc_implementation/g1/heapRegionRemSet.hpp" 32 #include "gc_implementation/shared/gcPolicyCounters.hpp" 33 #include "runtime/arguments.hpp" 34 #include "runtime/java.hpp" 35 #include "runtime/mutexLocker.hpp" 36 #include "utilities/debug.hpp" 37 38 #define PREDICTIONS_VERBOSE 0 39 40 // <NEW PREDICTION> 41 42 // Different defaults for different number of GC threads 43 // They were chosen by running GCOld and SPECjbb on debris with different 44 // numbers of GC threads and choosing them based on the results 45 46 // all the same 47 static double rs_length_diff_defaults[] = { 48 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 49 }; 50 51 static double cost_per_card_ms_defaults[] = { 52 0.01, 0.005, 0.005, 0.003, 0.003, 0.002, 0.002, 0.0015 53 }; 54 55 // all the same 56 static double fully_young_cards_per_entry_ratio_defaults[] = { 57 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 58 }; 59 60 static double cost_per_entry_ms_defaults[] = { 61 0.015, 0.01, 0.01, 0.008, 0.008, 0.0055, 0.0055, 0.005 62 }; 63 64 static double cost_per_byte_ms_defaults[] = { 65 0.00006, 0.00003, 0.00003, 0.000015, 0.000015, 0.00001, 0.00001, 0.000009 66 }; 67 68 // these should be pretty consistent 69 static double constant_other_time_ms_defaults[] = { 70 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 71 }; 72 73 74 static double young_other_cost_per_region_ms_defaults[] = { 75 0.3, 0.2, 0.2, 0.15, 0.15, 0.12, 0.12, 0.1 76 }; 77 78 static double non_young_other_cost_per_region_ms_defaults[] = { 79 1.0, 0.7, 0.7, 0.5, 0.5, 0.42, 0.42, 0.30 80 }; 81 82 // </NEW PREDICTION> 83 84 // Help class for avoiding interleaved logging 85 class LineBuffer: public StackObj { 86 87 private: 88 static const int BUFFER_LEN = 1024; 89 static const int INDENT_CHARS = 3; 90 char _buffer[BUFFER_LEN]; 91 int _indent_level; 92 int _cur; 93 94 void vappend(const char* format, va_list ap) { 95 int res = vsnprintf(&_buffer[_cur], BUFFER_LEN - _cur, format, ap); 96 if (res != -1) { 97 _cur += res; 98 } else { 99 DEBUG_ONLY(warning("buffer too small in LineBuffer");) 100 _buffer[BUFFER_LEN -1] = 0; 101 _cur = BUFFER_LEN; // vsnprintf above should not add to _buffer if we are called again 102 } 103 } 104 105 public: 106 explicit LineBuffer(int indent_level): _indent_level(indent_level), _cur(0) { 107 for (; (_cur < BUFFER_LEN && _cur < (_indent_level * INDENT_CHARS)); _cur++) { 108 _buffer[_cur] = ' '; 109 } 110 } 111 112 #ifndef PRODUCT 113 ~LineBuffer() { 114 assert(_cur == _indent_level * INDENT_CHARS, "pending data in buffer - append_and_print_cr() not called?"); 115 } 116 #endif 117 118 void append(const char* format, ...) { 119 va_list ap; 120 va_start(ap, format); 121 vappend(format, ap); 122 va_end(ap); 123 } 124 125 void append_and_print_cr(const char* format, ...) { 126 va_list ap; 127 va_start(ap, format); 128 vappend(format, ap); 129 va_end(ap); 130 gclog_or_tty->print_cr("%s", _buffer); 131 _cur = _indent_level * INDENT_CHARS; 132 } 133 }; 134 135 G1CollectorPolicy::G1CollectorPolicy() : 136 _parallel_gc_threads(G1CollectedHeap::use_parallel_gc_threads() 137 ? ParallelGCThreads : 1), 138 139 _n_pauses(0), 140 _recent_rs_scan_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), 141 _recent_pause_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), 142 _recent_rs_sizes(new TruncatedSeq(NumPrevPausesForHeuristics)), 143 _recent_gc_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), 144 _all_pause_times_ms(new NumberSeq()), 145 _stop_world_start(0.0), 146 _all_stop_world_times_ms(new NumberSeq()), 147 _all_yield_times_ms(new NumberSeq()), 148 149 _all_mod_union_times_ms(new NumberSeq()), 150 151 _summary(new Summary()), 152 153 #ifndef PRODUCT 154 _cur_clear_ct_time_ms(0.0), 155 _min_clear_cc_time_ms(-1.0), 156 _max_clear_cc_time_ms(-1.0), 157 _cur_clear_cc_time_ms(0.0), 158 _cum_clear_cc_time_ms(0.0), 159 _num_cc_clears(0L), 160 #endif 161 162 _region_num_young(0), 163 _region_num_tenured(0), 164 _prev_region_num_young(0), 165 _prev_region_num_tenured(0), 166 167 _aux_num(10), 168 _all_aux_times_ms(new NumberSeq[_aux_num]), 169 _cur_aux_start_times_ms(new double[_aux_num]), 170 _cur_aux_times_ms(new double[_aux_num]), 171 _cur_aux_times_set(new bool[_aux_num]), 172 173 _concurrent_mark_init_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), 174 _concurrent_mark_remark_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), 175 _concurrent_mark_cleanup_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), 176 177 // <NEW PREDICTION> 178 179 _alloc_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 180 _prev_collection_pause_end_ms(0.0), 181 _pending_card_diff_seq(new TruncatedSeq(TruncatedSeqLength)), 182 _rs_length_diff_seq(new TruncatedSeq(TruncatedSeqLength)), 183 _cost_per_card_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 184 _fully_young_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)), 185 _partially_young_cards_per_entry_ratio_seq( 186 new TruncatedSeq(TruncatedSeqLength)), 187 _cost_per_entry_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 188 _partially_young_cost_per_entry_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 189 _cost_per_byte_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 190 _cost_per_byte_ms_during_cm_seq(new TruncatedSeq(TruncatedSeqLength)), 191 _constant_other_time_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 192 _young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)), 193 _non_young_other_cost_per_region_ms_seq( 194 new TruncatedSeq(TruncatedSeqLength)), 195 196 _pending_cards_seq(new TruncatedSeq(TruncatedSeqLength)), 197 _scanned_cards_seq(new TruncatedSeq(TruncatedSeqLength)), 198 _rs_lengths_seq(new TruncatedSeq(TruncatedSeqLength)), 199 200 _pause_time_target_ms((double) MaxGCPauseMillis), 201 202 // </NEW PREDICTION> 203 204 _full_young_gcs(true), 205 _full_young_pause_num(0), 206 _partial_young_pause_num(0), 207 208 _during_marking(false), 209 _in_marking_window(false), 210 _in_marking_window_im(false), 211 212 _known_garbage_ratio(0.0), 213 _known_garbage_bytes(0), 214 215 _young_gc_eff_seq(new TruncatedSeq(TruncatedSeqLength)), 216 217 _recent_prev_end_times_for_all_gcs_sec(new TruncatedSeq(NumPrevPausesForHeuristics)), 218 219 _recent_CS_bytes_used_before(new TruncatedSeq(NumPrevPausesForHeuristics)), 220 _recent_CS_bytes_surviving(new TruncatedSeq(NumPrevPausesForHeuristics)), 221 222 _recent_avg_pause_time_ratio(0.0), 223 _num_markings(0), 224 _n_marks(0), 225 _n_pauses_at_mark_end(0), 226 227 _all_full_gc_times_ms(new NumberSeq()), 228 229 // G1PausesBtwnConcMark defaults to -1 230 // so the hack is to do the cast QQQ FIXME 231 _pauses_btwn_concurrent_mark((size_t)G1PausesBtwnConcMark), 232 _n_marks_since_last_pause(0), 233 _initiate_conc_mark_if_possible(false), 234 _during_initial_mark_pause(false), 235 _should_revert_to_full_young_gcs(false), 236 _last_full_young_gc(false), 237 238 _eden_bytes_before_gc(0), 239 _survivor_bytes_before_gc(0), 240 _capacity_before_gc(0), 241 242 _prev_collection_pause_used_at_end_bytes(0), 243 244 _collection_set(NULL), 245 _collection_set_size(0), 246 _collection_set_bytes_used_before(0), 247 248 // Incremental CSet attributes 249 _inc_cset_build_state(Inactive), 250 _inc_cset_head(NULL), 251 _inc_cset_tail(NULL), 252 _inc_cset_size(0), 253 _inc_cset_young_index(0), 254 _inc_cset_bytes_used_before(0), 255 _inc_cset_max_finger(NULL), 256 _inc_cset_recorded_young_bytes(0), 257 _inc_cset_recorded_rs_lengths(0), 258 _inc_cset_predicted_elapsed_time_ms(0.0), 259 _inc_cset_predicted_bytes_to_copy(0), 260 261 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away 262 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list 263 #endif // _MSC_VER 264 265 _short_lived_surv_rate_group(new SurvRateGroup(this, "Short Lived", 266 G1YoungSurvRateNumRegionsSummary)), 267 _survivor_surv_rate_group(new SurvRateGroup(this, "Survivor", 268 G1YoungSurvRateNumRegionsSummary)), 269 // add here any more surv rate groups 270 _recorded_survivor_regions(0), 271 _recorded_survivor_head(NULL), 272 _recorded_survivor_tail(NULL), 273 _survivors_age_table(true), 274 275 _gc_overhead_perc(0.0) 276 277 { 278 // Set up the region size and associated fields. Given that the 279 // policy is created before the heap, we have to set this up here, 280 // so it's done as soon as possible. 281 HeapRegion::setup_heap_region_size(Arguments::min_heap_size()); 282 HeapRegionRemSet::setup_remset_size(); 283 284 // Verify PLAB sizes 285 const uint region_size = HeapRegion::GrainWords; 286 if (YoungPLABSize > region_size || OldPLABSize > region_size) { 287 char buffer[128]; 288 jio_snprintf(buffer, sizeof(buffer), "%sPLABSize should be at most %u", 289 OldPLABSize > region_size ? "Old" : "Young", region_size); 290 vm_exit_during_initialization(buffer); 291 } 292 293 _recent_prev_end_times_for_all_gcs_sec->add(os::elapsedTime()); 294 _prev_collection_pause_end_ms = os::elapsedTime() * 1000.0; 295 296 _par_last_gc_worker_start_times_ms = new double[_parallel_gc_threads]; 297 _par_last_ext_root_scan_times_ms = new double[_parallel_gc_threads]; 298 _par_last_mark_stack_scan_times_ms = new double[_parallel_gc_threads]; 299 300 _par_last_update_rs_times_ms = new double[_parallel_gc_threads]; 301 _par_last_update_rs_processed_buffers = new double[_parallel_gc_threads]; 302 303 _par_last_scan_rs_times_ms = new double[_parallel_gc_threads]; 304 305 _par_last_obj_copy_times_ms = new double[_parallel_gc_threads]; 306 307 _par_last_termination_times_ms = new double[_parallel_gc_threads]; 308 _par_last_termination_attempts = new double[_parallel_gc_threads]; 309 _par_last_gc_worker_end_times_ms = new double[_parallel_gc_threads]; 310 _par_last_gc_worker_times_ms = new double[_parallel_gc_threads]; 311 312 // start conservatively 313 _expensive_region_limit_ms = 0.5 * (double) MaxGCPauseMillis; 314 315 // <NEW PREDICTION> 316 317 int index; 318 if (ParallelGCThreads == 0) 319 index = 0; 320 else if (ParallelGCThreads > 8) 321 index = 7; 322 else 323 index = ParallelGCThreads - 1; 324 325 _pending_card_diff_seq->add(0.0); 326 _rs_length_diff_seq->add(rs_length_diff_defaults[index]); 327 _cost_per_card_ms_seq->add(cost_per_card_ms_defaults[index]); 328 _fully_young_cards_per_entry_ratio_seq->add( 329 fully_young_cards_per_entry_ratio_defaults[index]); 330 _cost_per_entry_ms_seq->add(cost_per_entry_ms_defaults[index]); 331 _cost_per_byte_ms_seq->add(cost_per_byte_ms_defaults[index]); 332 _constant_other_time_ms_seq->add(constant_other_time_ms_defaults[index]); 333 _young_other_cost_per_region_ms_seq->add( 334 young_other_cost_per_region_ms_defaults[index]); 335 _non_young_other_cost_per_region_ms_seq->add( 336 non_young_other_cost_per_region_ms_defaults[index]); 337 338 // </NEW PREDICTION> 339 340 // Below, we might need to calculate the pause time target based on 341 // the pause interval. When we do so we are going to give G1 maximum 342 // flexibility and allow it to do pauses when it needs to. So, we'll 343 // arrange that the pause interval to be pause time target + 1 to 344 // ensure that a) the pause time target is maximized with respect to 345 // the pause interval and b) we maintain the invariant that pause 346 // time target < pause interval. If the user does not want this 347 // maximum flexibility, they will have to set the pause interval 348 // explicitly. 349 350 // First make sure that, if either parameter is set, its value is 351 // reasonable. 352 if (!FLAG_IS_DEFAULT(MaxGCPauseMillis)) { 353 if (MaxGCPauseMillis < 1) { 354 vm_exit_during_initialization("MaxGCPauseMillis should be " 355 "greater than 0"); 356 } 357 } 358 if (!FLAG_IS_DEFAULT(GCPauseIntervalMillis)) { 359 if (GCPauseIntervalMillis < 1) { 360 vm_exit_during_initialization("GCPauseIntervalMillis should be " 361 "greater than 0"); 362 } 363 } 364 365 // Then, if the pause time target parameter was not set, set it to 366 // the default value. 367 if (FLAG_IS_DEFAULT(MaxGCPauseMillis)) { 368 if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) { 369 // The default pause time target in G1 is 200ms 370 FLAG_SET_DEFAULT(MaxGCPauseMillis, 200); 371 } else { 372 // We do not allow the pause interval to be set without the 373 // pause time target 374 vm_exit_during_initialization("GCPauseIntervalMillis cannot be set " 375 "without setting MaxGCPauseMillis"); 376 } 377 } 378 379 // Then, if the interval parameter was not set, set it according to 380 // the pause time target (this will also deal with the case when the 381 // pause time target is the default value). 382 if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) { 383 FLAG_SET_DEFAULT(GCPauseIntervalMillis, MaxGCPauseMillis + 1); 384 } 385 386 // Finally, make sure that the two parameters are consistent. 387 if (MaxGCPauseMillis >= GCPauseIntervalMillis) { 388 char buffer[256]; 389 jio_snprintf(buffer, 256, 390 "MaxGCPauseMillis (%u) should be less than " 391 "GCPauseIntervalMillis (%u)", 392 MaxGCPauseMillis, GCPauseIntervalMillis); 393 vm_exit_during_initialization(buffer); 394 } 395 396 double max_gc_time = (double) MaxGCPauseMillis / 1000.0; 397 double time_slice = (double) GCPauseIntervalMillis / 1000.0; 398 _mmu_tracker = new G1MMUTrackerQueue(time_slice, max_gc_time); 399 _sigma = (double) G1ConfidencePercent / 100.0; 400 401 // start conservatively (around 50ms is about right) 402 _concurrent_mark_init_times_ms->add(0.05); 403 _concurrent_mark_remark_times_ms->add(0.05); 404 _concurrent_mark_cleanup_times_ms->add(0.20); 405 _tenuring_threshold = MaxTenuringThreshold; 406 407 // if G1FixedSurvivorSpaceSize is 0 which means the size is not 408 // fixed, then _max_survivor_regions will be calculated at 409 // calculate_young_list_target_length during initialization 410 _max_survivor_regions = G1FixedSurvivorSpaceSize / HeapRegion::GrainBytes; 411 412 assert(GCTimeRatio > 0, 413 "we should have set it to a default value set_g1_gc_flags() " 414 "if a user set it to 0"); 415 _gc_overhead_perc = 100.0 * (1.0 / (1.0 + GCTimeRatio)); 416 417 initialize_all(); 418 } 419 420 // Increment "i", mod "len" 421 static void inc_mod(int& i, int len) { 422 i++; if (i == len) i = 0; 423 } 424 425 void G1CollectorPolicy::initialize_flags() { 426 set_min_alignment(HeapRegion::GrainBytes); 427 set_max_alignment(GenRemSet::max_alignment_constraint(rem_set_name())); 428 if (SurvivorRatio < 1) { 429 vm_exit_during_initialization("Invalid survivor ratio specified"); 430 } 431 CollectorPolicy::initialize_flags(); 432 } 433 434 // The easiest way to deal with the parsing of the NewSize / 435 // MaxNewSize / etc. parameteres is to re-use the code in the 436 // TwoGenerationCollectorPolicy class. This is similar to what 437 // ParallelScavenge does with its GenerationSizer class (see 438 // ParallelScavengeHeap::initialize()). We might change this in the 439 // future, but it's a good start. 440 class G1YoungGenSizer : public TwoGenerationCollectorPolicy { 441 size_t size_to_region_num(size_t byte_size) { 442 return MAX2((size_t) 1, byte_size / HeapRegion::GrainBytes); 443 } 444 445 public: 446 G1YoungGenSizer() { 447 initialize_flags(); 448 initialize_size_info(); 449 } 450 451 size_t min_young_region_num() { 452 return size_to_region_num(_min_gen0_size); 453 } 454 size_t initial_young_region_num() { 455 return size_to_region_num(_initial_gen0_size); 456 } 457 size_t max_young_region_num() { 458 return size_to_region_num(_max_gen0_size); 459 } 460 }; 461 462 void G1CollectorPolicy::init() { 463 // Set aside an initial future to_space. 464 _g1 = G1CollectedHeap::heap(); 465 466 assert(Heap_lock->owned_by_self(), "Locking discipline."); 467 468 initialize_gc_policy_counters(); 469 470 G1YoungGenSizer sizer; 471 size_t initial_region_num = sizer.initial_young_region_num(); 472 473 if (UseAdaptiveSizePolicy) { 474 set_adaptive_young_list_length(true); 475 _young_list_fixed_length = 0; 476 } else { 477 set_adaptive_young_list_length(false); 478 _young_list_fixed_length = initial_region_num; 479 } 480 _free_regions_at_end_of_collection = _g1->free_regions(); 481 calculate_young_list_min_length(); 482 guarantee( _young_list_min_length == 0, "invariant, not enough info" ); 483 calculate_young_list_target_length(); 484 485 // We may immediately start allocating regions and placing them on the 486 // collection set list. Initialize the per-collection set info 487 start_incremental_cset_building(); 488 } 489 490 // Create the jstat counters for the policy. 491 void G1CollectorPolicy::initialize_gc_policy_counters() 492 { 493 _gc_policy_counters = new GCPolicyCounters("GarbageFirst", 1, 3); 494 } 495 496 void G1CollectorPolicy::calculate_young_list_min_length() { 497 _young_list_min_length = 0; 498 499 if (!adaptive_young_list_length()) 500 return; 501 502 if (_alloc_rate_ms_seq->num() > 3) { 503 double now_sec = os::elapsedTime(); 504 double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0; 505 double alloc_rate_ms = predict_alloc_rate_ms(); 506 size_t min_regions = (size_t) ceil(alloc_rate_ms * when_ms); 507 size_t current_region_num = _g1->young_list()->length(); 508 _young_list_min_length = min_regions + current_region_num; 509 } 510 } 511 512 void G1CollectorPolicy::calculate_young_list_target_length() { 513 if (adaptive_young_list_length()) { 514 size_t rs_lengths = (size_t) get_new_prediction(_rs_lengths_seq); 515 calculate_young_list_target_length(rs_lengths); 516 } else { 517 if (full_young_gcs()) 518 _young_list_target_length = _young_list_fixed_length; 519 else 520 _young_list_target_length = _young_list_fixed_length / 2; 521 } 522 523 // Make sure we allow the application to allocate at least one 524 // region before we need to do a collection again. 525 size_t min_length = _g1->young_list()->length() + 1; 526 _young_list_target_length = MAX2(_young_list_target_length, min_length); 527 calculate_max_gc_locker_expansion(); 528 calculate_survivors_policy(); 529 } 530 531 void G1CollectorPolicy::calculate_young_list_target_length(size_t rs_lengths) { 532 guarantee( adaptive_young_list_length(), "pre-condition" ); 533 guarantee( !_in_marking_window || !_last_full_young_gc, "invariant" ); 534 535 double start_time_sec = os::elapsedTime(); 536 size_t min_reserve_perc = MAX2((size_t)2, (size_t)G1ReservePercent); 537 min_reserve_perc = MIN2((size_t) 50, min_reserve_perc); 538 size_t reserve_regions = 539 (size_t) ((double) min_reserve_perc * (double) _g1->n_regions() / 100.0); 540 541 if (full_young_gcs() && _free_regions_at_end_of_collection > 0) { 542 // we are in fully-young mode and there are free regions in the heap 543 544 double survivor_regions_evac_time = 545 predict_survivor_regions_evac_time(); 546 547 double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0; 548 size_t pending_cards = (size_t) get_new_prediction(_pending_cards_seq); 549 size_t adj_rs_lengths = rs_lengths + predict_rs_length_diff(); 550 size_t scanned_cards = predict_young_card_num(adj_rs_lengths); 551 double base_time_ms = predict_base_elapsed_time_ms(pending_cards, scanned_cards) 552 + survivor_regions_evac_time; 553 554 // the result 555 size_t final_young_length = 0; 556 557 size_t init_free_regions = 558 MAX2((size_t)0, _free_regions_at_end_of_collection - reserve_regions); 559 560 // if we're still under the pause target... 561 if (base_time_ms <= target_pause_time_ms) { 562 // We make sure that the shortest young length that makes sense 563 // fits within the target pause time. 564 size_t min_young_length = 1; 565 566 if (predict_will_fit(min_young_length, base_time_ms, 567 init_free_regions, target_pause_time_ms)) { 568 // The shortest young length will fit within the target pause time; 569 // we'll now check whether the absolute maximum number of young 570 // regions will fit in the target pause time. If not, we'll do 571 // a binary search between min_young_length and max_young_length 572 size_t abs_max_young_length = _free_regions_at_end_of_collection - 1; 573 size_t max_young_length = abs_max_young_length; 574 575 if (max_young_length > min_young_length) { 576 // Let's check if the initial max young length will fit within the 577 // target pause. If so then there is no need to search for a maximal 578 // young length - we'll return the initial maximum 579 580 if (predict_will_fit(max_young_length, base_time_ms, 581 init_free_regions, target_pause_time_ms)) { 582 // The maximum young length will satisfy the target pause time. 583 // We are done so set min young length to this maximum length. 584 // The code after the loop will then set final_young_length using 585 // the value cached in the minimum length. 586 min_young_length = max_young_length; 587 } else { 588 // The maximum possible number of young regions will not fit within 589 // the target pause time so let's search.... 590 591 size_t diff = (max_young_length - min_young_length) / 2; 592 max_young_length = min_young_length + diff; 593 594 while (max_young_length > min_young_length) { 595 if (predict_will_fit(max_young_length, base_time_ms, 596 init_free_regions, target_pause_time_ms)) { 597 598 // The current max young length will fit within the target 599 // pause time. Note we do not exit the loop here. By setting 600 // min = max, and then increasing the max below means that 601 // we will continue searching for an upper bound in the 602 // range [max..max+diff] 603 min_young_length = max_young_length; 604 } 605 diff = (max_young_length - min_young_length) / 2; 606 max_young_length = min_young_length + diff; 607 } 608 // the above loop found a maximal young length that will fit 609 // within the target pause time. 610 } 611 assert(min_young_length <= abs_max_young_length, "just checking"); 612 } 613 final_young_length = min_young_length; 614 } 615 } 616 // and we're done! 617 618 // we should have at least one region in the target young length 619 _young_list_target_length = 620 final_young_length + _recorded_survivor_regions; 621 622 // let's keep an eye of how long we spend on this calculation 623 // right now, I assume that we'll print it when we need it; we 624 // should really adde it to the breakdown of a pause 625 double end_time_sec = os::elapsedTime(); 626 double elapsed_time_ms = (end_time_sec - start_time_sec) * 1000.0; 627 628 #ifdef TRACE_CALC_YOUNG_LENGTH 629 // leave this in for debugging, just in case 630 gclog_or_tty->print_cr("target = %1.1lf ms, young = " SIZE_FORMAT ", " 631 "elapsed %1.2lf ms, (%s%s) " SIZE_FORMAT SIZE_FORMAT, 632 target_pause_time_ms, 633 _young_list_target_length 634 elapsed_time_ms, 635 full_young_gcs() ? "full" : "partial", 636 during_initial_mark_pause() ? " i-m" : "", 637 _in_marking_window, 638 _in_marking_window_im); 639 #endif // TRACE_CALC_YOUNG_LENGTH 640 641 if (_young_list_target_length < _young_list_min_length) { 642 // bummer; this means that, if we do a pause when the maximal 643 // length dictates, we'll violate the pause spacing target (the 644 // min length was calculate based on the application's current 645 // alloc rate); 646 647 // so, we have to bite the bullet, and allocate the minimum 648 // number. We'll violate our target, but we just can't meet it. 649 650 #ifdef TRACE_CALC_YOUNG_LENGTH 651 // leave this in for debugging, just in case 652 gclog_or_tty->print_cr("adjusted target length from " 653 SIZE_FORMAT " to " SIZE_FORMAT, 654 _young_list_target_length, _young_list_min_length); 655 #endif // TRACE_CALC_YOUNG_LENGTH 656 657 _young_list_target_length = _young_list_min_length; 658 } 659 } else { 660 // we are in a partially-young mode or we've run out of regions (due 661 // to evacuation failure) 662 663 #ifdef TRACE_CALC_YOUNG_LENGTH 664 // leave this in for debugging, just in case 665 gclog_or_tty->print_cr("(partial) setting target to " SIZE_FORMAT 666 _young_list_min_length); 667 #endif // TRACE_CALC_YOUNG_LENGTH 668 // we'll do the pause as soon as possible by choosing the minimum 669 _young_list_target_length = _young_list_min_length; 670 } 671 672 _rs_lengths_prediction = rs_lengths; 673 } 674 675 // This is used by: calculate_young_list_target_length(rs_length). It 676 // returns true iff: 677 // the predicted pause time for the given young list will not overflow 678 // the target pause time 679 // and: 680 // the predicted amount of surviving data will not overflow the 681 // the amount of free space available for survivor regions. 682 // 683 bool 684 G1CollectorPolicy::predict_will_fit(size_t young_length, 685 double base_time_ms, 686 size_t init_free_regions, 687 double target_pause_time_ms) { 688 689 if (young_length >= init_free_regions) 690 // end condition 1: not enough space for the young regions 691 return false; 692 693 double accum_surv_rate_adj = 0.0; 694 double accum_surv_rate = 695 accum_yg_surv_rate_pred((int)(young_length - 1)) - accum_surv_rate_adj; 696 697 size_t bytes_to_copy = 698 (size_t) (accum_surv_rate * (double) HeapRegion::GrainBytes); 699 700 double copy_time_ms = predict_object_copy_time_ms(bytes_to_copy); 701 702 double young_other_time_ms = 703 predict_young_other_time_ms(young_length); 704 705 double pause_time_ms = 706 base_time_ms + copy_time_ms + young_other_time_ms; 707 708 if (pause_time_ms > target_pause_time_ms) 709 // end condition 2: over the target pause time 710 return false; 711 712 size_t free_bytes = 713 (init_free_regions - young_length) * HeapRegion::GrainBytes; 714 715 if ((2.0 + sigma()) * (double) bytes_to_copy > (double) free_bytes) 716 // end condition 3: out of to-space (conservatively) 717 return false; 718 719 // success! 720 return true; 721 } 722 723 double G1CollectorPolicy::predict_survivor_regions_evac_time() { 724 double survivor_regions_evac_time = 0.0; 725 for (HeapRegion * r = _recorded_survivor_head; 726 r != NULL && r != _recorded_survivor_tail->get_next_young_region(); 727 r = r->get_next_young_region()) { 728 survivor_regions_evac_time += predict_region_elapsed_time_ms(r, true); 729 } 730 return survivor_regions_evac_time; 731 } 732 733 void G1CollectorPolicy::check_prediction_validity() { 734 guarantee( adaptive_young_list_length(), "should not call this otherwise" ); 735 736 size_t rs_lengths = _g1->young_list()->sampled_rs_lengths(); 737 if (rs_lengths > _rs_lengths_prediction) { 738 // add 10% to avoid having to recalculate often 739 size_t rs_lengths_prediction = rs_lengths * 1100 / 1000; 740 calculate_young_list_target_length(rs_lengths_prediction); 741 } 742 } 743 744 HeapWord* G1CollectorPolicy::mem_allocate_work(size_t size, 745 bool is_tlab, 746 bool* gc_overhead_limit_was_exceeded) { 747 guarantee(false, "Not using this policy feature yet."); 748 return NULL; 749 } 750 751 // This method controls how a collector handles one or more 752 // of its generations being fully allocated. 753 HeapWord* G1CollectorPolicy::satisfy_failed_allocation(size_t size, 754 bool is_tlab) { 755 guarantee(false, "Not using this policy feature yet."); 756 return NULL; 757 } 758 759 760 #ifndef PRODUCT 761 bool G1CollectorPolicy::verify_young_ages() { 762 HeapRegion* head = _g1->young_list()->first_region(); 763 return 764 verify_young_ages(head, _short_lived_surv_rate_group); 765 // also call verify_young_ages on any additional surv rate groups 766 } 767 768 bool 769 G1CollectorPolicy::verify_young_ages(HeapRegion* head, 770 SurvRateGroup *surv_rate_group) { 771 guarantee( surv_rate_group != NULL, "pre-condition" ); 772 773 const char* name = surv_rate_group->name(); 774 bool ret = true; 775 int prev_age = -1; 776 777 for (HeapRegion* curr = head; 778 curr != NULL; 779 curr = curr->get_next_young_region()) { 780 SurvRateGroup* group = curr->surv_rate_group(); 781 if (group == NULL && !curr->is_survivor()) { 782 gclog_or_tty->print_cr("## %s: encountered NULL surv_rate_group", name); 783 ret = false; 784 } 785 786 if (surv_rate_group == group) { 787 int age = curr->age_in_surv_rate_group(); 788 789 if (age < 0) { 790 gclog_or_tty->print_cr("## %s: encountered negative age", name); 791 ret = false; 792 } 793 794 if (age <= prev_age) { 795 gclog_or_tty->print_cr("## %s: region ages are not strictly increasing " 796 "(%d, %d)", name, age, prev_age); 797 ret = false; 798 } 799 prev_age = age; 800 } 801 } 802 803 return ret; 804 } 805 #endif // PRODUCT 806 807 void G1CollectorPolicy::record_full_collection_start() { 808 _cur_collection_start_sec = os::elapsedTime(); 809 // Release the future to-space so that it is available for compaction into. 810 _g1->set_full_collection(); 811 } 812 813 void G1CollectorPolicy::record_full_collection_end() { 814 // Consider this like a collection pause for the purposes of allocation 815 // since last pause. 816 double end_sec = os::elapsedTime(); 817 double full_gc_time_sec = end_sec - _cur_collection_start_sec; 818 double full_gc_time_ms = full_gc_time_sec * 1000.0; 819 820 _all_full_gc_times_ms->add(full_gc_time_ms); 821 822 update_recent_gc_times(end_sec, full_gc_time_ms); 823 824 _g1->clear_full_collection(); 825 826 // "Nuke" the heuristics that control the fully/partially young GC 827 // transitions and make sure we start with fully young GCs after the 828 // Full GC. 829 set_full_young_gcs(true); 830 _last_full_young_gc = false; 831 _should_revert_to_full_young_gcs = false; 832 clear_initiate_conc_mark_if_possible(); 833 clear_during_initial_mark_pause(); 834 _known_garbage_bytes = 0; 835 _known_garbage_ratio = 0.0; 836 _in_marking_window = false; 837 _in_marking_window_im = false; 838 839 _short_lived_surv_rate_group->start_adding_regions(); 840 // also call this on any additional surv rate groups 841 842 record_survivor_regions(0, NULL, NULL); 843 844 _prev_region_num_young = _region_num_young; 845 _prev_region_num_tenured = _region_num_tenured; 846 847 _free_regions_at_end_of_collection = _g1->free_regions(); 848 // Reset survivors SurvRateGroup. 849 _survivor_surv_rate_group->reset(); 850 calculate_young_list_min_length(); 851 calculate_young_list_target_length(); 852 } 853 854 void G1CollectorPolicy::record_stop_world_start() { 855 _stop_world_start = os::elapsedTime(); 856 } 857 858 void G1CollectorPolicy::record_collection_pause_start(double start_time_sec, 859 size_t start_used) { 860 if (PrintGCDetails) { 861 gclog_or_tty->stamp(PrintGCTimeStamps); 862 gclog_or_tty->print("[GC pause"); 863 gclog_or_tty->print(" (%s)", full_young_gcs() ? "young" : "partial"); 864 } 865 866 assert(_g1->used() == _g1->recalculate_used(), 867 err_msg("sanity, used: "SIZE_FORMAT" recalculate_used: "SIZE_FORMAT, 868 _g1->used(), _g1->recalculate_used())); 869 870 double s_w_t_ms = (start_time_sec - _stop_world_start) * 1000.0; 871 _all_stop_world_times_ms->add(s_w_t_ms); 872 _stop_world_start = 0.0; 873 874 _cur_collection_start_sec = start_time_sec; 875 _cur_collection_pause_used_at_start_bytes = start_used; 876 _cur_collection_pause_used_regions_at_start = _g1->used_regions(); 877 _pending_cards = _g1->pending_card_num(); 878 _max_pending_cards = _g1->max_pending_card_num(); 879 880 _bytes_in_collection_set_before_gc = 0; 881 _bytes_copied_during_gc = 0; 882 883 YoungList* young_list = _g1->young_list(); 884 _eden_bytes_before_gc = young_list->eden_used_bytes(); 885 _survivor_bytes_before_gc = young_list->survivor_used_bytes(); 886 _capacity_before_gc = _g1->capacity(); 887 888 #ifdef DEBUG 889 // initialise these to something well known so that we can spot 890 // if they are not set properly 891 892 for (int i = 0; i < _parallel_gc_threads; ++i) { 893 _par_last_gc_worker_start_times_ms[i] = -1234.0; 894 _par_last_ext_root_scan_times_ms[i] = -1234.0; 895 _par_last_mark_stack_scan_times_ms[i] = -1234.0; 896 _par_last_update_rs_times_ms[i] = -1234.0; 897 _par_last_update_rs_processed_buffers[i] = -1234.0; 898 _par_last_scan_rs_times_ms[i] = -1234.0; 899 _par_last_obj_copy_times_ms[i] = -1234.0; 900 _par_last_termination_times_ms[i] = -1234.0; 901 _par_last_termination_attempts[i] = -1234.0; 902 _par_last_gc_worker_end_times_ms[i] = -1234.0; 903 _par_last_gc_worker_times_ms[i] = -1234.0; 904 } 905 #endif 906 907 for (int i = 0; i < _aux_num; ++i) { 908 _cur_aux_times_ms[i] = 0.0; 909 _cur_aux_times_set[i] = false; 910 } 911 912 _satb_drain_time_set = false; 913 _last_satb_drain_processed_buffers = -1; 914 915 _last_young_gc_full = false; 916 917 // do that for any other surv rate groups 918 _short_lived_surv_rate_group->stop_adding_regions(); 919 _survivors_age_table.clear(); 920 921 assert( verify_young_ages(), "region age verification" ); 922 } 923 924 void G1CollectorPolicy::record_mark_closure_time(double mark_closure_time_ms) { 925 _mark_closure_time_ms = mark_closure_time_ms; 926 } 927 928 void G1CollectorPolicy::record_concurrent_mark_init_start() { 929 _mark_init_start_sec = os::elapsedTime(); 930 } 931 932 void G1CollectorPolicy::record_concurrent_mark_init_end_pre(double 933 mark_init_elapsed_time_ms) { 934 _during_marking = true; 935 assert(!initiate_conc_mark_if_possible(), "we should have cleared it by now"); 936 clear_during_initial_mark_pause(); 937 _cur_mark_stop_world_time_ms = mark_init_elapsed_time_ms; 938 } 939 940 void G1CollectorPolicy::record_concurrent_mark_init_end() { 941 double end_time_sec = os::elapsedTime(); 942 double elapsed_time_ms = (end_time_sec - _mark_init_start_sec) * 1000.0; 943 _concurrent_mark_init_times_ms->add(elapsed_time_ms); 944 record_concurrent_mark_init_end_pre(elapsed_time_ms); 945 946 _mmu_tracker->add_pause(_mark_init_start_sec, end_time_sec, true); 947 } 948 949 void G1CollectorPolicy::record_concurrent_mark_remark_start() { 950 _mark_remark_start_sec = os::elapsedTime(); 951 _during_marking = false; 952 } 953 954 void G1CollectorPolicy::record_concurrent_mark_remark_end() { 955 double end_time_sec = os::elapsedTime(); 956 double elapsed_time_ms = (end_time_sec - _mark_remark_start_sec)*1000.0; 957 _concurrent_mark_remark_times_ms->add(elapsed_time_ms); 958 _cur_mark_stop_world_time_ms += elapsed_time_ms; 959 _prev_collection_pause_end_ms += elapsed_time_ms; 960 961 _mmu_tracker->add_pause(_mark_remark_start_sec, end_time_sec, true); 962 } 963 964 void G1CollectorPolicy::record_concurrent_mark_cleanup_start() { 965 _mark_cleanup_start_sec = os::elapsedTime(); 966 } 967 968 void 969 G1CollectorPolicy::record_concurrent_mark_cleanup_end(size_t freed_bytes, 970 size_t max_live_bytes) { 971 record_concurrent_mark_cleanup_end_work1(freed_bytes, max_live_bytes); 972 record_concurrent_mark_cleanup_end_work2(); 973 } 974 975 void 976 G1CollectorPolicy:: 977 record_concurrent_mark_cleanup_end_work1(size_t freed_bytes, 978 size_t max_live_bytes) { 979 if (_n_marks < 2) _n_marks++; 980 if (G1PolicyVerbose > 0) 981 gclog_or_tty->print_cr("At end of marking, max_live is " SIZE_FORMAT " MB " 982 " (of " SIZE_FORMAT " MB heap).", 983 max_live_bytes/M, _g1->capacity()/M); 984 } 985 986 // The important thing about this is that it includes "os::elapsedTime". 987 void G1CollectorPolicy::record_concurrent_mark_cleanup_end_work2() { 988 double end_time_sec = os::elapsedTime(); 989 double elapsed_time_ms = (end_time_sec - _mark_cleanup_start_sec)*1000.0; 990 _concurrent_mark_cleanup_times_ms->add(elapsed_time_ms); 991 _cur_mark_stop_world_time_ms += elapsed_time_ms; 992 _prev_collection_pause_end_ms += elapsed_time_ms; 993 994 _mmu_tracker->add_pause(_mark_cleanup_start_sec, end_time_sec, true); 995 996 _num_markings++; 997 998 // We did a marking, so reset the "since_last_mark" variables. 999 double considerConcMarkCost = 1.0; 1000 // If there are available processors, concurrent activity is free... 1001 if (Threads::number_of_non_daemon_threads() * 2 < 1002 os::active_processor_count()) { 1003 considerConcMarkCost = 0.0; 1004 } 1005 _n_pauses_at_mark_end = _n_pauses; 1006 _n_marks_since_last_pause++; 1007 } 1008 1009 void 1010 G1CollectorPolicy::record_concurrent_mark_cleanup_completed() { 1011 _should_revert_to_full_young_gcs = false; 1012 _last_full_young_gc = true; 1013 _in_marking_window = false; 1014 if (adaptive_young_list_length()) 1015 calculate_young_list_target_length(); 1016 } 1017 1018 void G1CollectorPolicy::record_concurrent_pause() { 1019 if (_stop_world_start > 0.0) { 1020 double yield_ms = (os::elapsedTime() - _stop_world_start) * 1000.0; 1021 _all_yield_times_ms->add(yield_ms); 1022 } 1023 } 1024 1025 void G1CollectorPolicy::record_concurrent_pause_end() { 1026 } 1027 1028 template<class T> 1029 T sum_of(T* sum_arr, int start, int n, int N) { 1030 T sum = (T)0; 1031 for (int i = 0; i < n; i++) { 1032 int j = (start + i) % N; 1033 sum += sum_arr[j]; 1034 } 1035 return sum; 1036 } 1037 1038 void G1CollectorPolicy::print_par_stats(int level, 1039 const char* str, 1040 double* data) { 1041 double min = data[0], max = data[0]; 1042 double total = 0.0; 1043 LineBuffer buf(level); 1044 buf.append("[%s (ms):", str); 1045 for (uint i = 0; i < ParallelGCThreads; ++i) { 1046 double val = data[i]; 1047 if (val < min) 1048 min = val; 1049 if (val > max) 1050 max = val; 1051 total += val; 1052 buf.append(" %3.1lf", val); 1053 } 1054 buf.append_and_print_cr(""); 1055 double avg = total / (double) ParallelGCThreads; 1056 buf.append_and_print_cr(" Avg: %5.1lf, Min: %5.1lf, Max: %5.1lf, Diff: %5.1lf]", 1057 avg, min, max, max - min); 1058 } 1059 1060 void G1CollectorPolicy::print_par_sizes(int level, 1061 const char* str, 1062 double* data) { 1063 double min = data[0], max = data[0]; 1064 double total = 0.0; 1065 LineBuffer buf(level); 1066 buf.append("[%s :", str); 1067 for (uint i = 0; i < ParallelGCThreads; ++i) { 1068 double val = data[i]; 1069 if (val < min) 1070 min = val; 1071 if (val > max) 1072 max = val; 1073 total += val; 1074 buf.append(" %d", (int) val); 1075 } 1076 buf.append_and_print_cr(""); 1077 double avg = total / (double) ParallelGCThreads; 1078 buf.append_and_print_cr(" Sum: %d, Avg: %d, Min: %d, Max: %d, Diff: %d]", 1079 (int)total, (int)avg, (int)min, (int)max, (int)max - (int)min); 1080 } 1081 1082 void G1CollectorPolicy::print_stats (int level, 1083 const char* str, 1084 double value) { 1085 LineBuffer(level).append_and_print_cr("[%s: %5.1lf ms]", str, value); 1086 } 1087 1088 void G1CollectorPolicy::print_stats (int level, 1089 const char* str, 1090 int value) { 1091 LineBuffer(level).append_and_print_cr("[%s: %d]", str, value); 1092 } 1093 1094 double G1CollectorPolicy::avg_value (double* data) { 1095 if (G1CollectedHeap::use_parallel_gc_threads()) { 1096 double ret = 0.0; 1097 for (uint i = 0; i < ParallelGCThreads; ++i) 1098 ret += data[i]; 1099 return ret / (double) ParallelGCThreads; 1100 } else { 1101 return data[0]; 1102 } 1103 } 1104 1105 double G1CollectorPolicy::max_value (double* data) { 1106 if (G1CollectedHeap::use_parallel_gc_threads()) { 1107 double ret = data[0]; 1108 for (uint i = 1; i < ParallelGCThreads; ++i) 1109 if (data[i] > ret) 1110 ret = data[i]; 1111 return ret; 1112 } else { 1113 return data[0]; 1114 } 1115 } 1116 1117 double G1CollectorPolicy::sum_of_values (double* data) { 1118 if (G1CollectedHeap::use_parallel_gc_threads()) { 1119 double sum = 0.0; 1120 for (uint i = 0; i < ParallelGCThreads; i++) 1121 sum += data[i]; 1122 return sum; 1123 } else { 1124 return data[0]; 1125 } 1126 } 1127 1128 double G1CollectorPolicy::max_sum (double* data1, 1129 double* data2) { 1130 double ret = data1[0] + data2[0]; 1131 1132 if (G1CollectedHeap::use_parallel_gc_threads()) { 1133 for (uint i = 1; i < ParallelGCThreads; ++i) { 1134 double data = data1[i] + data2[i]; 1135 if (data > ret) 1136 ret = data; 1137 } 1138 } 1139 return ret; 1140 } 1141 1142 // Anything below that is considered to be zero 1143 #define MIN_TIMER_GRANULARITY 0.0000001 1144 1145 void G1CollectorPolicy::record_collection_pause_end() { 1146 double end_time_sec = os::elapsedTime(); 1147 double elapsed_ms = _last_pause_time_ms; 1148 bool parallel = G1CollectedHeap::use_parallel_gc_threads(); 1149 size_t rs_size = 1150 _cur_collection_pause_used_regions_at_start - collection_set_size(); 1151 size_t cur_used_bytes = _g1->used(); 1152 assert(cur_used_bytes == _g1->recalculate_used(), "It should!"); 1153 bool last_pause_included_initial_mark = false; 1154 bool update_stats = !_g1->evacuation_failed(); 1155 1156 #ifndef PRODUCT 1157 if (G1YoungSurvRateVerbose) { 1158 gclog_or_tty->print_cr(""); 1159 _short_lived_surv_rate_group->print(); 1160 // do that for any other surv rate groups too 1161 } 1162 #endif // PRODUCT 1163 1164 last_pause_included_initial_mark = during_initial_mark_pause(); 1165 if (last_pause_included_initial_mark) 1166 record_concurrent_mark_init_end_pre(0.0); 1167 1168 size_t min_used_targ = 1169 (_g1->capacity() / 100) * InitiatingHeapOccupancyPercent; 1170 1171 1172 if (!_g1->mark_in_progress() && !_last_full_young_gc) { 1173 assert(!last_pause_included_initial_mark, "invariant"); 1174 if (cur_used_bytes > min_used_targ && 1175 cur_used_bytes > _prev_collection_pause_used_at_end_bytes) { 1176 assert(!during_initial_mark_pause(), "we should not see this here"); 1177 1178 // Note: this might have already been set, if during the last 1179 // pause we decided to start a cycle but at the beginning of 1180 // this pause we decided to postpone it. That's OK. 1181 set_initiate_conc_mark_if_possible(); 1182 } 1183 } 1184 1185 _prev_collection_pause_used_at_end_bytes = cur_used_bytes; 1186 1187 _mmu_tracker->add_pause(end_time_sec - elapsed_ms/1000.0, 1188 end_time_sec, false); 1189 1190 guarantee(_cur_collection_pause_used_regions_at_start >= 1191 collection_set_size(), 1192 "Negative RS size?"); 1193 1194 // This assert is exempted when we're doing parallel collection pauses, 1195 // because the fragmentation caused by the parallel GC allocation buffers 1196 // can lead to more memory being used during collection than was used 1197 // before. Best leave this out until the fragmentation problem is fixed. 1198 // Pauses in which evacuation failed can also lead to negative 1199 // collections, since no space is reclaimed from a region containing an 1200 // object whose evacuation failed. 1201 // Further, we're now always doing parallel collection. But I'm still 1202 // leaving this here as a placeholder for a more precise assertion later. 1203 // (DLD, 10/05.) 1204 assert((true || parallel) // Always using GC LABs now. 1205 || _g1->evacuation_failed() 1206 || _cur_collection_pause_used_at_start_bytes >= cur_used_bytes, 1207 "Negative collection"); 1208 1209 size_t freed_bytes = 1210 _cur_collection_pause_used_at_start_bytes - cur_used_bytes; 1211 size_t surviving_bytes = _collection_set_bytes_used_before - freed_bytes; 1212 1213 double survival_fraction = 1214 (double)surviving_bytes/ 1215 (double)_collection_set_bytes_used_before; 1216 1217 _n_pauses++; 1218 1219 double ext_root_scan_time = avg_value(_par_last_ext_root_scan_times_ms); 1220 double mark_stack_scan_time = avg_value(_par_last_mark_stack_scan_times_ms); 1221 double update_rs_time = avg_value(_par_last_update_rs_times_ms); 1222 double update_rs_processed_buffers = 1223 sum_of_values(_par_last_update_rs_processed_buffers); 1224 double scan_rs_time = avg_value(_par_last_scan_rs_times_ms); 1225 double obj_copy_time = avg_value(_par_last_obj_copy_times_ms); 1226 double termination_time = avg_value(_par_last_termination_times_ms); 1227 1228 double parallel_known_time = update_rs_time + 1229 ext_root_scan_time + 1230 mark_stack_scan_time + 1231 scan_rs_time + 1232 obj_copy_time + 1233 termination_time; 1234 1235 double parallel_other_time = _cur_collection_par_time_ms - parallel_known_time; 1236 1237 PauseSummary* summary = _summary; 1238 1239 if (update_stats) { 1240 _recent_rs_scan_times_ms->add(scan_rs_time); 1241 _recent_pause_times_ms->add(elapsed_ms); 1242 _recent_rs_sizes->add(rs_size); 1243 1244 MainBodySummary* body_summary = summary->main_body_summary(); 1245 guarantee(body_summary != NULL, "should not be null!"); 1246 1247 if (_satb_drain_time_set) 1248 body_summary->record_satb_drain_time_ms(_cur_satb_drain_time_ms); 1249 else 1250 body_summary->record_satb_drain_time_ms(0.0); 1251 1252 body_summary->record_ext_root_scan_time_ms(ext_root_scan_time); 1253 body_summary->record_mark_stack_scan_time_ms(mark_stack_scan_time); 1254 body_summary->record_update_rs_time_ms(update_rs_time); 1255 body_summary->record_scan_rs_time_ms(scan_rs_time); 1256 body_summary->record_obj_copy_time_ms(obj_copy_time); 1257 if (parallel) { 1258 body_summary->record_parallel_time_ms(_cur_collection_par_time_ms); 1259 body_summary->record_clear_ct_time_ms(_cur_clear_ct_time_ms); 1260 body_summary->record_termination_time_ms(termination_time); 1261 body_summary->record_parallel_other_time_ms(parallel_other_time); 1262 } 1263 body_summary->record_mark_closure_time_ms(_mark_closure_time_ms); 1264 1265 // We exempt parallel collection from this check because Alloc Buffer 1266 // fragmentation can produce negative collections. Same with evac 1267 // failure. 1268 // Further, we're now always doing parallel collection. But I'm still 1269 // leaving this here as a placeholder for a more precise assertion later. 1270 // (DLD, 10/05. 1271 assert((true || parallel) 1272 || _g1->evacuation_failed() 1273 || surviving_bytes <= _collection_set_bytes_used_before, 1274 "Or else negative collection!"); 1275 _recent_CS_bytes_used_before->add(_collection_set_bytes_used_before); 1276 _recent_CS_bytes_surviving->add(surviving_bytes); 1277 1278 // this is where we update the allocation rate of the application 1279 double app_time_ms = 1280 (_cur_collection_start_sec * 1000.0 - _prev_collection_pause_end_ms); 1281 if (app_time_ms < MIN_TIMER_GRANULARITY) { 1282 // This usually happens due to the timer not having the required 1283 // granularity. Some Linuxes are the usual culprits. 1284 // We'll just set it to something (arbitrarily) small. 1285 app_time_ms = 1.0; 1286 } 1287 size_t regions_allocated = 1288 (_region_num_young - _prev_region_num_young) + 1289 (_region_num_tenured - _prev_region_num_tenured); 1290 double alloc_rate_ms = (double) regions_allocated / app_time_ms; 1291 _alloc_rate_ms_seq->add(alloc_rate_ms); 1292 _prev_region_num_young = _region_num_young; 1293 _prev_region_num_tenured = _region_num_tenured; 1294 1295 double interval_ms = 1296 (end_time_sec - _recent_prev_end_times_for_all_gcs_sec->oldest()) * 1000.0; 1297 update_recent_gc_times(end_time_sec, elapsed_ms); 1298 _recent_avg_pause_time_ratio = _recent_gc_times_ms->sum()/interval_ms; 1299 if (recent_avg_pause_time_ratio() < 0.0 || 1300 (recent_avg_pause_time_ratio() - 1.0 > 0.0)) { 1301 #ifndef PRODUCT 1302 // Dump info to allow post-facto debugging 1303 gclog_or_tty->print_cr("recent_avg_pause_time_ratio() out of bounds"); 1304 gclog_or_tty->print_cr("-------------------------------------------"); 1305 gclog_or_tty->print_cr("Recent GC Times (ms):"); 1306 _recent_gc_times_ms->dump(); 1307 gclog_or_tty->print_cr("(End Time=%3.3f) Recent GC End Times (s):", end_time_sec); 1308 _recent_prev_end_times_for_all_gcs_sec->dump(); 1309 gclog_or_tty->print_cr("GC = %3.3f, Interval = %3.3f, Ratio = %3.3f", 1310 _recent_gc_times_ms->sum(), interval_ms, recent_avg_pause_time_ratio()); 1311 // In debug mode, terminate the JVM if the user wants to debug at this point. 1312 assert(!G1FailOnFPError, "Debugging data for CR 6898948 has been dumped above"); 1313 #endif // !PRODUCT 1314 // Clip ratio between 0.0 and 1.0, and continue. This will be fixed in 1315 // CR 6902692 by redoing the manner in which the ratio is incrementally computed. 1316 if (_recent_avg_pause_time_ratio < 0.0) { 1317 _recent_avg_pause_time_ratio = 0.0; 1318 } else { 1319 assert(_recent_avg_pause_time_ratio - 1.0 > 0.0, "Ctl-point invariant"); 1320 _recent_avg_pause_time_ratio = 1.0; 1321 } 1322 } 1323 } 1324 1325 if (G1PolicyVerbose > 1) { 1326 gclog_or_tty->print_cr(" Recording collection pause(%d)", _n_pauses); 1327 } 1328 1329 if (G1PolicyVerbose > 1) { 1330 gclog_or_tty->print_cr(" ET: %10.6f ms (avg: %10.6f ms)\n" 1331 " ET-RS: %10.6f ms (avg: %10.6f ms)\n" 1332 " |RS|: " SIZE_FORMAT, 1333 elapsed_ms, recent_avg_time_for_pauses_ms(), 1334 scan_rs_time, recent_avg_time_for_rs_scan_ms(), 1335 rs_size); 1336 1337 gclog_or_tty->print_cr(" Used at start: " SIZE_FORMAT"K" 1338 " At end " SIZE_FORMAT "K\n" 1339 " garbage : " SIZE_FORMAT "K" 1340 " of " SIZE_FORMAT "K\n" 1341 " survival : %6.2f%% (%6.2f%% avg)", 1342 _cur_collection_pause_used_at_start_bytes/K, 1343 _g1->used()/K, freed_bytes/K, 1344 _collection_set_bytes_used_before/K, 1345 survival_fraction*100.0, 1346 recent_avg_survival_fraction()*100.0); 1347 gclog_or_tty->print_cr(" Recent %% gc pause time: %6.2f", 1348 recent_avg_pause_time_ratio() * 100.0); 1349 } 1350 1351 double other_time_ms = elapsed_ms; 1352 1353 if (_satb_drain_time_set) { 1354 other_time_ms -= _cur_satb_drain_time_ms; 1355 } 1356 1357 if (parallel) { 1358 other_time_ms -= _cur_collection_par_time_ms + _cur_clear_ct_time_ms; 1359 } else { 1360 other_time_ms -= 1361 update_rs_time + 1362 ext_root_scan_time + mark_stack_scan_time + 1363 scan_rs_time + obj_copy_time; 1364 } 1365 1366 if (PrintGCDetails) { 1367 gclog_or_tty->print_cr("%s, %1.8lf secs]", 1368 (last_pause_included_initial_mark) ? " (initial-mark)" : "", 1369 elapsed_ms / 1000.0); 1370 1371 if (_satb_drain_time_set) { 1372 print_stats(1, "SATB Drain Time", _cur_satb_drain_time_ms); 1373 } 1374 if (_last_satb_drain_processed_buffers >= 0) { 1375 print_stats(2, "Processed Buffers", _last_satb_drain_processed_buffers); 1376 } 1377 if (parallel) { 1378 print_stats(1, "Parallel Time", _cur_collection_par_time_ms); 1379 print_par_stats(2, "GC Worker Start Time", _par_last_gc_worker_start_times_ms); 1380 print_par_stats(2, "Update RS", _par_last_update_rs_times_ms); 1381 print_par_sizes(3, "Processed Buffers", _par_last_update_rs_processed_buffers); 1382 print_par_stats(2, "Ext Root Scanning", _par_last_ext_root_scan_times_ms); 1383 print_par_stats(2, "Mark Stack Scanning", _par_last_mark_stack_scan_times_ms); 1384 print_par_stats(2, "Scan RS", _par_last_scan_rs_times_ms); 1385 print_par_stats(2, "Object Copy", _par_last_obj_copy_times_ms); 1386 print_par_stats(2, "Termination", _par_last_termination_times_ms); 1387 print_par_sizes(3, "Termination Attempts", _par_last_termination_attempts); 1388 print_par_stats(2, "GC Worker End Time", _par_last_gc_worker_end_times_ms); 1389 1390 for (int i = 0; i < _parallel_gc_threads; i++) { 1391 _par_last_gc_worker_times_ms[i] = _par_last_gc_worker_end_times_ms[i] - _par_last_gc_worker_start_times_ms[i]; 1392 } 1393 print_par_stats(2, "GC Worker Times", _par_last_gc_worker_times_ms); 1394 1395 print_stats(2, "Parallel Other", parallel_other_time); 1396 print_stats(1, "Clear CT", _cur_clear_ct_time_ms); 1397 } else { 1398 print_stats(1, "Update RS", update_rs_time); 1399 print_stats(2, "Processed Buffers", 1400 (int)update_rs_processed_buffers); 1401 print_stats(1, "Ext Root Scanning", ext_root_scan_time); 1402 print_stats(1, "Mark Stack Scanning", mark_stack_scan_time); 1403 print_stats(1, "Scan RS", scan_rs_time); 1404 print_stats(1, "Object Copying", obj_copy_time); 1405 } 1406 #ifndef PRODUCT 1407 print_stats(1, "Cur Clear CC", _cur_clear_cc_time_ms); 1408 print_stats(1, "Cum Clear CC", _cum_clear_cc_time_ms); 1409 print_stats(1, "Min Clear CC", _min_clear_cc_time_ms); 1410 print_stats(1, "Max Clear CC", _max_clear_cc_time_ms); 1411 if (_num_cc_clears > 0) { 1412 print_stats(1, "Avg Clear CC", _cum_clear_cc_time_ms / ((double)_num_cc_clears)); 1413 } 1414 #endif 1415 print_stats(1, "Other", other_time_ms); 1416 print_stats(2, "Choose CSet", _recorded_young_cset_choice_time_ms); 1417 1418 for (int i = 0; i < _aux_num; ++i) { 1419 if (_cur_aux_times_set[i]) { 1420 char buffer[96]; 1421 sprintf(buffer, "Aux%d", i); 1422 print_stats(1, buffer, _cur_aux_times_ms[i]); 1423 } 1424 } 1425 } 1426 1427 _all_pause_times_ms->add(elapsed_ms); 1428 if (update_stats) { 1429 summary->record_total_time_ms(elapsed_ms); 1430 summary->record_other_time_ms(other_time_ms); 1431 } 1432 for (int i = 0; i < _aux_num; ++i) 1433 if (_cur_aux_times_set[i]) 1434 _all_aux_times_ms[i].add(_cur_aux_times_ms[i]); 1435 1436 // Reset marks-between-pauses counter. 1437 _n_marks_since_last_pause = 0; 1438 1439 // Update the efficiency-since-mark vars. 1440 double proc_ms = elapsed_ms * (double) _parallel_gc_threads; 1441 if (elapsed_ms < MIN_TIMER_GRANULARITY) { 1442 // This usually happens due to the timer not having the required 1443 // granularity. Some Linuxes are the usual culprits. 1444 // We'll just set it to something (arbitrarily) small. 1445 proc_ms = 1.0; 1446 } 1447 double cur_efficiency = (double) freed_bytes / proc_ms; 1448 1449 bool new_in_marking_window = _in_marking_window; 1450 bool new_in_marking_window_im = false; 1451 if (during_initial_mark_pause()) { 1452 new_in_marking_window = true; 1453 new_in_marking_window_im = true; 1454 } 1455 1456 if (_last_full_young_gc) { 1457 set_full_young_gcs(false); 1458 _last_full_young_gc = false; 1459 } 1460 1461 if ( !_last_young_gc_full ) { 1462 if ( _should_revert_to_full_young_gcs || 1463 _known_garbage_ratio < 0.05 || 1464 (adaptive_young_list_length() && 1465 (get_gc_eff_factor() * cur_efficiency < predict_young_gc_eff())) ) { 1466 set_full_young_gcs(true); 1467 } 1468 } 1469 _should_revert_to_full_young_gcs = false; 1470 1471 if (_last_young_gc_full && !_during_marking) { 1472 _young_gc_eff_seq->add(cur_efficiency); 1473 } 1474 1475 _short_lived_surv_rate_group->start_adding_regions(); 1476 // do that for any other surv rate groupsx 1477 1478 // <NEW PREDICTION> 1479 1480 if (update_stats) { 1481 double pause_time_ms = elapsed_ms; 1482 1483 size_t diff = 0; 1484 if (_max_pending_cards >= _pending_cards) 1485 diff = _max_pending_cards - _pending_cards; 1486 _pending_card_diff_seq->add((double) diff); 1487 1488 double cost_per_card_ms = 0.0; 1489 if (_pending_cards > 0) { 1490 cost_per_card_ms = update_rs_time / (double) _pending_cards; 1491 _cost_per_card_ms_seq->add(cost_per_card_ms); 1492 } 1493 1494 size_t cards_scanned = _g1->cards_scanned(); 1495 1496 double cost_per_entry_ms = 0.0; 1497 if (cards_scanned > 10) { 1498 cost_per_entry_ms = scan_rs_time / (double) cards_scanned; 1499 if (_last_young_gc_full) 1500 _cost_per_entry_ms_seq->add(cost_per_entry_ms); 1501 else 1502 _partially_young_cost_per_entry_ms_seq->add(cost_per_entry_ms); 1503 } 1504 1505 if (_max_rs_lengths > 0) { 1506 double cards_per_entry_ratio = 1507 (double) cards_scanned / (double) _max_rs_lengths; 1508 if (_last_young_gc_full) 1509 _fully_young_cards_per_entry_ratio_seq->add(cards_per_entry_ratio); 1510 else 1511 _partially_young_cards_per_entry_ratio_seq->add(cards_per_entry_ratio); 1512 } 1513 1514 size_t rs_length_diff = _max_rs_lengths - _recorded_rs_lengths; 1515 if (rs_length_diff >= 0) 1516 _rs_length_diff_seq->add((double) rs_length_diff); 1517 1518 size_t copied_bytes = surviving_bytes; 1519 double cost_per_byte_ms = 0.0; 1520 if (copied_bytes > 0) { 1521 cost_per_byte_ms = obj_copy_time / (double) copied_bytes; 1522 if (_in_marking_window) 1523 _cost_per_byte_ms_during_cm_seq->add(cost_per_byte_ms); 1524 else 1525 _cost_per_byte_ms_seq->add(cost_per_byte_ms); 1526 } 1527 1528 double all_other_time_ms = pause_time_ms - 1529 (update_rs_time + scan_rs_time + obj_copy_time + 1530 _mark_closure_time_ms + termination_time); 1531 1532 double young_other_time_ms = 0.0; 1533 if (_recorded_young_regions > 0) { 1534 young_other_time_ms = 1535 _recorded_young_cset_choice_time_ms + 1536 _recorded_young_free_cset_time_ms; 1537 _young_other_cost_per_region_ms_seq->add(young_other_time_ms / 1538 (double) _recorded_young_regions); 1539 } 1540 double non_young_other_time_ms = 0.0; 1541 if (_recorded_non_young_regions > 0) { 1542 non_young_other_time_ms = 1543 _recorded_non_young_cset_choice_time_ms + 1544 _recorded_non_young_free_cset_time_ms; 1545 1546 _non_young_other_cost_per_region_ms_seq->add(non_young_other_time_ms / 1547 (double) _recorded_non_young_regions); 1548 } 1549 1550 double constant_other_time_ms = all_other_time_ms - 1551 (young_other_time_ms + non_young_other_time_ms); 1552 _constant_other_time_ms_seq->add(constant_other_time_ms); 1553 1554 double survival_ratio = 0.0; 1555 if (_bytes_in_collection_set_before_gc > 0) { 1556 survival_ratio = (double) _bytes_copied_during_gc / 1557 (double) _bytes_in_collection_set_before_gc; 1558 } 1559 1560 _pending_cards_seq->add((double) _pending_cards); 1561 _scanned_cards_seq->add((double) cards_scanned); 1562 _rs_lengths_seq->add((double) _max_rs_lengths); 1563 1564 double expensive_region_limit_ms = 1565 (double) MaxGCPauseMillis - predict_constant_other_time_ms(); 1566 if (expensive_region_limit_ms < 0.0) { 1567 // this means that the other time was predicted to be longer than 1568 // than the max pause time 1569 expensive_region_limit_ms = (double) MaxGCPauseMillis; 1570 } 1571 _expensive_region_limit_ms = expensive_region_limit_ms; 1572 1573 if (PREDICTIONS_VERBOSE) { 1574 gclog_or_tty->print_cr(""); 1575 gclog_or_tty->print_cr("PREDICTIONS %1.4lf %d " 1576 "REGIONS %d %d %d " 1577 "PENDING_CARDS %d %d " 1578 "CARDS_SCANNED %d %d " 1579 "RS_LENGTHS %d %d " 1580 "RS_UPDATE %1.6lf %1.6lf RS_SCAN %1.6lf %1.6lf " 1581 "SURVIVAL_RATIO %1.6lf %1.6lf " 1582 "OBJECT_COPY %1.6lf %1.6lf OTHER_CONSTANT %1.6lf %1.6lf " 1583 "OTHER_YOUNG %1.6lf %1.6lf " 1584 "OTHER_NON_YOUNG %1.6lf %1.6lf " 1585 "VTIME_DIFF %1.6lf TERMINATION %1.6lf " 1586 "ELAPSED %1.6lf %1.6lf ", 1587 _cur_collection_start_sec, 1588 (!_last_young_gc_full) ? 2 : 1589 (last_pause_included_initial_mark) ? 1 : 0, 1590 _recorded_region_num, 1591 _recorded_young_regions, 1592 _recorded_non_young_regions, 1593 _predicted_pending_cards, _pending_cards, 1594 _predicted_cards_scanned, cards_scanned, 1595 _predicted_rs_lengths, _max_rs_lengths, 1596 _predicted_rs_update_time_ms, update_rs_time, 1597 _predicted_rs_scan_time_ms, scan_rs_time, 1598 _predicted_survival_ratio, survival_ratio, 1599 _predicted_object_copy_time_ms, obj_copy_time, 1600 _predicted_constant_other_time_ms, constant_other_time_ms, 1601 _predicted_young_other_time_ms, young_other_time_ms, 1602 _predicted_non_young_other_time_ms, 1603 non_young_other_time_ms, 1604 _vtime_diff_ms, termination_time, 1605 _predicted_pause_time_ms, elapsed_ms); 1606 } 1607 1608 if (G1PolicyVerbose > 0) { 1609 gclog_or_tty->print_cr("Pause Time, predicted: %1.4lfms (predicted %s), actual: %1.4lfms", 1610 _predicted_pause_time_ms, 1611 (_within_target) ? "within" : "outside", 1612 elapsed_ms); 1613 } 1614 1615 } 1616 1617 _in_marking_window = new_in_marking_window; 1618 _in_marking_window_im = new_in_marking_window_im; 1619 _free_regions_at_end_of_collection = _g1->free_regions(); 1620 calculate_young_list_min_length(); 1621 calculate_young_list_target_length(); 1622 1623 // Note that _mmu_tracker->max_gc_time() returns the time in seconds. 1624 double update_rs_time_goal_ms = _mmu_tracker->max_gc_time() * MILLIUNITS * G1RSetUpdatingPauseTimePercent / 100.0; 1625 adjust_concurrent_refinement(update_rs_time, update_rs_processed_buffers, update_rs_time_goal_ms); 1626 // </NEW PREDICTION> 1627 } 1628 1629 #define EXT_SIZE_FORMAT "%d%s" 1630 #define EXT_SIZE_PARAMS(bytes) \ 1631 byte_size_in_proper_unit((bytes)), \ 1632 proper_unit_for_byte_size((bytes)) 1633 1634 void G1CollectorPolicy::print_heap_transition() { 1635 if (PrintGCDetails) { 1636 YoungList* young_list = _g1->young_list(); 1637 size_t eden_bytes = young_list->eden_used_bytes(); 1638 size_t survivor_bytes = young_list->survivor_used_bytes(); 1639 size_t used_before_gc = _cur_collection_pause_used_at_start_bytes; 1640 size_t used = _g1->used(); 1641 size_t capacity = _g1->capacity(); 1642 1643 gclog_or_tty->print_cr( 1644 " [Eden: "EXT_SIZE_FORMAT"->"EXT_SIZE_FORMAT" " 1645 "Survivors: "EXT_SIZE_FORMAT"->"EXT_SIZE_FORMAT" " 1646 "Heap: "EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")->" 1647 EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")]", 1648 EXT_SIZE_PARAMS(_eden_bytes_before_gc), 1649 EXT_SIZE_PARAMS(eden_bytes), 1650 EXT_SIZE_PARAMS(_survivor_bytes_before_gc), 1651 EXT_SIZE_PARAMS(survivor_bytes), 1652 EXT_SIZE_PARAMS(used_before_gc), 1653 EXT_SIZE_PARAMS(_capacity_before_gc), 1654 EXT_SIZE_PARAMS(used), 1655 EXT_SIZE_PARAMS(capacity)); 1656 } else if (PrintGC) { 1657 _g1->print_size_transition(gclog_or_tty, 1658 _cur_collection_pause_used_at_start_bytes, 1659 _g1->used(), _g1->capacity()); 1660 } 1661 } 1662 1663 // <NEW PREDICTION> 1664 1665 void G1CollectorPolicy::adjust_concurrent_refinement(double update_rs_time, 1666 double update_rs_processed_buffers, 1667 double goal_ms) { 1668 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); 1669 ConcurrentG1Refine *cg1r = G1CollectedHeap::heap()->concurrent_g1_refine(); 1670 1671 if (G1UseAdaptiveConcRefinement) { 1672 const int k_gy = 3, k_gr = 6; 1673 const double inc_k = 1.1, dec_k = 0.9; 1674 1675 int g = cg1r->green_zone(); 1676 if (update_rs_time > goal_ms) { 1677 g = (int)(g * dec_k); // Can become 0, that's OK. That would mean a mutator-only processing. 1678 } else { 1679 if (update_rs_time < goal_ms && update_rs_processed_buffers > g) { 1680 g = (int)MAX2(g * inc_k, g + 1.0); 1681 } 1682 } 1683 // Change the refinement threads params 1684 cg1r->set_green_zone(g); 1685 cg1r->set_yellow_zone(g * k_gy); 1686 cg1r->set_red_zone(g * k_gr); 1687 cg1r->reinitialize_threads(); 1688 1689 int processing_threshold_delta = MAX2((int)(cg1r->green_zone() * sigma()), 1); 1690 int processing_threshold = MIN2(cg1r->green_zone() + processing_threshold_delta, 1691 cg1r->yellow_zone()); 1692 // Change the barrier params 1693 dcqs.set_process_completed_threshold(processing_threshold); 1694 dcqs.set_max_completed_queue(cg1r->red_zone()); 1695 } 1696 1697 int curr_queue_size = dcqs.completed_buffers_num(); 1698 if (curr_queue_size >= cg1r->yellow_zone()) { 1699 dcqs.set_completed_queue_padding(curr_queue_size); 1700 } else { 1701 dcqs.set_completed_queue_padding(0); 1702 } 1703 dcqs.notify_if_necessary(); 1704 } 1705 1706 double 1707 G1CollectorPolicy:: 1708 predict_young_collection_elapsed_time_ms(size_t adjustment) { 1709 guarantee( adjustment == 0 || adjustment == 1, "invariant" ); 1710 1711 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1712 size_t young_num = g1h->young_list()->length(); 1713 if (young_num == 0) 1714 return 0.0; 1715 1716 young_num += adjustment; 1717 size_t pending_cards = predict_pending_cards(); 1718 size_t rs_lengths = g1h->young_list()->sampled_rs_lengths() + 1719 predict_rs_length_diff(); 1720 size_t card_num; 1721 if (full_young_gcs()) 1722 card_num = predict_young_card_num(rs_lengths); 1723 else 1724 card_num = predict_non_young_card_num(rs_lengths); 1725 size_t young_byte_size = young_num * HeapRegion::GrainBytes; 1726 double accum_yg_surv_rate = 1727 _short_lived_surv_rate_group->accum_surv_rate(adjustment); 1728 1729 size_t bytes_to_copy = 1730 (size_t) (accum_yg_surv_rate * (double) HeapRegion::GrainBytes); 1731 1732 return 1733 predict_rs_update_time_ms(pending_cards) + 1734 predict_rs_scan_time_ms(card_num) + 1735 predict_object_copy_time_ms(bytes_to_copy) + 1736 predict_young_other_time_ms(young_num) + 1737 predict_constant_other_time_ms(); 1738 } 1739 1740 double 1741 G1CollectorPolicy::predict_base_elapsed_time_ms(size_t pending_cards) { 1742 size_t rs_length = predict_rs_length_diff(); 1743 size_t card_num; 1744 if (full_young_gcs()) 1745 card_num = predict_young_card_num(rs_length); 1746 else 1747 card_num = predict_non_young_card_num(rs_length); 1748 return predict_base_elapsed_time_ms(pending_cards, card_num); 1749 } 1750 1751 double 1752 G1CollectorPolicy::predict_base_elapsed_time_ms(size_t pending_cards, 1753 size_t scanned_cards) { 1754 return 1755 predict_rs_update_time_ms(pending_cards) + 1756 predict_rs_scan_time_ms(scanned_cards) + 1757 predict_constant_other_time_ms(); 1758 } 1759 1760 double 1761 G1CollectorPolicy::predict_region_elapsed_time_ms(HeapRegion* hr, 1762 bool young) { 1763 size_t rs_length = hr->rem_set()->occupied(); 1764 size_t card_num; 1765 if (full_young_gcs()) 1766 card_num = predict_young_card_num(rs_length); 1767 else 1768 card_num = predict_non_young_card_num(rs_length); 1769 size_t bytes_to_copy = predict_bytes_to_copy(hr); 1770 1771 double region_elapsed_time_ms = 1772 predict_rs_scan_time_ms(card_num) + 1773 predict_object_copy_time_ms(bytes_to_copy); 1774 1775 if (young) 1776 region_elapsed_time_ms += predict_young_other_time_ms(1); 1777 else 1778 region_elapsed_time_ms += predict_non_young_other_time_ms(1); 1779 1780 return region_elapsed_time_ms; 1781 } 1782 1783 size_t 1784 G1CollectorPolicy::predict_bytes_to_copy(HeapRegion* hr) { 1785 size_t bytes_to_copy; 1786 if (hr->is_marked()) 1787 bytes_to_copy = hr->max_live_bytes(); 1788 else { 1789 guarantee( hr->is_young() && hr->age_in_surv_rate_group() != -1, 1790 "invariant" ); 1791 int age = hr->age_in_surv_rate_group(); 1792 double yg_surv_rate = predict_yg_surv_rate(age, hr->surv_rate_group()); 1793 bytes_to_copy = (size_t) ((double) hr->used() * yg_surv_rate); 1794 } 1795 1796 return bytes_to_copy; 1797 } 1798 1799 void 1800 G1CollectorPolicy::start_recording_regions() { 1801 _recorded_rs_lengths = 0; 1802 _recorded_young_regions = 0; 1803 _recorded_non_young_regions = 0; 1804 1805 #if PREDICTIONS_VERBOSE 1806 _recorded_marked_bytes = 0; 1807 _recorded_young_bytes = 0; 1808 _predicted_bytes_to_copy = 0; 1809 _predicted_rs_lengths = 0; 1810 _predicted_cards_scanned = 0; 1811 #endif // PREDICTIONS_VERBOSE 1812 } 1813 1814 void 1815 G1CollectorPolicy::record_cset_region_info(HeapRegion* hr, bool young) { 1816 #if PREDICTIONS_VERBOSE 1817 if (!young) { 1818 _recorded_marked_bytes += hr->max_live_bytes(); 1819 } 1820 _predicted_bytes_to_copy += predict_bytes_to_copy(hr); 1821 #endif // PREDICTIONS_VERBOSE 1822 1823 size_t rs_length = hr->rem_set()->occupied(); 1824 _recorded_rs_lengths += rs_length; 1825 } 1826 1827 void 1828 G1CollectorPolicy::record_non_young_cset_region(HeapRegion* hr) { 1829 assert(!hr->is_young(), "should not call this"); 1830 ++_recorded_non_young_regions; 1831 record_cset_region_info(hr, false); 1832 } 1833 1834 void 1835 G1CollectorPolicy::set_recorded_young_regions(size_t n_regions) { 1836 _recorded_young_regions = n_regions; 1837 } 1838 1839 void G1CollectorPolicy::set_recorded_young_bytes(size_t bytes) { 1840 #if PREDICTIONS_VERBOSE 1841 _recorded_young_bytes = bytes; 1842 #endif // PREDICTIONS_VERBOSE 1843 } 1844 1845 void G1CollectorPolicy::set_recorded_rs_lengths(size_t rs_lengths) { 1846 _recorded_rs_lengths = rs_lengths; 1847 } 1848 1849 void G1CollectorPolicy::set_predicted_bytes_to_copy(size_t bytes) { 1850 _predicted_bytes_to_copy = bytes; 1851 } 1852 1853 void 1854 G1CollectorPolicy::end_recording_regions() { 1855 // The _predicted_pause_time_ms field is referenced in code 1856 // not under PREDICTIONS_VERBOSE. Let's initialize it. 1857 _predicted_pause_time_ms = -1.0; 1858 1859 #if PREDICTIONS_VERBOSE 1860 _predicted_pending_cards = predict_pending_cards(); 1861 _predicted_rs_lengths = _recorded_rs_lengths + predict_rs_length_diff(); 1862 if (full_young_gcs()) 1863 _predicted_cards_scanned += predict_young_card_num(_predicted_rs_lengths); 1864 else 1865 _predicted_cards_scanned += 1866 predict_non_young_card_num(_predicted_rs_lengths); 1867 _recorded_region_num = _recorded_young_regions + _recorded_non_young_regions; 1868 1869 _predicted_rs_update_time_ms = 1870 predict_rs_update_time_ms(_g1->pending_card_num()); 1871 _predicted_rs_scan_time_ms = 1872 predict_rs_scan_time_ms(_predicted_cards_scanned); 1873 _predicted_object_copy_time_ms = 1874 predict_object_copy_time_ms(_predicted_bytes_to_copy); 1875 _predicted_constant_other_time_ms = 1876 predict_constant_other_time_ms(); 1877 _predicted_young_other_time_ms = 1878 predict_young_other_time_ms(_recorded_young_regions); 1879 _predicted_non_young_other_time_ms = 1880 predict_non_young_other_time_ms(_recorded_non_young_regions); 1881 1882 _predicted_pause_time_ms = 1883 _predicted_rs_update_time_ms + 1884 _predicted_rs_scan_time_ms + 1885 _predicted_object_copy_time_ms + 1886 _predicted_constant_other_time_ms + 1887 _predicted_young_other_time_ms + 1888 _predicted_non_young_other_time_ms; 1889 #endif // PREDICTIONS_VERBOSE 1890 } 1891 1892 void G1CollectorPolicy::check_if_region_is_too_expensive(double 1893 predicted_time_ms) { 1894 // I don't think we need to do this when in young GC mode since 1895 // marking will be initiated next time we hit the soft limit anyway... 1896 if (predicted_time_ms > _expensive_region_limit_ms) { 1897 // no point in doing another partial one 1898 _should_revert_to_full_young_gcs = true; 1899 } 1900 } 1901 1902 // </NEW PREDICTION> 1903 1904 1905 void G1CollectorPolicy::update_recent_gc_times(double end_time_sec, 1906 double elapsed_ms) { 1907 _recent_gc_times_ms->add(elapsed_ms); 1908 _recent_prev_end_times_for_all_gcs_sec->add(end_time_sec); 1909 _prev_collection_pause_end_ms = end_time_sec * 1000.0; 1910 } 1911 1912 double G1CollectorPolicy::recent_avg_time_for_pauses_ms() { 1913 if (_recent_pause_times_ms->num() == 0) { 1914 return (double) MaxGCPauseMillis; 1915 } 1916 return _recent_pause_times_ms->avg(); 1917 } 1918 1919 double G1CollectorPolicy::recent_avg_time_for_rs_scan_ms() { 1920 if (_recent_rs_scan_times_ms->num() == 0) { 1921 return (double)MaxGCPauseMillis/3.0; 1922 } 1923 return _recent_rs_scan_times_ms->avg(); 1924 } 1925 1926 int G1CollectorPolicy::number_of_recent_gcs() { 1927 assert(_recent_rs_scan_times_ms->num() == 1928 _recent_pause_times_ms->num(), "Sequence out of sync"); 1929 assert(_recent_pause_times_ms->num() == 1930 _recent_CS_bytes_used_before->num(), "Sequence out of sync"); 1931 assert(_recent_CS_bytes_used_before->num() == 1932 _recent_CS_bytes_surviving->num(), "Sequence out of sync"); 1933 1934 return _recent_pause_times_ms->num(); 1935 } 1936 1937 double G1CollectorPolicy::recent_avg_survival_fraction() { 1938 return recent_avg_survival_fraction_work(_recent_CS_bytes_surviving, 1939 _recent_CS_bytes_used_before); 1940 } 1941 1942 double G1CollectorPolicy::last_survival_fraction() { 1943 return last_survival_fraction_work(_recent_CS_bytes_surviving, 1944 _recent_CS_bytes_used_before); 1945 } 1946 1947 double 1948 G1CollectorPolicy::recent_avg_survival_fraction_work(TruncatedSeq* surviving, 1949 TruncatedSeq* before) { 1950 assert(surviving->num() == before->num(), "Sequence out of sync"); 1951 if (before->sum() > 0.0) { 1952 double recent_survival_rate = surviving->sum() / before->sum(); 1953 // We exempt parallel collection from this check because Alloc Buffer 1954 // fragmentation can produce negative collections. 1955 // Further, we're now always doing parallel collection. But I'm still 1956 // leaving this here as a placeholder for a more precise assertion later. 1957 // (DLD, 10/05.) 1958 assert((true || G1CollectedHeap::use_parallel_gc_threads()) || 1959 _g1->evacuation_failed() || 1960 recent_survival_rate <= 1.0, "Or bad frac"); 1961 return recent_survival_rate; 1962 } else { 1963 return 1.0; // Be conservative. 1964 } 1965 } 1966 1967 double 1968 G1CollectorPolicy::last_survival_fraction_work(TruncatedSeq* surviving, 1969 TruncatedSeq* before) { 1970 assert(surviving->num() == before->num(), "Sequence out of sync"); 1971 if (surviving->num() > 0 && before->last() > 0.0) { 1972 double last_survival_rate = surviving->last() / before->last(); 1973 // We exempt parallel collection from this check because Alloc Buffer 1974 // fragmentation can produce negative collections. 1975 // Further, we're now always doing parallel collection. But I'm still 1976 // leaving this here as a placeholder for a more precise assertion later. 1977 // (DLD, 10/05.) 1978 assert((true || G1CollectedHeap::use_parallel_gc_threads()) || 1979 last_survival_rate <= 1.0, "Or bad frac"); 1980 return last_survival_rate; 1981 } else { 1982 return 1.0; 1983 } 1984 } 1985 1986 static const int survival_min_obs = 5; 1987 static double survival_min_obs_limits[] = { 0.9, 0.7, 0.5, 0.3, 0.1 }; 1988 static const double min_survival_rate = 0.1; 1989 1990 double 1991 G1CollectorPolicy::conservative_avg_survival_fraction_work(double avg, 1992 double latest) { 1993 double res = avg; 1994 if (number_of_recent_gcs() < survival_min_obs) { 1995 res = MAX2(res, survival_min_obs_limits[number_of_recent_gcs()]); 1996 } 1997 res = MAX2(res, latest); 1998 res = MAX2(res, min_survival_rate); 1999 // In the parallel case, LAB fragmentation can produce "negative 2000 // collections"; so can evac failure. Cap at 1.0 2001 res = MIN2(res, 1.0); 2002 return res; 2003 } 2004 2005 size_t G1CollectorPolicy::expansion_amount() { 2006 if ((recent_avg_pause_time_ratio() * 100.0) > _gc_overhead_perc) { 2007 // We will double the existing space, or take 2008 // G1ExpandByPercentOfAvailable % of the available expansion 2009 // space, whichever is smaller, bounded below by a minimum 2010 // expansion (unless that's all that's left.) 2011 const size_t min_expand_bytes = 1*M; 2012 size_t reserved_bytes = _g1->max_capacity(); 2013 size_t committed_bytes = _g1->capacity(); 2014 size_t uncommitted_bytes = reserved_bytes - committed_bytes; 2015 size_t expand_bytes; 2016 size_t expand_bytes_via_pct = 2017 uncommitted_bytes * G1ExpandByPercentOfAvailable / 100; 2018 expand_bytes = MIN2(expand_bytes_via_pct, committed_bytes); 2019 expand_bytes = MAX2(expand_bytes, min_expand_bytes); 2020 expand_bytes = MIN2(expand_bytes, uncommitted_bytes); 2021 if (G1PolicyVerbose > 1) { 2022 gclog_or_tty->print("Decided to expand: ratio = %5.2f, " 2023 "committed = %d%s, uncommited = %d%s, via pct = %d%s.\n" 2024 " Answer = %d.\n", 2025 recent_avg_pause_time_ratio(), 2026 byte_size_in_proper_unit(committed_bytes), 2027 proper_unit_for_byte_size(committed_bytes), 2028 byte_size_in_proper_unit(uncommitted_bytes), 2029 proper_unit_for_byte_size(uncommitted_bytes), 2030 byte_size_in_proper_unit(expand_bytes_via_pct), 2031 proper_unit_for_byte_size(expand_bytes_via_pct), 2032 byte_size_in_proper_unit(expand_bytes), 2033 proper_unit_for_byte_size(expand_bytes)); 2034 } 2035 return expand_bytes; 2036 } else { 2037 return 0; 2038 } 2039 } 2040 2041 void G1CollectorPolicy::note_start_of_mark_thread() { 2042 _mark_thread_startup_sec = os::elapsedTime(); 2043 } 2044 2045 class CountCSClosure: public HeapRegionClosure { 2046 G1CollectorPolicy* _g1_policy; 2047 public: 2048 CountCSClosure(G1CollectorPolicy* g1_policy) : 2049 _g1_policy(g1_policy) {} 2050 bool doHeapRegion(HeapRegion* r) { 2051 _g1_policy->_bytes_in_collection_set_before_gc += r->used(); 2052 return false; 2053 } 2054 }; 2055 2056 void G1CollectorPolicy::count_CS_bytes_used() { 2057 CountCSClosure cs_closure(this); 2058 _g1->collection_set_iterate(&cs_closure); 2059 } 2060 2061 void G1CollectorPolicy::print_summary (int level, 2062 const char* str, 2063 NumberSeq* seq) const { 2064 double sum = seq->sum(); 2065 LineBuffer(level + 1).append_and_print_cr("%-24s = %8.2lf s (avg = %8.2lf ms)", 2066 str, sum / 1000.0, seq->avg()); 2067 } 2068 2069 void G1CollectorPolicy::print_summary_sd (int level, 2070 const char* str, 2071 NumberSeq* seq) const { 2072 print_summary(level, str, seq); 2073 LineBuffer(level + 6).append_and_print_cr("(num = %5d, std dev = %8.2lf ms, max = %8.2lf ms)", 2074 seq->num(), seq->sd(), seq->maximum()); 2075 } 2076 2077 void G1CollectorPolicy::check_other_times(int level, 2078 NumberSeq* other_times_ms, 2079 NumberSeq* calc_other_times_ms) const { 2080 bool should_print = false; 2081 LineBuffer buf(level + 2); 2082 2083 double max_sum = MAX2(fabs(other_times_ms->sum()), 2084 fabs(calc_other_times_ms->sum())); 2085 double min_sum = MIN2(fabs(other_times_ms->sum()), 2086 fabs(calc_other_times_ms->sum())); 2087 double sum_ratio = max_sum / min_sum; 2088 if (sum_ratio > 1.1) { 2089 should_print = true; 2090 buf.append_and_print_cr("## CALCULATED OTHER SUM DOESN'T MATCH RECORDED ###"); 2091 } 2092 2093 double max_avg = MAX2(fabs(other_times_ms->avg()), 2094 fabs(calc_other_times_ms->avg())); 2095 double min_avg = MIN2(fabs(other_times_ms->avg()), 2096 fabs(calc_other_times_ms->avg())); 2097 double avg_ratio = max_avg / min_avg; 2098 if (avg_ratio > 1.1) { 2099 should_print = true; 2100 buf.append_and_print_cr("## CALCULATED OTHER AVG DOESN'T MATCH RECORDED ###"); 2101 } 2102 2103 if (other_times_ms->sum() < -0.01) { 2104 buf.append_and_print_cr("## RECORDED OTHER SUM IS NEGATIVE ###"); 2105 } 2106 2107 if (other_times_ms->avg() < -0.01) { 2108 buf.append_and_print_cr("## RECORDED OTHER AVG IS NEGATIVE ###"); 2109 } 2110 2111 if (calc_other_times_ms->sum() < -0.01) { 2112 should_print = true; 2113 buf.append_and_print_cr("## CALCULATED OTHER SUM IS NEGATIVE ###"); 2114 } 2115 2116 if (calc_other_times_ms->avg() < -0.01) { 2117 should_print = true; 2118 buf.append_and_print_cr("## CALCULATED OTHER AVG IS NEGATIVE ###"); 2119 } 2120 2121 if (should_print) 2122 print_summary(level, "Other(Calc)", calc_other_times_ms); 2123 } 2124 2125 void G1CollectorPolicy::print_summary(PauseSummary* summary) const { 2126 bool parallel = G1CollectedHeap::use_parallel_gc_threads(); 2127 MainBodySummary* body_summary = summary->main_body_summary(); 2128 if (summary->get_total_seq()->num() > 0) { 2129 print_summary_sd(0, "Evacuation Pauses", summary->get_total_seq()); 2130 if (body_summary != NULL) { 2131 print_summary(1, "SATB Drain", body_summary->get_satb_drain_seq()); 2132 if (parallel) { 2133 print_summary(1, "Parallel Time", body_summary->get_parallel_seq()); 2134 print_summary(2, "Update RS", body_summary->get_update_rs_seq()); 2135 print_summary(2, "Ext Root Scanning", 2136 body_summary->get_ext_root_scan_seq()); 2137 print_summary(2, "Mark Stack Scanning", 2138 body_summary->get_mark_stack_scan_seq()); 2139 print_summary(2, "Scan RS", body_summary->get_scan_rs_seq()); 2140 print_summary(2, "Object Copy", body_summary->get_obj_copy_seq()); 2141 print_summary(2, "Termination", body_summary->get_termination_seq()); 2142 print_summary(2, "Other", body_summary->get_parallel_other_seq()); 2143 { 2144 NumberSeq* other_parts[] = { 2145 body_summary->get_update_rs_seq(), 2146 body_summary->get_ext_root_scan_seq(), 2147 body_summary->get_mark_stack_scan_seq(), 2148 body_summary->get_scan_rs_seq(), 2149 body_summary->get_obj_copy_seq(), 2150 body_summary->get_termination_seq() 2151 }; 2152 NumberSeq calc_other_times_ms(body_summary->get_parallel_seq(), 2153 6, other_parts); 2154 check_other_times(2, body_summary->get_parallel_other_seq(), 2155 &calc_other_times_ms); 2156 } 2157 print_summary(1, "Mark Closure", body_summary->get_mark_closure_seq()); 2158 print_summary(1, "Clear CT", body_summary->get_clear_ct_seq()); 2159 } else { 2160 print_summary(1, "Update RS", body_summary->get_update_rs_seq()); 2161 print_summary(1, "Ext Root Scanning", 2162 body_summary->get_ext_root_scan_seq()); 2163 print_summary(1, "Mark Stack Scanning", 2164 body_summary->get_mark_stack_scan_seq()); 2165 print_summary(1, "Scan RS", body_summary->get_scan_rs_seq()); 2166 print_summary(1, "Object Copy", body_summary->get_obj_copy_seq()); 2167 } 2168 } 2169 print_summary(1, "Other", summary->get_other_seq()); 2170 { 2171 if (body_summary != NULL) { 2172 NumberSeq calc_other_times_ms; 2173 if (parallel) { 2174 // parallel 2175 NumberSeq* other_parts[] = { 2176 body_summary->get_satb_drain_seq(), 2177 body_summary->get_parallel_seq(), 2178 body_summary->get_clear_ct_seq() 2179 }; 2180 calc_other_times_ms = NumberSeq(summary->get_total_seq(), 2181 3, other_parts); 2182 } else { 2183 // serial 2184 NumberSeq* other_parts[] = { 2185 body_summary->get_satb_drain_seq(), 2186 body_summary->get_update_rs_seq(), 2187 body_summary->get_ext_root_scan_seq(), 2188 body_summary->get_mark_stack_scan_seq(), 2189 body_summary->get_scan_rs_seq(), 2190 body_summary->get_obj_copy_seq() 2191 }; 2192 calc_other_times_ms = NumberSeq(summary->get_total_seq(), 2193 6, other_parts); 2194 } 2195 check_other_times(1, summary->get_other_seq(), &calc_other_times_ms); 2196 } 2197 } 2198 } else { 2199 LineBuffer(1).append_and_print_cr("none"); 2200 } 2201 LineBuffer(0).append_and_print_cr(""); 2202 } 2203 2204 void G1CollectorPolicy::print_tracing_info() const { 2205 if (TraceGen0Time) { 2206 gclog_or_tty->print_cr("ALL PAUSES"); 2207 print_summary_sd(0, "Total", _all_pause_times_ms); 2208 gclog_or_tty->print_cr(""); 2209 gclog_or_tty->print_cr(""); 2210 gclog_or_tty->print_cr(" Full Young GC Pauses: %8d", _full_young_pause_num); 2211 gclog_or_tty->print_cr(" Partial Young GC Pauses: %8d", _partial_young_pause_num); 2212 gclog_or_tty->print_cr(""); 2213 2214 gclog_or_tty->print_cr("EVACUATION PAUSES"); 2215 print_summary(_summary); 2216 2217 gclog_or_tty->print_cr("MISC"); 2218 print_summary_sd(0, "Stop World", _all_stop_world_times_ms); 2219 print_summary_sd(0, "Yields", _all_yield_times_ms); 2220 for (int i = 0; i < _aux_num; ++i) { 2221 if (_all_aux_times_ms[i].num() > 0) { 2222 char buffer[96]; 2223 sprintf(buffer, "Aux%d", i); 2224 print_summary_sd(0, buffer, &_all_aux_times_ms[i]); 2225 } 2226 } 2227 2228 size_t all_region_num = _region_num_young + _region_num_tenured; 2229 gclog_or_tty->print_cr(" New Regions %8d, Young %8d (%6.2lf%%), " 2230 "Tenured %8d (%6.2lf%%)", 2231 all_region_num, 2232 _region_num_young, 2233 (double) _region_num_young / (double) all_region_num * 100.0, 2234 _region_num_tenured, 2235 (double) _region_num_tenured / (double) all_region_num * 100.0); 2236 } 2237 if (TraceGen1Time) { 2238 if (_all_full_gc_times_ms->num() > 0) { 2239 gclog_or_tty->print("\n%4d full_gcs: total time = %8.2f s", 2240 _all_full_gc_times_ms->num(), 2241 _all_full_gc_times_ms->sum() / 1000.0); 2242 gclog_or_tty->print_cr(" (avg = %8.2fms).", _all_full_gc_times_ms->avg()); 2243 gclog_or_tty->print_cr(" [std. dev = %8.2f ms, max = %8.2f ms]", 2244 _all_full_gc_times_ms->sd(), 2245 _all_full_gc_times_ms->maximum()); 2246 } 2247 } 2248 } 2249 2250 void G1CollectorPolicy::print_yg_surv_rate_info() const { 2251 #ifndef PRODUCT 2252 _short_lived_surv_rate_group->print_surv_rate_summary(); 2253 // add this call for any other surv rate groups 2254 #endif // PRODUCT 2255 } 2256 2257 void 2258 G1CollectorPolicy::update_region_num(bool young) { 2259 if (young) { 2260 ++_region_num_young; 2261 } else { 2262 ++_region_num_tenured; 2263 } 2264 } 2265 2266 #ifndef PRODUCT 2267 // for debugging, bit of a hack... 2268 static char* 2269 region_num_to_mbs(int length) { 2270 static char buffer[64]; 2271 double bytes = (double) (length * HeapRegion::GrainBytes); 2272 double mbs = bytes / (double) (1024 * 1024); 2273 sprintf(buffer, "%7.2lfMB", mbs); 2274 return buffer; 2275 } 2276 #endif // PRODUCT 2277 2278 size_t G1CollectorPolicy::max_regions(int purpose) { 2279 switch (purpose) { 2280 case GCAllocForSurvived: 2281 return _max_survivor_regions; 2282 case GCAllocForTenured: 2283 return REGIONS_UNLIMITED; 2284 default: 2285 ShouldNotReachHere(); 2286 return REGIONS_UNLIMITED; 2287 }; 2288 } 2289 2290 void G1CollectorPolicy::calculate_max_gc_locker_expansion() { 2291 size_t expansion_region_num = 0; 2292 if (GCLockerEdenExpansionPercent > 0) { 2293 double perc = (double) GCLockerEdenExpansionPercent / 100.0; 2294 double expansion_region_num_d = perc * (double) _young_list_target_length; 2295 // We use ceiling so that if expansion_region_num_d is > 0.0 (but 2296 // less than 1.0) we'll get 1. 2297 expansion_region_num = (size_t) ceil(expansion_region_num_d); 2298 } else { 2299 assert(expansion_region_num == 0, "sanity"); 2300 } 2301 _young_list_max_length = _young_list_target_length + expansion_region_num; 2302 assert(_young_list_target_length <= _young_list_max_length, "post-condition"); 2303 } 2304 2305 // Calculates survivor space parameters. 2306 void G1CollectorPolicy::calculate_survivors_policy() 2307 { 2308 if (G1FixedSurvivorSpaceSize == 0) { 2309 _max_survivor_regions = _young_list_target_length / SurvivorRatio; 2310 } else { 2311 _max_survivor_regions = G1FixedSurvivorSpaceSize / HeapRegion::GrainBytes; 2312 } 2313 2314 if (G1FixedTenuringThreshold) { 2315 _tenuring_threshold = MaxTenuringThreshold; 2316 } else { 2317 _tenuring_threshold = _survivors_age_table.compute_tenuring_threshold( 2318 HeapRegion::GrainWords * _max_survivor_regions); 2319 } 2320 } 2321 2322 #ifndef PRODUCT 2323 class HRSortIndexIsOKClosure: public HeapRegionClosure { 2324 CollectionSetChooser* _chooser; 2325 public: 2326 HRSortIndexIsOKClosure(CollectionSetChooser* chooser) : 2327 _chooser(chooser) {} 2328 2329 bool doHeapRegion(HeapRegion* r) { 2330 if (!r->continuesHumongous()) { 2331 assert(_chooser->regionProperlyOrdered(r), "Ought to be."); 2332 } 2333 return false; 2334 } 2335 }; 2336 2337 bool G1CollectorPolicy_BestRegionsFirst::assertMarkedBytesDataOK() { 2338 HRSortIndexIsOKClosure cl(_collectionSetChooser); 2339 _g1->heap_region_iterate(&cl); 2340 return true; 2341 } 2342 #endif 2343 2344 bool 2345 G1CollectorPolicy::force_initial_mark_if_outside_cycle() { 2346 bool during_cycle = _g1->concurrent_mark()->cmThread()->during_cycle(); 2347 if (!during_cycle) { 2348 set_initiate_conc_mark_if_possible(); 2349 return true; 2350 } else { 2351 return false; 2352 } 2353 } 2354 2355 void 2356 G1CollectorPolicy::decide_on_conc_mark_initiation() { 2357 // We are about to decide on whether this pause will be an 2358 // initial-mark pause. 2359 2360 // First, during_initial_mark_pause() should not be already set. We 2361 // will set it here if we have to. However, it should be cleared by 2362 // the end of the pause (it's only set for the duration of an 2363 // initial-mark pause). 2364 assert(!during_initial_mark_pause(), "pre-condition"); 2365 2366 if (initiate_conc_mark_if_possible()) { 2367 // We had noticed on a previous pause that the heap occupancy has 2368 // gone over the initiating threshold and we should start a 2369 // concurrent marking cycle. So we might initiate one. 2370 2371 bool during_cycle = _g1->concurrent_mark()->cmThread()->during_cycle(); 2372 if (!during_cycle) { 2373 // The concurrent marking thread is not "during a cycle", i.e., 2374 // it has completed the last one. So we can go ahead and 2375 // initiate a new cycle. 2376 2377 set_during_initial_mark_pause(); 2378 2379 // And we can now clear initiate_conc_mark_if_possible() as 2380 // we've already acted on it. 2381 clear_initiate_conc_mark_if_possible(); 2382 } else { 2383 // The concurrent marking thread is still finishing up the 2384 // previous cycle. If we start one right now the two cycles 2385 // overlap. In particular, the concurrent marking thread might 2386 // be in the process of clearing the next marking bitmap (which 2387 // we will use for the next cycle if we start one). Starting a 2388 // cycle now will be bad given that parts of the marking 2389 // information might get cleared by the marking thread. And we 2390 // cannot wait for the marking thread to finish the cycle as it 2391 // periodically yields while clearing the next marking bitmap 2392 // and, if it's in a yield point, it's waiting for us to 2393 // finish. So, at this point we will not start a cycle and we'll 2394 // let the concurrent marking thread complete the last one. 2395 } 2396 } 2397 } 2398 2399 void 2400 G1CollectorPolicy_BestRegionsFirst:: 2401 record_collection_pause_start(double start_time_sec, size_t start_used) { 2402 G1CollectorPolicy::record_collection_pause_start(start_time_sec, start_used); 2403 } 2404 2405 class KnownGarbageClosure: public HeapRegionClosure { 2406 CollectionSetChooser* _hrSorted; 2407 2408 public: 2409 KnownGarbageClosure(CollectionSetChooser* hrSorted) : 2410 _hrSorted(hrSorted) 2411 {} 2412 2413 bool doHeapRegion(HeapRegion* r) { 2414 // We only include humongous regions in collection 2415 // sets when concurrent mark shows that their contained object is 2416 // unreachable. 2417 2418 // Do we have any marking information for this region? 2419 if (r->is_marked()) { 2420 // We don't include humongous regions in collection 2421 // sets because we collect them immediately at the end of a marking 2422 // cycle. We also don't include young regions because we *must* 2423 // include them in the next collection pause. 2424 if (!r->isHumongous() && !r->is_young()) { 2425 _hrSorted->addMarkedHeapRegion(r); 2426 } 2427 } 2428 return false; 2429 } 2430 }; 2431 2432 class ParKnownGarbageHRClosure: public HeapRegionClosure { 2433 CollectionSetChooser* _hrSorted; 2434 jint _marked_regions_added; 2435 jint _chunk_size; 2436 jint _cur_chunk_idx; 2437 jint _cur_chunk_end; // Cur chunk [_cur_chunk_idx, _cur_chunk_end) 2438 int _worker; 2439 int _invokes; 2440 2441 void get_new_chunk() { 2442 _cur_chunk_idx = _hrSorted->getParMarkedHeapRegionChunk(_chunk_size); 2443 _cur_chunk_end = _cur_chunk_idx + _chunk_size; 2444 } 2445 void add_region(HeapRegion* r) { 2446 if (_cur_chunk_idx == _cur_chunk_end) { 2447 get_new_chunk(); 2448 } 2449 assert(_cur_chunk_idx < _cur_chunk_end, "postcondition"); 2450 _hrSorted->setMarkedHeapRegion(_cur_chunk_idx, r); 2451 _marked_regions_added++; 2452 _cur_chunk_idx++; 2453 } 2454 2455 public: 2456 ParKnownGarbageHRClosure(CollectionSetChooser* hrSorted, 2457 jint chunk_size, 2458 int worker) : 2459 _hrSorted(hrSorted), _chunk_size(chunk_size), _worker(worker), 2460 _marked_regions_added(0), _cur_chunk_idx(0), _cur_chunk_end(0), 2461 _invokes(0) 2462 {} 2463 2464 bool doHeapRegion(HeapRegion* r) { 2465 // We only include humongous regions in collection 2466 // sets when concurrent mark shows that their contained object is 2467 // unreachable. 2468 _invokes++; 2469 2470 // Do we have any marking information for this region? 2471 if (r->is_marked()) { 2472 // We don't include humongous regions in collection 2473 // sets because we collect them immediately at the end of a marking 2474 // cycle. 2475 // We also do not include young regions in collection sets 2476 if (!r->isHumongous() && !r->is_young()) { 2477 add_region(r); 2478 } 2479 } 2480 return false; 2481 } 2482 jint marked_regions_added() { return _marked_regions_added; } 2483 int invokes() { return _invokes; } 2484 }; 2485 2486 class ParKnownGarbageTask: public AbstractGangTask { 2487 CollectionSetChooser* _hrSorted; 2488 jint _chunk_size; 2489 G1CollectedHeap* _g1; 2490 public: 2491 ParKnownGarbageTask(CollectionSetChooser* hrSorted, jint chunk_size) : 2492 AbstractGangTask("ParKnownGarbageTask"), 2493 _hrSorted(hrSorted), _chunk_size(chunk_size), 2494 _g1(G1CollectedHeap::heap()) 2495 {} 2496 2497 void work(int i) { 2498 ParKnownGarbageHRClosure parKnownGarbageCl(_hrSorted, _chunk_size, i); 2499 // Back to zero for the claim value. 2500 _g1->heap_region_par_iterate_chunked(&parKnownGarbageCl, i, 2501 HeapRegion::InitialClaimValue); 2502 jint regions_added = parKnownGarbageCl.marked_regions_added(); 2503 _hrSorted->incNumMarkedHeapRegions(regions_added); 2504 if (G1PrintParCleanupStats) { 2505 gclog_or_tty->print_cr(" Thread %d called %d times, added %d regions to list.", 2506 i, parKnownGarbageCl.invokes(), regions_added); 2507 } 2508 } 2509 }; 2510 2511 void 2512 G1CollectorPolicy_BestRegionsFirst:: 2513 record_concurrent_mark_cleanup_end(size_t freed_bytes, 2514 size_t max_live_bytes) { 2515 double start; 2516 if (G1PrintParCleanupStats) start = os::elapsedTime(); 2517 record_concurrent_mark_cleanup_end_work1(freed_bytes, max_live_bytes); 2518 2519 _collectionSetChooser->clearMarkedHeapRegions(); 2520 double clear_marked_end; 2521 if (G1PrintParCleanupStats) { 2522 clear_marked_end = os::elapsedTime(); 2523 gclog_or_tty->print_cr(" clear marked regions + work1: %8.3f ms.", 2524 (clear_marked_end - start)*1000.0); 2525 } 2526 if (G1CollectedHeap::use_parallel_gc_threads()) { 2527 const size_t OverpartitionFactor = 4; 2528 const size_t MinWorkUnit = 8; 2529 const size_t WorkUnit = 2530 MAX2(_g1->n_regions() / (ParallelGCThreads * OverpartitionFactor), 2531 MinWorkUnit); 2532 _collectionSetChooser->prepareForAddMarkedHeapRegionsPar(_g1->n_regions(), 2533 WorkUnit); 2534 ParKnownGarbageTask parKnownGarbageTask(_collectionSetChooser, 2535 (int) WorkUnit); 2536 _g1->workers()->run_task(&parKnownGarbageTask); 2537 2538 assert(_g1->check_heap_region_claim_values(HeapRegion::InitialClaimValue), 2539 "sanity check"); 2540 } else { 2541 KnownGarbageClosure knownGarbagecl(_collectionSetChooser); 2542 _g1->heap_region_iterate(&knownGarbagecl); 2543 } 2544 double known_garbage_end; 2545 if (G1PrintParCleanupStats) { 2546 known_garbage_end = os::elapsedTime(); 2547 gclog_or_tty->print_cr(" compute known garbage: %8.3f ms.", 2548 (known_garbage_end - clear_marked_end)*1000.0); 2549 } 2550 _collectionSetChooser->sortMarkedHeapRegions(); 2551 double sort_end; 2552 if (G1PrintParCleanupStats) { 2553 sort_end = os::elapsedTime(); 2554 gclog_or_tty->print_cr(" sorting: %8.3f ms.", 2555 (sort_end - known_garbage_end)*1000.0); 2556 } 2557 2558 record_concurrent_mark_cleanup_end_work2(); 2559 double work2_end; 2560 if (G1PrintParCleanupStats) { 2561 work2_end = os::elapsedTime(); 2562 gclog_or_tty->print_cr(" work2: %8.3f ms.", 2563 (work2_end - sort_end)*1000.0); 2564 } 2565 } 2566 2567 // Add the heap region at the head of the non-incremental collection set 2568 void G1CollectorPolicy:: 2569 add_to_collection_set(HeapRegion* hr) { 2570 assert(_inc_cset_build_state == Active, "Precondition"); 2571 assert(!hr->is_young(), "non-incremental add of young region"); 2572 2573 if (_g1->mark_in_progress()) 2574 _g1->concurrent_mark()->registerCSetRegion(hr); 2575 2576 assert(!hr->in_collection_set(), "should not already be in the CSet"); 2577 hr->set_in_collection_set(true); 2578 hr->set_next_in_collection_set(_collection_set); 2579 _collection_set = hr; 2580 _collection_set_size++; 2581 _collection_set_bytes_used_before += hr->used(); 2582 _g1->register_region_with_in_cset_fast_test(hr); 2583 } 2584 2585 // Initialize the per-collection-set information 2586 void G1CollectorPolicy::start_incremental_cset_building() { 2587 assert(_inc_cset_build_state == Inactive, "Precondition"); 2588 2589 _inc_cset_head = NULL; 2590 _inc_cset_tail = NULL; 2591 _inc_cset_size = 0; 2592 _inc_cset_bytes_used_before = 0; 2593 2594 _inc_cset_young_index = 0; 2595 2596 _inc_cset_max_finger = 0; 2597 _inc_cset_recorded_young_bytes = 0; 2598 _inc_cset_recorded_rs_lengths = 0; 2599 _inc_cset_predicted_elapsed_time_ms = 0; 2600 _inc_cset_predicted_bytes_to_copy = 0; 2601 _inc_cset_build_state = Active; 2602 } 2603 2604 void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length) { 2605 // This routine is used when: 2606 // * adding survivor regions to the incremental cset at the end of an 2607 // evacuation pause, 2608 // * adding the current allocation region to the incremental cset 2609 // when it is retired, and 2610 // * updating existing policy information for a region in the 2611 // incremental cset via young list RSet sampling. 2612 // Therefore this routine may be called at a safepoint by the 2613 // VM thread, or in-between safepoints by mutator threads (when 2614 // retiring the current allocation region) or a concurrent 2615 // refine thread (RSet sampling). 2616 2617 double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, true); 2618 size_t used_bytes = hr->used(); 2619 2620 _inc_cset_recorded_rs_lengths += rs_length; 2621 _inc_cset_predicted_elapsed_time_ms += region_elapsed_time_ms; 2622 2623 _inc_cset_bytes_used_before += used_bytes; 2624 2625 // Cache the values we have added to the aggregated informtion 2626 // in the heap region in case we have to remove this region from 2627 // the incremental collection set, or it is updated by the 2628 // rset sampling code 2629 hr->set_recorded_rs_length(rs_length); 2630 hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms); 2631 2632 #if PREDICTIONS_VERBOSE 2633 size_t bytes_to_copy = predict_bytes_to_copy(hr); 2634 _inc_cset_predicted_bytes_to_copy += bytes_to_copy; 2635 2636 // Record the number of bytes used in this region 2637 _inc_cset_recorded_young_bytes += used_bytes; 2638 2639 // Cache the values we have added to the aggregated informtion 2640 // in the heap region in case we have to remove this region from 2641 // the incremental collection set, or it is updated by the 2642 // rset sampling code 2643 hr->set_predicted_bytes_to_copy(bytes_to_copy); 2644 #endif // PREDICTIONS_VERBOSE 2645 } 2646 2647 void G1CollectorPolicy::remove_from_incremental_cset_info(HeapRegion* hr) { 2648 // This routine is currently only called as part of the updating of 2649 // existing policy information for regions in the incremental cset that 2650 // is performed by the concurrent refine thread(s) as part of young list 2651 // RSet sampling. Therefore we should not be at a safepoint. 2652 2653 assert(!SafepointSynchronize::is_at_safepoint(), "should not be at safepoint"); 2654 assert(hr->is_young(), "it should be"); 2655 2656 size_t used_bytes = hr->used(); 2657 size_t old_rs_length = hr->recorded_rs_length(); 2658 double old_elapsed_time_ms = hr->predicted_elapsed_time_ms(); 2659 2660 // Subtract the old recorded/predicted policy information for 2661 // the given heap region from the collection set info. 2662 _inc_cset_recorded_rs_lengths -= old_rs_length; 2663 _inc_cset_predicted_elapsed_time_ms -= old_elapsed_time_ms; 2664 2665 _inc_cset_bytes_used_before -= used_bytes; 2666 2667 // Clear the values cached in the heap region 2668 hr->set_recorded_rs_length(0); 2669 hr->set_predicted_elapsed_time_ms(0); 2670 2671 #if PREDICTIONS_VERBOSE 2672 size_t old_predicted_bytes_to_copy = hr->predicted_bytes_to_copy(); 2673 _inc_cset_predicted_bytes_to_copy -= old_predicted_bytes_to_copy; 2674 2675 // Subtract the number of bytes used in this region 2676 _inc_cset_recorded_young_bytes -= used_bytes; 2677 2678 // Clear the values cached in the heap region 2679 hr->set_predicted_bytes_to_copy(0); 2680 #endif // PREDICTIONS_VERBOSE 2681 } 2682 2683 void G1CollectorPolicy::update_incremental_cset_info(HeapRegion* hr, size_t new_rs_length) { 2684 // Update the collection set information that is dependent on the new RS length 2685 assert(hr->is_young(), "Precondition"); 2686 2687 remove_from_incremental_cset_info(hr); 2688 add_to_incremental_cset_info(hr, new_rs_length); 2689 } 2690 2691 void G1CollectorPolicy::add_region_to_incremental_cset_common(HeapRegion* hr) { 2692 assert( hr->is_young(), "invariant"); 2693 assert( hr->young_index_in_cset() == -1, "invariant" ); 2694 assert(_inc_cset_build_state == Active, "Precondition"); 2695 2696 // We need to clear and set the cached recorded/cached collection set 2697 // information in the heap region here (before the region gets added 2698 // to the collection set). An individual heap region's cached values 2699 // are calculated, aggregated with the policy collection set info, 2700 // and cached in the heap region here (initially) and (subsequently) 2701 // by the Young List sampling code. 2702 2703 size_t rs_length = hr->rem_set()->occupied(); 2704 add_to_incremental_cset_info(hr, rs_length); 2705 2706 HeapWord* hr_end = hr->end(); 2707 _inc_cset_max_finger = MAX2(_inc_cset_max_finger, hr_end); 2708 2709 assert(!hr->in_collection_set(), "invariant"); 2710 hr->set_in_collection_set(true); 2711 assert( hr->next_in_collection_set() == NULL, "invariant"); 2712 2713 _inc_cset_size++; 2714 _g1->register_region_with_in_cset_fast_test(hr); 2715 2716 hr->set_young_index_in_cset((int) _inc_cset_young_index); 2717 ++_inc_cset_young_index; 2718 } 2719 2720 // Add the region at the RHS of the incremental cset 2721 void G1CollectorPolicy::add_region_to_incremental_cset_rhs(HeapRegion* hr) { 2722 // We should only ever be appending survivors at the end of a pause 2723 assert( hr->is_survivor(), "Logic"); 2724 2725 // Do the 'common' stuff 2726 add_region_to_incremental_cset_common(hr); 2727 2728 // Now add the region at the right hand side 2729 if (_inc_cset_tail == NULL) { 2730 assert(_inc_cset_head == NULL, "invariant"); 2731 _inc_cset_head = hr; 2732 } else { 2733 _inc_cset_tail->set_next_in_collection_set(hr); 2734 } 2735 _inc_cset_tail = hr; 2736 } 2737 2738 // Add the region to the LHS of the incremental cset 2739 void G1CollectorPolicy::add_region_to_incremental_cset_lhs(HeapRegion* hr) { 2740 // Survivors should be added to the RHS at the end of a pause 2741 assert(!hr->is_survivor(), "Logic"); 2742 2743 // Do the 'common' stuff 2744 add_region_to_incremental_cset_common(hr); 2745 2746 // Add the region at the left hand side 2747 hr->set_next_in_collection_set(_inc_cset_head); 2748 if (_inc_cset_head == NULL) { 2749 assert(_inc_cset_tail == NULL, "Invariant"); 2750 _inc_cset_tail = hr; 2751 } 2752 _inc_cset_head = hr; 2753 } 2754 2755 #ifndef PRODUCT 2756 void G1CollectorPolicy::print_collection_set(HeapRegion* list_head, outputStream* st) { 2757 assert(list_head == inc_cset_head() || list_head == collection_set(), "must be"); 2758 2759 st->print_cr("\nCollection_set:"); 2760 HeapRegion* csr = list_head; 2761 while (csr != NULL) { 2762 HeapRegion* next = csr->next_in_collection_set(); 2763 assert(csr->in_collection_set(), "bad CS"); 2764 st->print_cr(" [%08x-%08x], t: %08x, P: %08x, N: %08x, C: %08x, " 2765 "age: %4d, y: %d, surv: %d", 2766 csr->bottom(), csr->end(), 2767 csr->top(), 2768 csr->prev_top_at_mark_start(), 2769 csr->next_top_at_mark_start(), 2770 csr->top_at_conc_mark_count(), 2771 csr->age_in_surv_rate_group_cond(), 2772 csr->is_young(), 2773 csr->is_survivor()); 2774 csr = next; 2775 } 2776 } 2777 #endif // !PRODUCT 2778 2779 void 2780 G1CollectorPolicy_BestRegionsFirst::choose_collection_set( 2781 double target_pause_time_ms) { 2782 // Set this here - in case we're not doing young collections. 2783 double non_young_start_time_sec = os::elapsedTime(); 2784 2785 start_recording_regions(); 2786 2787 guarantee(target_pause_time_ms > 0.0, 2788 err_msg("target_pause_time_ms = %1.6lf should be positive", 2789 target_pause_time_ms)); 2790 guarantee(_collection_set == NULL, "Precondition"); 2791 2792 double base_time_ms = predict_base_elapsed_time_ms(_pending_cards); 2793 double predicted_pause_time_ms = base_time_ms; 2794 2795 double time_remaining_ms = target_pause_time_ms - base_time_ms; 2796 2797 // the 10% and 50% values are arbitrary... 2798 if (time_remaining_ms < 0.10 * target_pause_time_ms) { 2799 time_remaining_ms = 0.50 * target_pause_time_ms; 2800 _within_target = false; 2801 } else { 2802 _within_target = true; 2803 } 2804 2805 // We figure out the number of bytes available for future to-space. 2806 // For new regions without marking information, we must assume the 2807 // worst-case of complete survival. If we have marking information for a 2808 // region, we can bound the amount of live data. We can add a number of 2809 // such regions, as long as the sum of the live data bounds does not 2810 // exceed the available evacuation space. 2811 size_t max_live_bytes = _g1->free_regions() * HeapRegion::GrainBytes; 2812 2813 size_t expansion_bytes = 2814 _g1->expansion_regions() * HeapRegion::GrainBytes; 2815 2816 _collection_set_bytes_used_before = 0; 2817 _collection_set_size = 0; 2818 2819 // Adjust for expansion and slop. 2820 max_live_bytes = max_live_bytes + expansion_bytes; 2821 2822 HeapRegion* hr; 2823 double young_start_time_sec = os::elapsedTime(); 2824 2825 if (G1PolicyVerbose > 0) { 2826 gclog_or_tty->print_cr("Adding %d young regions to the CSet", 2827 _g1->young_list()->length()); 2828 } 2829 2830 _young_cset_length = 0; 2831 _last_young_gc_full = full_young_gcs() ? true : false; 2832 2833 if (_last_young_gc_full) 2834 ++_full_young_pause_num; 2835 else 2836 ++_partial_young_pause_num; 2837 2838 // The young list is laid with the survivor regions from the previous 2839 // pause are appended to the RHS of the young list, i.e. 2840 // [Newly Young Regions ++ Survivors from last pause]. 2841 2842 hr = _g1->young_list()->first_survivor_region(); 2843 while (hr != NULL) { 2844 assert(hr->is_survivor(), "badly formed young list"); 2845 hr->set_young(); 2846 hr = hr->get_next_young_region(); 2847 } 2848 2849 // Clear the fields that point to the survivor list - they are 2850 // all young now. 2851 _g1->young_list()->clear_survivors(); 2852 2853 if (_g1->mark_in_progress()) 2854 _g1->concurrent_mark()->register_collection_set_finger(_inc_cset_max_finger); 2855 2856 _young_cset_length = _inc_cset_young_index; 2857 _collection_set = _inc_cset_head; 2858 _collection_set_size = _inc_cset_size; 2859 _collection_set_bytes_used_before = _inc_cset_bytes_used_before; 2860 2861 // For young regions in the collection set, we assume the worst 2862 // case of complete survival 2863 max_live_bytes -= _inc_cset_size * HeapRegion::GrainBytes; 2864 2865 time_remaining_ms -= _inc_cset_predicted_elapsed_time_ms; 2866 predicted_pause_time_ms += _inc_cset_predicted_elapsed_time_ms; 2867 2868 // The number of recorded young regions is the incremental 2869 // collection set's current size 2870 set_recorded_young_regions(_inc_cset_size); 2871 set_recorded_rs_lengths(_inc_cset_recorded_rs_lengths); 2872 set_recorded_young_bytes(_inc_cset_recorded_young_bytes); 2873 #if PREDICTIONS_VERBOSE 2874 set_predicted_bytes_to_copy(_inc_cset_predicted_bytes_to_copy); 2875 #endif // PREDICTIONS_VERBOSE 2876 2877 if (G1PolicyVerbose > 0) { 2878 gclog_or_tty->print_cr(" Added " PTR_FORMAT " Young Regions to CS.", 2879 _inc_cset_size); 2880 gclog_or_tty->print_cr(" (" SIZE_FORMAT " KB left in heap.)", 2881 max_live_bytes/K); 2882 } 2883 2884 assert(_inc_cset_size == _g1->young_list()->length(), "Invariant"); 2885 2886 double young_end_time_sec = os::elapsedTime(); 2887 _recorded_young_cset_choice_time_ms = 2888 (young_end_time_sec - young_start_time_sec) * 1000.0; 2889 2890 // We are doing young collections so reset this. 2891 non_young_start_time_sec = young_end_time_sec; 2892 2893 // Note we can use either _collection_set_size or 2894 // _young_cset_length here 2895 if (_collection_set_size > 0 && _last_young_gc_full) { 2896 // don't bother adding more regions... 2897 goto choose_collection_set_end; 2898 } 2899 2900 if (!full_young_gcs()) { 2901 bool should_continue = true; 2902 NumberSeq seq; 2903 double avg_prediction = 100000000000000000.0; // something very large 2904 2905 do { 2906 hr = _collectionSetChooser->getNextMarkedRegion(time_remaining_ms, 2907 avg_prediction); 2908 if (hr != NULL) { 2909 double predicted_time_ms = predict_region_elapsed_time_ms(hr, false); 2910 time_remaining_ms -= predicted_time_ms; 2911 predicted_pause_time_ms += predicted_time_ms; 2912 add_to_collection_set(hr); 2913 record_non_young_cset_region(hr); 2914 max_live_bytes -= MIN2(hr->max_live_bytes(), max_live_bytes); 2915 if (G1PolicyVerbose > 0) { 2916 gclog_or_tty->print_cr(" (" SIZE_FORMAT " KB left in heap.)", 2917 max_live_bytes/K); 2918 } 2919 seq.add(predicted_time_ms); 2920 avg_prediction = seq.avg() + seq.sd(); 2921 } 2922 should_continue = 2923 ( hr != NULL) && 2924 ( (adaptive_young_list_length()) ? time_remaining_ms > 0.0 2925 : _collection_set_size < _young_list_fixed_length ); 2926 } while (should_continue); 2927 2928 if (!adaptive_young_list_length() && 2929 _collection_set_size < _young_list_fixed_length) 2930 _should_revert_to_full_young_gcs = true; 2931 } 2932 2933 choose_collection_set_end: 2934 stop_incremental_cset_building(); 2935 2936 count_CS_bytes_used(); 2937 2938 end_recording_regions(); 2939 2940 double non_young_end_time_sec = os::elapsedTime(); 2941 _recorded_non_young_cset_choice_time_ms = 2942 (non_young_end_time_sec - non_young_start_time_sec) * 1000.0; 2943 } 2944 2945 void G1CollectorPolicy_BestRegionsFirst::record_full_collection_end() { 2946 G1CollectorPolicy::record_full_collection_end(); 2947 _collectionSetChooser->updateAfterFullCollection(); 2948 } 2949 2950 void G1CollectorPolicy_BestRegionsFirst:: 2951 expand_if_possible(size_t numRegions) { 2952 size_t expansion_bytes = numRegions * HeapRegion::GrainBytes; 2953 _g1->expand(expansion_bytes); 2954 } 2955 2956 void G1CollectorPolicy_BestRegionsFirst:: 2957 record_collection_pause_end() { 2958 G1CollectorPolicy::record_collection_pause_end(); 2959 assert(assertMarkedBytesDataOK(), "Marked regions not OK at pause end."); 2960 }