1 /*
   2  * Copyright (c) 2005, 2015, 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.hpp"
  35 #include "memory/universe.hpp"
  36 #include "oops/objArrayKlass.inline.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "prims/jvmtiExport.hpp"
  39 #include "runtime/fprofiler.hpp"
  40 #include "runtime/jniHandles.hpp"
  41 #include "runtime/thread.hpp"
  42 #include "runtime/vmThread.hpp"
  43 #include "services/management.hpp"
  44 #include "utilities/stack.inline.hpp"
  45 
  46 //
  47 // ThreadRootsMarkingTask
  48 //
  49 
  50 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) {
  51   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
  52 
  53   ResourceMark rm;
  54 
  55   ParCompactionManager* cm =
  56     ParCompactionManager::gc_thread_compaction_manager(which);
  57 
  58   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
  59   CLDToOopClosure mark_and_push_from_clds(&mark_and_push_closure, true);
  60   MarkingCodeBlobClosure mark_and_push_in_blobs(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
  61 
  62   if (_java_thread != NULL)
  63     _java_thread->oops_do(
  64         &mark_and_push_closure,
  65         &mark_and_push_from_clds,
  66         &mark_and_push_in_blobs);
  67 
  68   if (_vm_thread != NULL)
  69     _vm_thread->oops_do(
  70         &mark_and_push_closure,
  71         &mark_and_push_from_clds,
  72         &mark_and_push_in_blobs);
  73 
  74   // Do the real work
  75   cm->follow_marking_stacks();
  76 }
  77 
  78 
  79 void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) {
  80   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
  81 
  82   ParCompactionManager* cm =
  83     ParCompactionManager::gc_thread_compaction_manager(which);
  84   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
  85   ParCompactionManager::FollowKlassClosure follow_klass_closure(&mark_and_push_closure);
  86 
  87   switch (_root_type) {
  88     case universe:
  89       Universe::oops_do(&mark_and_push_closure);
  90       break;
  91 
  92     case jni_handles:
  93       JNIHandles::oops_do(&mark_and_push_closure);
  94       break;
  95 
  96     case threads:
  97     {
  98       ResourceMark rm;
  99       MarkingCodeBlobClosure each_active_code_blob(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
 100       CLDToOopClosure mark_and_push_from_cld(&mark_and_push_closure);
 101       Threads::oops_do(&mark_and_push_closure, &mark_and_push_from_cld, &each_active_code_blob);
 102     }
 103     break;
 104 
 105     case object_synchronizer:
 106       ObjectSynchronizer::oops_do(&mark_and_push_closure);
 107       break;
 108 
 109     case flat_profiler:
 110       FlatProfiler::oops_do(&mark_and_push_closure);
 111       break;
 112 
 113     case management:
 114       Management::oops_do(&mark_and_push_closure);
 115       break;
 116 
 117     case jvmti:
 118       JvmtiExport::oops_do(&mark_and_push_closure);
 119       break;
 120 
 121     case system_dictionary:
 122       SystemDictionary::always_strong_oops_do(&mark_and_push_closure);
 123       break;
 124 
 125     case class_loader_data:
 126       ClassLoaderDataGraph::always_strong_oops_do(&mark_and_push_closure, &follow_klass_closure, true);
 127       break;
 128 
 129     case code_cache:
 130       // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
 131       //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(&mark_and_push_closure));
 132       break;
 133 
 134     default:
 135       fatal("Unknown root type");
 136   }
 137 
 138   // Do the real work
 139   cm->follow_marking_stacks();
 140 }
 141 
 142 
 143 //
 144 // RefProcTaskProxy
 145 //
 146 
 147 void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which)
 148 {
 149   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 150 
 151   ParCompactionManager* cm =
 152     ParCompactionManager::gc_thread_compaction_manager(which);
 153   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
 154   ParCompactionManager::FollowStackClosure follow_stack_closure(cm);
 155   _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(),
 156                 mark_and_push_closure, follow_stack_closure);
 157 }
 158 
 159 //
 160 // RefProcTaskExecutor
 161 //
 162 
 163 void RefProcTaskExecutor::execute(ProcessTask& task)
 164 {
 165   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 166   uint parallel_gc_threads = heap->gc_task_manager()->workers();
 167   uint active_gc_threads = heap->gc_task_manager()->active_workers();
 168   RegionTaskQueueSet* qset = ParCompactionManager::region_array();
 169   ParallelTaskTerminator terminator(active_gc_threads, qset);
 170   GCTaskQueue* q = GCTaskQueue::create();
 171   for(uint i=0; i<parallel_gc_threads; i++) {
 172     q->enqueue(new RefProcTaskProxy(task, i));
 173   }
 174   if (task.marks_oops_alive()) {
 175     if (parallel_gc_threads>1) {
 176       for (uint j=0; j<active_gc_threads; j++) {
 177         q->enqueue(new StealMarkingTask(&terminator));
 178       }
 179     }
 180   }
 181   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
 182 }
 183 
 184 void RefProcTaskExecutor::execute(EnqueueTask& task)
 185 {
 186   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
 187   uint parallel_gc_threads = heap->gc_task_manager()->workers();
 188   GCTaskQueue* q = GCTaskQueue::create();
 189   for(uint i=0; i<parallel_gc_threads; i++) {
 190     q->enqueue(new RefEnqueueTaskProxy(task, i));
 191   }
 192   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
 193 }
 194 
 195 //
 196 // StealMarkingTask
 197 //
 198 
 199 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) :
 200   _terminator(t) {}
 201 
 202 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) {
 203   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 204 
 205   ParCompactionManager* cm =
 206     ParCompactionManager::gc_thread_compaction_manager(which);
 207   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
 208 
 209   oop obj = NULL;
 210   ObjArrayTask task;
 211   int random_seed = 17;
 212   do {
 213     while (ParCompactionManager::steal_objarray(which, &random_seed, task)) {
 214       cm->follow_contents((objArrayOop)task.obj(), task.index());
 215       cm->follow_marking_stacks();
 216     }
 217     while (ParCompactionManager::steal(which, &random_seed, obj)) {
 218       cm->follow_contents(obj);
 219       cm->follow_marking_stacks();
 220     }
 221   } while (!terminator()->offer_termination());
 222 }
 223 
 224 //
 225 // StealRegionCompactionTask
 226 //
 227 
 228 StealRegionCompactionTask::StealRegionCompactionTask(ParallelTaskTerminator* t):
 229   _terminator(t) {}
 230 
 231 void StealRegionCompactionTask::do_it(GCTaskManager* manager, uint which) {
 232   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 233 
 234   ParCompactionManager* cm =
 235     ParCompactionManager::gc_thread_compaction_manager(which);
 236 
 237 
 238   // If not all threads are active, get a draining stack
 239   // from the list.  Else, just use this threads draining stack.
 240   uint which_stack_index;
 241   bool use_all_workers = manager->all_workers_active();
 242   if (use_all_workers) {
 243     which_stack_index = which;
 244     assert(manager->active_workers() == ParallelGCThreads,
 245            "all_workers_active has been incorrectly set: "
 246            " active %d  ParallelGCThreads %u", manager->active_workers(),
 247            ParallelGCThreads);
 248   } else {
 249     which_stack_index = ParCompactionManager::pop_recycled_stack_index();
 250   }
 251 
 252   cm->set_region_stack_index(which_stack_index);
 253   cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
 254   if (TraceDynamicGCThreads) {
 255     gclog_or_tty->print_cr("StealRegionCompactionTask::do_it "
 256                            "region_stack_index %d region_stack = " PTR_FORMAT " "
 257                            " empty (%d) use all workers %d",
 258     which_stack_index, p2i(ParCompactionManager::region_list(which_stack_index)),
 259     cm->region_stack()->is_empty(),
 260     use_all_workers);
 261   }
 262 
 263   // Has to drain stacks first because there may be regions on
 264   // preloaded onto the stack and this thread may never have
 265   // done a draining task.  Are the draining tasks needed?
 266 
 267   cm->drain_region_stacks();
 268 
 269   size_t region_index = 0;
 270   int random_seed = 17;
 271 
 272   // If we're the termination task, try 10 rounds of stealing before
 273   // setting the termination flag
 274 
 275   while(true) {
 276     if (ParCompactionManager::steal(which, &random_seed, region_index)) {
 277       PSParallelCompact::fill_and_update_region(cm, region_index);
 278       cm->drain_region_stacks();
 279     } else {
 280       if (terminator()->offer_termination()) {
 281         break;
 282       }
 283       // Go around again.
 284     }
 285   }
 286   return;
 287 }
 288 
 289 UpdateDensePrefixTask::UpdateDensePrefixTask(
 290                                    PSParallelCompact::SpaceId space_id,
 291                                    size_t region_index_start,
 292                                    size_t region_index_end) :
 293   _space_id(space_id), _region_index_start(region_index_start),
 294   _region_index_end(region_index_end) {}
 295 
 296 void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) {
 297 
 298   ParCompactionManager* cm =
 299     ParCompactionManager::gc_thread_compaction_manager(which);
 300 
 301   PSParallelCompact::update_and_deadwood_in_dense_prefix(cm,
 302                                                          _space_id,
 303                                                          _region_index_start,
 304                                                          _region_index_end);
 305 }
 306 
 307 void DrainStacksCompactionTask::do_it(GCTaskManager* manager, uint which) {
 308   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 309 
 310   ParCompactionManager* cm =
 311     ParCompactionManager::gc_thread_compaction_manager(which);
 312 
 313   uint which_stack_index;
 314   bool use_all_workers = manager->all_workers_active();
 315   if (use_all_workers) {
 316     which_stack_index = which;
 317     assert(manager->active_workers() == ParallelGCThreads,
 318            "all_workers_active has been incorrectly set: "
 319            " active %d  ParallelGCThreads %u", manager->active_workers(),
 320            ParallelGCThreads);
 321   } else {
 322     which_stack_index = stack_index();
 323   }
 324 
 325   cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
 326   if (TraceDynamicGCThreads) {
 327     gclog_or_tty->print_cr("DrainStacksCompactionTask::do_it which = %d "
 328                            "which_stack_index = %d/empty(%d) "
 329                            "use all workers %d",
 330                            which, which_stack_index,
 331                            cm->region_stack()->is_empty(),
 332                            use_all_workers);
 333   }
 334 
 335   cm->set_region_stack_index(which_stack_index);
 336 
 337   // Process any regions already in the compaction managers stacks.
 338   cm->drain_region_stacks();
 339 
 340   assert(cm->region_stack()->is_empty(), "Not empty");
 341 
 342   if (!use_all_workers) {
 343     // Always give up the region stack.
 344     assert(cm->region_stack() ==
 345            ParCompactionManager::region_list(cm->region_stack_index()),
 346            "region_stack and region_stack_index are inconsistent");
 347     ParCompactionManager::push_recycled_stack_index(cm->region_stack_index());
 348 
 349     if (TraceDynamicGCThreads) {
 350       void* old_region_stack = (void*) cm->region_stack();
 351       int old_region_stack_index = cm->region_stack_index();
 352       gclog_or_tty->print_cr("Pushing region stack " PTR_FORMAT "/%d",
 353         p2i(old_region_stack), old_region_stack_index);
 354     }
 355 
 356     cm->set_region_stack(NULL);
 357     cm->set_region_stack_index((uint)max_uintx);
 358   }
 359 }