src/share/vm/gc_implementation/g1/g1CollectorPolicy.cpp

Mon, 20 Sep 2010 14:38:38 -0700

author
jmasa
date
Mon, 20 Sep 2010 14:38:38 -0700
changeset 2188
8b10f48633dc
parent 2134
6eddcbe17c83
child 2314
f95d63e2154a
permissions
-rw-r--r--

6984287: Regularize how GC parallel workers are specified.
Summary: Associate number of GC workers with the workgang as opposed to the task.
Reviewed-by: johnc, ysr

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

mercurial