< prev index next >

src/hotspot/share/cds/cdsConfig.cpp

Print this page

  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 "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   }
 65 
 66   // Initialize shared archive paths which could include both base and dynamic archive paths
 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() {

117     vm_exit_during_initialization("Base archive was not specified", archive_path);
118   }
119   size_t len = end_ptr - begin_ptr;
120   char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
121   strncpy(cur_path, begin_ptr, len);
122   cur_path[len] = '\0';
123   *base_archive_path = cur_path;
124 
125   begin_ptr = ++end_ptr;
126   if (*begin_ptr == '\0') {
127     vm_exit_during_initialization("Top archive was not specified", archive_path);
128   }
129   end_ptr = strchr(begin_ptr, '\0');
130   assert(end_ptr != nullptr, "sanity");
131   len = end_ptr - begin_ptr;
132   cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
133   strncpy(cur_path, begin_ptr, len + 1);
134   *top_archive_path = cur_path;
135 }
136 







137 void CDSConfig::init_shared_archive_paths() {
138   if (ArchiveClassesAtExit != nullptr) {
139     assert(!RecordDynamicDumpInfo, "already checked");
140     if (is_dumping_static_archive()) {
141       vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump");
142     }
143     check_unsupported_dumping_module_options();
144 
145     if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) {
146       vm_exit_during_initialization(
147         "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path());
148     }
149   }
150 
151   if (SharedArchiveFile == nullptr) {
152     _static_archive_path = default_archive_path();
153   } else {
154     int archives = num_archives(SharedArchiveFile);
155     assert(archives > 0, "must be");
156 
157     if (is_dumping_archive() && archives > 1) {
158       vm_exit_during_initialization(
159         "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping");
160     }
161 




162     if (is_dumping_static_archive()) {
163       assert(archives == 1, "must be");
164       // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file
165       // will be overwritten no matter regardless of its contents
166       _static_archive_path = os::strdup_check_oom(SharedArchiveFile, mtArguments);
167     } else {
168       // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa
169       // is read from top.jsa
170       //    (a) 1 file:  -XX:SharedArchiveFile=base.jsa
171       //    (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa
172       //    (c) 2 files: -XX:SharedArchiveFile=top.jsa
173       //
174       // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not
175       // allow cases (b) and (c). Case (b) is already checked above.
176 
177       if (archives > 2) {
178         vm_exit_during_initialization(
179           "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option");
180       }
181       if (archives == 1) {
182         char* base_archive_path = nullptr;
183         bool success =
184           FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path);
185         if (!success) {




186           // If +AutoCreateSharedArchive and the specified shared archive does not exist,
187           // regenerate the dynamic archive base on default archive.
188           if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) {
189             enable_dumping_dynamic_archive();
190             ArchiveClassesAtExit = const_cast<char *>(SharedArchiveFile);
191             _static_archive_path = default_archive_path();
192             SharedArchiveFile = nullptr;
193           } else {
194             if (AutoCreateSharedArchive) {
195               warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
196               AutoCreateSharedArchive = false;
197             }
198             Arguments::no_shared_spaces("invalid archive");
199           }
200         } else if (base_archive_path == nullptr) {
201           // User has specified a single archive, which is a static archive.
202           _static_archive_path = const_cast<char *>(SharedArchiveFile);
203         } else {
204           // User has specified a single archive, which is a dynamic archive.
205           _dynamic_archive_path = const_cast<char *>(SharedArchiveFile);

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 
349   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
350   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
351     jio_fprintf(defaultStream::output_stream(),
352                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
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     }

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;
427     return false;
428   }
429 }
430 
431 void CDSConfig::stop_dumping_full_module_graph(const char* reason) {
432   if (_is_dumping_full_module_graph) {
433     _is_dumping_full_module_graph = false;
434     if (reason != nullptr) {
435       log_info(cds)("full module graph cannot be dumped: %s", reason);
436     }
437   }
438 }
439 
440 void CDSConfig::stop_using_full_module_graph(const char* reason) {
441   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
442   if (_is_using_full_module_graph) {
443     _is_using_full_module_graph = false;
444     if (reason != nullptr) {
445       log_info(cds)("full module graph cannot be loaded: %s", reason);
446     }
447   }
448 }
























449 #endif // INCLUDE_CDS_JAVA_HEAP

















  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 "precompiled.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/heapShared.hpp"
 31 #include "cds/metaspaceShared.hpp"
 32 #include "classfile/classLoaderDataShared.hpp"
 33 #include "classfile/moduleEntry.hpp"
 34 #include "classfile/systemDictionaryShared.hpp"
 35 #include "include/jvm_io.h"
 36 #include "logging/log.hpp"
 37 #include "prims/jvmtiExport.hpp"
 38 #include "memory/universe.hpp"
 39 #include "runtime/arguments.hpp"
 40 #include "runtime/globals_extension.hpp"
 41 #include "runtime/java.hpp"
 42 #include "utilities/defaultStream.hpp"
 43 
 44 bool CDSConfig::_is_dumping_static_archive = false;
 45 bool CDSConfig::_is_dumping_dynamic_archive = false;
 46 bool CDSConfig::_is_using_optimized_module_handling = true;
 47 bool CDSConfig::_is_dumping_full_module_graph = true;
 48 bool CDSConfig::_is_using_full_module_graph = true;
 49 bool CDSConfig::_has_preloaded_classes = false;
 50 bool CDSConfig::_is_loading_invokedynamic = false;
 51 bool CDSConfig::_is_loading_packages = false;
 52 
 53 char* CDSConfig::_default_archive_path = nullptr;
 54 char* CDSConfig::_static_archive_path = nullptr;
 55 char* CDSConfig::_dynamic_archive_path = nullptr;
 56 
 57 int CDSConfig::get_status() {
 58   assert(Universe::is_fully_initialized(), "status is finalized only after Universe is initialized");
 59   return (is_dumping_archive()              ? IS_DUMPING_ARCHIVE : 0) |
 60          (is_dumping_static_archive()       ? IS_DUMPING_STATIC_ARCHIVE : 0) |
 61          (is_logging_lambda_form_invokers() ? IS_LOGGING_LAMBDA_FORM_INVOKERS : 0) |
 62          (is_using_archive()                ? IS_USING_ARCHIVE : 0) |
 63          (is_dumping_heap()                 ? IS_DUMPING_HEAP : 0) |
 64          (is_tracing_dynamic_proxy()        ? IS_LOGGING_DYNAMIC_PROXIES : 0) |
 65          (is_dumping_packages()             ? IS_DUMPING_PACKAGES : 0);
 66 }
 67 

 68 void CDSConfig::initialize() {
 69   if (is_dumping_static_archive() && !is_dumping_final_static_archive()) {
 70     if (RequireSharedSpaces) {
 71       warning("Cannot dump shared archive while using shared archive");
 72     }
 73     UseSharedSpaces = false;
 74   }
 75 
 76   // Initialize shared archive paths which could include both base and dynamic archive paths
 77   // This must be after set_ergonomics_flags() called so flag UseCompressedOops is set properly.
 78   //
 79   // UseSharedSpaces may be disabled if -XX:SharedArchiveFile is invalid.
 80   if (is_dumping_static_archive() || is_using_archive()) {
 81     init_shared_archive_paths();
 82   }
 83 
 84   if (!is_dumping_heap()) {
 85     _is_dumping_full_module_graph = false;
 86   }
 87 }
 88 
 89 char* CDSConfig::default_archive_path() {

127     vm_exit_during_initialization("Base archive was not specified", archive_path);
128   }
129   size_t len = end_ptr - begin_ptr;
130   char* cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
131   strncpy(cur_path, begin_ptr, len);
132   cur_path[len] = '\0';
133   *base_archive_path = cur_path;
134 
135   begin_ptr = ++end_ptr;
136   if (*begin_ptr == '\0') {
137     vm_exit_during_initialization("Top archive was not specified", archive_path);
138   }
139   end_ptr = strchr(begin_ptr, '\0');
140   assert(end_ptr != nullptr, "sanity");
141   len = end_ptr - begin_ptr;
142   cur_path = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
143   strncpy(cur_path, begin_ptr, len + 1);
144   *top_archive_path = cur_path;
145 }
146 
147 static void set_new_workflow_default_CachedCodeFile() {
148   size_t len = strlen(CacheDataStore) + 6;
149   char* file = AllocateHeap(len, mtArguments);
150   jio_snprintf(file, len, "%s.code", CacheDataStore);
151   FLAG_SET_ERGO(CachedCodeFile, file);
152 }
153 
154 void CDSConfig::init_shared_archive_paths() {
155   if (ArchiveClassesAtExit != nullptr) {
156     assert(!RecordDynamicDumpInfo, "already checked");
157     if (is_dumping_static_archive()) {
158       vm_exit_during_initialization("-XX:ArchiveClassesAtExit cannot be used with -Xshare:dump");
159     }
160     check_unsupported_dumping_module_options();
161 
162     if (os::same_files(default_archive_path(), ArchiveClassesAtExit)) {
163       vm_exit_during_initialization(
164         "Cannot specify the default CDS archive for -XX:ArchiveClassesAtExit", default_archive_path());
165     }
166   }
167 
168   if (SharedArchiveFile == nullptr) {
169     _static_archive_path = default_archive_path();
170   } else {
171     int archives = num_archives(SharedArchiveFile);
172     assert(archives > 0, "must be");
173 
174     if (is_dumping_archive() && archives > 1) {
175       vm_exit_during_initialization(
176         "Cannot have more than 1 archive file specified in -XX:SharedArchiveFile during CDS dumping");
177     }
178 
179     if (CDSPreimage != nullptr && archives > 1) {
180       vm_exit_during_initialization("CDSPreimage must point to a single file", CDSPreimage);
181     }
182 
183     if (is_dumping_static_archive()) {
184       assert(archives == 1, "must be");
185       // Static dump is simple: only one archive is allowed in SharedArchiveFile. This file
186       // will be overwritten no matter regardless of its contents
187       _static_archive_path = os::strdup_check_oom(SharedArchiveFile, mtArguments);
188     } else {
189       // SharedArchiveFile may specify one or two files. In case (c), the path for base.jsa
190       // is read from top.jsa
191       //    (a) 1 file:  -XX:SharedArchiveFile=base.jsa
192       //    (b) 2 files: -XX:SharedArchiveFile=base.jsa:top.jsa
193       //    (c) 2 files: -XX:SharedArchiveFile=top.jsa
194       //
195       // However, if either RecordDynamicDumpInfo or ArchiveClassesAtExit is used, we do not
196       // allow cases (b) and (c). Case (b) is already checked above.
197 
198       if (archives > 2) {
199         vm_exit_during_initialization(
200           "Cannot have more than 2 archive files specified in the -XX:SharedArchiveFile option");
201       }
202       if (archives == 1) {
203         char* base_archive_path = nullptr;
204         bool success =
205           FileMapInfo::get_base_archive_name_from_header(SharedArchiveFile, &base_archive_path);
206         if (!success) {
207           if (CDSPreimage != nullptr) {
208             vm_exit_during_initialization("Unable to map shared spaces from CDSPreimage", CDSPreimage);
209           }
210 
211           // If +AutoCreateSharedArchive and the specified shared archive does not exist,
212           // regenerate the dynamic archive base on default archive.
213           if (AutoCreateSharedArchive && !os::file_exists(SharedArchiveFile)) {
214             enable_dumping_dynamic_archive();
215             ArchiveClassesAtExit = const_cast<char *>(SharedArchiveFile);
216             _static_archive_path = default_archive_path();
217             SharedArchiveFile = nullptr;
218           } else {
219             if (AutoCreateSharedArchive) {
220               warning("-XX:+AutoCreateSharedArchive is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info.");
221               AutoCreateSharedArchive = false;
222             }
223             Arguments::no_shared_spaces("invalid archive");
224           }
225         } else if (base_archive_path == nullptr) {
226           // User has specified a single archive, which is a static archive.
227           _static_archive_path = const_cast<char *>(SharedArchiveFile);
228         } else {
229           // User has specified a single archive, which is a dynamic archive.
230           _dynamic_archive_path = const_cast<char *>(SharedArchiveFile);

333   assert(is_using_archive(), "this function is only used with -Xshare:{on,auto}");
334   if (ArchiveClassesAtExit != nullptr) {
335     // dynamic dumping, just return false for now.
336     // check_unsupported_dumping_properties() will be called later to check the same set of
337     // properties, and will exit the VM with the correct error message if the unsupported properties
338     // are used.
339     return false;
340   }
341   const char* option = find_any_unsupported_module_option();
342   if (option != nullptr) {
343     if (RequireSharedSpaces) {
344       warning("CDS is disabled when the %s option is specified.", option);
345     } else {
346       log_info(cds)("CDS is disabled when the %s option is specified.", option);
347     }
348     return true;
349   }
350   return false;
351 }
352 
353 bool CDSConfig::check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line, bool xshare_auto_cmd_line) {
354   if (CacheDataStore != nullptr) {
355     // Leyden temp work-around:
356     //
357     // By default, when using CacheDataStore, use the HeapBasedNarrowOop mode so that
358     // AOT code can be always work regardless of runtime heap range.
359     //
360     // If you are *absolutely sure* that the CompressedOops::mode() will be the same
361     // between training and production runs (e.g., if you specify -Xmx128m
362     // for both training and production runs, and you know the OS will always reserve
363     // the heap under 4GB), you can explicitly disable this with:
364     //     java -XX:-UseCompatibleCompressedOops -XX:CacheDataStore=...
365     // However, this is risky and there's a chance that the production run will be slower
366     // because it is unable to load the AOT code cache.
367     FLAG_SET_ERGO_IF_DEFAULT(UseCompatibleCompressedOops, true);
368 
369     // Leyden temp: make sure the user knows if CDS archive somehow fails to load.
370     if (UseSharedSpaces && !xshare_auto_cmd_line) {
371       log_info(cds)("Enabled -Xshare:on by default for troubleshooting Leyden prototype");
372       RequireSharedSpaces = true;
373     }
374 
375     if (FLAG_IS_DEFAULT(PreloadSharedClasses)) {
376       // New workflow - enable PreloadSharedClasses by default.
377       // TODO: make new workflow work, even when PreloadSharedClasses is false.
378       //
379       // NOTE: in old workflow, we cannot enable PreloadSharedClasses by default. That
380       // should be an opt-in option, per JEP nnn.
381       FLAG_SET_ERGO(PreloadSharedClasses, true);
382     }
383 
384     if (SharedArchiveFile != nullptr) {
385       vm_exit_during_initialization("CacheDataStore and SharedArchiveFile cannot be both specified");
386     }
387     if (!PreloadSharedClasses) {
388       // TODO: in the forked JVM, we should ensure all classes are loaded from the hotspot.cds.preimage.
389       // PreloadSharedClasses only loads the classes for built-in loaders. We need to load the classes
390       // for custom loaders as well.
391       vm_exit_during_initialization("CacheDataStore requires PreloadSharedClasses");
392     }
393 
394     if (CDSPreimage == nullptr) {
395       if (os::file_exists(CacheDataStore) /* && TODO: CDS file is valid*/) {
396         // The CacheDataStore is already up to date. Use it. Also turn on cached code by default.
397         SharedArchiveFile = CacheDataStore;
398         FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true);
399         FLAG_SET_ERGO_IF_DEFAULT(LoadCachedCode, true);
400         if (LoadCachedCode && FLAG_IS_DEFAULT(CachedCodeFile)) {
401           set_new_workflow_default_CachedCodeFile();
402         }
403       } else {
404         // The preimage dumping phase -- run the app and write the preimage file
405         size_t len = strlen(CacheDataStore) + 10;
406         char* preimage = AllocateHeap(len, mtArguments);
407         jio_snprintf(preimage, len, "%s.preimage", CacheDataStore);
408 
409         UseSharedSpaces = false;
410         enable_dumping_static_archive();
411         SharedArchiveFile = preimage;
412         log_info(cds)("CacheDataStore needs to be updated. Writing %s file", SharedArchiveFile);
413 
414         // At VM exit, the module graph may be contaminated with program states. We should rebuild the
415         // module graph when dumping the CDS final image.
416         log_info(cds)("full module graph: disabled when writing CDS preimage");
417         HeapShared::disable_writing();
418         stop_dumping_full_module_graph();
419         FLAG_SET_ERGO(ArchiveInvokeDynamic, false);
420         FLAG_SET_ERGO(ArchivePackages, false);
421 
422         FLAG_SET_ERGO_IF_DEFAULT(RecordTraining, true);
423       }
424     } else {
425       // The final image dumping phase -- load the preimage and write the final image file
426       SharedArchiveFile = CDSPreimage;
427       UseSharedSpaces = true;
428       log_info(cds)("Generate CacheDataStore %s from CDSPreimage %s", CacheDataStore, CDSPreimage);
429       // Force -Xbatch for AOT compilation.
430       if (FLAG_SET_CMDLINE(BackgroundCompilation, false) != JVMFlag::SUCCESS) {
431         return false;
432       }
433       RecordTraining = false; // This will be updated inside MetaspaceShared::preload_and_dump()
434 
435       FLAG_SET_ERGO_IF_DEFAULT(ReplayTraining, true);
436       // Settings for AOT
437       FLAG_SET_ERGO_IF_DEFAULT(StoreCachedCode, true);
438       if (StoreCachedCode && FLAG_IS_DEFAULT(CachedCodeFile)) {
439         set_new_workflow_default_CachedCodeFile();
440         // Cannot dump cached code until metadata and heap are dumped.
441         disable_dumping_cached_code();
442       }
443     }
444   } else {
445     // Old workflow
446     if (CDSPreimage != nullptr) {
447       vm_exit_during_initialization("CDSPreimage must be specified only when CacheDataStore is specified");
448     }
449   }
450 
451   if (FLAG_IS_DEFAULT(UsePermanentHeapObjects)) {
452     if (StoreCachedCode || PreloadSharedClasses) {
453       FLAG_SET_ERGO(UsePermanentHeapObjects, true);
454     }
455   }
456 
457   if (LoadCachedCode) {
458     // This must be true. Cached code is hard-wired to use permanent objects.
459     UsePermanentHeapObjects = true;
460   }
461 
462   if (PreloadSharedClasses) {
463     // If PreloadSharedClasses is specified, enable all these optimizations by default.
464     FLAG_SET_ERGO_IF_DEFAULT(ArchiveDynamicProxies, true);
465     FLAG_SET_ERGO_IF_DEFAULT(ArchiveFieldReferences, true);
466     FLAG_SET_ERGO_IF_DEFAULT(ArchiveInvokeDynamic, true);
467     FLAG_SET_ERGO_IF_DEFAULT(ArchiveLoaderLookupCache, true);
468     FLAG_SET_ERGO_IF_DEFAULT(ArchiveMethodReferences, true);
469     FLAG_SET_ERGO_IF_DEFAULT(ArchivePackages, true);
470     FLAG_SET_ERGO_IF_DEFAULT(ArchiveReflectionData, true);
471   } else {
472     // All of these *might* depend on PreloadSharedClasses. Better be safe than sorry.
473     // TODO: more fine-grained handling.
474     FLAG_SET_ERGO(ArchiveDynamicProxies, false);
475     FLAG_SET_ERGO(ArchiveFieldReferences, false);
476     FLAG_SET_ERGO(ArchiveInvokeDynamic, false);
477     FLAG_SET_ERGO(ArchiveLoaderLookupCache, false);
478     FLAG_SET_ERGO(ArchiveMethodReferences, false);
479     FLAG_SET_ERGO(ArchivePackages, false);
480     FLAG_SET_ERGO(ArchiveReflectionData, false);
481   }
482 
483   if (is_dumping_static_archive()) {
484     if (is_dumping_preimage_static_archive() || is_dumping_final_static_archive()) {
485       // Don't tweak execution mode
486     } else if (!mode_flag_cmd_line) {
487       // By default, -Xshare:dump runs in interpreter-only mode, which is required for deterministic archive.
488       //
489       // If your classlist is large and you don't care about deterministic dumping, you can use
490       // -Xshare:dump -Xmixed to improve dumping speed.
491       Arguments::set_mode_flags(Arguments::_int);
492     } else if (Arguments::mode() == Arguments::_comp) {
493       // -Xcomp may use excessive CPU for the test tiers. Also, -Xshare:dump runs a small and fixed set of
494       // Java code, so there's not much benefit in running -Xcomp.
495       log_info(cds)("reduced -Xcomp to -Xmixed for static dumping");
496       Arguments::set_mode_flags(Arguments::_mixed);
497     }
498 
499     // String deduplication may cause CDS to iterate the strings in different order from one
500     // run to another which resulting in non-determinstic CDS archives.
501     // Disable UseStringDeduplication while dumping CDS archive.
502     UseStringDeduplication = false;
503 
504     Arguments::PropertyList_add(new SystemProperty("java.lang.invoke.MethodHandle.NO_SOFT_CACHE", "true", false));
505   } else {
506     // These flags flag are useful only when dumping static archive (which supports archived heap)
507     ArchiveInvokeDynamic = false;
508     ArchivePackages = false;
509   }
510 
511   // RecordDynamicDumpInfo is not compatible with ArchiveClassesAtExit
512   if (ArchiveClassesAtExit != nullptr && RecordDynamicDumpInfo) {
513     jio_fprintf(defaultStream::output_stream(),
514                 "-XX:+RecordDynamicDumpInfo cannot be used with -XX:ArchiveClassesAtExit.\n");
515     return false;
516   }
517 
518   if (ArchiveClassesAtExit == nullptr && !RecordDynamicDumpInfo) {
519     disable_dumping_dynamic_archive();
520   } else {
521     enable_dumping_dynamic_archive();
522   }
523 
524   if (AutoCreateSharedArchive) {
525     if (SharedArchiveFile == nullptr) {
526       log_warning(cds)("-XX:+AutoCreateSharedArchive requires -XX:SharedArchiveFile");
527       return false;
528     }

530       log_warning(cds)("-XX:+AutoCreateSharedArchive does not work with ArchiveClassesAtExit");
531       return false;
532     }
533   }
534 
535   if (is_using_archive() && patch_mod_javabase) {
536     Arguments::no_shared_spaces("CDS is disabled when " JAVA_BASE_NAME " module is patched.");
537   }
538   if (is_using_archive() && has_unsupported_runtime_module_options()) {
539     UseSharedSpaces = false;
540   }
541 
542   if (is_dumping_archive()) {
543     // Always verify non-system classes during CDS dump
544     if (!BytecodeVerificationRemote) {
545       BytecodeVerificationRemote = true;
546       log_info(cds)("All non-system classes will be verified (-Xverify:remote) during CDS dump time.");
547     }
548   }
549 
550   if (!is_dumping_static_archive() || !PreloadSharedClasses) {
551     // FIXME -- is_dumping_heap() is not yet callable from here, as UseG1GC is not yet set by ergo!
552     //
553     // These optimizations require heap dumping and PreloadSharedClasses, or else
554     // the classes of some archived heap objects may be replaced at runtime.
555     ArchiveInvokeDynamic = false;
556     ArchivePackages = false;
557   }
558 
559   if (!ArchiveInvokeDynamic) {
560     ArchiveReflectionData = false; // reflection data use LambdaForm classes
561   }
562 
563   return true;
564 }
565 bool CDSConfig::is_dumping_classic_static_archive() {
566   return _is_dumping_static_archive && CacheDataStore == nullptr && CDSPreimage == nullptr;
567 }
568 
569 bool CDSConfig::is_dumping_preimage_static_archive() {
570   return _is_dumping_static_archive && CacheDataStore != nullptr && CDSPreimage == nullptr;
571 }
572 
573 bool CDSConfig::is_dumping_final_static_archive() {
574   if (CDSPreimage != nullptr) {
575     assert(CacheDataStore != nullptr, "must be"); // should have been properly initialized by arguments.cpp
576   }
577 
578   // Note: _is_dumping_static_archive is false! // FIXME -- refactor this so it makes more sense!
579   return CacheDataStore != nullptr && CDSPreimage != nullptr;
580 }
581 
582 bool CDSConfig::is_dumping_regenerated_lambdaform_invokers() {
583   if (is_dumping_final_static_archive()) {
584     // Not yet supported in new workflow -- the training data may point
585     // to a method in a lambdaform holder class that was not regenerated
586     // due to JDK-8318064.
587     return false;
588   } else {
589     return is_dumping_archive();
590   }
591 }
592 
593 bool CDSConfig::is_tracing_dynamic_proxy() {
594   return ClassListWriter::is_enabled() || is_dumping_preimage_static_archive();
595 }
596 
597 // Preserve all states that were examined used during dumptime verification, such
598 // that the verification result (pass or fail) cannot be changed at runtime.
599 //
600 // For example, if the verification of ik requires that class A must be a subtype of B,
601 // then this relationship between A and B cannot be changed at runtime. I.e., the app
602 // cannot load alternative versions of A and B such that A is not a subtype of B.
603 bool CDSConfig::preserve_all_dumptime_verification_states(const InstanceKlass* ik) {
604   return PreloadSharedClasses && SystemDictionaryShared::is_builtin(ik);
605 }
606 
607 bool CDSConfig::is_using_archive() {
608   return UseSharedSpaces;
609 }
610 
611 bool CDSConfig::is_logging_lambda_form_invokers() {
612   return ClassListWriter::is_enabled() || is_dumping_dynamic_archive() || is_dumping_preimage_static_archive();
613 }
614 
615 void CDSConfig::stop_using_optimized_module_handling() {
616   _is_using_optimized_module_handling = false;
617   _is_dumping_full_module_graph = false; // This requires is_using_optimized_module_handling()
618   _is_using_full_module_graph = false; // This requires is_using_optimized_module_handling()
619 }
620 
621 #if INCLUDE_CDS_JAVA_HEAP
622 bool CDSConfig::is_dumping_heap() {
623   return is_dumping_static_archive() && !is_dumping_preimage_static_archive()
624     && HeapShared::can_write();
625 }
626 
627 bool CDSConfig::is_loading_heap() {
628   return ArchiveHeapLoader::is_in_use();
629 }
630 
631 bool CDSConfig::is_using_full_module_graph() {
632   if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
633     return true;
634   }
635 
636   if (!_is_using_full_module_graph) {
637     return false;
638   }
639 
640   if (is_using_archive() && ArchiveHeapLoader::can_use()) {
641     // Classes used by the archived full module graph are loaded in JVMTI early phase.
642     assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
643            "CDS should be disabled if early class hooks are enabled");
644     return true;
645   } else {
646     _is_using_full_module_graph = false;
647     return false;
648   }
649 }
650 
651 void CDSConfig::stop_dumping_full_module_graph(const char* reason) {
652   if (_is_dumping_full_module_graph) {
653     _is_dumping_full_module_graph = false;
654     if (reason != nullptr) {
655       log_info(cds)("full module graph cannot be dumped: %s", reason);
656     }
657   }
658 }
659 
660 void CDSConfig::stop_using_full_module_graph(const char* reason) {
661   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
662   if (_is_using_full_module_graph) {
663     _is_using_full_module_graph = false;
664     if (reason != nullptr) {
665       log_info(cds)("full module graph cannot be loaded: %s", reason);
666     }
667   }
668 }
669 
670 bool CDSConfig::is_loading_invokedynamic() {
671   return UseSharedSpaces && is_loading_heap() && _is_loading_invokedynamic;
672 }
673 
674 bool CDSConfig::is_dumping_dynamic_proxy() {
675   return is_dumping_full_module_graph() && is_dumping_invokedynamic();
676 }
677 
678 bool CDSConfig::is_initing_classes_at_dump_time() {
679   return is_dumping_heap() && PreloadSharedClasses;
680 }
681 
682 bool CDSConfig::is_dumping_invokedynamic() {
683   return ArchiveInvokeDynamic && is_dumping_heap();
684 }
685 
686 bool CDSConfig::is_dumping_packages() {
687   return ArchivePackages && is_dumping_heap();
688 }
689 
690 bool CDSConfig::is_loading_packages() {
691   return UseSharedSpaces && is_loading_heap() && _is_loading_packages;
692 }
693 #endif // INCLUDE_CDS_JAVA_HEAP
694 
695 // This is allowed by default. We disable it only in the final image dump before the
696 // metadata and heap are dumped.
697 static bool _is_dumping_cached_code = true;
698 
699 bool CDSConfig::is_dumping_cached_code() {
700   return _is_dumping_cached_code;
701 }
702 
703 void CDSConfig::disable_dumping_cached_code() {
704   _is_dumping_cached_code = false;
705 }
706 
707 void CDSConfig::enable_dumping_cached_code() {
708   _is_dumping_cached_code = true;
709 }
< prev index next >