484 } else { |
496 } else { |
485 set_adaptive_young_list_length(false); |
497 set_adaptive_young_list_length(false); |
486 _young_list_fixed_length = initial_region_num; |
498 _young_list_fixed_length = initial_region_num; |
487 } |
499 } |
488 _free_regions_at_end_of_collection = _g1->free_regions(); |
500 _free_regions_at_end_of_collection = _g1->free_regions(); |
489 calculate_young_list_min_length(); |
501 update_young_list_target_length(); |
490 guarantee( _young_list_min_length == 0, "invariant, not enough info" ); |
|
491 calculate_young_list_target_length(); |
|
492 |
502 |
493 // We may immediately start allocating regions and placing them on the |
503 // We may immediately start allocating regions and placing them on the |
494 // collection set list. Initialize the per-collection set info |
504 // collection set list. Initialize the per-collection set info |
495 start_incremental_cset_building(); |
505 start_incremental_cset_building(); |
496 } |
506 } |
497 |
507 |
498 // Create the jstat counters for the policy. |
508 // Create the jstat counters for the policy. |
499 void G1CollectorPolicy::initialize_gc_policy_counters() |
509 void G1CollectorPolicy::initialize_gc_policy_counters() { |
500 { |
|
501 _gc_policy_counters = new GCPolicyCounters("GarbageFirst", 1, 3); |
510 _gc_policy_counters = new GCPolicyCounters("GarbageFirst", 1, 3); |
502 } |
511 } |
503 |
512 |
504 void G1CollectorPolicy::calculate_young_list_min_length() { |
513 bool G1CollectorPolicy::predict_will_fit(size_t young_length, |
505 _young_list_min_length = 0; |
514 double base_time_ms, |
506 |
515 size_t base_free_regions, |
507 if (!adaptive_young_list_length()) |
516 double target_pause_time_ms) { |
508 return; |
517 if (young_length >= base_free_regions) { |
509 |
|
510 if (_alloc_rate_ms_seq->num() > 3) { |
|
511 double now_sec = os::elapsedTime(); |
|
512 double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0; |
|
513 double alloc_rate_ms = predict_alloc_rate_ms(); |
|
514 size_t min_regions = (size_t) ceil(alloc_rate_ms * when_ms); |
|
515 size_t current_region_num = _g1->young_list()->length(); |
|
516 _young_list_min_length = min_regions + current_region_num; |
|
517 } |
|
518 } |
|
519 |
|
520 void G1CollectorPolicy::calculate_young_list_target_length() { |
|
521 if (adaptive_young_list_length()) { |
|
522 size_t rs_lengths = (size_t) get_new_prediction(_rs_lengths_seq); |
|
523 calculate_young_list_target_length(rs_lengths); |
|
524 } else { |
|
525 if (full_young_gcs()) |
|
526 _young_list_target_length = _young_list_fixed_length; |
|
527 else |
|
528 _young_list_target_length = _young_list_fixed_length / 2; |
|
529 } |
|
530 |
|
531 // Make sure we allow the application to allocate at least one |
|
532 // region before we need to do a collection again. |
|
533 size_t min_length = _g1->young_list()->length() + 1; |
|
534 _young_list_target_length = MAX2(_young_list_target_length, min_length); |
|
535 calculate_max_gc_locker_expansion(); |
|
536 calculate_survivors_policy(); |
|
537 } |
|
538 |
|
539 void G1CollectorPolicy::calculate_young_list_target_length(size_t rs_lengths) { |
|
540 guarantee( adaptive_young_list_length(), "pre-condition" ); |
|
541 guarantee( !_in_marking_window || !_last_full_young_gc, "invariant" ); |
|
542 |
|
543 double start_time_sec = os::elapsedTime(); |
|
544 size_t min_reserve_perc = MAX2((size_t)2, (size_t)G1ReservePercent); |
|
545 min_reserve_perc = MIN2((size_t) 50, min_reserve_perc); |
|
546 size_t reserve_regions = |
|
547 (size_t) ((double) min_reserve_perc * (double) _g1->n_regions() / 100.0); |
|
548 |
|
549 if (full_young_gcs() && _free_regions_at_end_of_collection > 0) { |
|
550 // we are in fully-young mode and there are free regions in the heap |
|
551 |
|
552 double survivor_regions_evac_time = |
|
553 predict_survivor_regions_evac_time(); |
|
554 |
|
555 double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0; |
|
556 size_t pending_cards = (size_t) get_new_prediction(_pending_cards_seq); |
|
557 size_t adj_rs_lengths = rs_lengths + predict_rs_length_diff(); |
|
558 size_t scanned_cards = predict_young_card_num(adj_rs_lengths); |
|
559 double base_time_ms = predict_base_elapsed_time_ms(pending_cards, scanned_cards) |
|
560 + survivor_regions_evac_time; |
|
561 |
|
562 // the result |
|
563 size_t final_young_length = 0; |
|
564 |
|
565 size_t init_free_regions = |
|
566 MAX2((size_t)0, _free_regions_at_end_of_collection - reserve_regions); |
|
567 |
|
568 // if we're still under the pause target... |
|
569 if (base_time_ms <= target_pause_time_ms) { |
|
570 // We make sure that the shortest young length that makes sense |
|
571 // fits within the target pause time. |
|
572 size_t min_young_length = 1; |
|
573 |
|
574 if (predict_will_fit(min_young_length, base_time_ms, |
|
575 init_free_regions, target_pause_time_ms)) { |
|
576 // The shortest young length will fit within the target pause time; |
|
577 // we'll now check whether the absolute maximum number of young |
|
578 // regions will fit in the target pause time. If not, we'll do |
|
579 // a binary search between min_young_length and max_young_length |
|
580 size_t abs_max_young_length = _free_regions_at_end_of_collection - 1; |
|
581 size_t max_young_length = abs_max_young_length; |
|
582 |
|
583 if (max_young_length > min_young_length) { |
|
584 // Let's check if the initial max young length will fit within the |
|
585 // target pause. If so then there is no need to search for a maximal |
|
586 // young length - we'll return the initial maximum |
|
587 |
|
588 if (predict_will_fit(max_young_length, base_time_ms, |
|
589 init_free_regions, target_pause_time_ms)) { |
|
590 // The maximum young length will satisfy the target pause time. |
|
591 // We are done so set min young length to this maximum length. |
|
592 // The code after the loop will then set final_young_length using |
|
593 // the value cached in the minimum length. |
|
594 min_young_length = max_young_length; |
|
595 } else { |
|
596 // The maximum possible number of young regions will not fit within |
|
597 // the target pause time so let's search.... |
|
598 |
|
599 size_t diff = (max_young_length - min_young_length) / 2; |
|
600 max_young_length = min_young_length + diff; |
|
601 |
|
602 while (max_young_length > min_young_length) { |
|
603 if (predict_will_fit(max_young_length, base_time_ms, |
|
604 init_free_regions, target_pause_time_ms)) { |
|
605 |
|
606 // The current max young length will fit within the target |
|
607 // pause time. Note we do not exit the loop here. By setting |
|
608 // min = max, and then increasing the max below means that |
|
609 // we will continue searching for an upper bound in the |
|
610 // range [max..max+diff] |
|
611 min_young_length = max_young_length; |
|
612 } |
|
613 diff = (max_young_length - min_young_length) / 2; |
|
614 max_young_length = min_young_length + diff; |
|
615 } |
|
616 // the above loop found a maximal young length that will fit |
|
617 // within the target pause time. |
|
618 } |
|
619 assert(min_young_length <= abs_max_young_length, "just checking"); |
|
620 } |
|
621 final_young_length = min_young_length; |
|
622 } |
|
623 } |
|
624 // and we're done! |
|
625 |
|
626 // we should have at least one region in the target young length |
|
627 _young_list_target_length = |
|
628 final_young_length + _recorded_survivor_regions; |
|
629 |
|
630 // let's keep an eye of how long we spend on this calculation |
|
631 // right now, I assume that we'll print it when we need it; we |
|
632 // should really adde it to the breakdown of a pause |
|
633 double end_time_sec = os::elapsedTime(); |
|
634 double elapsed_time_ms = (end_time_sec - start_time_sec) * 1000.0; |
|
635 |
|
636 #ifdef TRACE_CALC_YOUNG_LENGTH |
|
637 // leave this in for debugging, just in case |
|
638 gclog_or_tty->print_cr("target = %1.1lf ms, young = " SIZE_FORMAT ", " |
|
639 "elapsed %1.2lf ms, (%s%s) " SIZE_FORMAT SIZE_FORMAT, |
|
640 target_pause_time_ms, |
|
641 _young_list_target_length |
|
642 elapsed_time_ms, |
|
643 full_young_gcs() ? "full" : "partial", |
|
644 during_initial_mark_pause() ? " i-m" : "", |
|
645 _in_marking_window, |
|
646 _in_marking_window_im); |
|
647 #endif // TRACE_CALC_YOUNG_LENGTH |
|
648 |
|
649 if (_young_list_target_length < _young_list_min_length) { |
|
650 // bummer; this means that, if we do a pause when the maximal |
|
651 // length dictates, we'll violate the pause spacing target (the |
|
652 // min length was calculate based on the application's current |
|
653 // alloc rate); |
|
654 |
|
655 // so, we have to bite the bullet, and allocate the minimum |
|
656 // number. We'll violate our target, but we just can't meet it. |
|
657 |
|
658 #ifdef TRACE_CALC_YOUNG_LENGTH |
|
659 // leave this in for debugging, just in case |
|
660 gclog_or_tty->print_cr("adjusted target length from " |
|
661 SIZE_FORMAT " to " SIZE_FORMAT, |
|
662 _young_list_target_length, _young_list_min_length); |
|
663 #endif // TRACE_CALC_YOUNG_LENGTH |
|
664 |
|
665 _young_list_target_length = _young_list_min_length; |
|
666 } |
|
667 } else { |
|
668 // we are in a partially-young mode or we've run out of regions (due |
|
669 // to evacuation failure) |
|
670 |
|
671 #ifdef TRACE_CALC_YOUNG_LENGTH |
|
672 // leave this in for debugging, just in case |
|
673 gclog_or_tty->print_cr("(partial) setting target to " SIZE_FORMAT |
|
674 _young_list_min_length); |
|
675 #endif // TRACE_CALC_YOUNG_LENGTH |
|
676 // we'll do the pause as soon as possible by choosing the minimum |
|
677 _young_list_target_length = _young_list_min_length; |
|
678 } |
|
679 |
|
680 _rs_lengths_prediction = rs_lengths; |
|
681 } |
|
682 |
|
683 // This is used by: calculate_young_list_target_length(rs_length). It |
|
684 // returns true iff: |
|
685 // the predicted pause time for the given young list will not overflow |
|
686 // the target pause time |
|
687 // and: |
|
688 // the predicted amount of surviving data will not overflow the |
|
689 // the amount of free space available for survivor regions. |
|
690 // |
|
691 bool |
|
692 G1CollectorPolicy::predict_will_fit(size_t young_length, |
|
693 double base_time_ms, |
|
694 size_t init_free_regions, |
|
695 double target_pause_time_ms) { |
|
696 |
|
697 if (young_length >= init_free_regions) |
|
698 // end condition 1: not enough space for the young regions |
518 // end condition 1: not enough space for the young regions |
699 return false; |
519 return false; |
700 |
520 } |
701 double accum_surv_rate_adj = 0.0; |
521 |
702 double accum_surv_rate = |
522 double accum_surv_rate = accum_yg_surv_rate_pred((int)(young_length - 1)); |
703 accum_yg_surv_rate_pred((int)(young_length - 1)) - accum_surv_rate_adj; |
|
704 |
|
705 size_t bytes_to_copy = |
523 size_t bytes_to_copy = |
706 (size_t) (accum_surv_rate * (double) HeapRegion::GrainBytes); |
524 (size_t) (accum_surv_rate * (double) HeapRegion::GrainBytes); |
707 |
|
708 double copy_time_ms = predict_object_copy_time_ms(bytes_to_copy); |
525 double copy_time_ms = predict_object_copy_time_ms(bytes_to_copy); |
709 |
526 double young_other_time_ms = predict_young_other_time_ms(young_length); |
710 double young_other_time_ms = |
527 double pause_time_ms = base_time_ms + copy_time_ms + young_other_time_ms; |
711 predict_young_other_time_ms(young_length); |
528 if (pause_time_ms > target_pause_time_ms) { |
712 |
529 // end condition 2: prediction is over the target pause time |
713 double pause_time_ms = |
|
714 base_time_ms + copy_time_ms + young_other_time_ms; |
|
715 |
|
716 if (pause_time_ms > target_pause_time_ms) |
|
717 // end condition 2: over the target pause time |
|
718 return false; |
530 return false; |
|
531 } |
719 |
532 |
720 size_t free_bytes = |
533 size_t free_bytes = |
721 (init_free_regions - young_length) * HeapRegion::GrainBytes; |
534 (base_free_regions - young_length) * HeapRegion::GrainBytes; |
722 |
535 if ((2.0 * sigma()) * (double) bytes_to_copy > (double) free_bytes) { |
723 if ((2.0 + sigma()) * (double) bytes_to_copy > (double) free_bytes) |
536 // end condition 3: out-of-space (conservatively!) |
724 // end condition 3: out of to-space (conservatively) |
|
725 return false; |
537 return false; |
|
538 } |
726 |
539 |
727 // success! |
540 // success! |
728 return true; |
541 return true; |
|
542 } |
|
543 |
|
544 void G1CollectorPolicy::calculate_reserve(size_t all_regions) { |
|
545 double reserve_regions_d = (double) all_regions * _reserve_factor; |
|
546 // We use ceiling so that if reserve_regions_d is > 0.0 (but |
|
547 // smaller than 1.0) we'll get 1. |
|
548 _reserve_regions = (size_t) ceil(reserve_regions_d); |
|
549 } |
|
550 |
|
551 size_t G1CollectorPolicy::calculate_young_list_desired_min_length( |
|
552 size_t base_min_length) { |
|
553 size_t desired_min_length = 0; |
|
554 if (adaptive_young_list_length()) { |
|
555 if (_alloc_rate_ms_seq->num() > 3) { |
|
556 double now_sec = os::elapsedTime(); |
|
557 double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0; |
|
558 double alloc_rate_ms = predict_alloc_rate_ms(); |
|
559 desired_min_length = (size_t) ceil(alloc_rate_ms * when_ms); |
|
560 } else { |
|
561 // otherwise we don't have enough info to make the prediction |
|
562 } |
|
563 } |
|
564 // Here, we might want to also take into account any additional |
|
565 // constraints (i.e., user-defined minimum bound). Currently, we don't. |
|
566 return base_min_length + desired_min_length; |
|
567 } |
|
568 |
|
569 size_t G1CollectorPolicy::calculate_young_list_desired_max_length() { |
|
570 // Here, we might want to also take into account any additional |
|
571 // constraints (i.e., user-defined minimum bound). Currently, we |
|
572 // effectively don't set this bound. |
|
573 return _g1->n_regions(); |
|
574 } |
|
575 |
|
576 void G1CollectorPolicy::update_young_list_target_length(size_t rs_lengths) { |
|
577 if (rs_lengths == (size_t) -1) { |
|
578 // if it's set to the default value (-1), we should predict it; |
|
579 // otherwise, use the given value. |
|
580 rs_lengths = (size_t) get_new_prediction(_rs_lengths_seq); |
|
581 } |
|
582 |
|
583 // Calculate the absolute and desired min bounds. |
|
584 |
|
585 // This is how many young regions we already have (currently: the survivors). |
|
586 size_t base_min_length = recorded_survivor_regions(); |
|
587 // This is the absolute minimum young length, which ensures that we |
|
588 // can allocate one eden region in the worst-case. |
|
589 size_t absolute_min_length = base_min_length + 1; |
|
590 size_t desired_min_length = |
|
591 calculate_young_list_desired_min_length(base_min_length); |
|
592 if (desired_min_length < absolute_min_length) { |
|
593 desired_min_length = absolute_min_length; |
|
594 } |
|
595 |
|
596 // Calculate the absolute and desired max bounds. |
|
597 |
|
598 // We will try our best not to "eat" into the reserve. |
|
599 size_t absolute_max_length = 0; |
|
600 if (_free_regions_at_end_of_collection > _reserve_regions) { |
|
601 absolute_max_length = _free_regions_at_end_of_collection - _reserve_regions; |
|
602 } |
|
603 size_t desired_max_length = calculate_young_list_desired_max_length(); |
|
604 if (desired_max_length > absolute_max_length) { |
|
605 desired_max_length = absolute_max_length; |
|
606 } |
|
607 |
|
608 size_t young_list_target_length = 0; |
|
609 if (adaptive_young_list_length()) { |
|
610 if (full_young_gcs()) { |
|
611 young_list_target_length = |
|
612 calculate_young_list_target_length(rs_lengths, |
|
613 base_min_length, |
|
614 desired_min_length, |
|
615 desired_max_length); |
|
616 _rs_lengths_prediction = rs_lengths; |
|
617 } else { |
|
618 // Don't calculate anything and let the code below bound it to |
|
619 // the desired_min_length, i.e., do the next GC as soon as |
|
620 // possible to maximize how many old regions we can add to it. |
|
621 } |
|
622 } else { |
|
623 if (full_young_gcs()) { |
|
624 young_list_target_length = _young_list_fixed_length; |
|
625 } else { |
|
626 // A bit arbitrary: during partially-young GCs we allocate half |
|
627 // the young regions to try to add old regions to the CSet. |
|
628 young_list_target_length = _young_list_fixed_length / 2; |
|
629 // We choose to accept that we might go under the desired min |
|
630 // length given that we intentionally ask for a smaller young gen. |
|
631 desired_min_length = absolute_min_length; |
|
632 } |
|
633 } |
|
634 |
|
635 // Make sure we don't go over the desired max length, nor under the |
|
636 // desired min length. In case they clash, desired_min_length wins |
|
637 // which is why that test is second. |
|
638 if (young_list_target_length > desired_max_length) { |
|
639 young_list_target_length = desired_max_length; |
|
640 } |
|
641 if (young_list_target_length < desired_min_length) { |
|
642 young_list_target_length = desired_min_length; |
|
643 } |
|
644 |
|
645 assert(young_list_target_length > recorded_survivor_regions(), |
|
646 "we should be able to allocate at least one eden region"); |
|
647 assert(young_list_target_length >= absolute_min_length, "post-condition"); |
|
648 _young_list_target_length = young_list_target_length; |
|
649 |
|
650 update_max_gc_locker_expansion(); |
|
651 } |
|
652 |
|
653 size_t |
|
654 G1CollectorPolicy::calculate_young_list_target_length(size_t rs_lengths, |
|
655 size_t base_min_length, |
|
656 size_t desired_min_length, |
|
657 size_t desired_max_length) { |
|
658 assert(adaptive_young_list_length(), "pre-condition"); |
|
659 assert(full_young_gcs(), "only call this for fully-young GCs"); |
|
660 |
|
661 // In case some edge-condition makes the desired max length too small... |
|
662 if (desired_max_length <= desired_min_length) { |
|
663 return desired_min_length; |
|
664 } |
|
665 |
|
666 // We'll adjust min_young_length and max_young_length not to include |
|
667 // the already allocated young regions (i.e., so they reflect the |
|
668 // min and max eden regions we'll allocate). The base_min_length |
|
669 // will be reflected in the predictions by the |
|
670 // survivor_regions_evac_time prediction. |
|
671 assert(desired_min_length > base_min_length, "invariant"); |
|
672 size_t min_young_length = desired_min_length - base_min_length; |
|
673 assert(desired_max_length > base_min_length, "invariant"); |
|
674 size_t max_young_length = desired_max_length - base_min_length; |
|
675 |
|
676 double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0; |
|
677 double survivor_regions_evac_time = predict_survivor_regions_evac_time(); |
|
678 size_t pending_cards = (size_t) get_new_prediction(_pending_cards_seq); |
|
679 size_t adj_rs_lengths = rs_lengths + predict_rs_length_diff(); |
|
680 size_t scanned_cards = predict_young_card_num(adj_rs_lengths); |
|
681 double base_time_ms = |
|
682 predict_base_elapsed_time_ms(pending_cards, scanned_cards) + |
|
683 survivor_regions_evac_time; |
|
684 size_t available_free_regions = _free_regions_at_end_of_collection; |
|
685 size_t base_free_regions = 0; |
|
686 if (available_free_regions > _reserve_regions) { |
|
687 base_free_regions = available_free_regions - _reserve_regions; |
|
688 } |
|
689 |
|
690 // Here, we will make sure that the shortest young length that |
|
691 // makes sense fits within the target pause time. |
|
692 |
|
693 if (predict_will_fit(min_young_length, base_time_ms, |
|
694 base_free_regions, target_pause_time_ms)) { |
|
695 // The shortest young length will fit into the target pause time; |
|
696 // we'll now check whether the absolute maximum number of young |
|
697 // regions will fit in the target pause time. If not, we'll do |
|
698 // a binary search between min_young_length and max_young_length. |
|
699 if (predict_will_fit(max_young_length, base_time_ms, |
|
700 base_free_regions, target_pause_time_ms)) { |
|
701 // The maximum young length will fit into the target pause time. |
|
702 // We are done so set min young length to the maximum length (as |
|
703 // the result is assumed to be returned in min_young_length). |
|
704 min_young_length = max_young_length; |
|
705 } else { |
|
706 // The maximum possible number of young regions will not fit within |
|
707 // the target pause time so we'll search for the optimal |
|
708 // length. The loop invariants are: |
|
709 // |
|
710 // min_young_length < max_young_length |
|
711 // min_young_length is known to fit into the target pause time |
|
712 // max_young_length is known not to fit into the target pause time |
|
713 // |
|
714 // Going into the loop we know the above hold as we've just |
|
715 // checked them. Every time around the loop we check whether |
|
716 // the middle value between min_young_length and |
|
717 // max_young_length fits into the target pause time. If it |
|
718 // does, it becomes the new min. If it doesn't, it becomes |
|
719 // the new max. This way we maintain the loop invariants. |
|
720 |
|
721 assert(min_young_length < max_young_length, "invariant"); |
|
722 size_t diff = (max_young_length - min_young_length) / 2; |
|
723 while (diff > 0) { |
|
724 size_t young_length = min_young_length + diff; |
|
725 if (predict_will_fit(young_length, base_time_ms, |
|
726 base_free_regions, target_pause_time_ms)) { |
|
727 min_young_length = young_length; |
|
728 } else { |
|
729 max_young_length = young_length; |
|
730 } |
|
731 assert(min_young_length < max_young_length, "invariant"); |
|
732 diff = (max_young_length - min_young_length) / 2; |
|
733 } |
|
734 // The results is min_young_length which, according to the |
|
735 // loop invariants, should fit within the target pause time. |
|
736 |
|
737 // These are the post-conditions of the binary search above: |
|
738 assert(min_young_length < max_young_length, |
|
739 "otherwise we should have discovered that max_young_length " |
|
740 "fits into the pause target and not done the binary search"); |
|
741 assert(predict_will_fit(min_young_length, base_time_ms, |
|
742 base_free_regions, target_pause_time_ms), |
|
743 "min_young_length, the result of the binary search, should " |
|
744 "fit into the pause target"); |
|
745 assert(!predict_will_fit(min_young_length + 1, base_time_ms, |
|
746 base_free_regions, target_pause_time_ms), |
|
747 "min_young_length, the result of the binary search, should be " |
|
748 "optimal, so no larger length should fit into the pause target"); |
|
749 } |
|
750 } else { |
|
751 // Even the minimum length doesn't fit into the pause time |
|
752 // target, return it as the result nevertheless. |
|
753 } |
|
754 return base_min_length + min_young_length; |
729 } |
755 } |
730 |
756 |
731 double G1CollectorPolicy::predict_survivor_regions_evac_time() { |
757 double G1CollectorPolicy::predict_survivor_regions_evac_time() { |
732 double survivor_regions_evac_time = 0.0; |
758 double survivor_regions_evac_time = 0.0; |
733 for (HeapRegion * r = _recorded_survivor_head; |
759 for (HeapRegion * r = _recorded_survivor_head; |