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