1 /* 2 * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 "classfile/classLoaderData.hpp" 27 #include "classfile/classLoaderDataShared.hpp" 28 #include "classfile/javaClasses.inline.hpp" 29 #include "classfile/moduleEntry.hpp" 30 #include "classfile/stringTable.hpp" 31 #include "classfile/symbolTable.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "classfile/systemDictionaryShared.hpp" 34 #include "classfile/vmSymbols.hpp" 35 #include "gc/shared/gcLocker.hpp" 36 #include "logging/log.hpp" 37 #include "logging/logMessage.hpp" 38 #include "logging/logStream.hpp" 39 #include "memory/archiveUtils.hpp" 40 #include "memory/filemap.hpp" 41 #include "memory/heapShared.inline.hpp" 42 #include "memory/iterator.inline.hpp" 43 #include "memory/metadataFactory.hpp" 44 #include "memory/metaspaceClosure.hpp" 45 #include "memory/metaspaceShared.hpp" 46 #include "memory/resourceArea.hpp" 47 #include "memory/universe.hpp" 48 #include "oops/compressedOops.inline.hpp" 49 #include "oops/fieldStreams.inline.hpp" 50 #include "oops/oop.inline.hpp" 51 #include "runtime/fieldDescriptor.inline.hpp" 52 #include "runtime/javaCalls.hpp" 53 #include "runtime/safepointVerifiers.hpp" 54 #include "utilities/bitMap.inline.hpp" 55 #if INCLUDE_G1GC 56 #include "gc/g1/g1CollectedHeap.hpp" 57 #endif 58 59 #if INCLUDE_CDS_JAVA_HEAP 60 61 bool HeapShared::_closed_archive_heap_region_mapped = false; 62 bool HeapShared::_open_archive_heap_region_mapped = false; 63 bool HeapShared::_archive_heap_region_fixed = false; 64 65 address HeapShared::_narrow_oop_base; 66 int HeapShared::_narrow_oop_shift; 67 68 // 69 // If you add new entries to the following tables, you should know what you're doing! 70 // 71 72 // Entry fields for shareable subgraphs archived in the closed archive heap 73 // region. Warning: Objects in the subgraphs should not have reference fields 74 // assigned at runtime. 75 static ArchivableStaticFieldInfo closed_archive_subgraph_entry_fields[] = { 76 {"java/lang/Integer$IntegerCache", 0, "archivedCache"}, 77 {"java/lang/Long$LongCache", 0, "archivedCache"}, 78 {"java/lang/Byte$ByteCache", 0, "archivedCache"}, 79 {"java/lang/Short$ShortCache", 0, "archivedCache"}, 80 {"java/lang/Character$CharacterCache", 0, "archivedCache"}, 81 {"java/util/jar/Attributes$Name", 0, "KNOWN_NAMES"}, 82 {"sun/util/locale/BaseLocale", 0, "constantBaseLocales"}, 83 }; 84 // Entry fields for subgraphs archived in the open archive heap region. 85 static ArchivableStaticFieldInfo open_archive_subgraph_entry_fields[] = { 86 {"jdk/internal/loader/ArchivedClassLoaders", 1, "archivedClassLoaders"}, 87 {"jdk/internal/module/ArchivedBootLayer", 1, "archivedBootLayer"}, 88 {"jdk/internal/module/ArchivedModuleGraph", 0, "archivedModuleGraph"}, 89 {"java/util/ImmutableCollections", 0, "archivedObjects"}, 90 {"java/lang/Module$ArchivedData", 1, "archivedData"}, 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 assert(DumpSharedSpaces, "dump-time only"); 143 log_debug(cds)("Resetting platform loader"); 144 reset_states(SystemDictionary::java_platform_loader(), THREAD); 145 log_debug(cds)("Resetting system loader"); 146 reset_states(SystemDictionary::java_system_loader(), THREAD); 147 } 148 149 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = NULL; 150 oop HeapShared::find_archived_heap_object(oop obj) { 151 assert(DumpSharedSpaces, "dump-time only"); 152 ArchivedObjectCache* cache = archived_object_cache(); 153 oop* p = cache->get(obj); 154 if (p != NULL) { 155 return *p; 156 } else { 157 return NULL; 158 } 159 } 160 161 oop HeapShared::archive_heap_object(oop obj, Thread* THREAD) { 162 assert(DumpSharedSpaces, "dump-time only"); 163 164 oop ao = find_archived_heap_object(obj); 165 if (ao != NULL) { 166 // already archived 167 return ao; 168 } 169 170 int len = obj->size(); 171 if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) { 172 log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT, 173 p2i(obj), (size_t)obj->size()); 174 return NULL; 175 } 176 177 oop archived_oop = (oop)G1CollectedHeap::heap()->archive_mem_allocate(len); 178 if (archived_oop != NULL) { 179 Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(obj), cast_from_oop<HeapWord*>(archived_oop), len); 180 MetaspaceShared::relocate_klass_ptr(archived_oop); 181 // Reinitialize markword to remove age/marking/locking/etc. 182 // 183 // We need to retain the identity_hash, because it may have been used by some hashtables 184 // in the shared heap. This also has the side effect of pre-initializing the 185 // identity_hash for all shared objects, so they are less likely to be written 186 // into during run time, increasing the potential of memory sharing. 187 int hash_original = obj->identity_hash(); 188 archived_oop->set_mark_raw(markWord::prototype().copy_set_hash(hash_original)); 189 assert(archived_oop->mark().is_unlocked(), "sanity"); 190 191 DEBUG_ONLY(int hash_archived = archived_oop->identity_hash()); 192 assert(hash_original == hash_archived, "Different hash codes: original %x, archived %x", hash_original, hash_archived); 193 194 ArchivedObjectCache* cache = archived_object_cache(); 195 cache->put(obj, archived_oop); 196 log_debug(cds, heap)("Archived heap object " PTR_FORMAT " ==> " PTR_FORMAT, 197 p2i(obj), p2i(archived_oop)); 198 } else { 199 log_error(cds, heap)( 200 "Cannot allocate space for object " PTR_FORMAT " in archived heap region", 201 p2i(obj)); 202 vm_exit(1); 203 } 204 return archived_oop; 205 } 206 207 oop HeapShared::materialize_archived_object(narrowOop v) { 208 assert(archive_heap_region_fixed(), 209 "must be called after archive heap regions are fixed"); 210 if (!CompressedOops::is_null(v)) { 211 oop obj = HeapShared::decode_from_archive(v); 212 return G1CollectedHeap::heap()->materialize_archived_object(obj); 213 } 214 return NULL; 215 } 216 217 void HeapShared::archive_klass_objects(Thread* THREAD) { 218 GrowableArray<Klass*>* klasses = MetaspaceShared::collected_klasses(); 219 assert(klasses != NULL, "sanity"); 220 for (int i = 0; i < klasses->length(); i++) { 221 Klass* k = klasses->at(i); 222 223 // archive mirror object 224 java_lang_Class::archive_mirror(k, CHECK); 225 226 // archive the resolved_referenes array 227 if (k->is_instance_klass()) { 228 InstanceKlass* ik = InstanceKlass::cast(k); 229 ik->constants()->archive_resolved_references(THREAD); 230 } 231 } 232 } 233 234 void HeapShared::run_full_gc_in_vm_thread() { 235 if (is_heap_object_archiving_allowed()) { 236 // Avoid fragmentation while archiving heap objects. 237 // We do this inside a safepoint, so that no further allocation can happen after GC 238 // has finished. 239 if (GCLocker::is_active()) { 240 // Just checking for safety ... 241 // This should not happen during -Xshare:dump. If you see this, probably the Java core lib 242 // has been modified such that JNI code is executed in some clean up threads after 243 // we have finished class loading. 244 log_warning(cds)("GC locker is held, unable to start extra compacting GC. This may produce suboptimal results."); 245 } else { 246 log_info(cds)("Run GC ..."); 247 Universe::heap()->collect_as_vm_thread(GCCause::_archive_time_gc); 248 log_info(cds)("Run GC done"); 249 } 250 } 251 } 252 253 void HeapShared::archive_java_heap_objects(GrowableArray<MemRegion> *closed, 254 GrowableArray<MemRegion> *open) { 255 if (!is_heap_object_archiving_allowed()) { 256 log_info(cds)( 257 "Archived java heap is not supported as UseG1GC, " 258 "UseCompressedOops and UseCompressedClassPointers are required." 259 "Current settings: UseG1GC=%s, UseCompressedOops=%s, UseCompressedClassPointers=%s.", 260 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops), 261 BOOL_TO_STR(UseCompressedClassPointers)); 262 return; 263 } 264 265 G1HeapVerifier::verify_ready_for_archiving(); 266 267 { 268 NoSafepointVerifier nsv; 269 270 // Cache for recording where the archived objects are copied to 271 create_archived_object_cache(); 272 273 log_info(cds)("Dumping objects to closed archive heap region ..."); 274 NOT_PRODUCT(StringTable::verify()); 275 copy_closed_archive_heap_objects(closed); 276 277 log_info(cds)("Dumping objects to open archive heap region ..."); 278 copy_open_archive_heap_objects(open); 279 280 if (MetaspaceShared::use_full_module_graph()) { 281 ClassLoaderDataShared::init_archived_oops(); 282 } 283 284 destroy_archived_object_cache(); 285 } 286 287 G1HeapVerifier::verify_archive_regions(); 288 } 289 290 void HeapShared::copy_closed_archive_heap_objects( 291 GrowableArray<MemRegion> * closed_archive) { 292 assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects"); 293 294 Thread* THREAD = Thread::current(); 295 G1CollectedHeap::heap()->begin_archive_alloc_range(); 296 297 // Archive interned string objects 298 StringTable::write_to_archive(); 299 300 archive_object_subgraphs(closed_archive_subgraph_entry_fields, 301 num_closed_archive_subgraph_entry_fields, 302 true /* is_closed_archive */, THREAD); 303 304 G1CollectedHeap::heap()->end_archive_alloc_range(closed_archive, 305 os::vm_allocation_granularity()); 306 } 307 308 void HeapShared::copy_open_archive_heap_objects( 309 GrowableArray<MemRegion> * open_archive) { 310 assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects"); 311 312 Thread* THREAD = Thread::current(); 313 G1CollectedHeap::heap()->begin_archive_alloc_range(true /* open */); 314 315 java_lang_Class::archive_basic_type_mirrors(THREAD); 316 317 archive_klass_objects(THREAD); 318 319 archive_object_subgraphs(open_archive_subgraph_entry_fields, 320 num_open_archive_subgraph_entry_fields, 321 false /* is_closed_archive */, 322 THREAD); 323 324 G1CollectedHeap::heap()->end_archive_alloc_range(open_archive, 325 os::vm_allocation_granularity()); 326 } 327 328 void HeapShared::init_narrow_oop_decoding(address base, int shift) { 329 _narrow_oop_base = base; 330 _narrow_oop_shift = shift; 331 } 332 333 // 334 // Subgraph archiving support 335 // 336 HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = NULL; 337 HeapShared::RunTimeKlassSubGraphInfoTable HeapShared::_run_time_subgraph_info_table; 338 339 // Get the subgraph_info for Klass k. A new subgraph_info is created if 340 // there is no existing one for k. The subgraph_info records the relocated 341 // Klass* of the original k. 342 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) { 343 assert(DumpSharedSpaces, "dump time only"); 344 Klass* relocated_k = MetaspaceShared::get_relocated_klass(k); 345 KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(relocated_k); 346 if (info == NULL) { 347 _dump_time_subgraph_info_table->put(relocated_k, KlassSubGraphInfo(relocated_k)); 348 info = _dump_time_subgraph_info_table->get(relocated_k); 349 ++ _dump_time_subgraph_info_table->_count; 350 } 351 return info; 352 } 353 354 // Add an entry field to the current KlassSubGraphInfo. 355 void KlassSubGraphInfo::add_subgraph_entry_field( 356 int static_field_offset, oop v, bool is_closed_archive) { 357 assert(DumpSharedSpaces, "dump time only"); 358 if (_subgraph_entry_fields == NULL) { 359 _subgraph_entry_fields = 360 new(ResourceObj::C_HEAP, mtClass) GrowableArray<juint>(10, mtClass); 361 } 362 _subgraph_entry_fields->append((juint)static_field_offset); 363 _subgraph_entry_fields->append(CompressedOops::encode(v)); 364 _subgraph_entry_fields->append(is_closed_archive ? 1 : 0); 365 } 366 367 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs. 368 // Only objects of boot classes can be included in sub-graph. 369 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k, Klass *relocated_k) { 370 assert(DumpSharedSpaces, "dump time only"); 371 assert(relocated_k == MetaspaceShared::get_relocated_klass(orig_k), 372 "must be the relocated Klass in the shared space"); 373 374 if (_subgraph_object_klasses == NULL) { 375 _subgraph_object_klasses = 376 new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, mtClass); 377 } 378 379 assert(relocated_k->is_shared(), "must be a shared class"); 380 381 if (_k == relocated_k) { 382 // Don't add the Klass containing the sub-graph to it's own klass 383 // initialization list. 384 return; 385 } 386 387 if (relocated_k->is_instance_klass()) { 388 assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(), 389 "must be boot class"); 390 // SystemDictionary::xxx_klass() are not updated, need to check 391 // the original Klass* 392 if (orig_k == SystemDictionary::String_klass() || 393 orig_k == SystemDictionary::Object_klass()) { 394 // Initialized early during VM initialization. No need to be added 395 // to the sub-graph object class list. 396 return; 397 } 398 } else if (relocated_k->is_objArray_klass()) { 399 Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass(); 400 if (abk->is_instance_klass()) { 401 assert(InstanceKlass::cast(abk)->is_shared_boot_class(), 402 "must be boot class"); 403 } 404 if (relocated_k == Universe::objectArrayKlassObj()) { 405 // Initialized early during Universe::genesis. No need to be added 406 // to the list. 407 return; 408 } 409 } else { 410 assert(relocated_k->is_typeArray_klass(), "must be"); 411 // Primitive type arrays are created early during Universe::genesis. 412 return; 413 } 414 415 if (log_is_enabled(Debug, cds, heap)) { 416 if (!_subgraph_object_klasses->contains(relocated_k)) { 417 ResourceMark rm; 418 log_debug(cds, heap)("Adding klass %s", orig_k->external_name()); 419 } 420 } 421 422 _subgraph_object_klasses->append_if_missing(relocated_k); 423 } 424 425 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo. 426 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) { 427 _k = info->klass(); 428 _entry_field_records = NULL; 429 _subgraph_object_klasses = NULL; 430 431 // populate the entry fields 432 GrowableArray<juint>* entry_fields = info->subgraph_entry_fields(); 433 if (entry_fields != NULL) { 434 int num_entry_fields = entry_fields->length(); 435 assert(num_entry_fields % 3 == 0, "sanity"); 436 _entry_field_records = 437 MetaspaceShared::new_ro_array<juint>(num_entry_fields); 438 for (int i = 0 ; i < num_entry_fields; i++) { 439 _entry_field_records->at_put(i, entry_fields->at(i)); 440 } 441 } 442 443 // the Klasses of the objects in the sub-graphs 444 GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses(); 445 if (subgraph_object_klasses != NULL) { 446 int num_subgraphs_klasses = subgraph_object_klasses->length(); 447 _subgraph_object_klasses = 448 MetaspaceShared::new_ro_array<Klass*>(num_subgraphs_klasses); 449 for (int i = 0; i < num_subgraphs_klasses; i++) { 450 Klass* subgraph_k = subgraph_object_klasses->at(i); 451 if (log_is_enabled(Info, cds, heap)) { 452 ResourceMark rm; 453 log_info(cds, heap)( 454 "Archived object klass %s (%2d) => %s", 455 _k->external_name(), i, subgraph_k->external_name()); 456 } 457 _subgraph_object_klasses->at_put(i, subgraph_k); 458 ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i)); 459 } 460 } 461 462 ArchivePtrMarker::mark_pointer(&_k); 463 ArchivePtrMarker::mark_pointer(&_entry_field_records); 464 ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses); 465 } 466 467 struct CopyKlassSubGraphInfoToArchive : StackObj { 468 CompactHashtableWriter* _writer; 469 CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {} 470 471 bool do_entry(Klass* klass, KlassSubGraphInfo& info) { 472 if (info.subgraph_object_klasses() != NULL || info.subgraph_entry_fields() != NULL) { 473 ArchivedKlassSubGraphInfoRecord* record = 474 (ArchivedKlassSubGraphInfoRecord*)MetaspaceShared::read_only_space_alloc(sizeof(ArchivedKlassSubGraphInfoRecord)); 475 record->init(&info); 476 477 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(klass); 478 u4 delta = MetaspaceShared::object_delta_u4(record); 479 _writer->add(hash, delta); 480 } 481 return true; // keep on iterating 482 } 483 }; 484 485 // Build the records of archived subgraph infos, which include: 486 // - Entry points to all subgraphs from the containing class mirror. The entry 487 // points are static fields in the mirror. For each entry point, the field 488 // offset, value and is_closed_archive flag are recorded in the sub-graph 489 // info. The value is stored back to the corresponding field at runtime. 490 // - A list of klasses that need to be loaded/initialized before archived 491 // java object sub-graph can be accessed at runtime. 492 void HeapShared::write_subgraph_info_table() { 493 // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive. 494 DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table; 495 CompactHashtableStats stats; 496 497 _run_time_subgraph_info_table.reset(); 498 499 CompactHashtableWriter writer(d_table->_count, &stats); 500 CopyKlassSubGraphInfoToArchive copy(&writer); 501 d_table->iterate(©); 502 503 writer.dump(&_run_time_subgraph_info_table, "subgraphs"); 504 } 505 506 void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) { 507 _run_time_subgraph_info_table.serialize_header(soc); 508 } 509 510 void HeapShared::initialize_from_archived_subgraph(Klass* k) { 511 if (!open_archive_heap_region_mapped()) { 512 return; // nothing to do 513 } 514 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 515 516 if (!MetaspaceShared::use_full_module_graph()) { 517 for (int i = 0; i < num_open_archive_subgraph_entry_fields; i++) { 518 const ArchivableStaticFieldInfo* info = &open_archive_subgraph_entry_fields[i]; 519 if (info->full_module_graph_only && k->name()->equals(info->klass_name)) { 520 return; 521 } 522 } 523 } 524 525 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(k); 526 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0); 527 528 // Initialize from archived data. Currently this is done only 529 // during VM initialization time. No lock is needed. 530 if (record != NULL) { 531 Thread* THREAD = Thread::current(); 532 533 int i; 534 // Load/link/initialize the klasses of the objects in the subgraph. 535 // NULL class loader is used. 536 Array<Klass*>* klasses = record->subgraph_object_klasses(); 537 if (klasses != NULL) { 538 for (i = 0; i < klasses->length(); i++) { 539 Klass* obj_k = klasses->at(i); 540 Klass* resolved_k = SystemDictionary::resolve_or_null( 541 (obj_k)->name(), THREAD); 542 if (resolved_k != obj_k) { 543 assert(!SystemDictionary::is_well_known_klass(resolved_k), 544 "shared well-known classes must not be replaced by JVMTI ClassFileLoadHook"); 545 ResourceMark rm(THREAD); 546 log_info(cds, heap)("Failed to load subgraph because %s was not loaded from archive", 547 resolved_k->external_name()); 548 return; 549 } 550 if ((obj_k)->is_instance_klass()) { 551 InstanceKlass* ik = InstanceKlass::cast(obj_k); 552 ik->initialize(THREAD); 553 } else if ((obj_k)->is_objArray_klass()) { 554 ObjArrayKlass* oak = ObjArrayKlass::cast(obj_k); 555 oak->initialize(THREAD); 556 } 557 } 558 } 559 560 if (HAS_PENDING_EXCEPTION) { 561 CLEAR_PENDING_EXCEPTION; 562 // None of the field value will be set if there was an exception. 563 // The java code will not see any of the archived objects in the 564 // subgraphs referenced from k in this case. 565 return; 566 } 567 568 // Load the subgraph entry fields from the record and store them back to 569 // the corresponding fields within the mirror. 570 oop m = k->java_mirror(); 571 Array<juint>* entry_field_records = record->entry_field_records(); 572 if (entry_field_records != NULL) { 573 int efr_len = entry_field_records->length(); 574 assert(efr_len % 3 == 0, "sanity"); 575 for (i = 0; i < efr_len;) { 576 int field_offset = entry_field_records->at(i); 577 narrowOop nv = entry_field_records->at(i+1); 578 int is_closed_archive = entry_field_records->at(i+2); 579 oop v; 580 if (is_closed_archive == 0) { 581 // It's an archived object in the open archive heap regions, not shared. 582 // The object refereced by the field becomes 'known' by GC from this 583 // point. All objects in the subgraph reachable from the object are 584 // also 'known' by GC. 585 v = materialize_archived_object(nv); 586 } else { 587 // Shared object in the closed archive heap regions. Decode directly. 588 assert(!CompressedOops::is_null(nv), "shared object is null"); 589 v = HeapShared::decode_from_archive(nv); 590 } 591 m->obj_field_put(field_offset, v); 592 i += 3; 593 594 log_debug(cds, heap)(" " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v)); 595 } 596 597 // Done. Java code can see the archived sub-graphs referenced from k's 598 // mirror after this point. 599 if (log_is_enabled(Info, cds, heap)) { 600 ResourceMark rm; 601 log_info(cds, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT, 602 k->external_name(), p2i(k)); 603 } 604 } 605 } 606 } 607 608 class WalkOopAndArchiveClosure: public BasicOopIterateClosure { 609 int _level; 610 bool _is_closed_archive; 611 bool _record_klasses_only; 612 KlassSubGraphInfo* _subgraph_info; 613 oop _orig_referencing_obj; 614 oop _archived_referencing_obj; 615 Thread* _thread; 616 public: 617 WalkOopAndArchiveClosure(int level, 618 bool is_closed_archive, 619 bool record_klasses_only, 620 KlassSubGraphInfo* subgraph_info, 621 oop orig, oop archived, TRAPS) : 622 _level(level), _is_closed_archive(is_closed_archive), 623 _record_klasses_only(record_klasses_only), 624 _subgraph_info(subgraph_info), 625 _orig_referencing_obj(orig), _archived_referencing_obj(archived), 626 _thread(THREAD) {} 627 void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 628 void do_oop( oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); } 629 630 protected: 631 template <class T> void do_oop_work(T *p) { 632 oop obj = RawAccess<>::oop_load(p); 633 if (!CompressedOops::is_null(obj)) { 634 assert(!HeapShared::is_archived_object(obj), 635 "original objects must not point to archived objects"); 636 637 size_t field_delta = pointer_delta(p, _orig_referencing_obj, sizeof(char)); 638 T* new_p = (T*)(cast_from_oop<address>(_archived_referencing_obj) + field_delta); 639 Thread* THREAD = _thread; 640 641 if (!_record_klasses_only && log_is_enabled(Debug, cds, heap)) { 642 ResourceMark rm; 643 log_debug(cds, heap)("(%d) %s[" SIZE_FORMAT "] ==> " PTR_FORMAT " size %d %s", _level, 644 _orig_referencing_obj->klass()->external_name(), field_delta, 645 p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name()); 646 LogTarget(Trace, cds, heap) log; 647 LogStream out(log); 648 obj->print_on(&out); 649 } 650 651 oop archived = HeapShared::archive_reachable_objects_from( 652 _level + 1, _subgraph_info, obj, _is_closed_archive, THREAD); 653 assert(archived != NULL, "VM should have exited with unarchivable objects for _level > 1"); 654 assert(HeapShared::is_archived_object(archived), "must be"); 655 656 if (!_record_klasses_only) { 657 // Update the reference in the archived copy of the referencing object. 658 log_debug(cds, heap)("(%d) updating oop @[" PTR_FORMAT "] " PTR_FORMAT " ==> " PTR_FORMAT, 659 _level, p2i(new_p), p2i(obj), p2i(archived)); 660 RawAccess<IS_NOT_NULL>::oop_store(new_p, archived); 661 } 662 } 663 } 664 }; 665 666 void HeapShared::check_closed_archive_heap_region_object(InstanceKlass* k, 667 Thread* THREAD) { 668 // Check fields in the object 669 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { 670 if (!fs.access_flags().is_static()) { 671 BasicType ft = fs.field_descriptor().field_type(); 672 if (!fs.access_flags().is_final() && is_reference_type(ft)) { 673 ResourceMark rm(THREAD); 674 log_warning(cds, heap)( 675 "Please check reference field in %s instance in closed archive heap region: %s %s", 676 k->external_name(), (fs.name())->as_C_string(), 677 (fs.signature())->as_C_string()); 678 } 679 } 680 } 681 } 682 683 void HeapShared::check_module_oop(oop orig_module_obj) { 684 assert(DumpSharedSpaces, "must be"); 685 assert(java_lang_Module::is_instance(orig_module_obj), "must be"); 686 ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj); 687 if (orig_module_ent == NULL) { 688 // These special Module objects are created in Java code. They are not 689 // defined via Modules::define_module(), so they don't have a ModuleEntry: 690 // java.lang.Module::ALL_UNNAMED_MODULE 691 // java.lang.Module::EVERYONE_MODULE 692 // jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule 693 assert(java_lang_Module::name(orig_module_obj) == NULL, "must be unnamed"); 694 log_info(cds, heap)("Module oop with No ModuleEntry* @[" PTR_FORMAT "]", p2i(orig_module_obj)); 695 } else { 696 ClassLoaderData* loader_data = orig_module_ent->loader_data(); 697 assert(loader_data->is_builtin_class_loader_data(), "must be"); 698 } 699 } 700 701 702 // (1) If orig_obj has not been archived yet, archive it. 703 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called), 704 // trace all objects that are reachable from it, and make sure these objects are archived. 705 // (3) Record the klasses of all orig_obj and all reachable objects. 706 oop HeapShared::archive_reachable_objects_from(int level, 707 KlassSubGraphInfo* subgraph_info, 708 oop orig_obj, 709 bool is_closed_archive, 710 TRAPS) { 711 assert(orig_obj != NULL, "must be"); 712 assert(!is_archived_object(orig_obj), "sanity"); 713 714 if (!JavaClasses::is_supported_for_archiving(orig_obj)) { 715 // This object has injected fields that cannot be supported easily, so we disallow them for now. 716 // If you get an error here, you probably made a change in the JDK library that has added 717 // these objects that are referenced (directly or indirectly) by static fields. 718 ResourceMark rm; 719 log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name()); 720 vm_exit(1); 721 } 722 723 // java.lang.Class instances cannot be included in an archived object sub-graph. We only support 724 // them as Klass::_archived_mirror because they need to be specially restored at run time. 725 // 726 // If you get an error here, you probably made a change in the JDK library that has added a Class 727 // object that is referenced (directly or indirectly) by static fields. 728 if (java_lang_Class::is_instance(orig_obj)) { 729 log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level); 730 vm_exit(1); 731 } 732 733 oop archived_obj = find_archived_heap_object(orig_obj); 734 if (java_lang_String::is_instance(orig_obj) && archived_obj != NULL) { 735 // To save time, don't walk strings that are already archived. They just contain 736 // pointers to a type array, whose klass doesn't need to be recorded. 737 return archived_obj; 738 } 739 740 if (has_been_seen_during_subgraph_recording(orig_obj)) { 741 // orig_obj has already been archived and traced. Nothing more to do. 742 return archived_obj; 743 } else { 744 set_has_been_seen_during_subgraph_recording(orig_obj); 745 } 746 747 bool record_klasses_only = (archived_obj != NULL); 748 if (archived_obj == NULL) { 749 ++_num_new_archived_objs; 750 archived_obj = archive_heap_object(orig_obj, THREAD); 751 if (archived_obj == NULL) { 752 // Skip archiving the sub-graph referenced from the current entry field. 753 ResourceMark rm; 754 log_error(cds, heap)( 755 "Cannot archive the sub-graph referenced from %s object (" 756 PTR_FORMAT ") size %d, skipped.", 757 orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize); 758 if (level == 1) { 759 // Don't archive a subgraph root that's too big. For archives static fields, that's OK 760 // as the Java code will take care of initializing this field dynamically. 761 return NULL; 762 } else { 763 // We don't know how to handle an object that has been archived, but some of its reachable 764 // objects cannot be archived. Bail out for now. We might need to fix this in the future if 765 // we have a real use case. 766 vm_exit(1); 767 } 768 } 769 770 if (java_lang_Module::is_instance(orig_obj)) { 771 check_module_oop(orig_obj); 772 java_lang_Module::set_module_entry(archived_obj, NULL); 773 java_lang_Module::set_loader(archived_obj, NULL); 774 } else if (java_lang_ClassLoader::is_instance(orig_obj)) { 775 // class_data will be restored explicitly at run time. 776 guarantee(orig_obj == SystemDictionary::java_platform_loader() || 777 orig_obj == SystemDictionary::java_system_loader() || 778 java_lang_ClassLoader::loader_data_raw(orig_obj) == NULL, "must be"); 779 java_lang_ClassLoader::release_set_loader_data(archived_obj, NULL); 780 } 781 } 782 783 assert(archived_obj != NULL, "must be"); 784 Klass *orig_k = orig_obj->klass(); 785 Klass *relocated_k = archived_obj->klass(); 786 subgraph_info->add_subgraph_object_klass(orig_k, relocated_k); 787 788 WalkOopAndArchiveClosure walker(level, is_closed_archive, record_klasses_only, 789 subgraph_info, orig_obj, archived_obj, THREAD); 790 orig_obj->oop_iterate(&walker); 791 if (is_closed_archive && orig_k->is_instance_klass()) { 792 check_closed_archive_heap_region_object(InstanceKlass::cast(orig_k), THREAD); 793 } 794 return archived_obj; 795 } 796 797 // 798 // Start from the given static field in a java mirror and archive the 799 // complete sub-graph of java heap objects that are reached directly 800 // or indirectly from the starting object by following references. 801 // Sub-graph archiving restrictions (current): 802 // 803 // - All classes of objects in the archived sub-graph (including the 804 // entry class) must be boot class only. 805 // - No java.lang.Class instance (java mirror) can be included inside 806 // an archived sub-graph. Mirror can only be the sub-graph entry object. 807 // 808 // The Java heap object sub-graph archiving process (see 809 // WalkOopAndArchiveClosure): 810 // 811 // 1) Java object sub-graph archiving starts from a given static field 812 // within a Class instance (java mirror). If the static field is a 813 // refererence field and points to a non-null java object, proceed to 814 // the next step. 815 // 816 // 2) Archives the referenced java object. If an archived copy of the 817 // current object already exists, updates the pointer in the archived 818 // copy of the referencing object to point to the current archived object. 819 // Otherwise, proceed to the next step. 820 // 821 // 3) Follows all references within the current java object and recursively 822 // archive the sub-graph of objects starting from each reference. 823 // 824 // 4) Updates the pointer in the archived copy of referencing object to 825 // point to the current archived object. 826 // 827 // 5) The Klass of the current java object is added to the list of Klasses 828 // for loading and initialzing before any object in the archived graph can 829 // be accessed at runtime. 830 // 831 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k, 832 const char* klass_name, 833 int field_offset, 834 const char* field_name, 835 bool is_closed_archive, 836 TRAPS) { 837 assert(DumpSharedSpaces, "dump time only"); 838 assert(k->is_shared_boot_class(), "must be boot class"); 839 840 oop m = k->java_mirror(); 841 842 KlassSubGraphInfo* subgraph_info = get_subgraph_info(k); 843 oop f = m->obj_field(field_offset); 844 845 log_debug(cds, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f)); 846 847 if (!CompressedOops::is_null(f)) { 848 if (log_is_enabled(Trace, cds, heap)) { 849 LogTarget(Trace, cds, heap) log; 850 LogStream out(log); 851 f->print_on(&out); 852 } 853 854 oop af = archive_reachable_objects_from(1, subgraph_info, f, 855 is_closed_archive, CHECK); 856 857 if (af == NULL) { 858 log_error(cds, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)", 859 klass_name, field_name); 860 } else { 861 // Note: the field value is not preserved in the archived mirror. 862 // Record the field as a new subGraph entry point. The recorded 863 // information is restored from the archive at runtime. 864 subgraph_info->add_subgraph_entry_field(field_offset, af, is_closed_archive); 865 log_info(cds, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(af)); 866 } 867 } else { 868 // The field contains null, we still need to record the entry point, 869 // so it can be restored at runtime. 870 subgraph_info->add_subgraph_entry_field(field_offset, NULL, false); 871 } 872 } 873 874 #ifndef PRODUCT 875 class VerifySharedOopClosure: public BasicOopIterateClosure { 876 private: 877 bool _is_archived; 878 879 public: 880 VerifySharedOopClosure(bool is_archived) : _is_archived(is_archived) {} 881 882 void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); } 883 void do_oop( oop *p) { VerifySharedOopClosure::do_oop_work(p); } 884 885 protected: 886 template <class T> void do_oop_work(T *p) { 887 oop obj = RawAccess<>::oop_load(p); 888 if (!CompressedOops::is_null(obj)) { 889 HeapShared::verify_reachable_objects_from(obj, _is_archived); 890 } 891 } 892 }; 893 894 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) { 895 assert(DumpSharedSpaces, "dump time only"); 896 assert(k->is_shared_boot_class(), "must be boot class"); 897 898 oop m = k->java_mirror(); 899 oop f = m->obj_field(field_offset); 900 if (!CompressedOops::is_null(f)) { 901 verify_subgraph_from(f); 902 } 903 } 904 905 void HeapShared::verify_subgraph_from(oop orig_obj) { 906 oop archived_obj = find_archived_heap_object(orig_obj); 907 if (archived_obj == NULL) { 908 // It's OK for the root of a subgraph to be not archived. See comments in 909 // archive_reachable_objects_from(). 910 return; 911 } 912 913 // Verify that all objects reachable from orig_obj are archived. 914 init_seen_objects_table(); 915 verify_reachable_objects_from(orig_obj, false); 916 delete_seen_objects_table(); 917 918 // Note: we could also verify that all objects reachable from the archived 919 // copy of orig_obj can only point to archived objects, with: 920 // init_seen_objects_table(); 921 // verify_reachable_objects_from(archived_obj, true); 922 // init_seen_objects_table(); 923 // but that's already done in G1HeapVerifier::verify_archive_regions so we 924 // won't do it here. 925 } 926 927 void HeapShared::verify_reachable_objects_from(oop obj, bool is_archived) { 928 _num_total_verifications ++; 929 if (!has_been_seen_during_subgraph_recording(obj)) { 930 set_has_been_seen_during_subgraph_recording(obj); 931 932 if (is_archived) { 933 assert(is_archived_object(obj), "must be"); 934 assert(find_archived_heap_object(obj) == NULL, "must be"); 935 } else { 936 assert(!is_archived_object(obj), "must be"); 937 assert(find_archived_heap_object(obj) != NULL, "must be"); 938 } 939 940 VerifySharedOopClosure walker(is_archived); 941 obj->oop_iterate(&walker); 942 } 943 } 944 #endif 945 946 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = NULL; 947 int HeapShared::_num_new_walked_objs; 948 int HeapShared::_num_new_archived_objs; 949 int HeapShared::_num_old_recorded_klasses; 950 951 int HeapShared::_num_total_subgraph_recordings = 0; 952 int HeapShared::_num_total_walked_objs = 0; 953 int HeapShared::_num_total_archived_objs = 0; 954 int HeapShared::_num_total_recorded_klasses = 0; 955 int HeapShared::_num_total_verifications = 0; 956 957 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) { 958 return _seen_objects_table->get(obj) != NULL; 959 } 960 961 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) { 962 assert(!has_been_seen_during_subgraph_recording(obj), "sanity"); 963 _seen_objects_table->put(obj, true); 964 ++ _num_new_walked_objs; 965 } 966 967 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name) { 968 log_info(cds, heap)("Start recording subgraph(s) for archived fields in %s", class_name); 969 init_seen_objects_table(); 970 _num_new_walked_objs = 0; 971 _num_new_archived_objs = 0; 972 _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses(); 973 } 974 975 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) { 976 int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() - 977 _num_old_recorded_klasses; 978 log_info(cds, heap)("Done recording subgraph(s) for archived fields in %s: " 979 "walked %d objs, archived %d new objs, recorded %d classes", 980 class_name, _num_new_walked_objs, _num_new_archived_objs, 981 num_new_recorded_klasses); 982 983 delete_seen_objects_table(); 984 985 _num_total_subgraph_recordings ++; 986 _num_total_walked_objs += _num_new_walked_objs; 987 _num_total_archived_objs += _num_new_archived_objs; 988 _num_total_recorded_klasses += num_new_recorded_klasses; 989 } 990 991 class ArchivableStaticFieldFinder: public FieldClosure { 992 InstanceKlass* _ik; 993 Symbol* _field_name; 994 bool _found; 995 int _offset; 996 public: 997 ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) : 998 _ik(ik), _field_name(field_name), _found(false), _offset(-1) {} 999 1000 virtual void do_field(fieldDescriptor* fd) { 1001 if (fd->name() == _field_name) { 1002 assert(!_found, "fields cannot be overloaded"); 1003 assert(is_reference_type(fd->field_type()), "can archive only fields that are references"); 1004 _found = true; 1005 _offset = fd->offset(); 1006 } 1007 } 1008 bool found() { return _found; } 1009 int offset() { return _offset; } 1010 }; 1011 1012 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], 1013 int num, Thread* THREAD) { 1014 for (int i = 0; i < num; i++) { 1015 ArchivableStaticFieldInfo* info = &fields[i]; 1016 TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name); 1017 TempNewSymbol field_name = SymbolTable::new_symbol(info->field_name); 1018 1019 Klass* k = SystemDictionary::resolve_or_null(klass_name, THREAD); 1020 assert(k != NULL && !HAS_PENDING_EXCEPTION, "class must exist"); 1021 InstanceKlass* ik = InstanceKlass::cast(k); 1022 assert(InstanceKlass::cast(ik)->is_shared_boot_class(), 1023 "Only support boot classes"); 1024 ik->initialize(THREAD); 1025 guarantee(!HAS_PENDING_EXCEPTION, "exception in initialize"); 1026 1027 ArchivableStaticFieldFinder finder(ik, field_name); 1028 ik->do_local_static_fields(&finder); 1029 assert(finder.found(), "field must exist"); 1030 1031 info->klass = ik; 1032 info->offset = finder.offset(); 1033 } 1034 } 1035 1036 void HeapShared::init_subgraph_entry_fields(Thread* THREAD) { 1037 _dump_time_subgraph_info_table = new (ResourceObj::C_HEAP, mtClass)DumpTimeKlassSubGraphInfoTable(); 1038 1039 init_subgraph_entry_fields(closed_archive_subgraph_entry_fields, 1040 num_closed_archive_subgraph_entry_fields, 1041 THREAD); 1042 init_subgraph_entry_fields(open_archive_subgraph_entry_fields, 1043 num_open_archive_subgraph_entry_fields, 1044 THREAD); 1045 } 1046 1047 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[], 1048 int num, bool is_closed_archive, 1049 Thread* THREAD) { 1050 _num_total_subgraph_recordings = 0; 1051 _num_total_walked_objs = 0; 1052 _num_total_archived_objs = 0; 1053 _num_total_recorded_klasses = 0; 1054 _num_total_verifications = 0; 1055 1056 // For each class X that has one or more archived fields: 1057 // [1] Dump the subgraph of each archived field 1058 // [2] Create a list of all the class of the objects that can be reached 1059 // by any of these static fields. 1060 // At runtime, these classes are initialized before X's archived fields 1061 // are restored by HeapShared::initialize_from_archived_subgraph(). 1062 int i; 1063 for (i = 0; i < num; ) { 1064 ArchivableStaticFieldInfo* info = &fields[i]; 1065 const char* klass_name = info->klass_name; 1066 start_recording_subgraph(info->klass, klass_name); 1067 1068 // If you have specified consecutive fields of the same klass in 1069 // fields[], these will be archived in the same 1070 // {start_recording_subgraph ... done_recording_subgraph} pass to 1071 // save time. 1072 for (; i < num; i++) { 1073 ArchivableStaticFieldInfo* f = &fields[i]; 1074 if (f->klass_name != klass_name) { 1075 break; 1076 } 1077 1078 if (!info->full_module_graph_only || MetaspaceShared::use_full_module_graph()) { 1079 archive_reachable_objects_from_static_field(f->klass, f->klass_name, 1080 f->offset, f->field_name, 1081 is_closed_archive, CHECK); 1082 } 1083 } 1084 done_recording_subgraph(info->klass, klass_name); 1085 } 1086 1087 log_info(cds, heap)("Archived subgraph records in %s archive heap region = %d", 1088 is_closed_archive ? "closed" : "open", 1089 _num_total_subgraph_recordings); 1090 log_info(cds, heap)(" Walked %d objects", _num_total_walked_objs); 1091 log_info(cds, heap)(" Archived %d objects", _num_total_archived_objs); 1092 log_info(cds, heap)(" Recorded %d klasses", _num_total_recorded_klasses); 1093 1094 #ifndef PRODUCT 1095 for (int i = 0; i < num; i++) { 1096 ArchivableStaticFieldInfo* f = &fields[i]; 1097 verify_subgraph_from_static_field(f->klass, f->offset); 1098 } 1099 log_info(cds, heap)(" Verified %d references", _num_total_verifications); 1100 #endif 1101 } 1102 1103 // At dump-time, find the location of all the non-null oop pointers in an archived heap 1104 // region. This way we can quickly relocate all the pointers without using 1105 // BasicOopIterateClosure at runtime. 1106 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure { 1107 narrowOop* _start; 1108 BitMap *_oopmap; 1109 int _num_total_oops; 1110 int _num_null_oops; 1111 public: 1112 FindEmbeddedNonNullPointers(narrowOop* start, BitMap* oopmap) 1113 : _start(start), _oopmap(oopmap), _num_total_oops(0), _num_null_oops(0) {} 1114 1115 virtual bool should_verify_oops(void) { 1116 return false; 1117 } 1118 virtual void do_oop(narrowOop* p) { 1119 _num_total_oops ++; 1120 narrowOop v = *p; 1121 if (!CompressedOops::is_null(v)) { 1122 size_t idx = p - _start; 1123 _oopmap->set_bit(idx); 1124 } else { 1125 _num_null_oops ++; 1126 } 1127 } 1128 virtual void do_oop(oop *p) { 1129 ShouldNotReachHere(); 1130 } 1131 int num_total_oops() const { return _num_total_oops; } 1132 int num_null_oops() const { return _num_null_oops; } 1133 }; 1134 1135 ResourceBitMap HeapShared::calculate_oopmap(MemRegion region) { 1136 assert(UseCompressedOops, "must be"); 1137 size_t num_bits = region.byte_size() / sizeof(narrowOop); 1138 ResourceBitMap oopmap(num_bits); 1139 1140 HeapWord* p = region.start(); 1141 HeapWord* end = region.end(); 1142 FindEmbeddedNonNullPointers finder((narrowOop*)p, &oopmap); 1143 1144 int num_objs = 0; 1145 while (p < end) { 1146 oop o = (oop)p; 1147 o->oop_iterate(&finder); 1148 p += o->size(); 1149 ++ num_objs; 1150 } 1151 1152 log_info(cds, heap)("calculate_oopmap: objects = %6d, embedded oops = %7d, nulls = %7d", 1153 num_objs, finder.num_total_oops(), finder.num_null_oops()); 1154 return oopmap; 1155 } 1156 1157 // Patch all the embedded oop pointers inside an archived heap region, 1158 // to be consistent with the runtime oop encoding. 1159 class PatchEmbeddedPointers: public BitMapClosure { 1160 narrowOop* _start; 1161 1162 public: 1163 PatchEmbeddedPointers(narrowOop* start) : _start(start) {} 1164 1165 bool do_bit(size_t offset) { 1166 narrowOop* p = _start + offset; 1167 narrowOop v = *p; 1168 assert(!CompressedOops::is_null(v), "null oops should have been filtered out at dump time"); 1169 oop o = HeapShared::decode_from_archive(v); 1170 RawAccess<IS_NOT_NULL>::oop_store(p, o); 1171 return true; 1172 } 1173 }; 1174 1175 void HeapShared::patch_archived_heap_embedded_pointers(MemRegion region, address oopmap, 1176 size_t oopmap_size_in_bits) { 1177 BitMapView bm((BitMap::bm_word_t*)oopmap, oopmap_size_in_bits); 1178 1179 #ifndef PRODUCT 1180 ResourceMark rm; 1181 ResourceBitMap checkBm = calculate_oopmap(region); 1182 assert(bm.is_same(checkBm), "sanity"); 1183 #endif 1184 1185 PatchEmbeddedPointers patcher((narrowOop*)region.start()); 1186 bm.iterate(&patcher); 1187 } 1188 1189 #endif // INCLUDE_CDS_JAVA_HEAP