1 /* 2 * Copyright (c) 2012, 2019, 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/exceptions.hpp" 34 #include "utilities/macros.hpp" 35 #include "utilities/resourceHash.hpp" 36 37 #define MAX_SHARED_DELTA (0x7FFFFFFF) 38 39 class FileMapInfo; 40 41 class MetaspaceSharedStats { 42 public: 43 MetaspaceSharedStats() { 44 memset(this, 0, sizeof(*this)); 45 } 46 CompactHashtableStats symbol; 47 CompactHashtableStats string; 48 }; 49 50 // Class Data Sharing Support 51 class MetaspaceShared : AllStatic { 52 53 // CDS support 54 static ReservedSpace _shared_rs; 55 static VirtualSpace _shared_vs; 56 static int _max_alignment; 57 static MetaspaceSharedStats _stats; 58 static bool _has_error_classes; 59 static bool _archive_loading_failed; 60 static bool _remapped_readwrite; 61 static address _cds_i2i_entry_code_buffers; 62 static size_t _cds_i2i_entry_code_buffers_size; 63 static size_t _core_spaces_size; 64 public: 65 enum { 66 // core archive spaces 67 mc = 0, // miscellaneous code for method trampolines 68 rw = 1, // read-write shared space in the heap 69 ro = 2, // read-only shared space in the heap 70 md = 3, // miscellaneous data for initializing tables, etc. 71 num_core_spaces = 4, // number of non-string regions 72 num_non_heap_spaces = 4, 73 74 // mapped java heap regions 75 first_closed_archive_heap_region = md + 1, 76 max_closed_archive_heap_region = 2, 77 last_closed_archive_heap_region = first_closed_archive_heap_region + max_closed_archive_heap_region - 1, 78 first_open_archive_heap_region = last_closed_archive_heap_region + 1, 79 max_open_archive_heap_region = 2, 80 last_open_archive_heap_region = first_open_archive_heap_region + max_open_archive_heap_region - 1, 81 82 last_valid_region = last_open_archive_heap_region, 83 n_regions = last_valid_region + 1 // total number of regions 84 }; 85 86 static void prepare_for_dumping() NOT_CDS_RETURN; 87 static void preload_and_dump(TRAPS) NOT_CDS_RETURN; 88 static int preload_classes(const char * class_list_path, 89 TRAPS) NOT_CDS_RETURN_(0); 90 91 static GrowableArray<Klass*>* collected_klasses(); 92 93 static ReservedSpace* shared_rs() { 94 CDS_ONLY(return &_shared_rs); 95 NOT_CDS(return NULL); 96 } 97 static void commit_shared_space_to(char* newtop) NOT_CDS_RETURN; 98 static size_t core_spaces_size() { 99 assert(DumpSharedSpaces || UseSharedSpaces, "sanity"); 100 assert(_core_spaces_size != 0, "sanity"); 101 return _core_spaces_size; 102 } 103 static void initialize_dumptime_shared_and_meta_spaces() NOT_CDS_RETURN; 104 static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN; 105 static void post_initialize(TRAPS) NOT_CDS_RETURN; 106 107 // Delta of this object from the bottom of the archive. 108 static uintx object_delta_uintx(void* obj) { 109 assert(DumpSharedSpaces, "supported only for dumping"); 110 assert(shared_rs()->contains(obj), "must be"); 111 address base_address = address(shared_rs()->base()); 112 uintx deltax = address(obj) - base_address; 113 return deltax; 114 } 115 116 static u4 object_delta_u4(void* obj) { 117 // offset is guaranteed to be less than MAX_SHARED_DELTA in DumpRegion::expand_top_to() 118 uintx deltax = object_delta_uintx(obj); 119 guarantee(deltax <= MAX_SHARED_DELTA, "must be 32-bit offset"); 120 return (u4)deltax; 121 } 122 123 static void set_archive_loading_failed() { 124 _archive_loading_failed = true; 125 } 126 static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false); 127 static void initialize_shared_spaces() NOT_CDS_RETURN; 128 129 // Return true if given address is in the shared metaspace regions (i.e., excluding any 130 // mapped shared heap regions.) 131 static bool is_in_shared_metaspace(const void* p) { 132 // If no shared metaspace regions are mapped, MetaspceObj::_shared_metaspace_{base,top} will 133 // both be NULL and all values of p will be rejected quickly. 134 return (p < MetaspaceObj::shared_metaspace_top() && p >= MetaspaceObj::shared_metaspace_base()); 135 } 136 137 // Return true if given address is in the shared region corresponding to the idx 138 static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 139 140 static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false); 141 142 static void allocate_cpp_vtable_clones(); 143 static intptr_t* clone_cpp_vtables(intptr_t* p); 144 static void zero_cpp_vtable_clones_for_writing(); 145 static void patch_cpp_vtable_pointers(); 146 static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false); 147 static void serialize(SerializeClosure* sc) NOT_CDS_RETURN; 148 149 static MetaspaceSharedStats* stats() { 150 return &_stats; 151 } 152 153 static void report_out_of_space(const char* name, size_t needed_bytes); 154 155 // JVM/TI RedefineClasses() support: 156 // Remap the shared readonly space to shared readwrite, private if 157 // sharing is enabled. Simply returns true if sharing is not enabled 158 // or if the remapping has already been done by a prior call. 159 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 160 static bool remapped_readwrite() { 161 CDS_ONLY(return _remapped_readwrite); 162 NOT_CDS(return false); 163 } 164 165 static bool try_link_class(InstanceKlass* ik, TRAPS); 166 static void link_and_cleanup_shared_classes(TRAPS); 167 168 // Allocate a block of memory from the "mc", "ro", or "rw" regions. 169 static char* misc_code_space_alloc(size_t num_bytes); 170 static char* read_only_space_alloc(size_t num_bytes); 171 172 template <typename T> 173 static Array<T>* new_ro_array(int length) { 174 #if INCLUDE_CDS 175 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 176 Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size); 177 array->initialize(length); 178 return array; 179 #else 180 return NULL; 181 #endif 182 } 183 184 static address cds_i2i_entry_code_buffers(size_t total_size); 185 186 static address cds_i2i_entry_code_buffers() { 187 return _cds_i2i_entry_code_buffers; 188 } 189 static size_t cds_i2i_entry_code_buffers_size() { 190 return _cds_i2i_entry_code_buffers_size; 191 } 192 static void relocate_klass_ptr(oop o); 193 194 static Klass* get_relocated_klass(Klass *k); 195 196 private: 197 static void read_extra_data(const char* filename, TRAPS) NOT_CDS_RETURN; 198 }; 199 #endif // SHARE_MEMORY_METASPACESHARED_HPP