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