1 /*
   2  * Copyright (c) 2015, 2020, 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/stringTable.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "gc/g1/g1BarrierSet.hpp"
  31 #include "gc/g1/g1CodeBlobClosure.hpp"
  32 #include "gc/g1/g1CollectedHeap.inline.hpp"
  33 #include "gc/g1/g1CollectorState.hpp"
  34 #include "gc/g1/g1GCParPhaseTimesTracker.hpp"
  35 #include "gc/g1/g1GCPhaseTimes.hpp"
  36 #include "gc/g1/g1ParScanThreadState.inline.hpp"
  37 #include "gc/g1/g1Policy.hpp"
  38 #include "gc/g1/g1RootClosures.hpp"
  39 #include "gc/g1/g1RootProcessor.hpp"
  40 #include "gc/g1/heapRegion.inline.hpp"
  41 #include "gc/shared/oopStorage.inline.hpp"
  42 #include "gc/shared/oopStorageSet.hpp"
  43 #include "gc/shared/referenceProcessor.hpp"
  44 #include "memory/allocation.inline.hpp"
  45 #include "memory/universe.hpp"
  46 #include "runtime/mutex.hpp"
  47 #include "services/management.hpp"
  48 #include "utilities/macros.hpp"
  49 
  50 G1RootProcessor::G1RootProcessor(G1CollectedHeap* g1h, uint n_workers) :
  51     _g1h(g1h),
  52     _process_strong_tasks(G1RP_PS_NumElements),
  53     _srs(n_workers) {}
  54 
  55 void G1RootProcessor::evacuate_roots(G1ParScanThreadState* pss, uint worker_id) {
  56   G1GCPhaseTimes* phase_times = _g1h->phase_times();
  57 
  58   G1EvacPhaseTimesTracker timer(phase_times, pss, G1GCPhaseTimes::ExtRootScan, worker_id);
  59 
  60   G1EvacuationRootClosures* closures = pss->closures();
  61   process_java_roots(closures, phase_times, worker_id);
  62 
  63   process_vm_roots(closures, phase_times, worker_id);
  64 
  65   {
  66     // Now the CM ref_processor roots.
  67     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CMRefRoots, worker_id);
  68     if (_process_strong_tasks.try_claim_task(G1RP_PS_refProcessor_oops_do)) {
  69       // We need to treat the discovered reference lists of the
  70       // concurrent mark ref processor as roots and keep entries
  71       // (which are added by the marking threads) on them live
  72       // until they can be processed at the end of marking.
  73       _g1h->ref_processor_cm()->weak_oops_do(closures->strong_oops());
  74     }
  75   }
  76 
  77   _process_strong_tasks.all_tasks_completed(n_workers());
  78 }
  79 
  80 // Adaptor to pass the closures to the strong roots in the VM.
  81 class StrongRootsClosures : public G1RootClosures {
  82   OopClosure* _roots;
  83   CLDClosure* _clds;
  84   CodeBlobClosure* _blobs;
  85 public:
  86   StrongRootsClosures(OopClosure* roots, CLDClosure* clds, CodeBlobClosure* blobs) :
  87       _roots(roots), _clds(clds), _blobs(blobs) {}
  88 
  89   OopClosure* weak_oops()   { return NULL; }
  90   OopClosure* strong_oops() { return _roots; }
  91 
  92   CLDClosure* weak_clds()        { return NULL; }
  93   CLDClosure* strong_clds()      { return _clds; }
  94 
  95   CodeBlobClosure* strong_codeblobs() { return _blobs; }
  96 };
  97 
  98 void G1RootProcessor::process_strong_roots(OopClosure* oops,
  99                                            CLDClosure* clds,
 100                                            CodeBlobClosure* blobs) {
 101   StrongRootsClosures closures(oops, clds, blobs);
 102 
 103   process_java_roots(&closures, NULL, 0);
 104   process_vm_roots(&closures, NULL, 0);
 105 
 106   _process_strong_tasks.all_tasks_completed(n_workers());
 107 }
 108 
 109 // Adaptor to pass the closures to all the roots in the VM.
 110 class AllRootsClosures : public G1RootClosures {
 111   OopClosure* _roots;
 112   CLDClosure* _clds;
 113 public:
 114   AllRootsClosures(OopClosure* roots, CLDClosure* clds) :
 115       _roots(roots), _clds(clds) {}
 116 
 117   OopClosure* weak_oops() { return _roots; }
 118   OopClosure* strong_oops() { return _roots; }
 119 
 120   // By returning the same CLDClosure for both weak and strong CLDs we ensure
 121   // that a single walk of the CLDG will invoke the closure on all CLDs i the
 122   // system.
 123   CLDClosure* weak_clds() { return _clds; }
 124   CLDClosure* strong_clds() { return _clds; }
 125 
 126   // We don't want to visit code blobs more than once, so we return NULL for the
 127   // strong case and walk the entire code cache as a separate step.
 128   CodeBlobClosure* strong_codeblobs() { return NULL; }
 129 };
 130 
 131 void G1RootProcessor::process_all_roots(OopClosure* oops,
 132                                         CLDClosure* clds,
 133                                         CodeBlobClosure* blobs) {
 134   AllRootsClosures closures(oops, clds);
 135 
 136   process_java_roots(&closures, NULL, 0);
 137   process_vm_roots(&closures, NULL, 0);
 138 
 139   process_code_cache_roots(blobs, NULL, 0);
 140 
 141   _process_strong_tasks.all_tasks_completed(n_workers());
 142 }
 143 
 144 void G1RootProcessor::process_java_roots(G1RootClosures* closures,
 145                                          G1GCPhaseTimes* phase_times,
 146                                          uint worker_id) {
 147   // We need to make make sure that the "strong" nmethods are processed first
 148   // using the strong closure. Only after that we process the weakly reachable
 149   // nmethods.
 150   // We need to strictly separate the strong and weak nmethod processing because
 151   // any processing claims that nmethod, i.e. will not be iterated again.
 152   // Which means if an nmethod is processed first and claimed, the strong processing
 153   // will not happen, and the oops reachable by that nmethod will not be marked
 154   // properly.
 155   //
 156   // That is why we process strong nmethods first, synchronize all threads via a
 157   // barrier, and only then allow weak processing. To minimize the wait time at
 158   // that barrier we do the strong nmethod processing first, and immediately after-
 159   // wards indicate that that thread is done. Hopefully other root processing after
 160   // nmethod processing is enough so there is no need to wait.
 161   //
 162   // This is only required in the concurrent start pause with class unloading enabled.
 163   {
 164     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ThreadRoots, worker_id);
 165     bool is_par = n_workers() > 1;
 166     Threads::possibly_parallel_oops_do(is_par,
 167                                        closures->strong_oops(),
 168                                        closures->strong_codeblobs());
 169   }
 170 
 171   {
 172     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CLDGRoots, worker_id);
 173     if (_process_strong_tasks.try_claim_task(G1RP_PS_ClassLoaderDataGraph_oops_do)) {
 174       ClassLoaderDataGraph::roots_cld_do(closures->strong_clds(), closures->weak_clds());
 175     }
 176   }
 177 }
 178 
 179 void G1RootProcessor::process_vm_roots(G1RootClosures* closures,
 180                                        G1GCPhaseTimes* phase_times,
 181                                        uint worker_id) {
 182   OopClosure* strong_roots = closures->strong_oops();
 183 
 184   {
 185     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::UniverseRoots, worker_id);
 186     if (_process_strong_tasks.try_claim_task(G1RP_PS_Universe_oops_do)) {
 187       Universe::oops_do(strong_roots);
 188     }
 189   }
 190 
 191   {
 192     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JNIRoots, worker_id);
 193     if (_process_strong_tasks.try_claim_task(G1RP_PS_JNIHandles_oops_do)) {
 194       JNIHandles::oops_do(strong_roots);
 195     }
 196   }
 197 
 198   {
 199     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ObjectSynchronizerRoots, worker_id);
 200     if (_process_strong_tasks.try_claim_task(G1RP_PS_ObjectSynchronizer_oops_do)) {
 201       ObjectSynchronizer::oops_do(strong_roots);
 202     }
 203   }
 204 
 205   {
 206     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ManagementRoots, worker_id);
 207     if (_process_strong_tasks.try_claim_task(G1RP_PS_Management_oops_do)) {
 208       Management::oops_do(strong_roots);
 209     }
 210   }
 211 
 212   {
 213     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JVMTIRoots, worker_id);
 214     if (_process_strong_tasks.try_claim_task(G1RP_PS_jvmti_oops_do)) {
 215       JvmtiExport::oops_do(strong_roots);
 216     }
 217   }
 218 
 219 #if INCLUDE_AOT
 220   if (UseAOT) {
 221     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::AOTCodeRoots, worker_id);
 222     if (_process_strong_tasks.try_claim_task(G1RP_PS_aot_oops_do)) {
 223         AOTLoader::oops_do(strong_roots);
 224     }
 225   }
 226 #endif
 227 
 228   {
 229     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::VMGlobalRoots, worker_id);
 230     if (_process_strong_tasks.try_claim_task(G1RP_PS_VMGlobal_oops_do)) {
 231       OopStorageSet::vm_global()->oops_do(strong_roots);
 232     }
 233   }
 234 }
 235 
 236 void G1RootProcessor::process_code_cache_roots(CodeBlobClosure* code_closure,
 237                                                G1GCPhaseTimes* phase_times,
 238                                                uint worker_id) {
 239   if (_process_strong_tasks.try_claim_task(G1RP_PS_CodeCache_oops_do)) {
 240     CodeCache::blobs_do(code_closure);
 241   }
 242 }
 243 
 244 uint G1RootProcessor::n_workers() const {
 245   return _srs.n_threads();
 246 }