1 /* 2 * Copyright (c) 2012, 2020, 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_MEMORY_METASPACESHARED_HPP 26 #define SHARE_MEMORY_METASPACESHARED_HPP 27 28 #include "classfile/compactHashtable.hpp" 29 #include "memory/allocation.hpp" 30 #include "memory/memRegion.hpp" 31 #include "memory/virtualspace.hpp" 32 #include "oops/oop.hpp" 33 #include "utilities/macros.hpp" 34 #include "utilities/resourceHash.hpp" 35 36 #define MAX_SHARED_DELTA (0x7FFFFFFF) 37 38 class outputStream; 39 class CHeapBitMap; 40 class FileMapInfo; 41 class DumpRegion; 42 struct ArchiveHeapOopmapInfo; 43 44 enum MapArchiveResult { 45 MAP_ARCHIVE_SUCCESS, 46 MAP_ARCHIVE_MMAP_FAILURE, 47 MAP_ARCHIVE_OTHER_FAILURE 48 }; 49 50 class MetaspaceSharedStats { 51 public: 52 MetaspaceSharedStats() { 53 memset(this, 0, sizeof(*this)); 54 } 55 CompactHashtableStats symbol; 56 CompactHashtableStats string; 57 }; 58 59 // Class Data Sharing Support 60 class MetaspaceShared : AllStatic { 61 62 // CDS support 63 64 // Note: _shared_rs and _symbol_rs are only used at dump time. 65 static ReservedSpace _shared_rs; 66 static VirtualSpace _shared_vs; 67 static ReservedSpace _symbol_rs; 68 static VirtualSpace _symbol_vs; 69 static int _max_alignment; 70 static MetaspaceSharedStats _stats; 71 static bool _has_error_classes; 72 static bool _archive_loading_failed; 73 static bool _remapped_readwrite; 74 static address _i2i_entry_code_buffers; 75 static size_t _i2i_entry_code_buffers_size; 76 static size_t _core_spaces_size; 77 static void* _shared_metaspace_static_top; 78 static intx _relocation_delta; 79 static char* _requested_base_address; 80 static bool _use_optimized_module_handling; 81 static bool _use_full_module_graph; 82 public: 83 enum { 84 // core archive spaces 85 mc = 0, // miscellaneous code for method trampolines 86 rw = 1, // read-write shared space in the heap 87 ro = 2, // read-only shared space in the heap 88 bm = 3, // relocation bitmaps (freed after file mapping is finished) 89 num_core_region = 3, 90 num_non_heap_spaces = 4, 91 92 // mapped java heap regions 93 first_closed_archive_heap_region = bm + 1, 94 max_closed_archive_heap_region = 2, 95 last_closed_archive_heap_region = first_closed_archive_heap_region + max_closed_archive_heap_region - 1, 96 first_open_archive_heap_region = last_closed_archive_heap_region + 1, 97 max_open_archive_heap_region = 2, 98 last_open_archive_heap_region = first_open_archive_heap_region + max_open_archive_heap_region - 1, 99 100 last_valid_region = last_open_archive_heap_region, 101 n_regions = last_valid_region + 1 // total number of regions 102 }; 103 104 static void prepare_for_dumping() NOT_CDS_RETURN; 105 static void preload_and_dump(TRAPS) NOT_CDS_RETURN; 106 static int preload_classes(const char * class_list_path, 107 TRAPS) NOT_CDS_RETURN_(0); 108 109 static GrowableArray<Klass*>* collected_klasses(); 110 111 static ReservedSpace* shared_rs() { 112 CDS_ONLY(return &_shared_rs); 113 NOT_CDS(return NULL); 114 } 115 116 static Symbol* symbol_rs_base() { 117 return (Symbol*)_symbol_rs.base(); 118 } 119 120 static void set_shared_rs(ReservedSpace rs) { 121 CDS_ONLY(_shared_rs = rs); 122 } 123 124 static void commit_to(ReservedSpace* rs, VirtualSpace* vs, char* newtop) NOT_CDS_RETURN; 125 static void initialize_dumptime_shared_and_meta_spaces() NOT_CDS_RETURN; 126 static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN; 127 static void post_initialize(TRAPS) NOT_CDS_RETURN; 128 129 static void print_on(outputStream* st); 130 131 // Delta of this object from SharedBaseAddress 132 static uintx object_delta_uintx(void* obj); 133 134 static u4 object_delta_u4(void* obj) { 135 // offset is guaranteed to be less than MAX_SHARED_DELTA in DumpRegion::expand_top_to() 136 uintx deltax = object_delta_uintx(obj); 137 guarantee(deltax <= MAX_SHARED_DELTA, "must be 32-bit offset"); 138 return (u4)deltax; 139 } 140 141 static void set_archive_loading_failed() { 142 _archive_loading_failed = true; 143 } 144 static bool is_in_output_space(void* ptr) { 145 assert(DumpSharedSpaces, "must be"); 146 return shared_rs()->contains(ptr); 147 } 148 149 static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false); 150 static void initialize_shared_spaces() NOT_CDS_RETURN; 151 152 // Return true if given address is in the shared metaspace regions (i.e., excluding any 153 // mapped shared heap regions.) 154 static bool is_in_shared_metaspace(const void* p) { 155 return MetaspaceObj::is_shared((const MetaspaceObj*)p); 156 } 157 158 static address shared_metaspace_top() { 159 return (address)MetaspaceObj::shared_metaspace_top(); 160 } 161 162 static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN; 163 164 // Return true if given address is in the shared region corresponding to the idx 165 static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 166 167 static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false); 168 169 static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false); 170 171 static char* allocate_cpp_vtable_clones(); 172 static void clone_cpp_vtables(intptr_t* p); 173 static void zero_cpp_vtable_clones_for_writing(); 174 static void patch_cpp_vtable_pointers(); 175 static void serialize_cloned_cpp_vtptrs(SerializeClosure* sc); 176 177 static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false); 178 static void serialize(SerializeClosure* sc) NOT_CDS_RETURN; 179 180 static MetaspaceSharedStats* stats() { 181 return &_stats; 182 } 183 184 static void report_out_of_space(const char* name, size_t needed_bytes); 185 186 // JVM/TI RedefineClasses() support: 187 // Remap the shared readonly space to shared readwrite, private if 188 // sharing is enabled. Simply returns true if sharing is not enabled 189 // or if the remapping has already been done by a prior call. 190 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 191 static bool remapped_readwrite() { 192 CDS_ONLY(return _remapped_readwrite); 193 NOT_CDS(return false); 194 } 195 196 static bool try_link_class(InstanceKlass* ik, TRAPS); 197 static void link_and_cleanup_shared_classes(TRAPS) NOT_CDS_RETURN; 198 199 #if INCLUDE_CDS 200 static size_t reserved_space_alignment(); 201 static void init_shared_dump_space(DumpRegion* first_space); 202 static DumpRegion* misc_code_dump_space(); 203 static DumpRegion* read_write_dump_space(); 204 static DumpRegion* read_only_dump_space(); 205 static void pack_dump_space(DumpRegion* current, DumpRegion* next, 206 ReservedSpace* rs); 207 208 static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik); 209 #endif 210 211 // Allocate a block of memory from the temporary "symbol" region. 212 static char* symbol_space_alloc(size_t num_bytes); 213 214 // Allocate a block of memory from the "mc" or "ro" regions. 215 static char* misc_code_space_alloc(size_t num_bytes); 216 static char* read_only_space_alloc(size_t num_bytes); 217 static char* read_write_space_alloc(size_t num_bytes); 218 219 template <typename T> 220 static Array<T>* new_ro_array(int length) { 221 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 222 Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size); 223 array->initialize(length); 224 return array; 225 } 226 227 template <typename T> 228 static Array<T>* new_rw_array(int length) { 229 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 230 Array<T>* array = (Array<T>*)read_write_space_alloc(byte_size); 231 array->initialize(length); 232 return array; 233 } 234 235 template <typename T> 236 static size_t ro_array_bytesize(int length) { 237 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 238 return align_up(byte_size, BytesPerWord); 239 } 240 241 static address i2i_entry_code_buffers(size_t total_size); 242 243 static address i2i_entry_code_buffers() { 244 return _i2i_entry_code_buffers; 245 } 246 static size_t i2i_entry_code_buffers_size() { 247 return _i2i_entry_code_buffers_size; 248 } 249 static void relocate_klass_ptr(oop o); 250 251 static Klass* get_relocated_klass(Klass *k, bool is_final=false); 252 static Symbol* get_relocated_symbol(Symbol* orig_symbol); 253 254 static void allocate_cloned_cpp_vtptrs(); 255 static intptr_t* get_archived_cpp_vtable(MetaspaceObj::Type msotype, address obj); 256 static void initialize_ptr_marker(CHeapBitMap* ptrmap); 257 258 // This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump. 259 // Both the base/top archives are written using this as their base address. 260 static char* requested_base_address() { 261 return _requested_base_address; 262 } 263 264 // Non-zero if the archive(s) need to be mapped a non-default location due to ASLR. 265 static intx relocation_delta() { return _relocation_delta; } 266 static intx final_delta(); 267 static bool use_windows_memory_mapping() { 268 const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true)); 269 //const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc. 270 return is_windows; 271 } 272 273 static void write_core_archive_regions(FileMapInfo* mapinfo, 274 GrowableArray<ArchiveHeapOopmapInfo>* closed_oopmaps, 275 GrowableArray<ArchiveHeapOopmapInfo>* open_oopmaps); 276 277 // Can we skip some expensive operations related to modules? 278 static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); } 279 static void disable_optimized_module_handling() { _use_optimized_module_handling = false; } 280 281 // Can we use the full archived modue graph? 282 static bool use_full_module_graph() NOT_CDS_RETURN_(false); 283 static void disable_full_module_graph() { _use_full_module_graph = false; } 284 285 private: 286 #if INCLUDE_CDS 287 static void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, 288 bool read_only, bool allow_exec); 289 #endif 290 static void read_extra_data(const char* filename, TRAPS) NOT_CDS_RETURN; 291 static FileMapInfo* open_static_archive(); 292 static FileMapInfo* open_dynamic_archive(); 293 // use_requested_addr: If true (default), attempt to map at the address the 294 static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo, 295 bool use_requested_addr); 296 static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo, 297 FileMapInfo* dynamic_mapinfo, 298 bool use_archive_base_addr, 299 ReservedSpace& archive_space_rs, 300 ReservedSpace& class_space_rs); 301 static void release_reserved_spaces(ReservedSpace& archive_space_rs, 302 ReservedSpace& class_space_rs); 303 static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs); 304 static void unmap_archive(FileMapInfo* mapinfo); 305 }; 306 #endif // SHARE_MEMORY_METASPACESHARED_HPP