< prev index next >

src/hotspot/share/cds/metaspaceShared.cpp

Print this page
@@ -21,25 +21,28 @@
   * questions.
   *
   */
  
  #include "precompiled.hpp"
+ #include "cds/aotClassInitializer.hpp"
  #include "cds/aotClassLinker.hpp"
  #include "cds/aotConstantPoolResolver.hpp"
  #include "cds/aotLinkedClassBulkLoader.hpp"
  #include "cds/archiveBuilder.hpp"
  #include "cds/archiveHeapLoader.hpp"
  #include "cds/archiveHeapWriter.hpp"
  #include "cds/cds_globals.hpp"
+ #include "cds/cdsAccess.hpp"
  #include "cds/cdsConfig.hpp"
  #include "cds/cdsProtectionDomain.hpp"
  #include "cds/classListParser.hpp"
  #include "cds/classListWriter.hpp"
  #include "cds/cppVtables.hpp"
  #include "cds/dumpAllocStats.hpp"
  #include "cds/dynamicArchive.hpp"
  #include "cds/filemap.hpp"
+ #include "cds/finalImageRecipes.hpp"
  #include "cds/heapShared.hpp"
  #include "cds/lambdaFormInvokers.hpp"
  #include "cds/metaspaceShared.hpp"
  #include "classfile/classLoaderDataGraph.hpp"
  #include "classfile/classLoaderDataShared.hpp"

@@ -53,10 +56,13 @@
  #include "classfile/systemDictionary.hpp"
  #include "classfile/systemDictionaryShared.hpp"
  #include "classfile/vmClasses.hpp"
  #include "classfile/vmSymbols.hpp"
  #include "code/codeCache.hpp"
+ #include "code/SCCache.hpp"
+ #include "compiler/compileBroker.hpp"
+ #include "compiler/precompiler.hpp"
  #include "gc/shared/gcVMOperations.hpp"
  #include "interpreter/bytecodeStream.hpp"
  #include "interpreter/bytecodes.hpp"
  #include "jvm_io.h"
  #include "logging/log.hpp"

@@ -68,14 +74,17 @@
  #include "memory/universe.hpp"
  #include "nmt/memTracker.hpp"
  #include "oops/compressedKlass.hpp"
  #include "oops/instanceMirrorKlass.hpp"
  #include "oops/klass.inline.hpp"
+ #include "oops/method.inline.hpp"
  #include "oops/objArrayOop.hpp"
  #include "oops/oop.inline.hpp"
  #include "oops/oopHandle.hpp"
+ #include "oops/trainingData.hpp"
  #include "prims/jvmtiExport.hpp"
+ #include "prims/whitebox.hpp"
  #include "runtime/arguments.hpp"
  #include "runtime/globals.hpp"
  #include "runtime/globals_extension.hpp"
  #include "runtime/handles.inline.hpp"
  #include "runtime/javaCalls.hpp"

@@ -99,10 +108,11 @@
  void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
  intx MetaspaceShared::_relocation_delta;
  char* MetaspaceShared::_requested_base_address;
  Array<Method*>* MetaspaceShared::_archived_method_handle_intrinsics = nullptr;
  bool MetaspaceShared::_use_optimized_module_handling = true;
+ int volatile MetaspaceShared::_preimage_static_archive_dumped = 0;
  
  // The CDS archive is divided into the following regions:
  //     rw  - read-write metadata
  //     ro  - read-only metadata and read-only tables
  //     hp  - heap region

@@ -260,10 +270,18 @@
    return aligned_base;
  }
  
  void MetaspaceShared::initialize_for_static_dump() {
    assert(CDSConfig::is_dumping_static_archive(), "sanity");
+ 
+   if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
+     if (!((UseG1GC || UseParallelGC || UseSerialGC || UseEpsilonGC || UseShenandoahGC) && UseCompressedClassPointers)) {
+       vm_exit_during_initialization("Cannot create the CacheDataStore",
+                                     "UseCompressedClassPointers must be enabled, and collector must be G1, Parallel, Serial, Epsilon, or Shenandoah");
+     }
+   }
+ 
    log_info(cds)("Core region alignment: " SIZE_FORMAT, core_region_alignment());
    // The max allowed size for CDS archive. We use this to limit SharedBaseAddress
    // to avoid address space wrap around.
    size_t cds_max;
    const size_t reserve_alignment = core_region_alignment();

