coleenp@4037: /* coleenp@4434: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. coleenp@4037: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@4037: * coleenp@4037: * This code is free software; you can redistribute it and/or modify it coleenp@4037: * under the terms of the GNU General Public License version 2 only, as coleenp@4037: * published by the Free Software Foundation. coleenp@4037: * coleenp@4037: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@4037: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@4037: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@4037: * version 2 for more details (a copy is included in the LICENSE file that coleenp@4037: * accompanied this code). coleenp@4037: * coleenp@4037: * You should have received a copy of the GNU General Public License version coleenp@4037: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@4037: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@4037: * coleenp@4037: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@4037: * or visit www.oracle.com if you need additional information or have any coleenp@4037: * questions. coleenp@4037: * coleenp@4037: */ coleenp@4037: coleenp@4037: #include "precompiled.hpp" coleenp@4037: #include "classfile/dictionary.hpp" coleenp@4037: #include "classfile/loaderConstraints.hpp" coleenp@4037: #include "classfile/placeholders.hpp" coleenp@4037: #include "classfile/symbolTable.hpp" coleenp@4037: #include "classfile/systemDictionary.hpp" coleenp@4037: #include "code/codeCache.hpp" coleenp@4037: #include "memory/filemap.hpp" coleenp@4037: #include "memory/gcLocker.hpp" coleenp@4037: #include "memory/metaspace.hpp" coleenp@4037: #include "memory/metaspaceShared.hpp" coleenp@4037: #include "oops/objArrayOop.hpp" coleenp@4037: #include "oops/oop.inline.hpp" coleenp@4037: #include "runtime/signature.hpp" coleenp@4037: #include "runtime/vm_operations.hpp" coleenp@4037: #include "runtime/vmThread.hpp" coleenp@4037: #include "utilities/hashtable.inline.hpp" coleenp@4037: coleenp@4037: coleenp@4037: int MetaspaceShared::_max_alignment = 0; coleenp@4037: coleenp@4037: ReservedSpace* MetaspaceShared::_shared_rs = NULL; coleenp@4037: coleenp@4037: // Read/write a data stream for restoring/preserving metadata pointers and coleenp@4037: // miscellaneous data from/to the shared archive file. coleenp@4037: coleenp@4037: void MetaspaceShared::serialize(SerializeClosure* soc) { coleenp@4037: int tag = 0; coleenp@4037: soc->do_tag(--tag); coleenp@4037: coleenp@4037: assert(!UseCompressedOops, "UseCompressedOops doesn't work with shared archive"); coleenp@4037: // Verify the sizes of various metadata in the system. coleenp@4037: soc->do_tag(sizeof(Method)); coleenp@4037: soc->do_tag(sizeof(ConstMethod)); coleenp@4037: soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); coleenp@4037: soc->do_tag(sizeof(ConstantPool)); coleenp@4037: soc->do_tag(sizeof(ConstantPoolCache)); coleenp@4037: soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); coleenp@4037: soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); coleenp@4037: soc->do_tag(sizeof(Symbol)); coleenp@4037: coleenp@4037: // Dump/restore miscellaneous metadata. coleenp@4037: Universe::serialize(soc, true); coleenp@4037: soc->do_tag(--tag); coleenp@4037: coleenp@4037: // Dump/restore references to commonly used names and signatures. coleenp@4037: vmSymbols::serialize(soc); coleenp@4037: soc->do_tag(--tag); coleenp@4037: coleenp@4037: soc->do_tag(666); coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // CDS code for dumping shared archive. coleenp@4037: coleenp@4037: // Global object for holding classes that have been loaded. Since this coleenp@4037: // is run at a safepoint just before exit, this is the entire set of classes. coleenp@4037: static GrowableArray* _global_klass_objects; coleenp@4037: static void collect_classes(Klass* k) { coleenp@4037: _global_klass_objects->append_if_missing(k); coleenp@4037: if (k->oop_is_instance()) { coleenp@4037: // Add in the array classes too coleenp@4037: InstanceKlass* ik = InstanceKlass::cast(k); coleenp@4037: ik->array_klasses_do(collect_classes); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: static void remove_unshareable_in_classes() { coleenp@4037: for (int i = 0; i < _global_klass_objects->length(); i++) { coleenp@4037: Klass* k = _global_klass_objects->at(i); coleenp@4037: k->remove_unshareable_info(); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Walk all methods in the class list and assign a fingerprint. coleenp@4037: // so that this part of the ConstMethod* is read only. coleenp@4037: static void calculate_fingerprints() { coleenp@4037: for (int i = 0; i < _global_klass_objects->length(); i++) { coleenp@4037: Klass* k = _global_klass_objects->at(i); coleenp@4037: if (k->oop_is_instance()) { coleenp@4037: InstanceKlass* ik = InstanceKlass::cast(k); coleenp@4037: for (int i = 0; i < ik->methods()->length(); i++) { coleenp@4037: ResourceMark rm; coleenp@4037: Method* m = ik->methods()->at(i); coleenp@4037: (new Fingerprinter(m))->fingerprint(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Patch C++ vtable pointer in metadata. coleenp@4037: coleenp@4037: // Klass and other metadata objects contain references to c++ vtables in the coleenp@4037: // JVM library. coleenp@4037: // Fix them to point to our constructed vtables. However, don't iterate coleenp@4037: // across the space while doing this, as that causes the vtables to be coleenp@4037: // patched, undoing our useful work. Instead, iterate to make a list, coleenp@4037: // then use the list to do the fixing. coleenp@4037: // coleenp@4037: // Our constructed vtables: coleenp@4037: // Dump time: coleenp@4037: // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs coleenp@4037: // 2. generate_vtable_methods: create jump table, appended to above vtbl_list coleenp@4037: // 3. patch_klass_vtables: for Klass list, patch the vtable entry in klass and coleenp@4037: // associated metadata to point to jump table rather than to current vtbl coleenp@4037: // Table layout: NOTE FIXED SIZE coleenp@4037: // 1. vtbl pointers coleenp@4037: // 2. #Klass X #virtual methods per Klass coleenp@4037: // 1 entry for each, in the order: coleenp@4037: // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method entry coleenp@4037: // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method entry coleenp@4037: // ... coleenp@4037: // Klass:method1 entry, Klass:method2 entry, coleenp@4037: // ... Klass:method entry coleenp@4037: // Sample entry: (Sparc): coleenp@4037: // save(sp, -256, sp) coleenp@4037: // ba,pt common_code coleenp@4037: // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) coleenp@4037: // coleenp@4037: // Restore time: coleenp@4037: // 1. initialize_shared_space: reserve space for table coleenp@4037: // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text coleenp@4037: // coleenp@4037: // Execution time: coleenp@4037: // First virtual method call for any object of these metadata types: coleenp@4045: // 1. object->klass coleenp@4045: // 2. vtable entry for that klass points to the jump table entries coleenp@4045: // 3. branches to common_code with %O0/klass, %L0: Klass index <<8 + method index coleenp@4037: // 4. common_code: coleenp@4037: // Get address of new vtbl pointer for this Klass from updated table coleenp@4037: // Update new vtbl pointer in the Klass: future virtual calls go direct coleenp@4037: // Jump to method, using new vtbl pointer and method index coleenp@4037: coleenp@4037: coleenp@4037: static void* find_matching_vtbl_ptr(void** vtbl_list, void* new_vtable_start, void* obj) { coleenp@4037: void* old_vtbl_ptr = *(void**)obj; coleenp@4037: for (int i = 0; i < MetaspaceShared::vtbl_list_size; i++) { coleenp@4037: if (vtbl_list[i] == old_vtbl_ptr) { coleenp@4037: return (void**)new_vtable_start + i * MetaspaceShared::num_virtuals; coleenp@4037: } coleenp@4037: } coleenp@4037: ShouldNotReachHere(); coleenp@4037: return NULL; coleenp@4037: } coleenp@4037: coleenp@4037: // Assumes the vtable is in first slot in object. coleenp@4037: static void patch_klass_vtables(void** vtbl_list, void* new_vtable_start) { coleenp@4037: int n = _global_klass_objects->length(); coleenp@4037: for (int i = 0; i < n; i++) { coleenp@4037: Klass* obj = _global_klass_objects->at(i); coleenp@4037: // Note oop_is_instance() is a virtual call. After patching vtables coleenp@4037: // all virtual calls on the dummy vtables will restore the original! coleenp@4037: if (obj->oop_is_instance()) { coleenp@4037: InstanceKlass* ik = InstanceKlass::cast(obj); coleenp@4037: *(void**)ik = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, ik); coleenp@4037: ConstantPool* cp = ik->constants(); coleenp@4037: *(void**)cp = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, cp); coleenp@4037: for (int j = 0; j < ik->methods()->length(); j++) { coleenp@4037: Method* m = ik->methods()->at(j); coleenp@4037: *(void**)m = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, m); coleenp@4037: } coleenp@4037: } else { coleenp@4037: // Array klasses coleenp@4037: Klass* k = obj; coleenp@4037: *(void**)k = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, k); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Closure for serializing initialization data out to a data area to be coleenp@4037: // written to the shared file. coleenp@4037: coleenp@4037: class WriteClosure : public SerializeClosure { coleenp@4037: private: coleenp@4037: intptr_t* top; coleenp@4037: char* end; coleenp@4037: coleenp@4037: inline void check_space() { coleenp@4037: if ((char*)top + sizeof(intptr_t) > end) { coleenp@4037: report_out_of_shared_space(SharedMiscData); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: public: coleenp@4037: WriteClosure(char* md_top, char* md_end) { coleenp@4037: top = (intptr_t*)md_top; coleenp@4037: end = md_end; coleenp@4037: } coleenp@4037: coleenp@4037: char* get_top() { return (char*)top; } coleenp@4037: coleenp@4037: void do_ptr(void** p) { coleenp@4037: check_space(); coleenp@4037: *top = (intptr_t)*p; coleenp@4037: ++top; coleenp@4037: } coleenp@4037: coleenp@4037: void do_tag(int tag) { coleenp@4037: check_space(); coleenp@4037: *top = (intptr_t)tag; coleenp@4037: ++top; coleenp@4037: } coleenp@4037: coleenp@4037: void do_region(u_char* start, size_t size) { coleenp@4037: if ((char*)top + size > end) { coleenp@4037: report_out_of_shared_space(SharedMiscData); coleenp@4037: } coleenp@4037: assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); coleenp@4037: assert(size % sizeof(intptr_t) == 0, "bad size"); coleenp@4037: do_tag((int)size); coleenp@4037: while (size > 0) { coleenp@4037: *top = *(intptr_t*)start; coleenp@4037: ++top; coleenp@4037: start += sizeof(intptr_t); coleenp@4037: size -= sizeof(intptr_t); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: bool reading() const { return false; } coleenp@4037: }; coleenp@4037: coleenp@4037: coleenp@4037: // Populate the shared space. coleenp@4037: coleenp@4037: class VM_PopulateDumpSharedSpace: public VM_Operation { coleenp@4037: private: coleenp@4037: ClassLoaderData* _loader_data; coleenp@4037: GrowableArray *_class_promote_order; coleenp@4037: VirtualSpace _md_vs; coleenp@4037: VirtualSpace _mc_vs; coleenp@4037: coleenp@4037: public: coleenp@4037: VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, coleenp@4037: GrowableArray *class_promote_order) : coleenp@4037: _loader_data(loader_data) { coleenp@4037: coleenp@4037: // Split up and initialize the misc code and data spaces coleenp@4037: ReservedSpace* shared_rs = MetaspaceShared::shared_rs(); coleenp@4037: int metadata_size = SharedReadOnlySize+SharedReadWriteSize; coleenp@4037: ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size); coleenp@4037: ReservedSpace misc_section = shared_rs->last_part(metadata_size); coleenp@4037: coleenp@4037: // Now split into misc sections. coleenp@4037: ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); coleenp@4037: ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); coleenp@4037: _md_vs.initialize(md_rs, SharedMiscDataSize); coleenp@4037: _mc_vs.initialize(mc_rs, SharedMiscCodeSize); coleenp@4037: _class_promote_order = class_promote_order; coleenp@4037: } coleenp@4037: coleenp@4037: VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } coleenp@4037: void doit(); // outline because gdb sucks coleenp@4037: }; // class VM_PopulateDumpSharedSpace coleenp@4037: coleenp@4037: coleenp@4037: void VM_PopulateDumpSharedSpace::doit() { coleenp@4037: Thread* THREAD = VMThread::vm_thread(); coleenp@4037: NOT_PRODUCT(SystemDictionary::verify();) coleenp@4037: // The following guarantee is meant to ensure that no loader constraints coleenp@4037: // exist yet, since the constraints table is not shared. This becomes coleenp@4037: // more important now that we don't re-initialize vtables/itables for coleenp@4037: // shared classes at runtime, where constraints were previously created. coleenp@4037: guarantee(SystemDictionary::constraints()->number_of_entries() == 0, coleenp@4037: "loader constraints are not saved"); coleenp@4037: guarantee(SystemDictionary::placeholders()->number_of_entries() == 0, coleenp@4037: "placeholders are not saved"); coleenp@4037: // Revisit and implement this if we prelink method handle call sites: coleenp@4037: guarantee(SystemDictionary::invoke_method_table() == NULL || coleenp@4037: SystemDictionary::invoke_method_table()->number_of_entries() == 0, coleenp@4037: "invoke method table is not saved"); coleenp@4037: coleenp@4037: // At this point, many classes have been loaded. coleenp@4037: // Gather systemDictionary classes in a global array and do everything to coleenp@4037: // that so we don't have to walk the SystemDictionary again. coleenp@4037: _global_klass_objects = new GrowableArray(1000); coleenp@4037: Universe::basic_type_classes_do(collect_classes); coleenp@4037: SystemDictionary::classes_do(collect_classes); coleenp@4037: coleenp@4037: tty->print_cr("Number of classes %d", _global_klass_objects->length()); coleenp@4037: coleenp@4037: // Update all the fingerprints in the shared methods. coleenp@4037: tty->print("Calculating fingerprints ... "); coleenp@4037: calculate_fingerprints(); coleenp@4037: tty->print_cr("done. "); coleenp@4037: coleenp@4037: // Remove all references outside the metadata coleenp@4037: tty->print("Removing unshareable information ... "); coleenp@4037: remove_unshareable_in_classes(); coleenp@4037: tty->print_cr("done. "); coleenp@4037: coleenp@4037: // Set up the share data and shared code segments. coleenp@4037: char* md_low = _md_vs.low(); coleenp@4037: char* md_top = md_low; coleenp@4037: char* md_end = _md_vs.high(); coleenp@4037: char* mc_low = _mc_vs.low(); coleenp@4037: char* mc_top = mc_low; coleenp@4037: char* mc_end = _mc_vs.high(); coleenp@4037: coleenp@4037: // Reserve space for the list of Klass*s whose vtables are used coleenp@4037: // for patching others as needed. coleenp@4037: coleenp@4037: void** vtbl_list = (void**)md_top; coleenp@4037: int vtbl_list_size = MetaspaceShared::vtbl_list_size; coleenp@4037: Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); coleenp@4037: coleenp@4037: md_top += vtbl_list_size * sizeof(void*); coleenp@4037: void* vtable = md_top; coleenp@4037: coleenp@4037: // Reserve space for a new dummy vtable for klass objects in the coleenp@4037: // heap. Generate self-patching vtable entries. coleenp@4037: coleenp@4037: MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable, coleenp@4037: &md_top, md_end, coleenp@4037: &mc_top, mc_end); coleenp@4037: coleenp@4037: // Reorder the system dictionary. (Moving the symbols affects coleenp@4037: // how the hash table indices are calculated.) coleenp@4037: // Not doing this either. coleenp@4037: coleenp@4037: SystemDictionary::reorder_dictionary(); coleenp@4037: coleenp@4037: NOT_PRODUCT(SystemDictionary::verify();) coleenp@4037: coleenp@4037: // Copy the the symbol table, and the system dictionary to the shared coleenp@4037: // space in usable form. Copy the hastable coleenp@4037: // buckets first [read-write], then copy the linked lists of entries coleenp@4037: // [read-only]. coleenp@4037: coleenp@4037: SymbolTable::reverse(md_top); coleenp@4037: NOT_PRODUCT(SymbolTable::verify()); coleenp@4037: SymbolTable::copy_buckets(&md_top, md_end); coleenp@4037: coleenp@4037: SystemDictionary::reverse(); coleenp@4037: SystemDictionary::copy_buckets(&md_top, md_end); coleenp@4037: coleenp@4037: ClassLoader::verify(); coleenp@4037: ClassLoader::copy_package_info_buckets(&md_top, md_end); coleenp@4037: ClassLoader::verify(); coleenp@4037: coleenp@4037: SymbolTable::copy_table(&md_top, md_end); coleenp@4037: SystemDictionary::copy_table(&md_top, md_end); coleenp@4037: ClassLoader::verify(); coleenp@4037: ClassLoader::copy_package_info_table(&md_top, md_end); coleenp@4037: ClassLoader::verify(); coleenp@4037: coleenp@4037: // Write the other data to the output array. coleenp@4037: WriteClosure wc(md_top, md_end); coleenp@4037: MetaspaceShared::serialize(&wc); coleenp@4037: md_top = wc.get_top(); coleenp@4037: coleenp@4037: // Print shared spaces all the time coleenp@4434: const char* fmt = "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " PTR_FORMAT; coleenp@4037: Metaspace* ro_space = _loader_data->ro_metaspace(); coleenp@4037: Metaspace* rw_space = _loader_data->rw_metaspace(); coleenp@4434: const size_t BPW = BytesPerWord; coleenp@4434: coleenp@4434: // Allocated size of each space (may not be all occupied) coleenp@4434: const size_t ro_alloced = ro_space->capacity_words(Metaspace::NonClassType) * BPW; coleenp@4434: const size_t rw_alloced = rw_space->capacity_words(Metaspace::NonClassType) * BPW; coleenp@4434: const size_t md_alloced = md_end-md_low; coleenp@4434: const size_t mc_alloced = mc_end-mc_low; coleenp@4434: const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced; coleenp@4434: coleenp@4434: // Occupied size of each space. coleenp@4434: const size_t ro_bytes = ro_space->used_words(Metaspace::NonClassType) * BPW; coleenp@4434: const size_t rw_bytes = rw_space->used_words(Metaspace::NonClassType) * BPW; coleenp@4434: const size_t md_bytes = size_t(md_top - md_low); coleenp@4434: const size_t mc_bytes = size_t(mc_top - mc_low); coleenp@4434: coleenp@4434: // Percent of total size coleenp@4434: const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes; coleenp@4434: const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; coleenp@4434: const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; coleenp@4434: const double md_t_perc = md_bytes / double(total_bytes) * 100.0; coleenp@4434: const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; coleenp@4434: coleenp@4434: // Percent of fullness of each space coleenp@4434: const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; coleenp@4434: const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; coleenp@4434: const double md_u_perc = md_bytes / double(md_alloced) * 100.0; coleenp@4434: const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; coleenp@4434: const double total_u_perc = total_bytes / double(total_alloced) * 100.0; coleenp@4434: coleenp@4434: tty->print_cr(fmt, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom()); coleenp@4434: tty->print_cr(fmt, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom()); coleenp@4434: tty->print_cr(fmt, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low); coleenp@4434: tty->print_cr(fmt, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low); coleenp@4434: tty->print_cr("total : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]", coleenp@4434: total_bytes, total_alloced, total_u_perc); coleenp@4037: coleenp@4037: // Update the vtable pointers in all of the Klass objects in the coleenp@4037: // heap. They should point to newly generated vtable. coleenp@4037: patch_klass_vtables(vtbl_list, vtable); coleenp@4037: coleenp@4037: // dunno what this is for. coleenp@4037: char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass); coleenp@4037: memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); coleenp@4037: memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); coleenp@4037: coleenp@4037: // Create and write the archive file that maps the shared spaces. coleenp@4037: coleenp@4037: FileMapInfo* mapinfo = new FileMapInfo(); coleenp@4037: mapinfo->populate_header(MetaspaceShared::max_alignment()); coleenp@4037: coleenp@4037: // Pass 1 - update file offsets in header. coleenp@4037: mapinfo->write_header(); coleenp@4037: mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); coleenp@4037: mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); coleenp@4037: mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), coleenp@4037: pointer_delta(md_top, _md_vs.low(), sizeof(char)), coleenp@4037: SharedMiscDataSize, coleenp@4037: false, false); coleenp@4037: mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), coleenp@4037: pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), coleenp@4037: SharedMiscCodeSize, coleenp@4037: true, true); coleenp@4037: coleenp@4037: // Pass 2 - write data. coleenp@4037: mapinfo->open_for_write(); coleenp@4037: mapinfo->write_header(); coleenp@4037: mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); coleenp@4037: mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); coleenp@4037: mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), coleenp@4037: pointer_delta(md_top, _md_vs.low(), sizeof(char)), coleenp@4037: SharedMiscDataSize, coleenp@4037: false, false); coleenp@4037: mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), coleenp@4037: pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), coleenp@4037: SharedMiscCodeSize, coleenp@4037: true, true); coleenp@4037: mapinfo->close(); coleenp@4037: coleenp@4037: memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); coleenp@4037: } coleenp@4037: coleenp@4037: static void link_shared_classes(Klass* obj, TRAPS) { hseigel@4278: Klass* k = obj; coleenp@4037: if (k->oop_is_instance()) { coleenp@4037: InstanceKlass* ik = (InstanceKlass*) k; coleenp@4037: // Link the class to cause the bytecodes to be rewritten and the coleenp@4037: // cpcache to be created. coleenp@4037: if (ik->init_state() < InstanceKlass::linked) { coleenp@4037: ik->link_class(THREAD); coleenp@4037: guarantee(!HAS_PENDING_EXCEPTION, "exception in class rewriting"); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Support for a simple checksum of the contents of the class list coleenp@4037: // file to prevent trivial tampering. The algorithm matches that in coleenp@4037: // the MakeClassList program used by the J2SE build process. coleenp@4037: #define JSUM_SEED ((jlong)CONST64(0xcafebabebabecafe)) coleenp@4037: static jlong coleenp@4037: jsum(jlong start, const char *buf, const int len) coleenp@4037: { coleenp@4037: jlong h = start; coleenp@4037: char *p = (char *)buf, *e = p + len; coleenp@4037: while (p < e) { coleenp@4037: char c = *p++; coleenp@4037: if (c <= ' ') { coleenp@4037: /* Skip spaces and control characters */ coleenp@4037: continue; coleenp@4037: } coleenp@4037: h = 31 * h + c; coleenp@4037: } coleenp@4037: return h; coleenp@4037: } coleenp@4037: coleenp@4037: // Preload classes from a list, populate the shared spaces and dump to a coleenp@4037: // file. coleenp@4037: void MetaspaceShared::preload_and_dump(TRAPS) { coleenp@4037: TraceTime timer("Dump Shared Spaces", TraceStartupTime); coleenp@4037: ResourceMark rm; coleenp@4037: coleenp@4037: // Lock out GC - is it necessary? I don't think we care. coleenp@4037: No_GC_Verifier no_gc; coleenp@4037: coleenp@4037: // Preload classes to be shared. coleenp@4037: // Should use some os:: method rather than fopen() here. aB. coleenp@4037: // Construct the path to the class list (in jre/lib) coleenp@4037: // Walk up two directories from the location of the VM and coleenp@4037: // optionally tack on "lib" (depending on platform) coleenp@4037: char class_list_path[JVM_MAXPATHLEN]; coleenp@4037: os::jvm_path(class_list_path, sizeof(class_list_path)); coleenp@4037: for (int i = 0; i < 3; i++) { coleenp@4037: char *end = strrchr(class_list_path, *os::file_separator()); coleenp@4037: if (end != NULL) *end = '\0'; coleenp@4037: } coleenp@4037: int class_list_path_len = (int)strlen(class_list_path); coleenp@4037: if (class_list_path_len >= 3) { coleenp@4037: if (strcmp(class_list_path + class_list_path_len - 3, "lib") != 0) { coleenp@4037: strcat(class_list_path, os::file_separator()); coleenp@4037: strcat(class_list_path, "lib"); coleenp@4037: } coleenp@4037: } coleenp@4037: strcat(class_list_path, os::file_separator()); coleenp@4037: strcat(class_list_path, "classlist"); coleenp@4037: coleenp@4037: FILE* file = fopen(class_list_path, "r"); coleenp@4037: if (file != NULL) { coleenp@4037: jlong computed_jsum = JSUM_SEED; coleenp@4037: jlong file_jsum = 0; coleenp@4037: coleenp@4037: char class_name[256]; coleenp@4037: int class_count = 0; coleenp@4037: GrowableArray* class_promote_order = new GrowableArray(); coleenp@4037: coleenp@4037: // sun.io.Converters coleenp@4037: static const char obj_array_sig[] = "[[Ljava/lang/Object;"; coleenp@4037: SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); coleenp@4037: coleenp@4037: // java.util.HashMap coleenp@4037: static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; coleenp@4037: SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); coleenp@4037: coleenp@4037: tty->print("Loading classes to share ... "); coleenp@4037: while ((fgets(class_name, sizeof class_name, file)) != NULL) { coleenp@4037: if (*class_name == '#') { coleenp@4037: jint fsh, fsl; coleenp@4037: if (sscanf(class_name, "# %8x%8x\n", &fsh, &fsl) == 2) { coleenp@4037: file_jsum = ((jlong)(fsh) << 32) | (fsl & 0xffffffff); coleenp@4037: } coleenp@4037: coleenp@4037: continue; coleenp@4037: } coleenp@4037: // Remove trailing newline coleenp@4037: size_t name_len = strlen(class_name); coleenp@4037: class_name[name_len-1] = '\0'; coleenp@4037: coleenp@4037: computed_jsum = jsum(computed_jsum, class_name, (const int)name_len - 1); coleenp@4037: coleenp@4037: // Got a class name - load it. coleenp@4037: TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD); coleenp@4037: guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol."); coleenp@4037: Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol, coleenp@4037: THREAD); coleenp@4037: guarantee(!HAS_PENDING_EXCEPTION, "Exception resolving a class."); coleenp@4037: if (klass != NULL) { coleenp@4037: if (PrintSharedSpaces && Verbose && WizardMode) { coleenp@4037: tty->print_cr("Shared spaces preloaded: %s", class_name); coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: InstanceKlass* ik = InstanceKlass::cast(klass); coleenp@4037: coleenp@4037: // Should be class load order as per -XX:+TraceClassLoadingPreorder coleenp@4037: class_promote_order->append(ik); coleenp@4037: coleenp@4037: // Link the class to cause the bytecodes to be rewritten and the coleenp@4037: // cpcache to be created. The linking is done as soon as classes coleenp@4037: // are loaded in order that the related data structures (klass and coleenp@4037: // cpCache) are located together. coleenp@4037: coleenp@4037: if (ik->init_state() < InstanceKlass::linked) { coleenp@4037: ik->link_class(THREAD); coleenp@4037: guarantee(!(HAS_PENDING_EXCEPTION), "exception in class rewriting"); coleenp@4037: } coleenp@4037: coleenp@4037: // TODO: Resolve klasses in constant pool coleenp@4037: ik->constants()->resolve_class_constants(THREAD); coleenp@4037: coleenp@4037: class_count++; coleenp@4037: } else { coleenp@4037: if (PrintSharedSpaces && Verbose && WizardMode) { coleenp@4037: tty->cr(); coleenp@4037: tty->print_cr(" Preload failed: %s", class_name); coleenp@4037: } coleenp@4037: } coleenp@4037: file_jsum = 0; // Checksum must be on last line of file coleenp@4037: } coleenp@4037: if (computed_jsum != file_jsum) { coleenp@4037: tty->cr(); coleenp@4037: tty->print_cr("Preload failed: checksum of class list was incorrect."); coleenp@4037: exit(1); coleenp@4037: } coleenp@4037: coleenp@4037: tty->print_cr("done. "); coleenp@4037: coleenp@4037: if (PrintSharedSpaces) { coleenp@4037: tty->print_cr("Shared spaces: preloaded %d classes", class_count); coleenp@4037: } coleenp@4037: coleenp@4037: // Rewrite and unlink classes. coleenp@4037: tty->print("Rewriting and linking classes ... "); coleenp@4037: coleenp@4037: // Link any classes which got missed. (It's not quite clear why coleenp@4037: // they got missed.) This iteration would be unsafe if we weren't coleenp@4037: // single-threaded at this point; however we can't do it on the VM coleenp@4037: // thread because it requires object allocation. coleenp@4037: SystemDictionary::classes_do(link_shared_classes, CATCH); coleenp@4037: tty->print_cr("done. "); coleenp@4037: coleenp@4037: // Create and dump the shared spaces. Everything so far is loaded coleenp@4037: // with the null class loader. coleenp@4037: ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); coleenp@4037: VM_PopulateDumpSharedSpace op(loader_data, class_promote_order); coleenp@4037: VMThread::execute(&op); coleenp@4037: coleenp@4037: } else { coleenp@4037: char errmsg[JVM_MAXPATHLEN]; coleenp@4037: os::lasterror(errmsg, JVM_MAXPATHLEN); coleenp@4037: tty->print_cr("Loading classlist failed: %s", errmsg); coleenp@4037: exit(1); coleenp@4037: } coleenp@4037: coleenp@4037: // Since various initialization steps have been undone by this process, coleenp@4037: // it is not reasonable to continue running a java process. coleenp@4037: exit(0); coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Closure for serializing initialization data in from a data area coleenp@4037: // (ptr_array) read from the shared file. coleenp@4037: coleenp@4037: class ReadClosure : public SerializeClosure { coleenp@4037: private: coleenp@4037: intptr_t** _ptr_array; coleenp@4037: coleenp@4037: inline intptr_t nextPtr() { coleenp@4037: return *(*_ptr_array)++; coleenp@4037: } coleenp@4037: coleenp@4037: public: coleenp@4037: ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; } coleenp@4037: coleenp@4037: void do_ptr(void** p) { coleenp@4037: assert(*p == NULL, "initializing previous initialized pointer."); coleenp@4037: intptr_t obj = nextPtr(); coleenp@4037: assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, coleenp@4037: "hit tag while initializing ptrs."); coleenp@4037: *p = (void*)obj; coleenp@4037: } coleenp@4037: coleenp@4037: void do_tag(int tag) { coleenp@4037: int old_tag; coleenp@4037: old_tag = (int)(intptr_t)nextPtr(); coleenp@4037: // do_int(&old_tag); coleenp@4037: assert(tag == old_tag, "old tag doesn't match"); coleenp@4037: FileMapInfo::assert_mark(tag == old_tag); coleenp@4037: } coleenp@4037: coleenp@4037: void do_region(u_char* start, size_t size) { coleenp@4037: assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); coleenp@4037: assert(size % sizeof(intptr_t) == 0, "bad size"); coleenp@4037: do_tag((int)size); coleenp@4037: while (size > 0) { coleenp@4037: *(intptr_t*)start = nextPtr(); coleenp@4037: start += sizeof(intptr_t); coleenp@4037: size -= sizeof(intptr_t); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: bool reading() const { return true; } coleenp@4037: }; coleenp@4037: coleenp@4037: coleenp@4037: // Save bounds of shared spaces mapped in. coleenp@4037: static char* _ro_base = NULL; coleenp@4037: static char* _rw_base = NULL; coleenp@4037: static char* _md_base = NULL; coleenp@4037: static char* _mc_base = NULL; coleenp@4037: coleenp@4037: // Return true if given address is in the mapped shared space. coleenp@4037: bool MetaspaceShared::is_in_shared_space(const void* p) { coleenp@4037: if (_ro_base == NULL || _rw_base == NULL) { coleenp@4037: return false; coleenp@4037: } else { zgu@4193: return ((p >= _ro_base && p < (_ro_base + SharedReadOnlySize)) || zgu@4193: (p >= _rw_base && p < (_rw_base + SharedReadWriteSize))); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void MetaspaceShared::print_shared_spaces() { coleenp@4037: gclog_or_tty->print_cr("Shared Spaces:"); coleenp@4037: gclog_or_tty->print(" read-only " INTPTR_FORMAT "-" INTPTR_FORMAT, coleenp@4037: _ro_base, _ro_base + SharedReadOnlySize); coleenp@4037: gclog_or_tty->print(" read-write " INTPTR_FORMAT "-" INTPTR_FORMAT, coleenp@4037: _rw_base, _rw_base + SharedReadWriteSize); coleenp@4037: gclog_or_tty->cr(); coleenp@4037: gclog_or_tty->print(" misc-data " INTPTR_FORMAT "-" INTPTR_FORMAT, coleenp@4037: _md_base, _md_base + SharedMiscDataSize); coleenp@4037: gclog_or_tty->print(" misc-code " INTPTR_FORMAT "-" INTPTR_FORMAT, coleenp@4037: _mc_base, _mc_base + SharedMiscCodeSize); coleenp@4037: gclog_or_tty->cr(); coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: // Map shared spaces at requested addresses and return if succeeded. coleenp@4037: // Need to keep the bounds of the ro and rw space for the Metaspace::contains coleenp@4037: // call, or is_in_shared_space. coleenp@4037: bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { coleenp@4037: size_t image_alignment = mapinfo->alignment(); coleenp@4037: hseigel@4396: #ifndef _WINDOWS hseigel@4396: // Map in the shared memory and then map the regions on top of it. hseigel@4396: // On Windows, don't map the memory here because it will cause the hseigel@4396: // mappings of the regions to fail. coleenp@4037: ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); coleenp@4037: if (!shared_rs.is_reserved()) return false; hseigel@4396: #endif hseigel@4396: hseigel@4396: assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); coleenp@4037: coleenp@4037: // Map each shared region coleenp@4037: if ((_ro_base = mapinfo->map_region(ro)) != NULL && coleenp@4037: (_rw_base = mapinfo->map_region(rw)) != NULL && coleenp@4037: (_md_base = mapinfo->map_region(md)) != NULL && coleenp@4037: (_mc_base = mapinfo->map_region(mc)) != NULL && coleenp@4037: (image_alignment == (size_t)max_alignment())) { coleenp@4037: // Success (no need to do anything) coleenp@4037: return true; coleenp@4037: } else { coleenp@4037: // If there was a failure in mapping any of the spaces, unmap the ones coleenp@4037: // that succeeded coleenp@4037: if (_ro_base != NULL) mapinfo->unmap_region(ro); coleenp@4037: if (_rw_base != NULL) mapinfo->unmap_region(rw); coleenp@4037: if (_md_base != NULL) mapinfo->unmap_region(md); coleenp@4037: if (_mc_base != NULL) mapinfo->unmap_region(mc); hseigel@4396: #ifndef _WINDOWS coleenp@4037: // Release the entire mapped region coleenp@4037: shared_rs.release(); hseigel@4396: #endif coleenp@4037: // If -Xshare:on is specified, print out the error message and exit VM, coleenp@4037: // otherwise, set UseSharedSpaces to false and continue. coleenp@4037: if (RequireSharedSpaces) { coleenp@4037: vm_exit_during_initialization("Unable to use shared archive.", NULL); coleenp@4037: } else { coleenp@4037: FLAG_SET_DEFAULT(UseSharedSpaces, false); coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Read the miscellaneous data from the shared file, and coleenp@4037: // serialize it out to its various destinations. coleenp@4037: coleenp@4037: void MetaspaceShared::initialize_shared_spaces() { coleenp@4037: FileMapInfo *mapinfo = FileMapInfo::current_info(); coleenp@4037: coleenp@4037: char* buffer = mapinfo->region_base(md); coleenp@4037: coleenp@4037: // Skip over (reserve space for) a list of addresses of C++ vtables coleenp@4037: // for Klass objects. They get filled in later. coleenp@4037: coleenp@4037: void** vtbl_list = (void**)buffer; coleenp@4037: buffer += MetaspaceShared::vtbl_list_size * sizeof(void*); coleenp@4037: Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); coleenp@4037: coleenp@4037: // Skip over (reserve space for) dummy C++ vtables Klass objects. coleenp@4037: // They are used as is. coleenp@4037: coleenp@4037: intptr_t vtable_size = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: buffer += vtable_size; coleenp@4037: coleenp@4037: // Create the symbol table using the bucket array at this spot in the coleenp@4037: // misc data space. Since the symbol table is often modified, this coleenp@4037: // region (of mapped pages) will be copy-on-write. coleenp@4037: coleenp@4037: int symbolTableLen = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: int number_of_entries = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: SymbolTable::create_table((HashtableBucket*)buffer, symbolTableLen, coleenp@4037: number_of_entries); coleenp@4037: buffer += symbolTableLen; coleenp@4037: coleenp@4037: // Create the shared dictionary using the bucket array at this spot in coleenp@4037: // the misc data space. Since the shared dictionary table is never coleenp@4037: // modified, this region (of mapped pages) will be (effectively, if coleenp@4037: // not explicitly) read-only. coleenp@4037: coleenp@4037: int sharedDictionaryLen = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: number_of_entries = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: SystemDictionary::set_shared_dictionary((HashtableBucket*)buffer, coleenp@4037: sharedDictionaryLen, coleenp@4037: number_of_entries); coleenp@4037: buffer += sharedDictionaryLen; coleenp@4037: coleenp@4037: // Create the package info table using the bucket array at this spot in coleenp@4037: // the misc data space. Since the package info table is never coleenp@4037: // modified, this region (of mapped pages) will be (effectively, if coleenp@4037: // not explicitly) read-only. coleenp@4037: coleenp@4037: int pkgInfoLen = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: number_of_entries = *(intptr_t*)buffer; coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: ClassLoader::create_package_info_table((HashtableBucket*)buffer, pkgInfoLen, coleenp@4037: number_of_entries); coleenp@4037: buffer += pkgInfoLen; coleenp@4037: ClassLoader::verify(); coleenp@4037: coleenp@4037: // The following data in the shared misc data region are the linked coleenp@4037: // list elements (HashtableEntry objects) for the symbol table, string coleenp@4037: // table, and shared dictionary. The heap objects refered to by the coleenp@4037: // symbol table, string table, and shared dictionary are permanent and coleenp@4037: // unmovable. Since new entries added to the string and symbol tables coleenp@4037: // are always added at the beginning of the linked lists, THESE LINKED coleenp@4037: // LIST ELEMENTS ARE READ-ONLY. coleenp@4037: coleenp@4037: int len = *(intptr_t*)buffer; // skip over symbol table entries coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: buffer += len; coleenp@4037: coleenp@4037: len = *(intptr_t*)buffer; // skip over shared dictionary entries coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: buffer += len; coleenp@4037: coleenp@4037: len = *(intptr_t*)buffer; // skip over package info table entries coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: buffer += len; coleenp@4037: coleenp@4037: len = *(intptr_t*)buffer; // skip over package info table char[] arrays. coleenp@4037: buffer += sizeof(intptr_t); coleenp@4037: buffer += len; coleenp@4037: coleenp@4037: intptr_t* array = (intptr_t*)buffer; coleenp@4037: ReadClosure rc(&array); coleenp@4037: serialize(&rc); coleenp@4037: coleenp@4037: // Close the mapinfo file coleenp@4037: mapinfo->close(); coleenp@4037: } coleenp@4037: coleenp@4037: // JVM/TI RedefineClasses() support: coleenp@4037: bool MetaspaceShared::remap_shared_readonly_as_readwrite() { coleenp@4037: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); coleenp@4037: coleenp@4037: if (UseSharedSpaces) { coleenp@4037: // remap the shared readonly space to shared readwrite, private coleenp@4037: FileMapInfo* mapinfo = FileMapInfo::current_info(); coleenp@4037: if (!mapinfo->remap_shared_readonly_as_readwrite()) { coleenp@4037: return false; coleenp@4037: } coleenp@4037: } coleenp@4037: return true; coleenp@4037: }