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