src/share/vm/gc_implementation/g1/g1RootProcessor.cpp

Mon, 19 Aug 2019 10:11:31 +0200

author
neugens
date
Mon, 19 Aug 2019 10:11:31 +0200
changeset 9861
a248d0be1309
parent 9358
6a4a6c499e89
permissions
-rw-r--r--

8229401: Fix JFR code cache test failures
8223689: Add JFR Thread Sampling Support
8223690: Add JFR BiasedLock Event Support
8223691: Add JFR G1 Region Type Change Event Support
8223692: Add JFR G1 Heap Summary Event Support
Summary: Backport JFR from JDK11, additional fixes
Reviewed-by: neugens, apetushkov
Contributed-by: denghui.ddh@alibaba-inc.com

     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  */
    25 #include "precompiled.hpp"
    27 #include "classfile/symbolTable.hpp"
    28 #include "classfile/systemDictionary.hpp"
    29 #include "code/codeCache.hpp"
    30 #include "gc_implementation/g1/bufferingOopClosure.hpp"
    31 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
    32 #include "gc_implementation/g1/g1CollectorPolicy.hpp"
    33 #include "gc_implementation/g1/g1GCPhaseTimes.hpp"
    34 #include "gc_implementation/g1/g1RemSet.inline.hpp"
    35 #include "gc_implementation/g1/g1RootProcessor.hpp"
    36 #include "memory/allocation.inline.hpp"
    37 #include "runtime/fprofiler.hpp"
    38 #include "runtime/mutex.hpp"
    39 #include "services/management.hpp"
    41 class G1CodeBlobClosure : public CodeBlobClosure {
    42   class HeapRegionGatheringOopClosure : public OopClosure {
    43     G1CollectedHeap* _g1h;
    44     OopClosure* _work;
    45     nmethod* _nm;
    47     template <typename T>
    48     void do_oop_work(T* p) {
    49       _work->do_oop(p);
    50       T oop_or_narrowoop = oopDesc::load_heap_oop(p);
    51       if (!oopDesc::is_null(oop_or_narrowoop)) {
    52         oop o = oopDesc::decode_heap_oop_not_null(oop_or_narrowoop);
    53         HeapRegion* hr = _g1h->heap_region_containing_raw(o);
    54         assert(!_g1h->obj_in_cs(o) || hr->rem_set()->strong_code_roots_list_contains(_nm), "if o still in CS then evacuation failed and nm must already be in the remset");
    55         hr->add_strong_code_root(_nm);
    56       }
    57     }
    59   public:
    60     HeapRegionGatheringOopClosure(OopClosure* oc) : _g1h(G1CollectedHeap::heap()), _work(oc), _nm(NULL) {}
    62     void do_oop(oop* o) {
    63       do_oop_work(o);
    64     }
    66     void do_oop(narrowOop* o) {
    67       do_oop_work(o);
    68     }
    70     void set_nm(nmethod* nm) {
    71       _nm = nm;
    72     }
    73   };
    75   HeapRegionGatheringOopClosure _oc;
    76 public:
    77   G1CodeBlobClosure(OopClosure* oc) : _oc(oc) {}
    79   void do_code_blob(CodeBlob* cb) {
    80     nmethod* nm = cb->as_nmethod_or_null();
    81     if (nm != NULL) {
    82       if (!nm->test_set_oops_do_mark()) {
    83         _oc.set_nm(nm);
    84         nm->oops_do(&_oc);
    85         nm->fix_oop_relocations();
    86       }
    87     }
    88   }
    89 };
    92 void G1RootProcessor::worker_has_discovered_all_strong_classes() {
    93   uint n_workers = _g1h->n_par_threads();
    94   assert(ClassUnloadingWithConcurrentMark, "Currently only needed when doing G1 Class Unloading");
    96   if (n_workers > 0) {
    97     uint new_value = (uint)Atomic::add(1, &_n_workers_discovered_strong_classes);
    98     if (new_value == n_workers) {
    99       // This thread is last. Notify the others.
   100       MonitorLockerEx ml(&_lock, Mutex::_no_safepoint_check_flag);
   101       _lock.notify_all();
   102     }
   103   }
   104 }
   106 void G1RootProcessor::wait_until_all_strong_classes_discovered() {
   107   uint n_workers = _g1h->n_par_threads();
   108   assert(ClassUnloadingWithConcurrentMark, "Currently only needed when doing G1 Class Unloading");
   110   if (n_workers > 0 && (uint)_n_workers_discovered_strong_classes != n_workers) {
   111     MonitorLockerEx ml(&_lock, Mutex::_no_safepoint_check_flag);
   112     while ((uint)_n_workers_discovered_strong_classes != n_workers) {
   113       _lock.wait(Mutex::_no_safepoint_check_flag, 0, false);
   114     }
   115   }
   116 }
   118 G1RootProcessor::G1RootProcessor(G1CollectedHeap* g1h) :
   119     _g1h(g1h),
   120     _process_strong_tasks(G1RP_PS_NumElements),
   121     _srs(g1h),
   122     _lock(Mutex::leaf, "G1 Root Scanning barrier lock", false),
   123     _n_workers_discovered_strong_classes(0) {}
   125 void G1RootProcessor::evacuate_roots(OopClosure* scan_non_heap_roots,
   126                                      OopClosure* scan_non_heap_weak_roots,
   127                                      CLDClosure* scan_strong_clds,
   128                                      CLDClosure* scan_weak_clds,
   129                                      bool trace_metadata,
   130                                      uint worker_i) {
   131   // First scan the shared roots.
   132   double ext_roots_start = os::elapsedTime();
   133   G1GCPhaseTimes* phase_times = _g1h->g1_policy()->phase_times();
   135   BufferingOopClosure buf_scan_non_heap_roots(scan_non_heap_roots);
   136   BufferingOopClosure buf_scan_non_heap_weak_roots(scan_non_heap_weak_roots);
   138   OopClosure* const weak_roots = &buf_scan_non_heap_weak_roots;
   139   OopClosure* const strong_roots = &buf_scan_non_heap_roots;
   141   // CodeBlobClosures are not interoperable with BufferingOopClosures
   142   G1CodeBlobClosure root_code_blobs(scan_non_heap_roots);
   144   process_java_roots(strong_roots,
   145                      trace_metadata ? scan_strong_clds : NULL,
   146                      scan_strong_clds,
   147                      trace_metadata ? NULL : scan_weak_clds,
   148                      &root_code_blobs,
   149                      phase_times,
   150                      worker_i);
   152   // This is the point where this worker thread will not find more strong CLDs/nmethods.
   153   // Report this so G1 can synchronize the strong and weak CLDs/nmethods processing.
   154   if (trace_metadata) {
   155     worker_has_discovered_all_strong_classes();
   156   }
   158   process_vm_roots(strong_roots, weak_roots, phase_times, worker_i);
   159   process_string_table_roots(weak_roots, phase_times, worker_i);
   160   {
   161     // Now the CM ref_processor roots.
   162     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CMRefRoots, worker_i);
   163     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_refProcessor_oops_do)) {
   164       // We need to treat the discovered reference lists of the
   165       // concurrent mark ref processor as roots and keep entries
   166       // (which are added by the marking threads) on them live
   167       // until they can be processed at the end of marking.
   168       _g1h->ref_processor_cm()->weak_oops_do(&buf_scan_non_heap_roots);
   169     }
   170   }
   172   if (trace_metadata) {
   173     {
   174       G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::WaitForStrongCLD, worker_i);
   175       // Barrier to make sure all workers passed
   176       // the strong CLD and strong nmethods phases.
   177       wait_until_all_strong_classes_discovered();
   178     }
   180     // Now take the complement of the strong CLDs.
   181     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::WeakCLDRoots, worker_i);
   182     ClassLoaderDataGraph::roots_cld_do(NULL, scan_weak_clds);
   183   } else {
   184     phase_times->record_time_secs(G1GCPhaseTimes::WaitForStrongCLD, worker_i, 0.0);
   185     phase_times->record_time_secs(G1GCPhaseTimes::WeakCLDRoots, worker_i, 0.0);
   186   }
   188   // Finish up any enqueued closure apps (attributed as object copy time).
   189   buf_scan_non_heap_roots.done();
   190   buf_scan_non_heap_weak_roots.done();
   192   double obj_copy_time_sec = buf_scan_non_heap_roots.closure_app_seconds()
   193       + buf_scan_non_heap_weak_roots.closure_app_seconds();
   195   phase_times->record_time_secs(G1GCPhaseTimes::ObjCopy, worker_i, obj_copy_time_sec);
   197   double ext_root_time_sec = os::elapsedTime() - ext_roots_start - obj_copy_time_sec;
   199   phase_times->record_time_secs(G1GCPhaseTimes::ExtRootScan, worker_i, ext_root_time_sec);
   201   // During conc marking we have to filter the per-thread SATB buffers
   202   // to make sure we remove any oops into the CSet (which will show up
   203   // as implicitly live).
   204   {
   205     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::SATBFiltering, worker_i);
   206     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_filter_satb_buffers) && _g1h->mark_in_progress()) {
   207       JavaThread::satb_mark_queue_set().filter_thread_buffers();
   208     }
   209   }
   211   _process_strong_tasks.all_tasks_completed();
   212 }
   214 void G1RootProcessor::process_strong_roots(OopClosure* oops,
   215                                            CLDClosure* clds,
   216                                            CodeBlobClosure* blobs) {
   218   process_java_roots(oops, clds, clds, NULL, blobs, NULL, 0);
   219   process_vm_roots(oops, NULL, NULL, 0);
   221   _process_strong_tasks.all_tasks_completed();
   222 }
   224 void G1RootProcessor::process_all_roots(OopClosure* oops,
   225                                         CLDClosure* clds,
   226                                         CodeBlobClosure* blobs,
   227                                         bool process_string_table) {
   229   process_java_roots(oops, NULL, clds, clds, NULL, NULL, 0);
   230   process_vm_roots(oops, oops, NULL, 0);
   232   if (process_string_table) {
   233     process_string_table_roots(oops, NULL, 0);
   234    }
   235   process_code_cache_roots(blobs, NULL, 0);
   237   _process_strong_tasks.all_tasks_completed();
   238 }
   240 void G1RootProcessor::process_all_roots(OopClosure* oops,
   241                                         CLDClosure* clds,
   242                                         CodeBlobClosure* blobs) {
   243   process_all_roots(oops, clds, blobs, true);
   244 }
   246 void G1RootProcessor::process_all_roots_no_string_table(OopClosure* oops,
   247                                                         CLDClosure* clds,
   248                                                         CodeBlobClosure* blobs) {
   249   assert(!ClassUnloading, "Should only be used when class unloading is disabled");
   250   process_all_roots(oops, clds, blobs, false);
   251 }
   254 void G1RootProcessor::process_java_roots(OopClosure* strong_roots,
   255                                          CLDClosure* thread_stack_clds,
   256                                          CLDClosure* strong_clds,
   257                                          CLDClosure* weak_clds,
   258                                          CodeBlobClosure* strong_code,
   259                                          G1GCPhaseTimes* phase_times,
   260                                          uint worker_i) {
   261   assert(thread_stack_clds == NULL || weak_clds == NULL, "There is overlap between those, only one may be set");
   262   // Iterating over the CLDG and the Threads are done early to allow us to
   263   // first process the strong CLDs and nmethods and then, after a barrier,
   264   // let the thread process the weak CLDs and nmethods.
   265   {
   266     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CLDGRoots, worker_i);
   267     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_ClassLoaderDataGraph_oops_do)) {
   268       ClassLoaderDataGraph::roots_cld_do(strong_clds, weak_clds);
   269     }
   270   }
   272   {
   273     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ThreadRoots, worker_i);
   274     Threads::possibly_parallel_oops_do(strong_roots, thread_stack_clds, strong_code);
   275   }
   276 }
   278 void G1RootProcessor::process_vm_roots(OopClosure* strong_roots,
   279                                        OopClosure* weak_roots,
   280                                        G1GCPhaseTimes* phase_times,
   281                                        uint worker_i) {
   282   {
   283     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::UniverseRoots, worker_i);
   284     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_Universe_oops_do)) {
   285       Universe::oops_do(strong_roots);
   286     }
   287   }
   289   {
   290     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JNIRoots, worker_i);
   291     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_JNIHandles_oops_do)) {
   292       JNIHandles::oops_do(strong_roots);
   293     }
   294   }
   296   {
   297     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ObjectSynchronizerRoots, worker_i);
   298     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_ObjectSynchronizer_oops_do)) {
   299       ObjectSynchronizer::oops_do(strong_roots);
   300     }
   301   }
   303   {
   304     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::FlatProfilerRoots, worker_i);
   305     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_FlatProfiler_oops_do)) {
   306       FlatProfiler::oops_do(strong_roots);
   307     }
   308   }
   310   {
   311     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ManagementRoots, worker_i);
   312     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_Management_oops_do)) {
   313       Management::oops_do(strong_roots);
   314     }
   315   }
   317   {
   318     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JVMTIRoots, worker_i);
   319     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_jvmti_oops_do)) {
   320       JvmtiExport::oops_do(strong_roots);
   321     }
   322   }
   324   {
   325     G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::SystemDictionaryRoots, worker_i);
   326     if (!_process_strong_tasks.is_task_claimed(G1RP_PS_SystemDictionary_oops_do)) {
   327       SystemDictionary::roots_oops_do(strong_roots, weak_roots);
   328     }
   329   }
   330 }
   332 void G1RootProcessor::process_string_table_roots(OopClosure* weak_roots, G1GCPhaseTimes* phase_times,
   333                                                  uint worker_i) {
   334   assert(weak_roots != NULL, "Should only be called when all roots are processed");
   336   G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::StringTableRoots, worker_i);
   337   // All threads execute the following. A specific chunk of buckets
   338   // from the StringTable are the individual tasks.
   339   StringTable::possibly_parallel_oops_do(weak_roots);
   340 }
   342 void G1RootProcessor::process_code_cache_roots(CodeBlobClosure* code_closure,
   343                                                G1GCPhaseTimes* phase_times,
   344                                                uint worker_i) {
   345   if (!_process_strong_tasks.is_task_claimed(G1RP_PS_CodeCache_oops_do)) {
   346     CodeCache::blobs_do(code_closure);
   347   }
   348 }
   350 void G1RootProcessor::scan_remembered_sets(G1ParPushHeapRSClosure* scan_rs,
   351                                            OopClosure* scan_non_heap_weak_roots,
   352                                            uint worker_i) {
   353   G1GCPhaseTimes* phase_times = _g1h->g1_policy()->phase_times();
   354   G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CodeCacheRoots, worker_i);
   356   // Now scan the complement of the collection set.
   357   G1CodeBlobClosure scavenge_cs_nmethods(scan_non_heap_weak_roots);
   359   _g1h->g1_rem_set()->oops_into_collection_set_do(scan_rs, &scavenge_cs_nmethods, worker_i);
   360 }
   362 void G1RootProcessor::set_num_workers(int active_workers) {
   363   _process_strong_tasks.set_n_threads(active_workers);
   364 }

mercurial