6 * under the terms of the GNU General Public License version 2 only, as
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 "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapWriter.hpp"
28 #include "cds/archiveUtils.inline.hpp"
29 #include "cds/cds_globals.hpp"
30 #include "cds/cdsConfig.hpp"
31 #include "cds/classPrelinker.hpp"
32 #include "cds/dynamicArchive.hpp"
33 #include "cds/regeneratedClasses.hpp"
34 #include "classfile/classLoader.hpp"
35 #include "classfile/classLoaderData.inline.hpp"
36 #include "classfile/symbolTable.hpp"
37 #include "classfile/systemDictionaryShared.hpp"
38 #include "classfile/vmSymbols.hpp"
39 #include "gc/shared/collectedHeap.hpp"
40 #include "gc/shared/gcVMOperations.hpp"
41 #include "gc/shared/gc_globals.hpp"
42 #include "jvm.h"
43 #include "logging/log.hpp"
44 #include "memory/metaspaceClosure.hpp"
45 #include "memory/resourceArea.hpp"
46 #include "oops/klass.inline.hpp"
47 #include "runtime/arguments.hpp"
48 #include "runtime/os.hpp"
49 #include "runtime/sharedRuntime.hpp"
50 #include "runtime/vmThread.hpp"
51 #include "runtime/vmOperations.hpp"
52 #include "utilities/align.hpp"
95 public:
96 DynamicArchiveBuilder() : ArchiveBuilder() { }
97
98 // Do this before and after the archive dump to see if any corruption
99 // is caused by dynamic dumping.
100 void verify_universe(const char* info) {
101 if (VerifyBeforeExit) {
102 log_info(cds)("Verify %s", info);
103 // Among other things, this ensures that Eden top is correct.
104 Universe::heap()->prepare_for_verify();
105 Universe::verify(info);
106 }
107 }
108
109 void doit() {
110 verify_universe("Before CDS dynamic dump");
111 DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
112
113 // Block concurrent class unloading from changing the _dumptime_table
114 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
115 SystemDictionaryShared::check_excluded_classes();
116
117 if (SystemDictionaryShared::is_dumptime_table_empty()) {
118 log_warning(cds, dynamic)("There is no class to be included in the dynamic archive.");
119 return;
120 }
121
122 log_info(cds,dynamic)("CDS dynamic dump: clinit = " JLONG_FORMAT "ms)",
123 ClassLoader::class_init_time_ms());
124
125 init_header();
126 gather_source_objs();
127 gather_array_klasses();
128 reserve_buffer();
129
130 log_info(cds, dynamic)("Copying %d klasses and %d symbols",
131 klasses()->length(), symbols()->length());
132 dump_rw_metadata();
133 dump_ro_metadata();
134 relocate_metaspaceobj_embedded_pointers();
135
136 verify_estimate_size(_estimated_metaspaceobj_bytes, "MetaspaceObjs");
137
138 char* serialized_data;
139 {
140 // Write the symbol table and system dictionaries to the RO space.
141 // Note that these tables still point to the *original* objects, so
142 // they would need to call DynamicArchive::original_to_target() to
143 // get the correct addresses.
144 assert(current_dump_region() == ro_region(), "Must be RO space");
145 SymbolTable::write_to_archive(symbols());
146
147 ArchiveBuilder::OtherROAllocMark mark;
148 SystemDictionaryShared::write_to_archive(false);
149 DynamicArchive::dump_array_klasses();
150
151 serialized_data = ro_region()->top();
152 WriteClosure wc(ro_region());
153 ArchiveBuilder::serialize_dynamic_archivable_items(&wc);
154 }
155
156 verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
157
158 sort_methods();
159
160 log_info(cds)("Make classes shareable");
161 make_klasses_shareable();
162
163 log_info(cds)("Adjust lambda proxy class dictionary");
164 SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
165
166 relocate_to_requested();
167
168 write_archive(serialized_data);
169 release_header();
170 DynamicArchive::post_dump();
171
172 post_dump();
173
174 assert(_num_dump_regions_used == _total_dump_regions, "must be");
175 verify_universe("After CDS dynamic dump");
176 }
177
178 virtual void iterate_roots(MetaspaceClosure* it) {
179 FileMapInfo::metaspace_pointers_do(it);
180 SystemDictionaryShared::dumptime_classes_do(it);
181 iterate_primitive_array_klasses(it);
182 }
183
184 void iterate_primitive_array_klasses(MetaspaceClosure* it) {
185 for (int i = T_BOOLEAN; i <= T_LONG; i++) {
186 assert(is_java_primitive((BasicType)i), "sanity");
187 Klass* k = Universe::typeArrayKlass((BasicType)i); // this give you "[I", etc
188 assert(MetaspaceShared::is_shared_static((void*)k),
189 "one-dimensional primitive array should be in static archive");
190 ArrayKlass* ak = ArrayKlass::cast(k);
191 while (ak != nullptr && ak->is_shared()) {
192 Klass* next_k = ak->array_klass_or_null();
193 if (next_k != nullptr) {
194 ak = ArrayKlass::cast(next_k);
195 } else {
196 ak = nullptr;
197 }
198 }
199 if (ak != nullptr) {
200 assert(ak->dimension() > 1, "sanity");
217 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
218 _header->set_base_region_crc(i, base_info->region_crc(i));
219 }
220 }
221
222 void DynamicArchiveBuilder::release_header() {
223 // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
224 // have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
225 // Let's free it so that if class loading happens after we leave the safepoint, nothing
226 // bad will happen.
227 assert(SafepointSynchronize::is_at_safepoint(), "must be");
228 FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
229 assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be");
230 delete mapinfo;
231 assert(!DynamicArchive::is_mapped(), "must be");
232 _header = nullptr;
233 }
234
235 void DynamicArchiveBuilder::post_dump() {
236 ArchivePtrMarker::reset_map_and_vs();
237 ClassPrelinker::dispose();
238 }
239
240 void DynamicArchiveBuilder::sort_methods() {
241 InstanceKlass::disable_method_binary_search();
242 for (int i = 0; i < klasses()->length(); i++) {
243 Klass* k = get_buffered_addr(klasses()->at(i));
244 if (k->is_instance_klass()) {
245 sort_methods(InstanceKlass::cast(k));
246 }
247 }
248 }
249
250 // The address order of the copied Symbols may be different than when the original
251 // klasses were created. Re-sort all the tables. See Method::sort_methods().
252 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
253 assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
254 if (MetaspaceShared::is_in_shared_metaspace(ik)) {
255 // We have reached a supertype that's already in the base archive
256 return;
257 }
488 } else {
489 assert(ArchiveClassesAtExit != nullptr, "sanity");
490 log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
491 }
492 #undef __THEMSG
493 CDSConfig::disable_dumping_dynamic_archive();
494 }
495 }
496
497 void DynamicArchive::dump_at_exit(JavaThread* current, const char* archive_name) {
498 ExceptionMark em(current);
499 ResourceMark rm(current);
500
501 if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
502 return;
503 }
504
505 log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
506
507 JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
508 MetaspaceShared::link_shared_classes(false/*not from jcmd*/, THREAD);
509 if (!HAS_PENDING_EXCEPTION) {
510 // copy shared path table to saved.
511 if (!HAS_PENDING_EXCEPTION) {
512 VM_PopulateDynamicDumpSharedSpace op(archive_name);
513 VMThread::execute(&op);
514 return;
515 }
516 }
517
518 // One of the prepatory steps failed
519 oop ex = current->pending_exception();
520 log_error(cds)("Dynamic dump has failed");
521 log_error(cds)("%s: %s", ex->klass()->external_name(),
522 java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
523 CLEAR_PENDING_EXCEPTION;
524 CDSConfig::disable_dumping_dynamic_archive(); // Just for good measure
525 }
526
527 // This is called by "jcmd VM.cds dynamic_dump"
528 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
529 assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp");
530 assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
531 assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
532 MetaspaceShared::link_shared_classes(true/*from jcmd*/, CHECK);
533 // copy shared path table to saved.
534 VM_PopulateDynamicDumpSharedSpace op(archive_name);
535 VMThread::execute(&op);
536 }
537
538 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
539 assert(!dynamic_info->is_static(), "must be");
540 // Check if the recorded base archive matches with the current one
541 FileMapInfo* base_info = FileMapInfo::current_info();
542 DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
543
544 // Check the header crc
545 if (dynamic_header->base_header_crc() != base_info->crc()) {
546 log_warning(cds)("Dynamic archive cannot be used: static archive header checksum verification failed.");
547 return false;
548 }
549
550 // Check each space's crc
551 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
552 if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
553 log_warning(cds)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
|
6 * under the terms of the GNU General Public License version 2 only, as
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 "cds/aotClassLinker.hpp"
27 #include "cds/archiveBuilder.hpp"
28 #include "cds/archiveHeapWriter.hpp"
29 #include "cds/archiveUtils.inline.hpp"
30 #include "cds/cds_globals.hpp"
31 #include "cds/cdsConfig.hpp"
32 #include "cds/dynamicArchive.hpp"
33 #include "cds/metaspaceShared.hpp"
34 #include "cds/regeneratedClasses.hpp"
35 #include "classfile/classLoader.hpp"
36 #include "classfile/classLoaderData.inline.hpp"
37 #include "classfile/symbolTable.hpp"
38 #include "classfile/systemDictionaryShared.hpp"
39 #include "classfile/vmSymbols.hpp"
40 #include "gc/shared/collectedHeap.hpp"
41 #include "gc/shared/gcVMOperations.hpp"
42 #include "gc/shared/gc_globals.hpp"
43 #include "jvm.h"
44 #include "logging/log.hpp"
45 #include "memory/metaspaceClosure.hpp"
46 #include "memory/resourceArea.hpp"
47 #include "oops/klass.inline.hpp"
48 #include "runtime/arguments.hpp"
49 #include "runtime/os.hpp"
50 #include "runtime/sharedRuntime.hpp"
51 #include "runtime/vmThread.hpp"
52 #include "runtime/vmOperations.hpp"
53 #include "utilities/align.hpp"
96 public:
97 DynamicArchiveBuilder() : ArchiveBuilder() { }
98
99 // Do this before and after the archive dump to see if any corruption
100 // is caused by dynamic dumping.
101 void verify_universe(const char* info) {
102 if (VerifyBeforeExit) {
103 log_info(cds)("Verify %s", info);
104 // Among other things, this ensures that Eden top is correct.
105 Universe::heap()->prepare_for_verify();
106 Universe::verify(info);
107 }
108 }
109
110 void doit() {
111 verify_universe("Before CDS dynamic dump");
112 DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
113
114 // Block concurrent class unloading from changing the _dumptime_table
115 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
116 SystemDictionaryShared::find_all_archivable_classes();
117
118 if (SystemDictionaryShared::is_dumptime_table_empty()) {
119 log_warning(cds, dynamic)("There is no class to be included in the dynamic archive.");
120 return;
121 }
122
123 log_info(cds,dynamic)("CDS dynamic dump: clinit = " JLONG_FORMAT "ms)",
124 ClassLoader::class_init_time_ms());
125
126 init_header();
127 gather_source_objs();
128 gather_array_klasses();
129 reserve_buffer();
130
131 log_info(cds, dynamic)("Copying %d klasses and %d symbols",
132 klasses()->length(), symbols()->length());
133 dump_rw_metadata();
134 dump_ro_metadata();
135 relocate_metaspaceobj_embedded_pointers();
136
137 verify_estimate_size(_estimated_metaspaceobj_bytes, "MetaspaceObjs");
138
139 sort_methods();
140
141 log_info(cds)("Make classes shareable");
142 make_klasses_shareable();
143
144 char* serialized_data;
145 {
146 // Write the symbol table and system dictionaries to the RO space.
147 // Note that these tables still point to the *original* objects, so
148 // they would need to call DynamicArchive::original_to_target() to
149 // get the correct addresses.
150 assert(current_dump_region() == ro_region(), "Must be RO space");
151 SymbolTable::write_to_archive(symbols());
152
153 ArchiveBuilder::OtherROAllocMark mark;
154 SystemDictionaryShared::write_to_archive(false);
155
156 DynamicArchive::dump_array_klasses();
157 AOTClassLinker::write_to_archive();
158 TrainingData::dump_training_data();
159
160 serialized_data = ro_region()->top();
161 WriteClosure wc(ro_region());
162 ArchiveBuilder::serialize_dynamic_archivable_items(&wc);
163 }
164
165 verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
166
167 log_info(cds)("Adjust lambda proxy class dictionary");
168 SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
169
170 log_info(cds)("Make training data shareable");
171 make_training_data_shareable();
172
173 relocate_to_requested();
174
175 write_archive(serialized_data);
176 release_header();
177 DynamicArchive::post_dump();
178
179 post_dump();
180
181 assert(_num_dump_regions_used == _total_dump_regions, "must be");
182 verify_universe("After CDS dynamic dump");
183 }
184
185 virtual void iterate_roots(MetaspaceClosure* it) {
186 FileMapInfo::metaspace_pointers_do(it);
187 SystemDictionaryShared::dumptime_classes_do(it);
188 TrainingData::iterate_roots(it);
189 iterate_primitive_array_klasses(it);
190 }
191
192 void iterate_primitive_array_klasses(MetaspaceClosure* it) {
193 for (int i = T_BOOLEAN; i <= T_LONG; i++) {
194 assert(is_java_primitive((BasicType)i), "sanity");
195 Klass* k = Universe::typeArrayKlass((BasicType)i); // this give you "[I", etc
196 assert(MetaspaceShared::is_shared_static((void*)k),
197 "one-dimensional primitive array should be in static archive");
198 ArrayKlass* ak = ArrayKlass::cast(k);
199 while (ak != nullptr && ak->is_shared()) {
200 Klass* next_k = ak->array_klass_or_null();
201 if (next_k != nullptr) {
202 ak = ArrayKlass::cast(next_k);
203 } else {
204 ak = nullptr;
205 }
206 }
207 if (ak != nullptr) {
208 assert(ak->dimension() > 1, "sanity");
225 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
226 _header->set_base_region_crc(i, base_info->region_crc(i));
227 }
228 }
229
230 void DynamicArchiveBuilder::release_header() {
231 // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
232 // have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
233 // Let's free it so that if class loading happens after we leave the safepoint, nothing
234 // bad will happen.
235 assert(SafepointSynchronize::is_at_safepoint(), "must be");
236 FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
237 assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be");
238 delete mapinfo;
239 assert(!DynamicArchive::is_mapped(), "must be");
240 _header = nullptr;
241 }
242
243 void DynamicArchiveBuilder::post_dump() {
244 ArchivePtrMarker::reset_map_and_vs();
245 AOTClassLinker::dispose();
246 }
247
248 void DynamicArchiveBuilder::sort_methods() {
249 InstanceKlass::disable_method_binary_search();
250 for (int i = 0; i < klasses()->length(); i++) {
251 Klass* k = get_buffered_addr(klasses()->at(i));
252 if (k->is_instance_klass()) {
253 sort_methods(InstanceKlass::cast(k));
254 }
255 }
256 }
257
258 // The address order of the copied Symbols may be different than when the original
259 // klasses were created. Re-sort all the tables. See Method::sort_methods().
260 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
261 assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
262 if (MetaspaceShared::is_in_shared_metaspace(ik)) {
263 // We have reached a supertype that's already in the base archive
264 return;
265 }
496 } else {
497 assert(ArchiveClassesAtExit != nullptr, "sanity");
498 log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
499 }
500 #undef __THEMSG
501 CDSConfig::disable_dumping_dynamic_archive();
502 }
503 }
504
505 void DynamicArchive::dump_at_exit(JavaThread* current, const char* archive_name) {
506 ExceptionMark em(current);
507 ResourceMark rm(current);
508
509 if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
510 return;
511 }
512
513 log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
514
515 JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
516
517 {
518 // FIXME-HACK - make sure we have at least one class in the dynamic archive
519 TempNewSymbol class_name = SymbolTable::new_symbol("sun/nio/cs/IBM850"); // unusual class; shouldn't be used by our tests cases.
520 SystemDictionary::resolve_or_null(class_name, Handle(), Handle(), THREAD);
521 guarantee(!HAS_PENDING_EXCEPTION, "must have this class");
522 }
523
524 MetaspaceShared::link_shared_classes(false/*not from jcmd*/, THREAD);
525 if (!HAS_PENDING_EXCEPTION) {
526 // copy shared path table to saved.
527 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
528 if (!HAS_PENDING_EXCEPTION) {
529 VM_PopulateDynamicDumpSharedSpace op(archive_name);
530 VMThread::execute(&op);
531 return;
532 }
533 }
534
535 // One of the prepatory steps failed
536 oop ex = current->pending_exception();
537 log_error(cds)("Dynamic dump has failed");
538 log_error(cds)("%s: %s", ex->klass()->external_name(),
539 java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
540 CLEAR_PENDING_EXCEPTION;
541 CDSConfig::disable_dumping_dynamic_archive(); // Just for good measure
542 }
543
544 // This is called by "jcmd VM.cds dynamic_dump"
545 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
546 assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp");
547 assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
548 assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
549 MetaspaceShared::link_shared_classes(true/*from jcmd*/, CHECK);
550 // copy shared path table to saved.
551 TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
552 VM_PopulateDynamicDumpSharedSpace op(archive_name);
553 VMThread::execute(&op);
554 }
555
556 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
557 assert(!dynamic_info->is_static(), "must be");
558 // Check if the recorded base archive matches with the current one
559 FileMapInfo* base_info = FileMapInfo::current_info();
560 DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
561
562 // Check the header crc
563 if (dynamic_header->base_header_crc() != base_info->crc()) {
564 log_warning(cds)("Dynamic archive cannot be used: static archive header checksum verification failed.");
565 return false;
566 }
567
568 // Check each space's crc
569 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
570 if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
571 log_warning(cds)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
|