1 /*
   2  * Copyright (c) 2002, 2018, 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 "aot/aotLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "gc/parallel/gcTaskManager.hpp"
  30 #include "gc/parallel/psCardTable.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/resourceArea.hpp"
  38 #include "memory/universe.hpp"
  39 #include "oops/oop.inline.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       Threads::oops_do(&roots_closure, NULL);
  68     }
  69     break;
  70 
  71     case object_synchronizer:
  72       ObjectSynchronizer::oops_do(&roots_closure);
  73       break;
  74 
  75     case system_dictionary:
  76       SystemDictionary::oops_do(&roots_closure);
  77       break;
  78 
  79     case class_loader_data:
  80     {
  81       PSScavengeCLDClosure cld_closure(pm);
  82       ClassLoaderDataGraph::cld_do(&cld_closure);
  83     }
  84     break;
  85 
  86     case management:
  87       Management::oops_do(&roots_closure);
  88       break;
  89 
  90     case jvmti:
  91       JvmtiExport::oops_do(&roots_closure);
  92       break;
  93 
  94 
  95     case code_cache:
  96       {
  97         MarkingCodeBlobClosure each_scavengable_code_blob(&roots_to_old_closure, CodeBlobToOopClosure::FixRelocations);
  98         CodeCache::scavenge_root_nmethods_do(&each_scavengable_code_blob);
  99         AOTLoader::oops_do(&roots_closure);
 100       }
 101       break;
 102 
 103     default:
 104       fatal("Unknown root type");
 105   }
 106 
 107   // Do the real work
 108   pm->drain_stacks(false);
 109 }
 110 
 111 //
 112 // ThreadRootsTask
 113 //
 114 
 115 void ThreadRootsTask::do_it(GCTaskManager* manager, uint which) {
 116   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 117 
 118   PSPromotionManager* pm = PSPromotionManager::gc_thread_promotion_manager(which);
 119   PSScavengeRootsClosure roots_closure(pm);
 120   MarkingCodeBlobClosure roots_in_blobs(&roots_closure, CodeBlobToOopClosure::FixRelocations);
 121 
 122   if (_java_thread != NULL)
 123     _java_thread->oops_do(&roots_closure, &roots_in_blobs);
 124 
 125   if (_vm_thread != NULL)
 126     _vm_thread->oops_do(&roots_closure, &roots_in_blobs);
 127 
 128   // Do the real work
 129   pm->drain_stacks(false);
 130 }
 131 
 132 //
 133 // StealTask
 134 //
 135 
 136 StealTask::StealTask(ParallelTaskTerminator* t) :
 137   _terminator(t) {}
 138 
 139 void StealTask::do_it(GCTaskManager* manager, uint which) {
 140   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 141 
 142   PSPromotionManager* pm =
 143     PSPromotionManager::gc_thread_promotion_manager(which);
 144   pm->drain_stacks(true);
 145   guarantee(pm->stacks_empty(),
 146             "stacks should be empty at this point");
 147 
 148   int random_seed = 17;
 149   while(true) {
 150     StarTask p;
 151     if (PSPromotionManager::steal_depth(which, &random_seed, p)) {
 152       TASKQUEUE_STATS_ONLY(pm->record_steal(p));
 153       pm->process_popped_location_depth(p);
 154       pm->drain_stacks_depth(true);
 155     } else {
 156       if (terminator()->offer_termination()) {
 157         break;
 158       }
 159     }
 160   }
 161   guarantee(pm->stacks_empty(), "stacks should be empty at this point");
 162 }
 163 
 164 //
 165 // OldToYoungRootsTask
 166 //
 167 
 168 void OldToYoungRootsTask::do_it(GCTaskManager* manager, uint which) {
 169   // There are not old-to-young pointers if the old gen is empty.
 170   assert(!_old_gen->object_space()->is_empty(),
 171     "Should not be called is there is no work");
 172   assert(_old_gen != NULL, "Sanity");
 173   assert(_old_gen->object_space()->contains(_gen_top) || _gen_top == _old_gen->object_space()->top(), "Sanity");
 174   assert(_stripe_number < ParallelGCThreads, "Sanity");
 175 
 176   {
 177     PSPromotionManager* pm = PSPromotionManager::gc_thread_promotion_manager(which);
 178     PSCardTable* card_table = ParallelScavengeHeap::heap()->card_table();
 179 
 180     card_table->scavenge_contents_parallel(_old_gen->start_array(),
 181                                            _old_gen->object_space(),
 182                                            _gen_top,
 183                                            pm,
 184                                            _stripe_number,
 185                                            _stripe_total);
 186 
 187     // Do the real work
 188     pm->drain_stacks(false);
 189   }
 190 }