duke@435: /* duke@435: * Copyright 2000-2007 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: duke@435: void SharedHeap::process_strong_roots(bool collecting_perm_gen, duke@435: ScanningOption so, duke@435: OopClosure* roots, duke@435: OopsInGenClosure* perm_blk) { duke@435: // General strong roots. duke@435: if (n_par_threads() == 0) change_strong_roots_parity(); 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) { duke@435: Threads::possibly_parallel_oops_do(roots); duke@435: } else { duke@435: Threads::oops_do(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)) { duke@435: if (so & SO_CodeCache) { duke@435: CodeCache::oops_do(roots); duke@435: } duke@435: // Verify if the code cache contents are in the perm gen duke@435: NOT_PRODUCT(CodeCache::oops_do(&assert_is_perm_closure)); 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, duke@435: OopClosure* non_root_closure) { duke@435: // Global (weak) JNI handles duke@435: JNIHandles::weak_oops_do(&always_true, root_closure); duke@435: duke@435: CodeCache::oops_do(non_root_closure); 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: void SharedHeap::fill_region_with_object(MemRegion mr) { duke@435: // Disable the posting of JVMTI VMObjectAlloc events as we duke@435: // don't want the filling of tlabs with filler arrays to be duke@435: // reported to the profiler. duke@435: NoJvmtiVMObjectAllocMark njm; duke@435: duke@435: // Disable low memory detector because there is no real allocation. duke@435: LowMemoryDetectorDisabler lmd_dis; duke@435: duke@435: // It turns out that post_allocation_setup_array takes a handle, so the duke@435: // call below contains an implicit conversion. Best to free that handle duke@435: // as soon as possible. duke@435: HandleMark hm; duke@435: duke@435: size_t word_size = mr.word_size(); duke@435: size_t aligned_array_header_size = duke@435: align_object_size(typeArrayOopDesc::header_size(T_INT)); duke@435: duke@435: if (word_size >= aligned_array_header_size) { duke@435: const size_t array_length = duke@435: pointer_delta(mr.end(), mr.start()) - duke@435: typeArrayOopDesc::header_size(T_INT); duke@435: const size_t array_length_words = duke@435: array_length * (HeapWordSize/sizeof(jint)); duke@435: post_allocation_setup_array(Universe::intArrayKlassObj(), duke@435: mr.start(), duke@435: mr.word_size(), duke@435: (int)array_length_words); duke@435: #ifdef ASSERT duke@435: HeapWord* elt_words = (mr.start() + typeArrayOopDesc::header_size(T_INT)); duke@435: Copy::fill_to_words(elt_words, array_length, 0xDEAFBABE); duke@435: #endif duke@435: } else { duke@435: assert(word_size == (size_t)oopDesc::header_size(), "Unaligned?"); duke@435: post_allocation_setup_obj(SystemDictionary::object_klass(), duke@435: mr.start(), duke@435: mr.word_size()); duke@435: } 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: }