1 /*
  2  * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CDS_CDSCONFIG_HPP
 26 #define SHARE_CDS_CDSCONFIG_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "utilities/globalDefinitions.hpp"
 30 #include "utilities/macros.hpp"
 31 
 32 class JavaThread;
 33 
 34 class CDSConfig : public AllStatic {
 35 #if INCLUDE_CDS
 36   static bool _is_dumping_static_archive;
 37   static bool _is_dumping_preimage_static_archive;
 38   static bool _is_dumping_final_static_archive;
 39   static bool _is_dumping_dynamic_archive;
 40   static bool _is_using_optimized_module_handling;
 41   static bool _is_dumping_full_module_graph;
 42   static bool _is_using_full_module_graph;
 43   static bool _has_aot_linked_classes;
 44 
 45   static char* _default_archive_path;
 46   static char* _static_archive_path;
 47   static char* _dynamic_archive_path;
 48 
 49   static bool  _old_cds_flags_used;
 50   static bool  _new_aot_flags_used;
 51   static bool  _disable_heap_dumping;
 52 
 53   static JavaThread* _dumper_thread;
 54 #endif
 55 
 56   static void extract_shared_archive_paths(const char* archive_path,
 57                                            char** base_archive_path,
 58                                            char** top_archive_path);
 59   static void init_shared_archive_paths();
 60 
 61   static void check_flag_alias(bool alias_is_default, const char* alias_name);
 62   static void check_aot_flags();
 63   static void check_aotmode_off();
 64   static void check_aotmode_auto_or_on();
 65   static void check_aotmode_record();
 66   static void check_aotmode_create();
 67 
 68 public:
 69   // Used by jdk.internal.misc.CDS.getCDSConfigStatus();
 70   static const int IS_DUMPING_ARCHIVE              = 1 << 0;
 71   static const int IS_DUMPING_METHOD_HANDLES       = 1 << 1;
 72   static const int IS_DUMPING_STATIC_ARCHIVE       = 1 << 2;
 73   static const int IS_LOGGING_LAMBDA_FORM_INVOKERS = 1 << 3;
 74   static const int IS_USING_ARCHIVE                = 1 << 4;
 75 
 76   static int get_status() NOT_CDS_RETURN_(0);
 77 
 78   // Initialization and command-line checking
 79   static void initialize() NOT_CDS_RETURN;
 80   static void set_old_cds_flags_used()                       { CDS_ONLY(_old_cds_flags_used = true); }
 81   static bool old_cds_flags_used()                           { return CDS_ONLY(_old_cds_flags_used) NOT_CDS(false); }
 82   static bool new_aot_flags_used()                           { return CDS_ONLY(_new_aot_flags_used) NOT_CDS(false); }
 83   static void check_internal_module_property(const char* key, const char* value) NOT_CDS_RETURN;
 84   static void check_incompatible_property(const char* key, const char* value) NOT_CDS_RETURN;
 85   static void check_unsupported_dumping_module_options() NOT_CDS_RETURN;
 86   static bool has_unsupported_runtime_module_options() NOT_CDS_RETURN_(false);
 87   static bool check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) NOT_CDS_RETURN_(true);
 88   static const char* type_of_archive_being_loaded();
 89   static const char* type_of_archive_being_written();
 90 
 91   // --- Basic CDS features
 92 
 93   // archive(s) in general
 94   static bool is_dumping_archive()                           { return is_dumping_static_archive() || is_dumping_dynamic_archive(); }
 95   static bool is_using_archive()                             NOT_CDS_RETURN_(false);
 96   static int num_archives(const char* archive_path)          NOT_CDS_RETURN_(0);
 97 
 98   // static_archive
 99   static bool is_dumping_static_archive()                    { return CDS_ONLY(_is_dumping_static_archive) NOT_CDS(false); }
100   static void enable_dumping_static_archive()                { CDS_ONLY(_is_dumping_static_archive = true); }
101 
102   // A static CDS archive can be dumped in three modes:
103   //
104   // "classic"   - This is the traditional CDS workflow of
105   //               "java -Xshare:dump -XX:SharedClassListFile=file.txt".
106   //
107   // "preimage"  - This happens when we execute the JEP 483 training run, e.g:
108   //               "java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconfig -cp app.jar App"
109   //               The above command writes app.aotconfig as a "CDS preimage". This
110   //               is a binary file that contains all the classes loaded during the
111   //               training run, plus profiling data (e.g., the resolved constant pool entries).
112   //
113   // "final"     - This happens when we execute the JEP 483 assembly phase, e.g:
114   //               "java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconfig -XX:AOTCache=app.aot -cp app.jar"
115   //               The above command loads all classes from app.aotconfig, perform additional linking,
116   //               and writes app.aot as a "CDS final image" file.
117   //
118   // The main structural difference between "preimage" and "final" is that the preimage
119   // - has a different magic number (0xcafea07c)
120   // - does not have any archived Java heap objects
121   // - does not have aot-linked classes
122   static bool is_dumping_classic_static_archive()            NOT_CDS_RETURN_(false);
123   static bool is_dumping_preimage_static_archive()           NOT_CDS_RETURN_(false);
124   static bool is_dumping_final_static_archive()              NOT_CDS_RETURN_(false);
125 
126   // dynamic_archive
127   static bool is_dumping_dynamic_archive()                   { return CDS_ONLY(_is_dumping_dynamic_archive) NOT_CDS(false); }
128   static void enable_dumping_dynamic_archive()               { CDS_ONLY(_is_dumping_dynamic_archive = true); }
129   static void disable_dumping_dynamic_archive()              { CDS_ONLY(_is_dumping_dynamic_archive = false); }
130 
131   // Misc CDS features
132   static bool allow_only_single_java_thread()                NOT_CDS_RETURN_(false);
133 
134   // This is *Legacy* optimization for lambdas before JEP 483. May be removed in the future.
135   static bool is_dumping_lambdas_in_legacy_mode()            NOT_CDS_RETURN_(false);
136 
137   // optimized_module_handling -- can we skip some expensive operations related to modules?
138   static bool is_using_optimized_module_handling()           { return CDS_ONLY(_is_using_optimized_module_handling) NOT_CDS(false); }
139   static void stop_using_optimized_module_handling()         NOT_CDS_RETURN;
140 
141   static bool is_logging_lambda_form_invokers()              NOT_CDS_RETURN_(false);
142 
143   static bool is_dumping_aot_linked_classes()                NOT_CDS_JAVA_HEAP_RETURN_(false);
144   static bool is_using_aot_linked_classes()                  NOT_CDS_JAVA_HEAP_RETURN_(false);
145   static void set_has_aot_linked_classes(bool has_aot_linked_classes) NOT_CDS_JAVA_HEAP_RETURN;
146 
147   // archive_path
148 
149   // Points to the classes.jsa in $JAVA_HOME
150   static char* default_archive_path()                        NOT_CDS_RETURN_(nullptr);
151   // The actual static archive  (if any) selected at runtime
152   static const char* static_archive_path()                   { return CDS_ONLY(_static_archive_path) NOT_CDS(nullptr); }
153   // The actual dynamic archive  (if any) selected at runtime
154   static const char* dynamic_archive_path()                  { return CDS_ONLY(_dynamic_archive_path) NOT_CDS(nullptr); }
155 
156   // --- Archived java objects
157 
158   static bool are_vm_options_incompatible_with_dumping_heap() NOT_CDS_JAVA_HEAP_RETURN_(true);
159   static void log_reasons_for_not_dumping_heap();
160 
161   static void disable_heap_dumping()                         { CDS_ONLY(_disable_heap_dumping = true); }
162   static bool is_dumping_heap()                              NOT_CDS_JAVA_HEAP_RETURN_(false);
163   static bool is_loading_heap()                              NOT_CDS_JAVA_HEAP_RETURN_(false);
164   static bool is_initing_classes_at_dump_time()              NOT_CDS_JAVA_HEAP_RETURN_(false);
165 
166   static bool is_dumping_invokedynamic()                     NOT_CDS_JAVA_HEAP_RETURN_(false);
167   static bool is_dumping_method_handles()                    NOT_CDS_JAVA_HEAP_RETURN_(false);
168 
169   // full_module_graph (requires optimized_module_handling)
170   static bool is_dumping_full_module_graph()                 { return CDS_ONLY(_is_dumping_full_module_graph) NOT_CDS(false); }
171   static bool is_using_full_module_graph()                   NOT_CDS_JAVA_HEAP_RETURN_(false);
172   static void stop_dumping_full_module_graph(const char* reason = nullptr) NOT_CDS_JAVA_HEAP_RETURN;
173   static void stop_using_full_module_graph(const char* reason = nullptr) NOT_CDS_JAVA_HEAP_RETURN;
174 
175   // Some CDS functions assume that they are called only within a single-threaded context. I.e.,
176   // they are called from:
177   //    - The VM thread (e.g., inside VM_PopulateDumpSharedSpace)
178   //    - The thread that performs prepatory steps before switching to the VM thread
179   // Since these two threads never execute concurrently, we can avoid using locks in these CDS
180   // function. For safety, these functions should assert with CDSConfig::current_thread_is_vm_or_dumper().
181   class DumperThreadMark {
182   public:
183     DumperThreadMark(JavaThread* current);
184     ~DumperThreadMark();
185   };
186 
187   static bool current_thread_is_vm_or_dumper() NOT_CDS_RETURN_(false);
188 };
189 
190 #endif // SHARE_CDS_CDSCONFIG_HPP