compactingPermGenGen.cpp revision 2062
0N/A/*
1879N/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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/symbolTable.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "memory/compactingPermGenGen.hpp"
1879N/A#include "memory/filemap.hpp"
1879N/A#include "memory/genOopClosures.inline.hpp"
1879N/A#include "memory/generation.inline.hpp"
1879N/A#include "memory/generationSpec.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#ifndef SERIALGC
1879N/A#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
1879N/A#endif
0N/A
0N/A
47N/A// An ObjectClosure helper: Recursively adjust all pointers in an object
47N/A// and all objects by referenced it. Clear marks on objects in order to
47N/A// prevent visiting any object twice. This helper is used when the
47N/A// RedefineClasses() API has been called.
47N/A
47N/Aclass AdjustSharedObjectClosure : public ObjectClosure {
47N/Apublic:
47N/A void do_object(oop obj) {
47N/A if (obj->is_shared_readwrite()) {
47N/A if (obj->mark()->is_marked()) {
47N/A obj->init_mark(); // Don't revisit this object.
47N/A obj->adjust_pointers(); // Adjust this object's references.
47N/A }
47N/A }
47N/A }
47N/A};
47N/A
47N/A
47N/A// An OopClosure helper: Recursively adjust all pointers in an object
47N/A// and all objects by referenced it. Clear marks on objects in order
47N/A// to prevent visiting any object twice.
0N/A
0N/Aclass RecursiveAdjustSharedObjectClosure : public OopClosure {
113N/A protected:
113N/A template <class T> inline void do_oop_work(T* p) {
113N/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);
0N/A constantPoolOop cp = ik->constants();
0N/A if (cp->is_shared_readonly()) {
0N/A cp->oop_iterate(this);
0N/A }
0N/A }
0N/A }
0N/A }
113N/A }
113N/A public:
113N/A virtual void do_oop(oop* p) { RecursiveAdjustSharedObjectClosure::do_oop_work(p); }
113N/A virtual void do_oop(narrowOop* p) { RecursiveAdjustSharedObjectClosure::do_oop_work(p); }
0N/A};
0N/A
0N/A
0N/A// We need to go through all placeholders in the system dictionary and
0N/A// try to resolve them into shared classes. Other threads might be in
0N/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
0N/A// but will not be unmarked during the application of the
2062N/A// RecursiveAdjustSharedObjectClosure to the SystemDictionary.
2062N/Aclass TraversePlaceholdersClosure {
2062N/A public:
2062N/A static void placeholders_do(Symbol* sym, oop loader) {
2062N/A if (CompactingPermGenGen::is_shared(sym)) {
0N/A oop k = SystemDictionary::find_shared_class(sym);
0N/A if (k != NULL) {
0N/A RecursiveAdjustSharedObjectClosure clo;
0N/A clo.do_oop(&k);
0N/A }
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
0N/A // Generation Counters - generation 2, 1 subspace
0N/A _gen_counters = new GenerationCounters(gen_name, 2, 1, &_virtual_space);
0N/A
0N/A _space_counters = new CSpaceCounters(gen_name, 0,
0N/A _virtual_space.reserved_size(),
0N/A _the_space, _gen_counters);
0N/A}
0N/A
0N/Avoid CompactingPermGenGen::update_counters() {
0N/A if (UsePerfData) {
0N/A _space_counters->update_all();
0N/A _gen_counters->update_all();
0N/A }
0N/A}
0N/A
0N/A
0N/ACompactingPermGenGen::CompactingPermGenGen(ReservedSpace rs,
0N/A ReservedSpace shared_rs,
0N/A size_t initial_byte_size,
0N/A int level, GenRemSet* remset,
0N/A ContiguousSpace* space,
0N/A PermanentGenerationSpec* spec_) :
0N/A OneContigSpaceCardGeneration(rs, initial_byte_size, MinPermHeapExpansion,
0N/A level, remset, space) {
0N/A
0N/A set_spec(spec_);
0N/A if (!UseSharedSpaces && !DumpSharedSpaces) {
0N/A spec()->disable_sharing();
0N/A }
0N/A
0N/A // Break virtual space into address ranges for all spaces.
0N/A
0N/A if (spec()->enable_shared_spaces()) {
0N/A shared_end = (HeapWord*)(shared_rs.base() + shared_rs.size());
0N/A misccode_end = shared_end;
0N/A misccode_bottom = misccode_end - heap_word_size(spec()->misc_code_size());
0N/A miscdata_end = misccode_bottom;
0N/A miscdata_bottom = miscdata_end - heap_word_size(spec()->misc_data_size());
0N/A readwrite_end = miscdata_bottom;
0N/A readwrite_bottom =
0N/A readwrite_end - heap_word_size(spec()->read_write_size());
0N/A readonly_end = readwrite_bottom;
0N/A readonly_bottom =
0N/A readonly_end - heap_word_size(spec()->read_only_size());
0N/A shared_bottom = readonly_bottom;
0N/A unshared_end = shared_bottom;
0N/A assert((char*)shared_bottom == shared_rs.base(), "shared space mismatch");
0N/A } else {
0N/A shared_end = (HeapWord*)(rs.base() + rs.size());
0N/A misccode_end = shared_end;
0N/A misccode_bottom = shared_end;
0N/A miscdata_end = shared_end;
0N/A miscdata_bottom = shared_end;
0N/A readwrite_end = shared_end;
0N/A readwrite_bottom = shared_end;
0N/A readonly_end = shared_end;
0N/A readonly_bottom = shared_end;
0N/A shared_bottom = shared_end;
0N/A unshared_end = shared_bottom;
0N/A }
0N/A unshared_bottom = (HeapWord*) rs.base();
0N/A
0N/A // Verify shared and unshared spaces adjacent.
0N/A assert((char*)shared_bottom == rs.base()+rs.size(), "shared space mismatch");
0N/A assert(unshared_end > unshared_bottom, "shared space mismatch");
0N/A
0N/A // Split reserved memory into pieces.
0N/A
0N/A ReservedSpace ro_rs = shared_rs.first_part(spec()->read_only_size(),
0N/A UseSharedSpaces);
0N/A ReservedSpace tmp_rs1 = shared_rs.last_part(spec()->read_only_size());
0N/A ReservedSpace rw_rs = tmp_rs1.first_part(spec()->read_write_size(),
0N/A UseSharedSpaces);
0N/A ReservedSpace tmp_rs2 = tmp_rs1.last_part(spec()->read_write_size());
0N/A ReservedSpace md_rs = tmp_rs2.first_part(spec()->misc_data_size(),
0N/A UseSharedSpaces);
0N/A ReservedSpace mc_rs = tmp_rs2.last_part(spec()->misc_data_size());
0N/A
0N/A _shared_space_size = spec()->read_only_size()
0N/A + spec()->read_write_size()
0N/A + spec()->misc_data_size()
0N/A + spec()->misc_code_size();
0N/A
0N/A // Allocate the unshared (default) space.
0N/A _the_space = new ContigPermSpace(_bts,
0N/A MemRegion(unshared_bottom, heap_word_size(initial_byte_size)));
0N/A if (_the_space == NULL)
0N/A vm_exit_during_initialization("Could not allocate an unshared"
0N/A " CompactingPermGen Space");
0N/A
0N/A // Allocate shared spaces
0N/A if (spec()->enable_shared_spaces()) {
0N/A
0N/A // If mapping a shared file, the space is not committed, don't
0N/A // mangle.
0N/A NOT_PRODUCT(bool old_ZapUnusedHeapArea = ZapUnusedHeapArea;)
0N/A NOT_PRODUCT(if (UseSharedSpaces) ZapUnusedHeapArea = false;)
0N/A
0N/A // Commit the memory behind the shared spaces if dumping (not
0N/A // mapping).
0N/A if (DumpSharedSpaces) {
0N/A _ro_vs.initialize(ro_rs, spec()->read_only_size());
0N/A _rw_vs.initialize(rw_rs, spec()->read_write_size());
0N/A _md_vs.initialize(md_rs, spec()->misc_data_size());
0N/A _mc_vs.initialize(mc_rs, spec()->misc_code_size());
0N/A }
0N/A
0N/A // Allocate the shared spaces.
0N/A _ro_bts = new BlockOffsetSharedArray(
0N/A MemRegion(readonly_bottom,
0N/A heap_word_size(spec()->read_only_size())),
0N/A heap_word_size(spec()->read_only_size()));
0N/A _ro_space = new OffsetTableContigSpace(_ro_bts,
0N/A MemRegion(readonly_bottom, readonly_end));
0N/A _rw_bts = new BlockOffsetSharedArray(
0N/A MemRegion(readwrite_bottom,
0N/A heap_word_size(spec()->read_write_size())),
0N/A heap_word_size(spec()->read_write_size()));
0N/A _rw_space = new OffsetTableContigSpace(_rw_bts,
0N/A MemRegion(readwrite_bottom, readwrite_end));
0N/A
0N/A // Restore mangling flag.
0N/A NOT_PRODUCT(ZapUnusedHeapArea = old_ZapUnusedHeapArea;)
0N/A
0N/A if (_ro_space == NULL || _rw_space == NULL)
0N/A vm_exit_during_initialization("Could not allocate a shared space");
0N/A
0N/A // Cover both shared spaces entirely with cards.
0N/A _rs->resize_covered_region(MemRegion(readonly_bottom, readwrite_end));
0N/A
0N/A if (UseSharedSpaces) {
0N/A
0N/A // Map in the regions in the shared file.
0N/A FileMapInfo* mapinfo = FileMapInfo::current_info();
0N/A size_t image_alignment = mapinfo->alignment();
0N/A CollectedHeap* ch = Universe::heap();
0N/A if ((!mapinfo->map_space(ro, ro_rs, _ro_space)) ||
0N/A (!mapinfo->map_space(rw, rw_rs, _rw_space)) ||
0N/A (!mapinfo->map_space(md, md_rs, NULL)) ||
0N/A (!mapinfo->map_space(mc, mc_rs, NULL)) ||
0N/A // check the alignment constraints
0N/A (ch == NULL || ch->kind() != CollectedHeap::GenCollectedHeap ||
0N/A image_alignment !=
0N/A ((GenCollectedHeap*)ch)->gen_policy()->max_alignment())) {
0N/A // Base addresses didn't match; skip sharing, but continue
0N/A shared_rs.release();
0N/A spec()->disable_sharing();
0N/A // If -Xshare:on is specified, print out the error message and exit VM,
0N/A // otherwise, set UseSharedSpaces to false and continue.
0N/A if (RequireSharedSpaces) {
0N/A vm_exit_during_initialization("Unable to use shared archive.", NULL);
0N/A } else {
0N/A FLAG_SET_DEFAULT(UseSharedSpaces, false);
0N/A }
0N/A
0N/A // Note: freeing the block offset array objects does not
0N/A // currently free up the underlying storage.
0N/A delete _ro_bts;
0N/A _ro_bts = NULL;
0N/A delete _ro_space;
0N/A _ro_space = NULL;
0N/A delete _rw_bts;
0N/A _rw_bts = NULL;
0N/A delete _rw_space;
0N/A _rw_space = NULL;
0N/A shared_end = (HeapWord*)(rs.base() + rs.size());
0N/A _rs->resize_covered_region(MemRegion(shared_bottom, shared_bottom));
0N/A }
0N/A }
0N/A
0N/A // Reserved region includes shared spaces for oop.is_in_reserved().
0N/A _reserved.set_end(shared_end);
0N/A
0N/A } else {
0N/A _ro_space = NULL;
0N/A _rw_space = NULL;
0N/A }
0N/A}
0N/A
0N/A
0N/A// Do a complete scan of the shared read write space to catch all
0N/A// objects which contain references to any younger generation. Forward
0N/A// the pointers. Avoid space_iterate, as actually visiting all the
0N/A// objects in the space will page in more objects than we need.
0N/A// Instead, use the system dictionary as strong roots into the read
0N/A// write space.
47N/A//
47N/A// If a RedefineClasses() call has been made, then we have to iterate
47N/A// over the entire shared read-write space in order to find all the
47N/A// objects that need to be forwarded. For example, it is possible for
47N/A// an nmethod to be found and marked in GC phase-1 only for the nmethod
47N/A// to be freed by the time we reach GC phase-3. The underlying method
47N/A// is still marked, but we can't (easily) find it in GC phase-3 so we
47N/A// blow up in GC phase-4. With RedefineClasses() we want replaced code
47N/A// (EMCP or obsolete) to go away (i.e., be collectible) once it is no
47N/A// longer being executed by any thread so we keep minimal attachments
47N/A// to the replaced code. However, we can't guarantee when those EMCP
47N/A// or obsolete methods will be collected so they may still be out there
47N/A// even after we've severed our minimal attachments.
0N/A
0N/Avoid CompactingPermGenGen::pre_adjust_pointers() {
0N/A if (spec()->enable_shared_spaces()) {
47N/A if (JvmtiExport::has_redefined_a_class()) {
47N/A // RedefineClasses() requires a brute force approach
47N/A AdjustSharedObjectClosure blk;
47N/A rw_space()->object_iterate(&blk);
47N/A } else {
47N/A RecursiveAdjustSharedObjectClosure blk;
47N/A Universe::oops_do(&blk);
47N/A StringTable::oops_do(&blk);
47N/A SystemDictionary::always_strong_classes_do(&blk);
2062N/A SystemDictionary::placeholders_do(TraversePlaceholdersClosure::placeholders_do);
47N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A#ifdef ASSERT
0N/Aclass VerifyMarksClearedClosure : public ObjectClosure {
0N/Apublic:
0N/A void do_object(oop obj) {
0N/A assert(SharedSkipVerify || !obj->mark()->is_marked(),
0N/A "Shared oop still marked?");
0N/A }
0N/A};
0N/A#endif
0N/A
0N/A
0N/Avoid CompactingPermGenGen::post_compact() {
0N/A#ifdef ASSERT
0N/A if (!SharedSkipVerify && spec()->enable_shared_spaces()) {
0N/A VerifyMarksClearedClosure blk;
0N/A rw_space()->object_iterate(&blk);
0N/A }
0N/A#endif
0N/A}
0N/A
0N/A
1051N/A// Do not use in time-critical operations due to the possibility of paging
1051N/A// in otherwise untouched or previously unread portions of the perm gen,
1051N/A// for instance, the shared spaces. NOTE: Because CompactingPermGenGen
1051N/A// derives from OneContigSpaceCardGeneration which is supposed to have a
1051N/A// single space, and does not override its object_iterate() method,
1051N/A// object iteration via that interface does not look at the objects in
1051N/A// the shared spaces when using CDS. This should be fixed; see CR 6897798.
0N/Avoid CompactingPermGenGen::space_iterate(SpaceClosure* blk, bool usedOnly) {
0N/A OneContigSpaceCardGeneration::space_iterate(blk, usedOnly);
0N/A if (spec()->enable_shared_spaces()) {
0N/A // Making the rw_space walkable will page in the entire space, and
1051N/A // is to be avoided in the case of time-critical operations.
1051N/A // However, this is required for Verify and heap dump operations.
0N/A blk->do_space(ro_space());
0N/A blk->do_space(rw_space());
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid CompactingPermGenGen::print_on(outputStream* st) const {
0N/A OneContigSpaceCardGeneration::print_on(st);
0N/A if (spec()->enable_shared_spaces()) {
0N/A st->print(" ro");
0N/A ro_space()->print_on(st);
0N/A st->print(" rw");
0N/A rw_space()->print_on(st);
0N/A } else {
0N/A st->print_cr("No shared spaces configured.");
0N/A }
0N/A}
0N/A
0N/A
0N/A// References from the perm gen to the younger generation objects may
0N/A// occur in static fields in Java classes or in constant pool references
0N/A// to String objects.
0N/A
0N/Avoid CompactingPermGenGen::younger_refs_iterate(OopsInGenClosure* blk) {
0N/A OneContigSpaceCardGeneration::younger_refs_iterate(blk);
0N/A if (spec()->enable_shared_spaces()) {
0N/A blk->set_generation(this);
0N/A // ro_space has no younger gen refs.
0N/A _rs->younger_refs_in_space_iterate(rw_space(), blk);
0N/A blk->reset_generation();
0N/A }
0N/A}
0N/A
0N/A
0N/A// Shared spaces are addressed in pre_adjust_pointers.
0N/Avoid CompactingPermGenGen::adjust_pointers() {
0N/A the_space()->adjust_pointers();
0N/A}
0N/A
0N/A
0N/Avoid CompactingPermGenGen::compact() {
0N/A the_space()->compact();
0N/A}
0N/A
0N/A
0N/Asize_t CompactingPermGenGen::contiguous_available() const {
0N/A // Don't include shared spaces.
0N/A return OneContigSpaceCardGeneration::contiguous_available()
0N/A - _shared_space_size;
0N/A}
0N/A
0N/Asize_t CompactingPermGenGen::max_capacity() const {
0N/A // Don't include shared spaces.
0N/A assert(UseSharedSpaces || (_shared_space_size == 0),
0N/A "If not used, the size of shared spaces should be 0");
0N/A return OneContigSpaceCardGeneration::max_capacity()
0N/A - _shared_space_size;
0N/A}
0N/A
0N/A
0N/A// No young generation references, clear this generation's main space's
0N/A// card table entries. Do NOT clear the card table entries for the
0N/A// read-only space (always clear) or the read-write space (valuable
0N/A// information).
0N/A
0N/Avoid CompactingPermGenGen::clear_remembered_set() {
0N/A _rs->clear(MemRegion(the_space()->bottom(), the_space()->end()));
0N/A}
0N/A
0N/A
0N/A// Objects in this generation's main space may have moved, invalidate
0N/A// that space's cards. Do NOT invalidate the card table entries for the
0N/A// read-only or read-write spaces, as those objects never move.
0N/A
0N/Avoid CompactingPermGenGen::invalidate_remembered_set() {
0N/A _rs->invalidate(used_region());
0N/A}
0N/A
0N/A
0N/Avoid CompactingPermGenGen::verify(bool allow_dirty) {
0N/A the_space()->verify(allow_dirty);
0N/A if (!SharedSkipVerify && spec()->enable_shared_spaces()) {
0N/A ro_space()->verify(allow_dirty);
0N/A rw_space()->verify(allow_dirty);
0N/A }
0N/A}
0N/A
0N/A
0N/AHeapWord* CompactingPermGenGen::unshared_bottom;
0N/AHeapWord* CompactingPermGenGen::unshared_end;
0N/AHeapWord* CompactingPermGenGen::shared_bottom;
0N/AHeapWord* CompactingPermGenGen::shared_end;
0N/AHeapWord* CompactingPermGenGen::readonly_bottom;
0N/AHeapWord* CompactingPermGenGen::readonly_end;
0N/AHeapWord* CompactingPermGenGen::readwrite_bottom;
0N/AHeapWord* CompactingPermGenGen::readwrite_end;
0N/AHeapWord* CompactingPermGenGen::miscdata_bottom;
0N/AHeapWord* CompactingPermGenGen::miscdata_end;
0N/AHeapWord* CompactingPermGenGen::misccode_bottom;
0N/AHeapWord* CompactingPermGenGen::misccode_end;
0N/A
0N/A// JVM/TI RedefineClasses() support:
0N/Abool CompactingPermGenGen::remap_shared_readonly_as_readwrite() {
0N/A assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
0N/A
0N/A if (UseSharedSpaces) {
0N/A // remap the shared readonly space to shared readwrite, private
0N/A FileMapInfo* mapinfo = FileMapInfo::current_info();
0N/A if (!mapinfo->remap_shared_readonly_as_readwrite()) {
0N/A return false;
0N/A }
0N/A }
0N/A return true;
0N/A}