compactingPermGenGen.cpp revision 47
6443N/A/*
6443N/A * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
6443N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6443N/A *
6443N/A * This code is free software; you can redistribute it and/or modify it
6443N/A * under the terms of the GNU General Public License version 2 only, as
6443N/A * published by the Free Software Foundation.
6443N/A *
6443N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6443N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6443N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6443N/A * version 2 for more details (a copy is included in the LICENSE file that
6443N/A * accompanied this code).
6443N/A *
6443N/A * You should have received a copy of the GNU General Public License version
6443N/A * 2 along with this work; if not, write to the Free Software Foundation,
6443N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6443N/A *
6443N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
6443N/A * CA 95054 USA or visit www.sun.com if you need additional information or
6443N/A * have any questions.
7070N/A *
6443N/A */
6443N/A
6443N/A#include "incls/_precompiled.incl"
7255N/A#include "incls/_compactingPermGenGen.cpp.incl"
7255N/A
7255N/A
7255N/A// An ObjectClosure helper: Recursively adjust all pointers in an object
7255N/A// and all objects by referenced it. Clear marks on objects in order to
6443N/A// prevent visiting any object twice. This helper is used when the
6443N/A// RedefineClasses() API has been called.
6443N/A
6443N/Aclass AdjustSharedObjectClosure : public ObjectClosure {
6443N/Apublic:
6443N/A void do_object(oop obj) {
6443N/A if (obj->is_shared_readwrite()) {
6443N/A if (obj->mark()->is_marked()) {
6443N/A obj->init_mark(); // Don't revisit this object.
6443N/A obj->adjust_pointers(); // Adjust this object's references.
6443N/A }
6443N/A }
6443N/A }
6443N/A};
6443N/A
7255N/A
7255N/A// An OopClosure helper: Recursively adjust all pointers in an object
7255N/A// and all objects by referenced it. Clear marks on objects in order
7255N/A// to prevent visiting any object twice.
7255N/A
6443N/Aclass RecursiveAdjustSharedObjectClosure : public OopClosure {
7255N/Apublic:
7255N/A void do_oop(oop* o) {
7255N/A oop obj = *o;
7255N/A if (obj->is_shared_readwrite()) {
7255N/A if (obj->mark()->is_marked()) {
7255N/A obj->init_mark(); // Don't revisit this object.
7255N/A obj->oop_iterate(this); // Recurse - adjust objects referenced.
7255N/A obj->adjust_pointers(); // Adjust this object's references.
7255N/A
7255N/A // Special case: if a class has a read-only constant pool,
7255N/A // then the read-write objects referenced by the pool must
7255N/A // have their marks reset.
7255N/A
7255N/A if (obj->klass() == Universe::instanceKlassKlassObj()) {
7255N/A instanceKlass* ik = instanceKlass::cast((klassOop)obj);
7255N/A constantPoolOop cp = ik->constants();
7255N/A if (cp->is_shared_readonly()) {
7255N/A cp->oop_iterate(this);
7255N/A }
7255N/A }
7255N/A }
7255N/A }
6443N/A };
7255N/A};
6443N/A
7255N/A
7255N/A// We need to go through all placeholders in the system dictionary and
7255N/A// try to resolve them into shared classes. Other threads might be in
7255N/A// the process of loading a shared class and have strong roots on
7255N/A// their stack to the class without having added the class to the
7255N/A// dictionary yet. This means the class will be marked during phase 1
7255N/A// but will not be unmarked during the application of the
7255N/A// RecursiveAdjustSharedObjectClosure to the SystemDictionary. Note
7255N/A// that we must not call find_shared_class with non-read-only symbols
7255N/A// as doing so can cause hash codes to be computed, destroying
7255N/A// forwarding pointers.
7255N/Aclass TraversePlaceholdersClosure : public OopClosure {
7255N/A public:
7255N/A void do_oop(oop* o) {
7255N/A oop obj = *o;
7255N/A if (obj->klass() == Universe::symbolKlassObj() &&
7255N/A obj->is_shared_readonly()) {
7255N/A symbolHandle sym((symbolOop) obj);
7255N/A oop k = SystemDictionary::find_shared_class(sym);
7255N/A if (k != NULL) {
6443N/A RecursiveAdjustSharedObjectClosure clo;
6443N/A clo.do_oop(&k);
7255N/A }
7255N/A }
7255N/A }
7255N/A};
7255N/A
7255N/A
7255N/Avoid CompactingPermGenGen::initialize_performance_counters() {
7255N/A
7255N/A const char* gen_name = "perm";
7255N/A
7255N/A // Generation Counters - generation 2, 1 subspace
7255N/A _gen_counters = new GenerationCounters(gen_name, 2, 1, &_virtual_space);
7255N/A
7255N/A _space_counters = new CSpaceCounters(gen_name, 0,
7255N/A _virtual_space.reserved_size(),
7255N/A _the_space, _gen_counters);
7255N/A}
7255N/A
7255N/Avoid CompactingPermGenGen::update_counters() {
7255N/A if (UsePerfData) {
7255N/A _space_counters->update_all();
6443N/A _gen_counters->update_all();
7255N/A }
6443N/A}
7255N/A
7255N/A
7255N/ACompactingPermGenGen::CompactingPermGenGen(ReservedSpace rs,
7255N/A ReservedSpace shared_rs,
7255N/A size_t initial_byte_size,
7255N/A int level, GenRemSet* remset,
7255N/A ContiguousSpace* space,
7255N/A PermanentGenerationSpec* spec_) :
7255N/A OneContigSpaceCardGeneration(rs, initial_byte_size, MinPermHeapExpansion,
7255N/A level, remset, space) {
7255N/A
7255N/A set_spec(spec_);
7255N/A if (!UseSharedSpaces && !DumpSharedSpaces) {
7255N/A spec()->disable_sharing();
7255N/A }
7255N/A
7255N/A // Break virtual space into address ranges for all spaces.
7255N/A
7255N/A if (spec()->enable_shared_spaces()) {
7255N/A shared_end = (HeapWord*)(shared_rs.base() + shared_rs.size());
7255N/A misccode_end = shared_end;
6443N/A misccode_bottom = misccode_end - heap_word_size(spec()->misc_code_size());
7255N/A miscdata_end = misccode_bottom;
6443N/A miscdata_bottom = miscdata_end - heap_word_size(spec()->misc_data_size());
7255N/A readwrite_end = miscdata_bottom;
7255N/A readwrite_bottom =
7255N/A readwrite_end - heap_word_size(spec()->read_write_size());
7255N/A readonly_end = readwrite_bottom;
7255N/A readonly_bottom =
7255N/A readonly_end - heap_word_size(spec()->read_only_size());
7255N/A shared_bottom = readonly_bottom;
7255N/A unshared_end = shared_bottom;
6443N/A assert((char*)shared_bottom == shared_rs.base(), "shared space mismatch");
6443N/A } else {
6443N/A shared_end = (HeapWord*)(rs.base() + rs.size());
6443N/A misccode_end = shared_end;
6443N/A misccode_bottom = shared_end;
6443N/A miscdata_end = shared_end;
6443N/A miscdata_bottom = shared_end;
7097N/A readwrite_end = shared_end;
7097N/A readwrite_bottom = shared_end;
7097N/A readonly_end = shared_end;
7097N/A readonly_bottom = shared_end;
6443N/A shared_bottom = shared_end;
6443N/A unshared_end = shared_bottom;
7097N/A }
6443N/A unshared_bottom = (HeapWord*) rs.base();
7097N/A
7097N/A // Verify shared and unshared spaces adjacent.
7097N/A assert((char*)shared_bottom == rs.base()+rs.size(), "shared space mismatch");
7097N/A assert(unshared_end > unshared_bottom, "shared space mismatch");
7097N/A
7097N/A // Split reserved memory into pieces.
7097N/A
7097N/A ReservedSpace ro_rs = shared_rs.first_part(spec()->read_only_size(),
6443N/A UseSharedSpaces);
6443N/A ReservedSpace tmp_rs1 = shared_rs.last_part(spec()->read_only_size());
6443N/A ReservedSpace rw_rs = tmp_rs1.first_part(spec()->read_write_size(),
6443N/A UseSharedSpaces);
7097N/A ReservedSpace tmp_rs2 = tmp_rs1.last_part(spec()->read_write_size());
7097N/A ReservedSpace md_rs = tmp_rs2.first_part(spec()->misc_data_size(),
6443N/A UseSharedSpaces);
7097N/A ReservedSpace mc_rs = tmp_rs2.last_part(spec()->misc_data_size());
7097N/A
6443N/A _shared_space_size = spec()->read_only_size()
6443N/A + spec()->read_write_size()
6443N/A + spec()->misc_data_size()
6443N/A + spec()->misc_code_size();
6443N/A
6443N/A // Allocate the unshared (default) space.
6443N/A _the_space = new ContigPermSpace(_bts,
6443N/A MemRegion(unshared_bottom, heap_word_size(initial_byte_size)));
7097N/A if (_the_space == NULL)
7097N/A vm_exit_during_initialization("Could not allocate an unshared"
7097N/A " CompactingPermGen Space");
7097N/A
6443N/A // Allocate shared spaces
6443N/A if (spec()->enable_shared_spaces()) {
6443N/A
6443N/A // If mapping a shared file, the space is not committed, don't
6443N/A // mangle.
6443N/A NOT_PRODUCT(bool old_ZapUnusedHeapArea = ZapUnusedHeapArea;)
6443N/A NOT_PRODUCT(if (UseSharedSpaces) ZapUnusedHeapArea = false;)
6443N/A
6443N/A // Commit the memory behind the shared spaces if dumping (not
7097N/A // mapping).
6443N/A if (DumpSharedSpaces) {
7097N/A _ro_vs.initialize(ro_rs, spec()->read_only_size());
7097N/A _rw_vs.initialize(rw_rs, spec()->read_write_size());
7097N/A _md_vs.initialize(md_rs, spec()->misc_data_size());
7097N/A _mc_vs.initialize(mc_rs, spec()->misc_code_size());
7097N/A }
7097N/A
7097N/A // Allocate the shared spaces.
6443N/A _ro_bts = new BlockOffsetSharedArray(
6443N/A MemRegion(readonly_bottom,
7097N/A heap_word_size(spec()->read_only_size())),
7097N/A heap_word_size(spec()->read_only_size()));
6443N/A _ro_space = new OffsetTableContigSpace(_ro_bts,
7097N/A MemRegion(readonly_bottom, readonly_end));
6443N/A _rw_bts = new BlockOffsetSharedArray(
6443N/A MemRegion(readwrite_bottom,
7097N/A heap_word_size(spec()->read_write_size())),
7097N/A heap_word_size(spec()->read_write_size()));
7097N/A _rw_space = new OffsetTableContigSpace(_rw_bts,
7097N/A MemRegion(readwrite_bottom, readwrite_end));
6443N/A
7097N/A // Restore mangling flag.
7097N/A NOT_PRODUCT(ZapUnusedHeapArea = old_ZapUnusedHeapArea;)
6443N/A
6443N/A if (_ro_space == NULL || _rw_space == NULL)
6443N/A vm_exit_during_initialization("Could not allocate a shared space");
6443N/A
6443N/A // Cover both shared spaces entirely with cards.
6443N/A _rs->resize_covered_region(MemRegion(readonly_bottom, readwrite_end));
6443N/A
6443N/A if (UseSharedSpaces) {
6443N/A
6443N/A // Map in the regions in the shared file.
6443N/A FileMapInfo* mapinfo = FileMapInfo::current_info();
6443N/A size_t image_alignment = mapinfo->alignment();
6443N/A CollectedHeap* ch = Universe::heap();
6443N/A if ((!mapinfo->map_space(ro, ro_rs, _ro_space)) ||
7070N/A (!mapinfo->map_space(rw, rw_rs, _rw_space)) ||
7070N/A (!mapinfo->map_space(md, md_rs, NULL)) ||
7070N/A (!mapinfo->map_space(mc, mc_rs, NULL)) ||
7070N/A // check the alignment constraints
7070N/A (ch == NULL || ch->kind() != CollectedHeap::GenCollectedHeap ||
7070N/A image_alignment !=
7070N/A ((GenCollectedHeap*)ch)->gen_policy()->max_alignment())) {
7070N/A // Base addresses didn't match; skip sharing, but continue
7070N/A shared_rs.release();
7070N/A spec()->disable_sharing();
7070N/A // If -Xshare:on is specified, print out the error message and exit VM,
6443N/A // otherwise, set UseSharedSpaces to false and continue.
6443N/A if (RequireSharedSpaces) {
6443N/A vm_exit_during_initialization("Unable to use shared archive.", NULL);
6443N/A } else {
7097N/A FLAG_SET_DEFAULT(UseSharedSpaces, false);
7097N/A }
7097N/A
7097N/A // Note: freeing the block offset array objects does not
7097N/A // currently free up the underlying storage.
7097N/A delete _ro_bts;
7097N/A _ro_bts = NULL;
7097N/A delete _ro_space;
7097N/A _ro_space = NULL;
7097N/A delete _rw_bts;
7097N/A _rw_bts = NULL;
6443N/A delete _rw_space;
6443N/A _rw_space = NULL;
6443N/A shared_end = (HeapWord*)(rs.base() + rs.size());
6443N/A _rs->resize_covered_region(MemRegion(shared_bottom, shared_bottom));
6443N/A }
6443N/A }
6443N/A
6443N/A // Reserved region includes shared spaces for oop.is_in_reserved().
6443N/A _reserved.set_end(shared_end);
6443N/A
6443N/A } else {
6443N/A _ro_space = NULL;
6443N/A _rw_space = NULL;
6443N/A }
7097N/A}
7097N/A
7097N/A
7097N/A// Do a complete scan of the shared read write space to catch all
7097N/A// objects which contain references to any younger generation. Forward
7097N/A// the pointers. Avoid space_iterate, as actually visiting all the
7097N/A// objects in the space will page in more objects than we need.
7097N/A// Instead, use the system dictionary as strong roots into the read
7097N/A// write space.
7097N/A//
7097N/A// If a RedefineClasses() call has been made, then we have to iterate
7097N/A// over the entire shared read-write space in order to find all the
6443N/A// objects that need to be forwarded. For example, it is possible for
6443N/A// an nmethod to be found and marked in GC phase-1 only for the nmethod
6443N/A// to be freed by the time we reach GC phase-3. The underlying method
6443N/A// is still marked, but we can't (easily) find it in GC phase-3 so we
6443N/A// blow up in GC phase-4. With RedefineClasses() we want replaced code
// (EMCP or obsolete) to go away (i.e., be collectible) once it is no
// longer being executed by any thread so we keep minimal attachments
// to the replaced code. However, we can't guarantee when those EMCP
// or obsolete methods will be collected so they may still be out there
// even after we've severed our minimal attachments.
void CompactingPermGenGen::pre_adjust_pointers() {
if (spec()->enable_shared_spaces()) {
if (JvmtiExport::has_redefined_a_class()) {
// RedefineClasses() requires a brute force approach
AdjustSharedObjectClosure blk;
rw_space()->object_iterate(&blk);
} else {
RecursiveAdjustSharedObjectClosure blk;
Universe::oops_do(&blk);
StringTable::oops_do(&blk);
SystemDictionary::always_strong_classes_do(&blk);
TraversePlaceholdersClosure tpc;
SystemDictionary::placeholders_do(&tpc);
}
}
}
#ifdef ASSERT
class VerifyMarksClearedClosure : public ObjectClosure {
public:
void do_object(oop obj) {
assert(SharedSkipVerify || !obj->mark()->is_marked(),
"Shared oop still marked?");
}
};
#endif
void CompactingPermGenGen::post_compact() {
#ifdef ASSERT
if (!SharedSkipVerify && spec()->enable_shared_spaces()) {
VerifyMarksClearedClosure blk;
rw_space()->object_iterate(&blk);
}
#endif
}
void CompactingPermGenGen::space_iterate(SpaceClosure* blk, bool usedOnly) {
OneContigSpaceCardGeneration::space_iterate(blk, usedOnly);
if (spec()->enable_shared_spaces()) {
#ifdef PRODUCT
// Making the rw_space walkable will page in the entire space, and
// is to be avoided. However, this is required for Verify options.
ShouldNotReachHere();
#endif
blk->do_space(ro_space());
blk->do_space(rw_space());
}
}
void CompactingPermGenGen::print_on(outputStream* st) const {
OneContigSpaceCardGeneration::print_on(st);
if (spec()->enable_shared_spaces()) {
st->print(" ro");
ro_space()->print_on(st);
st->print(" rw");
rw_space()->print_on(st);
} else {
st->print_cr("No shared spaces configured.");
}
}
// References from the perm gen to the younger generation objects may
// occur in static fields in Java classes or in constant pool references
// to String objects.
void CompactingPermGenGen::younger_refs_iterate(OopsInGenClosure* blk) {
OneContigSpaceCardGeneration::younger_refs_iterate(blk);
if (spec()->enable_shared_spaces()) {
blk->set_generation(this);
// ro_space has no younger gen refs.
_rs->younger_refs_in_space_iterate(rw_space(), blk);
blk->reset_generation();
}
}
// Shared spaces are addressed in pre_adjust_pointers.
void CompactingPermGenGen::adjust_pointers() {
the_space()->adjust_pointers();
}
void CompactingPermGenGen::compact() {
the_space()->compact();
}
size_t CompactingPermGenGen::contiguous_available() const {
// Don't include shared spaces.
return OneContigSpaceCardGeneration::contiguous_available()
- _shared_space_size;
}
size_t CompactingPermGenGen::max_capacity() const {
// Don't include shared spaces.
assert(UseSharedSpaces || (_shared_space_size == 0),
"If not used, the size of shared spaces should be 0");
return OneContigSpaceCardGeneration::max_capacity()
- _shared_space_size;
}
bool CompactingPermGenGen::grow_by(size_t bytes) {
// Don't allow _virtual_size to expand into shared spaces.
size_t max_bytes = _virtual_space.uncommitted_size() - _shared_space_size;
if (bytes > _shared_space_size) {
bytes = _shared_space_size;
}
return OneContigSpaceCardGeneration::grow_by(bytes);
}
void CompactingPermGenGen::grow_to_reserved() {
// Don't allow _virtual_size to expand into shared spaces.
if (_virtual_space.uncommitted_size() > _shared_space_size) {
size_t remaining_bytes =
_virtual_space.uncommitted_size() - _shared_space_size;
bool success = OneContigSpaceCardGeneration::grow_by(remaining_bytes);
DEBUG_ONLY(if (!success) warning("grow to reserved failed");)
}
}
// No young generation references, clear this generation's main space's
// card table entries. Do NOT clear the card table entries for the
// read-only space (always clear) or the read-write space (valuable
// information).
void CompactingPermGenGen::clear_remembered_set() {
_rs->clear(MemRegion(the_space()->bottom(), the_space()->end()));
}
// Objects in this generation's main space may have moved, invalidate
// that space's cards. Do NOT invalidate the card table entries for the
// read-only or read-write spaces, as those objects never move.
void CompactingPermGenGen::invalidate_remembered_set() {
_rs->invalidate(used_region());
}
void CompactingPermGenGen::verify(bool allow_dirty) {
the_space()->verify(allow_dirty);
if (!SharedSkipVerify && spec()->enable_shared_spaces()) {
ro_space()->verify(allow_dirty);
rw_space()->verify(allow_dirty);
}
}
HeapWord* CompactingPermGenGen::unshared_bottom;
HeapWord* CompactingPermGenGen::unshared_end;
HeapWord* CompactingPermGenGen::shared_bottom;
HeapWord* CompactingPermGenGen::shared_end;
HeapWord* CompactingPermGenGen::readonly_bottom;
HeapWord* CompactingPermGenGen::readonly_end;
HeapWord* CompactingPermGenGen::readwrite_bottom;
HeapWord* CompactingPermGenGen::readwrite_end;
HeapWord* CompactingPermGenGen::miscdata_bottom;
HeapWord* CompactingPermGenGen::miscdata_end;
HeapWord* CompactingPermGenGen::misccode_bottom;
HeapWord* CompactingPermGenGen::misccode_end;
// JVM/TI RedefineClasses() support:
bool CompactingPermGenGen::remap_shared_readonly_as_readwrite() {
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
if (UseSharedSpaces) {
// remap the shared readonly space to shared readwrite, private
FileMapInfo* mapinfo = FileMapInfo::current_info();
if (!mapinfo->remap_shared_readonly_as_readwrite()) {
return false;
}
}
return true;
}
void** CompactingPermGenGen::_vtbl_list;