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/_psMarkSweepDecorator.cpp.incl" duke@435: duke@435: PSMarkSweepDecorator* PSMarkSweepDecorator::_destination_decorator = NULL; duke@435: duke@435: duke@435: void PSMarkSweepDecorator::set_destination_decorator_tenured() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: _destination_decorator = heap->old_gen()->object_mark_sweep(); duke@435: } duke@435: duke@435: void PSMarkSweepDecorator::set_destination_decorator_perm_gen() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: _destination_decorator = heap->perm_gen()->object_mark_sweep(); duke@435: } duke@435: duke@435: void PSMarkSweepDecorator::advance_destination_decorator() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: assert(_destination_decorator != NULL, "Sanity"); duke@435: guarantee(_destination_decorator != heap->perm_gen()->object_mark_sweep(), "Cannot advance perm gen decorator"); duke@435: duke@435: PSMarkSweepDecorator* first = heap->old_gen()->object_mark_sweep(); duke@435: PSMarkSweepDecorator* second = heap->young_gen()->eden_mark_sweep(); duke@435: PSMarkSweepDecorator* third = heap->young_gen()->from_mark_sweep(); duke@435: PSMarkSweepDecorator* fourth = heap->young_gen()->to_mark_sweep(); duke@435: duke@435: if ( _destination_decorator == first ) { duke@435: _destination_decorator = second; duke@435: } else if ( _destination_decorator == second ) { duke@435: _destination_decorator = third; duke@435: } else if ( _destination_decorator == third ) { duke@435: _destination_decorator = fourth; duke@435: } else { duke@435: fatal("PSMarkSweep attempting to advance past last compaction area"); duke@435: } duke@435: } duke@435: duke@435: PSMarkSweepDecorator* PSMarkSweepDecorator::destination_decorator() { duke@435: assert(_destination_decorator != NULL, "Sanity"); duke@435: duke@435: return _destination_decorator; duke@435: } duke@435: duke@435: // FIX ME FIX ME FIX ME FIX ME!!!!!!!!! duke@435: // The object forwarding code is duplicated. Factor this out!!!!! duke@435: // duke@435: // This method "precompacts" objects inside its space to dest. It places forwarding duke@435: // pointers into markOops for use by adjust_pointers. If "dest" should overflow, we duke@435: // finish by compacting into our own space. duke@435: duke@435: void PSMarkSweepDecorator::precompact() { duke@435: // Reset our own compact top. duke@435: set_compaction_top(space()->bottom()); duke@435: duke@435: /* We allow some amount of garbage towards the bottom of the space, so duke@435: * we don't start compacting before there is a significant gain to be made. duke@435: * Occasionally, we want to ensure a full compaction, which is determined duke@435: * by the MarkSweepAlwaysCompactCount parameter. This is a significant duke@435: * performance improvement! duke@435: */ duke@435: bool skip_dead = ((PSMarkSweep::total_invocations() % MarkSweepAlwaysCompactCount) != 0); duke@435: duke@435: ssize_t allowed_deadspace = 0; duke@435: if (skip_dead) { duke@435: int ratio = allowed_dead_ratio(); duke@435: allowed_deadspace = (space()->capacity_in_bytes() * ratio / 100) / HeapWordSize; duke@435: } duke@435: duke@435: // Fetch the current destination decorator duke@435: PSMarkSweepDecorator* dest = destination_decorator(); duke@435: ObjectStartArray* start_array = dest->start_array(); duke@435: duke@435: HeapWord* compact_top = dest->compaction_top(); duke@435: HeapWord* compact_end = dest->space()->end(); duke@435: duke@435: HeapWord* q = space()->bottom(); duke@435: HeapWord* t = space()->top(); duke@435: duke@435: HeapWord* end_of_live= q; /* One byte beyond the last byte of the last duke@435: live object. */ duke@435: HeapWord* first_dead = space()->end(); /* The first dead object. */ duke@435: LiveRange* liveRange = NULL; /* The current live range, recorded in the duke@435: first header of preceding free area. */ duke@435: _first_dead = first_dead; duke@435: duke@435: const intx interval = PrefetchScanIntervalInBytes; duke@435: duke@435: while (q < t) { duke@435: assert(oop(q)->mark()->is_marked() || oop(q)->mark()->is_unlocked() || duke@435: oop(q)->mark()->has_bias_pattern(), duke@435: "these are the only valid states during a mark sweep"); duke@435: if (oop(q)->is_gc_marked()) { duke@435: /* prefetch beyond q */ duke@435: Prefetch::write(q, interval); duke@435: size_t size = oop(q)->size(); duke@435: duke@435: size_t compaction_max_size = pointer_delta(compact_end, compact_top); duke@435: duke@435: // This should only happen if a space in the young gen overflows the duke@435: // old gen. If that should happen, we null out the start_array, because duke@435: // the young spaces are not covered by one. duke@435: while(size > compaction_max_size) { duke@435: // First record the last compact_top duke@435: dest->set_compaction_top(compact_top); duke@435: duke@435: // Advance to the next compaction decorator duke@435: advance_destination_decorator(); duke@435: dest = destination_decorator(); duke@435: duke@435: // Update compaction info duke@435: start_array = dest->start_array(); duke@435: compact_top = dest->compaction_top(); duke@435: compact_end = dest->space()->end(); duke@435: assert(compact_top == dest->space()->bottom(), "Advanced to space already in use"); duke@435: assert(compact_end > compact_top, "Must always be space remaining"); duke@435: compaction_max_size = duke@435: pointer_delta(compact_end, compact_top); duke@435: } duke@435: duke@435: // store the forwarding pointer into the mark word duke@435: if (q != compact_top) { duke@435: oop(q)->forward_to(oop(compact_top)); duke@435: assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark"); duke@435: } else { duke@435: // Don't clear the mark since it's confuses parallel old duke@435: // verification. duke@435: if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) { duke@435: // if the object isn't moving we can just set the mark to the default duke@435: // mark and handle it specially later on. duke@435: oop(q)->init_mark(); duke@435: } duke@435: assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL"); duke@435: } duke@435: duke@435: // Update object start array duke@435: if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) { duke@435: if (start_array) duke@435: start_array->allocate_block(compact_top); duke@435: } duke@435: coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(oop(q), size)); duke@435: compact_top += size; duke@435: assert(compact_top <= dest->space()->end(), duke@435: "Exceeding space in destination"); duke@435: duke@435: q += size; duke@435: end_of_live = q; duke@435: } else { duke@435: /* run over all the contiguous dead objects */ duke@435: HeapWord* end = q; duke@435: do { duke@435: /* prefetch beyond end */ duke@435: Prefetch::write(end, interval); duke@435: end += oop(end)->size(); duke@435: } while (end < t && (!oop(end)->is_gc_marked())); duke@435: duke@435: /* see if we might want to pretend this object is alive so that duke@435: * we don't have to compact quite as often. duke@435: */ duke@435: if (allowed_deadspace > 0 && q == compact_top) { duke@435: size_t sz = pointer_delta(end, q); duke@435: if (insert_deadspace(allowed_deadspace, q, sz)) { duke@435: size_t compaction_max_size = pointer_delta(compact_end, compact_top); duke@435: duke@435: // This should only happen if a space in the young gen overflows the duke@435: // old gen. If that should happen, we null out the start_array, because duke@435: // the young spaces are not covered by one. duke@435: while (sz > compaction_max_size) { duke@435: // First record the last compact_top duke@435: dest->set_compaction_top(compact_top); duke@435: duke@435: // Advance to the next compaction decorator duke@435: advance_destination_decorator(); duke@435: dest = destination_decorator(); duke@435: duke@435: // Update compaction info duke@435: start_array = dest->start_array(); duke@435: compact_top = dest->compaction_top(); duke@435: compact_end = dest->space()->end(); duke@435: assert(compact_top == dest->space()->bottom(), "Advanced to space already in use"); duke@435: assert(compact_end > compact_top, "Must always be space remaining"); duke@435: compaction_max_size = duke@435: pointer_delta(compact_end, compact_top); duke@435: } duke@435: duke@435: // store the forwarding pointer into the mark word duke@435: if (q != compact_top) { duke@435: oop(q)->forward_to(oop(compact_top)); duke@435: assert(oop(q)->is_gc_marked(), "encoding the pointer should preserve the mark"); duke@435: } else { duke@435: // if the object isn't moving we can just set the mark to the default duke@435: // Don't clear the mark since it's confuses parallel old duke@435: // verification. duke@435: if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) { duke@435: // mark and handle it specially later on. duke@435: oop(q)->init_mark(); duke@435: } duke@435: assert(oop(q)->forwardee() == NULL, "should be forwarded to NULL"); duke@435: } duke@435: duke@435: if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) { duke@435: // Update object start array duke@435: if (start_array) duke@435: start_array->allocate_block(compact_top); duke@435: } duke@435: coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::register_live_oop(oop(q), sz)); duke@435: compact_top += sz; duke@435: assert(compact_top <= dest->space()->end(), duke@435: "Exceeding space in destination"); duke@435: duke@435: q = end; duke@435: end_of_live = end; duke@435: continue; duke@435: } duke@435: } duke@435: duke@435: /* for the previous LiveRange, record the end of the live objects. */ duke@435: if (liveRange) { duke@435: liveRange->set_end(q); duke@435: } duke@435: duke@435: /* record the current LiveRange object. duke@435: * liveRange->start() is overlaid on the mark word. duke@435: */ duke@435: liveRange = (LiveRange*)q; duke@435: liveRange->set_start(end); duke@435: liveRange->set_end(end); duke@435: duke@435: /* see if this is the first dead region. */ duke@435: if (q < first_dead) { duke@435: first_dead = q; duke@435: } duke@435: duke@435: /* move on to the next object */ duke@435: q = end; duke@435: } duke@435: } duke@435: duke@435: assert(q == t, "just checking"); duke@435: if (liveRange != NULL) { duke@435: liveRange->set_end(q); duke@435: } duke@435: _end_of_live = end_of_live; duke@435: if (end_of_live < first_dead) { duke@435: first_dead = end_of_live; duke@435: } duke@435: _first_dead = first_dead; duke@435: duke@435: // Update compaction top duke@435: dest->set_compaction_top(compact_top); duke@435: } duke@435: duke@435: bool PSMarkSweepDecorator::insert_deadspace(ssize_t& allowed_deadspace_words, duke@435: HeapWord* q, size_t deadlength) { duke@435: allowed_deadspace_words -= deadlength; duke@435: if (allowed_deadspace_words >= 0) { duke@435: oop(q)->set_mark(markOopDesc::prototype()->set_marked()); duke@435: const size_t aligned_min_int_array_size = duke@435: align_object_size(typeArrayOopDesc::header_size(T_INT)); duke@435: if (deadlength >= aligned_min_int_array_size) { duke@435: oop(q)->set_klass(Universe::intArrayKlassObj()); duke@435: assert(((deadlength - aligned_min_int_array_size) * (HeapWordSize/sizeof(jint))) < (size_t)max_jint, duke@435: "deadspace too big for Arrayoop"); duke@435: typeArrayOop(q)->set_length((int)((deadlength - aligned_min_int_array_size) duke@435: * (HeapWordSize/sizeof(jint)))); duke@435: } else { duke@435: assert((int) deadlength == instanceOopDesc::header_size(), duke@435: "size for smallest fake dead object doesn't match"); duke@435: oop(q)->set_klass(SystemDictionary::object_klass()); duke@435: } duke@435: assert((int) deadlength == oop(q)->size(), duke@435: "make sure size for fake dead object match"); duke@435: // Recall that we required "q == compaction_top". duke@435: return true; duke@435: } else { duke@435: allowed_deadspace_words = 0; duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: void PSMarkSweepDecorator::adjust_pointers() { duke@435: // adjust all the interior pointers to point at the new locations of objects duke@435: // Used by MarkSweep::mark_sweep_phase3() duke@435: duke@435: HeapWord* q = space()->bottom(); duke@435: HeapWord* t = _end_of_live; // Established by "prepare_for_compaction". duke@435: duke@435: assert(_first_dead <= _end_of_live, "Stands to reason, no?"); duke@435: duke@435: if (q < t && _first_dead > q && duke@435: !oop(q)->is_gc_marked()) { duke@435: // we have a chunk of the space which hasn't moved and we've duke@435: // reinitialized the mark word during the previous pass, so we can't duke@435: // use is_gc_marked for the traversal. duke@435: HeapWord* end = _first_dead; duke@435: duke@435: while (q < end) { coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); duke@435: // point all the oops to the new location duke@435: size_t size = oop(q)->adjust_pointers(); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); duke@435: q += size; duke@435: } duke@435: duke@435: if (_first_dead == t) { duke@435: q = t; duke@435: } else { duke@435: // $$$ This is funky. Using this to read the previously written duke@435: // LiveRange. See also use below. duke@435: q = (HeapWord*)oop(_first_dead)->mark()->decode_pointer(); duke@435: } duke@435: } duke@435: const intx interval = PrefetchScanIntervalInBytes; duke@435: duke@435: debug_only(HeapWord* prev_q = NULL); duke@435: while (q < t) { duke@435: // prefetch beyond q duke@435: Prefetch::write(q, interval); duke@435: if (oop(q)->is_gc_marked()) { duke@435: // q is alive coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::track_interior_pointers(oop(q))); duke@435: // point all the oops to the new location duke@435: size_t size = oop(q)->adjust_pointers(); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::check_interior_pointers()); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::validate_live_oop(oop(q), size)); duke@435: debug_only(prev_q = q); duke@435: q += size; duke@435: } else { duke@435: // q is not a live object, so its mark should point at the next duke@435: // live object duke@435: debug_only(prev_q = q); duke@435: q = (HeapWord*) oop(q)->mark()->decode_pointer(); duke@435: assert(q > prev_q, "we should be moving forward through memory"); duke@435: } duke@435: } duke@435: duke@435: assert(q == t, "just checking"); duke@435: } duke@435: duke@435: void PSMarkSweepDecorator::compact(bool mangle_free_space ) { duke@435: // Copy all live objects to their new location duke@435: // Used by MarkSweep::mark_sweep_phase4() duke@435: duke@435: HeapWord* q = space()->bottom(); duke@435: HeapWord* const t = _end_of_live; duke@435: debug_only(HeapWord* prev_q = NULL); duke@435: duke@435: if (q < t && _first_dead > q && duke@435: !oop(q)->is_gc_marked()) { duke@435: #ifdef ASSERT duke@435: // we have a chunk of the space which hasn't moved and we've reinitialized the duke@435: // mark word during the previous pass, so we can't use is_gc_marked for the duke@435: // traversal. duke@435: HeapWord* const end = _first_dead; duke@435: duke@435: while (q < end) { duke@435: size_t size = oop(q)->size(); duke@435: assert(!oop(q)->is_gc_marked(), "should be unmarked (special dense prefix handling)"); coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, q)); duke@435: debug_only(prev_q = q); duke@435: q += size; duke@435: } duke@435: #endif duke@435: duke@435: if (_first_dead == t) { duke@435: q = t; duke@435: } else { duke@435: // $$$ Funky duke@435: q = (HeapWord*) oop(_first_dead)->mark()->decode_pointer(); duke@435: } duke@435: } duke@435: duke@435: const intx scan_interval = PrefetchScanIntervalInBytes; duke@435: const intx copy_interval = PrefetchCopyIntervalInBytes; duke@435: duke@435: while (q < t) { duke@435: if (!oop(q)->is_gc_marked()) { duke@435: // mark is pointer to next marked oop duke@435: debug_only(prev_q = q); duke@435: q = (HeapWord*) oop(q)->mark()->decode_pointer(); duke@435: assert(q > prev_q, "we should be moving forward through memory"); duke@435: } else { duke@435: // prefetch beyond q duke@435: Prefetch::read(q, scan_interval); duke@435: duke@435: // size and destination duke@435: size_t size = oop(q)->size(); duke@435: HeapWord* compaction_top = (HeapWord*)oop(q)->forwardee(); duke@435: duke@435: // prefetch beyond compaction_top duke@435: Prefetch::write(compaction_top, copy_interval); duke@435: duke@435: // copy object and reinit its mark coleenp@548: VALIDATE_MARK_SWEEP_ONLY(MarkSweep::live_oop_moved_to(q, size, compaction_top)); duke@435: assert(q != compaction_top, "everything in this pass should be moving"); duke@435: Copy::aligned_conjoint_words(q, compaction_top, size); duke@435: oop(compaction_top)->init_mark(); duke@435: assert(oop(compaction_top)->klass() != NULL, "should have a class"); duke@435: duke@435: debug_only(prev_q = q); duke@435: q += size; duke@435: } duke@435: } duke@435: duke@435: assert(compaction_top() >= space()->bottom() && compaction_top() <= space()->end(), duke@435: "should point inside space"); duke@435: space()->set_top(compaction_top()); duke@435: jmasa@698: if (mangle_free_space) { jmasa@698: space()->mangle_unused_area(); jmasa@698: } duke@435: }