dump.cpp revision 2265
2265N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 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 * 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 * 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. 1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A// Closure to set up the fingerprint field for all methods. 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// NOTE THAT the algorithm in StringTable::hash_string() MUST MATCH the 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 // 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 // Don't save compiler related special oops (shouldn't be any yet). 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 // Mark the symbol in the shared archive as immortal so it is read only 2062N/A // The symbol has not been relocated yet; copy it to _top address 2062N/A // Make the reference point to the shared copy of the symbol 0N/A// Closure: mark objects closure. 2062N/A// Closure: mark common read-only objects 0N/A // Mark all constMethod objects. 0N/A // Exception tables are needed by ci code during compilation. 0N/A // Mark objects referenced by klass objects which are read-only. 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. 2062N/A// Closure: find symbol references in Java Heap objects 2062N/A // Traverse symbols referenced by method objects. 2062N/A // Traverse symbols referenced by klass objects which are read-only. 2062N/A // Traverse symbols referenced by other constantpool entries. 0N/A// Closure: mark char arrays used by strings 0N/A // Character arrays referenced by String objects are read-only. 0N/A// Closure: Check for objects left in the heap which have not been moved. 0N/A// Closure: Mark remaining objects read-write, except Strings. 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 // Mark constantPool tags and the constantPoolCache. 0N/A // Mark all method objects. 0N/A// Closure: Mark String objects read-write. 0N/A // Mark String objects referenced by constant pool entries. 0N/A// Move objects matching specified type (ie. lock_bits) to the specified 0N/A // Readonly objects: set hash value to self pointer and make gc_marked. 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 // mark_and_move_for_policy(OP_favor_runtime, ik->java_mirror(), _move_rw); 0N/A// Adjust references in oops to refer to shared spaces. 2062N/A// The methods array must be reordered by Symbol* address. 2062N/A// sorted). The addresses of symbols have been changed as a result 2062N/A// of moving to the shared space. 2062N/A true /* idempotent, slow */);
2062N/A// Itable indices are calculated based on methods array order 2062N/A// (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. 0N/A// Adjust references in oops to refer to shared spaces. 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 // immutable objects. 0N/A // someone added an object we hadn't accounted for. 0N/A// Empty the young and old generations. 0N/A// Closure for serializing initialization data out to a data area to be 0N/A// written to the shared file. 0N/A "Oop in shared space not pointing into shared space.");
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 // High level summary of the read-only space: 0N/A // High level summary of the read-write space: 0N/A // Lower level summary of the read-only space: 0N/A // Lower level summary of the read-write space: 0N/A// Patch C++ vtable pointer in klass oops. 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// Our constructed vtables: 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// 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// 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// mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) 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// 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// 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 2062N/A for (
int i = 0; i < n; i++) {
2062N/A// Walk through all symbols and patch their vtable pointers. 2062N/A// Note that symbols have vtable pointers only in non-product builds 0N/A// Populate the shared space. 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 // shared classes at runtime, where constraints were previously created. 0N/A "loader constraints are not saved");
710N/A // Revisit and implement this if we prelink method handle call sites: 710N/A "invoke method table is not saved");
0N/A // At this point, many classes have been loaded. 0N/A // Update all the fingerprints in the shared methods. 0N/A // Remove all references outside the heap. 0N/A // Move the objects in three passes. 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 // At the class level of granularity, the promotion order is dictated 0N/A // by the classlist file whose generation is discussed elsewhere. 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 // 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 // (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 // Set up the share data and shared code segments. 0N/A // Reserve space for the list of klassOops whose vtables are used 0N/A // for patching others as needed. 0N/A // Reserve space for a new dummy vtable for klass objects in the 0N/A // heap. Generate self-patching vtable entries. 2062N/A // Reserve space for the total size and the number of stored symbols. 2062N/A // Phase 1a: remove symbols with _refcount == 0 2062N/A // Phase 1b: move commonly used symbols referenced by oop fields. 2062N/A // Phase 1c: move known names and signatures. 2062N/A // Phase 1d: move the remaining symbols by scanning the whole SymbolTable. 2062N/A // Record the total length of all symbols at the beginning of the block. 2062N/A // Advance the pointer to the end of symbol store. 2062N/A // Phase 2: move commonly used read-only objects to the read-only space. 2062N/A // Phase 3: move String character arrays to the read-only space. 2062N/A // Phase 4: move read-write objects to the read-write space, except 2062N/A // Phase 5: move String objects to the read-write space. 2062N/A // Check: scan for objects which were not moved. 2062N/A // Resolve forwarding in objects and saved C++ structures 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 // Previously method sorting was done concurrently with forwarding 0N/A // pointer resolution in the shared spaces. This imposed an ordering 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 // 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 // Reorder the system dictionary. (Moving the symbols opps affects 0N/A // how the hash table indices are calculated.) 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 // 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 // Print debug data. 0N/A // Write the oop data to the output array. 0N/A // Update the vtable pointers in all of the Klass objects in the 0N/A // heap. They should point to newly generated vtable. 2062N/A // Update the vtable pointers in all symbols, 2062N/A // but only in non-product builds where symbols DO have virtual methods. 0N/A // Create and write the archive file that maps the shared spaces. 0N/A // Pass 1 - update file offsets in header. 0N/A // Pass 2 - write data. 0N/A};
// class VM_PopulateDumpSharedSpace 0N/A// Populate the shared spaces and dump to a file. 0N/A // Calculate hash values for all of the (interned) strings to avoid 0N/A // writes to shared pages in the future. 0N/A tty->
print(
"Calculating hash values for String objects .. ");
0N/A // Link the class to cause the bytecodes to be rewritten and the 0N/A // cpcache to be created. 0N/A // Create String objects from string initializer symbols. 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 /* Skip spaces and control characters */ 0N/A// Preload classes from a list, populate the shared spaces and dump to 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 for (
int i = 0; i <
3; i++) {
0N/A // Preload (and intern) strings which will be used later. 0N/A // sun.io.Converters 0N/A // java.util.HashMap 0N/A // Remove trailing newline 0N/A // Got a class name - load it. 0N/A // Should be class load order as per -XX:+TraceClassLoadingPreorder 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 // Create String objects from string initializer symbols. 0N/A tty->
print_cr(
"Preload failed: checksum of class list was incorrect.");
0N/A // Rewrite and unlink classes. 0N/A // Make heap parsable 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 // Create and dump the shared spaces. 0N/A // Since various initialization steps have been undone by this process, 0N/A // it is not reasonable to continue running a java process.