annotate src/share/vm/memory/collectorPolicy.cpp @ 5655:3ad2b68d107e

8027911: Assertion in the collector policy when running gc/arguments/TestMaxNewSize.java Summary: Update NewSize when _initial_gen0_size is changed Reviewed-by: tschatzl, brutisso
author jwilhelm
date Sun, 10 Nov 2013 00:07:29 +0100
parents 8f07aa079343
children 236cecd9ec97
rev   line source
duke@0 1 /*
jwilhelm@4119 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
stefank@1879 27 #include "gc_implementation/shared/gcPolicyCounters.hpp"
stefank@1879 28 #include "gc_implementation/shared/vmGCOperations.hpp"
stefank@1879 29 #include "memory/cardTableRS.hpp"
stefank@1879 30 #include "memory/collectorPolicy.hpp"
stefank@1879 31 #include "memory/gcLocker.inline.hpp"
stefank@1879 32 #include "memory/genCollectedHeap.hpp"
stefank@1879 33 #include "memory/generationSpec.hpp"
stefank@1879 34 #include "memory/space.hpp"
stefank@1879 35 #include "memory/universe.hpp"
stefank@1879 36 #include "runtime/arguments.hpp"
stefank@1879 37 #include "runtime/globals_extension.hpp"
stefank@1879 38 #include "runtime/handles.inline.hpp"
stefank@1879 39 #include "runtime/java.hpp"
stefank@3864 40 #include "runtime/thread.inline.hpp"
stefank@1879 41 #include "runtime/vmThread.hpp"
jprovino@4107 42 #include "utilities/macros.hpp"
jprovino@4107 43 #if INCLUDE_ALL_GCS
stefank@1879 44 #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"
stefank@1879 45 #include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp"
jprovino@4107 46 #endif // INCLUDE_ALL_GCS
duke@0 47
duke@0 48 // CollectorPolicy methods.
duke@0 49
jwilhelm@5650 50 CollectorPolicy::CollectorPolicy() :
jwilhelm@5650 51 _space_alignment(0),
jwilhelm@5650 52 _heap_alignment(0),
jwilhelm@5650 53 _initial_heap_byte_size(InitialHeapSize),
jwilhelm@5650 54 _max_heap_byte_size(MaxHeapSize),
jwilhelm@5650 55 _min_heap_byte_size(Arguments::min_heap_size()),
jwilhelm@5650 56 _max_heap_size_cmdline(false),
jwilhelm@5650 57 _size_policy(NULL),
jwilhelm@5650 58 _should_clear_all_soft_refs(false),
jwilhelm@5650 59 _all_soft_refs_clear(false)
jwilhelm@5650 60 {}
jwilhelm@5650 61
jwilhelm@5650 62 #ifdef ASSERT
jwilhelm@5650 63 void CollectorPolicy::assert_flags() {
jwilhelm@5650 64 assert(InitialHeapSize <= MaxHeapSize, "Ergonomics decided on incompatible initial and maximum heap sizes");
jwilhelm@5650 65 assert(InitialHeapSize % _heap_alignment == 0, "InitialHeapSize alignment");
jwilhelm@5650 66 assert(MaxHeapSize % _heap_alignment == 0, "MaxHeapSize alignment");
jwilhelm@5650 67 }
jwilhelm@5650 68
jwilhelm@5650 69 void CollectorPolicy::assert_size_info() {
jwilhelm@5650 70 assert(InitialHeapSize == _initial_heap_byte_size, "Discrepancy between InitialHeapSize flag and local storage");
jwilhelm@5650 71 assert(MaxHeapSize == _max_heap_byte_size, "Discrepancy between MaxHeapSize flag and local storage");
jwilhelm@5650 72 assert(_max_heap_byte_size >= _min_heap_byte_size, "Ergonomics decided on incompatible minimum and maximum heap sizes");
jwilhelm@5650 73 assert(_initial_heap_byte_size >= _min_heap_byte_size, "Ergonomics decided on incompatible initial and minimum heap sizes");
jwilhelm@5650 74 assert(_max_heap_byte_size >= _initial_heap_byte_size, "Ergonomics decided on incompatible initial and maximum heap sizes");
jwilhelm@5650 75 assert(_min_heap_byte_size % _heap_alignment == 0, "min_heap_byte_size alignment");
jwilhelm@5650 76 assert(_initial_heap_byte_size % _heap_alignment == 0, "initial_heap_byte_size alignment");
jwilhelm@5650 77 assert(_max_heap_byte_size % _heap_alignment == 0, "max_heap_byte_size alignment");
jwilhelm@5650 78 }
jwilhelm@5650 79 #endif // ASSERT
jwilhelm@5650 80
duke@0 81 void CollectorPolicy::initialize_flags() {
jwilhelm@5650 82 assert(_space_alignment != 0, "Space alignment not set up properly");
jwilhelm@5650 83 assert(_heap_alignment != 0, "Heap alignment not set up properly");
jwilhelm@5650 84 assert(_heap_alignment >= _space_alignment,
jwilhelm@5650 85 err_msg("heap_alignment: " SIZE_FORMAT " less than space_alignment: " SIZE_FORMAT,
jwilhelm@5650 86 _heap_alignment, _space_alignment));
jwilhelm@5650 87 assert(_heap_alignment % _space_alignment == 0,
jwilhelm@5650 88 err_msg("heap_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
jwilhelm@5650 89 _heap_alignment, _space_alignment));
brutisso@4636 90
jwilhelm@5650 91 if (FLAG_IS_CMDLINE(MaxHeapSize)) {
jwilhelm@5650 92 if (FLAG_IS_CMDLINE(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
jwilhelm@5650 93 vm_exit_during_initialization("Initial heap size set to a larger value than the maximum heap size");
jwilhelm@5650 94 }
jwilhelm@5650 95 if (_min_heap_byte_size != 0 && MaxHeapSize < _min_heap_byte_size) {
jwilhelm@5650 96 vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified");
jwilhelm@5650 97 }
jwilhelm@5650 98 _max_heap_size_cmdline = true;
tschatzl@4638 99 }
tschatzl@4638 100
jwilhelm@5650 101 // Check heap parameter properties
jwilhelm@5650 102 if (InitialHeapSize < M) {
jwilhelm@5650 103 vm_exit_during_initialization("Too small initial heap");
jwilhelm@5650 104 }
jwilhelm@5650 105 if (_min_heap_byte_size < M) {
jwilhelm@5650 106 vm_exit_during_initialization("Too small minimum heap");
jwilhelm@5650 107 }
jwilhelm@5650 108
jwilhelm@5650 109 // User inputs from -Xmx and -Xms must be aligned
jwilhelm@5650 110 _min_heap_byte_size = align_size_up(_min_heap_byte_size, _heap_alignment);
jwilhelm@5650 111 uintx aligned_initial_heap_size = align_size_up(InitialHeapSize, _heap_alignment);
jwilhelm@5650 112 uintx aligned_max_heap_size = align_size_up(MaxHeapSize, _heap_alignment);
jwilhelm@5650 113
jwilhelm@5650 114 // Write back to flags if the values changed
jwilhelm@5650 115 if (aligned_initial_heap_size != InitialHeapSize) {
jwilhelm@5650 116 FLAG_SET_ERGO(uintx, InitialHeapSize, aligned_initial_heap_size);
jwilhelm@5650 117 }
jwilhelm@5650 118 if (aligned_max_heap_size != MaxHeapSize) {
jwilhelm@5650 119 FLAG_SET_ERGO(uintx, MaxHeapSize, aligned_max_heap_size);
jwilhelm@5650 120 }
jwilhelm@5650 121
jwilhelm@5650 122 if (FLAG_IS_CMDLINE(InitialHeapSize) && _min_heap_byte_size != 0 &&
jwilhelm@5650 123 InitialHeapSize < _min_heap_byte_size) {
jwilhelm@5650 124 vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified");
jwilhelm@5650 125 }
jwilhelm@5650 126 if (!FLAG_IS_DEFAULT(InitialHeapSize) && InitialHeapSize > MaxHeapSize) {
jwilhelm@5650 127 FLAG_SET_ERGO(uintx, MaxHeapSize, InitialHeapSize);
jwilhelm@5650 128 } else if (!FLAG_IS_DEFAULT(MaxHeapSize) && InitialHeapSize > MaxHeapSize) {
jwilhelm@5650 129 FLAG_SET_ERGO(uintx, InitialHeapSize, MaxHeapSize);
jwilhelm@5650 130 if (InitialHeapSize < _min_heap_byte_size) {
jwilhelm@5650 131 _min_heap_byte_size = InitialHeapSize;
jwilhelm@5650 132 }
jwilhelm@5650 133 }
jwilhelm@5650 134
jwilhelm@5650 135 _initial_heap_byte_size = InitialHeapSize;
jwilhelm@5650 136 _max_heap_byte_size = MaxHeapSize;
jwilhelm@5650 137
jwilhelm@5650 138 FLAG_SET_ERGO(uintx, MinHeapDeltaBytes, align_size_up(MinHeapDeltaBytes, _space_alignment));
jwilhelm@5650 139
jwilhelm@5650 140 DEBUG_ONLY(CollectorPolicy::assert_flags();)
duke@0 141 }
duke@0 142
duke@0 143 void CollectorPolicy::initialize_size_info() {
jmasa@13 144 if (PrintGCDetails && Verbose) {
jmasa@13 145 gclog_or_tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap "
jmasa@13 146 SIZE_FORMAT " Maximum heap " SIZE_FORMAT,
jwilhelm@5420 147 _min_heap_byte_size, _initial_heap_byte_size, _max_heap_byte_size);
jmasa@13 148 }
jwilhelm@5650 149
jwilhelm@5650 150 DEBUG_ONLY(CollectorPolicy::assert_size_info();)
duke@0 151 }
duke@0 152
jmasa@1387 153 bool CollectorPolicy::use_should_clear_all_soft_refs(bool v) {
jmasa@1387 154 bool result = _should_clear_all_soft_refs;
jmasa@1387 155 set_should_clear_all_soft_refs(false);
jmasa@1387 156 return result;
jmasa@1387 157 }
duke@0 158
duke@0 159 GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap,
duke@0 160 int max_covered_regions) {
jwilhelm@5383 161 return new CardTableRS(whole_heap, max_covered_regions);
duke@0 162 }
duke@0 163
jmasa@1387 164 void CollectorPolicy::cleared_all_soft_refs() {
jmasa@1387 165 // If near gc overhear limit, continue to clear SoftRefs. SoftRefs may
jmasa@1387 166 // have been cleared in the last collection but if the gc overhear
jmasa@1387 167 // limit continues to be near, SoftRefs should still be cleared.
jmasa@1387 168 if (size_policy() != NULL) {
jmasa@1387 169 _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near();
jmasa@1387 170 }
jmasa@1387 171 _all_soft_refs_clear = true;
jmasa@1387 172 }
jmasa@1387 173
jwilhelm@5650 174 size_t CollectorPolicy::compute_heap_alignment() {
tschatzl@5266 175 // The card marking array and the offset arrays for old generations are
tschatzl@5266 176 // committed in os pages as well. Make sure they are entirely full (to
tschatzl@5266 177 // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
tschatzl@5266 178 // byte entry and the os page size is 4096, the maximum heap size should
tschatzl@5266 179 // be 512*4096 = 2MB aligned.
tschatzl@5266 180
tschatzl@5266 181 // There is only the GenRemSet in Hotspot and only the GenRemSet::CardTable
tschatzl@5266 182 // is supported.
tschatzl@5266 183 // Requirements of any new remembered set implementations must be added here.
tschatzl@5266 184 size_t alignment = GenRemSet::max_alignment_constraint(GenRemSet::CardTable);
tschatzl@5266 185
tschatzl@5266 186 // Parallel GC does its own alignment of the generations to avoid requiring a
tschatzl@5266 187 // large page (256M on some platforms) for the permanent generation. The
tschatzl@5266 188 // other collectors should also be updated to do their own alignment and then
tschatzl@5266 189 // this use of lcm() should be removed.
tschatzl@5266 190 if (UseLargePages && !UseParallelGC) {
tschatzl@5266 191 // in presence of large pages we have to make sure that our
tschatzl@5266 192 // alignment is large page aware
tschatzl@5266 193 alignment = lcm(os::large_page_size(), alignment);
tschatzl@5266 194 }
tschatzl@5266 195
tschatzl@5266 196 return alignment;
tschatzl@5266 197 }
jmasa@1387 198
duke@0 199 // GenCollectorPolicy methods.
duke@0 200
jwilhelm@5650 201 GenCollectorPolicy::GenCollectorPolicy() :
jwilhelm@5650 202 _min_gen0_size(0),
jwilhelm@5650 203 _initial_gen0_size(0),
jwilhelm@5650 204 _max_gen0_size(0),
jwilhelm@5650 205 _gen_alignment(0),
jwilhelm@5650 206 _generations(NULL)
jwilhelm@5650 207 {}
jwilhelm@5650 208
jmasa@13 209 size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) {
jwilhelm@5650 210 return align_size_down_bounded(base_size / (NewRatio + 1), _gen_alignment);
jmasa@13 211 }
jmasa@13 212
jmasa@13 213 size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size,
jmasa@13 214 size_t maximum_size) {
jwilhelm@5650 215 size_t max_minus = maximum_size - _gen_alignment;
jmasa@13 216 return desired_size < max_minus ? desired_size : max_minus;
jmasa@13 217 }
jmasa@13 218
jmasa@13 219
duke@0 220 void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
duke@0 221 size_t init_promo_size,
duke@0 222 size_t init_survivor_size) {
jwilhelm@5649 223 const double max_gc_pause_sec = ((double) MaxGCPauseMillis) / 1000.0;
duke@0 224 _size_policy = new AdaptiveSizePolicy(init_eden_size,
duke@0 225 init_promo_size,
duke@0 226 init_survivor_size,
tamao@4178 227 max_gc_pause_sec,
duke@0 228 GCTimeRatio);
duke@0 229 }
duke@0 230
jwilhelm@5650 231 size_t GenCollectorPolicy::young_gen_size_lower_bound() {
jwilhelm@5650 232 // The young generation must be aligned and have room for eden + two survivors
jwilhelm@5650 233 return align_size_up(3 * _space_alignment, _gen_alignment);
jwilhelm@5650 234 }
jwilhelm@5650 235
jwilhelm@5650 236 #ifdef ASSERT
jwilhelm@5650 237 void GenCollectorPolicy::assert_flags() {
jwilhelm@5650 238 CollectorPolicy::assert_flags();
jwilhelm@5650 239 assert(NewSize >= _min_gen0_size, "Ergonomics decided on a too small young gen size");
jwilhelm@5650 240 assert(NewSize <= MaxNewSize, "Ergonomics decided on incompatible initial and maximum young gen sizes");
jwilhelm@5650 241 assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young gen and heap sizes");
jwilhelm@5650 242 assert(NewSize % _gen_alignment == 0, "NewSize alignment");
jwilhelm@5650 243 assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize % _gen_alignment == 0, "MaxNewSize alignment");
jwilhelm@5650 244 }
jwilhelm@5650 245
jwilhelm@5650 246 void TwoGenerationCollectorPolicy::assert_flags() {
jwilhelm@5650 247 GenCollectorPolicy::assert_flags();
jwilhelm@5650 248 assert(OldSize + NewSize <= MaxHeapSize, "Ergonomics decided on incompatible generation and heap sizes");
jwilhelm@5650 249 assert(OldSize % _gen_alignment == 0, "OldSize alignment");
jwilhelm@5650 250 }
jwilhelm@5650 251
jwilhelm@5650 252 void GenCollectorPolicy::assert_size_info() {
jwilhelm@5650 253 CollectorPolicy::assert_size_info();
jwilhelm@5650 254 // GenCollectorPolicy::initialize_size_info may update the MaxNewSize
jwilhelm@5650 255 assert(MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young and heap sizes");
jwilhelm@5650 256 assert(NewSize == _initial_gen0_size, "Discrepancy between NewSize flag and local storage");
jwilhelm@5650 257 assert(MaxNewSize == _max_gen0_size, "Discrepancy between MaxNewSize flag and local storage");
jwilhelm@5650 258 assert(_min_gen0_size <= _initial_gen0_size, "Ergonomics decided on incompatible minimum and initial young gen sizes");
jwilhelm@5650 259 assert(_initial_gen0_size <= _max_gen0_size, "Ergonomics decided on incompatible initial and maximum young gen sizes");
jwilhelm@5650 260 assert(_min_gen0_size % _gen_alignment == 0, "_min_gen0_size alignment");
jwilhelm@5650 261 assert(_initial_gen0_size % _gen_alignment == 0, "_initial_gen0_size alignment");
jwilhelm@5650 262 assert(_max_gen0_size % _gen_alignment == 0, "_max_gen0_size alignment");
jwilhelm@5650 263 }
jwilhelm@5650 264
jwilhelm@5650 265 void TwoGenerationCollectorPolicy::assert_size_info() {
jwilhelm@5650 266 GenCollectorPolicy::assert_size_info();
jwilhelm@5650 267 assert(OldSize == _initial_gen1_size, "Discrepancy between OldSize flag and local storage");
jwilhelm@5650 268 assert(_min_gen1_size <= _initial_gen1_size, "Ergonomics decided on incompatible minimum and initial old gen sizes");
jwilhelm@5650 269 assert(_initial_gen1_size <= _max_gen1_size, "Ergonomics decided on incompatible initial and maximum old gen sizes");
jwilhelm@5650 270 assert(_max_gen1_size % _gen_alignment == 0, "_max_gen1_size alignment");
jwilhelm@5650 271 assert(_initial_gen1_size % _gen_alignment == 0, "_initial_gen1_size alignment");
jwilhelm@5650 272 assert(_max_heap_byte_size <= (_max_gen0_size + _max_gen1_size), "Total maximum heap sizes must be sum of generation maximum sizes");
jwilhelm@5650 273 }
jwilhelm@5650 274 #endif // ASSERT
jwilhelm@5650 275
duke@0 276 void GenCollectorPolicy::initialize_flags() {
duke@0 277 CollectorPolicy::initialize_flags();
duke@0 278
jwilhelm@5650 279 assert(_gen_alignment != 0, "Generation alignment not set up properly");
jwilhelm@5650 280 assert(_heap_alignment >= _gen_alignment,
jwilhelm@5650 281 err_msg("heap_alignment: " SIZE_FORMAT " less than gen_alignment: " SIZE_FORMAT,
jwilhelm@5650 282 _heap_alignment, _gen_alignment));
jwilhelm@5650 283 assert(_gen_alignment % _space_alignment == 0,
jwilhelm@5650 284 err_msg("gen_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
jwilhelm@5650 285 _gen_alignment, _space_alignment));
jwilhelm@5650 286 assert(_heap_alignment % _gen_alignment == 0,
jwilhelm@5650 287 err_msg("heap_alignment: " SIZE_FORMAT " not aligned by gen_alignment: " SIZE_FORMAT,
jwilhelm@5650 288 _heap_alignment, _gen_alignment));
duke@0 289
jwilhelm@5650 290 // All generational heaps have a youngest gen; handle those flags here
jwilhelm@5650 291
jwilhelm@5650 292 // Make sure the heap is large enough for two generations
jwilhelm@5650 293 uintx smallest_new_size = young_gen_size_lower_bound();
jwilhelm@5650 294 uintx smallest_heap_size = align_size_up(smallest_new_size + align_size_up(_space_alignment, _gen_alignment),
jwilhelm@5650 295 _heap_alignment);
jwilhelm@5650 296 if (MaxHeapSize < smallest_heap_size) {
jwilhelm@5650 297 FLAG_SET_ERGO(uintx, MaxHeapSize, smallest_heap_size);
jwilhelm@5650 298 _max_heap_byte_size = MaxHeapSize;
jwilhelm@5650 299 }
jwilhelm@5650 300 // If needed, synchronize _min_heap_byte size and _initial_heap_byte_size
jwilhelm@5650 301 if (_min_heap_byte_size < smallest_heap_size) {
jwilhelm@5650 302 _min_heap_byte_size = smallest_heap_size;
jwilhelm@5650 303 if (InitialHeapSize < _min_heap_byte_size) {
jwilhelm@5650 304 FLAG_SET_ERGO(uintx, InitialHeapSize, smallest_heap_size);
jwilhelm@5650 305 _initial_heap_byte_size = smallest_heap_size;
jwilhelm@5650 306 }
jwilhelm@5650 307 }
jwilhelm@5650 308
jwilhelm@5650 309 // Now take the actual NewSize into account. We will silently increase NewSize
jwilhelm@5650 310 // if the user specified a smaller value.
jwilhelm@5650 311 smallest_new_size = MAX2(smallest_new_size, (uintx)align_size_down(NewSize, _gen_alignment));
jwilhelm@5650 312 if (smallest_new_size != NewSize) {
jwilhelm@5650 313 FLAG_SET_ERGO(uintx, NewSize, smallest_new_size);
jwilhelm@5650 314 }
jwilhelm@5650 315 _initial_gen0_size = NewSize;
jwilhelm@5650 316
jwilhelm@5650 317 if (!FLAG_IS_DEFAULT(MaxNewSize)) {
jwilhelm@5650 318 uintx min_new_size = MAX2(_gen_alignment, _min_gen0_size);
jwilhelm@5650 319
jwilhelm@5650 320 if (MaxNewSize >= MaxHeapSize) {
jwilhelm@5650 321 // Make sure there is room for an old generation
jwilhelm@5650 322 uintx smaller_max_new_size = MaxHeapSize - _gen_alignment;
jwilhelm@5650 323 if (FLAG_IS_CMDLINE(MaxNewSize)) {
jwilhelm@5650 324 warning("MaxNewSize (" SIZE_FORMAT "k) is equal to or greater than the entire "
jwilhelm@5650 325 "heap (" SIZE_FORMAT "k). A new max generation size of " SIZE_FORMAT "k will be used.",
jwilhelm@5650 326 MaxNewSize/K, MaxHeapSize/K, smaller_max_new_size/K);
jwilhelm@5650 327 }
jwilhelm@5650 328 FLAG_SET_ERGO(uintx, MaxNewSize, smaller_max_new_size);
jwilhelm@5650 329 if (NewSize > MaxNewSize) {
jwilhelm@5650 330 FLAG_SET_ERGO(uintx, NewSize, MaxNewSize);
jwilhelm@5650 331 _initial_gen0_size = NewSize;
jwilhelm@5650 332 }
jwilhelm@5650 333 } else if (MaxNewSize < min_new_size) {
jwilhelm@5650 334 FLAG_SET_ERGO(uintx, MaxNewSize, min_new_size);
jwilhelm@5650 335 } else if (!is_size_aligned(MaxNewSize, _gen_alignment)) {
jwilhelm@5650 336 FLAG_SET_ERGO(uintx, MaxNewSize, align_size_down(MaxNewSize, _gen_alignment));
jwilhelm@5650 337 }
jwilhelm@5650 338 _max_gen0_size = MaxNewSize;
jwilhelm@5650 339 }
jwilhelm@5650 340
duke@0 341 if (NewSize > MaxNewSize) {
jwilhelm@5650 342 // At this point this should only happen if the user specifies a large NewSize and/or
jwilhelm@5650 343 // a small (but not too small) MaxNewSize.
jwilhelm@5650 344 if (FLAG_IS_CMDLINE(MaxNewSize)) {
jwilhelm@5650 345 warning("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). "
jwilhelm@5650 346 "A new max generation size of " SIZE_FORMAT "k will be used.",
jwilhelm@5650 347 NewSize/K, MaxNewSize/K, NewSize/K);
jwilhelm@5650 348 }
jwilhelm@5650 349 FLAG_SET_ERGO(uintx, MaxNewSize, NewSize);
jwilhelm@5650 350 _max_gen0_size = MaxNewSize;
duke@0 351 }
jwilhelm@5649 352
duke@0 353 if (SurvivorRatio < 1 || NewRatio < 1) {
jwilhelm@5421 354 vm_exit_during_initialization("Invalid young gen ratio specified");
duke@0 355 }
jwilhelm@5650 356
jwilhelm@5650 357 DEBUG_ONLY(GenCollectorPolicy::assert_flags();)
duke@0 358 }
duke@0 359
duke@0 360 void TwoGenerationCollectorPolicy::initialize_flags() {
duke@0 361 GenCollectorPolicy::initialize_flags();
duke@0 362
jwilhelm@5650 363 if (!is_size_aligned(OldSize, _gen_alignment)) {
jwilhelm@5650 364 FLAG_SET_ERGO(uintx, OldSize, align_size_down(OldSize, _gen_alignment));
jwilhelm@5650 365 }
jwilhelm@4119 366
jwilhelm@5650 367 if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(MaxHeapSize)) {
jwilhelm@4119 368 // NewRatio will be used later to set the young generation size so we use
jwilhelm@4119 369 // it to calculate how big the heap should be based on the requested OldSize
jwilhelm@4119 370 // and NewRatio.
jwilhelm@4119 371 assert(NewRatio > 0, "NewRatio should have been set up earlier");
jwilhelm@4119 372 size_t calculated_heapsize = (OldSize / NewRatio) * (NewRatio + 1);
jwilhelm@4119 373
jwilhelm@5650 374 calculated_heapsize = align_size_up(calculated_heapsize, _heap_alignment);
jwilhelm@5650 375 FLAG_SET_ERGO(uintx, MaxHeapSize, calculated_heapsize);
jwilhelm@5650 376 _max_heap_byte_size = MaxHeapSize;
jwilhelm@5650 377 FLAG_SET_ERGO(uintx, InitialHeapSize, calculated_heapsize);
jwilhelm@5650 378 _initial_heap_byte_size = InitialHeapSize;
jwilhelm@4119 379 }
duke@0 380
tschatzl@4638 381 // adjust max heap size if necessary
tschatzl@4638 382 if (NewSize + OldSize > MaxHeapSize) {
jwilhelm@5650 383 if (_max_heap_size_cmdline) {
tschatzl@4638 384 // somebody set a maximum heap size with the intention that we should not
tschatzl@4638 385 // exceed it. Adjust New/OldSize as necessary.
tschatzl@4638 386 uintx calculated_size = NewSize + OldSize;
tschatzl@4638 387 double shrink_factor = (double) MaxHeapSize / calculated_size;
jwilhelm@5650 388 uintx smaller_new_size = align_size_down((uintx)(NewSize * shrink_factor), _gen_alignment);
jwilhelm@5650 389 FLAG_SET_ERGO(uintx, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size));
jwilhelm@5650 390 _initial_gen0_size = NewSize;
jwilhelm@5650 391
tschatzl@4638 392 // OldSize is already aligned because above we aligned MaxHeapSize to
jwilhelm@5650 393 // _heap_alignment, and we just made sure that NewSize is aligned to
jwilhelm@5650 394 // _gen_alignment. In initialize_flags() we verified that _heap_alignment
jwilhelm@5650 395 // is a multiple of _gen_alignment.
jwilhelm@5650 396 FLAG_SET_ERGO(uintx, OldSize, MaxHeapSize - NewSize);
tschatzl@4638 397 } else {
jwilhelm@5650 398 FLAG_SET_ERGO(uintx, MaxHeapSize, align_size_up(NewSize + OldSize, _heap_alignment));
jwilhelm@5650 399 _max_heap_byte_size = MaxHeapSize;
tschatzl@4638 400 }
tschatzl@4638 401 }
tschatzl@4681 402
duke@0 403 always_do_update_barrier = UseConcMarkSweepGC;
duke@0 404
jwilhelm@5650 405 DEBUG_ONLY(TwoGenerationCollectorPolicy::assert_flags();)
duke@0 406 }
duke@0 407
jmasa@13 408 // Values set on the command line win over any ergonomically
jmasa@13 409 // set command line parameters.
jmasa@13 410 // Ergonomic choice of parameters are done before this
jmasa@13 411 // method is called. Values for command line parameters such as NewSize
jmasa@13 412 // and MaxNewSize feed those ergonomic choices into this method.
jmasa@13 413 // This method makes the final generation sizings consistent with
jmasa@13 414 // themselves and with overall heap sizings.
jmasa@13 415 // In the absence of explicitly set command line flags, policies
jmasa@13 416 // such as the use of NewRatio are used to size the generation.
duke@0 417 void GenCollectorPolicy::initialize_size_info() {
duke@0 418 CollectorPolicy::initialize_size_info();
duke@0 419
jwilhelm@5650 420 // _space_alignment is used for alignment within a generation.
jmasa@13 421 // There is additional alignment done down stream for some
jmasa@13 422 // collectors that sometimes causes unwanted rounding up of
jmasa@13 423 // generations sizes.
jmasa@13 424
jmasa@13 425 // Determine maximum size of gen0
jmasa@13 426
jmasa@13 427 size_t max_new_size = 0;
jwilhelm@5650 428 if (!FLAG_IS_DEFAULT(MaxNewSize)) {
jwilhelm@5650 429 max_new_size = MaxNewSize;
duke@0 430 } else {
jwilhelm@5420 431 max_new_size = scale_by_NewRatio_aligned(_max_heap_byte_size);
jmasa@13 432 // Bound the maximum size by NewSize below (since it historically
duke@0 433 // would have been NewSize and because the NewRatio calculation could
duke@0 434 // yield a size that is too small) and bound it by MaxNewSize above.
jmasa@13 435 // Ergonomics plays here by previously calculating the desired
jmasa@13 436 // NewSize and MaxNewSize.
jmasa@13 437 max_new_size = MIN2(MAX2(max_new_size, NewSize), MaxNewSize);
jmasa@13 438 }
jmasa@13 439 assert(max_new_size > 0, "All paths should set max_new_size");
jmasa@13 440
jmasa@13 441 // Given the maximum gen0 size, determine the initial and
ysr@2215 442 // minimum gen0 sizes.
jmasa@13 443
jwilhelm@5420 444 if (_max_heap_byte_size == _min_heap_byte_size) {
jmasa@13 445 // The maximum and minimum heap sizes are the same so
jmasa@13 446 // the generations minimum and initial must be the
jmasa@13 447 // same as its maximum.
jwilhelm@5420 448 _min_gen0_size = max_new_size;
jwilhelm@5420 449 _initial_gen0_size = max_new_size;
jwilhelm@5420 450 _max_gen0_size = max_new_size;
jmasa@13 451 } else {
jmasa@13 452 size_t desired_new_size = 0;
jmasa@13 453 if (!FLAG_IS_DEFAULT(NewSize)) {
jmasa@13 454 // If NewSize is set ergonomically (for example by cms), it
jmasa@13 455 // would make sense to use it. If it is used, also use it
jmasa@13 456 // to set the initial size. Although there is no reason
jmasa@13 457 // the minimum size and the initial size have to be the same,
jmasa@13 458 // the current implementation gets into trouble during the calculation
jmasa@13 459 // of the tenured generation sizes if they are different.
jmasa@13 460 // Note that this makes the initial size and the minimum size
jmasa@13 461 // generally small compared to the NewRatio calculation.
jmasa@13 462 _min_gen0_size = NewSize;
jmasa@13 463 desired_new_size = NewSize;
jmasa@13 464 max_new_size = MAX2(max_new_size, NewSize);
jmasa@13 465 } else {
jmasa@13 466 // For the case where NewSize is the default, use NewRatio
jmasa@13 467 // to size the minimum and initial generation sizes.
jmasa@13 468 // Use the default NewSize as the floor for these values. If
jmasa@13 469 // NewRatio is overly large, the resulting sizes can be too
jmasa@13 470 // small.
jwilhelm@5420 471 _min_gen0_size = MAX2(scale_by_NewRatio_aligned(_min_heap_byte_size), NewSize);
jmasa@13 472 desired_new_size =
jwilhelm@5420 473 MAX2(scale_by_NewRatio_aligned(_initial_heap_byte_size), NewSize);
jmasa@13 474 }
jmasa@13 475
jmasa@13 476 assert(_min_gen0_size > 0, "Sanity check");
jwilhelm@5420 477 _initial_gen0_size = desired_new_size;
jwilhelm@5420 478 _max_gen0_size = max_new_size;
jmasa@13 479
jmasa@13 480 // At this point the desirable initial and minimum sizes have been
jmasa@13 481 // determined without regard to the maximum sizes.
jmasa@13 482
jmasa@13 483 // Bound the sizes by the corresponding overall heap sizes.
jwilhelm@5420 484 _min_gen0_size = bound_minus_alignment(_min_gen0_size, _min_heap_byte_size);
jwilhelm@5420 485 _initial_gen0_size = bound_minus_alignment(_initial_gen0_size, _initial_heap_byte_size);
jwilhelm@5420 486 _max_gen0_size = bound_minus_alignment(_max_gen0_size, _max_heap_byte_size);
jmasa@13 487
jmasa@13 488 // At this point all three sizes have been checked against the
jmasa@13 489 // maximum sizes but have not been checked for consistency
ysr@342 490 // among the three.
jmasa@13 491
jmasa@13 492 // Final check min <= initial <= max
jwilhelm@5420 493 _min_gen0_size = MIN2(_min_gen0_size, _max_gen0_size);
jwilhelm@5420 494 _initial_gen0_size = MAX2(MIN2(_initial_gen0_size, _max_gen0_size), _min_gen0_size);
jwilhelm@5420 495 _min_gen0_size = MIN2(_min_gen0_size, _initial_gen0_size);
duke@0 496 }
duke@0 497
jwilhelm@5650 498 // Write back to flags if necessary
jwilhelm@5650 499 if (NewSize != _initial_gen0_size) {
jwilhelm@5650 500 FLAG_SET_ERGO(uintx, NewSize, _initial_gen0_size);
jwilhelm@5650 501 }
jwilhelm@5650 502
jwilhelm@5650 503 if (MaxNewSize != _max_gen0_size) {
jwilhelm@5650 504 FLAG_SET_ERGO(uintx, MaxNewSize, _max_gen0_size);
jwilhelm@5650 505 }
jwilhelm@5650 506
jmasa@13 507 if (PrintGCDetails && Verbose) {
ysr@2215 508 gclog_or_tty->print_cr("1: Minimum gen0 " SIZE_FORMAT " Initial gen0 "
jmasa@13 509 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
jwilhelm@5420 510 _min_gen0_size, _initial_gen0_size, _max_gen0_size);
jmasa@13 511 }
jwilhelm@5650 512
jwilhelm@5650 513 DEBUG_ONLY(GenCollectorPolicy::assert_size_info();)
jmasa@13 514 }
duke@0 515
jmasa@13 516 // Call this method during the sizing of the gen1 to make
jmasa@13 517 // adjustments to gen0 because of gen1 sizing policy. gen0 initially has
jmasa@13 518 // the most freedom in sizing because it is done before the
jmasa@13 519 // policy for gen1 is applied. Once gen1 policies have been applied,
jmasa@13 520 // there may be conflicts in the shape of the heap and this method
jmasa@13 521 // is used to make the needed adjustments. The application of the
jmasa@13 522 // policies could be more sophisticated (iterative for example) but
jmasa@13 523 // keeping it simple also seems a worthwhile goal.
jmasa@13 524 bool TwoGenerationCollectorPolicy::adjust_gen0_sizes(size_t* gen0_size_ptr,
jmasa@13 525 size_t* gen1_size_ptr,
jwilhelm@4119 526 const size_t heap_size,
jwilhelm@4119 527 const size_t min_gen1_size) {
jmasa@13 528 bool result = false;
jwilhelm@4119 529
jmasa@13 530 if ((*gen1_size_ptr + *gen0_size_ptr) > heap_size) {
jwilhelm@5650 531 uintx smallest_new_size = young_gen_size_lower_bound();
jwilhelm@4119 532 if ((heap_size < (*gen0_size_ptr + min_gen1_size)) &&
jwilhelm@5650 533 (heap_size >= min_gen1_size + smallest_new_size)) {
jwilhelm@4119 534 // Adjust gen0 down to accommodate min_gen1_size
jwilhelm@5650 535 *gen0_size_ptr = align_size_down_bounded(heap_size - min_gen1_size, _gen_alignment);
jmasa@13 536 assert(*gen0_size_ptr > 0, "Min gen0 is too large");
jmasa@13 537 result = true;
jmasa@13 538 } else {
jwilhelm@5650 539 *gen1_size_ptr = align_size_down_bounded(heap_size - *gen0_size_ptr, _gen_alignment);
jmasa@13 540 }
jmasa@13 541 }
jmasa@13 542 return result;
jmasa@13 543 }
duke@0 544
jmasa@13 545 // Minimum sizes of the generations may be different than
jmasa@13 546 // the initial sizes. An inconsistently is permitted here
jmasa@13 547 // in the total size that can be specified explicitly by
jmasa@13 548 // command line specification of OldSize and NewSize and
jmasa@13 549 // also a command line specification of -Xms. Issue a warning
jmasa@13 550 // but allow the values to pass.
duke@0 551
duke@0 552 void TwoGenerationCollectorPolicy::initialize_size_info() {
duke@0 553 GenCollectorPolicy::initialize_size_info();
duke@0 554
jmasa@13 555 // At this point the minimum, initial and maximum sizes
jmasa@13 556 // of the overall heap and of gen0 have been determined.
jmasa@13 557 // The maximum gen1 size can be determined from the maximum gen0
ysr@2215 558 // and maximum heap size since no explicit flags exits
jmasa@13 559 // for setting the gen1 maximum.
jwilhelm@5650 560 _max_gen1_size = MAX2(_max_heap_byte_size - _max_gen0_size, _gen_alignment);
jwilhelm@5650 561
jmasa@13 562 // If no explicit command line flag has been set for the
jmasa@13 563 // gen1 size, use what is left for gen1.
jwilhelm@5650 564 if (!FLAG_IS_CMDLINE(OldSize)) {
jwilhelm@5650 565 // The user has not specified any value but the ergonomics
jwilhelm@5650 566 // may have chosen a value (which may or may not be consistent
jmasa@13 567 // with the overall heap size). In either case make
jmasa@13 568 // the minimum, maximum and initial sizes consistent
jmasa@13 569 // with the gen0 sizes and the overall heap sizes.
jwilhelm@5650 570 _min_gen1_size = MAX2(_min_heap_byte_size - _min_gen0_size, _gen_alignment);
jwilhelm@5650 571 _initial_gen1_size = MAX2(_initial_heap_byte_size - _initial_gen0_size, _gen_alignment);
jwilhelm@5650 572 // _max_gen1_size has already been made consistent above
jwilhelm@5650 573 FLAG_SET_ERGO(uintx, OldSize, _initial_gen1_size);
jmasa@13 574 } else {
jmasa@13 575 // It's been explicitly set on the command line. Use the
jmasa@13 576 // OldSize and then determine the consequences.
jwilhelm@5650 577 _min_gen1_size = MIN2(OldSize, _min_heap_byte_size - _min_gen0_size);
jwilhelm@5420 578 _initial_gen1_size = OldSize;
jmasa@13 579
jmasa@13 580 // If the user has explicitly set an OldSize that is inconsistent
jmasa@13 581 // with other command line flags, issue a warning.
duke@0 582 // The generation minimums and the overall heap mimimum should
jwilhelm@5650 583 // be within one generation alignment.
jwilhelm@5650 584 if ((_min_gen1_size + _min_gen0_size + _gen_alignment) < _min_heap_byte_size) {
duke@0 585 warning("Inconsistency between minimum heap size and minimum "
jwilhelm@5420 586 "generation sizes: using minimum heap = " SIZE_FORMAT,
jwilhelm@5420 587 _min_heap_byte_size);
duke@0 588 }
jwilhelm@5649 589 if (OldSize > _max_gen1_size) {
jmasa@13 590 warning("Inconsistency between maximum heap size and maximum "
jwilhelm@5420 591 "generation sizes: using maximum heap = " SIZE_FORMAT
jwilhelm@5420 592 " -XX:OldSize flag is being ignored",
jwilhelm@5420 593 _max_heap_byte_size);
ysr@2215 594 }
jmasa@13 595 // If there is an inconsistency between the OldSize and the minimum and/or
jmasa@13 596 // initial size of gen0, since OldSize was explicitly set, OldSize wins.
jmasa@13 597 if (adjust_gen0_sizes(&_min_gen0_size, &_min_gen1_size,
jwilhelm@5650 598 _min_heap_byte_size, _min_gen1_size)) {
jmasa@13 599 if (PrintGCDetails && Verbose) {
ysr@2215 600 gclog_or_tty->print_cr("2: Minimum gen0 " SIZE_FORMAT " Initial gen0 "
jmasa@13 601 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
jwilhelm@5420 602 _min_gen0_size, _initial_gen0_size, _max_gen0_size);
jmasa@13 603 }
jmasa@13 604 }
jmasa@13 605 // Initial size
jmasa@13 606 if (adjust_gen0_sizes(&_initial_gen0_size, &_initial_gen1_size,
jwilhelm@5650 607 _initial_heap_byte_size, _initial_gen1_size)) {
jmasa@13 608 if (PrintGCDetails && Verbose) {
ysr@2215 609 gclog_or_tty->print_cr("3: Minimum gen0 " SIZE_FORMAT " Initial gen0 "
jmasa@13 610 SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT,
jwilhelm@5420 611 _min_gen0_size, _initial_gen0_size, _max_gen0_size);
jmasa@13 612 }
jmasa@13 613 }
jmasa@13 614 }
jmasa@13 615 // Enforce the maximum gen1 size.
jwilhelm@5420 616 _min_gen1_size = MIN2(_min_gen1_size, _max_gen1_size);
duke@0 617
jmasa@13 618 // Check that min gen1 <= initial gen1 <= max gen1
jwilhelm@5420 619 _initial_gen1_size = MAX2(_initial_gen1_size, _min_gen1_size);
jwilhelm@5420 620 _initial_gen1_size = MIN2(_initial_gen1_size, _max_gen1_size);
jmasa@13 621
jwilhelm@5650 622 // Write back to flags if necessary
jwilhelm@5650 623 if (NewSize != _initial_gen0_size) {
jwilhelm@5655 624 FLAG_SET_ERGO(uintx, NewSize, _initial_gen0_size);
jwilhelm@5650 625 }
jwilhelm@5650 626
jwilhelm@5650 627 if (MaxNewSize != _max_gen0_size) {
jwilhelm@5650 628 FLAG_SET_ERGO(uintx, MaxNewSize, _max_gen0_size);
jwilhelm@5650 629 }
jwilhelm@5650 630
jwilhelm@5650 631 if (OldSize != _initial_gen1_size) {
jwilhelm@5650 632 FLAG_SET_ERGO(uintx, OldSize, _initial_gen1_size);
jwilhelm@5650 633 }
jwilhelm@5650 634
jmasa@13 635 if (PrintGCDetails && Verbose) {
jmasa@13 636 gclog_or_tty->print_cr("Minimum gen1 " SIZE_FORMAT " Initial gen1 "
jmasa@13 637 SIZE_FORMAT " Maximum gen1 " SIZE_FORMAT,
jwilhelm@5420 638 _min_gen1_size, _initial_gen1_size, _max_gen1_size);
jmasa@13 639 }
jwilhelm@5650 640
jwilhelm@5650 641 DEBUG_ONLY(TwoGenerationCollectorPolicy::assert_size_info();)
duke@0 642 }
duke@0 643
duke@0 644 HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size,
duke@0 645 bool is_tlab,
duke@0 646 bool* gc_overhead_limit_was_exceeded) {
duke@0 647 GenCollectedHeap *gch = GenCollectedHeap::heap();
duke@0 648
duke@0 649 debug_only(gch->check_for_valid_allocation_state());
duke@0 650 assert(gch->no_gc_in_progress(), "Allocation during gc not allowed");
jmasa@1387 651
jmasa@1387 652 // In general gc_overhead_limit_was_exceeded should be false so
jmasa@1387 653 // set it so here and reset it to true only if the gc time
jmasa@1387 654 // limit is being exceeded as checked below.
jmasa@1387 655 *gc_overhead_limit_was_exceeded = false;
jmasa@1387 656
duke@0 657 HeapWord* result = NULL;
duke@0 658
duke@0 659 // Loop until the allocation is satisified,
duke@0 660 // or unsatisfied after GC.
mgerdin@4418 661 for (int try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) {
duke@0 662 HandleMark hm; // discard any handles allocated in each iteration
duke@0 663
duke@0 664 // First allocation attempt is lock-free.
duke@0 665 Generation *gen0 = gch->get_gen(0);
duke@0 666 assert(gen0->supports_inline_contig_alloc(),
duke@0 667 "Otherwise, must do alloc within heap lock");
duke@0 668 if (gen0->should_allocate(size, is_tlab)) {
duke@0 669 result = gen0->par_allocate(size, is_tlab);
duke@0 670 if (result != NULL) {
duke@0 671 assert(gch->is_in_reserved(result), "result not in heap");
duke@0 672 return result;
duke@0 673 }
duke@0 674 }
duke@0 675 unsigned int gc_count_before; // read inside the Heap_lock locked region
duke@0 676 {
duke@0 677 MutexLocker ml(Heap_lock);
duke@0 678 if (PrintGC && Verbose) {
duke@0 679 gclog_or_tty->print_cr("TwoGenerationCollectorPolicy::mem_allocate_work:"
duke@0 680 " attempting locked slow path allocation");
duke@0 681 }
duke@0 682 // Note that only large objects get a shot at being
duke@0 683 // allocated in later generations.
duke@0 684 bool first_only = ! should_try_older_generation_allocation(size);
duke@0 685
duke@0 686 result = gch->attempt_allocation(size, is_tlab, first_only);
duke@0 687 if (result != NULL) {
duke@0 688 assert(gch->is_in_reserved(result), "result not in heap");
duke@0 689 return result;
duke@0 690 }
duke@0 691
duke@0 692 if (GC_locker::is_active_and_needs_gc()) {
duke@0 693 if (is_tlab) {
duke@0 694 return NULL; // Caller will retry allocating individual object
duke@0 695 }
duke@0 696 if (!gch->is_maximal_no_gc()) {
duke@0 697 // Try and expand heap to satisfy request
duke@0 698 result = expand_heap_and_allocate(size, is_tlab);
duke@0 699 // result could be null if we are out of space
duke@0 700 if (result != NULL) {
duke@0 701 return result;
duke@0 702 }
duke@0 703 }
duke@0 704
mgerdin@4418 705 if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
mgerdin@4418 706 return NULL; // we didn't get to do a GC and we didn't get any memory
mgerdin@4418 707 }
mgerdin@4418 708
duke@0 709 // If this thread is not in a jni critical section, we stall
duke@0 710 // the requestor until the critical section has cleared and
duke@0 711 // GC allowed. When the critical section clears, a GC is
duke@0 712 // initiated by the last thread exiting the critical section; so
duke@0 713 // we retry the allocation sequence from the beginning of the loop,
duke@0 714 // rather than causing more, now probably unnecessary, GC attempts.
duke@0 715 JavaThread* jthr = JavaThread::current();
duke@0 716 if (!jthr->in_critical()) {
duke@0 717 MutexUnlocker mul(Heap_lock);
duke@0 718 // Wait for JNI critical section to be exited
duke@0 719 GC_locker::stall_until_clear();
mgerdin@4418 720 gclocker_stalled_count += 1;
duke@0 721 continue;
duke@0 722 } else {
duke@0 723 if (CheckJNICalls) {
duke@0 724 fatal("Possible deadlock due to allocating while"
duke@0 725 " in jni critical section");
duke@0 726 }
duke@0 727 return NULL;
duke@0 728 }
duke@0 729 }
duke@0 730
duke@0 731 // Read the gc count while the heap lock is held.
duke@0 732 gc_count_before = Universe::heap()->total_collections();
duke@0 733 }
duke@0 734
jwilhelm@5649 735 VM_GenCollectForAllocation op(size, is_tlab, gc_count_before);
duke@0 736 VMThread::execute(&op);
duke@0 737 if (op.prologue_succeeded()) {
duke@0 738 result = op.result();
duke@0 739 if (op.gc_locked()) {
duke@0 740 assert(result == NULL, "must be NULL if gc_locked() is true");
duke@0 741 continue; // retry and/or stall as necessary
duke@0 742 }
jmasa@1387 743
jmasa@1387 744 // Allocation has failed and a collection
jmasa@1387 745 // has been done. If the gc time limit was exceeded the
jmasa@1387 746 // this time, return NULL so that an out-of-memory
jmasa@1387 747 // will be thrown. Clear gc_overhead_limit_exceeded
jmasa@1387 748 // so that the overhead exceeded does not persist.
jmasa@1387 749
jmasa@1387 750 const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
jmasa@1387 751 const bool softrefs_clear = all_soft_refs_clear();
jmasa@4308 752
jmasa@1387 753 if (limit_exceeded && softrefs_clear) {
jmasa@1387 754 *gc_overhead_limit_was_exceeded = true;
jmasa@1387 755 size_policy()->set_gc_overhead_limit_exceeded(false);
jmasa@1387 756 if (op.result() != NULL) {
jmasa@1387 757 CollectedHeap::fill_with_object(op.result(), size);
jmasa@1387 758 }
jmasa@1387 759 return NULL;
jmasa@1387 760 }
duke@0 761 assert(result == NULL || gch->is_in_reserved(result),
duke@0 762 "result not in heap");
duke@0 763 return result;
duke@0 764 }
duke@0 765
duke@0 766 // Give a warning if we seem to be looping forever.
duke@0 767 if ((QueuedAllocationWarningCount > 0) &&
duke@0 768 (try_count % QueuedAllocationWarningCount == 0)) {
duke@0 769 warning("TwoGenerationCollectorPolicy::mem_allocate_work retries %d times \n\t"
duke@0 770 " size=%d %s", try_count, size, is_tlab ? "(TLAB)" : "");
duke@0 771 }
duke@0 772 }
duke@0 773 }
duke@0 774
duke@0 775 HeapWord* GenCollectorPolicy::expand_heap_and_allocate(size_t size,
duke@0 776 bool is_tlab) {
duke@0 777 GenCollectedHeap *gch = GenCollectedHeap::heap();
duke@0 778 HeapWord* result = NULL;
duke@0 779 for (int i = number_of_generations() - 1; i >= 0 && result == NULL; i--) {
duke@0 780 Generation *gen = gch->get_gen(i);
duke@0 781 if (gen->should_allocate(size, is_tlab)) {
duke@0 782 result = gen->expand_and_allocate(size, is_tlab);
duke@0 783 }
duke@0 784 }
duke@0 785 assert(result == NULL || gch->is_in_reserved(result), "result not in heap");
duke@0 786 return result;
duke@0 787 }
duke@0 788
duke@0 789 HeapWord* GenCollectorPolicy::satisfy_failed_allocation(size_t size,
duke@0 790 bool is_tlab) {
duke@0 791 GenCollectedHeap *gch = GenCollectedHeap::heap();
duke@0 792 GCCauseSetter x(gch, GCCause::_allocation_failure);
duke@0 793 HeapWord* result = NULL;
duke@0 794
duke@0 795 assert(size != 0, "Precondition violated");
duke@0 796 if (GC_locker::is_active_and_needs_gc()) {
duke@0 797 // GC locker is active; instead of a collection we will attempt
duke@0 798 // to expand the heap, if there's room for expansion.
duke@0 799 if (!gch->is_maximal_no_gc()) {
duke@0 800 result = expand_heap_and_allocate(size, is_tlab);
duke@0 801 }
duke@0 802 return result; // could be null if we are out of space
ysr@1901 803 } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) {
duke@0 804 // Do an incremental collection.
duke@0 805 gch->do_collection(false /* full */,
duke@0 806 false /* clear_all_soft_refs */,
duke@0 807 size /* size */,
duke@0 808 is_tlab /* is_tlab */,
duke@0 809 number_of_generations() - 1 /* max_level */);
duke@0 810 } else {
ysr@1901 811 if (Verbose && PrintGCDetails) {
ysr@1901 812 gclog_or_tty->print(" :: Trying full because partial may fail :: ");
ysr@1901 813 }
duke@0 814 // Try a full collection; see delta for bug id 6266275
duke@0 815 // for the original code and why this has been simplified
duke@0 816 // with from-space allocation criteria modified and
duke@0 817 // such allocation moved out of the safepoint path.
duke@0 818 gch->do_collection(true /* full */,
duke@0 819 false /* clear_all_soft_refs */,
duke@0 820 size /* size */,
duke@0 821 is_tlab /* is_tlab */,
duke@0 822 number_of_generations() - 1 /* max_level */);
duke@0 823 }
duke@0 824
duke@0 825 result = gch->attempt_allocation(size, is_tlab, false /*first_only*/);
duke@0 826
duke@0 827 if (result != NULL) {
duke@0 828 assert(gch->is_in_reserved(result), "result not in heap");
duke@0 829 return result;
duke@0 830 }
duke@0 831
duke@0 832 // OK, collection failed, try expansion.
duke@0 833 result = expand_heap_and_allocate(size, is_tlab);
duke@0 834 if (result != NULL) {
duke@0 835 return result;
duke@0 836 }
duke@0 837
duke@0 838 // If we reach this point, we're really out of memory. Try every trick
duke@0 839 // we can to reclaim memory. Force collection of soft references. Force
duke@0 840 // a complete compaction of the heap. Any additional methods for finding
duke@0 841 // free memory should be here, especially if they are expensive. If this
duke@0 842 // attempt fails, an OOM exception will be thrown.
duke@0 843 {
tschatzl@4684 844 UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted
duke@0 845
duke@0 846 gch->do_collection(true /* full */,
duke@0 847 true /* clear_all_soft_refs */,
duke@0 848 size /* size */,
duke@0 849 is_tlab /* is_tlab */,
duke@0 850 number_of_generations() - 1 /* max_level */);
duke@0 851 }
duke@0 852
duke@0 853 result = gch->attempt_allocation(size, is_tlab, false /* first_only */);
duke@0 854 if (result != NULL) {
duke@0 855 assert(gch->is_in_reserved(result), "result not in heap");
duke@0 856 return result;
duke@0 857 }
duke@0 858
jmasa@1387 859 assert(!should_clear_all_soft_refs(),
jmasa@1387 860 "Flag should have been handled and cleared prior to this point");
jmasa@1387 861
duke@0 862 // What else? We might try synchronous finalization later. If the total
duke@0 863 // space available is large enough for the allocation, then a more
duke@0 864 // complete compaction phase than we've tried so far might be
duke@0 865 // appropriate.
duke@0 866 return NULL;
duke@0 867 }
duke@0 868
coleenp@3602 869 MetaWord* CollectorPolicy::satisfy_failed_metadata_allocation(
coleenp@3602 870 ClassLoaderData* loader_data,
coleenp@3602 871 size_t word_size,
coleenp@3602 872 Metaspace::MetadataType mdtype) {
coleenp@3602 873 uint loop_count = 0;
coleenp@3602 874 uint gc_count = 0;
coleenp@3602 875 uint full_gc_count = 0;
coleenp@3602 876
jmasa@3799 877 assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
jmasa@3799 878
coleenp@3602 879 do {
jmasa@3629 880 MetaWord* result = NULL;
jmasa@3629 881 if (GC_locker::is_active_and_needs_gc()) {
jmasa@3629 882 // If the GC_locker is active, just expand and allocate.
jmasa@3629 883 // If that does not succeed, wait if this thread is not
jmasa@3629 884 // in a critical section itself.
jmasa@3629 885 result =
jmasa@3629 886 loader_data->metaspace_non_null()->expand_and_allocate(word_size,
jmasa@3629 887 mdtype);
jmasa@3629 888 if (result != NULL) {
jmasa@3629 889 return result;
jmasa@3629 890 }
jmasa@3629 891 JavaThread* jthr = JavaThread::current();
jmasa@3629 892 if (!jthr->in_critical()) {
jmasa@3629 893 // Wait for JNI critical section to be exited
jmasa@3629 894 GC_locker::stall_until_clear();
jmasa@3629 895 // The GC invoked by the last thread leaving the critical
jmasa@3629 896 // section will be a young collection and a full collection
jmasa@3629 897 // is (currently) needed for unloading classes so continue
jmasa@3629 898 // to the next iteration to get a full GC.
jmasa@3629 899 continue;
jmasa@3629 900 } else {
jmasa@3629 901 if (CheckJNICalls) {
jmasa@3629 902 fatal("Possible deadlock due to allocating while"
jmasa@3629 903 " in jni critical section");
jmasa@3629 904 }
jmasa@3629 905 return NULL;
jmasa@3629 906 }
jmasa@3629 907 }
jmasa@3629 908
coleenp@3602 909 { // Need lock to get self consistent gc_count's
coleenp@3602 910 MutexLocker ml(Heap_lock);
coleenp@3602 911 gc_count = Universe::heap()->total_collections();
coleenp@3602 912 full_gc_count = Universe::heap()->total_full_collections();
coleenp@3602 913 }
coleenp@3602 914
coleenp@3602 915 // Generate a VM operation
coleenp@3602 916 VM_CollectForMetadataAllocation op(loader_data,
coleenp@3602 917 word_size,
coleenp@3602 918 mdtype,
coleenp@3602 919 gc_count,
coleenp@3602 920 full_gc_count,
coleenp@3602 921 GCCause::_metadata_GC_threshold);
coleenp@3602 922 VMThread::execute(&op);
jmasa@3947 923
jmasa@3947 924 // If GC was locked out, try again. Check
jmasa@3947 925 // before checking success because the prologue
jmasa@3947 926 // could have succeeded and the GC still have
jmasa@3947 927 // been locked out.
jmasa@3947 928 if (op.gc_locked()) {
jmasa@3947 929 continue;
jmasa@3947 930 }
jmasa@3947 931
coleenp@3602 932 if (op.prologue_succeeded()) {
coleenp@3602 933 return op.result();
coleenp@3602 934 }
coleenp@3602 935 loop_count++;
coleenp@3602 936 if ((QueuedAllocationWarningCount > 0) &&
coleenp@3602 937 (loop_count % QueuedAllocationWarningCount == 0)) {
coleenp@3602 938 warning("satisfy_failed_metadata_allocation() retries %d times \n\t"
coleenp@3602 939 " size=%d", loop_count, word_size);
coleenp@3602 940 }
coleenp@3602 941 } while (true); // Until a GC is done
coleenp@3602 942 }
coleenp@3602 943
duke@0 944 // Return true if any of the following is true:
duke@0 945 // . the allocation won't fit into the current young gen heap
duke@0 946 // . gc locker is occupied (jni critical section)
duke@0 947 // . heap memory is tight -- the most recent previous collection
duke@0 948 // was a full collection because a partial collection (would
duke@0 949 // have) failed and is likely to fail again
duke@0 950 bool GenCollectorPolicy::should_try_older_generation_allocation(
duke@0 951 size_t word_size) const {
duke@0 952 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 953 size_t gen0_capacity = gch->get_gen(0)->capacity_before_gc();
duke@0 954 return (word_size > heap_word_size(gen0_capacity))
ysr@1808 955 || GC_locker::is_active_and_needs_gc()
ysr@1808 956 || gch->incremental_collection_failed();
duke@0 957 }
duke@0 958
duke@0 959
duke@0 960 //
duke@0 961 // MarkSweepPolicy methods
duke@0 962 //
duke@0 963
jwilhelm@5650 964 void MarkSweepPolicy::initialize_alignments() {
jwilhelm@5650 965 _space_alignment = _gen_alignment = (uintx)Generation::GenGrain;
jwilhelm@5650 966 _heap_alignment = compute_heap_alignment();
duke@0 967 }
duke@0 968
duke@0 969 void MarkSweepPolicy::initialize_generations() {
minqi@4668 970 _generations = NEW_C_HEAP_ARRAY3(GenerationSpecPtr, number_of_generations(), mtGC, 0, AllocFailStrategy::RETURN_NULL);
jwilhelm@5649 971 if (_generations == NULL) {
duke@0 972 vm_exit_during_initialization("Unable to allocate gen spec");
jwilhelm@5649 973 }
duke@0 974
brutisso@3952 975 if (UseParNewGC) {
duke@0 976 _generations[0] = new GenerationSpec(Generation::ParNew, _initial_gen0_size, _max_gen0_size);
duke@0 977 } else {
duke@0 978 _generations[0] = new GenerationSpec(Generation::DefNew, _initial_gen0_size, _max_gen0_size);
duke@0 979 }
duke@0 980 _generations[1] = new GenerationSpec(Generation::MarkSweepCompact, _initial_gen1_size, _max_gen1_size);
duke@0 981
jwilhelm@5649 982 if (_generations[0] == NULL || _generations[1] == NULL) {
duke@0 983 vm_exit_during_initialization("Unable to allocate gen spec");
jwilhelm@5649 984 }
duke@0 985 }
duke@0 986
duke@0 987 void MarkSweepPolicy::initialize_gc_policy_counters() {
duke@0 988 // initialize the policy counters - 2 collectors, 3 generations
brutisso@3952 989 if (UseParNewGC) {
duke@0 990 _gc_policy_counters = new GCPolicyCounters("ParNew:MSC", 2, 3);
brutisso@3952 991 } else {
duke@0 992 _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3);
duke@0 993 }
duke@0 994 }