1 /* 2 * Copyright (c) 2004, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_ADAPTIVESIZEPOLICY_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_SHARED_ADAPTIVESIZEPOLICY_HPP 27 28 #include "gc_implementation/shared/gcUtil.hpp" 29 #include "gc_interface/collectedHeap.hpp" 30 #include "gc_interface/gcCause.hpp" 31 #include "memory/allocation.hpp" 32 #include "memory/universe.hpp" 33 34 // This class keeps statistical information and computes the 35 // size of the heap. 36 37 // Forward decls 38 class elapsedTimer; 39 class CollectorPolicy; 40 41 class AdaptiveSizePolicy : public CHeapObj { 42 friend class GCAdaptivePolicyCounters; 43 friend class PSGCAdaptivePolicyCounters; 44 friend class CMSGCAdaptivePolicyCounters; 45 protected: 46 47 enum GCPolicyKind { 48 _gc_adaptive_size_policy, 49 _gc_ps_adaptive_size_policy, 50 _gc_cms_adaptive_size_policy 51 }; 52 virtual GCPolicyKind kind() const { return _gc_adaptive_size_policy; } 53 54 enum SizePolicyTrueValues { 55 decrease_old_gen_for_throughput_true = -7, 56 decrease_young_gen_for_througput_true = -6, 57 58 increase_old_gen_for_min_pauses_true = -5, 59 decrease_old_gen_for_min_pauses_true = -4, 60 decrease_young_gen_for_maj_pauses_true = -3, 61 increase_young_gen_for_min_pauses_true = -2, 62 increase_old_gen_for_maj_pauses_true = -1, 63 64 decrease_young_gen_for_min_pauses_true = 1, 65 decrease_old_gen_for_maj_pauses_true = 2, 66 increase_young_gen_for_maj_pauses_true = 3, 67 68 increase_old_gen_for_throughput_true = 4, 69 increase_young_gen_for_througput_true = 5, 70 71 decrease_young_gen_for_footprint_true = 6, 72 decrease_old_gen_for_footprint_true = 7, 73 decide_at_full_gc_true = 8 74 }; 75 76 // Goal for the fraction of the total time during which application 77 // threads run. 78 const double _throughput_goal; 79 80 // Last calculated sizes, in bytes, and aligned 81 size_t _eden_size; // calculated eden free space in bytes 82 size_t _promo_size; // calculated cms gen free space in bytes 83 84 size_t _survivor_size; // calculated survivor size in bytes 85 86 // This is a hint for the heap: we've detected that gc times 87 // are taking longer than GCTimeLimit allows. 88 bool _gc_overhead_limit_exceeded; 89 // Use for diagnostics only. If UseGCOverheadLimit is false, 90 // this variable is still set. 91 bool _print_gc_overhead_limit_would_be_exceeded; 92 // Count of consecutive GC that have exceeded the 93 // GC time limit criterion. 94 uint _gc_overhead_limit_count; 95 // This flag signals that GCTimeLimit is being exceeded 96 // but may not have done so for the required number of consequetive 97 // collections. 98 99 // Minor collection timers used to determine both 100 // pause and interval times for collections. 101 static elapsedTimer _minor_timer; 102 103 // Major collection timers, used to determine both 104 // pause and interval times for collections 105 static elapsedTimer _major_timer; 106 107 // Time statistics 108 AdaptivePaddedAverage* _avg_minor_pause; 109 AdaptiveWeightedAverage* _avg_minor_interval; 110 AdaptiveWeightedAverage* _avg_minor_gc_cost; 111 112 AdaptiveWeightedAverage* _avg_major_interval; 113 AdaptiveWeightedAverage* _avg_major_gc_cost; 114 115 // Footprint statistics 116 AdaptiveWeightedAverage* _avg_young_live; 117 AdaptiveWeightedAverage* _avg_eden_live; 118 AdaptiveWeightedAverage* _avg_old_live; 119 120 // Statistics for survivor space calculation for young generation 121 AdaptivePaddedAverage* _avg_survived; 122 123 // Objects that have been directly allocated in the old generation. 124 AdaptivePaddedNoZeroDevAverage* _avg_pretenured; 125 126 // Variable for estimating the major and minor pause times. 127 // These variables represent linear least-squares fits of 128 // the data. 129 // minor pause time vs. old gen size 130 LinearLeastSquareFit* _minor_pause_old_estimator; 131 // minor pause time vs. young gen size 132 LinearLeastSquareFit* _minor_pause_young_estimator; 133 134 // Variables for estimating the major and minor collection costs 135 // minor collection time vs. young gen size 136 LinearLeastSquareFit* _minor_collection_estimator; 137 // major collection time vs. cms gen size 138 LinearLeastSquareFit* _major_collection_estimator; 139 140 // These record the most recent collection times. They 141 // are available as an alternative to using the averages 142 // for making ergonomic decisions. 143 double _latest_minor_mutator_interval_seconds; 144 145 // Allowed difference between major and minor gc times, used 146 // for computing tenuring_threshold. 147 const double _threshold_tolerance_percent; 148 149 const double _gc_pause_goal_sec; // goal for maximum gc pause 150 151 // Flag indicating that the adaptive policy is ready to use 152 bool _young_gen_policy_is_ready; 153 154 // decrease/increase the young generation for minor pause time 155 int _change_young_gen_for_min_pauses; 156 157 // decrease/increase the old generation for major pause time 158 int _change_old_gen_for_maj_pauses; 159 160 // change old geneneration for throughput 161 int _change_old_gen_for_throughput; 162 163 // change young generation for throughput 164 int _change_young_gen_for_throughput; 165 166 // Flag indicating that the policy would 167 // increase the tenuring threshold because of the total major gc cost 168 // is greater than the total minor gc cost 169 bool _increment_tenuring_threshold_for_gc_cost; 170 // decrease the tenuring threshold because of the the total minor gc 171 // cost is greater than the total major gc cost 172 bool _decrement_tenuring_threshold_for_gc_cost; 173 // decrease due to survivor size limit 174 bool _decrement_tenuring_threshold_for_survivor_limit; 175 176 // decrease generation sizes for footprint 177 int _decrease_for_footprint; 178 179 // Set if the ergonomic decisions were made at a full GC. 180 int _decide_at_full_gc; 181 182 // Changing the generation sizing depends on the data that is 183 // gathered about the effects of changes on the pause times and 184 // throughput. These variable count the number of data points 185 // gathered. The policy may use these counters as a threshhold 186 // for reliable data. 187 julong _young_gen_change_for_minor_throughput; 188 julong _old_gen_change_for_major_throughput; 189 190 // Accessors 191 192 double gc_pause_goal_sec() const { return _gc_pause_goal_sec; } 193 // The value returned is unitless: it's the proportion of time 194 // spent in a particular collection type. 195 // An interval time will be 0.0 if a collection type hasn't occurred yet. 196 // The 1.4.2 implementation put a floor on the values of major_gc_cost 197 // and minor_gc_cost. This was useful because of the way major_gc_cost 198 // and minor_gc_cost was used in calculating the sizes of the generations. 199 // Do not use a floor in this implementation because any finite value 200 // will put a limit on the throughput that can be achieved and any 201 // throughput goal above that limit will drive the generations sizes 202 // to extremes. 203 double major_gc_cost() const { 204 return MAX2(0.0F, _avg_major_gc_cost->average()); 205 } 206 207 // The value returned is unitless: it's the proportion of time 208 // spent in a particular collection type. 209 // An interval time will be 0.0 if a collection type hasn't occurred yet. 210 // The 1.4.2 implementation put a floor on the values of major_gc_cost 211 // and minor_gc_cost. This was useful because of the way major_gc_cost 212 // and minor_gc_cost was used in calculating the sizes of the generations. 213 // Do not use a floor in this implementation because any finite value 214 // will put a limit on the throughput that can be achieved and any 215 // throughput goal above that limit will drive the generations sizes 216 // to extremes. 217 218 double minor_gc_cost() const { 219 return MAX2(0.0F, _avg_minor_gc_cost->average()); 220 } 221 222 // Because we're dealing with averages, gc_cost() can be 223 // larger than 1.0 if just the sum of the minor cost the 224 // the major cost is used. Worse than that is the 225 // fact that the minor cost and the major cost each 226 // tend toward 1.0 in the extreme of high gc costs. 227 // Limit the value of gc_cost to 1.0 so that the mutator 228 // cost stays non-negative. 229 virtual double gc_cost() const { 230 double result = MIN2(1.0, minor_gc_cost() + major_gc_cost()); 231 assert(result >= 0.0, "Both minor and major costs are non-negative"); 232 return result; 233 } 234 235 // Elapsed time since the last major collection. 236 virtual double time_since_major_gc() const; 237 238 // Average interval between major collections to be used 239 // in calculating the decaying major gc cost. An overestimate 240 // of this time would be a conservative estimate because 241 // this time is used to decide if the major GC cost 242 // should be decayed (i.e., if the time since the last 243 // major gc is long compared to the time returned here, 244 // then the major GC cost will be decayed). See the 245 // implementations for the specifics. 246 virtual double major_gc_interval_average_for_decay() const { 247 return _avg_major_interval->average(); 248 } 249 250 // Return the cost of the GC where the major gc cost 251 // has been decayed based on the time since the last 252 // major collection. 253 double decaying_gc_cost() const; 254 255 // Decay the major gc cost. Use this only for decisions on 256 // whether to adjust, not to determine by how much to adjust. 257 // This approximation is crude and may not be good enough for the 258 // latter. 259 double decaying_major_gc_cost() const; 260 261 // Return the mutator cost using the decayed 262 // GC cost. 263 double adjusted_mutator_cost() const { 264 double result = 1.0 - decaying_gc_cost(); 265 assert(result >= 0.0, "adjusted mutator cost calculation is incorrect"); 266 return result; 267 } 268 269 virtual double mutator_cost() const { 270 double result = 1.0 - gc_cost(); 271 assert(result >= 0.0, "mutator cost calculation is incorrect"); 272 return result; 273 } 274 275 276 bool young_gen_policy_is_ready() { return _young_gen_policy_is_ready; } 277 278 void update_minor_pause_young_estimator(double minor_pause_in_ms); 279 virtual void update_minor_pause_old_estimator(double minor_pause_in_ms) { 280 // This is not meaningful for all policies but needs to be present 281 // to use minor_collection_end() in its current form. 282 } 283 284 virtual size_t eden_increment(size_t cur_eden); 285 virtual size_t eden_increment(size_t cur_eden, uint percent_change); 286 virtual size_t eden_decrement(size_t cur_eden); 287 virtual size_t promo_increment(size_t cur_eden); 288 virtual size_t promo_increment(size_t cur_eden, uint percent_change); 289 virtual size_t promo_decrement(size_t cur_eden); 290 291 virtual void clear_generation_free_space_flags(); 292 293 int change_old_gen_for_throughput() const { 294 return _change_old_gen_for_throughput; 295 } 296 void set_change_old_gen_for_throughput(int v) { 297 _change_old_gen_for_throughput = v; 298 } 299 int change_young_gen_for_throughput() const { 300 return _change_young_gen_for_throughput; 301 } 302 void set_change_young_gen_for_throughput(int v) { 303 _change_young_gen_for_throughput = v; 304 } 305 306 int change_old_gen_for_maj_pauses() const { 307 return _change_old_gen_for_maj_pauses; 308 } 309 void set_change_old_gen_for_maj_pauses(int v) { 310 _change_old_gen_for_maj_pauses = v; 311 } 312 313 bool decrement_tenuring_threshold_for_gc_cost() const { 314 return _decrement_tenuring_threshold_for_gc_cost; 315 } 316 void set_decrement_tenuring_threshold_for_gc_cost(bool v) { 317 _decrement_tenuring_threshold_for_gc_cost = v; 318 } 319 bool increment_tenuring_threshold_for_gc_cost() const { 320 return _increment_tenuring_threshold_for_gc_cost; 321 } 322 void set_increment_tenuring_threshold_for_gc_cost(bool v) { 323 _increment_tenuring_threshold_for_gc_cost = v; 324 } 325 bool decrement_tenuring_threshold_for_survivor_limit() const { 326 return _decrement_tenuring_threshold_for_survivor_limit; 327 } 328 void set_decrement_tenuring_threshold_for_survivor_limit(bool v) { 329 _decrement_tenuring_threshold_for_survivor_limit = v; 330 } 331 // Return true if the policy suggested a change. 332 bool tenuring_threshold_change() const; 333 334 public: 335 AdaptiveSizePolicy(size_t init_eden_size, 336 size_t init_promo_size, 337 size_t init_survivor_size, 338 double gc_pause_goal_sec, 339 uint gc_cost_ratio); 340 341 bool is_gc_cms_adaptive_size_policy() { 342 return kind() == _gc_cms_adaptive_size_policy; 343 } 344 bool is_gc_ps_adaptive_size_policy() { 345 return kind() == _gc_ps_adaptive_size_policy; 346 } 347 348 AdaptivePaddedAverage* avg_minor_pause() const { return _avg_minor_pause; } 349 AdaptiveWeightedAverage* avg_minor_interval() const { 350 return _avg_minor_interval; 351 } 352 AdaptiveWeightedAverage* avg_minor_gc_cost() const { 353 return _avg_minor_gc_cost; 354 } 355 356 AdaptiveWeightedAverage* avg_major_gc_cost() const { 357 return _avg_major_gc_cost; 358 } 359 360 AdaptiveWeightedAverage* avg_young_live() const { return _avg_young_live; } 361 AdaptiveWeightedAverage* avg_eden_live() const { return _avg_eden_live; } 362 AdaptiveWeightedAverage* avg_old_live() const { return _avg_old_live; } 363 364 AdaptivePaddedAverage* avg_survived() const { return _avg_survived; } 365 AdaptivePaddedNoZeroDevAverage* avg_pretenured() { return _avg_pretenured; } 366 367 // Methods indicating events of interest to the adaptive size policy, 368 // called by GC algorithms. It is the responsibility of users of this 369 // policy to call these methods at the correct times! 370 virtual void minor_collection_begin(); 371 virtual void minor_collection_end(GCCause::Cause gc_cause); 372 virtual LinearLeastSquareFit* minor_pause_old_estimator() const { 373 return _minor_pause_old_estimator; 374 } 375 376 LinearLeastSquareFit* minor_pause_young_estimator() { 377 return _minor_pause_young_estimator; 378 } 379 LinearLeastSquareFit* minor_collection_estimator() { 380 return _minor_collection_estimator; 381 } 382 383 LinearLeastSquareFit* major_collection_estimator() { 384 return _major_collection_estimator; 385 } 386 387 float minor_pause_young_slope() { 388 return _minor_pause_young_estimator->slope(); 389 } 390 391 float minor_collection_slope() { return _minor_collection_estimator->slope();} 392 float major_collection_slope() { return _major_collection_estimator->slope();} 393 394 float minor_pause_old_slope() { 395 return _minor_pause_old_estimator->slope(); 396 } 397 398 void set_eden_size(size_t new_size) { 399 _eden_size = new_size; 400 } 401 void set_survivor_size(size_t new_size) { 402 _survivor_size = new_size; 403 } 404 405 size_t calculated_eden_size_in_bytes() const { 406 return _eden_size; 407 } 408 409 size_t calculated_promo_size_in_bytes() const { 410 return _promo_size; 411 } 412 413 size_t calculated_survivor_size_in_bytes() const { 414 return _survivor_size; 415 } 416 417 // This is a hint for the heap: we've detected that gc times 418 // are taking longer than GCTimeLimit allows. 419 // Most heaps will choose to throw an OutOfMemoryError when 420 // this occurs but it is up to the heap to request this information 421 // of the policy 422 bool gc_overhead_limit_exceeded() { 423 return _gc_overhead_limit_exceeded; 424 } 425 void set_gc_overhead_limit_exceeded(bool v) { 426 _gc_overhead_limit_exceeded = v; 427 } 428 429 // Tests conditions indicate the GC overhead limit is being approached. 430 bool gc_overhead_limit_near() { 431 return gc_overhead_limit_count() >= 432 (AdaptiveSizePolicyGCTimeLimitThreshold - 1); 433 } 434 uint gc_overhead_limit_count() { return _gc_overhead_limit_count; } 435 void reset_gc_overhead_limit_count() { _gc_overhead_limit_count = 0; } 436 void inc_gc_overhead_limit_count() { _gc_overhead_limit_count++; } 437 // accessors for flags recording the decisions to resize the 438 // generations to meet the pause goal. 439 440 int change_young_gen_for_min_pauses() const { 441 return _change_young_gen_for_min_pauses; 442 } 443 void set_change_young_gen_for_min_pauses(int v) { 444 _change_young_gen_for_min_pauses = v; 445 } 446 void set_decrease_for_footprint(int v) { _decrease_for_footprint = v; } 447 int decrease_for_footprint() const { return _decrease_for_footprint; } 448 int decide_at_full_gc() { return _decide_at_full_gc; } 449 void set_decide_at_full_gc(int v) { _decide_at_full_gc = v; } 450 451 // Check the conditions for an out-of-memory due to excessive GC time. 452 // Set _gc_overhead_limit_exceeded if all the conditions have been met. 453 void check_gc_overhead_limit(size_t young_live, 454 size_t eden_live, 455 size_t max_old_gen_size, 456 size_t max_eden_size, 457 bool is_full_gc, 458 GCCause::Cause gc_cause, 459 CollectorPolicy* collector_policy); 460 461 // Printing support 462 virtual bool print_adaptive_size_policy_on(outputStream* st) const; 463 bool print_adaptive_size_policy_on(outputStream* st, int 464 tenuring_threshold) const; 465 }; 466 467 // Class that can be used to print information about the 468 // adaptive size policy at intervals specified by 469 // AdaptiveSizePolicyOutputInterval. Only print information 470 // if an adaptive size policy is in use. 471 class AdaptiveSizePolicyOutput : StackObj { 472 AdaptiveSizePolicy* _size_policy; 473 bool _do_print; 474 bool print_test(uint count) { 475 // A count of zero is a special value that indicates that the 476 // interval test should be ignored. An interval is of zero is 477 // a special value that indicates that the interval test should 478 // always fail (never do the print based on the interval test). 479 return PrintGCDetails && 480 UseAdaptiveSizePolicy && 481 (UseParallelGC || UseConcMarkSweepGC) && 482 (AdaptiveSizePolicyOutputInterval > 0) && 483 ((count == 0) || 484 ((count % AdaptiveSizePolicyOutputInterval) == 0)); 485 } 486 public: 487 // The special value of a zero count can be used to ignore 488 // the count test. 489 AdaptiveSizePolicyOutput(uint count) { 490 if (UseAdaptiveSizePolicy && (AdaptiveSizePolicyOutputInterval > 0)) { 491 CollectedHeap* heap = Universe::heap(); 492 _size_policy = heap->size_policy(); 493 _do_print = print_test(count); 494 } else { 495 _size_policy = NULL; 496 _do_print = false; 497 } 498 } 499 AdaptiveSizePolicyOutput(AdaptiveSizePolicy* size_policy, 500 uint count) : 501 _size_policy(size_policy) { 502 if (UseAdaptiveSizePolicy && (AdaptiveSizePolicyOutputInterval > 0)) { 503 _do_print = print_test(count); 504 } else { 505 _do_print = false; 506 } 507 } 508 ~AdaptiveSizePolicyOutput() { 509 if (_do_print) { 510 assert(UseAdaptiveSizePolicy, "Should not be in use"); 511 _size_policy->print_adaptive_size_policy_on(gclog_or_tty); 512 } 513 } 514 }; 515 516 #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_ADAPTIVESIZEPOLICY_HPP