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
618 _input_static_archive_path = default_archive_path();
619 } else {
620 _input_static_archive_path = AOTCache;
621 }
622 }
623 }
624
625 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
626 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
627
628 check_aot_flags();
629
630 if (!FLAG_IS_DEFAULT(AOTMode)) {
631 // Using any form of the new AOTMode switch enables enhanced optimizations.
632 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
633 }
634
635 setup_compiler_args();
636
637 if (AOTClassLinking) {
638 // If AOTClassLinking is specified, enable all AOT optimizations by default.
639 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
640 } else {
641 // AOTInvokeDynamicLinking depends on AOTClassLinking.
642 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
643 }
644
645 if (is_dumping_static_archive()) {
646 if (is_dumping_preimage_static_archive()) {
647 // Don't tweak execution mode during AOT training run
648 } else if (is_dumping_final_static_archive()) {
649 if (Arguments::mode() == Arguments::_comp) {
650 // AOT assembly phase submits the non-blocking compilation requests
651 // for methods collected during training run, then waits for all compilations
652 // to complete. With -Xcomp, we block for each compilation request, which is
653 // counter-productive. Switching back to mixed mode improves testing time
654 // with AOT and -Xcomp.
655 Arguments::set_mode_flags(Arguments::_mixed);
656 }
657 } else if (!mode_flag_cmd_line) {
658 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
659 //
660 // If your classlist is large and you don't care about deterministic dumping, you can use
661 // -Xshare:dump -Xmixed to improve dumping speed.
662 Arguments::set_mode_flags(Arguments::_int);
663 } else if (Arguments::mode() == Arguments::_comp) {
664 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
699
700 if (is_using_archive() && patch_mod_javabase) {
701 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
702 }
703 if (is_using_archive() && has_unsupported_runtime_module_options()) {
704 UseSharedSpaces = false;
705 }
706
707 if (is_dumping_archive()) {
708 // Always verify non-system classes during CDS dump
709 if (!BytecodeVerificationRemote) {
710 BytecodeVerificationRemote = true;
711 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
712 }
713 }
714
715 return true;
716 }
717
718 void CDSConfig::setup_compiler_args() {
719 // AOT profiles and AOT-compiled code are supported only in the JEP 483 workflow.
720 bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used();
721
722 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
723 // JEP 483 workflow -- training
724 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
725 FLAG_SET_ERGO(AOTReplayTraining, false);
726 AOTCodeCache::disable_caching(); // No AOT code generation during training run
727 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
728 // JEP 483 workflow -- assembly
729 FLAG_SET_ERGO(AOTRecordTraining, false);
730 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
731 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
732 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
733 } else if (is_using_archive() && new_aot_flags_used()) {
734 // JEP 483 workflow -- production
735 FLAG_SET_ERGO(AOTRecordTraining, false);
736 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
737 AOTCodeCache::enable_caching();
738 } else {
739 FLAG_SET_ERGO(AOTReplayTraining, false);
740 FLAG_SET_ERGO(AOTRecordTraining, false);
741 AOTCodeCache::disable_caching();
742 }
743 }
744
745 void CDSConfig::prepare_for_dumping() {
746 assert(CDSConfig::is_dumping_archive(), "sanity");
747
748 if (is_dumping_dynamic_archive() && AOTClassLinking) {
749 if (FLAG_IS_CMDLINE(AOTClassLinking)) {
750 log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
751 }
752 FLAG_SET_ERGO(AOTClassLinking, false);
753 }
754
755 if (is_dumping_dynamic_archive() && !is_using_archive()) {
756 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
757
758 // This could happen if SharedArchiveFile has failed to load:
759 // - -Xshare:off was specified
760 // - SharedArchiveFile points to an non-existent file.
761 // - SharedArchiveFile points to an archive that has failed CRC check
770 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
771 }
772 #undef __THEMSG
773 disable_dumping_dynamic_archive();
774 return;
775 }
776
777 check_unsupported_dumping_module_options();
778 }
779
780 bool CDSConfig::is_dumping_classic_static_archive() {
781 return _is_dumping_static_archive &&
782 !is_dumping_preimage_static_archive() &&
783 !is_dumping_final_static_archive();
784 }
785
786 bool CDSConfig::is_dumping_preimage_static_archive() {
787 return _is_dumping_preimage_static_archive;
788 }
789
790 bool CDSConfig::is_dumping_final_static_archive() {
791 return _is_dumping_final_static_archive;
792 }
793
794 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
795 _is_dumping_dynamic_archive = true;
796 if (output_path == nullptr) {
797 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
798 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
799 // output path.
800 _output_archive_path = nullptr;
801 } else {
802 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
803 }
804 }
805
806 bool CDSConfig::allow_only_single_java_thread() {
807 // See comments in JVM_StartThread()
808 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
809 }
810
811 bool CDSConfig::is_using_archive() {
812 return UseSharedSpaces;
813 }
814
815 bool CDSConfig::is_using_only_default_archive() {
816 return is_using_archive() &&
817 input_static_archive_path() != nullptr &&
818 default_archive_path() != nullptr &&
819 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
820 input_dynamic_archive_path() == nullptr;
821 }
822
823 bool CDSConfig::is_logging_lambda_form_invokers() {
824 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
825 }
826
827 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
828 if (is_dumping_final_static_archive()) {
829 // No need to regenerate -- the lambda form invokers should have been regenerated
830 // in the preimage archive (if allowed)
831 return false;
832 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
833 // The base archive has aot-linked classes that may have AOT-resolved CP references
834 // that point to the lambda form invokers in the base archive. Such pointers will
835 // be invalid if lambda form invokers are regenerated in the dynamic archive.
836 return false;
837 } else {
838 return is_dumping_archive();
839 }
840 }
841
842 void CDSConfig::stop_using_optimized_module_handling() {
843 _is_using_optimized_module_handling = false;
844 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
1005 }
1006 }
1007
1008 bool CDSConfig::is_dumping_aot_linked_classes() {
1009 if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1010 // FMG is required to guarantee that all cached boot/platform/app classes
1011 // are visible in the production run, so they can be unconditionally
1012 // loaded during VM bootstrap.
1013 return is_dumping_full_module_graph() && AOTClassLinking;
1014 } else {
1015 return false;
1016 }
1017 }
1018
1019 bool CDSConfig::is_using_aot_linked_classes() {
1020 // Make sure we have the exact same module graph as in the assembly phase, or else
1021 // some aot-linked classes may not be visible so cannot be loaded.
1022 return is_using_full_module_graph() && _has_aot_linked_classes;
1023 }
1024
1025 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1026 _has_aot_linked_classes |= has_aot_linked_classes;
1027 }
1028
1029 bool CDSConfig::is_dumping_invokedynamic() {
1030 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1031 // objects used by the archive indy callsites may be replaced at runtime.
1032 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1033 }
1034
1035 // When we are dumping aot-linked classes, we automatically enable the archiving of MethodHandles.
1036 // This will in turn enable the archiving of MethodTypes and hidden
1037 // classes that are used in the implementation of MethodHandles.
1038 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1039 // and dynamic proxies.
1040 bool CDSConfig::is_dumping_method_handles() {
1041 return is_dumping_aot_linked_classes();
1042 }
1043
1044 #endif // INCLUDE_CDS_JAVA_HEAP
1045
1046 // AOT code generation and its archiving is disabled by default.
1047 // We enable it only in the final image dump after the metadata and heap are dumped.
1048 // This affects only JITed code because it may have embedded oops and metadata pointers
1049 // which AOT code encodes as offsets in final CDS archive regions.
1050
1051 static bool _is_dumping_aot_code = false;
1052
1053 bool CDSConfig::is_dumping_aot_code() {
1054 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
656 _input_static_archive_path = default_archive_path();
657 } else {
658 _input_static_archive_path = AOTCache;
659 }
660 }
661 }
662
663 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
664 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
665
666 check_aot_flags();
667
668 if (!FLAG_IS_DEFAULT(AOTMode)) {
669 // Using any form of the new AOTMode switch enables enhanced optimizations.
670 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
671 }
672
673 setup_compiler_args();
674
675 if (AOTClassLinking) {
676 // If AOTClassLinking is specified, enable all these optimizations by default.
677 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
678 FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, false); // FIXME-merge
679 FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
680 FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
681
682 // For simplicity, enable these by default only for new workflow
683 if (new_aot_flags_used()) {
684 // These flags will be removed when JDK-8350550 is merged from mainline
685 FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
686 FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
687 }
688 } else {
689 // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
690 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
691 FLAG_SET_ERGO(ArchiveDynamicProxies, false);
692 FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
693 FLAG_SET_ERGO(ArchivePackages, false);
694 FLAG_SET_ERGO(ArchiveProtectionDomains, false);
695 FLAG_SET_ERGO(ArchiveReflectionData, false);
696
697 if (CDSConfig::is_dumping_archive()) {
698 FLAG_SET_ERGO(AOTRecordTraining, false);
699 FLAG_SET_ERGO(AOTReplayTraining, false);
700 AOTCodeCache::disable_caching();
701 }
702 }
703 if (is_dumping_static_archive()) {
704 if (is_dumping_preimage_static_archive()) {
705 // Don't tweak execution mode during AOT training run
706 } else if (is_dumping_final_static_archive()) {
707 if (Arguments::mode() == Arguments::_comp) {
708 // AOT assembly phase submits the non-blocking compilation requests
709 // for methods collected during training run, then waits for all compilations
710 // to complete. With -Xcomp, we block for each compilation request, which is
711 // counter-productive. Switching back to mixed mode improves testing time
712 // with AOT and -Xcomp.
713 Arguments::set_mode_flags(Arguments::_mixed);
714 }
715 } else if (!mode_flag_cmd_line) {
716 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
717 //
718 // If your classlist is large and you don't care about deterministic dumping, you can use
719 // -Xshare:dump -Xmixed to improve dumping speed.
720 Arguments::set_mode_flags(Arguments::_int);
721 } else if (Arguments::mode() == Arguments::_comp) {
722 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
757
758 if (is_using_archive() && patch_mod_javabase) {
759 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
760 }
761 if (is_using_archive() && has_unsupported_runtime_module_options()) {
762 UseSharedSpaces = false;
763 }
764
765 if (is_dumping_archive()) {
766 // Always verify non-system classes during CDS dump
767 if (!BytecodeVerificationRemote) {
768 BytecodeVerificationRemote = true;
769 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
770 }
771 }
772
773 return true;
774 }
775
776 void CDSConfig::setup_compiler_args() {
777 // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
778 bool can_dump_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && AOTClassLinking && new_aot_flags_used();
779 bool can_use_profile_and_compiled_code = !CompilerConfig::is_interpreter_only() && new_aot_flags_used();
780
781 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
782 // JEP 483 workflow -- training
783 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
784 FLAG_SET_ERGO(AOTReplayTraining, false);
785 AOTCodeCache::disable_caching(); // No AOT code generation during training run
786 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
787 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
788 // JEP 483 workflow -- assembly
789 FLAG_SET_ERGO(AOTRecordTraining, false);
790 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
791 AOTCodeCache::enable_caching(); // Generate AOT code during assembly phase.
792 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
793 disable_dumping_aot_code(); // Don't dump AOT code until metadata and heap are dumped.
794 } else if (is_using_archive() && can_use_profile_and_compiled_code) {
795 // JEP 483 workflow -- production
796 FLAG_SET_ERGO(AOTRecordTraining, false);
797 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
798 AOTCodeCache::enable_caching();
799 FLAG_SET_ERGO_IF_DEFAULT(UseAOTCodeLoadThread, true);
800 } else {
801 FLAG_SET_ERGO(AOTReplayTraining, false);
802 FLAG_SET_ERGO(AOTRecordTraining, false);
803 AOTCodeCache::disable_caching();
804 FLAG_SET_ERGO(UseAOTCodeLoadThread, false);
805 }
806 }
807
808 void CDSConfig::prepare_for_dumping() {
809 assert(CDSConfig::is_dumping_archive(), "sanity");
810
811 if (is_dumping_dynamic_archive() && AOTClassLinking) {
812 if (FLAG_IS_CMDLINE(AOTClassLinking)) {
813 log_warning(cds)("AOTClassLinking is not supported for dynamic CDS archive");
814 }
815 FLAG_SET_ERGO(AOTClassLinking, false);
816 }
817
818 if (is_dumping_dynamic_archive() && !is_using_archive()) {
819 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
820
821 // This could happen if SharedArchiveFile has failed to load:
822 // - -Xshare:off was specified
823 // - SharedArchiveFile points to an non-existent file.
824 // - SharedArchiveFile points to an archive that has failed CRC check
833 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
834 }
835 #undef __THEMSG
836 disable_dumping_dynamic_archive();
837 return;
838 }
839
840 check_unsupported_dumping_module_options();
841 }
842
843 bool CDSConfig::is_dumping_classic_static_archive() {
844 return _is_dumping_static_archive &&
845 !is_dumping_preimage_static_archive() &&
846 !is_dumping_final_static_archive();
847 }
848
849 bool CDSConfig::is_dumping_preimage_static_archive() {
850 return _is_dumping_preimage_static_archive;
851 }
852
853 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
854 return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
855 }
856
857 bool CDSConfig::is_dumping_final_static_archive() {
858 return _is_dumping_final_static_archive;
859 }
860
861 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
862 _is_dumping_dynamic_archive = true;
863 if (output_path == nullptr) {
864 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
865 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
866 // output path.
867 _output_archive_path = nullptr;
868 } else {
869 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
870 }
871 }
872
873 bool CDSConfig::allow_only_single_java_thread() {
874 // See comments in JVM_StartThread()
875 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
876 }
877
878 bool CDSConfig::is_logging_dynamic_proxies() {
879 return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
880 }
881
882 bool CDSConfig::is_using_archive() {
883 return UseSharedSpaces;
884 }
885
886 bool CDSConfig::is_using_only_default_archive() {
887 return is_using_archive() &&
888 input_static_archive_path() != nullptr &&
889 default_archive_path() != nullptr &&
890 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
891 input_dynamic_archive_path() == nullptr;
892 }
893
894 bool CDSConfig::is_logging_lambda_form_invokers() {
895 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
896 }
897
898 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
899 if (is_dumping_final_static_archive()) {
900 // No need to regenerate -- the lambda form invokers should have been regenerated
901 // in the preimage archive (if allowed)
902 return false;
903 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
904 // The base archive has aot-linked classes that may have AOT-resolved CP references
905 // that point to the lambda form invokers in the base archive. Such pointers will
906 // be invalid if lambda form invokers are regenerated in the dynamic archive.
907 return false;
908 } else {
909 return is_dumping_archive();
910 }
911 }
912
913 void CDSConfig::stop_using_optimized_module_handling() {
914 _is_using_optimized_module_handling = false;
915 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
1076 }
1077 }
1078
1079 bool CDSConfig::is_dumping_aot_linked_classes() {
1080 if (is_dumping_classic_static_archive() || is_dumping_final_static_archive()) {
1081 // FMG is required to guarantee that all cached boot/platform/app classes
1082 // are visible in the production run, so they can be unconditionally
1083 // loaded during VM bootstrap.
1084 return is_dumping_full_module_graph() && AOTClassLinking;
1085 } else {
1086 return false;
1087 }
1088 }
1089
1090 bool CDSConfig::is_using_aot_linked_classes() {
1091 // Make sure we have the exact same module graph as in the assembly phase, or else
1092 // some aot-linked classes may not be visible so cannot be loaded.
1093 return is_using_full_module_graph() && _has_aot_linked_classes;
1094 }
1095
1096 bool CDSConfig::is_dumping_dynamic_proxies() {
1097 return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1098 }
1099
1100 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1101 _has_aot_linked_classes |= has_aot_linked_classes;
1102 }
1103
1104 bool CDSConfig::is_dumping_invokedynamic() {
1105 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1106 // objects used by the archive indy callsites may be replaced at runtime.
1107 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1108 }
1109
1110 bool CDSConfig::is_dumping_reflection_data() {
1111 // reflection data use LambdaForm classes
1112 return ArchiveReflectionData && is_dumping_invokedynamic();
1113 }
1114
1115 // When we are dumping aot-linked classes, we automatically enable the archiving of MethodHandles.
1116 // This will in turn enable the archiving of MethodTypes and hidden
1117 // classes that are used in the implementation of MethodHandles.
1118 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1119 // and dynamic proxies.
1120 bool CDSConfig::is_dumping_method_handles() {
1121 return is_dumping_aot_linked_classes();
1122 }
1123
1124 #endif // INCLUDE_CDS_JAVA_HEAP
1125
1126 // AOT code generation and its archiving is disabled by default.
1127 // We enable it only in the final image dump after the metadata and heap are dumped.
1128 // This affects only JITed code because it may have embedded oops and metadata pointers
1129 // which AOT code encodes as offsets in final CDS archive regions.
1130
1131 static bool _is_dumping_aot_code = false;
1132
1133 bool CDSConfig::is_dumping_aot_code() {
1134 return _is_dumping_aot_code;
|