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