1 /* 2 * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 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 "precompiled.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 "interpreter/abstractInterpreter.hpp" 46 #include "jvm.h" 47 #include "logging/log.hpp" 48 #include "logging/logStream.hpp" 49 #include "memory/allStatic.hpp" 50 #include "memory/memoryReserver.hpp" 51 #include "memory/memRegion.hpp" 52 #include "memory/resourceArea.hpp" 53 #include "oops/compressedKlass.inline.hpp" 54 #include "oops/instanceKlass.hpp" 55 #include "oops/objArrayKlass.hpp" 56 #include "oops/objArrayOop.inline.hpp" 57 #include "oops/oopHandle.inline.hpp" 58 #include "runtime/arguments.hpp" 59 #include "runtime/fieldDescriptor.inline.hpp" 60 #include "runtime/globals_extension.hpp" 61 #include "runtime/javaThread.hpp" 62 #include "runtime/sharedRuntime.hpp" 63 #include "utilities/align.hpp" 64 #include "utilities/bitMap.inline.hpp" 65 #include "utilities/formatBuffer.hpp" 66 67 ArchiveBuilder* ArchiveBuilder::_current = nullptr; 68 69 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() { 70 char* newtop = ArchiveBuilder::current()->_ro_region.top(); 71 ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true); 72 } 73 74 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) { 75 _total_bytes = 0; 76 _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared); 77 } 78 79 ArchiveBuilder::SourceObjList::~SourceObjList() { 80 delete _objs; 81 } 82 83 void ArchiveBuilder::SourceObjList::append(SourceObjInfo* src_info) { 84 // Save this source object for copying 85 src_info->set_id(_objs->length()); 86 _objs->append(src_info); 87 88 // Prepare for marking the pointers in this source object 89 assert(is_aligned(_total_bytes, sizeof(address)), "must be"); 90 src_info->set_ptrmap_start(_total_bytes / sizeof(address)); 91 _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address)); 92 src_info->set_ptrmap_end(_total_bytes / sizeof(address)); 93 94 BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end()); 95 if (_ptrmap.size() <= bitmap_size_needed) { 96 _ptrmap.resize((bitmap_size_needed + 1) * 2); 97 } 98 } 99 100 void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) { 101 // src_obj contains a pointer. Remember the location of this pointer in _ptrmap, 102 // so that we can copy/relocate it later. 103 src_info->set_has_embedded_pointer(); 104 address src_obj = src_info->source_addr(); 105 address* field_addr = ref->addr(); 106 assert(src_info->ptrmap_start() < _total_bytes, "sanity"); 107 assert(src_info->ptrmap_end() <= _total_bytes, "sanity"); 108 assert(*field_addr != nullptr, "should have checked"); 109 110 intx field_offset_in_bytes = ((address)field_addr) - src_obj; 111 DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();) 112 assert(field_offset_in_bytes >= 0, "must be"); 113 assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be"); 114 assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be"); 115 116 BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address))); 117 _ptrmap.set_bit(BitMap::idx_t(idx)); 118 } 119 120 class RelocateEmbeddedPointers : public BitMapClosure { 121 ArchiveBuilder* _builder; 122 address _buffered_obj; 123 BitMap::idx_t _start_idx; 124 public: 125 RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) : 126 _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {} 127 128 bool do_bit(BitMap::idx_t bit_offset) { 129 size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address); 130 address* ptr_loc = (address*)(_buffered_obj + field_offset); 131 132 address old_p = *ptr_loc; 133 address new_p = _builder->get_buffered_addr(old_p); 134 135 log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT, 136 p2i(ptr_loc), p2i(old_p), p2i(new_p)); 137 138 ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p); 139 return true; // keep iterating the bitmap 140 } 141 }; 142 143 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) { 144 SourceObjInfo* src_info = objs()->at(i); 145 assert(src_info->should_copy(), "must be"); 146 BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive 147 BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive 148 149 RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start); 150 _ptrmap.iterate(&relocator, start, end); 151 } 152 153 ArchiveBuilder::ArchiveBuilder() : 154 _current_dump_region(nullptr), 155 _buffer_bottom(nullptr), 156 _last_verified_top(nullptr), 157 _num_dump_regions_used(0), 158 _other_region_used_bytes(0), 159 _requested_static_archive_bottom(nullptr), 160 _requested_static_archive_top(nullptr), 161 _requested_dynamic_archive_bottom(nullptr), 162 _requested_dynamic_archive_top(nullptr), 163 _mapped_static_archive_bottom(nullptr), 164 _mapped_static_archive_top(nullptr), 165 _buffer_to_requested_delta(0), 166 _rw_region("rw", MAX_SHARED_DELTA), 167 _ro_region("ro", 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 _estimated_metaspaceobj_bytes(0), 177 _estimated_hashtable_bytes(0) 178 { 179 _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared); 180 _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared); 181 _entropy_seed = 0x12345678; 182 assert(_current == nullptr, "must be"); 183 _current = this; 184 } 185 186 ArchiveBuilder::~ArchiveBuilder() { 187 assert(_current == this, "must be"); 188 _current = nullptr; 189 190 for (int i = 0; i < _symbols->length(); i++) { 191 _symbols->at(i)->decrement_refcount(); 192 } 193 194 delete _klasses; 195 delete _symbols; 196 if (_shared_rs.is_reserved()) { 197 MemoryReserver::release(_shared_rs); 198 } 199 } 200 201 // Returns a deterministic sequence of pseudo random numbers. The main purpose is NOT 202 // for randomness but to get good entropy for the identity_hash() of archived Symbols, 203 // while keeping the contents of static CDS archives deterministic to ensure 204 // reproducibility of JDK builds. 205 int ArchiveBuilder::entropy() { 206 assert(SafepointSynchronize::is_at_safepoint(), "needed to ensure deterministic sequence"); 207 _entropy_seed = os::next_random(_entropy_seed); 208 return static_cast<int>(_entropy_seed); 209 } 210 211 class GatherKlassesAndSymbols : public UniqueMetaspaceClosure { 212 ArchiveBuilder* _builder; 213 214 public: 215 GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {} 216 217 virtual bool do_unique_ref(Ref* ref, bool read_only) { 218 return _builder->gather_klass_and_symbol(ref, read_only); 219 } 220 }; 221 222 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) { 223 if (ref->obj() == nullptr) { 224 return false; 225 } 226 if (get_follow_mode(ref) != make_a_copy) { 227 return false; 228 } 229 if (ref->msotype() == MetaspaceObj::ClassType) { 230 Klass* klass = (Klass*)ref->obj(); 231 assert(klass->is_klass(), "must be"); 232 if (!is_excluded(klass)) { 233 _klasses->append(klass); 234 if (klass->is_hidden()) { 235 assert(klass->is_instance_klass(), "must be"); 236 assert(SystemDictionaryShared::should_hidden_class_be_archived(InstanceKlass::cast(klass)), "must be"); 237 } 238 } 239 // See RunTimeClassInfo::get_for(): make sure we have enough space for both maximum 240 // Klass alignment as well as the RuntimeInfo* pointer we will embed in front of a Klass. 241 _estimated_metaspaceobj_bytes += align_up(BytesPerWord, CompressedKlassPointers::klass_alignment_in_bytes()) + 242 align_up(sizeof(void*), SharedSpaceObjectAlignment); 243 } else if (ref->msotype() == MetaspaceObj::SymbolType) { 244 // Make sure the symbol won't be GC'ed while we are dumping the archive. 245 Symbol* sym = (Symbol*)ref->obj(); 246 sym->increment_refcount(); 247 _symbols->append(sym); 248 } 249 250 int bytes = ref->size() * BytesPerWord; 251 _estimated_metaspaceobj_bytes += align_up(bytes, SharedSpaceObjectAlignment); 252 253 return true; // recurse 254 } 255 256 void ArchiveBuilder::gather_klasses_and_symbols() { 257 ResourceMark rm; 258 log_info(cds)("Gathering classes and symbols ... "); 259 GatherKlassesAndSymbols doit(this); 260 iterate_roots(&doit); 261 #if INCLUDE_CDS_JAVA_HEAP 262 if (CDSConfig::is_dumping_full_module_graph()) { 263 ClassLoaderDataShared::iterate_symbols(&doit); 264 } 265 #endif 266 doit.finish(); 267 268 if (CDSConfig::is_dumping_static_archive()) { 269 // To ensure deterministic contents in the static archive, we need to ensure that 270 // we iterate the MetaspaceObjs in a deterministic order. It doesn't matter where 271 // the MetaspaceObjs are located originally, as they are copied sequentially into 272 // the archive during the iteration. 273 // 274 // The only issue here is that the symbol table and the system directories may be 275 // randomly ordered, so we copy the symbols and klasses into two arrays and sort 276 // them deterministically. 277 // 278 // During -Xshare:dump, the order of Symbol creation is strictly determined by 279 // the SharedClassListFile (class loading is done in a single thread and the JIT 280 // is disabled). Also, Symbols are allocated in monotonically increasing addresses 281 // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by 282 // ascending address order, we ensure that all Symbols are copied into deterministic 283 // locations in the archive. 284 // 285 // TODO: in the future, if we want to produce deterministic contents in the 286 // dynamic archive, we might need to sort the symbols alphabetically (also see 287 // DynamicArchiveBuilder::sort_methods()). 288 log_info(cds)("Sorting symbols ... "); 289 _symbols->sort(compare_symbols_by_address); 290 sort_klasses(); 291 292 // TODO -- we need a proper estimate for the archived modules, etc, 293 // but this should be enough for now 294 _estimated_metaspaceobj_bytes += 200 * 1024 * 1024; 295 } 296 297 AOTClassLinker::add_candidates(); 298 } 299 300 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) { 301 if (a[0] < b[0]) { 302 return -1; 303 } else { 304 assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string()); 305 return 1; 306 } 307 } 308 309 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) { 310 return a[0]->name()->fast_compare(b[0]->name()); 311 } 312 313 void ArchiveBuilder::sort_klasses() { 314 log_info(cds)("Sorting classes ... "); 315 _klasses->sort(compare_klass_by_name); 316 } 317 318 size_t ArchiveBuilder::estimate_archive_size() { 319 // size of the symbol table and two dictionaries, plus the RunTimeClassInfo's 320 size_t symbol_table_est = SymbolTable::estimate_size_for_archive(); 321 size_t dictionary_est = SystemDictionaryShared::estimate_size_for_archive(); 322 _estimated_hashtable_bytes = symbol_table_est + dictionary_est; 323 324 if (CDSConfig::is_dumping_aot_linked_classes()) { 325 // This is difficult to estimate when dumping the dynamic archive, as the 326 // AOTLinkedClassTable may need to contain classes in the static archive as well. 327 // 328 // Just give a generous estimate for now. We will remove estimate_archive_size() 329 // in JDK-8340416 330 _estimated_hashtable_bytes += 20 * 1024 * 1024; 331 } 332 333 size_t total = 0; 334 335 total += _estimated_metaspaceobj_bytes; 336 total += _estimated_hashtable_bytes; 337 338 // allow fragmentation at the end of each dump region 339 total += _total_dump_regions * MetaspaceShared::core_region_alignment(); 340 341 log_info(cds)("_estimated_hashtable_bytes = " SIZE_FORMAT " + " SIZE_FORMAT " = " SIZE_FORMAT, 342 symbol_table_est, dictionary_est, _estimated_hashtable_bytes); 343 log_info(cds)("_estimated_metaspaceobj_bytes = " SIZE_FORMAT, _estimated_metaspaceobj_bytes); 344 log_info(cds)("total estimate bytes = " SIZE_FORMAT, total); 345 346 return align_up(total, MetaspaceShared::core_region_alignment()); 347 } 348 349 address ArchiveBuilder::reserve_buffer() { 350 size_t buffer_size = estimate_archive_size(); 351 ReservedSpace rs = MemoryReserver::reserve(buffer_size, 352 MetaspaceShared::core_region_alignment(), 353 os::vm_page_size()); 354 if (!rs.is_reserved()) { 355 log_error(cds)("Failed to reserve " SIZE_FORMAT " bytes of output buffer.", buffer_size); 356 MetaspaceShared::unrecoverable_writing_error(); 357 } 358 359 // buffer_bottom is the lowest address of the 2 core regions (rw, ro) when 360 // we are copying the class metadata into the buffer. 361 address buffer_bottom = (address)rs.base(); 362 log_info(cds)("Reserved output buffer space at " PTR_FORMAT " [" SIZE_FORMAT " bytes]", 363 p2i(buffer_bottom), buffer_size); 364 _shared_rs = rs; 365 366 _buffer_bottom = buffer_bottom; 367 _last_verified_top = buffer_bottom; 368 _current_dump_region = &_rw_region; 369 _num_dump_regions_used = 1; 370 _other_region_used_bytes = 0; 371 _current_dump_region->init(&_shared_rs, &_shared_vs); 372 373 ArchivePtrMarker::initialize(&_ptrmap, &_shared_vs); 374 375 // The bottom of the static archive should be mapped at this address by default. 376 _requested_static_archive_bottom = (address)MetaspaceShared::requested_base_address(); 377 378 // The bottom of the archive (that I am writing now) should be mapped at this address by default. 379 address my_archive_requested_bottom; 380 381 if (CDSConfig::is_dumping_static_archive()) { 382 my_archive_requested_bottom = _requested_static_archive_bottom; 383 } else { 384 _mapped_static_archive_bottom = (address)MetaspaceObj::shared_metaspace_base(); 385 _mapped_static_archive_top = (address)MetaspaceObj::shared_metaspace_top(); 386 assert(_mapped_static_archive_top >= _mapped_static_archive_bottom, "must be"); 387 size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom; 388 389 // At run time, we will mmap the dynamic archive at my_archive_requested_bottom 390 _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size; 391 my_archive_requested_bottom = align_up(_requested_static_archive_top, MetaspaceShared::core_region_alignment()); 392 393 _requested_dynamic_archive_bottom = my_archive_requested_bottom; 394 } 395 396 _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom; 397 398 address my_archive_requested_top = my_archive_requested_bottom + buffer_size; 399 if (my_archive_requested_bottom < _requested_static_archive_bottom || 400 my_archive_requested_top <= _requested_static_archive_bottom) { 401 // Size overflow. 402 log_error(cds)("my_archive_requested_bottom = " INTPTR_FORMAT, p2i(my_archive_requested_bottom)); 403 log_error(cds)("my_archive_requested_top = " INTPTR_FORMAT, p2i(my_archive_requested_top)); 404 log_error(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is too high. " 405 "Please rerun java -Xshare:dump with a lower value", p2i(_requested_static_archive_bottom)); 406 MetaspaceShared::unrecoverable_writing_error(); 407 } 408 409 if (CDSConfig::is_dumping_static_archive()) { 410 // We don't want any valid object to be at the very bottom of the archive. 411 // See ArchivePtrMarker::mark_pointer(). 412 rw_region()->allocate(16); 413 } 414 415 return buffer_bottom; 416 } 417 418 void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it) { 419 int num_symbols = _symbols->length(); 420 for (int i = 0; i < num_symbols; i++) { 421 it->push(_symbols->adr_at(i)); 422 } 423 424 int num_klasses = _klasses->length(); 425 for (int i = 0; i < num_klasses; i++) { 426 it->push(_klasses->adr_at(i)); 427 } 428 429 iterate_roots(it); 430 } 431 432 class GatherSortedSourceObjs : public MetaspaceClosure { 433 ArchiveBuilder* _builder; 434 435 public: 436 GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {} 437 438 virtual bool do_ref(Ref* ref, bool read_only) { 439 return _builder->gather_one_source_obj(ref, read_only); 440 } 441 }; 442 443 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only) { 444 address src_obj = ref->obj(); 445 if (src_obj == nullptr) { 446 return false; 447 } 448 449 remember_embedded_pointer_in_enclosing_obj(ref); 450 if (RegeneratedClasses::has_been_regenerated(src_obj)) { 451 // No need to copy it. We will later relocate it to point to the regenerated klass/method. 452 return false; 453 } 454 455 FollowMode follow_mode = get_follow_mode(ref); 456 SourceObjInfo src_info(ref, read_only, follow_mode); 457 bool created; 458 SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created); 459 if (created) { 460 if (_src_obj_table.maybe_grow()) { 461 log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size()); 462 } 463 } 464 465 #ifdef ASSERT 466 if (ref->msotype() == MetaspaceObj::MethodType) { 467 Method* m = (Method*)ref->obj(); 468 assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()), 469 "Should not archive methods in a class that has been regenerated"); 470 } 471 #endif 472 473 assert(p->read_only() == src_info.read_only(), "must be"); 474 475 if (created && src_info.should_copy()) { 476 if (read_only) { 477 _ro_src_objs.append(p); 478 } else { 479 _rw_src_objs.append(p); 480 } 481 return true; // Need to recurse into this ref only if we are copying it 482 } else { 483 return false; 484 } 485 } 486 487 void ArchiveBuilder::record_regenerated_object(address orig_src_obj, address regen_src_obj) { 488 // Record the fact that orig_src_obj has been replaced by regen_src_obj. All calls to get_buffered_addr(orig_src_obj) 489 // should return the same value as get_buffered_addr(regen_src_obj). 490 SourceObjInfo* p = _src_obj_table.get(regen_src_obj); 491 assert(p != nullptr, "regenerated object should always be dumped"); 492 SourceObjInfo orig_src_info(orig_src_obj, p); 493 bool created; 494 _src_obj_table.put_if_absent(orig_src_obj, orig_src_info, &created); 495 assert(created, "We shouldn't have archived the original copy of a regenerated object"); 496 } 497 498 // Remember that we have a pointer inside ref->enclosing_obj() that points to ref->obj() 499 void ArchiveBuilder::remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref) { 500 assert(ref->obj() != nullptr, "should have checked"); 501 502 address enclosing_obj = ref->enclosing_obj(); 503 if (enclosing_obj == nullptr) { 504 return; 505 } 506 507 // We are dealing with 3 addresses: 508 // address o = ref->obj(): We have found an object whose address is o. 509 // address* mpp = ref->mpp(): The object o is pointed to by a pointer whose address is mpp. 510 // I.e., (*mpp == o) 511 // enclosing_obj : If non-null, it is the object which has a field that points to o. 512 // mpp is the address if that field. 513 // 514 // Example: We have an array whose first element points to a Method: 515 // Method* o = 0x0000abcd; 516 // Array<Method*>* enclosing_obj = 0x00001000; 517 // enclosing_obj->at_put(0, o); 518 // 519 // We the MetaspaceClosure iterates on the very first element of this array, we have 520 // ref->obj() == 0x0000abcd (the Method) 521 // ref->mpp() == 0x00001008 (the location of the first element in the array) 522 // ref->enclosing_obj() == 0x00001000 (the Array that contains the Method) 523 // 524 // We use the above information to mark the bitmap to indicate that there's a pointer on address 0x00001008. 525 SourceObjInfo* src_info = _src_obj_table.get(enclosing_obj); 526 if (src_info == nullptr || !src_info->should_copy()) { 527 // source objects of point_to_it/set_to_null types are not copied 528 // so we don't need to remember their pointers. 529 } else { 530 if (src_info->read_only()) { 531 _ro_src_objs.remember_embedded_pointer(src_info, ref); 532 } else { 533 _rw_src_objs.remember_embedded_pointer(src_info, ref); 534 } 535 } 536 } 537 538 void ArchiveBuilder::gather_source_objs() { 539 ResourceMark rm; 540 log_info(cds)("Gathering all archivable objects ... "); 541 gather_klasses_and_symbols(); 542 GatherSortedSourceObjs doit(this); 543 iterate_sorted_roots(&doit); 544 doit.finish(); 545 } 546 547 bool ArchiveBuilder::is_excluded(Klass* klass) { 548 if (klass->is_instance_klass()) { 549 InstanceKlass* ik = InstanceKlass::cast(klass); 550 return SystemDictionaryShared::is_excluded_class(ik); 551 } else if (klass->is_objArray_klass()) { 552 Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass(); 553 if (MetaspaceShared::is_shared_static(bottom)) { 554 // The bottom class is in the static archive so it's clearly not excluded. 555 assert(CDSConfig::is_dumping_dynamic_archive(), "sanity"); 556 return false; 557 } else if (bottom->is_instance_klass()) { 558 return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom)); 559 } 560 } 561 562 return false; 563 } 564 565 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) { 566 address obj = ref->obj(); 567 if (MetaspaceShared::is_in_shared_metaspace(obj)) { 568 // Don't dump existing shared metadata again. 569 return point_to_it; 570 } else if (ref->msotype() == MetaspaceObj::MethodDataType || 571 ref->msotype() == MetaspaceObj::MethodCountersType) { 572 return set_to_null; 573 } else { 574 if (ref->msotype() == MetaspaceObj::ClassType) { 575 Klass* klass = (Klass*)ref->obj(); 576 assert(klass->is_klass(), "must be"); 577 if (is_excluded(klass)) { 578 ResourceMark rm; 579 log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name()); 580 return set_to_null; 581 } 582 } 583 584 return make_a_copy; 585 } 586 } 587 588 void ArchiveBuilder::start_dump_region(DumpRegion* next) { 589 address bottom = _last_verified_top; 590 address top = (address)(current_dump_region()->top()); 591 _other_region_used_bytes += size_t(top - bottom); 592 593 current_dump_region()->pack(next); 594 _current_dump_region = next; 595 _num_dump_regions_used ++; 596 597 _last_verified_top = (address)(current_dump_region()->top()); 598 } 599 600 void ArchiveBuilder::verify_estimate_size(size_t estimate, const char* which) { 601 address bottom = _last_verified_top; 602 address top = (address)(current_dump_region()->top()); 603 size_t used = size_t(top - bottom) + _other_region_used_bytes; 604 int diff = int(estimate) - int(used); 605 606 log_info(cds)("%s estimate = " SIZE_FORMAT " used = " SIZE_FORMAT "; diff = %d bytes", which, estimate, used, diff); 607 assert(diff >= 0, "Estimate is too small"); 608 609 _last_verified_top = top; 610 _other_region_used_bytes = 0; 611 } 612 613 char* ArchiveBuilder::ro_strdup(const char* s) { 614 char* archived_str = ro_region_alloc((int)strlen(s) + 1); 615 strcpy(archived_str, s); 616 return archived_str; 617 } 618 619 // The objects that have embedded pointers will sink 620 // towards the end of the list. This ensures we have a maximum 621 // number of leading zero bits in the relocation bitmap. 622 int ArchiveBuilder::compare_src_objs(SourceObjInfo** a, SourceObjInfo** b) { 623 if ((*a)->has_embedded_pointer() && !(*b)->has_embedded_pointer()) { 624 return 1; 625 } else if (!(*a)->has_embedded_pointer() && (*b)->has_embedded_pointer()) { 626 return -1; 627 } else { 628 // This is necessary to keep the sorting order stable. Otherwise the 629 // archive's contents may not be deterministic. 630 return (*a)->id() - (*b)->id(); 631 } 632 } 633 634 void ArchiveBuilder::sort_metadata_objs() { 635 _rw_src_objs.objs()->sort(compare_src_objs); 636 _ro_src_objs.objs()->sort(compare_src_objs); 637 } 638 639 void ArchiveBuilder::dump_rw_metadata() { 640 ResourceMark rm; 641 log_info(cds)("Allocating RW objects ... "); 642 make_shallow_copies(&_rw_region, &_rw_src_objs); 643 644 #if INCLUDE_CDS_JAVA_HEAP 645 if (CDSConfig::is_dumping_full_module_graph()) { 646 // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders 647 char* start = rw_region()->top(); 648 ClassLoaderDataShared::allocate_archived_tables(); 649 alloc_stats()->record_modules(rw_region()->top() - start, /*read_only*/false); 650 } 651 #endif 652 } 653 654 void ArchiveBuilder::dump_ro_metadata() { 655 ResourceMark rm; 656 log_info(cds)("Allocating RO objects ... "); 657 658 start_dump_region(&_ro_region); 659 make_shallow_copies(&_ro_region, &_ro_src_objs); 660 661 #if INCLUDE_CDS_JAVA_HEAP 662 if (CDSConfig::is_dumping_full_module_graph()) { 663 char* start = ro_region()->top(); 664 ClassLoaderDataShared::init_archived_tables(); 665 alloc_stats()->record_modules(ro_region()->top() - start, /*read_only*/true); 666 } 667 #endif 668 669 RegeneratedClasses::record_regenerated_objects(); 670 } 671 672 void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region, 673 const ArchiveBuilder::SourceObjList* src_objs) { 674 for (int i = 0; i < src_objs->objs()->length(); i++) { 675 make_shallow_copy(dump_region, src_objs->objs()->at(i)); 676 } 677 log_info(cds)("done (%d objects)", src_objs->objs()->length()); 678 } 679 680 void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) { 681 address src = src_info->source_addr(); 682 int bytes = src_info->size_in_bytes(); 683 char* dest; 684 char* oldtop; 685 char* newtop; 686 687 oldtop = dump_region->top(); 688 if (src_info->msotype() == MetaspaceObj::ClassType) { 689 // Allocate space for a pointer directly in front of the future InstanceKlass, so 690 // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo* 691 // without building another hashtable. See RunTimeClassInfo::get_for() 692 // in systemDictionaryShared.cpp. 693 Klass* klass = (Klass*)src; 694 if (klass->is_instance_klass()) { 695 SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass)); 696 dump_region->allocate(sizeof(address)); 697 } 698 // Allocate space for the future InstanceKlass with proper alignment 699 const size_t alignment = 700 #ifdef _LP64 701 UseCompressedClassPointers ? 702 nth_bit(ArchiveBuilder::precomputed_narrow_klass_shift()) : 703 SharedSpaceObjectAlignment; 704 #else 705 SharedSpaceObjectAlignment; 706 #endif 707 dest = dump_region->allocate(bytes, alignment); 708 } else { 709 dest = dump_region->allocate(bytes); 710 } 711 newtop = dump_region->top(); 712 713 memcpy(dest, src, bytes); 714 715 // Update the hash of buffered sorted symbols for static dump so that the symbols have deterministic contents 716 if (CDSConfig::is_dumping_static_archive() && (src_info->msotype() == MetaspaceObj::SymbolType)) { 717 Symbol* buffered_symbol = (Symbol*)dest; 718 assert(((Symbol*)src)->is_permanent(), "archived symbols must be permanent"); 719 buffered_symbol->update_identity_hash(); 720 } 721 722 { 723 bool created; 724 _buffered_to_src_table.put_if_absent((address)dest, src, &created); 725 assert(created, "must be"); 726 if (_buffered_to_src_table.maybe_grow()) { 727 log_info(cds, hashtables)("Expanded _buffered_to_src_table table to %d", _buffered_to_src_table.table_size()); 728 } 729 } 730 731 intptr_t* archived_vtable = CppVtables::get_archived_vtable(src_info->msotype(), (address)dest); 732 if (archived_vtable != nullptr) { 733 *(address*)dest = (address)archived_vtable; 734 ArchivePtrMarker::mark_pointer((address*)dest); 735 } 736 737 log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes); 738 src_info->set_buffered_addr((address)dest); 739 740 _alloc_stats.record(src_info->msotype(), int(newtop - oldtop), src_info->read_only()); 741 742 DEBUG_ONLY(_alloc_stats.verify((int)dump_region->used(), src_info->read_only())); 743 } 744 745 // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are 746 // not handled by MetaspaceClosure. 747 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) { 748 assert(is_in_buffer_space(ptr_location), "must be"); 749 if (src_addr == nullptr) { 750 *ptr_location = nullptr; 751 ArchivePtrMarker::clear_pointer(ptr_location); 752 } else { 753 *ptr_location = get_buffered_addr(src_addr); 754 ArchivePtrMarker::mark_pointer(ptr_location); 755 } 756 } 757 758 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) { 759 assert(*ptr_location != nullptr, "sanity"); 760 if (!is_in_mapped_static_archive(*ptr_location)) { 761 *ptr_location = get_buffered_addr(*ptr_location); 762 } 763 ArchivePtrMarker::mark_pointer(ptr_location); 764 } 765 766 bool ArchiveBuilder::has_been_buffered(address src_addr) const { 767 if (RegeneratedClasses::has_been_regenerated(src_addr) || 768 _src_obj_table.get(src_addr) == nullptr || 769 get_buffered_addr(src_addr) == nullptr) { 770 return false; 771 } else { 772 return true; 773 } 774 } 775 776 address ArchiveBuilder::get_buffered_addr(address src_addr) const { 777 SourceObjInfo* p = _src_obj_table.get(src_addr); 778 assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived", 779 p2i(src_addr)); 780 781 return p->buffered_addr(); 782 } 783 784 address ArchiveBuilder::get_source_addr(address buffered_addr) const { 785 assert(is_in_buffer_space(buffered_addr), "must be"); 786 address* src_p = _buffered_to_src_table.get(buffered_addr); 787 assert(src_p != nullptr && *src_p != nullptr, "must be"); 788 return *src_p; 789 } 790 791 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) { 792 for (int i = 0; i < src_objs->objs()->length(); i++) { 793 src_objs->relocate(i, this); 794 } 795 } 796 797 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() { 798 log_info(cds)("Relocating embedded pointers in core regions ... "); 799 relocate_embedded_pointers(&_rw_src_objs); 800 relocate_embedded_pointers(&_ro_src_objs); 801 } 802 803 #define ADD_COUNT(x) \ 804 x += 1; \ 805 x ## _a += aotlinked ? 1 : 0; \ 806 x ## _i += inited ? 1 : 0; 807 808 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \ 809 int x = 0; \ 810 int x ## _a = 0; \ 811 int x ## _i = 0; 812 813 void ArchiveBuilder::make_klasses_shareable() { 814 DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses); 815 DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses); 816 DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses); 817 DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses); 818 DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses); 819 DECLARE_INSTANCE_KLASS_COUNTER(num_old_klasses); 820 DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses); 821 DECLARE_INSTANCE_KLASS_COUNTER(num_enum_klasses); 822 DECLARE_INSTANCE_KLASS_COUNTER(num_unregistered_klasses); 823 int num_unlinked_klasses = 0; 824 int num_obj_array_klasses = 0; 825 int num_type_array_klasses = 0; 826 827 int boot_unlinked = 0; 828 int platform_unlinked = 0; 829 int app_unlinked = 0; 830 int unreg_unlinked = 0; 831 832 for (int i = 0; i < klasses()->length(); i++) { 833 // Some of the code in ConstantPool::remove_unshareable_info() requires the classes 834 // to be in linked state, so it must be call here before the next loop, which returns 835 // all classes to unlinked state. 836 Klass* k = get_buffered_addr(klasses()->at(i)); 837 if (k->is_instance_klass()) { 838 InstanceKlass::cast(k)->constants()->remove_unshareable_info(); 839 } 840 } 841 842 for (int i = 0; i < klasses()->length(); i++) { 843 const char* type; 844 const char* unlinked = ""; 845 const char* kind = ""; 846 const char* hidden = ""; 847 const char* old = ""; 848 const char* generated = ""; 849 const char* aotlinked_msg = ""; 850 const char* inited_msg = ""; 851 Klass* k = get_buffered_addr(klasses()->at(i)); 852 k->remove_java_mirror(); 853 #ifdef _LP64 854 if (UseCompactObjectHeaders) { 855 Klass* requested_k = to_requested(k); 856 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start 857 const int narrow_klass_shift = precomputed_narrow_klass_shift(); 858 narrowKlass nk = CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift); 859 k->set_prototype_header(markWord::prototype().set_narrow_klass(nk)); 860 } 861 #endif //_LP64 862 if (k->is_objArray_klass()) { 863 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info 864 // on their array classes. 865 num_obj_array_klasses ++; 866 type = "array"; 867 } else if (k->is_typeArray_klass()) { 868 num_type_array_klasses ++; 869 type = "array"; 870 k->remove_unshareable_info(); 871 } else { 872 assert(k->is_instance_klass(), " must be"); 873 InstanceKlass* ik = InstanceKlass::cast(k); 874 InstanceKlass* src_ik = get_source_addr(ik); 875 bool aotlinked = AOTClassLinker::is_candidate(src_ik); 876 bool inited = ik->has_aot_initialized_mirror(); 877 ADD_COUNT(num_instance_klasses); 878 if (CDSConfig::is_dumping_dynamic_archive()) { 879 // For static dump, class loader type are already set. 880 ik->assign_class_loader_type(); 881 } 882 if (ik->is_hidden()) { 883 ADD_COUNT(num_hidden_klasses); 884 hidden = " hidden"; 885 oop loader = k->class_loader(); 886 if (loader == nullptr) { 887 type = "boot"; 888 ADD_COUNT(num_boot_klasses); 889 } else if (loader == SystemDictionary::java_platform_loader()) { 890 type = "plat"; 891 ADD_COUNT(num_platform_klasses); 892 } else if (loader == SystemDictionary::java_system_loader()) { 893 type = "app"; 894 ADD_COUNT(num_app_klasses); 895 } else { 896 type = "bad"; 897 assert(0, "shouldn't happen"); 898 } 899 if (CDSConfig::is_dumping_invokedynamic()) { 900 assert(HeapShared::is_archivable_hidden_klass(ik), "sanity"); 901 } else { 902 // Legacy CDS support for lambda proxies 903 CDS_JAVA_HEAP_ONLY(assert(HeapShared::is_lambda_proxy_klass(ik), "sanity");) 904 } 905 } else if (ik->is_shared_boot_class()) { 906 type = "boot"; 907 ADD_COUNT(num_boot_klasses); 908 } else if (ik->is_shared_platform_class()) { 909 type = "plat"; 910 ADD_COUNT(num_platform_klasses); 911 } else if (ik->is_shared_app_class()) { 912 type = "app"; 913 ADD_COUNT(num_app_klasses); 914 } else { 915 assert(ik->is_shared_unregistered_class(), "must be"); 916 type = "unreg"; 917 ADD_COUNT(num_unregistered_klasses); 918 } 919 920 if (AOTClassLinker::is_vm_class(src_ik)) { 921 ADD_COUNT(num_vm_klasses); 922 } 923 924 if (!ik->is_linked()) { 925 num_unlinked_klasses ++; 926 unlinked = " unlinked"; 927 if (ik->is_shared_boot_class()) { 928 boot_unlinked ++; 929 } else if (ik->is_shared_platform_class()) { 930 platform_unlinked ++; 931 } else if (ik->is_shared_app_class()) { 932 app_unlinked ++; 933 } else { 934 unreg_unlinked ++; 935 } 936 } 937 938 if (ik->is_interface()) { 939 kind = " interface"; 940 } else if (src_ik->is_enum_subclass()) { 941 kind = " enum"; 942 ADD_COUNT(num_enum_klasses); 943 } 944 945 if (!ik->can_be_verified_at_dumptime()) { 946 ADD_COUNT(num_old_klasses); 947 old = " old"; 948 } 949 950 if (ik->is_generated_shared_class()) { 951 generated = " generated"; 952 } 953 if (aotlinked) { 954 aotlinked_msg = " aot-linked"; 955 } 956 if (inited) { 957 inited_msg = " inited"; 958 } 959 960 MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik); 961 ik->remove_unshareable_info(); 962 } 963 964 if (log_is_enabled(Debug, cds, class)) { 965 ResourceMark rm; 966 log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s%s%s%s%s", i, 967 p2i(to_requested(k)), type, k->external_name(), 968 kind, hidden, old, unlinked, generated, aotlinked_msg, inited_msg); 969 } 970 } 971 972 #define STATS_FORMAT "= %5d, aot-linked = %5d, inited = %5d" 973 #define STATS_PARAMS(x) num_ ## x, num_ ## x ## _a, num_ ## x ## _i 974 975 log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses); 976 log_info(cds)(" instance classes " STATS_FORMAT, STATS_PARAMS(instance_klasses)); 977 log_info(cds)(" boot " STATS_FORMAT, STATS_PARAMS(boot_klasses)); 978 log_info(cds)(" vm " STATS_FORMAT, STATS_PARAMS(vm_klasses)); 979 log_info(cds)(" platform " STATS_FORMAT, STATS_PARAMS(platform_klasses)); 980 log_info(cds)(" app " STATS_FORMAT, STATS_PARAMS(app_klasses)); 981 log_info(cds)(" unregistered " STATS_FORMAT, STATS_PARAMS(unregistered_klasses)); 982 log_info(cds)(" (enum) " STATS_FORMAT, STATS_PARAMS(enum_klasses)); 983 log_info(cds)(" (hidden) " STATS_FORMAT, STATS_PARAMS(hidden_klasses)); 984 log_info(cds)(" (old) " STATS_FORMAT, STATS_PARAMS(old_klasses)); 985 log_info(cds)(" (unlinked) = %5d, boot = %d, plat = %d, app = %d, unreg = %d", 986 num_unlinked_klasses, boot_unlinked, platform_unlinked, app_unlinked, unreg_unlinked); 987 log_info(cds)(" obj array classes = %5d", num_obj_array_klasses); 988 log_info(cds)(" type array classes = %5d", num_type_array_klasses); 989 log_info(cds)(" symbols = %5d", _symbols->length()); 990 991 #undef STATS_FORMAT 992 #undef STATS_PARAMS 993 994 DynamicArchive::make_array_klasses_shareable(); 995 } 996 997 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) { 998 SymbolTable::serialize_shared_table_header(soc, false); 999 SystemDictionaryShared::serialize_dictionary_headers(soc, false); 1000 DynamicArchive::serialize_array_klasses(soc); 1001 AOTLinkedClassBulkLoader::serialize(soc, false); 1002 } 1003 1004 uintx ArchiveBuilder::buffer_to_offset(address p) const { 1005 address requested_p = to_requested(p); 1006 assert(requested_p >= _requested_static_archive_bottom, "must be"); 1007 return requested_p - _requested_static_archive_bottom; 1008 } 1009 1010 uintx ArchiveBuilder::any_to_offset(address p) const { 1011 if (is_in_mapped_static_archive(p)) { 1012 assert(CDSConfig::is_dumping_dynamic_archive(), "must be"); 1013 return p - _mapped_static_archive_bottom; 1014 } 1015 if (!is_in_buffer_space(p)) { 1016 // p must be a "source" address 1017 p = get_buffered_addr(p); 1018 } 1019 return buffer_to_offset(p); 1020 } 1021 1022 address ArchiveBuilder::offset_to_buffered_address(u4 offset) const { 1023 address requested_addr = _requested_static_archive_bottom + offset; 1024 address buffered_addr = requested_addr - _buffer_to_requested_delta; 1025 assert(is_in_buffer_space(buffered_addr), "bad offset"); 1026 return buffered_addr; 1027 } 1028 1029 #if INCLUDE_CDS_JAVA_HEAP 1030 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) { 1031 assert(CDSConfig::is_dumping_heap(), "sanity"); 1032 k = get_buffered_klass(k); 1033 Klass* requested_k = to_requested(k); 1034 const int narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift(); 1035 #ifdef ASSERT 1036 const size_t klass_alignment = MAX2(SharedSpaceObjectAlignment, (size_t)nth_bit(narrow_klass_shift)); 1037 assert(is_aligned(k, klass_alignment), "Klass " PTR_FORMAT " misaligned.", p2i(k)); 1038 #endif 1039 address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start 1040 // Note: use the "raw" version of encode that takes explicit narrow klass base and shift. Don't use any 1041 // of the variants that do sanity checks, nor any of those that use the current - dump - JVM's encoding setting. 1042 return CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift); 1043 } 1044 #endif // INCLUDE_CDS_JAVA_HEAP 1045 1046 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro, 1047 // so that the archive can be mapped to the "requested" location without runtime relocation. 1048 // 1049 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested" 1050 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions 1051 // - Every pointer must have one of the following values: 1052 // [a] nullptr: 1053 // No relocation is needed. Remove this pointer from ptrmap so we don't need to 1054 // consider it at runtime. 1055 // [b] Points into an object X which is inside the buffer: 1056 // Adjust this pointer by _buffer_to_requested_delta, so it points to X 1057 // when the archive is mapped at the requested location. 1058 // [c] Points into an object Y which is inside mapped static archive: 1059 // - This happens only during dynamic dump 1060 // - Adjust this pointer by _mapped_to_requested_static_archive_delta, 1061 // so it points to Y when the static archive is mapped at the requested location. 1062 template <bool STATIC_DUMP> 1063 class RelocateBufferToRequested : public BitMapClosure { 1064 ArchiveBuilder* _builder; 1065 address _buffer_bottom; 1066 intx _buffer_to_requested_delta; 1067 intx _mapped_to_requested_static_archive_delta; 1068 size_t _max_non_null_offset; 1069 1070 public: 1071 RelocateBufferToRequested(ArchiveBuilder* builder) { 1072 _builder = builder; 1073 _buffer_bottom = _builder->buffer_bottom(); 1074 _buffer_to_requested_delta = builder->buffer_to_requested_delta(); 1075 _mapped_to_requested_static_archive_delta = builder->requested_static_archive_bottom() - builder->mapped_static_archive_bottom(); 1076 _max_non_null_offset = 0; 1077 1078 address bottom = _builder->buffer_bottom(); 1079 address top = _builder->buffer_top(); 1080 address new_bottom = bottom + _buffer_to_requested_delta; 1081 address new_top = top + _buffer_to_requested_delta; 1082 log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to " 1083 "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]", 1084 p2i(bottom), p2i(top), 1085 p2i(new_bottom), p2i(new_top)); 1086 } 1087 1088 bool do_bit(size_t offset) { 1089 address* p = (address*)_buffer_bottom + offset; 1090 assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space"); 1091 1092 if (*p == nullptr) { 1093 // todo -- clear bit, etc 1094 ArchivePtrMarker::ptrmap()->clear_bit(offset); 1095 } else { 1096 if (STATIC_DUMP) { 1097 assert(_builder->is_in_buffer_space(*p), "old pointer must point inside buffer space"); 1098 *p += _buffer_to_requested_delta; 1099 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive"); 1100 } else { 1101 if (_builder->is_in_buffer_space(*p)) { 1102 *p += _buffer_to_requested_delta; 1103 // assert is in requested dynamic archive 1104 } else { 1105 assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive"); 1106 *p += _mapped_to_requested_static_archive_delta; 1107 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive"); 1108 } 1109 } 1110 _max_non_null_offset = offset; 1111 } 1112 1113 return true; // keep iterating 1114 } 1115 1116 void doit() { 1117 ArchivePtrMarker::ptrmap()->iterate(this); 1118 ArchivePtrMarker::compact(_max_non_null_offset); 1119 } 1120 }; 1121 1122 #ifdef _LP64 1123 int ArchiveBuilder::precomputed_narrow_klass_shift() { 1124 // Legacy Mode: 1125 // We use 32 bits for narrowKlass, which should cover the full 4G Klass range. Shift can be 0. 1126 // CompactObjectHeader Mode: 1127 // narrowKlass is much smaller, and we use the highest possible shift value to later get the maximum 1128 // Klass encoding range. 1129 // 1130 // Note that all of this may change in the future, if we decide to correct the pre-calculated 1131 // narrow Klass IDs at archive load time. 1132 assert(UseCompressedClassPointers, "Only needed for compressed class pointers"); 1133 return UseCompactObjectHeaders ? CompressedKlassPointers::max_shift() : 0; 1134 } 1135 #endif // _LP64 1136 1137 void ArchiveBuilder::relocate_to_requested() { 1138 ro_region()->pack(); 1139 1140 size_t my_archive_size = buffer_top() - buffer_bottom(); 1141 1142 if (CDSConfig::is_dumping_static_archive()) { 1143 _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size; 1144 RelocateBufferToRequested<true> patcher(this); 1145 patcher.doit(); 1146 } else { 1147 assert(CDSConfig::is_dumping_dynamic_archive(), "must be"); 1148 _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size; 1149 RelocateBufferToRequested<false> patcher(this); 1150 patcher.doit(); 1151 } 1152 } 1153 1154 // Write detailed info to a mapfile to analyze contents of the archive. 1155 // static dump: 1156 // java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0 1157 // dynamic dump: 1158 // java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \ 1159 // -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp 1160 // 1161 // We need to do some address translation because the buffers used at dump time may be mapped to 1162 // a different location at runtime. At dump time, the buffers may be at arbitrary locations 1163 // picked by the OS. At runtime, we try to map at a fixed location (SharedBaseAddress). For 1164 // consistency, we log everything using runtime addresses. 1165 class ArchiveBuilder::CDSMapLogger : AllStatic { 1166 static intx buffer_to_runtime_delta() { 1167 // Translate the buffers used by the RW/RO regions to their eventual (requested) locations 1168 // at runtime. 1169 return ArchiveBuilder::current()->buffer_to_requested_delta(); 1170 } 1171 1172 // rw/ro regions only 1173 static void log_metaspace_region(const char* name, DumpRegion* region, 1174 const ArchiveBuilder::SourceObjList* src_objs) { 1175 address region_base = address(region->base()); 1176 address region_top = address(region->top()); 1177 log_region(name, region_base, region_top, region_base + buffer_to_runtime_delta()); 1178 log_metaspace_objects(region, src_objs); 1179 } 1180 1181 #define _LOG_PREFIX PTR_FORMAT ": @@ %-17s %d" 1182 1183 static void log_klass(Klass* k, address runtime_dest, const char* type_name, int bytes, Thread* current) { 1184 ResourceMark rm(current); 1185 log_debug(cds, map)(_LOG_PREFIX " %s", 1186 p2i(runtime_dest), type_name, bytes, k->external_name()); 1187 } 1188 static void log_method(Method* m, address runtime_dest, const char* type_name, int bytes, Thread* current) { 1189 ResourceMark rm(current); 1190 log_debug(cds, map)(_LOG_PREFIX " %s", 1191 p2i(runtime_dest), type_name, bytes, m->external_name()); 1192 } 1193 1194 // rw/ro regions only 1195 static void log_metaspace_objects(DumpRegion* region, const ArchiveBuilder::SourceObjList* src_objs) { 1196 address last_obj_base = address(region->base()); 1197 address last_obj_end = address(region->base()); 1198 address region_end = address(region->end()); 1199 Thread* current = Thread::current(); 1200 for (int i = 0; i < src_objs->objs()->length(); i++) { 1201 SourceObjInfo* src_info = src_objs->at(i); 1202 address src = src_info->source_addr(); 1203 address dest = src_info->buffered_addr(); 1204 log_as_hex(last_obj_base, dest, last_obj_base + buffer_to_runtime_delta()); 1205 address runtime_dest = dest + buffer_to_runtime_delta(); 1206 int bytes = src_info->size_in_bytes(); 1207 1208 MetaspaceObj::Type type = src_info->msotype(); 1209 const char* type_name = MetaspaceObj::type_name(type); 1210 1211 switch (type) { 1212 case MetaspaceObj::ClassType: 1213 log_klass((Klass*)src, runtime_dest, type_name, bytes, current); 1214 break; 1215 case MetaspaceObj::ConstantPoolType: 1216 log_klass(((ConstantPool*)src)->pool_holder(), 1217 runtime_dest, type_name, bytes, current); 1218 break; 1219 case MetaspaceObj::ConstantPoolCacheType: 1220 log_klass(((ConstantPoolCache*)src)->constant_pool()->pool_holder(), 1221 runtime_dest, type_name, bytes, current); 1222 break; 1223 case MetaspaceObj::MethodType: 1224 log_method((Method*)src, runtime_dest, type_name, bytes, current); 1225 break; 1226 case MetaspaceObj::ConstMethodType: 1227 log_method(((ConstMethod*)src)->method(), runtime_dest, type_name, bytes, current); 1228 break; 1229 case MetaspaceObj::SymbolType: 1230 { 1231 ResourceMark rm(current); 1232 Symbol* s = (Symbol*)src; 1233 log_debug(cds, map)(_LOG_PREFIX " %s", p2i(runtime_dest), type_name, bytes, 1234 s->as_quoted_ascii()); 1235 } 1236 break; 1237 default: 1238 log_debug(cds, map)(_LOG_PREFIX, p2i(runtime_dest), type_name, bytes); 1239 break; 1240 } 1241 1242 last_obj_base = dest; 1243 last_obj_end = dest + bytes; 1244 } 1245 1246 log_as_hex(last_obj_base, last_obj_end, last_obj_base + buffer_to_runtime_delta()); 1247 if (last_obj_end < region_end) { 1248 log_debug(cds, map)(PTR_FORMAT ": @@ Misc data " SIZE_FORMAT " bytes", 1249 p2i(last_obj_end + buffer_to_runtime_delta()), 1250 size_t(region_end - last_obj_end)); 1251 log_as_hex(last_obj_end, region_end, last_obj_end + buffer_to_runtime_delta()); 1252 } 1253 } 1254 1255 #undef _LOG_PREFIX 1256 1257 // Log information about a region, whose address at dump time is [base .. top). At 1258 // runtime, this region will be mapped to requested_base. requested_base is 0 if this 1259 // region will be mapped at os-selected addresses (such as the bitmap region), or will 1260 // be accessed with os::read (the header). 1261 // 1262 // Note: across -Xshare:dump runs, base may be different, but requested_base should 1263 // be the same as the archive contents should be deterministic. 1264 static void log_region(const char* name, address base, address top, address requested_base) { 1265 size_t size = top - base; 1266 base = requested_base; 1267 top = requested_base + size; 1268 log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " " SIZE_FORMAT_W(9) " bytes]", 1269 name, p2i(base), p2i(top), size); 1270 } 1271 1272 #if INCLUDE_CDS_JAVA_HEAP 1273 static void log_heap_region(ArchiveHeapInfo* heap_info) { 1274 MemRegion r = heap_info->buffer_region(); 1275 address start = address(r.start()); // start of the current oop inside the buffer 1276 address end = address(r.end()); 1277 log_region("heap", start, end, ArchiveHeapWriter::buffered_addr_to_requested_addr(start)); 1278 1279 LogStreamHandle(Info, cds, map) st; 1280 1281 HeapRootSegments segments = heap_info->heap_root_segments(); 1282 assert(segments.base_offset() == 0, "Sanity"); 1283 1284 for (size_t seg_idx = 0; seg_idx < segments.count(); seg_idx++) { 1285 address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start); 1286 st.print_cr(PTR_FORMAT ": Heap roots segment [%d]", 1287 p2i(requested_start), segments.size_in_elems(seg_idx)); 1288 start += segments.size_in_bytes(seg_idx); 1289 } 1290 log_heap_roots(); 1291 1292 while (start < end) { 1293 size_t byte_size; 1294 oop source_oop = ArchiveHeapWriter::buffered_addr_to_source_obj(start); 1295 address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start); 1296 st.print(PTR_FORMAT ": @@ Object ", p2i(requested_start)); 1297 1298 if (source_oop != nullptr) { 1299 // This is a regular oop that got archived. 1300 // Don't print the requested addr again as we have just printed it at the beginning of the line. 1301 // Example: 1302 // 0x00000007ffd27938: @@ Object (0xfffa4f27) java.util.HashMap 1303 print_oop_info_cr(&st, source_oop, /*print_requested_addr=*/false); 1304 byte_size = source_oop->size() * BytesPerWord; 1305 } else if ((byte_size = ArchiveHeapWriter::get_filler_size_at(start)) > 0) { 1306 // We have a filler oop, which also does not exist in BufferOffsetToSourceObjectTable. 1307 // Example: 1308 // 0x00000007ffc3ffd8: @@ Object filler 40 bytes 1309 st.print_cr("filler " SIZE_FORMAT " bytes", byte_size); 1310 } else { 1311 ShouldNotReachHere(); 1312 } 1313 1314 address oop_end = start + byte_size; 1315 log_as_hex(start, oop_end, requested_start, /*is_heap=*/true); 1316 1317 if (source_oop != nullptr) { 1318 log_oop_details(heap_info, source_oop, /*buffered_addr=*/start); 1319 } 1320 start = oop_end; 1321 } 1322 } 1323 1324 // ArchivedFieldPrinter is used to print the fields of archived objects. We can't 1325 // use _source_obj->print_on(), because we want to print the oop fields 1326 // in _source_obj with their requested addresses using print_oop_info_cr(). 1327 class ArchivedFieldPrinter : public FieldClosure { 1328 ArchiveHeapInfo* _heap_info; 1329 outputStream* _st; 1330 oop _source_obj; 1331 address _buffered_addr; 1332 public: 1333 ArchivedFieldPrinter(ArchiveHeapInfo* heap_info, outputStream* st, oop src_obj, address buffered_addr) : 1334 _heap_info(heap_info), _st(st), _source_obj(src_obj), _buffered_addr(buffered_addr) {} 1335 1336 void do_field(fieldDescriptor* fd) { 1337 _st->print(" - "); 1338 BasicType ft = fd->field_type(); 1339 switch (ft) { 1340 case T_ARRAY: 1341 case T_OBJECT: 1342 { 1343 fd->print_on(_st); // print just the name and offset 1344 oop obj = _source_obj->obj_field(fd->offset()); 1345 if (java_lang_Class::is_instance(obj)) { 1346 obj = HeapShared::scratch_java_mirror(obj); 1347 } 1348 print_oop_info_cr(_st, obj); 1349 } 1350 break; 1351 default: 1352 if (ArchiveHeapWriter::is_marked_as_native_pointer(_heap_info, _source_obj, fd->offset())) { 1353 print_as_native_pointer(fd); 1354 } else { 1355 fd->print_on_for(_st, cast_to_oop(_buffered_addr)); // name, offset, value 1356 _st->cr(); 1357 } 1358 } 1359 } 1360 1361 void print_as_native_pointer(fieldDescriptor* fd) { 1362 LP64_ONLY(assert(fd->field_type() == T_LONG, "must be")); 1363 NOT_LP64 (assert(fd->field_type() == T_INT, "must be")); 1364 1365 // We have a field that looks like an integer, but it's actually a pointer to a MetaspaceObj. 1366 address source_native_ptr = (address) 1367 LP64_ONLY(_source_obj->long_field(fd->offset())) 1368 NOT_LP64( _source_obj->int_field (fd->offset())); 1369 ArchiveBuilder* builder = ArchiveBuilder::current(); 1370 1371 // The value of the native pointer at runtime. 1372 address requested_native_ptr = builder->to_requested(builder->get_buffered_addr(source_native_ptr)); 1373 1374 // The address of _source_obj at runtime 1375 oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(_source_obj); 1376 // The address of this field in the requested space 1377 assert(requested_obj != nullptr, "Attempting to load field from null oop"); 1378 address requested_field_addr = cast_from_oop<address>(requested_obj) + fd->offset(); 1379 1380 fd->print_on(_st); 1381 _st->print_cr(PTR_FORMAT " (marked metadata pointer @" PTR_FORMAT " )", 1382 p2i(requested_native_ptr), p2i(requested_field_addr)); 1383 } 1384 }; 1385 1386 // Print the fields of instanceOops, or the elements of arrayOops 1387 static void log_oop_details(ArchiveHeapInfo* heap_info, oop source_oop, address buffered_addr) { 1388 LogStreamHandle(Trace, cds, map, oops) st; 1389 if (st.is_enabled()) { 1390 Klass* source_klass = source_oop->klass(); 1391 ArchiveBuilder* builder = ArchiveBuilder::current(); 1392 Klass* requested_klass = builder->to_requested(builder->get_buffered_addr(source_klass)); 1393 1394 st.print(" - klass: "); 1395 source_klass->print_value_on(&st); 1396 st.print(" " PTR_FORMAT, p2i(requested_klass)); 1397 st.cr(); 1398 1399 if (source_oop->is_typeArray()) { 1400 TypeArrayKlass::cast(source_klass)->oop_print_elements_on(typeArrayOop(source_oop), &st); 1401 } else if (source_oop->is_objArray()) { 1402 objArrayOop source_obj_array = objArrayOop(source_oop); 1403 for (int i = 0; i < source_obj_array->length(); i++) { 1404 st.print(" -%4d: ", i); 1405 oop obj = source_obj_array->obj_at(i); 1406 if (java_lang_Class::is_instance(obj)) { 1407 obj = HeapShared::scratch_java_mirror(obj); 1408 } 1409 print_oop_info_cr(&st, obj); 1410 } 1411 } else { 1412 st.print_cr(" - fields (" SIZE_FORMAT " words):", source_oop->size()); 1413 ArchivedFieldPrinter print_field(heap_info, &st, source_oop, buffered_addr); 1414 InstanceKlass::cast(source_klass)->print_nonstatic_fields(&print_field); 1415 1416 if (java_lang_Class::is_instance(source_oop)) { 1417 oop scratch_mirror = source_oop; 1418 st.print(" - signature: "); 1419 print_class_signature_for_mirror(&st, scratch_mirror); 1420 st.cr(); 1421 1422 Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror); 1423 if (src_klass != nullptr && src_klass->is_instance_klass()) { 1424 oop rr = HeapShared::scratch_resolved_references(InstanceKlass::cast(src_klass)->constants()); 1425 st.print(" - archived_resolved_references: "); 1426 print_oop_info_cr(&st, rr); 1427 1428 // We need to print the fields in the scratch_mirror, not the original mirror. 1429 // (if a class is not aot-initialized, static fields in its scratch mirror will be cleared). 1430 assert(scratch_mirror == HeapShared::scratch_java_mirror(src_klass->java_mirror()), "sanity"); 1431 st.print_cr("- ---- static fields (%d):", java_lang_Class::static_oop_field_count(scratch_mirror)); 1432 InstanceKlass::cast(src_klass)->do_local_static_fields(&print_field); 1433 } 1434 } 1435 } 1436 } 1437 } 1438 1439 static void print_class_signature_for_mirror(outputStream* st, oop scratch_mirror) { 1440 assert(java_lang_Class::is_instance(scratch_mirror), "sanity"); 1441 if (java_lang_Class::is_primitive(scratch_mirror)) { 1442 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 1443 BasicType bt = (BasicType)i; 1444 if (!is_reference_type(bt) && scratch_mirror == HeapShared::scratch_java_mirror(bt)) { 1445 oop orig_mirror = Universe::java_mirror(bt); 1446 java_lang_Class::print_signature(orig_mirror, st); 1447 return; 1448 } 1449 } 1450 ShouldNotReachHere(); 1451 } 1452 java_lang_Class::print_signature(scratch_mirror, st); 1453 } 1454 1455 static void log_heap_roots() { 1456 LogStreamHandle(Trace, cds, map, oops) st; 1457 if (st.is_enabled()) { 1458 for (int i = 0; i < HeapShared::pending_roots()->length(); i++) { 1459 st.print("roots[%4d]: ", i); 1460 print_oop_info_cr(&st, HeapShared::pending_roots()->at(i)); 1461 } 1462 } 1463 } 1464 1465 // Example output: 1466 // - The first number is the requested address (if print_requested_addr == true) 1467 // - The second number is the narrowOop version of the requested address (if UseCompressedOops == true) 1468 // 0x00000007ffc7e840 (0xfff8fd08) java.lang.Class Ljava/util/Array; 1469 // 0x00000007ffc000f8 (0xfff8001f) [B length: 11 1470 static void print_oop_info_cr(outputStream* st, oop source_oop, bool print_requested_addr = true) { 1471 if (source_oop == nullptr) { 1472 st->print_cr("null"); 1473 } else { 1474 ResourceMark rm; 1475 oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop); 1476 if (print_requested_addr) { 1477 st->print(PTR_FORMAT " ", p2i(requested_obj)); 1478 } 1479 if (UseCompressedOops) { 1480 st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj)); 1481 } 1482 if (source_oop->is_array()) { 1483 int array_len = arrayOop(source_oop)->length(); 1484 st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len); 1485 } else { 1486 st->print("%s", source_oop->klass()->external_name()); 1487 1488 if (java_lang_String::is_instance(source_oop)) { 1489 st->print(" "); 1490 java_lang_String::print(source_oop, st); 1491 } else if (java_lang_Class::is_instance(source_oop)) { 1492 oop scratch_mirror = source_oop; 1493 1494 st->print(" "); 1495 print_class_signature_for_mirror(st, scratch_mirror); 1496 1497 Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror); 1498 if (src_klass != nullptr && src_klass->is_instance_klass()) { 1499 InstanceKlass* buffered_klass = 1500 ArchiveBuilder::current()->get_buffered_addr(InstanceKlass::cast(src_klass)); 1501 if (buffered_klass->has_aot_initialized_mirror()) { 1502 st->print(" (aot-inited)"); 1503 } 1504 } 1505 } 1506 st->cr(); 1507 } 1508 } 1509 } 1510 #endif // INCLUDE_CDS_JAVA_HEAP 1511 1512 // Log all the data [base...top). Pretend that the base address 1513 // will be mapped to requested_base at run-time. 1514 static void log_as_hex(address base, address top, address requested_base, bool is_heap = false) { 1515 assert(top >= base, "must be"); 1516 1517 LogStreamHandle(Trace, cds, map) lsh; 1518 if (lsh.is_enabled()) { 1519 int unitsize = sizeof(address); 1520 if (is_heap && UseCompressedOops) { 1521 // This makes the compressed oop pointers easier to read, but 1522 // longs and doubles will be split into two words. 1523 unitsize = sizeof(narrowOop); 1524 } 1525 os::print_hex_dump(&lsh, base, top, unitsize, /* print_ascii=*/true, /* bytes_per_line=*/32, requested_base); 1526 } 1527 } 1528 1529 static void log_header(FileMapInfo* mapinfo) { 1530 LogStreamHandle(Info, cds, map) lsh; 1531 if (lsh.is_enabled()) { 1532 mapinfo->print(&lsh); 1533 } 1534 } 1535 1536 public: 1537 static void log(ArchiveBuilder* builder, FileMapInfo* mapinfo, 1538 ArchiveHeapInfo* heap_info, 1539 char* bitmap, size_t bitmap_size_in_bytes) { 1540 log_info(cds, map)("%s CDS archive map for %s", CDSConfig::is_dumping_static_archive() ? "Static" : "Dynamic", mapinfo->full_path()); 1541 1542 address header = address(mapinfo->header()); 1543 address header_end = header + mapinfo->header()->header_size(); 1544 log_region("header", header, header_end, nullptr); 1545 log_header(mapinfo); 1546 log_as_hex(header, header_end, nullptr); 1547 1548 DumpRegion* rw_region = &builder->_rw_region; 1549 DumpRegion* ro_region = &builder->_ro_region; 1550 1551 log_metaspace_region("rw region", rw_region, &builder->_rw_src_objs); 1552 log_metaspace_region("ro region", ro_region, &builder->_ro_src_objs); 1553 1554 address bitmap_end = address(bitmap + bitmap_size_in_bytes); 1555 log_region("bitmap", address(bitmap), bitmap_end, nullptr); 1556 log_as_hex((address)bitmap, bitmap_end, nullptr); 1557 1558 #if INCLUDE_CDS_JAVA_HEAP 1559 if (heap_info->is_used()) { 1560 log_heap_region(heap_info); 1561 } 1562 #endif 1563 1564 log_info(cds, map)("[End of CDS archive map]"); 1565 } 1566 }; // end ArchiveBuilder::CDSMapLogger 1567 1568 void ArchiveBuilder::print_stats() { 1569 _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used())); 1570 } 1571 1572 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) { 1573 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with 1574 // MetaspaceShared::n_regions (internal to hotspot). 1575 assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity"); 1576 1577 write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false); 1578 write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false); 1579 1580 // Split pointer map into read-write and read-only bitmaps 1581 ArchivePtrMarker::initialize_rw_ro_maps(&_rw_ptrmap, &_ro_ptrmap); 1582 1583 size_t bitmap_size_in_bytes; 1584 char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::rw_ptrmap(), ArchivePtrMarker::ro_ptrmap(), heap_info, 1585 bitmap_size_in_bytes); 1586 1587 if (heap_info->is_used()) { 1588 _total_heap_region_size = mapinfo->write_heap_region(heap_info); 1589 } 1590 1591 print_region_stats(mapinfo, heap_info); 1592 1593 mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address()); 1594 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 1595 // After this point, we should not write any data into mapinfo->header() since this 1596 // would corrupt its checksum we have calculated before. 1597 mapinfo->write_header(); 1598 mapinfo->close(); 1599 1600 if (log_is_enabled(Info, cds)) { 1601 print_stats(); 1602 } 1603 1604 if (log_is_enabled(Info, cds, map)) { 1605 CDSMapLogger::log(this, mapinfo, heap_info, 1606 bitmap, bitmap_size_in_bytes); 1607 } 1608 CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache()); 1609 FREE_C_HEAP_ARRAY(char, bitmap); 1610 } 1611 1612 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) { 1613 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec); 1614 } 1615 1616 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) { 1617 // Print statistics of all the regions 1618 const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used(); 1619 const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned(); 1620 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() + 1621 bitmap_reserved + 1622 _total_heap_region_size; 1623 const size_t total_bytes = _ro_region.used() + _rw_region.used() + 1624 bitmap_used + 1625 _total_heap_region_size; 1626 const double total_u_perc = percent_of(total_bytes, total_reserved); 1627 1628 _rw_region.print(total_reserved); 1629 _ro_region.print(total_reserved); 1630 1631 print_bitmap_region_stats(bitmap_used, total_reserved); 1632 1633 if (heap_info->is_used()) { 1634 print_heap_region_stats(heap_info, total_reserved); 1635 } 1636 1637 log_debug(cds)("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]", 1638 total_bytes, total_reserved, total_u_perc); 1639 } 1640 1641 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) { 1642 log_debug(cds)("bm space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]", 1643 size, size/double(total_size)*100.0, size); 1644 } 1645 1646 void ArchiveBuilder::print_heap_region_stats(ArchiveHeapInfo *info, size_t total_size) { 1647 char* start = info->buffer_start(); 1648 size_t size = info->buffer_byte_size(); 1649 char* top = start + size; 1650 log_debug(cds)("hp space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT, 1651 size, size/double(total_size)*100.0, size, p2i(start)); 1652 } 1653 1654 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) { 1655 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space. 1656 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes 1657 // or so. 1658 _rw_region.print_out_of_space_msg(name, needed_bytes); 1659 _ro_region.print_out_of_space_msg(name, needed_bytes); 1660 1661 log_error(cds)("Unable to allocate from '%s' region: Please reduce the number of shared classes.", name); 1662 MetaspaceShared::unrecoverable_writing_error(); 1663 }