< prev index next >

src/hotspot/share/cds/dynamicArchive.cpp

Print this page

 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"

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;

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);

 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/classPreloader.hpp"
 33 #include "cds/dynamicArchive.hpp"
 34 #include "cds/metaspaceShared.hpp"
 35 #include "cds/regeneratedClasses.hpp"
 36 #include "classfile/classLoader.hpp"
 37 #include "classfile/classLoaderData.inline.hpp"
 38 #include "classfile/symbolTable.hpp"
 39 #include "classfile/systemDictionaryShared.hpp"
 40 #include "classfile/vmSymbols.hpp"
 41 #include "gc/shared/collectedHeap.hpp"
 42 #include "gc/shared/gcVMOperations.hpp"
 43 #include "gc/shared/gc_globals.hpp"
 44 #include "jvm.h"
 45 #include "logging/log.hpp"
 46 #include "memory/metaspaceClosure.hpp"
 47 #include "memory/resourceArea.hpp"
 48 #include "oops/klass.inline.hpp"
 49 #include "runtime/arguments.hpp"
 50 #include "runtime/os.hpp"
 51 #include "runtime/sharedRuntime.hpp"
 52 #include "runtime/vmThread.hpp"
 53 #include "runtime/vmOperations.hpp"
 54 #include "utilities/align.hpp"

120       log_warning(cds, dynamic)("There is no class to be included in the dynamic archive.");
121       return;
122     }
123 
124     log_info(cds,dynamic)("CDS dynamic dump: clinit = " JLONG_FORMAT "ms)",
125                           ClassLoader::class_init_time_ms());
126 
127     init_header();
128     gather_source_objs();
129     gather_array_klasses();
130     reserve_buffer();
131 
132     log_info(cds, dynamic)("Copying %d klasses and %d symbols",
133                            klasses()->length(), symbols()->length());
134     dump_rw_metadata();
135     dump_ro_metadata();
136     relocate_metaspaceobj_embedded_pointers();
137 
138     verify_estimate_size(_estimated_metaspaceobj_bytes, "MetaspaceObjs");
139 
140     sort_methods();
141 
142     {
143       ArchiveBuilder::OtherROAllocMark mark;
144       ClassPreloader::record_preloaded_classes(false);
145     }
146 
147     log_info(cds)("Make classes shareable");
148     make_klasses_shareable();
149 
150     char* serialized_data;
151     {
152       // Write the symbol table and system dictionaries to the RO space.
153       // Note that these tables still point to the *original* objects, so
154       // they would need to call DynamicArchive::original_to_target() to
155       // get the correct addresses.
156       assert(current_dump_region() == ro_region(), "Must be RO space");
157       SymbolTable::write_to_archive(symbols());
158 
159       ArchiveBuilder::OtherROAllocMark mark;
160       SystemDictionaryShared::write_to_archive(false);
161 
162       DynamicArchive::dump_array_klasses();
163       ClassPreloader::record_initiated_classes(false);
164       TrainingData::dump_training_data();
165 
166       serialized_data = ro_region()->top();
167       WriteClosure wc(ro_region());
168       ArchiveBuilder::serialize_dynamic_archivable_items(&wc);
169     }
170 
171     verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
172 





