duke@435: /* xdono@631: * Copyright 2001-2008 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_parallelScavengeHeap.cpp.incl" duke@435: duke@435: PSYoungGen* ParallelScavengeHeap::_young_gen = NULL; duke@435: PSOldGen* ParallelScavengeHeap::_old_gen = NULL; duke@435: PSPermGen* ParallelScavengeHeap::_perm_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 pg_min, size_t pg_max, 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 "," SIZE_FORMAT " " duke@435: SIZE_FORMAT, duke@435: str, pg_min / K, pg_max / K, duke@435: og_min / K, og_max / K, duke@435: yg_min / K, yg_max / K, duke@435: (pg_max + og_max + yg_max) / K); duke@435: } duke@435: } duke@435: duke@435: jint ParallelScavengeHeap::initialize() { duke@435: // Cannot be initialized until after the flags are parsed duke@435: GenerationSizer flag_parser; duke@435: duke@435: size_t yg_min_size = flag_parser.min_young_gen_size(); duke@435: size_t yg_max_size = flag_parser.max_young_gen_size(); duke@435: size_t og_min_size = flag_parser.min_old_gen_size(); duke@435: size_t og_max_size = flag_parser.max_old_gen_size(); duke@435: // Why isn't there a min_perm_gen_size()? duke@435: size_t pg_min_size = flag_parser.perm_gen_size(); duke@435: size_t pg_max_size = flag_parser.max_perm_gen_size(); duke@435: duke@435: trace_gen_sizes("ps heap raw", duke@435: pg_min_size, pg_max_size, duke@435: og_min_size, og_max_size, duke@435: yg_min_size, yg_max_size); duke@435: duke@435: // The ReservedSpace ctor used below requires that the page size for the perm duke@435: // gen is <= the page size for the rest of the heap (young + old gens). 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: const size_t pg_page_sz = MIN2(os::page_size_for_region(pg_min_size, duke@435: pg_max_size, 16), duke@435: og_page_sz); duke@435: duke@435: const size_t pg_align = set_alignment(_perm_gen_alignment, pg_page_sz); 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); duke@435: size_t yg_cur_size = align_size_up(flag_parser.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); duke@435: og_max_size = align_size_up(og_max_size, og_align); duke@435: size_t og_cur_size = align_size_up(flag_parser.old_gen_size(), og_align); duke@435: og_cur_size = MAX2(og_cur_size, og_min_size); duke@435: duke@435: pg_min_size = align_size_up(pg_min_size, pg_align); duke@435: pg_max_size = align_size_up(pg_max_size, pg_align); duke@435: size_t pg_cur_size = pg_min_size; duke@435: duke@435: trace_gen_sizes("ps heap rnd", duke@435: pg_min_size, pg_max_size, duke@435: og_min_size, og_max_size, duke@435: yg_min_size, yg_max_size); duke@435: kvn@1077: const size_t total_reserved = pg_max_size + og_max_size + yg_max_size; kvn@1077: char* addr = Universe::preferred_heap_base(total_reserved, Universe::UnscaledNarrowOop); kvn@1077: duke@435: // The main part of the heap (old gen + young gen) can often use a larger page duke@435: // size than is needed or wanted for the perm gen. Use the "compound duke@435: // alignment" ReservedSpace ctor to avoid having to use the same page size for duke@435: // all gens. kvn@1077: coleenp@672: ReservedHeapSpace heap_rs(pg_max_size, pg_align, og_max_size + yg_max_size, kvn@1077: og_align, addr); kvn@1077: kvn@1077: if (UseCompressedOops) { kvn@1077: if (addr != NULL && !heap_rs.is_reserved()) { kvn@1077: // Failed to reserve at specified address - the requested memory kvn@1077: // region is taken already, for example, by 'java' launcher. kvn@1077: // Try again to reserver heap higher. kvn@1077: addr = Universe::preferred_heap_base(total_reserved, Universe::ZeroBasedNarrowOop); kvn@1077: ReservedHeapSpace heap_rs0(pg_max_size, pg_align, og_max_size + yg_max_size, kvn@1077: og_align, addr); kvn@1077: if (addr != NULL && !heap_rs0.is_reserved()) { kvn@1077: // Failed to reserve at specified address again - give up. kvn@1077: addr = Universe::preferred_heap_base(total_reserved, Universe::HeapBasedNarrowOop); kvn@1077: assert(addr == NULL, ""); kvn@1077: ReservedHeapSpace heap_rs1(pg_max_size, pg_align, og_max_size + yg_max_size, kvn@1077: og_align, addr); kvn@1077: heap_rs = heap_rs1; kvn@1077: } else { kvn@1077: heap_rs = heap_rs0; kvn@1077: } kvn@1077: } kvn@1077: } kvn@1077: duke@435: os::trace_page_sizes("ps perm", pg_min_size, pg_max_size, pg_page_sz, duke@435: heap_rs.base(), pg_max_size); 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, duke@435: heap_rs.base() + pg_max_size, duke@435: heap_rs.size() - pg_max_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: // Split the reserved space into perm gen and the main heap (everything else). duke@435: // The main heap uses a different alignment. duke@435: ReservedSpace perm_rs = heap_rs.first_part(pg_max_size); duke@435: ReservedSpace main_rs = heap_rs.last_part(pg_max_size, og_align); 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: duke@435: _gens = new AdjoiningGenerations(main_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: _perm_gen = new PSPermGen(perm_rs, duke@435: pg_align, duke@435: pg_cur_size, duke@435: pg_cur_size, duke@435: pg_max_size, duke@435: "perm", 2); 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(); duke@435: perm_gen()->update_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::permanent_capacity() const { duke@435: return perm_gen()->capacity_in_bytes(); duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::permanent_used() const { duke@435: return perm_gen()->used_in_bytes(); duke@435: } duke@435: duke@435: size_t ParallelScavengeHeap::max_capacity() const { duke@435: size_t estimated = reserved_region().byte_size(); duke@435: estimated -= perm_gen()->reserved().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: if (perm_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: if (perm_gen()->is_in_reserved(p)) { duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // Static method duke@435: bool ParallelScavengeHeap::is_in_young(oop* p) { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, duke@435: "Must be ParallelScavengeHeap"); duke@435: duke@435: PSYoungGen* young_gen = heap->young_gen(); duke@435: duke@435: if (young_gen->is_in_reserved(p)) { duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: duke@435: // Static method duke@435: bool ParallelScavengeHeap::is_in_old_or_perm(oop* p) { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, duke@435: "Must be ParallelScavengeHeap"); duke@435: duke@435: PSOldGen* old_gen = heap->old_gen(); duke@435: PSPermGen* perm_gen = heap->perm_gen(); duke@435: duke@435: if (old_gen->is_in_reserved(p)) { duke@435: return true; duke@435: } duke@435: duke@435: if (perm_gen->is_in_reserved(p)) { duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: 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 is_noref, duke@435: bool is_tlab, 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: duke@435: HeapWord* result = young_gen()->allocate(size, is_tlab); duke@435: duke@435: uint loop_count = 0; duke@435: uint gc_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: duke@435: result = young_gen()->allocate(size, is_tlab); duke@435: duke@435: // (1) If the requested object is too large to easily fit in the duke@435: // young_gen, or duke@435: // (2) If GC is locked out via GCLocker, young gen is full and duke@435: // the need for a GC already signalled to GCLocker (done duke@435: // at a safepoint), duke@435: // ... then, rather than force a safepoint and (a potentially futile) duke@435: // collection (attempt) for each allocation, try allocation directly duke@435: // in old_gen. For case (2) above, we may in the future allow duke@435: // TLAB allocation directly in the old gen. duke@435: if (result != NULL) { duke@435: return result; duke@435: } duke@435: if (!is_tlab && iveresov@808: size >= (young_gen()->eden_space()->capacity_in_words(Thread::current()) / 2)) { duke@435: result = old_gen()->allocate(size, is_tlab); duke@435: if (result != NULL) { duke@435: return result; duke@435: } duke@435: } duke@435: if (GC_locker::is_active_and_needs_gc()) { duke@435: // GC is locked out. If this is a TLAB allocation, duke@435: // return NULL; the requestor will retry allocation duke@435: // of an idividual object at a time. duke@435: if (is_tlab) { duke@435: return NULL; duke@435: } duke@435: 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(); 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: duke@435: // Exit the loop if if the gc time limit has been exceeded. duke@435: // The allocation must have failed above (result must be NULL), duke@435: // and the most recent collection must have exceeded the duke@435: // gc time limit. Exit the loop so that an out-of-memory duke@435: // will be thrown (returning a NULL will do that), but duke@435: // clear gc_time_limit_exceeded so that the next collection duke@435: // will succeeded if the applications decides to handle the duke@435: // out-of-memory and tries to go on. duke@435: *gc_overhead_limit_was_exceeded = size_policy()->gc_time_limit_exceeded(); duke@435: if (size_policy()->gc_time_limit_exceeded()) { duke@435: size_policy()->set_gc_time_limit_exceeded(false); duke@435: if (PrintGCDetails && Verbose) { duke@435: gclog_or_tty->print_cr("ParallelScavengeHeap::mem_allocate: " duke@435: "return NULL because gc_time_limit_exceeded is set"); duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: // Generate a VM operation duke@435: VM_ParallelGCFailedAllocation op(size, is_tlab, 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: } duke@435: // If a NULL result is being returned, an out-of-memory duke@435: // will be thrown now. Clear the gc_time_limit_exceeded duke@435: // flag to avoid the following situation. duke@435: // gc_time_limit_exceeded is set during a collection duke@435: // the collection fails to return enough space and an OOM is thrown duke@435: // the next GC is skipped because the gc_time_limit_exceeded duke@435: // flag is set and another OOM is thrown duke@435: if (op.result() == NULL) { duke@435: size_policy()->set_gc_time_limit_exceeded(false); duke@435: } 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" duke@435: " size=%d %s", loop_count, size, is_tlab ? "(TLAB)" : ""); duke@435: } duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: 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. duke@435: HeapWord* ParallelScavengeHeap::failed_mem_allocate(size_t size, bool is_tlab) { 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: duke@435: size_t mark_sweep_invocation_count = total_invocations(); duke@435: duke@435: // We assume (and assert!) that an allocation at this point will fail duke@435: // 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); duke@435: PSScavenge::invoke(); duke@435: HeapWord* result = young_gen()->allocate(size, is_tlab); duke@435: duke@435: // Second level allocation failure. duke@435: // Mark sweep and allocate in young generation. duke@435: if (result == NULL) { duke@435: // There is some chance the scavenge method decided to invoke mark_sweep. duke@435: // Don't mark sweep twice if so. duke@435: if (mark_sweep_invocation_count == total_invocations()) { duke@435: invoke_full_gc(false); duke@435: result = young_gen()->allocate(size, is_tlab); duke@435: } duke@435: } duke@435: duke@435: // Third level allocation failure. duke@435: // After mark sweep and young generation allocation failure, duke@435: // allocate in old generation. duke@435: if (result == NULL && !is_tlab) { duke@435: result = old_gen()->allocate(size, is_tlab); 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) { duke@435: invoke_full_gc(true); duke@435: result = young_gen()->allocate(size, is_tlab); duke@435: } duke@435: duke@435: // Fifth level allocation failure. duke@435: // After more complete mark sweep, allocate in old generation. duke@435: if (result == NULL && !is_tlab) { duke@435: result = old_gen()->allocate(size, is_tlab); duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: // duke@435: // This is the policy loop for allocating in the permanent generation. duke@435: // If the initial allocation fails, we create a vm operation which will duke@435: // cause a collection. duke@435: HeapWord* ParallelScavengeHeap::permanent_mem_allocate(size_t size) { 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: duke@435: HeapWord* result; duke@435: duke@435: uint loop_count = 0; duke@435: uint gc_count = 0; duke@435: uint full_gc_count = 0; duke@435: duke@435: do { 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: full_gc_count = Universe::heap()->total_full_collections(); duke@435: duke@435: result = perm_gen()->allocate_permanent(size); apetrusenko@574: apetrusenko@574: if (result != NULL) { apetrusenko@574: return result; apetrusenko@574: } apetrusenko@574: apetrusenko@574: if (GC_locker::is_active_and_needs_gc()) { apetrusenko@574: // If this thread is not in a jni critical section, we stall apetrusenko@574: // the requestor until the critical section has cleared and apetrusenko@574: // GC allowed. When the critical section clears, a GC is apetrusenko@574: // initiated by the last thread exiting the critical section; so apetrusenko@574: // we retry the allocation sequence from the beginning of the loop, apetrusenko@574: // rather than causing more, now probably unnecessary, GC attempts. apetrusenko@574: JavaThread* jthr = JavaThread::current(); apetrusenko@574: if (!jthr->in_critical()) { apetrusenko@574: MutexUnlocker mul(Heap_lock); apetrusenko@574: GC_locker::stall_until_clear(); apetrusenko@574: continue; apetrusenko@574: } else { apetrusenko@574: if (CheckJNICalls) { apetrusenko@574: fatal("Possible deadlock due to allocating while" apetrusenko@574: " in jni critical section"); apetrusenko@574: } apetrusenko@574: return NULL; apetrusenko@574: } apetrusenko@574: } duke@435: } duke@435: duke@435: if (result == NULL) { duke@435: duke@435: // Exit the loop if the gc time limit has been exceeded. duke@435: // The allocation must have failed above (result must be NULL), duke@435: // and the most recent collection must have exceeded the duke@435: // gc time limit. Exit the loop so that an out-of-memory duke@435: // will be thrown (returning a NULL will do that), but duke@435: // clear gc_time_limit_exceeded so that the next collection duke@435: // will succeeded if the applications decides to handle the duke@435: // out-of-memory and tries to go on. duke@435: if (size_policy()->gc_time_limit_exceeded()) { duke@435: size_policy()->set_gc_time_limit_exceeded(false); duke@435: if (PrintGCDetails && Verbose) { duke@435: gclog_or_tty->print_cr("ParallelScavengeHeap::permanent_mem_allocate: " duke@435: "return NULL because gc_time_limit_exceeded is set"); duke@435: } duke@435: assert(result == NULL, "Allocation did not fail"); duke@435: return NULL; duke@435: } duke@435: duke@435: // Generate a VM operation duke@435: VM_ParallelGCFailedPermanentAllocation op(size, gc_count, full_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_permanent_or_null(op.result()), duke@435: "result not in heap"); apetrusenko@574: // If GC was locked out during VM operation then retry allocation apetrusenko@574: // and/or stall as necessary. apetrusenko@574: if (op.gc_locked()) { apetrusenko@574: assert(op.result() == NULL, "must be NULL if gc_locked() is true"); apetrusenko@574: continue; // retry and/or stall as necessary apetrusenko@574: } duke@435: // If a NULL results is being returned, an out-of-memory duke@435: // will be thrown now. Clear the gc_time_limit_exceeded duke@435: // flag to avoid the following situation. duke@435: // gc_time_limit_exceeded is set during a collection duke@435: // the collection fails to return enough space and an OOM is thrown duke@435: // the next GC is skipped because the gc_time_limit_exceeded duke@435: // flag is set and another OOM is thrown duke@435: if (op.result() == NULL) { duke@435: size_policy()->set_gc_time_limit_exceeded(false); duke@435: } 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 ((QueuedAllocationWarningCount > 0) && duke@435: (loop_count % QueuedAllocationWarningCount == 0)) { duke@435: warning("ParallelScavengeHeap::permanent_mem_allocate retries %d times \n\t" duke@435: " size=%d", loop_count, size); duke@435: } duke@435: } while (result == NULL); duke@435: duke@435: return result; duke@435: } duke@435: duke@435: // duke@435: // This is the policy code for permanent allocations which have failed duke@435: // and require a collection. Note that just as in failed_mem_allocate, duke@435: // we do not set collection policy, only where & when to allocate and duke@435: // collect. duke@435: HeapWord* ParallelScavengeHeap::failed_permanent_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: assert(size > perm_gen()->free_in_words(), "Allocation should fail"); duke@435: duke@435: // We assume (and assert!) that an allocation at this point will fail duke@435: // unless we collect. duke@435: duke@435: // First level allocation failure. Mark-sweep and allocate in perm gen. duke@435: GCCauseSetter gccs(this, GCCause::_allocation_failure); duke@435: invoke_full_gc(false); duke@435: HeapWord* result = perm_gen()->allocate_permanent(size); duke@435: duke@435: // Second level allocation failure. We're running out of memory. duke@435: if (result == NULL) { duke@435: invoke_full_gc(true); duke@435: result = perm_gen()->allocate_permanent(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) { duke@435: return young_gen()->allocate(size, true); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::fill_all_tlabs(bool retire) { duke@435: CollectedHeap::fill_all_tlabs(retire); 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: 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: duke@435: // This interface assumes that it's being called by the duke@435: // vm thread. It collects the heap assuming that the duke@435: // heap lock is already held and that we are executing in duke@435: // the context of the vm thread. duke@435: void ParallelScavengeHeap::collect_as_vm_thread(GCCause::Cause cause) { duke@435: assert(Thread::current()->is_VM_thread(), "Precondition#1"); duke@435: assert(Heap_lock->is_locked(), "Precondition#2"); duke@435: GCCauseSetter gcs(this, cause); duke@435: switch (cause) { duke@435: case GCCause::_heap_inspection: duke@435: case GCCause::_heap_dump: { duke@435: HandleMark hm; duke@435: invoke_full_gc(false); duke@435: break; duke@435: } duke@435: default: // XXX FIX ME duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: duke@435: duke@435: void ParallelScavengeHeap::oop_iterate(OopClosure* 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: perm_gen()->object_iterate(cl); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::permanent_oop_iterate(OopClosure* cl) { duke@435: Unimplemented(); duke@435: } duke@435: duke@435: void ParallelScavengeHeap::permanent_object_iterate(ObjectClosure* cl) { duke@435: perm_gen()->object_iterate(cl); 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"); 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: } else if (perm_gen()->is_in_reserved(addr)) { duke@435: assert(perm_gen()->is_in(addr), duke@435: "addr should be in allocated part of perm gen"); duke@435: return perm_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() const { print_on(tty); } 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); duke@435: perm_gen()->print_on(st); duke@435: } duke@435: 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) { duke@435: double time = 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: duke@435: void ParallelScavengeHeap::verify(bool allow_dirty, bool silent) { 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("permanent "); duke@435: } duke@435: perm_gen()->verify(allow_dirty); duke@435: duke@435: if (!silent) { duke@435: gclog_or_tty->print("tenured "); duke@435: } duke@435: old_gen()->verify(allow_dirty); duke@435: duke@435: if (!silent) { duke@435: gclog_or_tty->print("eden "); duke@435: } duke@435: young_gen()->verify(allow_dirty); duke@435: } duke@435: if (!silent) { duke@435: gclog_or_tty->print("ref_proc "); duke@435: } duke@435: ReferenceProcessor::verify(); 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: 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: perm_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: perm_gen()->object_space()->mangle_unused_area(); jmasa@698: } jmasa@698: } jmasa@698: #endif