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 "precompiled.hpp"
26 #include "cds/archiveHeapLoader.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/classListWriter.hpp"
29 #include "cds/heapShared.hpp"
30 #include "classfile/classLoaderDataShared.hpp"
31 #include "classfile/moduleEntry.hpp"
32 #include "include/jvm_io.h"
33 #include "logging/log.hpp"
34 #include "memory/universe.hpp"
35 #include "runtime/arguments.hpp"
36 #include "runtime/java.hpp"
37 #include "utilities/defaultStream.hpp"
38
39 bool CDSConfig::_is_dumping_static_archive = false;
40 bool CDSConfig::_is_dumping_dynamic_archive = false;
41 bool CDSConfig::_is_using_optimized_module_handling = true;
42 bool CDSConfig::_is_dumping_full_module_graph = true;
43 bool CDSConfig::_is_using_full_module_graph = true;
44
45 char* CDSConfig::_default_archive_path = nullptr;
46 char* CDSConfig::_static_archive_path = nullptr;
47 char* CDSConfig::_dynamic_archive_path = nullptr;
48
49 int CDSConfig::get_status() {
50 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
51 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
52 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
53 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
54 (is_using_archive() ? IS_USING_ARCHIVE : 0);
55 }
56
57
58 void CDSConfig::initialize() {
59 if (is_dumping_static_archive()) {
60 if (RequireSharedSpaces) {
61 warning("Cannot dump shared archive while using shared archive");
62 }
63 UseSharedSpaces = false;
64 }
67 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
68 //
69 // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid.
70 if (is_dumping_static_archive() || is_using_archive()) {
71 init_shared_archive_paths();
72 }
73
74 if (!is_dumping_heap()) {
75 _is_dumping_full_module_graph = false;
76 }
77 }
78
79 char* CDSConfig::default_archive_path() {
80 if (_default_archive_path == nullptr) {
81 char jvm_path[JVM_MAXPATHLEN];
82 os::jvm_path(jvm_path, sizeof(jvm_path));
83 char *end = strrchr(jvm_path, *os::file_separator());
84 if (end != nullptr) *end = '\0';
85 size_t jvm_path_len = strlen(jvm_path);
86 size_t file_sep_len = strlen(os::file_separator());
87 const size_t len = jvm_path_len + file_sep_len + 20;
88 _default_archive_path = NEW_C_HEAP_ARRAY(char, len, mtArguments);
89 jio_snprintf(_default_archive_path, len,
90 LP64_ONLY(!UseCompressedOops ? "%s%sclasses_nocoops.jsa":) "%s%sclasses.jsa",
91 jvm_path, os::file_separator());
92 }
93 return _default_archive_path;
94 }
95
96 int CDSConfig::num_archives(const char* archive_path) {
97 if (archive_path == nullptr) {
98 return 0;
99 }
100 int npaths = 1;
101 char* p = (char*)archive_path;
102 while (*p != '\0') {
103 if (*p == os::path_separator()[0]) {
104 npaths++;
105 }
106 p++;
107 }
108 return npaths;
109 }
110
111 void CDSConfig::extract_shared_archive_paths(const char* archive_path,
252 for (const char* property : incompatible_properties) {
253 if (strcmp(key, property) == 0) {
254 stop_dumping_full_module_graph();
255 stop_using_full_module_graph();
256 log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value);
257 break;
258 }
259 }
260
261 }
262
263 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
264 static const char* find_any_unsupported_module_option() {
265 // Note that arguments.cpp has translated the command-line options into properties. If we find an
266 // unsupported property, translate it back to its command-line option for better error reporting.
267
268 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
269 // directly specified in the command-line.
270 static const char* unsupported_module_properties[] = {
271 "jdk.module.limitmods",
272 "jdk.module.upgrade.path",
273 "jdk.module.patch.0"
274 };
275 static const char* unsupported_module_options[] = {
276 "--limit-modules",
277 "--upgrade-module-path",
278 "--patch-module"
279 };
280
281 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be");
282 SystemProperty* sp = Arguments::system_properties();
283 while (sp != nullptr) {
284 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) {
285 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) {
286 return unsupported_module_options[i];
287 }
288 }
289 sp = sp->next();
290 }
291
292 return nullptr; // not found
293 }
294
295 void CDSConfig::check_unsupported_dumping_module_options() {
296 assert(is_dumping_archive(), "this function is only used with CDS dump time");
297 const char* option = find_any_unsupported_module_option();
298 if (option != nullptr) {
299 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option);
300 }
301 // Check for an exploded module build in use with -Xshare:dump.
302 if (!Arguments::has_jimage()) {
303 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build");
304 }
305 }
306
307 bool CDSConfig::has_unsupported_runtime_module_options() {
308 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}");
309 if (ArchiveClassesAtExit != nullptr) {
310 // dynamic dumping, just return false for now.
311 // check_unsupported_dumping_properties() will be called later to check the same set of
312 // properties, and will exit the VM with the correct error message if the unsupported properties
313 // are used.
314 return false;
315 }
316 const char* option = find_any_unsupported_module_option();
317 if (option != nullptr) {
318 if (RequireSharedSpaces) {
319 warning("CDS is disabled when the %s option is specified.", option);
320 } else {
321 log_info(cds)("CDS is disabled when the %s option is specified.", option);
322 }
323 return true;
324 }
325 return false;
326 }
327
328 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) {
329 if (is_dumping_static_archive()) {
330 if (!mode_flag_cmd_line) {
331 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
332 //
333 // If your classlist is large and you don't care about deterministic dumping, you can use
334 // -Xshare:dump -Xmixed to improve dumping speed.
335 Arguments::set_mode_flags(Arguments::_int);
336 } else if (Arguments::mode() == Arguments::_comp) {
337 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
338 // Java code, so there's not much benefit in running -Xcomp.
339 log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
340 Arguments::set_mode_flags(Arguments::_mixed);
341 }
342
343 // String deduplication may cause CDS to iterate the strings in different order from one
344 // run to another which resulting in non-determinstic CDS archives.
345 // Disable UseStringDeduplication while dumping CDS archive.
346 UseStringDeduplication = false;
347 }
348
353 return false;
354 }
355
356 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
357 disable_dumping_dynamic_archive();
358 } else {
359 enable_dumping_dynamic_archive();
360 }
361
362 if (AutoCreateSharedArchive) {
363 if (SharedArchiveFile == nullptr) {
364 log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
365 return false;
366 }
367 if (ArchiveClassesAtExit != nullptr) {
368 log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
369 return false;
370 }
371 }
372
373 if (is_using_archive() && patch_mod_javabase) {
374 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
375 }
376 if (is_using_archive() && has_unsupported_runtime_module_options()) {
377 UseSharedSpaces = false;
378 }
379
380 if (is_dumping_archive()) {
381 // Always verify non-system classes during CDS dump
382 if (!BytecodeVerificationRemote) {
383 BytecodeVerificationRemote = true;
384 log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
385 }
386 }
387
388 return true;
389 }
390
391 bool CDSConfig::is_using_archive() {
392 return UseSharedSpaces;
393 }
394
395 bool CDSConfig::is_logging_lambda_form_invokers() {
396 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
397 }
398
399 void CDSConfig::stop_using_optimized_module_handling() {
400 _is_using_optimized_module_handling = false;
401 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
402 _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling()
403 }
404
405 #if INCLUDE_CDS_JAVA_HEAP
406 bool CDSConfig::is_dumping_heap() {
407 // heap dump is not supported in dynamic dump
408 return is_dumping_static_archive() && HeapShared::can_write();
409 }
410
411 bool CDSConfig::is_using_full_module_graph() {
412 if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
413 return true;
414 }
415
416 if (!_is_using_full_module_graph) {
417 return false;
418 }
419
420 if (is_using_archive() && ArchiveHeapLoader::can_use()) {
421 // Classes used by the archived full module graph are loaded in JVMTI early phase.
422 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
423 "CDS should be disabled if early class hooks are enabled");
424 return true;
425 } else {
426 _is_using_full_module_graph = false;
|
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 "precompiled.hpp"
26 #include "cds/archiveHeapLoader.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/classListWriter.hpp"
29 #include "cds/heapShared.hpp"
30 #include "classfile/classLoaderDataShared.hpp"
31 #include "classfile/moduleEntry.hpp"
32 #include "include/jvm_io.h"
33 #include "logging/log.hpp"
34 #include "memory/universe.hpp"
35 #include "runtime/arguments.hpp"
36 #include "runtime/globals.hpp"
37 #include "runtime/java.hpp"
38 #include "utilities/defaultStream.hpp"
39
40 bool CDSConfig::_is_dumping_static_archive = false;
41 bool CDSConfig::_is_dumping_dynamic_archive = false;
42 bool CDSConfig::_is_using_optimized_module_handling = true;
43 bool CDSConfig::_is_dumping_full_module_graph = true;
44 bool CDSConfig::_is_using_full_module_graph = true;
45
46 bool CDSConfig::_module_patching_disables_cds = false;
47 bool CDSConfig::_java_base_module_patching_disables_cds = false;
48
49 bool CDSConfig::is_valhalla_preview() {
50 return Arguments::enable_preview() && EnableValhalla;
51 }
52
53
54 char* CDSConfig::_default_archive_path = nullptr;
55 char* CDSConfig::_static_archive_path = nullptr;
56 char* CDSConfig::_dynamic_archive_path = nullptr;
57
58 int CDSConfig::get_status() {
59 assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
60 return (is_dumping_archive() ? IS_DUMPING_ARCHIVE : 0) |
61 (is_dumping_static_archive() ? IS_DUMPING_STATIC_ARCHIVE : 0) |
62 (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
63 (is_using_archive() ? IS_USING_ARCHIVE : 0);
64 }
65
66
67 void CDSConfig::initialize() {
68 if (is_dumping_static_archive()) {
69 if (RequireSharedSpaces) {
70 warning("Cannot dump shared archive while using shared archive");
71 }
72 UseSharedSpaces = false;
73 }
76 // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
77 //
78 // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid.
79 if (is_dumping_static_archive() || is_using_archive()) {
80 init_shared_archive_paths();
81 }
82
83 if (!is_dumping_heap()) {
84 _is_dumping_full_module_graph = false;
85 }
86 }
87
88 char* CDSConfig::default_archive_path() {
89 if (_default_archive_path == nullptr) {
90 char jvm_path[JVM_MAXPATHLEN];
91 os::jvm_path(jvm_path, sizeof(jvm_path));
92 char *end = strrchr(jvm_path, *os::file_separator());
93 if (end != nullptr) *end = '\0';
94 size_t jvm_path_len = strlen(jvm_path);
95 size_t file_sep_len = strlen(os::file_separator());
96 const size_t len = jvm_path_len + file_sep_len + strlen("classes_nocoops_valhalla.jsa") + 1;
97 _default_archive_path = NEW_C_HEAP_ARRAY(char, len, mtArguments);
98 LP64_ONLY(bool nocoops = !UseCompressedOops);
99 NOT_LP64(bool nocoops = false);
100 bool valhalla = is_valhalla_preview();
101 jio_snprintf(_default_archive_path, len, "%s%sclasses%s%s.jsa",
102 jvm_path, os::file_separator(),
103 nocoops ? "_nocoops" : "",
104 valhalla ? "_valhalla" : "");
105 }
106 return _default_archive_path;
107 }
108
109 int CDSConfig::num_archives(const char* archive_path) {
110 if (archive_path == nullptr) {
111 return 0;
112 }
113 int npaths = 1;
114 char* p = (char*)archive_path;
115 while (*p != '\0') {
116 if (*p == os::path_separator()[0]) {
117 npaths++;
118 }
119 p++;
120 }
121 return npaths;
122 }
123
124 void CDSConfig::extract_shared_archive_paths(const char* archive_path,
265 for (const char* property : incompatible_properties) {
266 if (strcmp(key, property) == 0) {
267 stop_dumping_full_module_graph();
268 stop_using_full_module_graph();
269 log_info(cds)("full module graph: disabled due to incompatible property: %s=%s", key, value);
270 break;
271 }
272 }
273
274 }
275
276 // Returns any JVM command-line option, such as "--patch-module", that's not supported by CDS.
277 static const char* find_any_unsupported_module_option() {
278 // Note that arguments.cpp has translated the command-line options into properties. If we find an
279 // unsupported property, translate it back to its command-line option for better error reporting.
280
281 // The following properties are checked by Arguments::is_internal_module_property() and cannot be
282 // directly specified in the command-line.
283 static const char* unsupported_module_properties[] = {
284 "jdk.module.limitmods",
285 "jdk.module.upgrade.path"
286 };
287 static const char* unsupported_module_options[] = {
288 "--limit-modules",
289 "--upgrade-module-path"
290 };
291
292 assert(ARRAY_SIZE(unsupported_module_properties) == ARRAY_SIZE(unsupported_module_options), "must be");
293 SystemProperty* sp = Arguments::system_properties();
294 while (sp != nullptr) {
295 for (uint i = 0; i < ARRAY_SIZE(unsupported_module_properties); i++) {
296 if (strcmp(sp->key(), unsupported_module_properties[i]) == 0) {
297 return unsupported_module_options[i];
298 }
299 }
300 sp = sp->next();
301 }
302
303 return nullptr; // not found
304 }
305
306 void CDSConfig::check_unsupported_dumping_module_options() {
307 assert(is_dumping_archive(), "this function is only used with CDS dump time");
308 const char* option = find_any_unsupported_module_option();
309 if (option != nullptr) {
310 vm_exit_during_initialization("Cannot use the following option when dumping the shared archive", option);
311 }
312
313 if (module_patching_disables_cds()) {
314 vm_exit_during_initialization(
315 "Cannot use the following option when dumping the shared archive", "--patch-module");
316 }
317
318 // Check for an exploded module build in use with -Xshare:dump.
319 if (!Arguments::has_jimage()) {
320 vm_exit_during_initialization("Dumping the shared archive is not supported with an exploded module build");
321 }
322 }
323
324 bool CDSConfig::has_unsupported_runtime_module_options() {
325 assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}");
326 if (ArchiveClassesAtExit != nullptr) {
327 // dynamic dumping, just return false for now.
328 // check_unsupported_dumping_properties() will be called later to check the same set of
329 // properties, and will exit the VM with the correct error message if the unsupported properties
330 // are used.
331 return false;
332 }
333 const char* option = find_any_unsupported_module_option();
334 if (option != nullptr) {
335 if (RequireSharedSpaces) {
336 warning("CDS is disabled when the %s option is specified.", option);
337 } else {
338 log_info(cds)("CDS is disabled when the %s option is specified.", option);
339 }
340 return true;
341 }
342
343 if (module_patching_disables_cds()) {
344 if (RequireSharedSpaces) {
345 warning("CDS is disabled when the %s option is specified.", "--patch-module");
346 } else {
347 log_info(cds)("CDS is disabled when the %s option is specified.", "--patch-module");
348 }
349 return true;
350 }
351
352 return false;
353 }
354
355 bool CDSConfig::check_vm_args_consistency(bool mode_flag_cmd_line) {
356 if (is_dumping_static_archive()) {
357 if (!mode_flag_cmd_line) {
358 // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
359 //
360 // If your classlist is large and you don't care about deterministic dumping, you can use
361 // -Xshare:dump -Xmixed to improve dumping speed.
362 Arguments::set_mode_flags(Arguments::_int);
363 } else if (Arguments::mode() == Arguments::_comp) {
364 // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
365 // Java code, so there's not much benefit in running -Xcomp.
366 log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
367 Arguments::set_mode_flags(Arguments::_mixed);
368 }
369
370 // String deduplication may cause CDS to iterate the strings in different order from one
371 // run to another which resulting in non-determinstic CDS archives.
372 // Disable UseStringDeduplication while dumping CDS archive.
373 UseStringDeduplication = false;
374 }
375
380 return false;
381 }
382
383 if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
384 disable_dumping_dynamic_archive();
385 } else {
386 enable_dumping_dynamic_archive();
387 }
388
389 if (AutoCreateSharedArchive) {
390 if (SharedArchiveFile == nullptr) {
391 log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
392 return false;
393 }
394 if (ArchiveClassesAtExit != nullptr) {
395 log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
396 return false;
397 }
398 }
399
400 if (is_using_archive() && java_base_module_patching_disables_cds() && module_patching_disables_cds()) {
401 Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
402 }
403 if (is_using_archive() && has_unsupported_runtime_module_options()) {
404 UseSharedSpaces = false;
405 }
406
407 if (is_dumping_archive()) {
408 // Always verify non-system classes during CDS dump
409 if (!BytecodeVerificationRemote) {
410 BytecodeVerificationRemote = true;
411 log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
412 }
413 }
414
415 return true;
416 }
417
418 bool CDSConfig::is_using_archive() {
419 return UseSharedSpaces;
420 }
421
422 bool CDSConfig::is_logging_lambda_form_invokers() {
423 return ClassListWriter::is_enabled() || is_dumping_dynamic_archive();
424 }
425
426 void CDSConfig::stop_using_optimized_module_handling() {
427 _is_using_optimized_module_handling = false;
428 _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
429 _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling()
430 }
431
432 #if INCLUDE_CDS_JAVA_HEAP
433 bool CDSConfig::is_dumping_heap() {
434 if (is_valhalla_preview()) {
435 // Not working yet -- e.g., HeapShared::oop_hash() needs to be implemented for value oops
436 return false;
437 }
438 // heap dump is not supported in dynamic dump
439 return is_dumping_static_archive() && HeapShared::can_write();
440 }
441
442 bool CDSConfig::is_using_full_module_graph() {
443 if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
444 return true;
445 }
446
447 if (!_is_using_full_module_graph) {
448 return false;
449 }
450
451 if (is_using_archive() && ArchiveHeapLoader::can_use()) {
452 // Classes used by the archived full module graph are loaded in JVMTI early phase.
453 assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
454 "CDS should be disabled if early class hooks are enabled");
455 return true;
456 } else {
457 _is_using_full_module_graph = false;
|