1 /*
  2  * Copyright (c) 2023, 2024, 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_dynamic_archive;
 38   static bool _is_using_optimized_module_handling;
 39   static bool _is_dumping_full_module_graph;
 40   static bool _is_using_full_module_graph;
 41   static bool _has_aot_linked_classes;
 42   static bool _has_archived_invokedynamic;
 43 
 44   static char* _default_archive_path;
 45   static char* _static_archive_path;
 46   static char* _dynamic_archive_path;
 47 
 48   static bool  _old_cds_flags_used;
 49 
 50   static JavaThread* _dumper_thread;
 51 #endif
 52 
 53   static void extract_shared_archive_paths(const char* archive_path,
 54                                            char** base_archive_path,
 55                                            char** top_archive_path);
 56   static void init_shared_archive_paths();
 57 
 58   static void check_flag_alias(bool alias_is_default, const char* alias_name);
 59   static void check_flag_aliases();
 60 
 61 public:
 62   // Used by jdk.internal.misc.CDS.getCDSConfigStatus();
 63   static const int IS_DUMPING_ARCHIVE              = 1 << 0;
 64   static const int IS_DUMPING_STATIC_ARCHIVE       = 1 << 1;
 65   static const int IS_LOGGING_LAMBDA_FORM_INVOKERS = 1 << 2;
 66   static const int IS_USING_ARCHIVE                = 1 << 3;
 67   static int get_status() NOT_CDS_RETURN_(0);
 68 
 69   // Initialization and command-line checking
 70   static void initialize() NOT_CDS_RETURN;
 71   static void set_old_cds_flags_used()                       { CDS_ONLY(_old_cds_flags_used = true); }
 72   static bool old_cds_flags_used()                           { return CDS_ONLY(_old_cds_flags_used) NOT_CDS(false); }
 73   static void check_internal_module_property(const char* key, const char* value) NOT_CDS_RETURN;
 74   static void check_incompatible_property(const char* key, const char* value) NOT_CDS_RETURN;
 75   static void check_unsupported_dumping_module_options() NOT_CDS_RETURN;
 76   static bool has_unsupported_runtime_module_options() NOT_CDS_RETURN_(false);
 77   static bool check_vm_args_consistency(bool patch_mod_javabase, bool mode_flag_cmd_line) NOT_CDS_RETURN_(true);
 78 
 79   // --- Basic CDS features
 80 
 81   // archive(s) in general
 82   static bool is_dumping_archive()                           { return is_dumping_static_archive() || is_dumping_dynamic_archive(); }
 83   static bool is_using_archive()                             NOT_CDS_RETURN_(false);
 84   static int num_archives(const char* archive_path)          NOT_CDS_RETURN_(0);
 85 
 86   // static_archive
 87   static bool is_dumping_static_archive()                    { return CDS_ONLY(_is_dumping_static_archive) NOT_CDS(false); }
 88   static void enable_dumping_static_archive()                { CDS_ONLY(_is_dumping_static_archive = true); }
 89 
 90   // dynamic_archive
 91   static bool is_dumping_dynamic_archive()                   { return CDS_ONLY(_is_dumping_dynamic_archive) NOT_CDS(false); }
 92   static void enable_dumping_dynamic_archive()               { CDS_ONLY(_is_dumping_dynamic_archive = true); }
 93   static void disable_dumping_dynamic_archive()              { CDS_ONLY(_is_dumping_dynamic_archive = false); }
 94 
 95   // Misc CDS features
 96   static bool allow_only_single_java_thread()                NOT_CDS_RETURN_(false);
 97 
 98   // optimized_module_handling -- can we skip some expensive operations related to modules?
 99   static bool is_using_optimized_module_handling()           { return CDS_ONLY(_is_using_optimized_module_handling) NOT_CDS(false); }
100   static void stop_using_optimized_module_handling()         NOT_CDS_RETURN;
101 
102   static bool is_logging_lambda_form_invokers()              NOT_CDS_RETURN_(false);
103 
104   static bool is_dumping_aot_linked_classes()                NOT_CDS_JAVA_HEAP_RETURN_(false);
105   static bool is_using_aot_linked_classes()                  NOT_CDS_JAVA_HEAP_RETURN_(false);
106   static void set_has_aot_linked_classes(bool has_aot_linked_classes) NOT_CDS_JAVA_HEAP_RETURN;
107 
108   // archive_path
109 
110   // Points to the classes.jsa in $JAVA_HOME
111   static char* default_archive_path()                        NOT_CDS_RETURN_(nullptr);
112   // The actual static archive  (if any) selected at runtime
113   static const char* static_archive_path()                   { return CDS_ONLY(_static_archive_path) NOT_CDS(nullptr); }
114   // The actual dynamic archive  (if any) selected at runtime
115   static const char* dynamic_archive_path()                  { return CDS_ONLY(_dynamic_archive_path) NOT_CDS(nullptr); }
116 
117   // --- Archived java objects
118 
119   static bool is_dumping_heap()                              NOT_CDS_JAVA_HEAP_RETURN_(false);
120   static bool is_loading_heap()                              NOT_CDS_JAVA_HEAP_RETURN_(false);
121   static bool is_initing_classes_at_dump_time()              NOT_CDS_JAVA_HEAP_RETURN_(false);
122 
123   static bool is_dumping_invokedynamic()                     NOT_CDS_JAVA_HEAP_RETURN_(false);
124   static bool is_loading_invokedynamic()                     NOT_CDS_JAVA_HEAP_RETURN_(false);
125   static void set_has_archived_invokedynamic()               { CDS_JAVA_HEAP_ONLY(_has_archived_invokedynamic = true); }
126 
127   // full_module_graph (requires optimized_module_handling)
128   static bool is_dumping_full_module_graph()                 { return CDS_ONLY(_is_dumping_full_module_graph) NOT_CDS(false); }
129   static bool is_using_full_module_graph()                   NOT_CDS_JAVA_HEAP_RETURN_(false);
130   static void stop_dumping_full_module_graph(const char* reason = nullptr) NOT_CDS_JAVA_HEAP_RETURN;
131   static void stop_using_full_module_graph(const char* reason = nullptr) NOT_CDS_JAVA_HEAP_RETURN;
132 
133 
134   // Some CDS functions assume that they are called only within a single-threaded context. I.e.,
135   // they are called from:
136   //    - The VM thread (e.g., inside VM_PopulateDumpSharedSpace)
137   //    - The thread that performs prepatory steps before switching to the VM thread
138   // Since these two threads never execute concurrently, we can avoid using locks in these CDS
139   // function. For safety, these functions should assert with CDSConfig::current_thread_is_vm_or_dumper().
140   class DumperThreadMark {
141   public:
142     DumperThreadMark(JavaThread* current);
143     ~DumperThreadMark();
144   };
145 
146   static bool current_thread_is_vm_or_dumper() NOT_CDS_RETURN_(false);
147 };
148 
149 #endif // SHARE_CDS_CDSCONFIG_HPP