1 /* 2 * Copyright (c) 2012, 2015, 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 35 #define DEFAULT_VTBL_LIST_SIZE (17) // number of entries in the shared space vtable list. 36 #define DEFAULT_VTBL_VIRTUALS_COUNT (200) // maximum number of virtual functions 37 // If virtual functions are added to Metadata, 38 // this number needs to be increased. Also, 39 // SharedMiscCodeSize will need to be increased. 40 // The following 2 sizes were based on 41 // MetaspaceShared::generate_vtable_methods() 42 #define DEFAULT_VTBL_METHOD_SIZE (16) // conservative size of the mov1 and jmp instructions 43 // for the x64 platform 44 #define DEFAULT_VTBL_COMMON_CODE_SIZE (1*K) // conservative size of the "common_code" for the x64 platform 45 46 #define DEFAULT_SHARED_READ_WRITE_SIZE (NOT_LP64(12*M) LP64_ONLY(16*M)) 47 #define MIN_SHARED_READ_WRITE_SIZE (NOT_LP64(7*M) LP64_ONLY(12*M)) 48 49 #define DEFAULT_SHARED_READ_ONLY_SIZE (NOT_LP64(12*M) LP64_ONLY(16*M)) 50 #define MIN_SHARED_READ_ONLY_SIZE (NOT_LP64(8*M) LP64_ONLY(9*M)) 51 52 // the MIN_SHARED_MISC_DATA_SIZE and MIN_SHARED_MISC_CODE_SIZE estimates are based on 53 // MetaspaceShared::generate_vtable_methods(). 54 // The minimum size only accounts for the vtable methods. Any size less than the 55 // minimum required size would cause vm crash when allocating the vtable methods. 56 #define SHARED_MISC_SIZE_FOR(size) (DEFAULT_VTBL_VIRTUALS_COUNT*DEFAULT_VTBL_LIST_SIZE*size) 57 58 #define DEFAULT_SHARED_MISC_DATA_SIZE (NOT_LP64(2*M) LP64_ONLY(4*M)) 59 #define MIN_SHARED_MISC_DATA_SIZE (SHARED_MISC_SIZE_FOR(sizeof(void*))) 60 61 #define DEFAULT_SHARED_MISC_CODE_SIZE (120*K) 62 #define MIN_SHARED_MISC_CODE_SIZE (SHARED_MISC_SIZE_FOR(sizeof(void*))+SHARED_MISC_SIZE_FOR(DEFAULT_VTBL_METHOD_SIZE)+DEFAULT_VTBL_COMMON_CODE_SIZE) 63 64 #define DEFAULT_COMBINED_SIZE (DEFAULT_SHARED_READ_WRITE_SIZE+DEFAULT_SHARED_READ_ONLY_SIZE+DEFAULT_SHARED_MISC_DATA_SIZE+DEFAULT_SHARED_MISC_CODE_SIZE) 65 66 // the max size is the MAX size (ie. 0x7FFFFFFF) - the total size of 67 // the other 3 sections - page size (to avoid overflow in case the final 68 // size will get aligned up on page size) 69 #define SHARED_PAGE ((size_t)os::vm_page_size()) 70 #define MAX_SHARED_DELTA (0x7FFFFFFF) 71 #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) 72 #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) 73 #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) 74 #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) 75 76 #define LargeSharedArchiveSize (300*M) 77 #define HugeSharedArchiveSize (800*M) 78 #define ReadOnlyRegionPercentage 0.4 79 #define ReadWriteRegionPercentage 0.55 80 #define MiscDataRegionPercentage 0.03 81 #define MiscCodeRegionPercentage 0.02 82 #define LargeThresholdClassCount 5000 83 #define HugeThresholdClassCount 40000 84 85 #define SET_ESTIMATED_SIZE(type, region) \ 86 Shared ##region## Size = FLAG_IS_DEFAULT(Shared ##region## Size) ? \ 87 (uintx)(type ## SharedArchiveSize * region ## RegionPercentage) : Shared ## region ## Size 88 89 class FileMapInfo; 90 91 class MetaspaceSharedStats VALUE_OBJ_CLASS_SPEC { 92 public: 93 MetaspaceSharedStats() { 94 memset(this, 0, sizeof(*this)); 95 } 96 CompactHashtableStats symbol; 97 CompactHashtableStats string; 98 }; 99 100 class SharedMiscRegion VALUE_OBJ_CLASS_SPEC { 101 private: 102 VirtualSpace _vs; 103 char* _alloc_top; 104 SharedSpaceType _space_type; 105 106 public: 107 void initialize(ReservedSpace rs, size_t committed_byte_size, SharedSpaceType space_type); 108 VirtualSpace* virtual_space() { 109 return &_vs; 110 } 111 char* low() const { 112 return _vs.low(); 113 } 114 char* alloc_top() const { 115 return _alloc_top; 116 } 117 char* alloc(size_t num_bytes) NOT_CDS_RETURN_(NULL); 118 }; 119 120 // Class Data Sharing Support 121 class MetaspaceShared : AllStatic { 122 123 // CDS support 124 static ReservedSpace* _shared_rs; 125 static int _max_alignment; 126 static MetaspaceSharedStats _stats; 127 static bool _link_classes_made_progress; 128 static bool _check_classes_made_progress; 129 static bool _has_error_classes; 130 static bool _archive_loading_failed; 131 static address _cds_i2i_entry_code_buffers; 132 static size_t _cds_i2i_entry_code_buffers_size; 133 134 // Used only during dumping. 135 static SharedMiscRegion _md; 136 static SharedMiscRegion _mc; 137 public: 138 enum { 139 vtbl_list_size = DEFAULT_VTBL_LIST_SIZE, 140 num_virtuals = DEFAULT_VTBL_VIRTUALS_COUNT, 141 vtbl_method_size = DEFAULT_VTBL_METHOD_SIZE, 142 vtbl_common_code_size = DEFAULT_VTBL_COMMON_CODE_SIZE 143 }; 144 145 enum { 146 ro = 0, // read-only shared space in the heap 147 rw = 1, // read-write shared space in the heap 148 md = 2, // miscellaneous data for initializing tables, etc. 149 mc = 3, // miscellaneous code - vtable replacement. 150 max_strings = 2, // max number of string regions in string space 151 num_non_strings = 4, // number of non-string regions 152 first_string = num_non_strings, // index of first string region 153 n_regions = max_strings + num_non_strings // total number of regions 154 }; 155 156 // Accessor functions to save shared space created for metadata, which has 157 // extra space allocated at the end for miscellaneous data and code. 158 static void set_max_alignment(int alignment) { 159 CDS_ONLY(_max_alignment = alignment); 160 } 161 162 static int max_alignment() { 163 CDS_ONLY(return _max_alignment); 164 NOT_CDS(return 0); 165 } 166 167 static void prepare_for_dumping() NOT_CDS_RETURN; 168 static void preload_and_dump(TRAPS) NOT_CDS_RETURN; 169 static int preload_and_dump(const char * class_list_path, 170 GrowableArray<Klass*>* class_promote_order, 171 TRAPS) NOT_CDS_RETURN_(0); 172 173 static ReservedSpace* shared_rs() { 174 CDS_ONLY(return _shared_rs); 175 NOT_CDS(return NULL); 176 } 177 178 static void initialize_shared_rs(ReservedSpace* rs) NOT_CDS_RETURN; 179 180 static void set_archive_loading_failed() { 181 _archive_loading_failed = true; 182 } 183 static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false); 184 static void initialize_shared_spaces() NOT_CDS_RETURN; 185 static void fixup_shared_string_regions() NOT_CDS_RETURN; 186 187 // Return true if given address is in the mapped shared space. 188 static bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false); 189 190 static bool is_string_region(int idx) NOT_CDS_RETURN_(false); 191 192 static void generate_vtable_methods(void** vtbl_list, 193 void** vtable, 194 char** md_top, char* md_end, 195 char** mc_top, char* mc_end); 196 static void serialize(SerializeClosure* sc); 197 198 static MetaspaceSharedStats* stats() { 199 return &_stats; 200 } 201 202 // JVM/TI RedefineClasses() support: 203 // Remap the shared readonly space to shared readwrite, private if 204 // sharing is enabled. Simply returns true if sharing is not enabled 205 // or if the remapping has already been done by a prior call. 206 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true); 207 208 static void print_shared_spaces(); 209 210 static bool try_link_class(InstanceKlass* ik, TRAPS); 211 static void link_one_shared_class(Klass* obj, TRAPS); 212 static void check_one_shared_class(Klass* obj); 213 static void link_and_cleanup_shared_classes(TRAPS); 214 215 static int count_class(const char* classlist_file); 216 static void estimate_regions_size() NOT_CDS_RETURN; 217 218 // Allocate a block of memory from the "mc" or "md" regions. 219 static char* misc_code_space_alloc(size_t num_bytes) { return _mc.alloc(num_bytes); } 220 static char* misc_data_space_alloc(size_t num_bytes) { return _md.alloc(num_bytes); } 221 222 static address cds_i2i_entry_code_buffers(size_t total_size); 223 224 static address cds_i2i_entry_code_buffers() { 225 return _cds_i2i_entry_code_buffers; 226 } 227 static size_t cds_i2i_entry_code_buffers_size() { 228 return _cds_i2i_entry_code_buffers_size; 229 } 230 231 static SharedMiscRegion* misc_code_region() { 232 assert(DumpSharedSpaces, "used during dumping only"); 233 return &_mc; 234 } 235 static SharedMiscRegion* misc_data_region() { 236 assert(DumpSharedSpaces, "used during dumping only"); 237 return &_md; 238 } 239 }; 240 #endif // SHARE_VM_MEMORY_METASPACESHARED_HPP