34 #include "utilities/exceptions.hpp"
35 #include "utilities/macros.hpp"
36 #include "runtime/nonJavaThread.hpp"
37 #include "runtime/semaphore.hpp"
38
39 class BootstrapInfo;
40 class ReservedSpace;
41 class VirtualSpace;
42
43 template<class E> class Array;
44 template<class E> class GrowableArray;
45
46 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an
47 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling
48 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is
49 // fixed, but _ptr_end can be expanded as more objects are dumped.
50 class ArchivePtrMarker : AllStatic {
51 static CHeapBitMap* _ptrmap;
52 static CHeapBitMap* _rw_ptrmap;
53 static CHeapBitMap* _ro_ptrmap;
54 static VirtualSpace* _vs;
55
56 // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to
57 // avoid unintentional copy operations after the bitmap has been finalized and written.
58 static bool _compacted;
59
60 static address* ptr_base() { return (address*)_vs->low(); } // committed lower bound (inclusive)
61 static address* ptr_end() { return (address*)_vs->high(); } // committed upper bound (exclusive)
62
63 public:
64 static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);
65 static void initialize_rw_ro_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap);
66 static void mark_pointer(address* ptr_loc);
67 static void clear_pointer(address* ptr_loc);
68 static void compact(address relocatable_base, address relocatable_end);
69 static void compact(size_t max_non_null_offset);
70
71 template <typename T>
72 static void mark_pointer(T* ptr_loc) {
73 mark_pointer((address*)ptr_loc);
74 }
75
76 template <typename T>
77 static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {
78 *ptr_loc = ptr_value;
79 mark_pointer(ptr_loc);
80 }
81
82 static CHeapBitMap* ptrmap() {
83 return _ptrmap;
84 }
85
86 static CHeapBitMap* rw_ptrmap() {
87 return _rw_ptrmap;
88 }
89
90 static CHeapBitMap* ro_ptrmap() {
91 return _ro_ptrmap;
92 }
93
94 static void reset_map_and_vs() {
95 _ptrmap = nullptr;
96 _rw_ptrmap = nullptr;
97 _ro_ptrmap = nullptr;
98 _vs = nullptr;
99 }
100 };
101
102 // SharedDataRelocator is used to shift pointers in the CDS archive.
103 //
104 // The CDS archive is basically a contiguous block of memory (divided into several regions)
105 // that contains multiple objects. The objects may contain direct pointers that point to other objects
106 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we
107 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).
108 //
109 // The contents of the archive assumes that it’s mapped at the default SharedBaseAddress (e.g. 0x800000000).
110 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator
111 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to
112 // the actually mapped location of the target object.
113 class SharedDataRelocator: public BitMapClosure {
114 // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }
115
116 // Patch all pointers within this region that are marked.
117 address* _patch_base;
164 DumpRegion(const char* name, uintx max_delta = 0)
165 : _name(name), _base(nullptr), _top(nullptr), _end(nullptr),
166 _max_delta(max_delta), _is_packed(false),
167 _rs(nullptr), _vs(nullptr) {}
168
169 char* expand_top_to(char* newtop);
170 char* allocate(size_t num_bytes, size_t alignment = 0);
171
172 void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN;
173
174 char* base() const { return _base; }
175 char* top() const { return _top; }
176 char* end() const { return _end; }
177 size_t reserved() const { return _end - _base; }
178 size_t used() const { return _top - _base; }
179 bool is_packed() const { return _is_packed; }
180 bool is_allocatable() const {
181 return !is_packed() && _base != nullptr;
182 }
183
184 void print(size_t total_bytes) const;
185 void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
186
187 void init(ReservedSpace* rs, VirtualSpace* vs);
188
189 void pack(DumpRegion* next = nullptr);
190
191 bool contains(char* p) {
192 return base() <= p && p < top();
193 }
194 };
195
196 // Closure for serializing initialization data out to a data area to be
197 // written to the shared file.
198
199 class WriteClosure : public SerializeClosure {
200 private:
201 DumpRegion* _dump_region;
202
203 public:
256
257 class ArchiveUtils {
258 template <typename T> static Array<T>* archive_non_ptr_array(GrowableArray<T>* tmp_array);
259 template <typename T> static Array<T>* archive_ptr_array(GrowableArray<T>* tmp_array);
260
261 public:
262 static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF;
263 static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;
264 static bool has_aot_initialized_mirror(InstanceKlass* src_ik);
265
266 template <typename T, ENABLE_IF(!std::is_pointer<T>::value)>
267 static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
268 return archive_non_ptr_array(tmp_array);
269 }
270
271 template <typename T, ENABLE_IF(std::is_pointer<T>::value)>
272 static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
273 return archive_ptr_array(tmp_array);
274 }
275
276 // The following functions translate between a u4 offset and an address in the
277 // the range of the mapped CDS archive (e.g., Metaspace::is_in_shared_metaspace()).
278 // Since the first 16 bytes in this range are dummy data (see ArchiveBuilder::reserve_buffer()),
279 // we know that offset 0 never represents a valid object. As a result, an offset of 0
280 // is used to encode a nullptr.
281 //
282 // Use the "archived_address_or_null" variants if a nullptr may be encoded.
283
284 // offset must represent an object of type T in the mapped shared space. Return
285 // a direct pointer to this object.
286 template <typename T> T static offset_to_archived_address(u4 offset) {
287 assert(offset != 0, "sanity");
288 T p = (T)(SharedBaseAddress + offset);
289 assert(Metaspace::is_in_shared_metaspace(p), "must be");
290 return p;
291 }
292
293 template <typename T> T static offset_to_archived_address_or_null(u4 offset) {
294 if (offset == 0) {
295 return nullptr;
|
34 #include "utilities/exceptions.hpp"
35 #include "utilities/macros.hpp"
36 #include "runtime/nonJavaThread.hpp"
37 #include "runtime/semaphore.hpp"
38
39 class BootstrapInfo;
40 class ReservedSpace;
41 class VirtualSpace;
42
43 template<class E> class Array;
44 template<class E> class GrowableArray;
45
46 // ArchivePtrMarker is used to mark the location of pointers embedded in a CDS archive. E.g., when an
47 // InstanceKlass k is dumped, we mark the location of the k->_name pointer by effectively calling
48 // mark_pointer(/*ptr_loc=*/&k->_name). It's required that (_prt_base <= ptr_loc < _ptr_end). _ptr_base is
49 // fixed, but _ptr_end can be expanded as more objects are dumped.
50 class ArchivePtrMarker : AllStatic {
51 static CHeapBitMap* _ptrmap;
52 static CHeapBitMap* _rw_ptrmap;
53 static CHeapBitMap* _ro_ptrmap;
54 static CHeapBitMap* _cc_ptrmap;
55 static VirtualSpace* _vs;
56
57 // Once _ptrmap is compacted, we don't allow bit marking anymore. This is to
58 // avoid unintentional copy operations after the bitmap has been finalized and written.
59 static bool _compacted;
60
61 static address* ptr_base() { return (address*)_vs->low(); } // committed lower bound (inclusive)
62 static address* ptr_end() { return (address*)_vs->high(); } // committed upper bound (exclusive)
63
64 public:
65 static void initialize(CHeapBitMap* ptrmap, VirtualSpace* vs);
66 static void initialize_rw_ro_cc_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* cc_ptrmap);
67 static void mark_pointer(address* ptr_loc);
68 static void clear_pointer(address* ptr_loc);
69 static void compact(address relocatable_base, address relocatable_end);
70 static void compact(size_t max_non_null_offset);
71
72 template <typename T>
73 static void mark_pointer(T* ptr_loc) {
74 mark_pointer((address*)ptr_loc);
75 }
76
77 template <typename T>
78 static void set_and_mark_pointer(T* ptr_loc, T ptr_value) {
79 *ptr_loc = ptr_value;
80 mark_pointer(ptr_loc);
81 }
82
83 static CHeapBitMap* ptrmap() {
84 return _ptrmap;
85 }
86
87 static CHeapBitMap* rw_ptrmap() {
88 return _rw_ptrmap;
89 }
90
91 static CHeapBitMap* ro_ptrmap() {
92 return _ro_ptrmap;
93 }
94
95 static CHeapBitMap* cc_ptrmap() {
96 return _cc_ptrmap;
97 }
98
99 static void reset_map_and_vs() {
100 _ptrmap = nullptr;
101 _rw_ptrmap = nullptr;
102 _ro_ptrmap = nullptr;
103 _cc_ptrmap = nullptr;
104 _vs = nullptr;
105 }
106 };
107
108 // SharedDataRelocator is used to shift pointers in the CDS archive.
109 //
110 // The CDS archive is basically a contiguous block of memory (divided into several regions)
111 // that contains multiple objects. The objects may contain direct pointers that point to other objects
112 // within the archive (e.g., InstanceKlass::_name points to a Symbol in the archive). During dumping, we
113 // built a bitmap that marks the locations of all these pointers (using ArchivePtrMarker, see comments above).
114 //
115 // The contents of the archive assumes that it’s mapped at the default SharedBaseAddress (e.g. 0x800000000).
116 // If the archive ends up being mapped at a different address (e.g. 0x810000000), SharedDataRelocator
117 // is used to shift each marked pointer by a delta (0x10000000 in this example), so that it points to
118 // the actually mapped location of the target object.
119 class SharedDataRelocator: public BitMapClosure {
120 // for all (address** p), where (is_marked(p) && _patch_base <= p && p < _patch_end) { *p += delta; }
121
122 // Patch all pointers within this region that are marked.
123 address* _patch_base;
170 DumpRegion(const char* name, uintx max_delta = 0)
171 : _name(name), _base(nullptr), _top(nullptr), _end(nullptr),
172 _max_delta(max_delta), _is_packed(false),
173 _rs(nullptr), _vs(nullptr) {}
174
175 char* expand_top_to(char* newtop);
176 char* allocate(size_t num_bytes, size_t alignment = 0);
177
178 void append_intptr_t(intptr_t n, bool need_to_mark = false) NOT_CDS_RETURN;
179
180 char* base() const { return _base; }
181 char* top() const { return _top; }
182 char* end() const { return _end; }
183 size_t reserved() const { return _end - _base; }
184 size_t used() const { return _top - _base; }
185 bool is_packed() const { return _is_packed; }
186 bool is_allocatable() const {
187 return !is_packed() && _base != nullptr;
188 }
189
190 bool is_empty() const { return _base == _top; }
191
192 void print(size_t total_bytes) const;
193 void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
194
195 void init(ReservedSpace* rs, VirtualSpace* vs);
196
197 void pack(DumpRegion* next = nullptr);
198
199 bool contains(char* p) {
200 return base() <= p && p < top();
201 }
202 };
203
204 // Closure for serializing initialization data out to a data area to be
205 // written to the shared file.
206
207 class WriteClosure : public SerializeClosure {
208 private:
209 DumpRegion* _dump_region;
210
211 public:
264
265 class ArchiveUtils {
266 template <typename T> static Array<T>* archive_non_ptr_array(GrowableArray<T>* tmp_array);
267 template <typename T> static Array<T>* archive_ptr_array(GrowableArray<T>* tmp_array);
268
269 public:
270 static const uintx MAX_SHARED_DELTA = 0x7FFFFFFF;
271 static void log_to_classlist(BootstrapInfo* bootstrap_specifier, TRAPS) NOT_CDS_RETURN;
272 static bool has_aot_initialized_mirror(InstanceKlass* src_ik);
273
274 template <typename T, ENABLE_IF(!std::is_pointer<T>::value)>
275 static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
276 return archive_non_ptr_array(tmp_array);
277 }
278
279 template <typename T, ENABLE_IF(std::is_pointer<T>::value)>
280 static Array<T>* archive_array(GrowableArray<T>* tmp_array) {
281 return archive_ptr_array(tmp_array);
282 }
283
284 static const char* builtin_loader_name_or_null(oop loader); // "boot", "platform", "app", or nullptr
285 static const char* builtin_loader_name(oop loader); // "boot", "platform", or "app". Asserts if not a built-in-loader.
286
287 static bool builtin_loader_from_type(const char* loader_type, oop* value_ret);
288 static oop builtin_loader_from_type(int loader_type);
289
290 // The following functions translate between a u4 offset and an address in the
291 // the range of the mapped CDS archive (e.g., Metaspace::is_in_shared_metaspace()).
292 // Since the first 16 bytes in this range are dummy data (see ArchiveBuilder::reserve_buffer()),
293 // we know that offset 0 never represents a valid object. As a result, an offset of 0
294 // is used to encode a nullptr.
295 //
296 // Use the "archived_address_or_null" variants if a nullptr may be encoded.
297
298 // offset must represent an object of type T in the mapped shared space. Return
299 // a direct pointer to this object.
300 template <typename T> T static offset_to_archived_address(u4 offset) {
301 assert(offset != 0, "sanity");
302 T p = (T)(SharedBaseAddress + offset);
303 assert(Metaspace::is_in_shared_metaspace(p), "must be");
304 return p;
305 }
306
307 template <typename T> T static offset_to_archived_address_or_null(u4 offset) {
308 if (offset == 0) {
309 return nullptr;
|