Path: blob/master/src/hotspot/share/cds/metaspaceShared.hpp
41144 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_CDS_METASPACESHARED_HPP25#define SHARE_CDS_METASPACESHARED_HPP2627#include "memory/allocation.hpp"28#include "memory/memRegion.hpp"29#include "memory/virtualspace.hpp"30#include "oops/oop.hpp"31#include "utilities/macros.hpp"32#include "utilities/resourceHash.hpp"3334class FileMapInfo;35class outputStream;3637template<class E> class GrowableArray;3839enum MapArchiveResult {40MAP_ARCHIVE_SUCCESS,41MAP_ARCHIVE_MMAP_FAILURE,42MAP_ARCHIVE_OTHER_FAILURE43};4445// Class Data Sharing Support46class MetaspaceShared : AllStatic {47static ReservedSpace _symbol_rs; // used only during -Xshare:dump48static VirtualSpace _symbol_vs; // used only during -Xshare:dump49static bool _has_error_classes;50static bool _archive_loading_failed;51static bool _remapped_readwrite;52static void* _shared_metaspace_static_top;53static intx _relocation_delta;54static char* _requested_base_address;55static bool _use_optimized_module_handling;56static bool _use_full_module_graph;57public:58enum {59// core archive spaces60rw = 0, // read-write shared space in the heap61ro = 1, // read-only shared space in the heap62bm = 2, // relocation bitmaps (freed after file mapping is finished)63num_core_region = 2, // rw and ro64num_non_heap_spaces = 3, // rw and ro and bm6566// mapped java heap regions67first_closed_archive_heap_region = bm + 1,68max_closed_archive_heap_region = 2,69last_closed_archive_heap_region = first_closed_archive_heap_region + max_closed_archive_heap_region - 1,70first_open_archive_heap_region = last_closed_archive_heap_region + 1,71max_open_archive_heap_region = 2,72last_open_archive_heap_region = first_open_archive_heap_region + max_open_archive_heap_region - 1,7374last_valid_region = last_open_archive_heap_region,75n_regions = last_valid_region + 1 // total number of regions76};7778static void prepare_for_dumping() NOT_CDS_RETURN;79static void preload_and_dump() NOT_CDS_RETURN;8081private:82static void preload_and_dump_impl(TRAPS) NOT_CDS_RETURN;83static void preload_classes(TRAPS) NOT_CDS_RETURN;84static int parse_classlist(const char * classlist_path,85TRAPS) NOT_CDS_RETURN_(0);868788public:89static Symbol* symbol_rs_base() {90return (Symbol*)_symbol_rs.base();91}9293static void initialize_for_static_dump() NOT_CDS_RETURN;94static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;95static void post_initialize(TRAPS) NOT_CDS_RETURN;9697static void print_on(outputStream* st);9899static void set_archive_loading_failed() {100_archive_loading_failed = true;101}102103static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);104static void initialize_shared_spaces() NOT_CDS_RETURN;105106// Return true if given address is in the shared metaspace regions (i.e., excluding any107// mapped shared heap regions.)108static bool is_in_shared_metaspace(const void* p) {109return MetaspaceObj::is_shared((const MetaspaceObj*)p);110}111112static address shared_metaspace_top() {113return (address)MetaspaceObj::shared_metaspace_top();114}115116static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN;117118// Return true if given address is in the shared region corresponding to the idx119static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);120121static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false);122123static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;124125// JVM/TI RedefineClasses() support:126// Remap the shared readonly space to shared readwrite, private if127// sharing is enabled. Simply returns true if sharing is not enabled128// or if the remapping has already been done by a prior call.129static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);130static bool remapped_readwrite() {131CDS_ONLY(return _remapped_readwrite);132NOT_CDS(return false);133}134135static bool try_link_class(JavaThread* current, InstanceKlass* ik);136static void link_and_cleanup_shared_classes(TRAPS) NOT_CDS_RETURN;137static bool link_class_for_cds(InstanceKlass* ik, TRAPS) NOT_CDS_RETURN_(false);138static bool linking_required(InstanceKlass* ik) NOT_CDS_RETURN_(false);139140#if INCLUDE_CDS141// Alignment for the 2 core CDS regions (RW/RO) only.142// (Heap region alignments are decided by GC).143static size_t core_region_alignment();144static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik);145// print loaded classes names to file.146static void dump_loaded_classes(const char* file_name, TRAPS);147#endif148149// Allocate a block of memory from the temporary "symbol" region.150static char* symbol_space_alloc(size_t num_bytes);151152// This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump.153// Both the base/top archives are written using this as their base address.154//155// During static dump: _requested_base_address == SharedBaseAddress.156//157// During dynamic dump: _requested_base_address is not always the same as SharedBaseAddress:158// - SharedBaseAddress is used for *reading the base archive*. I.e., CompactHashtable uses159// it to convert offsets to pointers to Symbols in the base archive.160// The base archive may be mapped to an OS-selected address due to ASLR. E.g.,161// you may have SharedBaseAddress == 0x00ff123400000000.162// - _requested_base_address is used for *writing the output archive*. It's usually163// 0x800000000 (unless it was set by -XX:SharedBaseAddress during -Xshare:dump).164static char* requested_base_address() {165return _requested_base_address;166}167168// Non-zero if the archive(s) need to be mapped a non-default location due to ASLR.169static intx relocation_delta() { return _relocation_delta; }170171static bool use_windows_memory_mapping() {172const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true));173//const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc.174return is_windows;175}176177// Can we skip some expensive operations related to modules?178static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); }179static void disable_optimized_module_handling() { _use_optimized_module_handling = false; }180181// Can we use the full archived modue graph?182static bool use_full_module_graph() NOT_CDS_RETURN_(false);183static void disable_full_module_graph() { _use_full_module_graph = false; }184185private:186static void read_extra_data(JavaThread* current, const char* filename) NOT_CDS_RETURN;187static FileMapInfo* open_static_archive();188static FileMapInfo* open_dynamic_archive();189// use_requested_addr: If true (default), attempt to map at the address the190static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,191bool use_requested_addr);192static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo,193FileMapInfo* dynamic_mapinfo,194bool use_archive_base_addr,195ReservedSpace& total_space_rs,196ReservedSpace& archive_space_rs,197ReservedSpace& class_space_rs);198static void release_reserved_spaces(ReservedSpace& total_space_rs,199ReservedSpace& archive_space_rs,200ReservedSpace& class_space_rs);201static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs);202static void unmap_archive(FileMapInfo* mapinfo);203};204#endif // SHARE_CDS_METASPACESHARED_HPP205206207