173     log_info(cds)("Adjust lambda proxy class dictionary");
174     SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
175 
176     log_info(cds)("Adjust method info dictionary");
177     SystemDictionaryShared::adjust_method_info_dictionary();
178 
179     log_info(cds)("Make training data shareable");
180     make_training_data_shareable();
181 
182     relocate_to_requested();
183 
184     write_archive(serialized_data);
185     release_header();
186     DynamicArchive::post_dump();
187 
188     post_dump();
189 
190     assert(_num_dump_regions_used == _total_dump_regions, "must be");
191     verify_universe("After CDS dynamic dump");
192   }
193 
194   virtual void iterate_roots(MetaspaceClosure* it) {
195     FileMapInfo::metaspace_pointers_do(it);
196     SystemDictionaryShared::dumptime_classes_do(it);
197     TrainingData::iterate_roots(it);
198     iterate_primitive_array_klasses(it);
199   }
200 
201   void iterate_primitive_array_klasses(MetaspaceClosure* it) {
202     for (int i = T_BOOLEAN; i <= T_LONG; i++) {
203       assert(is_java_primitive((BasicType)i), "sanity");
204       Klass* k = Universe::typeArrayKlass((BasicType)i);  // this give you "[I", etc
205       assert(MetaspaceShared::is_shared_static((void*)k),
206         "one-dimensional primitive array should be in static archive");
207       ArrayKlass* ak = ArrayKlass::cast(k);
208       while (ak != nullptr && ak->is_shared()) {
209         Klass* next_k = ak->array_klass_or_null();
210         if (next_k != nullptr) {
211           ak = ArrayKlass::cast(next_k);
212         } else {
213           ak = nullptr;
214         }
215       }
216       if (ak != nullptr) {
217         assert(ak->dimension() > 1, "sanity");

234   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
235     _header->set_base_region_crc(i, base_info->region_crc(i));
236   }
237 }
238 
239 void DynamicArchiveBuilder::release_header() {
240   // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
241   // have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
242   // Let's free it so that if class loading happens after we leave the safepoint, nothing
243   // bad will happen.
244   assert(SafepointSynchronize::is_at_safepoint(), "must be");
245   FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
246   assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be");
247   delete mapinfo;
248   assert(!DynamicArchive::is_mapped(), "must be");
249   _header = nullptr;
250 }
251 
252 void DynamicArchiveBuilder::post_dump() {
253   ArchivePtrMarker::reset_map_and_vs();
254   ClassPreloader::dispose();
255   ClassPrelinker::dispose();
256 }
257 
258 void DynamicArchiveBuilder::sort_methods() {
259   InstanceKlass::disable_method_binary_search();
260   for (int i = 0; i < klasses()->length(); i++) {
261     Klass* k = get_buffered_addr(klasses()->at(i));
262     if (k->is_instance_klass()) {
263       sort_methods(InstanceKlass::cast(k));
264     }
265   }
266 }
267 
268 // The address order of the copied Symbols may be different than when the original
269 // klasses were created. Re-sort all the tables. See Method::sort_methods().
270 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
271   assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
272   if (MetaspaceShared::is_in_shared_metaspace(ik)) {
273     // We have reached a supertype that's already in the base archive
274     return;

506     } else {
507       assert(ArchiveClassesAtExit != nullptr, "sanity");
508       log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
509     }
510 #undef __THEMSG
511     CDSConfig::disable_dumping_dynamic_archive();
512   }
513 }
514 
515 void DynamicArchive::dump_at_exit(JavaThread* current, const char* archive_name) {
516   ExceptionMark em(current);
517   ResourceMark rm(current);
518 
519   if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
520     return;
521   }
522 
523   log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
524 
525   JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
526 
527   {
528     // FIXME-HACK - make sure we have at least one class in the dynamic archive
529     TempNewSymbol class_name = SymbolTable::new_symbol("sun/nio/cs/IBM850"); // unusual class; shouldn't be used by our tests cases.
530     SystemDictionary::resolve_or_null(class_name, Handle(), Handle(), THREAD);
531     guarantee(!HAS_PENDING_EXCEPTION, "must have this class");
532   }
533 
534   MetaspaceShared::link_shared_classes(false/*not from jcmd*/, THREAD);
535   if (!HAS_PENDING_EXCEPTION) {
536     // copy shared path table to saved.
537     TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
538     if (!HAS_PENDING_EXCEPTION) {
539       VM_PopulateDynamicDumpSharedSpace op(archive_name);
540       VMThread::execute(&op);
541       return;
542     }
543   }
544 
545   // One of the prepatory steps failed
546   oop ex = current->pending_exception();
547   log_error(cds)("Dynamic dump has failed");
548   log_error(cds)("%s: %s", ex->klass()->external_name(),
549                  java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
550   CLEAR_PENDING_EXCEPTION;
551   CDSConfig::disable_dumping_dynamic_archive();  // Just for good measure
552 }
553 
554 // This is called by "jcmd VM.cds dynamic_dump"
555 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
556   assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp");
557   assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
558   assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
559   MetaspaceShared::link_shared_classes(true/*from jcmd*/, CHECK);
560   // copy shared path table to saved.
561   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
562   VM_PopulateDynamicDumpSharedSpace op(archive_name);
563   VMThread::execute(&op);
564 }
565 
566 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
567   assert(!dynamic_info->is_static(), "must be");
568   // Check if the recorded base archive matches with the current one
569   FileMapInfo* base_info = FileMapInfo::current_info();
570   DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
571 
572   // Check the header crc
573   if (dynamic_header->base_header_crc() != base_info->crc()) {
574     log_warning(cds)("Dynamic archive cannot be used: static archive header checksum verification failed.");
575     return false;
576   }
577 
578   // Check each space's crc
579   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
580     if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
581       log_warning(cds)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
< prev index next >