< prev index next >

src/hotspot/share/memory/heapShared.cpp

Print this page


   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/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(&copy);
 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 




   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   }


 254       BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedOops),
 255       BOOL_TO_STR(UseCompressedClassPointers));
 256     return;
 257   }
 258 
 259   G1HeapVerifier::verify_ready_for_archiving();
 260 
 261   {
 262     NoSafepointVerifier nsv;
 263 
 264     // Cache for recording where the archived objects are copied to
 265     create_archived_object_cache();
 266 
 267     log_info(cds)("Dumping objects to closed archive heap region ...");
 268     NOT_PRODUCT(StringTable::verify());
 269     copy_closed_archive_heap_objects(closed);
 270 
 271     log_info(cds)("Dumping objects to open archive heap region ...");
 272     copy_open_archive_heap_objects(open);
 273 
 274     ClassLoaderDataShared::init_archived_oops();
 275 
 276     destroy_archived_object_cache();
 277   }
 278 
 279   G1HeapVerifier::verify_archive_regions();
 280 }
 281 
 282 void HeapShared::copy_closed_archive_heap_objects(
 283                                     GrowableArray<MemRegion> * closed_archive) {
 284   assert(is_heap_object_archiving_allowed(), "Cannot archive java heap objects");
 285 
 286   Thread* THREAD = Thread::current();
 287   G1CollectedHeap::heap()->begin_archive_alloc_range();
 288 
 289   // Archive interned string objects
 290   StringTable::write_to_archive();
 291 
 292   archive_object_subgraphs(closed_archive_subgraph_entry_fields,
 293                            num_closed_archive_subgraph_entry_fields,
 294                            true /* is_closed_archive */, THREAD);
 295 


 488 
 489   _run_time_subgraph_info_table.reset();
 490 
 491   CompactHashtableWriter writer(d_table->_count, &stats);
 492   CopyKlassSubGraphInfoToArchive copy(&writer);
 493   d_table->iterate(&copy);
 494 
 495   writer.dump(&_run_time_subgraph_info_table, "subgraphs");
 496 }
 497 
 498 void HeapShared::serialize_subgraph_info_table_header(SerializeClosure* soc) {
 499   _run_time_subgraph_info_table.serialize_header(soc);
 500 }
 501 
 502 void HeapShared::initialize_from_archived_subgraph(Klass* k) {
 503   if (!open_archive_heap_region_mapped()) {
 504     return; // nothing to do
 505   }
 506   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
 507 
 508   if (!MetaspaceShared::use_full_module_graph()) {
 509     for (int i = 0; i < num_open_archive_subgraph_entry_fields; i++) {
 510       const ArchivableStaticFieldInfo* info = &open_archive_subgraph_entry_fields[i];
 511       if (info->full_module_graph_only && k->name()->equals(info->klass_name)) {
 512         return;
 513       }
 514     }
 515   }
 516 
 517   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(k);
 518   const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
 519 
 520   // Initialize from archived data. Currently this is done only
 521   // during VM initialization time. No lock is needed.
 522   if (record != NULL) {
 523     Thread* THREAD = Thread::current();
 524 
 525     int i;
 526     // Load/link/initialize the klasses of the objects in the subgraph.
 527     // NULL class loader is used.
 528     Array<Klass*>* klasses = record->subgraph_object_klasses();
 529     if (klasses != NULL) {
 530       for (i = 0; i < klasses->length(); i++) {
 531         Klass* obj_k = klasses->at(i);
 532         Klass* resolved_k = SystemDictionary::resolve_or_null(
 533                                               (obj_k)->name(), THREAD);
 534         if (resolved_k != obj_k) {
 535           assert(!SystemDictionary::is_well_known_klass(resolved_k),
 536                  "shared well-known classes must not be replaced by JVMTI ClassFileLoadHook");


 655   }
 656 };
 657 
 658 void HeapShared::check_closed_archive_heap_region_object(InstanceKlass* k,
 659                                                          Thread* THREAD) {
 660   // Check fields in the object
 661   for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
 662     if (!fs.access_flags().is_static()) {
 663       BasicType ft = fs.field_descriptor().field_type();
 664       if (!fs.access_flags().is_final() && is_reference_type(ft)) {
 665         ResourceMark rm(THREAD);
 666         log_warning(cds, heap)(
 667           "Please check reference field in %s instance in closed archive heap region: %s %s",
 668           k->external_name(), (fs.name())->as_C_string(),
 669           (fs.signature())->as_C_string());
 670       }
 671     }
 672   }
 673 }
 674 
 675 void HeapShared::check_module_oop(oop orig_module_obj) {
 676   assert(DumpSharedSpaces, "must be");
 677   assert(java_lang_Module::is_instance(orig_module_obj), "must be");
 678   ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj);
 679   if (orig_module_ent == NULL) {
 680     // These special Module objects are created in Java code. They are not
 681     // defined via Modules::define_module(), so they don't have a ModuleEntry:
 682     //     java.lang.Module::ALL_UNNAMED_MODULE
 683     //     java.lang.Module::EVERYONE_MODULE
 684     //     jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule
 685     assert(java_lang_Module::name(orig_module_obj) == NULL, "must be unnamed");
 686     log_info(cds, heap)("Module oop with No ModuleEntry* @[" PTR_FORMAT "]", p2i(orig_module_obj));
 687   } else {
 688     ClassLoaderData* loader_data = orig_module_ent->loader_data();
 689     assert(loader_data->is_builtin_class_loader_data(), "must be");
 690   }
 691 }
 692 
 693 
 694 // (1) If orig_obj has not been archived yet, archive it.
 695 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
 696 //     trace all  objects that are reachable from it, and make sure these objects are archived.
 697 // (3) Record the klasses of all orig_obj and all reachable objects.
 698 oop HeapShared::archive_reachable_objects_from(int level,
 699                                                KlassSubGraphInfo* subgraph_info,
 700                                                oop orig_obj,
 701                                                bool is_closed_archive,
 702                                                TRAPS) {
 703   assert(orig_obj != NULL, "must be");
 704   assert(!is_archived_object(orig_obj), "sanity");
 705 
 706   if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
 707     // This object has injected fields that cannot be supported easily, so we disallow them for now.
 708     // If you get an error here, you probably made a change in the JDK library that has added
 709     // these objects that are referenced (directly or indirectly) by static fields.
 710     ResourceMark rm;
 711     log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
 712     vm_exit(1);
 713   }


 741     ++_num_new_archived_objs;
 742     archived_obj = archive_heap_object(orig_obj, THREAD);
 743     if (archived_obj == NULL) {
 744       // Skip archiving the sub-graph referenced from the current entry field.
 745       ResourceMark rm;
 746       log_error(cds, heap)(
 747         "Cannot archive the sub-graph referenced from %s object ("
 748         PTR_FORMAT ") size %d, skipped.",
 749         orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
 750       if (level == 1) {
 751         // Don't archive a subgraph root that's too big. For archives static fields, that's OK
 752         // as the Java code will take care of initializing this field dynamically.
 753         return NULL;
 754       } else {
 755         // We don't know how to handle an object that has been archived, but some of its reachable
 756         // objects cannot be archived. Bail out for now. We might need to fix this in the future if
 757         // we have a real use case.
 758         vm_exit(1);
 759       }
 760     }
 761 
 762     if (java_lang_Module::is_instance(orig_obj)) {
 763       check_module_oop(orig_obj);
 764       java_lang_Module::set_module_entry(archived_obj, NULL);
 765       java_lang_Module::set_loader(archived_obj, NULL);
 766     } else if (java_lang_ClassLoader::is_instance(orig_obj)) {
 767       // class_data will be restored explicitly at run time. 
 768       guarantee(orig_obj == SystemDictionary::java_platform_loader() ||
 769                 orig_obj == SystemDictionary::java_system_loader() ||
 770                 java_lang_ClassLoader::loader_data_raw(orig_obj) == NULL, "must be");
 771       java_lang_ClassLoader::release_set_loader_data(archived_obj, NULL);
 772     }
 773   }
 774 
 775   assert(archived_obj != NULL, "must be");
 776   Klass *orig_k = orig_obj->klass();
 777   Klass *relocated_k = archived_obj->klass();
 778   subgraph_info->add_subgraph_object_klass(orig_k, relocated_k);
 779 
 780   WalkOopAndArchiveClosure walker(level, is_closed_archive, record_klasses_only,
 781                                   subgraph_info, orig_obj, archived_obj, THREAD);
 782   orig_obj->oop_iterate(&walker);
 783   if (is_closed_archive && orig_k->is_instance_klass()) {
 784     check_closed_archive_heap_region_object(InstanceKlass::cast(orig_k), THREAD);
 785   }
 786   return archived_obj;
 787 }
 788 
 789 //
 790 // Start from the given static field in a java mirror and archive the
 791 // complete sub-graph of java heap objects that are reached directly
 792 // or indirectly from the starting object by following references.


