hotspot/src/share/vm/memory/metaspaceShared.cpp
changeset 34257 4be3504cc03b
parent 33638 ef49ed90010b
child 35231 e89989198037
equal deleted inserted replaced
34132:aa73af640c61 34257:4be3504cc03b
    21  * questions.
    21  * questions.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
       
    26 #include "classfile/classListParser.hpp"
       
    27 #include "classfile/classLoaderExt.hpp"
    26 #include "classfile/dictionary.hpp"
    28 #include "classfile/dictionary.hpp"
    27 #include "classfile/loaderConstraints.hpp"
    29 #include "classfile/loaderConstraints.hpp"
    28 #include "classfile/placeholders.hpp"
    30 #include "classfile/placeholders.hpp"
    29 #include "classfile/sharedClassUtil.hpp"
    31 #include "classfile/sharedClassUtil.hpp"
    30 #include "classfile/symbolTable.hpp"
    32 #include "classfile/symbolTable.hpp"
    40 #include "oops/oop.inline.hpp"
    42 #include "oops/oop.inline.hpp"
    41 #include "runtime/os.hpp"
    43 #include "runtime/os.hpp"
    42 #include "runtime/signature.hpp"
    44 #include "runtime/signature.hpp"
    43 #include "runtime/vmThread.hpp"
    45 #include "runtime/vmThread.hpp"
    44 #include "runtime/vm_operations.hpp"
    46 #include "runtime/vm_operations.hpp"
       
    47 #include "utilities/defaultStream.hpp"
    45 #include "utilities/hashtable.inline.hpp"
    48 #include "utilities/hashtable.inline.hpp"
    46 
    49 
    47 int MetaspaceShared::_max_alignment = 0;
    50 int MetaspaceShared::_max_alignment = 0;
    48 
    51 
    49 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
    52 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
    93   if (k->is_instance_klass()) {
    96   if (k->is_instance_klass()) {
    94     // Add in the array classes too
    97     // Add in the array classes too
    95     InstanceKlass* ik = InstanceKlass::cast(k);
    98     InstanceKlass* ik = InstanceKlass::cast(k);
    96     ik->array_klasses_do(collect_classes);
    99     ik->array_klasses_do(collect_classes);
    97   }
   100   }
       
   101 }
       
   102 
       
   103 static void collect_classes2(Klass* k, ClassLoaderData* class_data) {
       
   104   collect_classes(k);
    98 }
   105 }
    99 
   106 
   100 static void remove_unshareable_in_classes() {
   107 static void remove_unshareable_in_classes() {
   101   for (int i = 0; i < _global_klass_objects->length(); i++) {
   108   for (int i = 0; i < _global_klass_objects->length(); i++) {
   102     Klass* k = _global_klass_objects->at(i);
   109     Klass* k = _global_klass_objects->at(i);
   420   GrowableArray<Klass*> *_class_promote_order;
   427   GrowableArray<Klass*> *_class_promote_order;
   421   VirtualSpace _md_vs;
   428   VirtualSpace _md_vs;
   422   VirtualSpace _mc_vs;
   429   VirtualSpace _mc_vs;
   423   CompactHashtableWriter* _string_cht;
   430   CompactHashtableWriter* _string_cht;
   424   GrowableArray<MemRegion> *_string_regions;
   431   GrowableArray<MemRegion> *_string_regions;
       
   432   char* _md_alloc_low;
       
   433   char* _md_alloc_top;
       
   434   char* _md_alloc_max;
       
   435   static VM_PopulateDumpSharedSpace* _instance;
   425 
   436 
   426 public:
   437 public:
   427   VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data,
   438   VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data,
   428                              GrowableArray<Klass*> *class_promote_order) :
   439                              GrowableArray<Klass*> *class_promote_order) :
   429     _loader_data(loader_data) {
   440     _loader_data(loader_data) {
   430 
       
   431     // Split up and initialize the misc code and data spaces
   441     // Split up and initialize the misc code and data spaces
   432     ReservedSpace* shared_rs = MetaspaceShared::shared_rs();
   442     ReservedSpace* shared_rs = MetaspaceShared::shared_rs();
   433     size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize;
   443     size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize;
   434     ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size);
   444     ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size);
   435     ReservedSpace misc_section = shared_rs->last_part(metadata_size);
   445     ReservedSpace misc_section = shared_rs->last_part(metadata_size);
   438     ReservedSpace md_rs   = misc_section.first_part(SharedMiscDataSize);
   448     ReservedSpace md_rs   = misc_section.first_part(SharedMiscDataSize);
   439     ReservedSpace mc_rs   = misc_section.last_part(SharedMiscDataSize);
   449     ReservedSpace mc_rs   = misc_section.last_part(SharedMiscDataSize);
   440     _md_vs.initialize(md_rs, SharedMiscDataSize);
   450     _md_vs.initialize(md_rs, SharedMiscDataSize);
   441     _mc_vs.initialize(mc_rs, SharedMiscCodeSize);
   451     _mc_vs.initialize(mc_rs, SharedMiscCodeSize);
   442     _class_promote_order = class_promote_order;
   452     _class_promote_order = class_promote_order;
       
   453 
       
   454     _md_alloc_low = _md_vs.low();
       
   455     _md_alloc_top = _md_alloc_low + sizeof(char*);
       
   456     _md_alloc_max = _md_vs.low() + SharedMiscDataSize;
       
   457 
       
   458     assert(_instance == NULL, "must be singleton");
       
   459     _instance = this;
       
   460   }
       
   461 
       
   462   ~VM_PopulateDumpSharedSpace() {
       
   463     assert(_instance == this, "must be singleton");
       
   464     _instance = NULL;
       
   465   }
       
   466 
       
   467   static VM_PopulateDumpSharedSpace* instance() {
       
   468     assert(_instance != NULL, "sanity");
       
   469     return _instance;
   443   }
   470   }
   444 
   471 
   445   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
   472   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
   446   void doit();   // outline because gdb sucks
   473   void doit();   // outline because gdb sucks
       
   474 
       
   475   char* misc_data_space_alloc(size_t num_bytes) {
       
   476     size_t alignment = sizeof(char*);
       
   477     num_bytes = align_size_up(num_bytes, alignment);
       
   478     _md_alloc_top = (char*)align_ptr_up(_md_alloc_top, alignment);
       
   479     if (_md_alloc_top + num_bytes > _md_alloc_max) {
       
   480       report_out_of_shared_space(SharedMiscData);
       
   481     }
       
   482 
       
   483     char* p = _md_alloc_top;
       
   484     _md_alloc_top += num_bytes;
       
   485 
       
   486     memset(p, 0, num_bytes);
       
   487     return p;
       
   488   }
   447 
   489 
   448 private:
   490 private:
   449   void handle_misc_data_space_failure(bool success) {
   491   void handle_misc_data_space_failure(bool success) {
   450     if (!success) {
   492     if (!success) {
   451       report_out_of_shared_space(SharedMiscData);
   493       report_out_of_shared_space(SharedMiscData);
   452     }
   494     }
   453   }
   495   }
   454 }; // class VM_PopulateDumpSharedSpace
   496 }; // class VM_PopulateDumpSharedSpace
   455 
   497 
       
   498 VM_PopulateDumpSharedSpace* VM_PopulateDumpSharedSpace::_instance;
   456 
   499 
   457 void VM_PopulateDumpSharedSpace::doit() {
   500 void VM_PopulateDumpSharedSpace::doit() {
   458   Thread* THREAD = VMThread::vm_thread();
   501   Thread* THREAD = VMThread::vm_thread();
   459   NOT_PRODUCT(SystemDictionary::verify();)
   502   NOT_PRODUCT(SystemDictionary::verify();)
   460   // The following guarantee is meant to ensure that no loader constraints
   503   // The following guarantee is meant to ensure that no loader constraints
   473   // At this point, many classes have been loaded.
   516   // At this point, many classes have been loaded.
   474   // Gather systemDictionary classes in a global array and do everything to
   517   // Gather systemDictionary classes in a global array and do everything to
   475   // that so we don't have to walk the SystemDictionary again.
   518   // that so we don't have to walk the SystemDictionary again.
   476   _global_klass_objects = new GrowableArray<Klass*>(1000);
   519   _global_klass_objects = new GrowableArray<Klass*>(1000);
   477   Universe::basic_type_classes_do(collect_classes);
   520   Universe::basic_type_classes_do(collect_classes);
   478   SystemDictionary::classes_do(collect_classes);
   521 
       
   522   // Need to call SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*))
       
   523   // as we may have some classes with NULL ClassLoaderData* in the dictionary. Other
       
   524   // variants of SystemDictionary::classes_do will skip those classes.
       
   525   SystemDictionary::classes_do(collect_classes2);
   479 
   526 
   480   tty->print_cr("Number of classes %d", _global_klass_objects->length());
   527   tty->print_cr("Number of classes %d", _global_klass_objects->length());
   481   {
   528   {
   482     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
   529     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
   483     for (int i = 0; i < _global_klass_objects->length(); i++) {
   530     for (int i = 0; i < _global_klass_objects->length(); i++) {
   512   char* md_top = md_low;
   559   char* md_top = md_low;
   513   char* md_end = _md_vs.high();
   560   char* md_end = _md_vs.high();
   514   char* mc_low = _mc_vs.low();
   561   char* mc_low = _mc_vs.low();
   515   char* mc_top = mc_low;
   562   char* mc_top = mc_low;
   516   char* mc_end = _mc_vs.high();
   563   char* mc_end = _mc_vs.high();
       
   564 
       
   565   assert(_md_alloc_top != NULL, "sanity");
       
   566   *(char**)_md_alloc_low = _md_alloc_top;
       
   567   md_top = _md_alloc_top;
   517 
   568 
   518   // Reserve space for the list of Klass*s whose vtables are used
   569   // Reserve space for the list of Klass*s whose vtables are used
   519   // for patching others as needed.
   570   // for patching others as needed.
   520 
   571 
   521   void** vtbl_list = (void**)md_top;
   572   void** vtbl_list = (void**)md_top;
   733 // Preload classes from a list, populate the shared spaces and dump to a
   784 // Preload classes from a list, populate the shared spaces and dump to a
   734 // file.
   785 // file.
   735 void MetaspaceShared::preload_and_dump(TRAPS) {
   786 void MetaspaceShared::preload_and_dump(TRAPS) {
   736   TraceTime timer("Dump Shared Spaces", TraceStartupTime);
   787   TraceTime timer("Dump Shared Spaces", TraceStartupTime);
   737   ResourceMark rm;
   788   ResourceMark rm;
       
   789   char class_list_path_str[JVM_MAXPATHLEN];
   738 
   790 
   739   tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
   791   tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
   740                 MetaspaceShared::shared_rs()->size(),
   792                 MetaspaceShared::shared_rs()->size(),
   741                 p2i(MetaspaceShared::shared_rs()->base()));
   793                 p2i(MetaspaceShared::shared_rs()->base()));
   742 
   794 
   745   const char* class_list_path;
   797   const char* class_list_path;
   746   if (SharedClassListFile == NULL) {
   798   if (SharedClassListFile == NULL) {
   747     // Construct the path to the class list (in jre/lib)
   799     // Construct the path to the class list (in jre/lib)
   748     // Walk up two directories from the location of the VM and
   800     // Walk up two directories from the location of the VM and
   749     // optionally tack on "lib" (depending on platform)
   801     // optionally tack on "lib" (depending on platform)
   750     char class_list_path_str[JVM_MAXPATHLEN];
       
   751     os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
   802     os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
   752     for (int i = 0; i < 3; i++) {
   803     for (int i = 0; i < 3; i++) {
   753       char *end = strrchr(class_list_path_str, *os::file_separator());
   804       char *end = strrchr(class_list_path_str, *os::file_separator());
   754       if (end != NULL) *end = '\0';
   805       if (end != NULL) *end = '\0';
   755     }
   806     }
   783 
   834 
   784   // java.util.HashMap
   835   // java.util.HashMap
   785   static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
   836   static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
   786   SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
   837   SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
   787 
   838 
       
   839   // Need to allocate the op here:
       
   840   // op.misc_data_space_alloc() will be called during preload_and_dump().
       
   841   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
       
   842   VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
       
   843 
   788   tty->print_cr("Loading classes to share ...");
   844   tty->print_cr("Loading classes to share ...");
   789   _has_error_classes = false;
   845   _has_error_classes = false;
   790   class_count += preload_and_dump(class_list_path, class_promote_order,
   846   class_count += preload_and_dump(class_list_path, class_promote_order,
   791                                   THREAD);
   847                                   THREAD);
   792   if (ExtraSharedClassListFile) {
   848   if (ExtraSharedClassListFile) {
   807   // fails verification, all other interfaces that were not specified in the classlist but
   863   // fails verification, all other interfaces that were not specified in the classlist but
   808   // are implemented by K are not verified.
   864   // are implemented by K are not verified.
   809   link_and_cleanup_shared_classes(CATCH);
   865   link_and_cleanup_shared_classes(CATCH);
   810   tty->print_cr("Rewriting and linking classes: done");
   866   tty->print_cr("Rewriting and linking classes: done");
   811 
   867 
   812   // Create and dump the shared spaces.   Everything so far is loaded
       
   813   // with the null class loader.
       
   814   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
       
   815   VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
       
   816   VMThread::execute(&op);
   868   VMThread::execute(&op);
   817 
       
   818   // Since various initialization steps have been undone by this process,
   869   // Since various initialization steps have been undone by this process,
   819   // it is not reasonable to continue running a java process.
   870   // it is not reasonable to continue running a java process.
   820   exit(0);
   871   exit(0);
   821 }
   872 }
   822 
   873 
   823 int MetaspaceShared::preload_and_dump(const char * class_list_path,
   874 
       
   875 int MetaspaceShared::preload_and_dump(const char* class_list_path,
   824                                       GrowableArray<Klass*>* class_promote_order,
   876                                       GrowableArray<Klass*>* class_promote_order,
   825                                       TRAPS) {
   877                                       TRAPS) {
   826   FILE* file = fopen(class_list_path, "r");
   878   ClassListParser parser(class_list_path);
   827   char class_name[256];
       
   828   int class_count = 0;
   879   int class_count = 0;
   829 
   880 
   830   if (file != NULL) {
   881     while (parser.parse_one_line()) {
   831     while ((fgets(class_name, sizeof class_name, file)) != NULL) {
   882       Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD);
   832       if (*class_name == '#') { // comment
   883 
   833         continue;
       
   834       }
       
   835       // Remove trailing newline
       
   836       size_t name_len = strlen(class_name);
       
   837       if (class_name[name_len-1] == '\n') {
       
   838         class_name[name_len-1] = '\0';
       
   839       }
       
   840 
       
   841       // Got a class name - load it.
       
   842       TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD);
       
   843       guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
       
   844       Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol,
       
   845                                                          THREAD);
       
   846       CLEAR_PENDING_EXCEPTION;
   884       CLEAR_PENDING_EXCEPTION;
   847       if (klass != NULL) {
   885       if (klass != NULL) {
   848         if (PrintSharedSpaces && Verbose && WizardMode) {
   886         if (PrintSharedSpaces && Verbose && WizardMode) {
   849           tty->print_cr("Shared spaces preloaded: %s", class_name);
   887           ResourceMark rm;
       
   888           tty->print_cr("Shared spaces preloaded: %s", klass->external_name());
   850         }
   889         }
   851 
   890 
   852         InstanceKlass* ik = InstanceKlass::cast(klass);
   891         InstanceKlass* ik = InstanceKlass::cast(klass);
   853 
   892 
   854         // Should be class load order as per -XX:+TraceClassLoadingPreorder
   893         // Should be class load order as per -XX:+TraceClassLoadingPreorder
   860         // cpCache) are located together.
   899         // cpCache) are located together.
   861         try_link_class(ik, THREAD);
   900         try_link_class(ik, THREAD);
   862         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
   901         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
   863 
   902 
   864         class_count++;
   903         class_count++;
   865       } else {
       
   866         //tty->print_cr("Preload failed: %s", class_name);
       
   867       }
   904       }
   868     }
   905     }
   869     fclose(file);
       
   870   } else {
       
   871     char errmsg[JVM_MAXPATHLEN];
       
   872     os::lasterror(errmsg, JVM_MAXPATHLEN);
       
   873     tty->print_cr("Loading classlist failed: %s", errmsg);
       
   874     exit(1);
       
   875   }
       
   876 
   906 
   877   return class_count;
   907   return class_count;
   878 }
   908 }
   879 
   909 
   880 // Returns true if the class's status has changed
   910 // Returns true if the class's status has changed
   904     BytecodeVerificationLocal = saved;
   934     BytecodeVerificationLocal = saved;
   905     return true;
   935     return true;
   906   } else {
   936   } else {
   907     return false;
   937     return false;
   908   }
   938   }
       
   939 }
       
   940 
       
   941 // Allocate misc data blocks during dumping.
       
   942 char* MetaspaceShared::misc_data_space_alloc(size_t num_bytes) {
       
   943   return VM_PopulateDumpSharedSpace::instance()->misc_data_space_alloc(num_bytes);
   909 }
   944 }
   910 
   945 
   911 // Closure for serializing initialization data in from a data area
   946 // Closure for serializing initialization data in from a data area
   912 // (ptr_array) read from the shared file.
   947 // (ptr_array) read from the shared file.
   913 
   948 
  1031 void MetaspaceShared::initialize_shared_spaces() {
  1066 void MetaspaceShared::initialize_shared_spaces() {
  1032   FileMapInfo *mapinfo = FileMapInfo::current_info();
  1067   FileMapInfo *mapinfo = FileMapInfo::current_info();
  1033 
  1068 
  1034   char* buffer = mapinfo->header()->region_addr(md);
  1069   char* buffer = mapinfo->header()->region_addr(md);
  1035 
  1070 
       
  1071   buffer = *((char**)buffer); // skip over the md_alloc'ed blocks
       
  1072 
  1036   // Skip over (reserve space for) a list of addresses of C++ vtables
  1073   // Skip over (reserve space for) a list of addresses of C++ vtables
  1037   // for Klass objects.  They get filled in later.
  1074   // for Klass objects.  They get filled in later.
  1038 
  1075 
  1039   void** vtbl_list = (void**)buffer;
  1076   void** vtbl_list = (void**)buffer;
  1040   buffer += MetaspaceShared::vtbl_list_size * sizeof(void*);
  1077   buffer += MetaspaceShared::vtbl_list_size * sizeof(void*);