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