1049   // [1] Dump the subgraph of each archived field
1050   // [2] Create a list of all the class of the objects that can be reached
1051   //     by any of these static fields.
1052   //     At runtime, these classes are initialized before X's archived fields
1053   //     are restored by HeapShared::initialize_from_archived_subgraph().
1054   int i;
1055   for (i = 0; i < num; ) {
1056     ArchivableStaticFieldInfo* info = &fields[i];
1057     const char* klass_name = info->klass_name;
1058     start_recording_subgraph(info->klass, klass_name);
1059 
1060     // If you have specified consecutive fields of the same klass in
1061     // fields[], these will be archived in the same
1062     // {start_recording_subgraph ... done_recording_subgraph} pass to
1063     // save time.
1064     for (; i < num; i++) {
1065       ArchivableStaticFieldInfo* f = &fields[i];
1066       if (f->klass_name != klass_name) {
1067         break;
1068       }
1069 
1070       if (!info->full_module_graph_only || MetaspaceShared::use_full_module_graph()) {
1071         archive_reachable_objects_from_static_field(f->klass, f->klass_name,
1072                                                     f->offset, f->field_name,
1073                                                     is_closed_archive, CHECK);
1074       }
1075     }
1076     done_recording_subgraph(info->klass, klass_name);
1077   }
1078 
1079   log_info(cds, heap)("Archived subgraph records in %s archive heap region = %d",
1080                       is_closed_archive ? "closed" : "open",
1081                       _num_total_subgraph_recordings);
1082   log_info(cds, heap)("  Walked %d objects", _num_total_walked_objs);
1083   log_info(cds, heap)("  Archived %d objects", _num_total_archived_objs);
1084   log_info(cds, heap)("  Recorded %d klasses", _num_total_recorded_klasses);
1085 
1086 #ifndef PRODUCT
1087   for (int i = 0; i < num; i++) {
1088     ArchivableStaticFieldInfo* f = &fields[i];
1089     verify_subgraph_from_static_field(f->klass, f->offset);
1090   }
1091   log_info(cds, heap)("  Verified %d references", _num_total_verifications);
1092 #endif
1093 }
1094 


< prev index next >