compactingPermGenGen.cpp revision 1879
0N/A/*
2581N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A *
0N/A */
647N/A
647N/A#include "precompiled.hpp"
2581N/A#include "classfile/symbolTable.hpp"
2922N/A#include "classfile/systemDictionary.hpp"
667N/A#include "memory/compactingPermGenGen.hpp"
1945N/A#include "memory/filemap.hpp"
647N/A#include "memory/genOopClosures.inline.hpp"
647N/A#include "memory/generation.inline.hpp"
647N/A#include "memory/generationSpec.hpp"
176N/A#include "oops/oop.inline.hpp"
176N/A#include "runtime/java.hpp"
647N/A#ifndef SERIALGC
176N/A#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
176N/A#endif
176N/A
176N/A
0N/A// An ObjectClosure helper: Recursively adjust all pointers in an object
176N/A// and all objects by referenced it. Clear marks on objects in order to
647N/A// prevent visiting any object twice. This helper is used when the
0N/A// RedefineClasses() API has been called.
647N/A
0N/Aclass AdjustSharedObjectClosure : public ObjectClosure {
0N/Apublic:
0N/A void do_object(oop obj) {
0N/A if (obj->is_shared_readwrite()) {
0N/A if (obj->mark()->is_marked()) {
647N/A obj->init_mark(); // Don't revisit this object.
0N/A obj->adjust_pointers(); // Adjust this object's references.
0N/A }
0N/A }
0N/A }
0N/A};
0N/A
0N/A
0N/A// An OopClosure helper: Recursively adjust all pointers in an object
0N/A// and all objects by referenced it. Clear marks on objects in order
0N/A// to prevent visiting any object twice.
0N/A
0N/Aclass RecursiveAdjustSharedObjectClosure : public OopClosure {
0N/A protected:
0N/A template <class T> inline void do_oop_work(T* p) {
647N/A oop obj = oopDesc::load_decode_heap_oop_not_null(p);
0N/A if (obj->is_shared_readwrite()) {
0N/A if (obj->mark()->is_marked()) {
0N/A obj->init_mark(); // Don't revisit this object.
0N/A obj->oop_iterate(this); // Recurse - adjust objects referenced.
0N/A obj->adjust_pointers(); // Adjust this object's references.
0N/A
0N/A // Special case: if a class has a read-only constant pool,
0N/A // then the read-write objects referenced by the pool must
0N/A // have their marks reset.
0N/A
0N/A if (obj->klass() == Universe::instanceKlassKlassObj()) {
0N/A instanceKlass* ik = instanceKlass::cast((klassOop)obj);
647N/A constantPoolOop cp = ik->constants();
647N/A if (cp->is_shared_readonly()) {
0N/A cp->oop_iterate(this);
0N/A }
0N/A }
0N/A }
0N/A }
647N/A }
0N/A public:
0N/A virtual void do_oop(oop* p) { RecursiveAdjustSharedObjectClosure::do_oop_work(p); }
0N/A virtual void do_oop(narrowOop* p) { RecursiveAdjustSharedObjectClosure::do_oop_work(p); }
0N/A};
0N/A
647N/A
647N/A// We need to go through all placeholders in the system dictionary and
647N/A// try to resolve them into shared classes. Other threads might be in
647N/A// the process of loading a shared class and have strong roots on
0N/A// their stack to the class without having added the class to the
0N/A// dictionary yet. This means the class will be marked during phase 1
647N/A// but will not be unmarked during the application of the
0N/A// RecursiveAdjustSharedObjectClosure to the SystemDictionary. Note
0N/A// that we must not call find_shared_class with non-read-only symbols
0N/A// as doing so can cause hash codes to be computed, destroying
0N/A// forwarding pointers.
0N/Aclass TraversePlaceholdersClosure : public OopClosure {
0N/A protected:
0N/A template <class T> inline void do_oop_work(T* p) {
0N/A oop obj = oopDesc::load_decode_heap_oop_not_null(p);
0N/A if (obj->klass() == Universe::symbolKlassObj() &&
0N/A obj->is_shared_readonly()) {
647N/A symbolHandle sym((symbolOop) obj);
0N/A oop k = SystemDictionary::find_shared_class(sym);
0N/A if (k != NULL) {
176N/A RecursiveAdjustSharedObjectClosure clo;
0N/A clo.do_oop(&k);
0N/A }
0N/A }
0N/A }
0N/A public:
0N/A virtual void do_oop(oop* p) { TraversePlaceholdersClosure::do_oop_work(p); }
0N/A virtual void do_oop(narrowOop* p) { TraversePlaceholdersClosure::do_oop_work(p); }
0N/A
0N/A};
0N/A
0N/A
0N/Avoid CompactingPermGenGen::initialize_performance_counters() {
0N/A
0N/A const char* gen_name = "perm";
0N/A
647N/A // Generation Counters - generation 2, 1 subspace
647N/A _gen_counters = new GenerationCounters(gen_name, 2, 1, &_virtual_space);
647N/A
0N/A _space_counters = new CSpaceCounters(gen_name, 0,
0N/A _virtual_space.reserved_size(),
0N/A _the_space, _gen_counters);
647N/A}
0N/A
0N/Avoid CompactingPermGenGen::update_counters() {
0N/A if (UsePerfData) {
0N/A _space_counters->update_all();
0N/A _gen_counters->update_all();
647N/A }
647N/A}
647N/A
647N/A
647N/ACompactingPermGenGen::CompactingPermGenGen(ReservedSpace rs,
647N/A ReservedSpace shared_rs,
647N/A size_t initial_byte_size,
647N/A int level, GenRemSet* remset,
647N/A ContiguousSpace* space,
647N/A PermanentGenerationSpec* spec_) :
647N/A OneContigSpaceCardGeneration(rs, initial_byte_size, MinPermHeapExpansion,
647N/A level, remset, space) {
647N/A
647N/A set_spec(spec_);
647N/A if (!UseSharedSpaces && !DumpSharedSpaces) {
647N/A spec()->disable_sharing();
647N/A }
647N/A
647N/A // Break virtual space into address ranges for all spaces.
647N/A
647N/A if (spec()->enable_shared_spaces()) {
647N/A shared_end = (HeapWord*)(shared_rs.base() + shared_rs.size());
647N/A misccode_end = shared_end;
647N/A misccode_bottom = misccode_end - heap_word_size(spec()->misc_code_size());
647N/A miscdata_end = misccode_bottom;
647N/A miscdata_bottom = miscdata_end - heap_word_size(spec()->misc_data_size());
647N/A readwrite_end = miscdata_bottom;
647N/A readwrite_bottom =
647N/A readwrite_end - heap_word_size(spec()->read_write_size());
647N/A readonly_end = readwrite_bottom;
647N/A readonly_bottom =
647N/A readonly_end - heap_word_size(spec()->read_only_size());
647N/A shared_bottom = readonly_bottom;
647N/A unshared_end = shared_bottom;
647N/A assert((char*)shared_bottom == shared_rs.base(), "shared space mismatch");
647N/A } else {
647N/A shared_end = (HeapWord*)(rs.base() + rs.size());
647N/A misccode_end = shared_end;
647N/A misccode_bottom = shared_end;
647N/A miscdata_end = shared_end;
647N/A miscdata_bottom = shared_end;
647N/A readwrite_end = shared_end;
647N/A readwrite_bottom = shared_end;
647N/A readonly_end = shared_end;
647N/A readonly_bottom = shared_end;
647N/A shared_bottom = shared_end;
647N/A unshared_end = shared_bottom;
647N/A }
647N/A unshared_bottom = (HeapWord*) rs.base();
647N/A
647N/A // Verify shared and unshared spaces adjacent.
647N/A assert((char*)shared_bottom == rs.base()+rs.size(), "shared space mismatch");
647N/A assert(unshared_end > unshared_bottom, "shared space mismatch");
647N/A
647N/A // Split reserved memory into pieces.
647N/A
647N/A ReservedSpace ro_rs = shared_rs.first_part(spec()->read_only_size(),
647N/A UseSharedSpaces);
647N/A ReservedSpace tmp_rs1 = shared_rs.last_part(spec()->read_only_size());
647N/A ReservedSpace rw_rs = tmp_rs1.first_part(spec()->read_write_size(),
647N/A UseSharedSpaces);
647N/A ReservedSpace tmp_rs2 = tmp_rs1.last_part(spec()->read_write_size());
647N/A ReservedSpace md_rs = tmp_rs2.first_part(spec()->misc_data_size(),
647N/A UseSharedSpaces);
647N/A ReservedSpace mc_rs = tmp_rs2.last_part(spec()->misc_data_size());
647N/A
647N/A _shared_space_size = spec()->read_only_size()
647N/A + spec()->read_write_size()
647N/A + spec()->misc_data_size()
647N/A + spec()->misc_code_size();
647N/A
647N/A // Allocate the unshared (default) space.
647N/A _the_space = new ContigPermSpace(_bts,
647N/A MemRegion(unshared_bottom, heap_word_size(initial_byte_size)));
647N/A if (_the_space == NULL)
647N/A vm_exit_during_initialization("Could not allocate an unshared"
647N/A " CompactingPermGen Space");
647N/A
647N/A // Allocate shared spaces
647N/A if (spec()->enable_shared_spaces()) {
647N/A
647N/A // If mapping a shared file, the space is not committed, don't
647N/A // mangle.
647N/A NOT_PRODUCT(bool old_ZapUnusedHeapArea = ZapUnusedHeapArea;)
647N/A NOT_PRODUCT(if (UseSharedSpaces) ZapUnusedHeapArea = false;)
647N/A
647N/A // Commit the memory behind the shared spaces if dumping (not
647N/A // mapping).
2581N/A if (DumpSharedSpaces) {
2581N/A _ro_vs.initialize(ro_rs, spec()->read_only_size());
2581N/A _rw_vs.initialize(rw_rs, spec()->read_write_size());
2581N/A _md_vs.initialize(md_rs, spec()->misc_data_size());
2581N/A _mc_vs.initialize(mc_rs, spec()->misc_code_size());
2581N/A }
2581N/A
2581N/A // Allocate the shared spaces.
2581N/A _ro_bts = new BlockOffsetSharedArray(
2581N/A MemRegion(readonly_bottom,
2581N/A heap_word_size(spec()->read_only_size())),
2581N/A heap_word_size(spec()->read_only_size()));
2581N/A _ro_space = new OffsetTableContigSpace(_ro_bts,
2581N/A MemRegion(readonly_bottom, readonly_end));
647N/A _rw_bts = new BlockOffsetSharedArray(
647N/A MemRegion(readwrite_bottom,
647N/A heap_word_size(spec()->read_write_size())),
647N/A heap_word_size(spec()->read_write_size()));
647N/A _rw_space = new OffsetTableContigSpace(_rw_bts,
647N/A MemRegion(readwrite_bottom, readwrite_end));
647N/A
647N/A // Restore mangling flag.
647N/A NOT_PRODUCT(ZapUnusedHeapArea = old_ZapUnusedHeapArea;)
653N/A
653N/A if (_ro_space == NULL || _rw_space == NULL)
647N/A vm_exit_during_initialization("Could not allocate a shared space");
667N/A
667N/A // Cover both shared spaces entirely with cards.
647N/A _rs->resize_covered_region(MemRegion(readonly_bottom, readwrite_end));
647N/A
647N/A if (UseSharedSpaces) {
647N/A
667N/A // Map in the regions in the shared file.
647N/A FileMapInfo* mapinfo = FileMapInfo::current_info();
647N/A size_t image_alignment = mapinfo->alignment();
647N/A CollectedHeap* ch = Universe::heap();
647N/A if ((!mapinfo->map_space(ro, ro_rs, _ro_space)) ||
647N/A (!mapinfo->map_space(rw, rw_rs, _rw_space)) ||
647N/A (!mapinfo->map_space(md, md_rs, NULL)) ||
647N/A (!mapinfo->map_space(mc, mc_rs, NULL)) ||
647N/A // check the alignment constraints
647N/A (ch == NULL || ch->kind() != CollectedHeap::GenCollectedHeap ||
647N/A image_alignment !=
647N/A ((GenCollectedHeap*)ch)->gen_policy()->max_alignment())) {
647N/A // Base addresses didn't match; skip sharing, but continue
647N/A shared_rs.release();
647N/A spec()->disable_sharing();
647N/A // If -Xshare:on is specified, print out the error message and exit VM,
647N/A // otherwise, set UseSharedSpaces to false and continue.
647N/A if (RequireSharedSpaces) {
647N/A vm_exit_during_initialization("Unable to use shared archive.", NULL);
647N/A } else {
647N/A FLAG_SET_DEFAULT(UseSharedSpaces, false);
647N/A }
647N/A
647N/A // Note: freeing the block offset array objects does not
647N/A // currently free up the underlying storage.
647N/A delete _ro_bts;
647N/A _ro_bts = NULL;
647N/A delete _ro_space;
647N/A _ro_space = NULL;
647N/A delete _rw_bts;
647N/A _rw_bts = NULL;
647N/A delete _rw_space;
647N/A _rw_space = NULL;
647N/A shared_end = (HeapWord*)(rs.base() + rs.size());
647N/A _rs->resize_covered_region(MemRegion(shared_bottom, shared_bottom));
647N/A }
647N/A }
647N/A
653N/A // Reserved region includes shared spaces for oop.is_in_reserved().
647N/A _reserved.set_end(shared_end);
653N/A
647N/A } else {
647N/A _ro_space = NULL;
647N/A _rw_space = NULL;
653N/A }
647N/A}
647N/A
647N/A
647N/A// Do a complete scan of the shared read write space to catch all
647N/A// objects which contain references to any younger generation. Forward
647N/A// the pointers. Avoid space_iterate, as actually visiting all the
647N/A// objects in the space will page in more objects than we need.
647N/A// Instead, use the system dictionary as strong roots into the read
647N/A// write space.
647N/A//
647N/A// If a RedefineClasses() call has been made, then we have to iterate
647N/A// over the entire shared read-write space in order to find all the
647N/A// objects that need to be forwarded. For example, it is possible for
647N/A// an nmethod to be found and marked in GC phase-1 only for the nmethod
647N/A// to be freed by the time we reach GC phase-3. The underlying method
647N/A// is still marked, but we can't (easily) find it in GC phase-3 so we
647N/A// blow up in GC phase-4. With RedefineClasses() we want replaced code
653N/A// (EMCP or obsolete) to go away (i.e., be collectible) once it is no
653N/A// longer being executed by any thread so we keep minimal attachments
653N/A// to the replaced code. However, we can't guarantee when those EMCP
653N/A// or obsolete methods will be collected so they may still be out there
653N/A// even after we've severed our minimal attachments.
653N/A
653N/Avoid CompactingPermGenGen::pre_adjust_pointers() {
0N/A if (spec()->enable_shared_spaces()) {
0N/A if (JvmtiExport::has_redefined_a_class()) {
2922N/A // RedefineClasses() requires a brute force approach
2922N/A AdjustSharedObjectClosure blk;
2922N/A rw_space()->object_iterate(&blk);
2922N/A } else {
2922N/A RecursiveAdjustSharedObjectClosure blk;
2922N/A Universe::oops_do(&blk);
2922N/A StringTable::oops_do(&blk);
2922N/A SystemDictionary::always_strong_classes_do(&blk);
2922N/A TraversePlaceholdersClosure tpc;
2922N/A SystemDictionary::placeholders_do(&tpc);
2922N/A }
2922N/A }
2922N/A}
0N/A
0N/A
647N/A#ifdef ASSERT
0N/Aclass VerifyMarksClearedClosure : public ObjectClosure {
647N/Apublic:
2922N/A void do_object(oop obj) {
2922N/A assert(SharedSkipVerify || !obj->mark()->is_marked(),
2922N/A "Shared oop still marked?");
2922N/A }
2922N/A};
2922N/A#endif
2922N/A
2922N/A
2922N/Avoid CompactingPermGenGen::post_compact() {
2922N/A#ifdef ASSERT
2922N/A if (!SharedSkipVerify && spec()->enable_shared_spaces()) {
2922N/A VerifyMarksClearedClosure blk;
0N/A rw_space()->object_iterate(&blk);
0N/A }
2922N/A#endif
}
// Do not use in time-critical operations due to the possibility of paging
// in otherwise untouched or previously unread portions of the perm gen,
// for instance, the shared spaces. NOTE: Because CompactingPermGenGen
// derives from OneContigSpaceCardGeneration which is supposed to have a
// single space, and does not override its object_iterate() method,
// object iteration via that interface does not look at the objects in
// the shared spaces when using CDS. This should be fixed; see CR 6897798.
void CompactingPermGenGen::space_iterate(SpaceClosure* blk, bool usedOnly) {
OneContigSpaceCardGeneration::space_iterate(blk, usedOnly);
if (spec()->enable_shared_spaces()) {
// Making the rw_space walkable will page in the entire space, and
// is to be avoided in the case of time-critical operations.
// However, this is required for Verify and heap dump operations.
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;
}
// 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;