duke@435: /* duke@435: * Copyright 2001-2007 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/_defNewGeneration.cpp.incl" duke@435: duke@435: // duke@435: // DefNewGeneration functions. duke@435: duke@435: // Methods of protected closure types. duke@435: duke@435: DefNewGeneration::IsAliveClosure::IsAliveClosure(Generation* g) : _g(g) { duke@435: assert(g->level() == 0, "Optimized for youngest gen."); duke@435: } duke@435: void DefNewGeneration::IsAliveClosure::do_object(oop p) { duke@435: assert(false, "Do not call."); duke@435: } duke@435: bool DefNewGeneration::IsAliveClosure::do_object_b(oop p) { duke@435: return (HeapWord*)p >= _g->reserved().end() || p->is_forwarded(); duke@435: } duke@435: duke@435: DefNewGeneration::KeepAliveClosure:: duke@435: KeepAliveClosure(ScanWeakRefClosure* cl) : _cl(cl) { duke@435: GenRemSet* rs = GenCollectedHeap::heap()->rem_set(); duke@435: assert(rs->rs_kind() == GenRemSet::CardTable, "Wrong rem set kind."); duke@435: _rs = (CardTableRS*)rs; duke@435: } duke@435: coleenp@548: void DefNewGeneration::KeepAliveClosure::do_oop(oop* p) { DefNewGeneration::KeepAliveClosure::do_oop_work(p); } coleenp@548: void DefNewGeneration::KeepAliveClosure::do_oop(narrowOop* p) { DefNewGeneration::KeepAliveClosure::do_oop_work(p); } duke@435: duke@435: duke@435: DefNewGeneration::FastKeepAliveClosure:: duke@435: FastKeepAliveClosure(DefNewGeneration* g, ScanWeakRefClosure* cl) : duke@435: DefNewGeneration::KeepAliveClosure(cl) { duke@435: _boundary = g->reserved().end(); duke@435: } duke@435: coleenp@548: void DefNewGeneration::FastKeepAliveClosure::do_oop(oop* p) { DefNewGeneration::FastKeepAliveClosure::do_oop_work(p); } coleenp@548: void DefNewGeneration::FastKeepAliveClosure::do_oop(narrowOop* p) { DefNewGeneration::FastKeepAliveClosure::do_oop_work(p); } duke@435: duke@435: DefNewGeneration::EvacuateFollowersClosure:: duke@435: EvacuateFollowersClosure(GenCollectedHeap* gch, int level, duke@435: ScanClosure* cur, ScanClosure* older) : duke@435: _gch(gch), _level(level), duke@435: _scan_cur_or_nonheap(cur), _scan_older(older) duke@435: {} duke@435: duke@435: void DefNewGeneration::EvacuateFollowersClosure::do_void() { duke@435: do { duke@435: _gch->oop_since_save_marks_iterate(_level, _scan_cur_or_nonheap, duke@435: _scan_older); duke@435: } while (!_gch->no_allocs_since_save_marks(_level)); duke@435: } duke@435: duke@435: DefNewGeneration::FastEvacuateFollowersClosure:: duke@435: FastEvacuateFollowersClosure(GenCollectedHeap* gch, int level, duke@435: DefNewGeneration* gen, duke@435: FastScanClosure* cur, FastScanClosure* older) : duke@435: _gch(gch), _level(level), _gen(gen), duke@435: _scan_cur_or_nonheap(cur), _scan_older(older) duke@435: {} duke@435: duke@435: void DefNewGeneration::FastEvacuateFollowersClosure::do_void() { duke@435: do { duke@435: _gch->oop_since_save_marks_iterate(_level, _scan_cur_or_nonheap, duke@435: _scan_older); duke@435: } while (!_gch->no_allocs_since_save_marks(_level)); duke@435: guarantee(_gen->promo_failure_scan_stack() == NULL duke@435: || _gen->promo_failure_scan_stack()->length() == 0, duke@435: "Failed to finish scan"); duke@435: } duke@435: duke@435: ScanClosure::ScanClosure(DefNewGeneration* g, bool gc_barrier) : duke@435: OopsInGenClosure(g), _g(g), _gc_barrier(gc_barrier) duke@435: { duke@435: assert(_g->level() == 0, "Optimized for youngest generation"); duke@435: _boundary = _g->reserved().end(); duke@435: } duke@435: coleenp@548: void ScanClosure::do_oop(oop* p) { ScanClosure::do_oop_work(p); } coleenp@548: void ScanClosure::do_oop(narrowOop* p) { ScanClosure::do_oop_work(p); } coleenp@548: duke@435: FastScanClosure::FastScanClosure(DefNewGeneration* g, bool gc_barrier) : duke@435: OopsInGenClosure(g), _g(g), _gc_barrier(gc_barrier) duke@435: { duke@435: assert(_g->level() == 0, "Optimized for youngest generation"); duke@435: _boundary = _g->reserved().end(); duke@435: } duke@435: coleenp@548: void FastScanClosure::do_oop(oop* p) { FastScanClosure::do_oop_work(p); } coleenp@548: void FastScanClosure::do_oop(narrowOop* p) { FastScanClosure::do_oop_work(p); } coleenp@548: duke@435: ScanWeakRefClosure::ScanWeakRefClosure(DefNewGeneration* g) : duke@435: OopClosure(g->ref_processor()), _g(g) duke@435: { duke@435: assert(_g->level() == 0, "Optimized for youngest generation"); duke@435: _boundary = _g->reserved().end(); duke@435: } duke@435: coleenp@548: void ScanWeakRefClosure::do_oop(oop* p) { ScanWeakRefClosure::do_oop_work(p); } coleenp@548: void ScanWeakRefClosure::do_oop(narrowOop* p) { ScanWeakRefClosure::do_oop_work(p); } coleenp@548: coleenp@548: void FilteringClosure::do_oop(oop* p) { FilteringClosure::do_oop_work(p); } coleenp@548: void FilteringClosure::do_oop(narrowOop* p) { FilteringClosure::do_oop_work(p); } duke@435: duke@435: DefNewGeneration::DefNewGeneration(ReservedSpace rs, duke@435: size_t initial_size, duke@435: int level, duke@435: const char* policy) duke@435: : Generation(rs, initial_size, level), duke@435: _objs_with_preserved_marks(NULL), duke@435: _preserved_marks_of_objs(NULL), duke@435: _promo_failure_scan_stack(NULL), duke@435: _promo_failure_drain_in_progress(false), duke@435: _should_allocate_from_space(false) duke@435: { duke@435: MemRegion cmr((HeapWord*)_virtual_space.low(), duke@435: (HeapWord*)_virtual_space.high()); duke@435: Universe::heap()->barrier_set()->resize_covered_region(cmr); duke@435: duke@435: if (GenCollectedHeap::heap()->collector_policy()->has_soft_ended_eden()) { duke@435: _eden_space = new ConcEdenSpace(this); duke@435: } else { duke@435: _eden_space = new EdenSpace(this); duke@435: } duke@435: _from_space = new ContiguousSpace(); duke@435: _to_space = new ContiguousSpace(); duke@435: duke@435: if (_eden_space == NULL || _from_space == NULL || _to_space == NULL) duke@435: vm_exit_during_initialization("Could not allocate a new gen space"); duke@435: duke@435: // Compute the maximum eden and survivor space sizes. These sizes duke@435: // are computed assuming the entire reserved space is committed. duke@435: // These values are exported as performance counters. duke@435: uintx alignment = GenCollectedHeap::heap()->collector_policy()->min_alignment(); duke@435: uintx size = _virtual_space.reserved_size(); duke@435: _max_survivor_size = compute_survivor_size(size, alignment); duke@435: _max_eden_size = size - (2*_max_survivor_size); duke@435: duke@435: // allocate the performance counters duke@435: duke@435: // Generation counters -- generation 0, 3 subspaces duke@435: _gen_counters = new GenerationCounters("new", 0, 3, &_virtual_space); duke@435: _gc_counters = new CollectorCounters(policy, 0); duke@435: duke@435: _eden_counters = new CSpaceCounters("eden", 0, _max_eden_size, _eden_space, duke@435: _gen_counters); duke@435: _from_counters = new CSpaceCounters("s0", 1, _max_survivor_size, _from_space, duke@435: _gen_counters); duke@435: _to_counters = new CSpaceCounters("s1", 2, _max_survivor_size, _to_space, duke@435: _gen_counters); duke@435: duke@435: compute_space_boundaries(0); duke@435: update_counters(); duke@435: _next_gen = NULL; duke@435: _tenuring_threshold = MaxTenuringThreshold; duke@435: _pretenure_size_threshold_words = PretenureSizeThreshold >> LogHeapWordSize; duke@435: } duke@435: duke@435: void DefNewGeneration::compute_space_boundaries(uintx minimum_eden_size) { duke@435: uintx alignment = GenCollectedHeap::heap()->collector_policy()->min_alignment(); duke@435: duke@435: // Compute sizes duke@435: uintx size = _virtual_space.committed_size(); duke@435: uintx survivor_size = compute_survivor_size(size, alignment); duke@435: uintx eden_size = size - (2*survivor_size); duke@435: assert(eden_size > 0 && survivor_size <= eden_size, "just checking"); duke@435: duke@435: if (eden_size < minimum_eden_size) { duke@435: // May happen due to 64Kb rounding, if so adjust eden size back up duke@435: minimum_eden_size = align_size_up(minimum_eden_size, alignment); duke@435: uintx maximum_survivor_size = (size - minimum_eden_size) / 2; duke@435: uintx unaligned_survivor_size = duke@435: align_size_down(maximum_survivor_size, alignment); duke@435: survivor_size = MAX2(unaligned_survivor_size, alignment); duke@435: eden_size = size - (2*survivor_size); duke@435: assert(eden_size > 0 && survivor_size <= eden_size, "just checking"); duke@435: assert(eden_size >= minimum_eden_size, "just checking"); duke@435: } duke@435: duke@435: char *eden_start = _virtual_space.low(); duke@435: char *from_start = eden_start + eden_size; duke@435: char *to_start = from_start + survivor_size; duke@435: char *to_end = to_start + survivor_size; duke@435: duke@435: assert(to_end == _virtual_space.high(), "just checking"); duke@435: assert(Space::is_aligned((HeapWord*)eden_start), "checking alignment"); duke@435: assert(Space::is_aligned((HeapWord*)from_start), "checking alignment"); duke@435: assert(Space::is_aligned((HeapWord*)to_start), "checking alignment"); duke@435: duke@435: MemRegion edenMR((HeapWord*)eden_start, (HeapWord*)from_start); duke@435: MemRegion fromMR((HeapWord*)from_start, (HeapWord*)to_start); duke@435: MemRegion toMR ((HeapWord*)to_start, (HeapWord*)to_end); duke@435: duke@435: eden()->initialize(edenMR, (minimum_eden_size == 0)); duke@435: // If minumum_eden_size != 0, we will not have cleared any duke@435: // portion of eden above its top. This can cause newly duke@435: // expanded space not to be mangled if using ZapUnusedHeapArea. duke@435: // We explicitly do such mangling here. duke@435: if (ZapUnusedHeapArea && (minimum_eden_size != 0)) { duke@435: eden()->mangle_unused_area(); duke@435: } duke@435: from()->initialize(fromMR, true); duke@435: to()->initialize(toMR , true); duke@435: eden()->set_next_compaction_space(from()); duke@435: // The to-space is normally empty before a compaction so need duke@435: // not be considered. The exception is during promotion duke@435: // failure handling when to-space can contain live objects. duke@435: from()->set_next_compaction_space(NULL); duke@435: } duke@435: duke@435: void DefNewGeneration::swap_spaces() { duke@435: ContiguousSpace* s = from(); duke@435: _from_space = to(); duke@435: _to_space = s; duke@435: eden()->set_next_compaction_space(from()); duke@435: // The to-space is normally empty before a compaction so need duke@435: // not be considered. The exception is during promotion duke@435: // failure handling when to-space can contain live objects. duke@435: from()->set_next_compaction_space(NULL); duke@435: duke@435: if (UsePerfData) { duke@435: CSpaceCounters* c = _from_counters; duke@435: _from_counters = _to_counters; duke@435: _to_counters = c; duke@435: } duke@435: } duke@435: duke@435: bool DefNewGeneration::expand(size_t bytes) { duke@435: MutexLocker x(ExpandHeap_lock); duke@435: bool success = _virtual_space.expand_by(bytes); duke@435: duke@435: // Do not attempt an expand-to-the reserve size. The duke@435: // request should properly observe the maximum size of duke@435: // the generation so an expand-to-reserve should be duke@435: // unnecessary. Also a second call to expand-to-reserve duke@435: // value potentially can cause an undue expansion. duke@435: // For example if the first expand fail for unknown reasons, duke@435: // but the second succeeds and expands the heap to its maximum duke@435: // value. duke@435: if (GC_locker::is_active()) { duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); duke@435: } duke@435: } duke@435: duke@435: return success; duke@435: } duke@435: duke@435: duke@435: void DefNewGeneration::compute_new_size() { duke@435: // This is called after a gc that includes the following generation duke@435: // (which is required to exist.) So from-space will normally be empty. duke@435: // Note that we check both spaces, since if scavenge failed they revert roles. duke@435: // If not we bail out (otherwise we would have to relocate the objects) duke@435: if (!from()->is_empty() || !to()->is_empty()) { duke@435: return; duke@435: } duke@435: duke@435: int next_level = level() + 1; duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: assert(next_level < gch->_n_gens, duke@435: "DefNewGeneration cannot be an oldest gen"); duke@435: duke@435: Generation* next_gen = gch->_gens[next_level]; duke@435: size_t old_size = next_gen->capacity(); duke@435: size_t new_size_before = _virtual_space.committed_size(); duke@435: size_t min_new_size = spec()->init_size(); duke@435: size_t max_new_size = reserved().byte_size(); duke@435: assert(min_new_size <= new_size_before && duke@435: new_size_before <= max_new_size, duke@435: "just checking"); duke@435: // All space sizes must be multiples of Generation::GenGrain. duke@435: size_t alignment = Generation::GenGrain; duke@435: duke@435: // Compute desired new generation size based on NewRatio and duke@435: // NewSizeThreadIncrease duke@435: size_t desired_new_size = old_size/NewRatio; duke@435: int threads_count = Threads::number_of_non_daemon_threads(); duke@435: size_t thread_increase_size = threads_count * NewSizeThreadIncrease; duke@435: desired_new_size = align_size_up(desired_new_size + thread_increase_size, alignment); duke@435: duke@435: // Adjust new generation size duke@435: desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size); duke@435: assert(desired_new_size <= max_new_size, "just checking"); duke@435: duke@435: bool changed = false; duke@435: if (desired_new_size > new_size_before) { duke@435: size_t change = desired_new_size - new_size_before; duke@435: assert(change % alignment == 0, "just checking"); duke@435: if (expand(change)) { duke@435: changed = true; duke@435: } duke@435: // If the heap failed to expand to the desired size, duke@435: // "changed" will be false. If the expansion failed duke@435: // (and at this point it was expected to succeed), duke@435: // ignore the failure (leaving "changed" as false). duke@435: } duke@435: if (desired_new_size < new_size_before && eden()->is_empty()) { duke@435: // bail out of shrinking if objects in eden duke@435: size_t change = new_size_before - desired_new_size; duke@435: assert(change % alignment == 0, "just checking"); duke@435: _virtual_space.shrink_by(change); duke@435: changed = true; duke@435: } duke@435: if (changed) { duke@435: compute_space_boundaries(eden()->used()); duke@435: MemRegion cmr((HeapWord*)_virtual_space.low(), (HeapWord*)_virtual_space.high()); duke@435: Universe::heap()->barrier_set()->resize_covered_region(cmr); duke@435: if (Verbose && PrintGC) { duke@435: size_t new_size_after = _virtual_space.committed_size(); duke@435: size_t eden_size_after = eden()->capacity(); duke@435: size_t survivor_size_after = from()->capacity(); duke@435: gclog_or_tty->print("New generation size " SIZE_FORMAT "K->" SIZE_FORMAT "K [eden=" duke@435: SIZE_FORMAT "K,survivor=" SIZE_FORMAT "K]", duke@435: new_size_before/K, new_size_after/K, eden_size_after/K, survivor_size_after/K); duke@435: if (WizardMode) { duke@435: gclog_or_tty->print("[allowed " SIZE_FORMAT "K extra for %d threads]", duke@435: thread_increase_size/K, threads_count); duke@435: } duke@435: gclog_or_tty->cr(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void DefNewGeneration::object_iterate_since_last_GC(ObjectClosure* cl) { duke@435: // $$$ This may be wrong in case of "scavenge failure"? duke@435: eden()->object_iterate(cl); duke@435: } duke@435: duke@435: void DefNewGeneration::younger_refs_iterate(OopsInGenClosure* cl) { duke@435: assert(false, "NYI -- are you sure you want to call this?"); duke@435: } duke@435: duke@435: duke@435: size_t DefNewGeneration::capacity() const { duke@435: return eden()->capacity() duke@435: + from()->capacity(); // to() is only used during scavenge duke@435: } duke@435: duke@435: duke@435: size_t DefNewGeneration::used() const { duke@435: return eden()->used() duke@435: + from()->used(); // to() is only used during scavenge duke@435: } duke@435: duke@435: duke@435: size_t DefNewGeneration::free() const { duke@435: return eden()->free() duke@435: + from()->free(); // to() is only used during scavenge duke@435: } duke@435: duke@435: size_t DefNewGeneration::max_capacity() const { duke@435: const size_t alignment = GenCollectedHeap::heap()->collector_policy()->min_alignment(); duke@435: const size_t reserved_bytes = reserved().byte_size(); duke@435: return reserved_bytes - compute_survivor_size(reserved_bytes, alignment); duke@435: } duke@435: duke@435: size_t DefNewGeneration::unsafe_max_alloc_nogc() const { duke@435: return eden()->free(); duke@435: } duke@435: duke@435: size_t DefNewGeneration::capacity_before_gc() const { duke@435: return eden()->capacity(); duke@435: } duke@435: duke@435: size_t DefNewGeneration::contiguous_available() const { duke@435: return eden()->free(); duke@435: } duke@435: duke@435: duke@435: HeapWord** DefNewGeneration::top_addr() const { return eden()->top_addr(); } duke@435: HeapWord** DefNewGeneration::end_addr() const { return eden()->end_addr(); } duke@435: duke@435: void DefNewGeneration::object_iterate(ObjectClosure* blk) { duke@435: eden()->object_iterate(blk); duke@435: from()->object_iterate(blk); duke@435: } duke@435: duke@435: duke@435: void DefNewGeneration::space_iterate(SpaceClosure* blk, duke@435: bool usedOnly) { duke@435: blk->do_space(eden()); duke@435: blk->do_space(from()); duke@435: blk->do_space(to()); duke@435: } duke@435: duke@435: // The last collection bailed out, we are running out of heap space, duke@435: // so we try to allocate the from-space, too. duke@435: HeapWord* DefNewGeneration::allocate_from_space(size_t size) { duke@435: HeapWord* result = NULL; duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print("DefNewGeneration::allocate_from_space(%u):" duke@435: " will_fail: %s" duke@435: " heap_lock: %s" duke@435: " free: " SIZE_FORMAT, duke@435: size, duke@435: GenCollectedHeap::heap()->incremental_collection_will_fail() ? "true" : "false", duke@435: Heap_lock->is_locked() ? "locked" : "unlocked", duke@435: from()->free()); duke@435: } duke@435: if (should_allocate_from_space() || GC_locker::is_active_and_needs_gc()) { duke@435: if (Heap_lock->owned_by_self() || duke@435: (SafepointSynchronize::is_at_safepoint() && duke@435: Thread::current()->is_VM_thread())) { duke@435: // If the Heap_lock is not locked by this thread, this will be called duke@435: // again later with the Heap_lock held. duke@435: result = from()->allocate(size); duke@435: } else if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" Heap_lock is not owned by self"); duke@435: } duke@435: } else if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" should_allocate_from_space: NOT"); duke@435: } duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" returns %s", result == NULL ? "NULL" : "object"); duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: HeapWord* DefNewGeneration::expand_and_allocate(size_t size, duke@435: bool is_tlab, duke@435: bool parallel) { duke@435: // We don't attempt to expand the young generation (but perhaps we should.) duke@435: return allocate(size, is_tlab); duke@435: } duke@435: duke@435: duke@435: void DefNewGeneration::collect(bool full, duke@435: bool clear_all_soft_refs, duke@435: size_t size, duke@435: bool is_tlab) { duke@435: assert(full || size > 0, "otherwise we don't want to collect"); duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: _next_gen = gch->next_gen(this); duke@435: assert(_next_gen != NULL, duke@435: "This must be the youngest gen, and not the only gen"); duke@435: duke@435: // If the next generation is too full to accomodate promotion duke@435: // from this generation, pass on collection; let the next generation duke@435: // do it. duke@435: if (!collection_attempt_is_safe()) { duke@435: gch->set_incremental_collection_will_fail(); duke@435: return; duke@435: } duke@435: assert(to()->is_empty(), "Else not collection_attempt_is_safe"); duke@435: duke@435: init_assuming_no_promotion_failure(); duke@435: duke@435: TraceTime t1("GC", PrintGC && !PrintGCDetails, true, gclog_or_tty); duke@435: // Capture heap used before collection (for printing). duke@435: size_t gch_prev_used = gch->used(); duke@435: duke@435: SpecializationStats::clear(); duke@435: duke@435: // These can be shared for all code paths duke@435: IsAliveClosure is_alive(this); duke@435: ScanWeakRefClosure scan_weak_ref(this); duke@435: duke@435: age_table()->clear(); duke@435: to()->clear(); duke@435: duke@435: gch->rem_set()->prepare_for_younger_refs_iterate(false); duke@435: duke@435: assert(gch->no_allocs_since_save_marks(0), duke@435: "save marks have not been newly set."); duke@435: duke@435: // Weak refs. duke@435: // FIXME: Are these storage leaks, or are they resource objects? duke@435: #ifdef COMPILER2 duke@435: ReferencePolicy *soft_ref_policy = new LRUMaxHeapPolicy(); duke@435: #else duke@435: ReferencePolicy *soft_ref_policy = new LRUCurrentHeapPolicy(); duke@435: #endif // COMPILER2 duke@435: duke@435: // Not very pretty. duke@435: CollectorPolicy* cp = gch->collector_policy(); duke@435: duke@435: FastScanClosure fsc_with_no_gc_barrier(this, false); duke@435: FastScanClosure fsc_with_gc_barrier(this, true); duke@435: duke@435: set_promo_failure_scan_stack_closure(&fsc_with_no_gc_barrier); duke@435: FastEvacuateFollowersClosure evacuate_followers(gch, _level, this, duke@435: &fsc_with_no_gc_barrier, duke@435: &fsc_with_gc_barrier); duke@435: duke@435: assert(gch->no_allocs_since_save_marks(0), duke@435: "save marks have not been newly set."); duke@435: duke@435: gch->gen_process_strong_roots(_level, duke@435: true, // Process younger gens, if any, as duke@435: // strong roots. duke@435: false,// not collecting permanent generation. duke@435: SharedHeap::SO_AllClasses, duke@435: &fsc_with_gc_barrier, duke@435: &fsc_with_no_gc_barrier); duke@435: duke@435: // "evacuate followers". duke@435: evacuate_followers.do_void(); duke@435: duke@435: FastKeepAliveClosure keep_alive(this, &scan_weak_ref); duke@435: ref_processor()->process_discovered_references( duke@435: soft_ref_policy, &is_alive, &keep_alive, &evacuate_followers, NULL); duke@435: if (!promotion_failed()) { duke@435: // Swap the survivor spaces. duke@435: eden()->clear(); duke@435: from()->clear(); duke@435: swap_spaces(); duke@435: duke@435: assert(to()->is_empty(), "to space should be empty now"); duke@435: duke@435: // Set the desired survivor size to half the real survivor space duke@435: _tenuring_threshold = duke@435: age_table()->compute_tenuring_threshold(to()->capacity()/HeapWordSize); duke@435: duke@435: if (PrintGC && !PrintGCDetails) { duke@435: gch->print_heap_change(gch_prev_used); duke@435: } duke@435: } else { duke@435: assert(HandlePromotionFailure, duke@435: "Should not be here unless promotion failure handling is on"); duke@435: assert(_promo_failure_scan_stack != NULL && duke@435: _promo_failure_scan_stack->length() == 0, "post condition"); duke@435: duke@435: // deallocate stack and it's elements duke@435: delete _promo_failure_scan_stack; duke@435: _promo_failure_scan_stack = NULL; duke@435: duke@435: remove_forwarding_pointers(); duke@435: if (PrintGCDetails) { duke@435: gclog_or_tty->print(" (promotion failed)"); duke@435: } duke@435: // Add to-space to the list of space to compact duke@435: // when a promotion failure has occurred. In that duke@435: // case there can be live objects in to-space duke@435: // as a result of a partial evacuation of eden duke@435: // and from-space. duke@435: swap_spaces(); // For the sake of uniformity wrt ParNewGeneration::collect(). duke@435: from()->set_next_compaction_space(to()); duke@435: gch->set_incremental_collection_will_fail(); duke@435: duke@435: // Reset the PromotionFailureALot counters. duke@435: NOT_PRODUCT(Universe::heap()->reset_promotion_should_fail();) duke@435: } duke@435: // set new iteration safe limit for the survivor spaces duke@435: from()->set_concurrent_iteration_safe_limit(from()->top()); duke@435: to()->set_concurrent_iteration_safe_limit(to()->top()); duke@435: SpecializationStats::print(); duke@435: update_time_of_last_gc(os::javaTimeMillis()); duke@435: } duke@435: duke@435: class RemoveForwardPointerClosure: public ObjectClosure { duke@435: public: duke@435: void do_object(oop obj) { duke@435: obj->init_mark(); duke@435: } duke@435: }; duke@435: duke@435: void DefNewGeneration::init_assuming_no_promotion_failure() { duke@435: _promotion_failed = false; duke@435: from()->set_next_compaction_space(NULL); duke@435: } duke@435: duke@435: void DefNewGeneration::remove_forwarding_pointers() { duke@435: RemoveForwardPointerClosure rspc; duke@435: eden()->object_iterate(&rspc); duke@435: from()->object_iterate(&rspc); duke@435: // Now restore saved marks, if any. duke@435: if (_objs_with_preserved_marks != NULL) { duke@435: assert(_preserved_marks_of_objs != NULL, "Both or none."); duke@435: assert(_objs_with_preserved_marks->length() == duke@435: _preserved_marks_of_objs->length(), "Both or none."); duke@435: for (int i = 0; i < _objs_with_preserved_marks->length(); i++) { duke@435: oop obj = _objs_with_preserved_marks->at(i); duke@435: markOop m = _preserved_marks_of_objs->at(i); duke@435: obj->set_mark(m); duke@435: } duke@435: delete _objs_with_preserved_marks; duke@435: delete _preserved_marks_of_objs; duke@435: _objs_with_preserved_marks = NULL; duke@435: _preserved_marks_of_objs = NULL; duke@435: } duke@435: } duke@435: duke@435: void DefNewGeneration::preserve_mark_if_necessary(oop obj, markOop m) { duke@435: if (m->must_be_preserved_for_promotion_failure(obj)) { duke@435: if (_objs_with_preserved_marks == NULL) { duke@435: assert(_preserved_marks_of_objs == NULL, "Both or none."); duke@435: _objs_with_preserved_marks = new (ResourceObj::C_HEAP) duke@435: GrowableArray(PreserveMarkStackSize, true); duke@435: _preserved_marks_of_objs = new (ResourceObj::C_HEAP) duke@435: GrowableArray(PreserveMarkStackSize, true); duke@435: } duke@435: _objs_with_preserved_marks->push(obj); duke@435: _preserved_marks_of_objs->push(m); duke@435: } duke@435: } duke@435: duke@435: void DefNewGeneration::handle_promotion_failure(oop old) { duke@435: preserve_mark_if_necessary(old, old->mark()); duke@435: // forward to self duke@435: old->forward_to(old); duke@435: _promotion_failed = true; duke@435: duke@435: push_on_promo_failure_scan_stack(old); duke@435: duke@435: if (!_promo_failure_drain_in_progress) { duke@435: // prevent recursion in copy_to_survivor_space() duke@435: _promo_failure_drain_in_progress = true; duke@435: drain_promo_failure_scan_stack(); duke@435: _promo_failure_drain_in_progress = false; duke@435: } duke@435: } duke@435: coleenp@548: oop DefNewGeneration::copy_to_survivor_space(oop old) { duke@435: assert(is_in_reserved(old) && !old->is_forwarded(), duke@435: "shouldn't be scavenging this oop"); duke@435: size_t s = old->size(); duke@435: oop obj = NULL; duke@435: duke@435: // Try allocating obj in to-space (unless too old) duke@435: if (old->age() < tenuring_threshold()) { duke@435: obj = (oop) to()->allocate(s); duke@435: } duke@435: duke@435: // Otherwise try allocating obj tenured duke@435: if (obj == NULL) { coleenp@548: obj = _next_gen->promote(old, s); duke@435: if (obj == NULL) { duke@435: if (!HandlePromotionFailure) { duke@435: // A failed promotion likely means the MaxLiveObjectEvacuationRatio flag duke@435: // is incorrectly set. In any case, its seriously wrong to be here! duke@435: vm_exit_out_of_memory(s*wordSize, "promotion"); duke@435: } duke@435: duke@435: handle_promotion_failure(old); duke@435: return old; duke@435: } duke@435: } else { duke@435: // Prefetch beyond obj duke@435: const intx interval = PrefetchCopyIntervalInBytes; duke@435: Prefetch::write(obj, interval); duke@435: duke@435: // Copy obj duke@435: Copy::aligned_disjoint_words((HeapWord*)old, (HeapWord*)obj, s); duke@435: duke@435: // Increment age if obj still in new generation duke@435: obj->incr_age(); duke@435: age_table()->add(obj, s); duke@435: } duke@435: duke@435: // Done, insert forward pointer to obj in this header duke@435: old->forward_to(obj); duke@435: duke@435: return obj; duke@435: } duke@435: duke@435: void DefNewGeneration::push_on_promo_failure_scan_stack(oop obj) { duke@435: if (_promo_failure_scan_stack == NULL) { duke@435: _promo_failure_scan_stack = new (ResourceObj::C_HEAP) duke@435: GrowableArray(40, true); duke@435: } duke@435: duke@435: _promo_failure_scan_stack->push(obj); duke@435: } duke@435: duke@435: void DefNewGeneration::drain_promo_failure_scan_stack() { duke@435: assert(_promo_failure_scan_stack != NULL, "precondition"); duke@435: duke@435: while (_promo_failure_scan_stack->length() > 0) { duke@435: oop obj = _promo_failure_scan_stack->pop(); duke@435: obj->oop_iterate(_promo_failure_scan_stack_closure); duke@435: } duke@435: } duke@435: duke@435: void DefNewGeneration::save_marks() { duke@435: eden()->set_saved_mark(); duke@435: to()->set_saved_mark(); duke@435: from()->set_saved_mark(); duke@435: } duke@435: duke@435: duke@435: void DefNewGeneration::reset_saved_marks() { duke@435: eden()->reset_saved_mark(); duke@435: to()->reset_saved_mark(); duke@435: from()->reset_saved_mark(); duke@435: } duke@435: duke@435: duke@435: bool DefNewGeneration::no_allocs_since_save_marks() { duke@435: assert(eden()->saved_mark_at_top(), "Violated spec - alloc in eden"); duke@435: assert(from()->saved_mark_at_top(), "Violated spec - alloc in from"); duke@435: return to()->saved_mark_at_top(); duke@435: } duke@435: duke@435: #define DefNew_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \ duke@435: \ duke@435: void DefNewGeneration:: \ duke@435: oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \ duke@435: cl->set_generation(this); \ duke@435: eden()->oop_since_save_marks_iterate##nv_suffix(cl); \ duke@435: to()->oop_since_save_marks_iterate##nv_suffix(cl); \ duke@435: from()->oop_since_save_marks_iterate##nv_suffix(cl); \ duke@435: cl->reset_generation(); \ duke@435: save_marks(); \ duke@435: } duke@435: duke@435: ALL_SINCE_SAVE_MARKS_CLOSURES(DefNew_SINCE_SAVE_MARKS_DEFN) duke@435: duke@435: #undef DefNew_SINCE_SAVE_MARKS_DEFN duke@435: duke@435: void DefNewGeneration::contribute_scratch(ScratchBlock*& list, Generation* requestor, duke@435: size_t max_alloc_words) { duke@435: if (requestor == this || _promotion_failed) return; duke@435: assert(requestor->level() > level(), "DefNewGeneration must be youngest"); duke@435: duke@435: /* $$$ Assert this? "trace" is a "MarkSweep" function so that's not appropriate. duke@435: if (to_space->top() > to_space->bottom()) { duke@435: trace("to_space not empty when contribute_scratch called"); duke@435: } duke@435: */ duke@435: duke@435: ContiguousSpace* to_space = to(); duke@435: assert(to_space->end() >= to_space->top(), "pointers out of order"); duke@435: size_t free_words = pointer_delta(to_space->end(), to_space->top()); duke@435: if (free_words >= MinFreeScratchWords) { duke@435: ScratchBlock* sb = (ScratchBlock*)to_space->top(); duke@435: sb->num_words = free_words; duke@435: sb->next = list; duke@435: list = sb; duke@435: } duke@435: } duke@435: duke@435: bool DefNewGeneration::collection_attempt_is_safe() { duke@435: if (!to()->is_empty()) { duke@435: return false; duke@435: } duke@435: if (_next_gen == NULL) { duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: _next_gen = gch->next_gen(this); duke@435: assert(_next_gen != NULL, duke@435: "This must be the youngest gen, and not the only gen"); duke@435: } duke@435: duke@435: // Decide if there's enough room for a full promotion duke@435: // When using extremely large edens, we effectively lose a duke@435: // large amount of old space. Use the "MaxLiveObjectEvacuationRatio" duke@435: // flag to reduce the minimum evacuation space requirements. If duke@435: // there is not enough space to evacuate eden during a scavenge, duke@435: // the VM will immediately exit with an out of memory error. duke@435: // This flag has not been tested duke@435: // with collectors other than simple mark & sweep. duke@435: // duke@435: // Note that with the addition of promotion failure handling, the duke@435: // VM will not immediately exit but will undo the young generation duke@435: // collection. The parameter is left here for compatibility. duke@435: const double evacuation_ratio = MaxLiveObjectEvacuationRatio / 100.0; duke@435: duke@435: // worst_case_evacuation is based on "used()". For the case where this duke@435: // method is called after a collection, this is still appropriate because duke@435: // the case that needs to be detected is one in which a full collection duke@435: // has been done and has overflowed into the young generation. In that duke@435: // case a minor collection will fail (the overflow of the full collection duke@435: // means there is no space in the old generation for any promotion). duke@435: size_t worst_case_evacuation = (size_t)(used() * evacuation_ratio); duke@435: duke@435: return _next_gen->promotion_attempt_is_safe(worst_case_evacuation, duke@435: HandlePromotionFailure); duke@435: } duke@435: duke@435: void DefNewGeneration::gc_epilogue(bool full) { duke@435: // Check if the heap is approaching full after a collection has duke@435: // been done. Generally the young generation is empty at duke@435: // a minimum at the end of a collection. If it is not, then duke@435: // the heap is approaching full. duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: clear_should_allocate_from_space(); duke@435: if (collection_attempt_is_safe()) { duke@435: gch->clear_incremental_collection_will_fail(); duke@435: } else { duke@435: gch->set_incremental_collection_will_fail(); duke@435: if (full) { // we seem to be running out of space duke@435: set_should_allocate_from_space(); duke@435: } duke@435: } duke@435: duke@435: // update the generation and space performance counters duke@435: update_counters(); duke@435: gch->collector_policy()->counters()->update_counters(); duke@435: } duke@435: duke@435: void DefNewGeneration::update_counters() { duke@435: if (UsePerfData) { duke@435: _eden_counters->update_all(); duke@435: _from_counters->update_all(); duke@435: _to_counters->update_all(); duke@435: _gen_counters->update_all(); duke@435: } duke@435: } duke@435: duke@435: void DefNewGeneration::verify(bool allow_dirty) { duke@435: eden()->verify(allow_dirty); duke@435: from()->verify(allow_dirty); duke@435: to()->verify(allow_dirty); duke@435: } duke@435: duke@435: void DefNewGeneration::print_on(outputStream* st) const { duke@435: Generation::print_on(st); duke@435: st->print(" eden"); duke@435: eden()->print_on(st); duke@435: st->print(" from"); duke@435: from()->print_on(st); duke@435: st->print(" to "); duke@435: to()->print_on(st); duke@435: } duke@435: duke@435: duke@435: const char* DefNewGeneration::name() const { duke@435: return "def new generation"; duke@435: } coleenp@548: coleenp@548: // Moved from inline file as they are not called inline coleenp@548: CompactibleSpace* DefNewGeneration::first_compaction_space() const { coleenp@548: return eden(); coleenp@548: } coleenp@548: coleenp@548: HeapWord* DefNewGeneration::allocate(size_t word_size, coleenp@548: bool is_tlab) { coleenp@548: // This is the slow-path allocation for the DefNewGeneration. coleenp@548: // Most allocations are fast-path in compiled code. coleenp@548: // We try to allocate from the eden. If that works, we are happy. coleenp@548: // Note that since DefNewGeneration supports lock-free allocation, we coleenp@548: // have to use it here, as well. coleenp@548: HeapWord* result = eden()->par_allocate(word_size); coleenp@548: if (result != NULL) { coleenp@548: return result; coleenp@548: } coleenp@548: do { coleenp@548: HeapWord* old_limit = eden()->soft_end(); coleenp@548: if (old_limit < eden()->end()) { coleenp@548: // Tell the next generation we reached a limit. coleenp@548: HeapWord* new_limit = coleenp@548: next_gen()->allocation_limit_reached(eden(), eden()->top(), word_size); coleenp@548: if (new_limit != NULL) { coleenp@548: Atomic::cmpxchg_ptr(new_limit, eden()->soft_end_addr(), old_limit); coleenp@548: } else { coleenp@548: assert(eden()->soft_end() == eden()->end(), coleenp@548: "invalid state after allocation_limit_reached returned null"); coleenp@548: } coleenp@548: } else { coleenp@548: // The allocation failed and the soft limit is equal to the hard limit, coleenp@548: // there are no reasons to do an attempt to allocate coleenp@548: assert(old_limit == eden()->end(), "sanity check"); coleenp@548: break; coleenp@548: } coleenp@548: // Try to allocate until succeeded or the soft limit can't be adjusted coleenp@548: result = eden()->par_allocate(word_size); coleenp@548: } while (result == NULL); coleenp@548: coleenp@548: // If the eden is full and the last collection bailed out, we are running coleenp@548: // out of heap space, and we try to allocate the from-space, too. coleenp@548: // allocate_from_space can't be inlined because that would introduce a coleenp@548: // circular dependency at compile time. coleenp@548: if (result == NULL) { coleenp@548: result = allocate_from_space(word_size); coleenp@548: } coleenp@548: return result; coleenp@548: } coleenp@548: coleenp@548: HeapWord* DefNewGeneration::par_allocate(size_t word_size, coleenp@548: bool is_tlab) { coleenp@548: return eden()->par_allocate(word_size); coleenp@548: } coleenp@548: coleenp@548: void DefNewGeneration::gc_prologue(bool full) { coleenp@548: // Ensure that _end and _soft_end are the same in eden space. coleenp@548: eden()->set_soft_end(eden()->end()); coleenp@548: } coleenp@548: coleenp@548: size_t DefNewGeneration::tlab_capacity() const { coleenp@548: return eden()->capacity(); coleenp@548: } coleenp@548: coleenp@548: size_t DefNewGeneration::unsafe_max_tlab_alloc() const { coleenp@548: return unsafe_max_alloc_nogc(); coleenp@548: }