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 #include "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassLinker.hpp"
27 #include "cds/aotLinkedClassBulkLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/archiveHeapWriter.hpp"
30 #include "cds/archiveUtils.hpp"
31 #include "cds/cdsConfig.hpp"
32 #include "cds/cppVtables.hpp"
33 #include "cds/dumpAllocStats.hpp"
34 #include "cds/dynamicArchive.hpp"
35 #include "cds/heapShared.hpp"
36 #include "cds/metaspaceShared.hpp"
37 #include "cds/regeneratedClasses.hpp"
38 #include "classfile/classLoader.hpp"
39 #include "classfile/classLoaderDataShared.hpp"
40 #include "classfile/classLoaderExt.hpp"
41 #include "classfile/javaClasses.hpp"
42 #include "classfile/symbolTable.hpp"
43 #include "classfile/systemDictionaryShared.hpp"
44 #include "classfile/vmClasses.hpp"
45 #include "code/aotCodeCache.hpp"
46 #include "interpreter/abstractInterpreter.hpp"
47 #include "jvm.h"
48 #include "logging/log.hpp"
49 #include "logging/logStream.hpp"
50 #include "memory/allStatic.hpp"
51 #include "memory/memoryReserver.hpp"
52 #include "memory/memRegion.hpp"
53 #include "memory/resourceArea.hpp"
54 #include "oops/compressedKlass.inline.hpp"
55 #include "oops/instanceKlass.hpp"
56 #include "oops/objArrayKlass.hpp"
57 #include "oops/objArrayOop.inline.hpp"
58 #include "oops/oopHandle.inline.hpp"
59 #include "runtime/arguments.hpp"
60 #include "runtime/fieldDescriptor.inline.hpp"
61 #include "runtime/globals_extension.hpp"
62 #include "runtime/javaThread.hpp"
63 #include "runtime/sharedRuntime.hpp"
64 #include "utilities/align.hpp"
65 #include "utilities/bitMap.inline.hpp"
66 #include "utilities/formatBuffer.hpp"
67
68 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
69
70 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
71 char* newtop = ArchiveBuilder::current()->_ro_region.top();
72 ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
73 }
74
75 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
76 _total_bytes = 0;
77 _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
78 }
113 assert(field_offset_in_bytes >= 0, "must be");
114 assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be");
115 assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be");
116
117 BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address)));
118 _ptrmap.set_bit(BitMap::idx_t(idx));
119 }
120
121 class RelocateEmbeddedPointers : public BitMapClosure {
122 ArchiveBuilder* _builder;
123 address _buffered_obj;
124 BitMap::idx_t _start_idx;
125 public:
126 RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) :
127 _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {}
128
129 bool do_bit(BitMap::idx_t bit_offset) {
130 size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address);
131 address* ptr_loc = (address*)(_buffered_obj + field_offset);
132
133 address old_p = *ptr_loc;
134 address new_p = _builder->get_buffered_addr(old_p);
135
136 log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT,
137 p2i(ptr_loc), p2i(old_p), p2i(new_p));
138
139 ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p);
140 return true; // keep iterating the bitmap
141 }
142 };
143
144 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) {
145 SourceObjInfo* src_info = objs()->at(i);
146 assert(src_info->should_copy(), "must be");
147 BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive
148 BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive
149
150 RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
151 _ptrmap.iterate(&relocator, start, end);
152 }
153
154 ArchiveBuilder::ArchiveBuilder() :
155 _current_dump_region(nullptr),
156 _buffer_bottom(nullptr),
157 _requested_static_archive_bottom(nullptr),
158 _requested_static_archive_top(nullptr),
159 _requested_dynamic_archive_bottom(nullptr),
160 _requested_dynamic_archive_top(nullptr),
161 _mapped_static_archive_bottom(nullptr),
162 _mapped_static_archive_top(nullptr),
163 _buffer_to_requested_delta(0),
164 _pz_region("pz", MAX_SHARED_DELTA), // protection zone -- used only during dumping; does NOT exist in cds archive.
165 _rw_region("rw", MAX_SHARED_DELTA),
166 _ro_region("ro", MAX_SHARED_DELTA),
167 _ac_region("ac", MAX_SHARED_DELTA),
168 _ptrmap(mtClassShared),
169 _rw_ptrmap(mtClassShared),
170 _ro_ptrmap(mtClassShared),
171 _rw_src_objs(),
172 _ro_src_objs(),
173 _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
174 _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
175 _total_heap_region_size(0)
176 {
177 _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
178 _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
179 _entropy_seed = 0x12345678;
180 assert(_current == nullptr, "must be");
181 _current = this;
182 }
183
184 ArchiveBuilder::~ArchiveBuilder() {
185 assert(_current == this, "must be");
186 _current = nullptr;
187
188 for (int i = 0; i < _symbols->length(); i++) {
189 _symbols->at(i)->decrement_refcount();
190 }
191
192 delete _klasses;
193 delete _symbols;
194 if (_shared_rs.is_reserved()) {
195 MemoryReserver::release(_shared_rs);
196 }
197
198 AOTArtifactFinder::dispose();
199 }
419 }
420
421 FollowMode follow_mode = get_follow_mode(ref);
422 SourceObjInfo src_info(ref, read_only, follow_mode);
423 bool created;
424 SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
425 if (created) {
426 if (_src_obj_table.maybe_grow()) {
427 log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
428 }
429 }
430
431 #ifdef ASSERT
432 if (ref->msotype() == MetaspaceObj::MethodType) {
433 Method* m = (Method*)ref->obj();
434 assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
435 "Should not archive methods in a class that has been regenerated");
436 }
437 #endif
438
439 assert(p->read_only() == src_info.read_only(), "must be");
440
441 if (created && src_info.should_copy()) {
442 if (read_only) {
443 _ro_src_objs.append(p);
444 } else {
445 _rw_src_objs.append(p);
446 }
447 return true; // Need to recurse into this ref only if we are copying it
448 } else {
449 return false;
450 }
451 }
452
453 void ArchiveBuilder::record_regenerated_object(address orig_src_obj, address regen_src_obj) {
454 // Record the fact that orig_src_obj has been replaced by regen_src_obj. All calls to get_buffered_addr(orig_src_obj)
455 // should return the same value as get_buffered_addr(regen_src_obj).
456 SourceObjInfo* p = _src_obj_table.get(regen_src_obj);
457 assert(p != nullptr, "regenerated object should always be dumped");
458 SourceObjInfo orig_src_info(orig_src_obj, p);
516 return SystemDictionaryShared::is_excluded_class(ik);
517 } else if (klass->is_objArray_klass()) {
518 Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
519 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_shared_static(bottom)) {
520 // The bottom class is in the static archive so it's clearly not excluded.
521 return false;
522 } else if (bottom->is_instance_klass()) {
523 return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
524 }
525 }
526
527 return false;
528 }
529
530 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
531 address obj = ref->obj();
532 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(obj)) {
533 // Don't dump existing shared metadata again.
534 return point_to_it;
535 } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
536 ref->msotype() == MetaspaceObj::MethodCountersType) {
537 return set_to_null;
538 } else if (ref->msotype() == MetaspaceObj::AdapterHandlerEntryType) {
539 if (AOTCodeCache::is_dumping_adapters()) {
540 AdapterHandlerEntry* entry = (AdapterHandlerEntry*)ref->obj();
541 return AdapterHandlerLibrary::is_abstract_method_adapter(entry) ? set_to_null : make_a_copy;
542 } else {
543 return set_to_null;
544 }
545 } else {
546 if (ref->msotype() == MetaspaceObj::ClassType) {
547 Klass* klass = (Klass*)ref->obj();
548 assert(klass->is_klass(), "must be");
549 if (is_excluded(klass)) {
550 ResourceMark rm;
551 log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
552 return set_to_null;
553 }
554 }
555
556 return make_a_copy;
557 }
558 }
559
698 // not handled by MetaspaceClosure.
699 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
700 assert(is_in_buffer_space(ptr_location), "must be");
701 if (src_addr == nullptr) {
702 *ptr_location = nullptr;
703 ArchivePtrMarker::clear_pointer(ptr_location);
704 } else {
705 *ptr_location = get_buffered_addr(src_addr);
706 ArchivePtrMarker::mark_pointer(ptr_location);
707 }
708 }
709
710 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) {
711 assert(*ptr_location != nullptr, "sanity");
712 if (!is_in_mapped_static_archive(*ptr_location)) {
713 *ptr_location = get_buffered_addr(*ptr_location);
714 }
715 ArchivePtrMarker::mark_pointer(ptr_location);
716 }
717
718 bool ArchiveBuilder::has_been_archived(address src_addr) const {
719 SourceObjInfo* p = _src_obj_table.get(src_addr);
720 return (p != nullptr);
721 }
722
723 bool ArchiveBuilder::has_been_buffered(address src_addr) const {
724 if (RegeneratedClasses::has_been_regenerated(src_addr) ||
725 _src_obj_table.get(src_addr) == nullptr ||
726 get_buffered_addr(src_addr) == nullptr) {
727 return false;
728 } else {
729 return true;
730 }
731 }
732
733 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
734 SourceObjInfo* p = _src_obj_table.get(src_addr);
735 assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
736 p2i(src_addr));
737
738 return p->buffered_addr();
739 }
740
741 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
742 assert(is_in_buffer_space(buffered_addr), "must be");
743 address* src_p = _buffered_to_src_table.get(buffered_addr);
744 assert(src_p != nullptr && *src_p != nullptr, "must be");
745 return *src_p;
746 }
747
748 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
749 for (int i = 0; i < src_objs->objs()->length(); i++) {
750 src_objs->relocate(i, this);
751 }
752 }
753
754 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
755 log_info(cds)("Relocating embedded pointers in core regions ... ");
756 relocate_embedded_pointers(&_rw_src_objs);
757 relocate_embedded_pointers(&_ro_src_objs);
758 }
759
760 #define ADD_COUNT(x) \
761 x += 1; \
762 x ## _a += aotlinked ? 1 : 0; \
763 x ## _i += inited ? 1 : 0;
764
765 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \
766 int x = 0; \
767 int x ## _a = 0; \
768 int x ## _i = 0;
769
770 void ArchiveBuilder::make_klasses_shareable() {
771 DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses);
772 DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses);
773 DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses);
774 DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses);
775 DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses);
776 DECLARE_INSTANCE_KLASS_COUNTER(num_old_klasses);
777 DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses);
939 log_info(cds)(" boot " STATS_FORMAT, STATS_PARAMS(boot_klasses));
940 log_info(cds)(" vm " STATS_FORMAT, STATS_PARAMS(vm_klasses));
941 log_info(cds)(" platform " STATS_FORMAT, STATS_PARAMS(platform_klasses));
942 log_info(cds)(" app " STATS_FORMAT, STATS_PARAMS(app_klasses));
943 log_info(cds)(" unregistered " STATS_FORMAT, STATS_PARAMS(unregistered_klasses));
944 log_info(cds)(" (enum) " STATS_FORMAT, STATS_PARAMS(enum_klasses));
945 log_info(cds)(" (hidden) " STATS_FORMAT, STATS_PARAMS(hidden_klasses));
946 log_info(cds)(" (old) " STATS_FORMAT, STATS_PARAMS(old_klasses));
947 log_info(cds)(" (unlinked) = %5d, boot = %d, plat = %d, app = %d, unreg = %d",
948 num_unlinked_klasses, boot_unlinked, platform_unlinked, app_unlinked, unreg_unlinked);
949 log_info(cds)(" obj array classes = %5d", num_obj_array_klasses);
950 log_info(cds)(" type array classes = %5d", num_type_array_klasses);
951 log_info(cds)(" symbols = %5d", _symbols->length());
952
953 #undef STATS_FORMAT
954 #undef STATS_PARAMS
955
956 DynamicArchive::make_array_klasses_shareable();
957 }
958
959 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
960 SymbolTable::serialize_shared_table_header(soc, false);
961 SystemDictionaryShared::serialize_dictionary_headers(soc, false);
962 DynamicArchive::serialize_array_klasses(soc);
963 AOTLinkedClassBulkLoader::serialize(soc, false);
964 }
965
966 uintx ArchiveBuilder::buffer_to_offset(address p) const {
967 address requested_p = to_requested(p);
968 assert(requested_p >= _requested_static_archive_bottom, "must be");
969 return requested_p - _requested_static_archive_bottom;
970 }
971
972 uintx ArchiveBuilder::any_to_offset(address p) const {
973 if (is_in_mapped_static_archive(p)) {
974 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
975 return p - _mapped_static_archive_bottom;
976 }
977 if (!is_in_buffer_space(p)) {
978 // p must be a "source" address
979 p = get_buffered_addr(p);
980 }
981 return buffer_to_offset(p);
982 }
983
984 address ArchiveBuilder::offset_to_buffered_address(u4 offset) const {
985 address requested_addr = _requested_static_archive_bottom + offset;
986 address buffered_addr = requested_addr - _buffer_to_requested_delta;
987 assert(is_in_buffer_space(buffered_addr), "bad offset");
988 return buffered_addr;
989 }
990
991 void ArchiveBuilder::start_ac_region() {
992 ro_region()->pack();
993 start_dump_region(&_ac_region);
994 }
995
996 void ArchiveBuilder::end_ac_region() {
997 _ac_region.pack();
998 }
999
1000 #if INCLUDE_CDS_JAVA_HEAP
1001 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
1002 assert(CDSConfig::is_dumping_heap(), "sanity");
1003 k = get_buffered_klass(k);
1004 Klass* requested_k = to_requested(k);
1005 const int narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
1006 #ifdef ASSERT
1007 const size_t klass_alignment = MAX2(SharedSpaceObjectAlignment, (size_t)nth_bit(narrow_klass_shift));
1008 assert(is_aligned(k, klass_alignment), "Klass " PTR_FORMAT " misaligned.", p2i(k));
1009 #endif
1010 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
1011 // Note: use the "raw" version of encode that takes explicit narrow klass base and shift. Don't use any
1012 // of the variants that do sanity checks, nor any of those that use the current - dump - JVM's encoding setting.
1013 return CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
1014 }
1015 #endif // INCLUDE_CDS_JAVA_HEAP
1016
1017 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
1018 // so that the archive can be mapped to the "requested" location without runtime relocation.
1019 //
1092
1093 #ifdef _LP64
1094 int ArchiveBuilder::precomputed_narrow_klass_shift() {
1095 // Legacy Mode:
1096 // We use 32 bits for narrowKlass, which should cover the full 4G Klass range. Shift can be 0.
1097 // CompactObjectHeader Mode:
1098 // narrowKlass is much smaller, and we use the highest possible shift value to later get the maximum
1099 // Klass encoding range.
1100 //
1101 // Note that all of this may change in the future, if we decide to correct the pre-calculated
1102 // narrow Klass IDs at archive load time.
1103 assert(UseCompressedClassPointers, "Only needed for compressed class pointers");
1104 return UseCompactObjectHeaders ? CompressedKlassPointers::max_shift() : 0;
1105 }
1106 #endif // _LP64
1107
1108 void ArchiveBuilder::relocate_to_requested() {
1109 if (!ro_region()->is_packed()) {
1110 ro_region()->pack();
1111 }
1112 size_t my_archive_size = buffer_top() - buffer_bottom();
1113
1114 if (CDSConfig::is_dumping_static_archive()) {
1115 _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
1116 RelocateBufferToRequested<true> patcher(this);
1117 patcher.doit();
1118 } else {
1119 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1120 _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
1121 RelocateBufferToRequested<false> patcher(this);
1122 patcher.doit();
1123 }
1124 }
1125
1126 // Write detailed info to a mapfile to analyze contents of the archive.
1127 // static dump:
1128 // java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
1129 // dynamic dump:
1130 // java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \
1131 // -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp
1416 assert(java_lang_Class::is_instance(scratch_mirror), "sanity");
1417 if (java_lang_Class::is_primitive(scratch_mirror)) {
1418 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
1419 BasicType bt = (BasicType)i;
1420 if (!is_reference_type(bt) && scratch_mirror == HeapShared::scratch_java_mirror(bt)) {
1421 oop orig_mirror = Universe::java_mirror(bt);
1422 java_lang_Class::print_signature(orig_mirror, st);
1423 return;
1424 }
1425 }
1426 ShouldNotReachHere();
1427 }
1428 java_lang_Class::print_signature(scratch_mirror, st);
1429 }
1430
1431 static void log_heap_roots() {
1432 LogStreamHandle(Trace, cds, map, oops) st;
1433 if (st.is_enabled()) {
1434 for (int i = 0; i < HeapShared::pending_roots()->length(); i++) {
1435 st.print("roots[%4d]: ", i);
1436 print_oop_info_cr(&st, HeapShared::pending_roots()->at(i));
1437 }
1438 }
1439 }
1440
1441 // Example output:
1442 // - The first number is the requested address (if print_requested_addr == true)
1443 // - The second number is the narrowOop version of the requested address (if UseCompressedOops == true)
1444 // 0x00000007ffc7e840 (0xfff8fd08) java.lang.Class Ljava/util/Array;
1445 // 0x00000007ffc000f8 (0xfff8001f) [B length: 11
1446 static void print_oop_info_cr(outputStream* st, oop source_oop, bool print_requested_addr = true) {
1447 if (source_oop == nullptr) {
1448 st->print_cr("null");
1449 } else {
1450 ResourceMark rm;
1451 oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop);
1452 if (print_requested_addr) {
1453 st->print(PTR_FORMAT " ", p2i(requested_obj));
1454 }
1455 if (UseCompressedOops) {
1456 st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj));
1457 }
1458 if (source_oop->is_array()) {
1459 int array_len = arrayOop(source_oop)->length();
1460 st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len);
1461 } else {
1462 st->print("%s", source_oop->klass()->external_name());
1463
1464 if (java_lang_String::is_instance(source_oop)) {
1465 st->print(" ");
1466 java_lang_String::print(source_oop, st);
1467 } else if (java_lang_Class::is_instance(source_oop)) {
1468 oop scratch_mirror = source_oop;
1469
1470 st->print(" ");
1471 print_class_signature_for_mirror(st, scratch_mirror);
1472
1473 Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror);
1474 if (src_klass != nullptr && src_klass->is_instance_klass()) {
1475 InstanceKlass* buffered_klass =
1476 ArchiveBuilder::current()->get_buffered_addr(InstanceKlass::cast(src_klass));
1477 if (buffered_klass->has_aot_initialized_mirror()) {
1478 st->print(" (aot-inited)");
1479 }
1480 }
1481 }
1482 st->cr();
1483 }
1484 }
1485 }
1486 #endif // INCLUDE_CDS_JAVA_HEAP
1538 #endif
1539
1540 log_info(cds, map)("[End of CDS archive map]");
1541 }
1542 }; // end ArchiveBuilder::CDSMapLogger
1543
1544 void ArchiveBuilder::print_stats() {
1545 _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1546 }
1547
1548 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) {
1549 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1550 // MetaspaceShared::n_regions (internal to hotspot).
1551 assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1552
1553 write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1554 write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1555 write_region(mapinfo, MetaspaceShared::ac, &_ac_region, /*read_only=*/false,/*allow_exec=*/false);
1556
1557 // Split pointer map into read-write and read-only bitmaps
1558 ArchivePtrMarker::initialize_rw_ro_maps(&_rw_ptrmap, &_ro_ptrmap);
1559
1560 size_t bitmap_size_in_bytes;
1561 char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::rw_ptrmap(), ArchivePtrMarker::ro_ptrmap(), heap_info,
1562 bitmap_size_in_bytes);
1563
1564 if (heap_info->is_used()) {
1565 _total_heap_region_size = mapinfo->write_heap_region(heap_info);
1566 }
1567
1568 print_region_stats(mapinfo, heap_info);
1569
1570 mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1571 mapinfo->set_header_crc(mapinfo->compute_header_crc());
1572 // After this point, we should not write any data into mapinfo->header() since this
1573 // would corrupt its checksum we have calculated before.
1574 mapinfo->write_header();
1575 mapinfo->close();
1576
1577 if (log_is_enabled(Info, cds)) {
1578 log_info(cds)("Full module graph = %s", CDSConfig::is_dumping_full_module_graph() ? "enabled" : "disabled");
1579 print_stats();
1580 }
1581
1582 if (log_is_enabled(Info, cds, map)) {
1583 CDSMapLogger::log(this, mapinfo, heap_info,
1584 bitmap, bitmap_size_in_bytes);
1585 }
1586 CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache());
1587 FREE_C_HEAP_ARRAY(char, bitmap);
1588 }
1589
1590 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) {
1591 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1592 }
1593
1594 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
1595 // Print statistics of all the regions
1596 const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used();
1597 const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned();
1598 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() +
1599 bitmap_reserved +
1600 _total_heap_region_size;
1601 const size_t total_bytes = _ro_region.used() + _rw_region.used() +
1602 bitmap_used +
1603 _total_heap_region_size;
1604 const double total_u_perc = percent_of(total_bytes, total_reserved);
1605
1606 _rw_region.print(total_reserved);
1607 _ro_region.print(total_reserved);
1608 _ac_region.print(total_reserved);
1609
1610 print_bitmap_region_stats(bitmap_used, total_reserved);
1611
1612 if (heap_info->is_used()) {
1613 print_heap_region_stats(heap_info, total_reserved);
|
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 #include "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassLinker.hpp"
27 #include "cds/aotLinkedClassBulkLoader.hpp"
28 #include "cds/archiveBuilder.hpp"
29 #include "cds/archiveHeapWriter.hpp"
30 #include "cds/archiveUtils.hpp"
31 #include "cds/cdsConfig.hpp"
32 #include "cds/cppVtables.hpp"
33 #include "cds/dumpAllocStats.hpp"
34 #include "cds/dynamicArchive.hpp"
35 #include "cds/finalImageRecipes.hpp"
36 #include "cds/heapShared.hpp"
37 #include "cds/metaspaceShared.hpp"
38 #include "cds/regeneratedClasses.hpp"
39 #include "classfile/classLoader.hpp"
40 #include "classfile/classLoaderDataShared.hpp"
41 #include "classfile/classLoaderExt.hpp"
42 #include "classfile/javaClasses.hpp"
43 #include "classfile/symbolTable.hpp"
44 #include "classfile/systemDictionaryShared.hpp"
45 #include "classfile/vmClasses.hpp"
46 #include "code/aotCodeCache.hpp"
47 #include "interpreter/abstractInterpreter.hpp"
48 #include "jvm.h"
49 #include "logging/log.hpp"
50 #include "logging/logStream.hpp"
51 #include "memory/allStatic.hpp"
52 #include "memory/memoryReserver.hpp"
53 #include "memory/memRegion.hpp"
54 #include "memory/resourceArea.hpp"
55 #include "oops/compressedKlass.inline.hpp"
56 #include "oops/instanceKlass.hpp"
57 #include "oops/methodCounters.hpp"
58 #include "oops/methodData.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oopHandle.inline.hpp"
62 #include "oops/trainingData.hpp"
63 #include "runtime/arguments.hpp"
64 #include "runtime/fieldDescriptor.inline.hpp"
65 #include "runtime/globals_extension.hpp"
66 #include "runtime/javaThread.hpp"
67 #include "runtime/sharedRuntime.hpp"
68 #include "utilities/align.hpp"
69 #include "utilities/bitMap.inline.hpp"
70 #include "utilities/formatBuffer.hpp"
71
72 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
73
74 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
75 char* newtop = ArchiveBuilder::current()->_ro_region.top();
76 ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
77 }
78
79 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
80 _total_bytes = 0;
81 _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
82 }
117 assert(field_offset_in_bytes >= 0, "must be");
118 assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be");
119 assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be");
120
121 BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address)));
122 _ptrmap.set_bit(BitMap::idx_t(idx));
123 }
124
125 class RelocateEmbeddedPointers : public BitMapClosure {
126 ArchiveBuilder* _builder;
127 address _buffered_obj;
128 BitMap::idx_t _start_idx;
129 public:
130 RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) :
131 _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {}
132
133 bool do_bit(BitMap::idx_t bit_offset) {
134 size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address);
135 address* ptr_loc = (address*)(_buffered_obj + field_offset);
136
137 address old_p_with_tags = *ptr_loc;
138 assert(old_p_with_tags != nullptr, "null ptrs shouldn't have been marked");
139
140 address old_p = MetaspaceClosure::strip_tags(old_p_with_tags);
141 uintx tags = MetaspaceClosure::decode_tags(old_p_with_tags);
142 address new_p = _builder->get_buffered_addr(old_p);
143
144 bool nulled;
145 if (new_p == nullptr) {
146 // old_p had a FollowMode of set_to_null
147 nulled = true;
148 } else {
149 new_p = MetaspaceClosure::add_tags(new_p, tags);
150 nulled = false;
151 }
152
153 log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT " %zu",
154 p2i(ptr_loc), p2i(old_p) + tags, p2i(new_p), tags);
155
156 ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p);
157 ArchiveBuilder::current()->count_relocated_pointer(tags != 0, nulled);
158 return true; // keep iterating the bitmap
159 }
160 };
161
162 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) {
163 SourceObjInfo* src_info = objs()->at(i);
164 assert(src_info->should_copy(), "must be");
165 BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive
166 BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive
167
168 RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
169 _ptrmap.iterate(&relocator, start, end);
170 }
171
172 ArchiveBuilder::ArchiveBuilder() :
173 _current_dump_region(nullptr),
174 _buffer_bottom(nullptr),
175 _requested_static_archive_bottom(nullptr),
176 _requested_static_archive_top(nullptr),
177 _requested_dynamic_archive_bottom(nullptr),
178 _requested_dynamic_archive_top(nullptr),
179 _mapped_static_archive_bottom(nullptr),
180 _mapped_static_archive_top(nullptr),
181 _buffer_to_requested_delta(0),
182 _pz_region("pz", MAX_SHARED_DELTA), // protection zone -- used only during dumping; does NOT exist in cds archive.
183 _rw_region("rw", MAX_SHARED_DELTA),
184 _ro_region("ro", MAX_SHARED_DELTA),
185 _ac_region("ac", MAX_SHARED_DELTA),
186 _ptrmap(mtClassShared),
187 _rw_ptrmap(mtClassShared),
188 _ro_ptrmap(mtClassShared),
189 _ac_ptrmap(mtClassShared),
190 _rw_src_objs(),
191 _ro_src_objs(),
192 _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
193 _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
194 _total_heap_region_size(0)
195 {
196 _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
197 _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
198 _entropy_seed = 0x12345678;
199 _relocated_ptr_info._num_ptrs = 0;
200 _relocated_ptr_info._num_tagged_ptrs = 0;
201 _relocated_ptr_info._num_nulled_ptrs = 0;
202 assert(_current == nullptr, "must be");
203 _current = this;
204 }
205
206 ArchiveBuilder::~ArchiveBuilder() {
207 assert(_current == this, "must be");
208 _current = nullptr;
209
210 for (int i = 0; i < _symbols->length(); i++) {
211 _symbols->at(i)->decrement_refcount();
212 }
213
214 delete _klasses;
215 delete _symbols;
216 if (_shared_rs.is_reserved()) {
217 MemoryReserver::release(_shared_rs);
218 }
219
220 AOTArtifactFinder::dispose();
221 }
441 }
442
443 FollowMode follow_mode = get_follow_mode(ref);
444 SourceObjInfo src_info(ref, read_only, follow_mode);
445 bool created;
446 SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
447 if (created) {
448 if (_src_obj_table.maybe_grow()) {
449 log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
450 }
451 }
452
453 #ifdef ASSERT
454 if (ref->msotype() == MetaspaceObj::MethodType) {
455 Method* m = (Method*)ref->obj();
456 assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
457 "Should not archive methods in a class that has been regenerated");
458 }
459 #endif
460
461 if (ref->msotype() == MetaspaceObj::MethodDataType) {
462 MethodData* md = (MethodData*)ref->obj();
463 md->clean_method_data(false /* always_clean */);
464 }
465
466 assert(p->read_only() == src_info.read_only(), "must be");
467
468 if (created && src_info.should_copy()) {
469 if (read_only) {
470 _ro_src_objs.append(p);
471 } else {
472 _rw_src_objs.append(p);
473 }
474 return true; // Need to recurse into this ref only if we are copying it
475 } else {
476 return false;
477 }
478 }
479
480 void ArchiveBuilder::record_regenerated_object(address orig_src_obj, address regen_src_obj) {
481 // Record the fact that orig_src_obj has been replaced by regen_src_obj. All calls to get_buffered_addr(orig_src_obj)
482 // should return the same value as get_buffered_addr(regen_src_obj).
483 SourceObjInfo* p = _src_obj_table.get(regen_src_obj);
484 assert(p != nullptr, "regenerated object should always be dumped");
485 SourceObjInfo orig_src_info(orig_src_obj, p);
543 return SystemDictionaryShared::is_excluded_class(ik);
544 } else if (klass->is_objArray_klass()) {
545 Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
546 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_shared_static(bottom)) {
547 // The bottom class is in the static archive so it's clearly not excluded.
548 return false;
549 } else if (bottom->is_instance_klass()) {
550 return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
551 }
552 }
553
554 return false;
555 }
556
557 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
558 address obj = ref->obj();
559 if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(obj)) {
560 // Don't dump existing shared metadata again.
561 return point_to_it;
562 } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
563 ref->msotype() == MetaspaceObj::MethodCountersType ||
564 ref->msotype() == MetaspaceObj::KlassTrainingDataType ||
565 ref->msotype() == MetaspaceObj::MethodTrainingDataType ||
566 ref->msotype() == MetaspaceObj::CompileTrainingDataType) {
567 return (TrainingData::need_data() || TrainingData::assembling_data()) ? make_a_copy : set_to_null;
568 } else if (ref->msotype() == MetaspaceObj::AdapterHandlerEntryType) {
569 if (AOTCodeCache::is_dumping_adapter()) {
570 AdapterHandlerEntry* entry = (AdapterHandlerEntry*)ref->obj();
571 return AdapterHandlerLibrary::is_abstract_method_adapter(entry) ? set_to_null : make_a_copy;
572 } else {
573 return set_to_null;
574 }
575 } else {
576 if (ref->msotype() == MetaspaceObj::ClassType) {
577 Klass* klass = (Klass*)ref->obj();
578 assert(klass->is_klass(), "must be");
579 if (is_excluded(klass)) {
580 ResourceMark rm;
581 log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
582 return set_to_null;
583 }
584 }
585
586 return make_a_copy;
587 }
588 }
589
728 // not handled by MetaspaceClosure.
729 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
730 assert(is_in_buffer_space(ptr_location), "must be");
731 if (src_addr == nullptr) {
732 *ptr_location = nullptr;
733 ArchivePtrMarker::clear_pointer(ptr_location);
734 } else {
735 *ptr_location = get_buffered_addr(src_addr);
736 ArchivePtrMarker::mark_pointer(ptr_location);
737 }
738 }
739
740 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) {
741 assert(*ptr_location != nullptr, "sanity");
742 if (!is_in_mapped_static_archive(*ptr_location)) {
743 *ptr_location = get_buffered_addr(*ptr_location);
744 }
745 ArchivePtrMarker::mark_pointer(ptr_location);
746 }
747
748 bool ArchiveBuilder::has_been_buffered(address src_addr) const {
749 if (RegeneratedClasses::has_been_regenerated(src_addr) ||
750 _src_obj_table.get(src_addr) == nullptr ||
751 get_buffered_addr(src_addr) == nullptr) {
752 return false;
753 } else {
754 return true;
755 }
756 }
757
758 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
759 SourceObjInfo* p = _src_obj_table.get(src_addr);
760 assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
761 p2i(src_addr));
762
763 return p->buffered_addr();
764 }
765
766 bool ArchiveBuilder::has_been_archived(address src_addr) const {
767 SourceObjInfo* p = _src_obj_table.get(src_addr);
768 return (p != nullptr);
769 }
770
771 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
772 assert(is_in_buffer_space(buffered_addr), "must be");
773 address* src_p = _buffered_to_src_table.get(buffered_addr);
774 assert(src_p != nullptr && *src_p != nullptr, "must be");
775 return *src_p;
776 }
777
778 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
779 for (int i = 0; i < src_objs->objs()->length(); i++) {
780 src_objs->relocate(i, this);
781 }
782 }
783
784 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
785 log_info(cds)("Relocating embedded pointers in core regions ... ");
786 relocate_embedded_pointers(&_rw_src_objs);
787 relocate_embedded_pointers(&_ro_src_objs);
788 log_info(cds)("Relocating %zu pointers, %zu tagged, %zu nulled",
789 _relocated_ptr_info._num_ptrs,
790 _relocated_ptr_info._num_tagged_ptrs,
791 _relocated_ptr_info._num_nulled_ptrs);
792 }
793
794 #define ADD_COUNT(x) \
795 x += 1; \
796 x ## _a += aotlinked ? 1 : 0; \
797 x ## _i += inited ? 1 : 0;
798
799 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \
800 int x = 0; \
801 int x ## _a = 0; \
802 int x ## _i = 0;
803
804 void ArchiveBuilder::make_klasses_shareable() {
805 DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses);
806 DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses);
807 DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses);
808 DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses);
809 DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses);
810 DECLARE_INSTANCE_KLASS_COUNTER(num_old_klasses);
811 DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses);
973 log_info(cds)(" boot " STATS_FORMAT, STATS_PARAMS(boot_klasses));
974 log_info(cds)(" vm " STATS_FORMAT, STATS_PARAMS(vm_klasses));
975 log_info(cds)(" platform " STATS_FORMAT, STATS_PARAMS(platform_klasses));
976 log_info(cds)(" app " STATS_FORMAT, STATS_PARAMS(app_klasses));
977 log_info(cds)(" unregistered " STATS_FORMAT, STATS_PARAMS(unregistered_klasses));
978 log_info(cds)(" (enum) " STATS_FORMAT, STATS_PARAMS(enum_klasses));
979 log_info(cds)(" (hidden) " STATS_FORMAT, STATS_PARAMS(hidden_klasses));
980 log_info(cds)(" (old) " STATS_FORMAT, STATS_PARAMS(old_klasses));
981 log_info(cds)(" (unlinked) = %5d, boot = %d, plat = %d, app = %d, unreg = %d",
982 num_unlinked_klasses, boot_unlinked, platform_unlinked, app_unlinked, unreg_unlinked);
983 log_info(cds)(" obj array classes = %5d", num_obj_array_klasses);
984 log_info(cds)(" type array classes = %5d", num_type_array_klasses);
985 log_info(cds)(" symbols = %5d", _symbols->length());
986
987 #undef STATS_FORMAT
988 #undef STATS_PARAMS
989
990 DynamicArchive::make_array_klasses_shareable();
991 }
992
993 void ArchiveBuilder::make_training_data_shareable() {
994 auto clean_td = [&] (address& src_obj, SourceObjInfo& info) {
995 if (!is_in_buffer_space(info.buffered_addr())) {
996 return;
997 }
998
999 if (info.msotype() == MetaspaceObj::KlassTrainingDataType ||
1000 info.msotype() == MetaspaceObj::MethodTrainingDataType ||
1001 info.msotype() == MetaspaceObj::CompileTrainingDataType) {
1002 TrainingData* buffered_td = (TrainingData*)info.buffered_addr();
1003 buffered_td->remove_unshareable_info();
1004 } else if (info.msotype() == MetaspaceObj::MethodDataType) {
1005 MethodData* buffered_mdo = (MethodData*)info.buffered_addr();
1006 buffered_mdo->remove_unshareable_info();
1007 } else if (info.msotype() == MetaspaceObj::MethodCountersType) {
1008 MethodCounters* buffered_mc = (MethodCounters*)info.buffered_addr();
1009 buffered_mc->remove_unshareable_info();
1010 }
1011 };
1012 _src_obj_table.iterate_all(clean_td);
1013 }
1014
1015 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
1016 SymbolTable::serialize_shared_table_header(soc, false);
1017 SystemDictionaryShared::serialize_dictionary_headers(soc, false);
1018 DynamicArchive::serialize_array_klasses(soc);
1019 AOTLinkedClassBulkLoader::serialize(soc, false);
1020 }
1021
1022 uintx ArchiveBuilder::buffer_to_offset(address p) const {
1023 address requested_p = to_requested(p);
1024 assert(requested_p >= _requested_static_archive_bottom, "must be");
1025 return requested_p - _requested_static_archive_bottom;
1026 }
1027
1028 uintx ArchiveBuilder::any_to_offset(address p) const {
1029 if (is_in_mapped_static_archive(p)) {
1030 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1031 return p - _mapped_static_archive_bottom;
1032 }
1033 if (!is_in_buffer_space(p)) {
1034 // p must be a "source" address
1035 p = get_buffered_addr(p);
1036 }
1037 return buffer_to_offset(p);
1038 }
1039
1040 void ArchiveBuilder::start_ac_region() {
1041 ro_region()->pack();
1042 start_dump_region(&_ac_region);
1043 }
1044
1045 void ArchiveBuilder::end_ac_region() {
1046 _ac_region.pack();
1047 }
1048
1049 address ArchiveBuilder::offset_to_buffered_address(u4 offset) const {
1050 address requested_addr = _requested_static_archive_bottom + offset;
1051 address buffered_addr = requested_addr - _buffer_to_requested_delta;
1052 assert(is_in_buffer_space(buffered_addr), "bad offset");
1053 return buffered_addr;
1054 }
1055
1056 #if INCLUDE_CDS_JAVA_HEAP
1057 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
1058 assert(CDSConfig::is_dumping_heap(), "sanity");
1059 k = get_buffered_klass(k);
1060 Klass* requested_k = to_requested(k);
1061 const int narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
1062 #ifdef ASSERT
1063 const size_t klass_alignment = MAX2(SharedSpaceObjectAlignment, (size_t)nth_bit(narrow_klass_shift));
1064 assert(is_aligned(k, klass_alignment), "Klass " PTR_FORMAT " misaligned.", p2i(k));
1065 #endif
1066 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
1067 // Note: use the "raw" version of encode that takes explicit narrow klass base and shift. Don't use any
1068 // of the variants that do sanity checks, nor any of those that use the current - dump - JVM's encoding setting.
1069 return CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
1070 }
1071 #endif // INCLUDE_CDS_JAVA_HEAP
1072
1073 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
1074 // so that the archive can be mapped to the "requested" location without runtime relocation.
1075 //
1148
1149 #ifdef _LP64
1150 int ArchiveBuilder::precomputed_narrow_klass_shift() {
1151 // Legacy Mode:
1152 // We use 32 bits for narrowKlass, which should cover the full 4G Klass range. Shift can be 0.
1153 // CompactObjectHeader Mode:
1154 // narrowKlass is much smaller, and we use the highest possible shift value to later get the maximum
1155 // Klass encoding range.
1156 //
1157 // Note that all of this may change in the future, if we decide to correct the pre-calculated
1158 // narrow Klass IDs at archive load time.
1159 assert(UseCompressedClassPointers, "Only needed for compressed class pointers");
1160 return UseCompactObjectHeaders ? CompressedKlassPointers::max_shift() : 0;
1161 }
1162 #endif // _LP64
1163
1164 void ArchiveBuilder::relocate_to_requested() {
1165 if (!ro_region()->is_packed()) {
1166 ro_region()->pack();
1167 }
1168
1169 size_t my_archive_size = buffer_top() - buffer_bottom();
1170
1171 if (CDSConfig::is_dumping_static_archive()) {
1172 _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
1173 RelocateBufferToRequested<true> patcher(this);
1174 patcher.doit();
1175 } else {
1176 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1177 _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
1178 RelocateBufferToRequested<false> patcher(this);
1179 patcher.doit();
1180 }
1181 }
1182
1183 // Write detailed info to a mapfile to analyze contents of the archive.
1184 // static dump:
1185 // java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
1186 // dynamic dump:
1187 // java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \
1188 // -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp
1473 assert(java_lang_Class::is_instance(scratch_mirror), "sanity");
1474 if (java_lang_Class::is_primitive(scratch_mirror)) {
1475 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
1476 BasicType bt = (BasicType)i;
1477 if (!is_reference_type(bt) && scratch_mirror == HeapShared::scratch_java_mirror(bt)) {
1478 oop orig_mirror = Universe::java_mirror(bt);
1479 java_lang_Class::print_signature(orig_mirror, st);
1480 return;
1481 }
1482 }
1483 ShouldNotReachHere();
1484 }
1485 java_lang_Class::print_signature(scratch_mirror, st);
1486 }
1487
1488 static void log_heap_roots() {
1489 LogStreamHandle(Trace, cds, map, oops) st;
1490 if (st.is_enabled()) {
1491 for (int i = 0; i < HeapShared::pending_roots()->length(); i++) {
1492 st.print("roots[%4d]: ", i);
1493 print_oop_info_cr(&st, HeapShared::pending_roots()->at(i).resolve());
1494 }
1495 }
1496 }
1497
1498 // Example output:
1499 // - The first number is the requested address (if print_requested_addr == true)
1500 // - The second number is the narrowOop version of the requested address (if UseCompressedOops == true)
1501 // 0x00000007ffc7e840 (0xfff8fd08) java.lang.Class Ljava/util/Array;
1502 // 0x00000007ffc000f8 (0xfff8001f) [B length: 11
1503 static void print_oop_info_cr(outputStream* st, oop source_oop, bool print_requested_addr = true) {
1504 if (source_oop == nullptr) {
1505 st->print_cr("null");
1506 } else {
1507 ResourceMark rm;
1508 oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop);
1509 if (print_requested_addr) {
1510 st->print(PTR_FORMAT " ", p2i(requested_obj));
1511 }
1512 if (UseCompressedOops) {
1513 st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj));
1514 }
1515 if (source_oop->is_array()) {
1516 int array_len = arrayOop(source_oop)->length();
1517 st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len);
1518 } else {
1519 st->print("%s", source_oop->klass()->external_name());
1520
1521 if (java_lang_String::is_instance(source_oop)) {
1522 st->print(" ");
1523 java_lang_String::print(source_oop, st);
1524 } else if (java_lang_invoke_MethodType::is_instance(source_oop)) {
1525 st->print(" ");
1526 java_lang_invoke_MethodType::print_signature(source_oop, st);
1527 } else if (java_lang_Class::is_instance(source_oop)) {
1528 oop scratch_mirror = source_oop;
1529
1530 st->print(" ");
1531 print_class_signature_for_mirror(st, scratch_mirror);
1532
1533 Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror);
1534 if (src_klass != nullptr && src_klass->is_instance_klass()) {
1535 InstanceKlass* buffered_klass =
1536 ArchiveBuilder::current()->get_buffered_addr(InstanceKlass::cast(src_klass));
1537 if (buffered_klass->has_aot_initialized_mirror()) {
1538 st->print(" (aot-inited)");
1539 }
1540 }
1541 }
1542 st->cr();
1543 }
1544 }
1545 }
1546 #endif // INCLUDE_CDS_JAVA_HEAP
1598 #endif
1599
1600 log_info(cds, map)("[End of CDS archive map]");
1601 }
1602 }; // end ArchiveBuilder::CDSMapLogger
1603
1604 void ArchiveBuilder::print_stats() {
1605 _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1606 }
1607
1608 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) {
1609 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1610 // MetaspaceShared::n_regions (internal to hotspot).
1611 assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1612
1613 write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1614 write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1615 write_region(mapinfo, MetaspaceShared::ac, &_ac_region, /*read_only=*/false,/*allow_exec=*/false);
1616
1617 // Split pointer map into read-write and read-only bitmaps
1618 ArchivePtrMarker::initialize_rw_ro_ac_maps(&_rw_ptrmap, &_ro_ptrmap, &_ac_ptrmap);
1619
1620 size_t bitmap_size_in_bytes;
1621 char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::rw_ptrmap(),
1622 ArchivePtrMarker::ro_ptrmap(),
1623 ArchivePtrMarker::ac_ptrmap(),
1624 heap_info,
1625 bitmap_size_in_bytes);
1626
1627 if (heap_info->is_used()) {
1628 _total_heap_region_size = mapinfo->write_heap_region(heap_info);
1629 }
1630
1631 print_region_stats(mapinfo, heap_info);
1632
1633 mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1634 mapinfo->set_header_crc(mapinfo->compute_header_crc());
1635 // After this point, we should not write any data into mapinfo->header() since this
1636 // would corrupt its checksum we have calculated before.
1637 mapinfo->write_header();
1638 mapinfo->close();
1639
1640 if (log_is_enabled(Info, cds)) {
1641 log_info(cds)("Full module graph = %s", CDSConfig::is_dumping_full_module_graph() ? "enabled" : "disabled");
1642 print_stats();
1643 }
1644
1645 if (log_is_enabled(Info, cds, map)) {
1646 CDSMapLogger::log(this, mapinfo, heap_info,
1647 bitmap, bitmap_size_in_bytes);
1648 }
1649 CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache());
1650 FREE_C_HEAP_ARRAY(char, bitmap);
1651 }
1652
1653 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) {
1654 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1655 }
1656
1657 void ArchiveBuilder::count_relocated_pointer(bool tagged, bool nulled) {
1658 _relocated_ptr_info._num_ptrs ++;
1659 _relocated_ptr_info._num_tagged_ptrs += tagged ? 1 : 0;
1660 _relocated_ptr_info._num_nulled_ptrs += nulled ? 1 : 0;
1661 }
1662
1663 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
1664 // Print statistics of all the regions
1665 const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used();
1666 const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned();
1667 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() +
1668 bitmap_reserved +
1669 _total_heap_region_size;
1670 const size_t total_bytes = _ro_region.used() + _rw_region.used() +
1671 bitmap_used +
1672 _total_heap_region_size;
1673 const double total_u_perc = percent_of(total_bytes, total_reserved);
1674
1675 _rw_region.print(total_reserved);
1676 _ro_region.print(total_reserved);
1677 _ac_region.print(total_reserved);
1678
1679 print_bitmap_region_stats(bitmap_used, total_reserved);
1680
1681 if (heap_info->is_used()) {
1682 print_heap_region_stats(heap_info, total_reserved);
|