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 "cds/aotLogging.hpp"
26 #include "cds/aotMapLogger.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/filemap.hpp"
31 #include "cds/heapShared.hpp"
32 #include "classfile/classLoaderDataShared.hpp"
33 #include "classfile/moduleEntry.hpp"
34 #include "code/aotCodeCache.hpp"
35 #include "include/jvm_io.h"
36 #include "logging/log.hpp"
37 #include "memory/universe.hpp"
38 #include "prims/jvmtiAgentList.hpp"
39 #include "runtime/arguments.hpp"
40 #include "runtime/globals_extension.hpp"
41 #include "runtime/java.hpp"
42 #include "runtime/vmThread.hpp"
43 #include "utilities/defaultStream.hpp"
44 #include "utilities/formatBuffer.hpp"
45
46 bool CDSConfig::_is_dumping_static_archive = false;
47 bool CDSConfig::_is_dumping_preimage_static_archive = false;
48 bool CDSConfig::_is_dumping_final_static_archive = false;
49 bool CDSConfig::_is_dumping_dynamic_archive = false;
50 bool CDSConfig::_is_using_optimized_module_handling = true;
51 bool CDSConfig::_is_dumping_full_module_graph = true;
52 bool CDSConfig::_is_using_full_module_graph = true;
53 bool CDSConfig::_has_aot_linked_classes = false;
54 bool CDSConfig::_is_single_command_training = false;
55 bool CDSConfig::_has_temp_aot_config_file = false;
56 bool CDSConfig::_old_cds_flags_used = false;
57 bool CDSConfig::_new_aot_flags_used = false;
58 bool CDSConfig::_disable_heap_dumping = false;
59 bool CDSConfig::_is_at_aot_safepoint = false;
60
61 const char* CDSConfig::_default_archive_path = nullptr;
62 const char* CDSConfig::_input_static_archive_path = nullptr;
63 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
64 const char* CDSConfig::_output_archive_path = nullptr;
65
66 JavaThread* CDSConfig::_dumper_thread = nullptr;
67
68 int CDSConfig::get_status() {
69 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
70 return (is_dumping_aot_linked_classes() ? IS_DUMPING_AOT_LINKED_CLASSES : 0) |
71 (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
72 (is_dumping_method_handles() ? IS_DUMPING_METHOD_HANDLES : 0) |
73 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
74 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
75 (is_using_archive() ? IS_USING_ARCHIVE : 0);
76 }
77
78 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
79
80 void CDSConfig::ergo_initialize() {
81 DEBUG_ONLY(_cds_ergo_initialize_started = true);
82
83 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
84 // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
85 // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
86 // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
87 // So we can never come to here with RequireSharedSpaces==true.
88 assert(!RequireSharedSpaces, "sanity");
89
90 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
91 // for sanity, parse all classes from classfiles.
92 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
93 // needs to be changed.
94 UseSharedSpaces = false;
95 }
96
97 // Initialize shared archive paths which could include both base and dynamic archive paths
98 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
99 if (is_dumping_static_archive() || is_using_archive()) {
100 if (new_aot_flags_used()) {
101 ergo_init_aot_paths();
102 } else {
103 ergo_init_classic_archive_paths();
104 }
105 }
106
107 if (!is_dumping_heap()) {
108 _is_dumping_full_module_graph = false;
109 }
110
111 AOTMapLogger::ergo_initialize();
112 }
113
114 const char* CDSConfig::default_archive_path() {
115 // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
116 // before CDSConfig::ergo_initialize() is called.
117 assert(_cds_ergo_initialize_started, "sanity");
118 if (_default_archive_path == nullptr) {
119 stringStream tmp;
120 if (is_vm_statically_linked()) {
121 // It's easier to form the path using JAVA_HOME as os::jvm_path
122 // gives the path to the launcher executable on static JDK.
123 const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
124 tmp.print("%s%s%s%s%s%sclasses",
125 Arguments::get_java_home(), os::file_separator(),
126 subdir, os::file_separator(),
127 Abstract_VM_Version::vm_variant(), os::file_separator());
128 } else {
129 // Assume .jsa is in the same directory where libjvm resides on
130 // non-static JDK.
131 char jvm_path[JVM_MAXPATHLEN];
281 // Check for case (c)
282 if (RecordDynamicDumpInfo) {
283 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
284 SharedArchiveFile);
285 }
286 if (ArchiveClassesAtExit != nullptr) {
287 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
288 SharedArchiveFile);
289 }
290 }
291
292 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
293 vm_exit_during_initialization(
294 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
295 SharedArchiveFile);
296 }
297 }
298 }
299 }
300
301 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
302 if (Arguments::is_incompatible_cds_internal_module_property(key)) {
303 stop_using_optimized_module_handling();
304 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
305 }
306 }
307
308 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
309 static const char* incompatible_properties[] = {
310 "java.system.class.loader",
311 "jdk.module.showModuleResolution",
312 "jdk.module.validation"
313 };
314
315 for (const char* property : incompatible_properties) {
316 if (strcmp(key, property) == 0) {
317 stop_dumping_full_module_graph();
318 stop_using_full_module_graph();
319 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
320 break;
321 }
322 }
323
324 }
325
326 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
327 static const char* find_any_unsupported_module_option() {
328 // Note that arguments.cpp has translated the command-line options into properties. If we find an
329 // unsupported property, translate it back to its command-line option for better error reporting.
330
331 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
332 // directly specified in the command-line.
333 static const char* unsupported_module_properties[] = {
334 "jdk.module.limitmods",
335 "jdk.module.upgrade.path",
336 "jdk.module.patch.0"
337 };
338 static const char* unsupported_module_options[] = {
339 "--limit-modules",
340 "--upgrade-module-path",
341 "--patch-module"
342 };
343
619 _input_static_archive_path = default_archive_path();
620 } else {
621 _input_static_archive_path = AOTCache;
622 }
623 }
624 }
625
626 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
627 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
628
629 check_aot_flags();
630
631 if (!FLAG_IS_DEFAULT(AOTMode)) {
632 // Using any form of the new AOTMode switch enables enhanced optimizations.
633 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
634 }
635
636 setup_compiler_args();
637
638 if (AOTClassLinking) {
639 // If AOTClassLinking is specified, enable all AOT optimizations by default.
640 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
641 } else {
642 // AOTInvokeDynamicLinking depends on AOTClassLinking.
643 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
644 }
645
646 if (is_dumping_static_archive()) {
647 if (is_dumping_preimage_static_archive()) {
648 // Don't tweak execution mode during AOT training run
649 } else if (is_dumping_final_static_archive()) {
650 if (Arguments::mode() == Arguments::_comp) {
651 // AOT assembly phase submits the non-blocking compilation requests
652 // for methods collected during training run, then waits for all compilations
653 // to complete. With -Xcomp, we block for each compilation request, which is
654 // counter-productive. Switching back to mixed mode improves testing time
655 // with AOT and -Xcomp.
656 Arguments::set_mode_flags(Arguments::_mixed);
657 }
658 } else if (!mode_flag_cmd_line) {
659 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
660 //
661 // If your classlist is large and you don't care about deterministic dumping, you can use
662 // -Xshare:dump -Xmixed to improve dumping speed.
663 Arguments::set_mode_flags(Arguments::_int);
664 } else if (Arguments::mode() == Arguments::_comp) {
665 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
696 aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
697 return false;
698 }
699 }
700
701 if (is_using_archive() && patch_mod_javabase) {
702 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
703 }
704 if (is_using_archive() && has_unsupported_runtime_module_options()) {
705 UseSharedSpaces = false;
706 }
707
708 if (is_dumping_archive()) {
709 // Always verify non-system classes during CDS dump
710 if (!BytecodeVerificationRemote) {
711 BytecodeVerificationRemote = true;
712 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
713 }
714 }
715
716 return true;
717 }
718
719 void CDSConfig::setup_compiler_args() {
720 // AOT profiles and AOT-compiled code are supported only in the JEP 483 workflow.
721 bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used();
722
723 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
724 // JEP 483 workflow -- training
725 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
726 FLAG_SET_ERGO(AOTReplayTraining, false);
727 AOTCodeCache::disable_caching(); // No AOT code generation during training run
728 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
729 // JEP 483 workflow -- assembly
730 FLAG_SET_ERGO(AOTRecordTraining, false);
731 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
732 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
733 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
734 } else if (is_using_archive() && new_aot_flags_used()) {
735 // JEP 483 workflow -- production
736 FLAG_SET_ERGO(AOTRecordTraining, false);
737 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
738 AOTCodeCache::enable_caching();
739 } else {
740 FLAG_SET_ERGO(AOTReplayTraining, false);
741 FLAG_SET_ERGO(AOTRecordTraining, false);
742 AOTCodeCache::disable_caching();
743 }
744 }
745
746 void CDSConfig::prepare_for_dumping() {
747 assert(CDSConfig::is_dumping_archive(), "sanity");
748
749 if (is_dumping_dynamic_archive() && AOTClassLinking) {
750 if (FLAG_IS_CMDLINE(AOTClassLinking)) {
751 log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
752 }
753 FLAG_SET_ERGO(AOTClassLinking, false);
754 }
755
756 if (is_dumping_dynamic_archive() && !is_using_archive()) {
757 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
758
759 // This could happen if SharedArchiveFile has failed to load:
760 // - -Xshare:off was specified
761 // - SharedArchiveFile points to an non-existent file.
762 // - SharedArchiveFile points to an archive that has failed CRC check
771 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
772 }
773 #undef __THEMSG
774 disable_dumping_dynamic_archive();
775 return;
776 }
777
778 check_unsupported_dumping_module_options();
779 }
780
781 bool CDSConfig::is_dumping_classic_static_archive() {
782 return _is_dumping_static_archive &&
783 !is_dumping_preimage_static_archive() &&
784 !is_dumping_final_static_archive();
785 }
786
787 bool CDSConfig::is_dumping_preimage_static_archive() {
788 return _is_dumping_preimage_static_archive;
789 }
790
791 bool CDSConfig::is_dumping_final_static_archive() {
792 return _is_dumping_final_static_archive;
793 }
794
795 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
796 _is_dumping_dynamic_archive = true;
797 if (output_path == nullptr) {
798 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
799 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
800 // output path.
801 _output_archive_path = nullptr;
802 } else {
803 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
804 }
805 }
806
807 bool CDSConfig::allow_only_single_java_thread() {
808 // See comments in JVM_StartThread()
809 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
810 }
811
812 bool CDSConfig::is_using_archive() {
813 return UseSharedSpaces;
814 }
815
816 bool CDSConfig::is_using_only_default_archive() {
817 return is_using_archive() &&
818 input_static_archive_path() != nullptr &&
819 default_archive_path() != nullptr &&
820 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
821 input_dynamic_archive_path() == nullptr;
822 }
823
824 bool CDSConfig::is_logging_lambda_form_invokers() {
825 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
826 }
827
828 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
829 if (is_dumping_final_static_archive()) {
830 // No need to regenerate -- the lambda form invokers should have been regenerated
831 // in the preimage archive (if allowed)
832 return false;
833 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
834 // The base archive has aot-linked classes that may have AOT-resolved CP references
835 // that point to the lambda form invokers in the base archive. Such pointers will
836 // be invalid if lambda form invokers are regenerated in the dynamic archive.
837 return false;
838 } else {
839 return is_dumping_archive();
840 }
841 }
842
843 void CDSConfig::stop_using_optimized_module_handling() {
844 _is_using_optimized_module_handling = false;
845 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
1010 }
1011 }
1012
1013 bool CDSConfig::is_dumping_aot_linked_classes() {
1014 if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1015 // FMG is required to guarantee that all cached boot/platform/app classes
1016 // are visible in the production run, so they can be unconditionally
1017 // loaded during VM bootstrap.
1018 return is_dumping_full_module_graph() && AOTClassLinking;
1019 } else {
1020 return false;
1021 }
1022 }
1023
1024 bool CDSConfig::is_using_aot_linked_classes() {
1025 // Make sure we have the exact same module graph as in the assembly phase, or else
1026 // some aot-linked classes may not be visible so cannot be loaded.
1027 return is_using_full_module_graph() && _has_aot_linked_classes;
1028 }
1029
1030 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1031 _has_aot_linked_classes |= has_aot_linked_classes;
1032 }
1033
1034 bool CDSConfig::is_initing_classes_at_dump_time() {
1035 return is_dumping_heap() && is_dumping_aot_linked_classes();
1036 }
1037
1038 bool CDSConfig::is_dumping_invokedynamic() {
1039 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1040 // objects used by the archive indy callsites may be replaced at runtime.
1041 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1042 }
1043
1044 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1045 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1046 // classes that are used in the implementation of MethodHandles.
1047 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1048 // and dynamic proxies.
1049 bool CDSConfig::is_dumping_method_handles() {
1050 return is_initing_classes_at_dump_time();
1051 }
1052
1053 #endif // INCLUDE_CDS_JAVA_HEAP
1054
1055 // AOT code generation and its archiving is disabled by default.
1056 // We enable it only in the final image dump after the metadata and heap are dumped.
1057 // This affects only JITed code because it may have embedded oops and metadata pointers
1058 // which AOT code encodes as offsets in final CDS archive regions.
1059
1060 static bool _is_dumping_aot_code = false;
1061
1062 bool CDSConfig::is_dumping_aot_code() {
1063 return _is_dumping_aot_code;
|
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 "cds/aotLogging.hpp"
26 #include "cds/aotMapLogger.hpp"
27 #include "cds/aotMetaspace.hpp"
28 #include "cds/archiveHeapLoader.hpp"
29 #include "cds/cds_globals.hpp"
30 #include "cds/cdsConfig.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/filemap.hpp"
33 #include "cds/heapShared.hpp"
34 #include "classfile/classLoaderDataShared.hpp"
35 #include "classfile/moduleEntry.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "code/aotCodeCache.hpp"
38 #include "compiler/compilerDefinitions.inline.hpp"
39 #include "include/jvm_io.h"
40 #include "logging/log.hpp"
41 #include "memory/universe.hpp"
42 #include "prims/jvmtiAgentList.hpp"
43 #include "prims/jvmtiExport.hpp"
44 #include "runtime/arguments.hpp"
45 #include "runtime/globals_extension.hpp"
46 #include "runtime/java.hpp"
47 #include "runtime/vmThread.hpp"
48 #include "utilities/defaultStream.hpp"
49 #include "utilities/formatBuffer.hpp"
50
51 bool CDSConfig::_is_dumping_static_archive = false;
52 bool CDSConfig::_is_dumping_preimage_static_archive = false;
53 bool CDSConfig::_is_dumping_final_static_archive = false;
54 bool CDSConfig::_is_dumping_dynamic_archive = false;
55 bool CDSConfig::_is_using_optimized_module_handling = true;
56 bool CDSConfig::_is_dumping_full_module_graph = true;
57 bool CDSConfig::_is_using_full_module_graph = true;
58 bool CDSConfig::_has_aot_linked_classes = false;
59 bool CDSConfig::_is_single_command_training = false;
60 bool CDSConfig::_has_temp_aot_config_file = false;
61 bool CDSConfig::_old_cds_flags_used = false;
62 bool CDSConfig::_new_aot_flags_used = false;
63 bool CDSConfig::_disable_heap_dumping = false;
64 bool CDSConfig::_is_at_aot_safepoint = false;
65
66 const char* CDSConfig::_default_archive_path = nullptr;
67 const char* CDSConfig::_input_static_archive_path = nullptr;
68 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
69 const char* CDSConfig::_output_archive_path = nullptr;
70
71 JavaThread* CDSConfig::_dumper_thread = nullptr;
72
73 int CDSConfig::get_status() {
74 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
75 return (is_dumping_aot_linked_classes() ? IS_DUMPING_AOT_LINKED_CLASSES : 0) |
76 (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 }
84
85 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
86
87 void CDSConfig::ergo_initialize() {
88 DEBUG_ONLY(_cds_ergo_initialize_started = true);
89
90 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
91 // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
92 // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
93 // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
94 // So we can never come to here with RequireSharedSpaces==true.
95 assert(!RequireSharedSpaces, "sanity");
96
97 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
98 // for sanity, parse all classes from classfiles.
99 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
100 // needs to be changed.
101 UseSharedSpaces = false;
102 }
103
104 // Initialize shared archive paths which could include both base and dynamic archive paths
105 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
106 if (is_dumping_static_archive() || is_using_archive()) {
107 if (new_aot_flags_used()) {
108 ergo_init_aot_paths();
109 } else {
110 ergo_init_classic_archive_paths();
111 }
112 }
113
114 if (!is_dumping_heap()) {
115 _is_dumping_full_module_graph = false;
116 }
117
118 AOTMapLogger::ergo_initialize();
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 stringStream tmp;
147 if (is_vm_statically_linked()) {
148 // It's easier to form the path using JAVA_HOME as os::jvm_path
149 // gives the path to the launcher executable on static JDK.
150 const char* subdir = WINDOWS_ONLY("bin") NOT_WINDOWS("lib");
151 tmp.print("%s%s%s%s%s%sclasses",
152 Arguments::get_java_home(), os::file_separator(),
153 subdir, os::file_separator(),
154 Abstract_VM_Version::vm_variant(), os::file_separator());
155 } else {
156 // Assume .jsa is in the same directory where libjvm resides on
157 // non-static JDK.
158 char jvm_path[JVM_MAXPATHLEN];
308 // Check for case (c)
309 if (RecordDynamicDumpInfo) {
310 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
311 SharedArchiveFile);
312 }
313 if (ArchiveClassesAtExit != nullptr) {
314 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
315 SharedArchiveFile);
316 }
317 }
318
319 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
320 vm_exit_during_initialization(
321 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
322 SharedArchiveFile);
323 }
324 }
325 }
326 }
327
328 static char* bad_module_prop_key = nullptr;
329 static char* bad_module_prop_value = nullptr;
330
331 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
332 if (Arguments::is_incompatible_cds_internal_module_property(key)) {
333 stop_using_optimized_module_handling();
334 if (bad_module_prop_key == nullptr) {
335 // We don't want to print an unconditional warning here, as we are still processing the command line.
336 // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant.
337 //
338 // Instead, we save the info so we can warn when necessary: we are doing it only during AOT Cache
339 // creation for now, but could add it to other places.
340 bad_module_prop_key = os::strdup(key);
341 bad_module_prop_value = os::strdup(value);
342 }
343 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
344 }
345 }
346
347 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
348 static const char* incompatible_properties[] = {
349 "java.system.class.loader",
350 "jdk.module.showModuleResolution",
351 "jdk.module.validation"
352 };
353
354 for (const char* property : incompatible_properties) {
355 if (strcmp(key, property) == 0) {
356 stop_dumping_full_module_graph();
357 stop_using_full_module_graph();
358 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
359 break;
360 }
361 }
362 }
363
364 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
365 static const char* find_any_unsupported_module_option() {
366 // Note that arguments.cpp has translated the command-line options into properties. If we find an
367 // unsupported property, translate it back to its command-line option for better error reporting.
368
369 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
370 // directly specified in the command-line.
371 static const char* unsupported_module_properties[] = {
372 "jdk.module.limitmods",
373 "jdk.module.upgrade.path",
374 "jdk.module.patch.0"
375 };
376 static const char* unsupported_module_options[] = {
377 "--limit-modules",
378 "--upgrade-module-path",
379 "--patch-module"
380 };
381
657 _input_static_archive_path = default_archive_path();
658 } else {
659 _input_static_archive_path = AOTCache;
660 }
661 }
662 }
663
664 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
665 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
666
667 check_aot_flags();
668
669 if (!FLAG_IS_DEFAULT(AOTMode)) {
670 // Using any form of the new AOTMode switch enables enhanced optimizations.
671 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
672 }
673
674 setup_compiler_args();
675
676 if (AOTClassLinking) {
677 // If AOTClassLinking is specified, enable all these optimizations by default.
678 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
679 FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
680 FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
681 FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
682
683 // For simplicity, enable these by default only for new workflow
684 if (new_aot_flags_used()) {
685 // These flags will be removed when JDK-8350550 is merged from mainline
686 FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
687 FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
688 }
689 } else {
690 // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
691 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
692 FLAG_SET_ERGO(ArchiveDynamicProxies, false);
693 FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
694 FLAG_SET_ERGO(ArchivePackages, false);
695 FLAG_SET_ERGO(ArchiveProtectionDomains, false);
696 FLAG_SET_ERGO(ArchiveReflectionData, false);
697
698 if (CDSConfig::is_dumping_archive()) {
699 FLAG_SET_ERGO(AOTRecordTraining, false);
700 FLAG_SET_ERGO(AOTReplayTraining, false);
701 AOTCodeCache::disable_caching();
702 }
703 }
704 if (is_dumping_static_archive()) {
705 if (is_dumping_preimage_static_archive()) {
706 // Don't tweak execution mode during AOT training run
707 } else if (is_dumping_final_static_archive()) {
708 if (Arguments::mode() == Arguments::_comp) {
709 // AOT assembly phase submits the non-blocking compilation requests
710 // for methods collected during training run, then waits for all compilations
711 // to complete. With -Xcomp, we block for each compilation request, which is
712 // counter-productive. Switching back to mixed mode improves testing time
713 // with AOT and -Xcomp.
714 Arguments::set_mode_flags(Arguments::_mixed);
715 }
716 } else if (!mode_flag_cmd_line) {
717 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
718 //
719 // If your classlist is large and you don't care about deterministic dumping, you can use
720 // -Xshare:dump -Xmixed to improve dumping speed.
721 Arguments::set_mode_flags(Arguments::_int);
722 } else if (Arguments::mode() == Arguments::_comp) {
723 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
754 aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
755 return false;
756 }
757 }
758
759 if (is_using_archive() && patch_mod_javabase) {
760 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
761 }
762 if (is_using_archive() && has_unsupported_runtime_module_options()) {
763 UseSharedSpaces = false;
764 }
765
766 if (is_dumping_archive()) {
767 // Always verify non-system classes during CDS dump
768 if (!BytecodeVerificationRemote) {
769 BytecodeVerificationRemote = true;
770 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
771 }
772 }
773
774 if (AOTClassLinking) {
775 if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) {
776 if (bad_module_prop_key != nullptr) {
777 log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s",
778 bad_module_prop_key, bad_module_prop_value);
779 }
780 vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled");
781 }
782 }
783
784 return true;
785 }
786
787 void CDSConfig::setup_compiler_args() {
788 // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
789 bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
790 bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
791
792 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
793 // JEP 483 workflow -- training
794 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
795 FLAG_SET_ERGO(AOTReplayTraining, false);
796 AOTCodeCache::disable_caching(); // No AOT code generation during training run
797 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
798 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
799 // JEP 483 workflow -- assembly
800 FLAG_SET_ERGO(AOTRecordTraining, false);
801 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
802 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
803 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
804 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
805 } else if (is_using_archive() && can_use_profile_and_compiled_code) {
806 // JEP 483 workflow -- production
807 FLAG_SET_ERGO(AOTRecordTraining, false);
808 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
809 AOTCodeCache::enable_caching();
810 FLAG_SET_ERGO_IF_DEFAULT(UseAOTCodeLoadThread, true);
811 } else {
812 FLAG_SET_ERGO(AOTReplayTraining, false);
813 FLAG_SET_ERGO(AOTRecordTraining, false);
814 AOTCodeCache::disable_caching();
815 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
816 }
817 }
818
819 void CDSConfig::prepare_for_dumping() {
820 assert(CDSConfig::is_dumping_archive(), "sanity");
821
822 if (is_dumping_dynamic_archive() && AOTClassLinking) {
823 if (FLAG_IS_CMDLINE(AOTClassLinking)) {
824 log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
825 }
826 FLAG_SET_ERGO(AOTClassLinking, false);
827 }
828
829 if (is_dumping_dynamic_archive() && !is_using_archive()) {
830 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
831
832 // This could happen if SharedArchiveFile has failed to load:
833 // - -Xshare:off was specified
834 // - SharedArchiveFile points to an non-existent file.
835 // - SharedArchiveFile points to an archive that has failed CRC check
844 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
845 }
846 #undef __THEMSG
847 disable_dumping_dynamic_archive();
848 return;
849 }
850
851 check_unsupported_dumping_module_options();
852 }
853
854 bool CDSConfig::is_dumping_classic_static_archive() {
855 return _is_dumping_static_archive &&
856 !is_dumping_preimage_static_archive() &&
857 !is_dumping_final_static_archive();
858 }
859
860 bool CDSConfig::is_dumping_preimage_static_archive() {
861 return _is_dumping_preimage_static_archive;
862 }
863
864 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
865 return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
866 }
867
868 bool CDSConfig::is_dumping_final_static_archive() {
869 return _is_dumping_final_static_archive;
870 }
871
872 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
873 _is_dumping_dynamic_archive = true;
874 if (output_path == nullptr) {
875 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
876 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
877 // output path.
878 _output_archive_path = nullptr;
879 } else {
880 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
881 }
882 }
883
884 bool CDSConfig::allow_only_single_java_thread() {
885 // See comments in JVM_StartThread()
886 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
887 }
888
889 bool CDSConfig::is_logging_dynamic_proxies() {
890 return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
891 }
892
893 bool CDSConfig::is_using_archive() {
894 return UseSharedSpaces;
895 }
896
897 bool CDSConfig::is_using_only_default_archive() {
898 return is_using_archive() &&
899 input_static_archive_path() != nullptr &&
900 default_archive_path() != nullptr &&
901 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
902 input_dynamic_archive_path() == nullptr;
903 }
904
905 bool CDSConfig::is_logging_lambda_form_invokers() {
906 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
907 }
908
909 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
910 if (is_dumping_final_static_archive()) {
911 // No need to regenerate -- the lambda form invokers should have been regenerated
912 // in the preimage archive (if allowed)
913 return false;
914 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
915 // The base archive has aot-linked classes that may have AOT-resolved CP references
916 // that point to the lambda form invokers in the base archive. Such pointers will
917 // be invalid if lambda form invokers are regenerated in the dynamic archive.
918 return false;
919 } else {
920 return is_dumping_archive();
921 }
922 }
923
924 void CDSConfig::stop_using_optimized_module_handling() {
925 _is_using_optimized_module_handling = false;
926 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
1091 }
1092 }
1093
1094 bool CDSConfig::is_dumping_aot_linked_classes() {
1095 if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1096 // FMG is required to guarantee that all cached boot/platform/app classes
1097 // are visible in the production run, so they can be unconditionally
1098 // loaded during VM bootstrap.
1099 return is_dumping_full_module_graph() && AOTClassLinking;
1100 } else {
1101 return false;
1102 }
1103 }
1104
1105 bool CDSConfig::is_using_aot_linked_classes() {
1106 // Make sure we have the exact same module graph as in the assembly phase, or else
1107 // some aot-linked classes may not be visible so cannot be loaded.
1108 return is_using_full_module_graph() && _has_aot_linked_classes;
1109 }
1110
1111 bool CDSConfig::is_dumping_dynamic_proxies() {
1112 return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1113 }
1114
1115 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1116 _has_aot_linked_classes |= has_aot_linked_classes;
1117 }
1118
1119 bool CDSConfig::is_initing_classes_at_dump_time() {
1120 return is_dumping_heap() && is_dumping_aot_linked_classes();
1121 }
1122
1123 bool CDSConfig::is_dumping_invokedynamic() {
1124 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1125 // objects used by the archive indy callsites may be replaced at runtime.
1126 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1127 }
1128
1129 bool CDSConfig::is_dumping_reflection_data() {
1130 // reflection data use LambdaForm classes
1131 return ArchiveReflectionData && is_dumping_invokedynamic();
1132 }
1133
1134 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1135 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1136 // classes that are used in the implementation of MethodHandles.
1137 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1138 // and dynamic proxies.
1139 bool CDSConfig::is_dumping_method_handles() {
1140 return is_initing_classes_at_dump_time();
1141 }
1142
1143 #endif // INCLUDE_CDS_JAVA_HEAP
1144
1145 // AOT code generation and its archiving is disabled by default.
1146 // We enable it only in the final image dump after the metadata and heap are dumped.
1147 // This affects only JITed code because it may have embedded oops and metadata pointers
1148 // which AOT code encodes as offsets in final CDS archive regions.
1149
1150 static bool _is_dumping_aot_code = false;
1151
1152 bool CDSConfig::is_dumping_aot_code() {
1153 return _is_dumping_aot_code;
|