sharedHeap.cpp revision 1753
2N/A/*
2N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2N/A *
2N/A * This code is free software; you can redistribute it and/or modify it
2N/A * under the terms of the GNU General Public License version 2 only, as
2N/A * published by the Free Software Foundation.
2N/A *
2N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2N/A * version 2 for more details (a copy is included in the LICENSE file that
2N/A * accompanied this code).
2N/A *
2N/A * You should have received a copy of the GNU General Public License version
2N/A * 2 along with this work; if not, write to the Free Software Foundation,
2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2N/A *
2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2N/A * or visit www.oracle.com if you need additional information or have any
2N/A * questions.
2N/A *
2N/A */
2N/A
2N/A# include "incls/_precompiled.incl"
2N/A# include "incls/_sharedHeap.cpp.incl"
2N/A
2N/ASharedHeap* SharedHeap::_sh;
2N/A
2N/A// The set of potentially parallel tasks in strong root scanning.
2N/Aenum SH_process_strong_roots_tasks {
2N/A SH_PS_Universe_oops_do,
2N/A SH_PS_JNIHandles_oops_do,
2N/A SH_PS_ObjectSynchronizer_oops_do,
2N/A SH_PS_FlatProfiler_oops_do,
2N/A SH_PS_Management_oops_do,
2N/A SH_PS_SystemDictionary_oops_do,
2N/A SH_PS_jvmti_oops_do,
2N/A SH_PS_vmSymbols_oops_do,
2N/A SH_PS_SymbolTable_oops_do,
2N/A SH_PS_StringTable_oops_do,
2N/A SH_PS_CodeCache_oops_do,
2N/A // Leave this one last.
2N/A SH_PS_NumElements
2N/A};
2N/A
2N/ASharedHeap::SharedHeap(CollectorPolicy* policy_) :
2N/A CollectedHeap(),
2N/A _collector_policy(policy_),
2N/A _perm_gen(NULL), _rem_set(NULL),
2N/A _strong_roots_parity(0),
2N/A _process_strong_tasks(new SubTasksDone(SH_PS_NumElements)),
2N/A _n_par_threads(0),
2N/A _workers(NULL)
2N/A{
2N/A if (_process_strong_tasks == NULL || !_process_strong_tasks->valid()) {
2N/A vm_exit_during_initialization("Failed necessary allocation.");
2N/A }
2N/A _sh = this; // ch is static, should be set only once.
2N/A if ((UseParNewGC ||
2N/A (UseConcMarkSweepGC && CMSParallelRemarkEnabled) ||
2N/A UseG1GC) &&
2N/A ParallelGCThreads > 0) {
2N/A _workers = new FlexibleWorkGang("Parallel GC Threads", ParallelGCThreads,
2N/A /* are_GC_task_threads */true,
2N/A /* are_ConcurrentGC_threads */false);
2N/A if (_workers == NULL) {
2N/A vm_exit_during_initialization("Failed necessary allocation.");
2N/A } else {
2N/A _workers->initialize_workers();
2N/A }
2N/A }
2N/A}
2N/A
2N/Abool SharedHeap::heap_lock_held_for_gc() {
2N/A Thread* t = Thread::current();
2N/A return Heap_lock->owned_by_self()
2N/A || ( (t->is_GC_task_thread() || t->is_VM_thread())
2N/A && _thread_holds_heap_lock_for_gc);
2N/A}
2N/A
2N/Avoid SharedHeap::set_par_threads(int t) {
2N/A assert(t == 0 || !UseSerialGC, "Cannot have parallel threads");
2N/A _n_par_threads = t;
2N/A _process_strong_tasks->set_n_threads(t);
2N/A}
2N/A
2N/Aclass AssertIsPermClosure: public OopClosure {
2N/Apublic:
2N/A virtual void do_oop(oop* p) {
2N/A assert((*p) == NULL || (*p)->is_perm(), "Referent should be perm.");
2N/A }
2N/A virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2N/A};
2N/Astatic AssertIsPermClosure assert_is_perm_closure;
2N/A
2N/Avoid SharedHeap::change_strong_roots_parity() {
2N/A // Also set the new collection parity.
2N/A assert(_strong_roots_parity >= 0 && _strong_roots_parity <= 2,
2N/A "Not in range.");
2N/A _strong_roots_parity++;
2N/A if (_strong_roots_parity == 3) _strong_roots_parity = 1;
2N/A assert(_strong_roots_parity >= 1 && _strong_roots_parity <= 2,
2N/A "Not in range.");
2N/A}
2N/A
2N/ASharedHeap::StrongRootsScope::StrongRootsScope(SharedHeap* outer, bool activate)
2N/A : MarkScope(activate)
2N/A{
2N/A if (_active) {
2N/A outer->change_strong_roots_parity();
2N/A }
2N/A}
2N/A
2N/ASharedHeap::StrongRootsScope::~StrongRootsScope() {
2N/A // nothing particular
2N/A}
2N/A
2N/Avoid SharedHeap::process_strong_roots(bool activate_scope,
2N/A bool collecting_perm_gen,
2N/A ScanningOption so,
2N/A OopClosure* roots,
2N/A CodeBlobClosure* code_roots,
2N/A OopsInGenClosure* perm_blk) {
2N/A StrongRootsScope srs(this, activate_scope);
2N/A // General strong roots.
2N/A assert(_strong_roots_parity != 0, "must have called prologue code");
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_Universe_oops_do)) {
2N/A Universe::oops_do(roots);
2N/A ReferenceProcessor::oops_do(roots);
2N/A // Consider perm-gen discovered lists to be strong.
2N/A perm_gen()->ref_processor()->weak_oops_do(roots);
2N/A }
2N/A // Global (strong) JNI handles
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_JNIHandles_oops_do))
2N/A JNIHandles::oops_do(roots);
2N/A // All threads execute this; the individual threads are task groups.
2N/A if (ParallelGCThreads > 0) {
2N/A Threads::possibly_parallel_oops_do(roots, code_roots);
2N/A } else {
2N/A Threads::oops_do(roots, code_roots);
2N/A }
2N/A if (!_process_strong_tasks-> is_task_claimed(SH_PS_ObjectSynchronizer_oops_do))
2N/A ObjectSynchronizer::oops_do(roots);
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_FlatProfiler_oops_do))
2N/A FlatProfiler::oops_do(roots);
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_Management_oops_do))
2N/A Management::oops_do(roots);
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_jvmti_oops_do))
2N/A JvmtiExport::oops_do(roots);
2N/A
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_SystemDictionary_oops_do)) {
2N/A if (so & SO_AllClasses) {
2N/A SystemDictionary::oops_do(roots);
2N/A } else
2N/A if (so & SO_SystemClasses) {
2N/A SystemDictionary::always_strong_oops_do(roots);
2N/A }
2N/A }
2N/A
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_SymbolTable_oops_do)) {
2N/A if (so & SO_Symbols) {
2N/A SymbolTable::oops_do(roots);
2N/A }
2N/A // Verify if the symbol table contents are in the perm gen
2N/A NOT_PRODUCT(SymbolTable::oops_do(&assert_is_perm_closure));
2N/A }
2N/A
2N/A if (!_process_strong_tasks->is_task_claimed(SH_PS_StringTable_oops_do)) {
2N/A if (so & SO_Strings) {
2N/A StringTable::oops_do(roots);
2N/A }
2N/A // Verify if the string table contents are in the perm gen
2N/A NOT_PRODUCT(StringTable::oops_do(&assert_is_perm_closure));
2N/A }
if (!_process_strong_tasks->is_task_claimed(SH_PS_CodeCache_oops_do)) {
if (so & SO_CodeCache) {
// (Currently, CMSCollector uses this to do intermediate-strength collections.)
assert(collecting_perm_gen, "scanning all of code cache");
assert(code_roots != NULL, "must supply closure for code cache");
if (code_roots != NULL) {
CodeCache::blobs_do(code_roots);
}
} else if (so & (SO_SystemClasses|SO_AllClasses)) {
if (!collecting_perm_gen) {
// If we are collecting from class statics, but we are not going to
// visit all of the CodeCache, collect from the non-perm roots if any.
// This makes the code cache function temporarily as a source of strong
// roots for oops, until the next major collection.
//
// If collecting_perm_gen is true, we require that this phase will call
// CodeCache::do_unloading. This will kill off nmethods with expired
// weak references, such as stale invokedynamic targets.
CodeCache::scavenge_root_nmethods_do(code_roots);
}
}
// Verify if the code cache contents are in the perm gen
NOT_PRODUCT(CodeBlobToOopClosure assert_code_is_perm(&assert_is_perm_closure, /*do_marking=*/ false));
NOT_PRODUCT(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_perm));
}
// Roots that should point only into permanent generation.
{
OopClosure* blk = NULL;
if (collecting_perm_gen) {
blk = roots;
} else {
debug_only(blk = &assert_is_perm_closure);
}
if (blk != NULL) {
if (!_process_strong_tasks->is_task_claimed(SH_PS_vmSymbols_oops_do))
vmSymbols::oops_do(blk);
}
}
if (!collecting_perm_gen) {
// All threads perform this; coordination is handled internally.
rem_set()->younger_refs_iterate(perm_gen(), perm_blk);
}
_process_strong_tasks->all_tasks_completed();
}
class AlwaysTrueClosure: public BoolObjectClosure {
public:
void do_object(oop p) { ShouldNotReachHere(); }
bool do_object_b(oop p) { return true; }
};
static AlwaysTrueClosure always_true;
class SkipAdjustingSharedStrings: public OopClosure {
OopClosure* _clo;
public:
SkipAdjustingSharedStrings(OopClosure* clo) : _clo(clo) {}
virtual void do_oop(oop* p) {
oop o = (*p);
if (!o->is_shared_readwrite()) {
_clo->do_oop(p);
}
}
virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
};
// Unmarked shared Strings in the StringTable (which got there due to
// being in the constant pools of as-yet unloaded shared classes) were
// not marked and therefore did not have their mark words preserved.
// These entries are also deliberately not purged from the string
// table during unloading of unmarked strings. If an identity hash
// code was computed for any of these objects, it will not have been
// cleared to zero during the forwarding process or by the
// RecursiveAdjustSharedObjectClosure, and will be confused by the
// adjusting process as a forwarding pointer. We need to skip
// forwarding StringTable entries which contain unmarked shared
// Strings. Actually, since shared strings won't be moving, we can
// just skip adjusting any shared entries in the string table.
void SharedHeap::process_weak_roots(OopClosure* root_closure,
CodeBlobClosure* code_roots,
OopClosure* non_root_closure) {
// Global (weak) JNI handles
JNIHandles::weak_oops_do(&always_true, root_closure);
CodeCache::blobs_do(code_roots);
SymbolTable::oops_do(root_closure);
if (UseSharedSpaces && !DumpSharedSpaces) {
SkipAdjustingSharedStrings skip_closure(root_closure);
StringTable::oops_do(&skip_closure);
} else {
StringTable::oops_do(root_closure);
}
}
void SharedHeap::set_barrier_set(BarrierSet* bs) {
_barrier_set = bs;
// Cached barrier set for fast access in oops
oopDesc::set_bs(bs);
}
void SharedHeap::post_initialize() {
ref_processing_init();
}
void SharedHeap::ref_processing_init() {
perm_gen()->ref_processor_init();
}
// Some utilities.
void SharedHeap::print_size_transition(outputStream* out,
size_t bytes_before,
size_t bytes_after,
size_t capacity) {
out->print(" %d%s->%d%s(%d%s)",
byte_size_in_proper_unit(bytes_before),
proper_unit_for_byte_size(bytes_before),
byte_size_in_proper_unit(bytes_after),
proper_unit_for_byte_size(bytes_after),
byte_size_in_proper_unit(capacity),
proper_unit_for_byte_size(capacity));
}