< prev index next >

src/hotspot/share/cds/cdsConfig.cpp

Print this page

   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 "cds/aotLogging.hpp"
  26 #include "cds/archiveHeapLoader.hpp"
  27 #include "cds/cdsConfig.hpp"

  28 #include "cds/classListWriter.hpp"
  29 #include "cds/filemap.hpp"
  30 #include "cds/heapShared.hpp"

  31 #include "classfile/classLoaderDataShared.hpp"
  32 #include "classfile/moduleEntry.hpp"



  33 #include "include/jvm_io.h"
  34 #include "logging/log.hpp"

  35 #include "memory/universe.hpp"
  36 #include "prims/jvmtiAgentList.hpp"
  37 #include "runtime/arguments.hpp"
  38 #include "runtime/globals_extension.hpp"
  39 #include "runtime/java.hpp"
  40 #include "runtime/vmThread.hpp"
  41 #include "utilities/defaultStream.hpp"
  42 #include "utilities/formatBuffer.hpp"
  43 
  44 bool CDSConfig::_is_dumping_static_archive = false;
  45 bool CDSConfig::_is_dumping_preimage_static_archive = false;
  46 bool CDSConfig::_is_dumping_final_static_archive = false;
  47 bool CDSConfig::_is_dumping_dynamic_archive = false;
  48 bool CDSConfig::_is_using_optimized_module_handling = true;
  49 bool CDSConfig::_is_dumping_full_module_graph = true;
  50 bool CDSConfig::_is_using_full_module_graph = true;
  51 bool CDSConfig::_has_aot_linked_classes = false;
  52 bool CDSConfig::_is_single_command_training = false;
  53 bool CDSConfig::_has_temp_aot_config_file = false;



  54 bool CDSConfig::_old_cds_flags_used = false;
  55 bool CDSConfig::_new_aot_flags_used = false;
  56 bool CDSConfig::_disable_heap_dumping = false;
  57 
  58 const char* CDSConfig::_default_archive_path = nullptr;
  59 const char* CDSConfig::_input_static_archive_path = nullptr;
  60 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
  61 const char* CDSConfig::_output_archive_path = nullptr;
  62 
  63 JavaThread* CDSConfig::_dumper_thread = nullptr;
  64 
  65 int CDSConfig::get_status() {
  66   assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
  67   return (is_dumping_archive()              ? IS_DUMPING_ARCHIVE : 0) |
  68          (is_dumping_method_handles()       ? IS_DUMPING_METHOD_HANDLES : 0) |
  69          (is_dumping_static_archive()       ? IS_DUMPING_STATIC_ARCHIVE : 0) |
  70          (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
  71          (is_using_archive()                ? IS_USING_ARCHIVE : 0);




  72 }
  73 
  74 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
  75 
  76 void CDSConfig::ergo_initialize() {
  77   DEBUG_ONLY(_cds_ergo_initialize_started = true);
  78 
  79   if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
  80     // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
  81     // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
  82     // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
  83     // So we can never come to here with RequireSharedSpaces==true.
  84     assert(!RequireSharedSpaces, "sanity");
  85 
  86     // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
  87     // for sanity, parse all classes from classfiles.
  88     // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
  89     // needs to be changed.
  90     UseSharedSpaces = false;
  91   }
  92 
  93   // Initialize shared archive paths which could include both base and dynamic archive paths
  94   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
  95   if (is_dumping_static_archive() || is_using_archive()) {
  96     if (new_aot_flags_used()) {
  97       ergo_init_aot_paths();
  98     } else {
  99       ergo_init_classic_archive_paths();
 100     }
 101   }
 102 
 103   if (!is_dumping_heap()) {
 104     _is_dumping_full_module_graph = false;
 105   }




















 106 }
 107 
 108 const char* CDSConfig::default_archive_path() {
 109   // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
 110   // before CDSConfig::ergo_initialize() is called.
 111   assert(_cds_ergo_initialize_started, "sanity");
 112   if (_default_archive_path == nullptr) {
 113     char jvm_path[JVM_MAXPATHLEN];
 114     os::jvm_path(jvm_path, sizeof(jvm_path));
 115     char *end = strrchr(jvm_path, *os::file_separator());
 116     if (end != nullptr) *end = '\0';
 117     stringStream tmp;
 118     tmp.print("%s%sclasses", jvm_path, os::file_separator());
 119 #ifdef _LP64
 120     if (!UseCompressedOops) {
 121       tmp.print_raw("_nocoops");
 122     }
 123     if (UseCompactObjectHeaders) {
 124       // Note that generation of xxx_coh.jsa variants require
 125       // --enable-cds-archive-coh at build time

 263         // Check for case (c)
 264         if (RecordDynamicDumpInfo) {
 265           vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 266                                         SharedArchiveFile);
 267         }
 268         if (ArchiveClassesAtExit != nullptr) {
 269           vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 270                                         SharedArchiveFile);
 271         }
 272       }
 273 
 274       if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
 275           vm_exit_during_initialization(
 276             "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
 277             SharedArchiveFile);
 278       }
 279     }
 280   }
 281 }
 282 



 283 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
 284   if (Arguments::is_incompatible_cds_internal_module_property(key)) {
 285     stop_using_optimized_module_handling();









 286     aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
 287   }
 288 }
 289 
 290 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
 291   static const char* incompatible_properties[] = {
 292     "java.system.class.loader",
 293     "jdk.module.showModuleResolution",
 294     "jdk.module.validation"
 295   };
 296 
 297   for (const char* property : incompatible_properties) {
 298     if (strcmp(key, property) == 0) {
 299       stop_dumping_full_module_graph();
 300       stop_using_full_module_graph();
 301       aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
 302       break;
 303     }
 304   }
 305 






 306 }
 307 
 308 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
 309 static const char* find_any_unsupported_module_option() {
 310   // Note that arguments.cpp has translated the command-line options into properties. If we find an
 311   // unsupported property, translate it back to its command-line option for better error reporting.
 312 
 313   // The following properties are checked by Arguments::is_internal_module_property() and cannot be
 314   // directly specified in the command-line.
 315   static const char* unsupported_module_properties[] = {
 316     "jdk.module.limitmods",
 317     "jdk.module.upgrade.path",
 318     "jdk.module.patch.0"
 319   };
 320   static const char* unsupported_module_options[] = {
 321     "--limit-modules",
 322     "--upgrade-module-path",
 323     "--patch-module"
 324   };
 325 

 604       _input_static_archive_path = default_archive_path();
 605     } else {
 606       _input_static_archive_path = AOTCache;
 607     }
 608   }
 609 }
 610 
 611 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
 612   assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
 613 
 614   check_aot_flags();
 615 
 616   if (!FLAG_IS_DEFAULT(AOTMode)) {
 617     // Using any form of the new AOTMode switch enables enhanced optimizations.
 618     FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
 619   }
 620 
 621   setup_compiler_args();
 622 
 623   if (AOTClassLinking) {
 624     // If AOTClassLinking is specified, enable all AOT optimizations by default.
 625     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);





 626   } else {
 627     // AOTInvokeDynamicLinking depends on AOTClassLinking.
 628     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);











 629   }
 630 





 631   if (is_dumping_static_archive()) {
 632     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
 633       // Don't tweak execution mode
 634     } else if (!mode_flag_cmd_line) {
 635       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 636       //
 637       // If your classlist is large and you don't care about deterministic dumping, you can use
 638       // -Xshare:dump -Xmixed to improve dumping speed.
 639       Arguments::set_mode_flags(Arguments::_int);
 640     } else if (Arguments::mode() == Arguments::_comp) {
 641       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 642       // Java code, so there's not much benefit in running -Xcomp.
 643       aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping");
 644       Arguments::set_mode_flags(Arguments::_mixed);
 645     }
 646 
 647     // String deduplication may cause CDS to iterate the strings in different order from one
 648     // run to another which resulting in non-determinstic CDS archives.
 649     // Disable UseStringDeduplication while dumping CDS archive.
 650     UseStringDeduplication = false;

 672       aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
 673       return false;
 674     }
 675   }
 676 
 677   if (is_using_archive() && patch_mod_javabase) {
 678     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
 679   }
 680   if (is_using_archive() && has_unsupported_runtime_module_options()) {
 681     UseSharedSpaces = false;
 682   }
 683 
 684   if (is_dumping_archive()) {
 685     // Always verify non-system classes during CDS dump
 686     if (!BytecodeVerificationRemote) {
 687       BytecodeVerificationRemote = true;
 688       aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
 689     }
 690   }
 691 










 692   return true;
 693 }
 694 
 695 void CDSConfig::setup_compiler_args() {
 696   // AOT profiles are supported only in the JEP 483 workflow.
 697   bool can_dump_profiles = AOTClassLinking && new_aot_flags_used();

 698 
 699   if (is_dumping_preimage_static_archive() && can_dump_profiles) {
 700     // JEP 483 workflow -- training
 701     FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
 702     FLAG_SET_ERGO(AOTReplayTraining, false);
 703   } else if (is_dumping_final_static_archive() && can_dump_profiles) {

 704     // JEP 483 workflow -- assembly
 705     FLAG_SET_ERGO(AOTRecordTraining, false);
 706     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 707   } else if (is_using_archive() && new_aot_flags_used()) {


 708     // JEP 483 workflow -- production
 709     FLAG_SET_ERGO(AOTRecordTraining, false);
 710     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);

 711   } else {
 712     FLAG_SET_ERGO(AOTReplayTraining, false);
 713     FLAG_SET_ERGO(AOTRecordTraining, false);

 714   }
 715 }
 716 
 717 void CDSConfig::prepare_for_dumping() {
 718   assert(CDSConfig::is_dumping_archive(), "sanity");
 719 
 720   if (is_dumping_dynamic_archive() && !is_using_archive()) {
 721     assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
 722 
 723     // This could happen if SharedArchiveFile has failed to load:
 724     // - -Xshare:off was specified
 725     // - SharedArchiveFile points to an non-existent file.
 726     // - SharedArchiveFile points to an archive that has failed CRC check
 727     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
 728 
 729 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
 730     if (RecordDynamicDumpInfo) {
 731       aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
 732       MetaspaceShared::unrecoverable_loading_error();
 733     } else {

 735       aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
 736     }
 737 #undef __THEMSG
 738     disable_dumping_dynamic_archive();
 739     return;
 740   }
 741 
 742   check_unsupported_dumping_module_options();
 743 }
 744 
 745 bool CDSConfig::is_dumping_classic_static_archive() {
 746   return _is_dumping_static_archive &&
 747     !is_dumping_preimage_static_archive() &&
 748     !is_dumping_final_static_archive();
 749 }
 750 
 751 bool CDSConfig::is_dumping_preimage_static_archive() {
 752   return _is_dumping_preimage_static_archive;
 753 }
 754 




 755 bool CDSConfig::is_dumping_final_static_archive() {
 756   return _is_dumping_final_static_archive;
 757 }
 758 
 759 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
 760   _is_dumping_dynamic_archive = true;
 761   if (output_path == nullptr) {
 762     // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
 763     // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
 764     // output path.
 765     _output_archive_path = nullptr;
 766   } else {
 767     _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
 768   }
 769 }
 770 
 771 bool CDSConfig::allow_only_single_java_thread() {
 772   // See comments in JVM_StartThread()
 773   return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
 774 }
 775 














 776 bool CDSConfig::is_using_archive() {
 777   return UseSharedSpaces;
 778 }
 779 
 780 bool CDSConfig::is_using_only_default_archive() {
 781   return is_using_archive() &&
 782          input_static_archive_path() != nullptr &&
 783          default_archive_path() != nullptr &&
 784          strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
 785          input_dynamic_archive_path() == nullptr;
 786 }
 787 
 788 bool CDSConfig::is_logging_lambda_form_invokers() {
 789   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
 790 }
 791 
 792 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
 793   if (is_dumping_final_static_archive()) {
 794     // No need to regenerate -- the lambda form invokers should have been regenerated
 795     // in the preimage archive (if allowed)
 796     return false;
 797   } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
 798     // The base archive has aot-linked classes that may have AOT-resolved CP references
 799     // that point to the lambda form invokers in the base archive. Such pointers will
 800     // be invalid if lambda form invokers are regenerated in the dynamic archive.
 801     return false;
 802   } else if (CDSConfig::is_dumping_method_handles()) {
 803     // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
 804     return false;
 805   } else {
 806     return is_dumping_archive();
 807   }
 808 }
 809 

 945 }
 946 
 947 bool CDSConfig::is_dumping_aot_linked_classes() {
 948   if (is_dumping_preimage_static_archive()) {
 949     return false;
 950   } else if (is_dumping_dynamic_archive()) {
 951     return is_using_full_module_graph() && AOTClassLinking;
 952   } else if (is_dumping_static_archive()) {
 953     return is_dumping_full_module_graph() && AOTClassLinking;
 954   } else {
 955     return false;
 956   }
 957 }
 958 
 959 bool CDSConfig::is_using_aot_linked_classes() {
 960   // Make sure we have the exact same module graph as in the assembly phase, or else
 961   // some aot-linked classes may not be visible so cannot be loaded.
 962   return is_using_full_module_graph() && _has_aot_linked_classes;
 963 }
 964 




 965 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
 966   _has_aot_linked_classes |= has_aot_linked_classes;
 967 }
 968 
 969 bool CDSConfig::is_initing_classes_at_dump_time() {
 970   return is_dumping_heap() && is_dumping_aot_linked_classes();
 971 }
 972 
 973 bool CDSConfig::is_dumping_invokedynamic() {
 974   // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
 975   // objects used by the archive indy callsites may be replaced at runtime.
 976   return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
 977 }
 978 































 979 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
 980 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
 981 // classes that are used in the implementation of MethodHandles.
 982 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
 983 // and dynamic proxies.
 984 bool CDSConfig::is_dumping_method_handles() {
 985   return is_initing_classes_at_dump_time();
 986 }
 987 
 988 #endif // INCLUDE_CDS_JAVA_HEAP
 989 
 990 // AOT code generation and its archiving is disabled by default.
 991 // We enable it only in the final image dump after the metadata and heap are dumped.
 992 // This affects only JITed code because it may have embedded oops and metadata pointers
 993 // which AOT code encodes as offsets in final CDS archive regions.
 994 
 995 static bool _is_dumping_aot_code = false;
 996 
 997 bool CDSConfig::is_dumping_aot_code() {
 998   return _is_dumping_aot_code;

   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 "cds/aotLogging.hpp"
  26 #include "cds/archiveHeapLoader.hpp"
  27 #include "cds/cdsConfig.hpp"
  28 #include "cds/cds_globals.hpp"
  29 #include "cds/classListWriter.hpp"
  30 #include "cds/filemap.hpp"
  31 #include "cds/heapShared.hpp"
  32 #include "cds/metaspaceShared.hpp"
  33 #include "classfile/classLoaderDataShared.hpp"
  34 #include "classfile/moduleEntry.hpp"
  35 #include "classfile/systemDictionaryShared.hpp"
  36 #include "code/aotCodeCache.hpp"
  37 #include "compiler/compilerDefinitions.inline.hpp"
  38 #include "include/jvm_io.h"
  39 #include "logging/log.hpp"
  40 #include "prims/jvmtiExport.hpp"
  41 #include "memory/universe.hpp"
  42 #include "prims/jvmtiAgentList.hpp"
  43 #include "runtime/arguments.hpp"
  44 #include "runtime/globals_extension.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/vmThread.hpp"
  47 #include "utilities/defaultStream.hpp"
  48 #include "utilities/formatBuffer.hpp"
  49 
  50 bool CDSConfig::_is_dumping_static_archive = false;
  51 bool CDSConfig::_is_dumping_preimage_static_archive = false;
  52 bool CDSConfig::_is_dumping_final_static_archive = false;
  53 bool CDSConfig::_is_dumping_dynamic_archive = false;
  54 bool CDSConfig::_is_using_optimized_module_handling = true;
  55 bool CDSConfig::_is_dumping_full_module_graph = true;
  56 bool CDSConfig::_is_using_full_module_graph = true;
  57 bool CDSConfig::_has_aot_linked_classes = false;
  58 bool CDSConfig::_is_single_command_training = false;
  59 bool CDSConfig::_has_temp_aot_config_file = false;
  60 bool CDSConfig::_is_loading_packages = false;
  61 bool CDSConfig::_is_loading_protection_domains = false;
  62 bool CDSConfig::_is_security_manager_allowed = false;
  63 bool CDSConfig::_old_cds_flags_used = false;
  64 bool CDSConfig::_new_aot_flags_used = false;
  65 bool CDSConfig::_disable_heap_dumping = false;
  66 
  67 const char* CDSConfig::_default_archive_path = nullptr;
  68 const char* CDSConfig::_input_static_archive_path = nullptr;
  69 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
  70 const char* CDSConfig::_output_archive_path = nullptr;
  71 
  72 JavaThread* CDSConfig::_dumper_thread = nullptr;
  73 
  74 int CDSConfig::get_status() {
  75   assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
  76   return (is_dumping_archive()              ? IS_DUMPING_ARCHIVE : 0) |
  77          (is_dumping_method_handles()       ? IS_DUMPING_METHOD_HANDLES : 0) |
  78          (is_dumping_static_archive()       ? IS_DUMPING_STATIC_ARCHIVE : 0) |
  79          (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
  80          (is_using_archive()                ? IS_USING_ARCHIVE : 0) |
  81          (is_dumping_heap()                 ? IS_DUMPING_HEAP : 0) |
  82          (is_logging_dynamic_proxies()      ? IS_LOGGING_DYNAMIC_PROXIES : 0) |
  83          (is_dumping_packages()             ? IS_DUMPING_PACKAGES : 0) |
  84          (is_dumping_protection_domains()   ? IS_DUMPING_PROTECTION_DOMAINS : 0);
  85 }
  86 
  87 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
  88 
  89 void CDSConfig::ergo_initialize() {
  90   DEBUG_ONLY(_cds_ergo_initialize_started = true);
  91 
  92   if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
  93     // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
  94     // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
  95     // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
  96     // So we can never come to here with RequireSharedSpaces==true.
  97     assert(!RequireSharedSpaces, "sanity");
  98 
  99     // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
 100     // for sanity, parse all classes from classfiles.
 101     // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
 102     // needs to be changed.
 103     UseSharedSpaces = false;
 104   }
 105 
 106   // Initialize shared archive paths which could include both base and dynamic archive paths
 107   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
 108   if (is_dumping_static_archive() || is_using_archive()) {
 109     if (new_aot_flags_used()) {
 110       ergo_init_aot_paths();
 111     } else {
 112       ergo_init_classic_archive_paths();
 113     }
 114   }
 115 
 116   if (!is_dumping_heap()) {
 117     _is_dumping_full_module_graph = false;
 118   }
 119 
 120 #ifdef _LP64
 121   //
 122   // By default, when using AOTClassLinking, use the CompressedOops::HeapBasedNarrowOop
 123   // mode so that AOT code can be always work regardless of runtime heap range.
 124   //
 125   // If you are *absolutely sure* that the CompressedOops::mode() will be the same
 126   // between training and production runs (e.g., if you specify -Xmx128m for
 127   // both training and production runs, and you know the OS will always reserve
 128   // the heap under 4GB), you can explicitly disable this with:
 129   //     java -XX:+UnlockDiagnosticVMOptions -XX:-UseCompatibleCompressedOops ...
 130   // However, this is risky and there's a chance that the production run will be slower
 131   // than expected because it is unable to load the AOT code cache.
 132   //
 133   if (UseCompressedOops && AOTCodeCache::is_caching_enabled()) {
 134     FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true);
 135   } else if (!FLAG_IS_DEFAULT(UseCompatibleCompressedOops)) {
 136     FLAG_SET_ERGO(UseCompatibleCompressedOops, false);
 137   }
 138 #endif // _LP64
 139 }
 140 
 141 const char* CDSConfig::default_archive_path() {
 142   // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
 143   // before CDSConfig::ergo_initialize() is called.
 144   assert(_cds_ergo_initialize_started, "sanity");
 145   if (_default_archive_path == nullptr) {
 146     char jvm_path[JVM_MAXPATHLEN];
 147     os::jvm_path(jvm_path, sizeof(jvm_path));
 148     char *end = strrchr(jvm_path, *os::file_separator());
 149     if (end != nullptr) *end = '\0';
 150     stringStream tmp;
 151     tmp.print("%s%sclasses", jvm_path, os::file_separator());
 152 #ifdef _LP64
 153     if (!UseCompressedOops) {
 154       tmp.print_raw("_nocoops");
 155     }
 156     if (UseCompactObjectHeaders) {
 157       // Note that generation of xxx_coh.jsa variants require
 158       // --enable-cds-archive-coh at build time

 296         // Check for case (c)
 297         if (RecordDynamicDumpInfo) {
 298           vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 299                                         SharedArchiveFile);
 300         }
 301         if (ArchiveClassesAtExit != nullptr) {
 302           vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
 303                                         SharedArchiveFile);
 304         }
 305       }
 306 
 307       if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
 308           vm_exit_during_initialization(
 309             "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
 310             SharedArchiveFile);
 311       }
 312     }
 313   }
 314 }
 315 
 316 static char* bad_module_prop_key   = nullptr;
 317 static char* bad_module_prop_value = nullptr;
 318 
 319 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
 320   if (Arguments::is_incompatible_cds_internal_module_property(key)) {
 321     stop_using_optimized_module_handling();
 322     if (bad_module_prop_key == nullptr) {
 323       // We don't want to print an unconditional warning here, as we are still processing the command line.
 324       // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant.
 325       //
 326       // Instead, we save the info so we can warn when necessary: we are doing it only during AOT Cache
 327       // creation for now, but could add it to other places.
 328       bad_module_prop_key   = os::strdup(key);
 329       bad_module_prop_value = os::strdup(value);
 330     }
 331     aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
 332   }
 333 }
 334 
 335 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
 336   static const char* incompatible_properties[] = {
 337     "java.system.class.loader",
 338     "jdk.module.showModuleResolution",
 339     "jdk.module.validation"
 340   };
 341 
 342   for (const char* property : incompatible_properties) {
 343     if (strcmp(key, property) == 0) {
 344       stop_dumping_full_module_graph();
 345       stop_using_full_module_graph();
 346       aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
 347       break;
 348     }
 349   }
 350 
 351   // Match the logic in java/lang/System.java, but we need to know this before the System class is initialized.
 352   if (strcmp(key, "java.security.manager") == 0) {
 353     if (strcmp(value, "disallowed") != 0) {
 354       _is_security_manager_allowed = true;
 355     }
 356   }
 357 }
 358 
 359 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
 360 static const char* find_any_unsupported_module_option() {
 361   // Note that arguments.cpp has translated the command-line options into properties. If we find an
 362   // unsupported property, translate it back to its command-line option for better error reporting.
 363 
 364   // The following properties are checked by Arguments::is_internal_module_property() and cannot be
 365   // directly specified in the command-line.
 366   static const char* unsupported_module_properties[] = {
 367     "jdk.module.limitmods",
 368     "jdk.module.upgrade.path",
 369     "jdk.module.patch.0"
 370   };
 371   static const char* unsupported_module_options[] = {
 372     "--limit-modules",
 373     "--upgrade-module-path",
 374     "--patch-module"
 375   };
 376 

 655       _input_static_archive_path = default_archive_path();
 656     } else {
 657       _input_static_archive_path = AOTCache;
 658     }
 659   }
 660 }
 661 
 662 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
 663   assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
 664 
 665   check_aot_flags();
 666 
 667   if (!FLAG_IS_DEFAULT(AOTMode)) {
 668     // Using any form of the new AOTMode switch enables enhanced optimizations.
 669     FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
 670   }
 671 
 672   setup_compiler_args();
 673 
 674   if (AOTClassLinking) {
 675     // If AOTClassLinking is specified, enable all these optimizations by default.
 676     FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
 677     FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
 678     FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
 679     FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
 680     FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
 681     FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
 682   } else {
 683     // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
 684     FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
 685     FLAG_SET_ERGO(ArchiveDynamicProxies, false);
 686     FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
 687     FLAG_SET_ERGO(ArchivePackages, false);
 688     FLAG_SET_ERGO(ArchiveProtectionDomains, false);
 689     FLAG_SET_ERGO(ArchiveReflectionData, false);
 690 
 691     if (CDSConfig::is_dumping_archive()) {
 692       FLAG_SET_ERGO(AOTRecordTraining, false);
 693       FLAG_SET_ERGO(AOTReplayTraining, false);
 694       AOTCodeCache::disable_caching();
 695     }
 696   }
 697 
 698 #ifdef _WINDOWS
 699   // This optimization is not working on Windows for some reason. See JDK-8338604.
 700   FLAG_SET_ERGO(ArchiveReflectionData, false);
 701 #endif
 702 
 703   if (is_dumping_static_archive()) {
 704     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
 705       // Don't tweak execution mode
 706     } else if (!mode_flag_cmd_line) {
 707       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
 708       //
 709       // If your classlist is large and you don't care about deterministic dumping, you can use
 710       // -Xshare:dump -Xmixed to improve dumping speed.
 711       Arguments::set_mode_flags(Arguments::_int);
 712     } else if (Arguments::mode() == Arguments::_comp) {
 713       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
 714       // Java code, so there's not much benefit in running -Xcomp.
 715       aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping");
 716       Arguments::set_mode_flags(Arguments::_mixed);
 717     }
 718 
 719     // String deduplication may cause CDS to iterate the strings in different order from one
 720     // run to another which resulting in non-determinstic CDS archives.
 721     // Disable UseStringDeduplication while dumping CDS archive.
 722     UseStringDeduplication = false;

 744       aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
 745       return false;
 746     }
 747   }
 748 
 749   if (is_using_archive() && patch_mod_javabase) {
 750     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
 751   }
 752   if (is_using_archive() && has_unsupported_runtime_module_options()) {
 753     UseSharedSpaces = false;
 754   }
 755 
 756   if (is_dumping_archive()) {
 757     // Always verify non-system classes during CDS dump
 758     if (!BytecodeVerificationRemote) {
 759       BytecodeVerificationRemote = true;
 760       aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
 761     }
 762   }
 763 
 764   if (AOTClassLinking) {
 765     if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) {
 766       if (bad_module_prop_key != nullptr) {
 767         log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s",
 768                          bad_module_prop_key, bad_module_prop_value);
 769       }
 770       vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled");
 771     }
 772   }
 773 
 774   return true;
 775 }
 776 
 777 void CDSConfig::setup_compiler_args() {
 778   // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
 779   bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
 780   bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
 781 
 782   if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
 783     // JEP 483 workflow -- training
 784     FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
 785     FLAG_SET_ERGO(AOTReplayTraining, false);
 786     AOTCodeCache::disable_caching();
 787   } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
 788     // JEP 483 workflow -- assembly
 789     FLAG_SET_ERGO(AOTRecordTraining, false);
 790     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 791     AOTCodeCache::enable_caching();
 792     disable_dumping_aot_code(); // Cannot dump aot code until metadata and heap are dumped.
 793   } else if (is_using_archive() && can_use_profile_and_compiled_code) {
 794     // JEP 483 workflow -- production
 795     FLAG_SET_ERGO(AOTRecordTraining, false);
 796     FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
 797     AOTCodeCache::enable_caching();
 798   } else {
 799     FLAG_SET_ERGO(AOTReplayTraining, false);
 800     FLAG_SET_ERGO(AOTRecordTraining, false);
 801     AOTCodeCache::disable_caching();
 802   }
 803 }
 804 
 805 void CDSConfig::prepare_for_dumping() {
 806   assert(CDSConfig::is_dumping_archive(), "sanity");
 807 
 808   if (is_dumping_dynamic_archive() && !is_using_archive()) {
 809     assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
 810 
 811     // This could happen if SharedArchiveFile has failed to load:
 812     // - -Xshare:off was specified
 813     // - SharedArchiveFile points to an non-existent file.
 814     // - SharedArchiveFile points to an archive that has failed CRC check
 815     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
 816 
 817 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
 818     if (RecordDynamicDumpInfo) {
 819       aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
 820       MetaspaceShared::unrecoverable_loading_error();
 821     } else {

 823       aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
 824     }
 825 #undef __THEMSG
 826     disable_dumping_dynamic_archive();
 827     return;
 828   }
 829 
 830   check_unsupported_dumping_module_options();
 831 }
 832 
 833 bool CDSConfig::is_dumping_classic_static_archive() {
 834   return _is_dumping_static_archive &&
 835     !is_dumping_preimage_static_archive() &&
 836     !is_dumping_final_static_archive();
 837 }
 838 
 839 bool CDSConfig::is_dumping_preimage_static_archive() {
 840   return _is_dumping_preimage_static_archive;
 841 }
 842 
 843 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
 844   return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
 845 }
 846 
 847 bool CDSConfig::is_dumping_final_static_archive() {
 848   return _is_dumping_final_static_archive;
 849 }
 850 
 851 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
 852   _is_dumping_dynamic_archive = true;
 853   if (output_path == nullptr) {
 854     // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
 855     // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
 856     // output path.
 857     _output_archive_path = nullptr;
 858   } else {
 859     _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
 860   }
 861 }
 862 
 863 bool CDSConfig::allow_only_single_java_thread() {
 864   // See comments in JVM_StartThread()
 865   return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
 866 }
 867 
 868 bool CDSConfig::is_logging_dynamic_proxies() {
 869   return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
 870 }
 871 
 872 // Preserve all states that were examined used during dumptime verification, such
 873 // that the verification result (pass or fail) cannot be changed at runtime.
 874 //
 875 // For example, if the verification of ik requires that class A must be a subtype of B,
 876 // then this relationship between A and B cannot be changed at runtime. I.e., the app
 877 // cannot load alternative versions of A and B such that A is not a subtype of B.
 878 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) {
 879   return is_dumping_aot_linked_classes() && SystemDictionaryShared::is_builtin(ik);
 880 }
 881 
 882 bool CDSConfig::is_using_archive() {
 883   return UseSharedSpaces;
 884 }
 885 
 886 bool CDSConfig::is_using_only_default_archive() {
 887   return is_using_archive() &&
 888          input_static_archive_path() != nullptr &&
 889          default_archive_path() != nullptr &&
 890          strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
 891          input_dynamic_archive_path() == nullptr;
 892 }
 893 
 894 bool CDSConfig::is_logging_lambda_form_invokers() {
 895   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
 896 }
 897 
 898 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
 899   if (is_dumping_final_static_archive()) {
 900     // No need to regenerate -- the lambda form invokers should have been regenerated
 901     // in the preimage archive (if allowed)
 902     return false;
 903   } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
 904     // The base archive has aot-linked classes that may have AOT-resolved CP references
 905     // that point to the lambda form invokers in the base archive. Such pointers will
 906     // be invalid if lambda form invokers are regenerated in the dynamic archive.
 907     return false;
 908   } else if (CDSConfig::is_dumping_method_handles()) {
 909     // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
 910     return false;
 911   } else {
 912     return is_dumping_archive();
 913   }
 914 }
 915 

