aoqi@0: /* aoqi@0: * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/dictionary.hpp" iklam@7322: #include "classfile/classLoaderExt.hpp" aoqi@0: #include "classfile/loaderConstraints.hpp" aoqi@0: #include "classfile/placeholders.hpp" iklam@7089: #include "classfile/sharedClassUtil.hpp" aoqi@0: #include "classfile/symbolTable.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "memory/filemap.hpp" aoqi@0: #include "memory/gcLocker.hpp" aoqi@0: #include "memory/metaspace.hpp" aoqi@0: #include "memory/metaspaceShared.hpp" aoqi@0: #include "oops/objArrayOop.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: #include "runtime/vm_operations.hpp" aoqi@0: #include "runtime/vmThread.hpp" iklam@7322: #include "utilities/hashtable.hpp" aoqi@0: #include "utilities/hashtable.inline.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: int MetaspaceShared::_max_alignment = 0; aoqi@0: aoqi@0: ReservedSpace* MetaspaceShared::_shared_rs = NULL; aoqi@0: iklam@7089: bool MetaspaceShared::_link_classes_made_progress; iklam@7089: bool MetaspaceShared::_check_classes_made_progress; iklam@7089: bool MetaspaceShared::_has_error_classes; iklam@7089: bool MetaspaceShared::_archive_loading_failed = false; aoqi@0: // Read/write a data stream for restoring/preserving metadata pointers and aoqi@0: // miscellaneous data from/to the shared archive file. aoqi@0: aoqi@0: void MetaspaceShared::serialize(SerializeClosure* soc) { aoqi@0: int tag = 0; aoqi@0: soc->do_tag(--tag); aoqi@0: aoqi@0: // Verify the sizes of various metadata in the system. aoqi@0: soc->do_tag(sizeof(Method)); aoqi@0: soc->do_tag(sizeof(ConstMethod)); aoqi@0: soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); aoqi@0: soc->do_tag(sizeof(ConstantPool)); aoqi@0: soc->do_tag(sizeof(ConstantPoolCache)); aoqi@0: soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); aoqi@0: soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); aoqi@0: soc->do_tag(sizeof(Symbol)); aoqi@0: aoqi@0: // Dump/restore miscellaneous metadata. aoqi@0: Universe::serialize(soc, true); aoqi@0: soc->do_tag(--tag); aoqi@0: aoqi@0: // Dump/restore references to commonly used names and signatures. aoqi@0: vmSymbols::serialize(soc); aoqi@0: soc->do_tag(--tag); aoqi@0: aoqi@0: soc->do_tag(666); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // CDS code for dumping shared archive. aoqi@0: aoqi@0: // Global object for holding classes that have been loaded. Since this aoqi@0: // is run at a safepoint just before exit, this is the entire set of classes. aoqi@0: static GrowableArray* _global_klass_objects; aoqi@0: static void collect_classes(Klass* k) { aoqi@0: _global_klass_objects->append_if_missing(k); aoqi@0: if (k->oop_is_instance()) { aoqi@0: // Add in the array classes too aoqi@0: InstanceKlass* ik = InstanceKlass::cast(k); aoqi@0: ik->array_klasses_do(collect_classes); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static void remove_unshareable_in_classes() { aoqi@0: for (int i = 0; i < _global_klass_objects->length(); i++) { aoqi@0: Klass* k = _global_klass_objects->at(i); aoqi@0: k->remove_unshareable_info(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Walk all methods in the class list and assign a fingerprint. aoqi@0: // so that this part of the ConstMethod* is read only. aoqi@0: static void calculate_fingerprints() { aoqi@0: for (int i = 0; i < _global_klass_objects->length(); i++) { aoqi@0: Klass* k = _global_klass_objects->at(i); aoqi@0: if (k->oop_is_instance()) { aoqi@0: InstanceKlass* ik = InstanceKlass::cast(k); aoqi@0: for (int i = 0; i < ik->methods()->length(); i++) { aoqi@0: Method* m = ik->methods()->at(i); aoqi@0: Fingerprinter fp(m); aoqi@0: // The side effect of this call sets method's fingerprint field. aoqi@0: fp.fingerprint(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Patch C++ vtable pointer in metadata. aoqi@0: aoqi@0: // Klass and other metadata objects contain references to c++ vtables in the aoqi@0: // JVM library. aoqi@0: // Fix them to point to our constructed vtables. However, don't iterate aoqi@0: // across the space while doing this, as that causes the vtables to be aoqi@0: // patched, undoing our useful work. Instead, iterate to make a list, aoqi@0: // then use the list to do the fixing. aoqi@0: // aoqi@0: // Our constructed vtables: aoqi@0: // Dump time: aoqi@0: // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs aoqi@0: // 2. generate_vtable_methods: create jump table, appended to above vtbl_list aoqi@0: // 3. patch_klass_vtables: for Klass list, patch the vtable entry in klass and aoqi@0: // associated metadata to point to jump table rather than to current vtbl aoqi@0: // Table layout: NOTE FIXED SIZE aoqi@0: // 1. vtbl pointers aoqi@0: // 2. #Klass X #virtual methods per Klass aoqi@0: // 1 entry for each, in the order: aoqi@0: // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method entry aoqi@0: // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method entry aoqi@0: // ... aoqi@0: // Klass:method1 entry, Klass:method2 entry, aoqi@0: // ... Klass:method entry aoqi@0: // Sample entry: (Sparc): aoqi@0: // save(sp, -256, sp) aoqi@0: // ba,pt common_code aoqi@0: // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) aoqi@0: // aoqi@0: // Restore time: aoqi@0: // 1. initialize_shared_space: reserve space for table aoqi@0: // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text aoqi@0: // aoqi@0: // Execution time: aoqi@0: // First virtual method call for any object of these metadata types: aoqi@0: // 1. object->klass aoqi@0: // 2. vtable entry for that klass points to the jump table entries aoqi@0: // 3. branches to common_code with %O0/klass, %L0: Klass index <<8 + method index aoqi@0: // 4. common_code: aoqi@0: // Get address of new vtbl pointer for this Klass from updated table aoqi@0: // Update new vtbl pointer in the Klass: future virtual calls go direct aoqi@0: // Jump to method, using new vtbl pointer and method index aoqi@0: aoqi@0: aoqi@0: static void* find_matching_vtbl_ptr(void** vtbl_list, void* new_vtable_start, void* obj) { aoqi@0: void* old_vtbl_ptr = *(void**)obj; aoqi@0: for (int i = 0; i < MetaspaceShared::vtbl_list_size; i++) { aoqi@0: if (vtbl_list[i] == old_vtbl_ptr) { aoqi@0: return (void**)new_vtable_start + i * MetaspaceShared::num_virtuals; aoqi@0: } aoqi@0: } aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // Assumes the vtable is in first slot in object. aoqi@0: static void patch_klass_vtables(void** vtbl_list, void* new_vtable_start) { aoqi@0: int n = _global_klass_objects->length(); aoqi@0: for (int i = 0; i < n; i++) { aoqi@0: Klass* obj = _global_klass_objects->at(i); aoqi@0: // Note oop_is_instance() is a virtual call. After patching vtables aoqi@0: // all virtual calls on the dummy vtables will restore the original! aoqi@0: if (obj->oop_is_instance()) { aoqi@0: InstanceKlass* ik = InstanceKlass::cast(obj); aoqi@0: *(void**)ik = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, ik); aoqi@0: ConstantPool* cp = ik->constants(); aoqi@0: *(void**)cp = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, cp); aoqi@0: for (int j = 0; j < ik->methods()->length(); j++) { aoqi@0: Method* m = ik->methods()->at(j); aoqi@0: *(void**)m = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, m); aoqi@0: } aoqi@0: } else { aoqi@0: // Array klasses aoqi@0: Klass* k = obj; aoqi@0: *(void**)k = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, k); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Closure for serializing initialization data out to a data area to be aoqi@0: // written to the shared file. aoqi@0: aoqi@0: class WriteClosure : public SerializeClosure { aoqi@0: private: aoqi@0: intptr_t* top; aoqi@0: char* end; aoqi@0: aoqi@0: inline void check_space() { aoqi@0: if ((char*)top + sizeof(intptr_t) > end) { aoqi@0: report_out_of_shared_space(SharedMiscData); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: WriteClosure(char* md_top, char* md_end) { aoqi@0: top = (intptr_t*)md_top; aoqi@0: end = md_end; aoqi@0: } aoqi@0: aoqi@0: char* get_top() { return (char*)top; } aoqi@0: aoqi@0: void do_ptr(void** p) { aoqi@0: check_space(); aoqi@0: *top = (intptr_t)*p; aoqi@0: ++top; aoqi@0: } aoqi@0: aoqi@0: void do_tag(int tag) { aoqi@0: check_space(); aoqi@0: *top = (intptr_t)tag; aoqi@0: ++top; aoqi@0: } aoqi@0: aoqi@0: void do_region(u_char* start, size_t size) { aoqi@0: if ((char*)top + size > end) { aoqi@0: report_out_of_shared_space(SharedMiscData); aoqi@0: } aoqi@0: assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); aoqi@0: assert(size % sizeof(intptr_t) == 0, "bad size"); aoqi@0: do_tag((int)size); aoqi@0: while (size > 0) { aoqi@0: *top = *(intptr_t*)start; aoqi@0: ++top; aoqi@0: start += sizeof(intptr_t); aoqi@0: size -= sizeof(intptr_t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool reading() const { return false; } aoqi@0: }; aoqi@0: aoqi@0: // This is for dumping detailed statistics for the allocations aoqi@0: // in the shared spaces. aoqi@0: class DumpAllocClosure : public Metaspace::AllocRecordClosure { aoqi@0: public: aoqi@0: aoqi@0: // Here's poor man's enum inheritance aoqi@0: #define SHAREDSPACE_OBJ_TYPES_DO(f) \ aoqi@0: METASPACE_OBJ_TYPES_DO(f) \ aoqi@0: f(SymbolHashentry) \ aoqi@0: f(SymbolBuckets) \ aoqi@0: f(Other) aoqi@0: aoqi@0: #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type, aoqi@0: #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name; aoqi@0: aoqi@0: enum Type { aoqi@0: // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc aoqi@0: SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE) aoqi@0: _number_of_types aoqi@0: }; aoqi@0: aoqi@0: static const char * type_name(Type type) { aoqi@0: switch(type) { aoqi@0: SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE) aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: enum { aoqi@0: RO = 0, aoqi@0: RW = 1 aoqi@0: }; aoqi@0: aoqi@0: int _counts[2][_number_of_types]; aoqi@0: int _bytes [2][_number_of_types]; aoqi@0: int _which; aoqi@0: aoqi@0: DumpAllocClosure() { aoqi@0: memset(_counts, 0, sizeof(_counts)); aoqi@0: memset(_bytes, 0, sizeof(_bytes)); aoqi@0: }; aoqi@0: aoqi@0: void iterate_metaspace(Metaspace* space, int which) { aoqi@0: assert(which == RO || which == RW, "sanity"); aoqi@0: _which = which; aoqi@0: space->iterate(this); aoqi@0: } aoqi@0: aoqi@0: virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) { aoqi@0: assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity"); aoqi@0: _counts[_which][type] ++; aoqi@0: _bytes [_which][type] += byte_size; aoqi@0: } aoqi@0: aoqi@0: void dump_stats(int ro_all, int rw_all, int md_all, int mc_all); aoqi@0: }; aoqi@0: aoqi@0: void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) { aoqi@0: rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write aoqi@0: int other_bytes = md_all + mc_all; aoqi@0: aoqi@0: // Calculate size of data that was not allocated by Metaspace::allocate() aoqi@0: int symbol_count = _counts[RO][MetaspaceObj::SymbolType]; aoqi@0: int symhash_bytes = symbol_count * sizeof (HashtableEntry); aoqi@0: int symbuck_count = SymbolTable::the_table()->table_size(); aoqi@0: int symbuck_bytes = symbuck_count * sizeof(HashtableBucket); aoqi@0: aoqi@0: _counts[RW][SymbolHashentryType] = symbol_count; aoqi@0: _bytes [RW][SymbolHashentryType] = symhash_bytes; aoqi@0: other_bytes -= symhash_bytes; aoqi@0: aoqi@0: _counts[RW][SymbolBucketsType] = symbuck_count; aoqi@0: _bytes [RW][SymbolBucketsType] = symbuck_bytes; aoqi@0: other_bytes -= symbuck_bytes; aoqi@0: aoqi@0: // TODO: count things like dictionary, vtable, etc aoqi@0: _bytes[RW][OtherType] = other_bytes; aoqi@0: aoqi@0: // prevent divide-by-zero aoqi@0: if (ro_all < 1) { aoqi@0: ro_all = 1; aoqi@0: } aoqi@0: if (rw_all < 1) { aoqi@0: rw_all = 1; aoqi@0: } aoqi@0: aoqi@0: int all_ro_count = 0; aoqi@0: int all_ro_bytes = 0; aoqi@0: int all_rw_count = 0; aoqi@0: int all_rw_bytes = 0; aoqi@0: aoqi@0: // To make fmt_stats be a syntactic constant (for format warnings), use #define. aoqi@0: #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f" aoqi@0: const char *sep = "--------------------+---------------------------+---------------------------+--------------------------"; aoqi@0: const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %"; aoqi@0: aoqi@0: tty->print_cr("Detailed metadata info (rw includes md and mc):"); aoqi@0: tty->print_cr("%s", hdr); aoqi@0: tty->print_cr("%s", sep); aoqi@0: for (int type = 0; type < int(_number_of_types); type ++) { aoqi@0: const char *name = type_name((Type)type); aoqi@0: int ro_count = _counts[RO][type]; aoqi@0: int ro_bytes = _bytes [RO][type]; aoqi@0: int rw_count = _counts[RW][type]; aoqi@0: int rw_bytes = _bytes [RW][type]; aoqi@0: int count = ro_count + rw_count; aoqi@0: int bytes = ro_bytes + rw_bytes; aoqi@0: aoqi@0: double ro_perc = 100.0 * double(ro_bytes) / double(ro_all); aoqi@0: double rw_perc = 100.0 * double(rw_bytes) / double(rw_all); aoqi@0: double perc = 100.0 * double(bytes) / double(ro_all + rw_all); aoqi@0: aoqi@0: tty->print_cr(fmt_stats, name, aoqi@0: ro_count, ro_bytes, ro_perc, aoqi@0: rw_count, rw_bytes, rw_perc, aoqi@0: count, bytes, perc); aoqi@0: aoqi@0: all_ro_count += ro_count; aoqi@0: all_ro_bytes += ro_bytes; aoqi@0: all_rw_count += rw_count; aoqi@0: all_rw_bytes += rw_bytes; aoqi@0: } aoqi@0: aoqi@0: int all_count = all_ro_count + all_rw_count; aoqi@0: int all_bytes = all_ro_bytes + all_rw_bytes; aoqi@0: aoqi@0: double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all); aoqi@0: double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all); aoqi@0: double all_perc = 100.0 * double(all_bytes) / double(ro_all + rw_all); aoqi@0: aoqi@0: tty->print_cr("%s", sep); aoqi@0: tty->print_cr(fmt_stats, "Total", aoqi@0: all_ro_count, all_ro_bytes, all_ro_perc, aoqi@0: all_rw_count, all_rw_bytes, all_rw_perc, aoqi@0: all_count, all_bytes, all_perc); aoqi@0: aoqi@0: assert(all_ro_bytes == ro_all, "everything should have been counted"); aoqi@0: assert(all_rw_bytes == rw_all, "everything should have been counted"); aoqi@0: #undef fmt_stats aoqi@0: } aoqi@0: aoqi@0: // Populate the shared space. aoqi@0: aoqi@0: class VM_PopulateDumpSharedSpace: public VM_Operation { aoqi@0: private: aoqi@0: ClassLoaderData* _loader_data; aoqi@0: GrowableArray *_class_promote_order; aoqi@0: VirtualSpace _md_vs; aoqi@0: VirtualSpace _mc_vs; aoqi@0: aoqi@0: public: aoqi@0: VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, aoqi@0: GrowableArray *class_promote_order) : aoqi@0: _loader_data(loader_data) { aoqi@0: aoqi@0: // Split up and initialize the misc code and data spaces aoqi@0: ReservedSpace* shared_rs = MetaspaceShared::shared_rs(); aoqi@0: int metadata_size = SharedReadOnlySize+SharedReadWriteSize; aoqi@0: ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size); aoqi@0: ReservedSpace misc_section = shared_rs->last_part(metadata_size); aoqi@0: aoqi@0: // Now split into misc sections. aoqi@0: ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); aoqi@0: ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); aoqi@0: _md_vs.initialize(md_rs, SharedMiscDataSize); aoqi@0: _mc_vs.initialize(mc_rs, SharedMiscCodeSize); aoqi@0: _class_promote_order = class_promote_order; aoqi@0: } aoqi@0: aoqi@0: VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } aoqi@0: void doit(); // outline because gdb sucks aoqi@0: }; // class VM_PopulateDumpSharedSpace aoqi@0: aoqi@0: aoqi@0: void VM_PopulateDumpSharedSpace::doit() { aoqi@0: Thread* THREAD = VMThread::vm_thread(); aoqi@0: NOT_PRODUCT(SystemDictionary::verify();) aoqi@0: // The following guarantee is meant to ensure that no loader constraints aoqi@0: // exist yet, since the constraints table is not shared. This becomes aoqi@0: // more important now that we don't re-initialize vtables/itables for aoqi@0: // shared classes at runtime, where constraints were previously created. aoqi@0: guarantee(SystemDictionary::constraints()->number_of_entries() == 0, aoqi@0: "loader constraints are not saved"); aoqi@0: guarantee(SystemDictionary::placeholders()->number_of_entries() == 0, aoqi@0: "placeholders are not saved"); aoqi@0: // Revisit and implement this if we prelink method handle call sites: aoqi@0: guarantee(SystemDictionary::invoke_method_table() == NULL || aoqi@0: SystemDictionary::invoke_method_table()->number_of_entries() == 0, aoqi@0: "invoke method table is not saved"); aoqi@0: aoqi@0: // At this point, many classes have been loaded. aoqi@0: // Gather systemDictionary classes in a global array and do everything to aoqi@0: // that so we don't have to walk the SystemDictionary again. aoqi@0: _global_klass_objects = new GrowableArray(1000); aoqi@0: Universe::basic_type_classes_do(collect_classes); aoqi@0: SystemDictionary::classes_do(collect_classes); aoqi@0: aoqi@0: tty->print_cr("Number of classes %d", _global_klass_objects->length()); iklam@7089: { iklam@7089: int num_type_array = 0, num_obj_array = 0, num_inst = 0; iklam@7089: for (int i = 0; i < _global_klass_objects->length(); i++) { iklam@7089: Klass* k = _global_klass_objects->at(i); iklam@7089: if (k->oop_is_instance()) { iklam@7089: num_inst ++; iklam@7089: } else if (k->oop_is_objArray()) { iklam@7089: num_obj_array ++; iklam@7089: } else { iklam@7089: assert(k->oop_is_typeArray(), "sanity"); iklam@7089: num_type_array ++; iklam@7089: } iklam@7089: } iklam@7089: tty->print_cr(" instance classes = %5d", num_inst); iklam@7089: tty->print_cr(" obj array classes = %5d", num_obj_array); iklam@7089: tty->print_cr(" type array classes = %5d", num_type_array); iklam@7089: } aoqi@0: aoqi@0: // Update all the fingerprints in the shared methods. aoqi@0: tty->print("Calculating fingerprints ... "); aoqi@0: calculate_fingerprints(); aoqi@0: tty->print_cr("done. "); aoqi@0: aoqi@0: // Remove all references outside the metadata aoqi@0: tty->print("Removing unshareable information ... "); aoqi@0: remove_unshareable_in_classes(); aoqi@0: tty->print_cr("done. "); aoqi@0: aoqi@0: // Set up the share data and shared code segments. aoqi@0: char* md_low = _md_vs.low(); aoqi@0: char* md_top = md_low; aoqi@0: char* md_end = _md_vs.high(); aoqi@0: char* mc_low = _mc_vs.low(); aoqi@0: char* mc_top = mc_low; aoqi@0: char* mc_end = _mc_vs.high(); aoqi@0: aoqi@0: // Reserve space for the list of Klass*s whose vtables are used aoqi@0: // for patching others as needed. aoqi@0: aoqi@0: void** vtbl_list = (void**)md_top; aoqi@0: int vtbl_list_size = MetaspaceShared::vtbl_list_size; aoqi@0: Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); aoqi@0: aoqi@0: md_top += vtbl_list_size * sizeof(void*); aoqi@0: void* vtable = md_top; aoqi@0: aoqi@0: // Reserve space for a new dummy vtable for klass objects in the aoqi@0: // heap. Generate self-patching vtable entries. aoqi@0: aoqi@0: MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable, aoqi@0: &md_top, md_end, aoqi@0: &mc_top, mc_end); aoqi@0: aoqi@0: // Reorder the system dictionary. (Moving the symbols affects aoqi@0: // how the hash table indices are calculated.) aoqi@0: // Not doing this either. aoqi@0: aoqi@0: SystemDictionary::reorder_dictionary(); aoqi@0: aoqi@0: NOT_PRODUCT(SystemDictionary::verify();) aoqi@0: aoqi@0: // Copy the the symbol table, and the system dictionary to the shared aoqi@0: // space in usable form. Copy the hastable aoqi@0: // buckets first [read-write], then copy the linked lists of entries aoqi@0: // [read-only]. aoqi@0: aoqi@0: SymbolTable::reverse(md_top); aoqi@0: NOT_PRODUCT(SymbolTable::verify()); aoqi@0: SymbolTable::copy_buckets(&md_top, md_end); aoqi@0: aoqi@0: SystemDictionary::reverse(); aoqi@0: SystemDictionary::copy_buckets(&md_top, md_end); aoqi@0: aoqi@0: ClassLoader::verify(); aoqi@0: ClassLoader::copy_package_info_buckets(&md_top, md_end); aoqi@0: ClassLoader::verify(); aoqi@0: aoqi@0: SymbolTable::copy_table(&md_top, md_end); aoqi@0: SystemDictionary::copy_table(&md_top, md_end); aoqi@0: ClassLoader::verify(); aoqi@0: ClassLoader::copy_package_info_table(&md_top, md_end); aoqi@0: ClassLoader::verify(); aoqi@0: iklam@7322: ClassLoaderExt::copy_lookup_cache_to_archive(&md_top, md_end); iklam@7322: aoqi@0: // Write the other data to the output array. aoqi@0: WriteClosure wc(md_top, md_end); aoqi@0: MetaspaceShared::serialize(&wc); aoqi@0: md_top = wc.get_top(); aoqi@0: aoqi@0: // Print shared spaces all the time aoqi@0: // To make fmt_space be a syntactic constant (for format warnings), use #define. aoqi@0: #define fmt_space "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " PTR_FORMAT aoqi@0: Metaspace* ro_space = _loader_data->ro_metaspace(); aoqi@0: Metaspace* rw_space = _loader_data->rw_metaspace(); aoqi@0: aoqi@0: // Allocated size of each space (may not be all occupied) aoqi@0: const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType); aoqi@0: const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType); aoqi@0: const size_t md_alloced = md_end-md_low; aoqi@0: const size_t mc_alloced = mc_end-mc_low; aoqi@0: const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced; aoqi@0: aoqi@0: // Occupied size of each space. aoqi@0: const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType); aoqi@0: const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType); aoqi@0: const size_t md_bytes = size_t(md_top - md_low); aoqi@0: const size_t mc_bytes = size_t(mc_top - mc_low); aoqi@0: aoqi@0: // Percent of total size aoqi@0: const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes; aoqi@0: const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; aoqi@0: const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; aoqi@0: const double md_t_perc = md_bytes / double(total_bytes) * 100.0; aoqi@0: const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; aoqi@0: aoqi@0: // Percent of fullness of each space aoqi@0: const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; aoqi@0: const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; aoqi@0: const double md_u_perc = md_bytes / double(md_alloced) * 100.0; aoqi@0: const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; aoqi@0: const double total_u_perc = total_bytes / double(total_alloced) * 100.0; aoqi@0: aoqi@0: tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom()); aoqi@0: tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom()); aoqi@0: tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low); aoqi@0: tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low); aoqi@0: tty->print_cr("total : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]", aoqi@0: total_bytes, total_alloced, total_u_perc); aoqi@0: aoqi@0: // Update the vtable pointers in all of the Klass objects in the aoqi@0: // heap. They should point to newly generated vtable. aoqi@0: patch_klass_vtables(vtbl_list, vtable); aoqi@0: aoqi@0: // dunno what this is for. aoqi@0: char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass); aoqi@0: memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); aoqi@0: memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); aoqi@0: aoqi@0: // Create and write the archive file that maps the shared spaces. aoqi@0: aoqi@0: FileMapInfo* mapinfo = new FileMapInfo(); aoqi@0: mapinfo->populate_header(MetaspaceShared::max_alignment()); aoqi@0: aoqi@0: // Pass 1 - update file offsets in header. aoqi@0: mapinfo->write_header(); aoqi@0: mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); aoqi@0: mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); aoqi@0: mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), aoqi@0: pointer_delta(md_top, _md_vs.low(), sizeof(char)), aoqi@0: SharedMiscDataSize, aoqi@0: false, false); aoqi@0: mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), aoqi@0: pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), aoqi@0: SharedMiscCodeSize, aoqi@0: true, true); aoqi@0: aoqi@0: // Pass 2 - write data. aoqi@0: mapinfo->open_for_write(); aoqi@0: mapinfo->set_header_crc(mapinfo->compute_header_crc()); aoqi@0: mapinfo->write_header(); aoqi@0: mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); aoqi@0: mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); aoqi@0: mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), aoqi@0: pointer_delta(md_top, _md_vs.low(), sizeof(char)), aoqi@0: SharedMiscDataSize, aoqi@0: false, false); aoqi@0: mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), aoqi@0: pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), aoqi@0: SharedMiscCodeSize, aoqi@0: true, true); aoqi@0: mapinfo->close(); aoqi@0: aoqi@0: memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); aoqi@0: aoqi@0: if (PrintSharedSpaces) { aoqi@0: DumpAllocClosure dac; aoqi@0: dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO); aoqi@0: dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW); aoqi@0: aoqi@0: dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); aoqi@0: } aoqi@0: #undef fmt_space aoqi@0: } aoqi@0: iklam@7089: iklam@7089: void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) { aoqi@0: Klass* k = obj; aoqi@0: if (k->oop_is_instance()) { aoqi@0: InstanceKlass* ik = (InstanceKlass*) k; aoqi@0: // Link the class to cause the bytecodes to be rewritten and the iklam@7089: // cpcache to be created. Class verification is done according iklam@7089: // to -Xverify setting. iklam@7089: _link_classes_made_progress |= try_link_class(ik, THREAD); iklam@7089: guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: void MetaspaceShared::check_one_shared_class(Klass* k) { iklam@7089: if (k->oop_is_instance() && InstanceKlass::cast(k)->check_sharing_error_state()) { iklam@7089: _check_classes_made_progress = true; iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) { iklam@7089: // We need to iterate because verification may cause additional classes iklam@7089: // to be loaded. iklam@7089: do { iklam@7089: _link_classes_made_progress = false; iklam@7089: SystemDictionary::classes_do(link_one_shared_class, THREAD); iklam@7089: guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); iklam@7089: } while (_link_classes_made_progress); iklam@7089: iklam@7089: if (_has_error_classes) { iklam@7089: // Mark all classes whose super class or interfaces failed verification. iklam@7089: do { iklam@7089: // Not completely sure if we need to do this iteratively. Anyway, iklam@7089: // we should come here only if there are unverifiable classes, which iklam@7089: // shouldn't happen in normal cases. So better safe than sorry. iklam@7089: _check_classes_made_progress = false; iklam@7089: SystemDictionary::classes_do(check_one_shared_class); iklam@7089: } while (_check_classes_made_progress); iklam@7089: iklam@7089: if (IgnoreUnverifiableClassesDuringDump) { iklam@7089: // This is useful when running JCK or SQE tests. You should not iklam@7089: // enable this when running real apps. iklam@7089: SystemDictionary::remove_classes_in_error_state(); iklam@7089: } else { iklam@7089: tty->print_cr("Please remove the unverifiable classes from your class list and try again"); iklam@7089: exit(1); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: iklam@7089: void MetaspaceShared::prepare_for_dumping() { iklam@7089: ClassLoader::initialize_shared_path(); iklam@7089: FileMapInfo::allocate_classpath_entry_table(); aoqi@0: } aoqi@0: aoqi@0: // Preload classes from a list, populate the shared spaces and dump to a aoqi@0: // file. aoqi@0: void MetaspaceShared::preload_and_dump(TRAPS) { aoqi@0: TraceTime timer("Dump Shared Spaces", TraceStartupTime); aoqi@0: ResourceMark rm; aoqi@0: iklam@7089: tty->print_cr("Allocated shared space: %d bytes at " PTR_FORMAT, iklam@7089: MetaspaceShared::shared_rs()->size(), iklam@7089: MetaspaceShared::shared_rs()->base()); iklam@7089: aoqi@0: // Preload classes to be shared. aoqi@0: // Should use some os:: method rather than fopen() here. aB. iklam@7089: const char* class_list_path; iklam@7089: if (SharedClassListFile == NULL) { iklam@7089: // Construct the path to the class list (in jre/lib) iklam@7089: // Walk up two directories from the location of the VM and iklam@7089: // optionally tack on "lib" (depending on platform) iklam@7089: char class_list_path_str[JVM_MAXPATHLEN]; iklam@7089: os::jvm_path(class_list_path_str, sizeof(class_list_path_str)); iklam@7089: for (int i = 0; i < 3; i++) { iklam@7089: char *end = strrchr(class_list_path_str, *os::file_separator()); iklam@7089: if (end != NULL) *end = '\0'; iklam@7089: } iklam@7089: int class_list_path_len = (int)strlen(class_list_path_str); iklam@7089: if (class_list_path_len >= 3) { iklam@7089: if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) { iklam@7089: strcat(class_list_path_str, os::file_separator()); iklam@7089: strcat(class_list_path_str, "lib"); iklam@7089: } iklam@7089: } iklam@7089: strcat(class_list_path_str, os::file_separator()); iklam@7089: strcat(class_list_path_str, "classlist"); iklam@7089: class_list_path = class_list_path_str; iklam@7089: } else { iklam@7089: class_list_path = SharedClassListFile; aoqi@0: } iklam@7089: iklam@7089: int class_count = 0; iklam@7089: GrowableArray* class_promote_order = new GrowableArray(); iklam@7089: iklam@7089: // sun.io.Converters iklam@7089: static const char obj_array_sig[] = "[[Ljava/lang/Object;"; iklam@7089: SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); iklam@7089: iklam@7089: // java.util.HashMap iklam@7089: static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; iklam@7089: SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); iklam@7089: iklam@7089: tty->print_cr("Loading classes to share ..."); iklam@7089: _has_error_classes = false; iklam@7089: class_count += preload_and_dump(class_list_path, class_promote_order, iklam@7089: THREAD); iklam@7089: if (ExtraSharedClassListFile) { iklam@7089: class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order, iklam@7089: THREAD); aoqi@0: } iklam@7089: tty->print_cr("Loading classes to share: done."); aoqi@0: iklam@7322: ClassLoaderExt::init_lookup_cache(THREAD); iklam@7322: iklam@7089: if (PrintSharedSpaces) { iklam@7089: tty->print_cr("Shared spaces: preloaded %d classes", class_count); iklam@7089: } iklam@7089: iklam@7089: // Rewrite and link classes iklam@7089: tty->print_cr("Rewriting and linking classes ..."); iklam@7089: iklam@7089: // Link any classes which got missed. This would happen if we have loaded classes that iklam@7089: // were not explicitly specified in the classlist. E.g., if an interface implemented by class K iklam@7089: // fails verification, all other interfaces that were not specified in the classlist but iklam@7089: // are implemented by K are not verified. iklam@7089: link_and_cleanup_shared_classes(CATCH); iklam@7089: tty->print_cr("Rewriting and linking classes: done"); iklam@7089: iklam@7089: // Create and dump the shared spaces. Everything so far is loaded iklam@7089: // with the null class loader. iklam@7089: ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); iklam@7089: VM_PopulateDumpSharedSpace op(loader_data, class_promote_order); iklam@7089: VMThread::execute(&op); iklam@7089: iklam@7089: // Since various initialization steps have been undone by this process, iklam@7089: // it is not reasonable to continue running a java process. iklam@7089: exit(0); iklam@7089: } iklam@7089: iklam@7089: int MetaspaceShared::preload_and_dump(const char * class_list_path, iklam@7089: GrowableArray* class_promote_order, iklam@7089: TRAPS) { aoqi@0: FILE* file = fopen(class_list_path, "r"); iklam@7089: char class_name[256]; iklam@7089: int class_count = 0; iklam@7089: aoqi@0: if (file != NULL) { aoqi@0: while ((fgets(class_name, sizeof class_name, file)) != NULL) { iklam@7089: if (*class_name == '#') { // comment aoqi@0: continue; aoqi@0: } aoqi@0: // Remove trailing newline aoqi@0: size_t name_len = strlen(class_name); iklam@7089: if (class_name[name_len-1] == '\n') { iklam@7089: class_name[name_len-1] = '\0'; iklam@7089: } aoqi@0: aoqi@0: // Got a class name - load it. aoqi@0: TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD); aoqi@0: guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol."); aoqi@0: Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol, aoqi@0: THREAD); iklam@7089: CLEAR_PENDING_EXCEPTION; aoqi@0: if (klass != NULL) { aoqi@0: if (PrintSharedSpaces && Verbose && WizardMode) { aoqi@0: tty->print_cr("Shared spaces preloaded: %s", class_name); aoqi@0: } aoqi@0: aoqi@0: InstanceKlass* ik = InstanceKlass::cast(klass); aoqi@0: aoqi@0: // Should be class load order as per -XX:+TraceClassLoadingPreorder aoqi@0: class_promote_order->append(ik); aoqi@0: aoqi@0: // Link the class to cause the bytecodes to be rewritten and the aoqi@0: // cpcache to be created. The linking is done as soon as classes aoqi@0: // are loaded in order that the related data structures (klass and aoqi@0: // cpCache) are located together. iklam@7089: try_link_class(ik, THREAD); iklam@7089: guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); aoqi@0: aoqi@0: class_count++; aoqi@0: } else { iklam@7089: //tty->print_cr("Preload failed: %s", class_name); aoqi@0: } aoqi@0: } ccheung@7103: fclose(file); aoqi@0: } else { aoqi@0: char errmsg[JVM_MAXPATHLEN]; aoqi@0: os::lasterror(errmsg, JVM_MAXPATHLEN); aoqi@0: tty->print_cr("Loading classlist failed: %s", errmsg); aoqi@0: exit(1); aoqi@0: } aoqi@0: iklam@7089: return class_count; aoqi@0: } aoqi@0: iklam@7089: // Returns true if the class's status has changed iklam@7089: bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) { iklam@7089: assert(DumpSharedSpaces, "should only be called during dumping"); iklam@7089: if (ik->init_state() < InstanceKlass::linked) { iklam@7089: bool saved = BytecodeVerificationLocal; iklam@7089: if (!SharedClassUtil::is_shared_boot_class(ik)) { iklam@7089: // The verification decision is based on BytecodeVerificationRemote iklam@7089: // for non-system classes. Since we are using the NULL classloader iklam@7089: // to load non-system classes during dumping, we need to temporarily iklam@7089: // change BytecodeVerificationLocal to be the same as iklam@7089: // BytecodeVerificationRemote. Note this can cause the parent system iklam@7089: // classes also being verified. The extra overhead is acceptable during iklam@7089: // dumping. iklam@7089: BytecodeVerificationLocal = BytecodeVerificationRemote; iklam@7089: } iklam@7089: ik->link_class(THREAD); iklam@7089: if (HAS_PENDING_EXCEPTION) { iklam@7089: ResourceMark rm; jiangli@7363: tty->print_cr("Preload Warning: Verification failed for %s", iklam@7089: ik->external_name()); iklam@7089: CLEAR_PENDING_EXCEPTION; iklam@7089: ik->set_in_error_state(); iklam@7089: _has_error_classes = true; iklam@7089: } iklam@7089: BytecodeVerificationLocal = saved; iklam@7089: return true; iklam@7089: } else { iklam@7089: return false; iklam@7089: } iklam@7089: } aoqi@0: aoqi@0: // Closure for serializing initialization data in from a data area aoqi@0: // (ptr_array) read from the shared file. aoqi@0: aoqi@0: class ReadClosure : public SerializeClosure { aoqi@0: private: aoqi@0: intptr_t** _ptr_array; aoqi@0: aoqi@0: inline intptr_t nextPtr() { aoqi@0: return *(*_ptr_array)++; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; } aoqi@0: aoqi@0: void do_ptr(void** p) { aoqi@0: assert(*p == NULL, "initializing previous initialized pointer."); aoqi@0: intptr_t obj = nextPtr(); aoqi@0: assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, aoqi@0: "hit tag while initializing ptrs."); aoqi@0: *p = (void*)obj; aoqi@0: } aoqi@0: aoqi@0: void do_tag(int tag) { aoqi@0: int old_tag; aoqi@0: old_tag = (int)(intptr_t)nextPtr(); aoqi@0: // do_int(&old_tag); aoqi@0: assert(tag == old_tag, "old tag doesn't match"); aoqi@0: FileMapInfo::assert_mark(tag == old_tag); aoqi@0: } aoqi@0: aoqi@0: void do_region(u_char* start, size_t size) { aoqi@0: assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); aoqi@0: assert(size % sizeof(intptr_t) == 0, "bad size"); aoqi@0: do_tag((int)size); aoqi@0: while (size > 0) { aoqi@0: *(intptr_t*)start = nextPtr(); aoqi@0: start += sizeof(intptr_t); aoqi@0: size -= sizeof(intptr_t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool reading() const { return true; } aoqi@0: }; aoqi@0: aoqi@0: // Return true if given address is in the mapped shared space. aoqi@0: bool MetaspaceShared::is_in_shared_space(const void* p) { aoqi@0: return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p); aoqi@0: } aoqi@0: aoqi@0: void MetaspaceShared::print_shared_spaces() { aoqi@0: if (UseSharedSpaces) { aoqi@0: FileMapInfo::current_info()->print_shared_spaces(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Map shared spaces at requested addresses and return if succeeded. aoqi@0: // Need to keep the bounds of the ro and rw space for the Metaspace::contains aoqi@0: // call, or is_in_shared_space. aoqi@0: bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { aoqi@0: size_t image_alignment = mapinfo->alignment(); aoqi@0: aoqi@0: #ifndef _WINDOWS aoqi@0: // Map in the shared memory and then map the regions on top of it. aoqi@0: // On Windows, don't map the memory here because it will cause the aoqi@0: // mappings of the regions to fail. aoqi@0: ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); aoqi@0: if (!shared_rs.is_reserved()) return false; aoqi@0: #endif aoqi@0: aoqi@0: assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); aoqi@0: aoqi@0: char* _ro_base = NULL; aoqi@0: char* _rw_base = NULL; aoqi@0: char* _md_base = NULL; aoqi@0: char* _mc_base = NULL; aoqi@0: aoqi@0: // Map each shared region aoqi@0: if ((_ro_base = mapinfo->map_region(ro)) != NULL && aoqi@0: mapinfo->verify_region_checksum(ro) && aoqi@0: (_rw_base = mapinfo->map_region(rw)) != NULL && aoqi@0: mapinfo->verify_region_checksum(rw) && aoqi@0: (_md_base = mapinfo->map_region(md)) != NULL && aoqi@0: mapinfo->verify_region_checksum(md) && aoqi@0: (_mc_base = mapinfo->map_region(mc)) != NULL && aoqi@0: mapinfo->verify_region_checksum(mc) && iklam@7089: (image_alignment == (size_t)max_alignment()) && iklam@7089: mapinfo->validate_classpath_entry_table()) { aoqi@0: // Success (no need to do anything) aoqi@0: return true; aoqi@0: } else { aoqi@0: // If there was a failure in mapping any of the spaces, unmap the ones aoqi@0: // that succeeded aoqi@0: if (_ro_base != NULL) mapinfo->unmap_region(ro); aoqi@0: if (_rw_base != NULL) mapinfo->unmap_region(rw); aoqi@0: if (_md_base != NULL) mapinfo->unmap_region(md); aoqi@0: if (_mc_base != NULL) mapinfo->unmap_region(mc); aoqi@0: #ifndef _WINDOWS aoqi@0: // Release the entire mapped region aoqi@0: shared_rs.release(); aoqi@0: #endif aoqi@0: // If -Xshare:on is specified, print out the error message and exit VM, aoqi@0: // otherwise, set UseSharedSpaces to false and continue. iklam@7414: if (RequireSharedSpaces || PrintSharedArchiveAndExit) { iklam@7089: vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on."); aoqi@0: } else { aoqi@0: FLAG_SET_DEFAULT(UseSharedSpaces, false); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Read the miscellaneous data from the shared file, and aoqi@0: // serialize it out to its various destinations. aoqi@0: aoqi@0: void MetaspaceShared::initialize_shared_spaces() { aoqi@0: FileMapInfo *mapinfo = FileMapInfo::current_info(); aoqi@0: aoqi@0: char* buffer = mapinfo->region_base(md); aoqi@0: aoqi@0: // Skip over (reserve space for) a list of addresses of C++ vtables aoqi@0: // for Klass objects. They get filled in later. aoqi@0: aoqi@0: void** vtbl_list = (void**)buffer; aoqi@0: buffer += MetaspaceShared::vtbl_list_size * sizeof(void*); aoqi@0: Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); aoqi@0: aoqi@0: // Skip over (reserve space for) dummy C++ vtables Klass objects. aoqi@0: // They are used as is. aoqi@0: aoqi@0: intptr_t vtable_size = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: buffer += vtable_size; aoqi@0: aoqi@0: // Create the symbol table using the bucket array at this spot in the aoqi@0: // misc data space. Since the symbol table is often modified, this aoqi@0: // region (of mapped pages) will be copy-on-write. aoqi@0: aoqi@0: int symbolTableLen = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: int number_of_entries = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: SymbolTable::create_table((HashtableBucket*)buffer, symbolTableLen, aoqi@0: number_of_entries); aoqi@0: buffer += symbolTableLen; aoqi@0: aoqi@0: // Create the shared dictionary using the bucket array at this spot in aoqi@0: // the misc data space. Since the shared dictionary table is never aoqi@0: // modified, this region (of mapped pages) will be (effectively, if aoqi@0: // not explicitly) read-only. aoqi@0: aoqi@0: int sharedDictionaryLen = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: number_of_entries = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: SystemDictionary::set_shared_dictionary((HashtableBucket*)buffer, aoqi@0: sharedDictionaryLen, aoqi@0: number_of_entries); aoqi@0: buffer += sharedDictionaryLen; aoqi@0: aoqi@0: // Create the package info table using the bucket array at this spot in aoqi@0: // the misc data space. Since the package info table is never aoqi@0: // modified, this region (of mapped pages) will be (effectively, if aoqi@0: // not explicitly) read-only. aoqi@0: aoqi@0: int pkgInfoLen = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: number_of_entries = *(intptr_t*)buffer; aoqi@0: buffer += sizeof(intptr_t); aoqi@0: ClassLoader::create_package_info_table((HashtableBucket*)buffer, pkgInfoLen, aoqi@0: number_of_entries); aoqi@0: buffer += pkgInfoLen; aoqi@0: ClassLoader::verify(); aoqi@0: aoqi@0: // The following data in the shared misc data region are the linked aoqi@0: // list elements (HashtableEntry objects) for the symbol table, string aoqi@0: // table, and shared dictionary. The heap objects refered to by the aoqi@0: // symbol table, string table, and shared dictionary are permanent and aoqi@0: // unmovable. Since new entries added to the string and symbol tables aoqi@0: // are always added at the beginning of the linked lists, THESE LINKED aoqi@0: // LIST ELEMENTS ARE READ-ONLY. aoqi@0: aoqi@0: int len = *(intptr_t*)buffer; // skip over symbol table entries aoqi@0: buffer += sizeof(intptr_t); aoqi@0: buffer += len; aoqi@0: aoqi@0: len = *(intptr_t*)buffer; // skip over shared dictionary entries aoqi@0: buffer += sizeof(intptr_t); aoqi@0: buffer += len; aoqi@0: aoqi@0: len = *(intptr_t*)buffer; // skip over package info table entries aoqi@0: buffer += sizeof(intptr_t); aoqi@0: buffer += len; aoqi@0: aoqi@0: len = *(intptr_t*)buffer; // skip over package info table char[] arrays. aoqi@0: buffer += sizeof(intptr_t); aoqi@0: buffer += len; aoqi@0: iklam@7322: buffer = ClassLoaderExt::restore_lookup_cache_from_archive(buffer); iklam@7322: aoqi@0: intptr_t* array = (intptr_t*)buffer; aoqi@0: ReadClosure rc(&array); aoqi@0: serialize(&rc); aoqi@0: aoqi@0: // Close the mapinfo file aoqi@0: mapinfo->close(); iklam@7089: iklam@7089: if (PrintSharedArchiveAndExit) { iklam@7089: if (PrintSharedDictionary) { iklam@7089: tty->print_cr("\nShared classes:\n"); iklam@7089: SystemDictionary::print_shared(false); iklam@7089: } iklam@7089: if (_archive_loading_failed) { iklam@7089: tty->print_cr("archive is invalid"); iklam@7089: vm_exit(1); iklam@7089: } else { iklam@7089: tty->print_cr("archive is valid"); iklam@7089: vm_exit(0); iklam@7089: } iklam@7089: } aoqi@0: } aoqi@0: aoqi@0: // JVM/TI RedefineClasses() support: aoqi@0: bool MetaspaceShared::remap_shared_readonly_as_readwrite() { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); aoqi@0: aoqi@0: if (UseSharedSpaces) { aoqi@0: // remap the shared readonly space to shared readwrite, private aoqi@0: FileMapInfo* mapinfo = FileMapInfo::current_info(); aoqi@0: if (!mapinfo->remap_shared_readonly_as_readwrite()) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: } ccheung@7103: ccheung@7103: int MetaspaceShared::count_class(const char* classlist_file) { ccheung@7103: if (classlist_file == NULL) { ccheung@7103: return 0; ccheung@7103: } ccheung@7103: char class_name[256]; ccheung@7103: int class_count = 0; ccheung@7103: FILE* file = fopen(classlist_file, "r"); ccheung@7103: if (file != NULL) { ccheung@7103: while ((fgets(class_name, sizeof class_name, file)) != NULL) { ccheung@7103: if (*class_name == '#') { // comment ccheung@7103: continue; ccheung@7103: } ccheung@7103: class_count++; ccheung@7103: } ccheung@7103: fclose(file); ccheung@7103: } else { ccheung@7103: char errmsg[JVM_MAXPATHLEN]; ccheung@7103: os::lasterror(errmsg, JVM_MAXPATHLEN); ccheung@7103: tty->print_cr("Loading classlist failed: %s", errmsg); ccheung@7103: exit(1); ccheung@7103: } ccheung@7103: ccheung@7103: return class_count; ccheung@7103: } ccheung@7103: ccheung@7103: // the sizes are good for typical large applications that have a lot of shared ccheung@7103: // classes ccheung@7103: void MetaspaceShared::estimate_regions_size() { ccheung@7103: int class_count = count_class(SharedClassListFile); ccheung@7103: class_count += count_class(ExtraSharedClassListFile); ccheung@7103: ccheung@7103: if (class_count > LargeThresholdClassCount) { ccheung@7103: if (class_count < HugeThresholdClassCount) { ccheung@7103: SET_ESTIMATED_SIZE(Large, ReadOnly); ccheung@7103: SET_ESTIMATED_SIZE(Large, ReadWrite); ccheung@7103: SET_ESTIMATED_SIZE(Large, MiscData); ccheung@7103: SET_ESTIMATED_SIZE(Large, MiscCode); ccheung@7103: } else { ccheung@7103: SET_ESTIMATED_SIZE(Huge, ReadOnly); ccheung@7103: SET_ESTIMATED_SIZE(Huge, ReadWrite); ccheung@7103: SET_ESTIMATED_SIZE(Huge, MiscData); ccheung@7103: SET_ESTIMATED_SIZE(Huge, MiscCode); ccheung@7103: } ccheung@7103: } ccheung@7103: }