src/share/vm/memory/collectorPolicy.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial