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