1 /* 2 * Copyright (c) 2020, 2022, 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/archiveBuilder.hpp" 27 #include "cds/archiveUtils.hpp" 28 #include "cds/cppVtables.hpp" 29 #include "cds/dumpAllocStats.hpp" 30 #include "cds/heapShared.hpp" 31 #include "cds/metaspaceShared.hpp" 32 #include "classfile/classLoaderDataShared.hpp" 33 #include "classfile/symbolTable.hpp" 34 #include "classfile/systemDictionaryShared.hpp" 35 #include "classfile/vmClasses.hpp" 36 #include "interpreter/abstractInterpreter.hpp" 37 #include "logging/log.hpp" 38 #include "logging/logStream.hpp" 39 #include "memory/allStatic.hpp" 40 #include "memory/memRegion.hpp" 41 #include "memory/resourceArea.hpp" 42 #include "oops/instanceKlass.hpp" 43 #include "oops/objArrayKlass.hpp" 44 #include "oops/objArrayOop.inline.hpp" 45 #include "oops/oopHandle.inline.hpp" 46 #include "runtime/arguments.hpp" 47 #include "runtime/globals_extension.hpp" 48 #include "runtime/javaThread.hpp" 49 #include "runtime/sharedRuntime.hpp" 50 #include "utilities/align.hpp" 51 #include "utilities/bitMap.inline.hpp" 52 #include "utilities/formatBuffer.hpp" 53 54 ArchiveBuilder* ArchiveBuilder::_current = NULL; 55 56 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() { 57 char* newtop = ArchiveBuilder::current()->_ro_region.top(); 58 ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true); 59 } 60 61 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) { 62 _total_bytes = 0; 63 _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared); 64 } 65 66 ArchiveBuilder::SourceObjList::~SourceObjList() { 67 delete _objs; 68 } 69 70 void ArchiveBuilder::SourceObjList::append(MetaspaceClosure::Ref* enclosing_ref, SourceObjInfo* src_info) { 71 // Save this source object for copying 72 _objs->append(src_info); 73 74 // Prepare for marking the pointers in this source object 75 assert(is_aligned(_total_bytes, sizeof(address)), "must be"); 76 src_info->set_ptrmap_start(_total_bytes / sizeof(address)); 77 _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address)); 78 src_info->set_ptrmap_end(_total_bytes / sizeof(address)); 79 80 BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end()); 81 if (_ptrmap.size() <= bitmap_size_needed) { 82 _ptrmap.resize((bitmap_size_needed + 1) * 2); 83 } 84 } 85 86 void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) { 87 // src_obj contains a pointer. Remember the location of this pointer in _ptrmap, 88 // so that we can copy/relocate it later. E.g., if we have 89 // class Foo { intx scala; Bar* ptr; } 90 // Foo *f = 0x100; 91 // To mark the f->ptr pointer on 64-bit platform, this function is called with 92 // src_info()->obj() == 0x100 93 // ref->addr() == 0x108 94 address src_obj = src_info->obj(); 95 address* field_addr = ref->addr(); 96 assert(src_info->ptrmap_start() < _total_bytes, "sanity"); 97 assert(src_info->ptrmap_end() <= _total_bytes, "sanity"); 98 assert(*field_addr != NULL, "should have checked"); 99 100 intx field_offset_in_bytes = ((address)field_addr) - src_obj; 101 DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();) 102 assert(field_offset_in_bytes >= 0, "must be"); 103 assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be"); 104 assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be"); 105 106 BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address))); 107 _ptrmap.set_bit(BitMap::idx_t(idx)); 108 } 109 110 class RelocateEmbeddedPointers : public BitMapClosure { 111 ArchiveBuilder* _builder; 112 address _buffered_obj; 113 BitMap::idx_t _start_idx; 114 public: 115 RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) : 116 _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {} 117 118 bool do_bit(BitMap::idx_t bit_offset) { 119 size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address); 120 address* ptr_loc = (address*)(_buffered_obj + field_offset); 121 122 address old_p = *ptr_loc; 123 address new_p = _builder->get_buffered_addr(old_p); 124 125 log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT, 126 p2i(ptr_loc), p2i(old_p), p2i(new_p)); 127 128 ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p); 129 return true; // keep iterating the bitmap 130 } 131 }; 132 133 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) { 134 SourceObjInfo* src_info = objs()->at(i); 135 assert(src_info->should_copy(), "must be"); 136 BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive 137 BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end()); // exclusive 138 139 RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start); 140 _ptrmap.iterate(&relocator, start, end); 141 } 142 143 ArchiveBuilder::ArchiveBuilder() : 144 _current_dump_space(NULL), 145 _buffer_bottom(NULL), 146 _last_verified_top(NULL), 147 _num_dump_regions_used(0), 148 _other_region_used_bytes(0), 149 _requested_static_archive_bottom(NULL), 150 _requested_static_archive_top(NULL), 151 _requested_dynamic_archive_bottom(NULL), 152 _requested_dynamic_archive_top(NULL), 153 _mapped_static_archive_bottom(NULL), 154 _mapped_static_archive_top(NULL), 155 _buffer_to_requested_delta(0), 156 _rw_region("rw", MAX_SHARED_DELTA), 157 _ro_region("ro", MAX_SHARED_DELTA), 158 _ptrmap(mtClassShared), 159 _rw_src_objs(), 160 _ro_src_objs(), 161 _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE), 162 _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE), 163 _total_closed_heap_region_size(0), 164 _total_open_heap_region_size(0), 165 _estimated_metaspaceobj_bytes(0), 166 _estimated_hashtable_bytes(0) 167 { 168 _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared); 169 _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared); 170 _special_refs = new (mtClassShared) GrowableArray<SpecialRefInfo>(24 * K, mtClassShared); 171 172 assert(_current == NULL, "must be"); 173 _current = this; 174 } 175 176 ArchiveBuilder::~ArchiveBuilder() { 177 assert(_current == this, "must be"); 178 _current = NULL; 179 180 clean_up_src_obj_table(); 181 182 for (int i = 0; i < _symbols->length(); i++) { 183 _symbols->at(i)->decrement_refcount(); 184 } 185 186 delete _klasses; 187 delete _symbols; 188 delete _special_refs; 189 if (_shared_rs.is_reserved()) { 190 _shared_rs.release(); 191 } 192 } 193 194 bool ArchiveBuilder::is_dumping_full_module_graph() { 195 return DumpSharedSpaces && MetaspaceShared::use_full_module_graph(); 196 } 197 198 class GatherKlassesAndSymbols : public UniqueMetaspaceClosure { 199 ArchiveBuilder* _builder; 200 201 public: 202 GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {} 203 204 virtual bool do_unique_ref(Ref* ref, bool read_only) { 205 return _builder->gather_klass_and_symbol(ref, read_only); 206 } 207 }; 208 209 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) { 210 if (ref->obj() == NULL) { 211 return false; 212 } 213 if (get_follow_mode(ref) != make_a_copy) { 214 return false; 215 } 216 if (ref->msotype() == MetaspaceObj::ClassType) { 217 Klass* klass = (Klass*)ref->obj(); 218 assert(klass->is_klass(), "must be"); 219 if (!is_excluded(klass)) { 220 _klasses->append(klass); 221 } 222 // See RunTimeClassInfo::get_for() 223 _estimated_metaspaceobj_bytes += align_up(BytesPerWord, SharedSpaceObjectAlignment); 224 } else if (ref->msotype() == MetaspaceObj::SymbolType) { 225 // Make sure the symbol won't be GC'ed while we are dumping the archive. 226 Symbol* sym = (Symbol*)ref->obj(); 227 sym->increment_refcount(); 228 _symbols->append(sym); 229 } 230 231 int bytes = ref->size() * BytesPerWord; 232 _estimated_metaspaceobj_bytes += align_up(bytes, SharedSpaceObjectAlignment); 233 234 return true; // recurse 235 } 236 237 void ArchiveBuilder::gather_klasses_and_symbols() { 238 ResourceMark rm; 239 log_info(cds)("Gathering classes and symbols ... "); 240 GatherKlassesAndSymbols doit(this); 241 iterate_roots(&doit, /*is_relocating_pointers=*/false); 242 #if INCLUDE_CDS_JAVA_HEAP 243 if (is_dumping_full_module_graph()) { 244 ClassLoaderDataShared::iterate_symbols(&doit); 245 } 246 #endif 247 doit.finish(); 248 249 if (DumpSharedSpaces) { 250 // To ensure deterministic contents in the static archive, we need to ensure that 251 // we iterate the MetaspaceObjs in a deterministic order. It doesn't matter where 252 // the MetaspaceObjs are located originally, as they are copied sequentially into 253 // the archive during the iteration. 254 // 255 // The only issue here is that the symbol table and the system directories may be 256 // randomly ordered, so we copy the symbols and klasses into two arrays and sort 257 // them deterministically. 258 // 259 // During -Xshare:dump, the order of Symbol creation is strictly determined by 260 // the SharedClassListFile (class loading is done in a single thread and the JIT 261 // is disabled). Also, Symbols are allocated in monotonically increasing addresses 262 // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by 263 // ascending address order, we ensure that all Symbols are copied into deterministic 264 // locations in the archive. 265 // 266 // TODO: in the future, if we want to produce deterministic contents in the 267 // dynamic archive, we might need to sort the symbols alphabetically (also see 268 // DynamicArchiveBuilder::sort_methods()). 269 sort_symbols_and_fix_hash(); 270 sort_klasses(); 271 272 // TODO -- we need a proper estimate for the archived modules, etc, 273 // but this should be enough for now 274 _estimated_metaspaceobj_bytes += 200 * 1024 * 1024; 275 } 276 } 277 278 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) { 279 if (a[0] < b[0]) { 280 return -1; 281 } else { 282 assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string()); 283 return 1; 284 } 285 } 286 287 void ArchiveBuilder::sort_symbols_and_fix_hash() { 288 log_info(cds)("Sorting symbols and fixing identity hash ... "); 289 os::init_random(0x12345678); 290 _symbols->sort(compare_symbols_by_address); 291 for (int i = 0; i < _symbols->length(); i++) { 292 assert(_symbols->at(i)->is_permanent(), "archived symbols must be permanent"); 293 _symbols->at(i)->update_identity_hash(); 294 } 295 } 296 297 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) { 298 return a[0]->name()->fast_compare(b[0]->name()); 299 } 300 301 void ArchiveBuilder::sort_klasses() { 302 log_info(cds)("Sorting classes ... "); 303 _klasses->sort(compare_klass_by_name); 304 } 305 306 size_t ArchiveBuilder::estimate_archive_size() { 307 // size of the symbol table and two dictionaries, plus the RunTimeClassInfo's 308 size_t symbol_table_est = SymbolTable::estimate_size_for_archive(); 309 size_t dictionary_est = SystemDictionaryShared::estimate_size_for_archive(); 310 _estimated_hashtable_bytes = symbol_table_est + dictionary_est; 311 312 size_t total = 0; 313 314 total += _estimated_metaspaceobj_bytes; 315 total += _estimated_hashtable_bytes; 316 317 // allow fragmentation at the end of each dump region 318 total += _total_dump_regions * MetaspaceShared::core_region_alignment(); 319 320 log_info(cds)("_estimated_hashtable_bytes = " SIZE_FORMAT " + " SIZE_FORMAT " = " SIZE_FORMAT, 321 symbol_table_est, dictionary_est, _estimated_hashtable_bytes); 322 log_info(cds)("_estimated_metaspaceobj_bytes = " SIZE_FORMAT, _estimated_metaspaceobj_bytes); 323 log_info(cds)("total estimate bytes = " SIZE_FORMAT, total); 324 325 return align_up(total, MetaspaceShared::core_region_alignment()); 326 } 327 328 address ArchiveBuilder::reserve_buffer() { 329 size_t buffer_size = estimate_archive_size(); 330 ReservedSpace rs(buffer_size, MetaspaceShared::core_region_alignment(), os::vm_page_size()); 331 if (!rs.is_reserved()) { 332 log_error(cds)("Failed to reserve " SIZE_FORMAT " bytes of output buffer.", buffer_size); 333 os::_exit(0); 334 } 335 336 // buffer_bottom is the lowest address of the 2 core regions (rw, ro) when 337 // we are copying the class metadata into the buffer. 338 address buffer_bottom = (address)rs.base(); 339 log_info(cds)("Reserved output buffer space at " PTR_FORMAT " [" SIZE_FORMAT " bytes]", 340 p2i(buffer_bottom), buffer_size); 341 _shared_rs = rs; 342 343 _buffer_bottom = buffer_bottom; 344 _last_verified_top = buffer_bottom; 345 _current_dump_space = &_rw_region; 346 _num_dump_regions_used = 1; 347 _other_region_used_bytes = 0; 348 _current_dump_space->init(&_shared_rs, &_shared_vs); 349 350 ArchivePtrMarker::initialize(&_ptrmap, &_shared_vs); 351 352 // The bottom of the static archive should be mapped at this address by default. 353 _requested_static_archive_bottom = (address)MetaspaceShared::requested_base_address(); 354 355 // The bottom of the archive (that I am writing now) should be mapped at this address by default. 356 address my_archive_requested_bottom; 357 358 if (DumpSharedSpaces) { 359 my_archive_requested_bottom = _requested_static_archive_bottom; 360 } else { 361 _mapped_static_archive_bottom = (address)MetaspaceObj::shared_metaspace_base(); 362 _mapped_static_archive_top = (address)MetaspaceObj::shared_metaspace_top(); 363 assert(_mapped_static_archive_top >= _mapped_static_archive_bottom, "must be"); 364 size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom; 365 366 // At run time, we will mmap the dynamic archive at my_archive_requested_bottom 367 _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size; 368 my_archive_requested_bottom = align_up(_requested_static_archive_top, MetaspaceShared::core_region_alignment()); 369 370 _requested_dynamic_archive_bottom = my_archive_requested_bottom; 371 } 372 373 _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom; 374 375 address my_archive_requested_top = my_archive_requested_bottom + buffer_size; 376 if (my_archive_requested_bottom < _requested_static_archive_bottom || 377 my_archive_requested_top <= _requested_static_archive_bottom) { 378 // Size overflow. 379 log_error(cds)("my_archive_requested_bottom = " INTPTR_FORMAT, p2i(my_archive_requested_bottom)); 380 log_error(cds)("my_archive_requested_top = " INTPTR_FORMAT, p2i(my_archive_requested_top)); 381 log_error(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is too high. " 382 "Please rerun java -Xshare:dump with a lower value", p2i(_requested_static_archive_bottom)); 383 os::_exit(0); 384 } 385 386 if (DumpSharedSpaces) { 387 // We don't want any valid object to be at the very bottom of the archive. 388 // See ArchivePtrMarker::mark_pointer(). 389 rw_region()->allocate(16); 390 } 391 392 return buffer_bottom; 393 } 394 395 void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it, bool is_relocating_pointers) { 396 int i; 397 398 if (!is_relocating_pointers) { 399 // Don't relocate _symbol, so we can safely call decrement_refcount on the 400 // original symbols. 401 int num_symbols = _symbols->length(); 402 for (i = 0; i < num_symbols; i++) { 403 it->push(_symbols->adr_at(i)); 404 } 405 } 406 407 int num_klasses = _klasses->length(); 408 for (i = 0; i < num_klasses; i++) { 409 it->push(_klasses->adr_at(i)); 410 } 411 412 iterate_roots(it, is_relocating_pointers); 413 } 414 415 class GatherSortedSourceObjs : public MetaspaceClosure { 416 ArchiveBuilder* _builder; 417 418 public: 419 GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {} 420 421 virtual bool do_ref(Ref* ref, bool read_only) { 422 return _builder->gather_one_source_obj(enclosing_ref(), ref, read_only); 423 } 424 425 virtual void push_special(SpecialRef type, Ref* ref, intptr_t* p) { 426 assert(type == _method_entry_ref, "only special type allowed for now"); 427 address src_obj = ref->obj(); 428 size_t field_offset = pointer_delta(p, src_obj, sizeof(u1)); 429 _builder->add_special_ref(type, src_obj, field_offset); 430 }; 431 432 virtual void do_pending_ref(Ref* ref) { 433 if (ref->obj() != NULL) { 434 _builder->remember_embedded_pointer_in_copied_obj(enclosing_ref(), ref); 435 } 436 } 437 }; 438 439 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref, 440 MetaspaceClosure::Ref* ref, bool read_only) { 441 address src_obj = ref->obj(); 442 if (src_obj == NULL) { 443 return false; 444 } 445 ref->set_keep_after_pushing(); 446 remember_embedded_pointer_in_copied_obj(enclosing_ref, ref); 447 448 FollowMode follow_mode = get_follow_mode(ref); 449 SourceObjInfo src_info(ref, read_only, follow_mode); 450 bool created; 451 SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created); 452 if (created) { 453 if (_src_obj_table.maybe_grow()) { 454 log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size()); 455 } 456 } 457 458 assert(p->read_only() == src_info.read_only(), "must be"); 459 460 if (created && src_info.should_copy()) { 461 ref->set_user_data((void*)p); 462 if (read_only) { 463 _ro_src_objs.append(enclosing_ref, p); 464 } else { 465 _rw_src_objs.append(enclosing_ref, p); 466 } 467 return true; // Need to recurse into this ref only if we are copying it 468 } else { 469 return false; 470 } 471 } 472 473 void ArchiveBuilder::add_special_ref(MetaspaceClosure::SpecialRef type, address src_obj, size_t field_offset) { 474 _special_refs->append(SpecialRefInfo(type, src_obj, field_offset)); 475 } 476 477 void ArchiveBuilder::remember_embedded_pointer_in_copied_obj(MetaspaceClosure::Ref* enclosing_ref, 478 MetaspaceClosure::Ref* ref) { 479 assert(ref->obj() != NULL, "should have checked"); 480 481 if (enclosing_ref != NULL) { 482 SourceObjInfo* src_info = (SourceObjInfo*)enclosing_ref->user_data(); 483 if (src_info == NULL) { 484 // source objects of point_to_it/set_to_null types are not copied 485 // so we don't need to remember their pointers. 486 } else { 487 if (src_info->read_only()) { 488 _ro_src_objs.remember_embedded_pointer(src_info, ref); 489 } else { 490 _rw_src_objs.remember_embedded_pointer(src_info, ref); 491 } 492 } 493 } 494 } 495 496 void ArchiveBuilder::gather_source_objs() { 497 ResourceMark rm; 498 log_info(cds)("Gathering all archivable objects ... "); 499 gather_klasses_and_symbols(); 500 GatherSortedSourceObjs doit(this); 501 iterate_sorted_roots(&doit, /*is_relocating_pointers=*/false); 502 doit.finish(); 503 } 504 505 bool ArchiveBuilder::is_excluded(Klass* klass) { 506 if (klass->is_instance_klass()) { 507 InstanceKlass* ik = InstanceKlass::cast(klass); 508 return SystemDictionaryShared::is_excluded_class(ik); 509 } else if (klass->is_objArray_klass()) { 510 if (DynamicDumpSharedSpaces) { 511 // Don't support archiving of array klasses for now (WHY???) 512 return true; 513 } 514 Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass(); 515 if (bottom->is_instance_klass()) { 516 return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom)); 517 } 518 } 519 520 return false; 521 } 522 523 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) { 524 address obj = ref->obj(); 525 if (MetaspaceShared::is_in_shared_metaspace(obj)) { 526 // Don't dump existing shared metadata again. 527 return point_to_it; 528 } else if (ref->msotype() == MetaspaceObj::MethodDataType || 529 ref->msotype() == MetaspaceObj::MethodCountersType) { 530 return set_to_null; 531 } else { 532 if (ref->msotype() == MetaspaceObj::ClassType) { 533 Klass* klass = (Klass*)ref->obj(); 534 assert(klass->is_klass(), "must be"); 535 if (is_excluded(klass)) { 536 ResourceMark rm; 537 log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name()); 538 return set_to_null; 539 } 540 } 541 542 return make_a_copy; 543 } 544 } 545 546 void ArchiveBuilder::start_dump_space(DumpRegion* next) { 547 address bottom = _last_verified_top; 548 address top = (address)(current_dump_space()->top()); 549 _other_region_used_bytes += size_t(top - bottom); 550 551 current_dump_space()->pack(next); 552 _current_dump_space = next; 553 _num_dump_regions_used ++; 554 555 _last_verified_top = (address)(current_dump_space()->top()); 556 } 557 558 void ArchiveBuilder::verify_estimate_size(size_t estimate, const char* which) { 559 address bottom = _last_verified_top; 560 address top = (address)(current_dump_space()->top()); 561 size_t used = size_t(top - bottom) + _other_region_used_bytes; 562 int diff = int(estimate) - int(used); 563 564 log_info(cds)("%s estimate = " SIZE_FORMAT " used = " SIZE_FORMAT "; diff = %d bytes", which, estimate, used, diff); 565 assert(diff >= 0, "Estimate is too small"); 566 567 _last_verified_top = top; 568 _other_region_used_bytes = 0; 569 } 570 571 void ArchiveBuilder::dump_rw_metadata() { 572 ResourceMark rm; 573 log_info(cds)("Allocating RW objects ... "); 574 make_shallow_copies(&_rw_region, &_rw_src_objs); 575 576 #if INCLUDE_CDS_JAVA_HEAP 577 if (is_dumping_full_module_graph()) { 578 // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders 579 char* start = rw_region()->top(); 580 ClassLoaderDataShared::allocate_archived_tables(); 581 alloc_stats()->record_modules(rw_region()->top() - start, /*read_only*/false); 582 } 583 #endif 584 } 585 586 void ArchiveBuilder::dump_ro_metadata() { 587 ResourceMark rm; 588 log_info(cds)("Allocating RO objects ... "); 589 590 start_dump_space(&_ro_region); 591 make_shallow_copies(&_ro_region, &_ro_src_objs); 592 593 #if INCLUDE_CDS_JAVA_HEAP 594 if (is_dumping_full_module_graph()) { 595 char* start = ro_region()->top(); 596 ClassLoaderDataShared::init_archived_tables(); 597 alloc_stats()->record_modules(ro_region()->top() - start, /*read_only*/true); 598 } 599 #endif 600 } 601 602 void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region, 603 const ArchiveBuilder::SourceObjList* src_objs) { 604 for (int i = 0; i < src_objs->objs()->length(); i++) { 605 make_shallow_copy(dump_region, src_objs->objs()->at(i)); 606 } 607 log_info(cds)("done (%d objects)", src_objs->objs()->length()); 608 } 609 610 void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) { 611 MetaspaceClosure::Ref* ref = src_info->ref(); 612 address src = ref->obj(); 613 int bytes = src_info->size_in_bytes(); 614 char* dest; 615 char* oldtop; 616 char* newtop; 617 618 oldtop = dump_region->top(); 619 if (ref->msotype() == MetaspaceObj::ClassType) { 620 // Save a pointer immediate in front of an InstanceKlass, so 621 // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo* 622 // without building another hashtable. See RunTimeClassInfo::get_for() 623 // in systemDictionaryShared.cpp. 624 Klass* klass = (Klass*)src; 625 if (klass->is_instance_klass()) { 626 SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass)); 627 dump_region->allocate(sizeof(address)); 628 } 629 } 630 dest = dump_region->allocate(bytes); 631 newtop = dump_region->top(); 632 633 memcpy(dest, src, bytes); 634 { 635 bool created; 636 _buffered_to_src_table.put_if_absent((address)dest, src, &created); 637 assert(created, "must be"); 638 if (_buffered_to_src_table.maybe_grow()) { 639 log_info(cds, hashtables)("Expanded _buffered_to_src_table table to %d", _buffered_to_src_table.table_size()); 640 } 641 } 642 643 intptr_t* archived_vtable = CppVtables::get_archived_vtable(ref->msotype(), (address)dest); 644 if (archived_vtable != NULL) { 645 *(address*)dest = (address)archived_vtable; 646 ArchivePtrMarker::mark_pointer((address*)dest); 647 } 648 649 log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes); 650 src_info->set_buffered_addr((address)dest); 651 652 _alloc_stats.record(ref->msotype(), int(newtop - oldtop), src_info->read_only()); 653 } 654 655 address ArchiveBuilder::get_buffered_addr(address src_addr) const { 656 SourceObjInfo* p = _src_obj_table.get(src_addr); 657 assert(p != NULL, "must be"); 658 659 return p->buffered_addr(); 660 } 661 662 address ArchiveBuilder::get_source_addr(address buffered_addr) const { 663 assert(is_in_buffer_space(buffered_addr), "must be"); 664 address* src_p = _buffered_to_src_table.get(buffered_addr); 665 assert(src_p != NULL && *src_p != NULL, "must be"); 666 return *src_p; 667 } 668 669 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) { 670 for (int i = 0; i < src_objs->objs()->length(); i++) { 671 src_objs->relocate(i, this); 672 } 673 } 674 675 void ArchiveBuilder::update_special_refs() { 676 for (int i = 0; i < _special_refs->length(); i++) { 677 SpecialRefInfo s = _special_refs->at(i); 678 size_t field_offset = s.field_offset(); 679 address src_obj = s.src_obj(); 680 address dst_obj = get_buffered_addr(src_obj); 681 intptr_t* src_p = (intptr_t*)(src_obj + field_offset); 682 intptr_t* dst_p = (intptr_t*)(dst_obj + field_offset); 683 assert(s.type() == MetaspaceClosure::_method_entry_ref, "only special type allowed for now"); 684 685 assert(*src_p == *dst_p, "must be a copy"); 686 ArchivePtrMarker::mark_pointer((address*)dst_p); 687 } 688 } 689 690 class RefRelocator: public MetaspaceClosure { 691 ArchiveBuilder* _builder; 692 693 public: 694 RefRelocator(ArchiveBuilder* builder) : _builder(builder) {} 695 696 virtual bool do_ref(Ref* ref, bool read_only) { 697 if (ref->not_null()) { 698 ref->update(_builder->get_buffered_addr(ref->obj())); 699 ArchivePtrMarker::mark_pointer(ref->addr()); 700 } 701 return false; // Do not recurse. 702 } 703 }; 704 705 void ArchiveBuilder::relocate_roots() { 706 log_info(cds)("Relocating external roots ... "); 707 ResourceMark rm; 708 RefRelocator doit(this); 709 iterate_sorted_roots(&doit, /*is_relocating_pointers=*/true); 710 doit.finish(); 711 log_info(cds)("done"); 712 } 713 714 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() { 715 log_info(cds)("Relocating embedded pointers in core regions ... "); 716 relocate_embedded_pointers(&_rw_src_objs); 717 relocate_embedded_pointers(&_ro_src_objs); 718 update_special_refs(); 719 } 720 721 // We must relocate vmClasses::_klasses[] only after we have copied the 722 // java objects in during dump_java_heap_objects(): during the object copy, we operate on 723 // old objects which assert that their klass is the original klass. 724 void ArchiveBuilder::relocate_vm_classes() { 725 log_info(cds)("Relocating vmClasses::_klasses[] ... "); 726 ResourceMark rm; 727 RefRelocator doit(this); 728 vmClasses::metaspace_pointers_do(&doit); 729 } 730 731 void ArchiveBuilder::make_klasses_shareable() { 732 int num_instance_klasses = 0; 733 int num_boot_klasses = 0; 734 int num_platform_klasses = 0; 735 int num_app_klasses = 0; 736 int num_hidden_klasses = 0; 737 int num_unlinked_klasses = 0; 738 int num_unregistered_klasses = 0; 739 int num_obj_array_klasses = 0; 740 int num_type_array_klasses = 0; 741 742 for (int i = 0; i < klasses()->length(); i++) { 743 const char* type; 744 const char* unlinked = ""; 745 const char* hidden = ""; 746 const char* generated = ""; 747 Klass* k = klasses()->at(i); 748 k->remove_java_mirror(); 749 if (k->is_objArray_klass()) { 750 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info 751 // on their array classes. 752 num_obj_array_klasses ++; 753 type = "array"; 754 } else if (k->is_typeArray_klass()) { 755 num_type_array_klasses ++; 756 type = "array"; 757 k->remove_unshareable_info(); 758 } else { 759 assert(k->is_instance_klass(), " must be"); 760 num_instance_klasses ++; 761 InstanceKlass* ik = InstanceKlass::cast(k); 762 if (DynamicDumpSharedSpaces) { 763 // For static dump, class loader type are already set. 764 ik->assign_class_loader_type(); 765 } 766 if (ik->is_shared_boot_class()) { 767 type = "boot"; 768 num_boot_klasses ++; 769 } else if (ik->is_shared_platform_class()) { 770 type = "plat"; 771 num_platform_klasses ++; 772 } else if (ik->is_shared_app_class()) { 773 type = "app"; 774 num_app_klasses ++; 775 } else { 776 assert(ik->is_shared_unregistered_class(), "must be"); 777 type = "unreg"; 778 num_unregistered_klasses ++; 779 } 780 781 if (!ik->is_linked()) { 782 num_unlinked_klasses ++; 783 unlinked = " ** unlinked"; 784 } 785 786 if (ik->is_hidden()) { 787 num_hidden_klasses ++; 788 hidden = " ** hidden"; 789 } 790 791 if (ik->is_generated_shared_class()) { 792 generated = " ** generated"; 793 } 794 MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik); 795 ik->remove_unshareable_info(); 796 } 797 798 if (log_is_enabled(Debug, cds, class)) { 799 ResourceMark rm; 800 log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s", i, 801 p2i(to_requested(k)), type, k->external_name(), 802 hidden, unlinked, generated); 803 } 804 } 805 806 log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses); 807 log_info(cds)(" instance classes = %5d", num_instance_klasses); 808 log_info(cds)(" boot = %5d", num_boot_klasses); 809 log_info(cds)(" app = %5d", num_app_klasses); 810 log_info(cds)(" platform = %5d", num_platform_klasses); 811 log_info(cds)(" unregistered = %5d", num_unregistered_klasses); 812 log_info(cds)(" (hidden) = %5d", num_hidden_klasses); 813 log_info(cds)(" (unlinked) = %5d", num_unlinked_klasses); 814 log_info(cds)(" obj array classes = %5d", num_obj_array_klasses); 815 log_info(cds)(" type array classes = %5d", num_type_array_klasses); 816 log_info(cds)(" symbols = %5d", _symbols->length()); 817 } 818 819 uintx ArchiveBuilder::buffer_to_offset(address p) const { 820 address requested_p = to_requested(p); 821 assert(requested_p >= _requested_static_archive_bottom, "must be"); 822 return requested_p - _requested_static_archive_bottom; 823 } 824 825 uintx ArchiveBuilder::any_to_offset(address p) const { 826 if (is_in_mapped_static_archive(p)) { 827 assert(DynamicDumpSharedSpaces, "must be"); 828 return p - _mapped_static_archive_bottom; 829 } 830 return buffer_to_offset(p); 831 } 832 833 // Update a Java object to point its Klass* to the address whene 834 // the class would be mapped at runtime. 835 void ArchiveBuilder::relocate_klass_ptr_of_oop(oop o) { 836 assert(DumpSharedSpaces, "sanity"); 837 Klass* k = get_buffered_klass(o->klass()); 838 Klass* requested_k = to_requested(k); 839 narrowKlass nk = CompressedKlassPointers::encode_not_null(requested_k, _requested_static_archive_bottom); 840 o->set_narrow_klass(nk); 841 } 842 843 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro, 844 // so that the archive can be mapped to the "requested" location without runtime relocation. 845 // 846 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested" 847 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions 848 // - Every pointer must have one of the following values: 849 // [a] NULL: 850 // No relocation is needed. Remove this pointer from ptrmap so we don't need to 851 // consider it at runtime. 852 // [b] Points into an object X which is inside the buffer: 853 // Adjust this pointer by _buffer_to_requested_delta, so it points to X 854 // when the archive is mapped at the requested location. 855 // [c] Points into an object Y which is inside mapped static archive: 856 // - This happens only during dynamic dump 857 // - Adjust this pointer by _mapped_to_requested_static_archive_delta, 858 // so it points to Y when the static archive is mapped at the requested location. 859 template <bool STATIC_DUMP> 860 class RelocateBufferToRequested : public BitMapClosure { 861 ArchiveBuilder* _builder; 862 address _buffer_bottom; 863 intx _buffer_to_requested_delta; 864 intx _mapped_to_requested_static_archive_delta; 865 size_t _max_non_null_offset; 866 867 public: 868 RelocateBufferToRequested(ArchiveBuilder* builder) { 869 _builder = builder; 870 _buffer_bottom = _builder->buffer_bottom(); 871 _buffer_to_requested_delta = builder->buffer_to_requested_delta(); 872 _mapped_to_requested_static_archive_delta = builder->requested_static_archive_bottom() - builder->mapped_static_archive_bottom(); 873 _max_non_null_offset = 0; 874 875 address bottom = _builder->buffer_bottom(); 876 address top = _builder->buffer_top(); 877 address new_bottom = bottom + _buffer_to_requested_delta; 878 address new_top = top + _buffer_to_requested_delta; 879 log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to " 880 "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]", 881 p2i(bottom), p2i(top), 882 p2i(new_bottom), p2i(new_top)); 883 } 884 885 bool do_bit(size_t offset) { 886 address* p = (address*)_buffer_bottom + offset; 887 assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space"); 888 889 if (*p == NULL) { 890 // todo -- clear bit, etc 891 ArchivePtrMarker::ptrmap()->clear_bit(offset); 892 } else { 893 if (STATIC_DUMP) { 894 assert(_builder->is_in_buffer_space(*p), "old pointer must point inside buffer space"); 895 *p += _buffer_to_requested_delta; 896 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive"); 897 } else { 898 if (_builder->is_in_buffer_space(*p)) { 899 *p += _buffer_to_requested_delta; 900 // assert is in requested dynamic archive 901 } else { 902 assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive"); 903 *p += _mapped_to_requested_static_archive_delta; 904 assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive"); 905 } 906 } 907 _max_non_null_offset = offset; 908 } 909 910 return true; // keep iterating 911 } 912 913 void doit() { 914 ArchivePtrMarker::ptrmap()->iterate(this); 915 ArchivePtrMarker::compact(_max_non_null_offset); 916 } 917 }; 918 919 920 void ArchiveBuilder::relocate_to_requested() { 921 ro_region()->pack(); 922 923 size_t my_archive_size = buffer_top() - buffer_bottom(); 924 925 if (DumpSharedSpaces) { 926 _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size; 927 RelocateBufferToRequested<true> patcher(this); 928 patcher.doit(); 929 } else { 930 assert(DynamicDumpSharedSpaces, "must be"); 931 _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size; 932 RelocateBufferToRequested<false> patcher(this); 933 patcher.doit(); 934 } 935 } 936 937 // Write detailed info to a mapfile to analyze contents of the archive. 938 // static dump: 939 // java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0 940 // dynamic dump: 941 // java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \ 942 // -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp 943 // 944 // We need to do some address translation because the buffers used at dump time may be mapped to 945 // a different location at runtime. At dump time, the buffers may be at arbitrary locations 946 // picked by the OS. At runtime, we try to map at a fixed location (SharedBaseAddress). For 947 // consistency, we log everything using runtime addresses. 948 class ArchiveBuilder::CDSMapLogger : AllStatic { 949 static intx buffer_to_runtime_delta() { 950 // Translate the buffers used by the RW/RO regions to their eventual (requested) locations 951 // at runtime. 952 return ArchiveBuilder::current()->buffer_to_requested_delta(); 953 } 954 955 // rw/ro regions only 956 static void log_metaspace_region(const char* name, DumpRegion* region, 957 const ArchiveBuilder::SourceObjList* src_objs) { 958 address region_base = address(region->base()); 959 address region_top = address(region->top()); 960 log_region(name, region_base, region_top, region_base + buffer_to_runtime_delta()); 961 log_metaspace_objects(region, src_objs); 962 } 963 964 #define _LOG_PREFIX PTR_FORMAT ": @@ %-17s %d" 965 966 static void log_klass(Klass* k, address runtime_dest, const char* type_name, int bytes, Thread* current) { 967 ResourceMark rm(current); 968 log_debug(cds, map)(_LOG_PREFIX " %s", 969 p2i(runtime_dest), type_name, bytes, k->external_name()); 970 } 971 static void log_method(Method* m, address runtime_dest, const char* type_name, int bytes, Thread* current) { 972 ResourceMark rm(current); 973 log_debug(cds, map)(_LOG_PREFIX " %s", 974 p2i(runtime_dest), type_name, bytes, m->external_name()); 975 } 976 977 // rw/ro regions only 978 static void log_metaspace_objects(DumpRegion* region, const ArchiveBuilder::SourceObjList* src_objs) { 979 address last_obj_base = address(region->base()); 980 address last_obj_end = address(region->base()); 981 address region_end = address(region->end()); 982 Thread* current = Thread::current(); 983 for (int i = 0; i < src_objs->objs()->length(); i++) { 984 SourceObjInfo* src_info = src_objs->at(i); 985 address src = src_info->source_addr(); 986 address dest = src_info->buffered_addr(); 987 log_data(last_obj_base, dest, last_obj_base + buffer_to_runtime_delta()); 988 address runtime_dest = dest + buffer_to_runtime_delta(); 989 int bytes = src_info->size_in_bytes(); 990 991 MetaspaceObj::Type type = src_info->msotype(); 992 const char* type_name = MetaspaceObj::type_name(type); 993 994 switch (type) { 995 case MetaspaceObj::ClassType: 996 log_klass((Klass*)src, runtime_dest, type_name, bytes, current); 997 break; 998 case MetaspaceObj::ConstantPoolType: 999 log_klass(((ConstantPool*)src)->pool_holder(), 1000 runtime_dest, type_name, bytes, current); 1001 break; 1002 case MetaspaceObj::ConstantPoolCacheType: 1003 log_klass(((ConstantPoolCache*)src)->constant_pool()->pool_holder(), 1004 runtime_dest, type_name, bytes, current); 1005 break; 1006 case MetaspaceObj::MethodType: 1007 log_method((Method*)src, runtime_dest, type_name, bytes, current); 1008 break; 1009 case MetaspaceObj::ConstMethodType: 1010 log_method(((ConstMethod*)src)->method(), runtime_dest, type_name, bytes, current); 1011 break; 1012 case MetaspaceObj::SymbolType: 1013 { 1014 ResourceMark rm(current); 1015 Symbol* s = (Symbol*)src; 1016 log_debug(cds, map)(_LOG_PREFIX " %s", p2i(runtime_dest), type_name, bytes, 1017 s->as_quoted_ascii()); 1018 } 1019 break; 1020 default: 1021 log_debug(cds, map)(_LOG_PREFIX, p2i(runtime_dest), type_name, bytes); 1022 break; 1023 } 1024 1025 last_obj_base = dest; 1026 last_obj_end = dest + bytes; 1027 } 1028 1029 log_data(last_obj_base, last_obj_end, last_obj_base + buffer_to_runtime_delta()); 1030 if (last_obj_end < region_end) { 1031 log_debug(cds, map)(PTR_FORMAT ": @@ Misc data " SIZE_FORMAT " bytes", 1032 p2i(last_obj_end + buffer_to_runtime_delta()), 1033 size_t(region_end - last_obj_end)); 1034 log_data(last_obj_end, region_end, last_obj_end + buffer_to_runtime_delta()); 1035 } 1036 } 1037 1038 #undef _LOG_PREFIX 1039 1040 // Log information about a region, whose address at dump time is [base .. top). At 1041 // runtime, this region will be mapped to requested_base. requested_base is 0 if this 1042 // region will be mapped at os-selected addresses (such as the bitmap region), or will 1043 // be accessed with os::read (the header). 1044 // 1045 // Note: across -Xshare:dump runs, base may be different, but requested_base should 1046 // be the same as the archive contents should be deterministic. 1047 static void log_region(const char* name, address base, address top, address requested_base) { 1048 size_t size = top - base; 1049 base = requested_base; 1050 top = requested_base + size; 1051 log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " " SIZE_FORMAT_W(9) " bytes]", 1052 name, p2i(base), p2i(top), size); 1053 } 1054 1055 #if INCLUDE_CDS_JAVA_HEAP 1056 // open and closed archive regions 1057 static void log_heap_regions(const char* which, GrowableArray<MemRegion> *regions) { 1058 for (int i = 0; i < regions->length(); i++) { 1059 address start = address(regions->at(i).start()); 1060 address end = address(regions->at(i).end()); 1061 log_region(which, start, end, to_requested(start)); 1062 1063 while (start < end) { 1064 size_t byte_size; 1065 oop archived_oop = cast_to_oop(start); 1066 oop original_oop = HeapShared::get_original_object(archived_oop); 1067 if (original_oop != NULL) { 1068 ResourceMark rm; 1069 log_info(cds, map)(PTR_FORMAT ": @@ Object %s", 1070 p2i(to_requested(start)), original_oop->klass()->external_name()); 1071 byte_size = original_oop->size() * BytesPerWord; 1072 } else if (archived_oop == HeapShared::roots()) { 1073 // HeapShared::roots() is copied specially so it doesn't exist in 1074 // HeapShared::OriginalObjectTable. See HeapShared::copy_roots(). 1075 log_info(cds, map)(PTR_FORMAT ": @@ Object HeapShared::roots (ObjArray)", 1076 p2i(to_requested(start))); 1077 byte_size = objArrayOopDesc::object_size(HeapShared::roots()->length()) * BytesPerWord; 1078 } else { 1079 // We have reached the end of the region 1080 break; 1081 } 1082 address oop_end = start + byte_size; 1083 log_data(start, oop_end, to_requested(start), /*is_heap=*/true); 1084 start = oop_end; 1085 } 1086 if (start < end) { 1087 log_info(cds, map)(PTR_FORMAT ": @@ Unused heap space " SIZE_FORMAT " bytes", 1088 p2i(to_requested(start)), size_t(end - start)); 1089 log_data(start, end, to_requested(start), /*is_heap=*/true); 1090 } 1091 } 1092 } 1093 static address to_requested(address p) { 1094 return HeapShared::to_requested_address(p); 1095 } 1096 #endif 1097 1098 // Log all the data [base...top). Pretend that the base address 1099 // will be mapped to requested_base at run-time. 1100 static void log_data(address base, address top, address requested_base, bool is_heap = false) { 1101 assert(top >= base, "must be"); 1102 1103 LogStreamHandle(Trace, cds, map) lsh; 1104 if (lsh.is_enabled()) { 1105 int unitsize = sizeof(address); 1106 if (is_heap && UseCompressedOops) { 1107 // This makes the compressed oop pointers easier to read, but 1108 // longs and doubles will be split into two words. 1109 unitsize = sizeof(narrowOop); 1110 } 1111 os::print_hex_dump(&lsh, base, top, unitsize, 32, requested_base); 1112 } 1113 } 1114 1115 static void log_header(FileMapInfo* mapinfo) { 1116 LogStreamHandle(Info, cds, map) lsh; 1117 if (lsh.is_enabled()) { 1118 mapinfo->print(&lsh); 1119 } 1120 } 1121 1122 public: 1123 static void log(ArchiveBuilder* builder, FileMapInfo* mapinfo, 1124 GrowableArray<MemRegion> *closed_heap_regions, 1125 GrowableArray<MemRegion> *open_heap_regions, 1126 char* bitmap, size_t bitmap_size_in_bytes) { 1127 log_info(cds, map)("%s CDS archive map for %s", DumpSharedSpaces ? "Static" : "Dynamic", mapinfo->full_path()); 1128 1129 address header = address(mapinfo->header()); 1130 address header_end = header + mapinfo->header()->header_size(); 1131 log_region("header", header, header_end, 0); 1132 log_header(mapinfo); 1133 log_data(header, header_end, 0); 1134 1135 DumpRegion* rw_region = &builder->_rw_region; 1136 DumpRegion* ro_region = &builder->_ro_region; 1137 1138 log_metaspace_region("rw region", rw_region, &builder->_rw_src_objs); 1139 log_metaspace_region("ro region", ro_region, &builder->_ro_src_objs); 1140 1141 address bitmap_end = address(bitmap + bitmap_size_in_bytes); 1142 log_region("bitmap", address(bitmap), bitmap_end, 0); 1143 log_data((address)bitmap, bitmap_end, 0); 1144 1145 #if INCLUDE_CDS_JAVA_HEAP 1146 if (closed_heap_regions != NULL) { 1147 log_heap_regions("closed heap region", closed_heap_regions); 1148 } 1149 if (open_heap_regions != NULL) { 1150 log_heap_regions("open heap region", open_heap_regions); 1151 } 1152 #endif 1153 1154 log_info(cds, map)("[End of CDS archive map]"); 1155 } 1156 }; // end ArchiveBuilder::CDSMapLogger 1157 1158 void ArchiveBuilder::print_stats() { 1159 _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used())); 1160 } 1161 1162 void ArchiveBuilder::clean_up_src_obj_table() { 1163 SrcObjTableCleaner cleaner; 1164 _src_obj_table.iterate(&cleaner); 1165 } 1166 1167 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, 1168 GrowableArray<MemRegion>* closed_heap_regions, 1169 GrowableArray<MemRegion>* open_heap_regions, 1170 GrowableArray<ArchiveHeapBitmapInfo>* closed_heap_bitmaps, 1171 GrowableArray<ArchiveHeapBitmapInfo>* open_heap_bitmaps) { 1172 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with 1173 // MetaspaceShared::n_regions (internal to hotspot). 1174 assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity"); 1175 1176 write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false); 1177 write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false); 1178 1179 size_t bitmap_size_in_bytes; 1180 char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap(), closed_heap_bitmaps, open_heap_bitmaps, 1181 bitmap_size_in_bytes); 1182 1183 if (closed_heap_regions != NULL) { 1184 _total_closed_heap_region_size = mapinfo->write_heap_regions( 1185 closed_heap_regions, 1186 closed_heap_bitmaps, 1187 MetaspaceShared::first_closed_heap_region, 1188 MetaspaceShared::max_num_closed_heap_regions); 1189 _total_open_heap_region_size = mapinfo->write_heap_regions( 1190 open_heap_regions, 1191 open_heap_bitmaps, 1192 MetaspaceShared::first_open_heap_region, 1193 MetaspaceShared::max_num_open_heap_regions); 1194 } 1195 1196 print_region_stats(mapinfo, closed_heap_regions, open_heap_regions); 1197 1198 mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address()); 1199 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 1200 // After this point, we should not write any data into mapinfo->header() since this 1201 // would corrupt its checksum we have calculated before. 1202 mapinfo->write_header(); 1203 mapinfo->close(); 1204 1205 if (log_is_enabled(Info, cds)) { 1206 print_stats(); 1207 } 1208 1209 if (log_is_enabled(Info, cds, map)) { 1210 CDSMapLogger::log(this, mapinfo, closed_heap_regions, open_heap_regions, 1211 bitmap, bitmap_size_in_bytes); 1212 } 1213 CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache()); 1214 FREE_C_HEAP_ARRAY(char, bitmap); 1215 } 1216 1217 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) { 1218 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec); 1219 } 1220 1221 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, 1222 GrowableArray<MemRegion>* closed_heap_regions, 1223 GrowableArray<MemRegion>* open_heap_regions) { 1224 // Print statistics of all the regions 1225 const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used(); 1226 const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned(); 1227 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() + 1228 bitmap_reserved + 1229 _total_closed_heap_region_size + 1230 _total_open_heap_region_size; 1231 const size_t total_bytes = _ro_region.used() + _rw_region.used() + 1232 bitmap_used + 1233 _total_closed_heap_region_size + 1234 _total_open_heap_region_size; 1235 const double total_u_perc = percent_of(total_bytes, total_reserved); 1236 1237 _rw_region.print(total_reserved); 1238 _ro_region.print(total_reserved); 1239 1240 print_bitmap_region_stats(bitmap_used, total_reserved); 1241 1242 if (closed_heap_regions != NULL) { 1243 print_heap_region_stats(closed_heap_regions, "ca", total_reserved); 1244 print_heap_region_stats(open_heap_regions, "oa", total_reserved); 1245 } 1246 1247 log_debug(cds)("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]", 1248 total_bytes, total_reserved, total_u_perc); 1249 } 1250 1251 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) { 1252 log_debug(cds)("bm space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]", 1253 size, size/double(total_size)*100.0, size); 1254 } 1255 1256 void ArchiveBuilder::print_heap_region_stats(GrowableArray<MemRegion>* regions, 1257 const char *name, size_t total_size) { 1258 int arr_len = regions == NULL ? 0 : regions->length(); 1259 for (int i = 0; i < arr_len; i++) { 1260 char* start = (char*)regions->at(i).start(); 1261 size_t size = regions->at(i).byte_size(); 1262 char* top = start + size; 1263 log_debug(cds)("%s%d space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used] at " INTPTR_FORMAT, 1264 name, i, size, size/double(total_size)*100.0, size, p2i(start)); 1265 } 1266 } 1267 1268 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) { 1269 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space. 1270 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes 1271 // or so. 1272 _rw_region.print_out_of_space_msg(name, needed_bytes); 1273 _ro_region.print_out_of_space_msg(name, needed_bytes); 1274 1275 vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name), 1276 "Please reduce the number of shared classes."); 1277 } 1278 1279 1280 #ifndef PRODUCT 1281 void ArchiveBuilder::assert_is_vm_thread() { 1282 assert(Thread::current()->is_VM_thread(), "ArchiveBuilder should be used only inside the VMThread"); 1283 } 1284 #endif