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/gcCause.hpp" 28 #include "gc/shared/gcUtil.inline.hpp" 29 #include "gc/shared/softRefPolicy.hpp" 30 #include "logging/log.hpp" 31 #include "runtime/timer.hpp" 32 33 elapsedTimer AdaptiveSizePolicy::_minor_timer; 34 elapsedTimer AdaptiveSizePolicy::_major_timer; 35 36 // The throughput goal is implemented as 37 // _throughput_goal = 1 - ( 1 / (1 + gc_cost_ratio)) 38 // gc_cost_ratio is the ratio 39 // application cost / gc cost 40 // For example a gc_cost_ratio of 4 translates into a 41 // throughput goal of .80 42 43 AdaptiveSizePolicy::AdaptiveSizePolicy(size_t init_eden_size, 44 size_t init_promo_size, 45 size_t init_survivor_size, 46 double gc_pause_goal_sec, 47 uint gc_cost_ratio) : 48 _throughput_goal(1.0 - double(1.0 / (1.0 + (double) gc_cost_ratio))), 49 _eden_size(init_eden_size), 50 _promo_size(init_promo_size), 51 _survivor_size(init_survivor_size), 52 _gc_overhead_limit_exceeded(false), 53 _print_gc_overhead_limit_would_be_exceeded(false), 54 _gc_overhead_limit_count(0), 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( 75 AdaptiveSizePolicyWeight, 76 SurvivorPadding); 77 78 _minor_pause_old_estimator = 79 new LinearLeastSquareFit(AdaptiveSizePolicyWeight); 80 _minor_pause_young_estimator = 81 new LinearLeastSquareFit(AdaptiveSizePolicyWeight); 82 _minor_collection_estimator = 83 new LinearLeastSquareFit(AdaptiveSizePolicyWeight); 84 _major_collection_estimator = 85 new LinearLeastSquareFit(AdaptiveSizePolicyWeight); 86 87 // Start the timers 88 _minor_timer.start(); 89 90 _young_gen_policy_is_ready = false; 91 } 92 93 bool AdaptiveSizePolicy::tenuring_threshold_change() const { 94 return decrement_tenuring_threshold_for_gc_cost() || 95 increment_tenuring_threshold_for_gc_cost() || 96 decrement_tenuring_threshold_for_survivor_limit(); 97 } 98 99 void AdaptiveSizePolicy::minor_collection_begin() { 100 // Update the interval time 101 _minor_timer.stop(); 102 // Save most recent collection time 103 _latest_minor_mutator_interval_seconds = _minor_timer.seconds(); 104 _minor_timer.reset(); 105 _minor_timer.start(); 106 } 107 108 void AdaptiveSizePolicy::update_minor_pause_young_estimator( 109 double minor_pause_in_ms) { 110 double eden_size_in_mbytes = ((double)_eden_size)/((double)M); 111 _minor_pause_young_estimator->update(eden_size_in_mbytes, 112 minor_pause_in_ms); 113 } 114 115 void AdaptiveSizePolicy::minor_collection_end(GCCause::Cause gc_cause) { 116 // Update the pause time. 117 _minor_timer.stop(); 118 119 if (!GCCause::is_user_requested_gc(gc_cause) || 120 UseAdaptiveSizePolicyWithSystemGC) { 121 double minor_pause_in_seconds = _minor_timer.seconds(); 122 double minor_pause_in_ms = minor_pause_in_seconds * MILLIUNITS; 123 124 // Sample for performance counter 125 _avg_minor_pause->sample(minor_pause_in_seconds); 126 127 // Cost of collection (unit-less) 128 double collection_cost = 0.0; 129 if ((_latest_minor_mutator_interval_seconds > 0.0) && 130 (minor_pause_in_seconds > 0.0)) { 131 double interval_in_seconds = 132 _latest_minor_mutator_interval_seconds + minor_pause_in_seconds; 133 collection_cost = 134 minor_pause_in_seconds / interval_in_seconds; 135 _avg_minor_gc_cost->sample(collection_cost); 136 // Sample for performance counter 137 _avg_minor_interval->sample(interval_in_seconds); 138 } 139 140 // The policy does not have enough data until at least some 141 // young collections have been done. 142 _young_gen_policy_is_ready = 143 (_avg_minor_gc_cost->count() >= AdaptiveSizePolicyReadyThreshold); 144 145 // Calculate variables used to estimate pause time vs. gen sizes 146 double eden_size_in_mbytes = ((double)_eden_size) / ((double)M); 147 update_minor_pause_young_estimator(minor_pause_in_ms); 148 update_minor_pause_old_estimator(minor_pause_in_ms); 149 150 log_trace(gc, ergo)("AdaptiveSizePolicy::minor_collection_end: minor gc cost: %f average: %f", 151 collection_cost, _avg_minor_gc_cost->average()); 152 log_trace(gc, ergo)(" minor pause: %f minor period %f", 153 minor_pause_in_ms, _latest_minor_mutator_interval_seconds * MILLIUNITS); 154 155 // Calculate variable used to estimate collection cost vs. gen sizes 156 assert(collection_cost >= 0.0, "Expected to be non-negative"); 157 _minor_collection_estimator->update(eden_size_in_mbytes, collection_cost); 158 } 159 160 // Interval times use this timer to measure the mutator time. 161 // Reset the timer after the GC pause. 162 _minor_timer.reset(); 163 _minor_timer.start(); 164 } 165 166 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden, uint percent_change) { 167 size_t eden_heap_delta; 168 eden_heap_delta = cur_eden / 100 * percent_change; 169 return eden_heap_delta; 170 } 171 172 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden) { 173 return eden_increment(cur_eden, YoungGenerationSizeIncrement); 174 } 175 176 size_t AdaptiveSizePolicy::eden_decrement(size_t cur_eden) { 177 size_t eden_heap_delta = eden_increment(cur_eden) / 178 AdaptiveSizeDecrementScaleFactor; 179 return eden_heap_delta; 180 } 181 182 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo, uint percent_change) { 183 size_t promo_heap_delta; 184 promo_heap_delta = cur_promo / 100 * percent_change; 185 return promo_heap_delta; 186 } 187 188 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo) { 189 return promo_increment(cur_promo, TenuredGenerationSizeIncrement); 190 } 191 192 size_t AdaptiveSizePolicy::promo_decrement(size_t cur_promo) { 193 size_t promo_heap_delta = promo_increment(cur_promo); 194 promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor; 195 return promo_heap_delta; 196 } 197 198 double AdaptiveSizePolicy::time_since_major_gc() const { 199 _major_timer.stop(); 200 double result = _major_timer.seconds(); 201 _major_timer.start(); 202 return result; 203 } 204 205 // Linear decay of major gc cost 206 double AdaptiveSizePolicy::decaying_major_gc_cost() const { 207 double major_interval = major_gc_interval_average_for_decay(); 208 double major_gc_cost_average = major_gc_cost(); 209 double decayed_major_gc_cost = major_gc_cost_average; 210 if(time_since_major_gc() > 0.0) { 211 decayed_major_gc_cost = major_gc_cost() * 212 (((double) AdaptiveSizeMajorGCDecayTimeScale) * major_interval) 213 / time_since_major_gc(); 214 } 215 216 // The decayed cost should always be smaller than the 217 // average cost but the vagaries of finite arithmetic could 218 // produce a larger value in decayed_major_gc_cost so protect 219 // against that. 220 return MIN2(major_gc_cost_average, decayed_major_gc_cost); 221 } 222 223 // Use a value of the major gc cost that has been decayed 224 // by the factor 225 // 226 // average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale / 227 // time-since-last-major-gc 228 // 229 // if the average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale 230 // is less than time-since-last-major-gc. 231 // 232 // In cases where there are initial major gc's that 233 // are of a relatively high cost but no later major 234 // gc's, the total gc cost can remain high because 235 // the major gc cost remains unchanged (since there are no major 236 // gc's). In such a situation the value of the unchanging 237 // major gc cost can keep the mutator throughput below 238 // the goal when in fact the major gc cost is becoming diminishingly 239 // small. Use the decaying gc cost only to decide whether to 240 // adjust for throughput. Using it also to determine the adjustment 241 // to be made for throughput also seems reasonable but there is 242 // no test case to use to decide if it is the right thing to do 243 // don't do it yet. 244 245 double AdaptiveSizePolicy::decaying_gc_cost() const { 246 double decayed_major_gc_cost = major_gc_cost(); 247 double avg_major_interval = major_gc_interval_average_for_decay(); 248 if (UseAdaptiveSizeDecayMajorGCCost && 249 (AdaptiveSizeMajorGCDecayTimeScale > 0) && 250 (avg_major_interval > 0.00)) { 251 double time_since_last_major_gc = time_since_major_gc(); 252 253 // Decay the major gc cost? 254 if (time_since_last_major_gc > 255 ((double) AdaptiveSizeMajorGCDecayTimeScale) * avg_major_interval) { 256 257 // Decay using the time-since-last-major-gc 258 decayed_major_gc_cost = decaying_major_gc_cost(); 259 log_trace(gc, ergo)("decaying_gc_cost: major interval average: %f time since last major gc: %f", 260 avg_major_interval, time_since_last_major_gc); 261 log_trace(gc, ergo)(" major gc cost: %f decayed major gc cost: %f", 262 major_gc_cost(), decayed_major_gc_cost); 263 } 264 } 265 double result = MIN2(1.0, decayed_major_gc_cost + minor_gc_cost()); 266 return result; 267 } 268 269 270 void AdaptiveSizePolicy::clear_generation_free_space_flags() { 271 set_change_young_gen_for_min_pauses(0); 272 set_change_old_gen_for_maj_pauses(0); 273 274 set_change_old_gen_for_throughput(0); 275 set_change_young_gen_for_throughput(0); 276 set_decrease_for_footprint(0); 277 set_decide_at_full_gc(0); 278 } 279 280 void AdaptiveSizePolicy::check_gc_overhead_limit( 281 size_t young_live, 282 size_t eden_live, 283 size_t max_old_gen_size, 284 size_t max_eden_size, 285 bool is_full_gc, 286 GCCause::Cause gc_cause, 287 SoftRefPolicy* soft_ref_policy) { 288 289 // Ignore explicit GC's. Exiting here does not set the flag and 290 // does not reset the count. Updating of the averages for system 291 // GC's is still controlled by UseAdaptiveSizePolicyWithSystemGC. 292 if (GCCause::is_user_requested_gc(gc_cause) || 293 GCCause::is_serviceability_requested_gc(gc_cause)) { 294 return; 295 } 296 // eden_limit is the upper limit on the size of eden based on 297 // the maximum size of the young generation and the sizes 298 // of the survivor space. 299 // The question being asked is whether the gc costs are high 300 // and the space being recovered by a collection is low. 301 // free_in_young_gen is the free space in the young generation 302 // after a collection and promo_live is the free space in the old 303 // generation after a collection. 304 // 305 // Use the minimum of the current value of the live in the 306 // young gen or the average of the live in the young gen. 307 // If the current value drops quickly, that should be taken 308 // into account (i.e., don't trigger if the amount of free 309 // space has suddenly jumped up). If the current is much 310 // higher than the average, use the average since it represents 311 // the longer term behavior. 312 const size_t live_in_eden = 313 MIN2(eden_live, (size_t) avg_eden_live()->average()); 314 const size_t free_in_eden = max_eden_size > live_in_eden ? 315 max_eden_size - live_in_eden : 0; 316 const size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average()); 317 const size_t total_free_limit = free_in_old_gen + free_in_eden; 318 const size_t total_mem = max_old_gen_size + max_eden_size; 319 const double mem_free_limit = total_mem * (GCHeapFreeLimit/100.0); 320 const double mem_free_old_limit = max_old_gen_size * (GCHeapFreeLimit/100.0); 321 const double mem_free_eden_limit = max_eden_size * (GCHeapFreeLimit/100.0); 322 const double gc_cost_limit = GCTimeLimit/100.0; 323 size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average()); 324 // But don't force a promo size below the current promo size. Otherwise, 325 // the promo size will shrink for no good reason. 326 promo_limit = MAX2(promo_limit, _promo_size); 327 328 329 log_trace(gc, ergo)( 330 "PSAdaptiveSizePolicy::check_gc_overhead_limit:" 331 " promo_limit: " SIZE_FORMAT 332 " max_eden_size: " SIZE_FORMAT 333 " total_free_limit: " SIZE_FORMAT 334 " max_old_gen_size: " SIZE_FORMAT 335 " max_eden_size: " SIZE_FORMAT 336 " mem_free_limit: " SIZE_FORMAT, 337 promo_limit, max_eden_size, total_free_limit, 338 max_old_gen_size, max_eden_size, 339 (size_t) mem_free_limit); 340 341 bool print_gc_overhead_limit_would_be_exceeded = false; 342 if (is_full_gc) { 343 if (gc_cost() > gc_cost_limit && 344 free_in_old_gen < (size_t) mem_free_old_limit && 345 free_in_eden < (size_t) mem_free_eden_limit) { 346 // Collections, on average, are taking too much time, and 347 // gc_cost() > gc_cost_limit 348 // we have too little space available after a full gc. 349 // total_free_limit < mem_free_limit 350 // where 351 // total_free_limit is the free space available in 352 // both generations 353 // total_mem is the total space available for allocation 354 // in both generations (survivor spaces are not included 355 // just as they are not included in eden_limit). 356 // mem_free_limit is a fraction of total_mem judged to be an 357 // acceptable amount that is still unused. 358 // The heap can ask for the value of this variable when deciding 359 // whether to thrown an OutOfMemory error. 360 // Note that the gc time limit test only works for the collections 361 // of the young gen + tenured gen and not for collections of the 362 // permanent gen. That is because the calculation of the space 363 // freed by the collection is the free space in the young gen + 364 // tenured gen. 365 // At this point the GC overhead limit is being exceeded. 366 inc_gc_overhead_limit_count(); 367 if (UseGCOverheadLimit) { 368 if (gc_overhead_limit_count() >= 369 AdaptiveSizePolicyGCTimeLimitThreshold){ 370 // All conditions have been met for throwing an out-of-memory 371 set_gc_overhead_limit_exceeded(true); 372 // Avoid consecutive OOM due to the gc time limit by resetting 373 // the counter. 374 reset_gc_overhead_limit_count(); 375 } else { 376 // The required consecutive collections which exceed the 377 // GC time limit may or may not have been reached. We 378 // are approaching that condition and so as not to 379 // throw an out-of-memory before all SoftRef's have been 380 // cleared, set _should_clear_all_soft_refs in CollectorPolicy. 381 // The clearing will be done on the next GC. 382 bool near_limit = gc_overhead_limit_near(); 383 if (near_limit) { 384 soft_ref_policy->set_should_clear_all_soft_refs(true); 385 log_trace(gc, ergo)("Nearing GC overhead limit, will be clearing all SoftReference"); 386 } 387 } 388 } 389 // Set this even when the overhead limit will not 390 // cause an out-of-memory. Diagnostic message indicating 391 // that the overhead limit is being exceeded is sometimes 392 // printed. 393 print_gc_overhead_limit_would_be_exceeded = true; 394 395 } else { 396 // Did not exceed overhead limits 397 reset_gc_overhead_limit_count(); 398 } 399 } 400 401 if (UseGCOverheadLimit) { 402 if (gc_overhead_limit_exceeded()) { 403 log_trace(gc, ergo)("GC is exceeding overhead limit of " UINTX_FORMAT "%%", GCTimeLimit); 404 reset_gc_overhead_limit_count(); 405 } else if (print_gc_overhead_limit_would_be_exceeded) { 406 assert(gc_overhead_limit_count() > 0, "Should not be printing"); 407 log_trace(gc, ergo)("GC would exceed overhead limit of " UINTX_FORMAT "%% %d consecutive time(s)", 408 GCTimeLimit, gc_overhead_limit_count()); 409 } 410 } 411 } 412 // Printing 413 414 bool AdaptiveSizePolicy::print() const { 415 assert(UseAdaptiveSizePolicy, "UseAdaptiveSizePolicy need to be enabled."); 416 417 if (!log_is_enabled(Debug, gc, ergo)) { 418 return false; 419 } 420 421 // Print goal for which action is needed. 422 char* action = NULL; 423 bool change_for_pause = false; 424 if ((change_old_gen_for_maj_pauses() == 425 decrease_old_gen_for_maj_pauses_true) || 426 (change_young_gen_for_min_pauses() == 427 decrease_young_gen_for_min_pauses_true)) { 428 action = (char*) " *** pause time goal ***"; 429 change_for_pause = true; 430 } else if ((change_old_gen_for_throughput() == 431 increase_old_gen_for_throughput_true) || 432 (change_young_gen_for_throughput() == 433 increase_young_gen_for_througput_true)) { 434 action = (char*) " *** throughput goal ***"; 435 } else if (decrease_for_footprint()) { 436 action = (char*) " *** reduced footprint ***"; 437 } else { 438 // No actions were taken. This can legitimately be the 439 // situation if not enough data has been gathered to make 440 // decisions. 441 return false; 442 } 443 444 // Pauses 445 // Currently the size of the old gen is only adjusted to 446 // change the major pause times. 447 char* young_gen_action = NULL; 448 char* tenured_gen_action = NULL; 449 450 char* shrink_msg = (char*) "(attempted to shrink)"; 451 char* grow_msg = (char*) "(attempted to grow)"; 452 char* no_change_msg = (char*) "(no change)"; 453 if (change_young_gen_for_min_pauses() == 454 decrease_young_gen_for_min_pauses_true) { 455 young_gen_action = shrink_msg; 456 } else if (change_for_pause) { 457 young_gen_action = no_change_msg; 458 } 459 460 if (change_old_gen_for_maj_pauses() == decrease_old_gen_for_maj_pauses_true) { 461 tenured_gen_action = shrink_msg; 462 } else if (change_for_pause) { 463 tenured_gen_action = no_change_msg; 464 } 465 466 // Throughput 467 if (change_old_gen_for_throughput() == increase_old_gen_for_throughput_true) { 468 assert(change_young_gen_for_throughput() == 469 increase_young_gen_for_througput_true, 470 "Both generations should be growing"); 471 young_gen_action = grow_msg; 472 tenured_gen_action = grow_msg; 473 } else if (change_young_gen_for_throughput() == 474 increase_young_gen_for_througput_true) { 475 // Only the young generation may grow at start up (before 476 // enough full collections have been done to grow the old generation). 477 young_gen_action = grow_msg; 478 tenured_gen_action = no_change_msg; 479 } 480 481 // Minimum footprint 482 if (decrease_for_footprint() != 0) { 483 young_gen_action = shrink_msg; 484 tenured_gen_action = shrink_msg; 485 } 486 487 log_debug(gc, ergo)("UseAdaptiveSizePolicy actions to meet %s", action); 488 log_debug(gc, ergo)(" GC overhead (%%)"); 489 log_debug(gc, ergo)(" Young generation: %7.2f\t %s", 490 100.0 * avg_minor_gc_cost()->average(), young_gen_action); 491 log_debug(gc, ergo)(" Tenured generation: %7.2f\t %s", 492 100.0 * avg_major_gc_cost()->average(), tenured_gen_action); 493 return true; 494 } 495 496 void AdaptiveSizePolicy::print_tenuring_threshold( uint new_tenuring_threshold_arg) const { 497 // Tenuring threshold 498 if (decrement_tenuring_threshold_for_survivor_limit()) { 499 log_debug(gc, ergo)("Tenuring threshold: (attempted to decrease to avoid survivor space overflow) = %u", new_tenuring_threshold_arg); 500 } else if (decrement_tenuring_threshold_for_gc_cost()) { 501 log_debug(gc, ergo)("Tenuring threshold: (attempted to decrease to balance GC costs) = %u", new_tenuring_threshold_arg); 502 } else if (increment_tenuring_threshold_for_gc_cost()) { 503 log_debug(gc, ergo)("Tenuring threshold: (attempted to increase to balance GC costs) = %u", new_tenuring_threshold_arg); 504 } else { 505 assert(!tenuring_threshold_change(), "(no change was attempted)"); 506 } 507 }