aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef __clang_major__ aoqi@0: #define ATTRIBUTE_PRINTF(x,y) // FIXME, formats are a mess. aoqi@0: #endif aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "gc_implementation/g1/concurrentG1Refine.hpp" aoqi@0: #include "gc_implementation/g1/concurrentMark.hpp" aoqi@0: #include "gc_implementation/g1/concurrentMarkThread.inline.hpp" aoqi@0: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" aoqi@0: #include "gc_implementation/g1/g1CollectorPolicy.hpp" aoqi@0: #include "gc_implementation/g1/g1ErgoVerbose.hpp" aoqi@0: #include "gc_implementation/g1/g1GCPhaseTimes.hpp" aoqi@0: #include "gc_implementation/g1/g1Log.hpp" aoqi@0: #include "gc_implementation/g1/heapRegionRemSet.hpp" aoqi@0: #include "gc_implementation/shared/gcPolicyCounters.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/java.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "utilities/debug.hpp" aoqi@0: aoqi@0: // Different defaults for different number of GC threads aoqi@0: // They were chosen by running GCOld and SPECjbb on debris with different aoqi@0: // numbers of GC threads and choosing them based on the results aoqi@0: aoqi@0: // all the same aoqi@0: static double rs_length_diff_defaults[] = { aoqi@0: 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 aoqi@0: }; aoqi@0: aoqi@0: static double cost_per_card_ms_defaults[] = { aoqi@0: 0.01, 0.005, 0.005, 0.003, 0.003, 0.002, 0.002, 0.0015 aoqi@0: }; aoqi@0: aoqi@0: // all the same aoqi@0: static double young_cards_per_entry_ratio_defaults[] = { aoqi@0: 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 aoqi@0: }; aoqi@0: aoqi@0: static double cost_per_entry_ms_defaults[] = { aoqi@0: 0.015, 0.01, 0.01, 0.008, 0.008, 0.0055, 0.0055, 0.005 aoqi@0: }; aoqi@0: aoqi@0: static double cost_per_byte_ms_defaults[] = { aoqi@0: 0.00006, 0.00003, 0.00003, 0.000015, 0.000015, 0.00001, 0.00001, 0.000009 aoqi@0: }; aoqi@0: aoqi@0: // these should be pretty consistent aoqi@0: static double constant_other_time_ms_defaults[] = { aoqi@0: 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0 aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: static double young_other_cost_per_region_ms_defaults[] = { aoqi@0: 0.3, 0.2, 0.2, 0.15, 0.15, 0.12, 0.12, 0.1 aoqi@0: }; aoqi@0: aoqi@0: static double non_young_other_cost_per_region_ms_defaults[] = { aoqi@0: 1.0, 0.7, 0.7, 0.5, 0.5, 0.42, 0.42, 0.30 aoqi@0: }; aoqi@0: aoqi@0: G1CollectorPolicy::G1CollectorPolicy() : aoqi@0: _parallel_gc_threads(G1CollectedHeap::use_parallel_gc_threads() aoqi@0: ? ParallelGCThreads : 1), aoqi@0: aoqi@0: _recent_gc_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), aoqi@0: _stop_world_start(0.0), aoqi@0: aoqi@0: _concurrent_mark_remark_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), aoqi@0: _concurrent_mark_cleanup_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)), aoqi@0: aoqi@0: _alloc_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _prev_collection_pause_end_ms(0.0), aoqi@0: _rs_length_diff_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _cost_per_card_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _young_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _mixed_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _cost_per_entry_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _mixed_cost_per_entry_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _cost_per_byte_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _cost_per_byte_ms_during_cm_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _constant_other_time_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _non_young_other_cost_per_region_ms_seq( aoqi@0: new TruncatedSeq(TruncatedSeqLength)), aoqi@0: aoqi@0: _pending_cards_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: _rs_lengths_seq(new TruncatedSeq(TruncatedSeqLength)), aoqi@0: aoqi@0: _pause_time_target_ms((double) MaxGCPauseMillis), aoqi@0: aoqi@0: _gcs_are_young(true), aoqi@0: aoqi@0: _during_marking(false), aoqi@0: _in_marking_window(false), aoqi@0: _in_marking_window_im(false), aoqi@0: aoqi@0: _recent_prev_end_times_for_all_gcs_sec( aoqi@0: new TruncatedSeq(NumPrevPausesForHeuristics)), aoqi@0: aoqi@0: _recent_avg_pause_time_ratio(0.0), aoqi@0: aoqi@0: _initiate_conc_mark_if_possible(false), aoqi@0: _during_initial_mark_pause(false), aoqi@0: _last_young_gc(false), aoqi@0: _last_gc_was_young(false), aoqi@0: aoqi@0: _eden_used_bytes_before_gc(0), aoqi@0: _survivor_used_bytes_before_gc(0), aoqi@0: _heap_used_bytes_before_gc(0), aoqi@0: _metaspace_used_bytes_before_gc(0), aoqi@0: _eden_capacity_bytes_before_gc(0), aoqi@0: _heap_capacity_bytes_before_gc(0), aoqi@0: aoqi@0: _eden_cset_region_length(0), aoqi@0: _survivor_cset_region_length(0), aoqi@0: _old_cset_region_length(0), aoqi@0: aoqi@0: _collection_set(NULL), aoqi@0: _collection_set_bytes_used_before(0), aoqi@0: aoqi@0: // Incremental CSet attributes aoqi@0: _inc_cset_build_state(Inactive), aoqi@0: _inc_cset_head(NULL), aoqi@0: _inc_cset_tail(NULL), aoqi@0: _inc_cset_bytes_used_before(0), aoqi@0: _inc_cset_max_finger(NULL), aoqi@0: _inc_cset_recorded_rs_lengths(0), aoqi@0: _inc_cset_recorded_rs_lengths_diffs(0), aoqi@0: _inc_cset_predicted_elapsed_time_ms(0.0), aoqi@0: _inc_cset_predicted_elapsed_time_ms_diffs(0.0), aoqi@0: aoqi@0: #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away aoqi@0: #pragma warning( disable:4355 ) // 'this' : used in base member initializer list aoqi@0: #endif // _MSC_VER aoqi@0: aoqi@0: _short_lived_surv_rate_group(new SurvRateGroup(this, "Short Lived", aoqi@0: G1YoungSurvRateNumRegionsSummary)), aoqi@0: _survivor_surv_rate_group(new SurvRateGroup(this, "Survivor", aoqi@0: G1YoungSurvRateNumRegionsSummary)), aoqi@0: // add here any more surv rate groups aoqi@0: _recorded_survivor_regions(0), aoqi@0: _recorded_survivor_head(NULL), aoqi@0: _recorded_survivor_tail(NULL), aoqi@0: _survivors_age_table(true), aoqi@0: aoqi@0: _gc_overhead_perc(0.0) { aoqi@0: aoqi@0: // Set up the region size and associated fields. Given that the aoqi@0: // policy is created before the heap, we have to set this up here, aoqi@0: // so it's done as soon as possible. aoqi@0: aoqi@0: // It would have been natural to pass initial_heap_byte_size() and aoqi@0: // max_heap_byte_size() to setup_heap_region_size() but those have aoqi@0: // not been set up at this point since they should be aligned with aoqi@0: // the region size. So, there is a circular dependency here. We base aoqi@0: // the region size on the heap size, but the heap size should be aoqi@0: // aligned with the region size. To get around this we use the aoqi@0: // unaligned values for the heap. aoqi@0: HeapRegion::setup_heap_region_size(InitialHeapSize, MaxHeapSize); aoqi@0: HeapRegionRemSet::setup_remset_size(); aoqi@0: aoqi@0: G1ErgoVerbose::initialize(); aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: // Currently, we only use a single switch for all the heuristics. aoqi@0: G1ErgoVerbose::set_enabled(true); aoqi@0: // Given that we don't currently have a verboseness level aoqi@0: // parameter, we'll hardcode this to high. This can be easily aoqi@0: // changed in the future. aoqi@0: G1ErgoVerbose::set_level(ErgoHigh); aoqi@0: } else { aoqi@0: G1ErgoVerbose::set_enabled(false); aoqi@0: } aoqi@0: aoqi@0: // Verify PLAB sizes aoqi@0: const size_t region_size = HeapRegion::GrainWords; aoqi@0: if (YoungPLABSize > region_size || OldPLABSize > region_size) { aoqi@0: char buffer[128]; aoqi@0: jio_snprintf(buffer, sizeof(buffer), "%sPLABSize should be at most "SIZE_FORMAT, aoqi@0: OldPLABSize > region_size ? "Old" : "Young", region_size); aoqi@0: vm_exit_during_initialization(buffer); aoqi@0: } aoqi@0: aoqi@0: _recent_prev_end_times_for_all_gcs_sec->add(os::elapsedTime()); aoqi@0: _prev_collection_pause_end_ms = os::elapsedTime() * 1000.0; aoqi@0: aoqi@0: _phase_times = new G1GCPhaseTimes(_parallel_gc_threads); aoqi@0: aoqi@0: int index = MIN2(_parallel_gc_threads - 1, 7); aoqi@0: aoqi@0: _rs_length_diff_seq->add(rs_length_diff_defaults[index]); aoqi@0: _cost_per_card_ms_seq->add(cost_per_card_ms_defaults[index]); aoqi@0: _young_cards_per_entry_ratio_seq->add( aoqi@0: young_cards_per_entry_ratio_defaults[index]); aoqi@0: _cost_per_entry_ms_seq->add(cost_per_entry_ms_defaults[index]); aoqi@0: _cost_per_byte_ms_seq->add(cost_per_byte_ms_defaults[index]); aoqi@0: _constant_other_time_ms_seq->add(constant_other_time_ms_defaults[index]); aoqi@0: _young_other_cost_per_region_ms_seq->add( aoqi@0: young_other_cost_per_region_ms_defaults[index]); aoqi@0: _non_young_other_cost_per_region_ms_seq->add( aoqi@0: non_young_other_cost_per_region_ms_defaults[index]); aoqi@0: aoqi@0: // Below, we might need to calculate the pause time target based on aoqi@0: // the pause interval. When we do so we are going to give G1 maximum aoqi@0: // flexibility and allow it to do pauses when it needs to. So, we'll aoqi@0: // arrange that the pause interval to be pause time target + 1 to aoqi@0: // ensure that a) the pause time target is maximized with respect to aoqi@0: // the pause interval and b) we maintain the invariant that pause aoqi@0: // time target < pause interval. If the user does not want this aoqi@0: // maximum flexibility, they will have to set the pause interval aoqi@0: // explicitly. aoqi@0: aoqi@0: // First make sure that, if either parameter is set, its value is aoqi@0: // reasonable. aoqi@0: if (!FLAG_IS_DEFAULT(MaxGCPauseMillis)) { aoqi@0: if (MaxGCPauseMillis < 1) { aoqi@0: vm_exit_during_initialization("MaxGCPauseMillis should be " aoqi@0: "greater than 0"); aoqi@0: } aoqi@0: } aoqi@0: if (!FLAG_IS_DEFAULT(GCPauseIntervalMillis)) { aoqi@0: if (GCPauseIntervalMillis < 1) { aoqi@0: vm_exit_during_initialization("GCPauseIntervalMillis should be " aoqi@0: "greater than 0"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Then, if the pause time target parameter was not set, set it to aoqi@0: // the default value. aoqi@0: if (FLAG_IS_DEFAULT(MaxGCPauseMillis)) { aoqi@0: if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) { aoqi@0: // The default pause time target in G1 is 200ms aoqi@0: FLAG_SET_DEFAULT(MaxGCPauseMillis, 200); aoqi@0: } else { aoqi@0: // We do not allow the pause interval to be set without the aoqi@0: // pause time target aoqi@0: vm_exit_during_initialization("GCPauseIntervalMillis cannot be set " aoqi@0: "without setting MaxGCPauseMillis"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Then, if the interval parameter was not set, set it according to aoqi@0: // the pause time target (this will also deal with the case when the aoqi@0: // pause time target is the default value). aoqi@0: if (FLAG_IS_DEFAULT(GCPauseIntervalMillis)) { aoqi@0: FLAG_SET_DEFAULT(GCPauseIntervalMillis, MaxGCPauseMillis + 1); aoqi@0: } aoqi@0: aoqi@0: // Finally, make sure that the two parameters are consistent. aoqi@0: if (MaxGCPauseMillis >= GCPauseIntervalMillis) { aoqi@0: char buffer[256]; aoqi@0: jio_snprintf(buffer, 256, aoqi@0: "MaxGCPauseMillis (%u) should be less than " aoqi@0: "GCPauseIntervalMillis (%u)", aoqi@0: MaxGCPauseMillis, GCPauseIntervalMillis); aoqi@0: vm_exit_during_initialization(buffer); aoqi@0: } aoqi@0: aoqi@0: double max_gc_time = (double) MaxGCPauseMillis / 1000.0; aoqi@0: double time_slice = (double) GCPauseIntervalMillis / 1000.0; aoqi@0: _mmu_tracker = new G1MMUTrackerQueue(time_slice, max_gc_time); aoqi@0: aoqi@0: uintx confidence_perc = G1ConfidencePercent; aoqi@0: // Put an artificial ceiling on this so that it's not set to a silly value. aoqi@0: if (confidence_perc > 100) { aoqi@0: confidence_perc = 100; aoqi@0: warning("G1ConfidencePercent is set to a value that is too large, " aoqi@0: "it's been updated to %u", confidence_perc); aoqi@0: } aoqi@0: _sigma = (double) confidence_perc / 100.0; aoqi@0: aoqi@0: // start conservatively (around 50ms is about right) aoqi@0: _concurrent_mark_remark_times_ms->add(0.05); aoqi@0: _concurrent_mark_cleanup_times_ms->add(0.20); aoqi@0: _tenuring_threshold = MaxTenuringThreshold; aoqi@0: // _max_survivor_regions will be calculated by aoqi@0: // update_young_list_target_length() during initialization. aoqi@0: _max_survivor_regions = 0; aoqi@0: aoqi@0: assert(GCTimeRatio > 0, aoqi@0: "we should have set it to a default value set_g1_gc_flags() " aoqi@0: "if a user set it to 0"); aoqi@0: _gc_overhead_perc = 100.0 * (1.0 / (1.0 + GCTimeRatio)); aoqi@0: aoqi@0: uintx reserve_perc = G1ReservePercent; aoqi@0: // Put an artificial ceiling on this so that it's not set to a silly value. aoqi@0: if (reserve_perc > 50) { aoqi@0: reserve_perc = 50; aoqi@0: warning("G1ReservePercent is set to a value that is too large, " aoqi@0: "it's been updated to %u", reserve_perc); aoqi@0: } aoqi@0: _reserve_factor = (double) reserve_perc / 100.0; aoqi@0: // This will be set when the heap is expanded aoqi@0: // for the first time during initialization. aoqi@0: _reserve_regions = 0; aoqi@0: aoqi@0: _collectionSetChooser = new CollectionSetChooser(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::initialize_alignments() { aoqi@0: _space_alignment = HeapRegion::GrainBytes; aoqi@0: size_t card_table_alignment = GenRemSet::max_alignment_constraint(GenRemSet::CardTable); aoqi@0: size_t page_size = UseLargePages ? os::large_page_size() : os::vm_page_size(); aoqi@0: _heap_alignment = MAX3(card_table_alignment, _space_alignment, page_size); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::initialize_flags() { aoqi@0: if (G1HeapRegionSize != HeapRegion::GrainBytes) { aoqi@0: FLAG_SET_ERGO(uintx, G1HeapRegionSize, HeapRegion::GrainBytes); aoqi@0: } aoqi@0: aoqi@0: if (SurvivorRatio < 1) { aoqi@0: vm_exit_during_initialization("Invalid survivor ratio specified"); aoqi@0: } aoqi@0: CollectorPolicy::initialize_flags(); aoqi@0: _young_gen_sizer = new G1YoungGenSizer(); // Must be after call to initialize_flags aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::post_heap_initialize() { aoqi@0: uintx max_regions = G1CollectedHeap::heap()->max_regions(); aoqi@0: size_t max_young_size = (size_t)_young_gen_sizer->max_young_length(max_regions) * HeapRegion::GrainBytes; aoqi@0: if (max_young_size != MaxNewSize) { aoqi@0: FLAG_SET_ERGO(uintx, MaxNewSize, max_young_size); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: G1YoungGenSizer::G1YoungGenSizer() : _sizer_kind(SizerDefaults), _adaptive_size(true), aoqi@0: _min_desired_young_length(0), _max_desired_young_length(0) { aoqi@0: if (FLAG_IS_CMDLINE(NewRatio)) { aoqi@0: if (FLAG_IS_CMDLINE(NewSize) || FLAG_IS_CMDLINE(MaxNewSize)) { aoqi@0: warning("-XX:NewSize and -XX:MaxNewSize override -XX:NewRatio"); aoqi@0: } else { aoqi@0: _sizer_kind = SizerNewRatio; aoqi@0: _adaptive_size = false; aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (NewSize > MaxNewSize) { aoqi@0: if (FLAG_IS_CMDLINE(MaxNewSize)) { aoqi@0: warning("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). " aoqi@0: "A new max generation size of " SIZE_FORMAT "k will be used.", aoqi@0: NewSize/K, MaxNewSize/K, NewSize/K); aoqi@0: } aoqi@0: MaxNewSize = NewSize; aoqi@0: } aoqi@0: aoqi@0: if (FLAG_IS_CMDLINE(NewSize)) { aoqi@0: _min_desired_young_length = MAX2((uint) (NewSize / HeapRegion::GrainBytes), aoqi@0: 1U); aoqi@0: if (FLAG_IS_CMDLINE(MaxNewSize)) { aoqi@0: _max_desired_young_length = aoqi@0: MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes), aoqi@0: 1U); aoqi@0: _sizer_kind = SizerMaxAndNewSize; aoqi@0: _adaptive_size = _min_desired_young_length == _max_desired_young_length; aoqi@0: } else { aoqi@0: _sizer_kind = SizerNewSizeOnly; aoqi@0: } aoqi@0: } else if (FLAG_IS_CMDLINE(MaxNewSize)) { aoqi@0: _max_desired_young_length = aoqi@0: MAX2((uint) (MaxNewSize / HeapRegion::GrainBytes), aoqi@0: 1U); aoqi@0: _sizer_kind = SizerMaxNewSizeOnly; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: uint G1YoungGenSizer::calculate_default_min_length(uint new_number_of_heap_regions) { aoqi@0: uint default_value = (new_number_of_heap_regions * G1NewSizePercent) / 100; aoqi@0: return MAX2(1U, default_value); aoqi@0: } aoqi@0: aoqi@0: uint G1YoungGenSizer::calculate_default_max_length(uint new_number_of_heap_regions) { aoqi@0: uint default_value = (new_number_of_heap_regions * G1MaxNewSizePercent) / 100; aoqi@0: return MAX2(1U, default_value); aoqi@0: } aoqi@0: aoqi@0: void G1YoungGenSizer::recalculate_min_max_young_length(uint number_of_heap_regions, uint* min_young_length, uint* max_young_length) { aoqi@0: assert(number_of_heap_regions > 0, "Heap must be initialized"); aoqi@0: aoqi@0: switch (_sizer_kind) { aoqi@0: case SizerDefaults: aoqi@0: *min_young_length = calculate_default_min_length(number_of_heap_regions); aoqi@0: *max_young_length = calculate_default_max_length(number_of_heap_regions); aoqi@0: break; aoqi@0: case SizerNewSizeOnly: aoqi@0: *max_young_length = calculate_default_max_length(number_of_heap_regions); aoqi@0: *max_young_length = MAX2(*min_young_length, *max_young_length); aoqi@0: break; aoqi@0: case SizerMaxNewSizeOnly: aoqi@0: *min_young_length = calculate_default_min_length(number_of_heap_regions); aoqi@0: *min_young_length = MIN2(*min_young_length, *max_young_length); aoqi@0: break; aoqi@0: case SizerMaxAndNewSize: aoqi@0: // Do nothing. Values set on the command line, don't update them at runtime. aoqi@0: break; aoqi@0: case SizerNewRatio: aoqi@0: *min_young_length = number_of_heap_regions / (NewRatio + 1); aoqi@0: *max_young_length = *min_young_length; aoqi@0: break; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: assert(*min_young_length <= *max_young_length, "Invalid min/max young gen size values"); aoqi@0: } aoqi@0: aoqi@0: uint G1YoungGenSizer::max_young_length(uint number_of_heap_regions) { aoqi@0: // We need to pass the desired values because recalculation may not update these aoqi@0: // values in some cases. aoqi@0: uint temp = _min_desired_young_length; aoqi@0: uint result = _max_desired_young_length; aoqi@0: recalculate_min_max_young_length(number_of_heap_regions, &temp, &result); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void G1YoungGenSizer::heap_size_changed(uint new_number_of_heap_regions) { aoqi@0: recalculate_min_max_young_length(new_number_of_heap_regions, &_min_desired_young_length, aoqi@0: &_max_desired_young_length); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::init() { aoqi@0: // Set aside an initial future to_space. aoqi@0: _g1 = G1CollectedHeap::heap(); aoqi@0: aoqi@0: assert(Heap_lock->owned_by_self(), "Locking discipline."); aoqi@0: aoqi@0: initialize_gc_policy_counters(); aoqi@0: aoqi@0: if (adaptive_young_list_length()) { aoqi@0: _young_list_fixed_length = 0; aoqi@0: } else { aoqi@0: _young_list_fixed_length = _young_gen_sizer->min_desired_young_length(); aoqi@0: } aoqi@0: _free_regions_at_end_of_collection = _g1->free_regions(); aoqi@0: update_young_list_target_length(); aoqi@0: aoqi@0: // We may immediately start allocating regions and placing them on the aoqi@0: // collection set list. Initialize the per-collection set info aoqi@0: start_incremental_cset_building(); aoqi@0: } aoqi@0: aoqi@0: // Create the jstat counters for the policy. aoqi@0: void G1CollectorPolicy::initialize_gc_policy_counters() { aoqi@0: _gc_policy_counters = new GCPolicyCounters("GarbageFirst", 1, 3); aoqi@0: } aoqi@0: aoqi@0: bool G1CollectorPolicy::predict_will_fit(uint young_length, aoqi@0: double base_time_ms, aoqi@0: uint base_free_regions, aoqi@0: double target_pause_time_ms) { aoqi@0: if (young_length >= base_free_regions) { aoqi@0: // end condition 1: not enough space for the young regions aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: double accum_surv_rate = accum_yg_surv_rate_pred((int) young_length - 1); aoqi@0: size_t bytes_to_copy = aoqi@0: (size_t) (accum_surv_rate * (double) HeapRegion::GrainBytes); aoqi@0: double copy_time_ms = predict_object_copy_time_ms(bytes_to_copy); aoqi@0: double young_other_time_ms = predict_young_other_time_ms(young_length); aoqi@0: double pause_time_ms = base_time_ms + copy_time_ms + young_other_time_ms; aoqi@0: if (pause_time_ms > target_pause_time_ms) { aoqi@0: // end condition 2: prediction is over the target pause time aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: size_t free_bytes = aoqi@0: (base_free_regions - young_length) * HeapRegion::GrainBytes; aoqi@0: if ((2.0 * sigma()) * (double) bytes_to_copy > (double) free_bytes) { aoqi@0: // end condition 3: out-of-space (conservatively!) aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // success! aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_new_heap_size(uint new_number_of_regions) { aoqi@0: // re-calculate the necessary reserve aoqi@0: double reserve_regions_d = (double) new_number_of_regions * _reserve_factor; aoqi@0: // We use ceiling so that if reserve_regions_d is > 0.0 (but aoqi@0: // smaller than 1.0) we'll get 1. aoqi@0: _reserve_regions = (uint) ceil(reserve_regions_d); aoqi@0: aoqi@0: _young_gen_sizer->heap_size_changed(new_number_of_regions); aoqi@0: } aoqi@0: aoqi@0: uint G1CollectorPolicy::calculate_young_list_desired_min_length( aoqi@0: uint base_min_length) { aoqi@0: uint desired_min_length = 0; aoqi@0: if (adaptive_young_list_length()) { aoqi@0: if (_alloc_rate_ms_seq->num() > 3) { aoqi@0: double now_sec = os::elapsedTime(); aoqi@0: double when_ms = _mmu_tracker->when_max_gc_sec(now_sec) * 1000.0; aoqi@0: double alloc_rate_ms = predict_alloc_rate_ms(); aoqi@0: desired_min_length = (uint) ceil(alloc_rate_ms * when_ms); aoqi@0: } else { aoqi@0: // otherwise we don't have enough info to make the prediction aoqi@0: } aoqi@0: } aoqi@0: desired_min_length += base_min_length; aoqi@0: // make sure we don't go below any user-defined minimum bound aoqi@0: return MAX2(_young_gen_sizer->min_desired_young_length(), desired_min_length); aoqi@0: } aoqi@0: aoqi@0: uint G1CollectorPolicy::calculate_young_list_desired_max_length() { aoqi@0: // Here, we might want to also take into account any additional aoqi@0: // constraints (i.e., user-defined minimum bound). Currently, we aoqi@0: // effectively don't set this bound. aoqi@0: return _young_gen_sizer->max_desired_young_length(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::update_young_list_target_length(size_t rs_lengths) { aoqi@0: if (rs_lengths == (size_t) -1) { aoqi@0: // if it's set to the default value (-1), we should predict it; aoqi@0: // otherwise, use the given value. aoqi@0: rs_lengths = (size_t) get_new_prediction(_rs_lengths_seq); aoqi@0: } aoqi@0: aoqi@0: // Calculate the absolute and desired min bounds. aoqi@0: aoqi@0: // This is how many young regions we already have (currently: the survivors). aoqi@0: uint base_min_length = recorded_survivor_regions(); aoqi@0: // This is the absolute minimum young length, which ensures that we aoqi@0: // can allocate one eden region in the worst-case. aoqi@0: uint absolute_min_length = base_min_length + 1; aoqi@0: uint desired_min_length = aoqi@0: calculate_young_list_desired_min_length(base_min_length); aoqi@0: if (desired_min_length < absolute_min_length) { aoqi@0: desired_min_length = absolute_min_length; aoqi@0: } aoqi@0: aoqi@0: // Calculate the absolute and desired max bounds. aoqi@0: aoqi@0: // We will try our best not to "eat" into the reserve. aoqi@0: uint absolute_max_length = 0; aoqi@0: if (_free_regions_at_end_of_collection > _reserve_regions) { aoqi@0: absolute_max_length = _free_regions_at_end_of_collection - _reserve_regions; aoqi@0: } aoqi@0: uint desired_max_length = calculate_young_list_desired_max_length(); aoqi@0: if (desired_max_length > absolute_max_length) { aoqi@0: desired_max_length = absolute_max_length; aoqi@0: } aoqi@0: aoqi@0: uint young_list_target_length = 0; aoqi@0: if (adaptive_young_list_length()) { aoqi@0: if (gcs_are_young()) { aoqi@0: young_list_target_length = aoqi@0: calculate_young_list_target_length(rs_lengths, aoqi@0: base_min_length, aoqi@0: desired_min_length, aoqi@0: desired_max_length); aoqi@0: _rs_lengths_prediction = rs_lengths; aoqi@0: } else { aoqi@0: // Don't calculate anything and let the code below bound it to aoqi@0: // the desired_min_length, i.e., do the next GC as soon as aoqi@0: // possible to maximize how many old regions we can add to it. aoqi@0: } aoqi@0: } else { aoqi@0: // The user asked for a fixed young gen so we'll fix the young gen aoqi@0: // whether the next GC is young or mixed. aoqi@0: young_list_target_length = _young_list_fixed_length; aoqi@0: } aoqi@0: aoqi@0: // Make sure we don't go over the desired max length, nor under the aoqi@0: // desired min length. In case they clash, desired_min_length wins aoqi@0: // which is why that test is second. aoqi@0: if (young_list_target_length > desired_max_length) { aoqi@0: young_list_target_length = desired_max_length; aoqi@0: } aoqi@0: if (young_list_target_length < desired_min_length) { aoqi@0: young_list_target_length = desired_min_length; aoqi@0: } aoqi@0: aoqi@0: assert(young_list_target_length > recorded_survivor_regions(), aoqi@0: "we should be able to allocate at least one eden region"); aoqi@0: assert(young_list_target_length >= absolute_min_length, "post-condition"); aoqi@0: _young_list_target_length = young_list_target_length; aoqi@0: aoqi@0: update_max_gc_locker_expansion(); aoqi@0: } aoqi@0: aoqi@0: uint aoqi@0: G1CollectorPolicy::calculate_young_list_target_length(size_t rs_lengths, aoqi@0: uint base_min_length, aoqi@0: uint desired_min_length, aoqi@0: uint desired_max_length) { aoqi@0: assert(adaptive_young_list_length(), "pre-condition"); aoqi@0: assert(gcs_are_young(), "only call this for young GCs"); aoqi@0: aoqi@0: // In case some edge-condition makes the desired max length too small... aoqi@0: if (desired_max_length <= desired_min_length) { aoqi@0: return desired_min_length; aoqi@0: } aoqi@0: aoqi@0: // We'll adjust min_young_length and max_young_length not to include aoqi@0: // the already allocated young regions (i.e., so they reflect the aoqi@0: // min and max eden regions we'll allocate). The base_min_length aoqi@0: // will be reflected in the predictions by the aoqi@0: // survivor_regions_evac_time prediction. aoqi@0: assert(desired_min_length > base_min_length, "invariant"); aoqi@0: uint min_young_length = desired_min_length - base_min_length; aoqi@0: assert(desired_max_length > base_min_length, "invariant"); aoqi@0: uint max_young_length = desired_max_length - base_min_length; aoqi@0: aoqi@0: double target_pause_time_ms = _mmu_tracker->max_gc_time() * 1000.0; aoqi@0: double survivor_regions_evac_time = predict_survivor_regions_evac_time(); aoqi@0: size_t pending_cards = (size_t) get_new_prediction(_pending_cards_seq); aoqi@0: size_t adj_rs_lengths = rs_lengths + predict_rs_length_diff(); aoqi@0: size_t scanned_cards = predict_young_card_num(adj_rs_lengths); aoqi@0: double base_time_ms = aoqi@0: predict_base_elapsed_time_ms(pending_cards, scanned_cards) + aoqi@0: survivor_regions_evac_time; aoqi@0: uint available_free_regions = _free_regions_at_end_of_collection; aoqi@0: uint base_free_regions = 0; aoqi@0: if (available_free_regions > _reserve_regions) { aoqi@0: base_free_regions = available_free_regions - _reserve_regions; aoqi@0: } aoqi@0: aoqi@0: // Here, we will make sure that the shortest young length that aoqi@0: // makes sense fits within the target pause time. aoqi@0: aoqi@0: if (predict_will_fit(min_young_length, base_time_ms, aoqi@0: base_free_regions, target_pause_time_ms)) { aoqi@0: // The shortest young length will fit into the target pause time; aoqi@0: // we'll now check whether the absolute maximum number of young aoqi@0: // regions will fit in the target pause time. If not, we'll do aoqi@0: // a binary search between min_young_length and max_young_length. aoqi@0: if (predict_will_fit(max_young_length, base_time_ms, aoqi@0: base_free_regions, target_pause_time_ms)) { aoqi@0: // The maximum young length will fit into the target pause time. aoqi@0: // We are done so set min young length to the maximum length (as aoqi@0: // the result is assumed to be returned in min_young_length). aoqi@0: min_young_length = max_young_length; aoqi@0: } else { aoqi@0: // The maximum possible number of young regions will not fit within aoqi@0: // the target pause time so we'll search for the optimal aoqi@0: // length. The loop invariants are: aoqi@0: // aoqi@0: // min_young_length < max_young_length aoqi@0: // min_young_length is known to fit into the target pause time aoqi@0: // max_young_length is known not to fit into the target pause time aoqi@0: // aoqi@0: // Going into the loop we know the above hold as we've just aoqi@0: // checked them. Every time around the loop we check whether aoqi@0: // the middle value between min_young_length and aoqi@0: // max_young_length fits into the target pause time. If it aoqi@0: // does, it becomes the new min. If it doesn't, it becomes aoqi@0: // the new max. This way we maintain the loop invariants. aoqi@0: aoqi@0: assert(min_young_length < max_young_length, "invariant"); aoqi@0: uint diff = (max_young_length - min_young_length) / 2; aoqi@0: while (diff > 0) { aoqi@0: uint young_length = min_young_length + diff; aoqi@0: if (predict_will_fit(young_length, base_time_ms, aoqi@0: base_free_regions, target_pause_time_ms)) { aoqi@0: min_young_length = young_length; aoqi@0: } else { aoqi@0: max_young_length = young_length; aoqi@0: } aoqi@0: assert(min_young_length < max_young_length, "invariant"); aoqi@0: diff = (max_young_length - min_young_length) / 2; aoqi@0: } aoqi@0: // The results is min_young_length which, according to the aoqi@0: // loop invariants, should fit within the target pause time. aoqi@0: aoqi@0: // These are the post-conditions of the binary search above: aoqi@0: assert(min_young_length < max_young_length, aoqi@0: "otherwise we should have discovered that max_young_length " aoqi@0: "fits into the pause target and not done the binary search"); aoqi@0: assert(predict_will_fit(min_young_length, base_time_ms, aoqi@0: base_free_regions, target_pause_time_ms), aoqi@0: "min_young_length, the result of the binary search, should " aoqi@0: "fit into the pause target"); aoqi@0: assert(!predict_will_fit(min_young_length + 1, base_time_ms, aoqi@0: base_free_regions, target_pause_time_ms), aoqi@0: "min_young_length, the result of the binary search, should be " aoqi@0: "optimal, so no larger length should fit into the pause target"); aoqi@0: } aoqi@0: } else { aoqi@0: // Even the minimum length doesn't fit into the pause time aoqi@0: // target, return it as the result nevertheless. aoqi@0: } aoqi@0: return base_min_length + min_young_length; aoqi@0: } aoqi@0: aoqi@0: double G1CollectorPolicy::predict_survivor_regions_evac_time() { aoqi@0: double survivor_regions_evac_time = 0.0; aoqi@0: for (HeapRegion * r = _recorded_survivor_head; aoqi@0: r != NULL && r != _recorded_survivor_tail->get_next_young_region(); aoqi@0: r = r->get_next_young_region()) { aoqi@0: survivor_regions_evac_time += predict_region_elapsed_time_ms(r, gcs_are_young()); aoqi@0: } aoqi@0: return survivor_regions_evac_time; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::revise_young_list_target_length_if_necessary() { aoqi@0: guarantee( adaptive_young_list_length(), "should not call this otherwise" ); aoqi@0: aoqi@0: size_t rs_lengths = _g1->young_list()->sampled_rs_lengths(); aoqi@0: if (rs_lengths > _rs_lengths_prediction) { aoqi@0: // add 10% to avoid having to recalculate often aoqi@0: size_t rs_lengths_prediction = rs_lengths * 1100 / 1000; aoqi@0: update_young_list_target_length(rs_lengths_prediction); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: HeapWord* G1CollectorPolicy::mem_allocate_work(size_t size, aoqi@0: bool is_tlab, aoqi@0: bool* gc_overhead_limit_was_exceeded) { aoqi@0: guarantee(false, "Not using this policy feature yet."); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // This method controls how a collector handles one or more aoqi@0: // of its generations being fully allocated. aoqi@0: HeapWord* G1CollectorPolicy::satisfy_failed_allocation(size_t size, aoqi@0: bool is_tlab) { aoqi@0: guarantee(false, "Not using this policy feature yet."); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: bool G1CollectorPolicy::verify_young_ages() { aoqi@0: HeapRegion* head = _g1->young_list()->first_region(); aoqi@0: return aoqi@0: verify_young_ages(head, _short_lived_surv_rate_group); aoqi@0: // also call verify_young_ages on any additional surv rate groups aoqi@0: } aoqi@0: aoqi@0: bool aoqi@0: G1CollectorPolicy::verify_young_ages(HeapRegion* head, aoqi@0: SurvRateGroup *surv_rate_group) { aoqi@0: guarantee( surv_rate_group != NULL, "pre-condition" ); aoqi@0: aoqi@0: const char* name = surv_rate_group->name(); aoqi@0: bool ret = true; aoqi@0: int prev_age = -1; aoqi@0: aoqi@0: for (HeapRegion* curr = head; aoqi@0: curr != NULL; aoqi@0: curr = curr->get_next_young_region()) { aoqi@0: SurvRateGroup* group = curr->surv_rate_group(); aoqi@0: if (group == NULL && !curr->is_survivor()) { aoqi@0: gclog_or_tty->print_cr("## %s: encountered NULL surv_rate_group", name); aoqi@0: ret = false; aoqi@0: } aoqi@0: aoqi@0: if (surv_rate_group == group) { aoqi@0: int age = curr->age_in_surv_rate_group(); aoqi@0: aoqi@0: if (age < 0) { aoqi@0: gclog_or_tty->print_cr("## %s: encountered negative age", name); aoqi@0: ret = false; aoqi@0: } aoqi@0: aoqi@0: if (age <= prev_age) { aoqi@0: gclog_or_tty->print_cr("## %s: region ages are not strictly increasing " aoqi@0: "(%d, %d)", name, age, prev_age); aoqi@0: ret = false; aoqi@0: } aoqi@0: prev_age = age; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return ret; aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: void G1CollectorPolicy::record_full_collection_start() { aoqi@0: _full_collection_start_sec = os::elapsedTime(); aoqi@0: record_heap_size_info_at_start(true /* full */); aoqi@0: // Release the future to-space so that it is available for compaction into. aoqi@0: _g1->set_full_collection(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_full_collection_end() { aoqi@0: // Consider this like a collection pause for the purposes of allocation aoqi@0: // since last pause. aoqi@0: double end_sec = os::elapsedTime(); aoqi@0: double full_gc_time_sec = end_sec - _full_collection_start_sec; aoqi@0: double full_gc_time_ms = full_gc_time_sec * 1000.0; aoqi@0: aoqi@0: _trace_gen1_time_data.record_full_collection(full_gc_time_ms); aoqi@0: aoqi@0: update_recent_gc_times(end_sec, full_gc_time_ms); aoqi@0: aoqi@0: _g1->clear_full_collection(); aoqi@0: aoqi@0: // "Nuke" the heuristics that control the young/mixed GC aoqi@0: // transitions and make sure we start with young GCs after the Full GC. aoqi@0: set_gcs_are_young(true); aoqi@0: _last_young_gc = false; aoqi@0: clear_initiate_conc_mark_if_possible(); aoqi@0: clear_during_initial_mark_pause(); aoqi@0: _in_marking_window = false; aoqi@0: _in_marking_window_im = false; aoqi@0: aoqi@0: _short_lived_surv_rate_group->start_adding_regions(); aoqi@0: // also call this on any additional surv rate groups aoqi@0: aoqi@0: record_survivor_regions(0, NULL, NULL); aoqi@0: aoqi@0: _free_regions_at_end_of_collection = _g1->free_regions(); aoqi@0: // Reset survivors SurvRateGroup. aoqi@0: _survivor_surv_rate_group->reset(); aoqi@0: update_young_list_target_length(); aoqi@0: _collectionSetChooser->clear(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_stop_world_start() { aoqi@0: _stop_world_start = os::elapsedTime(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_collection_pause_start(double start_time_sec) { aoqi@0: // We only need to do this here as the policy will only be applied aoqi@0: // to the GC we're about to start. so, no point is calculating this aoqi@0: // every time we calculate / recalculate the target young length. aoqi@0: update_survivors_policy(); aoqi@0: aoqi@0: assert(_g1->used() == _g1->recalculate_used(), aoqi@0: err_msg("sanity, used: "SIZE_FORMAT" recalculate_used: "SIZE_FORMAT, aoqi@0: _g1->used(), _g1->recalculate_used())); aoqi@0: aoqi@0: double s_w_t_ms = (start_time_sec - _stop_world_start) * 1000.0; aoqi@0: _trace_gen0_time_data.record_start_collection(s_w_t_ms); aoqi@0: _stop_world_start = 0.0; aoqi@0: aoqi@0: record_heap_size_info_at_start(false /* full */); aoqi@0: aoqi@0: phase_times()->record_cur_collection_start_sec(start_time_sec); aoqi@0: _pending_cards = _g1->pending_card_num(); aoqi@0: aoqi@0: _collection_set_bytes_used_before = 0; aoqi@0: _bytes_copied_during_gc = 0; aoqi@0: aoqi@0: _last_gc_was_young = false; aoqi@0: aoqi@0: // do that for any other surv rate groups aoqi@0: _short_lived_surv_rate_group->stop_adding_regions(); aoqi@0: _survivors_age_table.clear(); aoqi@0: aoqi@0: assert( verify_young_ages(), "region age verification" ); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_concurrent_mark_init_end(double aoqi@0: mark_init_elapsed_time_ms) { aoqi@0: _during_marking = true; aoqi@0: assert(!initiate_conc_mark_if_possible(), "we should have cleared it by now"); aoqi@0: clear_during_initial_mark_pause(); aoqi@0: _cur_mark_stop_world_time_ms = mark_init_elapsed_time_ms; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_concurrent_mark_remark_start() { aoqi@0: _mark_remark_start_sec = os::elapsedTime(); aoqi@0: _during_marking = false; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_concurrent_mark_remark_end() { aoqi@0: double end_time_sec = os::elapsedTime(); aoqi@0: double elapsed_time_ms = (end_time_sec - _mark_remark_start_sec)*1000.0; aoqi@0: _concurrent_mark_remark_times_ms->add(elapsed_time_ms); aoqi@0: _cur_mark_stop_world_time_ms += elapsed_time_ms; aoqi@0: _prev_collection_pause_end_ms += elapsed_time_ms; aoqi@0: aoqi@0: _mmu_tracker->add_pause(_mark_remark_start_sec, end_time_sec, true); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_concurrent_mark_cleanup_start() { aoqi@0: _mark_cleanup_start_sec = os::elapsedTime(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_concurrent_mark_cleanup_completed() { aoqi@0: _last_young_gc = true; aoqi@0: _in_marking_window = false; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::record_concurrent_pause() { aoqi@0: if (_stop_world_start > 0.0) { aoqi@0: double yield_ms = (os::elapsedTime() - _stop_world_start) * 1000.0; aoqi@0: _trace_gen0_time_data.record_yield_time(yield_ms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool G1CollectorPolicy::need_to_start_conc_mark(const char* source, size_t alloc_word_size) { aoqi@0: if (_g1->concurrent_mark()->cmThread()->during_cycle()) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: size_t marking_initiating_used_threshold = aoqi@0: (_g1->capacity() / 100) * InitiatingHeapOccupancyPercent; aoqi@0: size_t cur_used_bytes = _g1->non_young_capacity_bytes(); aoqi@0: size_t alloc_byte_size = alloc_word_size * HeapWordSize; aoqi@0: aoqi@0: if ((cur_used_bytes + alloc_byte_size) > marking_initiating_used_threshold) { aoqi@0: if (gcs_are_young() && !_last_young_gc) { aoqi@0: ergo_verbose5(ErgoConcCycles, aoqi@0: "request concurrent cycle initiation", aoqi@0: ergo_format_reason("occupancy higher than threshold") aoqi@0: ergo_format_byte("occupancy") aoqi@0: ergo_format_byte("allocation request") aoqi@0: ergo_format_byte_perc("threshold") aoqi@0: ergo_format_str("source"), aoqi@0: cur_used_bytes, aoqi@0: alloc_byte_size, aoqi@0: marking_initiating_used_threshold, aoqi@0: (double) InitiatingHeapOccupancyPercent, aoqi@0: source); aoqi@0: return true; aoqi@0: } else { aoqi@0: ergo_verbose5(ErgoConcCycles, aoqi@0: "do not request concurrent cycle initiation", aoqi@0: ergo_format_reason("still doing mixed collections") aoqi@0: ergo_format_byte("occupancy") aoqi@0: ergo_format_byte("allocation request") aoqi@0: ergo_format_byte_perc("threshold") aoqi@0: ergo_format_str("source"), aoqi@0: cur_used_bytes, aoqi@0: alloc_byte_size, aoqi@0: marking_initiating_used_threshold, aoqi@0: (double) InitiatingHeapOccupancyPercent, aoqi@0: source); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Anything below that is considered to be zero aoqi@0: #define MIN_TIMER_GRANULARITY 0.0000001 aoqi@0: aoqi@0: void G1CollectorPolicy::record_collection_pause_end(double pause_time_ms, EvacuationInfo& evacuation_info) { aoqi@0: double end_time_sec = os::elapsedTime(); aoqi@0: assert(_cur_collection_pause_used_regions_at_start >= cset_region_length(), aoqi@0: "otherwise, the subtraction below does not make sense"); aoqi@0: size_t rs_size = aoqi@0: _cur_collection_pause_used_regions_at_start - cset_region_length(); aoqi@0: size_t cur_used_bytes = _g1->used(); aoqi@0: assert(cur_used_bytes == _g1->recalculate_used(), "It should!"); aoqi@0: bool last_pause_included_initial_mark = false; aoqi@0: bool update_stats = !_g1->evacuation_failed(); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (G1YoungSurvRateVerbose) { aoqi@0: gclog_or_tty->cr(); aoqi@0: _short_lived_surv_rate_group->print(); aoqi@0: // do that for any other surv rate groups too aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: last_pause_included_initial_mark = during_initial_mark_pause(); aoqi@0: if (last_pause_included_initial_mark) { aoqi@0: record_concurrent_mark_init_end(0.0); aoqi@0: } else if (need_to_start_conc_mark("end of GC")) { aoqi@0: // Note: this might have already been set, if during the last aoqi@0: // pause we decided to start a cycle but at the beginning of aoqi@0: // this pause we decided to postpone it. That's OK. aoqi@0: set_initiate_conc_mark_if_possible(); aoqi@0: } aoqi@0: aoqi@0: _mmu_tracker->add_pause(end_time_sec - pause_time_ms/1000.0, aoqi@0: end_time_sec, false); aoqi@0: aoqi@0: evacuation_info.set_collectionset_used_before(_collection_set_bytes_used_before); aoqi@0: evacuation_info.set_bytes_copied(_bytes_copied_during_gc); aoqi@0: aoqi@0: if (update_stats) { aoqi@0: _trace_gen0_time_data.record_end_collection(pause_time_ms, phase_times()); aoqi@0: // this is where we update the allocation rate of the application aoqi@0: double app_time_ms = aoqi@0: (phase_times()->cur_collection_start_sec() * 1000.0 - _prev_collection_pause_end_ms); aoqi@0: if (app_time_ms < MIN_TIMER_GRANULARITY) { aoqi@0: // This usually happens due to the timer not having the required aoqi@0: // granularity. Some Linuxes are the usual culprits. aoqi@0: // We'll just set it to something (arbitrarily) small. aoqi@0: app_time_ms = 1.0; aoqi@0: } aoqi@0: // We maintain the invariant that all objects allocated by mutator aoqi@0: // threads will be allocated out of eden regions. So, we can use aoqi@0: // the eden region number allocated since the previous GC to aoqi@0: // calculate the application's allocate rate. The only exception aoqi@0: // to that is humongous objects that are allocated separately. But aoqi@0: // given that humongous object allocations do not really affect aoqi@0: // either the pause's duration nor when the next pause will take aoqi@0: // place we can safely ignore them here. aoqi@0: uint regions_allocated = eden_cset_region_length(); aoqi@0: double alloc_rate_ms = (double) regions_allocated / app_time_ms; aoqi@0: _alloc_rate_ms_seq->add(alloc_rate_ms); aoqi@0: aoqi@0: double interval_ms = aoqi@0: (end_time_sec - _recent_prev_end_times_for_all_gcs_sec->oldest()) * 1000.0; aoqi@0: update_recent_gc_times(end_time_sec, pause_time_ms); aoqi@0: _recent_avg_pause_time_ratio = _recent_gc_times_ms->sum()/interval_ms; aoqi@0: if (recent_avg_pause_time_ratio() < 0.0 || aoqi@0: (recent_avg_pause_time_ratio() - 1.0 > 0.0)) { aoqi@0: #ifndef PRODUCT aoqi@0: // Dump info to allow post-facto debugging aoqi@0: gclog_or_tty->print_cr("recent_avg_pause_time_ratio() out of bounds"); aoqi@0: gclog_or_tty->print_cr("-------------------------------------------"); aoqi@0: gclog_or_tty->print_cr("Recent GC Times (ms):"); aoqi@0: _recent_gc_times_ms->dump(); aoqi@0: gclog_or_tty->print_cr("(End Time=%3.3f) Recent GC End Times (s):", end_time_sec); aoqi@0: _recent_prev_end_times_for_all_gcs_sec->dump(); aoqi@0: gclog_or_tty->print_cr("GC = %3.3f, Interval = %3.3f, Ratio = %3.3f", aoqi@0: _recent_gc_times_ms->sum(), interval_ms, recent_avg_pause_time_ratio()); aoqi@0: // In debug mode, terminate the JVM if the user wants to debug at this point. aoqi@0: assert(!G1FailOnFPError, "Debugging data for CR 6898948 has been dumped above"); aoqi@0: #endif // !PRODUCT aoqi@0: // Clip ratio between 0.0 and 1.0, and continue. This will be fixed in aoqi@0: // CR 6902692 by redoing the manner in which the ratio is incrementally computed. aoqi@0: if (_recent_avg_pause_time_ratio < 0.0) { aoqi@0: _recent_avg_pause_time_ratio = 0.0; aoqi@0: } else { aoqi@0: assert(_recent_avg_pause_time_ratio - 1.0 > 0.0, "Ctl-point invariant"); aoqi@0: _recent_avg_pause_time_ratio = 1.0; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool new_in_marking_window = _in_marking_window; aoqi@0: bool new_in_marking_window_im = false; aoqi@0: if (during_initial_mark_pause()) { aoqi@0: new_in_marking_window = true; aoqi@0: new_in_marking_window_im = true; aoqi@0: } aoqi@0: aoqi@0: if (_last_young_gc) { aoqi@0: // This is supposed to to be the "last young GC" before we start aoqi@0: // doing mixed GCs. Here we decide whether to start mixed GCs or not. aoqi@0: aoqi@0: if (!last_pause_included_initial_mark) { aoqi@0: if (next_gc_should_be_mixed("start mixed GCs", aoqi@0: "do not start mixed GCs")) { aoqi@0: set_gcs_are_young(false); aoqi@0: } aoqi@0: } else { aoqi@0: ergo_verbose0(ErgoMixedGCs, aoqi@0: "do not start mixed GCs", aoqi@0: ergo_format_reason("concurrent cycle is about to start")); aoqi@0: } aoqi@0: _last_young_gc = false; aoqi@0: } aoqi@0: aoqi@0: if (!_last_gc_was_young) { aoqi@0: // This is a mixed GC. Here we decide whether to continue doing aoqi@0: // mixed GCs or not. aoqi@0: aoqi@0: if (!next_gc_should_be_mixed("continue mixed GCs", aoqi@0: "do not continue mixed GCs")) { aoqi@0: set_gcs_are_young(true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: _short_lived_surv_rate_group->start_adding_regions(); aoqi@0: // do that for any other surv rate groupsx aoqi@0: aoqi@0: if (update_stats) { aoqi@0: double cost_per_card_ms = 0.0; aoqi@0: if (_pending_cards > 0) { aoqi@0: cost_per_card_ms = phase_times()->average_last_update_rs_time() / (double) _pending_cards; aoqi@0: _cost_per_card_ms_seq->add(cost_per_card_ms); aoqi@0: } aoqi@0: aoqi@0: size_t cards_scanned = _g1->cards_scanned(); aoqi@0: aoqi@0: double cost_per_entry_ms = 0.0; aoqi@0: if (cards_scanned > 10) { aoqi@0: cost_per_entry_ms = phase_times()->average_last_scan_rs_time() / (double) cards_scanned; aoqi@0: if (_last_gc_was_young) { aoqi@0: _cost_per_entry_ms_seq->add(cost_per_entry_ms); aoqi@0: } else { aoqi@0: _mixed_cost_per_entry_ms_seq->add(cost_per_entry_ms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (_max_rs_lengths > 0) { aoqi@0: double cards_per_entry_ratio = aoqi@0: (double) cards_scanned / (double) _max_rs_lengths; aoqi@0: if (_last_gc_was_young) { aoqi@0: _young_cards_per_entry_ratio_seq->add(cards_per_entry_ratio); aoqi@0: } else { aoqi@0: _mixed_cards_per_entry_ratio_seq->add(cards_per_entry_ratio); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // This is defensive. For a while _max_rs_lengths could get aoqi@0: // smaller than _recorded_rs_lengths which was causing aoqi@0: // rs_length_diff to get very large and mess up the RSet length aoqi@0: // predictions. The reason was unsafe concurrent updates to the aoqi@0: // _inc_cset_recorded_rs_lengths field which the code below guards aoqi@0: // against (see CR 7118202). This bug has now been fixed (see CR aoqi@0: // 7119027). However, I'm still worried that aoqi@0: // _inc_cset_recorded_rs_lengths might still end up somewhat aoqi@0: // inaccurate. The concurrent refinement thread calculates an aoqi@0: // RSet's length concurrently with other CR threads updating it aoqi@0: // which might cause it to calculate the length incorrectly (if, aoqi@0: // say, it's in mid-coarsening). So I'll leave in the defensive aoqi@0: // conditional below just in case. aoqi@0: size_t rs_length_diff = 0; aoqi@0: if (_max_rs_lengths > _recorded_rs_lengths) { aoqi@0: rs_length_diff = _max_rs_lengths - _recorded_rs_lengths; aoqi@0: } aoqi@0: _rs_length_diff_seq->add((double) rs_length_diff); aoqi@0: aoqi@0: size_t freed_bytes = _heap_used_bytes_before_gc - cur_used_bytes; aoqi@0: size_t copied_bytes = _collection_set_bytes_used_before - freed_bytes; aoqi@0: double cost_per_byte_ms = 0.0; aoqi@0: aoqi@0: if (copied_bytes > 0) { aoqi@0: cost_per_byte_ms = phase_times()->average_last_obj_copy_time() / (double) copied_bytes; aoqi@0: if (_in_marking_window) { aoqi@0: _cost_per_byte_ms_during_cm_seq->add(cost_per_byte_ms); aoqi@0: } else { aoqi@0: _cost_per_byte_ms_seq->add(cost_per_byte_ms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: double all_other_time_ms = pause_time_ms - aoqi@0: (phase_times()->average_last_update_rs_time() + phase_times()->average_last_scan_rs_time() aoqi@0: + phase_times()->average_last_obj_copy_time() + phase_times()->average_last_termination_time()); aoqi@0: aoqi@0: double young_other_time_ms = 0.0; aoqi@0: if (young_cset_region_length() > 0) { aoqi@0: young_other_time_ms = aoqi@0: phase_times()->young_cset_choice_time_ms() + aoqi@0: phase_times()->young_free_cset_time_ms(); aoqi@0: _young_other_cost_per_region_ms_seq->add(young_other_time_ms / aoqi@0: (double) young_cset_region_length()); aoqi@0: } aoqi@0: double non_young_other_time_ms = 0.0; aoqi@0: if (old_cset_region_length() > 0) { aoqi@0: non_young_other_time_ms = aoqi@0: phase_times()->non_young_cset_choice_time_ms() + aoqi@0: phase_times()->non_young_free_cset_time_ms(); aoqi@0: aoqi@0: _non_young_other_cost_per_region_ms_seq->add(non_young_other_time_ms / aoqi@0: (double) old_cset_region_length()); aoqi@0: } aoqi@0: aoqi@0: double constant_other_time_ms = all_other_time_ms - aoqi@0: (young_other_time_ms + non_young_other_time_ms); aoqi@0: _constant_other_time_ms_seq->add(constant_other_time_ms); aoqi@0: aoqi@0: double survival_ratio = 0.0; aoqi@0: if (_collection_set_bytes_used_before > 0) { aoqi@0: survival_ratio = (double) _bytes_copied_during_gc / aoqi@0: (double) _collection_set_bytes_used_before; aoqi@0: } aoqi@0: aoqi@0: _pending_cards_seq->add((double) _pending_cards); aoqi@0: _rs_lengths_seq->add((double) _max_rs_lengths); aoqi@0: } aoqi@0: aoqi@0: _in_marking_window = new_in_marking_window; aoqi@0: _in_marking_window_im = new_in_marking_window_im; aoqi@0: _free_regions_at_end_of_collection = _g1->free_regions(); aoqi@0: update_young_list_target_length(); aoqi@0: aoqi@0: // Note that _mmu_tracker->max_gc_time() returns the time in seconds. aoqi@0: double update_rs_time_goal_ms = _mmu_tracker->max_gc_time() * MILLIUNITS * G1RSetUpdatingPauseTimePercent / 100.0; aoqi@0: adjust_concurrent_refinement(phase_times()->average_last_update_rs_time(), aoqi@0: phase_times()->sum_last_update_rs_processed_buffers(), update_rs_time_goal_ms); aoqi@0: aoqi@0: _collectionSetChooser->verify(); aoqi@0: } aoqi@0: aoqi@0: #define EXT_SIZE_FORMAT "%.1f%s" aoqi@0: #define EXT_SIZE_PARAMS(bytes) \ aoqi@0: byte_size_in_proper_unit((double)(bytes)), \ aoqi@0: proper_unit_for_byte_size((bytes)) aoqi@0: aoqi@0: void G1CollectorPolicy::record_heap_size_info_at_start(bool full) { aoqi@0: YoungList* young_list = _g1->young_list(); aoqi@0: _eden_used_bytes_before_gc = young_list->eden_used_bytes(); aoqi@0: _survivor_used_bytes_before_gc = young_list->survivor_used_bytes(); aoqi@0: _heap_capacity_bytes_before_gc = _g1->capacity(); aoqi@0: _heap_used_bytes_before_gc = _g1->used(); aoqi@0: _cur_collection_pause_used_regions_at_start = _g1->used_regions(); aoqi@0: aoqi@0: _eden_capacity_bytes_before_gc = aoqi@0: (_young_list_target_length * HeapRegion::GrainBytes) - _survivor_used_bytes_before_gc; aoqi@0: aoqi@0: if (full) { aoqi@0: _metaspace_used_bytes_before_gc = MetaspaceAux::used_bytes(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::print_heap_transition() { aoqi@0: _g1->print_size_transition(gclog_or_tty, aoqi@0: _heap_used_bytes_before_gc, aoqi@0: _g1->used(), aoqi@0: _g1->capacity()); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::print_detailed_heap_transition(bool full) { aoqi@0: YoungList* young_list = _g1->young_list(); aoqi@0: aoqi@0: size_t eden_used_bytes_after_gc = young_list->eden_used_bytes(); aoqi@0: size_t survivor_used_bytes_after_gc = young_list->survivor_used_bytes(); aoqi@0: size_t heap_used_bytes_after_gc = _g1->used(); aoqi@0: aoqi@0: size_t heap_capacity_bytes_after_gc = _g1->capacity(); aoqi@0: size_t eden_capacity_bytes_after_gc = aoqi@0: (_young_list_target_length * HeapRegion::GrainBytes) - survivor_used_bytes_after_gc; aoqi@0: aoqi@0: gclog_or_tty->print( aoqi@0: " [Eden: "EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")->"EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT") " aoqi@0: "Survivors: "EXT_SIZE_FORMAT"->"EXT_SIZE_FORMAT" " aoqi@0: "Heap: "EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")->" aoqi@0: EXT_SIZE_FORMAT"("EXT_SIZE_FORMAT")]", aoqi@0: EXT_SIZE_PARAMS(_eden_used_bytes_before_gc), aoqi@0: EXT_SIZE_PARAMS(_eden_capacity_bytes_before_gc), aoqi@0: EXT_SIZE_PARAMS(eden_used_bytes_after_gc), aoqi@0: EXT_SIZE_PARAMS(eden_capacity_bytes_after_gc), aoqi@0: EXT_SIZE_PARAMS(_survivor_used_bytes_before_gc), aoqi@0: EXT_SIZE_PARAMS(survivor_used_bytes_after_gc), aoqi@0: EXT_SIZE_PARAMS(_heap_used_bytes_before_gc), aoqi@0: EXT_SIZE_PARAMS(_heap_capacity_bytes_before_gc), aoqi@0: EXT_SIZE_PARAMS(heap_used_bytes_after_gc), aoqi@0: EXT_SIZE_PARAMS(heap_capacity_bytes_after_gc)); aoqi@0: aoqi@0: if (full) { aoqi@0: MetaspaceAux::print_metaspace_change(_metaspace_used_bytes_before_gc); aoqi@0: } aoqi@0: aoqi@0: gclog_or_tty->cr(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::adjust_concurrent_refinement(double update_rs_time, aoqi@0: double update_rs_processed_buffers, aoqi@0: double goal_ms) { aoqi@0: DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); aoqi@0: ConcurrentG1Refine *cg1r = G1CollectedHeap::heap()->concurrent_g1_refine(); aoqi@0: aoqi@0: if (G1UseAdaptiveConcRefinement) { aoqi@0: const int k_gy = 3, k_gr = 6; aoqi@0: const double inc_k = 1.1, dec_k = 0.9; aoqi@0: aoqi@0: int g = cg1r->green_zone(); aoqi@0: if (update_rs_time > goal_ms) { aoqi@0: g = (int)(g * dec_k); // Can become 0, that's OK. That would mean a mutator-only processing. aoqi@0: } else { aoqi@0: if (update_rs_time < goal_ms && update_rs_processed_buffers > g) { aoqi@0: g = (int)MAX2(g * inc_k, g + 1.0); aoqi@0: } aoqi@0: } aoqi@0: // Change the refinement threads params aoqi@0: cg1r->set_green_zone(g); aoqi@0: cg1r->set_yellow_zone(g * k_gy); aoqi@0: cg1r->set_red_zone(g * k_gr); aoqi@0: cg1r->reinitialize_threads(); aoqi@0: aoqi@0: int processing_threshold_delta = MAX2((int)(cg1r->green_zone() * sigma()), 1); aoqi@0: int processing_threshold = MIN2(cg1r->green_zone() + processing_threshold_delta, aoqi@0: cg1r->yellow_zone()); aoqi@0: // Change the barrier params aoqi@0: dcqs.set_process_completed_threshold(processing_threshold); aoqi@0: dcqs.set_max_completed_queue(cg1r->red_zone()); aoqi@0: } aoqi@0: aoqi@0: int curr_queue_size = dcqs.completed_buffers_num(); aoqi@0: if (curr_queue_size >= cg1r->yellow_zone()) { aoqi@0: dcqs.set_completed_queue_padding(curr_queue_size); aoqi@0: } else { aoqi@0: dcqs.set_completed_queue_padding(0); aoqi@0: } aoqi@0: dcqs.notify_if_necessary(); aoqi@0: } aoqi@0: aoqi@0: double aoqi@0: G1CollectorPolicy::predict_base_elapsed_time_ms(size_t pending_cards, aoqi@0: size_t scanned_cards) { aoqi@0: return aoqi@0: predict_rs_update_time_ms(pending_cards) + aoqi@0: predict_rs_scan_time_ms(scanned_cards) + aoqi@0: predict_constant_other_time_ms(); aoqi@0: } aoqi@0: aoqi@0: double aoqi@0: G1CollectorPolicy::predict_base_elapsed_time_ms(size_t pending_cards) { aoqi@0: size_t rs_length = predict_rs_length_diff(); aoqi@0: size_t card_num; aoqi@0: if (gcs_are_young()) { aoqi@0: card_num = predict_young_card_num(rs_length); aoqi@0: } else { aoqi@0: card_num = predict_non_young_card_num(rs_length); aoqi@0: } aoqi@0: return predict_base_elapsed_time_ms(pending_cards, card_num); aoqi@0: } aoqi@0: aoqi@0: size_t G1CollectorPolicy::predict_bytes_to_copy(HeapRegion* hr) { aoqi@0: size_t bytes_to_copy; aoqi@0: if (hr->is_marked()) aoqi@0: bytes_to_copy = hr->max_live_bytes(); aoqi@0: else { aoqi@0: assert(hr->is_young() && hr->age_in_surv_rate_group() != -1, "invariant"); aoqi@0: int age = hr->age_in_surv_rate_group(); aoqi@0: double yg_surv_rate = predict_yg_surv_rate(age, hr->surv_rate_group()); aoqi@0: bytes_to_copy = (size_t) ((double) hr->used() * yg_surv_rate); aoqi@0: } aoqi@0: return bytes_to_copy; aoqi@0: } aoqi@0: aoqi@0: double aoqi@0: G1CollectorPolicy::predict_region_elapsed_time_ms(HeapRegion* hr, aoqi@0: bool for_young_gc) { aoqi@0: size_t rs_length = hr->rem_set()->occupied(); aoqi@0: size_t card_num; aoqi@0: aoqi@0: // Predicting the number of cards is based on which type of GC aoqi@0: // we're predicting for. aoqi@0: if (for_young_gc) { aoqi@0: card_num = predict_young_card_num(rs_length); aoqi@0: } else { aoqi@0: card_num = predict_non_young_card_num(rs_length); aoqi@0: } aoqi@0: size_t bytes_to_copy = predict_bytes_to_copy(hr); aoqi@0: aoqi@0: double region_elapsed_time_ms = aoqi@0: predict_rs_scan_time_ms(card_num) + aoqi@0: predict_object_copy_time_ms(bytes_to_copy); aoqi@0: aoqi@0: // The prediction of the "other" time for this region is based aoqi@0: // upon the region type and NOT the GC type. aoqi@0: if (hr->is_young()) { aoqi@0: region_elapsed_time_ms += predict_young_other_time_ms(1); aoqi@0: } else { aoqi@0: region_elapsed_time_ms += predict_non_young_other_time_ms(1); aoqi@0: } aoqi@0: return region_elapsed_time_ms; aoqi@0: } aoqi@0: aoqi@0: void aoqi@0: G1CollectorPolicy::init_cset_region_lengths(uint eden_cset_region_length, aoqi@0: uint survivor_cset_region_length) { aoqi@0: _eden_cset_region_length = eden_cset_region_length; aoqi@0: _survivor_cset_region_length = survivor_cset_region_length; aoqi@0: _old_cset_region_length = 0; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::set_recorded_rs_lengths(size_t rs_lengths) { aoqi@0: _recorded_rs_lengths = rs_lengths; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::update_recent_gc_times(double end_time_sec, aoqi@0: double elapsed_ms) { aoqi@0: _recent_gc_times_ms->add(elapsed_ms); aoqi@0: _recent_prev_end_times_for_all_gcs_sec->add(end_time_sec); aoqi@0: _prev_collection_pause_end_ms = end_time_sec * 1000.0; aoqi@0: } aoqi@0: aoqi@0: size_t G1CollectorPolicy::expansion_amount() { aoqi@0: double recent_gc_overhead = recent_avg_pause_time_ratio() * 100.0; aoqi@0: double threshold = _gc_overhead_perc; aoqi@0: if (recent_gc_overhead > threshold) { aoqi@0: // We will double the existing space, or take aoqi@0: // G1ExpandByPercentOfAvailable % of the available expansion aoqi@0: // space, whichever is smaller, bounded below by a minimum aoqi@0: // expansion (unless that's all that's left.) aoqi@0: const size_t min_expand_bytes = 1*M; aoqi@0: size_t reserved_bytes = _g1->max_capacity(); aoqi@0: size_t committed_bytes = _g1->capacity(); aoqi@0: size_t uncommitted_bytes = reserved_bytes - committed_bytes; aoqi@0: size_t expand_bytes; aoqi@0: size_t expand_bytes_via_pct = aoqi@0: uncommitted_bytes * G1ExpandByPercentOfAvailable / 100; aoqi@0: expand_bytes = MIN2(expand_bytes_via_pct, committed_bytes); aoqi@0: expand_bytes = MAX2(expand_bytes, min_expand_bytes); aoqi@0: expand_bytes = MIN2(expand_bytes, uncommitted_bytes); aoqi@0: aoqi@0: ergo_verbose5(ErgoHeapSizing, aoqi@0: "attempt heap expansion", aoqi@0: ergo_format_reason("recent GC overhead higher than " aoqi@0: "threshold after GC") aoqi@0: ergo_format_perc("recent GC overhead") aoqi@0: ergo_format_perc("threshold") aoqi@0: ergo_format_byte("uncommitted") aoqi@0: ergo_format_byte_perc("calculated expansion amount"), aoqi@0: recent_gc_overhead, threshold, aoqi@0: uncommitted_bytes, aoqi@0: expand_bytes_via_pct, (double) G1ExpandByPercentOfAvailable); aoqi@0: aoqi@0: return expand_bytes; aoqi@0: } else { aoqi@0: return 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::print_tracing_info() const { aoqi@0: _trace_gen0_time_data.print(); aoqi@0: _trace_gen1_time_data.print(); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::print_yg_surv_rate_info() const { aoqi@0: #ifndef PRODUCT aoqi@0: _short_lived_surv_rate_group->print_surv_rate_summary(); aoqi@0: // add this call for any other surv rate groups aoqi@0: #endif // PRODUCT aoqi@0: } aoqi@0: aoqi@0: uint G1CollectorPolicy::max_regions(int purpose) { aoqi@0: switch (purpose) { aoqi@0: case GCAllocForSurvived: aoqi@0: return _max_survivor_regions; aoqi@0: case GCAllocForTenured: aoqi@0: return REGIONS_UNLIMITED; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: return REGIONS_UNLIMITED; aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::update_max_gc_locker_expansion() { aoqi@0: uint expansion_region_num = 0; aoqi@0: if (GCLockerEdenExpansionPercent > 0) { aoqi@0: double perc = (double) GCLockerEdenExpansionPercent / 100.0; aoqi@0: double expansion_region_num_d = perc * (double) _young_list_target_length; aoqi@0: // We use ceiling so that if expansion_region_num_d is > 0.0 (but aoqi@0: // less than 1.0) we'll get 1. aoqi@0: expansion_region_num = (uint) ceil(expansion_region_num_d); aoqi@0: } else { aoqi@0: assert(expansion_region_num == 0, "sanity"); aoqi@0: } aoqi@0: _young_list_max_length = _young_list_target_length + expansion_region_num; aoqi@0: assert(_young_list_target_length <= _young_list_max_length, "post-condition"); aoqi@0: } aoqi@0: aoqi@0: // Calculates survivor space parameters. aoqi@0: void G1CollectorPolicy::update_survivors_policy() { aoqi@0: double max_survivor_regions_d = aoqi@0: (double) _young_list_target_length / (double) SurvivorRatio; aoqi@0: // We use ceiling so that if max_survivor_regions_d is > 0.0 (but aoqi@0: // smaller than 1.0) we'll get 1. aoqi@0: _max_survivor_regions = (uint) ceil(max_survivor_regions_d); aoqi@0: aoqi@0: _tenuring_threshold = _survivors_age_table.compute_tenuring_threshold( aoqi@0: HeapRegion::GrainWords * _max_survivor_regions); aoqi@0: } aoqi@0: aoqi@0: bool G1CollectorPolicy::force_initial_mark_if_outside_cycle( aoqi@0: GCCause::Cause gc_cause) { aoqi@0: bool during_cycle = _g1->concurrent_mark()->cmThread()->during_cycle(); aoqi@0: if (!during_cycle) { aoqi@0: ergo_verbose1(ErgoConcCycles, aoqi@0: "request concurrent cycle initiation", aoqi@0: ergo_format_reason("requested by GC cause") aoqi@0: ergo_format_str("GC cause"), aoqi@0: GCCause::to_string(gc_cause)); aoqi@0: set_initiate_conc_mark_if_possible(); aoqi@0: return true; aoqi@0: } else { aoqi@0: ergo_verbose1(ErgoConcCycles, aoqi@0: "do not request concurrent cycle initiation", aoqi@0: ergo_format_reason("concurrent cycle already in progress") aoqi@0: ergo_format_str("GC cause"), aoqi@0: GCCause::to_string(gc_cause)); aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void aoqi@0: G1CollectorPolicy::decide_on_conc_mark_initiation() { aoqi@0: // We are about to decide on whether this pause will be an aoqi@0: // initial-mark pause. aoqi@0: aoqi@0: // First, during_initial_mark_pause() should not be already set. We aoqi@0: // will set it here if we have to. However, it should be cleared by aoqi@0: // the end of the pause (it's only set for the duration of an aoqi@0: // initial-mark pause). aoqi@0: assert(!during_initial_mark_pause(), "pre-condition"); aoqi@0: aoqi@0: if (initiate_conc_mark_if_possible()) { aoqi@0: // We had noticed on a previous pause that the heap occupancy has aoqi@0: // gone over the initiating threshold and we should start a aoqi@0: // concurrent marking cycle. So we might initiate one. aoqi@0: aoqi@0: bool during_cycle = _g1->concurrent_mark()->cmThread()->during_cycle(); aoqi@0: if (!during_cycle) { aoqi@0: // The concurrent marking thread is not "during a cycle", i.e., aoqi@0: // it has completed the last one. So we can go ahead and aoqi@0: // initiate a new cycle. aoqi@0: aoqi@0: set_during_initial_mark_pause(); aoqi@0: // We do not allow mixed GCs during marking. aoqi@0: if (!gcs_are_young()) { aoqi@0: set_gcs_are_young(true); aoqi@0: ergo_verbose0(ErgoMixedGCs, aoqi@0: "end mixed GCs", aoqi@0: ergo_format_reason("concurrent cycle is about to start")); aoqi@0: } aoqi@0: aoqi@0: // And we can now clear initiate_conc_mark_if_possible() as aoqi@0: // we've already acted on it. aoqi@0: clear_initiate_conc_mark_if_possible(); aoqi@0: aoqi@0: ergo_verbose0(ErgoConcCycles, aoqi@0: "initiate concurrent cycle", aoqi@0: ergo_format_reason("concurrent cycle initiation requested")); aoqi@0: } else { aoqi@0: // The concurrent marking thread is still finishing up the aoqi@0: // previous cycle. If we start one right now the two cycles aoqi@0: // overlap. In particular, the concurrent marking thread might aoqi@0: // be in the process of clearing the next marking bitmap (which aoqi@0: // we will use for the next cycle if we start one). Starting a aoqi@0: // cycle now will be bad given that parts of the marking aoqi@0: // information might get cleared by the marking thread. And we aoqi@0: // cannot wait for the marking thread to finish the cycle as it aoqi@0: // periodically yields while clearing the next marking bitmap aoqi@0: // and, if it's in a yield point, it's waiting for us to aoqi@0: // finish. So, at this point we will not start a cycle and we'll aoqi@0: // let the concurrent marking thread complete the last one. aoqi@0: ergo_verbose0(ErgoConcCycles, aoqi@0: "do not initiate concurrent cycle", aoqi@0: ergo_format_reason("concurrent cycle already in progress")); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class KnownGarbageClosure: public HeapRegionClosure { aoqi@0: G1CollectedHeap* _g1h; aoqi@0: CollectionSetChooser* _hrSorted; aoqi@0: aoqi@0: public: aoqi@0: KnownGarbageClosure(CollectionSetChooser* hrSorted) : aoqi@0: _g1h(G1CollectedHeap::heap()), _hrSorted(hrSorted) { } aoqi@0: aoqi@0: bool doHeapRegion(HeapRegion* r) { aoqi@0: // We only include humongous regions in collection aoqi@0: // sets when concurrent mark shows that their contained object is aoqi@0: // unreachable. aoqi@0: aoqi@0: // Do we have any marking information for this region? aoqi@0: if (r->is_marked()) { aoqi@0: // We will skip any region that's currently used as an old GC aoqi@0: // alloc region (we should not consider those for collection aoqi@0: // before we fill them up). aoqi@0: if (_hrSorted->should_add(r) && !_g1h->is_old_gc_alloc_region(r)) { aoqi@0: _hrSorted->add_region(r); aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class ParKnownGarbageHRClosure: public HeapRegionClosure { aoqi@0: G1CollectedHeap* _g1h; aoqi@0: CSetChooserParUpdater _cset_updater; aoqi@0: aoqi@0: public: aoqi@0: ParKnownGarbageHRClosure(CollectionSetChooser* hrSorted, aoqi@0: uint chunk_size) : aoqi@0: _g1h(G1CollectedHeap::heap()), aoqi@0: _cset_updater(hrSorted, true /* parallel */, chunk_size) { } aoqi@0: aoqi@0: bool doHeapRegion(HeapRegion* r) { aoqi@0: // Do we have any marking information for this region? aoqi@0: if (r->is_marked()) { aoqi@0: // We will skip any region that's currently used as an old GC aoqi@0: // alloc region (we should not consider those for collection aoqi@0: // before we fill them up). aoqi@0: if (_cset_updater.should_add(r) && !_g1h->is_old_gc_alloc_region(r)) { aoqi@0: _cset_updater.add_region(r); aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class ParKnownGarbageTask: public AbstractGangTask { aoqi@0: CollectionSetChooser* _hrSorted; aoqi@0: uint _chunk_size; aoqi@0: G1CollectedHeap* _g1; aoqi@0: public: aoqi@0: ParKnownGarbageTask(CollectionSetChooser* hrSorted, uint chunk_size) : aoqi@0: AbstractGangTask("ParKnownGarbageTask"), aoqi@0: _hrSorted(hrSorted), _chunk_size(chunk_size), aoqi@0: _g1(G1CollectedHeap::heap()) { } aoqi@0: aoqi@0: void work(uint worker_id) { aoqi@0: ParKnownGarbageHRClosure parKnownGarbageCl(_hrSorted, _chunk_size); aoqi@0: aoqi@0: // Back to zero for the claim value. aoqi@0: _g1->heap_region_par_iterate_chunked(&parKnownGarbageCl, worker_id, aoqi@0: _g1->workers()->active_workers(), aoqi@0: HeapRegion::InitialClaimValue); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void aoqi@0: G1CollectorPolicy::record_concurrent_mark_cleanup_end(int no_of_gc_threads) { aoqi@0: _collectionSetChooser->clear(); aoqi@0: aoqi@0: uint region_num = _g1->n_regions(); aoqi@0: if (G1CollectedHeap::use_parallel_gc_threads()) { aoqi@0: const uint OverpartitionFactor = 4; aoqi@0: uint WorkUnit; aoqi@0: // The use of MinChunkSize = 8 in the original code aoqi@0: // causes some assertion failures when the total number of aoqi@0: // region is less than 8. The code here tries to fix that. aoqi@0: // Should the original code also be fixed? aoqi@0: if (no_of_gc_threads > 0) { aoqi@0: const uint MinWorkUnit = MAX2(region_num / no_of_gc_threads, 1U); aoqi@0: WorkUnit = MAX2(region_num / (no_of_gc_threads * OverpartitionFactor), aoqi@0: MinWorkUnit); aoqi@0: } else { aoqi@0: assert(no_of_gc_threads > 0, aoqi@0: "The active gc workers should be greater than 0"); aoqi@0: // In a product build do something reasonable to avoid a crash. aoqi@0: const uint MinWorkUnit = MAX2(region_num / (uint) ParallelGCThreads, 1U); aoqi@0: WorkUnit = aoqi@0: MAX2(region_num / (uint) (ParallelGCThreads * OverpartitionFactor), aoqi@0: MinWorkUnit); aoqi@0: } aoqi@0: _collectionSetChooser->prepare_for_par_region_addition(_g1->n_regions(), aoqi@0: WorkUnit); aoqi@0: ParKnownGarbageTask parKnownGarbageTask(_collectionSetChooser, aoqi@0: (int) WorkUnit); aoqi@0: _g1->workers()->run_task(&parKnownGarbageTask); aoqi@0: aoqi@0: assert(_g1->check_heap_region_claim_values(HeapRegion::InitialClaimValue), aoqi@0: "sanity check"); aoqi@0: } else { aoqi@0: KnownGarbageClosure knownGarbagecl(_collectionSetChooser); aoqi@0: _g1->heap_region_iterate(&knownGarbagecl); aoqi@0: } aoqi@0: aoqi@0: _collectionSetChooser->sort_regions(); aoqi@0: aoqi@0: double end_sec = os::elapsedTime(); aoqi@0: double elapsed_time_ms = (end_sec - _mark_cleanup_start_sec) * 1000.0; aoqi@0: _concurrent_mark_cleanup_times_ms->add(elapsed_time_ms); aoqi@0: _cur_mark_stop_world_time_ms += elapsed_time_ms; aoqi@0: _prev_collection_pause_end_ms += elapsed_time_ms; aoqi@0: _mmu_tracker->add_pause(_mark_cleanup_start_sec, end_sec, true); aoqi@0: } aoqi@0: aoqi@0: // Add the heap region at the head of the non-incremental collection set aoqi@0: void G1CollectorPolicy::add_old_region_to_cset(HeapRegion* hr) { aoqi@0: assert(_inc_cset_build_state == Active, "Precondition"); aoqi@0: assert(!hr->is_young(), "non-incremental add of young region"); aoqi@0: aoqi@0: assert(!hr->in_collection_set(), "should not already be in the CSet"); aoqi@0: hr->set_in_collection_set(true); aoqi@0: hr->set_next_in_collection_set(_collection_set); aoqi@0: _collection_set = hr; aoqi@0: _collection_set_bytes_used_before += hr->used(); aoqi@0: _g1->register_region_with_in_cset_fast_test(hr); aoqi@0: size_t rs_length = hr->rem_set()->occupied(); aoqi@0: _recorded_rs_lengths += rs_length; aoqi@0: _old_cset_region_length += 1; aoqi@0: } aoqi@0: aoqi@0: // Initialize the per-collection-set information aoqi@0: void G1CollectorPolicy::start_incremental_cset_building() { aoqi@0: assert(_inc_cset_build_state == Inactive, "Precondition"); aoqi@0: aoqi@0: _inc_cset_head = NULL; aoqi@0: _inc_cset_tail = NULL; aoqi@0: _inc_cset_bytes_used_before = 0; aoqi@0: aoqi@0: _inc_cset_max_finger = 0; aoqi@0: _inc_cset_recorded_rs_lengths = 0; aoqi@0: _inc_cset_recorded_rs_lengths_diffs = 0; aoqi@0: _inc_cset_predicted_elapsed_time_ms = 0.0; aoqi@0: _inc_cset_predicted_elapsed_time_ms_diffs = 0.0; aoqi@0: _inc_cset_build_state = Active; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::finalize_incremental_cset_building() { aoqi@0: assert(_inc_cset_build_state == Active, "Precondition"); aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint"); aoqi@0: aoqi@0: // The two "main" fields, _inc_cset_recorded_rs_lengths and aoqi@0: // _inc_cset_predicted_elapsed_time_ms, are updated by the thread aoqi@0: // that adds a new region to the CSet. Further updates by the aoqi@0: // concurrent refinement thread that samples the young RSet lengths aoqi@0: // are accumulated in the *_diffs fields. Here we add the diffs to aoqi@0: // the "main" fields. aoqi@0: aoqi@0: if (_inc_cset_recorded_rs_lengths_diffs >= 0) { aoqi@0: _inc_cset_recorded_rs_lengths += _inc_cset_recorded_rs_lengths_diffs; aoqi@0: } else { aoqi@0: // This is defensive. The diff should in theory be always positive aoqi@0: // as RSets can only grow between GCs. However, given that we aoqi@0: // sample their size concurrently with other threads updating them aoqi@0: // it's possible that we might get the wrong size back, which aoqi@0: // could make the calculations somewhat inaccurate. aoqi@0: size_t diffs = (size_t) (-_inc_cset_recorded_rs_lengths_diffs); aoqi@0: if (_inc_cset_recorded_rs_lengths >= diffs) { aoqi@0: _inc_cset_recorded_rs_lengths -= diffs; aoqi@0: } else { aoqi@0: _inc_cset_recorded_rs_lengths = 0; aoqi@0: } aoqi@0: } aoqi@0: _inc_cset_predicted_elapsed_time_ms += aoqi@0: _inc_cset_predicted_elapsed_time_ms_diffs; aoqi@0: aoqi@0: _inc_cset_recorded_rs_lengths_diffs = 0; aoqi@0: _inc_cset_predicted_elapsed_time_ms_diffs = 0.0; aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::add_to_incremental_cset_info(HeapRegion* hr, size_t rs_length) { aoqi@0: // This routine is used when: aoqi@0: // * adding survivor regions to the incremental cset at the end of an aoqi@0: // evacuation pause, aoqi@0: // * adding the current allocation region to the incremental cset aoqi@0: // when it is retired, and aoqi@0: // * updating existing policy information for a region in the aoqi@0: // incremental cset via young list RSet sampling. aoqi@0: // Therefore this routine may be called at a safepoint by the aoqi@0: // VM thread, or in-between safepoints by mutator threads (when aoqi@0: // retiring the current allocation region) or a concurrent aoqi@0: // refine thread (RSet sampling). aoqi@0: aoqi@0: double region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, gcs_are_young()); aoqi@0: size_t used_bytes = hr->used(); aoqi@0: _inc_cset_recorded_rs_lengths += rs_length; aoqi@0: _inc_cset_predicted_elapsed_time_ms += region_elapsed_time_ms; aoqi@0: _inc_cset_bytes_used_before += used_bytes; aoqi@0: aoqi@0: // Cache the values we have added to the aggregated informtion aoqi@0: // in the heap region in case we have to remove this region from aoqi@0: // the incremental collection set, or it is updated by the aoqi@0: // rset sampling code aoqi@0: hr->set_recorded_rs_length(rs_length); aoqi@0: hr->set_predicted_elapsed_time_ms(region_elapsed_time_ms); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::update_incremental_cset_info(HeapRegion* hr, aoqi@0: size_t new_rs_length) { aoqi@0: // Update the CSet information that is dependent on the new RS length aoqi@0: assert(hr->is_young(), "Precondition"); aoqi@0: assert(!SafepointSynchronize::is_at_safepoint(), aoqi@0: "should not be at a safepoint"); aoqi@0: aoqi@0: // We could have updated _inc_cset_recorded_rs_lengths and aoqi@0: // _inc_cset_predicted_elapsed_time_ms directly but we'd need to do aoqi@0: // that atomically, as this code is executed by a concurrent aoqi@0: // refinement thread, potentially concurrently with a mutator thread aoqi@0: // allocating a new region and also updating the same fields. To aoqi@0: // avoid the atomic operations we accumulate these updates on two aoqi@0: // separate fields (*_diffs) and we'll just add them to the "main" aoqi@0: // fields at the start of a GC. aoqi@0: aoqi@0: ssize_t old_rs_length = (ssize_t) hr->recorded_rs_length(); aoqi@0: ssize_t rs_lengths_diff = (ssize_t) new_rs_length - old_rs_length; aoqi@0: _inc_cset_recorded_rs_lengths_diffs += rs_lengths_diff; aoqi@0: aoqi@0: double old_elapsed_time_ms = hr->predicted_elapsed_time_ms(); aoqi@0: double new_region_elapsed_time_ms = predict_region_elapsed_time_ms(hr, gcs_are_young()); aoqi@0: double elapsed_ms_diff = new_region_elapsed_time_ms - old_elapsed_time_ms; aoqi@0: _inc_cset_predicted_elapsed_time_ms_diffs += elapsed_ms_diff; aoqi@0: aoqi@0: hr->set_recorded_rs_length(new_rs_length); aoqi@0: hr->set_predicted_elapsed_time_ms(new_region_elapsed_time_ms); aoqi@0: } aoqi@0: aoqi@0: void G1CollectorPolicy::add_region_to_incremental_cset_common(HeapRegion* hr) { aoqi@0: assert(hr->is_young(), "invariant"); aoqi@0: assert(hr->young_index_in_cset() > -1, "should have already been set"); aoqi@0: assert(_inc_cset_build_state == Active, "Precondition"); aoqi@0: aoqi@0: // We need to clear and set the cached recorded/cached collection set aoqi@0: // information in the heap region here (before the region gets added aoqi@0: // to the collection set). An individual heap region's cached values aoqi@0: // are calculated, aggregated with the policy collection set info, aoqi@0: // and cached in the heap region here (initially) and (subsequently) aoqi@0: // by the Young List sampling code. aoqi@0: aoqi@0: size_t rs_length = hr->rem_set()->occupied(); aoqi@0: add_to_incremental_cset_info(hr, rs_length); aoqi@0: aoqi@0: HeapWord* hr_end = hr->end(); aoqi@0: _inc_cset_max_finger = MAX2(_inc_cset_max_finger, hr_end); aoqi@0: aoqi@0: assert(!hr->in_collection_set(), "invariant"); aoqi@0: hr->set_in_collection_set(true); aoqi@0: assert( hr->next_in_collection_set() == NULL, "invariant"); aoqi@0: aoqi@0: _g1->register_region_with_in_cset_fast_test(hr); aoqi@0: } aoqi@0: aoqi@0: // Add the region at the RHS of the incremental cset aoqi@0: void G1CollectorPolicy::add_region_to_incremental_cset_rhs(HeapRegion* hr) { aoqi@0: // We should only ever be appending survivors at the end of a pause aoqi@0: assert( hr->is_survivor(), "Logic"); aoqi@0: aoqi@0: // Do the 'common' stuff aoqi@0: add_region_to_incremental_cset_common(hr); aoqi@0: aoqi@0: // Now add the region at the right hand side aoqi@0: if (_inc_cset_tail == NULL) { aoqi@0: assert(_inc_cset_head == NULL, "invariant"); aoqi@0: _inc_cset_head = hr; aoqi@0: } else { aoqi@0: _inc_cset_tail->set_next_in_collection_set(hr); aoqi@0: } aoqi@0: _inc_cset_tail = hr; aoqi@0: } aoqi@0: aoqi@0: // Add the region to the LHS of the incremental cset aoqi@0: void G1CollectorPolicy::add_region_to_incremental_cset_lhs(HeapRegion* hr) { aoqi@0: // Survivors should be added to the RHS at the end of a pause aoqi@0: assert(!hr->is_survivor(), "Logic"); aoqi@0: aoqi@0: // Do the 'common' stuff aoqi@0: add_region_to_incremental_cset_common(hr); aoqi@0: aoqi@0: // Add the region at the left hand side aoqi@0: hr->set_next_in_collection_set(_inc_cset_head); aoqi@0: if (_inc_cset_head == NULL) { aoqi@0: assert(_inc_cset_tail == NULL, "Invariant"); aoqi@0: _inc_cset_tail = hr; aoqi@0: } aoqi@0: _inc_cset_head = hr; aoqi@0: } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void G1CollectorPolicy::print_collection_set(HeapRegion* list_head, outputStream* st) { aoqi@0: assert(list_head == inc_cset_head() || list_head == collection_set(), "must be"); aoqi@0: aoqi@0: st->print_cr("\nCollection_set:"); aoqi@0: HeapRegion* csr = list_head; aoqi@0: while (csr != NULL) { aoqi@0: HeapRegion* next = csr->next_in_collection_set(); aoqi@0: assert(csr->in_collection_set(), "bad CS"); aoqi@0: st->print_cr(" "HR_FORMAT", P: "PTR_FORMAT "N: "PTR_FORMAT", age: %4d", aoqi@0: HR_FORMAT_PARAMS(csr), aoqi@0: csr->prev_top_at_mark_start(), csr->next_top_at_mark_start(), aoqi@0: csr->age_in_surv_rate_group_cond()); aoqi@0: csr = next; aoqi@0: } aoqi@0: } aoqi@0: #endif // !PRODUCT aoqi@0: aoqi@0: double G1CollectorPolicy::reclaimable_bytes_perc(size_t reclaimable_bytes) { aoqi@0: // Returns the given amount of reclaimable bytes (that represents aoqi@0: // the amount of reclaimable space still to be collected) as a aoqi@0: // percentage of the current heap capacity. aoqi@0: size_t capacity_bytes = _g1->capacity(); aoqi@0: return (double) reclaimable_bytes * 100.0 / (double) capacity_bytes; aoqi@0: } aoqi@0: aoqi@0: bool G1CollectorPolicy::next_gc_should_be_mixed(const char* true_action_str, aoqi@0: const char* false_action_str) { aoqi@0: CollectionSetChooser* cset_chooser = _collectionSetChooser; aoqi@0: if (cset_chooser->is_empty()) { aoqi@0: ergo_verbose0(ErgoMixedGCs, aoqi@0: false_action_str, aoqi@0: ergo_format_reason("candidate old regions not available")); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Is the amount of uncollected reclaimable space above G1HeapWastePercent? aoqi@0: size_t reclaimable_bytes = cset_chooser->remaining_reclaimable_bytes(); aoqi@0: double reclaimable_perc = reclaimable_bytes_perc(reclaimable_bytes); aoqi@0: double threshold = (double) G1HeapWastePercent; aoqi@0: if (reclaimable_perc <= threshold) { aoqi@0: ergo_verbose4(ErgoMixedGCs, aoqi@0: false_action_str, aoqi@0: ergo_format_reason("reclaimable percentage not over threshold") aoqi@0: ergo_format_region("candidate old regions") aoqi@0: ergo_format_byte_perc("reclaimable") aoqi@0: ergo_format_perc("threshold"), aoqi@0: cset_chooser->remaining_regions(), aoqi@0: reclaimable_bytes, aoqi@0: reclaimable_perc, threshold); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: ergo_verbose4(ErgoMixedGCs, aoqi@0: true_action_str, aoqi@0: ergo_format_reason("candidate old regions available") aoqi@0: ergo_format_region("candidate old regions") aoqi@0: ergo_format_byte_perc("reclaimable") aoqi@0: ergo_format_perc("threshold"), aoqi@0: cset_chooser->remaining_regions(), aoqi@0: reclaimable_bytes, aoqi@0: reclaimable_perc, threshold); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: uint G1CollectorPolicy::calc_min_old_cset_length() { aoqi@0: // The min old CSet region bound is based on the maximum desired aoqi@0: // number of mixed GCs after a cycle. I.e., even if some old regions aoqi@0: // look expensive, we should add them to the CSet anyway to make aoqi@0: // sure we go through the available old regions in no more than the aoqi@0: // maximum desired number of mixed GCs. aoqi@0: // aoqi@0: // The calculation is based on the number of marked regions we added aoqi@0: // to the CSet chooser in the first place, not how many remain, so aoqi@0: // that the result is the same during all mixed GCs that follow a cycle. aoqi@0: aoqi@0: const size_t region_num = (size_t) _collectionSetChooser->length(); aoqi@0: const size_t gc_num = (size_t) MAX2(G1MixedGCCountTarget, (uintx) 1); aoqi@0: size_t result = region_num / gc_num; aoqi@0: // emulate ceiling aoqi@0: if (result * gc_num < region_num) { aoqi@0: result += 1; aoqi@0: } aoqi@0: return (uint) result; aoqi@0: } aoqi@0: aoqi@0: uint G1CollectorPolicy::calc_max_old_cset_length() { aoqi@0: // The max old CSet region bound is based on the threshold expressed aoqi@0: // as a percentage of the heap size. I.e., it should bound the aoqi@0: // number of old regions added to the CSet irrespective of how many aoqi@0: // of them are available. aoqi@0: aoqi@0: G1CollectedHeap* g1h = G1CollectedHeap::heap(); aoqi@0: const size_t region_num = g1h->n_regions(); aoqi@0: const size_t perc = (size_t) G1OldCSetRegionThresholdPercent; aoqi@0: size_t result = region_num * perc / 100; aoqi@0: // emulate ceiling aoqi@0: if (100 * result < region_num * perc) { aoqi@0: result += 1; aoqi@0: } aoqi@0: return (uint) result; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void G1CollectorPolicy::finalize_cset(double target_pause_time_ms, EvacuationInfo& evacuation_info) { aoqi@0: double young_start_time_sec = os::elapsedTime(); aoqi@0: aoqi@0: YoungList* young_list = _g1->young_list(); aoqi@0: finalize_incremental_cset_building(); aoqi@0: aoqi@0: guarantee(target_pause_time_ms > 0.0, aoqi@0: err_msg("target_pause_time_ms = %1.6lf should be positive", aoqi@0: target_pause_time_ms)); aoqi@0: guarantee(_collection_set == NULL, "Precondition"); aoqi@0: aoqi@0: double base_time_ms = predict_base_elapsed_time_ms(_pending_cards); aoqi@0: double predicted_pause_time_ms = base_time_ms; aoqi@0: double time_remaining_ms = MAX2(target_pause_time_ms - base_time_ms, 0.0); aoqi@0: aoqi@0: ergo_verbose4(ErgoCSetConstruction | ErgoHigh, aoqi@0: "start choosing CSet", aoqi@0: ergo_format_size("_pending_cards") aoqi@0: ergo_format_ms("predicted base time") aoqi@0: ergo_format_ms("remaining time") aoqi@0: ergo_format_ms("target pause time"), aoqi@0: _pending_cards, base_time_ms, time_remaining_ms, target_pause_time_ms); aoqi@0: aoqi@0: _last_gc_was_young = gcs_are_young() ? true : false; aoqi@0: aoqi@0: if (_last_gc_was_young) { aoqi@0: _trace_gen0_time_data.increment_young_collection_count(); aoqi@0: } else { aoqi@0: _trace_gen0_time_data.increment_mixed_collection_count(); aoqi@0: } aoqi@0: aoqi@0: // The young list is laid with the survivor regions from the previous aoqi@0: // pause are appended to the RHS of the young list, i.e. aoqi@0: // [Newly Young Regions ++ Survivors from last pause]. aoqi@0: aoqi@0: uint survivor_region_length = young_list->survivor_length(); aoqi@0: uint eden_region_length = young_list->length() - survivor_region_length; aoqi@0: init_cset_region_lengths(eden_region_length, survivor_region_length); aoqi@0: aoqi@0: HeapRegion* hr = young_list->first_survivor_region(); aoqi@0: while (hr != NULL) { aoqi@0: assert(hr->is_survivor(), "badly formed young list"); aoqi@0: hr->set_young(); aoqi@0: hr = hr->get_next_young_region(); aoqi@0: } aoqi@0: aoqi@0: // Clear the fields that point to the survivor list - they are all young now. aoqi@0: young_list->clear_survivors(); aoqi@0: aoqi@0: _collection_set = _inc_cset_head; aoqi@0: _collection_set_bytes_used_before = _inc_cset_bytes_used_before; aoqi@0: time_remaining_ms = MAX2(time_remaining_ms - _inc_cset_predicted_elapsed_time_ms, 0.0); aoqi@0: predicted_pause_time_ms += _inc_cset_predicted_elapsed_time_ms; aoqi@0: aoqi@0: ergo_verbose3(ErgoCSetConstruction | ErgoHigh, aoqi@0: "add young regions to CSet", aoqi@0: ergo_format_region("eden") aoqi@0: ergo_format_region("survivors") aoqi@0: ergo_format_ms("predicted young region time"), aoqi@0: eden_region_length, survivor_region_length, aoqi@0: _inc_cset_predicted_elapsed_time_ms); aoqi@0: aoqi@0: // The number of recorded young regions is the incremental aoqi@0: // collection set's current size aoqi@0: set_recorded_rs_lengths(_inc_cset_recorded_rs_lengths); aoqi@0: aoqi@0: double young_end_time_sec = os::elapsedTime(); aoqi@0: phase_times()->record_young_cset_choice_time_ms((young_end_time_sec - young_start_time_sec) * 1000.0); aoqi@0: aoqi@0: // Set the start of the non-young choice time. aoqi@0: double non_young_start_time_sec = young_end_time_sec; aoqi@0: aoqi@0: if (!gcs_are_young()) { aoqi@0: CollectionSetChooser* cset_chooser = _collectionSetChooser; aoqi@0: cset_chooser->verify(); aoqi@0: const uint min_old_cset_length = calc_min_old_cset_length(); aoqi@0: const uint max_old_cset_length = calc_max_old_cset_length(); aoqi@0: aoqi@0: uint expensive_region_num = 0; aoqi@0: bool check_time_remaining = adaptive_young_list_length(); aoqi@0: aoqi@0: HeapRegion* hr = cset_chooser->peek(); aoqi@0: while (hr != NULL) { aoqi@0: if (old_cset_region_length() >= max_old_cset_length) { aoqi@0: // Added maximum number of old regions to the CSet. aoqi@0: ergo_verbose2(ErgoCSetConstruction, aoqi@0: "finish adding old regions to CSet", aoqi@0: ergo_format_reason("old CSet region num reached max") aoqi@0: ergo_format_region("old") aoqi@0: ergo_format_region("max"), aoqi@0: old_cset_region_length(), max_old_cset_length); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Stop adding regions if the remaining reclaimable space is aoqi@0: // not above G1HeapWastePercent. aoqi@0: size_t reclaimable_bytes = cset_chooser->remaining_reclaimable_bytes(); aoqi@0: double reclaimable_perc = reclaimable_bytes_perc(reclaimable_bytes); aoqi@0: double threshold = (double) G1HeapWastePercent; aoqi@0: if (reclaimable_perc <= threshold) { aoqi@0: // We've added enough old regions that the amount of uncollected aoqi@0: // reclaimable space is at or below the waste threshold. Stop aoqi@0: // adding old regions to the CSet. aoqi@0: ergo_verbose5(ErgoCSetConstruction, aoqi@0: "finish adding old regions to CSet", aoqi@0: ergo_format_reason("reclaimable percentage not over threshold") aoqi@0: ergo_format_region("old") aoqi@0: ergo_format_region("max") aoqi@0: ergo_format_byte_perc("reclaimable") aoqi@0: ergo_format_perc("threshold"), aoqi@0: old_cset_region_length(), aoqi@0: max_old_cset_length, aoqi@0: reclaimable_bytes, aoqi@0: reclaimable_perc, threshold); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: double predicted_time_ms = predict_region_elapsed_time_ms(hr, gcs_are_young()); aoqi@0: if (check_time_remaining) { aoqi@0: if (predicted_time_ms > time_remaining_ms) { aoqi@0: // Too expensive for the current CSet. aoqi@0: aoqi@0: if (old_cset_region_length() >= min_old_cset_length) { aoqi@0: // We have added the minimum number of old regions to the CSet, aoqi@0: // we are done with this CSet. aoqi@0: ergo_verbose4(ErgoCSetConstruction, aoqi@0: "finish adding old regions to CSet", aoqi@0: ergo_format_reason("predicted time is too high") aoqi@0: ergo_format_ms("predicted time") aoqi@0: ergo_format_ms("remaining time") aoqi@0: ergo_format_region("old") aoqi@0: ergo_format_region("min"), aoqi@0: predicted_time_ms, time_remaining_ms, aoqi@0: old_cset_region_length(), min_old_cset_length); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // We'll add it anyway given that we haven't reached the aoqi@0: // minimum number of old regions. aoqi@0: expensive_region_num += 1; aoqi@0: } aoqi@0: } else { aoqi@0: if (old_cset_region_length() >= min_old_cset_length) { aoqi@0: // In the non-auto-tuning case, we'll finish adding regions aoqi@0: // to the CSet if we reach the minimum. aoqi@0: ergo_verbose2(ErgoCSetConstruction, aoqi@0: "finish adding old regions to CSet", aoqi@0: ergo_format_reason("old CSet region num reached min") aoqi@0: ergo_format_region("old") aoqi@0: ergo_format_region("min"), aoqi@0: old_cset_region_length(), min_old_cset_length); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // We will add this region to the CSet. aoqi@0: time_remaining_ms = MAX2(time_remaining_ms - predicted_time_ms, 0.0); aoqi@0: predicted_pause_time_ms += predicted_time_ms; aoqi@0: cset_chooser->remove_and_move_to_next(hr); aoqi@0: _g1->old_set_remove(hr); aoqi@0: add_old_region_to_cset(hr); aoqi@0: aoqi@0: hr = cset_chooser->peek(); aoqi@0: } aoqi@0: if (hr == NULL) { aoqi@0: ergo_verbose0(ErgoCSetConstruction, aoqi@0: "finish adding old regions to CSet", aoqi@0: ergo_format_reason("candidate old regions not available")); aoqi@0: } aoqi@0: aoqi@0: if (expensive_region_num > 0) { aoqi@0: // We print the information once here at the end, predicated on aoqi@0: // whether we added any apparently expensive regions or not, to aoqi@0: // avoid generating output per region. aoqi@0: ergo_verbose4(ErgoCSetConstruction, aoqi@0: "added expensive regions to CSet", aoqi@0: ergo_format_reason("old CSet region num not reached min") aoqi@0: ergo_format_region("old") aoqi@0: ergo_format_region("expensive") aoqi@0: ergo_format_region("min") aoqi@0: ergo_format_ms("remaining time"), aoqi@0: old_cset_region_length(), aoqi@0: expensive_region_num, aoqi@0: min_old_cset_length, aoqi@0: time_remaining_ms); aoqi@0: } aoqi@0: aoqi@0: cset_chooser->verify(); aoqi@0: } aoqi@0: aoqi@0: stop_incremental_cset_building(); aoqi@0: aoqi@0: ergo_verbose5(ErgoCSetConstruction, aoqi@0: "finish choosing CSet", aoqi@0: ergo_format_region("eden") aoqi@0: ergo_format_region("survivors") aoqi@0: ergo_format_region("old") aoqi@0: ergo_format_ms("predicted pause time") aoqi@0: ergo_format_ms("target pause time"), aoqi@0: eden_region_length, survivor_region_length, aoqi@0: old_cset_region_length(), aoqi@0: predicted_pause_time_ms, target_pause_time_ms); aoqi@0: aoqi@0: double non_young_end_time_sec = os::elapsedTime(); aoqi@0: phase_times()->record_non_young_cset_choice_time_ms((non_young_end_time_sec - non_young_start_time_sec) * 1000.0); aoqi@0: evacuation_info.set_collectionset_regions(cset_region_length()); aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::record_start_collection(double time_to_stop_the_world_ms) { aoqi@0: if(TraceGen0Time) { aoqi@0: _all_stop_world_times_ms.add(time_to_stop_the_world_ms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::record_yield_time(double yield_time_ms) { aoqi@0: if(TraceGen0Time) { aoqi@0: _all_yield_times_ms.add(yield_time_ms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::record_end_collection(double pause_time_ms, G1GCPhaseTimes* phase_times) { aoqi@0: if(TraceGen0Time) { aoqi@0: _total.add(pause_time_ms); aoqi@0: _other.add(pause_time_ms - phase_times->accounted_time_ms()); aoqi@0: _root_region_scan_wait.add(phase_times->root_region_scan_wait_time_ms()); aoqi@0: _parallel.add(phase_times->cur_collection_par_time_ms()); aoqi@0: _ext_root_scan.add(phase_times->average_last_ext_root_scan_time()); aoqi@0: _satb_filtering.add(phase_times->average_last_satb_filtering_times_ms()); aoqi@0: _update_rs.add(phase_times->average_last_update_rs_time()); aoqi@0: _scan_rs.add(phase_times->average_last_scan_rs_time()); aoqi@0: _obj_copy.add(phase_times->average_last_obj_copy_time()); aoqi@0: _termination.add(phase_times->average_last_termination_time()); aoqi@0: aoqi@0: double parallel_known_time = phase_times->average_last_ext_root_scan_time() + aoqi@0: phase_times->average_last_satb_filtering_times_ms() + aoqi@0: phase_times->average_last_update_rs_time() + aoqi@0: phase_times->average_last_scan_rs_time() + aoqi@0: phase_times->average_last_obj_copy_time() + aoqi@0: + phase_times->average_last_termination_time(); aoqi@0: aoqi@0: double parallel_other_time = phase_times->cur_collection_par_time_ms() - parallel_known_time; aoqi@0: _parallel_other.add(parallel_other_time); aoqi@0: _clear_ct.add(phase_times->cur_clear_ct_time_ms()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::increment_young_collection_count() { aoqi@0: if(TraceGen0Time) { aoqi@0: ++_young_pause_num; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::increment_mixed_collection_count() { aoqi@0: if(TraceGen0Time) { aoqi@0: ++_mixed_pause_num; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::print_summary(const char* str, aoqi@0: const NumberSeq* seq) const { aoqi@0: double sum = seq->sum(); aoqi@0: gclog_or_tty->print_cr("%-27s = %8.2lf s (avg = %8.2lf ms)", aoqi@0: str, sum / 1000.0, seq->avg()); aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::print_summary_sd(const char* str, aoqi@0: const NumberSeq* seq) const { aoqi@0: print_summary(str, seq); aoqi@0: gclog_or_tty->print_cr("%+45s = %5d, std dev = %8.2lf ms, max = %8.2lf ms)", aoqi@0: "(num", seq->num(), seq->sd(), seq->maximum()); aoqi@0: } aoqi@0: aoqi@0: void TraceGen0TimeData::print() const { aoqi@0: if (!TraceGen0Time) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: gclog_or_tty->print_cr("ALL PAUSES"); aoqi@0: print_summary_sd(" Total", &_total); aoqi@0: gclog_or_tty->cr(); aoqi@0: gclog_or_tty->cr(); aoqi@0: gclog_or_tty->print_cr(" Young GC Pauses: %8d", _young_pause_num); aoqi@0: gclog_or_tty->print_cr(" Mixed GC Pauses: %8d", _mixed_pause_num); aoqi@0: gclog_or_tty->cr(); aoqi@0: aoqi@0: gclog_or_tty->print_cr("EVACUATION PAUSES"); aoqi@0: aoqi@0: if (_young_pause_num == 0 && _mixed_pause_num == 0) { aoqi@0: gclog_or_tty->print_cr("none"); aoqi@0: } else { aoqi@0: print_summary_sd(" Evacuation Pauses", &_total); aoqi@0: print_summary(" Root Region Scan Wait", &_root_region_scan_wait); aoqi@0: print_summary(" Parallel Time", &_parallel); aoqi@0: print_summary(" Ext Root Scanning", &_ext_root_scan); aoqi@0: print_summary(" SATB Filtering", &_satb_filtering); aoqi@0: print_summary(" Update RS", &_update_rs); aoqi@0: print_summary(" Scan RS", &_scan_rs); aoqi@0: print_summary(" Object Copy", &_obj_copy); aoqi@0: print_summary(" Termination", &_termination); aoqi@0: print_summary(" Parallel Other", &_parallel_other); aoqi@0: print_summary(" Clear CT", &_clear_ct); aoqi@0: print_summary(" Other", &_other); aoqi@0: } aoqi@0: gclog_or_tty->cr(); aoqi@0: aoqi@0: gclog_or_tty->print_cr("MISC"); aoqi@0: print_summary_sd(" Stop World", &_all_stop_world_times_ms); aoqi@0: print_summary_sd(" Yields", &_all_yield_times_ms); aoqi@0: } aoqi@0: aoqi@0: void TraceGen1TimeData::record_full_collection(double full_gc_time_ms) { aoqi@0: if (TraceGen1Time) { aoqi@0: _all_full_gc_times.add(full_gc_time_ms); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TraceGen1TimeData::print() const { aoqi@0: if (!TraceGen1Time) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (_all_full_gc_times.num() > 0) { aoqi@0: gclog_or_tty->print("\n%4d full_gcs: total time = %8.2f s", aoqi@0: _all_full_gc_times.num(), aoqi@0: _all_full_gc_times.sum() / 1000.0); aoqi@0: gclog_or_tty->print_cr(" (avg = %8.2fms).", _all_full_gc_times.avg()); aoqi@0: gclog_or_tty->print_cr(" [std. dev = %8.2f ms, max = %8.2f ms]", aoqi@0: _all_full_gc_times.sd(), aoqi@0: _all_full_gc_times.maximum()); aoqi@0: } aoqi@0: }