@@ -460,10 +478,12 @@
    SymbolTable::serialize_shared_table_header(soc);
    StringTable::serialize_shared_table_header(soc);
    HeapShared::serialize_tables(soc);
    SystemDictionaryShared::serialize_dictionary_headers(soc);
    AOTLinkedClassBulkLoader::serialize(soc, true);
+   FinalImageRecipes::serialize(soc, true);
+   TrainingData::serialize_training_data(soc);
    InstanceMirrorKlass::serialize_offsets(soc);
  
    // Dump/restore well known classes (pointers)
    SystemDictionaryShared::serialize_vm_classes(soc);
    soc->do_tag(--tag);

@@ -544,10 +564,11 @@
    virtual void iterate_roots(MetaspaceClosure* it) {
      FileMapInfo::metaspace_pointers_do(it);
      SystemDictionaryShared::dumptime_classes_do(it);
      Universe::metaspace_pointers_do(it);
      vmSymbols::metaspace_pointers_do(it);
+     TrainingData::iterate_roots(it);
  
      // The above code should find all the symbols that are referenced by the
      // archived classes. We just need to add the extra symbols which
      // may not be used by any of the archived classes -- these are usually
      // symbols that we anticipate to be used at run time, so we can store

@@ -582,10 +603,15 @@
  char* VM_PopulateDumpSharedSpace::dump_read_only_tables() {
    ArchiveBuilder::OtherROAllocMark mark;
  
    SystemDictionaryShared::write_to_archive();
    AOTClassLinker::write_to_archive();
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+     FinalImageRecipes::record_recipes();
+   }
+   AOTLinkedClassBulkLoader::record_unregistered_classes();
+   TrainingData::dump_training_data();
    MetaspaceShared::write_method_handle_intrinsics();
  
    // Write lambform lines into archive
    LambdaFormInvokers::dump_static_archive_invokers();
  

@@ -597,11 +623,11 @@
  
    return start;
  }
  
  void VM_PopulateDumpSharedSpace::doit() {
-   guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
+   //guarantee(!CDSConfig::is_using_archive(), "We should not be using an archive when we dump");
  
    DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
  
    _pending_method_handle_intrinsics = new (mtClassShared) GrowableArray<Method*>(256, mtClassShared);
    if (CDSConfig::is_dumping_aot_linked_classes()) {

@@ -638,16 +664,26 @@
    char* early_serialized_data = dump_early_read_only_tables();
    char* serialized_data = dump_read_only_tables();
  
    SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
  
+   log_info(cds)("Make training data shareable");
+   _builder.make_training_data_shareable();
+ 
    // The vtable clones contain addresses of the current process.
    // We don't want to write these addresses into the archive.
    CppVtables::zero_archived_vtables();
  
    // Write the archive file
-   const char* static_archive = CDSConfig::static_archive_path();
+   const char* static_archive;
+   if (CDSConfig::is_dumping_final_static_archive()) {
+     static_archive = CacheDataStore;
+     assert(FileMapInfo::current_info() != nullptr, "sanity");
+     delete FileMapInfo::current_info();
+   } else {
+     static_archive = CDSConfig::static_archive_path();
+   }
    assert(static_archive != nullptr, "SharedArchiveFile not set?");
    _map_info = new FileMapInfo(static_archive, true);
    _map_info->populate_header(MetaspaceShared::core_region_alignment());
    _map_info->set_early_serialized_data(early_serialized_data);
    _map_info->set_serialized_data(serialized_data);

@@ -676,16 +712,20 @@
  };
  
  // Check if we can eagerly link this class at dump time, so we can avoid the
  // runtime linking overhead (especially verification)
  bool MetaspaceShared::may_be_eagerly_linked(InstanceKlass* ik) {
+   if (CDSConfig::preserve_all_dumptime_verification_states(ik)) {
+     assert(ik->can_be_verified_at_dumptime(), "sanity");
+   }
    if (!ik->can_be_verified_at_dumptime()) {
      // For old classes, try to leave them in the unlinked state, so
      // we can still store them in the archive. They must be
      // linked/verified at runtime.
      return false;
    }
+ 
    if (CDSConfig::is_dumping_dynamic_archive() && ik->is_shared_unregistered_class()) {
      // Linking of unregistered classes at this stage may cause more
      // classes to be resolved, resulting in calls to ClassLoader.loadClass()
      // that may not be expected by custom class loaders.
      //

@@ -694,23 +734,21 @@
      return false;
    }
    return true;
  }
  
- bool MetaspaceShared::link_class_for_cds(InstanceKlass* ik, TRAPS) {
-   // Link the class to cause the bytecodes to be rewritten and the
-   // cpcache to be created. Class verification is done according
-   // to -Xverify setting.
-   bool res = MetaspaceShared::try_link_class(THREAD, ik);
-   AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK_(false));
-   return res;
- }
  
  void MetaspaceShared::link_shared_classes(bool jcmd_request, TRAPS) {
    AOTClassLinker::initialize();
  
-   if (!jcmd_request) {
+   if (!jcmd_request && !CDSConfig::is_dumping_dynamic_archive()
+       && !CDSConfig::is_dumping_preimage_static_archive()
+       && !CDSConfig::is_dumping_final_static_archive()) {
+     // If we have regenerated invoker classes in the dynamic archive,
+     // they will conflict with the resolved CONSTANT_Klass references that are stored
+     // in the static archive. This is not easy to handle. Let's disable
+     // it for dynamic archive for now.
      LambdaFormInvokers::regenerate_holder_classes(CHECK);
    }
  
    // Collect all loaded ClassLoaderData.
    CollectCLDClosure collect_cld(THREAD);

@@ -729,11 +767,14 @@
        ClassLoaderData* cld = collect_cld.cld_at(i);
        for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
          if (klass->is_instance_klass()) {
            InstanceKlass* ik = InstanceKlass::cast(klass);
            if (may_be_eagerly_linked(ik)) {
-             has_linked |= link_class_for_cds(ik, CHECK);
+             has_linked |= try_link_class(THREAD, ik);
+           }
+           if (CDSConfig::is_dumping_heap() && ik->is_linked() && !ik->is_initialized()) {
+             AOTClassInitializer::maybe_preinit_class(ik, CHECK);
            }
          }
        }
      }
  

