1 /*
  2  * Copyright (c) 2005, 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/classLoaderDataGraph.hpp"
 28 #include "classfile/systemDictionary.hpp"
 29 #include "code/codeCache.hpp"
 30 #include "gc/parallel/parallelScavengeHeap.hpp"
 31 #include "gc/parallel/pcTasks.hpp"
 32 #include "gc/parallel/psCompactionManager.inline.hpp"
 33 #include "gc/parallel/psParallelCompact.inline.hpp"
 34 #include "gc/shared/collectedHeap.hpp"
 35 #include "gc/shared/gcTimer.hpp"
 36 #include "gc/shared/gcTraceTime.inline.hpp"
 37 #include "logging/log.hpp"
 38 #include "memory/iterator.inline.hpp"
 39 #include "memory/resourceArea.hpp"
 40 #include "memory/universe.hpp"
 41 #include "oops/objArrayKlass.inline.hpp"
 42 #include "oops/oop.inline.hpp"
 43 #include "prims/jvmtiExport.hpp"
 44 #include "runtime/jniHandles.hpp"
 45 #include "runtime/thread.hpp"
 46 #include "runtime/vmThread.hpp"
 47 #include "services/management.hpp"
 48 #include "utilities/stack.inline.hpp"
 49 
 50 //
 51 // ThreadRootsMarkingTask
 52 //
 53 
 54 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) {
 55   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 56 
 57   ResourceMark rm;
 58 
 59   ParCompactionManager* cm =
 60     ParCompactionManager::gc_thread_compaction_manager(which);
 61 
 62   PCMarkAndPushClosure mark_and_push_closure(cm);
 63   MarkingCodeBlobClosure mark_and_push_in_blobs(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
 64 
 65   _thread->oops_do(&mark_and_push_closure, &mark_and_push_in_blobs);
 66 
 67   // Do the real work
 68   cm->follow_marking_stacks();
 69 }
 70 
 71 
 72 void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) {
 73   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 74 
 75   ParCompactionManager* cm =
 76     ParCompactionManager::gc_thread_compaction_manager(which);
 77   PCMarkAndPushClosure mark_and_push_closure(cm);
 78 
 79   switch (_root_type) {
 80     case universe:
 81       Universe::oops_do(&mark_and_push_closure);
 82       break;
 83 
 84     case jni_handles:
 85       JNIHandles::oops_do(&mark_and_push_closure);
 86       break;
 87 
 88     case threads:
 89     {
 90       ResourceMark rm;
 91       MarkingCodeBlobClosure each_active_code_blob(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
 92       Threads::oops_do(&mark_and_push_closure, &each_active_code_blob);
 93     }
 94     break;
 95 
 96     case object_synchronizer:
 97       ObjectSynchronizer::oops_do(&mark_and_push_closure);
 98       break;
 99 
100     case management:
101       Management::oops_do(&mark_and_push_closure);
102       break;
103 
104     case jvmti:
105       JvmtiExport::oops_do(&mark_and_push_closure);
106       break;
107 
108     case system_dictionary:
109       SystemDictionary::oops_do(&mark_and_push_closure);
110       break;
111 
112     case class_loader_data: {
113         CLDToOopClosure cld_closure(&mark_and_push_closure);
114         ClassLoaderDataGraph::always_strong_cld_do(&cld_closure);
115       }
116       break;
117 
118     case code_cache:
119       // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
120       //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(&mark_and_push_closure));
121       AOTLoader::oops_do(&mark_and_push_closure);
122       break;
123 
124     default:
125       fatal("Unknown root type");
126   }
127 
128   // Do the real work
129   cm->follow_marking_stacks();
130 }
131 
132 
133 //
134 // RefProcTaskProxy
135 //
136 
137 void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which)
138 {
139   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
140 
141   ParCompactionManager* cm =
142     ParCompactionManager::gc_thread_compaction_manager(which);
143   PCMarkAndPushClosure mark_and_push_closure(cm);
144   ParCompactionManager::FollowStackClosure follow_stack_closure(cm);
145   _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(),
146                 mark_and_push_closure, follow_stack_closure);
147 }
148 
149 //
150 // RefProcTaskExecutor
151 //
152 
153 void RefProcTaskExecutor::execute(ProcessTask& task, uint ergo_workers)
154 {
155   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
156   uint active_gc_threads = heap->gc_task_manager()->active_workers();
157   assert(active_gc_threads == ergo_workers,
158          "Ergonomically chosen workers (%u) must be equal to active workers (%u)",
159          ergo_workers, active_gc_threads);
160   OopTaskQueueSet* qset = ParCompactionManager::stack_array();
161   ParallelTaskTerminator terminator(active_gc_threads, qset);
162   GCTaskQueue* q = GCTaskQueue::create();
163   for(uint i=0; i<active_gc_threads; i++) {
164     q->enqueue(new RefProcTaskProxy(task, i));
165   }
166   if (task.marks_oops_alive() && (active_gc_threads>1)) {
167     for (uint j=0; j<active_gc_threads; j++) {
168       q->enqueue(new StealMarkingTask(&terminator));
169     }
170   }
171   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
172 }
173 
174 //
175 // StealMarkingTask
176 //
177 
178 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) :
179   _terminator(t) {}
180 
181 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) {
182   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
183 
184   ParCompactionManager* cm =
185     ParCompactionManager::gc_thread_compaction_manager(which);
186 
187   oop obj = NULL;
188   ObjArrayTask task;
189   do {
190     while (ParCompactionManager::steal_objarray(which,  task)) {
191       cm->follow_array((objArrayOop)task.obj(), task.index());
192       cm->follow_marking_stacks();
193     }
194     while (ParCompactionManager::steal(which, obj)) {
195       cm->follow_contents(obj);
196       cm->follow_marking_stacks();
197     }
198   } while (!terminator()->offer_termination());
199 }
200 
201 //
202 // CompactionWithStealingTask
203 //
204 
205 CompactionWithStealingTask::CompactionWithStealingTask(ParallelTaskTerminator* t):
206   _terminator(t) {}
207 
208 void CompactionWithStealingTask::do_it(GCTaskManager* manager, uint which) {
209   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
210 
211   ParCompactionManager* cm =
212     ParCompactionManager::gc_thread_compaction_manager(which);
213 
214   // Drain the stacks that have been preloaded with regions
215   // that are ready to fill.
216 
217   cm->drain_region_stacks();
218 
219   guarantee(cm->region_stack()->is_empty(), "Not empty");
220 
221   size_t region_index = 0;
222 
223   while(true) {
224     if (ParCompactionManager::steal(which, region_index)) {
225       PSParallelCompact::fill_and_update_region(cm, region_index);
226       cm->drain_region_stacks();
227     } else {
228       if (terminator()->offer_termination()) {
229         break;
230       }
231       // Go around again.
232     }
233   }
234   return;
235 }
236 
237 UpdateDensePrefixTask::UpdateDensePrefixTask(
238                                    PSParallelCompact::SpaceId space_id,
239                                    size_t region_index_start,
240                                    size_t region_index_end) :
241   _space_id(space_id), _region_index_start(region_index_start),
242   _region_index_end(region_index_end) {}
243 
244 void UpdateDensePrefixTask::do_it(GCTaskManager* manager, uint which) {
245 
246   ParCompactionManager* cm =
247     ParCompactionManager::gc_thread_compaction_manager(which);
248 
249   PSParallelCompact::update_and_deadwood_in_dense_prefix(cm,
250                                                          _space_id,
251                                                          _region_index_start,
252                                                          _region_index_end);
253 }