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 "cds/archiveBuilder.hpp"
26 #include "cds/archiveHeapLoader.inline.hpp"
27 #include "cds/archiveUtils.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/classListParser.hpp"
30 #include "cds/classListWriter.hpp"
31 #include "cds/dynamicArchive.hpp"
32 #include "cds/filemap.hpp"
33 #include "cds/heapShared.hpp"
34 #include "cds/lambdaProxyClassDictionary.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* buff_bottom = (address*)ArchiveBuilder::current()->buffer_bottom();
78 address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base();
79 address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base();
80
81 // The bit in _ptrmap that cover the very first word in the rw/ro regions.
82 size_t rw_start = rw_bottom - buff_bottom;
83 size_t ro_start = ro_bottom - buff_bottom;
84
85 // The number of bits used by the rw/ro ptrmaps. We might have lots of zero
86 // bits at the bottom and top of rw/ro ptrmaps, but these zeros will be
87 // removed by FileMapInfo::write_bitmap_region().
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
91 // The last (exclusive) bit in _ptrmap that covers the rw/ro regions.
92 // Note: _ptrmap is dynamically expanded only when an actual pointer is written, so
93 // it may not be as large as we want.
94 size_t rw_end = MIN2<size_t>(rw_start + rw_size, _ptrmap->size());
95 size_t ro_end = MIN2<size_t>(ro_start + ro_size, _ptrmap->size());
96
97 rw_ptrmap->initialize(rw_size);
98 ro_ptrmap->initialize(ro_size);
99
100 for (size_t rw_bit = rw_start; rw_bit < rw_end; rw_bit++) {
101 rw_ptrmap->at_put(rw_bit - rw_start, _ptrmap->at(rw_bit));
102 }
103
104 for(size_t ro_bit = ro_start; ro_bit < ro_end; ro_bit++) {
105 ro_ptrmap->at_put(ro_bit - ro_start, _ptrmap->at(ro_bit));
106 }
107
108 _rw_ptrmap = rw_ptrmap;
109 _ro_ptrmap = ro_ptrmap;
110 }
111
112 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
113 assert(_ptrmap != nullptr, "not initialized");
114 assert(!_compacted, "cannot mark anymore");
115
116 if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
117 address value = *ptr_loc;
118 // We don't want any pointer that points to very bottom of the archive, otherwise when
119 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
120 // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
121 // of the archive.
122 assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
123
124 if (value != nullptr) {
125 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
126 size_t idx = ptr_loc - ptr_base();
127 if (_ptrmap->size() <= idx) {
128 _ptrmap->resize((idx + 1) * 2);
129 }
253 alignment = MAX2(SharedSpaceObjectAlignment, alignment);
254 char* p = (char*)align_up(_top, alignment);
255 char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment);
256 expand_top_to(newtop);
257 memset(p, 0, newtop - p);
258 return p;
259 }
260
261 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
262 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
263 intptr_t *p = (intptr_t*)_top;
264 char* newtop = _top + sizeof(intptr_t);
265 expand_top_to(newtop);
266 *p = n;
267 if (need_to_mark) {
268 ArchivePtrMarker::mark_pointer(p);
269 }
270 }
271
272 void DumpRegion::print(size_t total_bytes) const {
273 log_debug(cds)("%s space: %9zu [ %4.1f%% of total] out of %9zu bytes [%5.1f%% used] at " INTPTR_FORMAT,
274 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
275 p2i(ArchiveBuilder::current()->to_requested(_base)));
276 }
277
278 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
279 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
280 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
281 if (strcmp(_name, failing_region) == 0) {
282 log_error(cds)(" required = %d", int(needed_bytes));
283 }
284 }
285
286 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
287 _rs = rs;
288 _vs = vs;
289 // Start with 0 committed bytes. The memory will be committed as needed.
290 if (!_vs->initialize(*_rs, 0)) {
291 fatal("Unable to allocate memory for shared space");
292 }
293 _base = _top = _rs->base();
294 _end = _rs->end();
295 }
296
297 void DumpRegion::pack(DumpRegion* next) {
298 assert(!is_packed(), "sanity");
299 _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
300 _is_packed = true;
301 if (next != nullptr) {
302 next->_rs = _rs;
303 next->_vs = _vs;
304 next->_base = next->_top = this->_end;
305 next->_end = _rs->end();
306 }
307 }
308
309 void WriteClosure::do_ptr(void** p) {
310 // Write ptr into the archive; ptr can be:
311 // (a) null -> written as 0
312 // (b) a "buffered" address -> written as is
313 // (c) a "source" address -> convert to "buffered" and write
314 // The common case is (c). E.g., when writing the vmClasses into the archive.
315 // We have (b) only when we don't have a corresponding source object. E.g.,
316 // the archived c++ vtable entries.
317 address ptr = *(address*)p;
318 if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
319 ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
320 }
360 if (LambdaProxyClassDictionary::is_supported_invokedynamic(bootstrap_specifier)) {
361 const constantPoolHandle& pool = bootstrap_specifier->pool();
362 if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
363 // Currently lambda proxy classes are supported only for the built-in loaders.
364 ResourceMark rm(THREAD);
365 int pool_index = bootstrap_specifier->bss_index();
366 ClassListWriter w;
367 w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
368 CDSIndyInfo cii;
369 ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
370 GrowableArray<const char*>* indy_items = cii.items();
371 for (int i = 0; i < indy_items->length(); i++) {
372 w.stream()->print(" %s", indy_items->at(i));
373 }
374 w.stream()->cr();
375 }
376 }
377 }
378 }
379
380 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
381 if (SystemDictionaryShared::is_excluded_class(src_ik)) {
382 assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
383 return false;
384 }
385 return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
386 }
387
388 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
389 assert(seg_idx < _count, "In range");
390 return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
391 }
392
393 int HeapRootSegments::size_in_elems(size_t seg_idx) {
394 assert(seg_idx < _count, "In range");
395 if (seg_idx != _count - 1) {
396 return _max_size_in_elems;
397 } else {
398 // Last slice, leftover
399 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 "cds/archiveBuilder.hpp"
26 #include "cds/archiveHeapLoader.inline.hpp"
27 #include "cds/archiveUtils.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/classListParser.hpp"
30 #include "cds/classListWriter.hpp"
31 #include "cds/dynamicArchive.hpp"
32 #include "cds/filemap.hpp"
33 #include "cds/heapShared.hpp"
34 #include "cds/lambdaProxyClassDictionary.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* buff_bottom = (address*)ArchiveBuilder::current()->buffer_bottom();
81 address* rw_bottom = (address*)ArchiveBuilder::current()->rw_region()->base();
82 address* ro_bottom = (address*)ArchiveBuilder::current()->ro_region()->base();
83 address* cc_bottom = (address*)ArchiveBuilder::current()->cc_region()->base();
84
85 // The bit in _ptrmap that cover the very first word in the rw/ro/cc regions.
86 size_t rw_start = rw_bottom - buff_bottom;
87 size_t ro_start = ro_bottom - buff_bottom;
88 size_t cc_start = cc_bottom - buff_bottom;
89
90 // The number of bits used by the rw/ro ptrmaps. We might have lots of zero
91 // bits at the bottom and top of rw/ro ptrmaps, but these zeros will be
92 // removed by FileMapInfo::write_bitmap_region().
93 size_t rw_size = ArchiveBuilder::current()->rw_region()->used() / sizeof(address);
94 size_t ro_size = ArchiveBuilder::current()->ro_region()->used() / sizeof(address);
95 size_t cc_size = ArchiveBuilder::current()->cc_region()->used() / sizeof(address);
96
97 // The last (exclusive) bit in _ptrmap that covers the rw/ro regions.
98 // Note: _ptrmap is dynamically expanded only when an actual pointer is written, so
99 // it may not be as large as we want.
100 size_t rw_end = MIN2<size_t>(rw_start + rw_size, _ptrmap->size());
101 size_t ro_end = MIN2<size_t>(ro_start + ro_size, _ptrmap->size());
102 size_t cc_end = MIN2<size_t>(cc_start + cc_size, _ptrmap->size());
103
104 rw_ptrmap->initialize(rw_size);
105 ro_ptrmap->initialize(ro_size);
106 cc_ptrmap->initialize(cc_size);
107
108 for (size_t rw_bit = rw_start; rw_bit < rw_end; rw_bit++) {
109 rw_ptrmap->at_put(rw_bit - rw_start, _ptrmap->at(rw_bit));
110 }
111
112 for(size_t ro_bit = ro_start; ro_bit < ro_end; ro_bit++) {
113 ro_ptrmap->at_put(ro_bit - ro_start, _ptrmap->at(ro_bit));
114 }
115
116 for (size_t cc_bit = cc_start; cc_bit < cc_end; cc_bit++) {
117 cc_ptrmap->at_put(cc_bit - cc_start, _ptrmap->at(cc_bit));
118 }
119
120 _rw_ptrmap = rw_ptrmap;
121 _ro_ptrmap = ro_ptrmap;
122 _cc_ptrmap = cc_ptrmap;
123 }
124
125 void ArchivePtrMarker::mark_pointer(address* ptr_loc) {
126 assert(_ptrmap != nullptr, "not initialized");
127 assert(!_compacted, "cannot mark anymore");
128
129 if (ptr_base() <= ptr_loc && ptr_loc < ptr_end()) {
130 address value = *ptr_loc;
131 // We don't want any pointer that points to very bottom of the archive, otherwise when
132 // MetaspaceShared::default_base_address()==0, we can't distinguish between a pointer
133 // to nothing (null) vs a pointer to an objects that happens to be at the very bottom
134 // of the archive.
135 assert(value != (address)ptr_base(), "don't point to the bottom of the archive");
136
137 if (value != nullptr) {
138 assert(uintx(ptr_loc) % sizeof(intptr_t) == 0, "pointers must be stored in aligned addresses");
139 size_t idx = ptr_loc - ptr_base();
140 if (_ptrmap->size() <= idx) {
141 _ptrmap->resize((idx + 1) * 2);
142 }
266 alignment = MAX2(SharedSpaceObjectAlignment, alignment);
267 char* p = (char*)align_up(_top, alignment);
268 char* newtop = p + align_up(num_bytes, (size_t)SharedSpaceObjectAlignment);
269 expand_top_to(newtop);
270 memset(p, 0, newtop - p);
271 return p;
272 }
273
274 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) {
275 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
276 intptr_t *p = (intptr_t*)_top;
277 char* newtop = _top + sizeof(intptr_t);
278 expand_top_to(newtop);
279 *p = n;
280 if (need_to_mark) {
281 ArchivePtrMarker::mark_pointer(p);
282 }
283 }
284
285 void DumpRegion::print(size_t total_bytes) const {
286 char* base = used() > 0 ? ArchiveBuilder::current()->to_requested(_base) : nullptr;
287 log_debug(cds)("%s space: %9zu [ %4.1f%% of total] out of %9zu bytes [%5.1f%% used] at " INTPTR_FORMAT,
288 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()),
289 p2i(base));
290 }
291
292 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) {
293 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d",
294 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base));
295 if (strcmp(_name, failing_region) == 0) {
296 log_error(cds)(" required = %d", int(needed_bytes));
297 }
298 }
299
300 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) {
301 _rs = rs;
302 _vs = vs;
303 // Start with 0 committed bytes. The memory will be committed as needed.
304 if (!_vs->initialize(*_rs, 0)) {
305 fatal("Unable to allocate memory for shared space");
306 }
307 _base = _top = _rs->base();
308 _end = _rs->end();
309 }
310
311 void DumpRegion::pack(DumpRegion* next) {
312 if (!is_packed()) {
313 _end = (char*)align_up(_top, MetaspaceShared::core_region_alignment());
314 _is_packed = true;
315 }
316 if (next != nullptr) {
317 next->_rs = _rs;
318 next->_vs = _vs;
319 next->_base = next->_top = this->_end;
320 next->_end = _rs->end();
321 }
322 }
323
324 void WriteClosure::do_ptr(void** p) {
325 // Write ptr into the archive; ptr can be:
326 // (a) null -> written as 0
327 // (b) a "buffered" address -> written as is
328 // (c) a "source" address -> convert to "buffered" and write
329 // The common case is (c). E.g., when writing the vmClasses into the archive.
330 // We have (b) only when we don't have a corresponding source object. E.g.,
331 // the archived c++ vtable entries.
332 address ptr = *(address*)p;
333 if (ptr != nullptr && !ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
334 ptr = ArchiveBuilder::current()->get_buffered_addr(ptr);
335 }
375 if (LambdaProxyClassDictionary::is_supported_invokedynamic(bootstrap_specifier)) {
376 const constantPoolHandle& pool = bootstrap_specifier->pool();
377 if (SystemDictionaryShared::is_builtin_loader(pool->pool_holder()->class_loader_data())) {
378 // Currently lambda proxy classes are supported only for the built-in loaders.
379 ResourceMark rm(THREAD);
380 int pool_index = bootstrap_specifier->bss_index();
381 ClassListWriter w;
382 w.stream()->print("%s %s", ClassListParser::lambda_proxy_tag(), pool->pool_holder()->name()->as_C_string());
383 CDSIndyInfo cii;
384 ClassListParser::populate_cds_indy_info(pool, pool_index, &cii, CHECK);
385 GrowableArray<const char*>* indy_items = cii.items();
386 for (int i = 0; i < indy_items->length(); i++) {
387 w.stream()->print(" %s", indy_items->at(i));
388 }
389 w.stream()->cr();
390 }
391 }
392 }
393 }
394
395
396 // "boot", "platform", "app" or nullptr
397 const char* ArchiveUtils::builtin_loader_name_or_null(oop loader) {
398 if (loader == nullptr) {
399 return "boot";
400 } else if (loader == SystemDictionary::java_platform_loader()) {
401 return "platform";
402 } else if (loader == SystemDictionary::java_system_loader()) {
403 return "app";
404 } else {
405 return nullptr;
406 }
407 }
408
409 // "boot", "platform", "app". Asserts if not a built-in-loader
410 const char* ArchiveUtils::builtin_loader_name(oop loader) {
411 const char* name = builtin_loader_name_or_null(loader);
412 assert(name != nullptr, "must be a built-in loader");
413 return name;
414 }
415
416 bool ArchiveUtils::builtin_loader_from_type(const char* loader_type, oop* value_ret) {
417 if (strcmp(loader_type, "boot") == 0) {
418 *value_ret = nullptr;
419 return true;
420 } else if (strcmp(loader_type, "platform") == 0) {
421 *value_ret = SystemDictionary::java_platform_loader();
422 return true;
423 } else if (strcmp(loader_type, "app") == 0) {
424 *value_ret = SystemDictionary::java_system_loader();
425 return true;
426 } else {
427 DEBUG_ONLY(*value_ret = cast_to_oop((void*)badOopVal));
428 return false;
429 }
430 }
431
432 oop ArchiveUtils::builtin_loader_from_type(int loader_type) {
433 if (loader_type == ClassLoader::BOOT_LOADER) {
434 return nullptr;
435 } else if (loader_type == ClassLoader::PLATFORM_LOADER) {
436 return SystemDictionary::java_platform_loader();
437 } else if (loader_type == ClassLoader::APP_LOADER) {
438 return SystemDictionary::java_system_loader();
439 } else {
440 ShouldNotReachHere();
441 return nullptr;
442 }
443 }
444
445 bool ArchiveUtils::has_aot_initialized_mirror(InstanceKlass* src_ik) {
446 if (SystemDictionaryShared::is_excluded_class(src_ik)) {
447 assert(!ArchiveBuilder::current()->has_been_buffered(src_ik), "sanity");
448 return false;
449 }
450 return ArchiveBuilder::current()->get_buffered_addr(src_ik)->has_aot_initialized_mirror();
451 }
452
453 size_t HeapRootSegments::size_in_bytes(size_t seg_idx) {
454 assert(seg_idx < _count, "In range");
455 return objArrayOopDesc::object_size(size_in_elems(seg_idx)) * HeapWordSize;
456 }
457
458 int HeapRootSegments::size_in_elems(size_t seg_idx) {
459 assert(seg_idx < _count, "In range");
460 if (seg_idx != _count - 1) {
461 return _max_size_in_elems;
462 } else {
463 // Last slice, leftover
464 return _roots_count % _max_size_in_elems;
|