1 /*
   2  * Copyright (c) 2015, 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 
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "gc/g1/bufferingOopClosure.hpp"
  32 #include "gc/g1/g1BarrierSet.hpp"
  33 #include "gc/g1/g1CodeBlobClosure.hpp"
  34 #include "gc/g1/g1CollectedHeap.inline.hpp"
  35 #include "gc/g1/g1CollectorState.hpp"
  36 #include "gc/g1/g1GCPhaseTimes.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/referenceProcessor.hpp"
  42 #include "gc/shared/weakProcessor.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "runtime/mutex.hpp"
  45 #include "services/management.hpp"
  46 #include "utilities/macros.hpp"
  47 
  48 void G1RootProcessor::worker_has_discovered_all_strong_classes() {
  49   assert(ClassUnloadingWithConcurrentMark, "Currently only needed when doing G1 Class Unloading");
  50 
  51   uint new_value = (uint)Atomic::add(1, &_n_workers_discovered_strong_classes);
  52   if (new_value == n_workers()) {
  53     // This thread is last. Notify the others.
  54     MonitorLockerEx ml(&_lock, Mutex::_no_safepoint_check_flag);
  55     _lock.notify_all();
  56   }
  57 }
  58 
  59 void G1RootProcessor::wait_until_all_strong_classes_discovered() {
  60   assert(ClassUnloadingWithConcurrentMark, "Currently only needed when doing G1 Class Unloading");
  61 
  62   if ((uint)_n_workers_discovered_strong_classes != n_workers()) {
  63     MonitorLockerEx ml(&_lock, Mutex::_no_safepoint_check_flag);
  64     while ((uint)_n_workers_discovered_strong_classes != n_workers()) {
  65       _lock.wait(Mutex::_no_safepoint_check_flag, 0, false);
  66     }
  67   }
  68 }
  69 
  70 G1RootProcessor::G1RootProcessor(G1CollectedHeap* g1h, uint n_workers) :
  71     _g1h(g1h),
  72     _process_strong_tasks(G1RP_PS_NumElements),
  73     _srs(n_workers),
  74     _lock(Mutex::leaf, "G1 Root Scanning barrier lock", false, Monitor::_safepoint_check_never),
  75     _n_workers_discovered_strong_classes(0) {}
  76 
  77 void G1RootProcessor::evacuate_roots(G1EvacuationRootClosures* closures, uint worker_i) {
  78   double ext_roots_start = os::elapsedTime();
  79   G1GCPhaseTimes* phase_times = _g1h->g1_policy()->phase_times();
  80 
  81   process_java_roots(closures, phase_times, worker_i);
  82 
  83   // This is the point where this worker thread will not find more strong CLDs/nmethods.
  84   // Report this so G1 can synchronize the strong and weak CLDs/nmethods processing.
  85   if (closures->trace_metadata()) {
  86     worker_has_discovered_all_strong_classes();
  87   }
  88 
  89   process_vm_roots(closures, phase_times, worker_i);
  90   process_string_table_roots(closures, phase_times, worker_i);
  91 
  92   {
  93     // Now the CM ref_processor roots.
  94     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CMRefRoots, worker_i);
  95     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_refProcessor_oops_do)) {
  96       // We need to treat the discovered reference lists of the
  97       // concurrent mark ref processor as roots and keep entries
  98       // (which are added by the marking threads) on them live
  99       // until they can be processed at the end of marking.
 100       _g1h->ref_processor_cm()->weak_oops_do(closures->strong_oops());
 101     }
 102   }
 103 
 104   if (closures->trace_metadata()) {
 105     {
 106       G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::WaitForStrongCLD, worker_i);
 107       // Barrier to make sure all workers passed
 108       // the strong CLD and strong nmethods phases.
 109       wait_until_all_strong_classes_discovered();
 110     }
 111 
 112     // Now take the complement of the strong CLDs.
 113     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::WeakCLDRoots, worker_i);
 114     assert(closures->second_pass_weak_clds() != NULL, "Should be non-null if we are tracing metadata.");
 115     ClassLoaderDataGraph::roots_cld_do(NULL, closures->second_pass_weak_clds());
 116   } else {
 117     phase_times->record_time_secs(G1GCPhaseTimes::WaitForStrongCLD, worker_i, 0.0);
 118     phase_times->record_time_secs(G1GCPhaseTimes::WeakCLDRoots, worker_i, 0.0);
 119     assert(closures->second_pass_weak_clds() == NULL, "Should be null if not tracing metadata.");
 120   }
 121 
 122   // Finish up any enqueued closure apps (attributed as object copy time).
 123   closures->flush();
 124 
 125   double obj_copy_time_sec = closures->closure_app_seconds();
 126 
 127   phase_times->record_time_secs(G1GCPhaseTimes::ObjCopy, worker_i, obj_copy_time_sec);
 128 
 129   double ext_root_time_sec = os::elapsedTime() - ext_roots_start - obj_copy_time_sec;
 130 
 131   phase_times->record_time_secs(G1GCPhaseTimes::ExtRootScan, worker_i, ext_root_time_sec);
 132 
 133   // During conc marking we have to filter the per-thread SATB buffers
 134   // to make sure we remove any oops into the CSet (which will show up
 135   // as implicitly live).
 136   {
 137     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::SATBFiltering, worker_i);
 138     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_filter_satb_buffers) && _g1h->collector_state()->mark_or_rebuild_in_progress()) {
 139       G1BarrierSet::satb_mark_queue_set().filter_thread_buffers();
 140     }
 141   }
 142 
 143   _process_strong_tasks.all_tasks_completed(n_workers());
 144 }
 145 
 146 // Adaptor to pass the closures to the strong roots in the VM.
 147 class StrongRootsClosures : public G1RootClosures {
 148   OopClosure* _roots;
 149   CLDClosure* _clds;
 150   CodeBlobClosure* _blobs;
 151 public:
 152   StrongRootsClosures(OopClosure* roots, CLDClosure* clds, CodeBlobClosure* blobs) :
 153       _roots(roots), _clds(clds), _blobs(blobs) {}
 154 
 155   OopClosure* weak_oops()   { return NULL; }
 156   OopClosure* strong_oops() { return _roots; }
 157 
 158   CLDClosure* weak_clds()        { return NULL; }
 159   CLDClosure* strong_clds()      { return _clds; }
 160 
 161   CodeBlobClosure* strong_codeblobs() { return _blobs; }
 162 };
 163 
 164 void G1RootProcessor::process_strong_roots(OopClosure* oops,
 165                                            CLDClosure* clds,
 166                                            CodeBlobClosure* blobs) {
 167   StrongRootsClosures closures(oops, clds, blobs);
 168 
 169   process_java_roots(&closures, NULL, 0);
 170   process_vm_roots(&closures, NULL, 0);
 171 
 172   _process_strong_tasks.all_tasks_completed(n_workers());
 173 }
 174 
 175 // Adaptor to pass the closures to all the roots in the VM.
 176 class AllRootsClosures : public G1RootClosures {
 177   OopClosure* _roots;
 178   CLDClosure* _clds;
 179 public:
 180   AllRootsClosures(OopClosure* roots, CLDClosure* clds) :
 181       _roots(roots), _clds(clds) {}
 182 
 183   OopClosure* weak_oops() { return _roots; }
 184   OopClosure* strong_oops() { return _roots; }
 185 
 186   // By returning the same CLDClosure for both weak and strong CLDs we ensure
 187   // that a single walk of the CLDG will invoke the closure on all CLDs i the
 188   // system.
 189   CLDClosure* weak_clds() { return _clds; }
 190   CLDClosure* strong_clds() { return _clds; }
 191 
 192   // We don't want to visit code blobs more than once, so we return NULL for the
 193   // strong case and walk the entire code cache as a separate step.
 194   CodeBlobClosure* strong_codeblobs() { return NULL; }
 195 };
 196 
 197 void G1RootProcessor::process_all_roots(OopClosure* oops,
 198                                         CLDClosure* clds,
 199                                         CodeBlobClosure* blobs,
 200                                         bool process_string_table) {
 201   AllRootsClosures closures(oops, clds);
 202 
 203   process_java_roots(&closures, NULL, 0);
 204   process_vm_roots(&closures, NULL, 0);
 205 
 206   if (process_string_table) {
 207     process_string_table_roots(&closures, NULL, 0);
 208   }
 209   process_code_cache_roots(blobs, NULL, 0);
 210 
 211   _process_strong_tasks.all_tasks_completed(n_workers());
 212 }
 213 
 214 void G1RootProcessor::process_all_roots(OopClosure* oops,
 215                                         CLDClosure* clds,
 216                                         CodeBlobClosure* blobs) {
 217   process_all_roots(oops, clds, blobs, true);
 218 }
 219 
 220 void G1RootProcessor::process_all_roots_no_string_table(OopClosure* oops,
 221                                                         CLDClosure* clds,
 222                                                         CodeBlobClosure* blobs) {
 223   assert(!ClassUnloading, "Should only be used when class unloading is disabled");
 224   process_all_roots(oops, clds, blobs, false);
 225 }
 226 
 227 void G1RootProcessor::process_java_roots(G1RootClosures* closures,
 228                                          G1GCPhaseTimes* phase_times,
 229                                          uint worker_i) {
 230   // Iterating over the CLDG and the Threads are done early to allow us to
 231   // first process the strong CLDs and nmethods and then, after a barrier,
 232   // let the thread process the weak CLDs and nmethods.
 233   {
 234     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CLDGRoots, worker_i);
 235     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_ClassLoaderDataGraph_oops_do)) {
 236       ClassLoaderDataGraph::roots_cld_do(closures->strong_clds(), closures->weak_clds());
 237     }
 238   }
 239 
 240   {
 241     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ThreadRoots, worker_i);
 242     bool is_par = n_workers() > 1;
 243     Threads::possibly_parallel_oops_do(is_par,
 244                                        closures->strong_oops(),
 245                                        closures->strong_codeblobs());
 246   }
 247 }
 248 
 249 void G1RootProcessor::process_vm_roots(G1RootClosures* closures,
 250                                        G1GCPhaseTimes* phase_times,
 251                                        uint worker_i) {
 252   OopClosure* strong_roots = closures->strong_oops();
 253   OopClosure* weak_roots = closures->weak_oops();
 254 
 255   {
 256     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::UniverseRoots, worker_i);
 257     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_Universe_oops_do)) {
 258       Universe::oops_do(strong_roots);
 259     }
 260   }
 261 
 262   {
 263     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JNIRoots, worker_i);
 264     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_JNIHandles_oops_do)) {
 265       JNIHandles::oops_do(strong_roots);
 266     }
 267   }
 268 
 269   {
 270     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ObjectSynchronizerRoots, worker_i);
 271     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_ObjectSynchronizer_oops_do)) {
 272       ObjectSynchronizer::oops_do(strong_roots);
 273     }
 274   }
 275 
 276   {
 277     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ManagementRoots, worker_i);
 278     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_Management_oops_do)) {
 279       Management::oops_do(strong_roots);
 280     }
 281   }
 282 
 283   {
 284     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JVMTIRoots, worker_i);
 285     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_jvmti_oops_do)) {
 286       JvmtiExport::oops_do(strong_roots);
 287     }
 288   }
 289 
 290 #if INCLUDE_AOT
 291   if (UseAOT) {
 292     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::AOTCodeRoots, worker_i);
 293     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_aot_oops_do)) {
 294         AOTLoader::oops_do(strong_roots);
 295     }
 296   }
 297 #endif
 298 
 299   {
 300     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::SystemDictionaryRoots, worker_i);
 301     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_SystemDictionary_oops_do)) {
 302       SystemDictionary::roots_oops_do(strong_roots, weak_roots);
 303     }
 304   }
 305 }
 306 
 307 void G1RootProcessor::process_string_table_roots(G1RootClosures* closures,
 308                                                  G1GCPhaseTimes* phase_times,
 309                                                  uint worker_i) {
 310   assert(closures->weak_oops() != NULL, "Should only be called when all roots are processed");
 311   G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::StringTableRoots, worker_i);
 312   // All threads execute the following. A specific chunk of buckets
 313   // from the StringTable are the individual tasks.
 314   StringTable::possibly_parallel_oops_do(closures->weak_oops());
 315 }
 316 
 317 void G1RootProcessor::process_code_cache_roots(CodeBlobClosure* code_closure,
 318                                                G1GCPhaseTimes* phase_times,
 319                                                uint worker_i) {
 320   if (!_process_strong_tasks.is_task_claimed(G1RP_PS_CodeCache_oops_do)) {
 321     CodeCache::blobs_do(code_closure);
 322   }
 323 }
 324 
 325 void G1RootProcessor::process_full_gc_weak_roots(OopClosure* oops) {
 326   if (!_process_strong_tasks.is_task_claimed(G1RP_PS_refProcessor_oops_do)) {
 327     _g1h->ref_processor_stw()->weak_oops_do(oops);
 328   }
 329 
 330   if (!_process_strong_tasks.is_task_claimed(G1RP_PS_weakProcessor_oops_do)) {
 331     WeakProcessor::oops_do(oops);
 332   }
 333 }
 334 
 335 uint G1RootProcessor::n_workers() const {
 336   return _srs.n_threads();
 337 }