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