src/share/vm/memory/metaspaceShared.cpp

Print this page




  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     default: break;
 119     }
 120   }
 121 }
 122 
 123 // Walk all methods in the class list to ensure that they won't be modified at
 124 // run time. This includes:
 125 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
 126 //     at run time by RewriteBytecodes/RewriteFrequentPairs
 127 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
 128 static void rewrite_nofast_bytecodes_and_calculate_fingerprints() {
 129   for (int i = 0; i < _global_klass_objects->length(); i++) {
 130     Klass* k = _global_klass_objects->at(i);
 131     if (k->oop_is_instance()) {
 132       InstanceKlass* ik = InstanceKlass::cast(k);
 133       for (int i = 0; i < ik->methods()->length(); i++) {
 134         Method* m = ik->methods()->at(i);
 135         rewrite_nofast_bytecode(m);
 136         Fingerprinter fp(m);
 137         // The side effect of this call sets method's fingerprint field.
 138         fp.fingerprint();
 139       }
 140     }
 141   }
 142 }
 143 
 144 // Patch C++ vtable pointer in metadata.
 145 
 146 // Klass and other metadata objects contain references to c++ vtables in the
 147 // JVM library.
 148 // Fix them to point to our constructed vtables.  However, don't iterate
 149 // across the space while doing this, as that causes the vtables to be
 150 // patched, undoing our useful work.  Instead, iterate to make a list,
 151 // then use the list to do the fixing.
 152 //
 153 // Our constructed vtables:
 154 // Dump time:
 155 //  1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs


 479 
 480   tty->print_cr("Number of classes %d", _global_klass_objects->length());
 481   {
 482     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
 483     for (int i = 0; i < _global_klass_objects->length(); i++) {
 484       Klass* k = _global_klass_objects->at(i);
 485       if (k->oop_is_instance()) {
 486         num_inst ++;
 487       } else if (k->oop_is_objArray()) {
 488         num_obj_array ++;
 489       } else {
 490         assert(k->oop_is_typeArray(), "sanity");
 491         num_type_array ++;
 492       }
 493     }
 494     tty->print_cr("    instance classes   = %5d", num_inst);
 495     tty->print_cr("    obj array classes  = %5d", num_obj_array);
 496     tty->print_cr("    type array classes = %5d", num_type_array);
 497   }
 498 
 499 
 500   // Ensure the ConstMethods won't be modified at run-time
 501   tty->print("Updating ConstMethods ... ");
 502   rewrite_nofast_bytecodes_and_calculate_fingerprints();
 503   tty->print_cr("done. ");
 504 
 505   // Remove all references outside the metadata
 506   tty->print("Removing unshareable information ... ");
 507   remove_unshareable_in_classes();
 508   tty->print_cr("done. ");
 509 
 510   // Set up the share data and shared code segments.
 511   char* md_low = _md_vs.low();
 512   char* md_top = md_low;
 513   char* md_end = _md_vs.high();
 514   char* mc_low = _mc_vs.low();
 515   char* mc_top = mc_low;
 516   char* mc_end = _mc_vs.high();
 517 
 518   // Reserve space for the list of Klass*s whose vtables are used
 519   // for patching others as needed.
 520 
 521   void** vtbl_list = (void**)md_top;
 522   int vtbl_list_size = MetaspaceShared::vtbl_list_size;