duke@435: /* duke@435: * Copyright 2005-2006 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/_psCompactionManager.cpp.incl" duke@435: duke@435: PSOldGen* ParCompactionManager::_old_gen = NULL; duke@435: ParCompactionManager** ParCompactionManager::_manager_array = NULL; duke@435: OopTaskQueueSet* ParCompactionManager::_stack_array = NULL; duke@435: ObjectStartArray* ParCompactionManager::_start_array = NULL; duke@435: ParMarkBitMap* ParCompactionManager::_mark_bitmap = NULL; duke@435: ChunkTaskQueueSet* ParCompactionManager::_chunk_array = NULL; duke@435: duke@435: ParCompactionManager::ParCompactionManager() : duke@435: _action(CopyAndUpdate) { duke@435: duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: _old_gen = heap->old_gen(); duke@435: _start_array = old_gen()->start_array(); duke@435: duke@435: duke@435: marking_stack()->initialize(); duke@435: duke@435: // We want the overflow stack to be permanent duke@435: _overflow_stack = new (ResourceObj::C_HEAP) GrowableArray(10, true); duke@435: #ifdef USE_ChunkTaskQueueWithOverflow duke@435: chunk_stack()->initialize(); duke@435: #else duke@435: chunk_stack()->initialize(); duke@435: duke@435: // We want the overflow stack to be permanent duke@435: _chunk_overflow_stack = duke@435: new (ResourceObj::C_HEAP) GrowableArray(10, true); duke@435: #endif duke@435: duke@435: // Note that _revisit_klass_stack is allocated out of the duke@435: // C heap (as opposed to out of ResourceArena). duke@435: int size = duke@435: (SystemDictionary::number_of_classes() * 2) * 2 / ParallelGCThreads; duke@435: _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray(size, true); duke@435: duke@435: } duke@435: duke@435: ParCompactionManager::~ParCompactionManager() { duke@435: delete _overflow_stack; duke@435: delete _revisit_klass_stack; duke@435: // _manager_array and _stack_array are statics duke@435: // shared with all instances of ParCompactionManager duke@435: // should not be deallocated. duke@435: } duke@435: duke@435: void ParCompactionManager::initialize(ParMarkBitMap* mbm) { duke@435: assert(PSParallelCompact::gc_task_manager() != NULL, duke@435: "Needed for initialization"); duke@435: duke@435: _mark_bitmap = mbm; duke@435: duke@435: uint parallel_gc_threads = PSParallelCompact::gc_task_manager()->workers(); duke@435: duke@435: assert(_manager_array == NULL, "Attempt to initialize twice"); duke@435: _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1 ); duke@435: guarantee(_manager_array != NULL, "Could not initialize promotion manager"); duke@435: duke@435: _stack_array = new OopTaskQueueSet(parallel_gc_threads); duke@435: guarantee(_stack_array != NULL, "Count not initialize promotion manager"); duke@435: _chunk_array = new ChunkTaskQueueSet(parallel_gc_threads); duke@435: guarantee(_chunk_array != NULL, "Count not initialize promotion manager"); duke@435: duke@435: // Create and register the ParCompactionManager(s) for the worker threads. duke@435: for(uint i=0; iregister_queue(i, _manager_array[i]->marking_stack()); duke@435: #ifdef USE_ChunkTaskQueueWithOverflow duke@435: chunk_array()->register_queue(i, _manager_array[i]->chunk_stack()->task_queue()); duke@435: #else duke@435: chunk_array()->register_queue(i, _manager_array[i]->chunk_stack()); duke@435: #endif duke@435: } duke@435: duke@435: // The VMThread gets its own ParCompactionManager, which is not available duke@435: // for work stealing. duke@435: _manager_array[parallel_gc_threads] = new ParCompactionManager(); duke@435: guarantee(_manager_array[parallel_gc_threads] != NULL, duke@435: "Could not create ParCompactionManager"); duke@435: assert(PSParallelCompact::gc_task_manager()->workers() != 0, duke@435: "Not initialized?"); duke@435: } duke@435: duke@435: bool ParCompactionManager::should_update() { duke@435: assert(action() != NotValid, "Action is not set"); duke@435: return (action() == ParCompactionManager::Update) || duke@435: (action() == ParCompactionManager::CopyAndUpdate) || duke@435: (action() == ParCompactionManager::UpdateAndCopy); duke@435: } duke@435: duke@435: bool ParCompactionManager::should_copy() { duke@435: assert(action() != NotValid, "Action is not set"); duke@435: return (action() == ParCompactionManager::Copy) || duke@435: (action() == ParCompactionManager::CopyAndUpdate) || duke@435: (action() == ParCompactionManager::UpdateAndCopy); duke@435: } duke@435: duke@435: bool ParCompactionManager::should_verify_only() { duke@435: assert(action() != NotValid, "Action is not set"); duke@435: return action() == ParCompactionManager::VerifyUpdate; duke@435: } duke@435: duke@435: bool ParCompactionManager::should_reset_only() { duke@435: assert(action() != NotValid, "Action is not set"); duke@435: return action() == ParCompactionManager::ResetObjects; duke@435: } duke@435: duke@435: // For now save on a stack duke@435: void ParCompactionManager::save_for_scanning(oop m) { duke@435: stack_push(m); duke@435: } duke@435: duke@435: void ParCompactionManager::stack_push(oop obj) { duke@435: duke@435: if(!marking_stack()->push(obj)) { duke@435: overflow_stack()->push(obj); duke@435: } duke@435: } duke@435: duke@435: oop ParCompactionManager::retrieve_for_scanning() { duke@435: duke@435: // Should not be used in the parallel case duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: } duke@435: duke@435: // Save chunk on a stack duke@435: void ParCompactionManager::save_for_processing(size_t chunk_index) { duke@435: #ifdef ASSERT duke@435: const ParallelCompactData& sd = PSParallelCompact::summary_data(); duke@435: ParallelCompactData::ChunkData* const chunk_ptr = sd.chunk(chunk_index); duke@435: assert(chunk_ptr->claimed(), "must be claimed"); duke@435: assert(chunk_ptr->_pushed++ == 0, "should only be pushed once"); duke@435: #endif duke@435: chunk_stack_push(chunk_index); duke@435: } duke@435: duke@435: void ParCompactionManager::chunk_stack_push(size_t chunk_index) { duke@435: duke@435: #ifdef USE_ChunkTaskQueueWithOverflow duke@435: chunk_stack()->save(chunk_index); duke@435: #else duke@435: if(!chunk_stack()->push(chunk_index)) { duke@435: chunk_overflow_stack()->push(chunk_index); duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: bool ParCompactionManager::retrieve_for_processing(size_t& chunk_index) { duke@435: #ifdef USE_ChunkTaskQueueWithOverflow duke@435: return chunk_stack()->retrieve(chunk_index); duke@435: #else duke@435: // Should not be used in the parallel case duke@435: ShouldNotReachHere(); duke@435: return false; duke@435: #endif duke@435: } duke@435: duke@435: ParCompactionManager* duke@435: ParCompactionManager::gc_thread_compaction_manager(int index) { duke@435: assert(index >= 0 && index < (int)ParallelGCThreads, "index out of range"); duke@435: assert(_manager_array != NULL, "Sanity"); duke@435: return _manager_array[index]; duke@435: } duke@435: duke@435: void ParCompactionManager::reset() { duke@435: for(uint i=0; irevisit_klass_stack()->clear(); duke@435: } duke@435: } duke@435: duke@435: void ParCompactionManager::drain_marking_stacks(OopClosure* blk) { duke@435: #ifdef ASSERT duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: MutableSpace* to_space = heap->young_gen()->to_space(); duke@435: MutableSpace* old_space = heap->old_gen()->object_space(); duke@435: MutableSpace* perm_space = heap->perm_gen()->object_space(); duke@435: #endif /* ASSERT */ duke@435: duke@435: duke@435: do { duke@435: duke@435: // Drain overflow stack first, so other threads can steal from duke@435: // claimed stack while we work. duke@435: while(!overflow_stack()->is_empty()) { duke@435: oop obj = overflow_stack()->pop(); duke@435: obj->follow_contents(this); duke@435: } duke@435: duke@435: oop obj; duke@435: // obj is a reference!!! duke@435: while (marking_stack()->pop_local(obj)) { duke@435: // It would be nice to assert about the type of objects we might duke@435: // pop, but they can come from anywhere, unfortunately. duke@435: obj->follow_contents(this); duke@435: } duke@435: } while((marking_stack()->size() != 0) || (overflow_stack()->length() != 0)); duke@435: duke@435: assert(marking_stack()->size() == 0, "Sanity"); duke@435: assert(overflow_stack()->length() == 0, "Sanity"); duke@435: } duke@435: duke@435: void ParCompactionManager::drain_chunk_overflow_stack() { duke@435: size_t chunk_index = (size_t) -1; duke@435: while(chunk_stack()->retrieve_from_overflow(chunk_index)) { duke@435: PSParallelCompact::fill_and_update_chunk(this, chunk_index); duke@435: } duke@435: } duke@435: duke@435: void ParCompactionManager::drain_chunk_stacks() { duke@435: #ifdef ASSERT duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: MutableSpace* to_space = heap->young_gen()->to_space(); duke@435: MutableSpace* old_space = heap->old_gen()->object_space(); duke@435: MutableSpace* perm_space = heap->perm_gen()->object_space(); duke@435: #endif /* ASSERT */ duke@435: duke@435: #if 1 // def DO_PARALLEL - the serial code hasn't been updated duke@435: do { duke@435: duke@435: #ifdef USE_ChunkTaskQueueWithOverflow duke@435: // Drain overflow stack first, so other threads can steal from duke@435: // claimed stack while we work. duke@435: size_t chunk_index = (size_t) -1; duke@435: while(chunk_stack()->retrieve_from_overflow(chunk_index)) { duke@435: PSParallelCompact::fill_and_update_chunk(this, chunk_index); duke@435: } duke@435: duke@435: while (chunk_stack()->retrieve_from_stealable_queue(chunk_index)) { duke@435: PSParallelCompact::fill_and_update_chunk(this, chunk_index); duke@435: } duke@435: } while (!chunk_stack()->is_empty()); duke@435: #else duke@435: // Drain overflow stack first, so other threads can steal from duke@435: // claimed stack while we work. duke@435: while(!chunk_overflow_stack()->is_empty()) { duke@435: size_t chunk_index = chunk_overflow_stack()->pop(); duke@435: PSParallelCompact::fill_and_update_chunk(this, chunk_index); duke@435: } duke@435: duke@435: size_t chunk_index = -1; duke@435: // obj is a reference!!! duke@435: while (chunk_stack()->pop_local(chunk_index)) { duke@435: // It would be nice to assert about the type of objects we might duke@435: // pop, but they can come from anywhere, unfortunately. duke@435: PSParallelCompact::fill_and_update_chunk(this, chunk_index); duke@435: } duke@435: } while((chunk_stack()->size() != 0) || duke@435: (chunk_overflow_stack()->length() != 0)); duke@435: #endif duke@435: duke@435: #ifdef USE_ChunkTaskQueueWithOverflow duke@435: assert(chunk_stack()->is_empty(), "Sanity"); duke@435: #else duke@435: assert(chunk_stack()->size() == 0, "Sanity"); duke@435: assert(chunk_overflow_stack()->length() == 0, "Sanity"); duke@435: #endif duke@435: #else duke@435: oop obj; duke@435: while (obj = retrieve_for_scanning()) { duke@435: obj->follow_contents(this); duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: bool ParCompactionManager::stacks_have_been_allocated() { duke@435: return (revisit_klass_stack()->data_addr() != NULL); duke@435: } duke@435: #endif