1 /*
 2  * Copyright (c) 2023, 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 #include "precompiled.hpp"
26 #include "cds/archiveHeapLoader.hpp"
27 #include "cds/cdsConfig.hpp"
28 #include "cds/heapShared.hpp"
29 #include "classfile/classLoaderDataShared.hpp"
30 #include "logging/log.hpp"
31 
32 bool CDSConfig::_is_dumping_static_archive = false;
33 bool CDSConfig::_is_dumping_dynamic_archive = false;
34 
35 // The ability to dump the FMG depends on many factors checked by
36 // is_dumping_full_module_graph(), but can be unconditionally disabled by
37 // _dumping_full_module_graph_disabled. (Ditto for loading the FMG).
38 bool CDSConfig::_dumping_full_module_graph_disabled = false;
39 bool CDSConfig::_loading_full_module_graph_disabled = false;
40 
41 #if INCLUDE_CDS_JAVA_HEAP
42 bool CDSConfig::is_dumping_heap() {
43   // heap dump is not supported in dynamic dump
44   return is_dumping_static_archive() && HeapShared::can_write();
45 }
46 
47 bool CDSConfig::is_dumping_full_module_graph() {
48   if (!_dumping_full_module_graph_disabled &&
49       is_dumping_heap() &&
50       MetaspaceShared::use_optimized_module_handling()) {
51     return true;
52   } else {
53     return false;
54   }
55 }
56 
57 bool CDSConfig::is_loading_full_module_graph() {
58   if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
59     return true;
60   }
61 
62   if (!_loading_full_module_graph_disabled &&
63       UseSharedSpaces &&
64       ArchiveHeapLoader::can_use() &&
65       MetaspaceShared::use_optimized_module_handling()) {
66     // Classes used by the archived full module graph are loaded in JVMTI early phase.
67     assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
68            "CDS should be disabled if early class hooks are enabled");
69     return true;
70   } else {
71     return false;
72   }
73 }
74 
75 void CDSConfig::disable_dumping_full_module_graph(const char* reason) {
76   if (!_dumping_full_module_graph_disabled) {
77     _dumping_full_module_graph_disabled = true;
78     if (reason != nullptr) {
79       log_info(cds)("full module graph cannot be dumped: %s", reason);
80     }
81   }
82 }
83 
84 void CDSConfig::disable_loading_full_module_graph(const char* reason) {
85   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
86   if (!_loading_full_module_graph_disabled) {
87     _loading_full_module_graph_disabled = true;
88     if (reason != nullptr) {
89       log_info(cds)("full module graph cannot be loaded: %s", reason);
90     }
91   }
92 }
93 #endif // INCLUDE_CDS_JAVA_HEAP