hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.cpp
changeset 25502 92dc8bd62c69
parent 25456 39cfdc2dcaf3
parent 25501 1e2ffa5a345d
child 25503 9f471b837330
child 25623 1b07381274f2
child 25627 b5a1a1f02388
child 25636 eac9208a2516
equal deleted inserted replaced
25456:39cfdc2dcaf3 25502:92dc8bd62c69
     1 /*
       
     2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 #include "precompiled.hpp"
       
    26 #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"
       
    27 #include "gc_implementation/shared/gcStats.hpp"
       
    28 #include "memory/defNewGeneration.hpp"
       
    29 #include "memory/genCollectedHeap.hpp"
       
    30 #include "runtime/thread.hpp"
       
    31 #ifdef TARGET_OS_FAMILY_linux
       
    32 # include "os_linux.inline.hpp"
       
    33 #endif
       
    34 #ifdef TARGET_OS_FAMILY_solaris
       
    35 # include "os_solaris.inline.hpp"
       
    36 #endif
       
    37 #ifdef TARGET_OS_FAMILY_windows
       
    38 # include "os_windows.inline.hpp"
       
    39 #endif
       
    40 #ifdef TARGET_OS_FAMILY_aix
       
    41 # include "os_aix.inline.hpp"
       
    42 #endif
       
    43 #ifdef TARGET_OS_FAMILY_bsd
       
    44 # include "os_bsd.inline.hpp"
       
    45 #endif
       
    46 elapsedTimer CMSAdaptiveSizePolicy::_concurrent_timer;
       
    47 elapsedTimer CMSAdaptiveSizePolicy::_STW_timer;
       
    48 
       
    49 // Defined if the granularity of the time measurements is potentially too large.
       
    50 #define CLOCK_GRANULARITY_TOO_LARGE
       
    51 
       
    52 CMSAdaptiveSizePolicy::CMSAdaptiveSizePolicy(size_t init_eden_size,
       
    53                                              size_t init_promo_size,
       
    54                                              size_t init_survivor_size,
       
    55                                              double max_gc_minor_pause_sec,
       
    56                                              double max_gc_pause_sec,
       
    57                                              uint gc_cost_ratio) :
       
    58   AdaptiveSizePolicy(init_eden_size,
       
    59                      init_promo_size,
       
    60                      init_survivor_size,
       
    61                      max_gc_pause_sec,
       
    62                      gc_cost_ratio) {
       
    63 
       
    64   clear_internal_time_intervals();
       
    65 
       
    66   _processor_count = os::active_processor_count();
       
    67 
       
    68   if (CMSConcurrentMTEnabled && (ConcGCThreads > 1)) {
       
    69     assert(_processor_count > 0, "Processor count is suspect");
       
    70     _concurrent_processor_count = MIN2((uint) ConcGCThreads,
       
    71                                        (uint) _processor_count);
       
    72   } else {
       
    73     _concurrent_processor_count = 1;
       
    74   }
       
    75 
       
    76   _avg_concurrent_time  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    77   _avg_concurrent_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    78   _avg_concurrent_gc_cost = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    79 
       
    80   _avg_initial_pause    = new AdaptivePaddedAverage(AdaptiveTimeWeight,
       
    81                                                     PausePadding);
       
    82   _avg_remark_pause     = new AdaptivePaddedAverage(AdaptiveTimeWeight,
       
    83                                                     PausePadding);
       
    84 
       
    85   _avg_cms_STW_time     = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    86   _avg_cms_STW_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    87 
       
    88   _avg_cms_free         = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    89   _avg_cms_free_at_sweep = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    90   _avg_cms_promo        = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    91 
       
    92   // Mark-sweep-compact
       
    93   _avg_msc_pause        = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    94   _avg_msc_interval     = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    95   _avg_msc_gc_cost      = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    96 
       
    97   // Mark-sweep
       
    98   _avg_ms_pause = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    99   _avg_ms_interval      = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
   100   _avg_ms_gc_cost       = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
   101 
       
   102   // Variables that estimate pause times as a function of generation
       
   103   // size.
       
   104   _remark_pause_old_estimator =
       
   105     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
   106   _initial_pause_old_estimator =
       
   107     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
   108   _remark_pause_young_estimator =
       
   109     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
   110   _initial_pause_young_estimator =
       
   111     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
   112 
       
   113   // Alignment comes from that used in ReservedSpace.
       
   114   _generation_alignment = os::vm_allocation_granularity();
       
   115 
       
   116   // Start the concurrent timer here so that the first
       
   117   // concurrent_phases_begin() measures a finite mutator
       
   118   // time.  A finite mutator time is used to determine
       
   119   // if a concurrent collection has been started.  If this
       
   120   // proves to be a problem, use some explicit flag to
       
   121   // signal that a concurrent collection has been started.
       
   122   _concurrent_timer.start();
       
   123   _STW_timer.start();
       
   124 }
       
   125 
       
   126 double CMSAdaptiveSizePolicy::concurrent_processor_fraction() {
       
   127   // For now assume no other daemon threads are taking alway
       
   128   // cpu's from the application.
       
   129   return ((double) _concurrent_processor_count / (double) _processor_count);
       
   130 }
       
   131 
       
   132 double CMSAdaptiveSizePolicy::concurrent_collection_cost(
       
   133                                                   double interval_in_seconds) {
       
   134   //  When the precleaning and sweeping phases use multiple
       
   135   // threads, change one_processor_fraction to
       
   136   // concurrent_processor_fraction().
       
   137   double one_processor_fraction = 1.0 / ((double) processor_count());
       
   138   double concurrent_cost =
       
   139     collection_cost(_latest_cms_concurrent_marking_time_secs,
       
   140                 interval_in_seconds) * concurrent_processor_fraction() +
       
   141     collection_cost(_latest_cms_concurrent_precleaning_time_secs,
       
   142                 interval_in_seconds) * one_processor_fraction +
       
   143     collection_cost(_latest_cms_concurrent_sweeping_time_secs,
       
   144                 interval_in_seconds) * one_processor_fraction;
       
   145   if (PrintAdaptiveSizePolicy && Verbose) {
       
   146     gclog_or_tty->print_cr(
       
   147       "\nCMSAdaptiveSizePolicy::scaled_concurrent_collection_cost(%f) "
       
   148       "_latest_cms_concurrent_marking_cost %f "
       
   149       "_latest_cms_concurrent_precleaning_cost %f "
       
   150       "_latest_cms_concurrent_sweeping_cost %f "
       
   151       "concurrent_processor_fraction %f "
       
   152       "concurrent_cost %f ",
       
   153       interval_in_seconds,
       
   154       collection_cost(_latest_cms_concurrent_marking_time_secs,
       
   155         interval_in_seconds),
       
   156       collection_cost(_latest_cms_concurrent_precleaning_time_secs,
       
   157         interval_in_seconds),
       
   158       collection_cost(_latest_cms_concurrent_sweeping_time_secs,
       
   159         interval_in_seconds),
       
   160       concurrent_processor_fraction(),
       
   161       concurrent_cost);
       
   162   }
       
   163   return concurrent_cost;
       
   164 }
       
   165 
       
   166 double CMSAdaptiveSizePolicy::concurrent_collection_time() {
       
   167   double latest_cms_sum_concurrent_phases_time_secs =
       
   168     _latest_cms_concurrent_marking_time_secs +
       
   169     _latest_cms_concurrent_precleaning_time_secs +
       
   170     _latest_cms_concurrent_sweeping_time_secs;
       
   171   return latest_cms_sum_concurrent_phases_time_secs;
       
   172 }
       
   173 
       
   174 double CMSAdaptiveSizePolicy::scaled_concurrent_collection_time() {
       
   175   //  When the precleaning and sweeping phases use multiple
       
   176   // threads, change one_processor_fraction to
       
   177   // concurrent_processor_fraction().
       
   178   double one_processor_fraction = 1.0 / ((double) processor_count());
       
   179   double latest_cms_sum_concurrent_phases_time_secs =
       
   180     _latest_cms_concurrent_marking_time_secs * concurrent_processor_fraction() +
       
   181     _latest_cms_concurrent_precleaning_time_secs * one_processor_fraction +
       
   182     _latest_cms_concurrent_sweeping_time_secs * one_processor_fraction ;
       
   183   if (PrintAdaptiveSizePolicy && Verbose) {
       
   184     gclog_or_tty->print_cr(
       
   185       "\nCMSAdaptiveSizePolicy::scaled_concurrent_collection_time "
       
   186       "_latest_cms_concurrent_marking_time_secs %f "
       
   187       "_latest_cms_concurrent_precleaning_time_secs %f "
       
   188       "_latest_cms_concurrent_sweeping_time_secs %f "
       
   189       "concurrent_processor_fraction %f "
       
   190       "latest_cms_sum_concurrent_phases_time_secs %f ",
       
   191       _latest_cms_concurrent_marking_time_secs,
       
   192       _latest_cms_concurrent_precleaning_time_secs,
       
   193       _latest_cms_concurrent_sweeping_time_secs,
       
   194       concurrent_processor_fraction(),
       
   195       latest_cms_sum_concurrent_phases_time_secs);
       
   196   }
       
   197   return latest_cms_sum_concurrent_phases_time_secs;
       
   198 }
       
   199 
       
   200 void CMSAdaptiveSizePolicy::update_minor_pause_old_estimator(
       
   201     double minor_pause_in_ms) {
       
   202   // Get the equivalent of the free space
       
   203   // that is available for promotions in the CMS generation
       
   204   // and use that to update _minor_pause_old_estimator
       
   205 
       
   206   // Don't implement this until it is needed. A warning is
       
   207   // printed if _minor_pause_old_estimator is used.
       
   208 }
       
   209 
       
   210 void CMSAdaptiveSizePolicy::concurrent_marking_begin() {
       
   211   if (PrintAdaptiveSizePolicy && Verbose) {
       
   212     gclog_or_tty->print(" ");
       
   213     gclog_or_tty->stamp();
       
   214     gclog_or_tty->print(": concurrent_marking_begin ");
       
   215   }
       
   216   //  Update the interval time
       
   217   _concurrent_timer.stop();
       
   218   _latest_cms_collection_end_to_collection_start_secs = _concurrent_timer.seconds();
       
   219   if (PrintAdaptiveSizePolicy && Verbose) {
       
   220     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::concurrent_marking_begin: "
       
   221     "mutator time %f", _latest_cms_collection_end_to_collection_start_secs);
       
   222   }
       
   223   _concurrent_timer.reset();
       
   224   _concurrent_timer.start();
       
   225 }
       
   226 
       
   227 void CMSAdaptiveSizePolicy::concurrent_marking_end() {
       
   228   if (PrintAdaptiveSizePolicy && Verbose) {
       
   229     gclog_or_tty->stamp();
       
   230     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::concurrent_marking_end()");
       
   231   }
       
   232 
       
   233   _concurrent_timer.stop();
       
   234   _latest_cms_concurrent_marking_time_secs = _concurrent_timer.seconds();
       
   235 
       
   236   if (PrintAdaptiveSizePolicy && Verbose) {
       
   237     gclog_or_tty->print_cr("\n CMSAdaptiveSizePolicy::concurrent_marking_end"
       
   238       ":concurrent marking time (s) %f",
       
   239       _latest_cms_concurrent_marking_time_secs);
       
   240   }
       
   241 }
       
   242 
       
   243 void CMSAdaptiveSizePolicy::concurrent_precleaning_begin() {
       
   244   if (PrintAdaptiveSizePolicy && Verbose) {
       
   245     gclog_or_tty->stamp();
       
   246     gclog_or_tty->print_cr(
       
   247       "CMSAdaptiveSizePolicy::concurrent_precleaning_begin()");
       
   248   }
       
   249   _concurrent_timer.reset();
       
   250   _concurrent_timer.start();
       
   251 }
       
   252 
       
   253 
       
   254 void CMSAdaptiveSizePolicy::concurrent_precleaning_end() {
       
   255   if (PrintAdaptiveSizePolicy && Verbose) {
       
   256     gclog_or_tty->stamp();
       
   257     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::concurrent_precleaning_end()");
       
   258   }
       
   259 
       
   260   _concurrent_timer.stop();
       
   261   // May be set again by a second call during the same collection.
       
   262   _latest_cms_concurrent_precleaning_time_secs = _concurrent_timer.seconds();
       
   263 
       
   264   if (PrintAdaptiveSizePolicy && Verbose) {
       
   265     gclog_or_tty->print_cr("\n CMSAdaptiveSizePolicy::concurrent_precleaning_end"
       
   266       ":concurrent precleaning time (s) %f",
       
   267       _latest_cms_concurrent_precleaning_time_secs);
       
   268   }
       
   269 }
       
   270 
       
   271 void CMSAdaptiveSizePolicy::concurrent_sweeping_begin() {
       
   272   if (PrintAdaptiveSizePolicy && Verbose) {
       
   273     gclog_or_tty->stamp();
       
   274     gclog_or_tty->print_cr(
       
   275       "CMSAdaptiveSizePolicy::concurrent_sweeping_begin()");
       
   276   }
       
   277   _concurrent_timer.reset();
       
   278   _concurrent_timer.start();
       
   279 }
       
   280 
       
   281 
       
   282 void CMSAdaptiveSizePolicy::concurrent_sweeping_end() {
       
   283   if (PrintAdaptiveSizePolicy && Verbose) {
       
   284     gclog_or_tty->stamp();
       
   285     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::concurrent_sweeping_end()");
       
   286   }
       
   287 
       
   288   _concurrent_timer.stop();
       
   289   _latest_cms_concurrent_sweeping_time_secs = _concurrent_timer.seconds();
       
   290 
       
   291   if (PrintAdaptiveSizePolicy && Verbose) {
       
   292     gclog_or_tty->print_cr("\n CMSAdaptiveSizePolicy::concurrent_sweeping_end"
       
   293       ":concurrent sweeping time (s) %f",
       
   294       _latest_cms_concurrent_sweeping_time_secs);
       
   295   }
       
   296 }
       
   297 
       
   298 void CMSAdaptiveSizePolicy::concurrent_phases_end(GCCause::Cause gc_cause,
       
   299                                                   size_t cur_eden,
       
   300                                                   size_t cur_promo) {
       
   301   if (PrintAdaptiveSizePolicy && Verbose) {
       
   302     gclog_or_tty->print(" ");
       
   303     gclog_or_tty->stamp();
       
   304     gclog_or_tty->print(": concurrent_phases_end ");
       
   305   }
       
   306 
       
   307   // Update the concurrent timer
       
   308   _concurrent_timer.stop();
       
   309 
       
   310   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   311       UseAdaptiveSizePolicyWithSystemGC) {
       
   312 
       
   313     avg_cms_free()->sample(cur_promo);
       
   314     double latest_cms_sum_concurrent_phases_time_secs =
       
   315       concurrent_collection_time();
       
   316 
       
   317     _avg_concurrent_time->sample(latest_cms_sum_concurrent_phases_time_secs);
       
   318 
       
   319     // Cost of collection (unit-less)
       
   320 
       
   321     // Total interval for collection.  May not be valid.  Tests
       
   322     // below determine whether to use this.
       
   323     //
       
   324   if (PrintAdaptiveSizePolicy && Verbose) {
       
   325     gclog_or_tty->print_cr("\nCMSAdaptiveSizePolicy::concurrent_phases_end \n"
       
   326       "_latest_cms_reset_end_to_initial_mark_start_secs %f \n"
       
   327       "_latest_cms_initial_mark_start_to_end_time_secs %f \n"
       
   328       "_latest_cms_remark_start_to_end_time_secs %f \n"
       
   329       "_latest_cms_concurrent_marking_time_secs %f \n"
       
   330       "_latest_cms_concurrent_precleaning_time_secs %f \n"
       
   331       "_latest_cms_concurrent_sweeping_time_secs %f \n"
       
   332       "latest_cms_sum_concurrent_phases_time_secs %f \n"
       
   333       "_latest_cms_collection_end_to_collection_start_secs %f \n"
       
   334       "concurrent_processor_fraction %f",
       
   335       _latest_cms_reset_end_to_initial_mark_start_secs,
       
   336       _latest_cms_initial_mark_start_to_end_time_secs,
       
   337       _latest_cms_remark_start_to_end_time_secs,
       
   338       _latest_cms_concurrent_marking_time_secs,
       
   339       _latest_cms_concurrent_precleaning_time_secs,
       
   340       _latest_cms_concurrent_sweeping_time_secs,
       
   341       latest_cms_sum_concurrent_phases_time_secs,
       
   342       _latest_cms_collection_end_to_collection_start_secs,
       
   343       concurrent_processor_fraction());
       
   344   }
       
   345     double interval_in_seconds =
       
   346       _latest_cms_initial_mark_start_to_end_time_secs +
       
   347       _latest_cms_remark_start_to_end_time_secs +
       
   348       latest_cms_sum_concurrent_phases_time_secs +
       
   349       _latest_cms_collection_end_to_collection_start_secs;
       
   350     assert(interval_in_seconds >= 0.0,
       
   351       "Bad interval between cms collections");
       
   352 
       
   353     // Sample for performance counter
       
   354     avg_concurrent_interval()->sample(interval_in_seconds);
       
   355 
       
   356     // STW costs (initial and remark pauses)
       
   357     // Cost of collection (unit-less)
       
   358     assert(_latest_cms_initial_mark_start_to_end_time_secs >= 0.0,
       
   359       "Bad initial mark pause");
       
   360     assert(_latest_cms_remark_start_to_end_time_secs >= 0.0,
       
   361       "Bad remark pause");
       
   362     double STW_time_in_seconds =
       
   363       _latest_cms_initial_mark_start_to_end_time_secs +
       
   364       _latest_cms_remark_start_to_end_time_secs;
       
   365     double STW_collection_cost = 0.0;
       
   366     if (interval_in_seconds > 0.0) {
       
   367       // cost for the STW phases of the concurrent collection.
       
   368       STW_collection_cost = STW_time_in_seconds / interval_in_seconds;
       
   369       avg_cms_STW_gc_cost()->sample(STW_collection_cost);
       
   370     }
       
   371     if (PrintAdaptiveSizePolicy && Verbose) {
       
   372       gclog_or_tty->print("cmsAdaptiveSizePolicy::STW_collection_end: "
       
   373         "STW gc cost: %f  average: %f", STW_collection_cost,
       
   374         avg_cms_STW_gc_cost()->average());
       
   375       gclog_or_tty->print_cr("  STW pause: %f (ms) STW period %f (ms)",
       
   376         (double) STW_time_in_seconds * MILLIUNITS,
       
   377         (double) interval_in_seconds * MILLIUNITS);
       
   378     }
       
   379 
       
   380     double concurrent_cost = 0.0;
       
   381     if (latest_cms_sum_concurrent_phases_time_secs > 0.0) {
       
   382       concurrent_cost = concurrent_collection_cost(interval_in_seconds);
       
   383 
       
   384       avg_concurrent_gc_cost()->sample(concurrent_cost);
       
   385       // Average this ms cost into all the other types gc costs
       
   386 
       
   387       if (PrintAdaptiveSizePolicy && Verbose) {
       
   388         gclog_or_tty->print("cmsAdaptiveSizePolicy::concurrent_phases_end: "
       
   389           "concurrent gc cost: %f  average: %f",
       
   390           concurrent_cost,
       
   391           _avg_concurrent_gc_cost->average());
       
   392         gclog_or_tty->print_cr("  concurrent time: %f (ms) cms period %f (ms)"
       
   393           " processor fraction: %f",
       
   394           latest_cms_sum_concurrent_phases_time_secs * MILLIUNITS,
       
   395           interval_in_seconds * MILLIUNITS,
       
   396           concurrent_processor_fraction());
       
   397       }
       
   398     }
       
   399     double total_collection_cost = STW_collection_cost + concurrent_cost;
       
   400     avg_major_gc_cost()->sample(total_collection_cost);
       
   401 
       
   402     // Gather information for estimating future behavior
       
   403     double initial_pause_in_ms = _latest_cms_initial_mark_start_to_end_time_secs * MILLIUNITS;
       
   404     double remark_pause_in_ms = _latest_cms_remark_start_to_end_time_secs * MILLIUNITS;
       
   405 
       
   406     double cur_promo_size_in_mbytes = ((double)cur_promo)/((double)M);
       
   407     initial_pause_old_estimator()->update(cur_promo_size_in_mbytes,
       
   408       initial_pause_in_ms);
       
   409     remark_pause_old_estimator()->update(cur_promo_size_in_mbytes,
       
   410       remark_pause_in_ms);
       
   411     major_collection_estimator()->update(cur_promo_size_in_mbytes,
       
   412       total_collection_cost);
       
   413 
       
   414     // This estimate uses the average eden size.  It could also
       
   415     // have used the latest eden size.  Which is better?
       
   416     double cur_eden_size_in_mbytes = ((double)cur_eden)/((double) M);
       
   417     initial_pause_young_estimator()->update(cur_eden_size_in_mbytes,
       
   418       initial_pause_in_ms);
       
   419     remark_pause_young_estimator()->update(cur_eden_size_in_mbytes,
       
   420       remark_pause_in_ms);
       
   421   }
       
   422 
       
   423   clear_internal_time_intervals();
       
   424 
       
   425   set_first_after_collection();
       
   426 
       
   427   // The concurrent phases keeps track of it's own mutator interval
       
   428   // with this timer.  This allows the stop-the-world phase to
       
   429   // be included in the mutator time so that the stop-the-world time
       
   430   // is not double counted.  Reset and start it.
       
   431   _concurrent_timer.reset();
       
   432   _concurrent_timer.start();
       
   433 
       
   434   // The mutator time between STW phases does not include the
       
   435   // concurrent collection time.
       
   436   _STW_timer.reset();
       
   437   _STW_timer.start();
       
   438 }
       
   439 
       
   440 void CMSAdaptiveSizePolicy::checkpoint_roots_initial_begin() {
       
   441   //  Update the interval time
       
   442   _STW_timer.stop();
       
   443   _latest_cms_reset_end_to_initial_mark_start_secs = _STW_timer.seconds();
       
   444   // Reset for the initial mark
       
   445   _STW_timer.reset();
       
   446   _STW_timer.start();
       
   447 }
       
   448 
       
   449 void CMSAdaptiveSizePolicy::checkpoint_roots_initial_end(
       
   450     GCCause::Cause gc_cause) {
       
   451   _STW_timer.stop();
       
   452 
       
   453   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   454       UseAdaptiveSizePolicyWithSystemGC) {
       
   455     _latest_cms_initial_mark_start_to_end_time_secs = _STW_timer.seconds();
       
   456     avg_initial_pause()->sample(_latest_cms_initial_mark_start_to_end_time_secs);
       
   457 
       
   458     if (PrintAdaptiveSizePolicy && Verbose) {
       
   459       gclog_or_tty->print(
       
   460         "cmsAdaptiveSizePolicy::checkpoint_roots_initial_end: "
       
   461         "initial pause: %f ", _latest_cms_initial_mark_start_to_end_time_secs);
       
   462     }
       
   463   }
       
   464 
       
   465   _STW_timer.reset();
       
   466   _STW_timer.start();
       
   467 }
       
   468 
       
   469 void CMSAdaptiveSizePolicy::checkpoint_roots_final_begin() {
       
   470   _STW_timer.stop();
       
   471   _latest_cms_initial_mark_end_to_remark_start_secs = _STW_timer.seconds();
       
   472   // Start accumulating time for the remark in the STW timer.
       
   473   _STW_timer.reset();
       
   474   _STW_timer.start();
       
   475 }
       
   476 
       
   477 void CMSAdaptiveSizePolicy::checkpoint_roots_final_end(
       
   478     GCCause::Cause gc_cause) {
       
   479   _STW_timer.stop();
       
   480   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   481       UseAdaptiveSizePolicyWithSystemGC) {
       
   482     // Total initial mark pause + remark pause.
       
   483     _latest_cms_remark_start_to_end_time_secs = _STW_timer.seconds();
       
   484     double STW_time_in_seconds = _latest_cms_initial_mark_start_to_end_time_secs +
       
   485       _latest_cms_remark_start_to_end_time_secs;
       
   486     double STW_time_in_ms = STW_time_in_seconds * MILLIUNITS;
       
   487 
       
   488     avg_remark_pause()->sample(_latest_cms_remark_start_to_end_time_secs);
       
   489 
       
   490     // Sample total for initial mark + remark
       
   491     avg_cms_STW_time()->sample(STW_time_in_seconds);
       
   492 
       
   493     if (PrintAdaptiveSizePolicy && Verbose) {
       
   494       gclog_or_tty->print("cmsAdaptiveSizePolicy::checkpoint_roots_final_end: "
       
   495         "remark pause: %f", _latest_cms_remark_start_to_end_time_secs);
       
   496     }
       
   497 
       
   498   }
       
   499   // Don't start the STW times here because the concurrent
       
   500   // sweep and reset has not happened.
       
   501   //  Keep the old comment above in case I don't understand
       
   502   // what is going on but now
       
   503   // Start the STW timer because it is used by ms_collection_begin()
       
   504   // and ms_collection_end() to get the sweep time if a MS is being
       
   505   // done in the foreground.
       
   506   _STW_timer.reset();
       
   507   _STW_timer.start();
       
   508 }
       
   509 
       
   510 void CMSAdaptiveSizePolicy::msc_collection_begin() {
       
   511   if (PrintAdaptiveSizePolicy && Verbose) {
       
   512     gclog_or_tty->print(" ");
       
   513     gclog_or_tty->stamp();
       
   514     gclog_or_tty->print(": msc_collection_begin ");
       
   515   }
       
   516   _STW_timer.stop();
       
   517   _latest_cms_msc_end_to_msc_start_time_secs = _STW_timer.seconds();
       
   518   if (PrintAdaptiveSizePolicy && Verbose) {
       
   519     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::msc_collection_begin: "
       
   520       "mutator time %f",
       
   521       _latest_cms_msc_end_to_msc_start_time_secs);
       
   522   }
       
   523   avg_msc_interval()->sample(_latest_cms_msc_end_to_msc_start_time_secs);
       
   524   _STW_timer.reset();
       
   525   _STW_timer.start();
       
   526 }
       
   527 
       
   528 void CMSAdaptiveSizePolicy::msc_collection_end(GCCause::Cause gc_cause) {
       
   529   if (PrintAdaptiveSizePolicy && Verbose) {
       
   530     gclog_or_tty->print(" ");
       
   531     gclog_or_tty->stamp();
       
   532     gclog_or_tty->print(": msc_collection_end ");
       
   533   }
       
   534   _STW_timer.stop();
       
   535   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   536         UseAdaptiveSizePolicyWithSystemGC) {
       
   537     double msc_pause_in_seconds = _STW_timer.seconds();
       
   538     if ((_latest_cms_msc_end_to_msc_start_time_secs > 0.0) &&
       
   539         (msc_pause_in_seconds > 0.0)) {
       
   540       avg_msc_pause()->sample(msc_pause_in_seconds);
       
   541       double mutator_time_in_seconds = 0.0;
       
   542       if (_latest_cms_collection_end_to_collection_start_secs == 0.0) {
       
   543         // This assertion may fail because of time stamp granularity.
       
   544         // Comment it out and investigate it at a later time.  The large
       
   545         // time stamp granularity occurs on some older linux systems.
       
   546 #ifndef CLOCK_GRANULARITY_TOO_LARGE
       
   547         assert((_latest_cms_concurrent_marking_time_secs == 0.0) &&
       
   548                (_latest_cms_concurrent_precleaning_time_secs == 0.0) &&
       
   549                (_latest_cms_concurrent_sweeping_time_secs == 0.0),
       
   550           "There should not be any concurrent time");
       
   551 #endif
       
   552         // A concurrent collection did not start.  Mutator time
       
   553         // between collections comes from the STW MSC timer.
       
   554         mutator_time_in_seconds = _latest_cms_msc_end_to_msc_start_time_secs;
       
   555       } else {
       
   556         // The concurrent collection did start so count the mutator
       
   557         // time to the start of the concurrent collection.  In this
       
   558         // case the _latest_cms_msc_end_to_msc_start_time_secs measures
       
   559         // the time between the initial mark or remark and the
       
   560         // start of the MSC.  That has no real meaning.
       
   561         mutator_time_in_seconds = _latest_cms_collection_end_to_collection_start_secs;
       
   562       }
       
   563 
       
   564       double latest_cms_sum_concurrent_phases_time_secs =
       
   565         concurrent_collection_time();
       
   566       double interval_in_seconds =
       
   567         mutator_time_in_seconds +
       
   568         _latest_cms_initial_mark_start_to_end_time_secs +
       
   569         _latest_cms_remark_start_to_end_time_secs +
       
   570         latest_cms_sum_concurrent_phases_time_secs +
       
   571         msc_pause_in_seconds;
       
   572 
       
   573       if (PrintAdaptiveSizePolicy && Verbose) {
       
   574         gclog_or_tty->print_cr("  interval_in_seconds %f \n"
       
   575           "     mutator_time_in_seconds %f \n"
       
   576           "     _latest_cms_initial_mark_start_to_end_time_secs %f\n"
       
   577           "     _latest_cms_remark_start_to_end_time_secs %f\n"
       
   578           "     latest_cms_sum_concurrent_phases_time_secs %f\n"
       
   579           "     msc_pause_in_seconds %f\n",
       
   580           interval_in_seconds,
       
   581           mutator_time_in_seconds,
       
   582           _latest_cms_initial_mark_start_to_end_time_secs,
       
   583           _latest_cms_remark_start_to_end_time_secs,
       
   584           latest_cms_sum_concurrent_phases_time_secs,
       
   585           msc_pause_in_seconds);
       
   586       }
       
   587 
       
   588       // The concurrent cost is wasted cost but it should be
       
   589       // included.
       
   590       double concurrent_cost = concurrent_collection_cost(interval_in_seconds);
       
   591 
       
   592       // Initial mark and remark, also wasted.
       
   593       double STW_time_in_seconds = _latest_cms_initial_mark_start_to_end_time_secs +
       
   594         _latest_cms_remark_start_to_end_time_secs;
       
   595       double STW_collection_cost =
       
   596         collection_cost(STW_time_in_seconds, interval_in_seconds) +
       
   597         concurrent_cost;
       
   598 
       
   599       if (PrintAdaptiveSizePolicy && Verbose) {
       
   600         gclog_or_tty->print_cr(" msc_collection_end:\n"
       
   601           "_latest_cms_collection_end_to_collection_start_secs %f\n"
       
   602           "_latest_cms_msc_end_to_msc_start_time_secs %f\n"
       
   603           "_latest_cms_initial_mark_start_to_end_time_secs %f\n"
       
   604           "_latest_cms_remark_start_to_end_time_secs %f\n"
       
   605           "latest_cms_sum_concurrent_phases_time_secs %f\n",
       
   606           _latest_cms_collection_end_to_collection_start_secs,
       
   607           _latest_cms_msc_end_to_msc_start_time_secs,
       
   608           _latest_cms_initial_mark_start_to_end_time_secs,
       
   609           _latest_cms_remark_start_to_end_time_secs,
       
   610           latest_cms_sum_concurrent_phases_time_secs);
       
   611 
       
   612         gclog_or_tty->print_cr(" msc_collection_end: \n"
       
   613           "latest_cms_sum_concurrent_phases_time_secs %f\n"
       
   614           "STW_time_in_seconds %f\n"
       
   615           "msc_pause_in_seconds %f\n",
       
   616           latest_cms_sum_concurrent_phases_time_secs,
       
   617           STW_time_in_seconds,
       
   618           msc_pause_in_seconds);
       
   619       }
       
   620 
       
   621       double cost = concurrent_cost + STW_collection_cost +
       
   622         collection_cost(msc_pause_in_seconds, interval_in_seconds);
       
   623 
       
   624       _avg_msc_gc_cost->sample(cost);
       
   625 
       
   626       // Average this ms cost into all the other types gc costs
       
   627       avg_major_gc_cost()->sample(cost);
       
   628 
       
   629       // Sample for performance counter
       
   630       _avg_msc_interval->sample(interval_in_seconds);
       
   631       if (PrintAdaptiveSizePolicy && Verbose) {
       
   632         gclog_or_tty->print("cmsAdaptiveSizePolicy::msc_collection_end: "
       
   633           "MSC gc cost: %f  average: %f", cost,
       
   634           _avg_msc_gc_cost->average());
       
   635 
       
   636         double msc_pause_in_ms = msc_pause_in_seconds * MILLIUNITS;
       
   637         gclog_or_tty->print_cr("  MSC pause: %f (ms) MSC period %f (ms)",
       
   638           msc_pause_in_ms, (double) interval_in_seconds * MILLIUNITS);
       
   639       }
       
   640     }
       
   641   }
       
   642 
       
   643   clear_internal_time_intervals();
       
   644 
       
   645   // Can this call be put into the epilogue?
       
   646   set_first_after_collection();
       
   647 
       
   648   // The concurrent phases keeps track of it's own mutator interval
       
   649   // with this timer.  This allows the stop-the-world phase to
       
   650   // be included in the mutator time so that the stop-the-world time
       
   651   // is not double counted.  Reset and start it.
       
   652   _concurrent_timer.stop();
       
   653   _concurrent_timer.reset();
       
   654   _concurrent_timer.start();
       
   655 
       
   656   _STW_timer.reset();
       
   657   _STW_timer.start();
       
   658 }
       
   659 
       
   660 void CMSAdaptiveSizePolicy::ms_collection_begin() {
       
   661   if (PrintAdaptiveSizePolicy && Verbose) {
       
   662     gclog_or_tty->print(" ");
       
   663     gclog_or_tty->stamp();
       
   664     gclog_or_tty->print(": ms_collection_begin ");
       
   665   }
       
   666   _STW_timer.stop();
       
   667   _latest_cms_ms_end_to_ms_start = _STW_timer.seconds();
       
   668   if (PrintAdaptiveSizePolicy && Verbose) {
       
   669     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::ms_collection_begin: "
       
   670       "mutator time %f",
       
   671       _latest_cms_ms_end_to_ms_start);
       
   672   }
       
   673   avg_ms_interval()->sample(_STW_timer.seconds());
       
   674   _STW_timer.reset();
       
   675   _STW_timer.start();
       
   676 }
       
   677 
       
   678 void CMSAdaptiveSizePolicy::ms_collection_end(GCCause::Cause gc_cause) {
       
   679   if (PrintAdaptiveSizePolicy && Verbose) {
       
   680     gclog_or_tty->print(" ");
       
   681     gclog_or_tty->stamp();
       
   682     gclog_or_tty->print(": ms_collection_end ");
       
   683   }
       
   684   _STW_timer.stop();
       
   685   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   686         UseAdaptiveSizePolicyWithSystemGC) {
       
   687     // The MS collection is a foreground collection that does all
       
   688     // the parts of a mostly concurrent collection.
       
   689     //
       
   690     // For this collection include the cost of the
       
   691     //  initial mark
       
   692     //  remark
       
   693     //  all concurrent time (scaled down by the
       
   694     //    concurrent_processor_fraction).  Some
       
   695     //    may be zero if the baton was passed before
       
   696     //    it was reached.
       
   697     //    concurrent marking
       
   698     //    sweeping
       
   699     //    resetting
       
   700     //  STW after baton was passed (STW_in_foreground_in_seconds)
       
   701     double STW_in_foreground_in_seconds = _STW_timer.seconds();
       
   702 
       
   703     double latest_cms_sum_concurrent_phases_time_secs =
       
   704       concurrent_collection_time();
       
   705     if (PrintAdaptiveSizePolicy && Verbose) {
       
   706       gclog_or_tty->print_cr("\nCMSAdaptiveSizePolicy::ms_collection_end "
       
   707         "STW_in_foreground_in_seconds %f "
       
   708         "_latest_cms_initial_mark_start_to_end_time_secs %f "
       
   709         "_latest_cms_remark_start_to_end_time_secs %f "
       
   710         "latest_cms_sum_concurrent_phases_time_secs %f "
       
   711         "_latest_cms_ms_marking_start_to_end_time_secs %f "
       
   712         "_latest_cms_ms_end_to_ms_start %f",
       
   713         STW_in_foreground_in_seconds,
       
   714         _latest_cms_initial_mark_start_to_end_time_secs,
       
   715         _latest_cms_remark_start_to_end_time_secs,
       
   716         latest_cms_sum_concurrent_phases_time_secs,
       
   717         _latest_cms_ms_marking_start_to_end_time_secs,
       
   718         _latest_cms_ms_end_to_ms_start);
       
   719     }
       
   720 
       
   721     double STW_marking_in_seconds = _latest_cms_initial_mark_start_to_end_time_secs +
       
   722       _latest_cms_remark_start_to_end_time_secs;
       
   723 #ifndef CLOCK_GRANULARITY_TOO_LARGE
       
   724     assert(_latest_cms_ms_marking_start_to_end_time_secs == 0.0 ||
       
   725            latest_cms_sum_concurrent_phases_time_secs == 0.0,
       
   726            "marking done twice?");
       
   727 #endif
       
   728     double ms_time_in_seconds = STW_marking_in_seconds +
       
   729       STW_in_foreground_in_seconds +
       
   730       _latest_cms_ms_marking_start_to_end_time_secs +
       
   731       scaled_concurrent_collection_time();
       
   732     avg_ms_pause()->sample(ms_time_in_seconds);
       
   733     // Use the STW costs from the initial mark and remark plus
       
   734     // the cost of the concurrent phase to calculate a
       
   735     // collection cost.
       
   736     double cost = 0.0;
       
   737     if ((_latest_cms_ms_end_to_ms_start > 0.0) &&
       
   738         (ms_time_in_seconds > 0.0)) {
       
   739       double interval_in_seconds =
       
   740         _latest_cms_ms_end_to_ms_start + ms_time_in_seconds;
       
   741 
       
   742       if (PrintAdaptiveSizePolicy && Verbose) {
       
   743         gclog_or_tty->print_cr("\n ms_time_in_seconds  %f  "
       
   744           "latest_cms_sum_concurrent_phases_time_secs %f  "
       
   745           "interval_in_seconds %f",
       
   746           ms_time_in_seconds,
       
   747           latest_cms_sum_concurrent_phases_time_secs,
       
   748           interval_in_seconds);
       
   749       }
       
   750 
       
   751       cost = collection_cost(ms_time_in_seconds, interval_in_seconds);
       
   752 
       
   753       _avg_ms_gc_cost->sample(cost);
       
   754       // Average this ms cost into all the other types gc costs
       
   755       avg_major_gc_cost()->sample(cost);
       
   756 
       
   757       // Sample for performance counter
       
   758       _avg_ms_interval->sample(interval_in_seconds);
       
   759     }
       
   760     if (PrintAdaptiveSizePolicy && Verbose) {
       
   761       gclog_or_tty->print("cmsAdaptiveSizePolicy::ms_collection_end: "
       
   762         "MS gc cost: %f  average: %f", cost, _avg_ms_gc_cost->average());
       
   763 
       
   764       double ms_time_in_ms = ms_time_in_seconds * MILLIUNITS;
       
   765       gclog_or_tty->print_cr("  MS pause: %f (ms) MS period %f (ms)",
       
   766         ms_time_in_ms,
       
   767         _latest_cms_ms_end_to_ms_start * MILLIUNITS);
       
   768     }
       
   769   }
       
   770 
       
   771   // Consider putting this code (here to end) into a
       
   772   // method for convenience.
       
   773   clear_internal_time_intervals();
       
   774 
       
   775   set_first_after_collection();
       
   776 
       
   777   // The concurrent phases keeps track of it's own mutator interval
       
   778   // with this timer.  This allows the stop-the-world phase to
       
   779   // be included in the mutator time so that the stop-the-world time
       
   780   // is not double counted.  Reset and start it.
       
   781   _concurrent_timer.stop();
       
   782   _concurrent_timer.reset();
       
   783   _concurrent_timer.start();
       
   784 
       
   785   _STW_timer.reset();
       
   786   _STW_timer.start();
       
   787 }
       
   788 
       
   789 void CMSAdaptiveSizePolicy::clear_internal_time_intervals() {
       
   790   _latest_cms_reset_end_to_initial_mark_start_secs = 0.0;
       
   791   _latest_cms_initial_mark_end_to_remark_start_secs = 0.0;
       
   792   _latest_cms_collection_end_to_collection_start_secs = 0.0;
       
   793   _latest_cms_concurrent_marking_time_secs = 0.0;
       
   794   _latest_cms_concurrent_precleaning_time_secs = 0.0;
       
   795   _latest_cms_concurrent_sweeping_time_secs = 0.0;
       
   796   _latest_cms_msc_end_to_msc_start_time_secs = 0.0;
       
   797   _latest_cms_ms_end_to_ms_start = 0.0;
       
   798   _latest_cms_remark_start_to_end_time_secs = 0.0;
       
   799   _latest_cms_initial_mark_start_to_end_time_secs = 0.0;
       
   800   _latest_cms_ms_marking_start_to_end_time_secs = 0.0;
       
   801 }
       
   802 
       
   803 void CMSAdaptiveSizePolicy::clear_generation_free_space_flags() {
       
   804   AdaptiveSizePolicy::clear_generation_free_space_flags();
       
   805 
       
   806   set_change_young_gen_for_maj_pauses(0);
       
   807 }
       
   808 
       
   809 void CMSAdaptiveSizePolicy::concurrent_phases_resume() {
       
   810   if (PrintAdaptiveSizePolicy && Verbose) {
       
   811     gclog_or_tty->stamp();
       
   812     gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::concurrent_phases_resume()");
       
   813   }
       
   814   _concurrent_timer.start();
       
   815 }
       
   816 
       
   817 double CMSAdaptiveSizePolicy::time_since_major_gc() const {
       
   818   _concurrent_timer.stop();
       
   819   double time_since_cms_gc = _concurrent_timer.seconds();
       
   820   _concurrent_timer.start();
       
   821   _STW_timer.stop();
       
   822   double time_since_STW_gc = _STW_timer.seconds();
       
   823   _STW_timer.start();
       
   824 
       
   825   return MIN2(time_since_cms_gc, time_since_STW_gc);
       
   826 }
       
   827 
       
   828 double CMSAdaptiveSizePolicy::major_gc_interval_average_for_decay() const {
       
   829   double cms_interval = _avg_concurrent_interval->average();
       
   830   double msc_interval = _avg_msc_interval->average();
       
   831   double ms_interval = _avg_ms_interval->average();
       
   832 
       
   833   return MAX3(cms_interval, msc_interval, ms_interval);
       
   834 }
       
   835 
       
   836 double CMSAdaptiveSizePolicy::cms_gc_cost() const {
       
   837   return avg_major_gc_cost()->average();
       
   838 }
       
   839 
       
   840 void CMSAdaptiveSizePolicy::ms_collection_marking_begin() {
       
   841   _STW_timer.stop();
       
   842   // Start accumulating time for the marking in the STW timer.
       
   843   _STW_timer.reset();
       
   844   _STW_timer.start();
       
   845 }
       
   846 
       
   847 void CMSAdaptiveSizePolicy::ms_collection_marking_end(
       
   848     GCCause::Cause gc_cause) {
       
   849   _STW_timer.stop();
       
   850   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   851       UseAdaptiveSizePolicyWithSystemGC) {
       
   852     _latest_cms_ms_marking_start_to_end_time_secs = _STW_timer.seconds();
       
   853     if (PrintAdaptiveSizePolicy && Verbose) {
       
   854       gclog_or_tty->print_cr("CMSAdaptiveSizePolicy::"
       
   855         "msc_collection_marking_end: mutator time %f",
       
   856         _latest_cms_ms_marking_start_to_end_time_secs);
       
   857     }
       
   858   }
       
   859   _STW_timer.reset();
       
   860   _STW_timer.start();
       
   861 }
       
   862 
       
   863 double CMSAdaptiveSizePolicy::gc_cost() const {
       
   864   double cms_gen_cost = cms_gc_cost();
       
   865   double result =  MIN2(1.0, minor_gc_cost() + cms_gen_cost);
       
   866   assert(result >= 0.0, "Both minor and major costs are non-negative");
       
   867   return result;
       
   868 }
       
   869 
       
   870 // Cost of collection (unit-less)
       
   871 double CMSAdaptiveSizePolicy::collection_cost(double pause_in_seconds,
       
   872                                               double interval_in_seconds) {
       
   873   // Cost of collection (unit-less)
       
   874   double cost = 0.0;
       
   875   if ((interval_in_seconds > 0.0) &&
       
   876       (pause_in_seconds > 0.0)) {
       
   877     cost =
       
   878       pause_in_seconds / interval_in_seconds;
       
   879   }
       
   880   return cost;
       
   881 }
       
   882 
       
   883 size_t CMSAdaptiveSizePolicy::adjust_eden_for_pause_time(size_t cur_eden) {
       
   884   size_t change = 0;
       
   885   size_t desired_eden = cur_eden;
       
   886 
       
   887   // reduce eden size
       
   888   change = eden_decrement_aligned_down(cur_eden);
       
   889   desired_eden = cur_eden - change;
       
   890 
       
   891   if (PrintAdaptiveSizePolicy && Verbose) {
       
   892     gclog_or_tty->print_cr(
       
   893       "CMSAdaptiveSizePolicy::adjust_eden_for_pause_time "
       
   894       "adjusting eden for pause time. "
       
   895       " starting eden size " SIZE_FORMAT
       
   896       " reduced eden size " SIZE_FORMAT
       
   897       " eden delta " SIZE_FORMAT,
       
   898       cur_eden, desired_eden, change);
       
   899   }
       
   900 
       
   901   return desired_eden;
       
   902 }
       
   903 
       
   904 size_t CMSAdaptiveSizePolicy::adjust_eden_for_throughput(size_t cur_eden) {
       
   905 
       
   906   size_t desired_eden = cur_eden;
       
   907 
       
   908   set_change_young_gen_for_throughput(increase_young_gen_for_througput_true);
       
   909 
       
   910   size_t change = eden_increment_aligned_up(cur_eden);
       
   911   size_t scaled_change = scale_by_gen_gc_cost(change, minor_gc_cost());
       
   912 
       
   913   if (cur_eden + scaled_change > cur_eden) {
       
   914     desired_eden = cur_eden + scaled_change;
       
   915   }
       
   916 
       
   917   _young_gen_change_for_minor_throughput++;
       
   918 
       
   919   if (PrintAdaptiveSizePolicy && Verbose) {
       
   920     gclog_or_tty->print_cr(
       
   921       "CMSAdaptiveSizePolicy::adjust_eden_for_throughput "
       
   922       "adjusting eden for throughput. "
       
   923       " starting eden size " SIZE_FORMAT
       
   924       " increased eden size " SIZE_FORMAT
       
   925       " eden delta " SIZE_FORMAT,
       
   926       cur_eden, desired_eden, scaled_change);
       
   927   }
       
   928 
       
   929   return desired_eden;
       
   930 }
       
   931 
       
   932 size_t CMSAdaptiveSizePolicy::adjust_eden_for_footprint(size_t cur_eden) {
       
   933 
       
   934   set_decrease_for_footprint(decrease_young_gen_for_footprint_true);
       
   935 
       
   936   size_t change = eden_decrement(cur_eden);
       
   937   size_t desired_eden_size = cur_eden - change;
       
   938 
       
   939   if (PrintAdaptiveSizePolicy && Verbose) {
       
   940     gclog_or_tty->print_cr(
       
   941       "CMSAdaptiveSizePolicy::adjust_eden_for_footprint "
       
   942       "adjusting eden for footprint. "
       
   943       " starting eden size " SIZE_FORMAT
       
   944       " reduced eden size " SIZE_FORMAT
       
   945       " eden delta " SIZE_FORMAT,
       
   946       cur_eden, desired_eden_size, change);
       
   947   }
       
   948   return desired_eden_size;
       
   949 }
       
   950 
       
   951 // The eden and promo versions should be combined if possible.
       
   952 // They are the same except that the sizes of the decrement
       
   953 // and increment are different for eden and promo.
       
   954 size_t CMSAdaptiveSizePolicy::eden_decrement_aligned_down(size_t cur_eden) {
       
   955   size_t delta = eden_decrement(cur_eden);
       
   956   return align_size_down(delta, generation_alignment());
       
   957 }
       
   958 
       
   959 size_t CMSAdaptiveSizePolicy::eden_increment_aligned_up(size_t cur_eden) {
       
   960   size_t delta = eden_increment(cur_eden);
       
   961   return align_size_up(delta, generation_alignment());
       
   962 }
       
   963 
       
   964 size_t CMSAdaptiveSizePolicy::promo_decrement_aligned_down(size_t cur_promo) {
       
   965   size_t delta = promo_decrement(cur_promo);
       
   966   return align_size_down(delta, generation_alignment());
       
   967 }
       
   968 
       
   969 size_t CMSAdaptiveSizePolicy::promo_increment_aligned_up(size_t cur_promo) {
       
   970   size_t delta = promo_increment(cur_promo);
       
   971   return align_size_up(delta, generation_alignment());
       
   972 }
       
   973 
       
   974 
       
   975 void CMSAdaptiveSizePolicy::compute_eden_space_size(size_t cur_eden,
       
   976                                                     size_t max_eden_size)
       
   977 {
       
   978   size_t desired_eden_size = cur_eden;
       
   979   size_t eden_limit = max_eden_size;
       
   980 
       
   981   // Printout input
       
   982   if (PrintGC && PrintAdaptiveSizePolicy) {
       
   983     gclog_or_tty->print_cr(
       
   984       "CMSAdaptiveSizePolicy::compute_eden_space_size: "
       
   985       "cur_eden " SIZE_FORMAT,
       
   986       cur_eden);
       
   987   }
       
   988 
       
   989   // Used for diagnostics
       
   990   clear_generation_free_space_flags();
       
   991 
       
   992   if (_avg_minor_pause->padded_average() > gc_pause_goal_sec()) {
       
   993     if (minor_pause_young_estimator()->decrement_will_decrease()) {
       
   994       // If the minor pause is too long, shrink the young gen.
       
   995       set_change_young_gen_for_min_pauses(
       
   996         decrease_young_gen_for_min_pauses_true);
       
   997       desired_eden_size = adjust_eden_for_pause_time(desired_eden_size);
       
   998     }
       
   999   } else if ((avg_remark_pause()->padded_average() > gc_pause_goal_sec()) ||
       
  1000              (avg_initial_pause()->padded_average() > gc_pause_goal_sec())) {
       
  1001     // The remark or initial pauses are not meeting the goal.  Should
       
  1002     // the generation be shrunk?
       
  1003     if (get_and_clear_first_after_collection() &&
       
  1004         ((avg_remark_pause()->padded_average() > gc_pause_goal_sec() &&
       
  1005           remark_pause_young_estimator()->decrement_will_decrease()) ||
       
  1006          (avg_initial_pause()->padded_average() > gc_pause_goal_sec() &&
       
  1007           initial_pause_young_estimator()->decrement_will_decrease()))) {
       
  1008 
       
  1009        set_change_young_gen_for_maj_pauses(
       
  1010          decrease_young_gen_for_maj_pauses_true);
       
  1011 
       
  1012       // If the remark or initial pause is too long and this is the
       
  1013       // first young gen collection after a cms collection, shrink
       
  1014       // the young gen.
       
  1015       desired_eden_size = adjust_eden_for_pause_time(desired_eden_size);
       
  1016     }
       
  1017     // If not the first young gen collection after a cms collection,
       
  1018     // don't do anything.  In this case an adjustment has already
       
  1019     // been made and the results of the adjustment has not yet been
       
  1020     // measured.
       
  1021   } else if ((minor_gc_cost() >= 0.0) &&
       
  1022              (adjusted_mutator_cost() < _throughput_goal)) {
       
  1023     desired_eden_size = adjust_eden_for_throughput(desired_eden_size);
       
  1024   } else {
       
  1025     desired_eden_size = adjust_eden_for_footprint(desired_eden_size);
       
  1026   }
       
  1027 
       
  1028   if (PrintGC && PrintAdaptiveSizePolicy) {
       
  1029     gclog_or_tty->print_cr(
       
  1030       "CMSAdaptiveSizePolicy::compute_eden_space_size limits:"
       
  1031       " desired_eden_size: " SIZE_FORMAT
       
  1032       " old_eden_size: " SIZE_FORMAT,
       
  1033       desired_eden_size, cur_eden);
       
  1034   }
       
  1035 
       
  1036   set_eden_size(desired_eden_size);
       
  1037 }
       
  1038 
       
  1039 size_t CMSAdaptiveSizePolicy::adjust_promo_for_pause_time(size_t cur_promo) {
       
  1040   size_t change = 0;
       
  1041   size_t desired_promo = cur_promo;
       
  1042   // Move this test up to caller like the adjust_eden_for_pause_time()
       
  1043   // call.
       
  1044   if ((AdaptiveSizePausePolicy == 0) &&
       
  1045       ((avg_remark_pause()->padded_average() > gc_pause_goal_sec()) ||
       
  1046       (avg_initial_pause()->padded_average() > gc_pause_goal_sec()))) {
       
  1047     set_change_old_gen_for_maj_pauses(decrease_old_gen_for_maj_pauses_true);
       
  1048     change = promo_decrement_aligned_down(cur_promo);
       
  1049     desired_promo = cur_promo - change;
       
  1050   } else if ((AdaptiveSizePausePolicy > 0) &&
       
  1051       (((avg_remark_pause()->padded_average() > gc_pause_goal_sec()) &&
       
  1052        remark_pause_old_estimator()->decrement_will_decrease()) ||
       
  1053       ((avg_initial_pause()->padded_average() > gc_pause_goal_sec()) &&
       
  1054        initial_pause_old_estimator()->decrement_will_decrease()))) {
       
  1055     set_change_old_gen_for_maj_pauses(decrease_old_gen_for_maj_pauses_true);
       
  1056     change = promo_decrement_aligned_down(cur_promo);
       
  1057     desired_promo = cur_promo - change;
       
  1058   }
       
  1059 
       
  1060   if ((change != 0) &&PrintAdaptiveSizePolicy && Verbose) {
       
  1061     gclog_or_tty->print_cr(
       
  1062       "CMSAdaptiveSizePolicy::adjust_promo_for_pause_time "
       
  1063       "adjusting promo for pause time. "
       
  1064       " starting promo size " SIZE_FORMAT
       
  1065       " reduced promo size " SIZE_FORMAT
       
  1066       " promo delta " SIZE_FORMAT,
       
  1067       cur_promo, desired_promo, change);
       
  1068   }
       
  1069 
       
  1070   return desired_promo;
       
  1071 }
       
  1072 
       
  1073 // Try to share this with PS.
       
  1074 size_t CMSAdaptiveSizePolicy::scale_by_gen_gc_cost(size_t base_change,
       
  1075                                                   double gen_gc_cost) {
       
  1076 
       
  1077   // Calculate the change to use for the tenured gen.
       
  1078   size_t scaled_change = 0;
       
  1079   // Can the increment to the generation be scaled?
       
  1080   if (gc_cost() >= 0.0 && gen_gc_cost >= 0.0) {
       
  1081     double scale_by_ratio = gen_gc_cost / gc_cost();
       
  1082     scaled_change =
       
  1083       (size_t) (scale_by_ratio * (double) base_change);
       
  1084     if (PrintAdaptiveSizePolicy && Verbose) {
       
  1085       gclog_or_tty->print_cr(
       
  1086         "Scaled tenured increment: " SIZE_FORMAT " by %f down to "
       
  1087           SIZE_FORMAT,
       
  1088         base_change, scale_by_ratio, scaled_change);
       
  1089     }
       
  1090   } else if (gen_gc_cost >= 0.0) {
       
  1091     // Scaling is not going to work.  If the major gc time is the
       
  1092     // larger than the other GC costs, give it a full increment.
       
  1093     if (gen_gc_cost >= (gc_cost() - gen_gc_cost)) {
       
  1094       scaled_change = base_change;
       
  1095     }
       
  1096   } else {
       
  1097     // Don't expect to get here but it's ok if it does
       
  1098     // in the product build since the delta will be 0
       
  1099     // and nothing will change.
       
  1100     assert(false, "Unexpected value for gc costs");
       
  1101   }
       
  1102 
       
  1103   return scaled_change;
       
  1104 }
       
  1105 
       
  1106 size_t CMSAdaptiveSizePolicy::adjust_promo_for_throughput(size_t cur_promo) {
       
  1107 
       
  1108   size_t desired_promo = cur_promo;
       
  1109 
       
  1110   set_change_old_gen_for_throughput(increase_old_gen_for_throughput_true);
       
  1111 
       
  1112   size_t change = promo_increment_aligned_up(cur_promo);
       
  1113   size_t scaled_change = scale_by_gen_gc_cost(change, major_gc_cost());
       
  1114 
       
  1115   if (cur_promo + scaled_change > cur_promo) {
       
  1116     desired_promo = cur_promo + scaled_change;
       
  1117   }
       
  1118 
       
  1119   _old_gen_change_for_major_throughput++;
       
  1120 
       
  1121   if (PrintAdaptiveSizePolicy && Verbose) {
       
  1122     gclog_or_tty->print_cr(
       
  1123       "CMSAdaptiveSizePolicy::adjust_promo_for_throughput "
       
  1124       "adjusting promo for throughput. "
       
  1125       " starting promo size " SIZE_FORMAT
       
  1126       " increased promo size " SIZE_FORMAT
       
  1127       " promo delta " SIZE_FORMAT,
       
  1128       cur_promo, desired_promo, scaled_change);
       
  1129   }
       
  1130 
       
  1131   return desired_promo;
       
  1132 }
       
  1133 
       
  1134 size_t CMSAdaptiveSizePolicy::adjust_promo_for_footprint(size_t cur_promo,
       
  1135                                                          size_t cur_eden) {
       
  1136 
       
  1137   set_decrease_for_footprint(decrease_young_gen_for_footprint_true);
       
  1138 
       
  1139   size_t change = promo_decrement(cur_promo);
       
  1140   size_t desired_promo_size = cur_promo - change;
       
  1141 
       
  1142   if (PrintAdaptiveSizePolicy && Verbose) {
       
  1143     gclog_or_tty->print_cr(
       
  1144       "CMSAdaptiveSizePolicy::adjust_promo_for_footprint "
       
  1145       "adjusting promo for footprint. "
       
  1146       " starting promo size " SIZE_FORMAT
       
  1147       " reduced promo size " SIZE_FORMAT
       
  1148       " promo delta " SIZE_FORMAT,
       
  1149       cur_promo, desired_promo_size, change);
       
  1150   }
       
  1151   return desired_promo_size;
       
  1152 }
       
  1153 
       
  1154 void CMSAdaptiveSizePolicy::compute_tenured_generation_free_space(
       
  1155                                 size_t cur_tenured_free,
       
  1156                                 size_t max_tenured_available,
       
  1157                                 size_t cur_eden) {
       
  1158   // This can be bad if the desired value grows/shrinks without
       
  1159   // any connection to the read free space
       
  1160   size_t desired_promo_size = promo_size();
       
  1161   size_t tenured_limit = max_tenured_available;
       
  1162 
       
  1163   // Printout input
       
  1164   if (PrintGC && PrintAdaptiveSizePolicy) {
       
  1165     gclog_or_tty->print_cr(
       
  1166       "CMSAdaptiveSizePolicy::compute_tenured_generation_free_space: "
       
  1167       "cur_tenured_free " SIZE_FORMAT
       
  1168       " max_tenured_available " SIZE_FORMAT,
       
  1169       cur_tenured_free, max_tenured_available);
       
  1170   }
       
  1171 
       
  1172   // Used for diagnostics
       
  1173   clear_generation_free_space_flags();
       
  1174 
       
  1175   set_decide_at_full_gc(decide_at_full_gc_true);
       
  1176   if (avg_remark_pause()->padded_average() > gc_pause_goal_sec() ||
       
  1177       avg_initial_pause()->padded_average() > gc_pause_goal_sec()) {
       
  1178     desired_promo_size = adjust_promo_for_pause_time(cur_tenured_free);
       
  1179   } else if (avg_minor_pause()->padded_average() > gc_pause_goal_sec()) {
       
  1180     // Nothing to do since the minor collections are too large and
       
  1181     // this method only deals with the cms generation.
       
  1182   } else if ((cms_gc_cost() >= 0.0) &&
       
  1183              (adjusted_mutator_cost() < _throughput_goal)) {
       
  1184     desired_promo_size = adjust_promo_for_throughput(cur_tenured_free);
       
  1185   } else {
       
  1186     desired_promo_size = adjust_promo_for_footprint(cur_tenured_free,
       
  1187                                                     cur_eden);
       
  1188   }
       
  1189 
       
  1190   if (PrintGC && PrintAdaptiveSizePolicy) {
       
  1191     gclog_or_tty->print_cr(
       
  1192       "CMSAdaptiveSizePolicy::compute_tenured_generation_free_space limits:"
       
  1193       " desired_promo_size: " SIZE_FORMAT
       
  1194       " old_promo_size: " SIZE_FORMAT,
       
  1195       desired_promo_size, cur_tenured_free);
       
  1196   }
       
  1197 
       
  1198   set_promo_size(desired_promo_size);
       
  1199 }
       
  1200 
       
  1201 uint CMSAdaptiveSizePolicy::compute_survivor_space_size_and_threshold(
       
  1202                                              bool is_survivor_overflow,
       
  1203                                              uint tenuring_threshold,
       
  1204                                              size_t survivor_limit) {
       
  1205   assert(survivor_limit >= generation_alignment(),
       
  1206          "survivor_limit too small");
       
  1207   assert((size_t)align_size_down(survivor_limit, generation_alignment())
       
  1208          == survivor_limit, "survivor_limit not aligned");
       
  1209 
       
  1210   // Change UsePSAdaptiveSurvivorSizePolicy -> UseAdaptiveSurvivorSizePolicy?
       
  1211   if (!UsePSAdaptiveSurvivorSizePolicy ||
       
  1212       !young_gen_policy_is_ready()) {
       
  1213     return tenuring_threshold;
       
  1214   }
       
  1215 
       
  1216   // We'll decide whether to increase or decrease the tenuring
       
  1217   // threshold based partly on the newly computed survivor size
       
  1218   // (if we hit the maximum limit allowed, we'll always choose to
       
  1219   // decrement the threshold).
       
  1220   bool incr_tenuring_threshold = false;
       
  1221   bool decr_tenuring_threshold = false;
       
  1222 
       
  1223   set_decrement_tenuring_threshold_for_gc_cost(false);
       
  1224   set_increment_tenuring_threshold_for_gc_cost(false);
       
  1225   set_decrement_tenuring_threshold_for_survivor_limit(false);
       
  1226 
       
  1227   if (!is_survivor_overflow) {
       
  1228     // Keep running averages on how much survived
       
  1229 
       
  1230     // We use the tenuring threshold to equalize the cost of major
       
  1231     // and minor collections.
       
  1232     // ThresholdTolerance is used to indicate how sensitive the
       
  1233     // tenuring threshold is to differences in cost between the
       
  1234     // collection types.
       
  1235 
       
  1236     // Get the times of interest. This involves a little work, so
       
  1237     // we cache the values here.
       
  1238     const double major_cost = major_gc_cost();
       
  1239     const double minor_cost = minor_gc_cost();
       
  1240 
       
  1241     if (minor_cost > major_cost * _threshold_tolerance_percent) {
       
  1242       // Minor times are getting too long;  lower the threshold so
       
  1243       // less survives and more is promoted.
       
  1244       decr_tenuring_threshold = true;
       
  1245       set_decrement_tenuring_threshold_for_gc_cost(true);
       
  1246     } else if (major_cost > minor_cost * _threshold_tolerance_percent) {
       
  1247       // Major times are too long, so we want less promotion.
       
  1248       incr_tenuring_threshold = true;
       
  1249       set_increment_tenuring_threshold_for_gc_cost(true);
       
  1250     }
       
  1251 
       
  1252   } else {
       
  1253     // Survivor space overflow occurred, so promoted and survived are
       
  1254     // not accurate. We'll make our best guess by combining survived
       
  1255     // and promoted and count them as survivors.
       
  1256     //
       
  1257     // We'll lower the tenuring threshold to see if we can correct
       
  1258     // things. Also, set the survivor size conservatively. We're
       
  1259     // trying to avoid many overflows from occurring if defnew size
       
  1260     // is just too small.
       
  1261 
       
  1262     decr_tenuring_threshold = true;
       
  1263   }
       
  1264 
       
  1265   // The padded average also maintains a deviation from the average;
       
  1266   // we use this to see how good of an estimate we have of what survived.
       
  1267   // We're trying to pad the survivor size as little as possible without
       
  1268   // overflowing the survivor spaces.
       
  1269   size_t target_size = align_size_up((size_t)_avg_survived->padded_average(),
       
  1270                                      generation_alignment());
       
  1271   target_size = MAX2(target_size, generation_alignment());
       
  1272 
       
  1273   if (target_size > survivor_limit) {
       
  1274     // Target size is bigger than we can handle. Let's also reduce
       
  1275     // the tenuring threshold.
       
  1276     target_size = survivor_limit;
       
  1277     decr_tenuring_threshold = true;
       
  1278     set_decrement_tenuring_threshold_for_survivor_limit(true);
       
  1279   }
       
  1280 
       
  1281   // Finally, increment or decrement the tenuring threshold, as decided above.
       
  1282   // We test for decrementing first, as we might have hit the target size
       
  1283   // limit.
       
  1284   if (decr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) {
       
  1285     if (tenuring_threshold > 1) {
       
  1286       tenuring_threshold--;
       
  1287     }
       
  1288   } else if (incr_tenuring_threshold && !(AlwaysTenure || NeverTenure)) {
       
  1289     if (tenuring_threshold < MaxTenuringThreshold) {
       
  1290       tenuring_threshold++;
       
  1291     }
       
  1292   }
       
  1293 
       
  1294   // We keep a running average of the amount promoted which is used
       
  1295   // to decide when we should collect the old generation (when
       
  1296   // the amount of old gen free space is less than what we expect to
       
  1297   // promote).
       
  1298 
       
  1299   if (PrintAdaptiveSizePolicy) {
       
  1300     // A little more detail if Verbose is on
       
  1301     GenCollectedHeap* gch = GenCollectedHeap::heap();
       
  1302     if (Verbose) {
       
  1303       gclog_or_tty->print( "  avg_survived: %f"
       
  1304                   "  avg_deviation: %f",
       
  1305                   _avg_survived->average(),
       
  1306                   _avg_survived->deviation());
       
  1307     }
       
  1308 
       
  1309     gclog_or_tty->print( "  avg_survived_padded_avg: %f",
       
  1310                 _avg_survived->padded_average());
       
  1311 
       
  1312     if (Verbose) {
       
  1313       gclog_or_tty->print( "  avg_promoted_avg: %f"
       
  1314                   "  avg_promoted_dev: %f",
       
  1315                   gch->gc_stats(1)->avg_promoted()->average(),
       
  1316                   gch->gc_stats(1)->avg_promoted()->deviation());
       
  1317     }
       
  1318 
       
  1319     gclog_or_tty->print( "  avg_promoted_padded_avg: %f"
       
  1320                 "  avg_pretenured_padded_avg: %f"
       
  1321                 "  tenuring_thresh: %u"
       
  1322                 "  target_size: " SIZE_FORMAT
       
  1323                 "  survivor_limit: " SIZE_FORMAT,
       
  1324                 gch->gc_stats(1)->avg_promoted()->padded_average(),
       
  1325                 _avg_pretenured->padded_average(),
       
  1326                 tenuring_threshold, target_size, survivor_limit);
       
  1327     gclog_or_tty->cr();
       
  1328   }
       
  1329 
       
  1330   set_survivor_size(target_size);
       
  1331 
       
  1332   return tenuring_threshold;
       
  1333 }
       
  1334 
       
  1335 bool CMSAdaptiveSizePolicy::get_and_clear_first_after_collection() {
       
  1336   bool result = _first_after_collection;
       
  1337   _first_after_collection = false;
       
  1338   return result;
       
  1339 }
       
  1340 
       
  1341 bool CMSAdaptiveSizePolicy::print_adaptive_size_policy_on(
       
  1342                                                     outputStream* st) const {
       
  1343 
       
  1344   if (!UseAdaptiveSizePolicy) {
       
  1345     return false;
       
  1346   }
       
  1347 
       
  1348   GenCollectedHeap* gch = GenCollectedHeap::heap();
       
  1349   Generation* young = gch->get_gen(0);
       
  1350   DefNewGeneration* def_new = young->as_DefNewGeneration();
       
  1351   return AdaptiveSizePolicy::print_adaptive_size_policy_on(
       
  1352                                          st,
       
  1353                                          def_new->tenuring_threshold());
       
  1354 }