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 #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 #include "runtime/arguments.hpp"
 32 #include "runtime/globals.hpp"
 33 
 34 bool CDSConfig::_is_dumping_static_archive = false;
 35 bool CDSConfig::_is_dumping_dynamic_archive = false;
 36 
 37 // The ability to dump the FMG depends on many factors checked by
 38 // is_dumping_full_module_graph(), but can be unconditionally disabled by
 39 // _dumping_full_module_graph_disabled. (Ditto for loading the FMG).
 40 bool CDSConfig::_dumping_full_module_graph_disabled = false;
 41 bool CDSConfig::_loading_full_module_graph_disabled = false;
 42 
 43 bool CDSConfig::is_valhalla_preview() {
 44   return Arguments::enable_preview() && EnableValhalla;
 45 }
 46 
 47 
 48 #if INCLUDE_CDS_JAVA_HEAP
 49 bool CDSConfig::is_dumping_heap() {
 50   if (is_valhalla_preview()) {
 51     // Not working yet -- e.g., HeapShared::oop_hash() needs to be implemented for value oops
 52     return false;
 53   }
 54   // heap dump is not supported in dynamic dump
 55   return is_dumping_static_archive() && HeapShared::can_write();
 56 }
 57 
 58 bool CDSConfig::is_dumping_full_module_graph() {
 59   if (!_dumping_full_module_graph_disabled &&
 60       is_dumping_heap() &&
 61       MetaspaceShared::use_optimized_module_handling()) {
 62     return true;
 63   } else {
 64     return false;
 65   }
 66 }
 67 
 68 bool CDSConfig::is_loading_full_module_graph() {
 69   if (ClassLoaderDataShared::is_full_module_graph_loaded()) {
 70     return true;
 71   }
 72 
 73   if (!_loading_full_module_graph_disabled &&
 74       UseSharedSpaces &&
 75       ArchiveHeapLoader::can_use() &&
 76       MetaspaceShared::use_optimized_module_handling()) {
 77     // Classes used by the archived full module graph are loaded in JVMTI early phase.
 78     assert(!(JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()),
 79            "CDS should be disabled if early class hooks are enabled");
 80     return true;
 81   } else {
 82     return false;
 83   }
 84 }
 85 
 86 void CDSConfig::disable_dumping_full_module_graph(const char* reason) {
 87   if (!_dumping_full_module_graph_disabled) {
 88     _dumping_full_module_graph_disabled = true;
 89     if (reason != nullptr) {
 90       log_info(cds)("full module graph cannot be dumped: %s", reason);
 91     }
 92   }
 93 }
 94 
 95 void CDSConfig::disable_loading_full_module_graph(const char* reason) {
 96   assert(!ClassLoaderDataShared::is_full_module_graph_loaded(), "you call this function too late!");
 97   if (!_loading_full_module_graph_disabled) {
 98     _loading_full_module_graph_disabled = true;
 99     if (reason != nullptr) {
100       log_info(cds)("full module graph cannot be loaded: %s", reason);
101     }
102   }
103 }
104 #endif // INCLUDE_CDS_JAVA_HEAP