0N/A/*
3650N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/javaClasses.hpp"
1879N/A#include "classfile/loaderConstraints.hpp"
1879N/A#include "classfile/symbolTable.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "gc_implementation/shared/spaceDecorator.hpp"
1879N/A#include "memory/classify.hpp"
1879N/A#include "memory/filemap.hpp"
1879N/A#include "memory/oopFactory.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "oops/methodDataOop.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/javaCalls.hpp"
1879N/A#include "runtime/signature.hpp"
1879N/A#include "runtime/vmThread.hpp"
1879N/A#include "runtime/vm_operations.hpp"
1879N/A#include "utilities/copy.hpp"
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//
3829N/A// NOTE THAT we have to call java_lang_String::to_hash() to match the
3829N/A// algorithm in java.lang.String.toHash().
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;
3726N/A if (obj->klass() == SystemDictionary::String_klass() &&
3726N/A java_lang_String::has_hash_field()) {
3829N/A int hash = java_lang_String::to_hash(obj);
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
2062N/A
2062N/Aclass MoveSymbols : public SymbolClosure {
2062N/Aprivate:
2062N/A char* _start;
2062N/A char* _end;
2062N/A char* _top;
2062N/A int _count;
2062N/A
2062N/A bool in_shared_space(Symbol* sym) const {
2062N/A return (char*)sym >= _start && (char*)sym < _end;
2062N/A }
2062N/A
2062N/A Symbol* get_shared_copy(Symbol* sym) {
2062N/A return sym->refcount() > 0 ? NULL : (Symbol*)(_start - sym->refcount());
2062N/A }
2062N/A
2062N/A Symbol* make_shared_copy(Symbol* sym) {
2062N/A Symbol* new_sym = (Symbol*)_top;
2062N/A int size = sym->object_size();
2062N/A _top += size * HeapWordSize;
2062N/A if (_top <= _end) {
2062N/A Copy::disjoint_words((HeapWord*)sym, (HeapWord*)new_sym, size);
2062N/A // Encode a reference to the copy as a negative distance from _start
2062N/A // When a symbol is being copied to a shared space
2062N/A // during CDS archive creation, the original symbol is marked
2062N/A // as relocated by putting a negative value to its _refcount field,
2062N/A // This value is also used to find where exactly the shared copy is
2062N/A // (see MoveSymbols::get_shared_copy), so that the other references
2062N/A // to this symbol could be changed to point to the shared copy.
2062N/A sym->_refcount = (int)(_start - (char*)new_sym);
2062N/A // Mark the symbol in the shared archive as immortal so it is read only
2062N/A // and not refcounted.
2062N/A new_sym->_refcount = -1;
2062N/A _count++;
2062N/A } else {
2062N/A report_out_of_shared_space(SharedMiscData);
2062N/A }
2062N/A return new_sym;
2062N/A }
2062N/A
2062N/Apublic:
2062N/A MoveSymbols(char* top, char* end) :
2062N/A _start(top), _end(end), _top(top), _count(0) { }
2062N/A
2062N/A char* get_top() const { return _top; }
2062N/A int count() const { return _count; }
2062N/A
2062N/A void do_symbol(Symbol** p) {
2062N/A Symbol* sym = load_symbol(p);
2062N/A if (sym != NULL && !in_shared_space(sym)) {
2062N/A Symbol* new_sym = get_shared_copy(sym);
2062N/A if (new_sym == NULL) {
2062N/A // The symbol has not been relocated yet; copy it to _top address
2062N/A assert(sym->refcount() > 0, "should have positive reference count");
2062N/A new_sym = make_shared_copy(sym);
2062N/A }
2062N/A // Make the reference point to the shared copy of the symbol
2062N/A store_symbol(p, new_sym);
2062N/A }
2062N/A }
2062N/A};
2062N/A
2062N/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
2062N/A// Closure: mark common read-only objects
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 }
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
2062N/A// Closure: find symbol references in Java Heap objects
0N/A
2062N/Aclass CommonSymbolsClosure : public ObjectClosure {
0N/Aprivate:
2062N/A SymbolClosure* _closure;
0N/Apublic:
2062N/A CommonSymbolsClosure(SymbolClosure* closure) : _closure(closure) { }
2062N/A
0N/A void do_object(oop obj) {
0N/A
2062N/A // Traverse symbols referenced by method objects.
0N/A
0N/A if (obj->is_method()) {
0N/A methodOop m = methodOop(obj);
2062N/A constantPoolOop constants = m->constants();
2062N/A _closure->do_symbol(constants->symbol_at_addr(m->name_index()));
2062N/A _closure->do_symbol(constants->symbol_at_addr(m->signature_index()));
0N/A }
0N/A
2062N/A // Traverse symbols referenced by klass objects which are read-only.
0N/A
0N/A else if (obj->is_klass()) {
2062N/A Klass* k = Klass::cast((klassOop)obj);
2062N/A k->shared_symbols_iterate(_closure);
0N/A
0N/A if (obj->blueprint()->oop_is_instanceKlass()) {
0N/A instanceKlass* ik = instanceKlass::cast((klassOop)obj);
3638N/A instanceKlassHandle ik_h((klassOop)obj);
3638N/A InnerClassesIterator iter(ik_h);
3638N/A constantPoolOop constants = ik->constants();
3638N/A for (; !iter.done(); iter.next()) {
3638N/A int index = iter.inner_name_index();
3638N/A
3638N/A if (index != 0) {
3638N/A _closure->do_symbol(constants->symbol_at_addr(index));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
2062N/A // Traverse symbols referenced by other constantpool entries.
0N/A
2062N/A else if (obj->is_constantPool()) {
2062N/A constantPoolOop(obj)->shared_symbols_iterate(_closure);
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) {
2062N/A report_out_of_shared_space(_read_only ? SharedReadOnly : SharedReadWrite);
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 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 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()->stackmap_data(), _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
2062N/A// The methods array must be reordered by Symbol* address.
2062N/A// (See classFileParser.cpp where methods in a class are originally
2062N/A// sorted). The addresses of symbols have been changed as a result
2062N/A// of moving to the shared space.
0N/A
0N/Aclass SortMethodsClosure: public ObjectClosure {
2062N/Apublic:
2062N/A void do_object(oop obj) {
2062N/A if (obj->blueprint()->oop_is_instanceKlass()) {
2062N/A instanceKlass* ik = instanceKlass::cast((klassOop)obj);
2062N/A methodOopDesc::sort_methods(ik->methods(),
2062N/A ik->methods_annotations(),
2062N/A ik->methods_parameter_annotations(),
2062N/A ik->methods_default_annotations(),
2062N/A true /* idempotent, slow */);
2062N/A }
2062N/A }
2062N/A};
2062N/A
2342N/A// Vtable and Itable indices are calculated based on methods array
2342N/A// order (see klassItable::compute_itable_index()). Must reinitialize
2062N/A// after ALL methods of ALL classes have been reordered.
2062N/A// We assume that since checkconstraints is false, this method
2062N/A// cannot throw an exception. An exception here would be
2062N/A// problematic since this is the VMThread, not a JavaThread.
2062N/A
2342N/Aclass ReinitializeTables: public ObjectClosure {
0N/Aprivate:
0N/A Thread* _thread;
0N/A
0N/Apublic:
2342N/A ReinitializeTables(Thread* thread) : _thread(thread) {}
2342N/A
2342N/A // Initialize super vtable first, check if already initialized to avoid
2342N/A // quadradic behavior. The vtable is cleared in remove_unshareable_info.
2342N/A void reinitialize_vtables(klassOop k) {
2342N/A if (k->blueprint()->oop_is_instanceKlass()) {
2342N/A instanceKlass* ik = instanceKlass::cast(k);
2342N/A if (ik->vtable()->is_initialized()) return;
2342N/A if (ik->super() != NULL) {
2342N/A reinitialize_vtables(ik->super());
2342N/A }
2342N/A ik->vtable()->initialize_vtable(false, _thread);
2342N/A }
2342N/A }
0N/A
0N/A void do_object(oop obj) {
0N/A if (obj->blueprint()->oop_is_instanceKlass()) {
0N/A instanceKlass* ik = instanceKlass::cast((klassOop)obj);
2342N/A ResourceMark rm(_thread);
2062N/A ik->itable()->initialize_itable(false, _thread);
2342N/A reinitialize_vtables((klassOop)obj);
2342N/A#ifdef ASSERT
2342N/A ik->vtable()->verify(tty, true);
2342N/A#endif // ASSERT
2342N/A } else if (obj->blueprint()->oop_is_arrayKlass()) {
2342N/A // The vtable for array klasses are that of its super class,
2342N/A // ie. java.lang.Object.
2342N/A arrayKlass* ak = arrayKlass::cast((klassOop)obj);
2342N/A if (ak->vtable()->is_initialized()) return;
2342N/A ak->vtable()->initialize_vtable(false, _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() ||
1142N/A obj->is_a(SystemDictionary::Class_klass()) ||
1142N/A obj->is_a(SystemDictionary::Throwable_klass())) {
0N/A // Do nothing
0N/A }
1142N/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 inline void check_space() {
0N/A if ((char*)top + sizeof(oop) > end) {
2062N/A report_out_of_shared_space(SharedMiscData);
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) {
2062N/A report_out_of_shared_space(SharedMiscData);
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 GrowableArray<klassOop>* _klass_objects;
0N/A
0N/Apublic:
2062N/A PatchKlassVtables() {
0N/A _klass_objects = new GrowableArray<klassOop>();
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
2062N/A void patch(void** vtbl_list, void* new_vtable_start) {
2062N/A int n = _klass_objects->length();
2062N/A for (int i = 0; i < n; i++) {
0N/A klassOop obj = (klassOop)_klass_objects->at(i);
0N/A Klass* k = obj->klass_part();
2062N/A *(void**)k = CompactingPermGenGen::find_matching_vtbl_ptr(
2062N/A vtbl_list, new_vtable_start, k);
0N/A }
0N/A }
0N/A};
0N/A
2062N/A// Walk through all symbols and patch their vtable pointers.
2062N/A// Note that symbols have vtable pointers only in non-product builds
2062N/A// (see allocation.hpp).
2062N/A
2062N/A#ifndef PRODUCT
2062N/Aclass PatchSymbolVtables: public SymbolClosure {
2062N/Aprivate:
2062N/A void* _new_vtbl_ptr;
2062N/A
2062N/Apublic:
2062N/A PatchSymbolVtables(void** vtbl_list, void* new_vtable_start) {
2062N/A Symbol s;
2062N/A _new_vtbl_ptr = CompactingPermGenGen::find_matching_vtbl_ptr(
2062N/A vtbl_list, new_vtable_start, &s);
2062N/A }
2062N/A
2062N/A void do_symbol(Symbol** p) {
2062N/A Symbol* sym = load_symbol(p);
2062N/A *(void**)sym = _new_vtbl_ptr;
2062N/A }
2062N/A};
2062N/A#endif
2062N/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");
710N/A // Revisit and implement this if we prelink method handle call sites:
714N/A guarantee(SystemDictionary::invoke_method_table() == NULL ||
714N/A SystemDictionary::invoke_method_table()->number_of_entries() == 0,
710N/A "invoke method table is 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 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 // 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
2062N/A // Reserve space for the total size and the number of stored symbols.
2062N/A
2062N/A md_top += sizeof(intptr_t) * 2;
2062N/A
2062N/A MoveSymbols move_symbols(md_top, md_end);
2062N/A CommonSymbolsClosure traverse_common_symbols(&move_symbols);
2062N/A
2062N/A // Phase 1a: remove symbols with _refcount == 0
2062N/A
2062N/A SymbolTable::unlink();
2062N/A
2062N/A // Phase 1b: move commonly used symbols referenced by oop fields.
2062N/A
2062N/A tty->print("Moving common symbols to metadata section at " PTR_FORMAT " ... ",
2062N/A move_symbols.get_top());
2062N/A gch->object_iterate(&traverse_common_symbols);
2062N/A tty->print_cr("done. ");
2062N/A
2062N/A // Phase 1c: move known names and signatures.
2062N/A
2062N/A tty->print("Moving vmSymbols to metadata section at " PTR_FORMAT " ... ",
2062N/A move_symbols.get_top());
2062N/A vmSymbols::symbols_do(&move_symbols);
2062N/A tty->print_cr("done. ");
2062N/A
2062N/A // Phase 1d: move the remaining symbols by scanning the whole SymbolTable.
2062N/A
2062N/A void* extra_symbols = move_symbols.get_top();
2062N/A tty->print("Moving the remaining symbols to metadata section at " PTR_FORMAT " ... ",
2062N/A move_symbols.get_top());
2062N/A SymbolTable::symbols_do(&move_symbols);
2062N/A tty->print_cr("done. ");
2062N/A
2062N/A // Record the total length of all symbols at the beginning of the block.
2062N/A ((intptr_t*)md_top)[-2] = move_symbols.get_top() - md_top;
2062N/A ((intptr_t*)md_top)[-1] = move_symbols.count();
2062N/A tty->print_cr("Moved %d symbols, %d bytes.",
2062N/A move_symbols.count(), move_symbols.get_top() - md_top);
2062N/A // Advance the pointer to the end of symbol store.
2062N/A md_top = move_symbols.get_top();
2062N/A
2062N/A
2062N/A // Phase 2: move commonly used read-only objects to the read-only space.
2062N/A
2062N/A if (SharedOptimizeColdStart) {
2062N/A tty->print("Moving pre-ordered read-only objects to shared space at " PTR_FORMAT " ... ",
2062N/A _ro_space->top());
2062N/A for (int i = 0; i < _class_promote_order->length(); i++) {
2062N/A oop obj = _class_promote_order->at(i);
2062N/A mark_and_move_ordered_ro.do_object(obj);
2062N/A }
2062N/A tty->print_cr("done. ");
2062N/A }
2062N/A
2062N/A tty->print("Moving read-only objects to shared space at " PTR_FORMAT " ... ",
2062N/A _ro_space->top());
2062N/A gch->object_iterate(&mark_common_ro);
2062N/A gch->object_iterate(&move_ro);
2062N/A tty->print_cr("done. ");
2062N/A
2062N/A // Phase 3: move String character arrays to the read-only space.
2062N/A
2062N/A tty->print("Moving string char arrays to shared space at " PTR_FORMAT " ... ",
2062N/A _ro_space->top());
2062N/A gch->object_iterate(&mark_string_values);
2062N/A gch->object_iterate(&move_ro);
2062N/A tty->print_cr("done. ");
2062N/A
2062N/A // Phase 4: move read-write objects to the read-write space, except
2062N/A // Strings.
2062N/A
2062N/A if (SharedOptimizeColdStart) {
2062N/A tty->print("Moving pre-ordered read-write objects to shared space at " PTR_FORMAT " ... ",
2062N/A _rw_space->top());
2062N/A for (int i = 0; i < _class_promote_order->length(); i++) {
2062N/A oop obj = _class_promote_order->at(i);
2062N/A mark_and_move_ordered_rw.do_object(obj);
2062N/A }
2062N/A tty->print_cr("done. ");
2062N/A }
2062N/A tty->print("Moving read-write objects to shared space at " PTR_FORMAT " ... ",
2062N/A _rw_space->top());
2062N/A Universe::oops_do(&mark_all, true);
2062N/A SystemDictionary::oops_do(&mark_all);
2062N/A oop tmp = Universe::arithmetic_exception_instance();
2062N/A mark_object(java_lang_Throwable::message(tmp));
2062N/A gch->object_iterate(&mark_rw);
2062N/A gch->object_iterate(&move_rw);
2062N/A tty->print_cr("done. ");
2062N/A
2062N/A // Phase 5: move String objects to the read-write space.
2062N/A
2062N/A tty->print("Moving String objects to shared space at " PTR_FORMAT " ... ",
2062N/A _rw_space->top());
2062N/A StringTable::oops_do(&mark_all);
2062N/A gch->object_iterate(&mark_strings);
2062N/A gch->object_iterate(&move_rw);
2062N/A tty->print_cr("done. ");
2062N/A tty->print_cr("Read-write space ends at " PTR_FORMAT ", %d bytes.",
2062N/A _rw_space->top(), _rw_space->used());
2062N/A
2062N/A#ifdef DEBUG
2062N/A // Check: scan for objects which were not moved.
2062N/A
2062N/A CheckRemainingObjects check_objects;
2062N/A gch->object_iterate(&check_objects);
2062N/A check_objects.status();
2062N/A#endif
2062N/A
2062N/A // Resolve forwarding in objects and saved C++ structures
2062N/A tty->print("Updating references to shared objects ... ");
2062N/A ResolveForwardingClosure resolve;
2062N/A Universe::oops_do(&resolve);
2062N/A SystemDictionary::oops_do(&resolve);
2062N/A StringTable::oops_do(&resolve);
2062N/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
2062N/A SortMethodsClosure sort;
0N/A gen->ro_space()->object_iterate(&sort);
0N/A gen->rw_space()->object_iterate(&sort);
2062N/A
2342N/A ReinitializeTables reinit_tables(THREAD);
2342N/A gen->ro_space()->object_iterate(&reinit_tables);
2342N/A gen->rw_space()->object_iterate(&reinit_tables);
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
2062N/A PatchKlassVtables pkvt;
0N/A _rw_space->object_iterate(&pkvt);
2062N/A pkvt.patch(vtbl_list, vtable);
2062N/A
2062N/A#ifndef PRODUCT
2062N/A // Update the vtable pointers in all symbols,
2062N/A // but only in non-product builds where symbols DO have virtual methods.
2062N/A PatchSymbolVtables psvt(vtbl_list, vtable);
2062N/A SymbolTable::symbols_do(&psvt);
2062N/A#endif
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
2062N/Avoid* CompactingPermGenGen::find_matching_vtbl_ptr(void** vtbl_list,
2062N/A void* new_vtable_start,
2062N/A void* obj) {
2062N/A void* old_vtbl_ptr = *(void**)obj;
2062N/A for (int i = 0; i < vtbl_list_size; i++) {
2062N/A if (vtbl_list[i] == old_vtbl_ptr) {
2062N/A return (void**)new_vtable_start + i * num_virtuals;
2062N/A }
2062N/A }
2062N/A ShouldNotReachHere();
2062N/A return NULL;
2062N/A}
2062N/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.
3019N/A if (ik->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.
1887N/A // Should use some os:: 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;";
3650N/A (void)SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
0N/A
0N/A // java.util.HashMap
0N/A static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
3650N/A (void)SymbolTable::new_permanent_symbol(map_entry_array_sig, 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.
3650N/A Symbol* class_name_symbol = SymbolTable::new_permanent_symbol(class_name, 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
3019N/A if (ik->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);
2226N/A ensure_parsability(false); // arg is actually don't care
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];
1887N/A os::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}