@@ -741,10 +782,38 @@
        break;
      }
      // Class linking includes verification which may load more classes.
      // Keep scanning until we have linked no more classes.
    }
+ 
+   // Resolve constant pool entries -- we don't load any new classes during this stage
+   for (int i = 0; i < collect_cld.nof_cld(); i++) {
+     ClassLoaderData* cld = collect_cld.cld_at(i);
+     for (Klass* klass = cld->klasses(); klass != nullptr; klass = klass->next_link()) {
+       if (klass->is_instance_klass()) {
+         InstanceKlass* ik = InstanceKlass::cast(klass);
+         AOTConstantPoolResolver::dumptime_resolve_constants(ik, CHECK);
+         if (CDSConfig::is_dumping_preimage_static_archive()) {
+           FinalImageRecipes::add_reflection_data_flags(ik, CHECK);
+         }
+       }
+     }
+   }
+ 
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+     // Do this after all classes are verified by the above loop.
+     // Any classes loaded from here on will be automatically excluded, so
+     // there's no need to force verification or resolve CP entries.
+     RecordTraining = false;
+     SystemDictionaryShared::ignore_new_classes();
+     LambdaFormInvokers::regenerate_holder_classes(CHECK);
+     RecordTraining = true;
+   }
+ 
+   if (CDSConfig::is_dumping_final_static_archive()) {
+     FinalImageRecipes::apply_recipes(CHECK);
+   }
  }
  
  void MetaspaceShared::prepare_for_dumping() {
    assert(CDSConfig::is_dumping_archive(), "sanity");
    CDSConfig::check_unsupported_dumping_module_options();

@@ -754,10 +823,17 @@
  // Preload classes from a list, populate the shared spaces and dump to a
  // file.
  void MetaspaceShared::preload_and_dump(TRAPS) {
    CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
    ResourceMark rm(THREAD);
+   HandleMark hm(THREAD);
+ 
+   if (CDSConfig::is_dumping_final_static_archive() && PrintTrainingInfo) {
+     tty->print_cr("==================== archived_training_data ** before dumping ====================");
+     TrainingData::print_archived_training_data_on(tty);
+   }
+ 
    StaticArchiveBuilder builder;
    preload_and_dump_impl(builder, THREAD);
    if (HAS_PENDING_EXCEPTION) {
      if (PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())) {
        log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "

@@ -768,11 +844,11 @@
                       java_lang_String::as_utf8_string(java_lang_Throwable::message(PENDING_EXCEPTION)));
        MetaspaceShared::writing_error("Unexpected exception, use -Xlog:cds,exceptions=trace for detail");
      }
    }
  
