13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/dictionary.hpp" 27 #include "classfile/loaderConstraints.hpp" 28 #include "classfile/placeholders.hpp" 29 #include "classfile/sharedClassUtil.hpp" 30 #include "classfile/symbolTable.hpp" 31 #include "classfile/systemDictionary.hpp" 32 #include "code/codeCache.hpp" 33 #include "memory/filemap.hpp" 34 #include "memory/gcLocker.hpp" 35 #include "memory/metaspace.hpp" 36 #include "memory/metaspaceShared.hpp" 37 #include "oops/objArrayOop.hpp" 38 #include "oops/oop.inline.hpp" 39 #include "runtime/os.hpp" 40 #include "runtime/signature.hpp" 41 #include "runtime/vm_operations.hpp" 42 #include "runtime/vmThread.hpp" 43 #include "utilities/hashtable.inline.hpp" 44 45 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 46 47 int MetaspaceShared::_max_alignment = 0; 48 49 ReservedSpace* MetaspaceShared::_shared_rs = NULL; 50 51 MetaspaceSharedStats MetaspaceShared::_stats; 52 87 88 // Global object for holding classes that have been loaded. Since this 89 // is run at a safepoint just before exit, this is the entire set of classes. 90 static GrowableArray<Klass*>* _global_klass_objects; 91 static void collect_classes(Klass* k) { 92 _global_klass_objects->append_if_missing(k); 93 if (k->oop_is_instance()) { 94 // Add in the array classes too 95 InstanceKlass* ik = InstanceKlass::cast(k); 96 ik->array_klasses_do(collect_classes); 97 } 98 } 99 100 static void remove_unshareable_in_classes() { 101 for (int i = 0; i < _global_klass_objects->length(); i++) { 102 Klass* k = _global_klass_objects->at(i); 103 k->remove_unshareable_info(); 104 } 105 } 106 107 // Walk all methods in the class list and assign a fingerprint. 108 // so that this part of the ConstMethod* is read only. 109 static void calculate_fingerprints() { 110 for (int i = 0; i < _global_klass_objects->length(); i++) { 111 Klass* k = _global_klass_objects->at(i); 112 if (k->oop_is_instance()) { 113 InstanceKlass* ik = InstanceKlass::cast(k); 114 for (int i = 0; i < ik->methods()->length(); i++) { 115 Method* m = ik->methods()->at(i); 116 Fingerprinter fp(m); 117 // The side effect of this call sets method's fingerprint field. 118 fp.fingerprint(); 119 } 120 } 121 } 122 } 123 124 // Patch C++ vtable pointer in metadata. 125 126 // Klass and other metadata objects contain references to c++ vtables in the 127 // JVM library. 128 // Fix them to point to our constructed vtables. However, don't iterate 129 // across the space while doing this, as that causes the vtables to be 130 // patched, undoing our useful work. Instead, iterate to make a list, 131 // then use the list to do the fixing. 132 // 133 // Our constructed vtables: 134 // Dump time: 135 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs 459 460 tty->print_cr("Number of classes %d", _global_klass_objects->length()); 461 { 462 int num_type_array = 0, num_obj_array = 0, num_inst = 0; 463 for (int i = 0; i < _global_klass_objects->length(); i++) { 464 Klass* k = _global_klass_objects->at(i); 465 if (k->oop_is_instance()) { 466 num_inst ++; 467 } else if (k->oop_is_objArray()) { 468 num_obj_array ++; 469 } else { 470 assert(k->oop_is_typeArray(), "sanity"); 471 num_type_array ++; 472 } 473 } 474 tty->print_cr(" instance classes = %5d", num_inst); 475 tty->print_cr(" obj array classes = %5d", num_obj_array); 476 tty->print_cr(" type array classes = %5d", num_type_array); 477 } 478 479 // Update all the fingerprints in the shared methods. 480 tty->print("Calculating fingerprints ... "); 481 calculate_fingerprints(); 482 tty->print_cr("done. "); 483 484 // Remove all references outside the metadata 485 tty->print("Removing unshareable information ... "); 486 remove_unshareable_in_classes(); 487 tty->print_cr("done. "); 488 489 // Set up the share data and shared code segments. 490 char* md_low = _md_vs.low(); 491 char* md_top = md_low; 492 char* md_end = _md_vs.high(); 493 char* mc_low = _mc_vs.low(); 494 char* mc_top = mc_low; 495 char* mc_end = _mc_vs.high(); 496 497 // Reserve space for the list of Klass*s whose vtables are used 498 // for patching others as needed. 499 500 void** vtbl_list = (void**)md_top; 501 int vtbl_list_size = MetaspaceShared::vtbl_list_size; | 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/dictionary.hpp" 27 #include "classfile/loaderConstraints.hpp" 28 #include "classfile/placeholders.hpp" 29 #include "classfile/sharedClassUtil.hpp" 30 #include "classfile/symbolTable.hpp" 31 #include "classfile/systemDictionary.hpp" 32 #include "code/codeCache.hpp" 33 #include "interpreter/bytecodes.hpp" 34 #include "interpreter/bytecodeStream.hpp" 35 #include "memory/filemap.hpp" 36 #include "memory/gcLocker.hpp" 37 #include "memory/metaspace.hpp" 38 #include "memory/metaspaceShared.hpp" 39 #include "oops/objArrayOop.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "runtime/os.hpp" 42 #include "runtime/signature.hpp" 43 #include "runtime/vm_operations.hpp" 44 #include "runtime/vmThread.hpp" 45 #include "utilities/hashtable.inline.hpp" 46 47 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 48 49 int MetaspaceShared::_max_alignment = 0; 50 51 ReservedSpace* MetaspaceShared::_shared_rs = NULL; 52 53 MetaspaceSharedStats MetaspaceShared::_stats; 54 89 90 // Global object for holding classes that have been loaded. Since this 91 // is run at a safepoint just before exit, this is the entire set of classes. 92 static GrowableArray<Klass*>* _global_klass_objects; 93 static void collect_classes(Klass* k) { 94 _global_klass_objects->append_if_missing(k); 95 if (k->oop_is_instance()) { 96 // Add in the array classes too 97 InstanceKlass* ik = InstanceKlass::cast(k); 98 ik->array_klasses_do(collect_classes); 99 } 100 } 101 102 static void remove_unshareable_in_classes() { 103 for (int i = 0; i < _global_klass_objects->length(); i++) { 104 Klass* k = _global_klass_objects->at(i); 105 k->remove_unshareable_info(); 106 } 107 } 108 109 static void rewrite_nofast_bytecode(Method* method) { 110 RawBytecodeStream bcs(method); 111 while (!bcs.is_last_bytecode()) { 112 Bytecodes::Code opcode = bcs.raw_next(); 113 switch (opcode) { 114 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; 115 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; 116 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; 117 case Bytecodes::_iload: *bcs.bcp() = Bytecodes::_nofast_iload; break; 118 case Bytecodes::_invokevirtual: *bcs.bcp() = Bytecodes::_nofast_invokevirtual; break; 119 default: break; 120 } 121 } 122 } 123 124 // Walk all methods in the class list to ensure that they won't be modified at 125 // run time. This includes: 126 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified 127 // at run time by RewriteBytecodes/RewriteFrequentPairs 128 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time. 129 static void rewrite_nofast_bytecodes_and_calculate_fingerprints() { 130 for (int i = 0; i < _global_klass_objects->length(); i++) { 131 Klass* k = _global_klass_objects->at(i); 132 if (k->oop_is_instance()) { 133 InstanceKlass* ik = InstanceKlass::cast(k); 134 for (int i = 0; i < ik->methods()->length(); i++) { 135 Method* m = ik->methods()->at(i); 136 rewrite_nofast_bytecode(m); 137 Fingerprinter fp(m); 138 // The side effect of this call sets method's fingerprint field. 139 fp.fingerprint(); 140 } 141 } 142 } 143 } 144 145 // Patch C++ vtable pointer in metadata. 146 147 // Klass and other metadata objects contain references to c++ vtables in the 148 // JVM library. 149 // Fix them to point to our constructed vtables. However, don't iterate 150 // across the space while doing this, as that causes the vtables to be 151 // patched, undoing our useful work. Instead, iterate to make a list, 152 // then use the list to do the fixing. 153 // 154 // Our constructed vtables: 155 // Dump time: 156 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs 480 481 tty->print_cr("Number of classes %d", _global_klass_objects->length()); 482 { 483 int num_type_array = 0, num_obj_array = 0, num_inst = 0; 484 for (int i = 0; i < _global_klass_objects->length(); i++) { 485 Klass* k = _global_klass_objects->at(i); 486 if (k->oop_is_instance()) { 487 num_inst ++; 488 } else if (k->oop_is_objArray()) { 489 num_obj_array ++; 490 } else { 491 assert(k->oop_is_typeArray(), "sanity"); 492 num_type_array ++; 493 } 494 } 495 tty->print_cr(" instance classes = %5d", num_inst); 496 tty->print_cr(" obj array classes = %5d", num_obj_array); 497 tty->print_cr(" type array classes = %5d", num_type_array); 498 } 499 500 501 // Ensure the ConstMethods won't be modified at run-time 502 tty->print("Updating ConstMethods ... "); 503 rewrite_nofast_bytecodes_and_calculate_fingerprints(); 504 tty->print_cr("done. "); 505 506 // Remove all references outside the metadata 507 tty->print("Removing unshareable information ... "); 508 remove_unshareable_in_classes(); 509 tty->print_cr("done. "); 510 511 // Set up the share data and shared code segments. 512 char* md_low = _md_vs.low(); 513 char* md_top = md_low; 514 char* md_end = _md_vs.high(); 515 char* mc_low = _mc_vs.low(); 516 char* mc_top = mc_low; 517 char* mc_end = _mc_vs.high(); 518 519 // Reserve space for the list of Klass*s whose vtables are used 520 // for patching others as needed. 521 522 void** vtbl_list = (void**)md_top; 523 int vtbl_list_size = MetaspaceShared::vtbl_list_size; |