duke@435: /* coleenp@3682: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "classfile/loaderConstraints.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_implementation/shared/spaceDecorator.hpp" stefank@2314: #include "memory/classify.hpp" stefank@2314: #include "memory/filemap.hpp" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "oops/methodDataOop.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/signature.hpp" stefank@2314: #include "runtime/vmThread.hpp" stefank@2314: #include "runtime/vm_operations.hpp" stefank@2314: #include "utilities/copy.hpp" duke@435: duke@435: duke@435: // Closure to set up the fingerprint field for all methods. duke@435: duke@435: class FingerprintMethodsClosure: public ObjectClosure { duke@435: public: duke@435: void do_object(oop obj) { duke@435: if (obj->is_method()) { duke@435: methodOop mobj = (methodOop)obj; duke@435: ResourceMark rm; duke@435: (new Fingerprinter(mobj))->fingerprint(); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: duke@435: // Closure to set the hash value (String.hash field) in all of the duke@435: // String objects in the heap. Setting the hash value is not required. duke@435: // However, setting the value in advance prevents the value from being duke@435: // written later, increasing the likelihood that the shared page contain duke@435: // the hash can be shared. duke@435: // duke@435: // NOTE THAT the algorithm in StringTable::hash_string() MUST MATCH the duke@435: // algorithm in java.lang.String.hashCode(). duke@435: duke@435: class StringHashCodeClosure: public OopClosure { duke@435: private: duke@435: Thread* THREAD; duke@435: int hash_offset; duke@435: public: duke@435: StringHashCodeClosure(Thread* t) { duke@435: THREAD = t; duke@435: hash_offset = java_lang_String::hash_offset_in_bytes(); duke@435: } duke@435: coleenp@548: void do_oop(oop* p) { coleenp@548: if (p != NULL) { coleenp@548: oop obj = *p; never@1577: if (obj->klass() == SystemDictionary::String_klass()) { duke@435: never@2700: int hash = java_lang_String::hash_string(obj); duke@435: obj->int_field_put(hash_offset, hash); duke@435: } duke@435: } duke@435: } coleenp@548: void do_oop(narrowOop* p) { ShouldNotReachHere(); } duke@435: }; duke@435: duke@435: duke@435: // Remove data from objects which should not appear in the shared file duke@435: // (as it pertains only to the current JVM). duke@435: duke@435: class RemoveUnshareableInfoClosure : public ObjectClosure { duke@435: public: duke@435: void do_object(oop obj) { duke@435: // Zap data from the objects which is pertains only to this JVM. We duke@435: // want that data recreated in new JVMs when the shared file is used. duke@435: if (obj->is_method()) { duke@435: ((methodOop)obj)->remove_unshareable_info(); duke@435: } duke@435: else if (obj->is_klass()) { duke@435: Klass::cast((klassOop)obj)->remove_unshareable_info(); duke@435: } duke@435: duke@435: // Don't save compiler related special oops (shouldn't be any yet). duke@435: if (obj->is_methodData() || obj->is_compiledICHolder()) { duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: static bool mark_object(oop obj) { duke@435: if (obj != NULL && duke@435: !obj->is_shared() && duke@435: !obj->is_forwarded() && duke@435: !obj->is_gc_marked()) { duke@435: obj->set_mark(markOopDesc::prototype()->set_marked()); duke@435: return true; duke@435: } duke@435: duke@435: return false; duke@435: } duke@435: coleenp@2497: coleenp@2497: class MoveSymbols : public SymbolClosure { coleenp@2497: private: coleenp@2497: char* _start; coleenp@2497: char* _end; coleenp@2497: char* _top; coleenp@2497: int _count; coleenp@2497: coleenp@2497: bool in_shared_space(Symbol* sym) const { coleenp@2497: return (char*)sym >= _start && (char*)sym < _end; coleenp@2497: } coleenp@2497: coleenp@2497: Symbol* get_shared_copy(Symbol* sym) { coleenp@2497: return sym->refcount() > 0 ? NULL : (Symbol*)(_start - sym->refcount()); coleenp@2497: } coleenp@2497: coleenp@2497: Symbol* make_shared_copy(Symbol* sym) { coleenp@2497: Symbol* new_sym = (Symbol*)_top; coleenp@2497: int size = sym->object_size(); coleenp@2497: _top += size * HeapWordSize; coleenp@2497: if (_top <= _end) { coleenp@2497: Copy::disjoint_words((HeapWord*)sym, (HeapWord*)new_sym, size); coleenp@2497: // Encode a reference to the copy as a negative distance from _start coleenp@2497: // When a symbol is being copied to a shared space coleenp@2497: // during CDS archive creation, the original symbol is marked coleenp@2497: // as relocated by putting a negative value to its _refcount field, coleenp@2497: // This value is also used to find where exactly the shared copy is coleenp@2497: // (see MoveSymbols::get_shared_copy), so that the other references coleenp@2497: // to this symbol could be changed to point to the shared copy. coleenp@2497: sym->_refcount = (int)(_start - (char*)new_sym); coleenp@2497: // Mark the symbol in the shared archive as immortal so it is read only coleenp@2497: // and not refcounted. coleenp@2497: new_sym->_refcount = -1; coleenp@2497: _count++; coleenp@2497: } else { coleenp@2497: report_out_of_shared_space(SharedMiscData); coleenp@2497: } coleenp@2497: return new_sym; coleenp@2497: } coleenp@2497: coleenp@2497: public: coleenp@2497: MoveSymbols(char* top, char* end) : coleenp@2497: _start(top), _end(end), _top(top), _count(0) { } coleenp@2497: coleenp@2497: char* get_top() const { return _top; } coleenp@2497: int count() const { return _count; } coleenp@2497: coleenp@2497: void do_symbol(Symbol** p) { coleenp@2497: Symbol* sym = load_symbol(p); coleenp@2497: if (sym != NULL && !in_shared_space(sym)) { coleenp@2497: Symbol* new_sym = get_shared_copy(sym); coleenp@2497: if (new_sym == NULL) { coleenp@2497: // The symbol has not been relocated yet; copy it to _top address coleenp@2497: assert(sym->refcount() > 0, "should have positive reference count"); coleenp@2497: new_sym = make_shared_copy(sym); coleenp@2497: } coleenp@2497: // Make the reference point to the shared copy of the symbol coleenp@2497: store_symbol(p, new_sym); coleenp@2497: } coleenp@2497: } coleenp@2497: }; coleenp@2497: coleenp@2497: duke@435: // Closure: mark objects closure. duke@435: duke@435: class MarkObjectsOopClosure : public OopClosure { duke@435: public: coleenp@548: void do_oop(oop* p) { mark_object(*p); } coleenp@548: void do_oop(narrowOop* p) { ShouldNotReachHere(); } duke@435: }; duke@435: duke@435: duke@435: class MarkObjectsSkippingKlassesOopClosure : public OopClosure { duke@435: public: duke@435: void do_oop(oop* pobj) { duke@435: oop obj = *pobj; duke@435: if (obj != NULL && duke@435: !obj->is_klass()) { duke@435: mark_object(obj); duke@435: } duke@435: } coleenp@548: void do_oop(narrowOop* pobj) { ShouldNotReachHere(); } duke@435: }; duke@435: duke@435: duke@435: static void mark_object_recursive_skipping_klasses(oop obj) { duke@435: mark_object(obj); duke@435: if (obj != NULL) { duke@435: MarkObjectsSkippingKlassesOopClosure mark_all; duke@435: obj->oop_iterate(&mark_all); duke@435: } duke@435: } duke@435: duke@435: coleenp@2497: // Closure: mark common read-only objects duke@435: duke@435: class MarkCommonReadOnly : public ObjectClosure { duke@435: private: duke@435: MarkObjectsOopClosure mark_all; duke@435: public: duke@435: void do_object(oop obj) { duke@435: duke@435: // Mark all constMethod objects. duke@435: duke@435: if (obj->is_constMethod()) { duke@435: mark_object(obj); duke@435: mark_object(constMethodOop(obj)->stackmap_data()); duke@435: // Exception tables are needed by ci code during compilation. duke@435: mark_object(constMethodOop(obj)->exception_table()); duke@435: } duke@435: duke@435: // Mark objects referenced by klass objects which are read-only. duke@435: duke@435: else if (obj->is_klass()) { duke@435: Klass* k = Klass::cast((klassOop)obj); duke@435: mark_object(k->secondary_supers()); duke@435: duke@435: // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though duke@435: // it is never modified. Otherwise, they will be pre-marked; the duke@435: // GC marking phase will skip them; and by skipping them will fail duke@435: // to mark the methods objects referenced by the array. duke@435: duke@435: if (obj->blueprint()->oop_is_instanceKlass()) { duke@435: instanceKlass* ik = instanceKlass::cast((klassOop)obj); duke@435: mark_object(ik->method_ordering()); duke@435: mark_object(ik->local_interfaces()); duke@435: mark_object(ik->transitive_interfaces()); duke@435: mark_object(ik->fields()); duke@435: duke@435: mark_object(ik->class_annotations()); duke@435: duke@435: mark_object_recursive_skipping_klasses(ik->fields_annotations()); duke@435: mark_object_recursive_skipping_klasses(ik->methods_annotations()); duke@435: mark_object_recursive_skipping_klasses(ik->methods_parameter_annotations()); duke@435: mark_object_recursive_skipping_klasses(ik->methods_default_annotations()); duke@435: duke@435: typeArrayOop inner_classes = ik->inner_classes(); duke@435: if (inner_classes != NULL) { duke@435: mark_object(inner_classes); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: }; duke@435: duke@435: coleenp@2497: // Closure: find symbol references in Java Heap objects duke@435: coleenp@2497: class CommonSymbolsClosure : public ObjectClosure { duke@435: private: coleenp@2497: SymbolClosure* _closure; duke@435: public: coleenp@2497: CommonSymbolsClosure(SymbolClosure* closure) : _closure(closure) { } coleenp@2497: duke@435: void do_object(oop obj) { duke@435: coleenp@2497: // Traverse symbols referenced by method objects. duke@435: duke@435: if (obj->is_method()) { duke@435: methodOop m = methodOop(obj); coleenp@2497: constantPoolOop constants = m->constants(); coleenp@2497: _closure->do_symbol(constants->symbol_at_addr(m->name_index())); coleenp@2497: _closure->do_symbol(constants->symbol_at_addr(m->signature_index())); duke@435: } duke@435: coleenp@2497: // Traverse symbols referenced by klass objects which are read-only. duke@435: duke@435: else if (obj->is_klass()) { coleenp@2497: Klass* k = Klass::cast((klassOop)obj); coleenp@2497: k->shared_symbols_iterate(_closure); duke@435: duke@435: if (obj->blueprint()->oop_is_instanceKlass()) { duke@435: instanceKlass* ik = instanceKlass::cast((klassOop)obj); duke@435: typeArrayOop inner_classes = ik->inner_classes(); duke@435: if (inner_classes != NULL) { coleenp@2497: constantPoolOop constants = ik->constants(); coleenp@2497: int n = inner_classes->length(); coleenp@2497: for (int i = 0; i < n; i += instanceKlass::inner_class_next_offset) { duke@435: int ioff = i + instanceKlass::inner_class_inner_name_offset; duke@435: int index = inner_classes->ushort_at(ioff); duke@435: if (index != 0) { coleenp@2497: _closure->do_symbol(constants->symbol_at_addr(index)); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: coleenp@2497: // Traverse symbols referenced by other constantpool entries. duke@435: coleenp@2497: else if (obj->is_constantPool()) { coleenp@2497: constantPoolOop(obj)->shared_symbols_iterate(_closure); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Closure: mark char arrays used by strings duke@435: duke@435: class MarkStringValues : public ObjectClosure { duke@435: private: duke@435: MarkObjectsOopClosure mark_all; duke@435: public: duke@435: void do_object(oop obj) { duke@435: duke@435: // Character arrays referenced by String objects are read-only. duke@435: duke@435: if (java_lang_String::is_instance(obj)) { duke@435: mark_object(java_lang_String::value(obj)); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: #ifdef DEBUG duke@435: // Closure: Check for objects left in the heap which have not been moved. duke@435: duke@435: class CheckRemainingObjects : public ObjectClosure { duke@435: private: duke@435: int count; duke@435: duke@435: public: duke@435: CheckRemainingObjects() { duke@435: count = 0; duke@435: } duke@435: duke@435: void do_object(oop obj) { duke@435: if (!obj->is_shared() && duke@435: !obj->is_forwarded()) { duke@435: ++count; duke@435: if (Verbose) { duke@435: tty->print("Unreferenced object: "); duke@435: obj->print_on(tty); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void status() { duke@435: tty->print_cr("%d objects no longer referenced, not shared.", count); duke@435: } duke@435: }; duke@435: #endif duke@435: duke@435: duke@435: // Closure: Mark remaining objects read-write, except Strings. duke@435: duke@435: class MarkReadWriteObjects : public ObjectClosure { duke@435: private: duke@435: MarkObjectsOopClosure mark_objects; duke@435: public: duke@435: void do_object(oop obj) { duke@435: duke@435: // The METHODS() OBJARRAYS CANNOT BE MADE READ-ONLY, even though duke@435: // it is never modified. Otherwise, they will be pre-marked; the duke@435: // GC marking phase will skip them; and by skipping them will fail duke@435: // to mark the methods objects referenced by the array. duke@435: duke@435: if (obj->is_klass()) { duke@435: mark_object(obj); duke@435: Klass* k = klassOop(obj)->klass_part(); duke@435: mark_object(k->java_mirror()); duke@435: if (obj->blueprint()->oop_is_instanceKlass()) { duke@435: instanceKlass* ik = (instanceKlass*)k; duke@435: mark_object(ik->methods()); duke@435: mark_object(ik->constants()); duke@435: } duke@435: if (obj->blueprint()->oop_is_javaArray()) { duke@435: arrayKlass* ak = (arrayKlass*)k; duke@435: mark_object(ak->component_mirror()); duke@435: } duke@435: return; duke@435: } duke@435: duke@435: // Mark constantPool tags and the constantPoolCache. duke@435: duke@435: else if (obj->is_constantPool()) { duke@435: constantPoolOop pool = constantPoolOop(obj); duke@435: mark_object(pool->cache()); duke@435: pool->shared_tags_iterate(&mark_objects); duke@435: return; duke@435: } duke@435: duke@435: // Mark all method objects. duke@435: duke@435: if (obj->is_method()) { duke@435: mark_object(obj); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Closure: Mark String objects read-write. duke@435: duke@435: class MarkStringObjects : public ObjectClosure { duke@435: private: duke@435: MarkObjectsOopClosure mark_objects; duke@435: public: duke@435: void do_object(oop obj) { duke@435: duke@435: // Mark String objects referenced by constant pool entries. duke@435: duke@435: if (obj->is_constantPool()) { duke@435: constantPoolOop pool = constantPoolOop(obj); duke@435: pool->shared_strings_iterate(&mark_objects); duke@435: return; duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Move objects matching specified type (ie. lock_bits) to the specified duke@435: // space. duke@435: duke@435: class MoveMarkedObjects : public ObjectClosure { duke@435: private: duke@435: OffsetTableContigSpace* _space; duke@435: bool _read_only; duke@435: duke@435: public: duke@435: MoveMarkedObjects(OffsetTableContigSpace* space, bool read_only) { duke@435: _space = space; duke@435: _read_only = read_only; duke@435: } duke@435: duke@435: void do_object(oop obj) { duke@435: if (obj->is_shared()) { duke@435: return; duke@435: } duke@435: if (obj->is_gc_marked() && obj->forwardee() == NULL) { duke@435: int s = obj->size(); duke@435: oop sh_obj = (oop)_space->allocate(s); duke@435: if (sh_obj == NULL) { coleenp@2497: report_out_of_shared_space(_read_only ? SharedReadOnly : SharedReadWrite); duke@435: } duke@435: if (PrintSharedSpaces && Verbose && WizardMode) { duke@435: tty->print_cr("\nMoveMarkedObjects: " PTR_FORMAT " -> " PTR_FORMAT " %s", obj, sh_obj, duke@435: (_read_only ? "ro" : "rw")); duke@435: } duke@435: Copy::aligned_disjoint_words((HeapWord*)obj, (HeapWord*)sh_obj, s); duke@435: obj->forward_to(sh_obj); duke@435: if (_read_only) { duke@435: // Readonly objects: set hash value to self pointer and make gc_marked. duke@435: sh_obj->forward_to(sh_obj); duke@435: } else { duke@435: sh_obj->init_mark(); duke@435: } duke@435: } duke@435: } duke@435: }; duke@435: duke@435: static void mark_and_move(oop obj, MoveMarkedObjects* move) { duke@435: if (mark_object(obj)) move->do_object(obj); duke@435: } duke@435: duke@435: enum order_policy { duke@435: OP_favor_startup = 0, duke@435: OP_balanced = 1, duke@435: OP_favor_runtime = 2 duke@435: }; duke@435: duke@435: static void mark_and_move_for_policy(order_policy policy, oop obj, MoveMarkedObjects* move) { duke@435: if (SharedOptimizeColdStartPolicy >= policy) mark_and_move(obj, move); duke@435: } duke@435: duke@435: class MarkAndMoveOrderedReadOnly : public ObjectClosure { duke@435: private: duke@435: MoveMarkedObjects *_move_ro; duke@435: duke@435: public: duke@435: MarkAndMoveOrderedReadOnly(MoveMarkedObjects *move_ro) : _move_ro(move_ro) {} duke@435: duke@435: void do_object(oop obj) { duke@435: if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) { duke@435: instanceKlass* ik = instanceKlass::cast((klassOop)obj); duke@435: int i; duke@435: duke@435: if (ik->super() != NULL) { duke@435: do_object(ik->super()); duke@435: } duke@435: duke@435: objArrayOop interfaces = ik->local_interfaces(); duke@435: mark_and_move_for_policy(OP_favor_startup, interfaces, _move_ro); duke@435: for(i = 0; i < interfaces->length(); i++) { duke@435: klassOop k = klassOop(interfaces->obj_at(i)); duke@435: do_object(k); duke@435: } duke@435: duke@435: objArrayOop methods = ik->methods(); duke@435: for(i = 0; i < methods->length(); i++) { duke@435: methodOop m = methodOop(methods->obj_at(i)); duke@435: mark_and_move_for_policy(OP_favor_startup, m->constMethod(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->exception_table(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, m->constMethod()->stackmap_data(), _move_ro); duke@435: } duke@435: duke@435: mark_and_move_for_policy(OP_favor_startup, ik->transitive_interfaces(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_startup, ik->fields(), _move_ro); duke@435: duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->method_ordering(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->class_annotations(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->fields_annotations(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->methods_annotations(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->methods_parameter_annotations(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->methods_default_annotations(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->inner_classes(), _move_ro); duke@435: mark_and_move_for_policy(OP_favor_runtime, ik->secondary_supers(), _move_ro); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: class MarkAndMoveOrderedReadWrite: public ObjectClosure { duke@435: private: duke@435: MoveMarkedObjects *_move_rw; duke@435: duke@435: public: duke@435: MarkAndMoveOrderedReadWrite(MoveMarkedObjects *move_rw) : _move_rw(move_rw) {} duke@435: duke@435: void do_object(oop obj) { duke@435: if (obj->is_klass() && obj->blueprint()->oop_is_instanceKlass()) { duke@435: instanceKlass* ik = instanceKlass::cast((klassOop)obj); duke@435: int i; duke@435: duke@435: mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop(), _move_rw); duke@435: duke@435: if (ik->super() != NULL) { duke@435: do_object(ik->super()); duke@435: } duke@435: duke@435: objArrayOop interfaces = ik->local_interfaces(); duke@435: for(i = 0; i < interfaces->length(); i++) { duke@435: klassOop k = klassOop(interfaces->obj_at(i)); duke@435: mark_and_move_for_policy(OP_favor_startup, k, _move_rw); duke@435: do_object(k); duke@435: } duke@435: duke@435: objArrayOop methods = ik->methods(); duke@435: mark_and_move_for_policy(OP_favor_startup, methods, _move_rw); duke@435: for(i = 0; i < methods->length(); i++) { duke@435: methodOop m = methodOop(methods->obj_at(i)); duke@435: mark_and_move_for_policy(OP_favor_startup, m, _move_rw); duke@435: mark_and_move_for_policy(OP_favor_startup, ik->constants(), _move_rw); // idempotent duke@435: mark_and_move_for_policy(OP_balanced, ik->constants()->cache(), _move_rw); // idempotent duke@435: mark_and_move_for_policy(OP_balanced, ik->constants()->tags(), _move_rw); // idempotent duke@435: } duke@435: duke@435: mark_and_move_for_policy(OP_favor_startup, ik->as_klassOop()->klass(), _move_rw); duke@435: mark_and_move_for_policy(OP_favor_startup, ik->constants()->klass(), _move_rw); duke@435: duke@435: // Although Java mirrors are marked in MarkReadWriteObjects, duke@435: // apparently they were never moved into shared spaces since duke@435: // MoveMarkedObjects skips marked instance oops. This may duke@435: // be a bug in the original implementation or simply the vestige duke@435: // of an abandoned experiment. Nevertheless we leave a hint duke@435: // here in case this capability is ever correctly implemented. duke@435: // duke@435: // mark_and_move_for_policy(OP_favor_runtime, ik->java_mirror(), _move_rw); duke@435: } duke@435: } duke@435: duke@435: }; duke@435: duke@435: // Adjust references in oops to refer to shared spaces. duke@435: duke@435: class ResolveForwardingClosure: public OopClosure { duke@435: public: duke@435: void do_oop(oop* p) { duke@435: oop obj = *p; duke@435: if (!obj->is_shared()) { duke@435: if (obj != NULL) { duke@435: oop f = obj->forwardee(); duke@435: guarantee(f->is_shared(), "Oop doesn't refer to shared space."); duke@435: *p = f; duke@435: } duke@435: } duke@435: } coleenp@548: void do_oop(narrowOop* pobj) { ShouldNotReachHere(); } duke@435: }; duke@435: duke@435: coleenp@2497: // The methods array must be reordered by Symbol* address. coleenp@2497: // (See classFileParser.cpp where methods in a class are originally coleenp@2497: // sorted). The addresses of symbols have been changed as a result coleenp@2497: // of moving to the shared space. duke@435: duke@435: class SortMethodsClosure: public ObjectClosure { coleenp@2497: public: coleenp@2497: void do_object(oop obj) { coleenp@2497: if (obj->blueprint()->oop_is_instanceKlass()) { coleenp@2497: instanceKlass* ik = instanceKlass::cast((klassOop)obj); coleenp@2497: methodOopDesc::sort_methods(ik->methods(), coleenp@2497: ik->methods_annotations(), coleenp@2497: ik->methods_parameter_annotations(), coleenp@2497: ik->methods_default_annotations(), coleenp@2497: true /* idempotent, slow */); coleenp@2497: } coleenp@2497: } coleenp@2497: }; coleenp@2497: coleenp@2777: // Vtable and Itable indices are calculated based on methods array coleenp@2777: // order (see klassItable::compute_itable_index()). Must reinitialize coleenp@2497: // after ALL methods of ALL classes have been reordered. coleenp@2497: // We assume that since checkconstraints is false, this method coleenp@2497: // cannot throw an exception. An exception here would be coleenp@2497: // problematic since this is the VMThread, not a JavaThread. coleenp@2497: coleenp@2777: class ReinitializeTables: public ObjectClosure { duke@435: private: duke@435: Thread* _thread; duke@435: duke@435: public: coleenp@2777: ReinitializeTables(Thread* thread) : _thread(thread) {} coleenp@2777: coleenp@2777: // Initialize super vtable first, check if already initialized to avoid coleenp@2777: // quadradic behavior. The vtable is cleared in remove_unshareable_info. coleenp@2777: void reinitialize_vtables(klassOop k) { coleenp@2777: if (k->blueprint()->oop_is_instanceKlass()) { coleenp@2777: instanceKlass* ik = instanceKlass::cast(k); coleenp@2777: if (ik->vtable()->is_initialized()) return; coleenp@2777: if (ik->super() != NULL) { coleenp@2777: reinitialize_vtables(ik->super()); coleenp@2777: } coleenp@2777: ik->vtable()->initialize_vtable(false, _thread); coleenp@2777: } coleenp@2777: } duke@435: duke@435: void do_object(oop obj) { duke@435: if (obj->blueprint()->oop_is_instanceKlass()) { duke@435: instanceKlass* ik = instanceKlass::cast((klassOop)obj); coleenp@2777: ResourceMark rm(_thread); coleenp@2497: ik->itable()->initialize_itable(false, _thread); coleenp@2777: reinitialize_vtables((klassOop)obj); coleenp@2777: #ifdef ASSERT coleenp@2777: ik->vtable()->verify(tty, true); coleenp@2777: #endif // ASSERT coleenp@2777: } else if (obj->blueprint()->oop_is_arrayKlass()) { coleenp@2777: // The vtable for array klasses are that of its super class, coleenp@2777: // ie. java.lang.Object. coleenp@2777: arrayKlass* ak = arrayKlass::cast((klassOop)obj); coleenp@2777: if (ak->vtable()->is_initialized()) return; coleenp@2777: ak->vtable()->initialize_vtable(false, _thread); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Adjust references in oops to refer to shared spaces. duke@435: duke@435: class PatchOopsClosure: public ObjectClosure { duke@435: private: duke@435: Thread* _thread; duke@435: ResolveForwardingClosure resolve; duke@435: duke@435: public: duke@435: PatchOopsClosure(Thread* thread) : _thread(thread) {} duke@435: duke@435: void do_object(oop obj) { duke@435: obj->oop_iterate_header(&resolve); duke@435: obj->oop_iterate(&resolve); duke@435: duke@435: assert(obj->klass()->is_shared(), "Klass not pointing into shared space."); duke@435: duke@435: // If the object is a Java object or class which might (in the duke@435: // future) contain a reference to a young gen object, add it to the duke@435: // list. duke@435: duke@435: if (obj->is_klass() || obj->is_instance()) { duke@435: if (obj->is_klass() || never@1577: obj->is_a(SystemDictionary::Class_klass()) || never@1577: obj->is_a(SystemDictionary::Throwable_klass())) { duke@435: // Do nothing duke@435: } never@1577: else if (obj->is_a(SystemDictionary::String_klass())) { duke@435: // immutable objects. duke@435: } else { duke@435: // someone added an object we hadn't accounted for. duke@435: ShouldNotReachHere(); duke@435: } duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Empty the young and old generations. duke@435: duke@435: class ClearSpaceClosure : public SpaceClosure { duke@435: public: duke@435: void do_space(Space* s) { jmasa@698: s->clear(SpaceDecorator::Mangle); duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Closure for serializing initialization data out to a data area to be duke@435: // written to the shared file. duke@435: duke@435: class WriteClosure : public SerializeOopClosure { duke@435: private: duke@435: oop* top; duke@435: char* end; duke@435: duke@435: inline void check_space() { duke@435: if ((char*)top + sizeof(oop) > end) { coleenp@2497: report_out_of_shared_space(SharedMiscData); duke@435: } duke@435: } duke@435: duke@435: duke@435: public: duke@435: WriteClosure(char* md_top, char* md_end) { duke@435: top = (oop*)md_top; duke@435: end = md_end; duke@435: } duke@435: duke@435: char* get_top() { return (char*)top; } duke@435: duke@435: void do_oop(oop* p) { duke@435: check_space(); duke@435: oop obj = *p; duke@435: assert(obj->is_oop_or_null(), "invalid oop"); duke@435: assert(obj == NULL || obj->is_shared(), duke@435: "Oop in shared space not pointing into shared space."); duke@435: *top = obj; duke@435: ++top; duke@435: } duke@435: coleenp@548: void do_oop(narrowOop* pobj) { ShouldNotReachHere(); } coleenp@548: duke@435: void do_int(int* p) { duke@435: check_space(); duke@435: *top = (oop)(intptr_t)*p; duke@435: ++top; duke@435: } duke@435: duke@435: void do_size_t(size_t* p) { duke@435: check_space(); duke@435: *top = (oop)(intptr_t)*p; duke@435: ++top; duke@435: } duke@435: duke@435: void do_ptr(void** p) { duke@435: check_space(); duke@435: *top = (oop)*p; duke@435: ++top; duke@435: } duke@435: duke@435: void do_ptr(HeapWord** p) { do_ptr((void **) p); } duke@435: duke@435: void do_tag(int tag) { duke@435: check_space(); duke@435: *top = (oop)(intptr_t)tag; duke@435: ++top; duke@435: } duke@435: duke@435: void do_region(u_char* start, size_t size) { duke@435: if ((char*)top + size > end) { coleenp@2497: report_out_of_shared_space(SharedMiscData); duke@435: } duke@435: assert((intptr_t)start % sizeof(oop) == 0, "bad alignment"); duke@435: assert(size % sizeof(oop) == 0, "bad size"); duke@435: do_tag((int)size); duke@435: while (size > 0) { duke@435: *top = *(oop*)start; duke@435: ++top; duke@435: start += sizeof(oop); duke@435: size -= sizeof(oop); duke@435: } duke@435: } duke@435: duke@435: bool reading() const { return false; } duke@435: }; duke@435: duke@435: duke@435: class ResolveConstantPoolsClosure : public ObjectClosure { duke@435: private: duke@435: TRAPS; duke@435: public: duke@435: ResolveConstantPoolsClosure(Thread *t) { duke@435: __the_thread__ = t; duke@435: } duke@435: void do_object(oop obj) { duke@435: if (obj->is_constantPool()) { duke@435: constantPoolOop cpool = (constantPoolOop)obj; duke@435: int unresolved = cpool->pre_resolve_shared_klasses(THREAD); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Print a summary of the contents of the read/write spaces to help duke@435: // identify objects which might be able to be made read-only. At this duke@435: // point, the objects have been written, and we can trash them as duke@435: // needed. duke@435: duke@435: static void print_contents() { duke@435: if (PrintSharedSpaces) { duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); duke@435: duke@435: // High level summary of the read-only space: duke@435: duke@435: ClassifyObjectClosure coc; duke@435: tty->cr(); tty->print_cr("ReadOnly space:"); duke@435: gen->ro_space()->object_iterate(&coc); duke@435: coc.print(); duke@435: duke@435: // High level summary of the read-write space: duke@435: duke@435: coc.reset(); duke@435: tty->cr(); tty->print_cr("ReadWrite space:"); duke@435: gen->rw_space()->object_iterate(&coc); duke@435: coc.print(); duke@435: duke@435: // Reset counters duke@435: duke@435: ClearAllocCountClosure cacc; duke@435: gen->ro_space()->object_iterate(&cacc); duke@435: gen->rw_space()->object_iterate(&cacc); duke@435: coc.reset(); duke@435: duke@435: // Lower level summary of the read-only space: duke@435: duke@435: gen->ro_space()->object_iterate(&coc); duke@435: tty->cr(); tty->print_cr("ReadOnly space:"); duke@435: ClassifyInstanceKlassClosure cikc; duke@435: gen->rw_space()->object_iterate(&cikc); duke@435: cikc.print(); duke@435: duke@435: // Reset counters duke@435: duke@435: gen->ro_space()->object_iterate(&cacc); duke@435: gen->rw_space()->object_iterate(&cacc); duke@435: coc.reset(); duke@435: duke@435: // Lower level summary of the read-write space: duke@435: duke@435: gen->rw_space()->object_iterate(&coc); duke@435: cikc.reset(); duke@435: tty->cr(); tty->print_cr("ReadWrite space:"); duke@435: gen->rw_space()->object_iterate(&cikc); duke@435: cikc.print(); duke@435: } duke@435: } duke@435: duke@435: duke@435: // Patch C++ vtable pointer in klass oops. duke@435: duke@435: // Klass objects contain references to c++ vtables in the JVM library. duke@435: // Fix them to point to our constructed vtables. However, don't iterate duke@435: // across the space while doing this, as that causes the vtables to be duke@435: // patched, undoing our useful work. Instead, iterate to make a list, duke@435: // then use the list to do the fixing. acorn@843: // acorn@843: // Our constructed vtables: acorn@843: // Dump time: acorn@843: // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs acorn@843: // 2. generate_vtable_methods: create jump table, appended to above vtbl_list acorn@843: // 3. PatchKlassVtables: for Klass list, patch the vtable entry to point to jump table acorn@843: // rather than to current vtbl acorn@843: // Table layout: NOTE FIXED SIZE acorn@843: // 1. vtbl pointers acorn@843: // 2. #Klass X #virtual methods per Klass acorn@843: // 1 entry for each, in the order: acorn@843: // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method entry acorn@843: // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method entry acorn@843: // ... acorn@843: // Klass:method1 entry, Klass:method2 entry, acorn@843: // ... Klass:method entry acorn@843: // Sample entry: (Sparc): acorn@843: // save(sp, -256, sp) acorn@843: // ba,pt common_code acorn@843: // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) acorn@843: // acorn@843: // Restore time: acorn@843: // 1. initialize_oops: reserve space for table acorn@843: // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text acorn@843: // acorn@843: // Execution time: acorn@843: // First virtual method call for any object of these Klass types: acorn@843: // 1. object->klass->klass_part acorn@843: // 2. vtable entry for that klass_part points to the jump table entries acorn@843: // 3. branches to common_code with %O0/klass_part, %L0: Klass index <<8 + method index acorn@843: // 4. common_code: acorn@843: // Get address of new vtbl pointer for this Klass from updated table acorn@843: // Update new vtbl pointer in the Klass: future virtual calls go direct acorn@843: // Jump to method, using new vtbl pointer and method index duke@435: duke@435: class PatchKlassVtables: public ObjectClosure { duke@435: private: duke@435: GrowableArray* _klass_objects; duke@435: duke@435: public: coleenp@2497: PatchKlassVtables() { duke@435: _klass_objects = new GrowableArray(); duke@435: } duke@435: duke@435: void do_object(oop obj) { duke@435: if (obj->is_klass()) { duke@435: _klass_objects->append(klassOop(obj)); duke@435: } duke@435: } duke@435: coleenp@2497: void patch(void** vtbl_list, void* new_vtable_start) { coleenp@2497: int n = _klass_objects->length(); coleenp@2497: for (int i = 0; i < n; i++) { duke@435: klassOop obj = (klassOop)_klass_objects->at(i); duke@435: Klass* k = obj->klass_part(); coleenp@2497: *(void**)k = CompactingPermGenGen::find_matching_vtbl_ptr( coleenp@2497: vtbl_list, new_vtable_start, k); duke@435: } duke@435: } duke@435: }; duke@435: coleenp@2497: // Walk through all symbols and patch their vtable pointers. coleenp@2497: // Note that symbols have vtable pointers only in non-product builds coleenp@2497: // (see allocation.hpp). coleenp@2497: coleenp@2497: #ifndef PRODUCT coleenp@2497: class PatchSymbolVtables: public SymbolClosure { coleenp@2497: private: coleenp@2497: void* _new_vtbl_ptr; coleenp@2497: coleenp@2497: public: coleenp@2497: PatchSymbolVtables(void** vtbl_list, void* new_vtable_start) { coleenp@2497: Symbol s; coleenp@2497: _new_vtbl_ptr = CompactingPermGenGen::find_matching_vtbl_ptr( coleenp@2497: vtbl_list, new_vtable_start, &s); coleenp@2497: } coleenp@2497: coleenp@2497: void do_symbol(Symbol** p) { coleenp@2497: Symbol* sym = load_symbol(p); coleenp@2497: *(void**)sym = _new_vtbl_ptr; coleenp@2497: } coleenp@2497: }; coleenp@2497: #endif coleenp@2497: duke@435: duke@435: // Populate the shared space. duke@435: duke@435: class VM_PopulateDumpSharedSpace: public VM_Operation { duke@435: private: duke@435: GrowableArray *_class_promote_order; duke@435: OffsetTableContigSpace* _ro_space; duke@435: OffsetTableContigSpace* _rw_space; duke@435: VirtualSpace* _md_vs; duke@435: VirtualSpace* _mc_vs; duke@435: duke@435: public: duke@435: VM_PopulateDumpSharedSpace(GrowableArray *class_promote_order, duke@435: OffsetTableContigSpace* ro_space, duke@435: OffsetTableContigSpace* rw_space, duke@435: VirtualSpace* md_vs, VirtualSpace* mc_vs) { duke@435: _class_promote_order = class_promote_order; duke@435: _ro_space = ro_space; duke@435: _rw_space = rw_space; duke@435: _md_vs = md_vs; duke@435: _mc_vs = mc_vs; duke@435: } duke@435: duke@435: VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } duke@435: void doit() { duke@435: Thread* THREAD = VMThread::vm_thread(); duke@435: NOT_PRODUCT(SystemDictionary::verify();) duke@435: // The following guarantee is meant to ensure that no loader constraints duke@435: // exist yet, since the constraints table is not shared. This becomes duke@435: // more important now that we don't re-initialize vtables/itables for duke@435: // shared classes at runtime, where constraints were previously created. duke@435: guarantee(SystemDictionary::constraints()->number_of_entries() == 0, duke@435: "loader constraints are not saved"); jrose@1145: // Revisit and implement this if we prelink method handle call sites: never@1149: guarantee(SystemDictionary::invoke_method_table() == NULL || never@1149: SystemDictionary::invoke_method_table()->number_of_entries() == 0, jrose@1145: "invoke method table is not saved"); duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: duke@435: // At this point, many classes have been loaded. duke@435: duke@435: // Update all the fingerprints in the shared methods. duke@435: duke@435: tty->print("Calculating fingerprints ... "); duke@435: FingerprintMethodsClosure fpmc; duke@435: gch->object_iterate(&fpmc); duke@435: tty->print_cr("done. "); duke@435: duke@435: // Remove all references outside the heap. duke@435: duke@435: tty->print("Removing unshareable information ... "); duke@435: RemoveUnshareableInfoClosure ruic; duke@435: gch->object_iterate(&ruic); duke@435: tty->print_cr("done. "); duke@435: duke@435: // Move the objects in three passes. duke@435: duke@435: MarkObjectsOopClosure mark_all; duke@435: MarkCommonReadOnly mark_common_ro; duke@435: MarkStringValues mark_string_values; duke@435: MarkReadWriteObjects mark_rw; duke@435: MarkStringObjects mark_strings; duke@435: MoveMarkedObjects move_ro(_ro_space, true); duke@435: MoveMarkedObjects move_rw(_rw_space, false); duke@435: duke@435: // The SharedOptimizeColdStart VM option governs the new layout duke@435: // algorithm for promoting classes into the shared archive. duke@435: // The general idea is to minimize cold start time by laying duke@435: // out the objects in the order they are accessed at startup time. duke@435: // By doing this we are trying to eliminate out-of-order accesses duke@435: // in the shared archive. This benefits cold startup time by making duke@435: // disk reads as sequential as possible during class loading and duke@435: // bootstrapping activities. There may also be a small secondary duke@435: // effect of better "packing" of more commonly used data on a smaller duke@435: // number of pages, although no direct benefit has been measured from duke@435: // this effect. duke@435: // duke@435: // At the class level of granularity, the promotion order is dictated duke@435: // by the classlist file whose generation is discussed elsewhere. duke@435: // duke@435: // At smaller granularity, optimal ordering was determined by an duke@435: // offline analysis of object access order in the shared archive. duke@435: // The dbx watchpoint facility, combined with SA post-processing, duke@435: // was used to observe common access patterns primarily during duke@435: // classloading. This information was used to craft the promotion duke@435: // order seen in the following closures. duke@435: // duke@435: // The observed access order is mostly governed by what happens duke@435: // in SystemDictionary::load_shared_class(). NOTE WELL - care duke@435: // should be taken when making changes to this method, because it duke@435: // may invalidate assumptions made about access order! duke@435: // duke@435: // (Ideally, there would be a better way to manage changes to duke@435: // the access order. Unfortunately a generic in-VM solution for duke@435: // dynamically observing access order and optimizing shared duke@435: // archive layout is pretty difficult. We go with the static duke@435: // analysis because the code is fairly mature at this point duke@435: // and we're betting that the access order won't change much.) duke@435: duke@435: MarkAndMoveOrderedReadOnly mark_and_move_ordered_ro(&move_ro); duke@435: MarkAndMoveOrderedReadWrite mark_and_move_ordered_rw(&move_rw); duke@435: duke@435: // Set up the share data and shared code segments. duke@435: duke@435: char* md_top = _md_vs->low(); duke@435: char* md_end = _md_vs->high(); duke@435: char* mc_top = _mc_vs->low(); duke@435: char* mc_end = _mc_vs->high(); duke@435: duke@435: // Reserve space for the list of klassOops whose vtables are used duke@435: // for patching others as needed. duke@435: duke@435: void** vtbl_list = (void**)md_top; duke@435: int vtbl_list_size = CompactingPermGenGen::vtbl_list_size; duke@435: Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); duke@435: duke@435: md_top += vtbl_list_size * sizeof(void*); duke@435: void* vtable = md_top; duke@435: duke@435: // Reserve space for a new dummy vtable for klass objects in the duke@435: // heap. Generate self-patching vtable entries. duke@435: duke@435: CompactingPermGenGen::generate_vtable_methods(vtbl_list, duke@435: &vtable, duke@435: &md_top, md_end, duke@435: &mc_top, mc_end); duke@435: coleenp@2497: // Reserve space for the total size and the number of stored symbols. coleenp@2497: coleenp@2497: md_top += sizeof(intptr_t) * 2; coleenp@2497: coleenp@2497: MoveSymbols move_symbols(md_top, md_end); coleenp@2497: CommonSymbolsClosure traverse_common_symbols(&move_symbols); coleenp@2497: coleenp@2497: // Phase 1a: remove symbols with _refcount == 0 coleenp@2497: coleenp@2497: SymbolTable::unlink(); coleenp@2497: coleenp@2497: // Phase 1b: move commonly used symbols referenced by oop fields. coleenp@2497: coleenp@2497: tty->print("Moving common symbols to metadata section at " PTR_FORMAT " ... ", coleenp@2497: move_symbols.get_top()); coleenp@2497: gch->object_iterate(&traverse_common_symbols); coleenp@2497: tty->print_cr("done. "); coleenp@2497: coleenp@2497: // Phase 1c: move known names and signatures. coleenp@2497: coleenp@2497: tty->print("Moving vmSymbols to metadata section at " PTR_FORMAT " ... ", coleenp@2497: move_symbols.get_top()); coleenp@2497: vmSymbols::symbols_do(&move_symbols); coleenp@2497: tty->print_cr("done. "); coleenp@2497: coleenp@2497: // Phase 1d: move the remaining symbols by scanning the whole SymbolTable. coleenp@2497: coleenp@2497: void* extra_symbols = move_symbols.get_top(); coleenp@2497: tty->print("Moving the remaining symbols to metadata section at " PTR_FORMAT " ... ", coleenp@2497: move_symbols.get_top()); coleenp@2497: SymbolTable::symbols_do(&move_symbols); coleenp@2497: tty->print_cr("done. "); coleenp@2497: coleenp@2497: // Record the total length of all symbols at the beginning of the block. coleenp@2497: ((intptr_t*)md_top)[-2] = move_symbols.get_top() - md_top; coleenp@2497: ((intptr_t*)md_top)[-1] = move_symbols.count(); coleenp@2497: tty->print_cr("Moved %d symbols, %d bytes.", coleenp@2497: move_symbols.count(), move_symbols.get_top() - md_top); coleenp@2497: // Advance the pointer to the end of symbol store. coleenp@2497: md_top = move_symbols.get_top(); coleenp@2497: coleenp@2497: coleenp@2497: // Phase 2: move commonly used read-only objects to the read-only space. coleenp@2497: coleenp@2497: if (SharedOptimizeColdStart) { coleenp@2497: tty->print("Moving pre-ordered read-only objects to shared space at " PTR_FORMAT " ... ", coleenp@2497: _ro_space->top()); coleenp@2497: for (int i = 0; i < _class_promote_order->length(); i++) { coleenp@2497: oop obj = _class_promote_order->at(i); coleenp@2497: mark_and_move_ordered_ro.do_object(obj); coleenp@2497: } coleenp@2497: tty->print_cr("done. "); coleenp@2497: } coleenp@2497: coleenp@2497: tty->print("Moving read-only objects to shared space at " PTR_FORMAT " ... ", coleenp@2497: _ro_space->top()); coleenp@2497: gch->object_iterate(&mark_common_ro); coleenp@2497: gch->object_iterate(&move_ro); coleenp@2497: tty->print_cr("done. "); coleenp@2497: coleenp@2497: // Phase 3: move String character arrays to the read-only space. coleenp@2497: coleenp@2497: tty->print("Moving string char arrays to shared space at " PTR_FORMAT " ... ", coleenp@2497: _ro_space->top()); coleenp@2497: gch->object_iterate(&mark_string_values); coleenp@2497: gch->object_iterate(&move_ro); coleenp@2497: tty->print_cr("done. "); coleenp@2497: coleenp@2497: // Phase 4: move read-write objects to the read-write space, except coleenp@2497: // Strings. coleenp@2497: coleenp@2497: if (SharedOptimizeColdStart) { coleenp@2497: tty->print("Moving pre-ordered read-write objects to shared space at " PTR_FORMAT " ... ", coleenp@2497: _rw_space->top()); coleenp@2497: for (int i = 0; i < _class_promote_order->length(); i++) { coleenp@2497: oop obj = _class_promote_order->at(i); coleenp@2497: mark_and_move_ordered_rw.do_object(obj); coleenp@2497: } coleenp@2497: tty->print_cr("done. "); coleenp@2497: } coleenp@2497: tty->print("Moving read-write objects to shared space at " PTR_FORMAT " ... ", coleenp@2497: _rw_space->top()); coleenp@2497: Universe::oops_do(&mark_all, true); coleenp@2497: SystemDictionary::oops_do(&mark_all); coleenp@2497: oop tmp = Universe::arithmetic_exception_instance(); coleenp@2497: mark_object(java_lang_Throwable::message(tmp)); coleenp@2497: gch->object_iterate(&mark_rw); coleenp@2497: gch->object_iterate(&move_rw); coleenp@2497: tty->print_cr("done. "); coleenp@2497: coleenp@2497: // Phase 5: move String objects to the read-write space. coleenp@2497: coleenp@2497: tty->print("Moving String objects to shared space at " PTR_FORMAT " ... ", coleenp@2497: _rw_space->top()); coleenp@2497: StringTable::oops_do(&mark_all); coleenp@2497: gch->object_iterate(&mark_strings); coleenp@2497: gch->object_iterate(&move_rw); coleenp@2497: tty->print_cr("done. "); coleenp@2497: tty->print_cr("Read-write space ends at " PTR_FORMAT ", %d bytes.", coleenp@2497: _rw_space->top(), _rw_space->used()); coleenp@2497: coleenp@2497: #ifdef DEBUG coleenp@2497: // Check: scan for objects which were not moved. coleenp@2497: coleenp@2497: CheckRemainingObjects check_objects; coleenp@2497: gch->object_iterate(&check_objects); coleenp@2497: check_objects.status(); coleenp@2497: #endif coleenp@2497: coleenp@2497: // Resolve forwarding in objects and saved C++ structures coleenp@2497: tty->print("Updating references to shared objects ... "); coleenp@2497: ResolveForwardingClosure resolve; coleenp@2497: Universe::oops_do(&resolve); coleenp@2497: SystemDictionary::oops_do(&resolve); coleenp@2497: StringTable::oops_do(&resolve); coleenp@2497: duke@435: // Fix (forward) all of the references in these shared objects (which duke@435: // are required to point ONLY to objects in the shared spaces). duke@435: // Also, create a list of all objects which might later contain a duke@435: // reference to a younger generation object. duke@435: duke@435: CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); duke@435: PatchOopsClosure patch(THREAD); duke@435: gen->ro_space()->object_iterate(&patch); duke@435: gen->rw_space()->object_iterate(&patch); duke@435: duke@435: // Previously method sorting was done concurrently with forwarding duke@435: // pointer resolution in the shared spaces. This imposed an ordering duke@435: // restriction in that methods were required to be promoted/patched duke@435: // before their holder classes. (Because constant pool pointers in duke@435: // methodKlasses are required to be resolved before their holder class duke@435: // is visited for sorting, otherwise methods are sorted by incorrect, duke@435: // pre-forwarding addresses.) duke@435: // duke@435: // Now, we reorder methods as a separate step after ALL forwarding duke@435: // pointer resolution, so that methods can be promoted in any order duke@435: // with respect to their holder classes. duke@435: coleenp@2497: SortMethodsClosure sort; duke@435: gen->ro_space()->object_iterate(&sort); duke@435: gen->rw_space()->object_iterate(&sort); coleenp@2497: coleenp@2777: ReinitializeTables reinit_tables(THREAD); coleenp@2777: gen->ro_space()->object_iterate(&reinit_tables); coleenp@2777: gen->rw_space()->object_iterate(&reinit_tables); duke@435: tty->print_cr("done. "); duke@435: tty->cr(); duke@435: duke@435: // Reorder the system dictionary. (Moving the symbols opps affects duke@435: // how the hash table indices are calculated.) duke@435: duke@435: SystemDictionary::reorder_dictionary(); duke@435: duke@435: // Empty the non-shared heap (because most of the objects were duke@435: // copied out, and the remainder cannot be considered valid oops). duke@435: duke@435: ClearSpaceClosure csc; duke@435: for (int i = 0; i < gch->n_gens(); ++i) { duke@435: gch->get_gen(i)->space_iterate(&csc); duke@435: } duke@435: csc.do_space(gen->the_space()); duke@435: NOT_PRODUCT(SystemDictionary::verify();) duke@435: duke@435: // Copy the String table, the symbol table, and the system duke@435: // dictionary to the shared space in usable form. Copy the hastable duke@435: // buckets first [read-write], then copy the linked lists of entries duke@435: // [read-only]. duke@435: duke@435: SymbolTable::reverse(extra_symbols); duke@435: NOT_PRODUCT(SymbolTable::verify()); duke@435: SymbolTable::copy_buckets(&md_top, md_end); duke@435: duke@435: StringTable::reverse(); duke@435: NOT_PRODUCT(StringTable::verify()); duke@435: StringTable::copy_buckets(&md_top, md_end); duke@435: duke@435: SystemDictionary::reverse(); duke@435: SystemDictionary::copy_buckets(&md_top, md_end); duke@435: duke@435: ClassLoader::verify(); duke@435: ClassLoader::copy_package_info_buckets(&md_top, md_end); duke@435: ClassLoader::verify(); duke@435: duke@435: SymbolTable::copy_table(&md_top, md_end); duke@435: StringTable::copy_table(&md_top, md_end); duke@435: SystemDictionary::copy_table(&md_top, md_end); duke@435: ClassLoader::verify(); duke@435: ClassLoader::copy_package_info_table(&md_top, md_end); duke@435: ClassLoader::verify(); duke@435: duke@435: // Print debug data. duke@435: duke@435: if (PrintSharedSpaces) { duke@435: const char* fmt = "%s space: " PTR_FORMAT " out of " PTR_FORMAT " bytes allocated at " PTR_FORMAT "."; duke@435: tty->print_cr(fmt, "ro", _ro_space->used(), _ro_space->capacity(), duke@435: _ro_space->bottom()); duke@435: tty->print_cr(fmt, "rw", _rw_space->used(), _rw_space->capacity(), duke@435: _rw_space->bottom()); duke@435: } duke@435: duke@435: // Write the oop data to the output array. duke@435: duke@435: WriteClosure wc(md_top, md_end); duke@435: CompactingPermGenGen::serialize_oops(&wc); duke@435: md_top = wc.get_top(); duke@435: duke@435: // Update the vtable pointers in all of the Klass objects in the duke@435: // heap. They should point to newly generated vtable. duke@435: coleenp@2497: PatchKlassVtables pkvt; duke@435: _rw_space->object_iterate(&pkvt); coleenp@2497: pkvt.patch(vtbl_list, vtable); coleenp@2497: coleenp@2497: #ifndef PRODUCT coleenp@2497: // Update the vtable pointers in all symbols, coleenp@2497: // but only in non-product builds where symbols DO have virtual methods. coleenp@2497: PatchSymbolVtables psvt(vtbl_list, vtable); coleenp@2497: SymbolTable::symbols_do(&psvt); coleenp@2497: #endif duke@435: duke@435: char* saved_vtbl = (char*)malloc(vtbl_list_size * sizeof(void*)); duke@435: memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); duke@435: memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); duke@435: duke@435: // Create and write the archive file that maps the shared spaces. duke@435: duke@435: FileMapInfo* mapinfo = new FileMapInfo(); duke@435: mapinfo->populate_header(gch->gen_policy()->max_alignment()); duke@435: duke@435: // Pass 1 - update file offsets in header. duke@435: mapinfo->write_header(); duke@435: mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true); duke@435: _ro_space->set_saved_mark(); duke@435: mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false); duke@435: _rw_space->set_saved_mark(); duke@435: mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(), xlu@722: pointer_delta(md_top, _md_vs->low(), sizeof(char)), xlu@722: SharedMiscDataSize, duke@435: false, false); duke@435: mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(), xlu@722: pointer_delta(mc_top, _mc_vs->low(), sizeof(char)), xlu@722: SharedMiscCodeSize, duke@435: true, true); duke@435: duke@435: // Pass 2 - write data. duke@435: mapinfo->open_for_write(); duke@435: mapinfo->write_header(); duke@435: mapinfo->write_space(CompactingPermGenGen::ro, _ro_space, true); duke@435: mapinfo->write_space(CompactingPermGenGen::rw, _rw_space, false); duke@435: mapinfo->write_region(CompactingPermGenGen::md, _md_vs->low(), xlu@722: pointer_delta(md_top, _md_vs->low(), sizeof(char)), xlu@722: SharedMiscDataSize, duke@435: false, false); duke@435: mapinfo->write_region(CompactingPermGenGen::mc, _mc_vs->low(), xlu@722: pointer_delta(mc_top, _mc_vs->low(), sizeof(char)), xlu@722: SharedMiscCodeSize, duke@435: true, true); duke@435: mapinfo->close(); duke@435: duke@435: // Summarize heap. duke@435: memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); duke@435: print_contents(); duke@435: } duke@435: }; // class VM_PopulateDumpSharedSpace duke@435: duke@435: duke@435: // Populate the shared spaces and dump to a file. duke@435: duke@435: jint CompactingPermGenGen::dump_shared(GrowableArray* class_promote_order, TRAPS) { duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: duke@435: // Calculate hash values for all of the (interned) strings to avoid duke@435: // writes to shared pages in the future. duke@435: duke@435: tty->print("Calculating hash values for String objects .. "); duke@435: StringHashCodeClosure shcc(THREAD); duke@435: StringTable::oops_do(&shcc); duke@435: tty->print_cr("done. "); duke@435: duke@435: CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen(); duke@435: VM_PopulateDumpSharedSpace op(class_promote_order, duke@435: gen->ro_space(), gen->rw_space(), duke@435: gen->md_space(), gen->mc_space()); duke@435: VMThread::execute(&op); duke@435: return JNI_OK; duke@435: } duke@435: coleenp@2497: void* CompactingPermGenGen::find_matching_vtbl_ptr(void** vtbl_list, coleenp@2497: void* new_vtable_start, coleenp@2497: void* obj) { coleenp@2497: void* old_vtbl_ptr = *(void**)obj; coleenp@2497: for (int i = 0; i < vtbl_list_size; i++) { coleenp@2497: if (vtbl_list[i] == old_vtbl_ptr) { coleenp@2497: return (void**)new_vtable_start + i * num_virtuals; coleenp@2497: } coleenp@2497: } coleenp@2497: ShouldNotReachHere(); coleenp@2497: return NULL; coleenp@2497: } coleenp@2497: duke@435: duke@435: class LinkClassesClosure : public ObjectClosure { duke@435: private: duke@435: Thread* THREAD; duke@435: duke@435: public: duke@435: LinkClassesClosure(Thread* thread) : THREAD(thread) {} duke@435: duke@435: void do_object(oop obj) { duke@435: if (obj->is_klass()) { duke@435: Klass* k = Klass::cast((klassOop) obj); duke@435: if (k->oop_is_instance()) { duke@435: instanceKlass* ik = (instanceKlass*) k; duke@435: // Link the class to cause the bytecodes to be rewritten and the duke@435: // cpcache to be created. coleenp@3368: if (ik->init_state() < instanceKlass::linked) { duke@435: ik->link_class(THREAD); duke@435: guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting"); duke@435: } duke@435: duke@435: // Create String objects from string initializer symbols. duke@435: ik->constants()->resolve_string_constants(THREAD); duke@435: guarantee(!HAS_PENDING_EXCEPTION, "exception resolving string constants"); duke@435: } duke@435: } duke@435: } duke@435: }; duke@435: duke@435: duke@435: // Support for a simple checksum of the contents of the class list duke@435: // file to prevent trivial tampering. The algorithm matches that in duke@435: // the MakeClassList program used by the J2SE build process. duke@435: #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe)) duke@435: static jlong duke@435: jsum(jlong start, const char *buf, const int len) duke@435: { duke@435: jlong h = start; duke@435: char *p = (char *)buf, *e = p + len; duke@435: while (p < e) { duke@435: char c = *p++; duke@435: if (c <= ' ') { duke@435: /* Skip spaces and control characters */ duke@435: continue; duke@435: } duke@435: h = 31 * h + c; duke@435: } duke@435: return h; duke@435: } duke@435: duke@435: duke@435: duke@435: duke@435: duke@435: // Preload classes from a list, populate the shared spaces and dump to a duke@435: // file. duke@435: duke@435: void GenCollectedHeap::preload_and_dump(TRAPS) { duke@435: TraceTime timer("Dump Shared Spaces", TraceStartupTime); duke@435: ResourceMark rm; duke@435: duke@435: // Preload classes to be shared. ikrylov@2322: // Should use some os:: method rather than fopen() here. aB. duke@435: // Construct the path to the class list (in jre/lib) duke@435: // Walk up two directories from the location of the VM and duke@435: // optionally tack on "lib" (depending on platform) duke@435: char class_list_path[JVM_MAXPATHLEN]; duke@435: os::jvm_path(class_list_path, sizeof(class_list_path)); duke@435: for (int i = 0; i < 3; i++) { duke@435: char *end = strrchr(class_list_path, *os::file_separator()); duke@435: if (end != NULL) *end = '\0'; duke@435: } duke@435: int class_list_path_len = (int)strlen(class_list_path); duke@435: if (class_list_path_len >= 3) { duke@435: if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) { duke@435: strcat(class_list_path, os::file_separator()); duke@435: strcat(class_list_path, "lib"); duke@435: } duke@435: } duke@435: strcat(class_list_path, os::file_separator()); duke@435: strcat(class_list_path, "classlist"); duke@435: duke@435: FILE* file = fopen(class_list_path, "r"); duke@435: if (file != NULL) { duke@435: jlong computed_jsum = JSUM_SEED; duke@435: jlong file_jsum = 0; duke@435: duke@435: char class_name[256]; duke@435: int class_count = 0; duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: gch->_preloading_shared_classes = true; duke@435: GrowableArray* class_promote_order = new GrowableArray(); duke@435: duke@435: // Preload (and intern) strings which will be used later. duke@435: duke@435: StringTable::intern("main", THREAD); duke@435: StringTable::intern("([Ljava/lang/String;)V", THREAD); duke@435: StringTable::intern("Ljava/lang/Class;", THREAD); duke@435: duke@435: StringTable::intern("I", THREAD); // Needed for StringBuffer persistence? duke@435: StringTable::intern("Z", THREAD); // Needed for StringBuffer persistence? duke@435: duke@435: // sun.io.Converters duke@435: static const char obj_array_sig[] = "[[Ljava/lang/Object;"; coleenp@3682: (void)SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); duke@435: duke@435: // java.util.HashMap duke@435: static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; coleenp@3682: (void)SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); duke@435: duke@435: tty->print("Loading classes to share ... "); duke@435: while ((fgets(class_name, sizeof class_name, file)) != NULL) { duke@435: if (*class_name == '#') { duke@435: jint fsh, fsl; duke@435: if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) { duke@435: file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff); duke@435: } duke@435: duke@435: continue; duke@435: } duke@435: // Remove trailing newline duke@435: size_t name_len = strlen(class_name); duke@435: class_name[name_len-1] = '\0'; duke@435: duke@435: computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1); duke@435: duke@435: // Got a class name - load it. coleenp@3682: Symbol* class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD); duke@435: guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol."); duke@435: klassOop klass = SystemDictionary::resolve_or_null(class_name_symbol, duke@435: THREAD); duke@435: guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class."); duke@435: if (klass != NULL) { duke@435: if (PrintSharedSpaces) { duke@435: tty->print_cr("Shared spaces preloaded: %s", class_name); duke@435: } duke@435: duke@435: duke@435: instanceKlass* ik = instanceKlass::cast(klass); duke@435: duke@435: // Should be class load order as per -XX:+TraceClassLoadingPreorder duke@435: class_promote_order->append(ik->as_klassOop()); duke@435: duke@435: // Link the class to cause the bytecodes to be rewritten and the duke@435: // cpcache to be created. The linking is done as soon as classes duke@435: // are loaded in order that the related data structures (klass, duke@435: // cpCache, Sting constants) are located together. duke@435: coleenp@3368: if (ik->init_state() < instanceKlass::linked) { duke@435: ik->link_class(THREAD); duke@435: guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting"); duke@435: } duke@435: duke@435: // Create String objects from string initializer symbols. duke@435: duke@435: ik->constants()->resolve_string_constants(THREAD); duke@435: duke@435: class_count++; duke@435: } else { duke@435: if (PrintSharedSpaces) { duke@435: tty->cr(); duke@435: tty->print_cr(" Preload failed: %s", class_name); duke@435: } duke@435: } duke@435: file_jsum = 0; // Checksum must be on last line of file duke@435: } duke@435: if (computed_jsum != file_jsum) { duke@435: tty->cr(); duke@435: tty->print_cr("Preload failed: checksum of class list was incorrect."); duke@435: exit(1); duke@435: } duke@435: duke@435: tty->print_cr("done. "); duke@435: duke@435: if (PrintSharedSpaces) { duke@435: tty->print_cr("Shared spaces: preloaded %d classes", class_count); duke@435: } duke@435: duke@435: // Rewrite and unlink classes. duke@435: tty->print("Rewriting and unlinking classes ... "); duke@435: // Make heap parsable duke@435: ensure_parsability(false); // arg is actually don't care duke@435: duke@435: // Link any classes which got missed. (It's not quite clear why duke@435: // they got missed.) This iteration would be unsafe if we weren't duke@435: // single-threaded at this point; however we can't do it on the VM duke@435: // thread because it requires object allocation. duke@435: LinkClassesClosure lcc(Thread::current()); duke@435: object_iterate(&lcc); jcoomes@2661: ensure_parsability(false); // arg is actually don't care duke@435: tty->print_cr("done. "); duke@435: duke@435: // Create and dump the shared spaces. duke@435: jint err = CompactingPermGenGen::dump_shared(class_promote_order, THREAD); duke@435: if (err != JNI_OK) { duke@435: fatal("Dumping shared spaces failed."); duke@435: } duke@435: duke@435: } else { duke@435: char errmsg[JVM_MAXPATHLEN]; ikrylov@2322: os::lasterror(errmsg, JVM_MAXPATHLEN); duke@435: tty->print_cr("Loading classlist failed: %s", errmsg); duke@435: exit(1); duke@435: } duke@435: duke@435: // Since various initialization steps have been undone by this process, duke@435: // it is not reasonable to continue running a java process. duke@435: exit(0); duke@435: }