mgerdin@7659: /* fmatte@9358: * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. mgerdin@7659: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mgerdin@7659: * mgerdin@7659: * This code is free software; you can redistribute it and/or modify it mgerdin@7659: * under the terms of the GNU General Public License version 2 only, as mgerdin@7659: * published by the Free Software Foundation. mgerdin@7659: * mgerdin@7659: * This code is distributed in the hope that it will be useful, but WITHOUT mgerdin@7659: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mgerdin@7659: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mgerdin@7659: * version 2 for more details (a copy is included in the LICENSE file that mgerdin@7659: * accompanied this code). mgerdin@7659: * mgerdin@7659: * You should have received a copy of the GNU General Public License version mgerdin@7659: * 2 along with this work; if not, write to the Free Software Foundation, mgerdin@7659: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mgerdin@7659: * mgerdin@7659: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mgerdin@7659: * or visit www.oracle.com if you need additional information or have any mgerdin@7659: * questions. mgerdin@7659: * mgerdin@7659: */ mgerdin@7659: mgerdin@7659: #include "precompiled.hpp" mgerdin@7659: mgerdin@7659: #include "classfile/symbolTable.hpp" mgerdin@7659: #include "classfile/systemDictionary.hpp" mgerdin@7659: #include "code/codeCache.hpp" mgerdin@7659: #include "gc_implementation/g1/bufferingOopClosure.hpp" mgerdin@7659: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" mgerdin@7659: #include "gc_implementation/g1/g1CollectorPolicy.hpp" mgerdin@7659: #include "gc_implementation/g1/g1GCPhaseTimes.hpp" mgerdin@7659: #include "gc_implementation/g1/g1RemSet.inline.hpp" mgerdin@7659: #include "gc_implementation/g1/g1RootProcessor.hpp" mgerdin@7659: #include "memory/allocation.inline.hpp" mgerdin@7659: #include "runtime/fprofiler.hpp" mgerdin@7659: #include "runtime/mutex.hpp" mgerdin@7659: #include "services/management.hpp" mgerdin@7659: mgerdin@7659: class G1CodeBlobClosure : public CodeBlobClosure { mgerdin@7659: class HeapRegionGatheringOopClosure : public OopClosure { mgerdin@7659: G1CollectedHeap* _g1h; mgerdin@7659: OopClosure* _work; mgerdin@7659: nmethod* _nm; mgerdin@7659: mgerdin@7659: template mgerdin@7659: void do_oop_work(T* p) { mgerdin@7659: _work->do_oop(p); mgerdin@7659: T oop_or_narrowoop = oopDesc::load_heap_oop(p); mgerdin@7659: if (!oopDesc::is_null(oop_or_narrowoop)) { mgerdin@7659: oop o = oopDesc::decode_heap_oop_not_null(oop_or_narrowoop); mgerdin@7659: HeapRegion* hr = _g1h->heap_region_containing_raw(o); mgerdin@7659: 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"); mgerdin@7659: hr->add_strong_code_root(_nm); mgerdin@7659: } mgerdin@7659: } mgerdin@7659: mgerdin@7659: public: mgerdin@7659: HeapRegionGatheringOopClosure(OopClosure* oc) : _g1h(G1CollectedHeap::heap()), _work(oc), _nm(NULL) {} mgerdin@7659: mgerdin@7659: void do_oop(oop* o) { mgerdin@7659: do_oop_work(o); mgerdin@7659: } mgerdin@7659: mgerdin@7659: void do_oop(narrowOop* o) { mgerdin@7659: do_oop_work(o); mgerdin@7659: } mgerdin@7659: mgerdin@7659: void set_nm(nmethod* nm) { mgerdin@7659: _nm = nm; mgerdin@7659: } mgerdin@7659: }; mgerdin@7659: mgerdin@7659: HeapRegionGatheringOopClosure _oc; mgerdin@7659: public: mgerdin@7659: G1CodeBlobClosure(OopClosure* oc) : _oc(oc) {} mgerdin@7659: mgerdin@7659: void do_code_blob(CodeBlob* cb) { mgerdin@7659: nmethod* nm = cb->as_nmethod_or_null(); mgerdin@7659: if (nm != NULL) { mgerdin@7659: if (!nm->test_set_oops_do_mark()) { mgerdin@7659: _oc.set_nm(nm); mgerdin@7659: nm->oops_do(&_oc); mgerdin@7659: nm->fix_oop_relocations(); mgerdin@7659: } mgerdin@7659: } mgerdin@7659: } mgerdin@7659: }; mgerdin@7659: mgerdin@7659: mgerdin@7659: void G1RootProcessor::worker_has_discovered_all_strong_classes() { mgerdin@7659: uint n_workers = _g1h->n_par_threads(); mgerdin@7659: assert(ClassUnloadingWithConcurrentMark, "Currently only needed when doing G1 Class Unloading"); mgerdin@7659: sangheki@7764: if (n_workers > 0) { sangheki@7764: uint new_value = (uint)Atomic::add(1, &_n_workers_discovered_strong_classes); sangheki@7764: if (new_value == n_workers) { sangheki@7764: // This thread is last. Notify the others. sangheki@7764: MonitorLockerEx ml(&_lock, Mutex::_no_safepoint_check_flag); sangheki@7764: _lock.notify_all(); sangheki@7764: } mgerdin@7659: } mgerdin@7659: } mgerdin@7659: mgerdin@7659: void G1RootProcessor::wait_until_all_strong_classes_discovered() { mgerdin@7659: uint n_workers = _g1h->n_par_threads(); mgerdin@7659: assert(ClassUnloadingWithConcurrentMark, "Currently only needed when doing G1 Class Unloading"); mgerdin@7659: sangheki@7764: if (n_workers > 0 && (uint)_n_workers_discovered_strong_classes != n_workers) { mgerdin@7659: MonitorLockerEx ml(&_lock, Mutex::_no_safepoint_check_flag); mgerdin@7659: while ((uint)_n_workers_discovered_strong_classes != n_workers) { mgerdin@7659: _lock.wait(Mutex::_no_safepoint_check_flag, 0, false); mgerdin@7659: } mgerdin@7659: } mgerdin@7659: } mgerdin@7659: mgerdin@7659: G1RootProcessor::G1RootProcessor(G1CollectedHeap* g1h) : mgerdin@7659: _g1h(g1h), mgerdin@8070: _process_strong_tasks(G1RP_PS_NumElements), mgerdin@7659: _srs(g1h), mgerdin@7659: _lock(Mutex::leaf, "G1 Root Scanning barrier lock", false), mgerdin@7659: _n_workers_discovered_strong_classes(0) {} mgerdin@7659: mgerdin@7659: void G1RootProcessor::evacuate_roots(OopClosure* scan_non_heap_roots, mgerdin@7659: OopClosure* scan_non_heap_weak_roots, mgerdin@7659: CLDClosure* scan_strong_clds, mgerdin@7659: CLDClosure* scan_weak_clds, mgerdin@7659: bool trace_metadata, mgerdin@7659: uint worker_i) { mgerdin@7659: // First scan the shared roots. mgerdin@7659: double ext_roots_start = os::elapsedTime(); brutisso@7660: G1GCPhaseTimes* phase_times = _g1h->g1_policy()->phase_times(); mgerdin@7659: mgerdin@7659: BufferingOopClosure buf_scan_non_heap_roots(scan_non_heap_roots); mgerdin@7659: BufferingOopClosure buf_scan_non_heap_weak_roots(scan_non_heap_weak_roots); mgerdin@7659: mgerdin@7659: OopClosure* const weak_roots = &buf_scan_non_heap_weak_roots; mgerdin@7659: OopClosure* const strong_roots = &buf_scan_non_heap_roots; mgerdin@7659: mgerdin@7659: // CodeBlobClosures are not interoperable with BufferingOopClosures mgerdin@7659: G1CodeBlobClosure root_code_blobs(scan_non_heap_roots); mgerdin@7659: mgerdin@7659: process_java_roots(strong_roots, mgerdin@7659: trace_metadata ? scan_strong_clds : NULL, mgerdin@7659: scan_strong_clds, mgerdin@7659: trace_metadata ? NULL : scan_weak_clds, brutisso@7660: &root_code_blobs, brutisso@7660: phase_times, brutisso@7660: worker_i); mgerdin@7659: mgerdin@7659: // This is the point where this worker thread will not find more strong CLDs/nmethods. mgerdin@7659: // Report this so G1 can synchronize the strong and weak CLDs/nmethods processing. mgerdin@7659: if (trace_metadata) { mgerdin@7659: worker_has_discovered_all_strong_classes(); mgerdin@7659: } mgerdin@7659: brutisso@7660: process_vm_roots(strong_roots, weak_roots, phase_times, worker_i); fmatte@9358: process_string_table_roots(weak_roots, phase_times, worker_i); brutisso@7660: { brutisso@7660: // Now the CM ref_processor roots. brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CMRefRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_refProcessor_oops_do)) { brutisso@7660: // We need to treat the discovered reference lists of the brutisso@7660: // concurrent mark ref processor as roots and keep entries brutisso@7660: // (which are added by the marking threads) on them live brutisso@7660: // until they can be processed at the end of marking. brutisso@7660: _g1h->ref_processor_cm()->weak_oops_do(&buf_scan_non_heap_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: mgerdin@7659: if (trace_metadata) { brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::WaitForStrongCLD, worker_i); brutisso@7660: // Barrier to make sure all workers passed brutisso@7660: // the strong CLD and strong nmethods phases. brutisso@7660: wait_until_all_strong_classes_discovered(); brutisso@7660: } mgerdin@7659: mgerdin@7659: // Now take the complement of the strong CLDs. brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::WeakCLDRoots, worker_i); mgerdin@7659: ClassLoaderDataGraph::roots_cld_do(NULL, scan_weak_clds); brutisso@7660: } else { brutisso@7660: phase_times->record_time_secs(G1GCPhaseTimes::WaitForStrongCLD, worker_i, 0.0); brutisso@7660: phase_times->record_time_secs(G1GCPhaseTimes::WeakCLDRoots, worker_i, 0.0); mgerdin@7659: } mgerdin@7659: mgerdin@7659: // Finish up any enqueued closure apps (attributed as object copy time). mgerdin@7659: buf_scan_non_heap_roots.done(); mgerdin@7659: buf_scan_non_heap_weak_roots.done(); mgerdin@7659: mgerdin@7659: double obj_copy_time_sec = buf_scan_non_heap_roots.closure_app_seconds() mgerdin@7659: + buf_scan_non_heap_weak_roots.closure_app_seconds(); mgerdin@7659: mgerdin@7659: phase_times->record_time_secs(G1GCPhaseTimes::ObjCopy, worker_i, obj_copy_time_sec); mgerdin@7659: mgerdin@7659: double ext_root_time_sec = os::elapsedTime() - ext_roots_start - obj_copy_time_sec; mgerdin@7659: mgerdin@7659: phase_times->record_time_secs(G1GCPhaseTimes::ExtRootScan, worker_i, ext_root_time_sec); mgerdin@7659: mgerdin@7659: // During conc marking we have to filter the per-thread SATB buffers mgerdin@7659: // to make sure we remove any oops into the CSet (which will show up mgerdin@7659: // as implicitly live). mgerdin@7659: { mgerdin@7659: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::SATBFiltering, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_filter_satb_buffers) && _g1h->mark_in_progress()) { mgerdin@7659: JavaThread::satb_mark_queue_set().filter_thread_buffers(); mgerdin@7659: } mgerdin@7659: } mgerdin@7659: mgerdin@8070: _process_strong_tasks.all_tasks_completed(); mgerdin@7659: } mgerdin@7659: mgerdin@7659: void G1RootProcessor::process_strong_roots(OopClosure* oops, mgerdin@7659: CLDClosure* clds, mgerdin@7659: CodeBlobClosure* blobs) { mgerdin@7659: brutisso@7660: process_java_roots(oops, clds, clds, NULL, blobs, NULL, 0); brutisso@7660: process_vm_roots(oops, NULL, NULL, 0); mgerdin@7659: mgerdin@8070: _process_strong_tasks.all_tasks_completed(); mgerdin@7659: } mgerdin@7659: mgerdin@7659: void G1RootProcessor::process_all_roots(OopClosure* oops, mgerdin@7659: CLDClosure* clds, fmatte@9358: CodeBlobClosure* blobs, fmatte@9358: bool process_string_table) { mgerdin@7659: brutisso@7660: process_java_roots(oops, NULL, clds, clds, NULL, NULL, 0); brutisso@7660: process_vm_roots(oops, oops, NULL, 0); mgerdin@7659: fmatte@9358: if (process_string_table) { fmatte@9358: process_string_table_roots(oops, NULL, 0); fmatte@9358: } fmatte@9358: process_code_cache_roots(blobs, NULL, 0); mgerdin@7659: mgerdin@8070: _process_strong_tasks.all_tasks_completed(); mgerdin@7659: } mgerdin@7659: fmatte@9358: void G1RootProcessor::process_all_roots(OopClosure* oops, fmatte@9358: CLDClosure* clds, fmatte@9358: CodeBlobClosure* blobs) { fmatte@9358: process_all_roots(oops, clds, blobs, true); fmatte@9358: } fmatte@9358: fmatte@9358: void G1RootProcessor::process_all_roots_no_string_table(OopClosure* oops, fmatte@9358: CLDClosure* clds, fmatte@9358: CodeBlobClosure* blobs) { fmatte@9358: assert(!ClassUnloading, "Should only be used when class unloading is disabled"); fmatte@9358: process_all_roots(oops, clds, blobs, false); fmatte@9358: } fmatte@9358: fmatte@9358: mgerdin@7659: void G1RootProcessor::process_java_roots(OopClosure* strong_roots, mgerdin@7659: CLDClosure* thread_stack_clds, mgerdin@7659: CLDClosure* strong_clds, mgerdin@7659: CLDClosure* weak_clds, brutisso@7660: CodeBlobClosure* strong_code, brutisso@7660: G1GCPhaseTimes* phase_times, brutisso@7660: uint worker_i) { mgerdin@7659: assert(thread_stack_clds == NULL || weak_clds == NULL, "There is overlap between those, only one may be set"); mgerdin@7659: // Iterating over the CLDG and the Threads are done early to allow us to mgerdin@7659: // first process the strong CLDs and nmethods and then, after a barrier, mgerdin@7659: // let the thread process the weak CLDs and nmethods. brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CLDGRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_ClassLoaderDataGraph_oops_do)) { brutisso@7660: ClassLoaderDataGraph::roots_cld_do(strong_clds, weak_clds); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ThreadRoots, worker_i); brutisso@7660: Threads::possibly_parallel_oops_do(strong_roots, thread_stack_clds, strong_code); brutisso@7660: } mgerdin@7659: } mgerdin@7659: mgerdin@7659: void G1RootProcessor::process_vm_roots(OopClosure* strong_roots, brutisso@7660: OopClosure* weak_roots, brutisso@7660: G1GCPhaseTimes* phase_times, brutisso@7660: uint worker_i) { brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::UniverseRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_Universe_oops_do)) { brutisso@7660: Universe::oops_do(strong_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JNIRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_JNIHandles_oops_do)) { brutisso@7660: JNIHandles::oops_do(strong_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ObjectSynchronizerRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_ObjectSynchronizer_oops_do)) { brutisso@7660: ObjectSynchronizer::oops_do(strong_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::FlatProfilerRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_FlatProfiler_oops_do)) { brutisso@7660: FlatProfiler::oops_do(strong_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::ManagementRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_Management_oops_do)) { brutisso@7660: Management::oops_do(strong_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::JVMTIRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_jvmti_oops_do)) { brutisso@7660: JvmtiExport::oops_do(strong_roots); brutisso@7660: } mgerdin@7659: } mgerdin@7659: brutisso@7660: { brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::SystemDictionaryRoots, worker_i); mgerdin@8070: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_SystemDictionary_oops_do)) { brutisso@7660: SystemDictionary::roots_oops_do(strong_roots, weak_roots); brutisso@7660: } mgerdin@7659: } fmatte@9358: } mgerdin@7659: fmatte@9358: void G1RootProcessor::process_string_table_roots(OopClosure* weak_roots, G1GCPhaseTimes* phase_times, fmatte@9358: uint worker_i) { fmatte@9358: assert(weak_roots != NULL, "Should only be called when all roots are processed"); fmatte@9358: fmatte@9358: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::StringTableRoots, worker_i); fmatte@9358: // All threads execute the following. A specific chunk of buckets fmatte@9358: // from the StringTable are the individual tasks. fmatte@9358: StringTable::possibly_parallel_oops_do(weak_roots); fmatte@9358: } fmatte@9358: fmatte@9358: void G1RootProcessor::process_code_cache_roots(CodeBlobClosure* code_closure, fmatte@9358: G1GCPhaseTimes* phase_times, fmatte@9358: uint worker_i) { fmatte@9358: if (!_process_strong_tasks.is_task_claimed(G1RP_PS_CodeCache_oops_do)) { fmatte@9358: CodeCache::blobs_do(code_closure); mgerdin@7659: } mgerdin@7659: } mgerdin@7659: mgerdin@7659: void G1RootProcessor::scan_remembered_sets(G1ParPushHeapRSClosure* scan_rs, mgerdin@7659: OopClosure* scan_non_heap_weak_roots, mgerdin@7659: uint worker_i) { brutisso@7660: G1GCPhaseTimes* phase_times = _g1h->g1_policy()->phase_times(); brutisso@7660: G1GCParPhaseTimesTracker x(phase_times, G1GCPhaseTimes::CodeCacheRoots, worker_i); brutisso@7660: mgerdin@7659: // Now scan the complement of the collection set. mgerdin@7659: G1CodeBlobClosure scavenge_cs_nmethods(scan_non_heap_weak_roots); mgerdin@7659: mgerdin@7659: _g1h->g1_rem_set()->oops_into_collection_set_do(scan_rs, &scavenge_cs_nmethods, worker_i); mgerdin@7659: } mgerdin@7659: mgerdin@7659: void G1RootProcessor::set_num_workers(int active_workers) { mgerdin@8070: _process_strong_tasks.set_n_threads(active_workers); mgerdin@7659: }