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