1 /*
   2  * Copyright (c) 2002, 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/cardTableExtension.hpp"
  29 #include "gc/parallel/gcTaskManager.hpp"
  30 #include "gc/parallel/psMarkSweep.hpp"
  31 #include "gc/parallel/psPromotionManager.hpp"
  32 #include "gc/parallel/psPromotionManager.inline.hpp"
  33 #include "gc/parallel/psScavenge.inline.hpp"
  34 #include "gc/parallel/psTasks.hpp"
  35 #include "gc/shared/taskqueue.inline.hpp"
  36 #include "memory/iterator.hpp"
  37 #include "memory/universe.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "runtime/fprofiler.hpp"
  40 #include "runtime/thread.hpp"
  41 #include "runtime/vmThread.hpp"
  42 #include "services/management.hpp"
  43 
  44 //
  45 // ScavengeRootsTask
  46 //
  47 
  48 void ScavengeRootsTask::do_it(GCTaskManager* manager, uint which) {
  49   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
  50 
  51   PSPromotionManager* pm = PSPromotionManager::gc_thread_promotion_manager(which);
  52   PSScavengeRootsClosure roots_closure(pm);
  53   PSPromoteRootsClosure  roots_to_old_closure(pm);
  54 
  55   switch (_root_type) {
  56     case universe:
  57       Universe::oops_do(&roots_closure);
  58       break;
  59 
  60     case jni_handles:
  61       JNIHandles::oops_do(&roots_closure);
  62       break;
  63 
  64     case threads:
  65     {
  66       ResourceMark rm;
  67       CLDClosure* cld_closure = NULL; // Not needed. All CLDs are already visited.
  68       Threads::oops_do(&roots_closure, cld_closure, NULL);
  69     }
  70     break;
  71 
  72     case object_synchronizer:
  73       ObjectSynchronizer::oops_do(&roots_closure);
  74       break;
  75 
  76     case flat_profiler:
  77       FlatProfiler::oops_do(&roots_closure);
  78       break;
  79 
  80     case system_dictionary:
  81       SystemDictionary::oops_do(&roots_closure);
  82       break;
  83 
  84     case class_loader_data:
  85     {
  86       PSScavengeKlassClosure klass_closure(pm);
  87       ClassLoaderDataGraph::oops_do(&roots_closure, &klass_closure, false);
  88     }
  89     break;
  90 
  91     case management:
  92       Management::oops_do(&roots_closure);
  93       break;
  94 
  95     case jvmti:
  96       JvmtiExport::oops_do(&roots_closure);
  97       break;
  98 
  99 
 100     case code_cache:
 101       {
 102         MarkingCodeBlobClosure each_scavengable_code_blob(&roots_to_old_closure, CodeBlobToOopClosure::FixRelocations);
 103         CodeCache::scavenge_root_nmethods_do(&each_scavengable_code_blob);
 104       }
 105       break;
 106 
 107     default:
 108       fatal("Unknown root type");
 109   }
 110 
 111   // Do the real work
 112   pm->drain_stacks(false);
 113 }
 114 
 115 //
 116 // ThreadRootsTask
 117 //
 118 
 119 void ThreadRootsTask::do_it(GCTaskManager* manager, uint which) {
 120   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 121 
 122   PSPromotionManager* pm = PSPromotionManager::gc_thread_promotion_manager(which);
 123   PSScavengeRootsClosure roots_closure(pm);
 124   CLDClosure* roots_from_clds = NULL;  // Not needed. All CLDs are already visited.
 125   MarkingCodeBlobClosure roots_in_blobs(&roots_closure, CodeBlobToOopClosure::FixRelocations);
 126 
 127   if (_java_thread != NULL)
 128     _java_thread->oops_do(&roots_closure, roots_from_clds, &roots_in_blobs);
 129 
 130   if (_vm_thread != NULL)
 131     _vm_thread->oops_do(&roots_closure, roots_from_clds, &roots_in_blobs);
 132 
 133   // Do the real work
 134   pm->drain_stacks(false);
 135 }
 136 
 137 //
 138 // StealTask
 139 //
 140 
 141 StealTask::StealTask(ParallelTaskTerminator* t) :
 142   _terminator(t) {}
 143 
 144 void StealTask::do_it(GCTaskManager* manager, uint which) {
 145   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 146 
 147   PSPromotionManager* pm =
 148     PSPromotionManager::gc_thread_promotion_manager(which);
 149   pm->drain_stacks(true);
 150   guarantee(pm->stacks_empty(),
 151             "stacks should be empty at this point");
 152 
 153   int random_seed = 17;
 154   while(true) {
 155     StarTask p;
 156     if (PSPromotionManager::steal_depth(which, &random_seed, p)) {
 157       TASKQUEUE_STATS_ONLY(pm->record_steal(p));
 158       pm->process_popped_location_depth(p);
 159       pm->drain_stacks_depth(true);
 160     } else {
 161       if (terminator()->offer_termination()) {
 162         break;
 163       }
 164     }
 165   }
 166   guarantee(pm->stacks_empty(), "stacks should be empty at this point");
 167 }
 168 
 169 //
 170 // OldToYoungRootsTask
 171 //
 172 
 173 void OldToYoungRootsTask::do_it(GCTaskManager* manager, uint which) {
 174   // There are not old-to-young pointers if the old gen is empty.
 175   assert(!_old_gen->object_space()->is_empty(),
 176     "Should not be called is there is no work");
 177   assert(_old_gen != NULL, "Sanity");
 178   assert(_old_gen->object_space()->contains(_gen_top) || _gen_top == _old_gen->object_space()->top(), "Sanity");
 179   assert(_stripe_number < ParallelGCThreads, "Sanity");
 180 
 181   {
 182     PSPromotionManager* pm = PSPromotionManager::gc_thread_promotion_manager(which);
 183     CardTableExtension* card_table =
 184       barrier_set_cast<CardTableExtension>(ParallelScavengeHeap::heap()->barrier_set());
 185 
 186     card_table->scavenge_contents_parallel(_old_gen->start_array(),
 187                                            _old_gen->object_space(),
 188                                            _gen_top,
 189                                            pm,
 190                                            _stripe_number,
 191                                            _stripe_total);
 192 
 193     // Do the real work
 194     pm->drain_stacks(false);
 195   }
 196 }