1 /* 2 * Copyright (c) 2012, 2017, 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_VM_MEMORY_METASPACESHARED_HPP 26 #define SHARE_VM_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 "utilities/exceptions.hpp" 33 #include "utilities/macros.hpp" 34 #include "utilities/resourceHash.hpp" 35 36 #define MAX_SHARED_DELTA (0x7FFFFFFF) 37 38 class FileMapInfo; 39 40 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC { 41 public: 42 MetaspaceSharedStats() { 43 memset(this, 0, sizeof(*this)); 44 } 45 CompactHashtableStats symbol; 46 CompactHashtableStats string; 47 }; 48 49 // Class Data Sharing Support 50 class MetaspaceShared : AllStatic { 51 52 // CDS support 53 static ReservedSpace _shared_rs; 54 static VirtualSpace _shared_vs; 55 static int _max_alignment; 56 static MetaspaceSharedStats _stats; 57 static bool _has_error_classes; 58 static bool _archive_loading_failed; 59 static bool _remapped_readwrite; 60 static bool _open_archive_heap_region_mapped; 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 73 // optional mapped spaces 74 // Currently it only contains class file data. 75 od = num_core_spaces, 76 num_non_heap_spaces = od + 1, 77 78 // mapped java heap regions 79 first_string = od + 1, // index of first string region 80 max_strings = 2, // max number of string regions in string space 81 first_open_archive_heap_region = first_string + max_strings, 82 max_open_archive_heap_region = 2, 83 84 last_valid_region = first_open_archive_heap_region + max_open_archive_heap_region - 1, 85 n_regions = last_valid_region + 1 // total number of regions 86 }; 87 88 static void prepare_for_dumping() NOT_CDS_RETURN; 89 static void preload_and_dump(TRAPS) NOT_CDS_RETURN; 90 static int preload_classes(const char * class_list_path, 91 TRAPS) NOT_CDS_RETURN_(0); 92 93 #if INCLUDE_CDS_JAVA_HEAP 94 private: 95 static bool obj_equals(oop const& p1, oop const& p2) { 96 return p1 == p2; 97 } 98 static unsigned obj_hash(oop const& p) { 99 unsigned hash = (unsigned)((uintptr_t)&p); 100 return hash ^ (hash >> LogMinObjAlignment); 101 } 102 typedef ResourceHashtable<oop, oop, 103 MetaspaceShared::obj_hash, MetaspaceShared::obj_equals> ArchivedObjectCache; 104 static ArchivedObjectCache* _archive_object_cache; 105 106 public: 107 static ArchivedObjectCache* archive_object_cache() { 108 return _archive_object_cache; 109 } 110 static oop archive_heap_object(oop obj, Thread* THREAD); 111 static void archive_resolved_constants(Thread* THREAD); 112 #endif 113 static bool allow_archive_heap_object() { 114 CDS_JAVA_HEAP_ONLY(return (UseG1GC && UseCompressedOops && UseCompressedClassPointers);) 115 NOT_CDS_JAVA_HEAP(return false;) 116 } 117 static void create_archive_object_cache() { 118 CDS_JAVA_HEAP_ONLY(_archive_object_cache = new ArchivedObjectCache();); 119 } 120 static void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; 121 122 static void dump_open_archive_heap_objects(GrowableArray<MemRegion> * open_archive) NOT_CDS_JAVA_HEAP_RETURN; 123 static void set_open_archive_heap_region_mapped() { 124 CDS_JAVA_HEAP_ONLY(_open_archive_heap_region_mapped = true); 125 NOT_CDS_JAVA_HEAP_RETURN; 126 } 127 static bool open_archive_heap_region_mapped() { 128 CDS_JAVA_HEAP_ONLY(return _open_archive_heap_region_mapped); 129 NOT_CDS_JAVA_HEAP_RETURN_(false); 130 } 131 132 static ReservedSpace* shared_rs() { 133 CDS_ONLY(return &_shared_rs); 134 NOT_CDS(return NULL); 135 } 136 static void commit_shared_space_to(char* newtop) NOT_CDS_RETURN; 137 static size_t core_spaces_size() { 138 return _core_spaces_size; 139 } 140 static void initialize_shared_rs() NOT_CDS_RETURN; 141 142 // Delta of this object from the bottom of the archive. 143 static uintx object_delta(void* obj) { 144 assert(DumpSharedSpaces, "supported only for dumping"); 145 assert(shared_rs()->contains(obj), "must be"); 146 address base_address = address(shared_rs()->base()); 147 uintx delta = address(obj) - base_address; 148 return delta; 149 } 150 151 static void set_archive_loading_failed() { 152 _archive_loading_failed = true; 153 } 154 static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false); 155 static void initialize_shared_spaces() NOT_CDS_RETURN; 156 157 // Return true if given address is in the mapped shared space. 158 static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false); 159 160 // Return true if given address is in the shared region corresponding to the idx 161 static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 162 163 static bool is_heap_region(int idx) { 164 CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_string && 165 idx < MetaspaceShared::first_open_archive_heap_region + 166 MetaspaceShared::max_open_archive_heap_region)); 167 NOT_CDS_JAVA_HEAP_RETURN_(false); 168 } 169 static bool is_string_region(int idx) { 170 CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_string && 171 idx < MetaspaceShared::first_string + MetaspaceShared::max_strings)); 172 NOT_CDS_JAVA_HEAP_RETURN_(false); 173 } 174 static bool is_open_archive_heap_region(int idx) { 175 CDS_JAVA_HEAP_ONLY(return (idx >= MetaspaceShared::first_open_archive_heap_region && 176 idx < MetaspaceShared::first_open_archive_heap_region + 177 MetaspaceShared::max_open_archive_heap_region)); 178 NOT_CDS_JAVA_HEAP_RETURN_(false); 179 } 180 static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false); 181 182 static void allocate_cpp_vtable_clones(); 183 static intptr_t* clone_cpp_vtables(intptr_t* p); 184 static void zero_cpp_vtable_clones_for_writing(); 185 static void patch_cpp_vtable_pointers(); 186 static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false); 187 static void serialize(SerializeClosure* sc); 188 189 static MetaspaceSharedStats* stats() { 190 return &_stats; 191 } 192 193 static void report_out_of_space(const char* name, size_t needed_bytes); 194 195 // JVM/TI RedefineClasses() support: 196 // Remap the shared readonly space to shared readwrite, private if 197 // sharing is enabled. Simply returns true if sharing is not enabled 198 // or if the remapping has already been done by a prior call. 199 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 200 static bool remapped_readwrite() { 201 CDS_ONLY(return _remapped_readwrite); 202 NOT_CDS(return false); 203 } 204 205 static void print_shared_spaces(); 206 207 static bool try_link_class(InstanceKlass* ik, TRAPS); 208 static void link_and_cleanup_shared_classes(TRAPS); 209 static void check_shared_class_loader_type(Klass* obj); 210 211 // Allocate a block of memory from the "mc", "ro", or "rw" regions. 212 static char* misc_code_space_alloc(size_t num_bytes); 213 static char* read_only_space_alloc(size_t num_bytes); 214 215 template <typename T> 216 static Array<T>* new_ro_array(int length) { 217 #if INCLUDE_CDS 218 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 219 Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size); 220 array->initialize(length); 221 return array; 222 #else 223 return NULL; 224 #endif 225 } 226 227 static address cds_i2i_entry_code_buffers(size_t total_size); 228 229 static address cds_i2i_entry_code_buffers() { 230 return _cds_i2i_entry_code_buffers; 231 } 232 static size_t cds_i2i_entry_code_buffers_size() { 233 return _cds_i2i_entry_code_buffers_size; 234 } 235 static void relocate_klass_ptr(oop o); 236 }; 237 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP