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