src/share/vm/memory/collectorPolicy.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7686
fb69749583e8
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

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

mercurial