56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "classfile/systemDictionaryShared.hpp"
59 #include "classfile/vmClasses.hpp"
60 #include "classfile/vmSymbols.hpp"
61 #include "code/codeCache.hpp"
62 #include "gc/shared/gcVMOperations.hpp"
63 #include "interpreter/bytecodeStream.hpp"
64 #include "interpreter/bytecodes.hpp"
65 #include "jvm_io.h"
66 #include "logging/log.hpp"
67 #include "logging/logMessage.hpp"
68 #include "logging/logStream.hpp"
69 #include "memory/memoryReserver.hpp"
70 #include "memory/metaspace.hpp"
71 #include "memory/metaspaceClosure.hpp"
72 #include "memory/resourceArea.hpp"
73 #include "memory/universe.hpp"
74 #include "nmt/memTracker.hpp"
75 #include "oops/compressedKlass.hpp"
76 #include "oops/instanceMirrorKlass.hpp"
77 #include "oops/klass.inline.hpp"
78 #include "oops/objArrayOop.hpp"
79 #include "oops/oop.inline.hpp"
80 #include "oops/oopHandle.hpp"
81 #include "prims/jvmtiExport.hpp"
82 #include "runtime/arguments.hpp"
83 #include "runtime/globals.hpp"
84 #include "runtime/globals_extension.hpp"
85 #include "runtime/handles.inline.hpp"
86 #include "runtime/javaCalls.hpp"
87 #include "runtime/os.inline.hpp"
88 #include "runtime/safepointVerifiers.hpp"
89 #include "runtime/sharedRuntime.hpp"
90 #include "runtime/vmOperations.hpp"
91 #include "runtime/vmThread.hpp"
92 #include "sanitizers/leak.hpp"
93 #include "utilities/align.hpp"
94 #include "utilities/bitMap.inline.hpp"
95 #include "utilities/defaultStream.hpp"
102 ReservedSpace MetaspaceShared::_symbol_rs;
103 VirtualSpace MetaspaceShared::_symbol_vs;
104 bool MetaspaceShared::_archive_loading_failed = false;
105 bool MetaspaceShared::_remapped_readwrite = false;
106 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
107 intx MetaspaceShared::_relocation_delta;
108 char* MetaspaceShared::_requested_base_address;
109 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
110 bool MetaspaceShared::_use_optimized_module_handling = true;
111
112 // The CDS archive is divided into the following regions:
113 // rw - read-write metadata
114 // ro - read-only metadata and read-only tables
115 // hp - heap region
116 // bm - bitmap for relocating the above 7 regions.
117 //
118 // The rw and ro regions are linearly allocated, in the order of rw->ro.
119 // These regions are aligned with MetaspaceShared::core_region_alignment().
120 //
121 // These 2 regions are populated in the following steps:
122 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
123 // temporarily allocated outside of the shared regions.
124 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
125 // [2] C++ vtables are copied into the rw region.
126 // [3] ArchiveBuilder copies RW metadata into the rw region.
127 // [4] ArchiveBuilder copies RO metadata into the ro region.
128 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
129 // are copied into the ro region as read-only tables.
130 //
131 // The heap region is written by HeapShared::write_heap().
132 //
133 // The bitmap region is used to relocate the ro/rw/hp regions.
134
135 static DumpRegion _symbol_region("symbols");
136
137 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
138 return _symbol_region.allocate(num_bytes);
139 }
140
141 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
142 // such as linux-aarch64 and macos-x64 ...
841 log_debug(cds)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
842 FLAG_SET_ERGO(MinHeapSize, max_heap_size);
843 }
844 if (InitialHeapSize > max_heap_size) {
845 log_debug(cds)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
846 FLAG_SET_ERGO(InitialHeapSize, max_heap_size);
847 }
848 if (MaxHeapSize > max_heap_size) {
849 log_debug(cds)("Setting MaxHeapSize to 4G for CDS dumping, original size = %zuM", MaxHeapSize/M);
850 FLAG_SET_ERGO(MaxHeapSize, max_heap_size);
851 }
852 }
853 #endif // INCLUDE_CDS_JAVA_HEAP && _LP64
854
855 void MetaspaceShared::get_default_classlist(char* default_classlist, const size_t buf_size) {
856 const char* filesep = os::file_separator();
857 jio_snprintf(default_classlist, buf_size, "%s%slib%sclasslist",
858 Arguments::get_java_home(), filesep, filesep);
859 }
860
861 void MetaspaceShared::preload_classes(TRAPS) {
862 char default_classlist[JVM_MAXPATHLEN];
863 const char* classlist_path;
864
865 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
866 if (SharedClassListFile == nullptr) {
867 classlist_path = default_classlist;
868 } else {
869 classlist_path = SharedClassListFile;
870 }
871
872 log_info(cds)("Loading classes to share ...");
873 ClassListParser::parse_classlist(classlist_path,
874 ClassListParser::_parse_all, CHECK);
875 if (ExtraSharedClassListFile) {
876 ClassListParser::parse_classlist(ExtraSharedClassListFile,
877 ClassListParser::_parse_all, CHECK);
878 }
879 if (classlist_path != default_classlist) {
880 struct stat statbuf;
881 if (os::stat(default_classlist, &statbuf) == 0) {
888 // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
889 // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
890 // are archived.
891 exercise_runtime_cds_code(CHECK);
892
893 log_info(cds)("Loading classes to share: done.");
894 }
895
896 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
897 // Exercise the manifest processing code
898 const char* dummy = "Manifest-Version: 1.0\n";
899 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
900
901 // Exercise FileSystem and URL code
902 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
903 }
904
905 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
906 if (CDSConfig::is_dumping_classic_static_archive()) {
907 // We are running with -Xshare:dump
908 preload_classes(CHECK);
909
910 if (SharedArchiveConfigFile) {
911 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
912 read_extra_data(THREAD, SharedArchiveConfigFile);
913 log_info(cds)("Reading extra data: done.");
914 }
915 }
916
917 if (CDSConfig::is_dumping_preimage_static_archive()) {
918 log_info(cds)("Reading lambda form invokers from JDK default classlist ...");
919 char default_classlist[JVM_MAXPATHLEN];
920 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
921 struct stat statbuf;
922 if (os::stat(default_classlist, &statbuf) == 0) {
923 ClassListParser::parse_classlist(default_classlist,
924 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
925 }
926 }
927
928 #if INCLUDE_CDS_JAVA_HEAP
1054 }
1055 ik->link_class(THREAD);
1056 if (HAS_PENDING_EXCEPTION) {
1057 ResourceMark rm(THREAD);
1058 log_warning(cds)("Preload Warning: Verification failed for %s",
1059 ik->external_name());
1060 CLEAR_PENDING_EXCEPTION;
1061 SystemDictionaryShared::set_class_has_failed_verification(ik);
1062 } else {
1063 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1064 ik->compute_has_loops_flag_for_methods();
1065 }
1066 BytecodeVerificationLocal = saved;
1067 return true;
1068 } else {
1069 return false;
1070 }
1071 }
1072
1073 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1074 if (CDSConfig::is_dumping_heap()) {
1075 HeapShared::write_heap(&_heap_info);
1076 } else {
1077 CDSConfig::log_reasons_for_not_dumping_heap();
1078 }
1079 }
1080
1081 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1082 assert(base <= static_top && static_top <= top, "must be");
1083 _shared_metaspace_static_top = static_top;
1084 MetaspaceObj::set_shared_metaspace_range(base, top);
1085 }
1086
1087 bool MetaspaceShared::is_shared_dynamic(void* p) {
1088 if ((p < MetaspaceObj::shared_metaspace_top()) &&
1089 (p >= _shared_metaspace_static_top)) {
1090 return true;
1091 } else {
1092 return false;
1093 }
|
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "classfile/systemDictionaryShared.hpp"
59 #include "classfile/vmClasses.hpp"
60 #include "classfile/vmSymbols.hpp"
61 #include "code/codeCache.hpp"
62 #include "gc/shared/gcVMOperations.hpp"
63 #include "interpreter/bytecodeStream.hpp"
64 #include "interpreter/bytecodes.hpp"
65 #include "jvm_io.h"
66 #include "logging/log.hpp"
67 #include "logging/logMessage.hpp"
68 #include "logging/logStream.hpp"
69 #include "memory/memoryReserver.hpp"
70 #include "memory/metaspace.hpp"
71 #include "memory/metaspaceClosure.hpp"
72 #include "memory/resourceArea.hpp"
73 #include "memory/universe.hpp"
74 #include "nmt/memTracker.hpp"
75 #include "oops/compressedKlass.hpp"
76 #include "oops/flatArrayKlass.hpp"
77 #include "oops/inlineKlass.hpp"
78 #include "oops/instanceMirrorKlass.hpp"
79 #include "oops/klass.inline.hpp"
80 #include "oops/objArrayOop.hpp"
81 #include "oops/oop.inline.hpp"
82 #include "oops/oopHandle.hpp"
83 #include "prims/jvmtiExport.hpp"
84 #include "runtime/arguments.hpp"
85 #include "runtime/globals.hpp"
86 #include "runtime/globals_extension.hpp"
87 #include "runtime/handles.inline.hpp"
88 #include "runtime/javaCalls.hpp"
89 #include "runtime/os.inline.hpp"
90 #include "runtime/safepointVerifiers.hpp"
91 #include "runtime/sharedRuntime.hpp"
92 #include "runtime/vmOperations.hpp"
93 #include "runtime/vmThread.hpp"
94 #include "sanitizers/leak.hpp"
95 #include "utilities/align.hpp"
96 #include "utilities/bitMap.inline.hpp"
97 #include "utilities/defaultStream.hpp"
104 ReservedSpace MetaspaceShared::_symbol_rs;
105 VirtualSpace MetaspaceShared::_symbol_vs;
106 bool MetaspaceShared::_archive_loading_failed = false;
107 bool MetaspaceShared::_remapped_readwrite = false;
108 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
109 intx MetaspaceShared::_relocation_delta;
110 char* MetaspaceShared::_requested_base_address;
111 Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
112 bool MetaspaceShared::_use_optimized_module_handling = true;
113
114 // The CDS archive is divided into the following regions:
115 // rw - read-write metadata
116 // ro - read-only metadata and read-only tables
117 // hp - heap region
118 // bm - bitmap for relocating the above 7 regions.
119 //
120 // The rw and ro regions are linearly allocated, in the order of rw->ro.
121 // These regions are aligned with MetaspaceShared::core_region_alignment().
122 //
123 // These 2 regions are populated in the following steps:
124 // [0] All classes are loaded in MetaspaceShared::loadable_descriptors(). All metadata are
125 // temporarily allocated outside of the shared regions.
126 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
127 // [2] C++ vtables are copied into the rw region.
128 // [3] ArchiveBuilder copies RW metadata into the rw region.
129 // [4] ArchiveBuilder copies RO metadata into the ro region.
130 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
131 // are copied into the ro region as read-only tables.
132 //
133 // The heap region is written by HeapShared::write_heap().
134 //
135 // The bitmap region is used to relocate the ro/rw/hp regions.
136
137 static DumpRegion _symbol_region("symbols");
138
139 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
140 return _symbol_region.allocate(num_bytes);
141 }
142
143 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
144 // such as linux-aarch64 and macos-x64 ...
843 log_debug(cds)("Setting MinHeapSize to 4G for CDS dumping, original size = %zuM", MinHeapSize/M);
844 FLAG_SET_ERGO(MinHeapSize, max_heap_size);
845 }
846 if (InitialHeapSize > max_heap_size) {
847 log_debug(cds)("Setting InitialHeapSize to 4G for CDS dumping, original size = %zuM", InitialHeapSize/M);
848 FLAG_SET_ERGO(InitialHeapSize, max_heap_size);
849 }
850 if (MaxHeapSize > max_heap_size) {
851 log_debug(cds)("Setting MaxHeapSize to 4G for CDS dumping, original size = %zuM", MaxHeapSize/M);
852 FLAG_SET_ERGO(MaxHeapSize, max_heap_size);
853 }
854 }
855 #endif // INCLUDE_CDS_JAVA_HEAP && _LP64
856
857 void MetaspaceShared::get_default_classlist(char* default_classlist, const size_t buf_size) {
858 const char* filesep = os::file_separator();
859 jio_snprintf(default_classlist, buf_size, "%s%slib%sclasslist",
860 Arguments::get_java_home(), filesep, filesep);
861 }
862
863 void MetaspaceShared::loadable_descriptors(TRAPS) {
864 char default_classlist[JVM_MAXPATHLEN];
865 const char* classlist_path;
866
867 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
868 if (SharedClassListFile == nullptr) {
869 classlist_path = default_classlist;
870 } else {
871 classlist_path = SharedClassListFile;
872 }
873
874 log_info(cds)("Loading classes to share ...");
875 ClassListParser::parse_classlist(classlist_path,
876 ClassListParser::_parse_all, CHECK);
877 if (ExtraSharedClassListFile) {
878 ClassListParser::parse_classlist(ExtraSharedClassListFile,
879 ClassListParser::_parse_all, CHECK);
880 }
881 if (classlist_path != default_classlist) {
882 struct stat statbuf;
883 if (os::stat(default_classlist, &statbuf) == 0) {
890 // Some classes are used at CDS runtime but are not loaded, and therefore archived, at
891 // dumptime. We can perform dummmy calls to these classes at dumptime to ensure they
892 // are archived.
893 exercise_runtime_cds_code(CHECK);
894
895 log_info(cds)("Loading classes to share: done.");
896 }
897
898 void MetaspaceShared::exercise_runtime_cds_code(TRAPS) {
899 // Exercise the manifest processing code
900 const char* dummy = "Manifest-Version: 1.0\n";
901 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
902
903 // Exercise FileSystem and URL code
904 CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
905 }
906
907 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
908 if (CDSConfig::is_dumping_classic_static_archive()) {
909 // We are running with -Xshare:dump
910 loadable_descriptors(CHECK);
911
912 if (SharedArchiveConfigFile) {
913 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
914 read_extra_data(THREAD, SharedArchiveConfigFile);
915 log_info(cds)("Reading extra data: done.");
916 }
917 }
918
919 if (CDSConfig::is_dumping_preimage_static_archive()) {
920 log_info(cds)("Reading lambda form invokers from JDK default classlist ...");
921 char default_classlist[JVM_MAXPATHLEN];
922 get_default_classlist(default_classlist, JVM_MAXPATHLEN);
923 struct stat statbuf;
924 if (os::stat(default_classlist, &statbuf) == 0) {
925 ClassListParser::parse_classlist(default_classlist,
926 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
927 }
928 }
929
930 #if INCLUDE_CDS_JAVA_HEAP
1056 }
1057 ik->link_class(THREAD);
1058 if (HAS_PENDING_EXCEPTION) {
1059 ResourceMark rm(THREAD);
1060 log_warning(cds)("Preload Warning: Verification failed for %s",
1061 ik->external_name());
1062 CLEAR_PENDING_EXCEPTION;
1063 SystemDictionaryShared::set_class_has_failed_verification(ik);
1064 } else {
1065 assert(!SystemDictionaryShared::has_class_failed_verification(ik), "sanity");
1066 ik->compute_has_loops_flag_for_methods();
1067 }
1068 BytecodeVerificationLocal = saved;
1069 return true;
1070 } else {
1071 return false;
1072 }
1073 }
1074
1075 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() {
1076 if (CDSConfig::is_valhalla_preview()) {
1077 log_info(cds)("Archived java heap is not yet supported with Valhalla preview");
1078 return;
1079 }
1080
1081 if (CDSConfig::is_dumping_heap()) {
1082 HeapShared::write_heap(&_heap_info);
1083 } else {
1084 CDSConfig::log_reasons_for_not_dumping_heap();
1085 }
1086 }
1087
1088 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) {
1089 assert(base <= static_top && static_top <= top, "must be");
1090 _shared_metaspace_static_top = static_top;
1091 MetaspaceObj::set_shared_metaspace_range(base, top);
1092 }
1093
1094 bool MetaspaceShared::is_shared_dynamic(void* p) {
1095 if ((p < MetaspaceObj::shared_metaspace_top()) &&
1096 (p >= _shared_metaspace_static_top)) {
1097 return true;
1098 } else {
1099 return false;
1100 }
|