-   if (!CDSConfig::old_cds_flags_used()) {
+   if (!CDSConfig::old_cds_flags_used() && !CDSConfig::is_dumping_preimage_static_archive() && !CDSConfig::is_dumping_final_static_archive()) {
      // The JLI launcher only recognizes the "old" -Xshare:dump flag.
      // When the new -XX:AOTMode=create flag is used, we can't return
      // to the JLI launcher, as the launcher will fail when trying to
      // run the main class, which is not what we want.
      tty->print_cr("AOTCache creation is complete: %s", AOTCache);

@@ -873,17 +949,44 @@
  
    // Exercise FileSystem and URL code
    CDSProtectionDomain::to_file_URL("dummy.jar", Handle(), CHECK);
  }
  
+ bool MetaspaceShared::is_recording_preimage_static_archive() {
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+       return _preimage_static_archive_dumped == 0;
+   }
+   return false;
+ }
+ 
  void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
-   preload_classes(CHECK);
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+     if (Atomic::cmpxchg(&_preimage_static_archive_dumped, 0, 1) != 0) {
+       return;
+     }
+   }
+ 
+   if (CDSConfig::is_dumping_classic_static_archive()) {
+     // We are running with -Xshare:dump
+     preload_classes(CHECK);
  
-   if (SharedArchiveConfigFile) {
-     log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
-     read_extra_data(THREAD, SharedArchiveConfigFile);
-     log_info(cds)("Reading extra data: done.");
+     if (SharedArchiveConfigFile) {
+       log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
+       read_extra_data(THREAD, SharedArchiveConfigFile);
+       log_info(cds)("Reading extra data: done.");
+     }
+   }
+ 
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+     log_info(cds)("Reading lambda form invokers of in JDK default classlist ...");
+     char default_classlist[JVM_MAXPATHLEN];
+     get_default_classlist(default_classlist, sizeof(default_classlist));
+     struct stat statbuf;
+     if (os::stat(default_classlist, &statbuf) == 0) {
+       ClassListParser::parse_classlist(default_classlist,
+                                        ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
+     }
    }
  
    // Rewrite and link classes
    log_info(cds)("Rewriting and linking classes ...");
  

@@ -892,10 +995,17 @@
    // fails verification, all other interfaces that were not specified in the classlist but
    // are implemented by K are not verified.
    link_shared_classes(false/*not from jcmd*/, CHECK);
    log_info(cds)("Rewriting and linking classes: done");
  
+   if (CDSConfig::is_dumping_final_static_archive()) {
+     assert(RecordTraining == false, "must be");
+     RecordTraining = true;
+   }
+ 
+   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
+ 
  #if INCLUDE_CDS_JAVA_HEAP
    if (CDSConfig::is_dumping_heap()) {
      if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
        log_info(cds)("archivedBootLayer not available, disabling full module graph");
        CDSConfig::stop_dumping_full_module_graph();

@@ -904,10 +1014,14 @@
      ArchiveHeapWriter::init();
      if (CDSConfig::is_dumping_full_module_graph()) {
        HeapShared::reset_archived_object_states(CHECK);
      }
  
+     if (ArchiveLoaderLookupCache) {
+       SystemDictionaryShared::create_loader_positive_lookup_cache(CHECK);
+     }
+ 
      if (CDSConfig::is_dumping_invokedynamic()) {
        // This assert means that the MethodType and MethodTypeForm tables won't be
        // updated concurrently when we are saving their contents into a side table.
        assert(CDSConfig::allow_only_single_java_thread(), "Required");
  

@@ -938,12 +1052,46 @@
    }
  #endif
  
    VM_PopulateDumpSharedSpace op(builder);
    VMThread::execute(&op);
+   FileMapInfo* mapinfo = op.map_info();
+   ArchiveHeapInfo* heap_info = op.heap_info();
+   bool status;
+   if (CDSConfig::is_dumping_preimage_static_archive()) {
+     if ((status = write_static_archive(&builder, mapinfo, heap_info))) {
+       fork_and_dump_final_static_archive();
+     }
+   } else if (CDSConfig::is_dumping_final_static_archive()) {
+     RecordTraining = false;
+     if (StoreCachedCode && CachedCodeFile != nullptr) { // FIXME: new workflow -- remove the CachedCodeFile flag
+       if (log_is_enabled(Info, cds, jit)) {
+         CDSAccess::test_heap_access_api();
+       }
+ 
+       // We have just created the final image. Let's run the AOT compiler
+       if (PrintTrainingInfo) {
+         tty->print_cr("==================== archived_training_data ** after dumping ====================");
+         TrainingData::print_archived_training_data_on(tty);
+       }
+ 
+       CDSConfig::enable_dumping_cached_code();
+       {
+         builder.start_cc_region();
+         Precompiler::compile_cached_code(&builder, CHECK);
+         builder.end_cc_region();
+       }
+       CDSConfig::disable_dumping_cached_code();
+ 
+       SCCache::close(); // Write final data and close archive
+     }
+     status = write_static_archive(&builder, mapinfo, heap_info);
+   } else {
+     status = write_static_archive(&builder, mapinfo, heap_info);
+   }
  
-   if (!write_static_archive(&builder, op.map_info(), op.heap_info())) {
+   if (!status) {
      THROW_MSG(vmSymbols::java_io_IOException(), "Encountered error while dumping");
    }
  }
  
  bool MetaspaceShared::write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info) {

@@ -962,16 +1110,106 @@
              "for testing purposes only and should not be used in a production environment");
    }
    return true;
  }
  
