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