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/javaClasses.inline.hpp" 27 #include "classfile/stringTable.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "classfile/systemDictionaryShared.hpp" 30 #include "classfile/vmSymbols.hpp" 31 #include "gc/shared/gcLocker.hpp" 32 #include "logging/log.hpp" 33 #include "logging/logMessage.hpp" 34 #include "logging/logStream.hpp" 35 #include "memory/archiveUtils.hpp" 36 #include "memory/filemap.hpp" 37 #include "memory/heapShared.inline.hpp" 38 #include "memory/iterator.inline.hpp" 39 #include "memory/metadataFactory.hpp" 40 #include "memory/metaspaceClosure.hpp" 41 #include "memory/metaspaceShared.hpp" 42 #include "memory/resourceArea.hpp" 43 #include "memory/universe.hpp" 44 #include "oops/compressedOops.inline.hpp" 45 #include "oops/fieldStreams.inline.hpp" 46 #include "oops/oop.inline.hpp" 47 #include "runtime/fieldDescriptor.inline.hpp" 48 #include "runtime/safepointVerifiers.hpp" 49 #include "utilities/bitMap.inline.hpp" 50 #if INCLUDE_G1GC 51 #include "gc/g1/g1CollectedHeap.hpp" 52 #endif 53 54 #if INCLUDE_CDS_JAVA_HEAP 55 56 bool HeapShared::_closed_archive_heap_region_mapped = false; 57 bool HeapShared::_open_archive_heap_region_mapped = false; 58 bool HeapShared::_archive_heap_region_fixed = false; 59 60 address HeapShared::_narrow_oop_base; 61 int HeapShared::_narrow_oop_shift; 62 63 // 64 // If you add new entries to the following tables, you should know what you're doing! 65 // 66 67 // Entry fields for shareable subgraphs archived in the closed archive heap 68 // region. Warning: Objects in the subgraphs should not have reference fields 69 // assigned at runtime. 70 static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = { 71 {"java/lang/Integer$IntegerCache", "archivedCache"}, 72 {"java/lang/Long$LongCache", "archivedCache"}, 73 {"java/lang/Byte$ByteCache", "archivedCache"}, 74 {"java/lang/Short$ShortCache", "archivedCache"}, 75 {"java/lang/Character$CharacterCache", "archivedCache"}, 76 {"java/util/jar/Attributes$Name", "KNOWN_NAMES"}, 77 {"sun/util/locale/BaseLocale", "constantBaseLocales"}, 78 }; 79 // Entry fields for subgraphs archived in the open archive heap region. 80 static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = { 81 {"jdk/internal/module/ArchivedModuleGraph", "archivedModuleGraph"}, 82 {"java/util/ImmutableCollections", "archivedObjects"}, 83 {"java/lang/module/Configuration", "EMPTY_CONFIGURATION"}, 84 {"jdk/internal/math/FDBigInteger", "archivedCaches"}, 85 }; 86 87 const static int num_closed_archive_subgraph_entry_fields = 88 sizeof(closed_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo); 89 const static int num_open_archive_subgraph_entry_fields = 90 sizeof(open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo); 91 92 //////////////////////////////////////////////////////////////// 93 // 94 // Java heap object archiving support 95 // 96 //////////////////////////////////////////////////////////////// 97 void HeapShared::fixup_mapped_heap_regions() { 98 FileMapInfo *mapinfo = FileMapInfo::current_info(); 99 mapinfo->fixup_mapped_heap_regions(); 100 set_archive_heap_region_fixed(); 101 SystemDictionaryShared::update_archived_mirror_native_pointers(); 102 } 103 104 unsigned HeapShared::oop_hash(oop const& p) { 105 assert(!p->mark().has_bias_pattern(), 106 "this object should never have been locked"); // so identity_hash won't safepoin 107 unsigned hash = (unsigned)p->identity_hash(); 108 return hash; 109 } 110 111 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = NULL; 112 oop HeapShared::find_archived_heap_object(oop obj) { 113 assert(DumpSharedSpaces, "dump-time only"); 114 ArchivedObjectCache* cache = archived_object_cache(); 115 oop* p = cache->get(obj); 116 if (p != NULL) { 117 return *p; 118 } else { 119 return NULL; 120 } 121 } 122 123 oop HeapShared::archive_heap_object(oop obj, Thread* THREAD) { 124 assert(DumpSharedSpaces, "dump-time only"); 125 126 oop ao = find_archived_heap_object(obj); 127 if (ao != NULL) { 128 // already archived 129 return ao; 130 } 216 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops), 217 BOOL_TO_STR(UseCompressedClassPointers)); 218 return; 219 } 220 221 G1HeapVerifier::verify_ready_for_archiving(); 222 223 { 224 NoSafepointVerifier nsv; 225 226 // Cache for recording where the archived objects are copied to 227 create_archived_object_cache(); 228 229 log_info(cds)("Dumping objects to closed archive heap region ..."); 230 NOT_PRODUCT(StringTable::verify()); 231 copy_closed_archive_heap_objects(closed); 232 233 log_info(cds)("Dumping objects to open archive heap region ..."); 234 copy_open_archive_heap_objects(open); 235 236 destroy_archived_object_cache(); 237 } 238 239 G1HeapVerifier::verify_archive_regions(); 240 } 241 242 void HeapShared::copy_closed_archive_heap_objects( 243 GrowableArray<MemRegion> * closed_archive) { 244 assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects"); 245 246 Thread* THREAD = Thread::current(); 247 G1CollectedHeap::heap()->begin_archive_alloc_range(); 248 249 // Archive interned string objects 250 StringTable::write_to_archive(); 251 252 archive_object_subgraphs(closed_archive_subgraph_entry_fields, 253 num_closed_archive_subgraph_entry_fields, 254 true /* is_closed_archive */, THREAD); 255 448 449 _run_time_subgraph_info_table.reset(); 450 451 CompactHashtableWriter writer(d_table->_count, &stats); 452 CopyKlassSubGraphInfoToArchive copy(&writer); 453 d_table->iterate(©); 454 455 writer.dump(&_run_time_subgraph_info_table, "subgraphs"); 456 } 457 458 void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) { 459 _run_time_subgraph_info_table.serialize_header(soc); 460 } 461 462 void HeapShared::initialize_from_archived_subgraph(Klass* k) { 463 if (!open_archive_heap_region_mapped()) { 464 return; // nothing to do 465 } 466 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 467 468 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(k); 469 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0); 470 471 // Initialize from archived data. Currently this is done only 472 // during VM initialization time. No lock is needed. 473 if (record != NULL) { 474 Thread* THREAD = Thread::current(); 475 476 int i; 477 // Load/link/initialize the klasses of the objects in the subgraph. 478 // NULL class loader is used. 479 Array<Klass*>* klasses = record->subgraph_object_klasses(); 480 if (klasses != NULL) { 481 for (i = 0; i < klasses->length(); i++) { 482 Klass* obj_k = klasses->at(i); 483 Klass* resolved_k = SystemDictionary::resolve_or_null( 484 (obj_k)->name(), THREAD); 485 if (resolved_k != obj_k) { 486 assert(!SystemDictionary::is_well_known_klass(resolved_k), 487 "shared well-known classes must not be replaced by JVMTI ClassFileLoadHook"); 606 } 607 }; 608 609 void HeapShared::check_closed_archive_heap_region_object(InstanceKlass* k, 610 Thread* THREAD) { 611 // Check fields in the object 612 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 613 if (!fs.access_flags().is_static()) { 614 BasicType ft = fs.field_descriptor().field_type(); 615 if (!fs.access_flags().is_final() && is_reference_type(ft)) { 616 ResourceMark rm(THREAD); 617 log_warning(cds, heap)( 618 "Please check reference field in %s instance in closed archive heap region: %s %s", 619 k->external_name(), (fs.name())->as_C_string(), 620 (fs.signature())->as_C_string()); 621 } 622 } 623 } 624 } 625 626 // (1) If orig_obj has not been archived yet, archive it. 627 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called), 628 // trace all objects that are reachable from it, and make sure these objects are archived. 629 // (3) Record the klasses of all orig_obj and all reachable objects. 630 oop HeapShared::archive_reachable_objects_from(int level, 631 KlassSubGraphInfo* subgraph_info, 632 oop orig_obj, 633 bool is_closed_archive, 634 TRAPS) { 635 assert(orig_obj != NULL, "must be"); 636 assert(!is_archived_object(orig_obj), "sanity"); 637 638 if (!JavaClasses::is_supported_for_archiving(orig_obj)) { 639 // This object has injected fields that cannot be supported easily, so we disallow them for now. 640 // If you get an error here, you probably made a change in the JDK library that has added 641 // these objects that are referenced (directly or indirectly) by static fields. 642 ResourceMark rm; 643 log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name()); 644 vm_exit(1); 645 } 673 ++_num_new_archived_objs; 674 archived_obj = archive_heap_object(orig_obj, THREAD); 675 if (archived_obj == NULL) { 676 // Skip archiving the sub-graph referenced from the current entry field. 677 ResourceMark rm; 678 log_error(cds, heap)( 679 "Cannot archive the sub-graph referenced from %s object (" 680 PTR_FORMAT ") size %d, skipped.", 681 orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize); 682 if (level == 1) { 683 // Don't archive a subgraph root that's too big. For archives static fields, that's OK 684 // as the Java code will take care of initializing this field dynamically. 685 return NULL; 686 } else { 687 // We don't know how to handle an object that has been archived, but some of its reachable 688 // objects cannot be archived. Bail out for now. We might need to fix this in the future if 689 // we have a real use case. 690 vm_exit(1); 691 } 692 } 693 } 694 695 assert(archived_obj != NULL, "must be"); 696 Klass *orig_k = orig_obj->klass(); 697 Klass *relocated_k = archived_obj->klass(); 698 subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 699 700 WalkOopAndArchiveClosure walker(level, is_closed_archive, record_klasses_only, 701 subgraph_info, orig_obj, archived_obj, THREAD); 702 orig_obj->oop_iterate(&walker); 703 if (is_closed_archive && orig_k->is_instance_klass()) { 704 check_closed_archive_heap_region_object(InstanceKlass::cast(orig_k), THREAD); 705 } 706 return archived_obj; 707 } 708 709 // 710 // Start from the given static field in a java mirror and archive the 711 // complete sub-graph of java heap objects that are reached directly 712 // or indirectly from the starting object by following references. 969 // [1] Dump the subgraph of each archived field 970 // [2] Create a list of all the class of the objects that can be reached 971 // by any of these static fields. 972 // At runtime, these classes are initialized before X's archived fields 973 // are restored by HeapShared::initialize_from_archived_subgraph(). 974 int i; 975 for (i = 0; i < num; ) { 976 ArchivableStaticFieldInfo* info = &fields[i]; 977 const char* klass_name = info->klass_name; 978 start_recording_subgraph(info->klass, klass_name); 979 980 // If you have specified consecutive fields of the same klass in 981 // fields[], these will be archived in the same 982 // {start_recording_subgraph ... done_recording_subgraph} pass to 983 // save time. 984 for (; i < num; i++) { 985 ArchivableStaticFieldInfo* f = &fields[i]; 986 if (f->klass_name != klass_name) { 987 break; 988 } 989 archive_reachable_objects_from_static_field(f->klass, f->klass_name, 990 f->offset, f->field_name, 991 is_closed_archive, CHECK); 992 } 993 done_recording_subgraph(info->klass, klass_name); 994 } 995 996 log_info(cds, heap)("Archived subgraph records in %s archive heap region = %d", 997 is_closed_archive ? "closed" : "open", 998 _num_total_subgraph_recordings); 999 log_info(cds, heap)(" Walked %d objects", _num_total_walked_objs); 1000 log_info(cds, heap)(" Archived %d objects", _num_total_archived_objs); 1001 log_info(cds, heap)(" Recorded %d klasses", _num_total_recorded_klasses); 1002 1003 #ifndef PRODUCT 1004 for (int i = 0; i < num; i++) { 1005 ArchivableStaticFieldInfo* f = &fields[i]; 1006 verify_subgraph_from_static_field(f->klass, f->offset); 1007 } 1008 log_info(cds, heap)(" Verified %d references", _num_total_verifications); 1009 #endif 1010 } 1011 | 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/javaClasses.inline.hpp" 27 #include "classfile/moduleEntry.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "classfile/systemDictionaryShared.hpp" 32 #include "classfile/vmSymbols.hpp" 33 #include "gc/shared/gcLocker.hpp" 34 #include "logging/log.hpp" 35 #include "logging/logMessage.hpp" 36 #include "logging/logStream.hpp" 37 #include "memory/archiveUtils.hpp" 38 #include "memory/filemap.hpp" 39 #include "memory/heapShared.inline.hpp" 40 #include "memory/iterator.inline.hpp" 41 #include "memory/metadataFactory.hpp" 42 #include "memory/metaspaceClosure.hpp" 43 #include "memory/metaspaceShared.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/compressedOops.inline.hpp" 47 #include "oops/fieldStreams.inline.hpp" 48 #include "oops/oop.inline.hpp" 49 #include "runtime/fieldDescriptor.inline.hpp" 50 #include "runtime/javaCalls.hpp" 51 #include "runtime/safepointVerifiers.hpp" 52 #include "utilities/bitMap.inline.hpp" 53 #if INCLUDE_G1GC 54 #include "gc/g1/g1CollectedHeap.hpp" 55 #endif 56 57 #if INCLUDE_CDS_JAVA_HEAP 58 59 bool HeapShared::_closed_archive_heap_region_mapped = false; 60 bool HeapShared::_open_archive_heap_region_mapped = false; 61 bool HeapShared::_archive_heap_region_fixed = false; 62 63 address HeapShared::_narrow_oop_base; 64 int HeapShared::_narrow_oop_shift; 65 66 // 67 // If you add new entries to the following tables, you should know what you're doing! 68 // 69 70 // Entry fields for shareable subgraphs archived in the closed archive heap 71 // region. Warning: Objects in the subgraphs should not have reference fields 72 // assigned at runtime. 73 static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = { 74 {"java/lang/Integer$IntegerCache", 0, "archivedCache"}, 75 {"java/lang/Long$LongCache", 0, "archivedCache"}, 76 {"java/lang/Byte$ByteCache", 0, "archivedCache"}, 77 {"java/lang/Short$ShortCache", 0, "archivedCache"}, 78 {"java/lang/Character$CharacterCache", 0, "archivedCache"}, 79 {"java/util/jar/Attributes$Name", 0, "KNOWN_NAMES"}, 80 {"sun/util/locale/BaseLocale", 0, "constantBaseLocales"}, 81 }; 82 // Entry fields for subgraphs archived in the open archive heap region. 83 static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = { 84 {"jdk/internal/loader/BuiltinClassLoader$ArchivedData", 1, "packageToModule"}, 85 {"jdk/internal/loader/BootLoader$ArchivedData", 1, "servicesCatalog"}, 86 {"jdk/internal/loader/ClassLoaders$ArchivedData", 1, "singleton"}, 87 {"jdk/internal/module/ModuleBootstrap$ArchivedBootLayer", 1, "archivedBootLayer"}, 88 {"jdk/internal/module/ArchivedModuleGraph", 0, "archivedModuleGraph"}, 89 {"java/util/ImmutableCollections", 0, "archivedObjects"}, 90 {"java/lang/Module$ArchivedData", 1, "singleton"}, 91 {"java/lang/module/Configuration", 0, "EMPTY_CONFIGURATION"}, 92 {"jdk/internal/math/FDBigInteger", 0, "archivedCaches"}, 93 }; 94 95 const static int num_closed_archive_subgraph_entry_fields = 96 sizeof(closed_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo); 97 const static int num_open_archive_subgraph_entry_fields = 98 sizeof(open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo); 99 100 //////////////////////////////////////////////////////////////// 101 // 102 // Java heap object archiving support 103 // 104 //////////////////////////////////////////////////////////////// 105 void HeapShared::fixup_mapped_heap_regions() { 106 FileMapInfo *mapinfo = FileMapInfo::current_info(); 107 mapinfo->fixup_mapped_heap_regions(); 108 set_archive_heap_region_fixed(); 109 SystemDictionaryShared::update_archived_mirror_native_pointers(); 110 } 111 112 unsigned HeapShared::oop_hash(oop const& p) { 113 assert(!p->mark().has_bias_pattern(), 114 "this object should never have been locked"); // so identity_hash won't safepoin 115 unsigned hash = (unsigned)p->identity_hash(); 116 return hash; 117 } 118 119 static void reset_states(oop obj, TRAPS) { 120 Handle h_obj(THREAD, obj); 121 InstanceKlass* klass = InstanceKlass::cast(obj->klass()); 122 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates"); 123 Symbol* method_sig = vmSymbols::void_method_signature(); 124 125 while (klass != NULL) { 126 Method* method = klass->find_method(method_name, method_sig); 127 if (method != NULL) { 128 assert(method->is_private(), "must be"); 129 if (log_is_enabled(Debug, cds)) { 130 ResourceMark rm(THREAD); 131 log_debug(cds)(" calling %s", method->name_and_sig_as_C_string()); 132 } 133 JavaValue result(T_VOID); 134 JavaCalls::call_special(&result, h_obj, klass, 135 method_name, method_sig, CHECK); 136 } 137 klass = klass->java_super(); 138 } 139 } 140 141 void HeapShared::reset_archived_object_states(TRAPS) { 142 log_debug(cds)("Resetting platform loader"); 143 reset_states(SystemDictionary::java_platform_loader(), THREAD); 144 log_debug(cds)("Resetting system loader"); 145 reset_states(SystemDictionary::java_system_loader(), THREAD); 146 } 147 148 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = NULL; 149 oop HeapShared::find_archived_heap_object(oop obj) { 150 assert(DumpSharedSpaces, "dump-time only"); 151 ArchivedObjectCache* cache = archived_object_cache(); 152 oop* p = cache->get(obj); 153 if (p != NULL) { 154 return *p; 155 } else { 156 return NULL; 157 } 158 } 159 160 oop HeapShared::archive_heap_object(oop obj, Thread* THREAD) { 161 assert(DumpSharedSpaces, "dump-time only"); 162 163 oop ao = find_archived_heap_object(obj); 164 if (ao != NULL) { 165 // already archived 166 return ao; 167 } 253 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops), 254 BOOL_TO_STR(UseCompressedClassPointers)); 255 return; 256 } 257 258 G1HeapVerifier::verify_ready_for_archiving(); 259 260 { 261 NoSafepointVerifier nsv; 262 263 // Cache for recording where the archived objects are copied to 264 create_archived_object_cache(); 265 266 log_info(cds)("Dumping objects to closed archive heap region ..."); 267 NOT_PRODUCT(StringTable::verify()); 268 copy_closed_archive_heap_objects(closed); 269 270 log_info(cds)("Dumping objects to open archive heap region ..."); 271 copy_open_archive_heap_objects(open); 272 273 ClassLoaderData::init_archived_oops(); 274 275 destroy_archived_object_cache(); 276 } 277 278 G1HeapVerifier::verify_archive_regions(); 279 } 280 281 void HeapShared::copy_closed_archive_heap_objects( 282 GrowableArray<MemRegion> * closed_archive) { 283 assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects"); 284 285 Thread* THREAD = Thread::current(); 286 G1CollectedHeap::heap()->begin_archive_alloc_range(); 287 288 // Archive interned string objects 289 StringTable::write_to_archive(); 290 291 archive_object_subgraphs(closed_archive_subgraph_entry_fields, 292 num_closed_archive_subgraph_entry_fields, 293 true /* is_closed_archive */, THREAD); 294 487 488 _run_time_subgraph_info_table.reset(); 489 490 CompactHashtableWriter writer(d_table->_count, &stats); 491 CopyKlassSubGraphInfoToArchive copy(&writer); 492 d_table->iterate(©); 493 494 writer.dump(&_run_time_subgraph_info_table, "subgraphs"); 495 } 496 497 void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) { 498 _run_time_subgraph_info_table.serialize_header(soc); 499 } 500 501 void HeapShared::initialize_from_archived_subgraph(Klass* k) { 502 if (!open_archive_heap_region_mapped()) { 503 return; // nothing to do 504 } 505 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 506 507 if (!MetaspaceShared::use_full_module_graph()) { 508 for (int i = 0; i < num_open_archive_subgraph_entry_fields; i++) { 509 const ArchivableStaticFieldInfo* info = &open_archive_subgraph_entry_fields[i]; 510 if (info->full_module_graph_only && k->name()->equals(info->klass_name)) { 511 return; 512 } 513 } 514 } 515 516 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(k); 517 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0); 518 519 // Initialize from archived data. Currently this is done only 520 // during VM initialization time. No lock is needed. 521 if (record != NULL) { 522 Thread* THREAD = Thread::current(); 523 524 int i; 525 // Load/link/initialize the klasses of the objects in the subgraph. 526 // NULL class loader is used. 527 Array<Klass*>* klasses = record->subgraph_object_klasses(); 528 if (klasses != NULL) { 529 for (i = 0; i < klasses->length(); i++) { 530 Klass* obj_k = klasses->at(i); 531 Klass* resolved_k = SystemDictionary::resolve_or_null( 532 (obj_k)->name(), THREAD); 533 if (resolved_k != obj_k) { 534 assert(!SystemDictionary::is_well_known_klass(resolved_k), 535 "shared well-known classes must not be replaced by JVMTI ClassFileLoadHook"); 654 } 655 }; 656 657 void HeapShared::check_closed_archive_heap_region_object(InstanceKlass* k, 658 Thread* THREAD) { 659 // Check fields in the object 660 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 661 if (!fs.access_flags().is_static()) { 662 BasicType ft = fs.field_descriptor().field_type(); 663 if (!fs.access_flags().is_final() && is_reference_type(ft)) { 664 ResourceMark rm(THREAD); 665 log_warning(cds, heap)( 666 "Please check reference field in %s instance in closed archive heap region: %s %s", 667 k->external_name(), (fs.name())->as_C_string(), 668 (fs.signature())->as_C_string()); 669 } 670 } 671 } 672 } 673 674 void HeapShared::check_module_oop(oop orig_module_obj) { 675 assert(DumpSharedSpaces, "must be"); 676 assert(java_lang_Module::is_instance(orig_module_obj), "must be"); 677 ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj); 678 if (orig_module_ent == NULL) { 679 // These special Module objects are created in Java code. They are not 680 // defined via Modules::define_module(), so they don't have a ModuleEntry: 681 // java.lang.Module::ALL_UNNAMED_MODULE 682 // java.lang.Module::EVERYONE_MODULE 683 // jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule 684 assert(java_lang_Module::name(orig_module_obj) == NULL, "must be unnamed"); 685 log_info(cds, heap)("Module oop with No ModuleEntry* @[" PTR_FORMAT "]", p2i(orig_module_obj)); 686 } else { 687 ClassLoaderData* loader_data = orig_module_ent->loader_data(); 688 assert(loader_data->is_builtin_class_loader_data(), "must be"); 689 } 690 } 691 692 693 // (1) If orig_obj has not been archived yet, archive it. 694 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called), 695 // trace all objects that are reachable from it, and make sure these objects are archived. 696 // (3) Record the klasses of all orig_obj and all reachable objects. 697 oop HeapShared::archive_reachable_objects_from(int level, 698 KlassSubGraphInfo* subgraph_info, 699 oop orig_obj, 700 bool is_closed_archive, 701 TRAPS) { 702 assert(orig_obj != NULL, "must be"); 703 assert(!is_archived_object(orig_obj), "sanity"); 704 705 if (!JavaClasses::is_supported_for_archiving(orig_obj)) { 706 // This object has injected fields that cannot be supported easily, so we disallow them for now. 707 // If you get an error here, you probably made a change in the JDK library that has added 708 // these objects that are referenced (directly or indirectly) by static fields. 709 ResourceMark rm; 710 log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name()); 711 vm_exit(1); 712 } 740 ++_num_new_archived_objs; 741 archived_obj = archive_heap_object(orig_obj, THREAD); 742 if (archived_obj == NULL) { 743 // Skip archiving the sub-graph referenced from the current entry field. 744 ResourceMark rm; 745 log_error(cds, heap)( 746 "Cannot archive the sub-graph referenced from %s object (" 747 PTR_FORMAT ") size %d, skipped.", 748 orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize); 749 if (level == 1) { 750 // Don't archive a subgraph root that's too big. For archives static fields, that's OK 751 // as the Java code will take care of initializing this field dynamically. 752 return NULL; 753 } else { 754 // We don't know how to handle an object that has been archived, but some of its reachable 755 // objects cannot be archived. Bail out for now. We might need to fix this in the future if 756 // we have a real use case. 757 vm_exit(1); 758 } 759 } 760 761 if (java_lang_Module::is_instance(orig_obj)) { 762 check_module_oop(orig_obj); 763 } 764 765 if (java_lang_Module::is_instance(orig_obj)) { 766 java_lang_Module::set_module_entry(archived_obj, NULL); 767 java_lang_Module::set_loader(archived_obj, NULL); 768 } else if (java_lang_ClassLoader::is_instance(orig_obj)) { 769 // class_data will be restored explicitly at run time. 770 guarantee(orig_obj == SystemDictionary::java_platform_loader() || 771 orig_obj == SystemDictionary::java_system_loader() || 772 java_lang_ClassLoader::loader_data_raw(orig_obj) == NULL, "must be"); 773 java_lang_ClassLoader::release_set_loader_data(archived_obj, NULL); 774 } 775 } 776 777 assert(archived_obj != NULL, "must be"); 778 Klass *orig_k = orig_obj->klass(); 779 Klass *relocated_k = archived_obj->klass(); 780 subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 781 782 WalkOopAndArchiveClosure walker(level, is_closed_archive, record_klasses_only, 783 subgraph_info, orig_obj, archived_obj, THREAD); 784 orig_obj->oop_iterate(&walker); 785 if (is_closed_archive && orig_k->is_instance_klass()) { 786 check_closed_archive_heap_region_object(InstanceKlass::cast(orig_k), THREAD); 787 } 788 return archived_obj; 789 } 790 791 // 792 // Start from the given static field in a java mirror and archive the 793 // complete sub-graph of java heap objects that are reached directly 794 // or indirectly from the starting object by following references. 1051 // [1] Dump the subgraph of each archived field 1052 // [2] Create a list of all the class of the objects that can be reached 1053 // by any of these static fields. 1054 // At runtime, these classes are initialized before X's archived fields 1055 // are restored by HeapShared::initialize_from_archived_subgraph(). 1056 int i; 1057 for (i = 0; i < num; ) { 1058 ArchivableStaticFieldInfo* info = &fields[i]; 1059 const char* klass_name = info->klass_name; 1060 start_recording_subgraph(info->klass, klass_name); 1061 1062 // If you have specified consecutive fields of the same klass in 1063 // fields[], these will be archived in the same 1064 // {start_recording_subgraph ... done_recording_subgraph} pass to 1065 // save time. 1066 for (; i < num; i++) { 1067 ArchivableStaticFieldInfo* f = &fields[i]; 1068 if (f->klass_name != klass_name) { 1069 break; 1070 } 1071 1072 if (!info->full_module_graph_only || MetaspaceShared::use_full_module_graph()) { 1073 archive_reachable_objects_from_static_field(f->klass, f->klass_name, 1074 f->offset, f->field_name, 1075 is_closed_archive, CHECK); 1076 } 1077 } 1078 done_recording_subgraph(info->klass, klass_name); 1079 } 1080 1081 log_info(cds, heap)("Archived subgraph records in %s archive heap region = %d", 1082 is_closed_archive ? "closed" : "open", 1083 _num_total_subgraph_recordings); 1084 log_info(cds, heap)(" Walked %d objects", _num_total_walked_objs); 1085 log_info(cds, heap)(" Archived %d objects", _num_total_archived_objs); 1086 log_info(cds, heap)(" Recorded %d klasses", _num_total_recorded_klasses); 1087 1088 #ifndef PRODUCT 1089 for (int i = 0; i < num; i++) { 1090 ArchivableStaticFieldInfo* f = &fields[i]; 1091 verify_subgraph_from_static_field(f->klass, f->offset); 1092 } 1093 log_info(cds, heap)(" Verified %d references", _num_total_verifications); 1094 #endif 1095 } 1096 |