src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.cpp

Thu, 14 Mar 2013 09:37:38 +0100

author
tschatzl
date
Thu, 14 Mar 2013 09:37:38 +0100
changeset 4785
3c226052f7dc
parent 4743
82657b6a8cc0
child 4853
2e093b564241
permissions
-rw-r--r--

6733980: par compact - TraceGen1Time always shows 0.0000 seconds
Summary: Use the correct collector to retrieve accumulated gen1 trace time
Reviewed-by: johnc, jmasa

duke@435 1 /*
jcoomes@3541 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/parallelScavenge/adjoiningGenerations.hpp"
stefank@2314 27 #include "gc_implementation/parallelScavenge/adjoiningVirtualSpaces.hpp"
stefank@2314 28 #include "gc_implementation/parallelScavenge/cardTableExtension.hpp"
stefank@2314 29 #include "gc_implementation/parallelScavenge/gcTaskManager.hpp"
stefank@2314 30 #include "gc_implementation/parallelScavenge/generationSizer.hpp"
stefank@2314 31 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp"
stefank@2314 32 #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp"
stefank@2314 33 #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
stefank@2314 34 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
stefank@2314 35 #include "gc_implementation/parallelScavenge/psPromotionManager.hpp"
stefank@2314 36 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
stefank@2314 37 #include "gc_implementation/parallelScavenge/vmPSOperations.hpp"
stefank@2314 38 #include "memory/gcLocker.inline.hpp"
stefank@2314 39 #include "oops/oop.inline.hpp"
stefank@2314 40 #include "runtime/handles.inline.hpp"
stefank@2314 41 #include "runtime/java.hpp"
stefank@2314 42 #include "runtime/vmThread.hpp"
zgu@3900 43 #include "services/memTracker.hpp"
stefank@2314 44 #include "utilities/vmError.hpp"
duke@435 45
duke@435 46 PSYoungGen* ParallelScavengeHeap::_young_gen = NULL;
duke@435 47 PSOldGen* ParallelScavengeHeap::_old_gen = NULL;
duke@435 48 PSAdaptiveSizePolicy* ParallelScavengeHeap::_size_policy = NULL;
duke@435 49 PSGCAdaptivePolicyCounters* ParallelScavengeHeap::_gc_policy_counters = NULL;
duke@435 50 ParallelScavengeHeap* ParallelScavengeHeap::_psh = NULL;
duke@435 51 GCTaskManager* ParallelScavengeHeap::_gc_task_manager = NULL;
duke@435 52
duke@435 53 static void trace_gen_sizes(const char* const str,
duke@435 54 size_t og_min, size_t og_max,
duke@435 55 size_t yg_min, size_t yg_max)
duke@435 56 {
duke@435 57 if (TracePageSizes) {
duke@435 58 tty->print_cr("%s: " SIZE_FORMAT "," SIZE_FORMAT " "
duke@435 59 SIZE_FORMAT "," SIZE_FORMAT " "
duke@435 60 SIZE_FORMAT,
coleenp@4037 61 str,
duke@435 62 og_min / K, og_max / K,
duke@435 63 yg_min / K, yg_max / K,
coleenp@4037 64 (og_max + yg_max) / K);
duke@435 65 }
duke@435 66 }
duke@435 67
duke@435 68 jint ParallelScavengeHeap::initialize() {
ysr@1601 69 CollectedHeap::pre_initialize();
ysr@1601 70
duke@435 71 // Cannot be initialized until after the flags are parsed
jmasa@1822 72 // GenerationSizer flag_parser;
jmasa@1822 73 _collector_policy = new GenerationSizer();
duke@435 74
jmasa@1822 75 size_t yg_min_size = _collector_policy->min_young_gen_size();
jmasa@1822 76 size_t yg_max_size = _collector_policy->max_young_gen_size();
jmasa@1822 77 size_t og_min_size = _collector_policy->min_old_gen_size();
jmasa@1822 78 size_t og_max_size = _collector_policy->max_old_gen_size();
duke@435 79
duke@435 80 trace_gen_sizes("ps heap raw",
duke@435 81 og_min_size, og_max_size,
duke@435 82 yg_min_size, yg_max_size);
duke@435 83
duke@435 84 const size_t og_page_sz = os::page_size_for_region(yg_min_size + og_min_size,
duke@435 85 yg_max_size + og_max_size,
duke@435 86 8);
duke@435 87
duke@435 88 const size_t og_align = set_alignment(_old_gen_alignment, og_page_sz);
duke@435 89 const size_t yg_align = set_alignment(_young_gen_alignment, og_page_sz);
duke@435 90
duke@435 91 // Update sizes to reflect the selected page size(s).
duke@435 92 //
duke@435 93 // NEEDS_CLEANUP. The default TwoGenerationCollectorPolicy uses NewRatio; it
duke@435 94 // should check UseAdaptiveSizePolicy. Changes from generationSizer could
duke@435 95 // move to the common code.
duke@435 96 yg_min_size = align_size_up(yg_min_size, yg_align);
duke@435 97 yg_max_size = align_size_up(yg_max_size, yg_align);
jmasa@1822 98 size_t yg_cur_size =
jmasa@1822 99 align_size_up(_collector_policy->young_gen_size(), yg_align);
duke@435 100 yg_cur_size = MAX2(yg_cur_size, yg_min_size);
duke@435 101
duke@435 102 og_min_size = align_size_up(og_min_size, og_align);
kvn@2558 103 // Align old gen size down to preserve specified heap size.
kvn@2558 104 assert(og_align == yg_align, "sanity");
kvn@2558 105 og_max_size = align_size_down(og_max_size, og_align);
kvn@2558 106 og_max_size = MAX2(og_max_size, og_min_size);
jmasa@1822 107 size_t og_cur_size =
kvn@2558 108 align_size_down(_collector_policy->old_gen_size(), og_align);
duke@435 109 og_cur_size = MAX2(og_cur_size, og_min_size);
duke@435 110
duke@435 111 trace_gen_sizes("ps heap rnd",
duke@435 112 og_min_size, og_max_size,
duke@435 113 yg_min_size, yg_max_size);
duke@435 114
coleenp@4037 115 const size_t heap_size = og_max_size + yg_max_size;
kvn@1077 116
coleenp@4037 117 ReservedSpace heap_rs = Universe::reserve_heap(heap_size, og_align);
kvn@1077 118
zgu@3900 119 MemTracker::record_virtual_memory_type((address)heap_rs.base(), mtJavaHeap);
zgu@3900 120
duke@435 121 os::trace_page_sizes("ps main", og_min_size + yg_min_size,
duke@435 122 og_max_size + yg_max_size, og_page_sz,
coleenp@4037 123 heap_rs.base(),
coleenp@4037 124 heap_rs.size());
duke@435 125 if (!heap_rs.is_reserved()) {
duke@435 126 vm_shutdown_during_initialization(
duke@435 127 "Could not reserve enough space for object heap");
duke@435 128 return JNI_ENOMEM;
duke@435 129 }
duke@435 130
duke@435 131 _reserved = MemRegion((HeapWord*)heap_rs.base(),
duke@435 132 (HeapWord*)(heap_rs.base() + heap_rs.size()));
duke@435 133
duke@435 134 CardTableExtension* const barrier_set = new CardTableExtension(_reserved, 3);
duke@435 135 _barrier_set = barrier_set;
duke@435 136 oopDesc::set_bs(_barrier_set);
duke@435 137 if (_barrier_set == NULL) {
duke@435 138 vm_shutdown_during_initialization(
duke@435 139 "Could not reserve enough space for barrier set");
duke@435 140 return JNI_ENOMEM;
duke@435 141 }
duke@435 142
duke@435 143 // Initial young gen size is 4 Mb
duke@435 144 //
duke@435 145 // XXX - what about flag_parser.young_gen_size()?
duke@435 146 const size_t init_young_size = align_size_up(4 * M, yg_align);
duke@435 147 yg_cur_size = MAX2(MIN2(init_young_size, yg_max_size), yg_cur_size);
duke@435 148
duke@435 149 // Make up the generations
duke@435 150 // Calculate the maximum size that a generation can grow. This
duke@435 151 // includes growth into the other generation. Note that the
duke@435 152 // parameter _max_gen_size is kept as the maximum
duke@435 153 // size of the generation as the boundaries currently stand.
duke@435 154 // _max_gen_size is still used as that value.
duke@435 155 double max_gc_pause_sec = ((double) MaxGCPauseMillis)/1000.0;
duke@435 156 double max_gc_minor_pause_sec = ((double) MaxGCMinorPauseMillis)/1000.0;
duke@435 157
coleenp@4037 158 _gens = new AdjoiningGenerations(heap_rs,
duke@435 159 og_cur_size,
duke@435 160 og_min_size,
duke@435 161 og_max_size,
duke@435 162 yg_cur_size,
duke@435 163 yg_min_size,
duke@435 164 yg_max_size,
duke@435 165 yg_align);
duke@435 166
duke@435 167 _old_gen = _gens->old_gen();
duke@435 168 _young_gen = _gens->young_gen();
duke@435 169
duke@435 170 const size_t eden_capacity = _young_gen->eden_space()->capacity_in_bytes();
duke@435 171 const size_t old_capacity = _old_gen->capacity_in_bytes();
duke@435 172 const size_t initial_promo_size = MIN2(eden_capacity, old_capacity);
duke@435 173 _size_policy =
duke@435 174 new PSAdaptiveSizePolicy(eden_capacity,
duke@435 175 initial_promo_size,
duke@435 176 young_gen()->to_space()->capacity_in_bytes(),
jmasa@448 177 intra_heap_alignment(),
duke@435 178 max_gc_pause_sec,
duke@435 179 max_gc_minor_pause_sec,
duke@435 180 GCTimeRatio
duke@435 181 );
duke@435 182
duke@435 183 assert(!UseAdaptiveGCBoundary ||
duke@435 184 (old_gen()->virtual_space()->high_boundary() ==
duke@435 185 young_gen()->virtual_space()->low_boundary()),
duke@435 186 "Boundaries must meet");
duke@435 187 // initialize the policy counters - 2 collectors, 3 generations
duke@435 188 _gc_policy_counters =
duke@435 189 new PSGCAdaptivePolicyCounters("ParScav:MSC", 2, 3, _size_policy);
duke@435 190 _psh = this;
duke@435 191
duke@435 192 // Set up the GCTaskManager
duke@435 193 _gc_task_manager = GCTaskManager::create(ParallelGCThreads);
duke@435 194
duke@435 195 if (UseParallelOldGC && !PSParallelCompact::initialize()) {
duke@435 196 return JNI_ENOMEM;
duke@435 197 }
duke@435 198
duke@435 199 return JNI_OK;
duke@435 200 }
duke@435 201
duke@435 202 void ParallelScavengeHeap::post_initialize() {
duke@435 203 // Need to init the tenuring threshold
duke@435 204 PSScavenge::initialize();
duke@435 205 if (UseParallelOldGC) {
duke@435 206 PSParallelCompact::post_initialize();
duke@435 207 } else {
duke@435 208 PSMarkSweep::initialize();
duke@435 209 }
duke@435 210 PSPromotionManager::initialize();
duke@435 211 }
duke@435 212
duke@435 213 void ParallelScavengeHeap::update_counters() {
duke@435 214 young_gen()->update_counters();
duke@435 215 old_gen()->update_counters();
coleenp@4037 216 MetaspaceCounters::update_performance_counters();
duke@435 217 }
duke@435 218
duke@435 219 size_t ParallelScavengeHeap::capacity() const {
duke@435 220 size_t value = young_gen()->capacity_in_bytes() + old_gen()->capacity_in_bytes();
duke@435 221 return value;
duke@435 222 }
duke@435 223
duke@435 224 size_t ParallelScavengeHeap::used() const {
duke@435 225 size_t value = young_gen()->used_in_bytes() + old_gen()->used_in_bytes();
duke@435 226 return value;
duke@435 227 }
duke@435 228
duke@435 229 bool ParallelScavengeHeap::is_maximal_no_gc() const {
duke@435 230 return old_gen()->is_maximal_no_gc() && young_gen()->is_maximal_no_gc();
duke@435 231 }
duke@435 232
duke@435 233
duke@435 234 size_t ParallelScavengeHeap::max_capacity() const {
duke@435 235 size_t estimated = reserved_region().byte_size();
duke@435 236 if (UseAdaptiveSizePolicy) {
duke@435 237 estimated -= _size_policy->max_survivor_size(young_gen()->max_size());
duke@435 238 } else {
duke@435 239 estimated -= young_gen()->to_space()->capacity_in_bytes();
duke@435 240 }
duke@435 241 return MAX2(estimated, capacity());
duke@435 242 }
duke@435 243
duke@435 244 bool ParallelScavengeHeap::is_in(const void* p) const {
duke@435 245 if (young_gen()->is_in(p)) {
duke@435 246 return true;
duke@435 247 }
duke@435 248
duke@435 249 if (old_gen()->is_in(p)) {
duke@435 250 return true;
duke@435 251 }
duke@435 252
duke@435 253 return false;
duke@435 254 }
duke@435 255
duke@435 256 bool ParallelScavengeHeap::is_in_reserved(const void* p) const {
duke@435 257 if (young_gen()->is_in_reserved(p)) {
duke@435 258 return true;
duke@435 259 }
duke@435 260
duke@435 261 if (old_gen()->is_in_reserved(p)) {
duke@435 262 return true;
duke@435 263 }
duke@435 264
duke@435 265 return false;
duke@435 266 }
duke@435 267
jmasa@2909 268 bool ParallelScavengeHeap::is_scavengable(const void* addr) {
jmasa@2909 269 return is_in_young((oop)addr);
jmasa@2909 270 }
jmasa@2909 271
jmasa@2909 272 #ifdef ASSERT
jmasa@2909 273 // Don't implement this by using is_in_young(). This method is used
jmasa@2909 274 // in some cases to check that is_in_young() is correct.
jmasa@2909 275 bool ParallelScavengeHeap::is_in_partial_collection(const void *p) {
jmasa@2909 276 assert(is_in_reserved(p) || p == NULL,
jmasa@2909 277 "Does not work if address is non-null and outside of the heap");
coleenp@4037 278 // The order of the generations is old (low addr), young (high addr)
jmasa@2909 279 return p >= old_gen()->reserved().end();
jmasa@2909 280 }
jmasa@2909 281 #endif
jmasa@2909 282
duke@435 283 // There are two levels of allocation policy here.
duke@435 284 //
duke@435 285 // When an allocation request fails, the requesting thread must invoke a VM
duke@435 286 // operation, transfer control to the VM thread, and await the results of a
duke@435 287 // garbage collection. That is quite expensive, and we should avoid doing it
duke@435 288 // multiple times if possible.
duke@435 289 //
duke@435 290 // To accomplish this, we have a basic allocation policy, and also a
duke@435 291 // failed allocation policy.
duke@435 292 //
duke@435 293 // The basic allocation policy controls how you allocate memory without
duke@435 294 // attempting garbage collection. It is okay to grab locks and
duke@435 295 // expand the heap, if that can be done without coming to a safepoint.
duke@435 296 // It is likely that the basic allocation policy will not be very
duke@435 297 // aggressive.
duke@435 298 //
duke@435 299 // The failed allocation policy is invoked from the VM thread after
duke@435 300 // the basic allocation policy is unable to satisfy a mem_allocate
duke@435 301 // request. This policy needs to cover the entire range of collection,
duke@435 302 // heap expansion, and out-of-memory conditions. It should make every
duke@435 303 // attempt to allocate the requested memory.
duke@435 304
duke@435 305 // Basic allocation policy. Should never be called at a safepoint, or
duke@435 306 // from the VM thread.
duke@435 307 //
duke@435 308 // This method must handle cases where many mem_allocate requests fail
duke@435 309 // simultaneously. When that happens, only one VM operation will succeed,
duke@435 310 // and the rest will not be executed. For that reason, this method loops
duke@435 311 // during failed allocation attempts. If the java heap becomes exhausted,
duke@435 312 // we rely on the size_policy object to force a bail out.
duke@435 313 HeapWord* ParallelScavengeHeap::mem_allocate(
duke@435 314 size_t size,
duke@435 315 bool* gc_overhead_limit_was_exceeded) {
duke@435 316 assert(!SafepointSynchronize::is_at_safepoint(), "should not be at safepoint");
duke@435 317 assert(Thread::current() != (Thread*)VMThread::vm_thread(), "should not be in vm thread");
duke@435 318 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
duke@435 319
jmasa@1822 320 // In general gc_overhead_limit_was_exceeded should be false so
jmasa@1822 321 // set it so here and reset it to true only if the gc time
jmasa@1822 322 // limit is being exceeded as checked below.
jmasa@1822 323 *gc_overhead_limit_was_exceeded = false;
jmasa@1822 324
tonyp@2971 325 HeapWord* result = young_gen()->allocate(size);
duke@435 326
duke@435 327 uint loop_count = 0;
duke@435 328 uint gc_count = 0;
duke@435 329
duke@435 330 while (result == NULL) {
duke@435 331 // We don't want to have multiple collections for a single filled generation.
duke@435 332 // To prevent this, each thread tracks the total_collections() value, and if
duke@435 333 // the count has changed, does not do a new collection.
duke@435 334 //
duke@435 335 // The collection count must be read only while holding the heap lock. VM
duke@435 336 // operations also hold the heap lock during collections. There is a lock
duke@435 337 // contention case where thread A blocks waiting on the Heap_lock, while
duke@435 338 // thread B is holding it doing a collection. When thread A gets the lock,
duke@435 339 // the collection count has already changed. To prevent duplicate collections,
duke@435 340 // The policy MUST attempt allocations during the same period it reads the
duke@435 341 // total_collections() value!
duke@435 342 {
duke@435 343 MutexLocker ml(Heap_lock);
duke@435 344 gc_count = Universe::heap()->total_collections();
duke@435 345
tonyp@2971 346 result = young_gen()->allocate(size);
duke@435 347 if (result != NULL) {
duke@435 348 return result;
duke@435 349 }
jcoomes@3541 350
jcoomes@3541 351 // If certain conditions hold, try allocating from the old gen.
jcoomes@3541 352 result = mem_allocate_old_gen(size);
jcoomes@3541 353 if (result != NULL) {
jcoomes@3541 354 return result;
duke@435 355 }
jcoomes@3541 356
jcoomes@3541 357 // Failed to allocate without a gc.
duke@435 358 if (GC_locker::is_active_and_needs_gc()) {
duke@435 359 // If this thread is not in a jni critical section, we stall
duke@435 360 // the requestor until the critical section has cleared and
duke@435 361 // GC allowed. When the critical section clears, a GC is
duke@435 362 // initiated by the last thread exiting the critical section; so
duke@435 363 // we retry the allocation sequence from the beginning of the loop,
duke@435 364 // rather than causing more, now probably unnecessary, GC attempts.
duke@435 365 JavaThread* jthr = JavaThread::current();
duke@435 366 if (!jthr->in_critical()) {
duke@435 367 MutexUnlocker mul(Heap_lock);
duke@435 368 GC_locker::stall_until_clear();
duke@435 369 continue;
duke@435 370 } else {
duke@435 371 if (CheckJNICalls) {
duke@435 372 fatal("Possible deadlock due to allocating while"
duke@435 373 " in jni critical section");
duke@435 374 }
duke@435 375 return NULL;
duke@435 376 }
duke@435 377 }
duke@435 378 }
duke@435 379
duke@435 380 if (result == NULL) {
duke@435 381 // Generate a VM operation
tonyp@2971 382 VM_ParallelGCFailedAllocation op(size, gc_count);
duke@435 383 VMThread::execute(&op);
duke@435 384
duke@435 385 // Did the VM operation execute? If so, return the result directly.
duke@435 386 // This prevents us from looping until time out on requests that can
duke@435 387 // not be satisfied.
duke@435 388 if (op.prologue_succeeded()) {
duke@435 389 assert(Universe::heap()->is_in_or_null(op.result()),
duke@435 390 "result not in heap");
duke@435 391
duke@435 392 // If GC was locked out during VM operation then retry allocation
duke@435 393 // and/or stall as necessary.
duke@435 394 if (op.gc_locked()) {
duke@435 395 assert(op.result() == NULL, "must be NULL if gc_locked() is true");
duke@435 396 continue; // retry and/or stall as necessary
duke@435 397 }
jmasa@1822 398
jmasa@1822 399 // Exit the loop if the gc time limit has been exceeded.
jmasa@1822 400 // The allocation must have failed above ("result" guarding
jmasa@1822 401 // this path is NULL) and the most recent collection has exceeded the
jmasa@1822 402 // gc overhead limit (although enough may have been collected to
jmasa@1822 403 // satisfy the allocation). Exit the loop so that an out-of-memory
jmasa@1822 404 // will be thrown (return a NULL ignoring the contents of
jmasa@1822 405 // op.result()),
jmasa@1822 406 // but clear gc_overhead_limit_exceeded so that the next collection
jmasa@1822 407 // starts with a clean slate (i.e., forgets about previous overhead
jmasa@1822 408 // excesses). Fill op.result() with a filler object so that the
jmasa@1822 409 // heap remains parsable.
jmasa@1822 410 const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
jmasa@1822 411 const bool softrefs_clear = collector_policy()->all_soft_refs_clear();
jmasa@4743 412
jmasa@1822 413 if (limit_exceeded && softrefs_clear) {
jmasa@1822 414 *gc_overhead_limit_was_exceeded = true;
jmasa@1822 415 size_policy()->set_gc_overhead_limit_exceeded(false);
jmasa@1822 416 if (PrintGCDetails && Verbose) {
jmasa@1822 417 gclog_or_tty->print_cr("ParallelScavengeHeap::mem_allocate: "
jmasa@1822 418 "return NULL because gc_overhead_limit_exceeded is set");
jmasa@1822 419 }
jmasa@1822 420 if (op.result() != NULL) {
jmasa@1822 421 CollectedHeap::fill_with_object(op.result(), size);
jmasa@1822 422 }
jmasa@1822 423 return NULL;
duke@435 424 }
jmasa@1822 425
duke@435 426 return op.result();
duke@435 427 }
duke@435 428 }
duke@435 429
duke@435 430 // The policy object will prevent us from looping forever. If the
duke@435 431 // time spent in gc crosses a threshold, we will bail out.
duke@435 432 loop_count++;
duke@435 433 if ((result == NULL) && (QueuedAllocationWarningCount > 0) &&
duke@435 434 (loop_count % QueuedAllocationWarningCount == 0)) {
duke@435 435 warning("ParallelScavengeHeap::mem_allocate retries %d times \n\t"
tonyp@2971 436 " size=%d", loop_count, size);
duke@435 437 }
duke@435 438 }
duke@435 439
duke@435 440 return result;
duke@435 441 }
duke@435 442
jcoomes@3541 443 // A "death march" is a series of ultra-slow allocations in which a full gc is
jcoomes@3541 444 // done before each allocation, and after the full gc the allocation still
jcoomes@3541 445 // cannot be satisfied from the young gen. This routine detects that condition;
jcoomes@3541 446 // it should be called after a full gc has been done and the allocation
jcoomes@3541 447 // attempted from the young gen. The parameter 'addr' should be the result of
jcoomes@3541 448 // that young gen allocation attempt.
jcoomes@3541 449 void
jcoomes@3541 450 ParallelScavengeHeap::death_march_check(HeapWord* const addr, size_t size) {
jcoomes@3541 451 if (addr != NULL) {
jcoomes@3541 452 _death_march_count = 0; // death march has ended
jcoomes@3541 453 } else if (_death_march_count == 0) {
jcoomes@3541 454 if (should_alloc_in_eden(size)) {
jcoomes@3541 455 _death_march_count = 1; // death march has started
jcoomes@3541 456 }
jcoomes@3541 457 }
jcoomes@3541 458 }
jcoomes@3541 459
jcoomes@3541 460 HeapWord* ParallelScavengeHeap::mem_allocate_old_gen(size_t size) {
jcoomes@3541 461 if (!should_alloc_in_eden(size) || GC_locker::is_active_and_needs_gc()) {
jcoomes@3541 462 // Size is too big for eden, or gc is locked out.
jcoomes@3541 463 return old_gen()->allocate(size);
jcoomes@3541 464 }
jcoomes@3541 465
jcoomes@3541 466 // If a "death march" is in progress, allocate from the old gen a limited
jcoomes@3541 467 // number of times before doing a GC.
jcoomes@3541 468 if (_death_march_count > 0) {
jcoomes@3541 469 if (_death_march_count < 64) {
jcoomes@3541 470 ++_death_march_count;
jcoomes@3541 471 return old_gen()->allocate(size);
jcoomes@3541 472 } else {
jcoomes@3541 473 _death_march_count = 0;
jcoomes@3541 474 }
jcoomes@3541 475 }
jcoomes@3541 476 return NULL;
jcoomes@3541 477 }
jcoomes@3541 478
coleenp@4037 479 void ParallelScavengeHeap::do_full_collection(bool clear_all_soft_refs) {
coleenp@4037 480 if (UseParallelOldGC) {
coleenp@4037 481 // The do_full_collection() parameter clear_all_soft_refs
coleenp@4037 482 // is interpreted here as maximum_compaction which will
coleenp@4037 483 // cause SoftRefs to be cleared.
coleenp@4037 484 bool maximum_compaction = clear_all_soft_refs;
coleenp@4037 485 PSParallelCompact::invoke(maximum_compaction);
coleenp@4037 486 } else {
coleenp@4037 487 PSMarkSweep::invoke(clear_all_soft_refs);
coleenp@4037 488 }
coleenp@4037 489 }
coleenp@4037 490
duke@435 491 // Failed allocation policy. Must be called from the VM thread, and
duke@435 492 // only at a safepoint! Note that this method has policy for allocation
duke@435 493 // flow, and NOT collection policy. So we do not check for gc collection
duke@435 494 // time over limit here, that is the responsibility of the heap specific
duke@435 495 // collection methods. This method decides where to attempt allocations,
duke@435 496 // and when to attempt collections, but no collection specific policy.
tonyp@2971 497 HeapWord* ParallelScavengeHeap::failed_mem_allocate(size_t size) {
duke@435 498 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
duke@435 499 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
duke@435 500 assert(!Universe::heap()->is_gc_active(), "not reentrant");
duke@435 501 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
duke@435 502
jcoomes@3541 503 // We assume that allocation in eden will fail unless we collect.
duke@435 504
duke@435 505 // First level allocation failure, scavenge and allocate in young gen.
duke@435 506 GCCauseSetter gccs(this, GCCause::_allocation_failure);
jcoomes@3541 507 const bool invoked_full_gc = PSScavenge::invoke();
tonyp@2971 508 HeapWord* result = young_gen()->allocate(size);
duke@435 509
duke@435 510 // Second level allocation failure.
duke@435 511 // Mark sweep and allocate in young generation.
jcoomes@3541 512 if (result == NULL && !invoked_full_gc) {
coleenp@4037 513 do_full_collection(false);
jcoomes@3541 514 result = young_gen()->allocate(size);
duke@435 515 }
duke@435 516
jcoomes@3541 517 death_march_check(result, size);
jcoomes@3541 518
duke@435 519 // Third level allocation failure.
duke@435 520 // After mark sweep and young generation allocation failure,
duke@435 521 // allocate in old generation.
tonyp@2971 522 if (result == NULL) {
tonyp@2971 523 result = old_gen()->allocate(size);
duke@435 524 }
duke@435 525
duke@435 526 // Fourth level allocation failure. We're running out of memory.
duke@435 527 // More complete mark sweep and allocate in young generation.
duke@435 528 if (result == NULL) {
coleenp@4037 529 do_full_collection(true);
tonyp@2971 530 result = young_gen()->allocate(size);
duke@435 531 }
duke@435 532
duke@435 533 // Fifth level allocation failure.
duke@435 534 // After more complete mark sweep, allocate in old generation.
tonyp@2971 535 if (result == NULL) {
tonyp@2971 536 result = old_gen()->allocate(size);
duke@435 537 }
duke@435 538
duke@435 539 return result;
duke@435 540 }
duke@435 541
duke@435 542 void ParallelScavengeHeap::ensure_parsability(bool retire_tlabs) {
duke@435 543 CollectedHeap::ensure_parsability(retire_tlabs);
duke@435 544 young_gen()->eden_space()->ensure_parsability();
duke@435 545 }
duke@435 546
duke@435 547 size_t ParallelScavengeHeap::unsafe_max_alloc() {
duke@435 548 return young_gen()->eden_space()->free_in_bytes();
duke@435 549 }
duke@435 550
duke@435 551 size_t ParallelScavengeHeap::tlab_capacity(Thread* thr) const {
duke@435 552 return young_gen()->eden_space()->tlab_capacity(thr);
duke@435 553 }
duke@435 554
duke@435 555 size_t ParallelScavengeHeap::unsafe_max_tlab_alloc(Thread* thr) const {
duke@435 556 return young_gen()->eden_space()->unsafe_max_tlab_alloc(thr);
duke@435 557 }
duke@435 558
duke@435 559 HeapWord* ParallelScavengeHeap::allocate_new_tlab(size_t size) {
tonyp@2971 560 return young_gen()->allocate(size);
duke@435 561 }
duke@435 562
duke@435 563 void ParallelScavengeHeap::accumulate_statistics_all_tlabs() {
duke@435 564 CollectedHeap::accumulate_statistics_all_tlabs();
duke@435 565 }
duke@435 566
duke@435 567 void ParallelScavengeHeap::resize_all_tlabs() {
duke@435 568 CollectedHeap::resize_all_tlabs();
duke@435 569 }
duke@435 570
ysr@1462 571 bool ParallelScavengeHeap::can_elide_initializing_store_barrier(oop new_obj) {
ysr@1462 572 // We don't need barriers for stores to objects in the
ysr@1462 573 // young gen and, a fortiori, for initializing stores to
ysr@1462 574 // objects therein.
ysr@1462 575 return is_in_young(new_obj);
ysr@1462 576 }
ysr@1462 577
duke@435 578 // This method is used by System.gc() and JVMTI.
duke@435 579 void ParallelScavengeHeap::collect(GCCause::Cause cause) {
duke@435 580 assert(!Heap_lock->owned_by_self(),
duke@435 581 "this thread should not own the Heap_lock");
duke@435 582
duke@435 583 unsigned int gc_count = 0;
duke@435 584 unsigned int full_gc_count = 0;
duke@435 585 {
duke@435 586 MutexLocker ml(Heap_lock);
duke@435 587 // This value is guarded by the Heap_lock
duke@435 588 gc_count = Universe::heap()->total_collections();
duke@435 589 full_gc_count = Universe::heap()->total_full_collections();
duke@435 590 }
duke@435 591
duke@435 592 VM_ParallelGCSystemGC op(gc_count, full_gc_count, cause);
duke@435 593 VMThread::execute(&op);
duke@435 594 }
duke@435 595
coleenp@4037 596 void ParallelScavengeHeap::oop_iterate(ExtendedOopClosure* cl) {
duke@435 597 Unimplemented();
duke@435 598 }
duke@435 599
duke@435 600 void ParallelScavengeHeap::object_iterate(ObjectClosure* cl) {
duke@435 601 young_gen()->object_iterate(cl);
duke@435 602 old_gen()->object_iterate(cl);
duke@435 603 }
duke@435 604
duke@435 605
duke@435 606 HeapWord* ParallelScavengeHeap::block_start(const void* addr) const {
duke@435 607 if (young_gen()->is_in_reserved(addr)) {
duke@435 608 assert(young_gen()->is_in(addr),
duke@435 609 "addr should be in allocated part of young gen");
never@2262 610 // called from os::print_location by find or VMError
never@2262 611 if (Debugging || VMError::fatal_error_in_progress()) return NULL;
duke@435 612 Unimplemented();
duke@435 613 } else if (old_gen()->is_in_reserved(addr)) {
duke@435 614 assert(old_gen()->is_in(addr),
duke@435 615 "addr should be in allocated part of old gen");
duke@435 616 return old_gen()->start_array()->object_start((HeapWord*)addr);
duke@435 617 }
duke@435 618 return 0;
duke@435 619 }
duke@435 620
duke@435 621 size_t ParallelScavengeHeap::block_size(const HeapWord* addr) const {
duke@435 622 return oop(addr)->size();
duke@435 623 }
duke@435 624
duke@435 625 bool ParallelScavengeHeap::block_is_obj(const HeapWord* addr) const {
duke@435 626 return block_start(addr) == addr;
duke@435 627 }
duke@435 628
duke@435 629 jlong ParallelScavengeHeap::millis_since_last_gc() {
duke@435 630 return UseParallelOldGC ?
duke@435 631 PSParallelCompact::millis_since_last_gc() :
duke@435 632 PSMarkSweep::millis_since_last_gc();
duke@435 633 }
duke@435 634
duke@435 635 void ParallelScavengeHeap::prepare_for_verify() {
duke@435 636 ensure_parsability(false); // no need to retire TLABs for verification
duke@435 637 }
duke@435 638
duke@435 639 void ParallelScavengeHeap::print_on(outputStream* st) const {
duke@435 640 young_gen()->print_on(st);
duke@435 641 old_gen()->print_on(st);
coleenp@4037 642 MetaspaceAux::print_on(st);
duke@435 643 }
duke@435 644
duke@435 645 void ParallelScavengeHeap::gc_threads_do(ThreadClosure* tc) const {
duke@435 646 PSScavenge::gc_task_manager()->threads_do(tc);
duke@435 647 }
duke@435 648
duke@435 649 void ParallelScavengeHeap::print_gc_threads_on(outputStream* st) const {
duke@435 650 PSScavenge::gc_task_manager()->print_threads_on(st);
duke@435 651 }
duke@435 652
duke@435 653 void ParallelScavengeHeap::print_tracing_info() const {
duke@435 654 if (TraceGen0Time) {
duke@435 655 double time = PSScavenge::accumulated_time()->seconds();
duke@435 656 tty->print_cr("[Accumulated GC generation 0 time %3.7f secs]", time);
duke@435 657 }
duke@435 658 if (TraceGen1Time) {
tschatzl@4785 659 double time = UseParallelOldGC ? PSParallelCompact::accumulated_time()->seconds() : PSMarkSweep::accumulated_time()->seconds();
duke@435 660 tty->print_cr("[Accumulated GC generation 1 time %3.7f secs]", time);
duke@435 661 }
duke@435 662 }
duke@435 663
duke@435 664
brutisso@3711 665 void ParallelScavengeHeap::verify(bool silent, VerifyOption option /* ignored */) {
duke@435 666 // Why do we need the total_collections()-filter below?
duke@435 667 if (total_collections() > 0) {
duke@435 668 if (!silent) {
duke@435 669 gclog_or_tty->print("tenured ");
duke@435 670 }
brutisso@3711 671 old_gen()->verify();
duke@435 672
duke@435 673 if (!silent) {
duke@435 674 gclog_or_tty->print("eden ");
duke@435 675 }
brutisso@3711 676 young_gen()->verify();
duke@435 677 }
duke@435 678 }
duke@435 679
duke@435 680 void ParallelScavengeHeap::print_heap_change(size_t prev_used) {
duke@435 681 if (PrintGCDetails && Verbose) {
duke@435 682 gclog_or_tty->print(" " SIZE_FORMAT
duke@435 683 "->" SIZE_FORMAT
duke@435 684 "(" SIZE_FORMAT ")",
duke@435 685 prev_used, used(), capacity());
duke@435 686 } else {
duke@435 687 gclog_or_tty->print(" " SIZE_FORMAT "K"
duke@435 688 "->" SIZE_FORMAT "K"
duke@435 689 "(" SIZE_FORMAT "K)",
duke@435 690 prev_used / K, used() / K, capacity() / K);
duke@435 691 }
duke@435 692 }
duke@435 693
duke@435 694 ParallelScavengeHeap* ParallelScavengeHeap::heap() {
duke@435 695 assert(_psh != NULL, "Uninitialized access to ParallelScavengeHeap::heap()");
duke@435 696 assert(_psh->kind() == CollectedHeap::ParallelScavengeHeap, "not a parallel scavenge heap");
duke@435 697 return _psh;
duke@435 698 }
duke@435 699
duke@435 700 // Before delegating the resize to the young generation,
duke@435 701 // the reserved space for the young and old generations
duke@435 702 // may be changed to accomodate the desired resize.
duke@435 703 void ParallelScavengeHeap::resize_young_gen(size_t eden_size,
duke@435 704 size_t survivor_size) {
duke@435 705 if (UseAdaptiveGCBoundary) {
duke@435 706 if (size_policy()->bytes_absorbed_from_eden() != 0) {
duke@435 707 size_policy()->reset_bytes_absorbed_from_eden();
duke@435 708 return; // The generation changed size already.
duke@435 709 }
duke@435 710 gens()->adjust_boundary_for_young_gen_needs(eden_size, survivor_size);
duke@435 711 }
duke@435 712
duke@435 713 // Delegate the resize to the generation.
duke@435 714 _young_gen->resize(eden_size, survivor_size);
duke@435 715 }
duke@435 716
duke@435 717 // Before delegating the resize to the old generation,
duke@435 718 // the reserved space for the young and old generations
duke@435 719 // may be changed to accomodate the desired resize.
duke@435 720 void ParallelScavengeHeap::resize_old_gen(size_t desired_free_space) {
duke@435 721 if (UseAdaptiveGCBoundary) {
duke@435 722 if (size_policy()->bytes_absorbed_from_eden() != 0) {
duke@435 723 size_policy()->reset_bytes_absorbed_from_eden();
duke@435 724 return; // The generation changed size already.
duke@435 725 }
duke@435 726 gens()->adjust_boundary_for_old_gen_needs(desired_free_space);
duke@435 727 }
duke@435 728
duke@435 729 // Delegate the resize to the generation.
duke@435 730 _old_gen->resize(desired_free_space);
duke@435 731 }
jmasa@698 732
jrose@1424 733 ParallelScavengeHeap::ParStrongRootsScope::ParStrongRootsScope() {
jrose@1424 734 // nothing particular
jrose@1424 735 }
jrose@1424 736
jrose@1424 737 ParallelScavengeHeap::ParStrongRootsScope::~ParStrongRootsScope() {
jrose@1424 738 // nothing particular
jrose@1424 739 }
jrose@1424 740
jmasa@698 741 #ifndef PRODUCT
jmasa@698 742 void ParallelScavengeHeap::record_gen_tops_before_GC() {
jmasa@698 743 if (ZapUnusedHeapArea) {
jmasa@698 744 young_gen()->record_spaces_top();
jmasa@698 745 old_gen()->record_spaces_top();
jmasa@698 746 }
jmasa@698 747 }
jmasa@698 748
jmasa@698 749 void ParallelScavengeHeap::gen_mangle_unused_area() {
jmasa@698 750 if (ZapUnusedHeapArea) {
jmasa@698 751 young_gen()->eden_space()->mangle_unused_area();
jmasa@698 752 young_gen()->to_space()->mangle_unused_area();
jmasa@698 753 young_gen()->from_space()->mangle_unused_area();
jmasa@698 754 old_gen()->object_space()->mangle_unused_area();
jmasa@698 755 }
jmasa@698 756 }
jmasa@698 757 #endif

mercurial