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