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