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