duke@435: /* drchase@6680: * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "code/codeCache.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" stefank@2314: #include "memory/sharedHeap.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@6992: #include "runtime/atomic.inline.hpp" stefank@2314: #include "runtime/fprofiler.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "services/management.hpp" stefank@2314: #include "utilities/copy.hpp" stefank@2314: #include "utilities/workgroup.hpp" duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: duke@435: SharedHeap* SharedHeap::_sh; duke@435: stefank@6992: // The set of potentially parallel tasks in root scanning. stefank@6992: enum SH_process_roots_tasks { duke@435: SH_PS_Universe_oops_do, duke@435: SH_PS_JNIHandles_oops_do, duke@435: SH_PS_ObjectSynchronizer_oops_do, duke@435: SH_PS_FlatProfiler_oops_do, duke@435: SH_PS_Management_oops_do, duke@435: SH_PS_SystemDictionary_oops_do, stefank@5194: SH_PS_ClassLoaderDataGraph_oops_do, duke@435: SH_PS_jvmti_oops_do, duke@435: SH_PS_CodeCache_oops_do, duke@435: // Leave this one last. duke@435: SH_PS_NumElements duke@435: }; duke@435: duke@435: SharedHeap::SharedHeap(CollectorPolicy* policy_) : duke@435: CollectedHeap(), duke@435: _collector_policy(policy_), coleenp@4037: _rem_set(NULL), stefank@6992: _strong_roots_scope(NULL), duke@435: _strong_roots_parity(0), duke@435: _process_strong_tasks(new SubTasksDone(SH_PS_NumElements)), jmasa@2188: _workers(NULL) duke@435: { duke@435: if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) { duke@435: vm_exit_during_initialization("Failed necessary allocation."); duke@435: } duke@435: _sh = this; // ch is static, should be set only once. duke@435: if ((UseParNewGC || jmasa@5461: (UseConcMarkSweepGC && (CMSParallelInitialMarkEnabled || jmasa@5461: CMSParallelRemarkEnabled)) || ysr@777: UseG1GC) && duke@435: ParallelGCThreads > 0) { jmasa@2188: _workers = new FlexibleWorkGang("Parallel GC Threads", ParallelGCThreads, ysr@777: /* are_GC_task_threads */true, ysr@777: /* are_ConcurrentGC_threads */false); duke@435: if (_workers == NULL) { duke@435: vm_exit_during_initialization("Failed necessary allocation."); jmasa@2188: } else { jmasa@2188: _workers->initialize_workers(); duke@435: } duke@435: } duke@435: } duke@435: jmasa@3294: int SharedHeap::n_termination() { jmasa@3294: return _process_strong_tasks->n_threads(); jmasa@3294: } jmasa@3294: jmasa@3294: void SharedHeap::set_n_termination(int t) { jmasa@3294: _process_strong_tasks->set_n_threads(t); jmasa@3294: } jmasa@3294: ysr@777: bool SharedHeap::heap_lock_held_for_gc() { ysr@777: Thread* t = Thread::current(); ysr@777: return Heap_lock->owned_by_self() ysr@777: || ( (t->is_GC_task_thread() || t->is_VM_thread()) ysr@777: && _thread_holds_heap_lock_for_gc); ysr@777: } duke@435: jmasa@3357: void SharedHeap::set_par_threads(uint t) { jmasa@2188: assert(t == 0 || !UseSerialGC, "Cannot have parallel threads"); duke@435: _n_par_threads = t; jmasa@2188: _process_strong_tasks->set_n_threads(t); duke@435: } duke@435: jmasa@2909: #ifdef ASSERT jmasa@2909: class AssertNonScavengableClosure: public OopClosure { jmasa@2909: public: jmasa@2909: virtual void do_oop(oop* p) { jmasa@2909: assert(!Universe::heap()->is_in_partial_collection(*p), jmasa@2909: "Referent should not be scavengable."); } jmasa@2909: virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } jmasa@2909: }; jmasa@2909: static AssertNonScavengableClosure assert_is_non_scavengable_closure; jmasa@2909: #endif jmasa@2909: stefank@6992: SharedHeap::StrongRootsScope* SharedHeap::active_strong_roots_scope() const { stefank@6992: return _strong_roots_scope; stefank@6992: } stefank@6992: void SharedHeap::register_strong_roots_scope(SharedHeap::StrongRootsScope* scope) { stefank@6992: assert(_strong_roots_scope == NULL, "Should only have one StrongRootsScope active"); stefank@6992: assert(scope != NULL, "Illegal argument"); stefank@6992: _strong_roots_scope = scope; stefank@6992: } stefank@6992: void SharedHeap::unregister_strong_roots_scope(SharedHeap::StrongRootsScope* scope) { stefank@6992: assert(_strong_roots_scope == scope, "Wrong scope unregistered"); stefank@6992: _strong_roots_scope = NULL; stefank@6992: } stefank@6992: duke@435: void SharedHeap::change_strong_roots_parity() { duke@435: // Also set the new collection parity. duke@435: assert(_strong_roots_parity >= 0 && _strong_roots_parity <= 2, duke@435: "Not in range."); duke@435: _strong_roots_parity++; duke@435: if (_strong_roots_parity == 3) _strong_roots_parity = 1; duke@435: assert(_strong_roots_parity >= 1 && _strong_roots_parity <= 2, duke@435: "Not in range."); duke@435: } duke@435: stefank@6992: SharedHeap::StrongRootsScope::StrongRootsScope(SharedHeap* heap, bool activate) stefank@6992: : MarkScope(activate), _sh(heap), _n_workers_done_with_threads(0) jrose@1424: { jrose@1424: if (_active) { stefank@6992: _sh->register_strong_roots_scope(this); stefank@6992: _sh->change_strong_roots_parity(); johnc@5277: // Zero the claimed high water mark in the StringTable johnc@5277: StringTable::clear_parallel_claimed_index(); jrose@1424: } jrose@1424: } jrose@1424: jrose@1424: SharedHeap::StrongRootsScope::~StrongRootsScope() { stefank@6992: if (_active) { stefank@6992: _sh->unregister_strong_roots_scope(this); stefank@6992: } stefank@6992: } stefank@6992: stefank@6992: Monitor* SharedHeap::StrongRootsScope::_lock = new Monitor(Mutex::leaf, "StrongRootsScope lock", false); stefank@6992: stefank@6992: void SharedHeap::StrongRootsScope::mark_worker_done_with_threads(uint n_workers) { stefank@6992: // The Thread work barrier is only needed by G1. stefank@6992: // No need to use the barrier if this is single-threaded code. stefank@6992: if (UseG1GC && n_workers > 0) { stefank@6992: uint new_value = (uint)Atomic::add(1, &_n_workers_done_with_threads); stefank@6992: if (new_value == n_workers) { stefank@6992: // This thread is last. Notify the others. stefank@6992: MonitorLockerEx ml(_lock, Mutex::_no_safepoint_check_flag); stefank@6992: _lock->notify_all(); stefank@6992: } stefank@6992: } stefank@6992: } stefank@6992: stefank@6992: void SharedHeap::StrongRootsScope::wait_until_all_workers_done_with_threads(uint n_workers) { stefank@6992: // No need to use the barrier if this is single-threaded code. stefank@6992: if (n_workers > 0 && (uint)_n_workers_done_with_threads != n_workers) { stefank@6992: MonitorLockerEx ml(_lock, Mutex::_no_safepoint_check_flag); stefank@6992: while ((uint)_n_workers_done_with_threads != n_workers) { stefank@6992: _lock->wait(Mutex::_no_safepoint_check_flag, 0, false); stefank@6992: } stefank@6992: } stefank@6992: } stefank@6992: stefank@6992: void SharedHeap::process_roots(bool activate_scope, stefank@6992: ScanningOption so, stefank@6992: OopClosure* strong_roots, stefank@6992: OopClosure* weak_roots, stefank@6992: CLDClosure* strong_cld_closure, stefank@6992: CLDClosure* weak_cld_closure, stefank@6992: CodeBlobClosure* code_roots) { stefank@6992: StrongRootsScope srs(this, activate_scope); stefank@6992: stefank@6992: // General roots. stefank@6992: assert(_strong_roots_parity != 0, "must have called prologue code"); stefank@6992: assert(code_roots != NULL, "code root closure should always be set"); stefank@6992: // _n_termination for _process_strong_tasks should be set up stream stefank@6992: // in a method not running in a GC worker. Otherwise the GC worker stefank@6992: // could be trying to change the termination condition while the task stefank@6992: // is executing in another GC worker. stefank@6992: stefank@6992: // Iterating over the CLDG and the Threads are done early to allow G1 to stefank@6992: // first process the strong CLDs and nmethods and then, after a barrier, stefank@6992: // let the thread process the weak CLDs and nmethods. stefank@6992: stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_ClassLoaderDataGraph_oops_do)) { stefank@6992: ClassLoaderDataGraph::roots_cld_do(strong_cld_closure, weak_cld_closure); stefank@6992: } stefank@6992: stefank@6992: // Some CLDs contained in the thread frames should be considered strong. stefank@6992: // Don't process them if they will be processed during the ClassLoaderDataGraph phase. stefank@6992: CLDClosure* roots_from_clds_p = (strong_cld_closure != weak_cld_closure) ? strong_cld_closure : NULL; stefank@6992: // Only process code roots from thread stacks if we aren't visiting the entire CodeCache anyway stefank@6992: CodeBlobClosure* roots_from_code_p = (so & SO_AllCodeCache) ? NULL : code_roots; stefank@6992: stefank@6992: Threads::possibly_parallel_oops_do(strong_roots, roots_from_clds_p, roots_from_code_p); stefank@6992: stefank@6992: // This is the point where this worker thread will not find more strong CLDs/nmethods. stefank@6992: // Report this so G1 can synchronize the strong and weak CLDs/nmethods processing. stefank@6992: active_strong_roots_scope()->mark_worker_done_with_threads(n_par_threads()); stefank@6992: stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_Universe_oops_do)) { stefank@6992: Universe::oops_do(strong_roots); stefank@6992: } stefank@6992: // Global (strong) JNI handles stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do)) stefank@6992: JNIHandles::oops_do(strong_roots); stefank@6992: stefank@6992: if (!_process_strong_tasks-> is_task_claimed(SH_PS_ObjectSynchronizer_oops_do)) stefank@6992: ObjectSynchronizer::oops_do(strong_roots); stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_FlatProfiler_oops_do)) stefank@6992: FlatProfiler::oops_do(strong_roots); stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_Management_oops_do)) stefank@6992: Management::oops_do(strong_roots); stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_jvmti_oops_do)) stefank@6992: JvmtiExport::oops_do(strong_roots); stefank@6992: stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_SystemDictionary_oops_do)) { stefank@6992: SystemDictionary::roots_oops_do(strong_roots, weak_roots); stefank@6992: } stefank@6992: stefank@6992: // All threads execute the following. A specific chunk of buckets stefank@6992: // from the StringTable are the individual tasks. stefank@6992: if (weak_roots != NULL) { stefank@6992: if (CollectedHeap::use_parallel_gc_threads()) { stefank@6992: StringTable::possibly_parallel_oops_do(weak_roots); stefank@6992: } else { stefank@6992: StringTable::oops_do(weak_roots); stefank@6992: } stefank@6992: } stefank@6992: stefank@6992: if (!_process_strong_tasks->is_task_claimed(SH_PS_CodeCache_oops_do)) { stefank@6992: if (so & SO_ScavengeCodeCache) { stefank@6992: assert(code_roots != NULL, "must supply closure for code cache"); stefank@6992: stefank@6992: // We only visit parts of the CodeCache when scavenging. stefank@6992: CodeCache::scavenge_root_nmethods_do(code_roots); stefank@6992: } stefank@6992: if (so & SO_AllCodeCache) { stefank@6992: assert(code_roots != NULL, "must supply closure for code cache"); stefank@6992: stefank@6992: // CMSCollector uses this to do intermediate-strength collections. stefank@6992: // We scan the entire code cache, since CodeCache::do_unloading is not called. stefank@6992: CodeCache::blobs_do(code_roots); stefank@6992: } stefank@6992: // Verify that the code cache contents are not subject to stefank@6992: // movement by a scavenging collection. stefank@6992: DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, !CodeBlobToOopClosure::FixRelocations)); stefank@6992: DEBUG_ONLY(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable)); stefank@6992: } stefank@6992: stefank@6992: _process_strong_tasks->all_tasks_completed(); stefank@6992: } stefank@6992: stefank@6992: void SharedHeap::process_all_roots(bool activate_scope, stefank@6992: ScanningOption so, stefank@6992: OopClosure* roots, stefank@6992: CLDClosure* cld_closure, stefank@6992: CodeBlobClosure* code_closure) { stefank@6992: process_roots(activate_scope, so, stefank@6992: roots, roots, stefank@6992: cld_closure, cld_closure, stefank@6992: code_closure); jrose@1424: } jrose@1424: jrose@1424: void SharedHeap::process_strong_roots(bool activate_scope, duke@435: ScanningOption so, duke@435: OopClosure* roots, stefank@6992: CLDClosure* cld_closure, stefank@6992: CodeBlobClosure* code_closure) { stefank@6992: process_roots(activate_scope, so, stefank@6992: roots, NULL, stefank@6992: cld_closure, NULL, stefank@6992: code_closure); stefank@6992: } coleenp@4037: duke@435: duke@435: class AlwaysTrueClosure: public BoolObjectClosure { duke@435: public: duke@435: bool do_object_b(oop p) { return true; } duke@435: }; duke@435: static AlwaysTrueClosure always_true; duke@435: stefank@6971: void SharedHeap::process_weak_roots(OopClosure* root_closure) { duke@435: // Global (weak) JNI handles duke@435: JNIHandles::weak_oops_do(&always_true, root_closure); stefank@5011: } duke@435: duke@435: void SharedHeap::set_barrier_set(BarrierSet* bs) { duke@435: _barrier_set = bs; duke@435: // Cached barrier set for fast access in oops duke@435: oopDesc::set_bs(bs); duke@435: } duke@435: duke@435: void SharedHeap::post_initialize() { jwilhelm@6085: CollectedHeap::post_initialize(); duke@435: ref_processing_init(); duke@435: } duke@435: coleenp@4037: void SharedHeap::ref_processing_init() {} duke@435: duke@435: // Some utilities. ysr@777: void SharedHeap::print_size_transition(outputStream* out, ysr@777: size_t bytes_before, duke@435: size_t bytes_after, duke@435: size_t capacity) { ysr@777: out->print(" %d%s->%d%s(%d%s)", duke@435: byte_size_in_proper_unit(bytes_before), duke@435: proper_unit_for_byte_size(bytes_before), duke@435: byte_size_in_proper_unit(bytes_after), duke@435: proper_unit_for_byte_size(bytes_after), duke@435: byte_size_in_proper_unit(capacity), duke@435: proper_unit_for_byte_size(capacity)); duke@435: }