src/share/vm/gc_implementation/shared/adaptiveSizePolicy.cpp

Wed, 31 Jul 2019 14:28:51 -0400

author
kbarrett
date
Wed, 31 Jul 2019 14:28:51 -0400
changeset 9787
9f28a4cac6d9
parent 8432
79351ea143ee
child 8604
04d83ba48607
permissions
-rw-r--r--

8048556: Unnecessary GCLocker-initiated young GCs
Summary: Fixed recognition of unnecessary GCLocker collections.
Reviewed-by: pliden, tschatzl
Contributed-by: johnc@azul.com

duke@435 1 /*
drchase@6680 2 * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
stefank@2314 27 #include "gc_interface/gcCause.hpp"
stefank@2314 28 #include "memory/collectorPolicy.hpp"
stefank@2314 29 #include "runtime/timer.hpp"
stefank@2314 30 #include "utilities/ostream.hpp"
jmasa@3294 31 #include "utilities/workgroup.hpp"
duke@435 32 elapsedTimer AdaptiveSizePolicy::_minor_timer;
duke@435 33 elapsedTimer AdaptiveSizePolicy::_major_timer;
jmasa@3294 34 bool AdaptiveSizePolicy::_debug_perturbation = false;
duke@435 35
duke@435 36 // The throughput goal is implemented as
duke@435 37 // _throughput_goal = 1 - ( 1 / (1 + gc_cost_ratio))
duke@435 38 // gc_cost_ratio is the ratio
duke@435 39 // application cost / gc cost
duke@435 40 // For example a gc_cost_ratio of 4 translates into a
duke@435 41 // throughput goal of .80
duke@435 42
duke@435 43 AdaptiveSizePolicy::AdaptiveSizePolicy(size_t init_eden_size,
duke@435 44 size_t init_promo_size,
duke@435 45 size_t init_survivor_size,
duke@435 46 double gc_pause_goal_sec,
duke@435 47 uint gc_cost_ratio) :
duke@435 48 _eden_size(init_eden_size),
duke@435 49 _promo_size(init_promo_size),
duke@435 50 _survivor_size(init_survivor_size),
duke@435 51 _gc_pause_goal_sec(gc_pause_goal_sec),
duke@435 52 _throughput_goal(1.0 - double(1.0 / (1.0 + (double) gc_cost_ratio))),
jmasa@1822 53 _gc_overhead_limit_exceeded(false),
jmasa@1822 54 _print_gc_overhead_limit_would_be_exceeded(false),
jmasa@1822 55 _gc_overhead_limit_count(0),
duke@435 56 _latest_minor_mutator_interval_seconds(0),
duke@435 57 _threshold_tolerance_percent(1.0 + ThresholdTolerance/100.0),
duke@435 58 _young_gen_change_for_minor_throughput(0),
duke@435 59 _old_gen_change_for_major_throughput(0) {
jmasa@1822 60 assert(AdaptiveSizePolicyGCTimeLimitThreshold > 0,
jmasa@1822 61 "No opportunity to clear SoftReferences before GC overhead limit");
duke@435 62 _avg_minor_pause =
duke@435 63 new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding);
duke@435 64 _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
duke@435 65 _avg_minor_gc_cost = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
duke@435 66 _avg_major_gc_cost = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
duke@435 67
duke@435 68 _avg_young_live = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
duke@435 69 _avg_old_live = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
duke@435 70 _avg_eden_live = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
duke@435 71
duke@435 72 _avg_survived = new AdaptivePaddedAverage(AdaptiveSizePolicyWeight,
duke@435 73 SurvivorPadding);
duke@435 74 _avg_pretenured = new AdaptivePaddedNoZeroDevAverage(
duke@435 75 AdaptiveSizePolicyWeight,
duke@435 76 SurvivorPadding);
duke@435 77
duke@435 78 _minor_pause_old_estimator =
duke@435 79 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
duke@435 80 _minor_pause_young_estimator =
duke@435 81 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
duke@435 82 _minor_collection_estimator =
duke@435 83 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
duke@435 84 _major_collection_estimator =
duke@435 85 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
duke@435 86
duke@435 87 // Start the timers
duke@435 88 _minor_timer.start();
duke@435 89
duke@435 90 _young_gen_policy_is_ready = false;
duke@435 91 }
duke@435 92
jmasa@3294 93 // If the number of GC threads was set on the command line,
jmasa@3294 94 // use it.
jmasa@3294 95 // Else
jmasa@3294 96 // Calculate the number of GC threads based on the number of Java threads.
jmasa@3294 97 // Calculate the number of GC threads based on the size of the heap.
jmasa@3294 98 // Use the larger.
jmasa@3294 99
jmasa@3294 100 int AdaptiveSizePolicy::calc_default_active_workers(uintx total_workers,
jmasa@3294 101 const uintx min_workers,
jmasa@3294 102 uintx active_workers,
jmasa@3294 103 uintx application_workers) {
jmasa@3294 104 // If the user has specifically set the number of
jmasa@3294 105 // GC threads, use them.
jmasa@3294 106
jmasa@3294 107 // If the user has turned off using a dynamic number of GC threads
jmasa@3294 108 // or the users has requested a specific number, set the active
jmasa@3294 109 // number of workers to all the workers.
jmasa@3294 110
jmasa@3294 111 uintx new_active_workers = total_workers;
jmasa@3294 112 uintx prev_active_workers = active_workers;
jmasa@3294 113 uintx active_workers_by_JT = 0;
jmasa@3294 114 uintx active_workers_by_heap_size = 0;
jmasa@3294 115
jmasa@3294 116 // Always use at least min_workers but use up to
jmasa@3294 117 // GCThreadsPerJavaThreads * application threads.
jmasa@3294 118 active_workers_by_JT =
jmasa@3294 119 MAX2((uintx) GCWorkersPerJavaThread * application_workers,
jmasa@3294 120 min_workers);
jmasa@3294 121
jmasa@3294 122 // Choose a number of GC threads based on the current size
jmasa@3294 123 // of the heap. This may be complicated because the size of
jmasa@3294 124 // the heap depends on factors such as the thoughput goal.
jmasa@3294 125 // Still a large heap should be collected by more GC threads.
jmasa@3294 126 active_workers_by_heap_size =
jmasa@3294 127 MAX2((size_t) 2U, Universe::heap()->capacity() / HeapSizePerGCThread);
jmasa@3294 128
jmasa@3294 129 uintx max_active_workers =
jmasa@3294 130 MAX2(active_workers_by_JT, active_workers_by_heap_size);
jmasa@3294 131
jmasa@3294 132 // Limit the number of workers to the the number created,
jmasa@3294 133 // (workers()).
jmasa@3294 134 new_active_workers = MIN2(max_active_workers,
jmasa@3294 135 (uintx) total_workers);
jmasa@3294 136
jmasa@3294 137 // Increase GC workers instantly but decrease them more
jmasa@3294 138 // slowly.
jmasa@3294 139 if (new_active_workers < prev_active_workers) {
jmasa@3294 140 new_active_workers =
jmasa@3294 141 MAX2(min_workers, (prev_active_workers + new_active_workers) / 2);
jmasa@3294 142 }
jmasa@3294 143
jmasa@3294 144 // Check once more that the number of workers is within the limits.
jmasa@3294 145 assert(min_workers <= total_workers, "Minimum workers not consistent with total workers");
jmasa@3294 146 assert(new_active_workers >= min_workers, "Minimum workers not observed");
jmasa@3294 147 assert(new_active_workers <= total_workers, "Total workers not observed");
jmasa@3294 148
jmasa@3294 149 if (ForceDynamicNumberOfGCThreads) {
jmasa@3294 150 // Assume this is debugging and jiggle the number of GC threads.
jmasa@3294 151 if (new_active_workers == prev_active_workers) {
jmasa@3294 152 if (new_active_workers < total_workers) {
jmasa@3294 153 new_active_workers++;
jmasa@3294 154 } else if (new_active_workers > min_workers) {
jmasa@3294 155 new_active_workers--;
jmasa@3294 156 }
jmasa@3294 157 }
jmasa@3294 158 if (new_active_workers == total_workers) {
jmasa@3294 159 if (_debug_perturbation) {
jmasa@3294 160 new_active_workers = min_workers;
jmasa@3294 161 }
jmasa@3294 162 _debug_perturbation = !_debug_perturbation;
jmasa@3294 163 }
jmasa@3294 164 assert((new_active_workers <= (uintx) ParallelGCThreads) &&
jmasa@3294 165 (new_active_workers >= min_workers),
jmasa@3294 166 "Jiggled active workers too much");
jmasa@3294 167 }
jmasa@3294 168
jmasa@3294 169 if (TraceDynamicGCThreads) {
jmasa@3294 170 gclog_or_tty->print_cr("GCTaskManager::calc_default_active_workers() : "
dbuck@8432 171 "active_workers(): %d new_active_workers: %d "
jmasa@3294 172 "prev_active_workers: %d\n"
jmasa@3294 173 " active_workers_by_JT: %d active_workers_by_heap_size: %d",
drchase@6680 174 (int) active_workers, (int) new_active_workers, (int) prev_active_workers,
drchase@6680 175 (int) active_workers_by_JT, (int) active_workers_by_heap_size);
jmasa@3294 176 }
jmasa@3294 177 assert(new_active_workers > 0, "Always need at least 1");
jmasa@3294 178 return new_active_workers;
jmasa@3294 179 }
jmasa@3294 180
jmasa@3294 181 int AdaptiveSizePolicy::calc_active_workers(uintx total_workers,
jmasa@3294 182 uintx active_workers,
jmasa@3294 183 uintx application_workers) {
jmasa@3294 184 // If the user has specifically set the number of
jmasa@3294 185 // GC threads, use them.
jmasa@3294 186
jmasa@3294 187 // If the user has turned off using a dynamic number of GC threads
jmasa@3294 188 // or the users has requested a specific number, set the active
jmasa@3294 189 // number of workers to all the workers.
jmasa@3294 190
jmasa@3294 191 int new_active_workers;
jmasa@3294 192 if (!UseDynamicNumberOfGCThreads ||
jmasa@3294 193 (!FLAG_IS_DEFAULT(ParallelGCThreads) && !ForceDynamicNumberOfGCThreads)) {
jmasa@3294 194 new_active_workers = total_workers;
jmasa@3294 195 } else {
poonam@8335 196 uintx min_workers = (total_workers == 1) ? 1 : 2;
jmasa@3294 197 new_active_workers = calc_default_active_workers(total_workers,
poonam@8335 198 min_workers,
jmasa@3294 199 active_workers,
jmasa@3294 200 application_workers);
jmasa@3294 201 }
jmasa@3294 202 assert(new_active_workers > 0, "Always need at least 1");
jmasa@3294 203 return new_active_workers;
jmasa@3294 204 }
jmasa@3294 205
jmasa@3294 206 int AdaptiveSizePolicy::calc_active_conc_workers(uintx total_workers,
jmasa@3294 207 uintx active_workers,
jmasa@3294 208 uintx application_workers) {
jmasa@3294 209 if (!UseDynamicNumberOfGCThreads ||
jmasa@3294 210 (!FLAG_IS_DEFAULT(ConcGCThreads) && !ForceDynamicNumberOfGCThreads)) {
jmasa@3294 211 return ConcGCThreads;
jmasa@3294 212 } else {
jmasa@3294 213 int no_of_gc_threads = calc_default_active_workers(
jmasa@3294 214 total_workers,
jmasa@3294 215 1, /* Minimum number of workers */
jmasa@3294 216 active_workers,
jmasa@3294 217 application_workers);
jmasa@3294 218 return no_of_gc_threads;
jmasa@3294 219 }
jmasa@3294 220 }
jmasa@3294 221
duke@435 222 bool AdaptiveSizePolicy::tenuring_threshold_change() const {
duke@435 223 return decrement_tenuring_threshold_for_gc_cost() ||
duke@435 224 increment_tenuring_threshold_for_gc_cost() ||
duke@435 225 decrement_tenuring_threshold_for_survivor_limit();
duke@435 226 }
duke@435 227
duke@435 228 void AdaptiveSizePolicy::minor_collection_begin() {
duke@435 229 // Update the interval time
duke@435 230 _minor_timer.stop();
duke@435 231 // Save most recent collection time
duke@435 232 _latest_minor_mutator_interval_seconds = _minor_timer.seconds();
duke@435 233 _minor_timer.reset();
duke@435 234 _minor_timer.start();
duke@435 235 }
duke@435 236
duke@435 237 void AdaptiveSizePolicy::update_minor_pause_young_estimator(
duke@435 238 double minor_pause_in_ms) {
duke@435 239 double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
duke@435 240 _minor_pause_young_estimator->update(eden_size_in_mbytes,
duke@435 241 minor_pause_in_ms);
duke@435 242 }
duke@435 243
duke@435 244 void AdaptiveSizePolicy::minor_collection_end(GCCause::Cause gc_cause) {
duke@435 245 // Update the pause time.
duke@435 246 _minor_timer.stop();
duke@435 247
duke@435 248 if (gc_cause != GCCause::_java_lang_system_gc ||
duke@435 249 UseAdaptiveSizePolicyWithSystemGC) {
duke@435 250 double minor_pause_in_seconds = _minor_timer.seconds();
duke@435 251 double minor_pause_in_ms = minor_pause_in_seconds * MILLIUNITS;
duke@435 252
duke@435 253 // Sample for performance counter
duke@435 254 _avg_minor_pause->sample(minor_pause_in_seconds);
duke@435 255
duke@435 256 // Cost of collection (unit-less)
duke@435 257 double collection_cost = 0.0;
duke@435 258 if ((_latest_minor_mutator_interval_seconds > 0.0) &&
duke@435 259 (minor_pause_in_seconds > 0.0)) {
duke@435 260 double interval_in_seconds =
duke@435 261 _latest_minor_mutator_interval_seconds + minor_pause_in_seconds;
duke@435 262 collection_cost =
duke@435 263 minor_pause_in_seconds / interval_in_seconds;
duke@435 264 _avg_minor_gc_cost->sample(collection_cost);
duke@435 265 // Sample for performance counter
duke@435 266 _avg_minor_interval->sample(interval_in_seconds);
duke@435 267 }
duke@435 268
duke@435 269 // The policy does not have enough data until at least some
duke@435 270 // minor collections have been done.
duke@435 271 _young_gen_policy_is_ready =
duke@435 272 (_avg_minor_gc_cost->count() >= AdaptiveSizePolicyReadyThreshold);
duke@435 273
duke@435 274 // Calculate variables used to estimate pause time vs. gen sizes
duke@435 275 double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
duke@435 276 update_minor_pause_young_estimator(minor_pause_in_ms);
duke@435 277 update_minor_pause_old_estimator(minor_pause_in_ms);
duke@435 278
duke@435 279 if (PrintAdaptiveSizePolicy && Verbose) {
duke@435 280 gclog_or_tty->print("AdaptiveSizePolicy::minor_collection_end: "
duke@435 281 "minor gc cost: %f average: %f", collection_cost,
duke@435 282 _avg_minor_gc_cost->average());
duke@435 283 gclog_or_tty->print_cr(" minor pause: %f minor period %f",
duke@435 284 minor_pause_in_ms,
duke@435 285 _latest_minor_mutator_interval_seconds * MILLIUNITS);
duke@435 286 }
duke@435 287
duke@435 288 // Calculate variable used to estimate collection cost vs. gen sizes
duke@435 289 assert(collection_cost >= 0.0, "Expected to be non-negative");
duke@435 290 _minor_collection_estimator->update(eden_size_in_mbytes, collection_cost);
duke@435 291 }
duke@435 292
duke@435 293 // Interval times use this timer to measure the mutator time.
duke@435 294 // Reset the timer after the GC pause.
duke@435 295 _minor_timer.reset();
duke@435 296 _minor_timer.start();
duke@435 297 }
duke@435 298
duke@435 299 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden,
duke@435 300 uint percent_change) {
duke@435 301 size_t eden_heap_delta;
duke@435 302 eden_heap_delta = cur_eden / 100 * percent_change;
duke@435 303 return eden_heap_delta;
duke@435 304 }
duke@435 305
duke@435 306 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden) {
duke@435 307 return eden_increment(cur_eden, YoungGenerationSizeIncrement);
duke@435 308 }
duke@435 309
duke@435 310 size_t AdaptiveSizePolicy::eden_decrement(size_t cur_eden) {
duke@435 311 size_t eden_heap_delta = eden_increment(cur_eden) /
duke@435 312 AdaptiveSizeDecrementScaleFactor;
duke@435 313 return eden_heap_delta;
duke@435 314 }
duke@435 315
duke@435 316 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo,
duke@435 317 uint percent_change) {
duke@435 318 size_t promo_heap_delta;
duke@435 319 promo_heap_delta = cur_promo / 100 * percent_change;
duke@435 320 return promo_heap_delta;
duke@435 321 }
duke@435 322
duke@435 323 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo) {
duke@435 324 return promo_increment(cur_promo, TenuredGenerationSizeIncrement);
duke@435 325 }
duke@435 326
duke@435 327 size_t AdaptiveSizePolicy::promo_decrement(size_t cur_promo) {
duke@435 328 size_t promo_heap_delta = promo_increment(cur_promo);
duke@435 329 promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor;
duke@435 330 return promo_heap_delta;
duke@435 331 }
duke@435 332
duke@435 333 double AdaptiveSizePolicy::time_since_major_gc() const {
duke@435 334 _major_timer.stop();
duke@435 335 double result = _major_timer.seconds();
duke@435 336 _major_timer.start();
duke@435 337 return result;
duke@435 338 }
duke@435 339
duke@435 340 // Linear decay of major gc cost
duke@435 341 double AdaptiveSizePolicy::decaying_major_gc_cost() const {
duke@435 342 double major_interval = major_gc_interval_average_for_decay();
duke@435 343 double major_gc_cost_average = major_gc_cost();
duke@435 344 double decayed_major_gc_cost = major_gc_cost_average;
duke@435 345 if(time_since_major_gc() > 0.0) {
duke@435 346 decayed_major_gc_cost = major_gc_cost() *
duke@435 347 (((double) AdaptiveSizeMajorGCDecayTimeScale) * major_interval)
duke@435 348 / time_since_major_gc();
duke@435 349 }
duke@435 350
duke@435 351 // The decayed cost should always be smaller than the
duke@435 352 // average cost but the vagaries of finite arithmetic could
duke@435 353 // produce a larger value in decayed_major_gc_cost so protect
duke@435 354 // against that.
duke@435 355 return MIN2(major_gc_cost_average, decayed_major_gc_cost);
duke@435 356 }
duke@435 357
duke@435 358 // Use a value of the major gc cost that has been decayed
duke@435 359 // by the factor
duke@435 360 //
duke@435 361 // average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale /
duke@435 362 // time-since-last-major-gc
duke@435 363 //
duke@435 364 // if the average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale
duke@435 365 // is less than time-since-last-major-gc.
duke@435 366 //
duke@435 367 // In cases where there are initial major gc's that
duke@435 368 // are of a relatively high cost but no later major
duke@435 369 // gc's, the total gc cost can remain high because
duke@435 370 // the major gc cost remains unchanged (since there are no major
duke@435 371 // gc's). In such a situation the value of the unchanging
duke@435 372 // major gc cost can keep the mutator throughput below
duke@435 373 // the goal when in fact the major gc cost is becoming diminishingly
duke@435 374 // small. Use the decaying gc cost only to decide whether to
duke@435 375 // adjust for throughput. Using it also to determine the adjustment
duke@435 376 // to be made for throughput also seems reasonable but there is
duke@435 377 // no test case to use to decide if it is the right thing to do
duke@435 378 // don't do it yet.
duke@435 379
duke@435 380 double AdaptiveSizePolicy::decaying_gc_cost() const {
duke@435 381 double decayed_major_gc_cost = major_gc_cost();
duke@435 382 double avg_major_interval = major_gc_interval_average_for_decay();
duke@435 383 if (UseAdaptiveSizeDecayMajorGCCost &&
duke@435 384 (AdaptiveSizeMajorGCDecayTimeScale > 0) &&
duke@435 385 (avg_major_interval > 0.00)) {
duke@435 386 double time_since_last_major_gc = time_since_major_gc();
duke@435 387
duke@435 388 // Decay the major gc cost?
duke@435 389 if (time_since_last_major_gc >
duke@435 390 ((double) AdaptiveSizeMajorGCDecayTimeScale) * avg_major_interval) {
duke@435 391
duke@435 392 // Decay using the time-since-last-major-gc
duke@435 393 decayed_major_gc_cost = decaying_major_gc_cost();
duke@435 394 if (PrintGCDetails && Verbose) {
duke@435 395 gclog_or_tty->print_cr("\ndecaying_gc_cost: major interval average:"
duke@435 396 " %f time since last major gc: %f",
duke@435 397 avg_major_interval, time_since_last_major_gc);
duke@435 398 gclog_or_tty->print_cr(" major gc cost: %f decayed major gc cost: %f",
duke@435 399 major_gc_cost(), decayed_major_gc_cost);
duke@435 400 }
duke@435 401 }
duke@435 402 }
duke@435 403 double result = MIN2(1.0, decayed_major_gc_cost + minor_gc_cost());
duke@435 404 return result;
duke@435 405 }
duke@435 406
duke@435 407
duke@435 408 void AdaptiveSizePolicy::clear_generation_free_space_flags() {
duke@435 409 set_change_young_gen_for_min_pauses(0);
duke@435 410 set_change_old_gen_for_maj_pauses(0);
duke@435 411
duke@435 412 set_change_old_gen_for_throughput(0);
duke@435 413 set_change_young_gen_for_throughput(0);
duke@435 414 set_decrease_for_footprint(0);
duke@435 415 set_decide_at_full_gc(0);
duke@435 416 }
duke@435 417
jmasa@1822 418 void AdaptiveSizePolicy::check_gc_overhead_limit(
jmasa@1822 419 size_t young_live,
jmasa@1822 420 size_t eden_live,
jmasa@1822 421 size_t max_old_gen_size,
jmasa@1822 422 size_t max_eden_size,
jmasa@1822 423 bool is_full_gc,
jmasa@1822 424 GCCause::Cause gc_cause,
jmasa@1822 425 CollectorPolicy* collector_policy) {
jmasa@1822 426
jmasa@1822 427 // Ignore explicit GC's. Exiting here does not set the flag and
jmasa@1822 428 // does not reset the count. Updating of the averages for system
jmasa@1822 429 // GC's is still controlled by UseAdaptiveSizePolicyWithSystemGC.
jmasa@1822 430 if (GCCause::is_user_requested_gc(gc_cause) ||
jmasa@1822 431 GCCause::is_serviceability_requested_gc(gc_cause)) {
jmasa@1822 432 return;
jmasa@1822 433 }
jmasa@1822 434 // eden_limit is the upper limit on the size of eden based on
jmasa@1822 435 // the maximum size of the young generation and the sizes
jmasa@1822 436 // of the survivor space.
jmasa@1822 437 // The question being asked is whether the gc costs are high
jmasa@1822 438 // and the space being recovered by a collection is low.
jmasa@1822 439 // free_in_young_gen is the free space in the young generation
jmasa@1822 440 // after a collection and promo_live is the free space in the old
jmasa@1822 441 // generation after a collection.
jmasa@1822 442 //
jmasa@1822 443 // Use the minimum of the current value of the live in the
jmasa@1822 444 // young gen or the average of the live in the young gen.
jmasa@1822 445 // If the current value drops quickly, that should be taken
jmasa@1822 446 // into account (i.e., don't trigger if the amount of free
jmasa@1822 447 // space has suddenly jumped up). If the current is much
jmasa@1822 448 // higher than the average, use the average since it represents
jmasa@1822 449 // the longer term behavor.
jmasa@1822 450 const size_t live_in_eden =
jmasa@1822 451 MIN2(eden_live, (size_t) avg_eden_live()->average());
jmasa@1822 452 const size_t free_in_eden = max_eden_size > live_in_eden ?
jmasa@1822 453 max_eden_size - live_in_eden : 0;
jmasa@1822 454 const size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average());
jmasa@1822 455 const size_t total_free_limit = free_in_old_gen + free_in_eden;
jmasa@1822 456 const size_t total_mem = max_old_gen_size + max_eden_size;
jmasa@1822 457 const double mem_free_limit = total_mem * (GCHeapFreeLimit/100.0);
jmasa@1822 458 const double mem_free_old_limit = max_old_gen_size * (GCHeapFreeLimit/100.0);
jmasa@1822 459 const double mem_free_eden_limit = max_eden_size * (GCHeapFreeLimit/100.0);
jmasa@1822 460 const double gc_cost_limit = GCTimeLimit/100.0;
jmasa@1822 461 size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average());
jmasa@1822 462 // But don't force a promo size below the current promo size. Otherwise,
jmasa@1822 463 // the promo size will shrink for no good reason.
jmasa@1822 464 promo_limit = MAX2(promo_limit, _promo_size);
jmasa@1822 465
jmasa@1822 466
jmasa@1822 467 if (PrintAdaptiveSizePolicy && (Verbose ||
jmasa@1822 468 (free_in_old_gen < (size_t) mem_free_old_limit &&
jmasa@1822 469 free_in_eden < (size_t) mem_free_eden_limit))) {
jmasa@1822 470 gclog_or_tty->print_cr(
tamao@5192 471 "PSAdaptiveSizePolicy::check_gc_overhead_limit:"
jmasa@1822 472 " promo_limit: " SIZE_FORMAT
jmasa@1822 473 " max_eden_size: " SIZE_FORMAT
jmasa@1822 474 " total_free_limit: " SIZE_FORMAT
jmasa@1822 475 " max_old_gen_size: " SIZE_FORMAT
jmasa@1822 476 " max_eden_size: " SIZE_FORMAT
jmasa@1822 477 " mem_free_limit: " SIZE_FORMAT,
jmasa@1822 478 promo_limit, max_eden_size, total_free_limit,
jmasa@1822 479 max_old_gen_size, max_eden_size,
jmasa@1822 480 (size_t) mem_free_limit);
jmasa@1822 481 }
jmasa@1822 482
jmasa@1822 483 bool print_gc_overhead_limit_would_be_exceeded = false;
jmasa@1822 484 if (is_full_gc) {
jmasa@1822 485 if (gc_cost() > gc_cost_limit &&
jmasa@1822 486 free_in_old_gen < (size_t) mem_free_old_limit &&
jmasa@1822 487 free_in_eden < (size_t) mem_free_eden_limit) {
jmasa@1822 488 // Collections, on average, are taking too much time, and
jmasa@1822 489 // gc_cost() > gc_cost_limit
jmasa@1822 490 // we have too little space available after a full gc.
jmasa@1822 491 // total_free_limit < mem_free_limit
jmasa@1822 492 // where
jmasa@1822 493 // total_free_limit is the free space available in
jmasa@1822 494 // both generations
jmasa@1822 495 // total_mem is the total space available for allocation
jmasa@1822 496 // in both generations (survivor spaces are not included
jmasa@1822 497 // just as they are not included in eden_limit).
jmasa@1822 498 // mem_free_limit is a fraction of total_mem judged to be an
jmasa@1822 499 // acceptable amount that is still unused.
jmasa@1822 500 // The heap can ask for the value of this variable when deciding
jmasa@1822 501 // whether to thrown an OutOfMemory error.
jmasa@1822 502 // Note that the gc time limit test only works for the collections
jmasa@1822 503 // of the young gen + tenured gen and not for collections of the
jmasa@1822 504 // permanent gen. That is because the calculation of the space
jmasa@1822 505 // freed by the collection is the free space in the young gen +
jmasa@1822 506 // tenured gen.
jmasa@1822 507 // At this point the GC overhead limit is being exceeded.
jmasa@1822 508 inc_gc_overhead_limit_count();
jmasa@1822 509 if (UseGCOverheadLimit) {
jmasa@1822 510 if (gc_overhead_limit_count() >=
jmasa@1822 511 AdaptiveSizePolicyGCTimeLimitThreshold){
jmasa@1822 512 // All conditions have been met for throwing an out-of-memory
jmasa@1822 513 set_gc_overhead_limit_exceeded(true);
jmasa@1822 514 // Avoid consecutive OOM due to the gc time limit by resetting
jmasa@1822 515 // the counter.
jmasa@1822 516 reset_gc_overhead_limit_count();
jmasa@1822 517 } else {
jmasa@1822 518 // The required consecutive collections which exceed the
jmasa@1822 519 // GC time limit may or may not have been reached. We
jmasa@1822 520 // are approaching that condition and so as not to
jmasa@1822 521 // throw an out-of-memory before all SoftRef's have been
jmasa@1822 522 // cleared, set _should_clear_all_soft_refs in CollectorPolicy.
jmasa@1822 523 // The clearing will be done on the next GC.
jmasa@1822 524 bool near_limit = gc_overhead_limit_near();
jmasa@1822 525 if (near_limit) {
jmasa@1822 526 collector_policy->set_should_clear_all_soft_refs(true);
jmasa@1822 527 if (PrintGCDetails && Verbose) {
jmasa@1822 528 gclog_or_tty->print_cr(" Nearing GC overhead limit, "
jmasa@1822 529 "will be clearing all SoftReference");
jmasa@1822 530 }
jmasa@1822 531 }
jmasa@1822 532 }
jmasa@1822 533 }
jmasa@1822 534 // Set this even when the overhead limit will not
jmasa@1822 535 // cause an out-of-memory. Diagnostic message indicating
jmasa@1822 536 // that the overhead limit is being exceeded is sometimes
jmasa@1822 537 // printed.
jmasa@1822 538 print_gc_overhead_limit_would_be_exceeded = true;
jmasa@1822 539
jmasa@1822 540 } else {
jmasa@1822 541 // Did not exceed overhead limits
jmasa@1822 542 reset_gc_overhead_limit_count();
jmasa@1822 543 }
jmasa@1822 544 }
jmasa@1822 545
jmasa@1822 546 if (UseGCOverheadLimit && PrintGCDetails && Verbose) {
jmasa@1822 547 if (gc_overhead_limit_exceeded()) {
jmasa@1822 548 gclog_or_tty->print_cr(" GC is exceeding overhead limit "
drchase@6680 549 "of %d%%", (int) GCTimeLimit);
jmasa@1822 550 reset_gc_overhead_limit_count();
jmasa@1822 551 } else if (print_gc_overhead_limit_would_be_exceeded) {
jmasa@1822 552 assert(gc_overhead_limit_count() > 0, "Should not be printing");
jmasa@1822 553 gclog_or_tty->print_cr(" GC would exceed overhead limit "
jmasa@1822 554 "of %d%% %d consecutive time(s)",
drchase@6680 555 (int) GCTimeLimit, gc_overhead_limit_count());
jmasa@1822 556 }
jmasa@1822 557 }
jmasa@1822 558 }
duke@435 559 // Printing
duke@435 560
duke@435 561 bool AdaptiveSizePolicy::print_adaptive_size_policy_on(outputStream* st) const {
duke@435 562
duke@435 563 // Should only be used with adaptive size policy turned on.
duke@435 564 // Otherwise, there may be variables that are undefined.
duke@435 565 if (!UseAdaptiveSizePolicy) return false;
duke@435 566
duke@435 567 // Print goal for which action is needed.
duke@435 568 char* action = NULL;
duke@435 569 bool change_for_pause = false;
duke@435 570 if ((change_old_gen_for_maj_pauses() ==
duke@435 571 decrease_old_gen_for_maj_pauses_true) ||
duke@435 572 (change_young_gen_for_min_pauses() ==
duke@435 573 decrease_young_gen_for_min_pauses_true)) {
duke@435 574 action = (char*) " *** pause time goal ***";
duke@435 575 change_for_pause = true;
duke@435 576 } else if ((change_old_gen_for_throughput() ==
duke@435 577 increase_old_gen_for_throughput_true) ||
duke@435 578 (change_young_gen_for_throughput() ==
duke@435 579 increase_young_gen_for_througput_true)) {
duke@435 580 action = (char*) " *** throughput goal ***";
duke@435 581 } else if (decrease_for_footprint()) {
duke@435 582 action = (char*) " *** reduced footprint ***";
duke@435 583 } else {
duke@435 584 // No actions were taken. This can legitimately be the
duke@435 585 // situation if not enough data has been gathered to make
duke@435 586 // decisions.
duke@435 587 return false;
duke@435 588 }
duke@435 589
duke@435 590 // Pauses
duke@435 591 // Currently the size of the old gen is only adjusted to
duke@435 592 // change the major pause times.
duke@435 593 char* young_gen_action = NULL;
duke@435 594 char* tenured_gen_action = NULL;
duke@435 595
duke@435 596 char* shrink_msg = (char*) "(attempted to shrink)";
duke@435 597 char* grow_msg = (char*) "(attempted to grow)";
duke@435 598 char* no_change_msg = (char*) "(no change)";
duke@435 599 if (change_young_gen_for_min_pauses() ==
duke@435 600 decrease_young_gen_for_min_pauses_true) {
duke@435 601 young_gen_action = shrink_msg;
duke@435 602 } else if (change_for_pause) {
duke@435 603 young_gen_action = no_change_msg;
duke@435 604 }
duke@435 605
duke@435 606 if (change_old_gen_for_maj_pauses() == decrease_old_gen_for_maj_pauses_true) {
duke@435 607 tenured_gen_action = shrink_msg;
duke@435 608 } else if (change_for_pause) {
duke@435 609 tenured_gen_action = no_change_msg;
duke@435 610 }
duke@435 611
duke@435 612 // Throughput
duke@435 613 if (change_old_gen_for_throughput() == increase_old_gen_for_throughput_true) {
duke@435 614 assert(change_young_gen_for_throughput() ==
duke@435 615 increase_young_gen_for_througput_true,
duke@435 616 "Both generations should be growing");
duke@435 617 young_gen_action = grow_msg;
duke@435 618 tenured_gen_action = grow_msg;
duke@435 619 } else if (change_young_gen_for_throughput() ==
duke@435 620 increase_young_gen_for_througput_true) {
duke@435 621 // Only the young generation may grow at start up (before
duke@435 622 // enough full collections have been done to grow the old generation).
duke@435 623 young_gen_action = grow_msg;
duke@435 624 tenured_gen_action = no_change_msg;
duke@435 625 }
duke@435 626
duke@435 627 // Minimum footprint
duke@435 628 if (decrease_for_footprint() != 0) {
duke@435 629 young_gen_action = shrink_msg;
duke@435 630 tenured_gen_action = shrink_msg;
duke@435 631 }
duke@435 632
duke@435 633 st->print_cr(" UseAdaptiveSizePolicy actions to meet %s", action);
duke@435 634 st->print_cr(" GC overhead (%%)");
duke@435 635 st->print_cr(" Young generation: %7.2f\t %s",
duke@435 636 100.0 * avg_minor_gc_cost()->average(),
duke@435 637 young_gen_action);
duke@435 638 st->print_cr(" Tenured generation: %7.2f\t %s",
duke@435 639 100.0 * avg_major_gc_cost()->average(),
duke@435 640 tenured_gen_action);
duke@435 641 return true;
duke@435 642 }
duke@435 643
duke@435 644 bool AdaptiveSizePolicy::print_adaptive_size_policy_on(
duke@435 645 outputStream* st,
jwilhelm@4129 646 uint tenuring_threshold_arg) const {
duke@435 647 if (!AdaptiveSizePolicy::print_adaptive_size_policy_on(st)) {
duke@435 648 return false;
duke@435 649 }
duke@435 650
duke@435 651 // Tenuring threshold
duke@435 652 bool tenuring_threshold_changed = true;
duke@435 653 if (decrement_tenuring_threshold_for_survivor_limit()) {
duke@435 654 st->print(" Tenuring threshold: (attempted to decrease to avoid"
duke@435 655 " survivor space overflow) = ");
duke@435 656 } else if (decrement_tenuring_threshold_for_gc_cost()) {
duke@435 657 st->print(" Tenuring threshold: (attempted to decrease to balance"
duke@435 658 " GC costs) = ");
duke@435 659 } else if (increment_tenuring_threshold_for_gc_cost()) {
duke@435 660 st->print(" Tenuring threshold: (attempted to increase to balance"
duke@435 661 " GC costs) = ");
duke@435 662 } else {
duke@435 663 tenuring_threshold_changed = false;
duke@435 664 assert(!tenuring_threshold_change(), "(no change was attempted)");
duke@435 665 }
duke@435 666 if (tenuring_threshold_changed) {
jwilhelm@4129 667 st->print_cr("%u", tenuring_threshold_arg);
duke@435 668 }
duke@435 669 return true;
duke@435 670 }

mercurial