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/resourceArea.hpp"
 39 #include "memory/universe.hpp"
 40 #include "oops/objArrayKlass.inline.hpp"
 41 #include "oops/oop.inline.hpp"
 42 #include "prims/jvmtiExport.hpp"
 43 #include "runtime/jniHandles.hpp"
 44 #include "runtime/thread.hpp"
 45 #include "runtime/vmThread.hpp"
 46 #include "services/management.hpp"
 47 #include "utilities/stack.inline.hpp"
 48 
 49 //
 50 // ThreadRootsMarkingTask
 51 //
 52 
 53 void ThreadRootsMarkingTask::do_it(GCTaskManager* manager, uint which) {
 54   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 55 
 56   ResourceMark rm;
 57 
 58   ParCompactionManager* cm =
 59     ParCompactionManager::gc_thread_compaction_manager(which);
 60 
 61   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
 62   MarkingCodeBlobClosure mark_and_push_in_blobs(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
 63 
 64   _thread->oops_do(&mark_and_push_closure, &mark_and_push_in_blobs);
 65 
 66   // Do the real work
 67   cm->follow_marking_stacks();
 68 }
 69 
 70 
 71 void MarkFromRootsTask::do_it(GCTaskManager* manager, uint which) {
 72   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
 73 
 74   ParCompactionManager* cm =
 75     ParCompactionManager::gc_thread_compaction_manager(which);
 76   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
 77 
 78   switch (_root_type) {
 79     case universe:
 80       Universe::oops_do(&mark_and_push_closure);
 81       break;
 82 
 83     case jni_handles:
 84       JNIHandles::oops_do(&mark_and_push_closure);
 85       break;
 86 
 87     case threads:
 88     {
 89       ResourceMark rm;
 90       MarkingCodeBlobClosure each_active_code_blob(&mark_and_push_closure, !CodeBlobToOopClosure::FixRelocations);
 91       Threads::oops_do(&mark_and_push_closure, &each_active_code_blob);
 92     }
 93     break;
 94 
 95     case object_synchronizer:
 96       ObjectSynchronizer::oops_do(&mark_and_push_closure);
 97       break;
 98 
 99     case management:
100       Management::oops_do(&mark_and_push_closure);
101       break;
102 
103     case jvmti:
104       JvmtiExport::oops_do(&mark_and_push_closure);
105       break;
106 
107     case system_dictionary:
108       SystemDictionary::oops_do(&mark_and_push_closure);
109       break;
110 
111     case class_loader_data: {
112         CLDToOopClosure cld_closure(&mark_and_push_closure);
113         ClassLoaderDataGraph::always_strong_cld_do(&cld_closure);
114       }
115       break;
116 
117     case code_cache:
118       // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
119       //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(&mark_and_push_closure));
120       AOTLoader::oops_do(&mark_and_push_closure);
121       break;
122 
123     default:
124       fatal("Unknown root type");
125   }
126 
127   // Do the real work
128   cm->follow_marking_stacks();
129 }
130 
131 
132 //
133 // RefProcTaskProxy
134 //
135 
136 void RefProcTaskProxy::do_it(GCTaskManager* manager, uint which)
137 {
138   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
139 
140   ParCompactionManager* cm =
141     ParCompactionManager::gc_thread_compaction_manager(which);
142   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
143   ParCompactionManager::FollowStackClosure follow_stack_closure(cm);
144   _rp_task.work(_work_id, *PSParallelCompact::is_alive_closure(),
145                 mark_and_push_closure, follow_stack_closure);
146 }
147 
148 //
149 // RefProcTaskExecutor
150 //
151 
152 void RefProcTaskExecutor::execute(ProcessTask& task, uint ergo_workers)
153 {
154   ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
155   uint active_gc_threads = heap->gc_task_manager()->active_workers();
156   assert(active_gc_threads == ergo_workers,
157          "Ergonomically chosen workers (%u) must be equal to active workers (%u)",
158          ergo_workers, active_gc_threads);
159   OopTaskQueueSet* qset = ParCompactionManager::stack_array();
160   ParallelTaskTerminator terminator(active_gc_threads, qset);
161   GCTaskQueue* q = GCTaskQueue::create();
162   for(uint i=0; i<active_gc_threads; i++) {
163     q->enqueue(new RefProcTaskProxy(task, i));
164   }
165   if (task.marks_oops_alive() && (active_gc_threads>1)) {
166     for (uint j=0; j<active_gc_threads; j++) {
167       q->enqueue(new StealMarkingTask(&terminator));
168     }
169   }
170   PSParallelCompact::gc_task_manager()->execute_and_wait(q);
171 }
172 
173 //
174 // StealMarkingTask
175 //
176 
177 StealMarkingTask::StealMarkingTask(ParallelTaskTerminator* t) :
178   _terminator(t) {}
179 
180 void StealMarkingTask::do_it(GCTaskManager* manager, uint which) {
181   assert(ParallelScavengeHeap::heap()->is_gc_active(), "called outside gc");
182 
183   ParCompactionManager* cm =
184     ParCompactionManager::gc_thread_compaction_manager(which);
185   ParCompactionManager::MarkAndPushClosure mark_and_push_closure(cm);
186 
187   oop obj = NULL;
188   ObjArrayTask task;
189   do {
190     while (ParCompactionManager::steal_objarray(which,  task)) {
191       cm->follow_contents((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 }