1051 }
1052 
1053 bool CDSConfig::is_dumping_aot_linked_classes() {
1054   if (is_dumping_preimage_static_archive()) {
1055     return false;
1056   } else if (is_dumping_dynamic_archive()) {
1057     return is_using_full_module_graph() && AOTClassLinking;
1058   } else if (is_dumping_static_archive()) {
1059     return is_dumping_full_module_graph() && AOTClassLinking;
1060   } else {
1061     return false;
1062   }
1063 }
1064 
1065 bool CDSConfig::is_using_aot_linked_classes() {
1066   // Make sure we have the exact same module graph as in the assembly phase, or else
1067   // some aot-linked classes may not be visible so cannot be loaded.
1068   return is_using_full_module_graph() && _has_aot_linked_classes;
1069 }
1070 
1071 bool CDSConfig::is_dumping_dynamic_proxies() {
1072   return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1073 }
1074 
1075 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1076   _has_aot_linked_classes |= has_aot_linked_classes;
1077 }
1078 
1079 bool CDSConfig::is_initing_classes_at_dump_time() {
1080   return is_dumping_heap() && is_dumping_aot_linked_classes();
1081 }
1082 
1083 bool CDSConfig::is_dumping_invokedynamic() {
1084   // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1085   // objects used by the archive indy callsites may be replaced at runtime.
1086   return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1087 }
1088 
1089 bool CDSConfig::is_dumping_packages() {
1090   return ArchivePackages && is_dumping_heap();
1091 }
1092 
1093 bool CDSConfig::is_loading_packages() {
1094   return UseSharedSpaces && is_using_full_module_graph() && _is_loading_packages;
1095 }
1096 
1097 bool CDSConfig::is_dumping_protection_domains() {
1098   if (_is_security_manager_allowed) {
1099     // For sanity, don't archive PDs. TODO: can this be relaxed?
1100     return false;
1101   }
1102   // Archived PDs for the modules will reference their java.lang.Module, which must
1103   // also be archived.
1104   return ArchiveProtectionDomains && is_dumping_full_module_graph();
1105 }
1106 
1107 bool CDSConfig::is_loading_protection_domains() {
1108   if (_is_security_manager_allowed) {
1109     // For sanity, don't used any archived PDs. TODO: can this be relaxed?
1110     return false;
1111   }
1112   return UseSharedSpaces && is_using_full_module_graph() && _is_loading_protection_domains;
1113 }
1114 
1115 bool CDSConfig::is_dumping_reflection_data() {
1116   // reflection data use LambdaForm classes
1117   return ArchiveReflectionData && is_dumping_invokedynamic();
1118 }
1119 
1120 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1121 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1122 // classes that are used in the implementation of MethodHandles.
1123 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1124 // and dynamic proxies.
1125 bool CDSConfig::is_dumping_method_handles() {
1126   return is_initing_classes_at_dump_time();
1127 }
1128 
1129 #endif // INCLUDE_CDS_JAVA_HEAP
1130 
1131 // AOT code generation and its archiving is disabled by default.
1132 // We enable it only in the final image dump after the metadata and heap are dumped.
1133 // This affects only JITed code because it may have embedded oops and metadata pointers
1134 // which AOT code encodes as offsets in final CDS archive regions.
1135 
1136 static bool _is_dumping_aot_code = false;
1137 
1138 bool CDSConfig::is_dumping_aot_code() {
1139   return _is_dumping_aot_code;
< prev index next >