< prev index next >

src/hotspot/share/gc/g1/g1Policy.cpp

Print this page




  51 #include "utilities/pair.hpp"
  52 
  53 G1Policy::G1Policy(STWGCTimer* gc_timer) :
  54   _predictor(G1ConfidencePercent / 100.0),
  55   _analytics(new G1Analytics(&_predictor)),
  56   _remset_tracker(),
  57   _mmu_tracker(new G1MMUTrackerQueue(GCPauseIntervalMillis / 1000.0, MaxGCPauseMillis / 1000.0)),
  58   _ihop_control(create_ihop_control(&_predictor)),
  59   _policy_counters(new GCPolicyCounters("GarbageFirst", 1, 2)),
  60   _full_collection_start_sec(0.0),
  61   _collection_pause_end_millis(os::javaTimeNanos() / NANOSECS_PER_MILLISEC),
  62   _young_list_target_length(0),
  63   _young_list_fixed_length(0),
  64   _young_list_max_length(0),
  65   _short_lived_surv_rate_group(new SurvRateGroup()),
  66   _survivor_surv_rate_group(new SurvRateGroup()),
  67   _reserve_factor((double) G1ReservePercent / 100.0),
  68   _reserve_regions(0),
  69   _young_gen_sizer(G1YoungGenSizer::create_gen_sizer()),
  70   _free_regions_at_end_of_collection(0),
  71   _max_rs_length(0),
  72   _rs_length_prediction(0),
  73   _pending_cards(0),
  74   _bytes_allocated_in_old_since_last_gc(0),
  75   _initial_mark_to_mixed(),
  76   _collection_set(NULL),
  77   _bytes_copied_during_gc(0),
  78   _g1h(NULL),
  79   _phase_times(new G1GCPhaseTimes(gc_timer, ParallelGCThreads)),
  80   _mark_remark_start_sec(0),
  81   _mark_cleanup_start_sec(0),
  82   _tenuring_threshold(MaxTenuringThreshold),
  83   _max_survivor_regions(0),
  84   _survivors_age_table(true)
  85 {
  86 }
  87 
  88 G1Policy::~G1Policy() {
  89   delete _ihop_control;
  90   delete _young_gen_sizer;
  91 }
  92 


 202       double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0;
 203       double alloc_rate_ms = _analytics->predict_alloc_rate_ms();
 204       desired_min_length = (uint) ceil(alloc_rate_ms * when_ms);
 205     } else {
 206       // otherwise we don't have enough info to make the prediction
 207     }
 208   }
 209   desired_min_length += base_min_length;
 210   // make sure we don't go below any user-defined minimum bound
 211   return MAX2(_young_gen_sizer->min_desired_young_length(), desired_min_length);
 212 }
 213 
 214 uint G1Policy::calculate_young_list_desired_max_length() const {
 215   // Here, we might want to also take into account any additional
 216   // constraints (i.e., user-defined minimum bound). Currently, we
 217   // effectively don't set this bound.
 218   return _young_gen_sizer->max_desired_young_length();
 219 }
 220 
 221 uint G1Policy::update_young_list_max_and_target_length() {
 222   return update_young_list_max_and_target_length(_analytics->predict_rs_length());
 223 }
 224 
 225 uint G1Policy::update_young_list_max_and_target_length(size_t rs_length) {
 226   uint unbounded_target_length = update_young_list_target_length(rs_length);
 227   update_max_gc_locker_expansion();
 228   return unbounded_target_length;
 229 }
 230 
 231 uint G1Policy::update_young_list_target_length(size_t rs_length) {
 232   YoungTargetLengths young_lengths = young_list_target_lengths(rs_length);
 233   _young_list_target_length = young_lengths.first;
 234 
 235   return young_lengths.second;
 236 }
 237 
 238 G1Policy::YoungTargetLengths G1Policy::young_list_target_lengths(size_t rs_length) const {
 239   YoungTargetLengths result;
 240 
 241   // Calculate the absolute and desired min bounds first.
 242 
 243   // This is how many young regions we already have (currently: the survivors).
 244   const uint base_min_length = _g1h->survivor_regions_count();
 245   uint desired_min_length = calculate_young_list_desired_min_length(base_min_length);
 246   // This is the absolute minimum young length. Ensure that we
 247   // will at least have one eden region available for allocation.
 248   uint absolute_min_length = base_min_length + MAX2(_g1h->eden_regions_count(), (uint)1);
 249   // If we shrank the young list target it should not shrink below the current size.
 250   desired_min_length = MAX2(desired_min_length, absolute_min_length);
 251   // Calculate the absolute and desired max bounds.
 252 
 253   uint desired_max_length = calculate_young_list_desired_max_length();
 254 
 255   uint young_list_target_length = 0;
 256   if (use_adaptive_young_list_length()) {
 257     if (collector_state()->in_young_only_phase()) {
 258       young_list_target_length =
 259                         calculate_young_list_target_length(rs_length,
 260                                                            base_min_length,
 261                                                            desired_min_length,
 262                                                            desired_max_length);
 263     } else {
 264       // Don't calculate anything and let the code below bound it to
 265       // the desired_min_length, i.e., do the next GC as soon as
 266       // possible to maximize how many old regions we can add to it.
 267     }
 268   } else {
 269     // The user asked for a fixed young gen so we'll fix the young gen
 270     // whether the next GC is young or mixed.
 271     young_list_target_length = _young_list_fixed_length;
 272   }
 273 
 274   result.second = young_list_target_length;
 275 
 276   // We will try our best not to "eat" into the reserve.
 277   uint absolute_max_length = 0;
 278   if (_free_regions_at_end_of_collection > _reserve_regions) {
 279     absolute_max_length = _free_regions_at_end_of_collection - _reserve_regions;


 284 
 285   // Make sure we don't go over the desired max length, nor under the
 286   // desired min length. In case they clash, desired_min_length wins
 287   // which is why that test is second.
 288   if (young_list_target_length > desired_max_length) {
 289     young_list_target_length = desired_max_length;
 290   }
 291   if (young_list_target_length < desired_min_length) {
 292     young_list_target_length = desired_min_length;
 293   }
 294 
 295   assert(young_list_target_length > base_min_length,
 296          "we should be able to allocate at least one eden region");
 297   assert(young_list_target_length >= absolute_min_length, "post-condition");
 298 
 299   result.first = young_list_target_length;
 300   return result;
 301 }
 302 
 303 uint
 304 G1Policy::calculate_young_list_target_length(size_t rs_length,
 305                                                     uint base_min_length,
 306                                                     uint desired_min_length,
 307                                                     uint desired_max_length) const {
 308   assert(use_adaptive_young_list_length(), "pre-condition");
 309   assert(collector_state()->in_young_only_phase(), "only call this for young GCs");
 310 
 311   // In case some edge-condition makes the desired max length too small...
 312   if (desired_max_length <= desired_min_length) {
 313     return desired_min_length;
 314   }
 315 
 316   // We'll adjust min_young_length and max_young_length not to include
 317   // the already allocated young regions (i.e., so they reflect the
 318   // min and max eden regions we'll allocate). The base_min_length
 319   // will be reflected in the predictions by the
 320   // survivor_regions_evac_time prediction.
 321   assert(desired_min_length > base_min_length, "invariant");
 322   uint min_young_length = desired_min_length - base_min_length;
 323   assert(desired_max_length > base_min_length, "invariant");
 324   uint max_young_length = desired_max_length - base_min_length;
 325 
 326   const double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0;
 327   const double survivor_regions_evac_time = predict_survivor_regions_evac_time();
 328   const size_t pending_cards = _analytics->predict_pending_cards();
 329   const size_t adj_rs_length = rs_length + _analytics->predict_rs_length_diff();
 330   const size_t scanned_cards = _analytics->predict_card_num(adj_rs_length, true /* for_young_gc */);
 331   const double base_time_ms =
 332     predict_base_elapsed_time_ms(pending_cards, scanned_cards) +
 333     survivor_regions_evac_time;
 334   const uint available_free_regions = _free_regions_at_end_of_collection;
 335   const uint base_free_regions =
 336     available_free_regions > _reserve_regions ? available_free_regions - _reserve_regions : 0;
 337 
 338   // Here, we will make sure that the shortest young length that
 339   // makes sense fits within the target pause time.
 340 
 341   G1YoungLengthPredictor p(collector_state()->mark_or_rebuild_in_progress(),
 342                            base_time_ms,
 343                            base_free_regions,
 344                            target_pause_time_ms,
 345                            this);
 346   if (p.will_fit(min_young_length)) {
 347     // The shortest young length will fit into the target pause time;
 348     // we'll now check whether the absolute maximum number of young
 349     // regions will fit in the target pause time. If not, we'll do
 350     // a binary search between min_young_length and max_young_length.


 397     }
 398   } else {
 399     // Even the minimum length doesn't fit into the pause time
 400     // target, return it as the result nevertheless.
 401   }
 402   return base_min_length + min_young_length;
 403 }
 404 
 405 double G1Policy::predict_survivor_regions_evac_time() const {
 406   double survivor_regions_evac_time = 0.0;
 407   const GrowableArray<HeapRegion*>* survivor_regions = _g1h->survivor()->regions();
 408 
 409   for (GrowableArrayIterator<HeapRegion*> it = survivor_regions->begin();
 410        it != survivor_regions->end();
 411        ++it) {
 412     survivor_regions_evac_time += predict_region_elapsed_time_ms(*it, collector_state()->in_young_only_phase());
 413   }
 414   return survivor_regions_evac_time;
 415 }
 416 
 417 void G1Policy::revise_young_list_target_length_if_necessary(size_t rs_length) {
 418   guarantee(use_adaptive_young_list_length(), "should not call this otherwise" );
 419 
 420   if (rs_length > _rs_length_prediction) {
 421     // add 10% to avoid having to recalculate often
 422     size_t rs_length_prediction = rs_length * 1100 / 1000;
 423     update_rs_length_prediction(rs_length_prediction);
 424 
 425     update_young_list_max_and_target_length(rs_length_prediction);
 426   }
 427 }
 428 
 429 void G1Policy::update_rs_length_prediction() {
 430   update_rs_length_prediction(_analytics->predict_rs_length());
 431 }
 432 
 433 void G1Policy::update_rs_length_prediction(size_t prediction) {
 434   if (collector_state()->in_young_only_phase() && use_adaptive_young_list_length()) {
 435     _rs_length_prediction = prediction;
 436   }
 437 }
 438 
 439 void G1Policy::record_full_collection_start() {
 440   _full_collection_start_sec = os::elapsedTime();
 441   // Release the future to-space so that it is available for compaction into.
 442   collector_state()->set_in_young_only_phase(false);
 443   collector_state()->set_in_full_gc(true);
 444   _collection_set->clear_candidates();
 445 }
 446 
 447 void G1Policy::record_full_collection_end() {
 448   // Consider this like a collection pause for the purposes of allocation
 449   // since last pause.
 450   double end_sec = os::elapsedTime();
 451   double full_gc_time_sec = end_sec - _full_collection_start_sec;
 452   double full_gc_time_ms = full_gc_time_sec * 1000.0;
 453 
 454   _analytics->update_recent_gc_times(end_sec, full_gc_time_ms);
 455 
 456   collector_state()->set_in_full_gc(false);
 457 
 458   // "Nuke" the heuristics that control the young/mixed GC
 459   // transitions and make sure we start with young GCs after the Full GC.
 460   collector_state()->set_in_young_only_phase(true);
 461   collector_state()->set_in_young_gc_before_mixed(false);
 462   collector_state()->set_initiate_conc_mark_if_possible(need_to_start_conc_mark("end of Full GC", 0));
 463   collector_state()->set_in_initial_mark_gc(false);
 464   collector_state()->set_mark_or_rebuild_in_progress(false);
 465   collector_state()->set_clearing_next_bitmap(false);
 466 
 467   _short_lived_surv_rate_group->start_adding_regions();
 468   // also call this on any additional surv rate groups
 469 
 470   _free_regions_at_end_of_collection = _g1h->num_free_regions();
 471   // Reset survivors SurvRateGroup.
 472   _survivor_surv_rate_group->reset();
 473   update_young_list_max_and_target_length();
 474   update_rs_length_prediction();
 475 
 476   _bytes_allocated_in_old_since_last_gc = 0;
 477 
 478   record_pause(FullGC, _full_collection_start_sec, end_sec);
 479 }
 480 
 481 void G1Policy::record_collection_pause_start(double start_time_sec) {
 482   // We only need to do this here as the policy will only be applied
 483   // to the GC we're about to start. so, no point is calculating this
 484   // every time we calculate / recalculate the target young length.
 485   update_survivors_policy();
 486 
 487   assert(max_survivor_regions() + _g1h->num_used_regions() <= _g1h->max_regions(),
 488          "Maximum survivor regions %u plus used regions %u exceeds max regions %u",
 489          max_survivor_regions(), _g1h->num_used_regions(), _g1h->max_regions());
 490   assert_used_and_recalculate_used_equal(_g1h);
 491 
 492   phase_times()->record_cur_collection_start_sec(start_time_sec);
 493   _pending_cards = _g1h->pending_card_num();
 494 


 555     return false;
 556   }
 557 
 558   size_t marking_initiating_used_threshold = _ihop_control->get_conc_mark_start_threshold();
 559 
 560   size_t cur_used_bytes = _g1h->non_young_capacity_bytes();
 561   size_t alloc_byte_size = alloc_word_size * HeapWordSize;
 562   size_t marking_request_bytes = cur_used_bytes + alloc_byte_size;
 563 
 564   bool result = false;
 565   if (marking_request_bytes > marking_initiating_used_threshold) {
 566     result = collector_state()->in_young_only_phase() && !collector_state()->in_young_gc_before_mixed();
 567     log_debug(gc, ergo, ihop)("%s occupancy: " SIZE_FORMAT "B allocation request: " SIZE_FORMAT "B threshold: " SIZE_FORMAT "B (%1.2f) source: %s",
 568                               result ? "Request concurrent cycle initiation (occupancy higher than threshold)" : "Do not request concurrent cycle initiation (still doing mixed collections)",
 569                               cur_used_bytes, alloc_byte_size, marking_initiating_used_threshold, (double) marking_initiating_used_threshold / _g1h->capacity() * 100, source);
 570   }
 571 
 572   return result;
 573 }
 574 
 575 double G1Policy::logged_cards_processing_time() const {
 576   double all_cards_processing_time = average_time_ms(G1GCPhaseTimes::ScanHR) + average_time_ms(G1GCPhaseTimes::OptScanHR);
 577   size_t logged_dirty_cards = phase_times()->sum_thread_work_items(G1GCPhaseTimes::MergeLB, G1GCPhaseTimes::MergeLBDirtyCards);
 578   size_t scan_heap_roots_cards = phase_times()->sum_thread_work_items(G1GCPhaseTimes::ScanHR, G1GCPhaseTimes::ScanHRScannedCards) +
 579                                  phase_times()->sum_thread_work_items(G1GCPhaseTimes::OptScanHR, G1GCPhaseTimes::ScanHRScannedCards);
 580   // This may happen if there are duplicate cards in different log buffers.
 581   if (logged_dirty_cards > scan_heap_roots_cards) {
 582     return all_cards_processing_time + average_time_ms(G1GCPhaseTimes::MergeLB);
 583   }
 584   return (all_cards_processing_time * logged_dirty_cards / scan_heap_roots_cards) + average_time_ms(G1GCPhaseTimes::MergeLB);
 585 }
 586 
 587 // Anything below that is considered to be zero
 588 #define MIN_TIMER_GRANULARITY 0.0000001
 589 
 590 void G1Policy::record_collection_pause_end(double pause_time_ms, size_t heap_used_bytes_before_gc) {
 591   G1GCPhaseTimes* p = phase_times();
 592 
 593   double end_time_sec = os::elapsedTime();
 594 
 595   assert_used_and_recalculate_used_equal(_g1h);
 596   size_t cur_used_bytes = _g1h->used();
 597   bool this_pause_included_initial_mark = false;
 598   bool this_pause_was_young_only = collector_state()->in_young_only_phase();
 599 
 600   bool update_stats = !_g1h->evacuation_failed();
 601 
 602   record_pause(young_gc_pause_kind(), end_time_sec - pause_time_ms / 1000.0, end_time_sec);
 603 
 604   _collection_pause_end_millis = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
 605 
 606   this_pause_included_initial_mark = collector_state()->in_initial_mark_gc();
 607   if (this_pause_included_initial_mark) {
 608     record_concurrent_mark_init_end(0.0);
 609   } else {
 610     maybe_start_marking();
 611   }
 612 


 642     // This has been the young GC before we start doing mixed GCs. We already
 643     // decided to start mixed GCs much earlier, so there is nothing to do except
 644     // advancing the state.
 645     collector_state()->set_in_young_only_phase(false);
 646     collector_state()->set_in_young_gc_before_mixed(false);
 647   } else if (!this_pause_was_young_only) {
 648     // This is a mixed GC. Here we decide whether to continue doing more
 649     // mixed GCs or not.
 650     if (!next_gc_should_be_mixed("continue mixed GCs",
 651                                  "do not continue mixed GCs")) {
 652       collector_state()->set_in_young_only_phase(true);
 653 
 654       clear_collection_set_candidates();
 655       maybe_start_marking();
 656     }
 657   }
 658 
 659   _short_lived_surv_rate_group->start_adding_regions();
 660   // Do that for any other surv rate groups
 661 
 662   double scan_hcc_time_ms = G1HotCardCache::default_use_cache() ? average_time_ms(G1GCPhaseTimes::MergeHCC) : 0.0;
 663 
 664   if (update_stats) {
 665     double cost_per_logged_card = 0.0;
 666     size_t const pending_logged_cards = p->sum_thread_work_items(G1GCPhaseTimes::MergeLB, G1GCPhaseTimes::MergeLBDirtyCards);
 667     if (pending_logged_cards > 0) {
 668       cost_per_logged_card = logged_cards_processing_time() / pending_logged_cards;
 669       _analytics->report_cost_per_logged_card_ms(cost_per_logged_card);
 670     }
 671     _analytics->report_cost_scan_hcc(scan_hcc_time_ms);
 672 
 673     size_t const total_cards_scanned = p->sum_thread_work_items(G1GCPhaseTimes::ScanHR, G1GCPhaseTimes::ScanHRScannedCards) +
 674                                        p->sum_thread_work_items(G1GCPhaseTimes::OptScanHR, G1GCPhaseTimes::ScanHRScannedCards);
 675     size_t remset_cards_scanned = 0;
 676     // There might have been duplicate log buffer entries in the queues which could
 677     // increase this value beyond the cards scanned. In this case attribute all cards
 678     // to the log buffers.
 679     if (pending_logged_cards <= total_cards_scanned) {
 680       remset_cards_scanned = total_cards_scanned - pending_logged_cards;
 681     }
 682 
 683     double cost_per_remset_card_ms = 0.0;
 684     if (remset_cards_scanned > 10) {
 685       double avg_time_remset_scan = ((average_time_ms(G1GCPhaseTimes::ScanHR) + average_time_ms(G1GCPhaseTimes::OptScanHR)) *
 686                                      remset_cards_scanned / total_cards_scanned) +
 687                                      average_time_ms(G1GCPhaseTimes::MergeER) +
 688                                      average_time_ms(G1GCPhaseTimes::MergeRS) +
 689                                      average_time_ms(G1GCPhaseTimes::OptMergeRS);
 690 
 691       cost_per_remset_card_ms = avg_time_remset_scan / remset_cards_scanned;
 692       _analytics->report_cost_per_remset_card_ms(cost_per_remset_card_ms, this_pause_was_young_only);
 693     }
 694 
 695     if (_max_rs_length > 0) {
 696       double cards_per_entry_ratio =
 697         (double) remset_cards_scanned / (double) _max_rs_length;
 698       _analytics->report_cards_per_entry_ratio(cards_per_entry_ratio, this_pause_was_young_only);
 699     }
 700 
 701     // This is defensive. For a while _max_rs_length could get
 702     // smaller than _recorded_rs_length which was causing
 703     // rs_length_diff to get very large and mess up the RSet length
 704     // predictions. The reason was unsafe concurrent updates to the
 705     // _inc_cset_recorded_rs_length field which the code below guards
 706     // against (see CR 7118202). This bug has now been fixed (see CR
 707     // 7119027). However, I'm still worried that
 708     // _inc_cset_recorded_rs_length might still end up somewhat
 709     // inaccurate. The concurrent refinement thread calculates an
 710     // RSet's length concurrently with other CR threads updating it
 711     // which might cause it to calculate the length incorrectly (if,
 712     // say, it's in mid-coarsening). So I'll leave in the defensive
 713     // conditional below just in case.
 714     size_t rs_length_diff = 0;
 715     size_t recorded_rs_length = _collection_set->recorded_rs_length();
 716     if (_max_rs_length > recorded_rs_length) {
 717       rs_length_diff = _max_rs_length - recorded_rs_length;
 718     }
 719     _analytics->report_rs_length_diff((double) rs_length_diff);
 720 
 721     size_t freed_bytes = heap_used_bytes_before_gc - cur_used_bytes;
 722     size_t copied_bytes = _collection_set->bytes_used_before() - freed_bytes;
 723     double cost_per_byte_ms = 0.0;
 724 
 725     if (copied_bytes > 0) {
 726       cost_per_byte_ms = (average_time_ms(G1GCPhaseTimes::ObjCopy) + average_time_ms(G1GCPhaseTimes::OptObjCopy)) / (double) copied_bytes;
 727       _analytics->report_cost_per_byte_ms(cost_per_byte_ms, collector_state()->mark_or_rebuild_in_progress());
 728     }
 729 
 730     if (_collection_set->young_region_length() > 0) {
 731       _analytics->report_young_other_cost_per_region_ms(young_other_time_ms() /
 732                                                         _collection_set->young_region_length());
 733     }
 734 
 735     if (_collection_set->old_region_length() > 0) {
 736       _analytics->report_non_young_other_cost_per_region_ms(non_young_other_time_ms() /
 737                                                             _collection_set->old_region_length());
 738     }
 739 
 740     _analytics->report_constant_other_time_ms(constant_other_time_ms(pause_time_ms));
 741 
 742     // Do not update RS lengths and the number of pending cards with information from mixed gc:
 743     // these are is wildly different to during young only gc and mess up young gen sizing right
 744     // after the mixed gc phase.
 745     // During mixed gc we do not use them for young gen sizing.
 746     if (this_pause_was_young_only) {
 747       _analytics->report_pending_cards((double) _pending_cards);
 748       _analytics->report_rs_length((double) _max_rs_length);
 749     }
 750   }
 751 
 752   assert(!(this_pause_included_initial_mark && collector_state()->mark_or_rebuild_in_progress()),
 753          "If the last pause has been an initial mark, we should not have been in the marking window");
 754   if (this_pause_included_initial_mark) {
 755     collector_state()->set_mark_or_rebuild_in_progress(true);
 756   }
 757 
 758   _free_regions_at_end_of_collection = _g1h->num_free_regions();
 759 
 760   update_rs_length_prediction();
 761 
 762   // Do not update dynamic IHOP due to G1 periodic collection as it is highly likely
 763   // that in this case we are not running in a "normal" operating mode.
 764   if (_g1h->gc_cause() != GCCause::_g1_periodic_collection) {
 765     // IHOP control wants to know the expected young gen length if it were not
 766     // restrained by the heap reserve. Using the actual length would make the
 767     // prediction too small and the limit the young gen every time we get to the
 768     // predicted target occupancy.
 769     size_t last_unrestrained_young_length = update_young_list_max_and_target_length();
 770 
 771     update_ihop_prediction(app_time_ms / 1000.0,
 772                            _bytes_allocated_in_old_since_last_gc,
 773                            last_unrestrained_young_length * HeapRegion::GrainBytes,
 774                            this_pause_was_young_only);
 775     _bytes_allocated_in_old_since_last_gc = 0;
 776 
 777     _ihop_control->send_trace_event(_g1h->gc_tracer_stw());
 778   } else {
 779     // Any garbage collection triggered as periodic collection resets the time-to-mixed
 780     // measurement. Periodic collection typically means that the application is "inactive", i.e.
 781     // the marking threads may have received an uncharacterisic amount of cpu time
 782     // for completing the marking, i.e. are faster than expected.
 783     // This skews the predicted marking length towards smaller values which might cause
 784     // the mark start being too late.
 785     _initial_mark_to_mixed.reset();
 786   }
 787 
 788   // Note that _mmu_tracker->max_gc_time() returns the time in seconds.
 789   double scan_logged_cards_time_goal_ms = _mmu_tracker->max_gc_time() * MILLIUNITS * G1RSetUpdatingPauseTimePercent / 100.0;
 790 
 791   if (scan_logged_cards_time_goal_ms < scan_hcc_time_ms) {
 792     log_debug(gc, ergo, refine)("Adjust concurrent refinement thresholds (scanning the HCC expected to take longer than Update RS time goal)."
 793                                 "Logged Cards Scan time goal: %1.2fms Scan HCC time: %1.2fms",
 794                                 scan_logged_cards_time_goal_ms, scan_hcc_time_ms);
 795 
 796     scan_logged_cards_time_goal_ms = 0;
 797   } else {
 798     scan_logged_cards_time_goal_ms -= scan_hcc_time_ms;
 799   }
 800 
 801   double const logged_cards_time = logged_cards_processing_time();
 802 
 803   log_debug(gc, ergo, refine)("Concurrent refinement times: Logged Cards Scan time goal: %1.2fms Logged Cards Scan time: %1.2fms HCC time: %1.2fms",
 804                               scan_logged_cards_time_goal_ms, logged_cards_time, scan_hcc_time_ms);
 805 
 806   _g1h->concurrent_refine()->adjust(logged_cards_time,
 807                                     phase_times()->sum_thread_work_items(G1GCPhaseTimes::MergeLB, G1GCPhaseTimes::MergeLBDirtyCards),
 808                                     scan_logged_cards_time_goal_ms);
 809 }
 810 
 811 G1IHOPControl* G1Policy::create_ihop_control(const G1Predictions* predictor){
 812   if (G1UseAdaptiveIHOP) {
 813     return new G1AdaptiveIHOPControl(InitiatingHeapOccupancyPercent,
 814                                      predictor,
 815                                      G1ReservePercent,
 816                                      G1HeapWastePercent);
 817   } else {
 818     return new G1StaticIHOPControl(InitiatingHeapOccupancyPercent);
 819   }
 820 }
 821 
 822 void G1Policy::update_ihop_prediction(double mutator_time_s,
 823                                       size_t mutator_alloc_bytes,
 824                                       size_t young_gen_size,
 825                                       bool this_gc_was_young_only) {
 826   // Always try to update IHOP prediction. Even evacuation failures give information
 827   // about e.g. whether to start IHOP earlier next time.
 828 


 872   double pred = _predictor.get_new_prediction(seq);
 873   if (pred > 1.0) {
 874     pred = 1.0;
 875   }
 876   return pred;
 877 }
 878 
 879 double G1Policy::accum_yg_surv_rate_pred(int age) const {
 880   return _short_lived_surv_rate_group->accum_surv_rate_pred(age);
 881 }
 882 
 883 double G1Policy::predict_base_elapsed_time_ms(size_t pending_cards,
 884                                               size_t scanned_cards) const {
 885   return
 886     _analytics->predict_rs_update_time_ms(pending_cards) +
 887     _analytics->predict_rs_scan_time_ms(scanned_cards, collector_state()->in_young_only_phase()) +
 888     _analytics->predict_constant_other_time_ms();
 889 }
 890 
 891 double G1Policy::predict_base_elapsed_time_ms(size_t pending_cards) const {
 892   size_t rs_length = _analytics->predict_rs_length() + _analytics->predict_rs_length_diff();
 893   size_t card_num = _analytics->predict_card_num(rs_length, collector_state()->in_young_only_phase());
 894   return predict_base_elapsed_time_ms(pending_cards, card_num);
 895 }
 896 
 897 size_t G1Policy::predict_bytes_to_copy(HeapRegion* hr) const {
 898   size_t bytes_to_copy;
 899   if (!hr->is_young()) {
 900     bytes_to_copy = hr->max_live_bytes();
 901   } else {
 902     assert(hr->age_in_surv_rate_group() != -1, "invariant");
 903     int age = hr->age_in_surv_rate_group();
 904     double yg_surv_rate = predict_yg_surv_rate(age, hr->surv_rate_group());
 905     bytes_to_copy = (size_t) (hr->used() * yg_surv_rate);
 906   }
 907   return bytes_to_copy;
 908 }
 909 
 910 double G1Policy::predict_region_elapsed_time_ms(HeapRegion* hr,
 911                                                 bool for_young_gc) const {
 912   size_t rs_length = hr->rem_set()->occupied();




  51 #include "utilities/pair.hpp"
  52 
  53 G1Policy::G1Policy(STWGCTimer* gc_timer) :
  54   _predictor(G1ConfidencePercent / 100.0),
  55   _analytics(new G1Analytics(&_predictor)),
  56   _remset_tracker(),
  57   _mmu_tracker(new G1MMUTrackerQueue(GCPauseIntervalMillis / 1000.0, MaxGCPauseMillis / 1000.0)),
  58   _ihop_control(create_ihop_control(&_predictor)),
  59   _policy_counters(new GCPolicyCounters("GarbageFirst", 1, 2)),
  60   _full_collection_start_sec(0.0),
  61   _collection_pause_end_millis(os::javaTimeNanos() / NANOSECS_PER_MILLISEC),
  62   _young_list_target_length(0),
  63   _young_list_fixed_length(0),
  64   _young_list_max_length(0),
  65   _short_lived_surv_rate_group(new SurvRateGroup()),
  66   _survivor_surv_rate_group(new SurvRateGroup()),
  67   _reserve_factor((double) G1ReservePercent / 100.0),
  68   _reserve_regions(0),
  69   _young_gen_sizer(G1YoungGenSizer::create_gen_sizer()),
  70   _free_regions_at_end_of_collection(0),
  71   _max_rs_lengths(0),
  72   _rs_lengths_prediction(0),
  73   _pending_cards(0),
  74   _bytes_allocated_in_old_since_last_gc(0),
  75   _initial_mark_to_mixed(),
  76   _collection_set(NULL),
  77   _bytes_copied_during_gc(0),
  78   _g1h(NULL),
  79   _phase_times(new G1GCPhaseTimes(gc_timer, ParallelGCThreads)),
  80   _mark_remark_start_sec(0),
  81   _mark_cleanup_start_sec(0),
  82   _tenuring_threshold(MaxTenuringThreshold),
  83   _max_survivor_regions(0),
  84   _survivors_age_table(true)
  85 {
  86 }
  87 
  88 G1Policy::~G1Policy() {
  89   delete _ihop_control;
  90   delete _young_gen_sizer;
  91 }
  92 


 202       double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0;
 203       double alloc_rate_ms = _analytics->predict_alloc_rate_ms();
 204       desired_min_length = (uint) ceil(alloc_rate_ms * when_ms);
 205     } else {
 206       // otherwise we don't have enough info to make the prediction
 207     }
 208   }
 209   desired_min_length += base_min_length;
 210   // make sure we don't go below any user-defined minimum bound
 211   return MAX2(_young_gen_sizer->min_desired_young_length(), desired_min_length);
 212 }
 213 
 214 uint G1Policy::calculate_young_list_desired_max_length() const {
 215   // Here, we might want to also take into account any additional
 216   // constraints (i.e., user-defined minimum bound). Currently, we
 217   // effectively don't set this bound.
 218   return _young_gen_sizer->max_desired_young_length();
 219 }
 220 
 221 uint G1Policy::update_young_list_max_and_target_length() {
 222   return update_young_list_max_and_target_length(_analytics->predict_rs_lengths());
 223 }
 224 
 225 uint G1Policy::update_young_list_max_and_target_length(size_t rs_lengths) {
 226   uint unbounded_target_length = update_young_list_target_length(rs_lengths);
 227   update_max_gc_locker_expansion();
 228   return unbounded_target_length;
 229 }
 230 
 231 uint G1Policy::update_young_list_target_length(size_t rs_lengths) {
 232   YoungTargetLengths young_lengths = young_list_target_lengths(rs_lengths);
 233   _young_list_target_length = young_lengths.first;
 234 
 235   return young_lengths.second;
 236 }
 237 
 238 G1Policy::YoungTargetLengths G1Policy::young_list_target_lengths(size_t rs_lengths) const {
 239   YoungTargetLengths result;
 240 
 241   // Calculate the absolute and desired min bounds first.
 242 
 243   // This is how many young regions we already have (currently: the survivors).
 244   const uint base_min_length = _g1h->survivor_regions_count();
 245   uint desired_min_length = calculate_young_list_desired_min_length(base_min_length);
 246   // This is the absolute minimum young length. Ensure that we
 247   // will at least have one eden region available for allocation.
 248   uint absolute_min_length = base_min_length + MAX2(_g1h->eden_regions_count(), (uint)1);
 249   // If we shrank the young list target it should not shrink below the current size.
 250   desired_min_length = MAX2(desired_min_length, absolute_min_length);
 251   // Calculate the absolute and desired max bounds.
 252 
 253   uint desired_max_length = calculate_young_list_desired_max_length();
 254 
 255   uint young_list_target_length = 0;
 256   if (use_adaptive_young_list_length()) {
 257     if (collector_state()->in_young_only_phase()) {
 258       young_list_target_length =
 259                         calculate_young_list_target_length(rs_lengths,
 260                                                            base_min_length,
 261                                                            desired_min_length,
 262                                                            desired_max_length);
 263     } else {
 264       // Don't calculate anything and let the code below bound it to
 265       // the desired_min_length, i.e., do the next GC as soon as
 266       // possible to maximize how many old regions we can add to it.
 267     }
 268   } else {
 269     // The user asked for a fixed young gen so we'll fix the young gen
 270     // whether the next GC is young or mixed.
 271     young_list_target_length = _young_list_fixed_length;
 272   }
 273 
 274   result.second = young_list_target_length;
 275 
 276   // We will try our best not to "eat" into the reserve.
 277   uint absolute_max_length = 0;
 278   if (_free_regions_at_end_of_collection > _reserve_regions) {
 279     absolute_max_length = _free_regions_at_end_of_collection - _reserve_regions;


 284 
 285   // Make sure we don't go over the desired max length, nor under the
 286   // desired min length. In case they clash, desired_min_length wins
 287   // which is why that test is second.
 288   if (young_list_target_length > desired_max_length) {
 289     young_list_target_length = desired_max_length;
 290   }
 291   if (young_list_target_length < desired_min_length) {
 292     young_list_target_length = desired_min_length;
 293   }
 294 
 295   assert(young_list_target_length > base_min_length,
 296          "we should be able to allocate at least one eden region");
 297   assert(young_list_target_length >= absolute_min_length, "post-condition");
 298 
 299   result.first = young_list_target_length;
 300   return result;
 301 }
 302 
 303 uint
 304 G1Policy::calculate_young_list_target_length(size_t rs_lengths,
 305                                                     uint base_min_length,
 306                                                     uint desired_min_length,
 307                                                     uint desired_max_length) const {
 308   assert(use_adaptive_young_list_length(), "pre-condition");
 309   assert(collector_state()->in_young_only_phase(), "only call this for young GCs");
 310 
 311   // In case some edge-condition makes the desired max length too small...
 312   if (desired_max_length <= desired_min_length) {
 313     return desired_min_length;
 314   }
 315 
 316   // We'll adjust min_young_length and max_young_length not to include
 317   // the already allocated young regions (i.e., so they reflect the
 318   // min and max eden regions we'll allocate). The base_min_length
 319   // will be reflected in the predictions by the
 320   // survivor_regions_evac_time prediction.
 321   assert(desired_min_length > base_min_length, "invariant");
 322   uint min_young_length = desired_min_length - base_min_length;
 323   assert(desired_max_length > base_min_length, "invariant");
 324   uint max_young_length = desired_max_length - base_min_length;
 325 
 326   const double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0;
 327   const double survivor_regions_evac_time = predict_survivor_regions_evac_time();
 328   const size_t pending_cards = _analytics->predict_pending_cards();
 329   const size_t adj_rs_lengths = rs_lengths + _analytics->predict_rs_length_diff();
 330   const size_t scanned_cards = _analytics->predict_card_num(adj_rs_lengths, true /* for_young_gc */);
 331   const double base_time_ms =
 332     predict_base_elapsed_time_ms(pending_cards, scanned_cards) +
 333     survivor_regions_evac_time;
 334   const uint available_free_regions = _free_regions_at_end_of_collection;
 335   const uint base_free_regions =
 336     available_free_regions > _reserve_regions ? available_free_regions - _reserve_regions : 0;
 337 
 338   // Here, we will make sure that the shortest young length that
 339   // makes sense fits within the target pause time.
 340 
 341   G1YoungLengthPredictor p(collector_state()->mark_or_rebuild_in_progress(),
 342                            base_time_ms,
 343                            base_free_regions,
 344                            target_pause_time_ms,
 345                            this);
 346   if (p.will_fit(min_young_length)) {
 347     // The shortest young length will fit into the target pause time;
 348     // we'll now check whether the absolute maximum number of young
 349     // regions will fit in the target pause time. If not, we'll do
 350     // a binary search between min_young_length and max_young_length.


 397     }
 398   } else {
 399     // Even the minimum length doesn't fit into the pause time
 400     // target, return it as the result nevertheless.
 401   }
 402   return base_min_length + min_young_length;
 403 }
 404 
 405 double G1Policy::predict_survivor_regions_evac_time() const {
 406   double survivor_regions_evac_time = 0.0;
 407   const GrowableArray<HeapRegion*>* survivor_regions = _g1h->survivor()->regions();
 408 
 409   for (GrowableArrayIterator<HeapRegion*> it = survivor_regions->begin();
 410        it != survivor_regions->end();
 411        ++it) {
 412     survivor_regions_evac_time += predict_region_elapsed_time_ms(*it, collector_state()->in_young_only_phase());
 413   }
 414   return survivor_regions_evac_time;
 415 }
 416 
 417 void G1Policy::revise_young_list_target_length_if_necessary(size_t rs_lengths) {
 418   guarantee(use_adaptive_young_list_length(), "should not call this otherwise" );
 419 
 420   if (rs_lengths > _rs_lengths_prediction) {
 421     // add 10% to avoid having to recalculate often
 422     size_t rs_lengths_prediction = rs_lengths * 1100 / 1000;
 423     update_rs_lengths_prediction(rs_lengths_prediction);
 424 
 425     update_young_list_max_and_target_length(rs_lengths_prediction);
 426   }
 427 }
 428 
 429 void G1Policy::update_rs_lengths_prediction() {
 430   update_rs_lengths_prediction(_analytics->predict_rs_lengths());
 431 }
 432 
 433 void G1Policy::update_rs_lengths_prediction(size_t prediction) {
 434   if (collector_state()->in_young_only_phase() && use_adaptive_young_list_length()) {
 435     _rs_lengths_prediction = prediction;
 436   }
 437 }
 438 
 439 void G1Policy::record_full_collection_start() {
 440   _full_collection_start_sec = os::elapsedTime();
 441   // Release the future to-space so that it is available for compaction into.
 442   collector_state()->set_in_young_only_phase(false);
 443   collector_state()->set_in_full_gc(true);
 444   _collection_set->clear_candidates();
 445 }
 446 
 447 void G1Policy::record_full_collection_end() {
 448   // Consider this like a collection pause for the purposes of allocation
 449   // since last pause.
 450   double end_sec = os::elapsedTime();
 451   double full_gc_time_sec = end_sec - _full_collection_start_sec;
 452   double full_gc_time_ms = full_gc_time_sec * 1000.0;
 453 
 454   _analytics->update_recent_gc_times(end_sec, full_gc_time_ms);
 455 
 456   collector_state()->set_in_full_gc(false);
 457 
 458   // "Nuke" the heuristics that control the young/mixed GC
 459   // transitions and make sure we start with young GCs after the Full GC.
 460   collector_state()->set_in_young_only_phase(true);
 461   collector_state()->set_in_young_gc_before_mixed(false);
 462   collector_state()->set_initiate_conc_mark_if_possible(need_to_start_conc_mark("end of Full GC", 0));
 463   collector_state()->set_in_initial_mark_gc(false);
 464   collector_state()->set_mark_or_rebuild_in_progress(false);
 465   collector_state()->set_clearing_next_bitmap(false);
 466 
 467   _short_lived_surv_rate_group->start_adding_regions();
 468   // also call this on any additional surv rate groups
 469 
 470   _free_regions_at_end_of_collection = _g1h->num_free_regions();
 471   // Reset survivors SurvRateGroup.
 472   _survivor_surv_rate_group->reset();
 473   update_young_list_max_and_target_length();
 474   update_rs_lengths_prediction();
 475 
 476   _bytes_allocated_in_old_since_last_gc = 0;
 477 
 478   record_pause(FullGC, _full_collection_start_sec, end_sec);
 479 }
 480 
 481 void G1Policy::record_collection_pause_start(double start_time_sec) {
 482   // We only need to do this here as the policy will only be applied
 483   // to the GC we're about to start. so, no point is calculating this
 484   // every time we calculate / recalculate the target young length.
 485   update_survivors_policy();
 486 
 487   assert(max_survivor_regions() + _g1h->num_used_regions() <= _g1h->max_regions(),
 488          "Maximum survivor regions %u plus used regions %u exceeds max regions %u",
 489          max_survivor_regions(), _g1h->num_used_regions(), _g1h->max_regions());
 490   assert_used_and_recalculate_used_equal(_g1h);
 491 
 492   phase_times()->record_cur_collection_start_sec(start_time_sec);
 493   _pending_cards = _g1h->pending_card_num();
 494 


 555     return false;
 556   }
 557 
 558   size_t marking_initiating_used_threshold = _ihop_control->get_conc_mark_start_threshold();
 559 
 560   size_t cur_used_bytes = _g1h->non_young_capacity_bytes();
 561   size_t alloc_byte_size = alloc_word_size * HeapWordSize;
 562   size_t marking_request_bytes = cur_used_bytes + alloc_byte_size;
 563 
 564   bool result = false;
 565   if (marking_request_bytes > marking_initiating_used_threshold) {
 566     result = collector_state()->in_young_only_phase() && !collector_state()->in_young_gc_before_mixed();
 567     log_debug(gc, ergo, ihop)("%s occupancy: " SIZE_FORMAT "B allocation request: " SIZE_FORMAT "B threshold: " SIZE_FORMAT "B (%1.2f) source: %s",
 568                               result ? "Request concurrent cycle initiation (occupancy higher than threshold)" : "Do not request concurrent cycle initiation (still doing mixed collections)",
 569                               cur_used_bytes, alloc_byte_size, marking_initiating_used_threshold, (double) marking_initiating_used_threshold / _g1h->capacity() * 100, source);
 570   }
 571 
 572   return result;
 573 }
 574 












 575 // Anything below that is considered to be zero
 576 #define MIN_TIMER_GRANULARITY 0.0000001
 577 
 578 void G1Policy::record_collection_pause_end(double pause_time_ms, size_t cards_scanned, size_t heap_used_bytes_before_gc) {


 579   double end_time_sec = os::elapsedTime();
 580 
 581   assert_used_and_recalculate_used_equal(_g1h);
 582   size_t cur_used_bytes = _g1h->used();
 583   bool this_pause_included_initial_mark = false;
 584   bool this_pause_was_young_only = collector_state()->in_young_only_phase();
 585 
 586   bool update_stats = !_g1h->evacuation_failed();
 587 
 588   record_pause(young_gc_pause_kind(), end_time_sec - pause_time_ms / 1000.0, end_time_sec);
 589 
 590   _collection_pause_end_millis = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
 591 
 592   this_pause_included_initial_mark = collector_state()->in_initial_mark_gc();
 593   if (this_pause_included_initial_mark) {
 594     record_concurrent_mark_init_end(0.0);
 595   } else {
 596     maybe_start_marking();
 597   }
 598 


 628     // This has been the young GC before we start doing mixed GCs. We already
 629     // decided to start mixed GCs much earlier, so there is nothing to do except
 630     // advancing the state.
 631     collector_state()->set_in_young_only_phase(false);
 632     collector_state()->set_in_young_gc_before_mixed(false);
 633   } else if (!this_pause_was_young_only) {
 634     // This is a mixed GC. Here we decide whether to continue doing more
 635     // mixed GCs or not.
 636     if (!next_gc_should_be_mixed("continue mixed GCs",
 637                                  "do not continue mixed GCs")) {
 638       collector_state()->set_in_young_only_phase(true);
 639 
 640       clear_collection_set_candidates();
 641       maybe_start_marking();
 642     }
 643   }
 644 
 645   _short_lived_surv_rate_group->start_adding_regions();
 646   // Do that for any other surv rate groups
 647 
 648   double scan_hcc_time_ms = G1HotCardCache::default_use_cache() ? average_time_ms(G1GCPhaseTimes::ScanHCC) : 0.0;
 649 
 650   if (update_stats) {
 651     double cost_per_card_ms = 0.0;
 652     if (_pending_cards > 0) {
 653       cost_per_card_ms = (average_time_ms(G1GCPhaseTimes::UpdateRS)) / (double) _pending_cards;
 654       _analytics->report_cost_per_card_ms(cost_per_card_ms);

 655     }
 656     _analytics->report_cost_scan_hcc(scan_hcc_time_ms);
 657 
 658     double cost_per_entry_ms = 0.0;
 659     if (cards_scanned > 10) {
 660       double avg_time_scan_rs = average_time_ms(G1GCPhaseTimes::ScanRS);
 661       if (this_pause_was_young_only) {
 662         avg_time_scan_rs += average_time_ms(G1GCPhaseTimes::OptScanRS);
 663       }
 664       cost_per_entry_ms = avg_time_scan_rs / cards_scanned;
 665       _analytics->report_cost_per_entry_ms(cost_per_entry_ms, this_pause_was_young_only);












 666     }
 667 
 668     if (_max_rs_lengths > 0) {
 669       double cards_per_entry_ratio =
 670         (double) cards_scanned / (double) _max_rs_lengths;
 671       _analytics->report_cards_per_entry_ratio(cards_per_entry_ratio, this_pause_was_young_only);
 672     }
 673 
 674     // This is defensive. For a while _max_rs_lengths could get
 675     // smaller than _recorded_rs_lengths which was causing
 676     // rs_length_diff to get very large and mess up the RSet length
 677     // predictions. The reason was unsafe concurrent updates to the
 678     // _inc_cset_recorded_rs_lengths field which the code below guards
 679     // against (see CR 7118202). This bug has now been fixed (see CR
 680     // 7119027). However, I'm still worried that
 681     // _inc_cset_recorded_rs_lengths might still end up somewhat
 682     // inaccurate. The concurrent refinement thread calculates an
 683     // RSet's length concurrently with other CR threads updating it
 684     // which might cause it to calculate the length incorrectly (if,
 685     // say, it's in mid-coarsening). So I'll leave in the defensive
 686     // conditional below just in case.
 687     size_t rs_length_diff = 0;
 688     size_t recorded_rs_lengths = _collection_set->recorded_rs_lengths();
 689     if (_max_rs_lengths > recorded_rs_lengths) {
 690       rs_length_diff = _max_rs_lengths - recorded_rs_lengths;
 691     }
 692     _analytics->report_rs_length_diff((double) rs_length_diff);
 693 
 694     size_t freed_bytes = heap_used_bytes_before_gc - cur_used_bytes;
 695     size_t copied_bytes = _collection_set->bytes_used_before() - freed_bytes;
 696     double cost_per_byte_ms = 0.0;
 697 
 698     if (copied_bytes > 0) {
 699       cost_per_byte_ms = (average_time_ms(G1GCPhaseTimes::ObjCopy) + average_time_ms(G1GCPhaseTimes::OptObjCopy)) / (double) copied_bytes;
 700       _analytics->report_cost_per_byte_ms(cost_per_byte_ms, collector_state()->mark_or_rebuild_in_progress());
 701     }
 702 
 703     if (_collection_set->young_region_length() > 0) {
 704       _analytics->report_young_other_cost_per_region_ms(young_other_time_ms() /
 705                                                         _collection_set->young_region_length());
 706     }
 707 
 708     if (_collection_set->old_region_length() > 0) {
 709       _analytics->report_non_young_other_cost_per_region_ms(non_young_other_time_ms() /
 710                                                             _collection_set->old_region_length());
 711     }
 712 
 713     _analytics->report_constant_other_time_ms(constant_other_time_ms(pause_time_ms));
 714 
 715     // Do not update RS lengths and the number of pending cards with information from mixed gc:
 716     // these are is wildly different to during young only gc and mess up young gen sizing right
 717     // after the mixed gc phase.
 718     // During mixed gc we do not use them for young gen sizing.
 719     if (this_pause_was_young_only) {
 720       _analytics->report_pending_cards((double) _pending_cards);
 721       _analytics->report_rs_lengths((double) _max_rs_lengths);
 722     }
 723   }
 724 
 725   assert(!(this_pause_included_initial_mark && collector_state()->mark_or_rebuild_in_progress()),
 726          "If the last pause has been an initial mark, we should not have been in the marking window");
 727   if (this_pause_included_initial_mark) {
 728     collector_state()->set_mark_or_rebuild_in_progress(true);
 729   }
 730 
 731   _free_regions_at_end_of_collection = _g1h->num_free_regions();
 732 
 733   update_rs_lengths_prediction();
 734 
 735   // Do not update dynamic IHOP due to G1 periodic collection as it is highly likely
 736   // that in this case we are not running in a "normal" operating mode.
 737   if (_g1h->gc_cause() != GCCause::_g1_periodic_collection) {
 738     // IHOP control wants to know the expected young gen length if it were not
 739     // restrained by the heap reserve. Using the actual length would make the
 740     // prediction too small and the limit the young gen every time we get to the
 741     // predicted target occupancy.
 742     size_t last_unrestrained_young_length = update_young_list_max_and_target_length();
 743 
 744     update_ihop_prediction(app_time_ms / 1000.0,
 745                            _bytes_allocated_in_old_since_last_gc,
 746                            last_unrestrained_young_length * HeapRegion::GrainBytes,
 747                            this_pause_was_young_only);
 748     _bytes_allocated_in_old_since_last_gc = 0;
 749 
 750     _ihop_control->send_trace_event(_g1h->gc_tracer_stw());
 751   } else {
 752     // Any garbage collection triggered as periodic collection resets the time-to-mixed
 753     // measurement. Periodic collection typically means that the application is "inactive", i.e.
 754     // the marking threads may have received an uncharacterisic amount of cpu time
 755     // for completing the marking, i.e. are faster than expected.
 756     // This skews the predicted marking length towards smaller values which might cause
 757     // the mark start being too late.
 758     _initial_mark_to_mixed.reset();
 759   }
 760 
 761   // Note that _mmu_tracker->max_gc_time() returns the time in seconds.
 762   double update_rs_time_goal_ms = _mmu_tracker->max_gc_time() * MILLIUNITS * G1RSetUpdatingPauseTimePercent / 100.0;
 763 
 764   if (update_rs_time_goal_ms < scan_hcc_time_ms) {
 765     log_debug(gc, ergo, refine)("Adjust concurrent refinement thresholds (scanning the HCC expected to take longer than Update RS time goal)."
 766                                 "Update RS time goal: %1.2fms Scan HCC time: %1.2fms",
 767                                 update_rs_time_goal_ms, scan_hcc_time_ms);
 768 
 769     update_rs_time_goal_ms = 0;
 770   } else {
 771     update_rs_time_goal_ms -= scan_hcc_time_ms;
 772   }
 773   _g1h->concurrent_refine()->adjust(average_time_ms(G1GCPhaseTimes::UpdateRS),
 774                                     phase_times()->sum_thread_work_items(G1GCPhaseTimes::UpdateRS),
 775                                     update_rs_time_goal_ms);






 776 }
 777 
 778 G1IHOPControl* G1Policy::create_ihop_control(const G1Predictions* predictor){
 779   if (G1UseAdaptiveIHOP) {
 780     return new G1AdaptiveIHOPControl(InitiatingHeapOccupancyPercent,
 781                                      predictor,
 782                                      G1ReservePercent,
 783                                      G1HeapWastePercent);
 784   } else {
 785     return new G1StaticIHOPControl(InitiatingHeapOccupancyPercent);
 786   }
 787 }
 788 
 789 void G1Policy::update_ihop_prediction(double mutator_time_s,
 790                                       size_t mutator_alloc_bytes,
 791                                       size_t young_gen_size,
 792                                       bool this_gc_was_young_only) {
 793   // Always try to update IHOP prediction. Even evacuation failures give information
 794   // about e.g. whether to start IHOP earlier next time.
 795 


 839   double pred = _predictor.get_new_prediction(seq);
 840   if (pred > 1.0) {
 841     pred = 1.0;
 842   }
 843   return pred;
 844 }
 845 
 846 double G1Policy::accum_yg_surv_rate_pred(int age) const {
 847   return _short_lived_surv_rate_group->accum_surv_rate_pred(age);
 848 }
 849 
 850 double G1Policy::predict_base_elapsed_time_ms(size_t pending_cards,
 851                                               size_t scanned_cards) const {
 852   return
 853     _analytics->predict_rs_update_time_ms(pending_cards) +
 854     _analytics->predict_rs_scan_time_ms(scanned_cards, collector_state()->in_young_only_phase()) +
 855     _analytics->predict_constant_other_time_ms();
 856 }
 857 
 858 double G1Policy::predict_base_elapsed_time_ms(size_t pending_cards) const {
 859   size_t rs_length = _analytics->predict_rs_lengths() + _analytics->predict_rs_length_diff();
 860   size_t card_num = _analytics->predict_card_num(rs_length, collector_state()->in_young_only_phase());
 861   return predict_base_elapsed_time_ms(pending_cards, card_num);
 862 }
 863 
 864 size_t G1Policy::predict_bytes_to_copy(HeapRegion* hr) const {
 865   size_t bytes_to_copy;
 866   if (!hr->is_young()) {
 867     bytes_to_copy = hr->max_live_bytes();
 868   } else {
 869     assert(hr->age_in_surv_rate_group() != -1, "invariant");
 870     int age = hr->age_in_surv_rate_group();
 871     double yg_surv_rate = predict_yg_surv_rate(age, hr->surv_rate_group());
 872     bytes_to_copy = (size_t) (hr->used() * yg_surv_rate);
 873   }
 874   return bytes_to_copy;
 875 }
 876 
 877 double G1Policy::predict_region_elapsed_time_ms(HeapRegion* hr,
 878                                                 bool for_young_gc) const {
 879   size_t rs_length = hr->rem_set()->occupied();


< prev index next >