1 /* 2 * Copyright (c) 2012, 2016, 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/classListParser.hpp" 27 #include "classfile/classLoaderExt.hpp" 28 #include "classfile/dictionary.hpp" 29 #include "classfile/loaderConstraints.hpp" 30 #include "classfile/placeholders.hpp" 31 #include "classfile/sharedClassUtil.hpp" 32 #include "classfile/symbolTable.hpp" 33 #include "classfile/systemDictionary.hpp" 34 #include "classfile/systemDictionaryShared.hpp" 35 #include "code/codeCache.hpp" 36 #include "gc/shared/gcLocker.hpp" 37 #include "interpreter/bytecodeStream.hpp" 38 #include "interpreter/bytecodes.hpp" 39 #include "memory/filemap.hpp" 40 #include "memory/metaspace.hpp" 41 #include "memory/metaspaceShared.hpp" 42 #include "memory/resourceArea.hpp" 43 #include "oops/objArrayOop.hpp" 44 #include "oops/oop.inline.hpp" 45 #include "runtime/timerTrace.hpp" 46 #include "runtime/os.hpp" 47 #include "runtime/signature.hpp" 48 #include "runtime/vmThread.hpp" 49 #include "runtime/vm_operations.hpp" 50 #include "utilities/defaultStream.hpp" 51 #include "utilities/hashtable.inline.hpp" 52 53 int MetaspaceShared::_max_alignment = 0; 54 55 ReservedSpace* MetaspaceShared::_shared_rs = NULL; 56 57 MetaspaceSharedStats MetaspaceShared::_stats; 58 59 bool MetaspaceShared::_link_classes_made_progress; 60 bool MetaspaceShared::_check_classes_made_progress; 61 bool MetaspaceShared::_has_error_classes; 62 bool MetaspaceShared::_archive_loading_failed = false; 63 bool MetaspaceShared::_remapped_readwrite = false; 64 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL; 65 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0; 66 SharedMiscRegion MetaspaceShared::_mc; 67 SharedMiscRegion MetaspaceShared::_md; 68 SharedMiscRegion MetaspaceShared::_od; 69 70 void SharedMiscRegion::initialize(ReservedSpace rs, size_t committed_byte_size, SharedSpaceType space_type) { 71 _vs.initialize(rs, committed_byte_size); 72 _alloc_top = _vs.low(); 73 _space_type = space_type; 74 } 75 76 // NOT thread-safe, but this is called during dump time in single-threaded mode. 77 char* SharedMiscRegion::alloc(size_t num_bytes) { 78 assert(DumpSharedSpaces, "dump time only"); 79 size_t alignment = sizeof(char*); 80 num_bytes = align_size_up(num_bytes, alignment); 81 _alloc_top = (char*)align_ptr_up(_alloc_top, alignment); 82 if (_alloc_top + num_bytes > _vs.high()) { 83 report_out_of_shared_space(_space_type); 84 } 85 86 char* p = _alloc_top; 87 _alloc_top += num_bytes; 88 89 memset(p, 0, num_bytes); 90 return p; 91 } 92 93 void MetaspaceShared::initialize_shared_rs(ReservedSpace* rs) { 94 assert(DumpSharedSpaces, "dump time only"); 95 _shared_rs = rs; 96 97 size_t core_spaces_size = FileMapInfo::core_spaces_size(); 98 size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize; 99 100 // Split into the core and optional sections 101 ReservedSpace core_data = _shared_rs->first_part(core_spaces_size); 102 ReservedSpace optional_data = _shared_rs->last_part(core_spaces_size); 103 104 // The RO/RW and the misc sections 105 ReservedSpace shared_ro_rw = core_data.first_part(metadata_size); 106 ReservedSpace misc_section = core_data.last_part(metadata_size); 107 108 // Now split the misc code and misc data sections. 109 ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); 110 ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); 111 112 _md.initialize(md_rs, SharedMiscDataSize, SharedMiscData); 113 _mc.initialize(mc_rs, SharedMiscCodeSize, SharedMiscCode); 114 _od.initialize(optional_data, metadata_size, SharedOptional); 115 } 116 117 // Read/write a data stream for restoring/preserving metadata pointers and 118 // miscellaneous data from/to the shared archive file. 119 120 void MetaspaceShared::serialize(SerializeClosure* soc, GrowableArray<MemRegion> *string_space, 121 size_t* space_size) { 122 int tag = 0; 123 soc->do_tag(--tag); 124 125 // Verify the sizes of various metadata in the system. 126 soc->do_tag(sizeof(Method)); 127 soc->do_tag(sizeof(ConstMethod)); 128 soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE)); 129 soc->do_tag(sizeof(ConstantPool)); 130 soc->do_tag(sizeof(ConstantPoolCache)); 131 soc->do_tag(objArrayOopDesc::base_offset_in_bytes()); 132 soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE)); 133 soc->do_tag(sizeof(Symbol)); 134 135 // Dump/restore miscellaneous metadata. 136 Universe::serialize(soc, true); 137 soc->do_tag(--tag); 138 139 // Dump/restore references to commonly used names and signatures. 140 vmSymbols::serialize(soc); 141 soc->do_tag(--tag); 142 143 // Dump/restore the symbol and string tables 144 SymbolTable::serialize(soc); 145 StringTable::serialize(soc, string_space, space_size); 146 soc->do_tag(--tag); 147 148 // Dump/restore the misc information for system dictionary 149 SystemDictionaryShared::serialize(soc); 150 soc->do_tag(--tag); 151 152 soc->do_tag(666); 153 } 154 155 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) { 156 if (DumpSharedSpaces) { 157 if (_cds_i2i_entry_code_buffers == NULL) { 158 _cds_i2i_entry_code_buffers = (address)misc_data_space_alloc(total_size); 159 _cds_i2i_entry_code_buffers_size = total_size; 160 } 161 } else if (UseSharedSpaces) { 162 assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized"); 163 } else { 164 return NULL; 165 } 166 167 assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change"); 168 return _cds_i2i_entry_code_buffers; 169 } 170 171 // CDS code for dumping shared archive. 172 173 // Global object for holding classes that have been loaded. Since this 174 // is run at a safepoint just before exit, this is the entire set of classes. 175 static GrowableArray<Klass*>* _global_klass_objects; 176 static void collect_classes(Klass* k) { 177 _global_klass_objects->append_if_missing(k); 178 if (k->is_instance_klass()) { 179 // Add in the array classes too 180 InstanceKlass* ik = InstanceKlass::cast(k); 181 ik->array_klasses_do(collect_classes); 182 } 183 } 184 185 static void collect_classes2(Klass* k, ClassLoaderData* class_data) { 186 collect_classes(k); 187 } 188 189 static void remove_unshareable_in_classes() { 190 for (int i = 0; i < _global_klass_objects->length(); i++) { 191 Klass* k = _global_klass_objects->at(i); 192 k->remove_unshareable_info(); 193 } 194 } 195 196 static void rewrite_nofast_bytecode(Method* method) { 197 RawBytecodeStream bcs(method); 198 while (!bcs.is_last_bytecode()) { 199 Bytecodes::Code opcode = bcs.raw_next(); 200 switch (opcode) { 201 case Bytecodes::_getfield: *bcs.bcp() = Bytecodes::_nofast_getfield; break; 202 case Bytecodes::_putfield: *bcs.bcp() = Bytecodes::_nofast_putfield; break; 203 case Bytecodes::_aload_0: *bcs.bcp() = Bytecodes::_nofast_aload_0; break; 204 case Bytecodes::_iload: { 205 if (!bcs.is_wide()) { 206 *bcs.bcp() = Bytecodes::_nofast_iload; 207 } 208 break; 209 } 210 default: break; 211 } 212 } 213 } 214 215 // Walk all methods in the class list to ensure that they won't be modified at 216 // run time. This includes: 217 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified 218 // at run time by RewriteBytecodes/RewriteFrequentPairs 219 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time. 220 static void rewrite_nofast_bytecodes_and_calculate_fingerprints() { 221 for (int i = 0; i < _global_klass_objects->length(); i++) { 222 Klass* k = _global_klass_objects->at(i); 223 if (k->is_instance_klass()) { 224 InstanceKlass* ik = InstanceKlass::cast(k); 225 for (int i = 0; i < ik->methods()->length(); i++) { 226 Method* m = ik->methods()->at(i); 227 rewrite_nofast_bytecode(m); 228 Fingerprinter fp(m); 229 // The side effect of this call sets method's fingerprint field. 230 fp.fingerprint(); 231 } 232 } 233 } 234 } 235 236 // Patch C++ vtable pointer in metadata. 237 238 // Klass and other metadata objects contain references to c++ vtables in the 239 // JVM library. 240 // Fix them to point to our constructed vtables. However, don't iterate 241 // across the space while doing this, as that causes the vtables to be 242 // patched, undoing our useful work. Instead, iterate to make a list, 243 // then use the list to do the fixing. 244 // 245 // Our constructed vtables: 246 // Dump time: 247 // 1. init_self_patching_vtbl_list: table of pointers to current virtual method addrs 248 // 2. generate_vtable_methods: create jump table, appended to above vtbl_list 249 // 3. patch_klass_vtables: for Klass list, patch the vtable entry in klass and 250 // associated metadata to point to jump table rather than to current vtbl 251 // Table layout: NOTE FIXED SIZE 252 // 1. vtbl pointers 253 // 2. #Klass X #virtual methods per Klass 254 // 1 entry for each, in the order: 255 // Klass1:method1 entry, Klass1:method2 entry, ... Klass1:method<num_virtuals> entry 256 // Klass2:method1 entry, Klass2:method2 entry, ... Klass2:method<num_virtuals> entry 257 // ... 258 // Klass<vtbl_list_size>:method1 entry, Klass<vtbl_list_size>:method2 entry, 259 // ... Klass<vtbl_list_size>:method<num_virtuals> entry 260 // Sample entry: (Sparc): 261 // save(sp, -256, sp) 262 // ba,pt common_code 263 // mov XXX, %L0 %L0 gets: Klass index <<8 + method index (note: max method index 255) 264 // 265 // Restore time: 266 // 1. initialize_shared_space: reserve space for table 267 // 2. init_self_patching_vtbl_list: update pointers to NEW virtual method addrs in text 268 // 269 // Execution time: 270 // First virtual method call for any object of these metadata types: 271 // 1. object->klass 272 // 2. vtable entry for that klass points to the jump table entries 273 // 3. branches to common_code with %O0/klass, %L0: Klass index <<8 + method index 274 // 4. common_code: 275 // Get address of new vtbl pointer for this Klass from updated table 276 // Update new vtbl pointer in the Klass: future virtual calls go direct 277 // Jump to method, using new vtbl pointer and method index 278 279 280 static void* find_matching_vtbl_ptr(void** vtbl_list, void* new_vtable_start, void* obj) { 281 void* old_vtbl_ptr = *(void**)obj; 282 for (int i = 0; i < MetaspaceShared::vtbl_list_size; i++) { 283 if (vtbl_list[i] == old_vtbl_ptr) { 284 return (void**)new_vtable_start + i * MetaspaceShared::num_virtuals; 285 } 286 } 287 ShouldNotReachHere(); 288 return NULL; 289 } 290 291 // Assumes the vtable is in first slot in object. 292 static void patch_klass_vtables(void** vtbl_list, void* new_vtable_start) { 293 int n = _global_klass_objects->length(); 294 for (int i = 0; i < n; i++) { 295 Klass* obj = _global_klass_objects->at(i); 296 // Note is_instance_klass() is a virtual call in debug. After patching vtables 297 // all virtual calls on the dummy vtables will restore the original! 298 if (obj->is_instance_klass()) { 299 InstanceKlass* ik = InstanceKlass::cast(obj); 300 *(void**)ik = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, ik); 301 ConstantPool* cp = ik->constants(); 302 *(void**)cp = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, cp); 303 for (int j = 0; j < ik->methods()->length(); j++) { 304 Method* m = ik->methods()->at(j); 305 *(void**)m = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, m); 306 } 307 } else { 308 // Array klasses 309 Klass* k = obj; 310 *(void**)k = find_matching_vtbl_ptr(vtbl_list, new_vtable_start, k); 311 } 312 } 313 } 314 315 // Closure for serializing initialization data out to a data area to be 316 // written to the shared file. 317 318 class WriteClosure : public SerializeClosure { 319 private: 320 intptr_t* top; 321 char* end; 322 323 inline void check_space() { 324 if ((char*)top + sizeof(intptr_t) > end) { 325 report_out_of_shared_space(SharedMiscData); 326 } 327 } 328 329 public: 330 WriteClosure(char* md_top, char* md_end) { 331 top = (intptr_t*)md_top; 332 end = md_end; 333 } 334 335 char* get_top() { return (char*)top; } 336 337 void do_ptr(void** p) { 338 check_space(); 339 *top = (intptr_t)*p; 340 ++top; 341 } 342 343 void do_u4(u4* p) { 344 void* ptr = (void*)(uintx(*p)); 345 do_ptr(&ptr); 346 } 347 348 void do_tag(int tag) { 349 check_space(); 350 *top = (intptr_t)tag; 351 ++top; 352 } 353 354 void do_region(u_char* start, size_t size) { 355 if ((char*)top + size > end) { 356 report_out_of_shared_space(SharedMiscData); 357 } 358 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 359 assert(size % sizeof(intptr_t) == 0, "bad size"); 360 do_tag((int)size); 361 while (size > 0) { 362 *top = *(intptr_t*)start; 363 ++top; 364 start += sizeof(intptr_t); 365 size -= sizeof(intptr_t); 366 } 367 } 368 369 bool reading() const { return false; } 370 }; 371 372 // This is for dumping detailed statistics for the allocations 373 // in the shared spaces. 374 class DumpAllocClosure : public Metaspace::AllocRecordClosure { 375 public: 376 377 // Here's poor man's enum inheritance 378 #define SHAREDSPACE_OBJ_TYPES_DO(f) \ 379 METASPACE_OBJ_TYPES_DO(f) \ 380 f(SymbolHashentry) \ 381 f(SymbolBucket) \ 382 f(StringHashentry) \ 383 f(StringBucket) \ 384 f(Other) 385 386 #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type, 387 #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name; 388 389 enum Type { 390 // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc 391 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE) 392 _number_of_types 393 }; 394 395 static const char * type_name(Type type) { 396 switch(type) { 397 SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE) 398 default: 399 ShouldNotReachHere(); 400 return NULL; 401 } 402 } 403 404 public: 405 enum { 406 RO = 0, 407 RW = 1 408 }; 409 410 int _counts[2][_number_of_types]; 411 int _bytes [2][_number_of_types]; 412 int _which; 413 414 DumpAllocClosure() { 415 memset(_counts, 0, sizeof(_counts)); 416 memset(_bytes, 0, sizeof(_bytes)); 417 }; 418 419 void iterate_metaspace(Metaspace* space, int which) { 420 assert(which == RO || which == RW, "sanity"); 421 _which = which; 422 space->iterate(this); 423 } 424 425 virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) { 426 assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity"); 427 _counts[_which][type] ++; 428 _bytes [_which][type] += byte_size; 429 } 430 431 void dump_stats(int ro_all, int rw_all, int md_all, int mc_all); 432 }; 433 434 void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) { 435 rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write 436 int other_bytes = md_all + mc_all; 437 438 // Calculate size of data that was not allocated by Metaspace::allocate() 439 MetaspaceSharedStats *stats = MetaspaceShared::stats(); 440 441 // symbols 442 _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count; 443 _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes; 444 _bytes [RO][TypeArrayU4Type] -= stats->symbol.hashentry_bytes; 445 446 _counts[RO][SymbolBucketType] = stats->symbol.bucket_count; 447 _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes; 448 _bytes [RO][TypeArrayU4Type] -= stats->symbol.bucket_bytes; 449 450 // strings 451 _counts[RO][StringHashentryType] = stats->string.hashentry_count; 452 _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes; 453 _bytes [RO][TypeArrayU4Type] -= stats->string.hashentry_bytes; 454 455 _counts[RO][StringBucketType] = stats->string.bucket_count; 456 _bytes [RO][StringBucketType] = stats->string.bucket_bytes; 457 _bytes [RO][TypeArrayU4Type] -= stats->string.bucket_bytes; 458 459 // TODO: count things like dictionary, vtable, etc 460 _bytes[RW][OtherType] = other_bytes; 461 462 // prevent divide-by-zero 463 if (ro_all < 1) { 464 ro_all = 1; 465 } 466 if (rw_all < 1) { 467 rw_all = 1; 468 } 469 470 int all_ro_count = 0; 471 int all_ro_bytes = 0; 472 int all_rw_count = 0; 473 int all_rw_bytes = 0; 474 475 // To make fmt_stats be a syntactic constant (for format warnings), use #define. 476 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f" 477 const char *sep = "--------------------+---------------------------+---------------------------+--------------------------"; 478 const char *hdr = " ro_cnt ro_bytes % | rw_cnt rw_bytes % | all_cnt all_bytes %"; 479 480 tty->print_cr("Detailed metadata info (rw includes md and mc):"); 481 tty->print_cr("%s", hdr); 482 tty->print_cr("%s", sep); 483 for (int type = 0; type < int(_number_of_types); type ++) { 484 const char *name = type_name((Type)type); 485 int ro_count = _counts[RO][type]; 486 int ro_bytes = _bytes [RO][type]; 487 int rw_count = _counts[RW][type]; 488 int rw_bytes = _bytes [RW][type]; 489 int count = ro_count + rw_count; 490 int bytes = ro_bytes + rw_bytes; 491 492 double ro_perc = 100.0 * double(ro_bytes) / double(ro_all); 493 double rw_perc = 100.0 * double(rw_bytes) / double(rw_all); 494 double perc = 100.0 * double(bytes) / double(ro_all + rw_all); 495 496 tty->print_cr(fmt_stats, name, 497 ro_count, ro_bytes, ro_perc, 498 rw_count, rw_bytes, rw_perc, 499 count, bytes, perc); 500 501 all_ro_count += ro_count; 502 all_ro_bytes += ro_bytes; 503 all_rw_count += rw_count; 504 all_rw_bytes += rw_bytes; 505 } 506 507 int all_count = all_ro_count + all_rw_count; 508 int all_bytes = all_ro_bytes + all_rw_bytes; 509 510 double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all); 511 double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all); 512 double all_perc = 100.0 * double(all_bytes) / double(ro_all + rw_all); 513 514 tty->print_cr("%s", sep); 515 tty->print_cr(fmt_stats, "Total", 516 all_ro_count, all_ro_bytes, all_ro_perc, 517 all_rw_count, all_rw_bytes, all_rw_perc, 518 all_count, all_bytes, all_perc); 519 520 assert(all_ro_bytes == ro_all, "everything should have been counted"); 521 assert(all_rw_bytes == rw_all, "everything should have been counted"); 522 #undef fmt_stats 523 } 524 525 // Populate the shared space. 526 527 class VM_PopulateDumpSharedSpace: public VM_Operation { 528 private: 529 ClassLoaderData* _loader_data; 530 GrowableArray<Klass*> *_class_promote_order; 531 VirtualSpace _md_vs; 532 VirtualSpace _mc_vs; 533 VirtualSpace _od_vs; 534 GrowableArray<MemRegion> *_string_regions; 535 536 public: 537 VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, 538 GrowableArray<Klass*> *class_promote_order) : 539 _loader_data(loader_data) { 540 _class_promote_order = class_promote_order; 541 } 542 543 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 544 void doit(); // outline because gdb sucks 545 546 private: 547 void handle_misc_data_space_failure(bool success) { 548 if (!success) { 549 report_out_of_shared_space(SharedMiscData); 550 } 551 } 552 }; // class VM_PopulateDumpSharedSpace 553 554 void VM_PopulateDumpSharedSpace::doit() { 555 Thread* THREAD = VMThread::vm_thread(); 556 NOT_PRODUCT(SystemDictionary::verify();) 557 // The following guarantee is meant to ensure that no loader constraints 558 // exist yet, since the constraints table is not shared. This becomes 559 // more important now that we don't re-initialize vtables/itables for 560 // shared classes at runtime, where constraints were previously created. 561 guarantee(SystemDictionary::constraints()->number_of_entries() == 0, 562 "loader constraints are not saved"); 563 guarantee(SystemDictionary::placeholders()->number_of_entries() == 0, 564 "placeholders are not saved"); 565 // Revisit and implement this if we prelink method handle call sites: 566 guarantee(SystemDictionary::invoke_method_table() == NULL || 567 SystemDictionary::invoke_method_table()->number_of_entries() == 0, 568 "invoke method table is not saved"); 569 570 // At this point, many classes have been loaded. 571 // Gather systemDictionary classes in a global array and do everything to 572 // that so we don't have to walk the SystemDictionary again. 573 _global_klass_objects = new GrowableArray<Klass*>(1000); 574 Universe::basic_type_classes_do(collect_classes); 575 576 // Need to call SystemDictionary::classes_do(void f(Klass*, ClassLoaderData*)) 577 // as we may have some classes with NULL ClassLoaderData* in the dictionary. Other 578 // variants of SystemDictionary::classes_do will skip those classes. 579 SystemDictionary::classes_do(collect_classes2); 580 581 tty->print_cr("Number of classes %d", _global_klass_objects->length()); 582 { 583 int num_type_array = 0, num_obj_array = 0, num_inst = 0; 584 for (int i = 0; i < _global_klass_objects->length(); i++) { 585 Klass* k = _global_klass_objects->at(i); 586 if (k->is_instance_klass()) { 587 num_inst ++; 588 } else if (k->is_objArray_klass()) { 589 num_obj_array ++; 590 } else { 591 assert(k->is_typeArray_klass(), "sanity"); 592 num_type_array ++; 593 } 594 } 595 tty->print_cr(" instance classes = %5d", num_inst); 596 tty->print_cr(" obj array classes = %5d", num_obj_array); 597 tty->print_cr(" type array classes = %5d", num_type_array); 598 } 599 600 601 // Ensure the ConstMethods won't be modified at run-time 602 tty->print("Updating ConstMethods ... "); 603 rewrite_nofast_bytecodes_and_calculate_fingerprints(); 604 tty->print_cr("done. "); 605 606 // Remove all references outside the metadata 607 tty->print("Removing unshareable information ... "); 608 remove_unshareable_in_classes(); 609 tty->print_cr("done. "); 610 611 // Set up the misc data, misc code and optional data segments. 612 _md_vs = *MetaspaceShared::misc_data_region()->virtual_space(); 613 _mc_vs = *MetaspaceShared::misc_code_region()->virtual_space(); 614 _od_vs = *MetaspaceShared::optional_data_region()->virtual_space(); 615 char* md_low = _md_vs.low(); 616 char* md_top = MetaspaceShared::misc_data_region()->alloc_top(); 617 char* md_end = _md_vs.high(); 618 char* mc_low = _mc_vs.low(); 619 char* mc_top = MetaspaceShared::misc_code_region()->alloc_top(); 620 char* mc_end = _mc_vs.high(); 621 char* od_low = _od_vs.low(); 622 char* od_top = MetaspaceShared::optional_data_region()->alloc_top(); 623 char* od_end = _od_vs.high(); 624 625 // Reserve space for the list of Klass*s whose vtables are used 626 // for patching others as needed. 627 628 void** vtbl_list = (void**)md_top; 629 int vtbl_list_size = MetaspaceShared::vtbl_list_size; 630 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 631 632 md_top += vtbl_list_size * sizeof(void*); 633 void* vtable = md_top; 634 635 // Reserve space for a new dummy vtable for klass objects in the 636 // heap. Generate self-patching vtable entries. 637 638 MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable, 639 &md_top, md_end, 640 &mc_top, mc_end); 641 642 guarantee(md_top <= md_end, "Insufficient space for vtables."); 643 644 // Reorder the system dictionary. (Moving the symbols affects 645 // how the hash table indices are calculated.) 646 // Not doing this either. 647 648 SystemDictionary::reorder_dictionary(); 649 NOT_PRODUCT(SystemDictionary::verify();) 650 SystemDictionary::reverse(); 651 SystemDictionary::copy_buckets(&md_top, md_end); 652 653 SystemDictionary::copy_table(&md_top, md_end); 654 655 // Write the other data to the output array. 656 // SymbolTable, StringTable and extra information for system dictionary 657 NOT_PRODUCT(SymbolTable::verify()); 658 NOT_PRODUCT(StringTable::verify()); 659 size_t ss_bytes = 0; 660 char* ss_low; 661 // The string space has maximum two regions. See FileMapInfo::write_string_regions() for details. 662 _string_regions = new GrowableArray<MemRegion>(2); 663 664 WriteClosure wc(md_top, md_end); 665 MetaspaceShared::serialize(&wc, _string_regions, &ss_bytes); 666 md_top = wc.get_top(); 667 ss_low = _string_regions->is_empty() ? NULL : (char*)_string_regions->first().start(); 668 669 // Print shared spaces all the time 670 Metaspace* ro_space = _loader_data->ro_metaspace(); 671 Metaspace* rw_space = _loader_data->rw_metaspace(); 672 673 // Allocated size of each space (may not be all occupied) 674 const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType); 675 const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType); 676 const size_t md_alloced = md_end-md_low; 677 const size_t mc_alloced = mc_end-mc_low; 678 const size_t od_alloced = od_end-od_low; 679 const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced 680 + ss_bytes + od_alloced; 681 682 // Occupied size of each space. 683 const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType); 684 const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType); 685 const size_t md_bytes = size_t(md_top - md_low); 686 const size_t mc_bytes = size_t(mc_top - mc_low); 687 const size_t od_bytes = size_t(od_top - od_low); 688 689 // Percent of total size 690 const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes + ss_bytes + od_bytes; 691 const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; 692 const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; 693 const double md_t_perc = md_bytes / double(total_bytes) * 100.0; 694 const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; 695 const double ss_t_perc = ss_bytes / double(total_bytes) * 100.0; 696 const double od_t_perc = od_bytes / double(total_bytes) * 100.0; 697 698 // Percent of fullness of each space 699 const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; 700 const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; 701 const double md_u_perc = md_bytes / double(md_alloced) * 100.0; 702 const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; 703 const double od_u_perc = od_bytes / double(od_alloced) * 100.0; 704 const double total_u_perc = total_bytes / double(total_alloced) * 100.0; 705 706 #define fmt_space "%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT 707 tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, p2i(ro_space->bottom())); 708 tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, p2i(rw_space->bottom())); 709 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, p2i(md_low)); 710 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, p2i(mc_low)); 711 tty->print_cr(fmt_space, "st", ss_bytes, ss_t_perc, ss_bytes, 100.0, p2i(ss_low)); 712 tty->print_cr(fmt_space, "od", od_bytes, od_t_perc, od_alloced, od_u_perc, p2i(od_low)); 713 tty->print_cr("total : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]", 714 total_bytes, total_alloced, total_u_perc); 715 716 // Update the vtable pointers in all of the Klass objects in the 717 // heap. They should point to newly generated vtable. 718 patch_klass_vtables(vtbl_list, vtable); 719 720 // dunno what this is for. 721 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass); 722 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); 723 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); 724 725 // Create and write the archive file that maps the shared spaces. 726 727 FileMapInfo* mapinfo = new FileMapInfo(); 728 mapinfo->populate_header(MetaspaceShared::max_alignment()); 729 mapinfo->set_misc_data_patching_start((char*)vtbl_list); 730 mapinfo->set_cds_i2i_entry_code_buffers(MetaspaceShared::cds_i2i_entry_code_buffers()); 731 mapinfo->set_cds_i2i_entry_code_buffers_size(MetaspaceShared::cds_i2i_entry_code_buffers_size()); 732 733 for (int pass=1; pass<=2; pass++) { 734 if (pass == 1) { 735 // The first pass doesn't actually write the data to disk. All it 736 // does is to update the fields in the mapinfo->_header. 737 } else { 738 // After the first pass, the contents of mapinfo->_header are finalized, 739 // so we can compute the header's CRC, and write the contents of the header 740 // and the regions into disk. 741 mapinfo->open_for_write(); 742 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 743 } 744 mapinfo->write_header(); 745 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 746 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 747 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 748 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 749 SharedMiscDataSize, 750 false, true); 751 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 752 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 753 SharedMiscCodeSize, 754 true, true); 755 mapinfo->write_string_regions(_string_regions); 756 mapinfo->write_region(MetaspaceShared::od, _od_vs.low(), 757 pointer_delta(od_top, _od_vs.low(), sizeof(char)), 758 pointer_delta(od_end, _od_vs.low(), sizeof(char)), 759 true, false); 760 } 761 762 mapinfo->close(); 763 764 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); 765 os::free(saved_vtbl); 766 767 if (PrintSharedSpaces) { 768 DumpAllocClosure dac; 769 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO); 770 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW); 771 772 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); 773 } 774 #undef fmt_space 775 } 776 777 778 void MetaspaceShared::link_one_shared_class(Klass* k, TRAPS) { 779 if (k->is_instance_klass()) { 780 InstanceKlass* ik = InstanceKlass::cast(k); 781 // Link the class to cause the bytecodes to be rewritten and the 782 // cpcache to be created. Class verification is done according 783 // to -Xverify setting. 784 _link_classes_made_progress |= try_link_class(ik, THREAD); 785 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 786 } 787 } 788 789 void MetaspaceShared::check_one_shared_class(Klass* k) { 790 if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) { 791 _check_classes_made_progress = true; 792 } 793 } 794 795 void MetaspaceShared::check_shared_class_loader_type(Klass* k) { 796 if (k->is_instance_klass()) { 797 InstanceKlass* ik = InstanceKlass::cast(k); 798 u2 loader_type = ik->loader_type(); 799 ResourceMark rm; 800 guarantee(loader_type != 0, 801 "Class loader type is not set for this class %s", ik->name()->as_C_string()); 802 } 803 } 804 805 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) { 806 // We need to iterate because verification may cause additional classes 807 // to be loaded. 808 do { 809 _link_classes_made_progress = false; 810 SystemDictionary::classes_do(link_one_shared_class, THREAD); 811 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 812 } while (_link_classes_made_progress); 813 814 if (_has_error_classes) { 815 // Mark all classes whose super class or interfaces failed verification. 816 do { 817 // Not completely sure if we need to do this iteratively. Anyway, 818 // we should come here only if there are unverifiable classes, which 819 // shouldn't happen in normal cases. So better safe than sorry. 820 _check_classes_made_progress = false; 821 SystemDictionary::classes_do(check_one_shared_class); 822 } while (_check_classes_made_progress); 823 824 if (IgnoreUnverifiableClassesDuringDump) { 825 // This is useful when running JCK or SQE tests. You should not 826 // enable this when running real apps. 827 SystemDictionary::remove_classes_in_error_state(); 828 } else { 829 tty->print_cr("Please remove the unverifiable classes from your class list and try again"); 830 exit(1); 831 } 832 } 833 834 // Copy the verification constraints from C_HEAP-alloced GrowableArrays to RO-alloced 835 // Arrays 836 SystemDictionaryShared::finalize_verification_constraints(); 837 } 838 839 void MetaspaceShared::prepare_for_dumping() { 840 Arguments::check_unsupported_dumping_properties(); 841 ClassLoader::initialize_shared_path(); 842 FileMapInfo::allocate_classpath_entry_table(); 843 } 844 845 // Preload classes from a list, populate the shared spaces and dump to a 846 // file. 847 void MetaspaceShared::preload_and_dump(TRAPS) { 848 { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime)); 849 ResourceMark rm; 850 char class_list_path_str[JVM_MAXPATHLEN]; 851 852 tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT, 853 MetaspaceShared::shared_rs()->size(), 854 p2i(MetaspaceShared::shared_rs()->base())); 855 856 // Preload classes to be shared. 857 // Should use some os:: method rather than fopen() here. aB. 858 const char* class_list_path; 859 if (SharedClassListFile == NULL) { 860 // Construct the path to the class list (in jre/lib) 861 // Walk up two directories from the location of the VM and 862 // optionally tack on "lib" (depending on platform) 863 os::jvm_path(class_list_path_str, sizeof(class_list_path_str)); 864 for (int i = 0; i < 3; i++) { 865 char *end = strrchr(class_list_path_str, *os::file_separator()); 866 if (end != NULL) *end = '\0'; 867 } 868 int class_list_path_len = (int)strlen(class_list_path_str); 869 if (class_list_path_len >= 3) { 870 if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) { 871 if (class_list_path_len < JVM_MAXPATHLEN - 4) { 872 jio_snprintf(class_list_path_str + class_list_path_len, 873 sizeof(class_list_path_str) - class_list_path_len, 874 "%slib", os::file_separator()); 875 class_list_path_len += 4; 876 } 877 } 878 } 879 if (class_list_path_len < JVM_MAXPATHLEN - 10) { 880 jio_snprintf(class_list_path_str + class_list_path_len, 881 sizeof(class_list_path_str) - class_list_path_len, 882 "%sclasslist", os::file_separator()); 883 } 884 class_list_path = class_list_path_str; 885 } else { 886 class_list_path = SharedClassListFile; 887 } 888 889 int class_count = 0; 890 GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>(); 891 892 // sun.io.Converters 893 static const char obj_array_sig[] = "[[Ljava/lang/Object;"; 894 SymbolTable::new_permanent_symbol(obj_array_sig, THREAD); 895 896 // java.util.HashMap 897 static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;"; 898 SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD); 899 900 // Need to allocate the op here: 901 // op.misc_data_space_alloc() will be called during preload_and_dump(). 902 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 903 VM_PopulateDumpSharedSpace op(loader_data, class_promote_order); 904 905 tty->print_cr("Loading classes to share ..."); 906 _has_error_classes = false; 907 class_count += preload_and_dump(class_list_path, class_promote_order, 908 THREAD); 909 if (ExtraSharedClassListFile) { 910 class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order, 911 THREAD); 912 } 913 tty->print_cr("Loading classes to share: done."); 914 915 if (PrintSharedSpaces) { 916 tty->print_cr("Shared spaces: preloaded %d classes", class_count); 917 } 918 919 // Rewrite and link classes 920 tty->print_cr("Rewriting and linking classes ..."); 921 922 // Link any classes which got missed. This would happen if we have loaded classes that 923 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K 924 // fails verification, all other interfaces that were not specified in the classlist but 925 // are implemented by K are not verified. 926 link_and_cleanup_shared_classes(CATCH); 927 tty->print_cr("Rewriting and linking classes: done"); 928 929 VMThread::execute(&op); 930 } 931 // Since various initialization steps have been undone by this process, 932 // it is not reasonable to continue running a java process. 933 exit(0); 934 } 935 936 937 int MetaspaceShared::preload_and_dump(const char* class_list_path, 938 GrowableArray<Klass*>* class_promote_order, 939 TRAPS) { 940 ClassListParser parser(class_list_path); 941 int class_count = 0; 942 943 while (parser.parse_one_line()) { 944 Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD); 945 946 CLEAR_PENDING_EXCEPTION; 947 if (klass != NULL) { 948 if (PrintSharedSpaces && Verbose && WizardMode) { 949 ResourceMark rm; 950 tty->print_cr("Shared spaces preloaded: %s", klass->external_name()); 951 } 952 953 InstanceKlass* ik = InstanceKlass::cast(klass); 954 955 // Should be class load order as per -Xlog:class+preorder 956 class_promote_order->append(ik); 957 958 // Link the class to cause the bytecodes to be rewritten and the 959 // cpcache to be created. The linking is done as soon as classes 960 // are loaded in order that the related data structures (klass and 961 // cpCache) are located together. 962 try_link_class(ik, THREAD); 963 guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class"); 964 965 class_count++; 966 } 967 } 968 969 return class_count; 970 } 971 972 // Returns true if the class's status has changed 973 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) { 974 assert(DumpSharedSpaces, "should only be called during dumping"); 975 if (ik->init_state() < InstanceKlass::linked) { 976 bool saved = BytecodeVerificationLocal; 977 if (!(ik->is_shared_boot_class())) { 978 // The verification decision is based on BytecodeVerificationRemote 979 // for non-system classes. Since we are using the NULL classloader 980 // to load non-system classes during dumping, we need to temporarily 981 // change BytecodeVerificationLocal to be the same as 982 // BytecodeVerificationRemote. Note this can cause the parent system 983 // classes also being verified. The extra overhead is acceptable during 984 // dumping. 985 BytecodeVerificationLocal = BytecodeVerificationRemote; 986 } 987 ik->link_class(THREAD); 988 if (HAS_PENDING_EXCEPTION) { 989 ResourceMark rm; 990 tty->print_cr("Preload Warning: Verification failed for %s", 991 ik->external_name()); 992 CLEAR_PENDING_EXCEPTION; 993 ik->set_in_error_state(); 994 _has_error_classes = true; 995 } 996 BytecodeVerificationLocal = saved; 997 return true; 998 } else { 999 return false; 1000 } 1001 } 1002 1003 // Closure for serializing initialization data in from a data area 1004 // (ptr_array) read from the shared file. 1005 1006 class ReadClosure : public SerializeClosure { 1007 private: 1008 intptr_t** _ptr_array; 1009 1010 inline intptr_t nextPtr() { 1011 return *(*_ptr_array)++; 1012 } 1013 1014 public: 1015 ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; } 1016 1017 void do_ptr(void** p) { 1018 assert(*p == NULL, "initializing previous initialized pointer."); 1019 intptr_t obj = nextPtr(); 1020 assert((intptr_t)obj >= 0 || (intptr_t)obj < -100, 1021 "hit tag while initializing ptrs."); 1022 *p = (void*)obj; 1023 } 1024 1025 void do_u4(u4* p) { 1026 intptr_t obj = nextPtr(); 1027 *p = (u4)(uintx(obj)); 1028 } 1029 1030 void do_tag(int tag) { 1031 int old_tag; 1032 old_tag = (int)(intptr_t)nextPtr(); 1033 // do_int(&old_tag); 1034 assert(tag == old_tag, "old tag doesn't match"); 1035 FileMapInfo::assert_mark(tag == old_tag); 1036 } 1037 1038 void do_region(u_char* start, size_t size) { 1039 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 1040 assert(size % sizeof(intptr_t) == 0, "bad size"); 1041 do_tag((int)size); 1042 while (size > 0) { 1043 *(intptr_t*)start = nextPtr(); 1044 start += sizeof(intptr_t); 1045 size -= sizeof(intptr_t); 1046 } 1047 } 1048 1049 bool reading() const { return true; } 1050 }; 1051 1052 // Return true if given address is in the mapped shared space. 1053 bool MetaspaceShared::is_in_shared_space(const void* p) { 1054 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p); 1055 } 1056 1057 // Return true if given address is in the misc data region 1058 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) { 1059 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx); 1060 } 1061 1062 bool MetaspaceShared::is_string_region(int idx) { 1063 return (idx >= MetaspaceShared::first_string && 1064 idx < MetaspaceShared::first_string + MetaspaceShared::max_strings); 1065 } 1066 1067 void MetaspaceShared::print_shared_spaces() { 1068 if (UseSharedSpaces) { 1069 FileMapInfo::current_info()->print_shared_spaces(); 1070 } 1071 } 1072 1073 1074 // Map shared spaces at requested addresses and return if succeeded. 1075 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { 1076 size_t image_alignment = mapinfo->alignment(); 1077 1078 #ifndef _WINDOWS 1079 // Map in the shared memory and then map the regions on top of it. 1080 // On Windows, don't map the memory here because it will cause the 1081 // mappings of the regions to fail. 1082 ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); 1083 if (!shared_rs.is_reserved()) return false; 1084 #endif 1085 1086 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 1087 1088 char* _ro_base = NULL; 1089 char* _rw_base = NULL; 1090 char* _md_base = NULL; 1091 char* _mc_base = NULL; 1092 char* _od_base = NULL; 1093 1094 // Map each shared region 1095 if ((_ro_base = mapinfo->map_region(ro)) != NULL && 1096 mapinfo->verify_region_checksum(ro) && 1097 (_rw_base = mapinfo->map_region(rw)) != NULL && 1098 mapinfo->verify_region_checksum(rw) && 1099 (_md_base = mapinfo->map_region(md)) != NULL && 1100 mapinfo->verify_region_checksum(md) && 1101 (_mc_base = mapinfo->map_region(mc)) != NULL && 1102 mapinfo->verify_region_checksum(mc) && 1103 (_od_base = mapinfo->map_region(od)) != NULL && 1104 mapinfo->verify_region_checksum(od) && 1105 (image_alignment == (size_t)max_alignment()) && 1106 mapinfo->validate_classpath_entry_table()) { 1107 // Success (no need to do anything) 1108 return true; 1109 } else { 1110 // If there was a failure in mapping any of the spaces, unmap the ones 1111 // that succeeded 1112 if (_ro_base != NULL) mapinfo->unmap_region(ro); 1113 if (_rw_base != NULL) mapinfo->unmap_region(rw); 1114 if (_md_base != NULL) mapinfo->unmap_region(md); 1115 if (_mc_base != NULL) mapinfo->unmap_region(mc); 1116 if (_od_base != NULL) mapinfo->unmap_region(od); 1117 #ifndef _WINDOWS 1118 // Release the entire mapped region 1119 shared_rs.release(); 1120 #endif 1121 // If -Xshare:on is specified, print out the error message and exit VM, 1122 // otherwise, set UseSharedSpaces to false and continue. 1123 if (RequireSharedSpaces || PrintSharedArchiveAndExit) { 1124 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on."); 1125 } else { 1126 FLAG_SET_DEFAULT(UseSharedSpaces, false); 1127 } 1128 return false; 1129 } 1130 } 1131 1132 // Read the miscellaneous data from the shared file, and 1133 // serialize it out to its various destinations. 1134 1135 void MetaspaceShared::initialize_shared_spaces() { 1136 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1137 _cds_i2i_entry_code_buffers = mapinfo->cds_i2i_entry_code_buffers(); 1138 _cds_i2i_entry_code_buffers_size = mapinfo->cds_i2i_entry_code_buffers_size(); 1139 char* buffer = mapinfo->misc_data_patching_start(); 1140 1141 // Skip over (reserve space for) a list of addresses of C++ vtables 1142 // for Klass objects. They get filled in later. 1143 1144 void** vtbl_list = (void**)buffer; 1145 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*); 1146 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 1147 1148 // Skip over (reserve space for) dummy C++ vtables Klass objects. 1149 // They are used as is. 1150 1151 intptr_t vtable_size = *(intptr_t*)buffer; 1152 buffer += sizeof(intptr_t); 1153 buffer += vtable_size; 1154 1155 int sharedDictionaryLen = *(intptr_t*)buffer; 1156 buffer += sizeof(intptr_t); 1157 int number_of_entries = *(intptr_t*)buffer; 1158 buffer += sizeof(intptr_t); 1159 SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer, 1160 sharedDictionaryLen, 1161 number_of_entries); 1162 buffer += sharedDictionaryLen; 1163 1164 // The following data in the shared misc data region are the linked 1165 // list elements (HashtableEntry objects) for the shared dictionary 1166 // table. 1167 1168 int len = *(intptr_t*)buffer; // skip over shared dictionary entries 1169 buffer += sizeof(intptr_t); 1170 buffer += len; 1171 1172 // Verify various attributes of the archive, plus initialize the 1173 // shared string/symbol tables 1174 intptr_t* array = (intptr_t*)buffer; 1175 ReadClosure rc(&array); 1176 serialize(&rc, NULL, NULL); 1177 1178 // Initialize the run-time symbol table. 1179 SymbolTable::create_table(); 1180 1181 // Close the mapinfo file 1182 mapinfo->close(); 1183 1184 if (PrintSharedArchiveAndExit) { 1185 if (PrintSharedDictionary) { 1186 tty->print_cr("\nShared classes:\n"); 1187 SystemDictionary::print_shared(false); 1188 } 1189 if (_archive_loading_failed) { 1190 tty->print_cr("archive is invalid"); 1191 vm_exit(1); 1192 } else { 1193 tty->print_cr("archive is valid"); 1194 vm_exit(0); 1195 } 1196 } 1197 } 1198 1199 void MetaspaceShared::fixup_shared_string_regions() { 1200 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1201 mapinfo->fixup_string_regions(); 1202 } 1203 1204 // JVM/TI RedefineClasses() support: 1205 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1206 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1207 1208 if (UseSharedSpaces) { 1209 // remap the shared readonly space to shared readwrite, private 1210 FileMapInfo* mapinfo = FileMapInfo::current_info(); 1211 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1212 return false; 1213 } 1214 _remapped_readwrite = true; 1215 } 1216 return true; 1217 } 1218 1219 int MetaspaceShared::count_class(const char* classlist_file) { 1220 if (classlist_file == NULL) { 1221 return 0; 1222 } 1223 char class_name[256]; 1224 int class_count = 0; 1225 FILE* file = fopen(classlist_file, "r"); 1226 if (file != NULL) { 1227 while ((fgets(class_name, sizeof class_name, file)) != NULL) { 1228 if (*class_name == '#') { // comment 1229 continue; 1230 } 1231 class_count++; 1232 } 1233 fclose(file); 1234 } else { 1235 char errmsg[JVM_MAXPATHLEN]; 1236 os::lasterror(errmsg, JVM_MAXPATHLEN); 1237 tty->print_cr("Loading classlist failed: %s", errmsg); 1238 exit(1); 1239 } 1240 1241 return class_count; 1242 } 1243 1244 // the sizes are good for typical large applications that have a lot of shared 1245 // classes 1246 void MetaspaceShared::estimate_regions_size() { 1247 int class_count = count_class(SharedClassListFile); 1248 class_count += count_class(ExtraSharedClassListFile); 1249 1250 if (class_count > LargeThresholdClassCount) { 1251 if (class_count < HugeThresholdClassCount) { 1252 SET_ESTIMATED_SIZE(Large, ReadOnly); 1253 SET_ESTIMATED_SIZE(Large, ReadWrite); 1254 SET_ESTIMATED_SIZE(Large, MiscData); 1255 SET_ESTIMATED_SIZE(Large, MiscCode); 1256 } else { 1257 SET_ESTIMATED_SIZE(Huge, ReadOnly); 1258 SET_ESTIMATED_SIZE(Huge, ReadWrite); 1259 SET_ESTIMATED_SIZE(Huge, MiscData); 1260 SET_ESTIMATED_SIZE(Huge, MiscCode); 1261 } 1262 } 1263 }