duke@435: /* xdono@1014: * Copyright 2000-2009 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_sharedHeap.cpp.incl" duke@435: duke@435: SharedHeap* SharedHeap::_sh; duke@435: duke@435: // The set of potentially parallel tasks in strong root scanning. duke@435: enum SH_process_strong_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, duke@435: SH_PS_jvmti_oops_do, duke@435: SH_PS_vmSymbols_oops_do, duke@435: SH_PS_SymbolTable_oops_do, duke@435: SH_PS_StringTable_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_), duke@435: _perm_gen(NULL), _rem_set(NULL), duke@435: _strong_roots_parity(0), duke@435: _process_strong_tasks(new SubTasksDone(SH_PS_NumElements)), duke@435: _workers(NULL), _n_par_threads(0) 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 || ysr@777: (UseConcMarkSweepGC && CMSParallelRemarkEnabled) || ysr@777: UseG1GC) && duke@435: ParallelGCThreads > 0) { ysr@777: _workers = new WorkGang("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."); duke@435: } duke@435: } duke@435: } duke@435: 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: duke@435: void SharedHeap::set_par_threads(int t) { duke@435: _n_par_threads = t; duke@435: _process_strong_tasks->set_par_threads(t); duke@435: } duke@435: duke@435: class AssertIsPermClosure: public OopClosure { duke@435: public: coleenp@548: virtual void do_oop(oop* p) { duke@435: assert((*p) == NULL || (*p)->is_perm(), "Referent should be perm."); duke@435: } coleenp@548: virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } duke@435: }; duke@435: static AssertIsPermClosure assert_is_perm_closure; duke@435: 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: jrose@1424: SharedHeap::StrongRootsScope::StrongRootsScope(SharedHeap* outer, bool activate) jrose@1424: : MarkScope(activate) jrose@1424: { jrose@1424: if (_active) { jrose@1424: outer->change_strong_roots_parity(); jrose@1424: } jrose@1424: } jrose@1424: jrose@1424: SharedHeap::StrongRootsScope::~StrongRootsScope() { jrose@1424: // nothing particular jrose@1424: } jrose@1424: jrose@1424: void SharedHeap::process_strong_roots(bool activate_scope, jrose@1424: bool collecting_perm_gen, duke@435: ScanningOption so, duke@435: OopClosure* roots, jrose@1424: CodeBlobClosure* code_roots, duke@435: OopsInGenClosure* perm_blk) { jrose@1424: StrongRootsScope srs(this, activate_scope); duke@435: // General strong roots. jrose@1424: assert(_strong_roots_parity != 0, "must have called prologue code"); duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_Universe_oops_do)) { duke@435: Universe::oops_do(roots); duke@435: ReferenceProcessor::oops_do(roots); duke@435: // Consider perm-gen discovered lists to be strong. duke@435: perm_gen()->ref_processor()->weak_oops_do(roots); duke@435: } duke@435: // Global (strong) JNI handles duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do)) duke@435: JNIHandles::oops_do(roots); duke@435: // All threads execute this; the individual threads are task groups. duke@435: if (ParallelGCThreads > 0) { jrose@1424: Threads::possibly_parallel_oops_do(roots, code_roots); duke@435: } else { jrose@1424: Threads::oops_do(roots, code_roots); duke@435: } duke@435: if (!_process_strong_tasks-> is_task_claimed(SH_PS_ObjectSynchronizer_oops_do)) duke@435: ObjectSynchronizer::oops_do(roots); duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_FlatProfiler_oops_do)) duke@435: FlatProfiler::oops_do(roots); duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_Management_oops_do)) duke@435: Management::oops_do(roots); duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_jvmti_oops_do)) duke@435: JvmtiExport::oops_do(roots); duke@435: duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_SystemDictionary_oops_do)) { duke@435: if (so & SO_AllClasses) { duke@435: SystemDictionary::oops_do(roots); duke@435: } else duke@435: if (so & SO_SystemClasses) { duke@435: SystemDictionary::always_strong_oops_do(roots); duke@435: } duke@435: } duke@435: duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_SymbolTable_oops_do)) { duke@435: if (so & SO_Symbols) { duke@435: SymbolTable::oops_do(roots); duke@435: } duke@435: // Verify if the symbol table contents are in the perm gen duke@435: NOT_PRODUCT(SymbolTable::oops_do(&assert_is_perm_closure)); duke@435: } duke@435: duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_StringTable_oops_do)) { duke@435: if (so & SO_Strings) { duke@435: StringTable::oops_do(roots); duke@435: } duke@435: // Verify if the string table contents are in the perm gen duke@435: NOT_PRODUCT(StringTable::oops_do(&assert_is_perm_closure)); duke@435: } duke@435: duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_CodeCache_oops_do)) { jrose@1424: if (so & SO_CodeCache) { jrose@1424: // (Currently, CMSCollector uses this to do intermediate-strength collections.) jrose@1424: assert(collecting_perm_gen, "scanning all of code cache"); jrose@1424: assert(code_roots != NULL, "must supply closure for code cache"); jrose@1424: if (code_roots != NULL) { jrose@1424: CodeCache::blobs_do(code_roots); jrose@1424: } jrose@1424: } else if (so & (SO_SystemClasses|SO_AllClasses)) { jrose@1424: if (!collecting_perm_gen) { jrose@1424: // If we are collecting from class statics, but we are not going to jrose@1424: // visit all of the CodeCache, collect from the non-perm roots if any. jrose@1424: // This makes the code cache function temporarily as a source of strong jrose@1424: // roots for oops, until the next major collection. jrose@1424: // jrose@1424: // If collecting_perm_gen is true, we require that this phase will call jrose@1424: // CodeCache::do_unloading. This will kill off nmethods with expired jrose@1424: // weak references, such as stale invokedynamic targets. jrose@1424: CodeCache::scavenge_root_nmethods_do(code_roots); jrose@1424: } jrose@1424: } duke@435: // Verify if the code cache contents are in the perm gen jrose@1424: NOT_PRODUCT(CodeBlobToOopClosure assert_code_is_perm(&assert_is_perm_closure, /*do_marking=*/ false)); jrose@1424: NOT_PRODUCT(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_perm)); duke@435: } duke@435: duke@435: // Roots that should point only into permanent generation. duke@435: { duke@435: OopClosure* blk = NULL; duke@435: if (collecting_perm_gen) { duke@435: blk = roots; duke@435: } else { duke@435: debug_only(blk = &assert_is_perm_closure); duke@435: } duke@435: if (blk != NULL) { duke@435: if (!_process_strong_tasks->is_task_claimed(SH_PS_vmSymbols_oops_do)) duke@435: vmSymbols::oops_do(blk); duke@435: } duke@435: } duke@435: duke@435: if (!collecting_perm_gen) { duke@435: // All threads perform this; coordination is handled internally. duke@435: duke@435: rem_set()->younger_refs_iterate(perm_gen(), perm_blk); duke@435: } duke@435: _process_strong_tasks->all_tasks_completed(); duke@435: } duke@435: duke@435: class AlwaysTrueClosure: public BoolObjectClosure { duke@435: public: duke@435: void do_object(oop p) { ShouldNotReachHere(); } duke@435: bool do_object_b(oop p) { return true; } duke@435: }; duke@435: static AlwaysTrueClosure always_true; duke@435: duke@435: class SkipAdjustingSharedStrings: public OopClosure { duke@435: OopClosure* _clo; duke@435: public: duke@435: SkipAdjustingSharedStrings(OopClosure* clo) : _clo(clo) {} duke@435: coleenp@548: virtual void do_oop(oop* p) { duke@435: oop o = (*p); duke@435: if (!o->is_shared_readwrite()) { duke@435: _clo->do_oop(p); duke@435: } duke@435: } coleenp@548: virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); } duke@435: }; duke@435: duke@435: // Unmarked shared Strings in the StringTable (which got there due to duke@435: // being in the constant pools of as-yet unloaded shared classes) were duke@435: // not marked and therefore did not have their mark words preserved. duke@435: // These entries are also deliberately not purged from the string duke@435: // table during unloading of unmarked strings. If an identity hash duke@435: // code was computed for any of these objects, it will not have been duke@435: // cleared to zero during the forwarding process or by the duke@435: // RecursiveAdjustSharedObjectClosure, and will be confused by the duke@435: // adjusting process as a forwarding pointer. We need to skip duke@435: // forwarding StringTable entries which contain unmarked shared duke@435: // Strings. Actually, since shared strings won't be moving, we can duke@435: // just skip adjusting any shared entries in the string table. duke@435: duke@435: void SharedHeap::process_weak_roots(OopClosure* root_closure, jrose@1424: CodeBlobClosure* code_roots, duke@435: OopClosure* non_root_closure) { duke@435: // Global (weak) JNI handles duke@435: JNIHandles::weak_oops_do(&always_true, root_closure); duke@435: jrose@1424: CodeCache::blobs_do(code_roots); duke@435: SymbolTable::oops_do(root_closure); duke@435: if (UseSharedSpaces && !DumpSharedSpaces) { duke@435: SkipAdjustingSharedStrings skip_closure(root_closure); duke@435: StringTable::oops_do(&skip_closure); duke@435: } else { duke@435: StringTable::oops_do(root_closure); duke@435: } duke@435: } 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() { duke@435: ref_processing_init(); duke@435: } duke@435: duke@435: void SharedHeap::ref_processing_init() { duke@435: perm_gen()->ref_processor_init(); duke@435: } 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: }