duke@435: /* minqi@5097: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/gcTaskManager.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/objectStartArray.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/parMarkBitMap.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psCompactionManager.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psOldGen.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psParallelCompact.hpp" stefank@2314: #include "oops/objArrayKlass.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "oops/oop.pcgc.inline.hpp" stefank@2314: #include "utilities/stack.inline.hpp" duke@435: duke@435: PSOldGen* ParCompactionManager::_old_gen = NULL; duke@435: ParCompactionManager** ParCompactionManager::_manager_array = NULL; jmasa@3294: jmasa@3294: RegionTaskQueue** ParCompactionManager::_region_list = NULL; jmasa@3294: duke@435: OopTaskQueueSet* ParCompactionManager::_stack_array = NULL; jcoomes@1746: ParCompactionManager::ObjArrayTaskQueueSet* jcoomes@1746: ParCompactionManager::_objarray_queues = NULL; duke@435: ObjectStartArray* ParCompactionManager::_start_array = NULL; duke@435: ParMarkBitMap* ParCompactionManager::_mark_bitmap = NULL; jcoomes@1993: RegionTaskQueueSet* ParCompactionManager::_region_array = NULL; duke@435: jmasa@3294: uint* ParCompactionManager::_recycled_stack_index = NULL; jmasa@3294: int ParCompactionManager::_recycled_top = -1; jmasa@3294: int ParCompactionManager::_recycled_bottom = -1; jmasa@3294: duke@435: ParCompactionManager::ParCompactionManager() : jmasa@3294: _action(CopyAndUpdate), jmasa@3294: _region_stack(NULL), jmasa@3294: _region_stack_index((uint)max_uintx) { 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: marking_stack()->initialize(); jcoomes@1993: _objarray_stack.initialize(); jmasa@3294: } jmasa@3294: jmasa@3294: ParCompactionManager::~ParCompactionManager() { jmasa@3294: delete _recycled_stack_index; 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"); zgu@3900: _manager_array = NEW_C_HEAP_ARRAY(ParCompactionManager*, parallel_gc_threads+1, mtGC); jcoomes@1746: guarantee(_manager_array != NULL, "Could not allocate manager_array"); duke@435: jmasa@3294: _region_list = NEW_C_HEAP_ARRAY(RegionTaskQueue*, zgu@3900: parallel_gc_threads+1, mtGC); jmasa@3294: guarantee(_region_list != NULL, "Could not initialize promotion manager"); jmasa@3294: zgu@3900: _recycled_stack_index = NEW_C_HEAP_ARRAY(uint, parallel_gc_threads, mtGC); jmasa@3294: jmasa@3294: // parallel_gc-threads + 1 to be consistent with the number of jmasa@3294: // compaction managers. jmasa@3294: for(uint i=0; iinitialize(); jmasa@3294: } jmasa@3294: duke@435: _stack_array = new OopTaskQueueSet(parallel_gc_threads); jcoomes@1746: guarantee(_stack_array != NULL, "Could not allocate stack_array"); jcoomes@1746: _objarray_queues = new ObjArrayTaskQueueSet(parallel_gc_threads); jcoomes@1746: guarantee(_objarray_queues != NULL, "Could not allocate objarray_queues"); jcoomes@810: _region_array = new RegionTaskQueueSet(parallel_gc_threads); jcoomes@1746: guarantee(_region_array != NULL, "Could not allocate region_array"); 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()); jcoomes@1993: _objarray_queues->register_queue(i, &_manager_array[i]->_objarray_stack); jmasa@3294: region_array()->register_queue(i, region_list(i)); 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: jmasa@3294: int ParCompactionManager::pop_recycled_stack_index() { jmasa@3294: assert(_recycled_bottom <= _recycled_top, "list is empty"); jmasa@3294: // Get the next available index jmasa@3294: if (_recycled_bottom < _recycled_top) { jmasa@3294: uint cur, next, last; jmasa@3294: do { jmasa@3294: cur = _recycled_bottom; jmasa@3294: next = cur + 1; jmasa@3294: last = Atomic::cmpxchg(next, &_recycled_bottom, cur); jmasa@3294: } while (cur != last); jmasa@3294: return _recycled_stack_index[next]; jmasa@3294: } else { jmasa@3294: return -1; jmasa@3294: } jmasa@3294: } jmasa@3294: jmasa@3294: void ParCompactionManager::push_recycled_stack_index(uint v) { jmasa@3294: // Get the next available index jmasa@3294: int cur = Atomic::add(1, &_recycled_top); jmasa@3294: _recycled_stack_index[cur] = v; jmasa@3294: assert(_recycled_bottom <= _recycled_top, "list top and bottom are wrong"); jmasa@3294: } jmasa@3294: 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: jmasa@3294: void ParCompactionManager::region_list_push(uint list_index, jmasa@3294: size_t region_index) { jmasa@3294: region_list(list_index)->push(region_index); jmasa@3294: } jmasa@3294: jmasa@3294: void ParCompactionManager::verify_region_list_empty(uint list_index) { jmasa@3294: assert(region_list(list_index)->is_empty(), "Not empty"); jmasa@3294: } jmasa@3294: 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: jcoomes@1746: void ParCompactionManager::follow_marking_stacks() { duke@435: do { jcoomes@1746: // Drain the overflow stack first, to allow stealing from the marking stack. jcoomes@1750: oop obj; jcoomes@1993: while (marking_stack()->pop_overflow(obj)) { jcoomes@1993: obj->follow_contents(this); jcoomes@1746: } jcoomes@1746: while (marking_stack()->pop_local(obj)) { duke@435: obj->follow_contents(this); duke@435: } duke@435: jcoomes@1750: // Process ObjArrays one at a time to avoid marking stack bloat. jcoomes@1746: ObjArrayTask task; minqi@5097: if (_objarray_stack.pop_overflow(task) || _objarray_stack.pop_local(task)) { minqi@5097: ObjArrayKlass* k = (ObjArrayKlass*)task.obj()->klass(); jcoomes@1746: k->oop_follow_contents(this, task.obj(), task.index()); jcoomes@1746: } jcoomes@1746: } while (!marking_stacks_empty()); duke@435: jcoomes@1746: assert(marking_stacks_empty(), "Sanity"); duke@435: } duke@435: jcoomes@810: void ParCompactionManager::drain_region_stacks() { duke@435: do { jcoomes@1993: // Drain overflow stack first so other threads can steal. jcoomes@1993: size_t region_index; jcoomes@1993: while (region_stack()->pop_overflow(region_index)) { jcoomes@810: PSParallelCompact::fill_and_update_region(this, region_index); duke@435: } duke@435: jcoomes@1993: while (region_stack()->pop_local(region_index)) { jcoomes@810: PSParallelCompact::fill_and_update_region(this, region_index); duke@435: } jcoomes@810: } while (!region_stack()->is_empty()); duke@435: }