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