1 /*
   2  * Copyright (c) 2005, 2013, 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_implementation/parallelScavenge/pcTasks.hpp"
  29 #include "gc_implementation/parallelScavenge/psParallelCompact.hpp"
  30 #include "gc_implementation/shared/gcTimer.hpp"
  31 #include "gc_implementation/shared/gcTraceTime.hpp"
  32 #include "gc_interface/collectedHeap.hpp"
  33 #include "memory/universe.hpp"
  34 #include "oops/objArrayKlass.inline.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "oops/oop.pcgc.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "runtime/fprofiler.hpp"
  39 #include "runtime/jniHandles.hpp"
  40 #include "runtime/thread.hpp"
  41 #include "runtime/vmThread.hpp"
  42 #include "services/management.hpp"
  43 
  44 //
  45 // ThreadRootsMarkingTask
  46 //
  47 
  48 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) {
  49   assert(Universe::heap()->is_gc_active(), "called outside gc");
  50 
  51   ResourceMark rm;
  52 
  53   NOT_PRODUCT(GCTraceTime tm("ThreadRootsMarkingTask",
  54     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
  55   ParCompactionManager* cm =
  56     ParCompactionManager::gc_thread_compaction_manager(which);
  57 
  58   PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
  59   CLDToOopClosure mark_and_push_from_clds(&mark_and_push_closure, true);
  60   CodeBlobToOopClosure mark_and_push_in_blobs(&mark_and_push_closure, /*do_marking=*/ true);
  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(Universe::heap()->is_gc_active(), "called outside gc");
  81 
  82   NOT_PRODUCT(GCTraceTime tm("MarkFromRootsTask",
  83     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
  84   ParCompactionManager* cm =
  85     ParCompactionManager::gc_thread_compaction_manager(which);
  86   PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
  87   PSParallelCompact::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       CodeBlobToOopClosure each_active_code_blob(&mark_and_push_closure, /*do_marking=*/ true);
 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(Universe::heap()->is_gc_active(), "called outside gc");
 152 
 153   NOT_PRODUCT(GCTraceTime tm("RefProcTask",
 154     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
 155   ParCompactionManager* cm =
 156     ParCompactionManager::gc_thread_compaction_manager(which);
 157   PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
 158   PSParallelCompact::FollowStackClosure follow_stack_closure(cm);
 159   _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(),
 160                 mark_and_push_closure, follow_stack_closure);
 161 }
 162 
 163 //
 164 // RefProcTaskExecutor
 165 //
 166 
 167 void RefProcTaskExecutor::execute(ProcessTask& task)
 168 {
 169   ParallelScavengeHeap* heap = PSParallelCompact::gc_heap();
 170   uint parallel_gc_threads = heap->gc_task_manager()->workers();
 171   uint active_gc_threads = heap->gc_task_manager()->active_workers();
 172   RegionTaskQueueSet* qset = ParCompactionManager::region_array();
 173   ParallelTaskTerminator terminator(active_gc_threads, qset);
 174   GCTaskQueue* q = GCTaskQueue::create();
 175   for(uint i=0; i<parallel_gc_threads; i++) {
 176     q->enqueue(new RefProcTaskProxy(task, i));
 177   }
 178   if (task.marks_oops_alive()) {
 179     if (parallel_gc_threads>1) {
 180       for (uint j=0; j<active_gc_threads; j++) {
 181         q->enqueue(new StealMarkingTask(&terminator));
 182       }
 183     }
 184   }
 185   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
 186 }
 187 
 188 void RefProcTaskExecutor::execute(EnqueueTask& task)
 189 {
 190   ParallelScavengeHeap* heap = PSParallelCompact::gc_heap();
 191   uint parallel_gc_threads = heap->gc_task_manager()->workers();
 192   GCTaskQueue* q = GCTaskQueue::create();
 193   for(uint i=0; i<parallel_gc_threads; i++) {
 194     q->enqueue(new RefEnqueueTaskProxy(task, i));
 195   }
 196   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
 197 }
 198 
 199 //
 200 // StealMarkingTask
 201 //
 202 
 203 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) :
 204   _terminator(t) {}
 205 
 206 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) {
 207   assert(Universe::heap()->is_gc_active(), "called outside gc");
 208 
 209   NOT_PRODUCT(GCTraceTime tm("StealMarkingTask",
 210     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
 211 
 212   ParCompactionManager* cm =
 213     ParCompactionManager::gc_thread_compaction_manager(which);
 214   PSParallelCompact::MarkAndPushClosure mark_and_push_closure(cm);
 215 
 216   oop obj = NULL;
 217   ObjArrayTask task;
 218   int random_seed = 17;
 219   do {
 220     while (ParCompactionManager::steal_objarray(which, &random_seed, task)) {
 221       ObjArrayKlass* k = (ObjArrayKlass*)task.obj()->klass();
 222       k->oop_follow_contents(cm, task.obj(), task.index());
 223       cm->follow_marking_stacks();
 224     }
 225     while (ParCompactionManager::steal(which, &random_seed, obj)) {
 226       obj->follow_contents(cm);
 227       cm->follow_marking_stacks();
 228     }
 229   } while (!terminator()->offer_termination());
 230 }
 231 
 232 //
 233 // StealRegionCompactionTask
 234 //
 235 
 236 StealRegionCompactionTask::StealRegionCompactionTask(ParallelTaskTerminator* t):
 237   _terminator(t) {}
 238 
 239 void StealRegionCompactionTask::do_it(GCTaskManager* manager, uint which) {
 240   assert(Universe::heap()->is_gc_active(), "called outside gc");
 241 
 242   NOT_PRODUCT(GCTraceTime tm("StealRegionCompactionTask",
 243     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
 244 
 245   ParCompactionManager* cm =
 246     ParCompactionManager::gc_thread_compaction_manager(which);
 247 
 248 
 249   // If not all threads are active, get a draining stack
 250   // from the list.  Else, just use this threads draining stack.
 251   uint which_stack_index;
 252   bool use_all_workers = manager->all_workers_active();
 253   if (use_all_workers) {
 254     which_stack_index = which;
 255     assert(manager->active_workers() == ParallelGCThreads,
 256            err_msg("all_workers_active has been incorrectly set: "
 257                    " active %d  ParallelGCThreads %d", manager->active_workers(),
 258                    ParallelGCThreads));
 259   } else {
 260     which_stack_index = ParCompactionManager::pop_recycled_stack_index();
 261   }
 262 
 263   cm->set_region_stack_index(which_stack_index);
 264   cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
 265   if (TraceDynamicGCThreads) {
 266     gclog_or_tty->print_cr("StealRegionCompactionTask::do_it "
 267                            "region_stack_index %d region_stack = 0x%x "
 268                            " empty (%d) use all workers %d",
 269     which_stack_index, ParCompactionManager::region_list(which_stack_index),
 270     cm->region_stack()->is_empty(),
 271     use_all_workers);
 272   }
 273 
 274   // Has to drain stacks first because there may be regions on
 275   // preloaded onto the stack and this thread may never have
 276   // done a draining task.  Are the draining tasks needed?
 277 
 278   cm->drain_region_stacks();
 279 
 280   size_t region_index = 0;
 281   int random_seed = 17;
 282 
 283   // If we're the termination task, try 10 rounds of stealing before
 284   // setting the termination flag
 285 
 286   while(true) {
 287     if (ParCompactionManager::steal(which, &random_seed, region_index)) {
 288       PSParallelCompact::fill_and_update_region(cm, region_index);
 289       cm->drain_region_stacks();
 290     } else {
 291       if (terminator()->offer_termination()) {
 292         break;
 293       }
 294       // Go around again.
 295     }
 296   }
 297   return;
 298 }
 299 
 300 UpdateDensePrefixTask::UpdateDensePrefixTask(
 301                                    PSParallelCompact::SpaceId space_id,
 302                                    size_t region_index_start,
 303                                    size_t region_index_end) :
 304   _space_id(space_id), _region_index_start(region_index_start),
 305   _region_index_end(region_index_end) {}
 306 
 307 void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) {
 308 
 309   NOT_PRODUCT(GCTraceTime tm("UpdateDensePrefixTask",
 310     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
 311 
 312   ParCompactionManager* cm =
 313     ParCompactionManager::gc_thread_compaction_manager(which);
 314 
 315   PSParallelCompact::update_and_deadwood_in_dense_prefix(cm,
 316                                                          _space_id,
 317                                                          _region_index_start,
 318                                                          _region_index_end);
 319 }
 320 
 321 void DrainStacksCompactionTask::do_it(GCTaskManager* manager, uint which) {
 322   assert(Universe::heap()->is_gc_active(), "called outside gc");
 323 
 324   NOT_PRODUCT(GCTraceTime tm("DrainStacksCompactionTask",
 325     PrintGCDetails && TraceParallelOldGCTasks, true, NULL));
 326 
 327   ParCompactionManager* cm =
 328     ParCompactionManager::gc_thread_compaction_manager(which);
 329 
 330   uint which_stack_index;
 331   bool use_all_workers = manager->all_workers_active();
 332   if (use_all_workers) {
 333     which_stack_index = which;
 334     assert(manager->active_workers() == ParallelGCThreads,
 335            err_msg("all_workers_active has been incorrectly set: "
 336                    " active %d  ParallelGCThreads %d", manager->active_workers(),
 337                    ParallelGCThreads));
 338   } else {
 339     which_stack_index = stack_index();
 340   }
 341 
 342   cm->set_region_stack(ParCompactionManager::region_list(which_stack_index));
 343   if (TraceDynamicGCThreads) {
 344     gclog_or_tty->print_cr("DrainStacksCompactionTask::do_it which = %d "
 345                            "which_stack_index = %d/empty(%d) "
 346                            "use all workers %d",
 347                            which, which_stack_index,
 348                            cm->region_stack()->is_empty(),
 349                            use_all_workers);
 350   }
 351 
 352   cm->set_region_stack_index(which_stack_index);
 353 
 354   // Process any regions already in the compaction managers stacks.
 355   cm->drain_region_stacks();
 356 
 357   assert(cm->region_stack()->is_empty(), "Not empty");
 358 
 359   if (!use_all_workers) {
 360     // Always give up the region stack.
 361     assert(cm->region_stack() ==
 362            ParCompactionManager::region_list(cm->region_stack_index()),
 363            "region_stack and region_stack_index are inconsistent");
 364     ParCompactionManager::push_recycled_stack_index(cm->region_stack_index());
 365 
 366     if (TraceDynamicGCThreads) {
 367       void* old_region_stack = (void*) cm->region_stack();
 368       int old_region_stack_index = cm->region_stack_index();
 369       gclog_or_tty->print_cr("Pushing region stack 0x%x/%d",
 370         old_region_stack, old_region_stack_index);
 371     }
 372 
 373     cm->set_region_stack(NULL);
 374     cm->set_region_stack_index((uint)max_uintx);
 375   }
 376 }