src/share/vm/memory/collectorPolicy.cpp

Thu, 24 Mar 2011 15:47:01 -0700

author
ysr
date
Thu, 24 Mar 2011 15:47:01 -0700
changeset 2710
5134fa1cfe63
parent 2650
dde920245681
child 2853
a1d5f532838d
permissions
-rw-r--r--

7029036: Card-table verification hangs with all framework collectors, except G1, even before the first GC
Summary: When verifying clean card ranges, use memory-range-bounded iteration over oops of objects overlapping that range, thus avoiding the otherwise quadratic worst-case cost of scanning large object arrays.
Reviewed-by: jmasa, jwilhelm, tonyp

duke@435 1 /*
ysr@2650 2 * Copyright (c) 2001, 2011, 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_implementation/shared/gcPolicyCounters.hpp"
stefank@2314 28 #include "gc_implementation/shared/vmGCOperations.hpp"
stefank@2314 29 #include "memory/cardTableRS.hpp"
stefank@2314 30 #include "memory/collectorPolicy.hpp"
stefank@2314 31 #include "memory/gcLocker.inline.hpp"
stefank@2314 32 #include "memory/genCollectedHeap.hpp"
stefank@2314 33 #include "memory/generationSpec.hpp"
stefank@2314 34 #include "memory/space.hpp"
stefank@2314 35 #include "memory/universe.hpp"
stefank@2314 36 #include "runtime/arguments.hpp"
stefank@2314 37 #include "runtime/globals_extension.hpp"
stefank@2314 38 #include "runtime/handles.inline.hpp"
stefank@2314 39 #include "runtime/java.hpp"
stefank@2314 40 #include "runtime/vmThread.hpp"
stefank@2314 41 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 42 # include "thread_linux.inline.hpp"
stefank@2314 43 #endif
stefank@2314 44 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 45 # include "thread_solaris.inline.hpp"
stefank@2314 46 #endif
stefank@2314 47 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 48 # include "thread_windows.inline.hpp"
stefank@2314 49 #endif
stefank@2314 50 #ifndef SERIALGC
stefank@2314 51 #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"
stefank@2314 52 #include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp"
stefank@2314 53 #endif
duke@435 54
duke@435 55 // CollectorPolicy methods.
duke@435 56
duke@435 57 void CollectorPolicy::initialize_flags() {
duke@435 58 if (PermSize > MaxPermSize) {
duke@435 59 MaxPermSize = PermSize;
duke@435 60 }
ysr@777 61 PermSize = MAX2(min_alignment(), align_size_down_(PermSize, min_alignment()));
kvn@2150 62 // Don't increase Perm size limit above specified.
kvn@2150 63 MaxPermSize = align_size_down(MaxPermSize, max_alignment());
kvn@2150 64 if (PermSize > MaxPermSize) {
kvn@2150 65 PermSize = MaxPermSize;
kvn@2150 66 }
duke@435 67
ysr@777 68 MinPermHeapExpansion = MAX2(min_alignment(), align_size_down_(MinPermHeapExpansion, min_alignment()));
ysr@777 69 MaxPermHeapExpansion = MAX2(min_alignment(), align_size_down_(MaxPermHeapExpansion, min_alignment()));
duke@435 70
duke@435 71 MinHeapDeltaBytes = align_size_up(MinHeapDeltaBytes, min_alignment());
duke@435 72
duke@435 73 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment());
duke@435 74 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment());
duke@435 75 SharedMiscDataSize = align_size_up(SharedMiscDataSize, max_alignment());
duke@435 76
duke@435 77 assert(PermSize % min_alignment() == 0, "permanent space alignment");
duke@435 78 assert(MaxPermSize % max_alignment() == 0, "maximum permanent space alignment");
duke@435 79 assert(SharedReadOnlySize % max_alignment() == 0, "read-only space alignment");
duke@435 80 assert(SharedReadWriteSize % max_alignment() == 0, "read-write space alignment");
duke@435 81 assert(SharedMiscDataSize % max_alignment() == 0, "misc-data space alignment");
duke@435 82 if (PermSize < M) {
duke@435 83 vm_exit_during_initialization("Too small initial permanent heap");
duke@435 84 }
duke@435 85 }
duke@435 86
duke@435 87 void CollectorPolicy::initialize_size_info() {
duke@435 88 // User inputs from -mx and ms are aligned
phh@1499 89 set_initial_heap_byte_size(InitialHeapSize);
jmasa@448 90 if (initial_heap_byte_size() == 0) {
jmasa@448 91 set_initial_heap_byte_size(NewSize + OldSize);
duke@435 92 }
ysr@777 93 set_initial_heap_byte_size(align_size_up(_initial_heap_byte_size,
ysr@777 94 min_alignment()));
ysr@777 95
ysr@777 96 set_min_heap_byte_size(Arguments::min_heap_size());
jmasa@448 97 if (min_heap_byte_size() == 0) {
jmasa@448 98 set_min_heap_byte_size(NewSize + OldSize);
duke@435 99 }
ysr@777 100 set_min_heap_byte_size(align_size_up(_min_heap_byte_size,
ysr@777 101 min_alignment()));
ysr@777 102
ysr@777 103 set_max_heap_byte_size(align_size_up(MaxHeapSize, max_alignment()));
duke@435 104
duke@435 105 // Check heap parameter properties
jmasa@448 106 if (initial_heap_byte_size() < M) {
duke@435 107 vm_exit_during_initialization("Too small initial heap");
duke@435 108 }
duke@435 109 // Check heap parameter properties
jmasa@448 110 if (min_heap_byte_size() < M) {
duke@435 111 vm_exit_during_initialization("Too small minimum heap");
duke@435 112 }
jmasa@448 113 if (initial_heap_byte_size() <= NewSize) {
duke@435 114 // make sure there is at least some room in old space
duke@435 115 vm_exit_during_initialization("Too small initial heap for new size specified");
duke@435 116 }
jmasa@448 117 if (max_heap_byte_size() < min_heap_byte_size()) {
duke@435 118 vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
duke@435 119 }
jmasa@448 120 if (initial_heap_byte_size() < min_heap_byte_size()) {
duke@435 121 vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
duke@435 122 }
jmasa@448 123 if (max_heap_byte_size() < initial_heap_byte_size()) {
duke@435 124 vm_exit_during_initialization("Incompatible initial and maximum heap sizes specified");
duke@435 125 }
jmasa@448 126
jmasa@448 127 if (PrintGCDetails && Verbose) {
jmasa@448 128 gclog_or_tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap "
jmasa@448 129 SIZE_FORMAT " Maximum heap " SIZE_FORMAT,
jmasa@448 130 min_heap_byte_size(), initial_heap_byte_size(), max_heap_byte_size());
jmasa@448 131 }
duke@435 132 }
duke@435 133
duke@435 134 void CollectorPolicy::initialize_perm_generation(PermGen::Name pgnm) {
duke@435 135 _permanent_generation =
duke@435 136 new PermanentGenerationSpec(pgnm, PermSize, MaxPermSize,
duke@435 137 SharedReadOnlySize,
duke@435 138 SharedReadWriteSize,
duke@435 139 SharedMiscDataSize,
duke@435 140 SharedMiscCodeSize);
duke@435 141 if (_permanent_generation == NULL) {
duke@435 142 vm_exit_during_initialization("Unable to allocate gen spec");
duke@435 143 }
duke@435 144 }
duke@435 145
jmasa@1822 146 bool CollectorPolicy::use_should_clear_all_soft_refs(bool v) {
jmasa@1822 147 bool result = _should_clear_all_soft_refs;
jmasa@1822 148 set_should_clear_all_soft_refs(false);
jmasa@1822 149 return result;
jmasa@1822 150 }
duke@435 151
duke@435 152 GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap,
duke@435 153 int max_covered_regions) {
duke@435 154 switch (rem_set_name()) {
duke@435 155 case GenRemSet::CardTable: {
duke@435 156 CardTableRS* res = new CardTableRS(whole_heap, max_covered_regions);
duke@435 157 return res;
duke@435 158 }
duke@435 159 default:
duke@435 160 guarantee(false, "unrecognized GenRemSet::Name");
duke@435 161 return NULL;
duke@435 162 }
duke@435 163 }
duke@435 164
jmasa@1822 165 void CollectorPolicy::cleared_all_soft_refs() {
jmasa@1822 166 // If near gc overhear limit, continue to clear SoftRefs. SoftRefs may
jmasa@1822 167 // have been cleared in the last collection but if the gc overhear
jmasa@1822 168 // limit continues to be near, SoftRefs should still be cleared.
jmasa@1822 169 if (size_policy() != NULL) {
jmasa@1822 170 _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near();
jmasa@1822 171 }
jmasa@1822 172 _all_soft_refs_clear = true;
jmasa@1822 173 }
jmasa@1822 174
jmasa@1822 175
duke@435 176 // GenCollectorPolicy methods.
duke@435 177
jmasa@448 178 size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) {
jmasa@448 179 size_t x = base_size / (NewRatio+1);
jmasa@448 180 size_t new_gen_size = x > min_alignment() ?
jmasa@448 181 align_size_down(x, min_alignment()) :
jmasa@448 182 min_alignment();
jmasa@448 183 return new_gen_size;
jmasa@448 184 }
jmasa@448 185
jmasa@448 186 size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size,
jmasa@448 187 size_t maximum_size) {
jmasa@448 188 size_t alignment = min_alignment();
jmasa@448 189 size_t max_minus = maximum_size - alignment;
jmasa@448 190 return desired_size < max_minus ? desired_size : max_minus;
jmasa@448 191 }
jmasa@448 192
jmasa@448 193
duke@435 194 void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
duke@435 195 size_t init_promo_size,
duke@435 196 size_t init_survivor_size) {
jmasa@448 197 const double max_gc_minor_pause_sec = ((double) MaxGCMinorPauseMillis)/1000.0;
duke@435 198 _size_policy = new AdaptiveSizePolicy(init_eden_size,
duke@435 199 init_promo_size,
duke@435 200 init_survivor_size,
duke@435 201 max_gc_minor_pause_sec,
duke@435 202 GCTimeRatio);
duke@435 203 }
duke@435 204
duke@435 205 size_t GenCollectorPolicy::compute_max_alignment() {
duke@435 206 // The card marking array and the offset arrays for old generations are
duke@435 207 // committed in os pages as well. Make sure they are entirely full (to
duke@435 208 // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
duke@435 209 // byte entry and the os page size is 4096, the maximum heap size should
duke@435 210 // be 512*4096 = 2MB aligned.
duke@435 211 size_t alignment = GenRemSet::max_alignment_constraint(rem_set_name());
duke@435 212
duke@435 213 // Parallel GC does its own alignment of the generations to avoid requiring a
duke@435 214 // large page (256M on some platforms) for the permanent generation. The
duke@435 215 // other collectors should also be updated to do their own alignment and then
duke@435 216 // this use of lcm() should be removed.
duke@435 217 if (UseLargePages && !UseParallelGC) {
duke@435 218 // in presence of large pages we have to make sure that our
duke@435 219 // alignment is large page aware
duke@435 220 alignment = lcm(os::large_page_size(), alignment);
duke@435 221 }
duke@435 222
duke@435 223 return alignment;
duke@435 224 }
duke@435 225
duke@435 226 void GenCollectorPolicy::initialize_flags() {
duke@435 227 // All sizes must be multiples of the generation granularity.
duke@435 228 set_min_alignment((uintx) Generation::GenGrain);
duke@435 229 set_max_alignment(compute_max_alignment());
duke@435 230 assert(max_alignment() >= min_alignment() &&
duke@435 231 max_alignment() % min_alignment() == 0,
duke@435 232 "invalid alignment constraints");
duke@435 233
duke@435 234 CollectorPolicy::initialize_flags();
duke@435 235
duke@435 236 // All generational heaps have a youngest gen; handle those flags here.
duke@435 237
duke@435 238 // Adjust max size parameters
duke@435 239 if (NewSize > MaxNewSize) {
duke@435 240 MaxNewSize = NewSize;
duke@435 241 }
duke@435 242 NewSize = align_size_down(NewSize, min_alignment());
duke@435 243 MaxNewSize = align_size_down(MaxNewSize, min_alignment());
duke@435 244
duke@435 245 // Check validity of heap flags
duke@435 246 assert(NewSize % min_alignment() == 0, "eden space alignment");
duke@435 247 assert(MaxNewSize % min_alignment() == 0, "survivor space alignment");
duke@435 248
duke@435 249 if (NewSize < 3*min_alignment()) {
duke@435 250 // make sure there room for eden and two survivor spaces
duke@435 251 vm_exit_during_initialization("Too small new size specified");
duke@435 252 }
duke@435 253 if (SurvivorRatio < 1 || NewRatio < 1) {
duke@435 254 vm_exit_during_initialization("Invalid heap ratio specified");
duke@435 255 }
duke@435 256 }
duke@435 257
duke@435 258 void TwoGenerationCollectorPolicy::initialize_flags() {
duke@435 259 GenCollectorPolicy::initialize_flags();
duke@435 260
duke@435 261 OldSize = align_size_down(OldSize, min_alignment());
duke@435 262 if (NewSize + OldSize > MaxHeapSize) {
duke@435 263 MaxHeapSize = NewSize + OldSize;
duke@435 264 }
duke@435 265 MaxHeapSize = align_size_up(MaxHeapSize, max_alignment());
duke@435 266
duke@435 267 always_do_update_barrier = UseConcMarkSweepGC;
duke@435 268 BlockOffsetArrayUseUnallocatedBlock =
duke@435 269 BlockOffsetArrayUseUnallocatedBlock || ParallelGCThreads > 0;
duke@435 270
duke@435 271 // Check validity of heap flags
duke@435 272 assert(OldSize % min_alignment() == 0, "old space alignment");
duke@435 273 assert(MaxHeapSize % max_alignment() == 0, "maximum heap alignment");
duke@435 274 }
duke@435 275
jmasa@448 276 // Values set on the command line win over any ergonomically
jmasa@448 277 // set command line parameters.
jmasa@448 278 // Ergonomic choice of parameters are done before this
jmasa@448 279 // method is called. Values for command line parameters such as NewSize
jmasa@448 280 // and MaxNewSize feed those ergonomic choices into this method.
jmasa@448 281 // This method makes the final generation sizings consistent with
jmasa@448 282 // themselves and with overall heap sizings.
jmasa@448 283 // In the absence of explicitly set command line flags, policies
jmasa@448 284 // such as the use of NewRatio are used to size the generation.
duke@435 285 void GenCollectorPolicy::initialize_size_info() {
duke@435 286 CollectorPolicy::initialize_size_info();
duke@435 287
jmasa@448 288 // min_alignment() is used for alignment within a generation.
jmasa@448 289 // There is additional alignment done down stream for some
jmasa@448 290 // collectors that sometimes causes unwanted rounding up of
jmasa@448 291 // generations sizes.
jmasa@448 292
jmasa@448 293 // Determine maximum size of gen0
jmasa@448 294
jmasa@448 295 size_t max_new_size = 0;
ysr@2650 296 if (FLAG_IS_CMDLINE(MaxNewSize) || FLAG_IS_ERGO(MaxNewSize)) {
jmasa@448 297 if (MaxNewSize < min_alignment()) {
jmasa@448 298 max_new_size = min_alignment();
ysr@2650 299 }
ysr@2650 300 if (MaxNewSize >= max_heap_byte_size()) {
jmasa@448 301 max_new_size = align_size_down(max_heap_byte_size() - min_alignment(),
jmasa@448 302 min_alignment());
jmasa@448 303 warning("MaxNewSize (" SIZE_FORMAT "k) is equal to or "
jmasa@448 304 "greater than the entire heap (" SIZE_FORMAT "k). A "
jmasa@448 305 "new generation size of " SIZE_FORMAT "k will be used.",
jmasa@448 306 MaxNewSize/K, max_heap_byte_size()/K, max_new_size/K);
jmasa@448 307 } else {
jmasa@448 308 max_new_size = align_size_down(MaxNewSize, min_alignment());
jmasa@448 309 }
jmasa@448 310
jmasa@448 311 // The case for FLAG_IS_ERGO(MaxNewSize) could be treated
jmasa@448 312 // specially at this point to just use an ergonomically set
jmasa@448 313 // MaxNewSize to set max_new_size. For cases with small
jmasa@448 314 // heaps such a policy often did not work because the MaxNewSize
jmasa@448 315 // was larger than the entire heap. The interpretation given
jmasa@448 316 // to ergonomically set flags is that the flags are set
jmasa@448 317 // by different collectors for their own special needs but
jmasa@448 318 // are not allowed to badly shape the heap. This allows the
jmasa@448 319 // different collectors to decide what's best for themselves
jmasa@448 320 // without having to factor in the overall heap shape. It
jmasa@448 321 // can be the case in the future that the collectors would
jmasa@448 322 // only make "wise" ergonomics choices and this policy could
jmasa@448 323 // just accept those choices. The choices currently made are
jmasa@448 324 // not always "wise".
duke@435 325 } else {
jmasa@448 326 max_new_size = scale_by_NewRatio_aligned(max_heap_byte_size());
jmasa@448 327 // Bound the maximum size by NewSize below (since it historically
duke@435 328 // would have been NewSize and because the NewRatio calculation could
duke@435 329 // yield a size that is too small) and bound it by MaxNewSize above.
jmasa@448 330 // Ergonomics plays here by previously calculating the desired
jmasa@448 331 // NewSize and MaxNewSize.
jmasa@448 332 max_new_size = MIN2(MAX2(max_new_size, NewSize), MaxNewSize);
jmasa@448 333 }
jmasa@448 334 assert(max_new_size > 0, "All paths should set max_new_size");
jmasa@448 335
jmasa@448 336 // Given the maximum gen0 size, determine the initial and
ysr@2650 337 // minimum gen0 sizes.
jmasa@448 338
jmasa@448 339 if (max_heap_byte_size() == min_heap_byte_size()) {
jmasa@448 340 // The maximum and minimum heap sizes are the same so
jmasa@448 341 // the generations minimum and initial must be the
jmasa@448 342 // same as its maximum.
jmasa@448 343 set_min_gen0_size(max_new_size);
jmasa@448 344 set_initial_gen0_size(max_new_size);
jmasa@448 345 set_max_gen0_size(max_new_size);
jmasa@448 346 } else {
jmasa@448 347 size_t desired_new_size = 0;
jmasa@448 348 if (!FLAG_IS_DEFAULT(NewSize)) {
jmasa@448 349 // If NewSize is set ergonomically (for example by cms), it
jmasa@448 350 // would make sense to use it. If it is used, also use it
jmasa@448 351 // to set the initial size. Although there is no reason
jmasa@448 352 // the minimum size and the initial size have to be the same,
jmasa@448 353 // the current implementation gets into trouble during the calculation
jmasa@448 354 // of the tenured generation sizes if they are different.
jmasa@448 355 // Note that this makes the initial size and the minimum size
jmasa@448 356 // generally small compared to the NewRatio calculation.
jmasa@448 357 _min_gen0_size = NewSize;
jmasa@448 358 desired_new_size = NewSize;
jmasa@448 359 max_new_size = MAX2(max_new_size, NewSize);
jmasa@448 360 } else {
jmasa@448 361 // For the case where NewSize is the default, use NewRatio
jmasa@448 362 // to size the minimum and initial generation sizes.
jmasa@448 363 // Use the default NewSize as the floor for these values. If
jmasa@448 364 // NewRatio is overly large, the resulting sizes can be too
jmasa@448 365 // small.
jmasa@448 366 _min_gen0_size = MAX2(scale_by_NewRatio_aligned(min_heap_byte_size()),
jmasa@448 367 NewSize);
jmasa@448 368 desired_new_size =
jmasa@448 369 MAX2(scale_by_NewRatio_aligned(initial_heap_byte_size()),
jmasa@448 370 NewSize);
jmasa@448 371 }
jmasa@448 372
jmasa@448 373 assert(_min_gen0_size > 0, "Sanity check");
jmasa@448 374 set_initial_gen0_size(desired_new_size);
jmasa@448 375 set_max_gen0_size(max_new_size);
jmasa@448 376
jmasa@448 377 // At this point the desirable initial and minimum sizes have been
jmasa@448 378 // determined without regard to the maximum sizes.
jmasa@448 379
jmasa@448 380 // Bound the sizes by the corresponding overall heap sizes.
jmasa@448 381 set_min_gen0_size(
jmasa@448 382 bound_minus_alignment(_min_gen0_size, min_heap_byte_size()));
jmasa@448 383 set_initial_gen0_size(
jmasa@448 384 bound_minus_alignment(_initial_gen0_size, initial_heap_byte_size()));
jmasa@448 385 set_max_gen0_size(
jmasa@448 386 bound_minus_alignment(_max_gen0_size, max_heap_byte_size()));
jmasa@448 387
jmasa@448 388 // At this point all three sizes have been checked against the
jmasa@448 389 // maximum sizes but have not been checked for consistency
ysr@777 390 // among the three.
jmasa@448 391
jmasa@448 392 // Final check min <= initial <= max
jmasa@448 393 set_min_gen0_size(MIN2(_min_gen0_size, _max_gen0_size));
jmasa@448 394 set_initial_gen0_size(
jmasa@448 395 MAX2(MIN2(_initial_gen0_size, _max_gen0_size), _min_gen0_size));
jmasa@448 396 set_min_gen0_size(MIN2(_min_gen0_size, _initial_gen0_size));
duke@435 397 }
duke@435 398
jmasa@448 399 if (PrintGCDetails && Verbose) {
ysr@2650 400 gclog_or_tty->print_cr("1: Minimum gen0 " SIZE_FORMAT " Initial gen0 "
jmasa@448 401 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
jmasa@448 402 min_gen0_size(), initial_gen0_size(), max_gen0_size());
jmasa@448 403 }
jmasa@448 404 }
duke@435 405
jmasa@448 406 // Call this method during the sizing of the gen1 to make
jmasa@448 407 // adjustments to gen0 because of gen1 sizing policy. gen0 initially has
jmasa@448 408 // the most freedom in sizing because it is done before the
jmasa@448 409 // policy for gen1 is applied. Once gen1 policies have been applied,
jmasa@448 410 // there may be conflicts in the shape of the heap and this method
jmasa@448 411 // is used to make the needed adjustments. The application of the
jmasa@448 412 // policies could be more sophisticated (iterative for example) but
jmasa@448 413 // keeping it simple also seems a worthwhile goal.
jmasa@448 414 bool TwoGenerationCollectorPolicy::adjust_gen0_sizes(size_t* gen0_size_ptr,
jmasa@448 415 size_t* gen1_size_ptr,
jmasa@448 416 size_t heap_size,
jmasa@448 417 size_t min_gen0_size) {
jmasa@448 418 bool result = false;
jmasa@448 419 if ((*gen1_size_ptr + *gen0_size_ptr) > heap_size) {
jmasa@448 420 if (((*gen0_size_ptr + OldSize) > heap_size) &&
jmasa@448 421 (heap_size - min_gen0_size) >= min_alignment()) {
jmasa@448 422 // Adjust gen0 down to accomodate OldSize
jmasa@448 423 *gen0_size_ptr = heap_size - min_gen0_size;
jmasa@448 424 *gen0_size_ptr =
jmasa@448 425 MAX2((uintx)align_size_down(*gen0_size_ptr, min_alignment()),
jmasa@448 426 min_alignment());
jmasa@448 427 assert(*gen0_size_ptr > 0, "Min gen0 is too large");
jmasa@448 428 result = true;
jmasa@448 429 } else {
jmasa@448 430 *gen1_size_ptr = heap_size - *gen0_size_ptr;
jmasa@448 431 *gen1_size_ptr =
jmasa@448 432 MAX2((uintx)align_size_down(*gen1_size_ptr, min_alignment()),
jmasa@448 433 min_alignment());
jmasa@448 434 }
jmasa@448 435 }
jmasa@448 436 return result;
jmasa@448 437 }
duke@435 438
jmasa@448 439 // Minimum sizes of the generations may be different than
jmasa@448 440 // the initial sizes. An inconsistently is permitted here
jmasa@448 441 // in the total size that can be specified explicitly by
jmasa@448 442 // command line specification of OldSize and NewSize and
jmasa@448 443 // also a command line specification of -Xms. Issue a warning
jmasa@448 444 // but allow the values to pass.
duke@435 445
duke@435 446 void TwoGenerationCollectorPolicy::initialize_size_info() {
duke@435 447 GenCollectorPolicy::initialize_size_info();
duke@435 448
jmasa@448 449 // At this point the minimum, initial and maximum sizes
jmasa@448 450 // of the overall heap and of gen0 have been determined.
jmasa@448 451 // The maximum gen1 size can be determined from the maximum gen0
ysr@2650 452 // and maximum heap size since no explicit flags exits
jmasa@448 453 // for setting the gen1 maximum.
jmasa@448 454 _max_gen1_size = max_heap_byte_size() - _max_gen0_size;
jmasa@448 455 _max_gen1_size =
jmasa@448 456 MAX2((uintx)align_size_down(_max_gen1_size, min_alignment()),
jmasa@448 457 min_alignment());
jmasa@448 458 // If no explicit command line flag has been set for the
jmasa@448 459 // gen1 size, use what is left for gen1.
jmasa@448 460 if (FLAG_IS_DEFAULT(OldSize) || FLAG_IS_ERGO(OldSize)) {
jmasa@448 461 // The user has not specified any value or ergonomics
jmasa@448 462 // has chosen a value (which may or may not be consistent
jmasa@448 463 // with the overall heap size). In either case make
jmasa@448 464 // the minimum, maximum and initial sizes consistent
jmasa@448 465 // with the gen0 sizes and the overall heap sizes.
jmasa@448 466 assert(min_heap_byte_size() > _min_gen0_size,
jmasa@448 467 "gen0 has an unexpected minimum size");
jmasa@448 468 set_min_gen1_size(min_heap_byte_size() - min_gen0_size());
jmasa@448 469 set_min_gen1_size(
jmasa@448 470 MAX2((uintx)align_size_down(_min_gen1_size, min_alignment()),
jmasa@448 471 min_alignment()));
jmasa@448 472 set_initial_gen1_size(initial_heap_byte_size() - initial_gen0_size());
jmasa@448 473 set_initial_gen1_size(
jmasa@448 474 MAX2((uintx)align_size_down(_initial_gen1_size, min_alignment()),
jmasa@448 475 min_alignment()));
jmasa@448 476
jmasa@448 477 } else {
jmasa@448 478 // It's been explicitly set on the command line. Use the
jmasa@448 479 // OldSize and then determine the consequences.
jmasa@448 480 set_min_gen1_size(OldSize);
jmasa@448 481 set_initial_gen1_size(OldSize);
jmasa@448 482
jmasa@448 483 // If the user has explicitly set an OldSize that is inconsistent
jmasa@448 484 // with other command line flags, issue a warning.
duke@435 485 // The generation minimums and the overall heap mimimum should
duke@435 486 // be within one heap alignment.
jmasa@448 487 if ((_min_gen1_size + _min_gen0_size + min_alignment()) <
jmasa@448 488 min_heap_byte_size()) {
duke@435 489 warning("Inconsistency between minimum heap size and minimum "
jmasa@448 490 "generation sizes: using minimum heap = " SIZE_FORMAT,
jmasa@448 491 min_heap_byte_size());
duke@435 492 }
jmasa@448 493 if ((OldSize > _max_gen1_size)) {
jmasa@448 494 warning("Inconsistency between maximum heap size and maximum "
jmasa@448 495 "generation sizes: using maximum heap = " SIZE_FORMAT
jmasa@448 496 " -XX:OldSize flag is being ignored",
jmasa@448 497 max_heap_byte_size());
ysr@2650 498 }
jmasa@448 499 // If there is an inconsistency between the OldSize and the minimum and/or
jmasa@448 500 // initial size of gen0, since OldSize was explicitly set, OldSize wins.
jmasa@448 501 if (adjust_gen0_sizes(&_min_gen0_size, &_min_gen1_size,
jmasa@448 502 min_heap_byte_size(), OldSize)) {
jmasa@448 503 if (PrintGCDetails && Verbose) {
ysr@2650 504 gclog_or_tty->print_cr("2: Minimum gen0 " SIZE_FORMAT " Initial gen0 "
jmasa@448 505 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
jmasa@448 506 min_gen0_size(), initial_gen0_size(), max_gen0_size());
jmasa@448 507 }
jmasa@448 508 }
jmasa@448 509 // Initial size
jmasa@448 510 if (adjust_gen0_sizes(&_initial_gen0_size, &_initial_gen1_size,
jmasa@448 511 initial_heap_byte_size(), OldSize)) {
jmasa@448 512 if (PrintGCDetails && Verbose) {
ysr@2650 513 gclog_or_tty->print_cr("3: Minimum gen0 " SIZE_FORMAT " Initial gen0 "
jmasa@448 514 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
jmasa@448 515 min_gen0_size(), initial_gen0_size(), max_gen0_size());
jmasa@448 516 }
jmasa@448 517 }
jmasa@448 518 }
jmasa@448 519 // Enforce the maximum gen1 size.
jmasa@448 520 set_min_gen1_size(MIN2(_min_gen1_size, _max_gen1_size));
duke@435 521
jmasa@448 522 // Check that min gen1 <= initial gen1 <= max gen1
jmasa@448 523 set_initial_gen1_size(MAX2(_initial_gen1_size, _min_gen1_size));
jmasa@448 524 set_initial_gen1_size(MIN2(_initial_gen1_size, _max_gen1_size));
jmasa@448 525
jmasa@448 526 if (PrintGCDetails && Verbose) {
jmasa@448 527 gclog_or_tty->print_cr("Minimum gen1 " SIZE_FORMAT " Initial gen1 "
jmasa@448 528 SIZE_FORMAT " Maximum gen1 " SIZE_FORMAT,
jmasa@448 529 min_gen1_size(), initial_gen1_size(), max_gen1_size());
jmasa@448 530 }
duke@435 531 }
duke@435 532
duke@435 533 HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size,
duke@435 534 bool is_tlab,
duke@435 535 bool* gc_overhead_limit_was_exceeded) {
duke@435 536 GenCollectedHeap *gch = GenCollectedHeap::heap();
duke@435 537
duke@435 538 debug_only(gch->check_for_valid_allocation_state());
duke@435 539 assert(gch->no_gc_in_progress(), "Allocation during gc not allowed");
jmasa@1822 540
jmasa@1822 541 // In general gc_overhead_limit_was_exceeded should be false so
jmasa@1822 542 // set it so here and reset it to true only if the gc time
jmasa@1822 543 // limit is being exceeded as checked below.
jmasa@1822 544 *gc_overhead_limit_was_exceeded = false;
jmasa@1822 545
duke@435 546 HeapWord* result = NULL;
duke@435 547
duke@435 548 // Loop until the allocation is satisified,
duke@435 549 // or unsatisfied after GC.
duke@435 550 for (int try_count = 1; /* return or throw */; try_count += 1) {
duke@435 551 HandleMark hm; // discard any handles allocated in each iteration
duke@435 552
duke@435 553 // First allocation attempt is lock-free.
duke@435 554 Generation *gen0 = gch->get_gen(0);
duke@435 555 assert(gen0->supports_inline_contig_alloc(),
duke@435 556 "Otherwise, must do alloc within heap lock");
duke@435 557 if (gen0->should_allocate(size, is_tlab)) {
duke@435 558 result = gen0->par_allocate(size, is_tlab);
duke@435 559 if (result != NULL) {
duke@435 560 assert(gch->is_in_reserved(result), "result not in heap");
duke@435 561 return result;
duke@435 562 }
duke@435 563 }
duke@435 564 unsigned int gc_count_before; // read inside the Heap_lock locked region
duke@435 565 {
duke@435 566 MutexLocker ml(Heap_lock);
duke@435 567 if (PrintGC && Verbose) {
duke@435 568 gclog_or_tty->print_cr("TwoGenerationCollectorPolicy::mem_allocate_work:"
duke@435 569 " attempting locked slow path allocation");
duke@435 570 }
duke@435 571 // Note that only large objects get a shot at being
duke@435 572 // allocated in later generations.
duke@435 573 bool first_only = ! should_try_older_generation_allocation(size);
duke@435 574
duke@435 575 result = gch->attempt_allocation(size, is_tlab, first_only);
duke@435 576 if (result != NULL) {
duke@435 577 assert(gch->is_in_reserved(result), "result not in heap");
duke@435 578 return result;
duke@435 579 }
duke@435 580
duke@435 581 if (GC_locker::is_active_and_needs_gc()) {
duke@435 582 if (is_tlab) {
duke@435 583 return NULL; // Caller will retry allocating individual object
duke@435 584 }
duke@435 585 if (!gch->is_maximal_no_gc()) {
duke@435 586 // Try and expand heap to satisfy request
duke@435 587 result = expand_heap_and_allocate(size, is_tlab);
duke@435 588 // result could be null if we are out of space
duke@435 589 if (result != NULL) {
duke@435 590 return result;
duke@435 591 }
duke@435 592 }
duke@435 593
duke@435 594 // If this thread is not in a jni critical section, we stall
duke@435 595 // the requestor until the critical section has cleared and
duke@435 596 // GC allowed. When the critical section clears, a GC is
duke@435 597 // initiated by the last thread exiting the critical section; so
duke@435 598 // we retry the allocation sequence from the beginning of the loop,
duke@435 599 // rather than causing more, now probably unnecessary, GC attempts.
duke@435 600 JavaThread* jthr = JavaThread::current();
duke@435 601 if (!jthr->in_critical()) {
duke@435 602 MutexUnlocker mul(Heap_lock);
duke@435 603 // Wait for JNI critical section to be exited
duke@435 604 GC_locker::stall_until_clear();
duke@435 605 continue;
duke@435 606 } else {
duke@435 607 if (CheckJNICalls) {
duke@435 608 fatal("Possible deadlock due to allocating while"
duke@435 609 " in jni critical section");
duke@435 610 }
duke@435 611 return NULL;
duke@435 612 }
duke@435 613 }
duke@435 614
duke@435 615 // Read the gc count while the heap lock is held.
duke@435 616 gc_count_before = Universe::heap()->total_collections();
duke@435 617 }
duke@435 618
duke@435 619 VM_GenCollectForAllocation op(size,
duke@435 620 is_tlab,
duke@435 621 gc_count_before);
duke@435 622 VMThread::execute(&op);
duke@435 623 if (op.prologue_succeeded()) {
duke@435 624 result = op.result();
duke@435 625 if (op.gc_locked()) {
duke@435 626 assert(result == NULL, "must be NULL if gc_locked() is true");
duke@435 627 continue; // retry and/or stall as necessary
duke@435 628 }
jmasa@1822 629
jmasa@1822 630 // Allocation has failed and a collection
jmasa@1822 631 // has been done. If the gc time limit was exceeded the
jmasa@1822 632 // this time, return NULL so that an out-of-memory
jmasa@1822 633 // will be thrown. Clear gc_overhead_limit_exceeded
jmasa@1822 634 // so that the overhead exceeded does not persist.
jmasa@1822 635
jmasa@1822 636 const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
jmasa@1822 637 const bool softrefs_clear = all_soft_refs_clear();
jmasa@1822 638 assert(!limit_exceeded || softrefs_clear, "Should have been cleared");
jmasa@1822 639 if (limit_exceeded && softrefs_clear) {
jmasa@1822 640 *gc_overhead_limit_was_exceeded = true;
jmasa@1822 641 size_policy()->set_gc_overhead_limit_exceeded(false);
jmasa@1822 642 if (op.result() != NULL) {
jmasa@1822 643 CollectedHeap::fill_with_object(op.result(), size);
jmasa@1822 644 }
jmasa@1822 645 return NULL;
jmasa@1822 646 }
duke@435 647 assert(result == NULL || gch->is_in_reserved(result),
duke@435 648 "result not in heap");
duke@435 649 return result;
duke@435 650 }
duke@435 651
duke@435 652 // Give a warning if we seem to be looping forever.
duke@435 653 if ((QueuedAllocationWarningCount > 0) &&
duke@435 654 (try_count % QueuedAllocationWarningCount == 0)) {
duke@435 655 warning("TwoGenerationCollectorPolicy::mem_allocate_work retries %d times \n\t"
duke@435 656 " size=%d %s", try_count, size, is_tlab ? "(TLAB)" : "");
duke@435 657 }
duke@435 658 }
duke@435 659 }
duke@435 660
duke@435 661 HeapWord* GenCollectorPolicy::expand_heap_and_allocate(size_t size,
duke@435 662 bool is_tlab) {
duke@435 663 GenCollectedHeap *gch = GenCollectedHeap::heap();
duke@435 664 HeapWord* result = NULL;
duke@435 665 for (int i = number_of_generations() - 1; i >= 0 && result == NULL; i--) {
duke@435 666 Generation *gen = gch->get_gen(i);
duke@435 667 if (gen->should_allocate(size, is_tlab)) {
duke@435 668 result = gen->expand_and_allocate(size, is_tlab);
duke@435 669 }
duke@435 670 }
duke@435 671 assert(result == NULL || gch->is_in_reserved(result), "result not in heap");
duke@435 672 return result;
duke@435 673 }
duke@435 674
duke@435 675 HeapWord* GenCollectorPolicy::satisfy_failed_allocation(size_t size,
duke@435 676 bool is_tlab) {
duke@435 677 GenCollectedHeap *gch = GenCollectedHeap::heap();
duke@435 678 GCCauseSetter x(gch, GCCause::_allocation_failure);
duke@435 679 HeapWord* result = NULL;
duke@435 680
duke@435 681 assert(size != 0, "Precondition violated");
duke@435 682 if (GC_locker::is_active_and_needs_gc()) {
duke@435 683 // GC locker is active; instead of a collection we will attempt
duke@435 684 // to expand the heap, if there's room for expansion.
duke@435 685 if (!gch->is_maximal_no_gc()) {
duke@435 686 result = expand_heap_and_allocate(size, is_tlab);
duke@435 687 }
duke@435 688 return result; // could be null if we are out of space
ysr@2336 689 } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) {
duke@435 690 // Do an incremental collection.
duke@435 691 gch->do_collection(false /* full */,
duke@435 692 false /* clear_all_soft_refs */,
duke@435 693 size /* size */,
duke@435 694 is_tlab /* is_tlab */,
duke@435 695 number_of_generations() - 1 /* max_level */);
duke@435 696 } else {
ysr@2336 697 if (Verbose && PrintGCDetails) {
ysr@2336 698 gclog_or_tty->print(" :: Trying full because partial may fail :: ");
ysr@2336 699 }
duke@435 700 // Try a full collection; see delta for bug id 6266275
duke@435 701 // for the original code and why this has been simplified
duke@435 702 // with from-space allocation criteria modified and
duke@435 703 // such allocation moved out of the safepoint path.
duke@435 704 gch->do_collection(true /* full */,
duke@435 705 false /* clear_all_soft_refs */,
duke@435 706 size /* size */,
duke@435 707 is_tlab /* is_tlab */,
duke@435 708 number_of_generations() - 1 /* max_level */);
duke@435 709 }
duke@435 710
duke@435 711 result = gch->attempt_allocation(size, is_tlab, false /*first_only*/);
duke@435 712
duke@435 713 if (result != NULL) {
duke@435 714 assert(gch->is_in_reserved(result), "result not in heap");
duke@435 715 return result;
duke@435 716 }
duke@435 717
duke@435 718 // OK, collection failed, try expansion.
duke@435 719 result = expand_heap_and_allocate(size, is_tlab);
duke@435 720 if (result != NULL) {
duke@435 721 return result;
duke@435 722 }
duke@435 723
duke@435 724 // If we reach this point, we're really out of memory. Try every trick
duke@435 725 // we can to reclaim memory. Force collection of soft references. Force
duke@435 726 // a complete compaction of the heap. Any additional methods for finding
duke@435 727 // free memory should be here, especially if they are expensive. If this
duke@435 728 // attempt fails, an OOM exception will be thrown.
duke@435 729 {
duke@435 730 IntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted
duke@435 731
duke@435 732 gch->do_collection(true /* full */,
duke@435 733 true /* clear_all_soft_refs */,
duke@435 734 size /* size */,
duke@435 735 is_tlab /* is_tlab */,
duke@435 736 number_of_generations() - 1 /* max_level */);
duke@435 737 }
duke@435 738
duke@435 739 result = gch->attempt_allocation(size, is_tlab, false /* first_only */);
duke@435 740 if (result != NULL) {
duke@435 741 assert(gch->is_in_reserved(result), "result not in heap");
duke@435 742 return result;
duke@435 743 }
duke@435 744
jmasa@1822 745 assert(!should_clear_all_soft_refs(),
jmasa@1822 746 "Flag should have been handled and cleared prior to this point");
jmasa@1822 747
duke@435 748 // What else? We might try synchronous finalization later. If the total
duke@435 749 // space available is large enough for the allocation, then a more
duke@435 750 // complete compaction phase than we've tried so far might be
duke@435 751 // appropriate.
duke@435 752 return NULL;
duke@435 753 }
duke@435 754
duke@435 755 size_t GenCollectorPolicy::large_typearray_limit() {
duke@435 756 return FastAllocateSizeLimit;
duke@435 757 }
duke@435 758
duke@435 759 // Return true if any of the following is true:
duke@435 760 // . the allocation won't fit into the current young gen heap
duke@435 761 // . gc locker is occupied (jni critical section)
duke@435 762 // . heap memory is tight -- the most recent previous collection
duke@435 763 // was a full collection because a partial collection (would
duke@435 764 // have) failed and is likely to fail again
duke@435 765 bool GenCollectorPolicy::should_try_older_generation_allocation(
duke@435 766 size_t word_size) const {
duke@435 767 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 768 size_t gen0_capacity = gch->get_gen(0)->capacity_before_gc();
duke@435 769 return (word_size > heap_word_size(gen0_capacity))
ysr@2243 770 || GC_locker::is_active_and_needs_gc()
ysr@2243 771 || gch->incremental_collection_failed();
duke@435 772 }
duke@435 773
duke@435 774
duke@435 775 //
duke@435 776 // MarkSweepPolicy methods
duke@435 777 //
duke@435 778
duke@435 779 MarkSweepPolicy::MarkSweepPolicy() {
duke@435 780 initialize_all();
duke@435 781 }
duke@435 782
duke@435 783 void MarkSweepPolicy::initialize_generations() {
duke@435 784 initialize_perm_generation(PermGen::MarkSweepCompact);
duke@435 785 _generations = new GenerationSpecPtr[number_of_generations()];
duke@435 786 if (_generations == NULL)
duke@435 787 vm_exit_during_initialization("Unable to allocate gen spec");
duke@435 788
duke@435 789 if (UseParNewGC && ParallelGCThreads > 0) {
duke@435 790 _generations[0] = new GenerationSpec(Generation::ParNew, _initial_gen0_size, _max_gen0_size);
duke@435 791 } else {
duke@435 792 _generations[0] = new GenerationSpec(Generation::DefNew, _initial_gen0_size, _max_gen0_size);
duke@435 793 }
duke@435 794 _generations[1] = new GenerationSpec(Generation::MarkSweepCompact, _initial_gen1_size, _max_gen1_size);
duke@435 795
duke@435 796 if (_generations[0] == NULL || _generations[1] == NULL)
duke@435 797 vm_exit_during_initialization("Unable to allocate gen spec");
duke@435 798 }
duke@435 799
duke@435 800 void MarkSweepPolicy::initialize_gc_policy_counters() {
duke@435 801 // initialize the policy counters - 2 collectors, 3 generations
duke@435 802 if (UseParNewGC && ParallelGCThreads > 0) {
duke@435 803 _gc_policy_counters = new GCPolicyCounters("ParNew:MSC", 2, 3);
duke@435 804 }
duke@435 805 else {
duke@435 806 _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3);
duke@435 807 }
duke@435 808 }

mercurial