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_CDS_METASPACESHARED_HPP
26 #define SHARE_CDS_METASPACESHARED_HPP
27
28 #include "memory/allocation.hpp"
29 #include "memory/memRegion.hpp"
30 #include "memory/virtualspace.hpp"
31 #include "oops/oop.hpp"
32 #include "utilities/macros.hpp"
33
34 class ArchiveBuilder;
35 class ArchiveHeapInfo;
36 class FileMapInfo;
37 class outputStream;
38 class SerializeClosure;
39 class StaticArchiveBuilder;
40
41 template<class E> class GrowableArray;
42
43 enum MapArchiveResult {
44 MAP_ARCHIVE_SUCCESS,
45 MAP_ARCHIVE_MMAP_FAILURE,
46 MAP_ARCHIVE_OTHER_FAILURE
47 };
48
49 // Class Data Sharing Support
50 class MetaspaceShared : AllStatic {
51 static ReservedSpace _symbol_rs; // used only during -Xshare:dump
52 static VirtualSpace _symbol_vs; // used only during -Xshare:dump
53 static bool _archive_loading_failed;
54 static bool _remapped_readwrite;
55 static void* _shared_metaspace_static_top;
56 static intx _relocation_delta;
57 static char* _requested_base_address;
58 static bool _use_optimized_module_handling;
59
60 public:
61 enum {
62 // core archive spaces
63 rw = 0, // read-write shared space
64 ro = 1, // read-only shared space
65 bm = 2, // relocation bitmaps (freed after file mapping is finished)
66 hp = 3, // heap region
67 num_core_region = 2, // rw and ro
68 n_regions = 4 // total number of regions
69 };
70
71 static void prepare_for_dumping() NOT_CDS_RETURN;
72 static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
73 #ifdef _LP64
74 static void adjust_heap_sizes_for_dumping() NOT_CDS_JAVA_HEAP_RETURN;
75 #endif
76
77 private:
78 static void preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) NOT_CDS_RETURN;
79 static void preload_classes(TRAPS) NOT_CDS_RETURN;
80
81 public:
82 static Symbol* symbol_rs_base() {
83 return (Symbol*)_symbol_rs.base();
84 }
85
86 static void initialize_for_static_dump() NOT_CDS_RETURN;
87 static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;
88 static void post_initialize(TRAPS) NOT_CDS_RETURN;
89
90 static void print_on(outputStream* st);
91
92 static void set_archive_loading_failed() {
93 _archive_loading_failed = true;
94 }
95
96 static void initialize_shared_spaces() NOT_CDS_RETURN;
97
98 // Return true if given address is in the shared metaspace regions (i.e., excluding the
99 // mapped heap region.)
100 static bool is_in_shared_metaspace(const void* p) {
101 return MetaspaceObj::is_shared((const MetaspaceObj*)p);
102 }
103
104 static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN;
105
106 static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false);
107 static bool is_shared_static(void* p) NOT_CDS_RETURN_(false);
108
109 static void unrecoverable_loading_error(const char* message = nullptr);
110 static void unrecoverable_writing_error(const char* message = nullptr);
111 static void writing_error(const char* message = nullptr);
112
113 static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;
114
115 // JVM/TI RedefineClasses() support:
116 // Remap the shared readonly space to shared readwrite, private if
117 // sharing is enabled. Simply returns true if sharing is not enabled
118 // or if the remapping has already been done by a prior call.
119 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
120 static bool remapped_readwrite() {
121 CDS_ONLY(return _remapped_readwrite);
122 NOT_CDS(return false);
123 }
124
125 static bool try_link_class(JavaThread* current, InstanceKlass* ik);
126 static void link_shared_classes(bool jcmd_request, TRAPS) NOT_CDS_RETURN;
127 static bool link_class_for_cds(InstanceKlass* ik, TRAPS) NOT_CDS_RETURN_(false);
128 static bool may_be_eagerly_linked(InstanceKlass* ik) NOT_CDS_RETURN_(false);
129
130 #if INCLUDE_CDS
131 // Alignment for the 2 core CDS regions (RW/RO) only.
132 // (Heap region alignments are decided by GC).
133 static size_t core_region_alignment();
134 static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik);
135 // print loaded classes names to file.
136 static void dump_loaded_classes(const char* file_name, TRAPS);
137 #endif
138
139 // Allocate a block of memory from the temporary "symbol" region.
140 static char* symbol_space_alloc(size_t num_bytes);
141
142 // This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump.
143 // Both the base/top archives are written using this as their base address.
144 //
145 // During static dump: _requested_base_address == SharedBaseAddress.
146 //
147 // During dynamic dump: _requested_base_address is not always the same as SharedBaseAddress:
153 // 0x800000000 (unless it was set by -XX:SharedBaseAddress during -Xshare:dump).
154 static char* requested_base_address() {
155 return _requested_base_address;
156 }
157
158 // Non-zero if the archive(s) need to be mapped a non-default location due to ASLR.
159 static intx relocation_delta() { return _relocation_delta; }
160
161 static bool use_windows_memory_mapping() {
162 const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true));
163 //const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc.
164 return is_windows;
165 }
166
167 // Can we skip some expensive operations related to modules?
168 static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); }
169 static void disable_optimized_module_handling() { _use_optimized_module_handling = false; }
170
171 private:
172 static void read_extra_data(JavaThread* current, const char* filename) NOT_CDS_RETURN;
173 static bool write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info);
174 static FileMapInfo* open_static_archive();
175 static FileMapInfo* open_dynamic_archive();
176 // use_requested_addr: If true (default), attempt to map at the address the
177 static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,
178 bool use_requested_addr);
179 static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
180 FileMapInfo* dynamic_mapinfo,
181 bool use_archive_base_addr,
182 ReservedSpace& total_space_rs,
183 ReservedSpace& archive_space_rs,
184 ReservedSpace& class_space_rs);
185 static void release_reserved_spaces(ReservedSpace& total_space_rs,
186 ReservedSpace& archive_space_rs,
187 ReservedSpace& class_space_rs);
188 static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs);
189 static void unmap_archive(FileMapInfo* mapinfo);
190 static void get_default_classlist(char* default_classlist, const size_t buf_size);
191 };
192 #endif // SHARE_CDS_METASPACESHARED_HPP
|
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_CDS_METASPACESHARED_HPP
26 #define SHARE_CDS_METASPACESHARED_HPP
27
28 #include "memory/allocation.hpp"
29 #include "memory/memRegion.hpp"
30 #include "memory/virtualspace.hpp"
31 #include "oops/oop.hpp"
32 #include "utilities/macros.hpp"
33
34 class ArchiveBuilder;
35 class ArchiveHeapInfo;
36 class FileMapInfo;
37 class Method;
38 class outputStream;
39 class SerializeClosure;
40 class StaticArchiveBuilder;
41
42 template<class E> class Array;
43 template<class E> class GrowableArray;
44
45 enum MapArchiveResult {
46 MAP_ARCHIVE_SUCCESS,
47 MAP_ARCHIVE_MMAP_FAILURE,
48 MAP_ARCHIVE_OTHER_FAILURE
49 };
50
51 class StaticArchiveBuilder;
52
53 // Class Data Sharing Support
54 class MetaspaceShared : AllStatic {
55 static ReservedSpace _symbol_rs; // used only during -Xshare:dump
56 static VirtualSpace _symbol_vs; // used only during -Xshare:dump
57 static bool _archive_loading_failed;
58 static bool _remapped_readwrite;
59 static void* _shared_metaspace_static_top;
60 static intx _relocation_delta;
61 static char* _requested_base_address;
62 static Array<Method*>* _archived_method_handle_intrinsics;
63 static bool _use_optimized_module_handling;
64
65 public:
66 enum {
67 // core archive spaces
68 rw = 0, // read-write shared space
69 ro = 1, // read-only shared space
70 bm = 2, // relocation bitmaps (freed after file mapping is finished)
71 hp = 3, // heap region
72 cc = 4, // cached code
73 num_core_region = 2, // rw and ro
74 n_regions = 5 // total number of regions
75 };
76
77 static void prepare_for_dumping() NOT_CDS_RETURN;
78 static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
79 #ifdef _LP64
80 static void adjust_heap_sizes_for_dumping() NOT_CDS_JAVA_HEAP_RETURN;
81 #endif
82
83 private:
84 static void preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) NOT_CDS_RETURN;
85 static void preload_classes(TRAPS) NOT_CDS_RETURN;
86
87 public:
88 static Symbol* symbol_rs_base() {
89 return (Symbol*)_symbol_rs.base();
90 }
91
92 static void initialize_for_static_dump() NOT_CDS_RETURN;
93 static void open_static_archive() NOT_CDS_RETURN;
94 static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;
95 static void post_initialize(TRAPS) NOT_CDS_RETURN;
96
97 static void print_on(outputStream* st);
98
99 static void set_archive_loading_failed() {
100 _archive_loading_failed = true;
101 }
102
103 static void initialize_shared_spaces() NOT_CDS_RETURN;
104
105 // Return true if given address is in the shared metaspace regions (i.e., excluding the
106 // mapped heap region.)
107 static bool is_in_shared_metaspace(const void* p) {
108 return MetaspaceObj::is_shared((const MetaspaceObj*)p);
109 }
110
111 static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN;
112
113 static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false);
114 static bool is_shared_static(void* p) NOT_CDS_RETURN_(false);
115
116 static void unrecoverable_loading_error(const char* message = nullptr);
117 static void unrecoverable_writing_error(const char* message = nullptr);
118 static void writing_error(const char* message = nullptr);
119
120 static void make_method_handle_intrinsics_shareable() NOT_CDS_RETURN;
121 static void write_method_handle_intrinsics() NOT_CDS_RETURN;
122 static Array<Method*>* archived_method_handle_intrinsics() { return _archived_method_handle_intrinsics; }
123 static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;
124
125 // JVM/TI RedefineClasses() support:
126 // Remap the shared readonly space to shared readwrite, private if
127 // sharing is enabled. Simply returns true if sharing is not enabled
128 // or if the remapping has already been done by a prior call.
129 static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
130 static bool remapped_readwrite() {
131 CDS_ONLY(return _remapped_readwrite);
132 NOT_CDS(return false);
133 }
134
135 static bool try_link_class(JavaThread* current, InstanceKlass* ik);
136 static void link_shared_classes(bool jcmd_request, TRAPS) NOT_CDS_RETURN;
137 static bool may_be_eagerly_linked(InstanceKlass* ik) NOT_CDS_RETURN_(false);
138
139 #if INCLUDE_CDS
140 // Alignment for the 2 core CDS regions (RW/RO) only.
141 // (Heap region alignments are decided by GC).
142 static size_t core_region_alignment();
143 static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik);
144 // print loaded classes names to file.
145 static void dump_loaded_classes(const char* file_name, TRAPS);
146 #endif
147
148 // Allocate a block of memory from the temporary "symbol" region.
149 static char* symbol_space_alloc(size_t num_bytes);
150
151 // This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump.
152 // Both the base/top archives are written using this as their base address.
153 //
154 // During static dump: _requested_base_address == SharedBaseAddress.
155 //
156 // During dynamic dump: _requested_base_address is not always the same as SharedBaseAddress:
162 // 0x800000000 (unless it was set by -XX:SharedBaseAddress during -Xshare:dump).
163 static char* requested_base_address() {
164 return _requested_base_address;
165 }
166
167 // Non-zero if the archive(s) need to be mapped a non-default location due to ASLR.
168 static intx relocation_delta() { return _relocation_delta; }
169
170 static bool use_windows_memory_mapping() {
171 const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true));
172 //const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc.
173 return is_windows;
174 }
175
176 // Can we skip some expensive operations related to modules?
177 static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); }
178 static void disable_optimized_module_handling() { _use_optimized_module_handling = false; }
179
180 private:
181 static void read_extra_data(JavaThread* current, const char* filename) NOT_CDS_RETURN;
182 static void fork_and_dump_final_static_archive();
183 static bool write_static_archive(ArchiveBuilder* builder, FileMapInfo* map_info, ArchiveHeapInfo* heap_info);
184 static FileMapInfo* open_dynamic_archive();
185 // use_requested_addr: If true (default), attempt to map at the address the
186 static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,
187 bool use_requested_addr);
188 static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
189 FileMapInfo* dynamic_mapinfo,
190 bool use_archive_base_addr,
191 ReservedSpace& total_space_rs,
192 ReservedSpace& archive_space_rs,
193 ReservedSpace& class_space_rs);
194 static void release_reserved_spaces(ReservedSpace& total_space_rs,
195 ReservedSpace& archive_space_rs,
196 ReservedSpace& class_space_rs);
197 static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs);
198 static void unmap_archive(FileMapInfo* mapinfo);
199 static void get_default_classlist(char* default_classlist, const size_t buf_size);
200 };
201 #endif // SHARE_CDS_METASPACESHARED_HPP
|