src/share/vm/memory/collectorPolicy.cpp

Thu, 21 Aug 2008 23:36:31 -0400

author
tonyp
date
Thu, 21 Aug 2008 23:36:31 -0400
changeset 791
1ee8caae33af
parent 777
37f87013dfd8
parent 631
d1605aabd0a1
child 1499
473cce303f13
permissions
-rw-r--r--

Merge

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

mercurial