8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotLogging.hpp"
26 #include "cds/archiveHeapLoader.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/classListWriter.hpp"
29 #include "cds/filemap.hpp"
30 #include "cds/heapShared.hpp"
31 #include "classfile/classLoaderDataShared.hpp"
32 #include "classfile/moduleEntry.hpp"
33 #include "include/jvm_io.h"
34 #include "logging/log.hpp"
35 #include "memory/universe.hpp"
36 #include "prims/jvmtiAgentList.hpp"
37 #include "runtime/arguments.hpp"
38 #include "runtime/globals_extension.hpp"
39 #include "runtime/java.hpp"
40 #include "runtime/vmThread.hpp"
41 #include "utilities/defaultStream.hpp"
42 #include "utilities/formatBuffer.hpp"
43
44 bool CDSConfig::_is_dumping_static_archive = false;
45 bool CDSConfig::_is_dumping_preimage_static_archive = false;
46 bool CDSConfig::_is_dumping_final_static_archive = false;
47 bool CDSConfig::_is_dumping_dynamic_archive = false;
48 bool CDSConfig::_is_using_optimized_module_handling = true;
49 bool CDSConfig::_is_dumping_full_module_graph = true;
50 bool CDSConfig::_is_using_full_module_graph = true;
51 bool CDSConfig::_has_aot_linked_classes = false;
52 bool CDSConfig::_is_single_command_training = false;
53 bool CDSConfig::_has_temp_aot_config_file = false;
54 bool CDSConfig::_old_cds_flags_used = false;
55 bool CDSConfig::_new_aot_flags_used = false;
56 bool CDSConfig::_disable_heap_dumping = false;
57
58 const char* CDSConfig::_default_archive_path = nullptr;
59 const char* CDSConfig::_input_static_archive_path = nullptr;
60 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
61 const char* CDSConfig::_output_archive_path = nullptr;
62
63 JavaThread* CDSConfig::_dumper_thread = nullptr;
64
65 int CDSConfig::get_status() {
66 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
67 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
68 (is_dumping_method_handles() ? IS_DUMPING_METHOD_HANDLES : 0) |
69 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
70 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
71 (is_using_archive() ? IS_USING_ARCHIVE : 0);
72 }
73
74 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
75
76 void CDSConfig::ergo_initialize() {
77 DEBUG_ONLY(_cds_ergo_initialize_started = true);
78
79 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
80 // Note: -Xshare and -XX:AOTMode flags are mutually exclusive.
81 // - Classic workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
82 // - JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
83 // So we can never come to here with RequireSharedSpaces==true.
84 assert(!RequireSharedSpaces, "sanity");
85
86 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
87 // for sanity, parse all classes from classfiles.
88 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
89 // needs to be changed.
90 UseSharedSpaces = false;
91 }
92
93 // Initialize shared archive paths which could include both base and dynamic archive paths
94 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
95 if (is_dumping_static_archive() || is_using_archive()) {
96 if (new_aot_flags_used()) {
97 ergo_init_aot_paths();
98 } else {
99 ergo_init_classic_archive_paths();
100 }
101 }
102
103 if (!is_dumping_heap()) {
104 _is_dumping_full_module_graph = false;
105 }
106 }
107
108 const char* CDSConfig::default_archive_path() {
109 // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
110 // before CDSConfig::ergo_initialize() is called.
111 assert(_cds_ergo_initialize_started, "sanity");
112 if (_default_archive_path == nullptr) {
113 char jvm_path[JVM_MAXPATHLEN];
114 os::jvm_path(jvm_path, sizeof(jvm_path));
115 char *end = strrchr(jvm_path, *os::file_separator());
116 if (end != nullptr) *end = '\0';
117 stringStream tmp;
118 tmp.print("%s%sclasses", jvm_path, os::file_separator());
119 #ifdef _LP64
120 if (!UseCompressedOops) {
121 tmp.print_raw("_nocoops");
122 }
123 if (UseCompactObjectHeaders) {
124 // Note that generation of xxx_coh.jsa variants require
125 // --enable-cds-archive-coh at build time
263 // Check for case (c)
264 if (RecordDynamicDumpInfo) {
265 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
266 SharedArchiveFile);
267 }
268 if (ArchiveClassesAtExit != nullptr) {
269 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
270 SharedArchiveFile);
271 }
272 }
273
274 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
275 vm_exit_during_initialization(
276 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
277 SharedArchiveFile);
278 }
279 }
280 }
281 }
282
283 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
284 if (Arguments::is_incompatible_cds_internal_module_property(key)) {
285 stop_using_optimized_module_handling();
286 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
287 }
288 }
289
290 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
291 static const char* incompatible_properties[] = {
292 "java.system.class.loader",
293 "jdk.module.showModuleResolution",
294 "jdk.module.validation"
295 };
296
297 for (const char* property : incompatible_properties) {
298 if (strcmp(key, property) == 0) {
299 stop_dumping_full_module_graph();
300 stop_using_full_module_graph();
301 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
302 break;
303 }
304 }
305
306 }
307
308 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
309 static const char* find_any_unsupported_module_option() {
310 // Note that arguments.cpp has translated the command-line options into properties. If we find an
311 // unsupported property, translate it back to its command-line option for better error reporting.
312
313 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
314 // directly specified in the command-line.
315 static const char* unsupported_module_properties[] = {
316 "jdk.module.limitmods",
317 "jdk.module.upgrade.path",
318 "jdk.module.patch.0"
319 };
320 static const char* unsupported_module_options[] = {
321 "--limit-modules",
322 "--upgrade-module-path",
323 "--patch-module"
324 };
325
326 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be");
327 SystemProperty* sp = Arguments::system_properties();
328 while (sp != nullptr) {
329 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) {
330 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) {
331 return unsupported_module_options[i];
332 }
333 }
334 sp = sp->next();
335 }
336
337 return nullptr; // not found
338 }
339
340 void CDSConfig::check_unsupported_dumping_module_options() {
341 assert(is_dumping_archive(), "this function is only used with CDS dump time");
342 const char* option = find_any_unsupported_module_option();
343 if (option != nullptr) {
344 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option);
345 }
346 // Check for an exploded module build in use with -Xshare:dump.
347 if (!Arguments::has_jimage()) {
348 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build");
349 }
350 }
351
352 bool CDSConfig::has_unsupported_runtime_module_options() {
353 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}");
354 if (ArchiveClassesAtExit != nullptr) {
355 // dynamic dumping, just return false for now.
366 if (new_aot_flags_used()) {
367 aot_log_warning(aot)("AOT cache is disabled when the %s option is specified.", option);
368 } else {
369 aot_log_info(aot)("CDS is disabled when the %s option is specified.", option);
370 }
371 }
372 return true;
373 }
374 return false;
375 }
376
377 #define CHECK_NEW_FLAG(f) check_new_flag(FLAG_IS_DEFAULT(f), #f)
378
379 void CDSConfig::check_new_flag(bool new_flag_is_default, const char* new_flag_name) {
380 if (old_cds_flags_used() && !new_flag_is_default) {
381 vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
382 "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, "
383 "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile",
384 new_flag_name));
385 }
386 }
387
388 #define CHECK_SINGLE_PATH(f) check_flag_single_path(#f, f)
389
390 void CDSConfig::check_flag_single_path(const char* flag_name, const char* value) {
391 if (value != nullptr && num_archive_paths(value) != 1) {
392 vm_exit_during_initialization(err_msg("Option %s must specify a single file name", flag_name));
393 }
394 }
395
396 void CDSConfig::check_aot_flags() {
397 if (!FLAG_IS_DEFAULT(DumpLoadedClassList) ||
398 !FLAG_IS_DEFAULT(SharedClassListFile) ||
399 !FLAG_IS_DEFAULT(SharedArchiveFile)) {
400 _old_cds_flags_used = true;
401 }
402
403 // "New" AOT flags must not be mixed with "classic" CDS flags such as -Xshare:dump
404 CHECK_NEW_FLAG(AOTCache);
405 CHECK_NEW_FLAG(AOTCacheOutput);
406 CHECK_NEW_FLAG(AOTConfiguration);
407 CHECK_NEW_FLAG(AOTMode);
408
409 CHECK_SINGLE_PATH(AOTCache);
410 CHECK_SINGLE_PATH(AOTCacheOutput);
411 CHECK_SINGLE_PATH(AOTConfiguration);
412
413 if (FLAG_IS_DEFAULT(AOTCache) && AOTAdapterCaching) {
414 log_debug(aot,codecache,init)("AOTCache is not specified - AOTAdapterCaching is ignored");
415 }
416 if (FLAG_IS_DEFAULT(AOTCache) && AOTStubCaching) {
417 log_debug(aot,codecache,init)("AOTCache is not specified - AOTStubCaching is ignored");
418 }
419
420 bool has_cache = !FLAG_IS_DEFAULT(AOTCache);
421 bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
422 bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
423 bool has_mode = !FLAG_IS_DEFAULT(AOTMode);
424
425 if (!has_cache && !has_cache_output && !has_config && !has_mode) {
426 // AOT flags are not used. Use classic CDS workflow
427 return;
428 }
429
430 if (has_cache && has_cache_output) {
431 vm_exit_during_initialization("Only one of AOTCache or AOTCacheOutput can be specified");
432 }
433
434 if (!has_cache && (!has_mode || strcmp(AOTMode, "auto") == 0)) {
591 void CDSConfig::ergo_init_aot_paths() {
592 assert(_cds_ergo_initialize_started, "sanity");
593 if (is_dumping_static_archive()) {
594 if (is_dumping_preimage_static_archive()) {
595 _output_archive_path = AOTConfiguration;
596 } else {
597 assert(is_dumping_final_static_archive(), "must be");
598 _input_static_archive_path = AOTConfiguration;
599 _output_archive_path = AOTCache;
600 }
601 } else if (is_using_archive()) {
602 if (FLAG_IS_DEFAULT(AOTCache)) {
603 // Only -XX:AOTMode={auto,on} is specified
604 _input_static_archive_path = default_archive_path();
605 } else {
606 _input_static_archive_path = AOTCache;
607 }
608 }
609 }
610
611 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
612 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
613
614 check_aot_flags();
615
616 if (!FLAG_IS_DEFAULT(AOTMode)) {
617 // Using any form of the new AOTMode switch enables enhanced optimizations.
618 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
619 }
620
621 setup_compiler_args();
622
623 if (AOTClassLinking) {
624 // If AOTClassLinking is specified, enable all AOT optimizations by default.
625 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
626 } else {
627 // AOTInvokeDynamicLinking depends on AOTClassLinking.
628 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
629 }
630
631 if (is_dumping_static_archive()) {
632 if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
633 // Don't tweak execution mode
634 } else if (!mode_flag_cmd_line) {
635 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
636 //
637 // If your classlist is large and you don't care about deterministic dumping, you can use
638 // -Xshare:dump -Xmixed to improve dumping speed.
639 Arguments::set_mode_flags(Arguments::_int);
640 } else if (Arguments::mode() == Arguments::_comp) {
641 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
642 // Java code, so there's not much benefit in running -Xcomp.
643 aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping");
644 Arguments::set_mode_flags(Arguments::_mixed);
645 }
646
647 // String deduplication may cause CDS to iterate the strings in different order from one
648 // run to another which resulting in non-determinstic CDS archives.
649 // Disable UseStringDeduplication while dumping CDS archive.
650 UseStringDeduplication = false;
672 aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
673 return false;
674 }
675 }
676
677 if (is_using_archive() && patch_mod_javabase) {
678 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
679 }
680 if (is_using_archive() && has_unsupported_runtime_module_options()) {
681 UseSharedSpaces = false;
682 }
683
684 if (is_dumping_archive()) {
685 // Always verify non-system classes during CDS dump
686 if (!BytecodeVerificationRemote) {
687 BytecodeVerificationRemote = true;
688 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
689 }
690 }
691
692 return true;
693 }
694
695 void CDSConfig::setup_compiler_args() {
696 // AOT profiles are supported only in the JEP 483 workflow.
697 bool can_dump_profiles = AOTClassLinking && new_aot_flags_used();
698
699 if (is_dumping_preimage_static_archive() && can_dump_profiles) {
700 // JEP 483 workflow -- training
701 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
702 FLAG_SET_ERGO(AOTReplayTraining, false);
703 } else if (is_dumping_final_static_archive() && can_dump_profiles) {
704 // JEP 483 workflow -- assembly
705 FLAG_SET_ERGO(AOTRecordTraining, false);
706 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
707 } else if (is_using_archive() && new_aot_flags_used()) {
708 // JEP 483 workflow -- production
709 FLAG_SET_ERGO(AOTRecordTraining, false);
710 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
711 } else {
712 FLAG_SET_ERGO(AOTReplayTraining, false);
713 FLAG_SET_ERGO(AOTRecordTraining, false);
714 }
715 }
716
717 void CDSConfig::prepare_for_dumping() {
718 assert(CDSConfig::is_dumping_archive(), "sanity");
719
720 if (is_dumping_dynamic_archive() && !is_using_archive()) {
721 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
722
723 // This could happen if SharedArchiveFile has failed to load:
724 // - -Xshare:off was specified
725 // - SharedArchiveFile points to an non-existent file.
726 // - SharedArchiveFile points to an archive that has failed CRC check
727 // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
728
729 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
730 if (RecordDynamicDumpInfo) {
731 aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
732 MetaspaceShared::unrecoverable_loading_error();
733 } else {
734 assert(ArchiveClassesAtExit != nullptr, "sanity");
735 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
736 }
737 #undef __THEMSG
738 disable_dumping_dynamic_archive();
739 return;
740 }
741
742 check_unsupported_dumping_module_options();
743 }
744
745 bool CDSConfig::is_dumping_classic_static_archive() {
746 return _is_dumping_static_archive &&
747 !is_dumping_preimage_static_archive() &&
748 !is_dumping_final_static_archive();
749 }
750
751 bool CDSConfig::is_dumping_preimage_static_archive() {
752 return _is_dumping_preimage_static_archive;
753 }
754
755 bool CDSConfig::is_dumping_final_static_archive() {
756 return _is_dumping_final_static_archive;
757 }
758
759 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
760 _is_dumping_dynamic_archive = true;
761 if (output_path == nullptr) {
762 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
763 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
764 // output path.
765 _output_archive_path = nullptr;
766 } else {
767 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
768 }
769 }
770
771 bool CDSConfig::allow_only_single_java_thread() {
772 // See comments in JVM_StartThread()
773 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
774 }
775
776 bool CDSConfig::is_using_archive() {
777 return UseSharedSpaces;
778 }
779
780 bool CDSConfig::is_using_only_default_archive() {
781 return is_using_archive() &&
782 input_static_archive_path() != nullptr &&
783 default_archive_path() != nullptr &&
784 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
785 input_dynamic_archive_path() == nullptr;
786 }
787
788 bool CDSConfig::is_logging_lambda_form_invokers() {
789 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
790 }
791
792 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
793 if (is_dumping_final_static_archive()) {
794 // No need to regenerate -- the lambda form invokers should have been regenerated
795 // in the preimage archive (if allowed)
796 return false;
797 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
798 // The base archive has aot-linked classes that may have AOT-resolved CP references
799 // that point to the lambda form invokers in the base archive. Such pointers will
800 // be invalid if lambda form invokers are regenerated in the dynamic archive.
801 return false;
802 } else if (CDSConfig::is_dumping_method_handles()) {
803 // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
804 return false;
805 } else {
806 return is_dumping_archive();
807 }
808 }
809
945 }
946
947 bool CDSConfig::is_dumping_aot_linked_classes() {
948 if (is_dumping_preimage_static_archive()) {
949 return false;
950 } else if (is_dumping_dynamic_archive()) {
951 return is_using_full_module_graph() && AOTClassLinking;
952 } else if (is_dumping_static_archive()) {
953 return is_dumping_full_module_graph() && AOTClassLinking;
954 } else {
955 return false;
956 }
957 }
958
959 bool CDSConfig::is_using_aot_linked_classes() {
960 // Make sure we have the exact same module graph as in the assembly phase, or else
961 // some aot-linked classes may not be visible so cannot be loaded.
962 return is_using_full_module_graph() && _has_aot_linked_classes;
963 }
964
965 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
966 _has_aot_linked_classes |= has_aot_linked_classes;
967 }
968
969 bool CDSConfig::is_initing_classes_at_dump_time() {
970 return is_dumping_heap() && is_dumping_aot_linked_classes();
971 }
972
973 bool CDSConfig::is_dumping_invokedynamic() {
974 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
975 // objects used by the archive indy callsites may be replaced at runtime.
976 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
977 }
978
979 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
980 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
981 // classes that are used in the implementation of MethodHandles.
982 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
983 // and dynamic proxies.
984 bool CDSConfig::is_dumping_method_handles() {
985 return is_initing_classes_at_dump_time();
986 }
987
988 #endif // INCLUDE_CDS_JAVA_HEAP
989
990 // AOT code generation and its archiving is disabled by default.
991 // We enable it only in the final image dump after the metadata and heap are dumped.
992 // This affects only JITed code because it may have embedded oops and metadata pointers
993 // which AOT code encodes as offsets in final CDS archive regions.
994
995 static bool _is_dumping_aot_code = false;
996
997 bool CDSConfig::is_dumping_aot_code() {
998 return _is_dumping_aot_code;
999 }
1000
1001 void CDSConfig::disable_dumping_aot_code() {
1002 _is_dumping_aot_code = false;
1003 }
1004
1005 void CDSConfig::enable_dumping_aot_code() {
1006 _is_dumping_aot_code = true;
1007 }
1008
1009 bool CDSConfig::is_dumping_adapters() {
1010 return (AOTAdapterCaching && is_dumping_final_static_archive());
1011 }
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/aotLogging.hpp"
26 #include "cds/archiveHeapLoader.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/cds_globals.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/filemap.hpp"
31 #include "cds/heapShared.hpp"
32 #include "cds/metaspaceShared.hpp"
33 #include "classfile/classLoaderDataShared.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "classfile/systemDictionaryShared.hpp"
36 #include "code/aotCodeCache.hpp"
37 #include "include/jvm_io.h"
38 #include "logging/log.hpp"
39 #include "prims/jvmtiExport.hpp"
40 #include "memory/universe.hpp"
41 #include "prims/jvmtiAgentList.hpp"
42 #include "runtime/arguments.hpp"
43 #include "runtime/globals_extension.hpp"
44 #include "runtime/java.hpp"
45 #include "runtime/vmThread.hpp"
46 #include "utilities/defaultStream.hpp"
47 #include "utilities/formatBuffer.hpp"
48
49 bool CDSConfig::_is_dumping_static_archive = false;
50 bool CDSConfig::_is_dumping_preimage_static_archive = false;
51 bool CDSConfig::_is_dumping_final_static_archive = false;
52 bool CDSConfig::_is_dumping_dynamic_archive = false;
53 bool CDSConfig::_is_using_optimized_module_handling = true;
54 bool CDSConfig::_is_dumping_full_module_graph = true;
55 bool CDSConfig::_is_using_full_module_graph = true;
56 bool CDSConfig::_has_aot_linked_classes = false;
57 bool CDSConfig::_is_single_command_training = false;
58 bool CDSConfig::_has_temp_aot_config_file = false;
59 bool CDSConfig::_is_loading_packages = false;
60 bool CDSConfig::_is_loading_protection_domains = false;
61 bool CDSConfig::_is_security_manager_allowed = false;
62 bool CDSConfig::_old_cds_flags_used = false;
63 bool CDSConfig::_new_aot_flags_used = false;
64 bool CDSConfig::_experimental_leyden_flags_used = false;
65 bool CDSConfig::_disable_heap_dumping = false;
66
67 const char* CDSConfig::_default_archive_path = nullptr;
68 const char* CDSConfig::_input_static_archive_path = nullptr;
69 const char* CDSConfig::_input_dynamic_archive_path = nullptr;
70 const char* CDSConfig::_output_archive_path = nullptr;
71
72 JavaThread* CDSConfig::_dumper_thread = nullptr;
73
74 int CDSConfig::get_status() {
75 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
76 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
77 (is_dumping_method_handles() ? IS_DUMPING_METHOD_HANDLES : 0) |
78 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
79 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
80 (is_using_archive() ? IS_USING_ARCHIVE : 0) |
81 (is_dumping_heap() ? IS_DUMPING_HEAP : 0) |
82 (is_logging_dynamic_proxies() ? IS_LOGGING_DYNAMIC_PROXIES : 0) |
83 (is_dumping_packages() ? IS_DUMPING_PACKAGES : 0) |
84 (is_dumping_protection_domains() ? IS_DUMPING_PROTECTION_DOMAINS : 0);
85 }
86
87 DEBUG_ONLY(static bool _cds_ergo_initialize_started = false);
88
89 void CDSConfig::ergo_initialize() {
90 DEBUG_ONLY(_cds_ergo_initialize_started = true);
91
92 if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
93 // If dumping the classic archive, or making an AOT training run (dumping a preimage archive),
94 // for sanity, parse all classes from classfiles.
95 // TODO: in the future, if we want to support re-training on top of an existing AOT cache, this
96 // needs to be changed.
97 if (RequireSharedSpaces) {
98 if (is_experimental_leyden_workflow()) {
99 log_info(cds)("-Xshare:on flag is ignored when creating a CacheDataStore");
100 } else {
101 // -Xshare and -XX:AOTMode flags are mutually exclusive:
102 // Class workflow: -Xshare:on and -Xshare:dump cannot take effect at the same time.
103 // JEP 483 workflow: -XX:AOTMode:record and -XX:AOTMode=on cannot take effect at the same time.
104 ShouldNotReachHere();
105 }
106 }
107 UseSharedSpaces = false;
108 }
109
110 // Initialize shared archive paths which could include both base and dynamic archive paths
111 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
112 if (is_dumping_static_archive() || is_using_archive()) {
113 if (new_aot_flags_used()) {
114 ergo_init_aot_paths();
115 } else if (is_experimental_leyden_workflow()) {
116 ergo_init_experimental_leyden_paths();
117 } else {
118 ergo_init_classic_archive_paths();
119 }
120 }
121
122 if (!is_dumping_heap()) {
123 _is_dumping_full_module_graph = false;
124 }
125
126 #ifdef _LP64
127 //
128 // By default, when using AOTClassLinking, use the CompressedOops::HeapBasedNarrowOop
129 // mode so that AOT code can be always work regardless of runtime heap range.
130 //
131 // If you are *absolutely sure* that the CompressedOops::mode() will be the same
132 // between training and production runs (e.g., if you specify -Xmx128m for
133 // both training and production runs, and you know the OS will always reserve
134 // the heap under 4GB), you can explicitly disable this with:
135 // java -XX:+UnlockDiagnosticVMOptions -XX:-UseCompatibleCompressedOops ...
136 // However, this is risky and there's a chance that the production run will be slower
137 // than expected because it is unable to load the AOT code cache.
138 //
139 if (UseCompressedOops && AOTCodeCache::is_caching_enabled()) {
140 FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true);
141 } else if (!FLAG_IS_DEFAULT(UseCompatibleCompressedOops)) {
142 FLAG_SET_ERGO(UseCompatibleCompressedOops, false);
143 }
144 #endif // _LP64
145 }
146
147 const char* CDSConfig::default_archive_path() {
148 // The path depends on UseCompressedOops, etc, which are set by GC ergonomics just
149 // before CDSConfig::ergo_initialize() is called.
150 assert(_cds_ergo_initialize_started, "sanity");
151 if (_default_archive_path == nullptr) {
152 char jvm_path[JVM_MAXPATHLEN];
153 os::jvm_path(jvm_path, sizeof(jvm_path));
154 char *end = strrchr(jvm_path, *os::file_separator());
155 if (end != nullptr) *end = '\0';
156 stringStream tmp;
157 tmp.print("%s%sclasses", jvm_path, os::file_separator());
158 #ifdef _LP64
159 if (!UseCompressedOops) {
160 tmp.print_raw("_nocoops");
161 }
162 if (UseCompactObjectHeaders) {
163 // Note that generation of xxx_coh.jsa variants require
164 // --enable-cds-archive-coh at build time
302 // Check for case (c)
303 if (RecordDynamicDumpInfo) {
304 vm_exit_during_initialization("-XX:+RecordDynamicDumpInfo is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
305 SharedArchiveFile);
306 }
307 if (ArchiveClassesAtExit != nullptr) {
308 vm_exit_during_initialization("-XX:ArchiveClassesAtExit is unsupported when a dynamic CDS archive is specified in -XX:SharedArchiveFile",
309 SharedArchiveFile);
310 }
311 }
312
313 if (ArchiveClassesAtExit != nullptr && os::same_files(SharedArchiveFile, ArchiveClassesAtExit)) {
314 vm_exit_during_initialization(
315 "Cannot have the same archive file specified for -XX:SharedArchiveFile and -XX:ArchiveClassesAtExit",
316 SharedArchiveFile);
317 }
318 }
319 }
320 }
321
322 static char* bad_module_prop_key = nullptr;
323 static char* bad_module_prop_value = nullptr;
324
325 void CDSConfig::check_internal_module_property(const char* key, const char* value) {
326 if (Arguments::is_incompatible_cds_internal_module_property(key)) {
327 stop_using_optimized_module_handling();
328 if (bad_module_prop_key == nullptr) {
329 // We don't want to print an unconditional warning here, as we are still processing the command line.
330 // A later argument may specify something like -Xshare:off, which makes such a warning irrelevant.
331 //
332 // Instead, we save the info so we can warn when necessary: we are doing it only during CacheDataStore
333 // creation for now, but could add it to other places.
334 bad_module_prop_key = os::strdup(key);
335 bad_module_prop_value = os::strdup(value);
336 }
337 aot_log_info(aot)("optimized module handling: disabled due to incompatible property: %s=%s", key, value);
338 }
339 }
340
341 void CDSConfig::check_incompatible_property(const char* key, const char* value) {
342 static const char* incompatible_properties[] = {
343 "java.system.class.loader",
344 "jdk.module.showModuleResolution",
345 "jdk.module.validation"
346 };
347
348 for (const char* property : incompatible_properties) {
349 if (strcmp(key, property) == 0) {
350 stop_dumping_full_module_graph();
351 stop_using_full_module_graph();
352 aot_log_info(aot)("full module graph: disabled due to incompatible property: %s=%s", key, value);
353 break;
354 }
355 }
356
357 // Match the logic in java/lang/System.java, but we need to know this before the System class is initialized.
358 if (strcmp(key, "java.security.manager") == 0) {
359 if (strcmp(value, "disallowed") != 0) {
360 _is_security_manager_allowed = true;
361 }
362 }
363 }
364
365 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
366 static const char* find_any_unsupported_module_option() {
367 // Note that arguments.cpp has translated the command-line options into properties. If we find an
368 // unsupported property, translate it back to its command-line option for better error reporting.
369
370 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
371 // directly specified in the command-line.
372 static const char* unsupported_module_properties[] = {
373 "jdk.module.limitmods",
374 "jdk.module.upgrade.path",
375 "jdk.module.patch.0"
376 };
377 static const char* unsupported_module_options[] = {
378 "--limit-modules",
379 "--upgrade-module-path",
380 "--patch-module"
381 };
382
383 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be");
384 SystemProperty* sp = Arguments::system_properties();
385 while (sp != nullptr) {
386 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) {
387 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) {
388 return unsupported_module_options[i];
389 }
390 }
391 sp = sp->next();
392 }
393 if (FLAG_IS_DEFAULT(AOTCache) && AOTStubCaching) {
394 log_debug(aot,codecache,init)("AOTCache is not specified - AOTStubCaching is ignored");
395 }
396
397 return nullptr; // not found
398 }
399
400 void CDSConfig::check_unsupported_dumping_module_options() {
401 assert(is_dumping_archive(), "this function is only used with CDS dump time");
402 const char* option = find_any_unsupported_module_option();
403 if (option != nullptr) {
404 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option);
405 }
406 // Check for an exploded module build in use with -Xshare:dump.
407 if (!Arguments::has_jimage()) {
408 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build");
409 }
410 }
411
412 bool CDSConfig::has_unsupported_runtime_module_options() {
413 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}");
414 if (ArchiveClassesAtExit != nullptr) {
415 // dynamic dumping, just return false for now.
426 if (new_aot_flags_used()) {
427 aot_log_warning(aot)("AOT cache is disabled when the %s option is specified.", option);
428 } else {
429 aot_log_info(aot)("CDS is disabled when the %s option is specified.", option);
430 }
431 }
432 return true;
433 }
434 return false;
435 }
436
437 #define CHECK_NEW_FLAG(f) check_new_flag(FLAG_IS_DEFAULT(f), #f)
438
439 void CDSConfig::check_new_flag(bool new_flag_is_default, const char* new_flag_name) {
440 if (old_cds_flags_used() && !new_flag_is_default) {
441 vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
442 "-Xshare:on, -Xshare:auto, -Xshare:off, -Xshare:dump, "
443 "DumpLoadedClassList, SharedClassListFile, or SharedArchiveFile",
444 new_flag_name));
445 }
446 if (experimental_leyden_flags_used() && !new_flag_is_default) {
447 vm_exit_during_initialization(err_msg("Option %s cannot be used at the same time with "
448 "CacheDataStore, CDSManualFinalImage, or CDSPreimage",
449 new_flag_name));
450 }
451 }
452
453 #define CHECK_SINGLE_PATH(f) check_flag_single_path(#f, f)
454
455 void CDSConfig::check_flag_single_path(const char* flag_name, const char* value) {
456 if (value != nullptr && num_archive_paths(value) != 1) {
457 vm_exit_during_initialization(err_msg("Option %s must specify a single file name", flag_name));
458 }
459 }
460
461 void CDSConfig::check_aot_flags() {
462 if (!FLAG_IS_DEFAULT(DumpLoadedClassList) ||
463 !FLAG_IS_DEFAULT(SharedClassListFile) ||
464 !FLAG_IS_DEFAULT(SharedArchiveFile)) {
465 _old_cds_flags_used = true;
466 }
467 if (!FLAG_IS_DEFAULT(CacheDataStore) ||
468 !FLAG_IS_DEFAULT(CDSManualFinalImage) ||
469 !FLAG_IS_DEFAULT(CDSPreimage)) {
470 _experimental_leyden_flags_used = true;
471 }
472
473 // "New" AOT flags must not be mixed with "classic" CDS flags such as -Xshare:dump
474 CHECK_NEW_FLAG(AOTCache);
475 CHECK_NEW_FLAG(AOTCacheOutput);
476 CHECK_NEW_FLAG(AOTConfiguration);
477 CHECK_NEW_FLAG(AOTMode);
478
479 CHECK_SINGLE_PATH(AOTCache);
480 CHECK_SINGLE_PATH(AOTCacheOutput);
481 CHECK_SINGLE_PATH(AOTConfiguration);
482
483 if (FLAG_IS_DEFAULT(AOTCache) &&
484 FLAG_IS_DEFAULT(AOTMode)) {
485 bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
486 bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
487 if (!has_cache_output && !has_config) {
488 // AOT flags are not used. Use classic CDS workflow
489 return;
490 } else if (has_cache_output) {
491 // If AOTCacheOutput has been set, default mode is "record".
492 // Default value for AOTConfiguration, if necessary, will be assigned in check_aotmode_record().
493 FLAG_SET_ERGO(AOTMode, "record");
494 }
495 }
496 if (FLAG_IS_DEFAULT(AOTCache) && AOTStubCaching) {
497 log_debug(aot,codecache,init)("AOTCache is not specified - AOTStubCaching is ignored");
498 }
499
500 bool has_cache = !FLAG_IS_DEFAULT(AOTCache);
501 bool has_cache_output = !FLAG_IS_DEFAULT(AOTCacheOutput);
502 bool has_config = !FLAG_IS_DEFAULT(AOTConfiguration);
503 bool has_mode = !FLAG_IS_DEFAULT(AOTMode);
504
505 if (!has_cache && !has_cache_output && !has_config && !has_mode) {
506 // AOT flags are not used. Use classic CDS workflow
507 return;
508 }
509
510 if (has_cache && has_cache_output) {
511 vm_exit_during_initialization("Only one of AOTCache or AOTCacheOutput can be specified");
512 }
513
514 if (!has_cache && (!has_mode || strcmp(AOTMode, "auto") == 0)) {
671 void CDSConfig::ergo_init_aot_paths() {
672 assert(_cds_ergo_initialize_started, "sanity");
673 if (is_dumping_static_archive()) {
674 if (is_dumping_preimage_static_archive()) {
675 _output_archive_path = AOTConfiguration;
676 } else {
677 assert(is_dumping_final_static_archive(), "must be");
678 _input_static_archive_path = AOTConfiguration;
679 _output_archive_path = AOTCache;
680 }
681 } else if (is_using_archive()) {
682 if (FLAG_IS_DEFAULT(AOTCache)) {
683 // Only -XX:AOTMode={auto,on} is specified
684 _input_static_archive_path = default_archive_path();
685 } else {
686 _input_static_archive_path = AOTCache;
687 }
688 }
689 }
690
691 void CDSConfig::ergo_init_experimental_leyden_paths() {
692 assert(_cds_ergo_initialize_started, "sanity");
693 if (is_dumping_static_archive()) {
694 if (is_dumping_preimage_static_archive()) {
695 _output_archive_path = CDSPreimage;
696 } else {
697 assert(is_dumping_final_static_archive(), "must be");
698 _input_static_archive_path = CDSPreimage;
699 _output_archive_path = CacheDataStore;
700 }
701 } else if (is_using_archive()) {
702 _input_static_archive_path = CacheDataStore;
703 }
704 }
705
706 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line, bool xshare_auto_cmd_line) {
707 assert(!_cds_ergo_initialize_started, "This is called earlier than CDSConfig::ergo_initialize()");
708
709 check_aot_flags();
710
711 if (!FLAG_IS_DEFAULT(AOTMode)) {
712 // Using any form of the new AOTMode switch enables enhanced optimizations.
713 FLAG_SET_ERGO_IF_DEFAULT(AOTClassLinking, true);
714 }
715
716 if (CacheDataStore != nullptr) {
717 if (!setup_experimental_leyden_workflow(xshare_auto_cmd_line)) {
718 return false;
719 }
720 } else {
721 if (CDSPreimage != nullptr) {
722 vm_exit_during_initialization("CDSPreimage must be specified only when CacheDataStore is specified");
723 }
724 }
725
726 setup_compiler_args();
727
728 if (AOTClassLinking) {
729 // If AOTClassLinking is specified, enable all these optimizations by default.
730 FLAG_SET_ERGO_IF_DEFAULT(AOTInvokeDynamicLinking, true);
731 FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
732 FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
733 FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
734 FLAG_SET_ERGO_IF_DEFAULT(ArchiveProtectionDomains, true);
735 FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
736 } else {
737 // All of these *might* depend on AOTClassLinking. Better be safe than sorry.
738 FLAG_SET_ERGO(AOTInvokeDynamicLinking, false);
739 FLAG_SET_ERGO(ArchiveDynamicProxies, false);
740 FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
741 FLAG_SET_ERGO(ArchivePackages, false);
742 FLAG_SET_ERGO(ArchiveProtectionDomains, false);
743 FLAG_SET_ERGO(ArchiveReflectionData, false);
744
745 if (CDSConfig::is_dumping_archive()) {
746 FLAG_SET_ERGO(AOTRecordTraining, false);
747 FLAG_SET_ERGO(AOTReplayTraining, false);
748 AOTCodeCache::disable_caching();
749 }
750 }
751
752 #ifdef _WINDOWS
753 // This optimization is not working on Windows for some reason. See JDK-8338604.
754 FLAG_SET_ERGO(ArchiveReflectionData, false);
755 #endif
756
757 if (is_dumping_static_archive()) {
758 if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
759 // Don't tweak execution mode
760 } else if (!mode_flag_cmd_line) {
761 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
762 //
763 // If your classlist is large and you don't care about deterministic dumping, you can use
764 // -Xshare:dump -Xmixed to improve dumping speed.
765 Arguments::set_mode_flags(Arguments::_int);
766 } else if (Arguments::mode() == Arguments::_comp) {
767 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
768 // Java code, so there's not much benefit in running -Xcomp.
769 aot_log_info(aot)("reduced -Xcomp to -Xmixed for static dumping");
770 Arguments::set_mode_flags(Arguments::_mixed);
771 }
772
773 // String deduplication may cause CDS to iterate the strings in different order from one
774 // run to another which resulting in non-determinstic CDS archives.
775 // Disable UseStringDeduplication while dumping CDS archive.
776 UseStringDeduplication = false;
798 aot_log_warning(aot)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
799 return false;
800 }
801 }
802
803 if (is_using_archive() && patch_mod_javabase) {
804 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
805 }
806 if (is_using_archive() && has_unsupported_runtime_module_options()) {
807 UseSharedSpaces = false;
808 }
809
810 if (is_dumping_archive()) {
811 // Always verify non-system classes during CDS dump
812 if (!BytecodeVerificationRemote) {
813 BytecodeVerificationRemote = true;
814 aot_log_info(aot)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
815 }
816 }
817
818 if (AOTClassLinking) {
819 if (is_dumping_final_static_archive() && !is_dumping_full_module_graph()) {
820 if (bad_module_prop_key != nullptr) {
821 log_warning(cds)("optimized module handling/full module graph: disabled due to incompatible property: %s=%s",
822 bad_module_prop_key, bad_module_prop_value);
823 }
824 if (is_experimental_leyden_workflow()) {
825 vm_exit_during_initialization("CacheDataStore cannot be created because AOTClassLinking is enabled but full module graph is disabled");
826 } else {
827 vm_exit_during_initialization("AOT cache cannot be created because AOTClassLinking is enabled but full module graph is disabled");
828 }
829 }
830 }
831
832 return true;
833 }
834
835 void CDSConfig::setup_compiler_args() {
836 // AOT profiles and AOT-compiled methods are supported only in the JEP 483 workflow.
837 bool can_dump_profile_and_compiled_code = AOTClassLinking && new_aot_flags_used();
838
839 if (is_dumping_preimage_static_archive() && can_dump_profile_and_compiled_code) {
840 // JEP 483 workflow -- training
841 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
842 FLAG_SET_ERGO(AOTReplayTraining, false);
843 AOTCodeCache::disable_caching();
844 } else if (is_dumping_final_static_archive() && can_dump_profile_and_compiled_code) {
845 // JEP 483 workflow -- assembly
846 FLAG_SET_ERGO(AOTRecordTraining, false);
847 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
848 AOTCodeCache::enable_caching();
849 disable_dumping_aot_code(); // Cannot dump aot code until metadata and heap are dumped.
850 } else if (is_using_archive() && new_aot_flags_used()) {
851 // JEP 483 workflow -- production
852 FLAG_SET_ERGO(AOTRecordTraining, false);
853 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
854 AOTCodeCache::enable_caching();
855
856 if (UseSharedSpaces && FLAG_IS_DEFAULT(AOTMode)) {
857 log_info(aot)("Enabled -XX:AOTMode=on by default for troubleshooting Leyden prototype");
858 RequireSharedSpaces = true;
859 }
860 } else {
861 FLAG_SET_ERGO(AOTReplayTraining, false);
862 FLAG_SET_ERGO(AOTRecordTraining, false);
863 AOTCodeCache::disable_caching();
864 }
865 }
866
867 // Ergo set-up of various flags used by the experimental workflow that uses -XX:CacheDataStore. This workflow
868 // is deprecated and will be removed from Leyden.
869 bool CDSConfig::setup_experimental_leyden_workflow(bool xshare_auto_cmd_line) {
870 if (FLAG_IS_DEFAULT(AOTClassLinking)) {
871 FLAG_SET_ERGO(AOTClassLinking, true);
872 }
873
874 if (SharedArchiveFile != nullptr) {
875 vm_exit_during_initialization("CacheDataStore and SharedArchiveFile cannot be both specified");
876 }
877 if (!AOTClassLinking) {
878 vm_exit_during_initialization("CacheDataStore requires AOTClassLinking");
879 }
880
881 if (CDSPreimage == nullptr) {
882 if (os::file_exists(CacheDataStore) /* && TODO: Need to check if CDS file is valid*/) {
883 // The CacheDataStore is already up to date. Use it. Also turn on aot code by default.
884 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
885 AOTCodeCache::enable_caching();
886
887 // Leyden temp: make sure the user knows if CDS archive somehow fails to load.
888 if (UseSharedSpaces && !xshare_auto_cmd_line) {
889 log_info(cds)("Enabled -Xshare:on by default for troubleshooting Leyden prototype");
890 RequireSharedSpaces = true;
891 }
892 } else {
893 // The preimage dumping phase -- run the app and write the preimage file
894 size_t len = strlen(CacheDataStore) + 10;
895 char* preimage = AllocateHeap(len, mtArguments);
896 jio_snprintf(preimage, len, "%s.preimage", CacheDataStore);
897
898 UseSharedSpaces = false;
899 enable_dumping_static_archive();
900 CDSPreimage = preimage;
901 log_info(cds)("CacheDataStore needs to be updated. Writing %s file", CDSPreimage);
902
903 // At VM exit, the module graph may be contaminated with program states. We should rebuild the
904 // module graph when dumping the CDS final image.
905 log_info(cds)("full module graph: disabled when writing CDS preimage");
906 disable_heap_dumping();
907 stop_dumping_full_module_graph();
908 FLAG_SET_ERGO(ArchivePackages, false);
909 FLAG_SET_ERGO(ArchiveProtectionDomains, false);
910 FLAG_SET_ERGO_IF_DEFAULT(AOTRecordTraining, true);
911 _is_dumping_static_archive = true;
912 _is_dumping_preimage_static_archive = true;
913 }
914 } else {
915 // The final image dumping phase -- load the preimage and write the final image file
916 UseSharedSpaces = true;
917 log_info(cds)("Generate CacheDataStore %s from CDSPreimage %s", CacheDataStore, CDSPreimage);
918 // Force -Xbatch for AOT compilation.
919 if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) {
920 return false;
921 }
922 AOTRecordTraining = false; // This will be updated inside MetaspaceShared::preload_and_dump()
923
924 FLAG_SET_ERGO_IF_DEFAULT(AOTReplayTraining, true);
925 // Settings for AOT
926 AOTCodeCache::enable_caching(); // Update default settings
927 if (AOTCodeCache::is_caching_enabled()) {
928 // Cannot dump aot code until metadata and heap are dumped.
929 disable_dumping_aot_code();
930 }
931 _is_dumping_static_archive = true;
932 _is_dumping_final_static_archive = true;
933 }
934
935 return true;
936 }
937
938 void CDSConfig::prepare_for_dumping() {
939 assert(CDSConfig::is_dumping_archive(), "sanity");
940
941 if (is_dumping_dynamic_archive() && !is_using_archive()) {
942 assert(!is_dumping_static_archive(), "cannot be dumping both static and dynamic archives");
943
944 // This could happen if SharedArchiveFile has failed to load:
945 // - -Xshare:off was specified
946 // - SharedArchiveFile points to an non-existent file.
947 // - SharedArchiveFile points to an archive that has failed CRC check
948 // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
949
950 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
951 if (RecordDynamicDumpInfo) {
952 aot_log_error(aot)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
953 MetaspaceShared::unrecoverable_loading_error();
954 } else {
955 assert(ArchiveClassesAtExit != nullptr, "sanity");
956 aot_log_warning(aot)("-XX:ArchiveClassesAtExit" __THEMSG);
957 }
958 #undef __THEMSG
959 disable_dumping_dynamic_archive();
960 return;
961 }
962
963 check_unsupported_dumping_module_options();
964 }
965
966 bool CDSConfig::is_dumping_classic_static_archive() {
967 return _is_dumping_static_archive &&
968 !is_dumping_preimage_static_archive() &&
969 !is_dumping_final_static_archive();
970 }
971
972 bool CDSConfig::is_dumping_preimage_static_archive() {
973 return _is_dumping_preimage_static_archive;
974 }
975
976 bool CDSConfig::is_dumping_preimage_static_archive_with_triggers() {
977 return (!FLAG_IS_DEFAULT(AOTEndTrainingOnMethodEntry)) && is_dumping_preimage_static_archive();
978 }
979
980 bool CDSConfig::is_dumping_final_static_archive() {
981 return _is_dumping_final_static_archive;
982 }
983
984 void CDSConfig::enable_dumping_dynamic_archive(const char* output_path) {
985 _is_dumping_dynamic_archive = true;
986 if (output_path == nullptr) {
987 // output_path can be null when the VM is started with -XX:+RecordDynamicDumpInfo
988 // in anticipation of "jcmd VM.cds dynamic_dump", which will provide the actual
989 // output path.
990 _output_archive_path = nullptr;
991 } else {
992 _output_archive_path = os::strdup_check_oom(output_path, mtArguments);
993 }
994 }
995
996 bool CDSConfig::allow_only_single_java_thread() {
997 // See comments in JVM_StartThread()
998 return is_dumping_classic_static_archive() || is_dumping_final_static_archive();
999 }
1000
1001 bool CDSConfig::is_logging_dynamic_proxies() {
1002 return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
1003 }
1004
1005 // Preserve all states that were examined used during dumptime verification, such
1006 // that the verification result (pass or fail) cannot be changed at runtime.
1007 //
1008 // For example, if the verification of ik requires that class A must be a subtype of B,
1009 // then this relationship between A and B cannot be changed at runtime. I.e., the app
1010 // cannot load alternative versions of A and B such that A is not a subtype of B.
1011 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) {
1012 return is_dumping_aot_linked_classes() && SystemDictionaryShared::is_builtin(ik);
1013 }
1014
1015 bool CDSConfig::is_using_archive() {
1016 return UseSharedSpaces;
1017 }
1018
1019 bool CDSConfig::is_using_only_default_archive() {
1020 return is_using_archive() &&
1021 input_static_archive_path() != nullptr &&
1022 default_archive_path() != nullptr &&
1023 strcmp(input_static_archive_path(), default_archive_path()) == 0 &&
1024 input_dynamic_archive_path() == nullptr;
1025 }
1026
1027 bool CDSConfig::is_logging_lambda_form_invokers() {
1028 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
1029 }
1030
1031 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
1032 if (is_dumping_final_static_archive()) {
1033 // No need to regenerate -- the lambda form invokers should have been regenerated
1034 // in the preimage archive (if allowed)
1035 return false;
1036 } else if (is_dumping_dynamic_archive() && is_using_aot_linked_classes()) {
1037 // The base archive has aot-linked classes that may have AOT-resolved CP references
1038 // that point to the lambda form invokers in the base archive. Such pointers will
1039 // be invalid if lambda form invokers are regenerated in the dynamic archive.
1040 return false;
1041 } else if (CDSConfig::is_dumping_method_handles()) {
1042 // Work around JDK-8310831, as some methods in lambda form holder classes may not get generated.
1043 return false;
1044 } else {
1045 return is_dumping_archive();
1046 }
1047 }
1048
1184 }
1185
1186 bool CDSConfig::is_dumping_aot_linked_classes() {
1187 if (is_dumping_preimage_static_archive()) {
1188 return false;
1189 } else if (is_dumping_dynamic_archive()) {
1190 return is_using_full_module_graph() && AOTClassLinking;
1191 } else if (is_dumping_static_archive()) {
1192 return is_dumping_full_module_graph() && AOTClassLinking;
1193 } else {
1194 return false;
1195 }
1196 }
1197
1198 bool CDSConfig::is_using_aot_linked_classes() {
1199 // Make sure we have the exact same module graph as in the assembly phase, or else
1200 // some aot-linked classes may not be visible so cannot be loaded.
1201 return is_using_full_module_graph() && _has_aot_linked_classes;
1202 }
1203
1204 bool CDSConfig::is_dumping_dynamic_proxies() {
1205 return is_dumping_full_module_graph() && is_dumping_invokedynamic() && ArchiveDynamicProxies;
1206 }
1207
1208 void CDSConfig::set_has_aot_linked_classes(bool has_aot_linked_classes) {
1209 _has_aot_linked_classes |= has_aot_linked_classes;
1210 }
1211
1212 bool CDSConfig::is_initing_classes_at_dump_time() {
1213 return is_dumping_heap() && is_dumping_aot_linked_classes();
1214 }
1215
1216 bool CDSConfig::is_dumping_invokedynamic() {
1217 // Requires is_dumping_aot_linked_classes(). Otherwise the classes of some archived heap
1218 // objects used by the archive indy callsites may be replaced at runtime.
1219 return AOTInvokeDynamicLinking && is_dumping_aot_linked_classes() && is_dumping_heap();
1220 }
1221
1222 bool CDSConfig::is_dumping_packages() {
1223 return ArchivePackages && is_dumping_heap();
1224 }
1225
1226 bool CDSConfig::is_loading_packages() {
1227 return UseSharedSpaces && is_using_full_module_graph() && _is_loading_packages;
1228 }
1229
1230 bool CDSConfig::is_dumping_protection_domains() {
1231 if (_is_security_manager_allowed) {
1232 // For sanity, don't archive PDs. TODO: can this be relaxed?
1233 return false;
1234 }
1235 // Archived PDs for the modules will reference their java.lang.Module, which must
1236 // also be archived.
1237 return ArchiveProtectionDomains && is_dumping_full_module_graph();
1238 }
1239
1240 bool CDSConfig::is_loading_protection_domains() {
1241 if (_is_security_manager_allowed) {
1242 // For sanity, don't used any archived PDs. TODO: can this be relaxed?
1243 return false;
1244 }
1245 return UseSharedSpaces && is_using_full_module_graph() && _is_loading_protection_domains;
1246 }
1247
1248 bool CDSConfig::is_dumping_reflection_data() {
1249 // reflection data use LambdaForm classes
1250 return ArchiveReflectionData && is_dumping_invokedynamic();
1251 }
1252
1253 // When we are dumping aot-linked classes and we are able to write archived heap objects, we automatically
1254 // enable the archiving of MethodHandles. This will in turn enable the archiving of MethodTypes and hidden
1255 // classes that are used in the implementation of MethodHandles.
1256 // Archived MethodHandles are required for higher-level optimizations such as AOT resolution of invokedynamic
1257 // and dynamic proxies.
1258 bool CDSConfig::is_dumping_method_handles() {
1259 return is_initing_classes_at_dump_time();
1260 }
1261
1262 #endif // INCLUDE_CDS_JAVA_HEAP
1263
1264 // AOT code generation and its archiving is disabled by default.
1265 // We enable it only in the final image dump after the metadata and heap are dumped.
1266 // This affects only JITed code because it may have embedded oops and metadata pointers
1267 // which AOT code encodes as offsets in final CDS archive regions.
1268
1269 static bool _is_dumping_aot_code = false;
1270
1271 bool CDSConfig::is_dumping_aot_code() {
1272 return _is_dumping_aot_code;
1273 }
1274
1275 void CDSConfig::disable_dumping_aot_code() {
1276 _is_dumping_aot_code = false;
1277 }
1278
1279 void CDSConfig::enable_dumping_aot_code() {
1280 _is_dumping_aot_code = true;
1281 }
1282
1283 bool CDSConfig::is_experimental_leyden_workflow() {
1284 return CacheDataStore != nullptr || CDSPreimage != nullptr;
1285 }
1286
1287 bool CDSConfig::is_dumping_adapters() {
1288 return (AOTAdapterCaching && is_dumping_final_static_archive());
1289 }
|