src/share/vm/memory/collectorPolicy.cpp

Wed, 27 Aug 2014 08:19:12 -0400

author
zgu
date
Wed, 27 Aug 2014 08:19:12 -0400
changeset 7074
833b0f92429a
parent 6680
78bbf4d43a14
child 7301
d63ce76a0f0e
permissions
-rw-r--r--

8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill

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

mercurial