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 35 #define DEFAULT_SHARED_READ_WRITE_SIZE (NOT_LP64(6*M) LP64_ONLY(10*M)) 36 #define MIN_SHARED_READ_WRITE_SIZE (NOT_LP64(6*M) LP64_ONLY(10*M)) 37 38 #define DEFAULT_SHARED_READ_ONLY_SIZE (NOT_LP64(8*M) LP64_ONLY(13*M)) 39 #define MIN_SHARED_READ_ONLY_SIZE (NOT_LP64(8*M) LP64_ONLY(13*M)) 40 41 // the MIN_SHARED_MISC_DATA_SIZE and MIN_SHARED_MISC_CODE_SIZE estimates are based on 42 // the sizes required for dumping the archive using the default classlist. The sizes 43 // are multiplied by 1.5 for a safety margin. 44 45 #define DEFAULT_SHARED_MISC_DATA_SIZE (NOT_LP64(2*M) LP64_ONLY(4*M)) 46 #define MIN_SHARED_MISC_DATA_SIZE (NOT_LP64(1*M) LP64_ONLY(1200*K)) 47 48 #define DEFAULT_SHARED_MISC_CODE_SIZE (120*K) 49 #define MIN_SHARED_MISC_CODE_SIZE (NOT_LP64(63*K) LP64_ONLY(69*K)) 50 #define DEFAULT_COMBINED_SIZE (DEFAULT_SHARED_READ_WRITE_SIZE+DEFAULT_SHARED_READ_ONLY_SIZE+DEFAULT_SHARED_MISC_DATA_SIZE+DEFAULT_SHARED_MISC_CODE_SIZE) 51 52 // the max size is the MAX size (ie. 0x7FFFFFFF) - the total size of 53 // the other 3 sections - page size (to avoid overflow in case the final 54 // size will get aligned up on page size) 55 #define SHARED_PAGE ((size_t)os::vm_page_size()) 56 #define MAX_SHARED_DELTA (0x7FFFFFFF) 57 #define MAX_SHARED_READ_WRITE_SIZE (MAX_SHARED_DELTA-(MIN_SHARED_READ_ONLY_SIZE+MIN_SHARED_MISC_DATA_SIZE+MIN_SHARED_MISC_CODE_SIZE)-SHARED_PAGE) 58 #define MAX_SHARED_READ_ONLY_SIZE (MAX_SHARED_DELTA-(MIN_SHARED_READ_WRITE_SIZE+MIN_SHARED_MISC_DATA_SIZE+MIN_SHARED_MISC_CODE_SIZE)-SHARED_PAGE) 59 #define MAX_SHARED_MISC_DATA_SIZE (MAX_SHARED_DELTA-(MIN_SHARED_READ_WRITE_SIZE+MIN_SHARED_READ_ONLY_SIZE+MIN_SHARED_MISC_CODE_SIZE)-SHARED_PAGE) 60 #define MAX_SHARED_MISC_CODE_SIZE (MAX_SHARED_DELTA-(MIN_SHARED_READ_WRITE_SIZE+MIN_SHARED_READ_ONLY_SIZE+MIN_SHARED_MISC_DATA_SIZE)-SHARED_PAGE) 61 62 #define LargeSharedArchiveSize (300*M) 63 #define HugeSharedArchiveSize (800*M) 64 #define ReadOnlyRegionPercentage 0.52 65 #define ReadWriteRegionPercentage 0.43 66 #define MiscDataRegionPercentage 0.03 67 #define MiscCodeRegionPercentage 0.02 68 #define LargeThresholdClassCount 5000 69 #define HugeThresholdClassCount 40000 70 71 #define SET_ESTIMATED_SIZE(type, region) \ 72 Shared ##region## Size = FLAG_IS_DEFAULT(Shared ##region## Size) ? \ 73 (uintx)(type ## SharedArchiveSize * region ## RegionPercentage) : Shared ## region ## Size 74 75 class FileMapInfo; 76 77 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC { 78 public: 79 MetaspaceSharedStats() { 80 memset(this, 0, sizeof(*this)); 81 } 82 CompactHashtableStats symbol; 83 CompactHashtableStats string; 84 }; 85 86 class SharedMiscRegion VALUE_OBJ_CLASS_SPEC { 87 private: 88 VirtualSpace _vs; 89 char* _alloc_top; 90 SharedSpaceType _space_type; 91 92 public: 93 void initialize(ReservedSpace rs, size_t committed_byte_size, SharedSpaceType space_type); 94 VirtualSpace* virtual_space() { 95 return &_vs; 96 } 97 char* low() const { 98 return _vs.low(); 99 } 100 char* alloc_top() const { 101 return _alloc_top; 102 } 103 char* alloc(size_t num_bytes) NOT_CDS_RETURN_(NULL); 104 }; 105 106 // Class Data Sharing Support 107 class MetaspaceShared : AllStatic { 108 109 // CDS support 110 static ReservedSpace* _shared_rs; 111 static int _max_alignment; 112 static MetaspaceSharedStats _stats; 113 static bool _has_error_classes; 114 static bool _archive_loading_failed; 115 static bool _remapped_readwrite; 116 static address _cds_i2i_entry_code_buffers; 117 static size_t _cds_i2i_entry_code_buffers_size; 118 119 // Used only during dumping. 120 static SharedMiscRegion _md; 121 static SharedMiscRegion _mc; 122 static SharedMiscRegion _od; 123 public: 124 enum { 125 ro = 0, // read-only shared space in the heap 126 rw = 1, // read-write shared space in the heap 127 md = 2, // miscellaneous data for initializing tables, etc. 128 mc = 3, // miscellaneous code - vtable replacement. 129 max_strings = 2, // max number of string regions in string space 130 num_non_strings = 4, // number of non-string regions 131 first_string = num_non_strings, // index of first string region 132 // The optional data region is the last region. 133 // Currently it only contains class file data. 134 od = max_strings + num_non_strings, 135 n_regions = od + 1 // total number of regions 136 }; 137 138 // Accessor functions to save shared space created for metadata, which has 139 // extra space allocated at the end for miscellaneous data and code. 140 static void set_max_alignment(int alignment) { 141 CDS_ONLY(_max_alignment = alignment); 142 } 143 144 static int max_alignment() { 145 CDS_ONLY(return _max_alignment); 146 NOT_CDS(return 0); 147 } 148 149 static void prepare_for_dumping() NOT_CDS_RETURN; 150 static void preload_and_dump(TRAPS) NOT_CDS_RETURN; 151 static int preload_and_dump(const char * class_list_path, 152 GrowableArray<Klass*>* class_promote_order, 153 TRAPS) NOT_CDS_RETURN_(0); 154 155 static ReservedSpace* shared_rs() { 156 CDS_ONLY(return _shared_rs); 157 NOT_CDS(return NULL); 158 } 159 160 static void initialize_shared_rs(ReservedSpace* rs) NOT_CDS_RETURN; 161 162 static void set_archive_loading_failed() { 163 _archive_loading_failed = true; 164 } 165 static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false); 166 static void initialize_shared_spaces() NOT_CDS_RETURN; 167 static void fixup_shared_string_regions() NOT_CDS_RETURN; 168 169 // Return true if given address is in the mapped shared space. 170 static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false); 171 172 // Return true if given address is in the shared region corresponding to the idx 173 static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 174 175 static bool is_string_region(int idx) NOT_CDS_RETURN_(false); 176 177 static intptr_t* allocate_cpp_vtable_clones(intptr_t* top, intptr_t* end); 178 static intptr_t* clone_cpp_vtables(intptr_t* p); 179 static void zero_cpp_vtable_clones_for_writing(); 180 static void patch_cpp_vtable_pointers(); 181 static bool is_valid_shared_method(const Method* m); 182 183 static void serialize(SerializeClosure* sc, GrowableArray<MemRegion> *string_space, 184 size_t* space_size); 185 186 static MetaspaceSharedStats* stats() { 187 return &_stats; 188 } 189 190 // JVM/TI RedefineClasses() support: 191 // Remap the shared readonly space to shared readwrite, private if 192 // sharing is enabled. Simply returns true if sharing is not enabled 193 // or if the remapping has already been done by a prior call. 194 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 195 static bool remapped_readwrite() { 196 CDS_ONLY(return _remapped_readwrite); 197 NOT_CDS(return false); 198 } 199 200 static void print_shared_spaces(); 201 202 static bool try_link_class(InstanceKlass* ik, TRAPS); 203 static void link_and_cleanup_shared_classes(TRAPS); 204 static void check_shared_class_loader_type(Klass* obj); 205 206 static int count_class(const char* classlist_file); 207 static void estimate_regions_size() NOT_CDS_RETURN; 208 209 // Allocate a block of memory from the "mc", "md", or "od" regions. 210 static char* misc_code_space_alloc(size_t num_bytes) { return _mc.alloc(num_bytes); } 211 static char* misc_data_space_alloc(size_t num_bytes) { return _md.alloc(num_bytes); } 212 static char* optional_data_space_alloc(size_t num_bytes) { return _od.alloc(num_bytes); } 213 214 static address cds_i2i_entry_code_buffers(size_t total_size); 215 216 static address cds_i2i_entry_code_buffers() { 217 return _cds_i2i_entry_code_buffers; 218 } 219 static size_t cds_i2i_entry_code_buffers_size() { 220 return _cds_i2i_entry_code_buffers_size; 221 } 222 223 static SharedMiscRegion* misc_code_region() { 224 assert(DumpSharedSpaces, "used during dumping only"); 225 return &_mc; 226 } 227 static SharedMiscRegion* misc_data_region() { 228 assert(DumpSharedSpaces, "used during dumping only"); 229 return &_md; 230 } 231 static SharedMiscRegion* optional_data_region() { 232 assert(DumpSharedSpaces, "used during dumping only"); 233 return &_od; 234 } 235 }; 236 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP | 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 35 #define MAX_SHARED_DELTA (0x7FFFFFFF) 36 37 class FileMapInfo; 38 39 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC { 40 public: 41 MetaspaceSharedStats() { 42 memset(this, 0, sizeof(*this)); 43 } 44 CompactHashtableStats symbol; 45 CompactHashtableStats string; 46 }; 47 48 // Class Data Sharing Support 49 class MetaspaceShared : AllStatic { 50 51 // CDS support 52 static ReservedSpace _shared_rs; 53 static VirtualSpace _shared_vs; 54 static int _max_alignment; 55 static MetaspaceSharedStats _stats; 56 static bool _has_error_classes; 57 static bool _archive_loading_failed; 58 static bool _remapped_readwrite; 59 static address _cds_i2i_entry_code_buffers; 60 static size_t _cds_i2i_entry_code_buffers_size; 61 static size_t _core_spaces_size; 62 public: 63 enum { 64 mc = 0, // miscellaneous code for method trampolines 65 rw = 1, // read-write shared space in the heap 66 ro = 2, // read-only shared space in the heap 67 md = 3, // miscellaneous data for initializing tables, etc. 68 max_strings = 2, // max number of string regions in string space 69 num_non_strings = 4, // number of non-string regions 70 first_string = num_non_strings, // index of first string region 71 // The optional data region is the last region. 72 // Currently it only contains class file data. 73 od = max_strings + num_non_strings, 74 last_valid_region = od, 75 n_regions = od + 1 // total number of regions 76 }; 77 78 static void prepare_for_dumping() NOT_CDS_RETURN; 79 static void preload_and_dump(TRAPS) NOT_CDS_RETURN; 80 static int preload_classes(const char * class_list_path, 81 TRAPS) NOT_CDS_RETURN_(0); 82 83 static ReservedSpace* shared_rs() { 84 CDS_ONLY(return &_shared_rs); 85 NOT_CDS(return NULL); 86 } 87 static void commit_shared_space_to(char* newtop) NOT_CDS_RETURN; 88 static size_t core_spaces_size() { 89 return _core_spaces_size; 90 } 91 static void initialize_shared_rs() NOT_CDS_RETURN; 92 93 // Delta of this object from the bottom of the archive. 94 static uintx object_delta(void* obj) { 95 assert(DumpSharedSpaces, "supported only for dumping"); 96 assert(shared_rs()->contains(obj), "must be"); 97 address base_address = address(shared_rs()->base()); 98 uintx delta = address(obj) - base_address; 99 return delta; 100 } 101 102 static void set_archive_loading_failed() { 103 _archive_loading_failed = true; 104 } 105 static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false); 106 static void initialize_shared_spaces() NOT_CDS_RETURN; 107 static void fixup_shared_string_regions() NOT_CDS_RETURN; 108 109 // Return true if given address is in the mapped shared space. 110 static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false); 111 112 // Return true if given address is in the shared region corresponding to the idx 113 static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 114 115 static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false); 116 static bool is_string_region(int idx) NOT_CDS_RETURN_(false); 117 118 static void allocate_cpp_vtable_clones(); 119 static intptr_t* clone_cpp_vtables(intptr_t* p); 120 static void zero_cpp_vtable_clones_for_writing(); 121 static void patch_cpp_vtable_pointers(); 122 static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false); 123 static void serialize(SerializeClosure* sc); 124 125 126 static MetaspaceSharedStats* stats() { 127 return &_stats; 128 } 129 130 static void report_out_of_space(const char* name, size_t needed_bytes); 131 132 // JVM/TI RedefineClasses() support: 133 // Remap the shared readonly space to shared readwrite, private if 134 // sharing is enabled. Simply returns true if sharing is not enabled 135 // or if the remapping has already been done by a prior call. 136 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 137 static bool remapped_readwrite() { 138 CDS_ONLY(return _remapped_readwrite); 139 NOT_CDS(return false); 140 } 141 142 static void print_shared_spaces(); 143 144 static bool try_link_class(InstanceKlass* ik, TRAPS); 145 static void link_and_cleanup_shared_classes(TRAPS); 146 static void check_shared_class_loader_type(Klass* obj); 147 148 // Allocate a block of memory from the "mc", "ro", or "rw" regions. 149 static char* misc_code_space_alloc(size_t num_bytes); 150 static char* read_only_space_alloc(size_t num_bytes); 151 152 template <typename T> 153 static Array<T>* new_ro_array(int length) { 154 #if INCLUDE_CDS 155 size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T)); 156 Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size); 157 array->initialize(length); 158 return array; 159 #else 160 return NULL; 161 #endif 162 } 163 164 static address cds_i2i_entry_code_buffers(size_t total_size); 165 166 static address cds_i2i_entry_code_buffers() { 167 return _cds_i2i_entry_code_buffers; 168 } 169 static size_t cds_i2i_entry_code_buffers_size() { 170 return _cds_i2i_entry_code_buffers_size; 171 } 172 static void relocate_klass_ptr(oop o); 173 }; 174 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP |