< prev index next >

src/hotspot/share/cds/metaspaceShared.cpp

Print this page

  50 #include "classfile/stringTable.hpp"
  51 #include "classfile/symbolTable.hpp"
  52 #include "classfile/systemDictionary.hpp"
  53 #include "classfile/systemDictionaryShared.hpp"
  54 #include "classfile/vmClasses.hpp"
  55 #include "classfile/vmSymbols.hpp"
  56 #include "code/codeCache.hpp"
  57 #include "gc/shared/gcVMOperations.hpp"
  58 #include "interpreter/bytecodeStream.hpp"
  59 #include "interpreter/bytecodes.hpp"
  60 #include "jvm_io.h"
  61 #include "logging/log.hpp"
  62 #include "logging/logMessage.hpp"
  63 #include "logging/logStream.hpp"
  64 #include "memory/metaspace.hpp"
  65 #include "memory/metaspaceClosure.hpp"
  66 #include "memory/resourceArea.hpp"
  67 #include "memory/universe.hpp"
  68 #include "nmt/memTracker.hpp"
  69 #include "oops/compressedKlass.hpp"


  70 #include "oops/instanceMirrorKlass.hpp"
  71 #include "oops/klass.inline.hpp"
  72 #include "oops/objArrayOop.hpp"
  73 #include "oops/oop.inline.hpp"
  74 #include "oops/oopHandle.hpp"
  75 #include "prims/jvmtiExport.hpp"
  76 #include "runtime/arguments.hpp"
  77 #include "runtime/globals.hpp"
  78 #include "runtime/globals_extension.hpp"
  79 #include "runtime/handles.inline.hpp"
  80 #include "runtime/javaCalls.hpp"
  81 #include "runtime/os.inline.hpp"
  82 #include "runtime/safepointVerifiers.hpp"
  83 #include "runtime/sharedRuntime.hpp"
  84 #include "runtime/vmOperations.hpp"
  85 #include "runtime/vmThread.hpp"
  86 #include "sanitizers/leak.hpp"
  87 #include "utilities/align.hpp"
  88 #include "utilities/bitMap.inline.hpp"
  89 #include "utilities/defaultStream.hpp"

  92 
  93 ReservedSpace MetaspaceShared::_symbol_rs;
  94 VirtualSpace MetaspaceShared::_symbol_vs;
  95 bool MetaspaceShared::_archive_loading_failed = false;
  96 bool MetaspaceShared::_remapped_readwrite = false;
  97 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
  98 intx MetaspaceShared::_relocation_delta;
  99 char* MetaspaceShared::_requested_base_address;
 100 bool MetaspaceShared::_use_optimized_module_handling = true;
 101 
 102 // The CDS archive is divided into the following regions:
 103 //     rw  - read-write metadata
 104 //     ro  - read-only metadata and read-only tables
 105 //     hp  - heap region
 106 //     bm  - bitmap for relocating the above 7 regions.
 107 //
 108 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 109 // These regions are aligned with MetaspaceShared::core_region_alignment().
 110 //
 111 // These 2 regions are populated in the following steps:
 112 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
 113 //     temporarily allocated outside of the shared regions.
 114 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 115 // [2] C++ vtables are copied into the rw region.
 116 // [3] ArchiveBuilder copies RW metadata into the rw region.
 117 // [4] ArchiveBuilder copies RO metadata into the ro region.
 118 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 119 //     are copied into the ro region as read-only tables.
 120 //
 121 // The heap region is populated by HeapShared::archive_objects.
 122 //
 123 // The bitmap region is used to relocate the ro/rw/hp regions.
 124 
 125 static DumpRegion _symbol_region("symbols");
 126 
 127 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
 128   return _symbol_region.allocate(num_bytes);
 129 }
 130 
 131 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
 132 // such as linux-aarch64 and macos-x64 ...

 710     if (end != nullptr) *end = '\0';
 711   }
 712   size_t classlist_path_len = strlen(default_classlist);
 713   if (classlist_path_len >= 3) {
 714     if (strcmp(default_classlist + classlist_path_len - 3, "lib") != 0) {
 715       if (classlist_path_len < buf_size - 4) {
 716         jio_snprintf(default_classlist + classlist_path_len,
 717                      buf_size - classlist_path_len,
 718                      "%slib", os::file_separator());
 719         classlist_path_len += 4;
 720       }
 721     }
 722   }
 723   if (classlist_path_len < buf_size - 10) {
 724     jio_snprintf(default_classlist + classlist_path_len,
 725                  buf_size - classlist_path_len,
 726                  "%sclasslist", os::file_separator());
 727   }
 728 }
 729 
 730 void MetaspaceShared::preload_classes(TRAPS) {
 731   char default_classlist[JVM_MAXPATHLEN];
 732   const char* classlist_path;
 733 
 734   get_default_classlist(default_classlist, sizeof(default_classlist));
 735   if (SharedClassListFile == nullptr) {
 736     classlist_path = default_classlist;
 737   } else {
 738     classlist_path = SharedClassListFile;
 739   }
 740 
 741   log_info(cds)("Loading classes to share ...");
 742   ClassListParser::parse_classlist(classlist_path,
 743                                    ClassListParser::_parse_all, CHECK);
 744   if (ExtraSharedClassListFile) {
 745     ClassListParser::parse_classlist(ExtraSharedClassListFile,
 746                                      ClassListParser::_parse_all, CHECK);
 747   }
 748   if (classlist_path != default_classlist) {
 749     struct stat statbuf;
 750     if (os::stat(default_classlist, &statbuf) == 0) {

 755   }
 756 
 757   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 758   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 759   // are archived.
 760   exercise_runtime_cds_code(CHECK);
 761 
 762   log_info(cds)("Loading classes to share: done.");
 763 }
 764 
 765 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 766   // Exercise the manifest processing code
 767   const char* dummy = "Manifest-Version: 1.0\n";
 768   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 769 
 770   // Exercise FileSystem and URL code
 771   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 772 }
 773 
 774 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
 775   preload_classes(CHECK);
 776 
 777   if (SharedArchiveConfigFile) {
 778     log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 779     read_extra_data(THREAD, SharedArchiveConfigFile);
 780     log_info(cds)("Reading extra data: done.");
 781   }
 782 
 783   // Rewrite and link classes
 784   log_info(cds)("Rewriting and linking classes ...");
 785 
 786   // Link any classes which got missed. This would happen if we have loaded classes that
 787   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 788   // fails verification, all other interfaces that were not specified in the classlist but
 789   // are implemented by K are not verified.
 790   link_shared_classes(false/*not from jcmd*/, CHECK);
 791   log_info(cds)("Rewriting and linking classes: done");
 792 
 793 #if INCLUDE_CDS_JAVA_HEAP
 794   if (CDSConfig::is_dumping_heap()) {
 795     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {

 856       BytecodeVerificationLocal = BytecodeVerificationRemote;
 857     }
 858     ik->link_class(THREAD);
 859     if (HAS_PENDING_EXCEPTION) {
 860       ResourceMark rm(THREAD);
 861       log_warning(cds)("Preload Warning: Verification failed for %s",
 862                     ik->external_name());
 863       CLEAR_PENDING_EXCEPTION;
 864       SystemDictionaryShared::set_class_has_failed_verification(ik);
 865     }
 866     ik->compute_has_loops_flag_for_methods();
 867     BytecodeVerificationLocal = saved;
 868     return true;
 869   } else {
 870     return false;
 871   }
 872 }
 873 
 874 #if INCLUDE_CDS_JAVA_HEAP
 875 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {




 876   if(!HeapShared::can_write()) {
 877     log_info(cds)(
 878       "Archived java heap is not supported as UseG1GC "
 879       "and UseCompressedClassPointers are required."
 880       "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
 881       BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
 882     return;
 883   }
 884   // Find all the interned strings that should be dumped.
 885   int i;
 886   for (i = 0; i < klasses->length(); i++) {
 887     Klass* k = klasses->at(i);
 888     if (k->is_instance_klass()) {
 889       InstanceKlass* ik = InstanceKlass::cast(k);
 890       if (ik->is_linked()) {
 891         ik->constants()->add_dumped_interned_strings();
 892       }
 893     }
 894   }
 895   if (_extra_interned_strings != nullptr) {

  50 #include "classfile/stringTable.hpp"
  51 #include "classfile/symbolTable.hpp"
  52 #include "classfile/systemDictionary.hpp"
  53 #include "classfile/systemDictionaryShared.hpp"
  54 #include "classfile/vmClasses.hpp"
  55 #include "classfile/vmSymbols.hpp"
  56 #include "code/codeCache.hpp"
  57 #include "gc/shared/gcVMOperations.hpp"
  58 #include "interpreter/bytecodeStream.hpp"
  59 #include "interpreter/bytecodes.hpp"
  60 #include "jvm_io.h"
  61 #include "logging/log.hpp"
  62 #include "logging/logMessage.hpp"
  63 #include "logging/logStream.hpp"
  64 #include "memory/metaspace.hpp"
  65 #include "memory/metaspaceClosure.hpp"
  66 #include "memory/resourceArea.hpp"
  67 #include "memory/universe.hpp"
  68 #include "nmt/memTracker.hpp"
  69 #include "oops/compressedKlass.hpp"
  70 #include "oops/flatArrayKlass.hpp"
  71 #include "oops/inlineKlass.hpp"
  72 #include "oops/instanceMirrorKlass.hpp"
  73 #include "oops/klass.inline.hpp"
  74 #include "oops/objArrayOop.hpp"
  75 #include "oops/oop.inline.hpp"
  76 #include "oops/oopHandle.hpp"
  77 #include "prims/jvmtiExport.hpp"
  78 #include "runtime/arguments.hpp"
  79 #include "runtime/globals.hpp"
  80 #include "runtime/globals_extension.hpp"
  81 #include "runtime/handles.inline.hpp"
  82 #include "runtime/javaCalls.hpp"
  83 #include "runtime/os.inline.hpp"
  84 #include "runtime/safepointVerifiers.hpp"
  85 #include "runtime/sharedRuntime.hpp"
  86 #include "runtime/vmOperations.hpp"
  87 #include "runtime/vmThread.hpp"
  88 #include "sanitizers/leak.hpp"
  89 #include "utilities/align.hpp"
  90 #include "utilities/bitMap.inline.hpp"
  91 #include "utilities/defaultStream.hpp"

  94 
  95 ReservedSpace MetaspaceShared::_symbol_rs;
  96 VirtualSpace MetaspaceShared::_symbol_vs;
  97 bool MetaspaceShared::_archive_loading_failed = false;
  98 bool MetaspaceShared::_remapped_readwrite = false;
  99 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
 100 intx MetaspaceShared::_relocation_delta;
 101 char* MetaspaceShared::_requested_base_address;
 102 bool MetaspaceShared::_use_optimized_module_handling = true;
 103 
 104 // The CDS archive is divided into the following regions:
 105 //     rw  - read-write metadata
 106 //     ro  - read-only metadata and read-only tables
 107 //     hp  - heap region
 108 //     bm  - bitmap for relocating the above 7 regions.
 109 //
 110 // The rw and ro regions are linearly allocated, in the order of rw->ro.
 111 // These regions are aligned with MetaspaceShared::core_region_alignment().
 112 //
 113 // These 2 regions are populated in the following steps:
 114 // [0] All classes are loaded in MetaspaceShared::loadable_descriptors(). All metadata are
 115 //     temporarily allocated outside of the shared regions.
 116 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
 117 // [2] C++ vtables are copied into the rw region.
 118 // [3] ArchiveBuilder copies RW metadata into the rw region.
 119 // [4] ArchiveBuilder copies RO metadata into the ro region.
 120 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
 121 //     are copied into the ro region as read-only tables.
 122 //
 123 // The heap region is populated by HeapShared::archive_objects.
 124 //
 125 // The bitmap region is used to relocate the ro/rw/hp regions.
 126 
 127 static DumpRegion _symbol_region("symbols");
 128 
 129 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
 130   return _symbol_region.allocate(num_bytes);
 131 }
 132 
 133 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
 134 // such as linux-aarch64 and macos-x64 ...

 712     if (end != nullptr) *end = '\0';
 713   }
 714   size_t classlist_path_len = strlen(default_classlist);
 715   if (classlist_path_len >= 3) {
 716     if (strcmp(default_classlist + classlist_path_len - 3, "lib") != 0) {
 717       if (classlist_path_len < buf_size - 4) {
 718         jio_snprintf(default_classlist + classlist_path_len,
 719                      buf_size - classlist_path_len,
 720                      "%slib", os::file_separator());
 721         classlist_path_len += 4;
 722       }
 723     }
 724   }
 725   if (classlist_path_len < buf_size - 10) {
 726     jio_snprintf(default_classlist + classlist_path_len,
 727                  buf_size - classlist_path_len,
 728                  "%sclasslist", os::file_separator());
 729   }
 730 }
 731 
 732 void MetaspaceShared::loadable_descriptors(TRAPS) {
 733   char default_classlist[JVM_MAXPATHLEN];
 734   const char* classlist_path;
 735 
 736   get_default_classlist(default_classlist, sizeof(default_classlist));
 737   if (SharedClassListFile == nullptr) {
 738     classlist_path = default_classlist;
 739   } else {
 740     classlist_path = SharedClassListFile;
 741   }
 742 
 743   log_info(cds)("Loading classes to share ...");
 744   ClassListParser::parse_classlist(classlist_path,
 745                                    ClassListParser::_parse_all, CHECK);
 746   if (ExtraSharedClassListFile) {
 747     ClassListParser::parse_classlist(ExtraSharedClassListFile,
 748                                      ClassListParser::_parse_all, CHECK);
 749   }
 750   if (classlist_path != default_classlist) {
 751     struct stat statbuf;
 752     if (os::stat(default_classlist, &statbuf) == 0) {

 757   }
 758 
 759   // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
 760   // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
 761   // are archived.
 762   exercise_runtime_cds_code(CHECK);
 763 
 764   log_info(cds)("Loading classes to share: done.");
 765 }
 766 
 767 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
 768   // Exercise the manifest processing code
 769   const char* dummy = "Manifest-Version: 1.0\n";
 770   CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
 771 
 772   // Exercise FileSystem and URL code
 773   CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
 774 }
 775 
 776 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
 777   loadable_descriptors(CHECK);
 778 
 779   if (SharedArchiveConfigFile) {
 780     log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
 781     read_extra_data(THREAD, SharedArchiveConfigFile);
 782     log_info(cds)("Reading extra data: done.");
 783   }
 784 
 785   // Rewrite and link classes
 786   log_info(cds)("Rewriting and linking classes ...");
 787 
 788   // Link any classes which got missed. This would happen if we have loaded classes that
 789   // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
 790   // fails verification, all other interfaces that were not specified in the classlist but
 791   // are implemented by K are not verified.
 792   link_shared_classes(false/*not from jcmd*/, CHECK);
 793   log_info(cds)("Rewriting and linking classes: done");
 794 
 795 #if INCLUDE_CDS_JAVA_HEAP
 796   if (CDSConfig::is_dumping_heap()) {
 797     if (!HeapShared::is_archived_boot_layer_available(THREAD)) {

 858       BytecodeVerificationLocal = BytecodeVerificationRemote;
 859     }
 860     ik->link_class(THREAD);
 861     if (HAS_PENDING_EXCEPTION) {
 862       ResourceMark rm(THREAD);
 863       log_warning(cds)("Preload Warning: Verification failed for %s",
 864                     ik->external_name());
 865       CLEAR_PENDING_EXCEPTION;
 866       SystemDictionaryShared::set_class_has_failed_verification(ik);
 867     }
 868     ik->compute_has_loops_flag_for_methods();
 869     BytecodeVerificationLocal = saved;
 870     return true;
 871   } else {
 872     return false;
 873   }
 874 }
 875 
 876 #if INCLUDE_CDS_JAVA_HEAP
 877 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
 878   if (CDSConfig::is_valhalla_preview()) {
 879     log_info(cds)("Archived java heap is not yet supported with Valhalla preview");
 880     return;
 881   }
 882   if(!HeapShared::can_write()) {
 883     log_info(cds)(
 884       "Archived java heap is not supported as UseG1GC "
 885       "and UseCompressedClassPointers are required."
 886       "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
 887       BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
 888     return;
 889   }
 890   // Find all the interned strings that should be dumped.
 891   int i;
 892   for (i = 0; i < klasses->length(); i++) {
 893     Klass* k = klasses->at(i);
 894     if (k->is_instance_klass()) {
 895       InstanceKlass* ik = InstanceKlass::cast(k);
 896       if (ik->is_linked()) {
 897         ik->constants()->add_dumped_interned_strings();
 898       }
 899     }
 900   }
 901   if (_extra_interned_strings != nullptr) {
< prev index next >