< prev index next >

src/hotspot/share/gc/shared/adaptiveSizePolicy.cpp

Print this page
rev 52582 : 8212206: Refactor AdaptiveSizePolicy to separate out code related to GC overhead
Summary: Move check_gc_overhead_limit() and related code to its own class
Reviewed-by:
   1 /*
   2  * Copyright (c) 2004, 2016, 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/shared/adaptiveSizePolicy.hpp"
  27 #include "gc/shared/collectorPolicy.hpp"
  28 #include "gc/shared/gcCause.hpp"
  29 #include "gc/shared/gcUtil.inline.hpp"
  30 #include "gc/shared/softRefPolicy.hpp"
  31 #include "gc/shared/workgroup.hpp"
  32 #include "logging/log.hpp"
  33 #include "runtime/timer.hpp"
  34 #include "utilities/ostream.hpp"
  35 
  36 elapsedTimer AdaptiveSizePolicy::_minor_timer;
  37 elapsedTimer AdaptiveSizePolicy::_major_timer;
  38 bool AdaptiveSizePolicy::_debug_perturbation = false;
  39 
  40 // The throughput goal is implemented as
  41 //      _throughput_goal = 1 - ( 1 / (1 + gc_cost_ratio))
  42 // gc_cost_ratio is the ratio
  43 //      application cost / gc cost
  44 // For example a gc_cost_ratio of 4 translates into a
  45 // throughput goal of .80
  46 
  47 AdaptiveSizePolicy::AdaptiveSizePolicy(size_t init_eden_size,
  48                                        size_t init_promo_size,
  49                                        size_t init_survivor_size,
  50                                        double gc_pause_goal_sec,
  51                                        uint gc_cost_ratio) :
  52     _throughput_goal(1.0 - double(1.0 / (1.0 + (double) gc_cost_ratio))),
  53     _eden_size(init_eden_size),
  54     _promo_size(init_promo_size),
  55     _survivor_size(init_survivor_size),
  56     _gc_overhead_limit_exceeded(false),
  57     _print_gc_overhead_limit_would_be_exceeded(false),
  58     _gc_overhead_limit_count(0),
  59     _latest_minor_mutator_interval_seconds(0),
  60     _threshold_tolerance_percent(1.0 + ThresholdTolerance/100.0),
  61     _gc_pause_goal_sec(gc_pause_goal_sec),
  62     _young_gen_change_for_minor_throughput(0),
  63     _old_gen_change_for_major_throughput(0) {
  64   assert(AdaptiveSizePolicyGCTimeLimitThreshold > 0,
  65     "No opportunity to clear SoftReferences before GC overhead limit");
  66   _avg_minor_pause    =
  67     new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding);
  68   _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  69   _avg_minor_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  70   _avg_major_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  71 
  72   _avg_young_live     = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  73   _avg_old_live       = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  74   _avg_eden_live      = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  75 
  76   _avg_survived       = new AdaptivePaddedAverage(AdaptiveSizePolicyWeight,
  77                                                   SurvivorPadding);
  78   _avg_pretenured     = new AdaptivePaddedNoZeroDevAverage(


 387                     avg_major_interval, time_since_last_major_gc);
 388       log_trace(gc, ergo)("  major gc cost: %f  decayed major gc cost: %f",
 389                     major_gc_cost(), decayed_major_gc_cost);
 390     }
 391   }
 392   double result = MIN2(1.0, decayed_major_gc_cost + minor_gc_cost());
 393   return result;
 394 }
 395 
 396 
 397 void AdaptiveSizePolicy::clear_generation_free_space_flags() {
 398   set_change_young_gen_for_min_pauses(0);
 399   set_change_old_gen_for_maj_pauses(0);
 400 
 401   set_change_old_gen_for_throughput(0);
 402   set_change_young_gen_for_throughput(0);
 403   set_decrease_for_footprint(0);
 404   set_decide_at_full_gc(0);
 405 }
 406 
 407 void AdaptiveSizePolicy::check_gc_overhead_limit(
 408                                           size_t young_live,
 409                                           size_t eden_live,























 410                                           size_t max_old_gen_size,
 411                                           size_t max_eden_size,
 412                                           bool   is_full_gc,
 413                                           GCCause::Cause gc_cause,
 414                                           SoftRefPolicy* soft_ref_policy) {






 415 
 416   // Ignore explicit GC's.  Exiting here does not set the flag and
 417   // does not reset the count.  Updating of the averages for system
 418   // GC's is still controlled by UseAdaptiveSizePolicyWithSystemGC.
 419   if (GCCause::is_user_requested_gc(gc_cause) ||
 420       GCCause::is_serviceability_requested_gc(gc_cause)) {
 421     return;
 422   }
 423   // eden_limit is the upper limit on the size of eden based on
 424   // the maximum size of the young generation and the sizes
 425   // of the survivor space.
 426   // The question being asked is whether the gc costs are high
 427   // and the space being recovered by a collection is low.
 428   // free_in_young_gen is the free space in the young generation
 429   // after a collection and promo_live is the free space in the old
 430   // generation after a collection.
 431   //
 432   // Use the minimum of the current value of the live in the
 433   // young gen or the average of the live in the young gen.
 434   // If the current value drops quickly, that should be taken
 435   // into account (i.e., don't trigger if the amount of free
 436   // space has suddenly jumped up).  If the current is much
 437   // higher than the average, use the average since it represents
 438   // the longer term behavior.
 439   const size_t live_in_eden =
 440     MIN2(eden_live, (size_t) avg_eden_live()->average());
 441   const size_t free_in_eden = max_eden_size > live_in_eden ?
 442     max_eden_size - live_in_eden : 0;
 443   const size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average());
 444   const size_t total_free_limit = free_in_old_gen + free_in_eden;
 445   const size_t total_mem = max_old_gen_size + max_eden_size;
 446   const double mem_free_limit = total_mem * (GCHeapFreeLimit/100.0);
 447   const double mem_free_old_limit = max_old_gen_size * (GCHeapFreeLimit/100.0);
 448   const double mem_free_eden_limit = max_eden_size * (GCHeapFreeLimit/100.0);
 449   const double gc_cost_limit = GCTimeLimit/100.0;
 450   size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average());
 451   // But don't force a promo size below the current promo size. Otherwise,
 452   // the promo size will shrink for no good reason.
 453   promo_limit = MAX2(promo_limit, _promo_size);
 454 
 455 
 456   log_trace(gc, ergo)(
 457         "PSAdaptiveSizePolicy::check_gc_overhead_limit:"
 458         " promo_limit: " SIZE_FORMAT
 459         " max_eden_size: " SIZE_FORMAT
 460         " total_free_limit: " SIZE_FORMAT
 461         " max_old_gen_size: " SIZE_FORMAT
 462         " max_eden_size: " SIZE_FORMAT
 463         " mem_free_limit: " SIZE_FORMAT,
 464         promo_limit, max_eden_size, total_free_limit,
 465         max_old_gen_size, max_eden_size,
 466         (size_t) mem_free_limit);
 467 
 468   bool print_gc_overhead_limit_would_be_exceeded = false;
 469   if (is_full_gc) {
 470     if (gc_cost() > gc_cost_limit &&
 471       free_in_old_gen < (size_t) mem_free_old_limit &&
 472       free_in_eden < (size_t) mem_free_eden_limit) {
 473       // Collections, on average, are taking too much time, and
 474       //      gc_cost() > gc_cost_limit
 475       // we have too little space available after a full gc.
 476       //      total_free_limit < mem_free_limit
 477       // where
 478       //   total_free_limit is the free space available in
 479       //     both generations
 480       //   total_mem is the total space available for allocation
 481       //     in both generations (survivor spaces are not included
 482       //     just as they are not included in eden_limit).
 483       //   mem_free_limit is a fraction of total_mem judged to be an
 484       //     acceptable amount that is still unused.
 485       // The heap can ask for the value of this variable when deciding
 486       // whether to thrown an OutOfMemory error.
 487       // Note that the gc time limit test only works for the collections
 488       // of the young gen + tenured gen and not for collections of the
 489       // permanent gen.  That is because the calculation of the space
 490       // freed by the collection is the free space in the young gen +
 491       // tenured gen.
 492       // At this point the GC overhead limit is being exceeded.
 493       inc_gc_overhead_limit_count();
 494       if (UseGCOverheadLimit) {
 495         if (gc_overhead_limit_count() >=
 496             AdaptiveSizePolicyGCTimeLimitThreshold){
 497           // All conditions have been met for throwing an out-of-memory
 498           set_gc_overhead_limit_exceeded(true);
 499           // Avoid consecutive OOM due to the gc time limit by resetting
 500           // the counter.
 501           reset_gc_overhead_limit_count();
 502         } else {
 503           // The required consecutive collections which exceed the
 504           // GC time limit may or may not have been reached. We
 505           // are approaching that condition and so as not to
 506           // throw an out-of-memory before all SoftRef's have been
 507           // cleared, set _should_clear_all_soft_refs in CollectorPolicy.
 508           // The clearing will be done on the next GC.
 509           bool near_limit = gc_overhead_limit_near();
 510           if (near_limit) {
 511             soft_ref_policy->set_should_clear_all_soft_refs(true);
 512             log_trace(gc, ergo)("Nearing GC overhead limit, will be clearing all SoftReference");
 513           }
 514         }
 515       }
 516       // Set this even when the overhead limit will not
 517       // cause an out-of-memory.  Diagnostic message indicating
 518       // that the overhead limit is being exceeded is sometimes
 519       // printed.
 520       print_gc_overhead_limit_would_be_exceeded = true;
 521 
 522     } else {
 523       // Did not exceed overhead limits
 524       reset_gc_overhead_limit_count();
 525     }
 526   }
 527 
 528   if (UseGCOverheadLimit) {
 529     if (gc_overhead_limit_exceeded()) {
 530       log_trace(gc, ergo)("GC is exceeding overhead limit of " UINTX_FORMAT "%%", GCTimeLimit);
 531       reset_gc_overhead_limit_count();
 532     } else if (print_gc_overhead_limit_would_be_exceeded) {
 533       assert(gc_overhead_limit_count() > 0, "Should not be printing");
 534       log_trace(gc, ergo)("GC would exceed overhead limit of " UINTX_FORMAT "%% %d consecutive time(s)",
 535                           GCTimeLimit, gc_overhead_limit_count());
 536     }
 537   }












 538 }
 539 // Printing
 540 
 541 bool AdaptiveSizePolicy::print() const {
 542   assert(UseAdaptiveSizePolicy, "UseAdaptiveSizePolicy need to be enabled.");
 543 
 544   if (!log_is_enabled(Debug, gc, ergo)) {
 545     return false;
 546   }
 547 
 548   // Print goal for which action is needed.
 549   char* action = NULL;
 550   bool change_for_pause = false;
 551   if ((change_old_gen_for_maj_pauses() ==
 552          decrease_old_gen_for_maj_pauses_true) ||
 553       (change_young_gen_for_min_pauses() ==
 554          decrease_young_gen_for_min_pauses_true)) {
 555     action = (char*) " *** pause time goal ***";
 556     change_for_pause = true;
 557   } else if ((change_old_gen_for_throughput() ==


   1 /*
   2  * Copyright (c) 2004, 2018, 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/shared/adaptiveSizePolicy.hpp"
  27 #include "gc/shared/collectorPolicy.hpp"
  28 #include "gc/shared/gcCause.hpp"
  29 #include "gc/shared/gcUtil.inline.hpp"

  30 #include "gc/shared/workgroup.hpp"
  31 #include "logging/log.hpp"
  32 #include "runtime/timer.hpp"
  33 #include "utilities/ostream.hpp"
  34 
  35 elapsedTimer AdaptiveSizePolicy::_minor_timer;
  36 elapsedTimer AdaptiveSizePolicy::_major_timer;
  37 bool AdaptiveSizePolicy::_debug_perturbation = false;
  38 
  39 // The throughput goal is implemented as
  40 //      _throughput_goal = 1 - ( 1 / (1 + gc_cost_ratio))
  41 // gc_cost_ratio is the ratio
  42 //      application cost / gc cost
  43 // For example a gc_cost_ratio of 4 translates into a
  44 // throughput goal of .80
  45 
  46 AdaptiveSizePolicy::AdaptiveSizePolicy(size_t init_eden_size,
  47                                        size_t init_promo_size,
  48                                        size_t init_survivor_size,
  49                                        double gc_pause_goal_sec,
  50                                        uint gc_cost_ratio) :
  51     _throughput_goal(1.0 - double(1.0 / (1.0 + (double) gc_cost_ratio))),
  52     _eden_size(init_eden_size),
  53     _promo_size(init_promo_size),
  54     _survivor_size(init_survivor_size),



  55     _latest_minor_mutator_interval_seconds(0),
  56     _threshold_tolerance_percent(1.0 + ThresholdTolerance/100.0),
  57     _gc_pause_goal_sec(gc_pause_goal_sec),
  58     _young_gen_change_for_minor_throughput(0),
  59     _old_gen_change_for_major_throughput(0) {
  60   assert(AdaptiveSizePolicyGCTimeLimitThreshold > 0,
  61     "No opportunity to clear SoftReferences before GC overhead limit");
  62   _avg_minor_pause    =
  63     new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding);
  64   _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  65   _avg_minor_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  66   _avg_major_gc_cost  = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
  67 
  68   _avg_young_live     = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  69   _avg_old_live       = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  70   _avg_eden_live      = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
  71 
  72   _avg_survived       = new AdaptivePaddedAverage(AdaptiveSizePolicyWeight,
  73                                                   SurvivorPadding);
  74   _avg_pretenured     = new AdaptivePaddedNoZeroDevAverage(


 383                     avg_major_interval, time_since_last_major_gc);
 384       log_trace(gc, ergo)("  major gc cost: %f  decayed major gc cost: %f",
 385                     major_gc_cost(), decayed_major_gc_cost);
 386     }
 387   }
 388   double result = MIN2(1.0, decayed_major_gc_cost + minor_gc_cost());
 389   return result;
 390 }
 391 
 392 
 393 void AdaptiveSizePolicy::clear_generation_free_space_flags() {
 394   set_change_young_gen_for_min_pauses(0);
 395   set_change_old_gen_for_maj_pauses(0);
 396 
 397   set_change_old_gen_for_throughput(0);
 398   set_change_young_gen_for_throughput(0);
 399   set_decrease_for_footprint(0);
 400   set_decide_at_full_gc(0);
 401 }
 402 
 403 class AdaptiveSizePolicyTimeOverheadTester: public OverheadTester {
 404   double _gc_cost;
 405 
 406  public:
 407   AdaptiveSizePolicyTimeOverheadTester(double gc_cost) : _gc_cost(gc_cost) {}
 408 
 409   bool is_exceeded() {
 410     // Note that the gc time limit test only works for the collections
 411     // of the young gen + tenured gen and not for collections of the
 412     // permanent gen.  That is because the calculation of the space
 413     // freed by the collection is the free space in the young gen +
 414     // tenured gen.
 415     return _gc_cost > (GCTimeLimit / 100.0);
 416   }
 417 };
 418 
 419 class AdaptiveSizePolicySpaceOverheadTester: public OverheadTester {
 420   size_t _eden_live;
 421   size_t _max_old_gen_size;
 422   size_t _max_eden_size;
 423   size_t _promo_size;
 424   double _avg_eden_live;
 425   double _avg_old_live;
 426 
 427  public:
 428   AdaptiveSizePolicySpaceOverheadTester(size_t eden_live,
 429                                         size_t max_old_gen_size,
 430                                         size_t max_eden_size,
 431                                         size_t promo_size,
 432                                         double avg_eden_live,
 433                                         double avg_old_live) :
 434     _eden_live(eden_live),
 435     _max_old_gen_size(max_old_gen_size),
 436     _max_eden_size(max_eden_size),
 437     _promo_size(promo_size),
 438     _avg_eden_live(avg_eden_live),
 439     _avg_old_live(avg_old_live) {}
 440 
 441   bool is_exceeded() {
 442     // _max_eden_size is the upper limit on the size of eden based on






 443     // the maximum size of the young generation and the sizes
 444     // of the survivor space.
 445     // The question being asked is whether the space being recovered by
 446     // a collection is low.
 447     // free_in_eden is the free space in eden after a collection and
 448     // free_in_old_gen is the free space in the old generation after
 449     // a collection.
 450     //
 451     // Use the minimum of the current value of the live in eden
 452     // or the average of the live in eden.
 453     // If the current value drops quickly, that should be taken
 454     // into account (i.e., don't trigger if the amount of free
 455     // space has suddenly jumped up).  If the current is much
 456     // higher than the average, use the average since it represents
 457     // the longer term behavior.
 458     const size_t live_in_eden =
 459       MIN2(_eden_live, (size_t)_avg_eden_live);
 460     const size_t free_in_eden = _max_eden_size > live_in_eden ?
 461       _max_eden_size - live_in_eden : 0;
 462     const size_t free_in_old_gen = (size_t)(_max_old_gen_size - _avg_old_live);
 463     const size_t total_free_limit = free_in_old_gen + free_in_eden;
 464     const size_t total_mem = _max_old_gen_size + _max_eden_size;
 465     const double free_limit_ratio = GCHeapFreeLimit / 100.0;
 466     const double mem_free_limit = total_mem * free_limit_ratio;
 467     const double mem_free_old_limit = _max_old_gen_size * free_limit_ratio;
 468     const double mem_free_eden_limit = _max_eden_size * free_limit_ratio;
 469     size_t promo_limit = (size_t)(_max_old_gen_size - _avg_old_live);
 470     // But don't force a promo size below the current promo size. Otherwise,
 471     // the promo size will shrink for no good reason.
 472     promo_limit = MAX2(promo_limit, _promo_size);
 473 

 474     log_trace(gc, ergo)(
 475           "AdaptiveSizePolicySpaceOverheadTester::is_exceeded:"
 476           " promo_limit: " SIZE_FORMAT
 477           " max_eden_size: " SIZE_FORMAT
 478           " total_free_limit: " SIZE_FORMAT
 479           " max_old_gen_size: " SIZE_FORMAT
 480           " max_eden_size: " SIZE_FORMAT
 481           " mem_free_limit: " SIZE_FORMAT,
 482           promo_limit, _max_eden_size, total_free_limit,
 483           _max_old_gen_size, _max_eden_size,
 484           (size_t)mem_free_limit);






















































 485 
 486     return free_in_old_gen < (size_t)mem_free_old_limit &&
 487            free_in_eden < (size_t)mem_free_eden_limit;


 488   }
 489 
 490 };
 491 
 492 void AdaptiveSizePolicy::check_gc_overhead_limit(
 493                                           size_t eden_live,
 494                                           size_t max_old_gen_size,
 495                                           size_t max_eden_size,
 496                                           bool   is_full_gc,
 497                                           GCCause::Cause gc_cause,
 498                                           SoftRefPolicy* soft_ref_policy) {
 499 
 500   AdaptiveSizePolicyTimeOverheadTester time_overhead(gc_cost());
 501   AdaptiveSizePolicySpaceOverheadTester space_overhead(eden_live,
 502                                                        max_old_gen_size,
 503                                                        max_eden_size,
 504                                                        _promo_size,
 505                                                        avg_eden_live()->average(),
 506                                                        avg_old_live()->average());
 507   _overhead_checker.check_gc_overhead_limit(&time_overhead,
 508                                             &space_overhead,
 509                                             is_full_gc,
 510                                             gc_cause,
 511                                             soft_ref_policy);
 512 }
 513 // Printing
 514 
 515 bool AdaptiveSizePolicy::print() const {
 516   assert(UseAdaptiveSizePolicy, "UseAdaptiveSizePolicy need to be enabled.");
 517 
 518   if (!log_is_enabled(Debug, gc, ergo)) {
 519     return false;
 520   }
 521 
 522   // Print goal for which action is needed.
 523   char* action = NULL;
 524   bool change_for_pause = false;
 525   if ((change_old_gen_for_maj_pauses() ==
 526          decrease_old_gen_for_maj_pauses_true) ||
 527       (change_young_gen_for_min_pauses() ==
 528          decrease_young_gen_for_min_pauses_true)) {
 529     action = (char*) " *** pause time goal ***";
 530     change_for_pause = true;
 531   } else if ((change_old_gen_for_throughput() ==


< prev index next >