7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
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/classLoaderDataGraph.hpp"
27 #include "classfile/classListParser.hpp"
28 #include "classfile/classLoaderExt.hpp"
29 #include "classfile/loaderConstraints.hpp"
30 #include "classfile/javaClasses.inline.hpp"
31 #include "classfile/placeholders.hpp"
32 #include "classfile/symbolTable.hpp"
33 #include "classfile/stringTable.hpp"
34 #include "classfile/systemDictionary.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "code/codeCache.hpp"
37 #include "interpreter/bytecodeStream.hpp"
38 #include "interpreter/bytecodes.hpp"
39 #include "logging/log.hpp"
40 #include "logging/logMessage.hpp"
41 #include "memory/archiveBuilder.hpp"
42 #include "memory/archiveUtils.inline.hpp"
43 #include "memory/dynamicArchive.hpp"
44 #include "memory/filemap.hpp"
45 #include "memory/heapShared.inline.hpp"
46 #include "memory/metaspace.hpp"
47 #include "memory/metaspaceClosure.hpp"
48 #include "memory/metaspaceShared.hpp"
49 #include "memory/resourceArea.hpp"
50 #include "memory/universe.hpp"
51 #include "oops/compressedOops.inline.hpp"
52 #include "oops/instanceClassLoaderKlass.hpp"
53 #include "oops/instanceMirrorKlass.hpp"
54 #include "oops/instanceRefKlass.hpp"
55 #include "oops/methodData.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.hpp"
58 #include "oops/oop.inline.hpp"
59 #include "oops/typeArrayKlass.hpp"
60 #include "runtime/handles.inline.hpp"
61 #include "runtime/os.hpp"
62 #include "runtime/safepointVerifiers.hpp"
69 #include "utilities/defaultStream.hpp"
70 #include "utilities/hashtable.inline.hpp"
71 #if INCLUDE_G1GC
72 #include "gc/g1/g1CollectedHeap.hpp"
73 #endif
74
75 ReservedSpace MetaspaceShared::_shared_rs;
76 VirtualSpace MetaspaceShared::_shared_vs;
77 ReservedSpace MetaspaceShared::_symbol_rs;
78 VirtualSpace MetaspaceShared::_symbol_vs;
79 MetaspaceSharedStats MetaspaceShared::_stats;
80 bool MetaspaceShared::_has_error_classes;
81 bool MetaspaceShared::_archive_loading_failed = false;
82 bool MetaspaceShared::_remapped_readwrite = false;
83 address MetaspaceShared::_i2i_entry_code_buffers = NULL;
84 size_t MetaspaceShared::_i2i_entry_code_buffers_size = 0;
85 void* MetaspaceShared::_shared_metaspace_static_top = NULL;
86 intx MetaspaceShared::_relocation_delta;
87 char* MetaspaceShared::_requested_base_address;
88 bool MetaspaceShared::_use_optimized_module_handling = true;
89
90 // The CDS archive is divided into the following regions:
91 // mc - misc code (the method entry trampolines, c++ vtables)
92 // rw - read-write metadata
93 // ro - read-only metadata and read-only tables
94 //
95 // ca0 - closed archive heap space #0
96 // ca1 - closed archive heap space #1 (may be empty)
97 // oa0 - open archive heap space #0
98 // oa1 - open archive heap space #1 (may be empty)
99 //
100 // The mc, rw, and ro regions are linearly allocated, starting from
101 // SharedBaseAddress, in the order of mc->rw->ro. The size of these 3 regions
102 // are page-aligned, and there's no gap between any consecutive regions.
103 //
104 // These 3 regions are populated in the following steps:
105 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
106 // temporarily allocated outside of the shared regions. Only the method entry
107 // trampolines are written into the mc region.
108 // [2] C++ vtables are copied into the mc region.
133 return &_ro_region;
134 }
135
136 void MetaspaceShared::pack_dump_space(DumpRegion* current, DumpRegion* next,
137 ReservedSpace* rs) {
138 current->pack(next);
139 }
140
141 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
142 return _symbol_region.allocate(num_bytes);
143 }
144
145 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
146 return _mc_region.allocate(num_bytes);
147 }
148
149 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
150 return _ro_region.allocate(num_bytes);
151 }
152
153 size_t MetaspaceShared::reserved_space_alignment() { return os::vm_allocation_granularity(); }
154
155 static bool shared_base_valid(char* shared_base) {
156 #ifdef _LP64
157 return CompressedKlassPointers::is_valid_base((address)shared_base);
158 #else
159 return true;
160 #endif
161 }
162
163 static bool shared_base_too_high(char* shared_base, size_t cds_total) {
164 if (SharedBaseAddress != 0 && shared_base < (char*)SharedBaseAddress) {
165 // SharedBaseAddress is very high (e.g., 0xffffffffffffff00) so
166 // align_up(SharedBaseAddress, MetaspaceShared::reserved_space_alignment()) has wrapped around.
167 return true;
168 }
169 if (max_uintx - uintx(shared_base) < uintx(cds_total)) {
170 // The end of the archive will wrap around
171 return true;
172 }
482
483 // Dump/restore references to commonly used names and signatures.
484 vmSymbols::serialize(soc);
485 soc->do_tag(--tag);
486
487 // Dump/restore the symbol/string/subgraph_info tables
488 SymbolTable::serialize_shared_table_header(soc);
489 StringTable::serialize_shared_table_header(soc);
490 HeapShared::serialize_subgraph_info_table_header(soc);
491 SystemDictionaryShared::serialize_dictionary_headers(soc);
492
493 InstanceMirrorKlass::serialize_offsets(soc);
494
495 // Dump/restore well known classes (pointers)
496 SystemDictionaryShared::serialize_well_known_klasses(soc);
497 soc->do_tag(--tag);
498
499 serialize_cloned_cpp_vtptrs(soc);
500 soc->do_tag(--tag);
501
502 soc->do_tag(666);
503 }
504
505 address MetaspaceShared::i2i_entry_code_buffers(size_t total_size) {
506 if (DumpSharedSpaces) {
507 if (_i2i_entry_code_buffers == NULL) {
508 _i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
509 _i2i_entry_code_buffers_size = total_size;
510 }
511 } else if (UseSharedSpaces) {
512 assert(_i2i_entry_code_buffers != NULL, "must already been initialized");
513 } else {
514 return NULL;
515 }
516
517 assert(_i2i_entry_code_buffers_size == total_size, "must not change");
518 return _i2i_entry_code_buffers;
519 }
520
521 uintx MetaspaceShared::object_delta_uintx(void* obj) {
1046 StaticArchiveBuilder builder(&_rw_region, &_ro_region);
1047 builder.gather_klasses_and_symbols();
1048 _global_klass_objects = builder.klasses();
1049
1050 // Ensure the ConstMethods won't be modified at run-time
1051 log_info(cds)("Updating ConstMethods ... ");
1052 rewrite_nofast_bytecodes_and_calculate_fingerprints(THREAD);
1053 log_info(cds)("done. ");
1054
1055 // Remove all references outside the metadata
1056 log_info(cds)("Removing unshareable information ... ");
1057 remove_unshareable_in_classes();
1058 log_info(cds)("done. ");
1059
1060 builder.gather_source_objs();
1061
1062 MetaspaceShared::allocate_cloned_cpp_vtptrs();
1063 char* cloned_vtables = _mc_region.top();
1064 MetaspaceShared::allocate_cpp_vtable_clones();
1065
1066 _mc_region.pack(&_rw_region);
1067 builder.dump_rw_region();
1068 _rw_region.pack(&_ro_region);
1069 builder.dump_ro_region();
1070 builder.relocate_pointers();
1071
1072 dump_symbols();
1073
1074 // Dump supported java heap objects
1075 _closed_archive_heap_regions = NULL;
1076 _open_archive_heap_regions = NULL;
1077 dump_java_heap_objects();
1078
1079 builder.relocate_well_known_klasses();
1080
1081 char* serialized_data = dump_read_only_tables();
1082 _ro_region.pack();
1083
1084 // The vtable clones contain addresses of the current process.
1085 // We don't want to write these addresses into the archive. Same for i2i buffer.
1086 MetaspaceShared::zero_cpp_vtable_clones_for_writing();
1087 memset(MetaspaceShared::i2i_entry_code_buffers(), 0,
1088 MetaspaceShared::i2i_entry_code_buffers_size());
1089
1201 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1202 }
1203
1204 // Update a Java object to point its Klass* to the new location after
1205 // shared archive has been compacted.
1206 void MetaspaceShared::relocate_klass_ptr(oop o) {
1207 assert(DumpSharedSpaces, "sanity");
1208 Klass* k = ArchiveBuilder::get_relocated_klass(o->klass());
1209 o->set_klass(k);
1210 }
1211
1212 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1213 assert(DumpSharedSpaces, "sanity");
1214 k = ArchiveBuilder::get_relocated_klass(k);
1215 if (is_final) {
1216 k = (Klass*)(address(k) + final_delta());
1217 }
1218 return k;
1219 }
1220
1221 class LinkSharedClassesClosure : public KlassClosure {
1222 Thread* THREAD;
1223 bool _made_progress;
1224 public:
1225 LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1226
1227 void reset() { _made_progress = false; }
1228 bool made_progress() const { return _made_progress; }
1229
1230 void do_klass(Klass* k) {
1231 if (k->is_instance_klass()) {
1232 InstanceKlass* ik = InstanceKlass::cast(k);
1233 // For dynamic CDS dump, only link classes loaded by the builtin class loaders.
1234 bool do_linking = DumpSharedSpaces ? true : !ik->is_shared_unregistered_class();
1235 if (do_linking) {
1236 // Link the class to cause the bytecodes to be rewritten and the
1237 // cpcache to be created. Class verification is done according
1238 // to -Xverify setting.
1239 _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1240 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1315 log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1316
1317 if (SharedArchiveConfigFile) {
1318 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1319 read_extra_data(SharedArchiveConfigFile, THREAD);
1320 }
1321 log_info(cds)("Reading extra data: done.");
1322
1323 HeapShared::init_subgraph_entry_fields(THREAD);
1324
1325 // Rewrite and link classes
1326 log_info(cds)("Rewriting and linking classes ...");
1327
1328 // Link any classes which got missed. This would happen if we have loaded classes that
1329 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1330 // fails verification, all other interfaces that were not specified in the classlist but
1331 // are implemented by K are not verified.
1332 link_and_cleanup_shared_classes(CATCH);
1333 log_info(cds)("Rewriting and linking classes: done");
1334
1335 VM_PopulateDumpSharedSpace op;
1336 MutexLocker ml(THREAD, HeapShared::is_heap_object_archiving_allowed() ?
1337 Heap_lock : NULL); // needed by HeapShared::run_gc()
1338 VMThread::execute(&op);
1339 }
1340 }
1341
1342
1343 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1344 ClassListParser parser(class_list_path);
1345 int class_count = 0;
1346
1347 while (parser.parse_one_line()) {
1348 Klass* klass = parser.load_current_class(THREAD);
1349 if (HAS_PENDING_EXCEPTION) {
1350 if (klass == NULL &&
1351 (PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_ClassNotFoundException())) {
1352 // print a warning only when the pending exception is class not found
1353 log_warning(cds)("Preload Warning: Cannot find %s", parser.current_class_name());
1354 }
1682 }
1683
1684 if (result == MAP_ARCHIVE_SUCCESS) {
1685 SharedBaseAddress = (size_t)mapped_base_address;
1686 LP64_ONLY({
1687 if (Metaspace::using_class_space()) {
1688 // Set up ccs in metaspace.
1689 Metaspace::initialize_class_space(class_space_rs);
1690
1691 // Set up compressed Klass pointer encoding: the encoding range must
1692 // cover both archive and class space.
1693 address cds_base = (address)static_mapinfo->mapped_base();
1694 address ccs_end = (address)class_space_rs.end();
1695 CompressedKlassPointers::initialize(cds_base, ccs_end - cds_base);
1696
1697 // map_heap_regions() compares the current narrow oop and klass encodings
1698 // with the archived ones, so it must be done after all encodings are determined.
1699 static_mapinfo->map_heap_regions();
1700 }
1701 });
1702 log_info(cds)("Using optimized module handling %s", MetaspaceShared::use_optimized_module_handling() ? "enabled" : "disabled");
1703 } else {
1704 unmap_archive(static_mapinfo);
1705 unmap_archive(dynamic_mapinfo);
1706 release_reserved_spaces(archive_space_rs, class_space_rs);
1707 }
1708
1709 return result;
1710 }
1711
1712
1713 // This will reserve two address spaces suitable to house Klass structures, one
1714 // for the cds archives (static archive and optionally dynamic archive) and
1715 // optionally one move for ccs.
1716 //
1717 // Since both spaces must fall within the compressed class pointer encoding
1718 // range, they are allocated close to each other.
1719 //
1720 // Space for archives will be reserved first, followed by a potential gap,
1721 // followed by the space for ccs:
1722 //
2005 return true;
2006 }
2007
2008 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
2009 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
2010 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
2011 // or so.
2012 _mc_region.print_out_of_space_msg(name, needed_bytes);
2013 _rw_region.print_out_of_space_msg(name, needed_bytes);
2014 _ro_region.print_out_of_space_msg(name, needed_bytes);
2015
2016 vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
2017 "Please reduce the number of shared classes.");
2018 }
2019
2020 // This is used to relocate the pointers so that the base archive can be mapped at
2021 // MetaspaceShared::requested_base_address() without runtime relocation.
2022 intx MetaspaceShared::final_delta() {
2023 return intx(MetaspaceShared::requested_base_address()) // We want the base archive to be mapped to here at runtime
2024 - intx(SharedBaseAddress); // .. but the base archive is mapped at here at dump time
2025 }
2026
2027 void MetaspaceShared::print_on(outputStream* st) {
2028 if (UseSharedSpaces || DumpSharedSpaces) {
2029 st->print("CDS archive(s) mapped at: ");
2030 address base;
2031 address top;
2032 if (UseSharedSpaces) { // Runtime
2033 base = (address)MetaspaceObj::shared_metaspace_base();
2034 address static_top = (address)_shared_metaspace_static_top;
2035 top = (address)MetaspaceObj::shared_metaspace_top();
2036 st->print("[" PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(static_top), p2i(top));
2037 } else if (DumpSharedSpaces) { // Dump Time
2038 base = (address)_shared_rs.base();
2039 top = (address)_shared_rs.end();
2040 st->print("[" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(top));
2041 }
2042 st->print("size " SIZE_FORMAT ", ", top - base);
2043 st->print("SharedBaseAddress: " PTR_FORMAT ", ArchiveRelocationMode: %d.", SharedBaseAddress, (int)ArchiveRelocationMode);
2044 } else {
|
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
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/classLoaderDataGraph.hpp"
27 #include "classfile/classLoaderDataShared.hpp"
28 #include "classfile/classListParser.hpp"
29 #include "classfile/classLoaderExt.hpp"
30 #include "classfile/loaderConstraints.hpp"
31 #include "classfile/javaClasses.inline.hpp"
32 #include "classfile/placeholders.hpp"
33 #include "classfile/symbolTable.hpp"
34 #include "classfile/stringTable.hpp"
35 #include "classfile/systemDictionary.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "code/codeCache.hpp"
38 #include "interpreter/bytecodeStream.hpp"
39 #include "interpreter/bytecodes.hpp"
40 #include "logging/log.hpp"
41 #include "logging/logMessage.hpp"
42 #include "memory/archiveBuilder.hpp"
43 #include "memory/archiveUtils.inline.hpp"
44 #include "memory/dumpAllocStats.hpp"
45 #include "memory/dynamicArchive.hpp"
46 #include "memory/filemap.hpp"
47 #include "memory/heapShared.inline.hpp"
48 #include "memory/metaspace.hpp"
49 #include "memory/metaspaceClosure.hpp"
50 #include "memory/metaspaceShared.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "oops/compressedOops.inline.hpp"
54 #include "oops/instanceClassLoaderKlass.hpp"
55 #include "oops/instanceMirrorKlass.hpp"
56 #include "oops/instanceRefKlass.hpp"
57 #include "oops/methodData.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/objArrayOop.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/typeArrayKlass.hpp"
62 #include "runtime/handles.inline.hpp"
63 #include "runtime/os.hpp"
64 #include "runtime/safepointVerifiers.hpp"
71 #include "utilities/defaultStream.hpp"
72 #include "utilities/hashtable.inline.hpp"
73 #if INCLUDE_G1GC
74 #include "gc/g1/g1CollectedHeap.hpp"
75 #endif
76
77 ReservedSpace MetaspaceShared::_shared_rs;
78 VirtualSpace MetaspaceShared::_shared_vs;
79 ReservedSpace MetaspaceShared::_symbol_rs;
80 VirtualSpace MetaspaceShared::_symbol_vs;
81 MetaspaceSharedStats MetaspaceShared::_stats;
82 bool MetaspaceShared::_has_error_classes;
83 bool MetaspaceShared::_archive_loading_failed = false;
84 bool MetaspaceShared::_remapped_readwrite = false;
85 address MetaspaceShared::_i2i_entry_code_buffers = NULL;
86 size_t MetaspaceShared::_i2i_entry_code_buffers_size = 0;
87 void* MetaspaceShared::_shared_metaspace_static_top = NULL;
88 intx MetaspaceShared::_relocation_delta;
89 char* MetaspaceShared::_requested_base_address;
90 bool MetaspaceShared::_use_optimized_module_handling = true;
91 bool MetaspaceShared::_use_full_module_graph = true;
92
93 // The CDS archive is divided into the following regions:
94 // mc - misc code (the method entry trampolines, c++ vtables)
95 // rw - read-write metadata
96 // ro - read-only metadata and read-only tables
97 //
98 // ca0 - closed archive heap space #0
99 // ca1 - closed archive heap space #1 (may be empty)
100 // oa0 - open archive heap space #0
101 // oa1 - open archive heap space #1 (may be empty)
102 //
103 // The mc, rw, and ro regions are linearly allocated, starting from
104 // SharedBaseAddress, in the order of mc->rw->ro. The size of these 3 regions
105 // are page-aligned, and there's no gap between any consecutive regions.
106 //
107 // These 3 regions are populated in the following steps:
108 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
109 // temporarily allocated outside of the shared regions. Only the method entry
110 // trampolines are written into the mc region.
111 // [2] C++ vtables are copied into the mc region.
136 return &_ro_region;
137 }
138
139 void MetaspaceShared::pack_dump_space(DumpRegion* current, DumpRegion* next,
140 ReservedSpace* rs) {
141 current->pack(next);
142 }
143
144 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
145 return _symbol_region.allocate(num_bytes);
146 }
147
148 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) {
149 return _mc_region.allocate(num_bytes);
150 }
151
152 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) {
153 return _ro_region.allocate(num_bytes);
154 }
155
156 char* MetaspaceShared::read_write_space_alloc(size_t num_bytes) {
157 return _rw_region.allocate(num_bytes);
158 }
159
160 size_t MetaspaceShared::reserved_space_alignment() { return os::vm_allocation_granularity(); }
161
162 static bool shared_base_valid(char* shared_base) {
163 #ifdef _LP64
164 return CompressedKlassPointers::is_valid_base((address)shared_base);
165 #else
166 return true;
167 #endif
168 }
169
170 static bool shared_base_too_high(char* shared_base, size_t cds_total) {
171 if (SharedBaseAddress != 0 && shared_base < (char*)SharedBaseAddress) {
172 // SharedBaseAddress is very high (e.g., 0xffffffffffffff00) so
173 // align_up(SharedBaseAddress, MetaspaceShared::reserved_space_alignment()) has wrapped around.
174 return true;
175 }
176 if (max_uintx - uintx(shared_base) < uintx(cds_total)) {
177 // The end of the archive will wrap around
178 return true;
179 }
489
490 // Dump/restore references to commonly used names and signatures.
491 vmSymbols::serialize(soc);
492 soc->do_tag(--tag);
493
494 // Dump/restore the symbol/string/subgraph_info tables
495 SymbolTable::serialize_shared_table_header(soc);
496 StringTable::serialize_shared_table_header(soc);
497 HeapShared::serialize_subgraph_info_table_header(soc);
498 SystemDictionaryShared::serialize_dictionary_headers(soc);
499
500 InstanceMirrorKlass::serialize_offsets(soc);
501
502 // Dump/restore well known classes (pointers)
503 SystemDictionaryShared::serialize_well_known_klasses(soc);
504 soc->do_tag(--tag);
505
506 serialize_cloned_cpp_vtptrs(soc);
507 soc->do_tag(--tag);
508
509 CDS_JAVA_HEAP_ONLY(ClassLoaderDataShared::serialize(soc));
510
511 soc->do_tag(666);
512 }
513
514 address MetaspaceShared::i2i_entry_code_buffers(size_t total_size) {
515 if (DumpSharedSpaces) {
516 if (_i2i_entry_code_buffers == NULL) {
517 _i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size);
518 _i2i_entry_code_buffers_size = total_size;
519 }
520 } else if (UseSharedSpaces) {
521 assert(_i2i_entry_code_buffers != NULL, "must already been initialized");
522 } else {
523 return NULL;
524 }
525
526 assert(_i2i_entry_code_buffers_size == total_size, "must not change");
527 return _i2i_entry_code_buffers;
528 }
529
530 uintx MetaspaceShared::object_delta_uintx(void* obj) {
1055 StaticArchiveBuilder builder(&_rw_region, &_ro_region);
1056 builder.gather_klasses_and_symbols();
1057 _global_klass_objects = builder.klasses();
1058
1059 // Ensure the ConstMethods won't be modified at run-time
1060 log_info(cds)("Updating ConstMethods ... ");
1061 rewrite_nofast_bytecodes_and_calculate_fingerprints(THREAD);
1062 log_info(cds)("done. ");
1063
1064 // Remove all references outside the metadata
1065 log_info(cds)("Removing unshareable information ... ");
1066 remove_unshareable_in_classes();
1067 log_info(cds)("done. ");
1068
1069 builder.gather_source_objs();
1070
1071 MetaspaceShared::allocate_cloned_cpp_vtptrs();
1072 char* cloned_vtables = _mc_region.top();
1073 MetaspaceShared::allocate_cpp_vtable_clones();
1074
1075 {
1076 _mc_region.pack(&_rw_region);
1077 builder.dump_rw_region();
1078 #if INCLUDE_CDS_JAVA_HEAP
1079 if (MetaspaceShared::use_full_module_graph()) {
1080 // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders
1081 char* start = _rw_region.top();
1082 ClassLoaderDataShared::allocate_archived_tables();
1083 ArchiveBuilder::alloc_stats()->record_modules(_rw_region.top() - start, /*read_only*/false);
1084 }
1085 #endif
1086 }
1087 {
1088 _rw_region.pack(&_ro_region);
1089 builder.dump_ro_region();
1090 #if INCLUDE_CDS_JAVA_HEAP
1091 if (MetaspaceShared::use_full_module_graph()) {
1092 char* start = _ro_region.top();
1093 ClassLoaderDataShared::init_archived_tables();
1094 ArchiveBuilder::alloc_stats()->record_modules(_ro_region.top() - start, /*read_only*/true);
1095 }
1096 #endif
1097 }
1098 builder.relocate_pointers();
1099
1100 dump_symbols();
1101
1102 // Dump supported java heap objects
1103 _closed_archive_heap_regions = NULL;
1104 _open_archive_heap_regions = NULL;
1105 dump_java_heap_objects();
1106
1107 builder.relocate_well_known_klasses();
1108
1109 char* serialized_data = dump_read_only_tables();
1110 _ro_region.pack();
1111
1112 // The vtable clones contain addresses of the current process.
1113 // We don't want to write these addresses into the archive. Same for i2i buffer.
1114 MetaspaceShared::zero_cpp_vtable_clones_for_writing();
1115 memset(MetaspaceShared::i2i_entry_code_buffers(), 0,
1116 MetaspaceShared::i2i_entry_code_buffers_size());
1117
1229 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1230 }
1231
1232 // Update a Java object to point its Klass* to the new location after
1233 // shared archive has been compacted.
1234 void MetaspaceShared::relocate_klass_ptr(oop o) {
1235 assert(DumpSharedSpaces, "sanity");
1236 Klass* k = ArchiveBuilder::get_relocated_klass(o->klass());
1237 o->set_klass(k);
1238 }
1239
1240 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) {
1241 assert(DumpSharedSpaces, "sanity");
1242 k = ArchiveBuilder::get_relocated_klass(k);
1243 if (is_final) {
1244 k = (Klass*)(address(k) + final_delta());
1245 }
1246 return k;
1247 }
1248
1249 Symbol* MetaspaceShared::get_relocated_symbol(Symbol* orig_symbol) {
1250 return ArchiveBuilder::get_relocated_symbol(orig_symbol);
1251 }
1252
1253 class LinkSharedClassesClosure : public KlassClosure {
1254 Thread* THREAD;
1255 bool _made_progress;
1256 public:
1257 LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
1258
1259 void reset() { _made_progress = false; }
1260 bool made_progress() const { return _made_progress; }
1261
1262 void do_klass(Klass* k) {
1263 if (k->is_instance_klass()) {
1264 InstanceKlass* ik = InstanceKlass::cast(k);
1265 // For dynamic CDS dump, only link classes loaded by the builtin class loaders.
1266 bool do_linking = DumpSharedSpaces ? true : !ik->is_shared_unregistered_class();
1267 if (do_linking) {
1268 // Link the class to cause the bytecodes to be rewritten and the
1269 // cpcache to be created. Class verification is done according
1270 // to -Xverify setting.
1271 _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
1272 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1347 log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1348
1349 if (SharedArchiveConfigFile) {
1350 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
1351 read_extra_data(SharedArchiveConfigFile, THREAD);
1352 }
1353 log_info(cds)("Reading extra data: done.");
1354
1355 HeapShared::init_subgraph_entry_fields(THREAD);
1356
1357 // Rewrite and link classes
1358 log_info(cds)("Rewriting and linking classes ...");
1359
1360 // Link any classes which got missed. This would happen if we have loaded classes that
1361 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1362 // fails verification, all other interfaces that were not specified in the classlist but
1363 // are implemented by K are not verified.
1364 link_and_cleanup_shared_classes(CATCH);
1365 log_info(cds)("Rewriting and linking classes: done");
1366
1367 #if INCLUDE_CDS_JAVA_HEAP
1368 if (use_full_module_graph()) {
1369 HeapShared::reset_archived_object_states(THREAD);
1370 }
1371 #endif
1372
1373 VM_PopulateDumpSharedSpace op;
1374 MutexLocker ml(THREAD, HeapShared::is_heap_object_archiving_allowed() ?
1375 Heap_lock : NULL); // needed by HeapShared::run_gc()
1376 VMThread::execute(&op);
1377 }
1378 }
1379
1380
1381 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) {
1382 ClassListParser parser(class_list_path);
1383 int class_count = 0;
1384
1385 while (parser.parse_one_line()) {
1386 Klass* klass = parser.load_current_class(THREAD);
1387 if (HAS_PENDING_EXCEPTION) {
1388 if (klass == NULL &&
1389 (PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_ClassNotFoundException())) {
1390 // print a warning only when the pending exception is class not found
1391 log_warning(cds)("Preload Warning: Cannot find %s", parser.current_class_name());
1392 }
1720 }
1721
1722 if (result == MAP_ARCHIVE_SUCCESS) {
1723 SharedBaseAddress = (size_t)mapped_base_address;
1724 LP64_ONLY({
1725 if (Metaspace::using_class_space()) {
1726 // Set up ccs in metaspace.
1727 Metaspace::initialize_class_space(class_space_rs);
1728
1729 // Set up compressed Klass pointer encoding: the encoding range must
1730 // cover both archive and class space.
1731 address cds_base = (address)static_mapinfo->mapped_base();
1732 address ccs_end = (address)class_space_rs.end();
1733 CompressedKlassPointers::initialize(cds_base, ccs_end - cds_base);
1734
1735 // map_heap_regions() compares the current narrow oop and klass encodings
1736 // with the archived ones, so it must be done after all encodings are determined.
1737 static_mapinfo->map_heap_regions();
1738 }
1739 });
1740 log_info(cds)("optimized module handling: %s", MetaspaceShared::use_optimized_module_handling() ? "enabled" : "disabled");
1741 log_info(cds)("full module graph: %s", MetaspaceShared::use_full_module_graph() ? "enabled" : "disabled");
1742 } else {
1743 unmap_archive(static_mapinfo);
1744 unmap_archive(dynamic_mapinfo);
1745 release_reserved_spaces(archive_space_rs, class_space_rs);
1746 }
1747
1748 return result;
1749 }
1750
1751
1752 // This will reserve two address spaces suitable to house Klass structures, one
1753 // for the cds archives (static archive and optionally dynamic archive) and
1754 // optionally one move for ccs.
1755 //
1756 // Since both spaces must fall within the compressed class pointer encoding
1757 // range, they are allocated close to each other.
1758 //
1759 // Space for archives will be reserved first, followed by a potential gap,
1760 // followed by the space for ccs:
1761 //
2044 return true;
2045 }
2046
2047 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) {
2048 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
2049 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
2050 // or so.
2051 _mc_region.print_out_of_space_msg(name, needed_bytes);
2052 _rw_region.print_out_of_space_msg(name, needed_bytes);
2053 _ro_region.print_out_of_space_msg(name, needed_bytes);
2054
2055 vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name),
2056 "Please reduce the number of shared classes.");
2057 }
2058
2059 // This is used to relocate the pointers so that the base archive can be mapped at
2060 // MetaspaceShared::requested_base_address() without runtime relocation.
2061 intx MetaspaceShared::final_delta() {
2062 return intx(MetaspaceShared::requested_base_address()) // We want the base archive to be mapped to here at runtime
2063 - intx(SharedBaseAddress); // .. but the base archive is mapped at here at dump time
2064 }
2065
2066 bool MetaspaceShared::use_full_module_graph() {
2067 bool result = _use_optimized_module_handling && _use_full_module_graph &&
2068 (UseSharedSpaces || DumpSharedSpaces) && HeapShared::is_heap_object_archiving_allowed();
2069 if (result && UseSharedSpaces) {
2070 // Classes used by the archived full module graph are loaded in JVMTI early phase.
2071 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
2072 "CDS should be disabled if early class hooks are enabled");
2073 }
2074 return result;
2075 }
2076
2077 void MetaspaceShared::print_on(outputStream* st) {
2078 if (UseSharedSpaces || DumpSharedSpaces) {
2079 st->print("CDS archive(s) mapped at: ");
2080 address base;
2081 address top;
2082 if (UseSharedSpaces) { // Runtime
2083 base = (address)MetaspaceObj::shared_metaspace_base();
2084 address static_top = (address)_shared_metaspace_static_top;
2085 top = (address)MetaspaceObj::shared_metaspace_top();
2086 st->print("[" PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(static_top), p2i(top));
2087 } else if (DumpSharedSpaces) { // Dump Time
2088 base = (address)_shared_rs.base();
2089 top = (address)_shared_rs.end();
2090 st->print("[" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(top));
2091 }
2092 st->print("size " SIZE_FORMAT ", ", top - base);
2093 st->print("SharedBaseAddress: " PTR_FORMAT ", ArchiveRelocationMode: %d.", SharedBaseAddress, (int)ArchiveRelocationMode);
2094 } else {
|