1 /*
   2  * Copyright (c) 2002, 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 // This class keeps statistical information and computes the
  26 // optimal free space for both the young and old generation
  27 // based on current application characteristics (based on gc cost
  28 // and application footprint).
  29 //
  30 // It also computes an optimal tenuring threshold between the young
  31 // and old generations, so as to equalize the cost of collections
  32 // of those generations, as well as optimial survivor space sizes
  33 // for the young generation.
  34 //
  35 // While this class is specifically intended for a generational system
  36 // consisting of a young gen (containing an Eden and two semi-spaces)
  37 // and a tenured gen, as well as a perm gen for reflective data, it
  38 // makes NO references to specific generations.
  39 //
  40 // 05/02/2003 Update
  41 // The 1.5 policy makes use of data gathered for the costs of GC on
  42 // specific generations.  That data does reference specific
  43 // generation.  Also diagnostics specific to generations have
  44 // been added.
  45 
  46 // Forward decls
  47 class elapsedTimer;
  48 class GenerationSizer;
  49 
  50 class PSAdaptiveSizePolicy : public AdaptiveSizePolicy {
  51  friend class PSGCAdaptivePolicyCounters;
  52  private:
  53   // These values are used to record decisions made during the
  54   // policy.  For example, if the young generation was decreased
  55   // to decrease the GC cost of minor collections the value
  56   // decrease_young_gen_for_throughput_true is used.
  57 
  58   // Last calculated sizes, in bytes, and aligned
  59   // NEEDS_CLEANUP should use sizes.hpp,  but it works in ints, not size_t's
  60 
  61   // Time statistics
  62   AdaptivePaddedAverage* _avg_major_pause;
  63 
  64   // Footprint statistics
  65   AdaptiveWeightedAverage* _avg_base_footprint;
  66 
  67   // Statistical data gathered for GC
  68   GCStats _gc_stats;
  69 
  70   size_t _survivor_size_limit;   // Limit in bytes of survivor size
  71   const double _collection_cost_margin_fraction;
  72 
  73   // Variable for estimating the major and minor pause times.
  74   // These variables represent linear least-squares fits of
  75   // the data.
  76   //   major pause time vs. old gen size
  77   LinearLeastSquareFit* _major_pause_old_estimator;
  78   //   major pause time vs. young gen size
  79   LinearLeastSquareFit* _major_pause_young_estimator;
  80 
  81 
  82   // These record the most recent collection times.  They
  83   // are available as an alternative to using the averages
  84   // for making ergonomic decisions.
  85   double _latest_major_mutator_interval_seconds;
  86 
  87   const size_t _intra_generation_alignment; // alignment for eden, survivors
  88 
  89   const double _gc_minor_pause_goal_sec;    // goal for maximum minor gc pause
  90 
  91   // The amount of live data in the heap at the last full GC, used
  92   // as a baseline to help us determine when we need to perform the
  93   // next full GC.
  94   size_t _live_at_last_full_gc;
  95 
  96   // decrease/increase the old generation for minor pause time
  97   int _change_old_gen_for_min_pauses;
  98 
  99   // increase/decrease the young generation for major pause time
 100   int _change_young_gen_for_maj_pauses;
 101 
 102 
 103   // Flag indicating that the adaptive policy is ready to use
 104   bool _old_gen_policy_is_ready;
 105 
 106   // Changing the generation sizing depends on the data that is
 107   // gathered about the effects of changes on the pause times and
 108   // throughput.  These variable count the number of data points
 109   // gathered.  The policy may use these counters as a threshhold
 110   // for reliable data.
 111   julong _young_gen_change_for_major_pause_count;
 112 
 113   // To facilitate faster growth at start up, supplement the normal
 114   // growth percentage for the young gen eden and the
 115   // old gen space for promotion with these value which decay
 116   // with increasing collections.
 117   uint _young_gen_size_increment_supplement;
 118   uint _old_gen_size_increment_supplement;
 119 
 120   // The number of bytes absorbed from eden into the old gen by moving the
 121   // boundary over live data.
 122   size_t _bytes_absorbed_from_eden;
 123 
 124  private:
 125 
 126   // Accessors
 127   AdaptivePaddedAverage* avg_major_pause() const { return _avg_major_pause; }
 128   double gc_minor_pause_goal_sec() const { return _gc_minor_pause_goal_sec; }
 129 
 130   // Change the young generation size to achieve a minor GC pause time goal
 131   void adjust_for_minor_pause_time(bool is_full_gc,
 132                                    size_t* desired_promo_size_ptr,
 133                                    size_t* desired_eden_size_ptr);
 134   // Change the generation sizes to achieve a GC pause time goal
 135   // Returned sizes are not necessarily aligned.
 136   void adjust_for_pause_time(bool is_full_gc,
 137                          size_t* desired_promo_size_ptr,
 138                          size_t* desired_eden_size_ptr);
 139   // Change the generation sizes to achieve an application throughput goal
 140   // Returned sizes are not necessarily aligned.
 141   void adjust_for_throughput(bool is_full_gc,
 142                              size_t* desired_promo_size_ptr,
 143                              size_t* desired_eden_size_ptr);
 144   // Change the generation sizes to achieve minimum footprint
 145   // Returned sizes are not aligned.
 146   size_t adjust_promo_for_footprint(size_t desired_promo_size,
 147                                     size_t desired_total);
 148   size_t adjust_eden_for_footprint(size_t desired_promo_size,
 149                                    size_t desired_total);
 150 
 151   // Size in bytes for an increment or decrement of eden.
 152   virtual size_t eden_increment(size_t cur_eden, uint percent_change);
 153   virtual size_t eden_decrement(size_t cur_eden);
 154   size_t eden_decrement_aligned_down(size_t cur_eden);
 155   size_t eden_increment_with_supplement_aligned_up(size_t cur_eden);
 156 
 157   // Size in bytes for an increment or decrement of the promotion area
 158   virtual size_t promo_increment(size_t cur_promo, uint percent_change);
 159   virtual size_t promo_decrement(size_t cur_promo);
 160   size_t promo_decrement_aligned_down(size_t cur_promo);
 161   size_t promo_increment_with_supplement_aligned_up(size_t cur_promo);
 162 
 163   // Decay the supplemental growth additive.
 164   void decay_supplemental_growth(bool is_full_gc);
 165 
 166   // Returns a change that has been scaled down.  Result
 167   // is not aligned.  (If useful, move to some shared
 168   // location.)
 169   size_t scale_down(size_t change, double part, double total);
 170 
 171  protected:
 172   // Time accessors
 173 
 174   // Footprint accessors
 175   size_t live_space() const {
 176     return (size_t)(avg_base_footprint()->average() +
 177                     avg_young_live()->average() +
 178                     avg_old_live()->average());
 179   }
 180   size_t free_space() const {
 181     return _eden_size + _promo_size;
 182   }
 183 
 184   void set_promo_size(size_t new_size) {
 185     _promo_size = new_size;
 186   }
 187   void set_survivor_size(size_t new_size) {
 188     _survivor_size = new_size;
 189   }
 190 
 191   // Update estimators
 192   void update_minor_pause_old_estimator(double minor_pause_in_ms);
 193 
 194   virtual GCPolicyKind kind() const { return _gc_ps_adaptive_size_policy; }
 195 
 196  public:
 197   // Use by ASPSYoungGen and ASPSOldGen to limit boundary moving.
 198   size_t eden_increment_aligned_up(size_t cur_eden);
 199   size_t eden_increment_aligned_down(size_t cur_eden);
 200   size_t promo_increment_aligned_up(size_t cur_promo);
 201   size_t promo_increment_aligned_down(size_t cur_promo);
 202 
 203   virtual size_t eden_increment(size_t cur_eden);
 204   virtual size_t promo_increment(size_t cur_promo);
 205 
 206   // Accessors for use by performance counters
 207   AdaptivePaddedNoZeroDevAverage*  avg_promoted() const {
 208     return _gc_stats.avg_promoted();
 209   }
 210   AdaptiveWeightedAverage* avg_base_footprint() const {
 211     return _avg_base_footprint;
 212   }
 213 
 214   // Input arguments are initial free space sizes for young and old
 215   // generations, the initial survivor space size, the
 216   // alignment values and the pause & throughput goals.
 217   //
 218   // NEEDS_CLEANUP this is a singleton object
 219   PSAdaptiveSizePolicy(size_t init_eden_size,
 220                        size_t init_promo_size,
 221                        size_t init_survivor_size,
 222                        size_t intra_generation_alignment,
 223                        double gc_pause_goal_sec,
 224                        double gc_minor_pause_goal_sec,
 225                        uint gc_time_ratio);
 226 
 227   // Methods indicating events of interest to the adaptive size policy,
 228   // called by GC algorithms. It is the responsibility of users of this
 229   // policy to call these methods at the correct times!
 230   void major_collection_begin();
 231   void major_collection_end(size_t amount_live, GCCause::Cause gc_cause);
 232 
 233   //
 234   void tenured_allocation(size_t size) {
 235     _avg_pretenured->sample(size);
 236   }
 237 
 238   // Accessors
 239   // NEEDS_CLEANUP   should use sizes.hpp
 240 
 241   size_t calculated_old_free_size_in_bytes() const {
 242     return (size_t)(_promo_size + avg_promoted()->padded_average());
 243   }
 244 
 245   size_t average_old_live_in_bytes() const {
 246     return (size_t) avg_old_live()->average();
 247   }
 248 
 249   size_t average_promoted_in_bytes() const {
 250     return (size_t)avg_promoted()->average();
 251   }
 252 
 253   size_t padded_average_promoted_in_bytes() const {
 254     return (size_t)avg_promoted()->padded_average();
 255   }
 256 
 257   int change_young_gen_for_maj_pauses() {
 258     return _change_young_gen_for_maj_pauses;
 259   }
 260   void set_change_young_gen_for_maj_pauses(int v) {
 261     _change_young_gen_for_maj_pauses = v;
 262   }
 263 
 264   int change_old_gen_for_min_pauses() {
 265     return _change_old_gen_for_min_pauses;
 266   }
 267   void set_change_old_gen_for_min_pauses(int v) {
 268     _change_old_gen_for_min_pauses = v;
 269   }
 270 
 271   // Return true if the old generation size was changed
 272   // to try to reach a pause time goal.
 273   bool old_gen_changed_for_pauses() {
 274     bool result = _change_old_gen_for_maj_pauses != 0 ||
 275                   _change_old_gen_for_min_pauses != 0;
 276     return result;
 277   }
 278 
 279   // Return true if the young generation size was changed
 280   // to try to reach a pause time goal.
 281   bool young_gen_changed_for_pauses() {
 282     bool result = _change_young_gen_for_min_pauses != 0 ||
 283                   _change_young_gen_for_maj_pauses != 0;
 284     return result;
 285   }
 286   // end flags for pause goal
 287 
 288   // Return true if the old generation size was changed
 289   // to try to reach a throughput goal.
 290   bool old_gen_changed_for_throughput() {
 291     bool result = _change_old_gen_for_throughput != 0;
 292     return result;
 293   }
 294 
 295   // Return true if the young generation size was changed
 296   // to try to reach a throughput goal.
 297   bool young_gen_changed_for_throughput() {
 298     bool result = _change_young_gen_for_throughput != 0;
 299     return result;
 300   }
 301 
 302   int decrease_for_footprint() { return _decrease_for_footprint; }
 303 
 304 
 305   // Accessors for estimators.  The slope of the linear fit is
 306   // currently all that is used for making decisions.
 307 
 308   LinearLeastSquareFit* major_pause_old_estimator() {
 309     return _major_pause_old_estimator;
 310   }
 311 
 312   LinearLeastSquareFit* major_pause_young_estimator() {
 313     return _major_pause_young_estimator;
 314   }
 315 
 316 
 317   virtual void clear_generation_free_space_flags();
 318 
 319   float major_pause_old_slope() { return _major_pause_old_estimator->slope(); }
 320   float major_pause_young_slope() {
 321     return _major_pause_young_estimator->slope();
 322   }
 323   float major_collection_slope() { return _major_collection_estimator->slope();}
 324 
 325   bool old_gen_policy_is_ready() { return _old_gen_policy_is_ready; }
 326 
 327   // Given the amount of live data in the heap, should we
 328   // perform a Full GC?
 329   bool should_full_GC(size_t live_in_old_gen);
 330 
 331   // Calculates optimial free space sizes for both the old and young
 332   // generations.  Stores results in _eden_size and _promo_size.
 333   // Takes current used space in all generations as input, as well
 334   // as an indication if a full gc has just been performed, for use
 335   // in deciding if an OOM error should be thrown.
 336   void compute_generation_free_space(size_t young_live,
 337                                      size_t eden_live,
 338                                      size_t old_live,
 339                                      size_t perm_live,
 340                                      size_t cur_eden,  // current eden in bytes
 341                                      size_t max_old_gen_size,
 342                                      size_t max_eden_size,
 343                                      bool   is_full_gc,
 344                                      GCCause::Cause gc_cause,
 345                                      CollectorPolicy* collector_policy);
 346 
 347   // Calculates new survivor space size;  returns a new tenuring threshold
 348   // value. Stores new survivor size in _survivor_size.
 349   int compute_survivor_space_size_and_threshold(bool   is_survivor_overflow,
 350                                                 int    tenuring_threshold,
 351                                                 size_t survivor_limit);
 352 
 353   // Return the maximum size of a survivor space if the young generation were of
 354   // size gen_size.
 355   size_t max_survivor_size(size_t gen_size) {
 356     // Never allow the target survivor size to grow more than MinSurvivorRatio
 357     // of the young generation size.  We cannot grow into a two semi-space
 358     // system, with Eden zero sized.  Even if the survivor space grows, from()
 359     // might grow by moving the bottom boundary "down" -- so from space will
 360     // remain almost full anyway (top() will be near end(), but there will be a
 361     // large filler object at the bottom).
 362     const size_t sz = gen_size / MinSurvivorRatio;
 363     const size_t alignment = _intra_generation_alignment;
 364     return sz > alignment ? align_size_down(sz, alignment) : alignment;
 365   }
 366 
 367   size_t live_at_last_full_gc() {
 368     return _live_at_last_full_gc;
 369   }
 370 
 371   size_t bytes_absorbed_from_eden() const { return _bytes_absorbed_from_eden; }
 372   void   reset_bytes_absorbed_from_eden() { _bytes_absorbed_from_eden = 0; }
 373 
 374   void set_bytes_absorbed_from_eden(size_t val) {
 375     _bytes_absorbed_from_eden = val;
 376   }
 377 
 378   // Update averages that are always used (even
 379   // if adaptive sizing is turned off).
 380   void update_averages(bool is_survivor_overflow,
 381                        size_t survived,
 382                        size_t promoted);
 383 
 384   // Printing support
 385   virtual bool print_adaptive_size_policy_on(outputStream* st) const;
 386 };