duke@435: /* drchase@6680: * Copyright (c) 2005, 2014, 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 "code/codeCache.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/pcTasks.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psParallelCompact.hpp" sla@5237: #include "gc_implementation/shared/gcTimer.hpp" sla@5237: #include "gc_implementation/shared/gcTraceTime.hpp" stefank@2314: #include "gc_interface/collectedHeap.hpp" stefank@2314: #include "memory/universe.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 "prims/jvmtiExport.hpp" stefank@2314: #include "runtime/fprofiler.hpp" stefank@2314: #include "runtime/jniHandles.hpp" stefank@2314: #include "runtime/thread.hpp" stefank@2314: #include "runtime/vmThread.hpp" stefank@2314: #include "services/management.hpp" duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: duke@435: // duke@435: // ThreadRootsMarkingTask duke@435: // duke@435: duke@435: void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) { duke@435: assert(Universe::heap()->is_gc_active(), "called outside gc"); duke@435: duke@435: ResourceMark rm; duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("ThreadRootsMarkingTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); stefank@4298: duke@435: PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm); stefank@4298: CLDToOopClosure mark_and_push_from_clds(&mark_and_push_closure, true); stefank@6992: MarkingCodeBlobClosure mark_and_push_in_blobs(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations); duke@435: duke@435: if (_java_thread != NULL) stefank@4298: _java_thread->oops_do( stefank@4298: &mark_and_push_closure, stefank@4298: &mark_and_push_from_clds, stefank@4298: &mark_and_push_in_blobs); duke@435: duke@435: if (_vm_thread != NULL) stefank@4298: _vm_thread->oops_do( stefank@4298: &mark_and_push_closure, stefank@4298: &mark_and_push_from_clds, stefank@4298: &mark_and_push_in_blobs); duke@435: duke@435: // Do the real work jcoomes@1746: cm->follow_marking_stacks(); duke@435: } duke@435: duke@435: duke@435: void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) { duke@435: assert(Universe::heap()->is_gc_active(), "called outside gc"); duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("MarkFromRootsTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); duke@435: PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm); coleenp@4037: PSParallelCompact::FollowKlassClosure follow_klass_closure(&mark_and_push_closure); duke@435: duke@435: switch (_root_type) { duke@435: case universe: duke@435: Universe::oops_do(&mark_and_push_closure); duke@435: break; duke@435: duke@435: case jni_handles: duke@435: JNIHandles::oops_do(&mark_and_push_closure); duke@435: break; duke@435: duke@435: case threads: duke@435: { duke@435: ResourceMark rm; stefank@6992: MarkingCodeBlobClosure each_active_code_blob(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations); stefank@4298: CLDToOopClosure mark_and_push_from_cld(&mark_and_push_closure); stefank@4298: Threads::oops_do(&mark_and_push_closure, &mark_and_push_from_cld, &each_active_code_blob); duke@435: } duke@435: break; duke@435: duke@435: case object_synchronizer: duke@435: ObjectSynchronizer::oops_do(&mark_and_push_closure); duke@435: break; duke@435: duke@435: case flat_profiler: duke@435: FlatProfiler::oops_do(&mark_and_push_closure); duke@435: break; duke@435: duke@435: case management: duke@435: Management::oops_do(&mark_and_push_closure); duke@435: break; duke@435: duke@435: case jvmti: duke@435: JvmtiExport::oops_do(&mark_and_push_closure); duke@435: break; duke@435: duke@435: case system_dictionary: duke@435: SystemDictionary::always_strong_oops_do(&mark_and_push_closure); stefank@5194: break; stefank@5194: stefank@5194: case class_loader_data: coleenp@4037: ClassLoaderDataGraph::always_strong_oops_do(&mark_and_push_closure, &follow_klass_closure, true); duke@435: break; duke@435: jrose@1424: case code_cache: jrose@1424: // Do not treat nmethods as strong roots for mark/sweep, since we can unload them. jrose@1424: //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(&mark_and_push_closure)); jrose@1424: break; jrose@1424: duke@435: default: duke@435: fatal("Unknown root type"); duke@435: } duke@435: duke@435: // Do the real work jcoomes@1746: cm->follow_marking_stacks(); duke@435: } duke@435: duke@435: duke@435: // duke@435: // RefProcTaskProxy duke@435: // duke@435: duke@435: void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which) duke@435: { duke@435: assert(Universe::heap()->is_gc_active(), "called outside gc"); duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("RefProcTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); duke@435: PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm); duke@435: PSParallelCompact::FollowStackClosure follow_stack_closure(cm); duke@435: _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(), duke@435: mark_and_push_closure, follow_stack_closure); duke@435: } duke@435: duke@435: // duke@435: // RefProcTaskExecutor duke@435: // duke@435: duke@435: void RefProcTaskExecutor::execute(ProcessTask& task) duke@435: { duke@435: ParallelScavengeHeap* heap = PSParallelCompact::gc_heap(); duke@435: uint parallel_gc_threads = heap->gc_task_manager()->workers(); jmasa@3294: uint active_gc_threads = heap->gc_task_manager()->active_workers(); jcoomes@810: RegionTaskQueueSet* qset = ParCompactionManager::region_array(); jmasa@3294: ParallelTaskTerminator terminator(active_gc_threads, qset); duke@435: GCTaskQueue* q = GCTaskQueue::create(); duke@435: for(uint i=0; ienqueue(new RefProcTaskProxy(task, i)); duke@435: } duke@435: if (task.marks_oops_alive()) { duke@435: if (parallel_gc_threads>1) { jmasa@3294: for (uint j=0; jenqueue(new StealMarkingTask(&terminator)); duke@435: } duke@435: } duke@435: } duke@435: PSParallelCompact::gc_task_manager()->execute_and_wait(q); duke@435: } duke@435: duke@435: void RefProcTaskExecutor::execute(EnqueueTask& task) duke@435: { duke@435: ParallelScavengeHeap* heap = PSParallelCompact::gc_heap(); duke@435: uint parallel_gc_threads = heap->gc_task_manager()->workers(); duke@435: GCTaskQueue* q = GCTaskQueue::create(); duke@435: for(uint i=0; ienqueue(new RefEnqueueTaskProxy(task, i)); duke@435: } duke@435: PSParallelCompact::gc_task_manager()->execute_and_wait(q); duke@435: } duke@435: duke@435: // duke@435: // StealMarkingTask duke@435: // duke@435: duke@435: StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) : duke@435: _terminator(t) {} duke@435: duke@435: void StealMarkingTask::do_it(GCTaskManager* manager, uint which) { duke@435: assert(Universe::heap()->is_gc_active(), "called outside gc"); duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("StealMarkingTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); duke@435: PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm); duke@435: duke@435: oop obj = NULL; jcoomes@1746: ObjArrayTask task; duke@435: int random_seed = 17; jcoomes@1746: do { jcoomes@1746: while (ParCompactionManager::steal_objarray(which, &random_seed, task)) { minqi@5097: ObjArrayKlass* k = (ObjArrayKlass*)task.obj()->klass(); jcoomes@1746: k->oop_follow_contents(cm, task.obj(), task.index()); jcoomes@1746: cm->follow_marking_stacks(); jcoomes@1746: } jcoomes@1746: while (ParCompactionManager::steal(which, &random_seed, obj)) { duke@435: obj->follow_contents(cm); jcoomes@1746: cm->follow_marking_stacks(); duke@435: } jcoomes@1746: } while (!terminator()->offer_termination()); duke@435: } duke@435: duke@435: // jcoomes@810: // StealRegionCompactionTask duke@435: // duke@435: jcoomes@810: StealRegionCompactionTask::StealRegionCompactionTask(ParallelTaskTerminator* t): jcoomes@810: _terminator(t) {} duke@435: jcoomes@810: void StealRegionCompactionTask::do_it(GCTaskManager* manager, uint which) { duke@435: assert(Universe::heap()->is_gc_active(), "called outside gc"); duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("StealRegionCompactionTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); duke@435: jmasa@3294: jmasa@3294: // If not all threads are active, get a draining stack jmasa@3294: // from the list. Else, just use this threads draining stack. jmasa@3294: uint which_stack_index; jmasa@3294: bool use_all_workers = manager->all_workers_active(); jmasa@3294: if (use_all_workers) { jmasa@3294: which_stack_index = which; jmasa@3294: assert(manager->active_workers() == ParallelGCThreads, jmasa@3294: err_msg("all_workers_active has been incorrectly set: " jmasa@3294: " active %d ParallelGCThreads %d", manager->active_workers(), jmasa@3294: ParallelGCThreads)); jmasa@3294: } else { jmasa@3294: which_stack_index = ParCompactionManager::pop_recycled_stack_index(); jmasa@3294: } jmasa@3294: jmasa@3294: cm->set_region_stack_index(which_stack_index); jmasa@3294: cm->set_region_stack(ParCompactionManager::region_list(which_stack_index)); jmasa@3294: if (TraceDynamicGCThreads) { jmasa@3294: gclog_or_tty->print_cr("StealRegionCompactionTask::do_it " jmasa@3294: "region_stack_index %d region_stack = 0x%x " jmasa@3294: " empty (%d) use all workers %d", jmasa@3294: which_stack_index, ParCompactionManager::region_list(which_stack_index), jmasa@3294: cm->region_stack()->is_empty(), jmasa@3294: use_all_workers); jmasa@3294: } jmasa@3294: jcoomes@810: // Has to drain stacks first because there may be regions on duke@435: // preloaded onto the stack and this thread may never have duke@435: // done a draining task. Are the draining tasks needed? duke@435: jcoomes@810: cm->drain_region_stacks(); duke@435: jcoomes@810: size_t region_index = 0; duke@435: int random_seed = 17; duke@435: duke@435: // If we're the termination task, try 10 rounds of stealing before duke@435: // setting the termination flag duke@435: duke@435: while(true) { jcoomes@810: if (ParCompactionManager::steal(which, &random_seed, region_index)) { jcoomes@810: PSParallelCompact::fill_and_update_region(cm, region_index); jcoomes@810: cm->drain_region_stacks(); duke@435: } else { duke@435: if (terminator()->offer_termination()) { duke@435: break; duke@435: } duke@435: // Go around again. duke@435: } duke@435: } duke@435: return; duke@435: } duke@435: duke@435: UpdateDensePrefixTask::UpdateDensePrefixTask( duke@435: PSParallelCompact::SpaceId space_id, jcoomes@810: size_t region_index_start, jcoomes@810: size_t region_index_end) : jcoomes@810: _space_id(space_id), _region_index_start(region_index_start), jcoomes@810: _region_index_end(region_index_end) {} duke@435: duke@435: void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) { duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("UpdateDensePrefixTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); duke@435: duke@435: PSParallelCompact::update_and_deadwood_in_dense_prefix(cm, duke@435: _space_id, jcoomes@810: _region_index_start, jcoomes@810: _region_index_end); duke@435: } duke@435: duke@435: void DrainStacksCompactionTask::do_it(GCTaskManager* manager, uint which) { duke@435: assert(Universe::heap()->is_gc_active(), "called outside gc"); duke@435: sla@5237: NOT_PRODUCT(GCTraceTime tm("DrainStacksCompactionTask", brutisso@6904: PrintGCDetails && TraceParallelOldGCTasks, true, NULL, PSParallelCompact::gc_tracer()->gc_id())); duke@435: duke@435: ParCompactionManager* cm = duke@435: ParCompactionManager::gc_thread_compaction_manager(which); duke@435: jmasa@3294: uint which_stack_index; jmasa@3294: bool use_all_workers = manager->all_workers_active(); jmasa@3294: if (use_all_workers) { jmasa@3294: which_stack_index = which; jmasa@3294: assert(manager->active_workers() == ParallelGCThreads, jmasa@3294: err_msg("all_workers_active has been incorrectly set: " jmasa@3294: " active %d ParallelGCThreads %d", manager->active_workers(), jmasa@3294: ParallelGCThreads)); jmasa@3294: } else { jmasa@3294: which_stack_index = stack_index(); jmasa@3294: } jmasa@3294: jmasa@3294: cm->set_region_stack(ParCompactionManager::region_list(which_stack_index)); jmasa@3294: if (TraceDynamicGCThreads) { jmasa@3294: gclog_or_tty->print_cr("DrainStacksCompactionTask::do_it which = %d " jmasa@3294: "which_stack_index = %d/empty(%d) " jmasa@3294: "use all workers %d", jmasa@3294: which, which_stack_index, jmasa@3294: cm->region_stack()->is_empty(), jmasa@3294: use_all_workers); jmasa@3294: } jmasa@3294: jmasa@3294: cm->set_region_stack_index(which_stack_index); jmasa@3294: jcoomes@810: // Process any regions already in the compaction managers stacks. jcoomes@810: cm->drain_region_stacks(); jmasa@3294: jmasa@3294: assert(cm->region_stack()->is_empty(), "Not empty"); jmasa@3294: jmasa@3294: if (!use_all_workers) { jmasa@3294: // Always give up the region stack. jmasa@3294: assert(cm->region_stack() == jmasa@3294: ParCompactionManager::region_list(cm->region_stack_index()), jmasa@3294: "region_stack and region_stack_index are inconsistent"); jmasa@3294: ParCompactionManager::push_recycled_stack_index(cm->region_stack_index()); jmasa@3294: jmasa@3294: if (TraceDynamicGCThreads) { jmasa@3294: void* old_region_stack = (void*) cm->region_stack(); jmasa@3294: int old_region_stack_index = cm->region_stack_index(); jmasa@3294: gclog_or_tty->print_cr("Pushing region stack 0x%x/%d", jmasa@3294: old_region_stack, old_region_stack_index); jmasa@3294: } jmasa@3294: jmasa@3294: cm->set_region_stack(NULL); jmasa@3294: cm->set_region_stack_index((uint)max_uintx); jmasa@3294: } duke@435: }