duke@435: /* jcoomes@3541: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/adjoiningGenerations.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/adjoiningVirtualSpaces.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/cardTableExtension.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/gcTaskManager.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/generationSizer.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psMarkSweep.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psParallelCompact.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psPromotionManager.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psScavenge.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/vmPSOperations.hpp" stefank@2314: #include "memory/gcLocker.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "runtime/vmThread.hpp" zgu@3900: #include "services/memTracker.hpp" stefank@2314: #include "utilities/vmError.hpp" duke@435: duke@435: PSYoungGen* ParallelScavengeHeap::_young_gen = NULL; duke@435: PSOldGen* ParallelScavengeHeap::_old_gen = NULL; duke@435: PSAdaptiveSizePolicy* ParallelScavengeHeap::_size_policy = NULL; duke@435: PSGCAdaptivePolicyCounters* ParallelScavengeHeap::_gc_policy_counters = NULL; duke@435: ParallelScavengeHeap* ParallelScavengeHeap::_psh = NULL; duke@435: GCTaskManager* ParallelScavengeHeap::_gc_task_manager = NULL; duke@435: duke@435: static void trace_gen_sizes(const char* const str, duke@435: size_t og_min, size_t og_max, duke@435: size_t yg_min, size_t yg_max) duke@435: { duke@435: if (TracePageSizes) { duke@435: tty->print_cr("%s: " SIZE_FORMAT "," SIZE_FORMAT " " duke@435: SIZE_FORMAT "," SIZE_FORMAT " " duke@435: SIZE_FORMAT, coleenp@4037: str, duke@435: og_min / K, og_max / K, duke@435: yg_min / K, yg_max / K, coleenp@4037: (og_max + yg_max) / K); duke@435: } duke@435: } duke@435: duke@435: jint ParallelScavengeHeap::initialize() { ysr@1601: CollectedHeap::pre_initialize(); ysr@1601: duke@435: // Cannot be initialized until after the flags are parsed jmasa@1822: // GenerationSizer flag_parser; jmasa@1822: _collector_policy = new GenerationSizer(); duke@435: jmasa@1822: size_t yg_min_size = _collector_policy->min_young_gen_size(); jmasa@1822: size_t yg_max_size = _collector_policy->max_young_gen_size(); jmasa@1822: size_t og_min_size = _collector_policy->min_old_gen_size(); jmasa@1822: size_t og_max_size = _collector_policy->max_old_gen_size(); duke@435: duke@435: trace_gen_sizes("ps heap raw", duke@435: og_min_size, og_max_size, duke@435: yg_min_size, yg_max_size); duke@435: duke@435: const size_t og_page_sz = os::page_size_for_region(yg_min_size + og_min_size, duke@435: yg_max_size + og_max_size, duke@435: 8); duke@435: duke@435: const size_t og_align = set_alignment(_old_gen_alignment, og_page_sz); duke@435: const size_t yg_align = set_alignment(_young_gen_alignment, og_page_sz); duke@435: duke@435: // Update sizes to reflect the selected page size(s). duke@435: // duke@435: // NEEDS_CLEANUP. The default TwoGenerationCollectorPolicy uses NewRatio; it duke@435: // should check UseAdaptiveSizePolicy. Changes from generationSizer could duke@435: // move to the common code. duke@435: yg_min_size = align_size_up(yg_min_size, yg_align); duke@435: yg_max_size = align_size_up(yg_max_size, yg_align); jmasa@1822: size_t yg_cur_size = jmasa@1822: align_size_up(_collector_policy->young_gen_size(), yg_align); duke@435: yg_cur_size = MAX2(yg_cur_size, yg_min_size); duke@435: duke@435: og_min_size = align_size_up(og_min_size, og_align); kvn@2558: // Align old gen size down to preserve specified heap size. kvn@2558: assert(og_align == yg_align, "sanity"); kvn@2558: og_max_size = align_size_down(og_max_size, og_align); kvn@2558: og_max_size = MAX2(og_max_size, og_min_size); jmasa@1822: size_t og_cur_size = kvn@2558: align_size_down(_collector_policy->old_gen_size(), og_align); duke@435: og_cur_size = MAX2(og_cur_size, og_min_size); duke@435: duke@435: trace_gen_sizes("ps heap rnd", duke@435: og_min_size, og_max_size, duke@435: yg_min_size, yg_max_size); duke@435: coleenp@4037: const size_t heap_size = og_max_size + yg_max_size; kvn@1077: coleenp@4037: ReservedSpace heap_rs = Universe::reserve_heap(heap_size, og_align); kvn@1077: zgu@3900: MemTracker::record_virtual_memory_type((address)heap_rs.base(), mtJavaHeap); zgu@3900: duke@435: os::trace_page_sizes("ps main", og_min_size + yg_min_size, duke@435: og_max_size + yg_max_size, og_page_sz, coleenp@4037: heap_rs.base(), coleenp@4037: heap_rs.size()); duke@435: if (!heap_rs.is_reserved()) { duke@435: vm_shutdown_during_initialization( duke@435: "Could not reserve enough space for object heap"); duke@435: return JNI_ENOMEM; duke@435: } duke@435: duke@435: _reserved = MemRegion((HeapWord*)heap_rs.base(), duke@435: (HeapWord*)(heap_rs.base() + heap_rs.size())); duke@435: duke@435: CardTableExtension* const barrier_set = new CardTableExtension(_reserved, 3); duke@435: _barrier_set = barrier_set; duke@435: oopDesc::set_bs(_barrier_set); duke@435: if (_barrier_set == NULL) { duke@435: vm_shutdown_during_initialization( duke@435: "Could not reserve enough space for barrier set"); duke@435: return JNI_ENOMEM; duke@435: } duke@435: duke@435: // Initial young gen size is 4 Mb duke@435: // duke@435: // XXX - what about flag_parser.young_gen_size()? duke@435: const size_t init_young_size = align_size_up(4 * M, yg_align); duke@435: yg_cur_size = MAX2(MIN2(init_young_size, yg_max_size), yg_cur_size); duke@435: duke@435: // Make up the generations duke@435: // Calculate the maximum size that a generation can grow. This duke@435: // includes growth into the other generation. Note that the duke@435: // parameter _max_gen_size is kept as the maximum duke@435: // size of the generation as the boundaries currently stand. duke@435: // _max_gen_size is still used as that value. duke@435: double max_gc_pause_sec = ((double) MaxGCPauseMillis)/1000.0; duke@435: double max_gc_minor_pause_sec = ((double) MaxGCMinorPauseMillis)/1000.0; duke@435: coleenp@4037: _gens = new AdjoiningGenerations(heap_rs, duke@435: og_cur_size, duke@435: og_min_size, duke@435: og_max_size, duke@435: yg_cur_size, duke@435: yg_min_size, duke@435: yg_max_size, duke@435: yg_align); duke@435: duke@435: _old_gen = _gens->old_gen(); duke@435: _young_gen = _gens->young_gen(); duke@435: duke@435: const size_t eden_capacity = _young_gen->eden_space()->capacity_in_bytes(); duke@435: const size_t old_capacity = _old_gen->capacity_in_bytes(); duke@435: const size_t initial_promo_size = MIN2(eden_capacity, old_capacity); duke@435: _size_policy = duke@435: new PSAdaptiveSizePolicy(eden_capacity, duke@435: initial_promo_size, duke@435: young_gen()->to_space()->capacity_in_bytes(), jmasa@448: intra_heap_alignment(), duke@435: max_gc_pause_sec, duke@435: max_gc_minor_pause_sec, duke@435: GCTimeRatio duke@435: ); duke@435: duke@435: assert(!UseAdaptiveGCBoundary || duke@435: (old_gen()->virtual_space()->high_boundary() == duke@435: young_gen()->virtual_space()->low_boundary()), duke@435: "Boundaries must meet"); duke@435: // initialize the policy counters - 2 collectors, 3 generations duke@435: _gc_policy_counters = duke@435: new PSGCAdaptivePolicyCounters("ParScav:MSC", 2, 3, _size_policy); duke@435: _psh = this; duke@435: duke@435: // Set up the GCTaskManager duke@435: _gc_task_manager = GCTaskManager::create(ParallelGCThreads); duke@435: duke@435: if (UseParallelOldGC && !PSParallelCompact::initialize()) { duke@435: return JNI_ENOMEM; duke@435: } duke@435: duke@435: return JNI_OK; duke@435: } duke@435: duke@435: void ParallelScavengeHeap::post_initialize() { duke@435: // Need to init the tenuring threshold duke@435: PSScavenge::initialize(); duke@435: if (UseParallelOldGC) { duke@435: PSParallelCompact::post_initialize(); duke@435: } else { duke@435: PSMarkSweep::initialize(); duke@435: } duke@435: PSPromotionManager::initialize(); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::update_counters() { duke@435: young_gen()->update_counters(); duke@435: old_gen()->update_counters(); coleenp@4037: MetaspaceCounters::update_performance_counters(); duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::capacity() const { duke@435: size_t value = young_gen()->capacity_in_bytes() + old_gen()->capacity_in_bytes(); duke@435: return value; duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::used() const { duke@435: size_t value = young_gen()->used_in_bytes() + old_gen()->used_in_bytes(); duke@435: return value; duke@435: } duke@435: duke@435: bool ParallelScavengeHeap::is_maximal_no_gc() const { duke@435: return old_gen()->is_maximal_no_gc() && young_gen()->is_maximal_no_gc(); duke@435: } duke@435: duke@435: duke@435: size_t ParallelScavengeHeap::max_capacity() const { duke@435: size_t estimated = reserved_region().byte_size(); duke@435: if (UseAdaptiveSizePolicy) { duke@435: estimated -= _size_policy->max_survivor_size(young_gen()->max_size()); duke@435: } else { duke@435: estimated -= young_gen()->to_space()->capacity_in_bytes(); duke@435: } duke@435: return MAX2(estimated, capacity()); duke@435: } duke@435: duke@435: bool ParallelScavengeHeap::is_in(const void* p) const { duke@435: if (young_gen()->is_in(p)) { duke@435: return true; duke@435: } duke@435: duke@435: if (old_gen()->is_in(p)) { duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: bool ParallelScavengeHeap::is_in_reserved(const void* p) const { duke@435: if (young_gen()->is_in_reserved(p)) { duke@435: return true; duke@435: } duke@435: duke@435: if (old_gen()->is_in_reserved(p)) { duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: jmasa@2909: bool ParallelScavengeHeap::is_scavengable(const void* addr) { jmasa@2909: return is_in_young((oop)addr); jmasa@2909: } jmasa@2909: jmasa@2909: #ifdef ASSERT jmasa@2909: // Don't implement this by using is_in_young(). This method is used jmasa@2909: // in some cases to check that is_in_young() is correct. jmasa@2909: bool ParallelScavengeHeap::is_in_partial_collection(const void *p) { jmasa@2909: assert(is_in_reserved(p) || p == NULL, jmasa@2909: "Does not work if address is non-null and outside of the heap"); coleenp@4037: // The order of the generations is old (low addr), young (high addr) jmasa@2909: return p >= old_gen()->reserved().end(); jmasa@2909: } jmasa@2909: #endif jmasa@2909: duke@435: // There are two levels of allocation policy here. duke@435: // duke@435: // When an allocation request fails, the requesting thread must invoke a VM duke@435: // operation, transfer control to the VM thread, and await the results of a duke@435: // garbage collection. That is quite expensive, and we should avoid doing it duke@435: // multiple times if possible. duke@435: // duke@435: // To accomplish this, we have a basic allocation policy, and also a duke@435: // failed allocation policy. duke@435: // duke@435: // The basic allocation policy controls how you allocate memory without duke@435: // attempting garbage collection. It is okay to grab locks and duke@435: // expand the heap, if that can be done without coming to a safepoint. duke@435: // It is likely that the basic allocation policy will not be very duke@435: // aggressive. duke@435: // duke@435: // The failed allocation policy is invoked from the VM thread after duke@435: // the basic allocation policy is unable to satisfy a mem_allocate duke@435: // request. This policy needs to cover the entire range of collection, duke@435: // heap expansion, and out-of-memory conditions. It should make every duke@435: // attempt to allocate the requested memory. duke@435: duke@435: // Basic allocation policy. Should never be called at a safepoint, or duke@435: // from the VM thread. duke@435: // duke@435: // This method must handle cases where many mem_allocate requests fail duke@435: // simultaneously. When that happens, only one VM operation will succeed, duke@435: // and the rest will not be executed. For that reason, this method loops duke@435: // during failed allocation attempts. If the java heap becomes exhausted, duke@435: // we rely on the size_policy object to force a bail out. duke@435: HeapWord* ParallelScavengeHeap::mem_allocate( duke@435: size_t size, duke@435: bool* gc_overhead_limit_was_exceeded) { duke@435: assert(!SafepointSynchronize::is_at_safepoint(), "should not be at safepoint"); duke@435: assert(Thread::current() != (Thread*)VMThread::vm_thread(), "should not be in vm thread"); duke@435: assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock"); duke@435: jmasa@1822: // In general gc_overhead_limit_was_exceeded should be false so jmasa@1822: // set it so here and reset it to true only if the gc time jmasa@1822: // limit is being exceeded as checked below. jmasa@1822: *gc_overhead_limit_was_exceeded = false; jmasa@1822: tonyp@2971: HeapWord* result = young_gen()->allocate(size); duke@435: duke@435: uint loop_count = 0; duke@435: uint gc_count = 0; mgerdin@4853: int gclocker_stalled_count = 0; duke@435: duke@435: while (result == NULL) { duke@435: // We don't want to have multiple collections for a single filled generation. duke@435: // To prevent this, each thread tracks the total_collections() value, and if duke@435: // the count has changed, does not do a new collection. duke@435: // duke@435: // The collection count must be read only while holding the heap lock. VM duke@435: // operations also hold the heap lock during collections. There is a lock duke@435: // contention case where thread A blocks waiting on the Heap_lock, while duke@435: // thread B is holding it doing a collection. When thread A gets the lock, duke@435: // the collection count has already changed. To prevent duplicate collections, duke@435: // The policy MUST attempt allocations during the same period it reads the duke@435: // total_collections() value! duke@435: { duke@435: MutexLocker ml(Heap_lock); duke@435: gc_count = Universe::heap()->total_collections(); duke@435: tonyp@2971: result = young_gen()->allocate(size); duke@435: if (result != NULL) { duke@435: return result; duke@435: } jcoomes@3541: jcoomes@3541: // If certain conditions hold, try allocating from the old gen. jcoomes@3541: result = mem_allocate_old_gen(size); jcoomes@3541: if (result != NULL) { jcoomes@3541: return result; duke@435: } jcoomes@3541: mgerdin@4853: if (gclocker_stalled_count > GCLockerRetryAllocationCount) { mgerdin@4853: return NULL; mgerdin@4853: } mgerdin@4853: jcoomes@3541: // Failed to allocate without a gc. duke@435: if (GC_locker::is_active_and_needs_gc()) { duke@435: // If this thread is not in a jni critical section, we stall duke@435: // the requestor until the critical section has cleared and duke@435: // GC allowed. When the critical section clears, a GC is duke@435: // initiated by the last thread exiting the critical section; so duke@435: // we retry the allocation sequence from the beginning of the loop, duke@435: // rather than causing more, now probably unnecessary, GC attempts. duke@435: JavaThread* jthr = JavaThread::current(); duke@435: if (!jthr->in_critical()) { duke@435: MutexUnlocker mul(Heap_lock); duke@435: GC_locker::stall_until_clear(); mgerdin@4853: gclocker_stalled_count += 1; duke@435: continue; duke@435: } else { duke@435: if (CheckJNICalls) { duke@435: fatal("Possible deadlock due to allocating while" duke@435: " in jni critical section"); duke@435: } duke@435: return NULL; duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (result == NULL) { duke@435: // Generate a VM operation tonyp@2971: VM_ParallelGCFailedAllocation op(size, gc_count); duke@435: VMThread::execute(&op); duke@435: duke@435: // Did the VM operation execute? If so, return the result directly. duke@435: // This prevents us from looping until time out on requests that can duke@435: // not be satisfied. duke@435: if (op.prologue_succeeded()) { duke@435: assert(Universe::heap()->is_in_or_null(op.result()), duke@435: "result not in heap"); duke@435: duke@435: // If GC was locked out during VM operation then retry allocation duke@435: // and/or stall as necessary. duke@435: if (op.gc_locked()) { duke@435: assert(op.result() == NULL, "must be NULL if gc_locked() is true"); duke@435: continue; // retry and/or stall as necessary duke@435: } jmasa@1822: jmasa@1822: // Exit the loop if the gc time limit has been exceeded. jmasa@1822: // The allocation must have failed above ("result" guarding jmasa@1822: // this path is NULL) and the most recent collection has exceeded the jmasa@1822: // gc overhead limit (although enough may have been collected to jmasa@1822: // satisfy the allocation). Exit the loop so that an out-of-memory jmasa@1822: // will be thrown (return a NULL ignoring the contents of jmasa@1822: // op.result()), jmasa@1822: // but clear gc_overhead_limit_exceeded so that the next collection jmasa@1822: // starts with a clean slate (i.e., forgets about previous overhead jmasa@1822: // excesses). Fill op.result() with a filler object so that the jmasa@1822: // heap remains parsable. jmasa@1822: const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded(); jmasa@1822: const bool softrefs_clear = collector_policy()->all_soft_refs_clear(); jmasa@4743: jmasa@1822: if (limit_exceeded && softrefs_clear) { jmasa@1822: *gc_overhead_limit_was_exceeded = true; jmasa@1822: size_policy()->set_gc_overhead_limit_exceeded(false); jmasa@1822: if (PrintGCDetails && Verbose) { jmasa@1822: gclog_or_tty->print_cr("ParallelScavengeHeap::mem_allocate: " jmasa@1822: "return NULL because gc_overhead_limit_exceeded is set"); jmasa@1822: } jmasa@1822: if (op.result() != NULL) { jmasa@1822: CollectedHeap::fill_with_object(op.result(), size); jmasa@1822: } jmasa@1822: return NULL; duke@435: } jmasa@1822: duke@435: return op.result(); duke@435: } duke@435: } duke@435: duke@435: // The policy object will prevent us from looping forever. If the duke@435: // time spent in gc crosses a threshold, we will bail out. duke@435: loop_count++; duke@435: if ((result == NULL) && (QueuedAllocationWarningCount > 0) && duke@435: (loop_count % QueuedAllocationWarningCount == 0)) { duke@435: warning("ParallelScavengeHeap::mem_allocate retries %d times \n\t" tonyp@2971: " size=%d", loop_count, size); duke@435: } duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: jcoomes@3541: // A "death march" is a series of ultra-slow allocations in which a full gc is jcoomes@3541: // done before each allocation, and after the full gc the allocation still jcoomes@3541: // cannot be satisfied from the young gen. This routine detects that condition; jcoomes@3541: // it should be called after a full gc has been done and the allocation jcoomes@3541: // attempted from the young gen. The parameter 'addr' should be the result of jcoomes@3541: // that young gen allocation attempt. jcoomes@3541: void jcoomes@3541: ParallelScavengeHeap::death_march_check(HeapWord* const addr, size_t size) { jcoomes@3541: if (addr != NULL) { jcoomes@3541: _death_march_count = 0; // death march has ended jcoomes@3541: } else if (_death_march_count == 0) { jcoomes@3541: if (should_alloc_in_eden(size)) { jcoomes@3541: _death_march_count = 1; // death march has started jcoomes@3541: } jcoomes@3541: } jcoomes@3541: } jcoomes@3541: jcoomes@3541: HeapWord* ParallelScavengeHeap::mem_allocate_old_gen(size_t size) { jcoomes@3541: if (!should_alloc_in_eden(size) || GC_locker::is_active_and_needs_gc()) { jcoomes@3541: // Size is too big for eden, or gc is locked out. jcoomes@3541: return old_gen()->allocate(size); jcoomes@3541: } jcoomes@3541: jcoomes@3541: // If a "death march" is in progress, allocate from the old gen a limited jcoomes@3541: // number of times before doing a GC. jcoomes@3541: if (_death_march_count > 0) { jcoomes@3541: if (_death_march_count < 64) { jcoomes@3541: ++_death_march_count; jcoomes@3541: return old_gen()->allocate(size); jcoomes@3541: } else { jcoomes@3541: _death_march_count = 0; jcoomes@3541: } jcoomes@3541: } jcoomes@3541: return NULL; jcoomes@3541: } jcoomes@3541: coleenp@4037: void ParallelScavengeHeap::do_full_collection(bool clear_all_soft_refs) { coleenp@4037: if (UseParallelOldGC) { coleenp@4037: // The do_full_collection() parameter clear_all_soft_refs coleenp@4037: // is interpreted here as maximum_compaction which will coleenp@4037: // cause SoftRefs to be cleared. coleenp@4037: bool maximum_compaction = clear_all_soft_refs; coleenp@4037: PSParallelCompact::invoke(maximum_compaction); coleenp@4037: } else { coleenp@4037: PSMarkSweep::invoke(clear_all_soft_refs); coleenp@4037: } coleenp@4037: } coleenp@4037: duke@435: // Failed allocation policy. Must be called from the VM thread, and duke@435: // only at a safepoint! Note that this method has policy for allocation duke@435: // flow, and NOT collection policy. So we do not check for gc collection duke@435: // time over limit here, that is the responsibility of the heap specific duke@435: // collection methods. This method decides where to attempt allocations, duke@435: // and when to attempt collections, but no collection specific policy. tonyp@2971: HeapWord* ParallelScavengeHeap::failed_mem_allocate(size_t size) { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint"); duke@435: assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread"); duke@435: assert(!Universe::heap()->is_gc_active(), "not reentrant"); duke@435: assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock"); duke@435: jcoomes@3541: // We assume that allocation in eden will fail unless we collect. duke@435: duke@435: // First level allocation failure, scavenge and allocate in young gen. duke@435: GCCauseSetter gccs(this, GCCause::_allocation_failure); jcoomes@3541: const bool invoked_full_gc = PSScavenge::invoke(); tonyp@2971: HeapWord* result = young_gen()->allocate(size); duke@435: duke@435: // Second level allocation failure. duke@435: // Mark sweep and allocate in young generation. jcoomes@3541: if (result == NULL && !invoked_full_gc) { coleenp@4037: do_full_collection(false); jcoomes@3541: result = young_gen()->allocate(size); duke@435: } duke@435: jcoomes@3541: death_march_check(result, size); jcoomes@3541: duke@435: // Third level allocation failure. duke@435: // After mark sweep and young generation allocation failure, duke@435: // allocate in old generation. tonyp@2971: if (result == NULL) { tonyp@2971: result = old_gen()->allocate(size); duke@435: } duke@435: duke@435: // Fourth level allocation failure. We're running out of memory. duke@435: // More complete mark sweep and allocate in young generation. duke@435: if (result == NULL) { coleenp@4037: do_full_collection(true); tonyp@2971: result = young_gen()->allocate(size); duke@435: } duke@435: duke@435: // Fifth level allocation failure. duke@435: // After more complete mark sweep, allocate in old generation. tonyp@2971: if (result == NULL) { tonyp@2971: result = old_gen()->allocate(size); duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: void ParallelScavengeHeap::ensure_parsability(bool retire_tlabs) { duke@435: CollectedHeap::ensure_parsability(retire_tlabs); duke@435: young_gen()->eden_space()->ensure_parsability(); duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::unsafe_max_alloc() { duke@435: return young_gen()->eden_space()->free_in_bytes(); duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::tlab_capacity(Thread* thr) const { duke@435: return young_gen()->eden_space()->tlab_capacity(thr); duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::unsafe_max_tlab_alloc(Thread* thr) const { duke@435: return young_gen()->eden_space()->unsafe_max_tlab_alloc(thr); duke@435: } duke@435: duke@435: HeapWord* ParallelScavengeHeap::allocate_new_tlab(size_t size) { tonyp@2971: return young_gen()->allocate(size); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::accumulate_statistics_all_tlabs() { duke@435: CollectedHeap::accumulate_statistics_all_tlabs(); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::resize_all_tlabs() { duke@435: CollectedHeap::resize_all_tlabs(); duke@435: } duke@435: ysr@1462: bool ParallelScavengeHeap::can_elide_initializing_store_barrier(oop new_obj) { ysr@1462: // We don't need barriers for stores to objects in the ysr@1462: // young gen and, a fortiori, for initializing stores to ysr@1462: // objects therein. ysr@1462: return is_in_young(new_obj); ysr@1462: } ysr@1462: duke@435: // This method is used by System.gc() and JVMTI. duke@435: void ParallelScavengeHeap::collect(GCCause::Cause cause) { duke@435: assert(!Heap_lock->owned_by_self(), duke@435: "this thread should not own the Heap_lock"); duke@435: duke@435: unsigned int gc_count = 0; duke@435: unsigned int full_gc_count = 0; duke@435: { duke@435: MutexLocker ml(Heap_lock); duke@435: // This value is guarded by the Heap_lock duke@435: gc_count = Universe::heap()->total_collections(); duke@435: full_gc_count = Universe::heap()->total_full_collections(); duke@435: } duke@435: duke@435: VM_ParallelGCSystemGC op(gc_count, full_gc_count, cause); duke@435: VMThread::execute(&op); duke@435: } duke@435: coleenp@4037: void ParallelScavengeHeap::oop_iterate(ExtendedOopClosure* cl) { duke@435: Unimplemented(); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::object_iterate(ObjectClosure* cl) { duke@435: young_gen()->object_iterate(cl); duke@435: old_gen()->object_iterate(cl); duke@435: } duke@435: duke@435: duke@435: HeapWord* ParallelScavengeHeap::block_start(const void* addr) const { duke@435: if (young_gen()->is_in_reserved(addr)) { duke@435: assert(young_gen()->is_in(addr), duke@435: "addr should be in allocated part of young gen"); never@2262: // called from os::print_location by find or VMError never@2262: if (Debugging || VMError::fatal_error_in_progress()) return NULL; duke@435: Unimplemented(); duke@435: } else if (old_gen()->is_in_reserved(addr)) { duke@435: assert(old_gen()->is_in(addr), duke@435: "addr should be in allocated part of old gen"); duke@435: return old_gen()->start_array()->object_start((HeapWord*)addr); duke@435: } duke@435: return 0; duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::block_size(const HeapWord* addr) const { duke@435: return oop(addr)->size(); duke@435: } duke@435: duke@435: bool ParallelScavengeHeap::block_is_obj(const HeapWord* addr) const { duke@435: return block_start(addr) == addr; duke@435: } duke@435: duke@435: jlong ParallelScavengeHeap::millis_since_last_gc() { duke@435: return UseParallelOldGC ? duke@435: PSParallelCompact::millis_since_last_gc() : duke@435: PSMarkSweep::millis_since_last_gc(); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::prepare_for_verify() { duke@435: ensure_parsability(false); // no need to retire TLABs for verification duke@435: } duke@435: duke@435: void ParallelScavengeHeap::print_on(outputStream* st) const { duke@435: young_gen()->print_on(st); duke@435: old_gen()->print_on(st); coleenp@4037: MetaspaceAux::print_on(st); duke@435: } duke@435: stefank@4904: void ParallelScavengeHeap::print_on_error(outputStream* st) const { stefank@4904: this->CollectedHeap::print_on_error(st); stefank@4904: stefank@4904: if (UseParallelOldGC) { stefank@4904: st->cr(); stefank@4904: PSParallelCompact::print_on_error(st); stefank@4904: } stefank@4904: } stefank@4904: duke@435: void ParallelScavengeHeap::gc_threads_do(ThreadClosure* tc) const { duke@435: PSScavenge::gc_task_manager()->threads_do(tc); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::print_gc_threads_on(outputStream* st) const { duke@435: PSScavenge::gc_task_manager()->print_threads_on(st); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::print_tracing_info() const { duke@435: if (TraceGen0Time) { duke@435: double time = PSScavenge::accumulated_time()->seconds(); duke@435: tty->print_cr("[Accumulated GC generation 0 time %3.7f secs]", time); duke@435: } duke@435: if (TraceGen1Time) { tschatzl@4785: double time = UseParallelOldGC ? PSParallelCompact::accumulated_time()->seconds() : PSMarkSweep::accumulated_time()->seconds(); duke@435: tty->print_cr("[Accumulated GC generation 1 time %3.7f secs]", time); duke@435: } duke@435: } duke@435: duke@435: brutisso@3711: void ParallelScavengeHeap::verify(bool silent, VerifyOption option /* ignored */) { duke@435: // Why do we need the total_collections()-filter below? duke@435: if (total_collections() > 0) { duke@435: if (!silent) { duke@435: gclog_or_tty->print("tenured "); duke@435: } brutisso@3711: old_gen()->verify(); duke@435: duke@435: if (!silent) { duke@435: gclog_or_tty->print("eden "); duke@435: } brutisso@3711: young_gen()->verify(); duke@435: } duke@435: } duke@435: duke@435: void ParallelScavengeHeap::print_heap_change(size_t prev_used) { duke@435: if (PrintGCDetails && Verbose) { duke@435: gclog_or_tty->print(" " SIZE_FORMAT duke@435: "->" SIZE_FORMAT duke@435: "(" SIZE_FORMAT ")", duke@435: prev_used, used(), capacity()); duke@435: } else { duke@435: gclog_or_tty->print(" " SIZE_FORMAT "K" duke@435: "->" SIZE_FORMAT "K" duke@435: "(" SIZE_FORMAT "K)", duke@435: prev_used / K, used() / K, capacity() / K); duke@435: } duke@435: } duke@435: duke@435: ParallelScavengeHeap* ParallelScavengeHeap::heap() { duke@435: assert(_psh != NULL, "Uninitialized access to ParallelScavengeHeap::heap()"); duke@435: assert(_psh->kind() == CollectedHeap::ParallelScavengeHeap, "not a parallel scavenge heap"); duke@435: return _psh; duke@435: } duke@435: duke@435: // Before delegating the resize to the young generation, duke@435: // the reserved space for the young and old generations duke@435: // may be changed to accomodate the desired resize. duke@435: void ParallelScavengeHeap::resize_young_gen(size_t eden_size, duke@435: size_t survivor_size) { duke@435: if (UseAdaptiveGCBoundary) { duke@435: if (size_policy()->bytes_absorbed_from_eden() != 0) { duke@435: size_policy()->reset_bytes_absorbed_from_eden(); duke@435: return; // The generation changed size already. duke@435: } duke@435: gens()->adjust_boundary_for_young_gen_needs(eden_size, survivor_size); duke@435: } duke@435: duke@435: // Delegate the resize to the generation. duke@435: _young_gen->resize(eden_size, survivor_size); duke@435: } duke@435: duke@435: // Before delegating the resize to the old generation, duke@435: // the reserved space for the young and old generations duke@435: // may be changed to accomodate the desired resize. duke@435: void ParallelScavengeHeap::resize_old_gen(size_t desired_free_space) { duke@435: if (UseAdaptiveGCBoundary) { duke@435: if (size_policy()->bytes_absorbed_from_eden() != 0) { duke@435: size_policy()->reset_bytes_absorbed_from_eden(); duke@435: return; // The generation changed size already. duke@435: } duke@435: gens()->adjust_boundary_for_old_gen_needs(desired_free_space); duke@435: } duke@435: duke@435: // Delegate the resize to the generation. duke@435: _old_gen->resize(desired_free_space); duke@435: } jmasa@698: jrose@1424: ParallelScavengeHeap::ParStrongRootsScope::ParStrongRootsScope() { jrose@1424: // nothing particular jrose@1424: } jrose@1424: jrose@1424: ParallelScavengeHeap::ParStrongRootsScope::~ParStrongRootsScope() { jrose@1424: // nothing particular jrose@1424: } jrose@1424: jmasa@698: #ifndef PRODUCT jmasa@698: void ParallelScavengeHeap::record_gen_tops_before_GC() { jmasa@698: if (ZapUnusedHeapArea) { jmasa@698: young_gen()->record_spaces_top(); jmasa@698: old_gen()->record_spaces_top(); jmasa@698: } jmasa@698: } jmasa@698: jmasa@698: void ParallelScavengeHeap::gen_mangle_unused_area() { jmasa@698: if (ZapUnusedHeapArea) { jmasa@698: young_gen()->eden_space()->mangle_unused_area(); jmasa@698: young_gen()->to_space()->mangle_unused_area(); jmasa@698: young_gen()->from_space()->mangle_unused_area(); jmasa@698: old_gen()->object_space()->mangle_unused_area(); jmasa@698: } jmasa@698: } jmasa@698: #endif