1 /*
   2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "gc/parallel/parallelScavengeHeap.hpp"
  29 #include "gc/parallel/pcTasks.hpp"
  30 #include "gc/parallel/psCompactionManager.inline.hpp"
  31 #include "gc/parallel/psParallelCompact.hpp"
  32 #include "gc/shared/collectedHeap.hpp"
  33 #include "gc/shared/gcTimer.hpp"
  34 #include "gc/shared/gcTraceTime.inline.hpp"
  35 #include "logging/log.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "memory/universe.hpp"
  38 #include "oops/objArrayKlass.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "runtime/fprofiler.hpp"
  42 #include "runtime/jniHandles.hpp"
  43 #include "runtime/thread.hpp"
  44 #include "runtime/vmThread.hpp"
  45 #include "services/management.hpp"
  46 #include "utilities/stack.inline.hpp"
  47 
  48 //
  49 // ThreadRootsMarkingTask
  50 //
  51 
  52 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) {
  53   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
  54 
  55   ResourceMark rm;
  56 
  57   ParCompactionManager* cm =
  58     ParCompactionManager::gc_thread_compaction_manager(which);
  59 
  60   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
  61   CLDToOopClosure mark_and_push_from_clds(&mark_and_push_closure, true);
  62   MarkingCodeBlobClosure mark_and_push_in_blobs(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
  63 
  64   if (_java_thread != NULL)
  65     _java_thread->oops_do(
  66         &mark_and_push_closure,
  67         &mark_and_push_from_clds,
  68         &mark_and_push_in_blobs);
  69 
  70   if (_vm_thread != NULL)
  71     _vm_thread->oops_do(
  72         &mark_and_push_closure,
  73         &mark_and_push_from_clds,
  74         &mark_and_push_in_blobs);
  75 
  76   // Do the real work
  77   cm->follow_marking_stacks();
  78 }
  79 
  80 
  81 void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) {
  82   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
  83 
  84   ParCompactionManager* cm =
  85     ParCompactionManager::gc_thread_compaction_manager(which);
  86   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
  87   ParCompactionManager::FollowKlassClosure follow_klass_closure(&mark_and_push_closure);
  88 
  89   switch (_root_type) {
  90     case universe:
  91       Universe::oops_do(&mark_and_push_closure);
  92       break;
  93 
  94     case jni_handles:
  95       JNIHandles::oops_do(&mark_and_push_closure);
  96       break;
  97 
  98     case threads:
  99     {
 100       ResourceMark rm;
 101       MarkingCodeBlobClosure each_active_code_blob(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
 102       CLDToOopClosure mark_and_push_from_cld(&mark_and_push_closure);
 103       Threads::oops_do(&mark_and_push_closure, &mark_and_push_from_cld, &each_active_code_blob);
 104     }
 105     break;
 106 
 107     case object_synchronizer:
 108       ObjectSynchronizer::oops_do(&mark_and_push_closure);
 109       break;
 110 
 111     case flat_profiler:
 112       FlatProfiler::oops_do(&mark_and_push_closure);
 113       break;
 114 
 115     case management:
 116       Management::oops_do(&mark_and_push_closure);
 117       break;
 118 
 119     case jvmti:
 120       JvmtiExport::oops_do(&mark_and_push_closure);
 121       break;
 122 
 123     case system_dictionary:
 124       SystemDictionary::always_strong_oops_do(&mark_and_push_closure);
 125       break;
 126 
 127     case class_loader_data:
 128       ClassLoaderDataGraph::always_strong_oops_do(&mark_and_push_closure, &follow_klass_closure, true);
 129       break;
 130 
 131     case code_cache:
 132       // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
 133       //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(&mark_and_push_closure));
 134       break;
 135 
 136     default:
 137       fatal("Unknown root type");
 138   }
 139 
 140   // Do the real work
 141   cm->follow_marking_stacks();
 142 }
 143 
 144 
 145 //
 146 // RefProcTaskProxy
 147 //
 148 
 149 void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which)
 150 {
 151   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 152 
 153   ParCompactionManager* cm =
 154     ParCompactionManager::gc_thread_compaction_manager(which);
 155   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
 156   ParCompactionManager::FollowStackClosure follow_stack_closure(cm);
 157   _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(),
 158                 mark_and_push_closure, follow_stack_closure);
 159 }
 160 
 161 //
 162 // RefProcTaskExecutor
 163 //
 164 
 165 void RefProcTaskExecutor::execute(ProcessTask& task)
 166 {
 167   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 168   uint parallel_gc_threads = heap->gc_task_manager()->workers();
 169   uint active_gc_threads = heap->gc_task_manager()->active_workers();
 170   RegionTaskQueueSet* qset = ParCompactionManager::region_array();
 171   ParallelTaskTerminator terminator(active_gc_threads, qset);
 172   GCTaskQueue* q = GCTaskQueue::create();
 173   for(uint i=0; i<parallel_gc_threads; i++) {
 174     q->enqueue(new RefProcTaskProxy(task, i));
 175   }
 176   if (task.marks_oops_alive()) {
 177     if (parallel_gc_threads>1) {
 178       for (uint j=0; j<active_gc_threads; j++) {
 179         q->enqueue(new StealMarkingTask(&terminator));
 180       }
 181     }
 182   }
 183   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
 184 }
 185 
 186 void RefProcTaskExecutor::execute(EnqueueTask& task)
 187 {
 188   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 189   uint parallel_gc_threads = heap->gc_task_manager()->workers();
 190   GCTaskQueue* q = GCTaskQueue::create();
 191   for(uint i=0; i<parallel_gc_threads; i++) {
 192     q->enqueue(new RefEnqueueTaskProxy(task, i));
 193   }
 194   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
 195 }
 196 
 197 //
 198 // StealMarkingTask
 199 //
 200 
 201 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) :
 202   _terminator(t) {}
 203 
 204 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) {
 205   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 206 
 207   ParCompactionManager* cm =
 208     ParCompactionManager::gc_thread_compaction_manager(which);
 209   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
 210 
 211   oop obj = NULL;
 212   ObjArrayTask task;
 213   int random_seed = 17;
 214   do {
 215     while (ParCompactionManager::steal_objarray(which, &random_seed, task)) {
 216       cm->follow_contents((objArrayOop)task.obj(), task.index());
 217       cm->follow_marking_stacks();
 218     }
 219     while (ParCompactionManager::steal(which, &random_seed, obj)) {
 220       cm->follow_contents(obj);
 221       cm->follow_marking_stacks();
 222     }
 223   } while (!terminator()->offer_termination());
 224 }
 225 
 226 //
 227 // StealRegionCompactionTask
 228 //
 229 
 230 StealRegionCompactionTask::StealRegionCompactionTask(ParallelTaskTerminator* t):
 231   _terminator(t) {}
 232 
 233 void StealRegionCompactionTask::do_it(GCTaskManager* manager, uint which) {
 234   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 235 
 236   ParCompactionManager* cm =
 237     ParCompactionManager::gc_thread_compaction_manager(which);
 238 
 239 
 240   // If not all threads are active, get a draining stack
 241   // from the list.  Else, just use this threads draining stack.
 242   uint which_stack_index;
 243   bool use_all_workers = manager->all_workers_active();
 244   if (use_all_workers) {
 245     which_stack_index = which;
 246     assert(manager->active_workers() == ParallelGCThreads,
 247            "all_workers_active has been incorrectly set: "
 248            " active %d  ParallelGCThreads %u", manager->active_workers(),
 249            ParallelGCThreads);
 250   } else {
 251     which_stack_index = ParCompactionManager::pop_recycled_stack_index();
 252   }
 253 
 254   cm->set_region_stack_index(which_stack_index);
 255   cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
 256 
 257   // Has to drain stacks first because there may be regions on
 258   // preloaded onto the stack and this thread may never have
 259   // done a draining task.  Are the draining tasks needed?
 260 
 261   cm->drain_region_stacks();
 262 
 263   size_t region_index = 0;
 264   int random_seed = 17;
 265 
 266   // If we're the termination task, try 10 rounds of stealing before
 267   // setting the termination flag
 268 
 269   while(true) {
 270     if (ParCompactionManager::steal(which, &random_seed, region_index)) {
 271       PSParallelCompact::fill_and_update_region(cm, region_index);
 272       cm->drain_region_stacks();
 273     } else {
 274       if (terminator()->offer_termination()) {
 275         break;
 276       }
 277       // Go around again.
 278     }
 279   }
 280   return;
 281 }
 282 
 283 UpdateDensePrefixTask::UpdateDensePrefixTask(
 284                                    PSParallelCompact::SpaceId space_id,
 285                                    size_t region_index_start,
 286                                    size_t region_index_end) :
 287   _space_id(space_id), _region_index_start(region_index_start),
 288   _region_index_end(region_index_end) {}
 289 
 290 void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) {
 291 
 292   ParCompactionManager* cm =
 293     ParCompactionManager::gc_thread_compaction_manager(which);
 294 
 295   PSParallelCompact::update_and_deadwood_in_dense_prefix(cm,
 296                                                          _space_id,
 297                                                          _region_index_start,
 298                                                          _region_index_end);
 299 }
 300 
 301 void DrainStacksCompactionTask::do_it(GCTaskManager* manager, uint which) {
 302   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 303 
 304   ParCompactionManager* cm =
 305     ParCompactionManager::gc_thread_compaction_manager(which);
 306 
 307   uint which_stack_index;
 308   bool use_all_workers = manager->all_workers_active();
 309   if (use_all_workers) {
 310     which_stack_index = which;
 311     assert(manager->active_workers() == ParallelGCThreads,
 312            "all_workers_active has been incorrectly set: "
 313            " active %d  ParallelGCThreads %u", manager->active_workers(),
 314            ParallelGCThreads);
 315   } else {
 316     which_stack_index = stack_index();
 317   }
 318 
 319   cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
 320 
 321   cm->set_region_stack_index(which_stack_index);
 322 
 323   // Process any regions already in the compaction managers stacks.
 324   cm->drain_region_stacks();
 325 
 326   assert(cm->region_stack()->is_empty(), "Not empty");
 327 
 328   if (!use_all_workers) {
 329     // Always give up the region stack.
 330     assert(cm->region_stack() ==
 331            ParCompactionManager::region_list(cm->region_stack_index()),
 332            "region_stack and region_stack_index are inconsistent");
 333     ParCompactionManager::push_recycled_stack_index(cm->region_stack_index());
 334 
 335     cm->set_region_stack(NULL);
 336     cm->set_region_stack_index((uint)max_uintx);
 337   }
 338 }
--- EOF ---