1 /* 2 * Copyright (c) 2012, 2020, 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 "classfile/classLoaderDataGraph.hpp" 27 #include "classfile/classListParser.hpp" 28 #include "classfile/classLoaderExt.hpp" 29 #include "classfile/loaderConstraints.hpp" 30 #include "classfile/javaClasses.inline.hpp" 31 #include "classfile/placeholders.hpp" 32 #include "classfile/symbolTable.hpp" 33 #include "classfile/stringTable.hpp" 34 #include "classfile/systemDictionary.hpp" 35 #include "classfile/systemDictionaryShared.hpp" 36 #include "code/codeCache.hpp" 37 #include "interpreter/bytecodeStream.hpp" 38 #include "interpreter/bytecodes.hpp" 39 #include "logging/log.hpp" 40 #include "logging/logMessage.hpp" 41 #include "memory/archiveBuilder.hpp" 42 #include "memory/archiveUtils.inline.hpp" 43 #include "memory/dynamicArchive.hpp" 44 #include "memory/filemap.hpp" 45 #include "memory/heapShared.inline.hpp" 46 #include "memory/metaspace.hpp" 47 #include "memory/metaspaceClosure.hpp" 48 #include "memory/metaspaceShared.hpp" 49 #include "memory/resourceArea.hpp" 50 #include "memory/universe.hpp" 51 #include "oops/compressedOops.inline.hpp" 52 #include "oops/instanceClassLoaderKlass.hpp" 53 #include "oops/instanceMirrorKlass.hpp" 54 #include "oops/instanceRefKlass.hpp" 55 #include "oops/methodData.hpp" 56 #include "oops/objArrayKlass.hpp" 57 #include "oops/objArrayOop.hpp" 58 #include "oops/oop.inline.hpp" 59 #include "oops/typeArrayKlass.hpp" 60 #include "runtime/handles.inline.hpp" 61 #include "runtime/os.hpp" 62 #include "runtime/safepointVerifiers.hpp" 63 #include "runtime/timerTrace.hpp" 64 #include "runtime/vmThread.hpp" 65 #include "runtime/vmOperations.hpp" 66 #include "utilities/align.hpp" 67 #include "utilities/bitMap.inline.hpp" 68 #include "utilities/ostream.hpp" 69 #include "utilities/defaultStream.hpp" 70 #include "utilities/hashtable.inline.hpp" 71 #if INCLUDE_G1GC 72 #include "gc/g1/g1CollectedHeap.hpp" 73 #endif 74 75 ReservedSpace MetaspaceShared::_shared_rs; 76 VirtualSpace MetaspaceShared::_shared_vs; 77 ReservedSpace MetaspaceShared::_symbol_rs; 78 VirtualSpace MetaspaceShared::_symbol_vs; 79 MetaspaceSharedStats MetaspaceShared::_stats; 80 bool MetaspaceShared::_has_error_classes; 81 bool MetaspaceShared::_archive_loading_failed = false; 82 bool MetaspaceShared::_remapped_readwrite = false; 83 address MetaspaceShared::_i2i_entry_code_buffers = NULL; 84 size_t MetaspaceShared::_i2i_entry_code_buffers_size = 0; 85 void* MetaspaceShared::_shared_metaspace_static_top = NULL; 86 intx MetaspaceShared::_relocation_delta; 87 char* MetaspaceShared::_requested_base_address; 88 bool MetaspaceShared::_use_optimized_module_handling = true; 89 90 // The CDS archive is divided into the following regions: 91 // mc - misc code (the method entry trampolines, c++ vtables) 92 // rw - read-write metadata 93 // ro - read-only metadata and read-only tables 94 // 95 // ca0 - closed archive heap space #0 96 // ca1 - closed archive heap space #1 (may be empty) 97 // oa0 - open archive heap space #0 98 // oa1 - open archive heap space #1 (may be empty) 99 // 100 // The mc, rw, and ro regions are linearly allocated, starting from 101 // SharedBaseAddress, in the order of mc->rw->ro. The size of these 3 regions 102 // are page-aligned, and there's no gap between any consecutive regions. 103 // 104 // These 3 regions are populated in the following steps: 105 // [1] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are 106 // temporarily allocated outside of the shared regions. Only the method entry 107 // trampolines are written into the mc region. 108 // [2] C++ vtables are copied into the mc region. 109 // [3] ArchiveCompactor copies RW metadata into the rw region. 110 // [4] ArchiveCompactor copies RO metadata into the ro region. 111 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data 112 // are copied into the ro region as read-only tables. 113 // 114 // The s0/s1 and oa0/oa1 regions are populated inside HeapShared::archive_java_heap_objects. 115 // Their layout is independent of the other 4 regions. 116 117 char* DumpRegion::expand_top_to(char* newtop) { 118 assert(is_allocatable(), "must be initialized and not packed"); 119 assert(newtop >= _top, "must not grow backwards"); 120 if (newtop > _end) { 121 MetaspaceShared::report_out_of_space(_name, newtop - _top); 122 ShouldNotReachHere(); 123 } 124 125 if (_rs == MetaspaceShared::shared_rs()) { 126 uintx delta; 127 if (DynamicDumpSharedSpaces) { 128 delta = DynamicArchive::object_delta_uintx(newtop); 129 } else { 130 delta = MetaspaceShared::object_delta_uintx(newtop); 131 } 132 if (delta > MAX_SHARED_DELTA) { 133 // This is just a sanity check and should not appear in any real world usage. This 134 // happens only if you allocate more than 2GB of shared objects and would require 135 // millions of shared classes. 136 vm_exit_during_initialization("Out of memory in the CDS archive", 137 "Please reduce the number of shared classes."); 138 } 139 } 140 141 MetaspaceShared::commit_to(_rs, _vs, newtop); 142 _top = newtop; 143 return _top; 144 } 145 146 char* DumpRegion::allocate(size_t num_bytes, size_t alignment) { 147 char* p = (char*)align_up(_top, alignment); 148 char* newtop = p + align_up(num_bytes, alignment); 149 expand_top_to(newtop); 150 memset(p, 0, newtop - p); 151 return p; 152 } 153 154 void DumpRegion::append_intptr_t(intptr_t n, bool need_to_mark) { 155 assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment"); 156 intptr_t *p = (intptr_t*)_top; 157 char* newtop = _top + sizeof(intptr_t); 158 expand_top_to(newtop); 159 *p = n; 160 if (need_to_mark) { 161 ArchivePtrMarker::mark_pointer(p); 162 } 163 } 164 165 void DumpRegion::print(size_t total_bytes) const { 166 log_debug(cds)("%-3s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT, 167 _name, used(), percent_of(used(), total_bytes), reserved(), percent_of(used(), reserved()), 168 p2i(_base + MetaspaceShared::final_delta())); 169 } 170 171 void DumpRegion::print_out_of_space_msg(const char* failing_region, size_t needed_bytes) { 172 log_error(cds)("[%-8s] " PTR_FORMAT " - " PTR_FORMAT " capacity =%9d, allocated =%9d", 173 _name, p2i(_base), p2i(_top), int(_end - _base), int(_top - _base)); 174 if (strcmp(_name, failing_region) == 0) { 175 log_error(cds)(" required = %d", int(needed_bytes)); 176 } 177 } 178 179 void DumpRegion::init(ReservedSpace* rs, VirtualSpace* vs) { 180 _rs = rs; 181 _vs = vs; 182 // Start with 0 committed bytes. The memory will be committed as needed by 183 // MetaspaceShared::commit_to(). 184 if (!_vs->initialize(*_rs, 0)) { 185 fatal("Unable to allocate memory for shared space"); 186 } 187 _base = _top = _rs->base(); 188 _end = _rs->end(); 189 } 190 191 void DumpRegion::pack(DumpRegion* next) { 192 assert(!is_packed(), "sanity"); 193 _end = (char*)align_up(_top, MetaspaceShared::reserved_space_alignment()); 194 _is_packed = true; 195 if (next != NULL) { 196 next->_rs = _rs; 197 next->_vs = _vs; 198 next->_base = next->_top = this->_end; 199 next->_end = _rs->end(); 200 } 201 } 202 203 static DumpRegion _mc_region("mc"), _ro_region("ro"), _rw_region("rw"), _symbol_region("symbols"); 204 static size_t _total_closed_archive_region_size = 0, _total_open_archive_region_size = 0; 205 206 void MetaspaceShared::init_shared_dump_space(DumpRegion* first_space) { 207 first_space->init(&_shared_rs, &_shared_vs); 208 } 209 210 DumpRegion* MetaspaceShared::misc_code_dump_space() { 211 return &_mc_region; 212 } 213 214 DumpRegion* MetaspaceShared::read_write_dump_space() { 215 return &_rw_region; 216 } 217 218 DumpRegion* MetaspaceShared::read_only_dump_space() { 219 return &_ro_region; 220 } 221 222 void MetaspaceShared::pack_dump_space(DumpRegion* current, DumpRegion* next, 223 ReservedSpace* rs) { 224 current->pack(next); 225 } 226 227 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) { 228 return _symbol_region.allocate(num_bytes); 229 } 230 231 char* MetaspaceShared::misc_code_space_alloc(size_t num_bytes) { 232 return _mc_region.allocate(num_bytes); 233 } 234 235 char* MetaspaceShared::read_only_space_alloc(size_t num_bytes) { 236 return _ro_region.allocate(num_bytes); 237 } 238 239 size_t MetaspaceShared::reserved_space_alignment() { return os::vm_allocation_granularity(); } 240 241 static bool shared_base_valid(char* shared_base) { 242 #ifdef _LP64 243 return CompressedKlassPointers::is_valid_base((address)shared_base); 244 #else 245 return true; 246 #endif 247 } 248 249 static bool shared_base_too_high(char* shared_base, size_t cds_total) { 250 if (SharedBaseAddress != 0 && shared_base < (char*)SharedBaseAddress) { 251 // SharedBaseAddress is very high (e.g., 0xffffffffffffff00) so 252 // align_up(SharedBaseAddress, MetaspaceShared::reserved_space_alignment()) has wrapped around. 253 return true; 254 } 255 if (max_uintx - uintx(shared_base) < uintx(cds_total)) { 256 // The end of the archive will wrap around 257 return true; 258 } 259 260 return false; 261 } 262 263 static char* compute_shared_base(size_t cds_total) { 264 char* shared_base = (char*)align_up((char*)SharedBaseAddress, MetaspaceShared::reserved_space_alignment()); 265 const char* err = NULL; 266 if (shared_base_too_high(shared_base, cds_total)) { 267 err = "too high"; 268 } else if (!shared_base_valid(shared_base)) { 269 err = "invalid for this platform"; 270 } 271 if (err) { 272 log_warning(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is %s. Reverted to " INTPTR_FORMAT, 273 p2i((void*)SharedBaseAddress), err, 274 p2i((void*)Arguments::default_SharedBaseAddress())); 275 SharedBaseAddress = Arguments::default_SharedBaseAddress(); 276 shared_base = (char*)align_up((char*)SharedBaseAddress, MetaspaceShared::reserved_space_alignment()); 277 } 278 assert(!shared_base_too_high(shared_base, cds_total) && shared_base_valid(shared_base), "Sanity"); 279 return shared_base; 280 } 281 282 void MetaspaceShared::initialize_dumptime_shared_and_meta_spaces() { 283 assert(DumpSharedSpaces, "should be called for dump time only"); 284 285 const size_t reserve_alignment = MetaspaceShared::reserved_space_alignment(); 286 287 #ifdef _LP64 288 // On 64-bit VM we reserve a 4G range and, if UseCompressedClassPointers=1, 289 // will use that to house both the archives and the ccs. See below for 290 // details. 291 const uint64_t UnscaledClassSpaceMax = (uint64_t(max_juint) + 1); 292 const size_t cds_total = align_down(UnscaledClassSpaceMax, reserve_alignment); 293 #else 294 // We don't support archives larger than 256MB on 32-bit due to limited 295 // virtual address space. 296 size_t cds_total = align_down(256*M, reserve_alignment); 297 #endif 298 299 char* shared_base = compute_shared_base(cds_total); 300 _requested_base_address = shared_base; 301 302 // Whether to use SharedBaseAddress as attach address. 303 bool use_requested_base = true; 304 305 if (shared_base == NULL) { 306 use_requested_base = false; 307 } 308 309 if (ArchiveRelocationMode == 1) { 310 log_info(cds)("ArchiveRelocationMode == 1: always allocate class space at an alternative address"); 311 use_requested_base = false; 312 } 313 314 // First try to reserve the space at the specified SharedBaseAddress. 315 assert(!_shared_rs.is_reserved(), "must be"); 316 if (use_requested_base) { 317 _shared_rs = ReservedSpace(cds_total, reserve_alignment, 318 false /* large */, (char*)shared_base); 319 if (_shared_rs.is_reserved()) { 320 assert(_shared_rs.base() == shared_base, "should match"); 321 } else { 322 log_info(cds)("dumptime space reservation: failed to map at " 323 "SharedBaseAddress " PTR_FORMAT, p2i(shared_base)); 324 } 325 } 326 if (!_shared_rs.is_reserved()) { 327 // Get a reserved space anywhere if attaching at the SharedBaseAddress 328 // fails: 329 if (UseCompressedClassPointers) { 330 // If we need to reserve class space as well, let the platform handle 331 // the reservation. 332 LP64_ONLY(_shared_rs = 333 Metaspace::reserve_address_space_for_compressed_classes(cds_total);) 334 NOT_LP64(ShouldNotReachHere();) 335 } else { 336 // anywhere is fine. 337 _shared_rs = ReservedSpace(cds_total, reserve_alignment, 338 false /* large */, (char*)NULL); 339 } 340 } 341 342 if (!_shared_rs.is_reserved()) { 343 vm_exit_during_initialization("Unable to reserve memory for shared space", 344 err_msg(SIZE_FORMAT " bytes.", cds_total)); 345 } 346 347 #ifdef _LP64 348 349 if (UseCompressedClassPointers) { 350 351 assert(CompressedKlassPointers::is_valid_base((address)_shared_rs.base()), "Sanity"); 352 353 // On 64-bit VM, if UseCompressedClassPointers=1, the compressed class space 354 // must be allocated near the cds such as that the compressed Klass pointer 355 // encoding can be used to en/decode pointers from both cds and ccs. Since 356 // Metaspace cannot do this (it knows nothing about cds), we do it for 357 // Metaspace here and pass it the space to use for ccs. 358 // 359 // We do this by reserving space for the ccs behind the archives. Note 360 // however that ccs follows a different alignment 361 // (Metaspace::reserve_alignment), so there may be a gap between ccs and 362 // cds. 363 // We use a similar layout at runtime, see reserve_address_space_for_archives(). 364 // 365 // +-- SharedBaseAddress (default = 0x800000000) 366 // v 367 // +-..---------+---------+ ... +----+----+----+--------+-----------------+ 368 // | Heap | Archive | | MC | RW | RO | [gap] | class space | 369 // +-..---------+---------+ ... +----+----+----+--------+-----------------+ 370 // |<-- MaxHeapSize -->| |<-- UnscaledClassSpaceMax = 4GB -->| 371 // 372 // Note: ccs must follow the archives, and the archives must start at the 373 // encoding base. However, the exact placement of ccs does not matter as 374 // long as it it resides in the encoding range of CompressedKlassPointers 375 // and comes after the archive. 376 // 377 // We do this by splitting up the allocated 4G into 3G of archive space, 378 // followed by 1G for the ccs: 379 // + The upper 1 GB is used as the "temporary compressed class space" 380 // -- preload_classes() will store Klasses into this space. 381 // + The lower 3 GB is used for the archive -- when preload_classes() 382 // is done, ArchiveCompactor will copy the class metadata into this 383 // space, first the RW parts, then the RO parts. 384 385 // Starting address of ccs must be aligned to Metaspace::reserve_alignment()... 386 size_t class_space_size = align_down(_shared_rs.size() / 4, Metaspace::reserve_alignment()); 387 address class_space_start = (address)align_down(_shared_rs.end() - class_space_size, Metaspace::reserve_alignment()); 388 size_t archive_size = class_space_start - (address)_shared_rs.base(); 389 390 ReservedSpace tmp_class_space = _shared_rs.last_part(archive_size); 391 _shared_rs = _shared_rs.first_part(archive_size); 392 393 // ... as does the size of ccs. 394 tmp_class_space = tmp_class_space.first_part(class_space_size); 395 CompressedClassSpaceSize = class_space_size; 396 397 // Let Metaspace initialize ccs 398 Metaspace::initialize_class_space(tmp_class_space); 399 400 // and set up CompressedKlassPointers encoding. 401 CompressedKlassPointers::initialize((address)_shared_rs.base(), cds_total); 402 403 log_info(cds)("narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", 404 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift()); 405 406 log_info(cds)("Allocated temporary class space: " SIZE_FORMAT " bytes at " PTR_FORMAT, 407 CompressedClassSpaceSize, p2i(tmp_class_space.base())); 408 409 assert(_shared_rs.end() == tmp_class_space.base() && 410 is_aligned(_shared_rs.base(), MetaspaceShared::reserved_space_alignment()) && 411 is_aligned(tmp_class_space.base(), Metaspace::reserve_alignment()) && 412 is_aligned(tmp_class_space.size(), Metaspace::reserve_alignment()), "Sanity"); 413 } 414 415 #endif 416 417 init_shared_dump_space(&_mc_region); 418 SharedBaseAddress = (size_t)_shared_rs.base(); 419 log_info(cds)("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT, 420 _shared_rs.size(), p2i(_shared_rs.base())); 421 422 // We don't want any valid object to be at the very bottom of the archive. 423 // See ArchivePtrMarker::mark_pointer(). 424 MetaspaceShared::misc_code_space_alloc(16); 425 426 size_t symbol_rs_size = LP64_ONLY(3 * G) NOT_LP64(128 * M); 427 _symbol_rs = ReservedSpace(symbol_rs_size); 428 if (!_symbol_rs.is_reserved()) { 429 vm_exit_during_initialization("Unable to reserve memory for symbols", 430 err_msg(SIZE_FORMAT " bytes.", symbol_rs_size)); 431 } 432 _symbol_region.init(&_symbol_rs, &_symbol_vs); 433 } 434 435 // Called by universe_post_init() 436 void MetaspaceShared::post_initialize(TRAPS) { 437 if (UseSharedSpaces) { 438 int size = FileMapInfo::get_number_of_shared_paths(); 439 if (size > 0) { 440 SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD); 441 if (!DynamicDumpSharedSpaces) { 442 FileMapInfo* info; 443 if (FileMapInfo::dynamic_info() == NULL) { 444 info = FileMapInfo::current_info(); 445 } else { 446 info = FileMapInfo::dynamic_info(); 447 } 448 ClassLoaderExt::init_paths_start_index(info->app_class_paths_start_index()); 449 ClassLoaderExt::init_app_module_paths_start_index(info->app_module_paths_start_index()); 450 } 451 } 452 } 453 } 454 455 static GrowableArrayCHeap<Handle, mtClassShared>* _extra_interned_strings = NULL; 456 457 void MetaspaceShared::read_extra_data(const char* filename, TRAPS) { 458 _extra_interned_strings = new GrowableArrayCHeap<Handle, mtClassShared>(10000); 459 460 HashtableTextDump reader(filename); 461 reader.check_version("VERSION: 1.0"); 462 463 while (reader.remain() > 0) { 464 int utf8_length; 465 int prefix_type = reader.scan_prefix(&utf8_length); 466 ResourceMark rm(THREAD); 467 if (utf8_length == 0x7fffffff) { 468 // buf_len will overflown 32-bit value. 469 vm_exit_during_initialization(err_msg("string length too large: %d", utf8_length)); 470 } 471 int buf_len = utf8_length+1; 472 char* utf8_buffer = NEW_RESOURCE_ARRAY(char, buf_len); 473 reader.get_utf8(utf8_buffer, utf8_length); 474 utf8_buffer[utf8_length] = '\0'; 475 476 if (prefix_type == HashtableTextDump::SymbolPrefix) { 477 SymbolTable::new_permanent_symbol(utf8_buffer); 478 } else{ 479 assert(prefix_type == HashtableTextDump::StringPrefix, "Sanity"); 480 oop s = StringTable::intern(utf8_buffer, THREAD); 481 482 if (HAS_PENDING_EXCEPTION) { 483 log_warning(cds, heap)("[line %d] extra interned string allocation failed; size too large: %d", 484 reader.last_line_no(), utf8_length); 485 CLEAR_PENDING_EXCEPTION; 486 } else { 487 #if INCLUDE_G1GC 488 if (UseG1GC) { 489 typeArrayOop body = java_lang_String::value(s); 490 const HeapRegion* hr = G1CollectedHeap::heap()->heap_region_containing(body); 491 if (hr->is_humongous()) { 492 // Don't keep it alive, so it will be GC'ed before we dump the strings, in order 493 // to maximize free heap space and minimize fragmentation. 494 log_warning(cds, heap)("[line %d] extra interned string ignored; size too large: %d", 495 reader.last_line_no(), utf8_length); 496 continue; 497 } 498 } 499 #endif 500 // Interned strings are GC'ed if there are no references to it, so let's 501 // add a reference to keep this string alive. 502 assert(s != NULL, "must succeed"); 503 Handle h(THREAD, s); 504 _extra_interned_strings->append(h); 505 } 506 } 507 } 508 } 509 510 void MetaspaceShared::commit_to(ReservedSpace* rs, VirtualSpace* vs, char* newtop) { 511 Arguments::assert_is_dumping_archive(); 512 char* base = rs->base(); 513 size_t need_committed_size = newtop - base; 514 size_t has_committed_size = vs->committed_size(); 515 if (need_committed_size < has_committed_size) { 516 return; 517 } 518 519 size_t min_bytes = need_committed_size - has_committed_size; 520 size_t preferred_bytes = 1 * M; 521 size_t uncommitted = vs->reserved_size() - has_committed_size; 522 523 size_t commit =MAX2(min_bytes, preferred_bytes); 524 commit = MIN2(commit, uncommitted); 525 assert(commit <= uncommitted, "sanity"); 526 527 bool result = vs->expand_by(commit, false); 528 if (rs == &_shared_rs) { 529 ArchivePtrMarker::expand_ptr_end((address*)vs->high()); 530 } 531 532 if (!result) { 533 vm_exit_during_initialization(err_msg("Failed to expand shared space to " SIZE_FORMAT " bytes", 534 need_committed_size)); 535 } 536 537 assert(rs == &_shared_rs || rs == &_symbol_rs, "must be"); 538 const char* which = (rs == &_shared_rs) ? "shared" : "symbol"; 539 log_debug(cds)("Expanding %s spaces by " SIZE_FORMAT_W(7) " bytes [total " SIZE_FORMAT_W(9) " bytes ending at %p]", 540 which, commit, vs->actual_committed_size(), vs->high()); 541 } 542 543 void MetaspaceShared::initialize_ptr_marker(CHeapBitMap* ptrmap) { 544 ArchivePtrMarker::initialize(ptrmap, (address*)_shared_vs.low(), (address*)_shared_vs.high()); 545 } 546 547 // Read/write a data stream for restoring/preserving metadata pointers and 548 // miscellaneous data from/to the shared archive file. 549 550 void MetaspaceShared::serialize(SerializeClosure* soc) { 551 int tag = 0; 552 soc->do_tag(--tag); 553 554 // Verify the sizes of various metadata in the system. 555 soc->do_tag(sizeof(Method)); 556 soc->do_tag(sizeof(ConstMethod)); 557 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); 558 soc->do_tag(sizeof(ConstantPool)); 559 soc->do_tag(sizeof(ConstantPoolCache)); 560 soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); 561 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); 562 soc->do_tag(sizeof(Symbol)); 563 564 // Dump/restore miscellaneous metadata. 565 JavaClasses::serialize_offsets(soc); 566 Universe::serialize(soc); 567 soc->do_tag(--tag); 568 569 // Dump/restore references to commonly used names and signatures. 570 vmSymbols::serialize(soc); 571 soc->do_tag(--tag); 572 573 // Dump/restore the symbol/string/subgraph_info tables 574 SymbolTable::serialize_shared_table_header(soc); 575 StringTable::serialize_shared_table_header(soc); 576 HeapShared::serialize_subgraph_info_table_header(soc); 577 SystemDictionaryShared::serialize_dictionary_headers(soc); 578 579 InstanceMirrorKlass::serialize_offsets(soc); 580 581 // Dump/restore well known classes (pointers) 582 SystemDictionaryShared::serialize_well_known_klasses(soc); 583 soc->do_tag(--tag); 584 585 serialize_cloned_cpp_vtptrs(soc); 586 soc->do_tag(--tag); 587 588 soc->do_tag(666); 589 } 590 591 address MetaspaceShared::i2i_entry_code_buffers(size_t total_size) { 592 if (DumpSharedSpaces) { 593 if (_i2i_entry_code_buffers == NULL) { 594 _i2i_entry_code_buffers = (address)misc_code_space_alloc(total_size); 595 _i2i_entry_code_buffers_size = total_size; 596 } 597 } else if (UseSharedSpaces) { 598 assert(_i2i_entry_code_buffers != NULL, "must already been initialized"); 599 } else { 600 return NULL; 601 } 602 603 assert(_i2i_entry_code_buffers_size == total_size, "must not change"); 604 return _i2i_entry_code_buffers; 605 } 606 607 uintx MetaspaceShared::object_delta_uintx(void* obj) { 608 Arguments::assert_is_dumping_archive(); 609 if (DumpSharedSpaces) { 610 assert(shared_rs()->contains(obj), "must be"); 611 } else { 612 assert(is_in_shared_metaspace(obj) || DynamicArchive::is_in_target_space(obj), "must be"); 613 } 614 address base_address = address(SharedBaseAddress); 615 uintx deltax = address(obj) - base_address; 616 return deltax; 617 } 618 619 // Global object for holding classes that have been loaded. Since this 620 // is run at a safepoint just before exit, this is the entire set of classes. 621 static GrowableArray<Klass*>* _global_klass_objects; 622 623 GrowableArray<Klass*>* MetaspaceShared::collected_klasses() { 624 return _global_klass_objects; 625 } 626 627 static void remove_unshareable_in_classes() { 628 for (int i = 0; i < _global_klass_objects->length(); i++) { 629 Klass* k = _global_klass_objects->at(i); 630 if (!k->is_objArray_klass()) { 631 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info 632 // on their array classes. 633 assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be"); 634 k->remove_unshareable_info(); 635 } 636 } 637 } 638 639 static void remove_java_mirror_in_classes() { 640 for (int i = 0; i < _global_klass_objects->length(); i++) { 641 Klass* k = _global_klass_objects->at(i); 642 if (!k->is_objArray_klass()) { 643 // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info 644 // on their array classes. 645 assert(k->is_instance_klass() || k->is_typeArray_klass(), "must be"); 646 k->remove_java_mirror(); 647 } 648 } 649 } 650 651 static void rewrite_nofast_bytecode(const methodHandle& method) { 652 BytecodeStream bcs(method); 653 while (!bcs.is_last_bytecode()) { 654 Bytecodes::Code opcode = bcs.next(); 655 switch (opcode) { 656 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; 657 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; 658 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; 659 case Bytecodes::_iload: { 660 if (!bcs.is_wide()) { 661 *bcs.bcp() = Bytecodes::_nofast_iload; 662 } 663 break; 664 } 665 default: break; 666 } 667 } 668 } 669 670 // Walk all methods in the class list to ensure that they won't be modified at 671 // run time. This includes: 672 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified 673 // at run time by RewriteBytecodes/RewriteFrequentPairs 674 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time. 675 static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread) { 676 for (int i = 0; i < _global_klass_objects->length(); i++) { 677 Klass* k = _global_klass_objects->at(i); 678 if (k->is_instance_klass()) { 679 InstanceKlass* ik = InstanceKlass::cast(k); 680 MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(thread, ik); 681 } 682 } 683 } 684 685 void MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik) { 686 for (int i = 0; i < ik->methods()->length(); i++) { 687 methodHandle m(thread, ik->methods()->at(i)); 688 rewrite_nofast_bytecode(m); 689 Fingerprinter fp(m); 690 // The side effect of this call sets method's fingerprint field. 691 fp.fingerprint(); 692 } 693 } 694 695 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables. 696 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.) 697 // 698 // Addresses of the vtables and the methods may be different across JVM runs, 699 // if libjvm.so is dynamically loaded at a different base address. 700 // 701 // To ensure that the Metadata objects in the CDS archive always have the correct vtable: 702 // 703 // + at dump time: we redirect the _vptr to point to our own vtables inside 704 // the CDS image 705 // + at run time: we clone the actual contents of the vtables from libjvm.so 706 // into our own tables. 707 708 // Currently, the archive contain ONLY the following types of objects that have C++ vtables. 709 #define CPP_VTABLE_PATCH_TYPES_DO(f) \ 710 f(ConstantPool) \ 711 f(InstanceKlass) \ 712 f(InstanceClassLoaderKlass) \ 713 f(InstanceMirrorKlass) \ 714 f(InstanceRefKlass) \ 715 f(Method) \ 716 f(ObjArrayKlass) \ 717 f(TypeArrayKlass) 718 719 class CppVtableInfo { 720 intptr_t _vtable_size; 721 intptr_t _cloned_vtable[1]; 722 public: 723 static int num_slots(int vtable_size) { 724 return 1 + vtable_size; // Need to add the space occupied by _vtable_size; 725 } 726 int vtable_size() { return int(uintx(_vtable_size)); } 727 void set_vtable_size(int n) { _vtable_size = intptr_t(n); } 728 intptr_t* cloned_vtable() { return &_cloned_vtable[0]; } 729 void zero() { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); } 730 // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo 731 static size_t byte_size(int vtable_size) { 732 CppVtableInfo i; 733 return pointer_delta(&i._cloned_vtable[vtable_size], &i, sizeof(u1)); 734 } 735 }; 736 737 static inline intptr_t* vtable_of(Metadata* m) { 738 return *((intptr_t**)m); 739 } 740 741 template <class T> class CppVtableCloner : public T { 742 static CppVtableInfo* _info; 743 744 static int get_vtable_length(const char* name); 745 746 public: 747 // Allocate and initialize the C++ vtable, starting from top, but do not go past end. 748 static intptr_t* allocate(const char* name); 749 750 // Clone the vtable to ... 751 static intptr_t* clone_vtable(const char* name, CppVtableInfo* info); 752 753 static void zero_vtable_clone() { 754 assert(DumpSharedSpaces, "dump-time only"); 755 _info->zero(); 756 } 757 758 static bool is_valid_shared_object(const T* obj) { 759 intptr_t* vptr = *(intptr_t**)obj; 760 return vptr == _info->cloned_vtable(); 761 } 762 763 static void init_orig_cpp_vtptr(int kind); 764 }; 765 766 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL; 767 768 template <class T> 769 intptr_t* CppVtableCloner<T>::allocate(const char* name) { 770 assert(is_aligned(_mc_region.top(), sizeof(intptr_t)), "bad alignment"); 771 int n = get_vtable_length(name); 772 _info = (CppVtableInfo*)_mc_region.allocate(CppVtableInfo::byte_size(n), sizeof(intptr_t)); 773 _info->set_vtable_size(n); 774 775 intptr_t* p = clone_vtable(name, _info); 776 assert((char*)p == _mc_region.top(), "must be"); 777 778 return _info->cloned_vtable(); 779 } 780 781 template <class T> 782 intptr_t* CppVtableCloner<T>::clone_vtable(const char* name, CppVtableInfo* info) { 783 if (!DumpSharedSpaces) { 784 assert(_info == 0, "_info is initialized only at dump time"); 785 _info = info; // Remember it -- it will be used by MetaspaceShared::is_valid_shared_method() 786 } 787 T tmp; // Allocate temporary dummy metadata object to get to the original vtable. 788 int n = info->vtable_size(); 789 intptr_t* srcvtable = vtable_of(&tmp); 790 intptr_t* dstvtable = info->cloned_vtable(); 791 792 // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are 793 // safe to do memcpy. 794 log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name); 795 memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n); 796 return dstvtable + n; 797 } 798 799 // To determine the size of the vtable for each type, we use the following 800 // trick by declaring 2 subclasses: 801 // 802 // class CppVtableTesterA: public InstanceKlass {virtual int last_virtual_method() {return 1;} }; 803 // class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; }; 804 // 805 // CppVtableTesterA and CppVtableTesterB's vtables have the following properties: 806 // - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N) 807 // - The first N entries have are exactly the same as in InstanceKlass's vtable. 808 // - Their last entry is different. 809 // 810 // So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables 811 // and find the first entry that's different. 812 // 813 // This works on all C++ compilers supported by Oracle, but you may need to tweak it for more 814 // esoteric compilers. 815 816 template <class T> class CppVtableTesterB: public T { 817 public: 818 virtual int last_virtual_method() {return 1;} 819 }; 820 821 template <class T> class CppVtableTesterA : public T { 822 public: 823 virtual void* last_virtual_method() { 824 // Make this different than CppVtableTesterB::last_virtual_method so the C++ 825 // compiler/linker won't alias the two functions. 826 return NULL; 827 } 828 }; 829 830 template <class T> 831 int CppVtableCloner<T>::get_vtable_length(const char* name) { 832 CppVtableTesterA<T> a; 833 CppVtableTesterB<T> b; 834 835 intptr_t* avtable = vtable_of(&a); 836 intptr_t* bvtable = vtable_of(&b); 837 838 // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc) 839 int vtable_len = 1; 840 for (; ; vtable_len++) { 841 if (avtable[vtable_len] != bvtable[vtable_len]) { 842 break; 843 } 844 } 845 log_debug(cds, vtables)("Found %3d vtable entries for %s", vtable_len, name); 846 847 return vtable_len; 848 } 849 850 #define ALLOC_CPP_VTABLE_CLONE(c) \ 851 _cloned_cpp_vtptrs[c##_Kind] = CppVtableCloner<c>::allocate(#c); \ 852 ArchivePtrMarker::mark_pointer(&_cloned_cpp_vtptrs[c##_Kind]); 853 854 #define CLONE_CPP_VTABLE(c) \ 855 p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p); 856 857 #define ZERO_CPP_VTABLE(c) \ 858 CppVtableCloner<c>::zero_vtable_clone(); 859 860 #define INIT_ORIG_CPP_VTPTRS(c) \ 861 CppVtableCloner<c>::init_orig_cpp_vtptr(c##_Kind); 862 863 #define DECLARE_CLONED_VTABLE_KIND(c) c ## _Kind, 864 865 enum ClonedVtableKind { 866 // E.g., ConstantPool_Kind == 0, InstanceKlass_Kind == 1, etc. 867 CPP_VTABLE_PATCH_TYPES_DO(DECLARE_CLONED_VTABLE_KIND) 868 _num_cloned_vtable_kinds 869 }; 870 871 // This is a map of all the original vtptrs. E.g., for 872 // ConstantPool *cp = new (...) ConstantPool(...) ; // a dynamically allocated constant pool 873 // the following holds true: 874 // _orig_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0] 875 static intptr_t* _orig_cpp_vtptrs[_num_cloned_vtable_kinds]; 876 static bool _orig_cpp_vtptrs_inited = false; 877 878 template <class T> 879 void CppVtableCloner<T>::init_orig_cpp_vtptr(int kind) { 880 assert(kind < _num_cloned_vtable_kinds, "sanity"); 881 T tmp; // Allocate temporary dummy metadata object to get to the original vtable. 882 intptr_t* srcvtable = vtable_of(&tmp); 883 _orig_cpp_vtptrs[kind] = srcvtable; 884 } 885 886 // This is the index of all the cloned vtables. E.g., for 887 // ConstantPool* cp = ....; // an archived constant pool 888 // InstanceKlass* ik = ....;// an archived class 889 // the following holds true: 890 // _cloned_cpp_vtptrs[ConstantPool_Kind] == ((intptr_t**)cp)[0] 891 // _cloned_cpp_vtptrs[InstanceKlass_Kind] == ((intptr_t**)ik)[0] 892 static intptr_t** _cloned_cpp_vtptrs = NULL; 893 894 void MetaspaceShared::allocate_cloned_cpp_vtptrs() { 895 assert(DumpSharedSpaces, "must"); 896 size_t vtptrs_bytes = _num_cloned_vtable_kinds * sizeof(intptr_t*); 897 _cloned_cpp_vtptrs = (intptr_t**)_mc_region.allocate(vtptrs_bytes, sizeof(intptr_t*)); 898 } 899 900 void MetaspaceShared::serialize_cloned_cpp_vtptrs(SerializeClosure* soc) { 901 soc->do_ptr((void**)&_cloned_cpp_vtptrs); 902 } 903 904 intptr_t* MetaspaceShared::get_archived_cpp_vtable(MetaspaceObj::Type msotype, address obj) { 905 if (!_orig_cpp_vtptrs_inited) { 906 CPP_VTABLE_PATCH_TYPES_DO(INIT_ORIG_CPP_VTPTRS); 907 _orig_cpp_vtptrs_inited = true; 908 } 909 910 Arguments::assert_is_dumping_archive(); 911 int kind = -1; 912 switch (msotype) { 913 case MetaspaceObj::SymbolType: 914 case MetaspaceObj::TypeArrayU1Type: 915 case MetaspaceObj::TypeArrayU2Type: 916 case MetaspaceObj::TypeArrayU4Type: 917 case MetaspaceObj::TypeArrayU8Type: 918 case MetaspaceObj::TypeArrayOtherType: 919 case MetaspaceObj::ConstMethodType: 920 case MetaspaceObj::ConstantPoolCacheType: 921 case MetaspaceObj::AnnotationsType: 922 case MetaspaceObj::MethodCountersType: 923 case MetaspaceObj::RecordComponentType: 924 // These have no vtables. 925 break; 926 case MetaspaceObj::MethodDataType: 927 // We don't archive MethodData <-- should have been removed in removed_unsharable_info 928 ShouldNotReachHere(); 929 break; 930 default: 931 for (kind = 0; kind < _num_cloned_vtable_kinds; kind ++) { 932 if (vtable_of((Metadata*)obj) == _orig_cpp_vtptrs[kind]) { 933 break; 934 } 935 } 936 if (kind >= _num_cloned_vtable_kinds) { 937 fatal("Cannot find C++ vtable for " INTPTR_FORMAT " -- you probably added" 938 " a new subtype of Klass or MetaData without updating CPP_VTABLE_PATCH_TYPES_DO", 939 p2i(obj)); 940 } 941 } 942 943 if (kind >= 0) { 944 assert(kind < _num_cloned_vtable_kinds, "must be"); 945 return _cloned_cpp_vtptrs[kind]; 946 } else { 947 return NULL; 948 } 949 } 950 951 // This can be called at both dump time and run time: 952 // - clone the contents of the c++ vtables into the space 953 // allocated by allocate_cpp_vtable_clones() 954 void MetaspaceShared::clone_cpp_vtables(intptr_t* p) { 955 assert(DumpSharedSpaces || UseSharedSpaces, "sanity"); 956 CPP_VTABLE_PATCH_TYPES_DO(CLONE_CPP_VTABLE); 957 } 958 959 void MetaspaceShared::zero_cpp_vtable_clones_for_writing() { 960 assert(DumpSharedSpaces, "dump-time only"); 961 CPP_VTABLE_PATCH_TYPES_DO(ZERO_CPP_VTABLE); 962 } 963 964 // Allocate and initialize the C++ vtables, starting from top, but do not go past end. 965 char* MetaspaceShared::allocate_cpp_vtable_clones() { 966 char* cloned_vtables = _mc_region.top(); // This is the beginning of all the cloned vtables 967 968 assert(DumpSharedSpaces, "dump-time only"); 969 // Layout (each slot is a intptr_t): 970 // [number of slots in the first vtable = n1] 971 // [ <n1> slots for the first vtable] 972 // [number of slots in the first second = n2] 973 // [ <n2> slots for the second vtable] 974 // ... 975 // The order of the vtables is the same as the CPP_VTAB_PATCH_TYPES_DO macro. 976 CPP_VTABLE_PATCH_TYPES_DO(ALLOC_CPP_VTABLE_CLONE); 977 978 return cloned_vtables; 979 } 980 981 bool MetaspaceShared::is_valid_shared_method(const Method* m) { 982 assert(is_in_shared_metaspace(m), "must be"); 983 return CppVtableCloner<Method>::is_valid_shared_object(m); 984 } 985 986 void WriteClosure::do_oop(oop* o) { 987 if (*o == NULL) { 988 _dump_region->append_intptr_t(0); 989 } else { 990 assert(HeapShared::is_heap_object_archiving_allowed(), 991 "Archiving heap object is not allowed"); 992 _dump_region->append_intptr_t( 993 (intptr_t)CompressedOops::encode_not_null(*o)); 994 } 995 } 996 997 void WriteClosure::do_region(u_char* start, size_t size) { 998 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 999 assert(size % sizeof(intptr_t) == 0, "bad size"); 1000 do_tag((int)size); 1001 while (size > 0) { 1002 _dump_region->append_intptr_t(*(intptr_t*)start, true); 1003 start += sizeof(intptr_t); 1004 size -= sizeof(intptr_t); 1005 } 1006 } 1007 1008 // Populate the shared space. 1009 1010 class VM_PopulateDumpSharedSpace: public VM_Operation { 1011 private: 1012 GrowableArray<MemRegion> *_closed_archive_heap_regions; 1013 GrowableArray<MemRegion> *_open_archive_heap_regions; 1014 1015 GrowableArray<ArchiveHeapOopmapInfo> *_closed_archive_heap_oopmaps; 1016 GrowableArray<ArchiveHeapOopmapInfo> *_open_archive_heap_oopmaps; 1017 1018 void dump_java_heap_objects() NOT_CDS_JAVA_HEAP_RETURN; 1019 void dump_archive_heap_oopmaps() NOT_CDS_JAVA_HEAP_RETURN; 1020 void dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions, 1021 GrowableArray<ArchiveHeapOopmapInfo>* oopmaps); 1022 void dump_symbols(); 1023 char* dump_read_only_tables(); 1024 void print_region_stats(FileMapInfo* map_info); 1025 void print_bitmap_region_stats(size_t size, size_t total_size); 1026 void print_heap_region_stats(GrowableArray<MemRegion> *heap_mem, 1027 const char *name, size_t total_size); 1028 void relocate_to_requested_base_address(CHeapBitMap* ptrmap); 1029 1030 public: 1031 1032 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 1033 void doit(); // outline because gdb sucks 1034 bool allow_nested_vm_operations() const { return true; } 1035 }; // class VM_PopulateDumpSharedSpace 1036 1037 class StaticArchiveBuilder : public ArchiveBuilder { 1038 public: 1039 StaticArchiveBuilder(DumpRegion* rw_region, DumpRegion* ro_region) 1040 : ArchiveBuilder(rw_region, ro_region) {} 1041 1042 virtual void iterate_roots(MetaspaceClosure* it, bool is_relocating_pointers) { 1043 FileMapInfo::metaspace_pointers_do(it, false); 1044 SystemDictionaryShared::dumptime_classes_do(it); 1045 Universe::metaspace_pointers_do(it); 1046 SymbolTable::metaspace_pointers_do(it); 1047 vmSymbols::metaspace_pointers_do(it); 1048 } 1049 }; 1050 1051 void VM_PopulateDumpSharedSpace::dump_symbols() { 1052 log_info(cds)("Dumping symbol table ..."); 1053 1054 NOT_PRODUCT(SymbolTable::verify()); 1055 SymbolTable::write_to_archive(); 1056 } 1057 1058 char* VM_PopulateDumpSharedSpace::dump_read_only_tables() { 1059 ArchiveBuilder::OtherROAllocMark mark; 1060 1061 log_info(cds)("Removing java_mirror ... "); 1062 if (!HeapShared::is_heap_object_archiving_allowed()) { 1063 Universe::clear_basic_type_mirrors(); 1064 } 1065 remove_java_mirror_in_classes(); 1066 log_info(cds)("done. "); 1067 1068 SystemDictionaryShared::write_to_archive(); 1069 1070 // Write the other data to the output array. 1071 char* start = _ro_region.top(); 1072 WriteClosure wc(&_ro_region); 1073 MetaspaceShared::serialize(&wc); 1074 1075 // Write the bitmaps for patching the archive heap regions 1076 _closed_archive_heap_oopmaps = NULL; 1077 _open_archive_heap_oopmaps = NULL; 1078 dump_archive_heap_oopmaps(); 1079 1080 return start; 1081 } 1082 1083 void VM_PopulateDumpSharedSpace::relocate_to_requested_base_address(CHeapBitMap* ptrmap) { 1084 intx addr_delta = MetaspaceShared::final_delta(); 1085 if (addr_delta == 0) { 1086 ArchivePtrMarker::compact((address)SharedBaseAddress, (address)_ro_region.top()); 1087 } else { 1088 // We are not able to reserve space at MetaspaceShared::requested_base_address() (due to ASLR). 1089 // This means that the current content of the archive is based on a random 1090 // address. Let's relocate all the pointers, so that it can be mapped to 1091 // MetaspaceShared::requested_base_address() without runtime relocation. 1092 // 1093 // Note: both the base and dynamic archive are written with 1094 // FileMapHeader::_requested_base_address == MetaspaceShared::requested_base_address() 1095 1096 // Patch all pointers that are marked by ptrmap within this region, 1097 // where we have just dumped all the metaspace data. 1098 address patch_base = (address)SharedBaseAddress; 1099 address patch_end = (address)_ro_region.top(); 1100 size_t size = patch_end - patch_base; 1101 1102 // the current value of the pointers to be patched must be within this 1103 // range (i.e., must point to valid metaspace objects) 1104 address valid_old_base = patch_base; 1105 address valid_old_end = patch_end; 1106 1107 // after patching, the pointers must point inside this range 1108 // (the requested location of the archive, as mapped at runtime). 1109 address valid_new_base = (address)MetaspaceShared::requested_base_address(); 1110 address valid_new_end = valid_new_base + size; 1111 1112 log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT " ] to " 1113 "[" INTPTR_FORMAT " - " INTPTR_FORMAT " ]", p2i(patch_base), p2i(patch_end), 1114 p2i(valid_new_base), p2i(valid_new_end)); 1115 1116 SharedDataRelocator<true> patcher((address*)patch_base, (address*)patch_end, valid_old_base, valid_old_end, 1117 valid_new_base, valid_new_end, addr_delta, ptrmap); 1118 ptrmap->iterate(&patcher); 1119 ArchivePtrMarker::compact(patcher.max_non_null_offset()); 1120 } 1121 } 1122 1123 void VM_PopulateDumpSharedSpace::doit() { 1124 HeapShared::run_full_gc_in_vm_thread(); 1125 CHeapBitMap ptrmap; 1126 MetaspaceShared::initialize_ptr_marker(&ptrmap); 1127 1128 // We should no longer allocate anything from the metaspace, so that: 1129 // 1130 // (1) Metaspace::allocate might trigger GC if we have run out of 1131 // committed metaspace, but we can't GC because we're running 1132 // in the VM thread. 1133 // (2) ArchiveCompactor needs to work with a stable set of MetaspaceObjs. 1134 Metaspace::freeze(); 1135 DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm); 1136 1137 Thread* THREAD = VMThread::vm_thread(); 1138 1139 FileMapInfo::check_nonempty_dir_in_shared_path_table(); 1140 1141 NOT_PRODUCT(SystemDictionary::verify();) 1142 // The following guarantee is meant to ensure that no loader constraints 1143 // exist yet, since the constraints table is not shared. This becomes 1144 // more important now that we don't re-initialize vtables/itables for 1145 // shared classes at runtime, where constraints were previously created. 1146 guarantee(SystemDictionary::constraints()->number_of_entries() == 0, 1147 "loader constraints are not saved"); 1148 guarantee(SystemDictionary::placeholders()->number_of_entries() == 0, 1149 "placeholders are not saved"); 1150 1151 // At this point, many classes have been loaded. 1152 // Gather systemDictionary classes in a global array and do everything to 1153 // that so we don't have to walk the SystemDictionary again. 1154 SystemDictionaryShared::check_excluded_classes(); 1155 1156 StaticArchiveBuilder builder(&_rw_region, &_ro_region); 1157 builder.gather_klasses_and_symbols(); 1158 _global_klass_objects = builder.klasses(); 1159 1160 // Ensure the ConstMethods won't be modified at run-time 1161 log_info(cds)("Updating ConstMethods ... "); 1162 rewrite_nofast_bytecodes_and_calculate_fingerprints(THREAD); 1163 log_info(cds)("done. "); 1164 1165 // Remove all references outside the metadata 1166 log_info(cds)("Removing unshareable information ... "); 1167 remove_unshareable_in_classes(); 1168 log_info(cds)("done. "); 1169 1170 builder.gather_source_objs(); 1171 1172 MetaspaceShared::allocate_cloned_cpp_vtptrs(); 1173 char* cloned_vtables = _mc_region.top(); 1174 MetaspaceShared::allocate_cpp_vtable_clones(); 1175 1176 _mc_region.pack(&_rw_region); 1177 builder.dump_rw_region(); 1178 _rw_region.pack(&_ro_region); 1179 builder.dump_ro_region(); 1180 builder.relocate_pointers(); 1181 1182 dump_symbols(); 1183 1184 // Dump supported java heap objects 1185 _closed_archive_heap_regions = NULL; 1186 _open_archive_heap_regions = NULL; 1187 dump_java_heap_objects(); 1188 1189 builder.relocate_well_known_klasses(); 1190 1191 char* serialized_data = dump_read_only_tables(); 1192 _ro_region.pack(); 1193 1194 // The vtable clones contain addresses of the current process. 1195 // We don't want to write these addresses into the archive. Same for i2i buffer. 1196 MetaspaceShared::zero_cpp_vtable_clones_for_writing(); 1197 memset(MetaspaceShared::i2i_entry_code_buffers(), 0, 1198 MetaspaceShared::i2i_entry_code_buffers_size()); 1199 1200 // relocate the data so that it can be mapped to MetaspaceShared::requested_base_address() 1201 // without runtime relocation. 1202 relocate_to_requested_base_address(&ptrmap); 1203 1204 // Create and write the archive file that maps the shared spaces. 1205 1206 FileMapInfo* mapinfo = new FileMapInfo(true); 1207 mapinfo->populate_header(os::vm_allocation_granularity()); 1208 mapinfo->set_serialized_data(serialized_data); 1209 mapinfo->set_cloned_vtables(cloned_vtables); 1210 mapinfo->set_i2i_entry_code_buffers(MetaspaceShared::i2i_entry_code_buffers(), 1211 MetaspaceShared::i2i_entry_code_buffers_size()); 1212 mapinfo->open_for_write(); 1213 MetaspaceShared::write_core_archive_regions(mapinfo, _closed_archive_heap_oopmaps, _open_archive_heap_oopmaps); 1214 _total_closed_archive_region_size = mapinfo->write_archive_heap_regions( 1215 _closed_archive_heap_regions, 1216 _closed_archive_heap_oopmaps, 1217 MetaspaceShared::first_closed_archive_heap_region, 1218 MetaspaceShared::max_closed_archive_heap_region); 1219 _total_open_archive_region_size = mapinfo->write_archive_heap_regions( 1220 _open_archive_heap_regions, 1221 _open_archive_heap_oopmaps, 1222 MetaspaceShared::first_open_archive_heap_region, 1223 MetaspaceShared::max_open_archive_heap_region); 1224 1225 mapinfo->set_final_requested_base((char*)MetaspaceShared::requested_base_address()); 1226 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 1227 mapinfo->write_header(); 1228 print_region_stats(mapinfo); 1229 mapinfo->close(); 1230 1231 if (log_is_enabled(Info, cds)) { 1232 builder.print_stats(int(_ro_region.used()), int(_rw_region.used()), int(_mc_region.used())); 1233 } 1234 1235 if (PrintSystemDictionaryAtExit) { 1236 SystemDictionary::print(); 1237 } 1238 1239 if (AllowArchivingWithJavaAgent) { 1240 warning("This archive was created with AllowArchivingWithJavaAgent. It should be used " 1241 "for testing purposes only and should not be used in a production environment"); 1242 } 1243 1244 // There may be other pending VM operations that operate on the InstanceKlasses, 1245 // which will fail because InstanceKlasses::remove_unshareable_info() 1246 // has been called. Forget these operations and exit the VM directly. 1247 vm_direct_exit(0); 1248 } 1249 1250 void VM_PopulateDumpSharedSpace::print_region_stats(FileMapInfo *map_info) { 1251 // Print statistics of all the regions 1252 const size_t bitmap_used = map_info->space_at(MetaspaceShared::bm)->used(); 1253 const size_t bitmap_reserved = map_info->space_at(MetaspaceShared::bm)->used_aligned(); 1254 const size_t total_reserved = _ro_region.reserved() + _rw_region.reserved() + 1255 _mc_region.reserved() + 1256 bitmap_reserved + 1257 _total_closed_archive_region_size + 1258 _total_open_archive_region_size; 1259 const size_t total_bytes = _ro_region.used() + _rw_region.used() + 1260 _mc_region.used() + 1261 bitmap_used + 1262 _total_closed_archive_region_size + 1263 _total_open_archive_region_size; 1264 const double total_u_perc = percent_of(total_bytes, total_reserved); 1265 1266 _mc_region.print(total_reserved); 1267 _rw_region.print(total_reserved); 1268 _ro_region.print(total_reserved); 1269 print_bitmap_region_stats(bitmap_used, total_reserved); 1270 print_heap_region_stats(_closed_archive_heap_regions, "ca", total_reserved); 1271 print_heap_region_stats(_open_archive_heap_regions, "oa", total_reserved); 1272 1273 log_debug(cds)("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]", 1274 total_bytes, total_reserved, total_u_perc); 1275 } 1276 1277 void VM_PopulateDumpSharedSpace::print_bitmap_region_stats(size_t size, size_t total_size) { 1278 log_debug(cds)("bm space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [100.0%% used]", 1279 size, size/double(total_size)*100.0, size); 1280 } 1281 1282 void VM_PopulateDumpSharedSpace::print_heap_region_stats(GrowableArray<MemRegion> *heap_mem, 1283 const char *name, size_t total_size) { 1284 int arr_len = heap_mem == NULL ? 0 : heap_mem->length(); 1285 for (int i = 0; i < arr_len; i++) { 1286 char* start = (char*)heap_mem->at(i).start(); 1287 size_t size = heap_mem->at(i).byte_size(); 1288 char* top = start + size; 1289 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, 1290 name, i, size, size/double(total_size)*100.0, size, p2i(start)); 1291 1292 } 1293 } 1294 1295 void MetaspaceShared::write_core_archive_regions(FileMapInfo* mapinfo, 1296 GrowableArray<ArchiveHeapOopmapInfo>* closed_oopmaps, 1297 GrowableArray<ArchiveHeapOopmapInfo>* open_oopmaps) { 1298 // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with 1299 // MetaspaceShared::n_regions (internal to hotspot). 1300 assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity"); 1301 1302 // mc contains the trampoline code for method entries, which are patched at run time, 1303 // so it needs to be read/write. 1304 write_region(mapinfo, mc, &_mc_region, /*read_only=*/false,/*allow_exec=*/true); 1305 write_region(mapinfo, rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false); 1306 write_region(mapinfo, ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false); 1307 mapinfo->write_bitmap_region(ArchivePtrMarker::ptrmap(), closed_oopmaps, open_oopmaps); 1308 } 1309 1310 void MetaspaceShared::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only, bool allow_exec) { 1311 mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec); 1312 } 1313 1314 // Update a Java object to point its Klass* to the new location after 1315 // shared archive has been compacted. 1316 void MetaspaceShared::relocate_klass_ptr(oop o) { 1317 assert(DumpSharedSpaces, "sanity"); 1318 Klass* k = ArchiveBuilder::get_relocated_klass(o->klass()); 1319 o->set_klass(k); 1320 } 1321 1322 Klass* MetaspaceShared::get_relocated_klass(Klass *k, bool is_final) { 1323 assert(DumpSharedSpaces, "sanity"); 1324 k = ArchiveBuilder::get_relocated_klass(k); 1325 if (is_final) { 1326 k = (Klass*)(address(k) + final_delta()); 1327 } 1328 return k; 1329 } 1330 1331 class LinkSharedClassesClosure : public KlassClosure { 1332 Thread* THREAD; 1333 bool _made_progress; 1334 public: 1335 LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {} 1336 1337 void reset() { _made_progress = false; } 1338 bool made_progress() const { return _made_progress; } 1339 1340 void do_klass(Klass* k) { 1341 if (k->is_instance_klass()) { 1342 InstanceKlass* ik = InstanceKlass::cast(k); 1343 // For dynamic CDS dump, only link classes loaded by the builtin class loaders. 1344 bool do_linking = DumpSharedSpaces ? true : !ik->is_shared_unregistered_class(); 1345 if (do_linking) { 1346 // Link the class to cause the bytecodes to be rewritten and the 1347 // cpcache to be created. Class verification is done according 1348 // to -Xverify setting. 1349 _made_progress |= MetaspaceShared::try_link_class(ik, THREAD); 1350 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 1351 1352 if (DumpSharedSpaces) { 1353 // The following function is used to resolve all Strings in the statically 1354 // dumped classes to archive all the Strings. The archive heap is not supported 1355 // for the dynamic archive. 1356 ik->constants()->resolve_class_constants(THREAD); 1357 } 1358 } 1359 } 1360 } 1361 }; 1362 1363 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) { 1364 // We need to iterate because verification may cause additional classes 1365 // to be loaded. 1366 LinkSharedClassesClosure link_closure(THREAD); 1367 do { 1368 link_closure.reset(); 1369 ClassLoaderDataGraph::unlocked_loaded_classes_do(&link_closure); 1370 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 1371 } while (link_closure.made_progress()); 1372 } 1373 1374 void MetaspaceShared::prepare_for_dumping() { 1375 Arguments::check_unsupported_dumping_properties(); 1376 ClassLoader::initialize_shared_path(); 1377 } 1378 1379 // Preload classes from a list, populate the shared spaces and dump to a 1380 // file. 1381 void MetaspaceShared::preload_and_dump(TRAPS) { 1382 { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime)); 1383 ResourceMark rm(THREAD); 1384 char class_list_path_str[JVM_MAXPATHLEN]; 1385 // Preload classes to be shared. 1386 const char* class_list_path; 1387 if (SharedClassListFile == NULL) { 1388 // Construct the path to the class list (in jre/lib) 1389 // Walk up two directories from the location of the VM and 1390 // optionally tack on "lib" (depending on platform) 1391 os::jvm_path(class_list_path_str, sizeof(class_list_path_str)); 1392 for (int i = 0; i < 3; i++) { 1393 char *end = strrchr(class_list_path_str, *os::file_separator()); 1394 if (end != NULL) *end = '\0'; 1395 } 1396 int class_list_path_len = (int)strlen(class_list_path_str); 1397 if (class_list_path_len >= 3) { 1398 if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) { 1399 if (class_list_path_len < JVM_MAXPATHLEN - 4) { 1400 jio_snprintf(class_list_path_str + class_list_path_len, 1401 sizeof(class_list_path_str) - class_list_path_len, 1402 "%slib", os::file_separator()); 1403 class_list_path_len += 4; 1404 } 1405 } 1406 } 1407 if (class_list_path_len < JVM_MAXPATHLEN - 10) { 1408 jio_snprintf(class_list_path_str + class_list_path_len, 1409 sizeof(class_list_path_str) - class_list_path_len, 1410 "%sclasslist", os::file_separator()); 1411 } 1412 class_list_path = class_list_path_str; 1413 } else { 1414 class_list_path = SharedClassListFile; 1415 } 1416 1417 log_info(cds)("Loading classes to share ..."); 1418 _has_error_classes = false; 1419 int class_count = preload_classes(class_list_path, THREAD); 1420 if (ExtraSharedClassListFile) { 1421 class_count += preload_classes(ExtraSharedClassListFile, THREAD); 1422 } 1423 log_info(cds)("Loading classes to share: done."); 1424 1425 log_info(cds)("Shared spaces: preloaded %d classes", class_count); 1426 1427 if (SharedArchiveConfigFile) { 1428 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile); 1429 read_extra_data(SharedArchiveConfigFile, THREAD); 1430 } 1431 log_info(cds)("Reading extra data: done."); 1432 1433 HeapShared::init_subgraph_entry_fields(THREAD); 1434 1435 // Rewrite and link classes 1436 log_info(cds)("Rewriting and linking classes ..."); 1437 1438 // Link any classes which got missed. This would happen if we have loaded classes that 1439 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K 1440 // fails verification, all other interfaces that were not specified in the classlist but 1441 // are implemented by K are not verified. 1442 link_and_cleanup_shared_classes(CATCH); 1443 log_info(cds)("Rewriting and linking classes: done"); 1444 1445 VM_PopulateDumpSharedSpace op; 1446 MutexLocker ml(THREAD, HeapShared::is_heap_object_archiving_allowed() ? 1447 Heap_lock : NULL); // needed by HeapShared::run_gc() 1448 VMThread::execute(&op); 1449 } 1450 } 1451 1452 1453 int MetaspaceShared::preload_classes(const char* class_list_path, TRAPS) { 1454 ClassListParser parser(class_list_path); 1455 int class_count = 0; 1456 1457 while (parser.parse_one_line()) { 1458 Klass* klass = parser.load_current_class(THREAD); 1459 if (HAS_PENDING_EXCEPTION) { 1460 if (klass == NULL && 1461 (PENDING_EXCEPTION->klass()->name() == vmSymbols::java_lang_ClassNotFoundException())) { 1462 // print a warning only when the pending exception is class not found 1463 log_warning(cds)("Preload Warning: Cannot find %s", parser.current_class_name()); 1464 } 1465 CLEAR_PENDING_EXCEPTION; 1466 } 1467 if (klass != NULL) { 1468 if (log_is_enabled(Trace, cds)) { 1469 ResourceMark rm(THREAD); 1470 log_trace(cds)("Shared spaces preloaded: %s", klass->external_name()); 1471 } 1472 1473 if (klass->is_instance_klass()) { 1474 InstanceKlass* ik = InstanceKlass::cast(klass); 1475 1476 // Link the class to cause the bytecodes to be rewritten and the 1477 // cpcache to be created. The linking is done as soon as classes 1478 // are loaded in order that the related data structures (klass and 1479 // cpCache) are located together. 1480 try_link_class(ik, THREAD); 1481 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 1482 } 1483 1484 class_count++; 1485 } 1486 } 1487 1488 return class_count; 1489 } 1490 1491 // Returns true if the class's status has changed 1492 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) { 1493 Arguments::assert_is_dumping_archive(); 1494 if (ik->init_state() < InstanceKlass::linked && 1495 !SystemDictionaryShared::has_class_failed_verification(ik)) { 1496 bool saved = BytecodeVerificationLocal; 1497 if (ik->is_shared_unregistered_class() && ik->class_loader() == NULL) { 1498 // The verification decision is based on BytecodeVerificationRemote 1499 // for non-system classes. Since we are using the NULL classloader 1500 // to load non-system classes for customized class loaders during dumping, 1501 // we need to temporarily change BytecodeVerificationLocal to be the same as 1502 // BytecodeVerificationRemote. Note this can cause the parent system 1503 // classes also being verified. The extra overhead is acceptable during 1504 // dumping. 1505 BytecodeVerificationLocal = BytecodeVerificationRemote; 1506 } 1507 ik->link_class(THREAD); 1508 if (HAS_PENDING_EXCEPTION) { 1509 ResourceMark rm(THREAD); 1510 log_warning(cds)("Preload Warning: Verification failed for %s", 1511 ik->external_name()); 1512 CLEAR_PENDING_EXCEPTION; 1513 SystemDictionaryShared::set_class_has_failed_verification(ik); 1514 _has_error_classes = true; 1515 } 1516 BytecodeVerificationLocal = saved; 1517 return true; 1518 } else { 1519 return false; 1520 } 1521 } 1522 1523 #if INCLUDE_CDS_JAVA_HEAP 1524 void VM_PopulateDumpSharedSpace::dump_java_heap_objects() { 1525 // The closed and open archive heap space has maximum two regions. 1526 // See FileMapInfo::write_archive_heap_regions() for details. 1527 _closed_archive_heap_regions = new GrowableArray<MemRegion>(2); 1528 _open_archive_heap_regions = new GrowableArray<MemRegion>(2); 1529 HeapShared::archive_java_heap_objects(_closed_archive_heap_regions, 1530 _open_archive_heap_regions); 1531 ArchiveBuilder::OtherROAllocMark mark; 1532 HeapShared::write_subgraph_info_table(); 1533 } 1534 1535 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps() { 1536 if (HeapShared::is_heap_object_archiving_allowed()) { 1537 _closed_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2); 1538 dump_archive_heap_oopmaps(_closed_archive_heap_regions, _closed_archive_heap_oopmaps); 1539 1540 _open_archive_heap_oopmaps = new GrowableArray<ArchiveHeapOopmapInfo>(2); 1541 dump_archive_heap_oopmaps(_open_archive_heap_regions, _open_archive_heap_oopmaps); 1542 } 1543 } 1544 1545 void VM_PopulateDumpSharedSpace::dump_archive_heap_oopmaps(GrowableArray<MemRegion>* regions, 1546 GrowableArray<ArchiveHeapOopmapInfo>* oopmaps) { 1547 for (int i=0; i<regions->length(); i++) { 1548 ResourceBitMap oopmap = HeapShared::calculate_oopmap(regions->at(i)); 1549 size_t size_in_bits = oopmap.size(); 1550 size_t size_in_bytes = oopmap.size_in_bytes(); 1551 uintptr_t* buffer = (uintptr_t*)NEW_C_HEAP_ARRAY(char, size_in_bytes, mtInternal); 1552 oopmap.write_to(buffer, size_in_bytes); 1553 log_info(cds, heap)("Oopmap = " INTPTR_FORMAT " (" SIZE_FORMAT_W(6) " bytes) for heap region " 1554 INTPTR_FORMAT " (" SIZE_FORMAT_W(8) " bytes)", 1555 p2i(buffer), size_in_bytes, 1556 p2i(regions->at(i).start()), regions->at(i).byte_size()); 1557 1558 ArchiveHeapOopmapInfo info; 1559 info._oopmap = (address)buffer; 1560 info._oopmap_size_in_bits = size_in_bits; 1561 info._oopmap_size_in_bytes = size_in_bytes; 1562 oopmaps->append(info); 1563 } 1564 } 1565 #endif // INCLUDE_CDS_JAVA_HEAP 1566 1567 void ReadClosure::do_ptr(void** p) { 1568 assert(*p == NULL, "initializing previous initialized pointer."); 1569 intptr_t obj = nextPtr(); 1570 assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, 1571 "hit tag while initializing ptrs."); 1572 *p = (void*)obj; 1573 } 1574 1575 void ReadClosure::do_u4(u4* p) { 1576 intptr_t obj = nextPtr(); 1577 *p = (u4)(uintx(obj)); 1578 } 1579 1580 void ReadClosure::do_bool(bool* p) { 1581 intptr_t obj = nextPtr(); 1582 *p = (bool)(uintx(obj)); 1583 } 1584 1585 void ReadClosure::do_tag(int tag) { 1586 int old_tag; 1587 old_tag = (int)(intptr_t)nextPtr(); 1588 // do_int(&old_tag); 1589 assert(tag == old_tag, "old tag doesn't match"); 1590 FileMapInfo::assert_mark(tag == old_tag); 1591 } 1592 1593 void ReadClosure::do_oop(oop *p) { 1594 narrowOop o = (narrowOop)nextPtr(); 1595 if (o == 0 || !HeapShared::open_archive_heap_region_mapped()) { 1596 *p = NULL; 1597 } else { 1598 assert(HeapShared::is_heap_object_archiving_allowed(), 1599 "Archived heap object is not allowed"); 1600 assert(HeapShared::open_archive_heap_region_mapped(), 1601 "Open archive heap region is not mapped"); 1602 *p = HeapShared::decode_from_archive(o); 1603 } 1604 } 1605 1606 void ReadClosure::do_region(u_char* start, size_t size) { 1607 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 1608 assert(size % sizeof(intptr_t) == 0, "bad size"); 1609 do_tag((int)size); 1610 while (size > 0) { 1611 *(intptr_t*)start = nextPtr(); 1612 start += sizeof(intptr_t); 1613 size -= sizeof(intptr_t); 1614 } 1615 } 1616 1617 void MetaspaceShared::set_shared_metaspace_range(void* base, void *static_top, void* top) { 1618 assert(base <= static_top && static_top <= top, "must be"); 1619 _shared_metaspace_static_top = static_top; 1620 MetaspaceObj::set_shared_metaspace_range(base, top); 1621 } 1622 1623 // Return true if given address is in the misc data region 1624 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) { 1625 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx); 1626 } 1627 1628 bool MetaspaceShared::is_in_trampoline_frame(address addr) { 1629 if (UseSharedSpaces && is_in_shared_region(addr, MetaspaceShared::mc)) { 1630 return true; 1631 } 1632 return false; 1633 } 1634 1635 bool MetaspaceShared::is_shared_dynamic(void* p) { 1636 if ((p < MetaspaceObj::shared_metaspace_top()) && 1637 (p >= _shared_metaspace_static_top)) { 1638 return true; 1639 } else { 1640 return false; 1641 } 1642 } 1643 1644 void MetaspaceShared::initialize_runtime_shared_and_meta_spaces() { 1645 assert(UseSharedSpaces, "Must be called when UseSharedSpaces is enabled"); 1646 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE; 1647 1648 FileMapInfo* static_mapinfo = open_static_archive(); 1649 FileMapInfo* dynamic_mapinfo = NULL; 1650 1651 if (static_mapinfo != NULL) { 1652 dynamic_mapinfo = open_dynamic_archive(); 1653 1654 // First try to map at the requested address 1655 result = map_archives(static_mapinfo, dynamic_mapinfo, true); 1656 if (result == MAP_ARCHIVE_MMAP_FAILURE) { 1657 // Mapping has failed (probably due to ASLR). Let's map at an address chosen 1658 // by the OS. 1659 log_info(cds)("Try to map archive(s) at an alternative address"); 1660 result = map_archives(static_mapinfo, dynamic_mapinfo, false); 1661 } 1662 } 1663 1664 if (result == MAP_ARCHIVE_SUCCESS) { 1665 bool dynamic_mapped = (dynamic_mapinfo != NULL && dynamic_mapinfo->is_mapped()); 1666 char* cds_base = static_mapinfo->mapped_base(); 1667 char* cds_end = dynamic_mapped ? dynamic_mapinfo->mapped_end() : static_mapinfo->mapped_end(); 1668 set_shared_metaspace_range(cds_base, static_mapinfo->mapped_end(), cds_end); 1669 _relocation_delta = static_mapinfo->relocation_delta(); 1670 if (dynamic_mapped) { 1671 FileMapInfo::set_shared_path_table(dynamic_mapinfo); 1672 } else { 1673 FileMapInfo::set_shared_path_table(static_mapinfo); 1674 } 1675 _requested_base_address = static_mapinfo->requested_base_address(); 1676 } else { 1677 set_shared_metaspace_range(NULL, NULL, NULL); 1678 UseSharedSpaces = false; 1679 FileMapInfo::fail_continue("Unable to map shared spaces"); 1680 if (PrintSharedArchiveAndExit) { 1681 vm_exit_during_initialization("Unable to use shared archive."); 1682 } 1683 } 1684 1685 if (static_mapinfo != NULL && !static_mapinfo->is_mapped()) { 1686 delete static_mapinfo; 1687 } 1688 if (dynamic_mapinfo != NULL && !dynamic_mapinfo->is_mapped()) { 1689 delete dynamic_mapinfo; 1690 } 1691 } 1692 1693 FileMapInfo* MetaspaceShared::open_static_archive() { 1694 FileMapInfo* mapinfo = new FileMapInfo(true); 1695 if (!mapinfo->initialize()) { 1696 delete(mapinfo); 1697 return NULL; 1698 } 1699 return mapinfo; 1700 } 1701 1702 FileMapInfo* MetaspaceShared::open_dynamic_archive() { 1703 if (DynamicDumpSharedSpaces) { 1704 return NULL; 1705 } 1706 if (Arguments::GetSharedDynamicArchivePath() == NULL) { 1707 return NULL; 1708 } 1709 1710 FileMapInfo* mapinfo = new FileMapInfo(false); 1711 if (!mapinfo->initialize()) { 1712 delete(mapinfo); 1713 return NULL; 1714 } 1715 return mapinfo; 1716 } 1717 1718 // use_requested_addr: 1719 // true = map at FileMapHeader::_requested_base_address 1720 // false = map at an alternative address picked by OS. 1721 MapArchiveResult MetaspaceShared::map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo, 1722 bool use_requested_addr) { 1723 if (use_requested_addr && static_mapinfo->requested_base_address() == NULL) { 1724 log_info(cds)("Archive(s) were created with -XX:SharedBaseAddress=0. Always map at os-selected address."); 1725 return MAP_ARCHIVE_MMAP_FAILURE; 1726 } 1727 1728 PRODUCT_ONLY(if (ArchiveRelocationMode == 1 && use_requested_addr) { 1729 // For product build only -- this is for benchmarking the cost of doing relocation. 1730 // For debug builds, the check is done below, after reserving the space, for better test coverage 1731 // (see comment below). 1732 log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address"); 1733 return MAP_ARCHIVE_MMAP_FAILURE; 1734 }); 1735 1736 if (ArchiveRelocationMode == 2 && !use_requested_addr) { 1737 log_info(cds)("ArchiveRelocationMode == 2: never map archive(s) at an alternative address"); 1738 return MAP_ARCHIVE_MMAP_FAILURE; 1739 }; 1740 1741 if (dynamic_mapinfo != NULL) { 1742 // Ensure that the OS won't be able to allocate new memory spaces between the two 1743 // archives, or else it would mess up the simple comparision in MetaspaceObj::is_shared(). 1744 assert(static_mapinfo->mapping_end_offset() == dynamic_mapinfo->mapping_base_offset(), "no gap"); 1745 } 1746 1747 ReservedSpace archive_space_rs, class_space_rs; 1748 MapArchiveResult result = MAP_ARCHIVE_OTHER_FAILURE; 1749 char* mapped_base_address = reserve_address_space_for_archives(static_mapinfo, dynamic_mapinfo, 1750 use_requested_addr, archive_space_rs, 1751 class_space_rs); 1752 if (mapped_base_address == NULL) { 1753 result = MAP_ARCHIVE_MMAP_FAILURE; 1754 log_debug(cds)("Failed to reserve spaces (use_requested_addr=%u)", (unsigned)use_requested_addr); 1755 } else { 1756 1757 #ifdef ASSERT 1758 // Some sanity checks after reserving address spaces for archives 1759 // and class space. 1760 assert(archive_space_rs.is_reserved(), "Sanity"); 1761 if (Metaspace::using_class_space()) { 1762 // Class space must closely follow the archive space. Both spaces 1763 // must be aligned correctly. 1764 assert(class_space_rs.is_reserved(), 1765 "A class space should have been reserved"); 1766 assert(class_space_rs.base() >= archive_space_rs.end(), 1767 "class space should follow the cds archive space"); 1768 assert(is_aligned(archive_space_rs.base(), 1769 MetaspaceShared::reserved_space_alignment()), 1770 "Archive space misaligned"); 1771 assert(is_aligned(class_space_rs.base(), 1772 Metaspace::reserve_alignment()), 1773 "class space misaligned"); 1774 } 1775 #endif // ASSERT 1776 1777 log_debug(cds)("Reserved archive_space_rs [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (" SIZE_FORMAT ") bytes", 1778 p2i(archive_space_rs.base()), p2i(archive_space_rs.end()), archive_space_rs.size()); 1779 log_debug(cds)("Reserved class_space_rs [" INTPTR_FORMAT " - " INTPTR_FORMAT "] (" SIZE_FORMAT ") bytes", 1780 p2i(class_space_rs.base()), p2i(class_space_rs.end()), class_space_rs.size()); 1781 1782 if (MetaspaceShared::use_windows_memory_mapping()) { 1783 // We have now reserved address space for the archives, and will map in 1784 // the archive files into this space. 1785 // 1786 // Special handling for Windows: on Windows we cannot map a file view 1787 // into an existing memory mapping. So, we unmap the address range we 1788 // just reserved again, which will make it available for mapping the 1789 // archives. 1790 // Reserving this range has not been for naught however since it makes 1791 // us reasonably sure the address range is available. 1792 // 1793 // But still it may fail, since between unmapping the range and mapping 1794 // in the archive someone else may grab the address space. Therefore 1795 // there is a fallback in FileMap::map_region() where we just read in 1796 // the archive files sequentially instead of mapping it in. We couple 1797 // this with use_requested_addr, since we're going to patch all the 1798 // pointers anyway so there's no benefit to mmap. 1799 if (use_requested_addr) { 1800 log_info(cds)("Windows mmap workaround: releasing archive space."); 1801 archive_space_rs.release(); 1802 } 1803 } 1804 MapArchiveResult static_result = map_archive(static_mapinfo, mapped_base_address, archive_space_rs); 1805 MapArchiveResult dynamic_result = (static_result == MAP_ARCHIVE_SUCCESS) ? 1806 map_archive(dynamic_mapinfo, mapped_base_address, archive_space_rs) : MAP_ARCHIVE_OTHER_FAILURE; 1807 1808 DEBUG_ONLY(if (ArchiveRelocationMode == 1 && use_requested_addr) { 1809 // This is for simulating mmap failures at the requested address. In 1810 // debug builds, we do it here (after all archives have possibly been 1811 // mapped), so we can thoroughly test the code for failure handling 1812 // (releasing all allocated resource, etc). 1813 log_info(cds)("ArchiveRelocationMode == 1: always map archive(s) at an alternative address"); 1814 if (static_result == MAP_ARCHIVE_SUCCESS) { 1815 static_result = MAP_ARCHIVE_MMAP_FAILURE; 1816 } 1817 if (dynamic_result == MAP_ARCHIVE_SUCCESS) { 1818 dynamic_result = MAP_ARCHIVE_MMAP_FAILURE; 1819 } 1820 }); 1821 1822 if (static_result == MAP_ARCHIVE_SUCCESS) { 1823 if (dynamic_result == MAP_ARCHIVE_SUCCESS) { 1824 result = MAP_ARCHIVE_SUCCESS; 1825 } else if (dynamic_result == MAP_ARCHIVE_OTHER_FAILURE) { 1826 assert(dynamic_mapinfo != NULL && !dynamic_mapinfo->is_mapped(), "must have failed"); 1827 // No need to retry mapping the dynamic archive again, as it will never succeed 1828 // (bad file, etc) -- just keep the base archive. 1829 log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s", 1830 dynamic_mapinfo->full_path()); 1831 result = MAP_ARCHIVE_SUCCESS; 1832 // TODO, we can give the unused space for the dynamic archive to class_space_rs, but there's no 1833 // easy API to do that right now. 1834 } else { 1835 result = MAP_ARCHIVE_MMAP_FAILURE; 1836 } 1837 } else if (static_result == MAP_ARCHIVE_OTHER_FAILURE) { 1838 result = MAP_ARCHIVE_OTHER_FAILURE; 1839 } else { 1840 result = MAP_ARCHIVE_MMAP_FAILURE; 1841 } 1842 } 1843 1844 if (result == MAP_ARCHIVE_SUCCESS) { 1845 SharedBaseAddress = (size_t)mapped_base_address; 1846 LP64_ONLY({ 1847 if (Metaspace::using_class_space()) { 1848 // Set up ccs in metaspace. 1849 Metaspace::initialize_class_space(class_space_rs); 1850 1851 // Set up compressed Klass pointer encoding: the encoding range must 1852 // cover both archive and class space. 1853 address cds_base = (address)static_mapinfo->mapped_base(); 1854 address ccs_end = (address)class_space_rs.end(); 1855 CompressedKlassPointers::initialize(cds_base, ccs_end - cds_base); 1856 1857 // map_heap_regions() compares the current narrow oop and klass encodings 1858 // with the archived ones, so it must be done after all encodings are determined. 1859 static_mapinfo->map_heap_regions(); 1860 } 1861 }); 1862 log_info(cds)("Using optimized module handling %s", MetaspaceShared::use_optimized_module_handling() ? "enabled" : "disabled"); 1863 } else { 1864 unmap_archive(static_mapinfo); 1865 unmap_archive(dynamic_mapinfo); 1866 release_reserved_spaces(archive_space_rs, class_space_rs); 1867 } 1868 1869 return result; 1870 } 1871 1872 1873 // This will reserve two address spaces suitable to house Klass structures, one 1874 // for the cds archives (static archive and optionally dynamic archive) and 1875 // optionally one move for ccs. 1876 // 1877 // Since both spaces must fall within the compressed class pointer encoding 1878 // range, they are allocated close to each other. 1879 // 1880 // Space for archives will be reserved first, followed by a potential gap, 1881 // followed by the space for ccs: 1882 // 1883 // +-- Base address A B End 1884 // | | | | 1885 // v v v v 1886 // +-------------+--------------+ +----------------------+ 1887 // | static arc | [dyn. arch] | [gap] | compr. class space | 1888 // +-------------+--------------+ +----------------------+ 1889 // 1890 // (The gap may result from different alignment requirements between metaspace 1891 // and CDS) 1892 // 1893 // If UseCompressedClassPointers is disabled, only one address space will be 1894 // reserved: 1895 // 1896 // +-- Base address End 1897 // | | 1898 // v v 1899 // +-------------+--------------+ 1900 // | static arc | [dyn. arch] | 1901 // +-------------+--------------+ 1902 // 1903 // Base address: If use_archive_base_addr address is true, the Base address is 1904 // determined by the address stored in the static archive. If 1905 // use_archive_base_addr address is false, this base address is determined 1906 // by the platform. 1907 // 1908 // If UseCompressedClassPointers=1, the range encompassing both spaces will be 1909 // suitable to en/decode narrow Klass pointers: the base will be valid for 1910 // encoding, the range [Base, End) not surpass KlassEncodingMetaspaceMax. 1911 // 1912 // Return: 1913 // 1914 // - On success: 1915 // - archive_space_rs will be reserved and large enough to host static and 1916 // if needed dynamic archive: [Base, A). 1917 // archive_space_rs.base and size will be aligned to CDS reserve 1918 // granularity. 1919 // - class_space_rs: If UseCompressedClassPointers=1, class_space_rs will 1920 // be reserved. Its start address will be aligned to metaspace reserve 1921 // alignment, which may differ from CDS alignment. It will follow the cds 1922 // archive space, close enough such that narrow class pointer encoding 1923 // covers both spaces. 1924 // If UseCompressedClassPointers=0, class_space_rs remains unreserved. 1925 // - On error: NULL is returned and the spaces remain unreserved. 1926 char* MetaspaceShared::reserve_address_space_for_archives(FileMapInfo* static_mapinfo, 1927 FileMapInfo* dynamic_mapinfo, 1928 bool use_archive_base_addr, 1929 ReservedSpace& archive_space_rs, 1930 ReservedSpace& class_space_rs) { 1931 1932 address const base_address = (address) (use_archive_base_addr ? static_mapinfo->requested_base_address() : NULL); 1933 const size_t archive_space_alignment = MetaspaceShared::reserved_space_alignment(); 1934 1935 // Size and requested location of the archive_space_rs (for both static and dynamic archives) 1936 assert(static_mapinfo->mapping_base_offset() == 0, "Must be"); 1937 size_t archive_end_offset = (dynamic_mapinfo == NULL) ? static_mapinfo->mapping_end_offset() : dynamic_mapinfo->mapping_end_offset(); 1938 size_t archive_space_size = align_up(archive_end_offset, archive_space_alignment); 1939 1940 // If a base address is given, it must have valid alignment and be suitable as encoding base. 1941 if (base_address != NULL) { 1942 assert(is_aligned(base_address, archive_space_alignment), 1943 "Archive base address invalid: " PTR_FORMAT ".", p2i(base_address)); 1944 if (Metaspace::using_class_space()) { 1945 assert(CompressedKlassPointers::is_valid_base(base_address), 1946 "Archive base address invalid: " PTR_FORMAT ".", p2i(base_address)); 1947 } 1948 } 1949 1950 if (!Metaspace::using_class_space()) { 1951 // Get the simple case out of the way first: 1952 // no compressed class space, simple allocation. 1953 archive_space_rs = ReservedSpace(archive_space_size, archive_space_alignment, 1954 false /* bool large */, (char*)base_address); 1955 if (archive_space_rs.is_reserved()) { 1956 assert(base_address == NULL || 1957 (address)archive_space_rs.base() == base_address, "Sanity"); 1958 // Register archive space with NMT. 1959 MemTracker::record_virtual_memory_type(archive_space_rs.base(), mtClassShared); 1960 return archive_space_rs.base(); 1961 } 1962 return NULL; 1963 } 1964 1965 #ifdef _LP64 1966 1967 // Complex case: two spaces adjacent to each other, both to be addressable 1968 // with narrow class pointers. 1969 // We reserve the whole range spanning both spaces, then split that range up. 1970 1971 const size_t class_space_alignment = Metaspace::reserve_alignment(); 1972 1973 // To simplify matters, lets assume that metaspace alignment will always be 1974 // equal or a multiple of archive alignment. 1975 assert(is_power_of_2(class_space_alignment) && 1976 is_power_of_2(archive_space_alignment) && 1977 class_space_alignment >= archive_space_alignment, 1978 "Sanity"); 1979 1980 const size_t class_space_size = CompressedClassSpaceSize; 1981 assert(CompressedClassSpaceSize > 0 && 1982 is_aligned(CompressedClassSpaceSize, class_space_alignment), 1983 "CompressedClassSpaceSize malformed: " 1984 SIZE_FORMAT, CompressedClassSpaceSize); 1985 1986 const size_t ccs_begin_offset = align_up(base_address + archive_space_size, 1987 class_space_alignment) - base_address; 1988 const size_t gap_size = ccs_begin_offset - archive_space_size; 1989 1990 const size_t total_range_size = 1991 align_up(archive_space_size + gap_size + class_space_size, 1992 os::vm_allocation_granularity()); 1993 1994 ReservedSpace total_rs; 1995 if (base_address != NULL) { 1996 // Reserve at the given archive base address, or not at all. 1997 total_rs = ReservedSpace(total_range_size, archive_space_alignment, 1998 false /* bool large */, (char*) base_address); 1999 } else { 2000 // Reserve at any address, but leave it up to the platform to choose a good one. 2001 total_rs = Metaspace::reserve_address_space_for_compressed_classes(total_range_size); 2002 } 2003 2004 if (!total_rs.is_reserved()) { 2005 return NULL; 2006 } 2007 2008 // Paranoid checks: 2009 assert(base_address == NULL || (address)total_rs.base() == base_address, 2010 "Sanity (" PTR_FORMAT " vs " PTR_FORMAT ")", p2i(base_address), p2i(total_rs.base())); 2011 assert(is_aligned(total_rs.base(), archive_space_alignment), "Sanity"); 2012 assert(total_rs.size() == total_range_size, "Sanity"); 2013 assert(CompressedKlassPointers::is_valid_base((address)total_rs.base()), "Sanity"); 2014 2015 // Now split up the space into ccs and cds archive. For simplicity, just leave 2016 // the gap reserved at the end of the archive space. 2017 archive_space_rs = total_rs.first_part(ccs_begin_offset, 2018 (size_t)os::vm_allocation_granularity(), 2019 /*split=*/true); 2020 class_space_rs = total_rs.last_part(ccs_begin_offset); 2021 2022 assert(is_aligned(archive_space_rs.base(), archive_space_alignment), "Sanity"); 2023 assert(is_aligned(archive_space_rs.size(), archive_space_alignment), "Sanity"); 2024 assert(is_aligned(class_space_rs.base(), class_space_alignment), "Sanity"); 2025 assert(is_aligned(class_space_rs.size(), class_space_alignment), "Sanity"); 2026 2027 // NMT: fix up the space tags 2028 MemTracker::record_virtual_memory_type(archive_space_rs.base(), mtClassShared); 2029 MemTracker::record_virtual_memory_type(class_space_rs.base(), mtClass); 2030 2031 return archive_space_rs.base(); 2032 2033 #else 2034 ShouldNotReachHere(); 2035 return NULL; 2036 #endif 2037 2038 } 2039 2040 void MetaspaceShared::release_reserved_spaces(ReservedSpace& archive_space_rs, 2041 ReservedSpace& class_space_rs) { 2042 if (archive_space_rs.is_reserved()) { 2043 log_debug(cds)("Released shared space (archive) " INTPTR_FORMAT, p2i(archive_space_rs.base())); 2044 archive_space_rs.release(); 2045 } 2046 if (class_space_rs.is_reserved()) { 2047 log_debug(cds)("Released shared space (classes) " INTPTR_FORMAT, p2i(class_space_rs.base())); 2048 class_space_rs.release(); 2049 } 2050 } 2051 2052 static int archive_regions[] = {MetaspaceShared::mc, 2053 MetaspaceShared::rw, 2054 MetaspaceShared::ro}; 2055 static int archive_regions_count = 3; 2056 2057 MapArchiveResult MetaspaceShared::map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs) { 2058 assert(UseSharedSpaces, "must be runtime"); 2059 if (mapinfo == NULL) { 2060 return MAP_ARCHIVE_SUCCESS; // The dynamic archive has not been specified. No error has happened -- trivially succeeded. 2061 } 2062 2063 mapinfo->set_is_mapped(false); 2064 2065 if (mapinfo->alignment() != (size_t)os::vm_allocation_granularity()) { 2066 log_error(cds)("Unable to map CDS archive -- os::vm_allocation_granularity() expected: " SIZE_FORMAT 2067 " actual: %d", mapinfo->alignment(), os::vm_allocation_granularity()); 2068 return MAP_ARCHIVE_OTHER_FAILURE; 2069 } 2070 2071 MapArchiveResult result = 2072 mapinfo->map_regions(archive_regions, archive_regions_count, mapped_base_address, rs); 2073 2074 if (result != MAP_ARCHIVE_SUCCESS) { 2075 unmap_archive(mapinfo); 2076 return result; 2077 } 2078 2079 if (!mapinfo->validate_shared_path_table()) { 2080 unmap_archive(mapinfo); 2081 return MAP_ARCHIVE_OTHER_FAILURE; 2082 } 2083 2084 mapinfo->set_is_mapped(true); 2085 return MAP_ARCHIVE_SUCCESS; 2086 } 2087 2088 void MetaspaceShared::unmap_archive(FileMapInfo* mapinfo) { 2089 assert(UseSharedSpaces, "must be runtime"); 2090 if (mapinfo != NULL) { 2091 mapinfo->unmap_regions(archive_regions, archive_regions_count); 2092 mapinfo->set_is_mapped(false); 2093 } 2094 } 2095 2096 // Read the miscellaneous data from the shared file, and 2097 // serialize it out to its various destinations. 2098 2099 void MetaspaceShared::initialize_shared_spaces() { 2100 FileMapInfo *static_mapinfo = FileMapInfo::current_info(); 2101 _i2i_entry_code_buffers = static_mapinfo->i2i_entry_code_buffers(); 2102 _i2i_entry_code_buffers_size = static_mapinfo->i2i_entry_code_buffers_size(); 2103 char* buffer = static_mapinfo->cloned_vtables(); 2104 clone_cpp_vtables((intptr_t*)buffer); 2105 2106 // Verify various attributes of the archive, plus initialize the 2107 // shared string/symbol tables 2108 buffer = static_mapinfo->serialized_data(); 2109 intptr_t* array = (intptr_t*)buffer; 2110 ReadClosure rc(&array); 2111 serialize(&rc); 2112 2113 // Initialize the run-time symbol table. 2114 SymbolTable::create_table(); 2115 2116 static_mapinfo->patch_archived_heap_embedded_pointers(); 2117 2118 // Close the mapinfo file 2119 static_mapinfo->close(); 2120 2121 static_mapinfo->unmap_region(MetaspaceShared::bm); 2122 2123 FileMapInfo *dynamic_mapinfo = FileMapInfo::dynamic_info(); 2124 if (dynamic_mapinfo != NULL) { 2125 intptr_t* buffer = (intptr_t*)dynamic_mapinfo->serialized_data(); 2126 ReadClosure rc(&buffer); 2127 SymbolTable::serialize_shared_table_header(&rc, false); 2128 SystemDictionaryShared::serialize_dictionary_headers(&rc, false); 2129 dynamic_mapinfo->close(); 2130 } 2131 2132 if (PrintSharedArchiveAndExit) { 2133 if (PrintSharedDictionary) { 2134 tty->print_cr("\nShared classes:\n"); 2135 SystemDictionaryShared::print_on(tty); 2136 } 2137 if (FileMapInfo::current_info() == NULL || _archive_loading_failed) { 2138 tty->print_cr("archive is invalid"); 2139 vm_exit(1); 2140 } else { 2141 tty->print_cr("archive is valid"); 2142 vm_exit(0); 2143 } 2144 } 2145 } 2146 2147 // JVM/TI RedefineClasses() support: 2148 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 2149 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 2150 2151 if (UseSharedSpaces) { 2152 // remap the shared readonly space to shared readwrite, private 2153 FileMapInfo* mapinfo = FileMapInfo::current_info(); 2154 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 2155 return false; 2156 } 2157 if (FileMapInfo::dynamic_info() != NULL) { 2158 mapinfo = FileMapInfo::dynamic_info(); 2159 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 2160 return false; 2161 } 2162 } 2163 _remapped_readwrite = true; 2164 } 2165 return true; 2166 } 2167 2168 void MetaspaceShared::report_out_of_space(const char* name, size_t needed_bytes) { 2169 // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space. 2170 // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes 2171 // or so. 2172 _mc_region.print_out_of_space_msg(name, needed_bytes); 2173 _rw_region.print_out_of_space_msg(name, needed_bytes); 2174 _ro_region.print_out_of_space_msg(name, needed_bytes); 2175 2176 vm_exit_during_initialization(err_msg("Unable to allocate from '%s' region", name), 2177 "Please reduce the number of shared classes."); 2178 } 2179 2180 // This is used to relocate the pointers so that the base archive can be mapped at 2181 // MetaspaceShared::requested_base_address() without runtime relocation. 2182 intx MetaspaceShared::final_delta() { 2183 return intx(MetaspaceShared::requested_base_address()) // We want the base archive to be mapped to here at runtime 2184 - intx(SharedBaseAddress); // .. but the base archive is mapped at here at dump time 2185 } 2186 2187 void MetaspaceShared::print_on(outputStream* st) { 2188 if (UseSharedSpaces || DumpSharedSpaces) { 2189 st->print("CDS archive(s) mapped at: "); 2190 address base; 2191 address top; 2192 if (UseSharedSpaces) { // Runtime 2193 base = (address)MetaspaceObj::shared_metaspace_base(); 2194 address static_top = (address)_shared_metaspace_static_top; 2195 top = (address)MetaspaceObj::shared_metaspace_top(); 2196 st->print("[" PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(static_top), p2i(top)); 2197 } else if (DumpSharedSpaces) { // Dump Time 2198 base = (address)_shared_rs.base(); 2199 top = (address)_shared_rs.end(); 2200 st->print("[" PTR_FORMAT "-" PTR_FORMAT "), ", p2i(base), p2i(top)); 2201 } 2202 st->print("size " SIZE_FORMAT ", ", top - base); 2203 st->print("SharedBaseAddress: " PTR_FORMAT ", ArchiveRelocationMode: %d.", SharedBaseAddress, (int)ArchiveRelocationMode); 2204 } else { 2205 st->print("CDS disabled."); 2206 } 2207 st->cr(); 2208 } 2209 2210 2211 2212 2213