hotspot/src/share/vm/gc_implementation/shared/adaptiveSizePolicy.cpp
changeset 1 489c9b5090e2
child 5343 95a5c4b89273
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 2004-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  *
       
    23  */
       
    24 #include "incls/_precompiled.incl"
       
    25 #include "incls/_adaptiveSizePolicy.cpp.incl"
       
    26 
       
    27 elapsedTimer AdaptiveSizePolicy::_minor_timer;
       
    28 elapsedTimer AdaptiveSizePolicy::_major_timer;
       
    29 
       
    30 // The throughput goal is implemented as
       
    31 //      _throughput_goal = 1 - ( 1 / (1 + gc_cost_ratio))
       
    32 // gc_cost_ratio is the ratio
       
    33 //      application cost / gc cost
       
    34 // For example a gc_cost_ratio of 4 translates into a
       
    35 // throughput goal of .80
       
    36 
       
    37 AdaptiveSizePolicy::AdaptiveSizePolicy(size_t init_eden_size,
       
    38                                        size_t init_promo_size,
       
    39                                        size_t init_survivor_size,
       
    40                                        double gc_pause_goal_sec,
       
    41                                        uint gc_cost_ratio) :
       
    42     _eden_size(init_eden_size),
       
    43     _promo_size(init_promo_size),
       
    44     _survivor_size(init_survivor_size),
       
    45     _gc_pause_goal_sec(gc_pause_goal_sec),
       
    46     _throughput_goal(1.0 - double(1.0 / (1.0 + (double) gc_cost_ratio))),
       
    47     _gc_time_limit_exceeded(false),
       
    48     _print_gc_time_limit_would_be_exceeded(false),
       
    49     _gc_time_limit_count(0),
       
    50     _latest_minor_mutator_interval_seconds(0),
       
    51     _threshold_tolerance_percent(1.0 + ThresholdTolerance/100.0),
       
    52     _young_gen_change_for_minor_throughput(0),
       
    53     _old_gen_change_for_major_throughput(0) {
       
    54   _avg_minor_pause    =
       
    55     new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding);
       
    56   _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    57   _avg_minor_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    58   _avg_major_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
       
    59 
       
    60   _avg_young_live     = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
       
    61   _avg_old_live       = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
       
    62   _avg_eden_live      = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
       
    63 
       
    64   _avg_survived       = new AdaptivePaddedAverage(AdaptiveSizePolicyWeight,
       
    65                                                   SurvivorPadding);
       
    66   _avg_pretenured     = new AdaptivePaddedNoZeroDevAverage(
       
    67                                                   AdaptiveSizePolicyWeight,
       
    68                                                   SurvivorPadding);
       
    69 
       
    70   _minor_pause_old_estimator =
       
    71     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
    72   _minor_pause_young_estimator =
       
    73     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
    74   _minor_collection_estimator =
       
    75     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
    76   _major_collection_estimator =
       
    77     new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
       
    78 
       
    79   // Start the timers
       
    80   _minor_timer.start();
       
    81 
       
    82   _young_gen_policy_is_ready = false;
       
    83 }
       
    84 
       
    85 bool AdaptiveSizePolicy::tenuring_threshold_change() const {
       
    86   return decrement_tenuring_threshold_for_gc_cost() ||
       
    87          increment_tenuring_threshold_for_gc_cost() ||
       
    88          decrement_tenuring_threshold_for_survivor_limit();
       
    89 }
       
    90 
       
    91 void AdaptiveSizePolicy::minor_collection_begin() {
       
    92   // Update the interval time
       
    93   _minor_timer.stop();
       
    94   // Save most recent collection time
       
    95   _latest_minor_mutator_interval_seconds = _minor_timer.seconds();
       
    96   _minor_timer.reset();
       
    97   _minor_timer.start();
       
    98 }
       
    99 
       
   100 void AdaptiveSizePolicy::update_minor_pause_young_estimator(
       
   101     double minor_pause_in_ms) {
       
   102   double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
       
   103   _minor_pause_young_estimator->update(eden_size_in_mbytes,
       
   104     minor_pause_in_ms);
       
   105 }
       
   106 
       
   107 void AdaptiveSizePolicy::minor_collection_end(GCCause::Cause gc_cause) {
       
   108   // Update the pause time.
       
   109   _minor_timer.stop();
       
   110 
       
   111   if (gc_cause != GCCause::_java_lang_system_gc ||
       
   112       UseAdaptiveSizePolicyWithSystemGC) {
       
   113     double minor_pause_in_seconds = _minor_timer.seconds();
       
   114     double minor_pause_in_ms = minor_pause_in_seconds * MILLIUNITS;
       
   115 
       
   116     // Sample for performance counter
       
   117     _avg_minor_pause->sample(minor_pause_in_seconds);
       
   118 
       
   119     // Cost of collection (unit-less)
       
   120     double collection_cost = 0.0;
       
   121     if ((_latest_minor_mutator_interval_seconds > 0.0) &&
       
   122         (minor_pause_in_seconds > 0.0)) {
       
   123       double interval_in_seconds =
       
   124         _latest_minor_mutator_interval_seconds + minor_pause_in_seconds;
       
   125       collection_cost =
       
   126         minor_pause_in_seconds / interval_in_seconds;
       
   127       _avg_minor_gc_cost->sample(collection_cost);
       
   128       // Sample for performance counter
       
   129       _avg_minor_interval->sample(interval_in_seconds);
       
   130     }
       
   131 
       
   132     // The policy does not have enough data until at least some
       
   133     // minor collections have been done.
       
   134     _young_gen_policy_is_ready =
       
   135       (_avg_minor_gc_cost->count() >= AdaptiveSizePolicyReadyThreshold);
       
   136 
       
   137     // Calculate variables used to estimate pause time vs. gen sizes
       
   138     double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
       
   139     update_minor_pause_young_estimator(minor_pause_in_ms);
       
   140     update_minor_pause_old_estimator(minor_pause_in_ms);
       
   141 
       
   142     if (PrintAdaptiveSizePolicy && Verbose) {
       
   143       gclog_or_tty->print("AdaptiveSizePolicy::minor_collection_end: "
       
   144         "minor gc cost: %f  average: %f", collection_cost,
       
   145         _avg_minor_gc_cost->average());
       
   146       gclog_or_tty->print_cr("  minor pause: %f minor period %f",
       
   147         minor_pause_in_ms,
       
   148         _latest_minor_mutator_interval_seconds * MILLIUNITS);
       
   149     }
       
   150 
       
   151     // Calculate variable used to estimate collection cost vs. gen sizes
       
   152     assert(collection_cost >= 0.0, "Expected to be non-negative");
       
   153     _minor_collection_estimator->update(eden_size_in_mbytes, collection_cost);
       
   154   }
       
   155 
       
   156   // Interval times use this timer to measure the mutator time.
       
   157   // Reset the timer after the GC pause.
       
   158   _minor_timer.reset();
       
   159   _minor_timer.start();
       
   160 }
       
   161 
       
   162 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden,
       
   163                                             uint percent_change) {
       
   164   size_t eden_heap_delta;
       
   165   eden_heap_delta = cur_eden / 100 * percent_change;
       
   166   return eden_heap_delta;
       
   167 }
       
   168 
       
   169 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden) {
       
   170   return eden_increment(cur_eden, YoungGenerationSizeIncrement);
       
   171 }
       
   172 
       
   173 size_t AdaptiveSizePolicy::eden_decrement(size_t cur_eden) {
       
   174   size_t eden_heap_delta = eden_increment(cur_eden) /
       
   175     AdaptiveSizeDecrementScaleFactor;
       
   176   return eden_heap_delta;
       
   177 }
       
   178 
       
   179 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo,
       
   180                                              uint percent_change) {
       
   181   size_t promo_heap_delta;
       
   182   promo_heap_delta = cur_promo / 100 * percent_change;
       
   183   return promo_heap_delta;
       
   184 }
       
   185 
       
   186 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo) {
       
   187   return promo_increment(cur_promo, TenuredGenerationSizeIncrement);
       
   188 }
       
   189 
       
   190 size_t AdaptiveSizePolicy::promo_decrement(size_t cur_promo) {
       
   191   size_t promo_heap_delta = promo_increment(cur_promo);
       
   192   promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor;
       
   193   return promo_heap_delta;
       
   194 }
       
   195 
       
   196 double AdaptiveSizePolicy::time_since_major_gc() const {
       
   197   _major_timer.stop();
       
   198   double result = _major_timer.seconds();
       
   199   _major_timer.start();
       
   200   return result;
       
   201 }
       
   202 
       
   203 // Linear decay of major gc cost
       
   204 double AdaptiveSizePolicy::decaying_major_gc_cost() const {
       
   205   double major_interval = major_gc_interval_average_for_decay();
       
   206   double major_gc_cost_average = major_gc_cost();
       
   207   double decayed_major_gc_cost = major_gc_cost_average;
       
   208   if(time_since_major_gc() > 0.0) {
       
   209     decayed_major_gc_cost = major_gc_cost() *
       
   210       (((double) AdaptiveSizeMajorGCDecayTimeScale) * major_interval)
       
   211       / time_since_major_gc();
       
   212   }
       
   213 
       
   214   // The decayed cost should always be smaller than the
       
   215   // average cost but the vagaries of finite arithmetic could
       
   216   // produce a larger value in decayed_major_gc_cost so protect
       
   217   // against that.
       
   218   return MIN2(major_gc_cost_average, decayed_major_gc_cost);
       
   219 }
       
   220 
       
   221 // Use a value of the major gc cost that has been decayed
       
   222 // by the factor
       
   223 //
       
   224 //      average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale /
       
   225 //        time-since-last-major-gc
       
   226 //
       
   227 // if the average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale
       
   228 // is less than time-since-last-major-gc.
       
   229 //
       
   230 // In cases where there are initial major gc's that
       
   231 // are of a relatively high cost but no later major
       
   232 // gc's, the total gc cost can remain high because
       
   233 // the major gc cost remains unchanged (since there are no major
       
   234 // gc's).  In such a situation the value of the unchanging
       
   235 // major gc cost can keep the mutator throughput below
       
   236 // the goal when in fact the major gc cost is becoming diminishingly
       
   237 // small.  Use the decaying gc cost only to decide whether to
       
   238 // adjust for throughput.  Using it also to determine the adjustment
       
   239 // to be made for throughput also seems reasonable but there is
       
   240 // no test case to use to decide if it is the right thing to do
       
   241 // don't do it yet.
       
   242 
       
   243 double AdaptiveSizePolicy::decaying_gc_cost() const {
       
   244   double decayed_major_gc_cost = major_gc_cost();
       
   245   double avg_major_interval = major_gc_interval_average_for_decay();
       
   246   if (UseAdaptiveSizeDecayMajorGCCost &&
       
   247       (AdaptiveSizeMajorGCDecayTimeScale > 0) &&
       
   248       (avg_major_interval > 0.00)) {
       
   249     double time_since_last_major_gc = time_since_major_gc();
       
   250 
       
   251     // Decay the major gc cost?
       
   252     if (time_since_last_major_gc >
       
   253         ((double) AdaptiveSizeMajorGCDecayTimeScale) * avg_major_interval) {
       
   254 
       
   255       // Decay using the time-since-last-major-gc
       
   256       decayed_major_gc_cost = decaying_major_gc_cost();
       
   257       if (PrintGCDetails && Verbose) {
       
   258         gclog_or_tty->print_cr("\ndecaying_gc_cost: major interval average:"
       
   259           " %f  time since last major gc: %f",
       
   260           avg_major_interval, time_since_last_major_gc);
       
   261         gclog_or_tty->print_cr("  major gc cost: %f  decayed major gc cost: %f",
       
   262           major_gc_cost(), decayed_major_gc_cost);
       
   263       }
       
   264     }
       
   265   }
       
   266   double result = MIN2(1.0, decayed_major_gc_cost + minor_gc_cost());
       
   267   return result;
       
   268 }
       
   269 
       
   270 
       
   271 void AdaptiveSizePolicy::clear_generation_free_space_flags() {
       
   272   set_change_young_gen_for_min_pauses(0);
       
   273   set_change_old_gen_for_maj_pauses(0);
       
   274 
       
   275   set_change_old_gen_for_throughput(0);
       
   276   set_change_young_gen_for_throughput(0);
       
   277   set_decrease_for_footprint(0);
       
   278   set_decide_at_full_gc(0);
       
   279 }
       
   280 
       
   281 // Printing
       
   282 
       
   283 bool AdaptiveSizePolicy::print_adaptive_size_policy_on(outputStream* st) const {
       
   284 
       
   285   //  Should only be used with adaptive size policy turned on.
       
   286   // Otherwise, there may be variables that are undefined.
       
   287   if (!UseAdaptiveSizePolicy) return false;
       
   288 
       
   289   // Print goal for which action is needed.
       
   290   char* action = NULL;
       
   291   bool change_for_pause = false;
       
   292   if ((change_old_gen_for_maj_pauses() ==
       
   293          decrease_old_gen_for_maj_pauses_true) ||
       
   294       (change_young_gen_for_min_pauses() ==
       
   295          decrease_young_gen_for_min_pauses_true)) {
       
   296     action = (char*) " *** pause time goal ***";
       
   297     change_for_pause = true;
       
   298   } else if ((change_old_gen_for_throughput() ==
       
   299                increase_old_gen_for_throughput_true) ||
       
   300             (change_young_gen_for_throughput() ==
       
   301                increase_young_gen_for_througput_true)) {
       
   302     action = (char*) " *** throughput goal ***";
       
   303   } else if (decrease_for_footprint()) {
       
   304     action = (char*) " *** reduced footprint ***";
       
   305   } else {
       
   306     // No actions were taken.  This can legitimately be the
       
   307     // situation if not enough data has been gathered to make
       
   308     // decisions.
       
   309     return false;
       
   310   }
       
   311 
       
   312   // Pauses
       
   313   // Currently the size of the old gen is only adjusted to
       
   314   // change the major pause times.
       
   315   char* young_gen_action = NULL;
       
   316   char* tenured_gen_action = NULL;
       
   317 
       
   318   char* shrink_msg = (char*) "(attempted to shrink)";
       
   319   char* grow_msg = (char*) "(attempted to grow)";
       
   320   char* no_change_msg = (char*) "(no change)";
       
   321   if (change_young_gen_for_min_pauses() ==
       
   322       decrease_young_gen_for_min_pauses_true) {
       
   323     young_gen_action = shrink_msg;
       
   324   } else if (change_for_pause) {
       
   325     young_gen_action = no_change_msg;
       
   326   }
       
   327 
       
   328   if (change_old_gen_for_maj_pauses() == decrease_old_gen_for_maj_pauses_true) {
       
   329     tenured_gen_action = shrink_msg;
       
   330   } else if (change_for_pause) {
       
   331     tenured_gen_action = no_change_msg;
       
   332   }
       
   333 
       
   334   // Throughput
       
   335   if (change_old_gen_for_throughput() == increase_old_gen_for_throughput_true) {
       
   336     assert(change_young_gen_for_throughput() ==
       
   337            increase_young_gen_for_througput_true,
       
   338            "Both generations should be growing");
       
   339     young_gen_action = grow_msg;
       
   340     tenured_gen_action = grow_msg;
       
   341   } else if (change_young_gen_for_throughput() ==
       
   342              increase_young_gen_for_througput_true) {
       
   343     // Only the young generation may grow at start up (before
       
   344     // enough full collections have been done to grow the old generation).
       
   345     young_gen_action = grow_msg;
       
   346     tenured_gen_action = no_change_msg;
       
   347   }
       
   348 
       
   349   // Minimum footprint
       
   350   if (decrease_for_footprint() != 0) {
       
   351     young_gen_action = shrink_msg;
       
   352     tenured_gen_action = shrink_msg;
       
   353   }
       
   354 
       
   355   st->print_cr("    UseAdaptiveSizePolicy actions to meet %s", action);
       
   356   st->print_cr("                       GC overhead (%%)");
       
   357   st->print_cr("    Young generation:     %7.2f\t  %s",
       
   358     100.0 * avg_minor_gc_cost()->average(),
       
   359     young_gen_action);
       
   360   st->print_cr("    Tenured generation:   %7.2f\t  %s",
       
   361     100.0 * avg_major_gc_cost()->average(),
       
   362     tenured_gen_action);
       
   363   return true;
       
   364 }
       
   365 
       
   366 bool AdaptiveSizePolicy::print_adaptive_size_policy_on(
       
   367                                             outputStream* st,
       
   368                                             int tenuring_threshold_arg) const {
       
   369   if (!AdaptiveSizePolicy::print_adaptive_size_policy_on(st)) {
       
   370     return false;
       
   371   }
       
   372 
       
   373   // Tenuring threshold
       
   374   bool tenuring_threshold_changed = true;
       
   375   if (decrement_tenuring_threshold_for_survivor_limit()) {
       
   376     st->print("    Tenuring threshold:    (attempted to decrease to avoid"
       
   377               " survivor space overflow) = ");
       
   378   } else if (decrement_tenuring_threshold_for_gc_cost()) {
       
   379     st->print("    Tenuring threshold:    (attempted to decrease to balance"
       
   380               " GC costs) = ");
       
   381   } else if (increment_tenuring_threshold_for_gc_cost()) {
       
   382     st->print("    Tenuring threshold:    (attempted to increase to balance"
       
   383               " GC costs) = ");
       
   384   } else {
       
   385     tenuring_threshold_changed = false;
       
   386     assert(!tenuring_threshold_change(), "(no change was attempted)");
       
   387   }
       
   388   if (tenuring_threshold_changed) {
       
   389     st->print_cr("%d", tenuring_threshold_arg);
       
   390   }
       
   391   return true;
       
   392 }