+ static void print_java_launcher(outputStream* st) {
+   st->print("%s%sbin%sjava", Arguments::get_java_home(), os::file_separator(), os::file_separator());
+ }
+ 
+ static void print_vm_arguments(outputStream* st) {
+   const char* cp = Arguments::get_appclasspath();
+   if (cp != nullptr && strlen(cp) > 0 && strcmp(cp, ".") != 0) {
+     st->print(" -cp ");  st->print_raw(cp);
+   }
+   for (int i = 0; i < Arguments::num_jvm_flags(); i++) {
+     st->print(" %s", Arguments::jvm_flags_array()[i]);
+   }
+   for (int i = 0; i < Arguments::num_jvm_args(); i++) {
+     st->print(" %s", Arguments::jvm_args_array()[i]);
+   }
+ }
+ 
+ void MetaspaceShared::fork_and_dump_final_static_archive() {
+   assert(CDSConfig::is_dumping_preimage_static_archive(), "sanity");
+ 
+   ResourceMark rm;
+   stringStream ss;
+   print_java_launcher(&ss);
+   print_vm_arguments(&ss);
+   ss.print(" -XX:CDSPreimage=%s", SharedArchiveFile);
+ 
+   const char* cmd = ss.freeze();
+   if (CDSManualFinalImage) {
+     tty->print_cr("-XX:+CDSManualFinalImage is specified");
+     tty->print_cr("Please manually execute the following command to create the final CDS image:");
+     tty->print("    "); tty->print_raw_cr(cmd);
+ 
+     // The following is useful if the dumping was trigger by a script that builds
+     // a complex command-line.
+     tty->print_cr("Note: to recreate the preimage only:");
+     tty->print_cr("    rm -f %s", CacheDataStore);
+     tty->print("    ");
+     print_java_launcher(tty);
+     print_vm_arguments(tty);
+     if (Arguments::java_command() != nullptr) {
+       tty->print(" %s", Arguments::java_command());
+     }
+     tty->cr();
+   } else {
+     // FIXME: space characters are not properly quoated. E.g.,
+     //      java -Dfoo='a b c' HelloWorld
+     log_info(cds)("Launching child process to create final CDS image:");
+     log_info(cds)("    %s", cmd);
+     int status = os::fork_and_exec(cmd);
+     if (status != 0) {
+       log_error(cds)("Child process finished; status = %d", status);
+       log_error(cds)("To reproduce the error");
+       ResourceMark rm;
+       LogStream ls(Log(cds)::error());
+       ls.print("    "); ls.print_raw_cr(cmd);
+ 
+       // The following is useful if the dumping was trigger by a script that builds
+       // a complex command-line.
+       ls.print_cr("Note: to recreate the preimage only:");
+       ls.print_cr("    rm -f %s", CacheDataStore);
+       ls.print("    ");
+       print_java_launcher(&ls);
+       print_vm_arguments(&ls);
+       ls.print(" -XX:+UnlockDiagnosticVMOptions -XX:+CDSManualFinalImage");
+       if (Arguments::java_command() != nullptr) {
+         ls.print(" %s", Arguments::java_command());
+       }
+       ls.cr();
+ 
+       vm_direct_exit(status);
+     } else {
+       log_info(cds)("Child process finished; status = %d", status);
+       // On Windows, need WRITE permission to remove the file.
+       WINDOWS_ONLY(chmod(SharedArchiveFile, _S_IREAD | _S_IWRITE));
+       status = remove(SharedArchiveFile);
+       if (status != 0) {
+         log_error(cds)("Failed to remove CDSPreimage file %s", SharedArchiveFile);
+       } else {
+         log_info(cds)("Removed CDSPreimage file %s", SharedArchiveFile);
+       }
+     }
+   }
+ }
+ 
  // Returns true if the class's status has changed.
  bool MetaspaceShared::try_link_class(JavaThread* current, InstanceKlass* ik) {
    ExceptionMark em(current);
    JavaThread* THREAD = current; // For exception macros.
    assert(CDSConfig::is_dumping_archive(), "sanity");
-   if (!ik->is_shared() && ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
+ 
+   if (ik->is_shared() && !CDSConfig::is_dumping_final_static_archive()) {
+     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
+     return false;
+   }
+ 
+   if (ik->is_loaded() && !ik->is_linked() && ik->can_be_verified_at_dumptime() &&
        !SystemDictionaryShared::has_class_failed_verification(ik)) {
      bool saved = BytecodeVerificationLocal;
      if (ik->is_shared_unregistered_class() && ik->class_loader() == nullptr) {
        // The verification decision is based on BytecodeVerificationRemote
        // for non-system classes. Since we are using the null classloader

@@ -999,15 +1237,17 @@
  }
  
  #if INCLUDE_CDS_JAVA_HEAP
  void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
    if(!HeapShared::can_write()) {
-     log_info(cds)(
+     if (!CDSConfig::is_dumping_preimage_static_archive()) {
+      log_info(cds)(
        "Archived java heap is not supported as UseG1GC "
        "and UseCompressedClassPointers are required."
        "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
        BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
+     }
      return;
    }
    // Find all the interned strings that should be dumped.
    int i;
    for (i = 0; i < klasses->length(); i++) {

@@ -1086,11 +1326,11 @@
  
  void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() {
    assert(CDSConfig::is_using_archive(), "Must be called when UseSharedSpaces is enabled");
    MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE;
  
-   FileMapInfo* static_mapinfo = open_static_archive();
+   FileMapInfo* static_mapinfo = FileMapInfo::current_info();
    FileMapInfo* dynamic_mapinfo = nullptr;
  
    if (static_mapinfo != nullptr) {
      log_info(cds)("Core region alignment: " SIZE_FORMAT, static_mapinfo->core_region_alignment());
      dynamic_mapinfo = open_dynamic_archive();

@@ -1133,10 +1373,14 @@
      log_info(cds)("Unable to map shared spaces");
      if (PrintSharedArchiveAndExit) {
        MetaspaceShared::unrecoverable_loading_error("Unable to use shared archive.");
      } else if (RequireSharedSpaces) {
        MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
+     } else if (CDSConfig::is_dumping_final_static_archive()) {
+       assert(CDSPreimage != nullptr, "must be");
+       log_error(cds)("Unable to map shared spaces for CDSPreimage = %s", CDSPreimage);
+       MetaspaceShared::unrecoverable_loading_error();
      }
    }
  
    // If mapping failed and -XShare:on, the vm should exit
    bool has_failed = false;

@@ -1147,23 +1391,30 @@
    if (dynamic_mapinfo != nullptr && !dynamic_mapinfo->is_mapped()) {
      has_failed = true;
      delete dynamic_mapinfo;
    }
    if (RequireSharedSpaces && has_failed) {
+     // static archive mapped but dynamic archive failed
        MetaspaceShared::unrecoverable_loading_error("Unable to map shared spaces");
    }
  }
  
- FileMapInfo* MetaspaceShared::open_static_archive() {
+ // This is called very early at VM start up to get the size of the cached_code region, which
+ // is used in CodeCache::initialize_heaps()
+ void MetaspaceShared::open_static_archive() {
+   if (!UseSharedSpaces) {
+     return;
+   }
    const char* static_archive = CDSConfig::static_archive_path();
    assert(static_archive != nullptr, "sanity");
    FileMapInfo* mapinfo = new FileMapInfo(static_archive, true);
    if (!mapinfo->initialize()) {
      delete(mapinfo);
-     return nullptr;
+   } else {
+     FileMapRegion* r = mapinfo->region_at(MetaspaceShared::cc);
+     CDSAccess::set_cached_code_size(r->used_aligned());
    }
-   return mapinfo;
  }
  
  FileMapInfo* MetaspaceShared::open_dynamic_archive() {
    if (CDSConfig::is_dumping_dynamic_archive()) {
      return nullptr;

@@ -1584,10 +1835,14 @@
    assert(CDSConfig::is_using_archive(), "must be runtime");
    if (mapinfo == nullptr) {
      return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded.
    }
  
+   if (!mapinfo->validate_aot_class_linking()) {
+     return MAP_ARCHIVE_OTHER_FAILURE;
+   }
+ 
    mapinfo->set_is_mapped(false);
    if (mapinfo->core_region_alignment() != (size_t)core_region_alignment()) {
      log_info(cds)("Unable to map CDS archive -- core_region_alignment() expected: " SIZE_FORMAT
                    " actual: " SIZE_FORMAT, mapinfo->core_region_alignment(), core_region_alignment());
      return MAP_ARCHIVE_OTHER_FAILURE;

@@ -1661,10 +1916,11 @@
    // Finish up archived heap initialization. These must be
    // done after ReadClosure.
    static_mapinfo->patch_heap_embedded_pointers();
    ArchiveHeapLoader::finish_initialization();
    Universe::load_archived_object_instances();
+   SCCache::new_workflow_load_cache();
  
    // Close the mapinfo file
    static_mapinfo->close();
  
    static_mapinfo->unmap_region(MetaspaceShared::bm);

@@ -1711,10 +1967,16 @@
      if (dynamic_mapinfo != nullptr) {
        tty->print_cr("\n\nDynamic archive name: %s", dynamic_mapinfo->full_path());
        tty->print_cr("Dynamic archive version %d", dynamic_mapinfo->version());
        SystemDictionaryShared::print_shared_archive(tty, false/*dynamic*/);
      }
+     TrainingData::print_archived_training_data_on(tty);
+ 
+     if (LoadCachedCode) {
+       tty->print_cr("\n\nCached Code file: %s", CachedCodeFile);
+       SCCache::print_on(tty);
+     }
  
      // collect shared symbols and strings
      CountSharedSymbols cl;
      SymbolTable::shared_symbols_do(&cl);
      tty->print_cr("Number of shared symbols: %d", cl.total());
< prev index next >