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 #include "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapLoader.inline.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classListParser.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/dynamicArchive.hpp"
33 #include "cds/filemap.hpp"
34 #include "cds/heapShared.hpp"
35 #include "cds/metaspaceShared.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "classfile/vmClasses.hpp"
38 #include "interpreter/bootstrapInfo.hpp"
39 #include "memory/metaspaceUtils.hpp"
40 #include "memory/resourceArea.hpp"
41 #include "oops/compressedOops.inline.hpp"
42 #include "oops/klass.inline.hpp"
43 #include "runtime/arguments.hpp"
44 #include "utilities/bitMap.inline.hpp"
45 #include "utilities/debug.hpp"
46 #include "utilities/formatBuffer.hpp"
47 #include "utilities/globalDefinitions.hpp"
48 #include "utilities/spinYield.hpp"
49
50 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr;
51 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr;
52 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr;
53 VirtualSpace* ArchivePtrMarker::_vs;
54
55 bool ArchivePtrMarker::_compacted;
56
57 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
58 assert(_ptrmap == nullptr, "initialize only once");
59 assert(_rw_ptrmap == nullptr, "initialize only once");
60 assert(_ro_ptrmap == nullptr, "initialize only once");
61 _vs = vs;
62 _compacted = false;
63 _ptrmap = ptrmap;
64
65 // Use this as initial guesstimate. We should need less space in the
66 // archive, but if we're wrong the bitmap will be expanded automatically.
67 size_t estimated_archive_size = MetaspaceGC::capacity_until_GC();
68 // But set it smaller in debug builds so we always test the expansion code.
69 // (Default archive is about 12MB).
70 DEBUG_ONLY(estimated_archive_size = 6 * M);
71
72 // We need one bit per pointer in the archive.
73 _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t));
74 }
75
76 void ArchivePtrMarker::initialize_rw_ro_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap) {
77 address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base();
78 address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base();
79
80 _rw_ptrmap = rw_ptrmap;
81 _ro_ptrmap = ro_ptrmap;
82
83 size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
84 size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);
85 // ro_start is the first bit in _ptrmap that covers the pointer that would sit at ro_bottom.
86 // E.g., if rw_bottom = (address*)100
87 // ro_bottom = (address*)116
88 // then for 64-bit platform:
89 // ro_start = ro_bottom - rw_bottom = (116 - 100) / sizeof(address) = 2;
90 size_t ro_start = ro_bottom - rw_bottom;
91
92 // Note: ptrmap is big enough only to cover the last pointer in ro_region.
93 // See ArchivePtrMarker::compact()
94 _rw_ptrmap->initialize(rw_size);
95 _ro_ptrmap->initialize(_ptrmap->size() - ro_start);
96
97 for (size_t rw_bit = 0; rw_bit < _rw_ptrmap->size(); rw_bit++) {
98 _rw_ptrmap->at_put(rw_bit, _ptrmap->at(rw_bit));
99 }
100
101 for(size_t ro_bit = ro_start; ro_bit < _ptrmap->size(); ro_bit++) {
102 _ro_ptrmap->at_put(ro_bit-ro_start, _ptrmap->at(ro_bit));
103 }
104 assert(_ptrmap->size() - ro_start == _ro_ptrmap->size(), "must be");
105 }
106
107 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
108 assert(_ptrmap != nullptr, "not initialized");
109 assert(!_compacted, "cannot mark anymore");
110
111 if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
112 address value = *ptr_loc;
113 // We don't want any pointer that points to very bottom of the archive, otherwise when
114 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
115 // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
116 // of the archive.
117 assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
118
119 if (value != nullptr) {
120 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
121 size_t idx = ptr_loc - ptr_base();
122 if (_ptrmap->size() <= idx) {
123 _ptrmap->resize((idx + 1) * 2);
124 }
248 alignment = MAX2(SharedSpaceObjectAlignment, alignment);
249 char* p = (char*)align_up(_top, alignment);
250 char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment);
251 expand_top_to(newtop);
252 memset(p, 0, newtop - p);
253 return p;
254 }
255
256 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
257 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
258 intptr_t *p = (intptr_t*)_top;
259 char* newtop = _top + sizeof(intptr_t);
260 expand_top_to(newtop);
261 *p = n;
262 if (need_to_mark) {
263 ArchivePtrMarker::mark_pointer(p);
264 }
265 }
266
267 void DumpRegion::print(size_t total_bytes) const {
268 log_debug(cds)("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
269 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
270 p2i(ArchiveBuilder::current()->to_requested(_base)));
271 }
272
273 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
274 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
275 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
276 if (strcmp(_name, failing_region) == 0) {
277 log_error(cds)(" required = %d", int(needed_bytes));
278 }
279 }
280
281 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
282 _rs = rs;
283 _vs = vs;
284 // Start with 0 committed bytes. The memory will be committed as needed.
285 if (!_vs->initialize(*_rs, 0)) {
286 fatal("Unable to allocate memory for shared space");
287 }
288 _base = _top = _rs->base();
289 _end = _rs->end();
290 }
291
292 void DumpRegion::pack(DumpRegion* next) {
293 assert(!is_packed(), "sanity");
294 _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
295 _is_packed = true;
296 if (next != nullptr) {
297 next->_rs = _rs;
298 next->_vs = _vs;
299 next->_base = next->_top = this->_end;
300 next->_end = _rs->end();
301 }
302 }
303
304 void WriteClosure::do_ptr(void** p) {
305 // Write ptr into the archive; ptr can be:
306 // (a) null -> written as 0
307 // (b) a "buffered" address -> written as is
308 // (c) a "source" address -> convert to "buffered" and write
309 // The common case is (c). E.g., when writing the vmClasses into the archive.
310 // We have (b) only when we don't have a corresponding source object. E.g.,
311 // the archived c++ vtable entries.
312 address ptr = *(address*)p;
313 if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
314 ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
315 }
355 if (SystemDictionaryShared::is_supported_invokedynamic(bootstrap_specifier)) {
356 const constantPoolHandle& pool = bootstrap_specifier->pool();
357 if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
358 // Currently lambda proxy classes are supported only for the built-in loaders.
359 ResourceMark rm(THREAD);
360 int pool_index = bootstrap_specifier->bss_index();
361 ClassListWriter w;
362 w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
363 CDSIndyInfo cii;
364 ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
365 GrowableArray<const char*>* indy_items = cii.items();
366 for (int i = 0; i < indy_items->length(); i++) {
367 w.stream()->print(" %s", indy_items->at(i));
368 }
369 w.stream()->cr();
370 }
371 }
372 }
373 }
374
375 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
376 if (SystemDictionaryShared::is_excluded_class(src_ik)) {
377 assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
378 return false;
379 }
380 return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
381 }
382
383 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
384 assert(seg_idx < _count, "In range");
385 return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
386 }
387
388 int HeapRootSegments::size_in_elems(size_t seg_idx) {
389 assert(seg_idx < _count, "In range");
390 if (seg_idx != _count - 1) {
391 return _max_size_in_elems;
392 } else {
393 // Last slice, leftover
394 return _roots_count % _max_size_in_elems;
|
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 #include "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapLoader.inline.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classListParser.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/dynamicArchive.hpp"
33 #include "cds/filemap.hpp"
34 #include "cds/heapShared.hpp"
35 #include "cds/metaspaceShared.hpp"
36 #include "classfile/classLoader.hpp"
37 #include "classfile/systemDictionaryShared.hpp"
38 #include "classfile/vmClasses.hpp"
39 #include "interpreter/bootstrapInfo.hpp"
40 #include "memory/metaspaceUtils.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "oops/compressedOops.inline.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "runtime/arguments.hpp"
45 #include "utilities/bitMap.inline.hpp"
46 #include "utilities/debug.hpp"
47 #include "utilities/formatBuffer.hpp"
48 #include "utilities/globalDefinitions.hpp"
49 #include "utilities/spinYield.hpp"
50
51 CHeapBitMap* ArchivePtrMarker::_ptrmap = nullptr;
52 CHeapBitMap* ArchivePtrMarker::_rw_ptrmap = nullptr;
53 CHeapBitMap* ArchivePtrMarker::_ro_ptrmap = nullptr;
54 CHeapBitMap* ArchivePtrMarker::_cc_ptrmap = nullptr;
55 VirtualSpace* ArchivePtrMarker::_vs;
56
57 bool ArchivePtrMarker::_compacted;
58
59 void ArchivePtrMarker::initialize(CHeapBitMap* ptrmap, VirtualSpace* vs) {
60 assert(_ptrmap == nullptr, "initialize only once");
61 assert(_rw_ptrmap == nullptr, "initialize only once");
62 assert(_ro_ptrmap == nullptr, "initialize only once");
63 assert(_cc_ptrmap == nullptr, "initialize only once");
64 _vs = vs;
65 _compacted = false;
66 _ptrmap = ptrmap;
67
68 // Use this as initial guesstimate. We should need less space in the
69 // archive, but if we're wrong the bitmap will be expanded automatically.
70 size_t estimated_archive_size = MetaspaceGC::capacity_until_GC();
71 // But set it smaller in debug builds so we always test the expansion code.
72 // (Default archive is about 12MB).
73 DEBUG_ONLY(estimated_archive_size = 6 * M);
74
75 // We need one bit per pointer in the archive.
76 _ptrmap->initialize(estimated_archive_size / sizeof(intptr_t));
77 }
78
79 void ArchivePtrMarker::initialize_rw_ro_cc_maps(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, CHeapBitMap* cc_ptrmap) {
80 address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base();
81 address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base();
82 address* cc_bottom = (address*)ArchiveBuilder::current()->cc_region()->base();
83
84 _rw_ptrmap = rw_ptrmap;
85 _ro_ptrmap = ro_ptrmap;
86 _cc_ptrmap = cc_ptrmap;
87
88 size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
89 size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);
90 size_t cc_size = ArchiveBuilder::current()->cc_region()->used() / sizeof(address);
91 // ro_start is the first bit in _ptrmap that covers the pointer that would sit at ro_bottom.
92 // E.g., if rw_bottom = (address*)100
93 // ro_bottom = (address*)116
94 // then for 64-bit platform:
95 // ro_start = ro_bottom - rw_bottom = (116 - 100) / sizeof(address) = 2;
96 size_t ro_start = ro_bottom - rw_bottom;
97 size_t cc_start = cc_bottom - rw_bottom;
98
99 // Note: ptrmap is big enough only to cover the last pointer in cc_region or ro_region.
100 // See ArchivePtrMarker::compact()
101 if (ro_start + ro_size >_ptrmap->size()) {
102 ro_size = _ptrmap->size() - ro_start; // ro is smaller than we thought
103 cc_size = 0; // cc is empty
104 } else if (cc_size != 0 && cc_start + cc_size > _ptrmap->size()) {
105 cc_size = _ptrmap->size() - cc_start; // ro is smaller than we thought
106 }
107
108 assert(rw_size < _ptrmap->size(), "sanity");
109 assert(ro_size < _ptrmap->size(), "sanity");
110 assert(cc_size < _ptrmap->size(), "sanity");
111 assert(rw_size + ro_size + cc_size <= _ptrmap->size(), "sanity");
112
113 _rw_ptrmap->initialize(rw_size);
114 _ro_ptrmap->initialize(ro_size);
115 _cc_ptrmap->initialize(cc_size);
116
117 for (size_t i = 0; i < rw_size; i++) {
118 _rw_ptrmap->at_put(i, _ptrmap->at(i));
119 }
120 for (size_t i = 0; i < ro_size; i++) {
121 _ro_ptrmap->at_put(i, _ptrmap->at(ro_start + i));
122 }
123 for (size_t i = 0; i < cc_size; i++) {
124 _cc_ptrmap->at_put(i, _ptrmap->at(cc_start + i));
125 }
126 }
127
128 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
129 assert(_ptrmap != nullptr, "not initialized");
130 assert(!_compacted, "cannot mark anymore");
131
132 if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
133 address value = *ptr_loc;
134 // We don't want any pointer that points to very bottom of the archive, otherwise when
135 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
136 // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
137 // of the archive.
138 assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
139
140 if (value != nullptr) {
141 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
142 size_t idx = ptr_loc - ptr_base();
143 if (_ptrmap->size() <= idx) {
144 _ptrmap->resize((idx + 1) * 2);
145 }
269 alignment = MAX2(SharedSpaceObjectAlignment, alignment);
270 char* p = (char*)align_up(_top, alignment);
271 char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment);
272 expand_top_to(newtop);
273 memset(p, 0, newtop - p);
274 return p;
275 }
276
277 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
278 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
279 intptr_t *p = (intptr_t*)_top;
280 char* newtop = _top + sizeof(intptr_t);
281 expand_top_to(newtop);
282 *p = n;
283 if (need_to_mark) {
284 ArchivePtrMarker::mark_pointer(p);
285 }
286 }
287
288 void DumpRegion::print(size_t total_bytes) const {
289 char* base = used() > 0 ? ArchiveBuilder::current()->to_requested(_base) : nullptr;
290 log_debug(cds)("%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT,
291 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
292 p2i(base));
293 }
294
295 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
296 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
297 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
298 if (strcmp(_name, failing_region) == 0) {
299 log_error(cds)(" required = %d", int(needed_bytes));
300 }
301 }
302
303 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
304 _rs = rs;
305 _vs = vs;
306 // Start with 0 committed bytes. The memory will be committed as needed.
307 if (!_vs->initialize(*_rs, 0)) {
308 fatal("Unable to allocate memory for shared space");
309 }
310 _base = _top = _rs->base();
311 _end = _rs->end();
312 }
313
314 void DumpRegion::pack(DumpRegion* next) {
315 if (!is_packed()) {
316 _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
317 _is_packed = true;
318 }
319 if (next != nullptr) {
320 next->_rs = _rs;
321 next->_vs = _vs;
322 next->_base = next->_top = this->_end;
323 next->_end = _rs->end();
324 }
325 }
326
327 void WriteClosure::do_ptr(void** p) {
328 // Write ptr into the archive; ptr can be:
329 // (a) null -> written as 0
330 // (b) a "buffered" address -> written as is
331 // (c) a "source" address -> convert to "buffered" and write
332 // The common case is (c). E.g., when writing the vmClasses into the archive.
333 // We have (b) only when we don't have a corresponding source object. E.g.,
334 // the archived c++ vtable entries.
335 address ptr = *(address*)p;
336 if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
337 ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
338 }
378 if (SystemDictionaryShared::is_supported_invokedynamic(bootstrap_specifier)) {
379 const constantPoolHandle& pool = bootstrap_specifier->pool();
380 if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
381 // Currently lambda proxy classes are supported only for the built-in loaders.
382 ResourceMark rm(THREAD);
383 int pool_index = bootstrap_specifier->bss_index();
384 ClassListWriter w;
385 w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
386 CDSIndyInfo cii;
387 ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
388 GrowableArray<const char*>* indy_items = cii.items();
389 for (int i = 0; i < indy_items->length(); i++) {
390 w.stream()->print(" %s", indy_items->at(i));
391 }
392 w.stream()->cr();
393 }
394 }
395 }
396 }
397
398
399 // "boot", "platform", "app" or nullptr
400 const char* ArchiveUtils::builtin_loader_name_or_null(oop loader) {
401 if (loader == nullptr) {
402 return "boot";
403 } else if (loader == SystemDictionary::java_platform_loader()) {
404 return "platform";
405 } else if (loader == SystemDictionary::java_system_loader()) {
406 return "app";
407 } else {
408 return nullptr;
409 }
410 }
411
412 // "boot", "platform", "app". Asserts if not a built-in-loader
413 const char* ArchiveUtils::builtin_loader_name(oop loader) {
414 const char* name = builtin_loader_name_or_null(loader);
415 assert(name != nullptr, "must be a built-in loader");
416 return name;
417 }
418
419 bool ArchiveUtils::builtin_loader_from_type(const char* loader_type, oop* value_ret) {
420 if (strcmp(loader_type, "boot") == 0) {
421 *value_ret = nullptr;
422 return true;
423 } else if (strcmp(loader_type, "platform") == 0) {
424 *value_ret = SystemDictionary::java_platform_loader();
425 return true;
426 } else if (strcmp(loader_type, "app") == 0) {
427 *value_ret = SystemDictionary::java_system_loader();
428 return true;
429 } else {
430 DEBUG_ONLY(*value_ret = cast_to_oop((void*)badOopVal));
431 return false;
432 }
433 }
434
435 oop ArchiveUtils::builtin_loader_from_type(int loader_type) {
436 if (loader_type == ClassLoader::BOOT_LOADER) {
437 return nullptr;
438 } else if (loader_type == ClassLoader::PLATFORM_LOADER) {
439 return SystemDictionary::java_platform_loader();
440 } else if (loader_type == ClassLoader::APP_LOADER) {
441 return SystemDictionary::java_system_loader();
442 } else {
443 ShouldNotReachHere();
444 return nullptr;
445 }
446 }
447
448 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
449 if (SystemDictionaryShared::is_excluded_class(src_ik)) {
450 assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
451 return false;
452 }
453 return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
454 }
455
456 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
457 assert(seg_idx < _count, "In range");
458 return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
459 }
460
461 int HeapRootSegments::size_in_elems(size_t seg_idx) {
462 assert(seg_idx < _count, "In range");
463 if (seg_idx != _count - 1) {
464 return _max_size_in_elems;
465 } else {
466 // Last slice, leftover
467 return _roots_count % _max_size_in_elems;
|