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
685 // For simplicity, enable these by default only for new workflow
686 if (new_aot_flags_used()) {
687 // These flags will be removed when JDK-8350550 is merged from mainline
688 FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
689 FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
690 }
691 } else {
692 // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
693 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
694 FLAG_SET_ERGO(ArchiveDynamicProxies, false);
695 FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
696 FLAG_SET_ERGO(ArchivePackages, false);
697 FLAG_SET_ERGO(ArchiveProtectionDomains, false);
698 FLAG_SET_ERGO(ArchiveReflectionData, false);
699
700 if (CDSConfig::is_dumping_archive()) {
701 FLAG_SET_ERGO(AOTRecordTraining, false);
702 FLAG_SET_ERGO(AOTReplayTraining, false);
703 AOTCodeCache::disable_caching();
704 }
705 }
706 if (is_dumping_static_archive()) {
707 if (is_dumping_preimage_static_archive()) {
708 // Don't tweak execution mode during AOT training run
709 } else if (is_dumping_final_static_archive()) {
710 if (Arguments::mode() == Arguments::_comp) {
711 // AOT assembly phase submits the non-blocking compilation requests
712 // for methods collected during training run, then waits for all compilations
713 // to complete. With -Xcomp, we block for each compilation request, which is
714 // counter-productive. Switching back to mixed mode improves testing time
715 // with AOT and -Xcomp.
716 Arguments::set_mode_flags(Arguments::_mixed);
717 }
718 } else if (!mode_flag_cmd_line) {
719 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
720 //
721 // If your classlist is large and you don't care about deterministic dumping, you can use
722 // -Xshare:dump -Xmixed to improve dumping speed.
723 Arguments::set_mode_flags(Arguments::_int);
724 } else if (Arguments::mode() == Arguments::_comp) {
725 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
760
761 if (is_using_archive() && patch_mod_javabase) {
762 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
763 }
764 if (is_using_archive() && has_unsupported_runtime_module_options()) {
765 UseSharedSpaces = false;
766 }
767
768 if (is_dumping_archive()) {
769 // Always verify non-system classes during CDS dump
770 if (!BytecodeVerificationRemote) {
771 BytecodeVerificationRemote = true;
772 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
773 }
774 }
775
776 return true;
777 }
778
779 void CDSConfig::setup_compiler_args() {
780 // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
781 bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
782 bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
783
784 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
785 // JEP 483 workflow -- training
786 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
787 FLAG_SET_ERGO(AOTReplayTraining, false);
788 AOTCodeCache::disable_caching(); // No AOT code generation during training run
789 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
790 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
791 // JEP 483 workflow -- assembly
792 FLAG_SET_ERGO(AOTRecordTraining, false);
793 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
794 // Generate AOT code only when training data enabled
795 if (AOTReplayTraining) {
796 AOTCodeCache::enable_caching();
797 } else {
798 AOTCodeCache::disable_caching();
799 }
800 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
801 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
802 } else if (is_using_archive() && can_use_profile_and_compiled_code) {
803 // JEP 483 workflow -- production
804 FLAG_SET_ERGO(AOTRecordTraining, false);
805 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
806 // Use AOT code only when training data enabled
807 if (AOTReplayTraining) {
808 AOTCodeCache::enable_caching();
809 FLAG_SET_ERGO_IF_DEFAULT(UseAOTCodeLoadThread, true);
810 } else {
811 AOTCodeCache::disable_caching();
812 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
813 }
814 } else {
815 FLAG_SET_ERGO(AOTReplayTraining, false);
816 FLAG_SET_ERGO(AOTRecordTraining, false);
817 AOTCodeCache::disable_caching();
818 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
819 }
820 }
821
822 void CDSConfig::prepare_for_dumping() {
823 assert(CDSConfig::is_dumping_archive(), "sanity");
824
825 if (is_dumping_dynamic_archive() && AOTClassLinking) {
826 if (FLAG_IS_CMDLINE(AOTClassLinking)) {
827 log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
828 }
829 FLAG_SET_ERGO(AOTClassLinking, false);
830 }
831
832 if (is_dumping_dynamic_archive() && !is_using_archive()) {
833 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
834
835 // This could happen if SharedArchiveFile has failed to load:
836 // - -Xshare:off was specified
837 // - SharedArchiveFile points to an non-existent file.
838 // - SharedArchiveFile points to an archive that has failed CRC check
847 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
848 }
849 #undef __THEMSG
850 disable_dumping_dynamic_archive();
851 return;
852 }
853
854 check_unsupported_dumping_module_options();
855 }
856
857 bool CDSConfig::is_dumping_classic_static_archive() {
858 return _is_dumping_static_archive &&
859 !is_dumping_preimage_static_archive() &&
860 !is_dumping_final_static_archive();
861 }
862
863 bool CDSConfig::is_dumping_preimage_static_archive() {
864 return _is_dumping_preimage_static_archive;
865 }
866
867 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
868 return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
869 }
870
871 bool CDSConfig::is_dumping_final_static_archive() {
872 return _is_dumping_final_static_archive;
873 }
874
875 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
876 _is_dumping_dynamic_archive = true;
877 if (output_path == nullptr) {
878 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
879 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
880 // output path.
881 _output_archive_path = nullptr;
882 } else {
883 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
884 }
885 }
886
887 bool CDSConfig::allow_only_single_java_thread() {
888 // See comments in JVM_StartThread()
889 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
890 }
891
892 bool CDSConfig::is_logging_dynamic_proxies() {
893 return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
894 }
895
896 bool CDSConfig::is_using_archive() {
897 return UseSharedSpaces;
898 }
899
900 bool CDSConfig::is_using_only_default_archive() {
901 return is_using_archive() &&
902 input_static_archive_path() != nullptr &&
903 default_archive_path() != nullptr &&
904 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
905 input_dynamic_archive_path() == nullptr;
906 }
907
908 bool CDSConfig::is_logging_lambda_form_invokers() {
909 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
910 }
911
912 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
913 if (is_dumping_final_static_archive()) {
914 // No need to regenerate -- the lambda form invokers should have been regenerated
915 // in the preimage archive (if allowed)
916 return false;
917 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
918 // The base archive has aot-linked classes that may have AOT-resolved CP references
919 // that point to the lambda form invokers in the base archive. Such pointers will
920 // be invalid if lambda form invokers are regenerated in the dynamic archive.
921 return false;
922 } else {
923 return is_dumping_archive();
924 }
925 }
926
927 void CDSConfig::stop_using_optimized_module_handling() {
928 _is_using_optimized_module_handling = false;
929 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
1112 }
1113 }
1114
1115 bool CDSConfig::is_dumping_aot_linked_classes() {
1116 if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1117 // FMG is required to guarantee that all cached boot/platform/app classes
1118 // are visible in the production run, so they can be unconditionally
1119 // loaded during VM bootstrap.
1120 return is_dumping_full_module_graph() && AOTClassLinking;
1121 } else {
1122 return false;
1123 }
1124 }
1125
1126 bool CDSConfig::is_using_aot_linked_classes() {
1127 // Make sure we have the exact same module graph as in the assembly phase, or else
1128 // some aot-linked classes may not be visible so cannot be loaded.
1129 return is_using_full_module_graph() && _has_aot_linked_classes;
1130 }
1131
1132 bool CDSConfig::is_dumping_dynamic_proxies() {
1133 return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1134 }
1135
1136 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1137 _has_aot_linked_classes |= has_aot_linked_classes;
1138 }
1139
1140 bool CDSConfig::is_dumping_invokedynamic() {
1141 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1142 // objects used by the archive indy callsites may be replaced at runtime.
1143 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1144 }
1145
1146 bool CDSConfig::is_dumping_reflection_data() {
1147 // reflection data use LambdaForm classes
1148 return ArchiveReflectionData && is_dumping_invokedynamic();
1149 }
1150
1151 // When we are dumping aot-linked classes, we automatically enable the archiving of MethodHandles.
1152 // This will in turn enable the archiving of MethodTypes and hidden
1153 // classes that are used in the implementation of MethodHandles.
1154 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1155 // and dynamic proxies.
1156 bool CDSConfig::is_dumping_method_handles() {
1157 return is_dumping_aot_linked_classes();
1158 }
1159
1160 #endif // INCLUDE_CDS_JAVA_HEAP
1161
1162 // AOT code generation and its archiving is disabled by default.
1163 // We enable it only in the final image dump after the metadata and heap are dumped.
1164 // This affects only JITed code because it may have embedded oops and metadata pointers
1165 // which AOT code encodes as offsets in final CDS archive regions.
1166
1167 static bool _is_dumping_aot_code = false;
1168
1169 bool CDSConfig::is_dumping_aot_code() {
1170 return _is_dumping_aot_code;
|