405 406 tty->print_cr("%s", sep); 407 tty->print_cr(fmt_stats, "Total", 408 all_ro_count, all_ro_bytes, all_ro_perc, 409 all_rw_count, all_rw_bytes, all_rw_perc, 410 all_count, all_bytes, all_perc); 411 412 assert(all_ro_bytes == ro_all, "everything should have been counted"); 413 assert(all_rw_bytes == rw_all, "everything should have been counted"); 414 #undef fmt_stats 415 } 416 417 // Populate the shared space. 418 419 class VM_PopulateDumpSharedSpace: public VM_Operation { 420 private: 421 ClassLoaderData* _loader_data; 422 GrowableArray<Klass*> *_class_promote_order; 423 VirtualSpace _md_vs; 424 VirtualSpace _mc_vs; 425 426 public: 427 VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, 428 GrowableArray<Klass*> *class_promote_order) : 429 _loader_data(loader_data) { 430 431 // Split up and initialize the misc code and data spaces 432 ReservedSpace* shared_rs = MetaspaceShared::shared_rs(); 433 size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize; 434 ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size); 435 ReservedSpace misc_section = shared_rs->last_part(metadata_size); 436 437 // Now split into misc sections. 438 ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); 439 ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); 440 _md_vs.initialize(md_rs, SharedMiscDataSize); 441 _mc_vs.initialize(mc_rs, SharedMiscCodeSize); 442 _class_promote_order = class_promote_order; 443 } 444 523 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 524 525 md_top += vtbl_list_size * sizeof(void*); 526 void* vtable = md_top; 527 528 // Reserve space for a new dummy vtable for klass objects in the 529 // heap. Generate self-patching vtable entries. 530 531 MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable, 532 &md_top, md_end, 533 &mc_top, mc_end); 534 535 // Reorder the system dictionary. (Moving the symbols affects 536 // how the hash table indices are calculated.) 537 // Not doing this either. 538 539 SystemDictionary::reorder_dictionary(); 540 541 NOT_PRODUCT(SystemDictionary::verify();) 542 543 // Copy the the symbol table, and the system dictionary to the shared 544 // space in usable form. Copy the hashtable 545 // buckets first [read-write], then copy the linked lists of entries 546 // [read-only]. 547 548 NOT_PRODUCT(SymbolTable::verify()); 549 handle_misc_data_space_failure(SymbolTable::copy_compact_table(&md_top, md_end)); 550 551 SystemDictionary::reverse(); 552 SystemDictionary::copy_buckets(&md_top, md_end); 553 554 ClassLoader::verify(); 555 ClassLoader::copy_package_info_buckets(&md_top, md_end); 556 ClassLoader::verify(); 557 558 SystemDictionary::copy_table(&md_top, md_end); 559 ClassLoader::verify(); 560 ClassLoader::copy_package_info_table(&md_top, md_end); 561 ClassLoader::verify(); 562 563 // Write the other data to the output array. 564 WriteClosure wc(md_top, md_end); 565 MetaspaceShared::serialize(&wc); 566 md_top = wc.get_top(); 567 568 // Print shared spaces all the time 569 // To make fmt_space be a syntactic constant (for format warnings), use #define. 570 #define fmt_space "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " INTPTR_FORMAT 571 Metaspace* ro_space = _loader_data->ro_metaspace(); 572 Metaspace* rw_space = _loader_data->rw_metaspace(); 573 574 // Allocated size of each space (may not be all occupied) 575 const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType); 576 const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType); 577 const size_t md_alloced = md_end-md_low; 578 const size_t mc_alloced = mc_end-mc_low; 579 const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced; 580 581 // Occupied size of each space. 582 const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType); 583 const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType); 584 const size_t md_bytes = size_t(md_top - md_low); 585 const size_t mc_bytes = size_t(mc_top - mc_low); 586 587 // Percent of total size 588 const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes; 589 const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; 590 const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; 591 const double md_t_perc = md_bytes / double(total_bytes) * 100.0; 592 const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; 593 594 // Percent of fullness of each space 595 const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; 596 const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; 597 const double md_u_perc = md_bytes / double(md_alloced) * 100.0; 598 const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; 599 const double total_u_perc = total_bytes / double(total_alloced) * 100.0; 600 601 tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom()); 602 tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom()); 603 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low); 604 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low); 605 tty->print_cr("total : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]", 606 total_bytes, total_alloced, total_u_perc); 607 608 // Update the vtable pointers in all of the Klass objects in the 609 // heap. They should point to newly generated vtable. 610 patch_klass_vtables(vtbl_list, vtable); 611 612 // dunno what this is for. 613 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass); 614 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); 615 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); 616 617 // Create and write the archive file that maps the shared spaces. 618 619 FileMapInfo* mapinfo = new FileMapInfo(); 620 mapinfo->populate_header(MetaspaceShared::max_alignment()); 621 622 // Pass 1 - update file offsets in header. 623 mapinfo->write_header(); 624 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 625 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 626 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 627 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 628 SharedMiscDataSize, 629 false, false); 630 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 631 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 632 SharedMiscCodeSize, 633 true, true); 634 635 // Pass 2 - write data. 636 mapinfo->open_for_write(); 637 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 638 mapinfo->write_header(); 639 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 640 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 641 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 642 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 643 SharedMiscDataSize, 644 false, false); 645 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 646 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 647 SharedMiscCodeSize, 648 true, true); 649 mapinfo->close(); 650 651 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); 652 os::free(saved_vtbl); 653 654 if (PrintSharedSpaces) { 655 DumpAllocClosure dac; 656 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO); 657 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW); 658 659 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); 660 } 661 #undef fmt_space 662 } 663 664 665 void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) { 666 Klass* k = obj; 667 if (k->oop_is_instance()) { 668 InstanceKlass* ik = (InstanceKlass*) k; 925 926 void do_region(u_char* start, size_t size) { 927 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 928 assert(size % sizeof(intptr_t) == 0, "bad size"); 929 do_tag((int)size); 930 while (size > 0) { 931 *(intptr_t*)start = nextPtr(); 932 start += sizeof(intptr_t); 933 size -= sizeof(intptr_t); 934 } 935 } 936 937 bool reading() const { return true; } 938 }; 939 940 // Return true if given address is in the mapped shared space. 941 bool MetaspaceShared::is_in_shared_space(const void* p) { 942 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p); 943 } 944 945 void MetaspaceShared::print_shared_spaces() { 946 if (UseSharedSpaces) { 947 FileMapInfo::current_info()->print_shared_spaces(); 948 } 949 } 950 951 952 // Map shared spaces at requested addresses and return if succeeded. 953 // Need to keep the bounds of the ro and rw space for the Metaspace::contains 954 // call, or is_in_shared_space. 955 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { 956 size_t image_alignment = mapinfo->alignment(); 957 958 #ifndef _WINDOWS 959 // Map in the shared memory and then map the regions on top of it. 960 // On Windows, don't map the memory here because it will cause the 961 // mappings of the regions to fail. 962 ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); 963 if (!shared_rs.is_reserved()) return false; 964 #endif 965 966 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 967 968 char* _ro_base = NULL; 969 char* _rw_base = NULL; 970 char* _md_base = NULL; 971 char* _mc_base = NULL; 972 973 // Map each shared region 974 if ((_ro_base = mapinfo->map_region(ro)) != NULL && 975 mapinfo->verify_region_checksum(ro) && 976 (_rw_base = mapinfo->map_region(rw)) != NULL && 977 mapinfo->verify_region_checksum(rw) && 978 (_md_base = mapinfo->map_region(md)) != NULL && 979 mapinfo->verify_region_checksum(md) && 980 (_mc_base = mapinfo->map_region(mc)) != NULL && 981 mapinfo->verify_region_checksum(mc) && 982 (image_alignment == (size_t)max_alignment()) && 983 mapinfo->validate_classpath_entry_table()) { 984 // Success (no need to do anything) 985 return true; 986 } else { 987 // If there was a failure in mapping any of the spaces, unmap the ones 988 // that succeeded 989 if (_ro_base != NULL) mapinfo->unmap_region(ro); 990 if (_rw_base != NULL) mapinfo->unmap_region(rw); 991 if (_md_base != NULL) mapinfo->unmap_region(md); 992 if (_mc_base != NULL) mapinfo->unmap_region(mc); 993 #ifndef _WINDOWS 994 // Release the entire mapped region 995 shared_rs.release(); 996 #endif 997 // If -Xshare:on is specified, print out the error message and exit VM, 998 // otherwise, set UseSharedSpaces to false and continue. 999 if (RequireSharedSpaces || PrintSharedArchiveAndExit) { 1000 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on."); 1001 } else { 1002 FLAG_SET_DEFAULT(UseSharedSpaces, false); 1003 } 1004 return false; 1005 } 1006 } 1007 1008 // Read the miscellaneous data from the shared file, and 1009 // serialize it out to its various destinations. 1010 1011 void MetaspaceShared::initialize_shared_spaces() { 1012 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1013 1014 char* buffer = mapinfo->region_base(md); 1015 1016 // Skip over (reserve space for) a list of addresses of C++ vtables 1017 // for Klass objects. They get filled in later. 1018 1019 void** vtbl_list = (void**)buffer; 1020 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*); 1021 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 1022 1023 // Skip over (reserve space for) dummy C++ vtables Klass objects. 1024 // They are used as is. 1025 1026 intptr_t vtable_size = *(intptr_t*)buffer; 1027 buffer += sizeof(intptr_t); 1028 buffer += vtable_size; 1029 1030 // Create the shared symbol table using the bucket array at this spot in the 1031 // misc data space. (Todo: move this to read-only space. Currently 1032 // this is mapped copy-on-write but will never be written into). 1033 1034 buffer = (char*)SymbolTable::init_shared_table(buffer); 1035 SymbolTable::create_table(); 1036 1037 // Create the shared dictionary using the bucket array at this spot in 1038 // the misc data space. Since the shared dictionary table is never 1039 // modified, this region (of mapped pages) will be (effectively, if 1040 // not explicitly) read-only. 1041 1042 int sharedDictionaryLen = *(intptr_t*)buffer; 1043 buffer += sizeof(intptr_t); 1044 int number_of_entries = *(intptr_t*)buffer; 1045 buffer += sizeof(intptr_t); 1046 SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer, 1047 sharedDictionaryLen, 1048 number_of_entries); 1049 buffer += sharedDictionaryLen; 1050 1051 // Create the package info table using the bucket array at this spot in 1052 // the misc data space. Since the package info table is never 1053 // modified, this region (of mapped pages) will be (effectively, if 1054 // not explicitly) read-only. 1055 1056 int pkgInfoLen = *(intptr_t*)buffer; 1081 intptr_t* array = (intptr_t*)buffer; 1082 ReadClosure rc(&array); 1083 serialize(&rc); 1084 1085 // Close the mapinfo file 1086 mapinfo->close(); 1087 1088 if (PrintSharedArchiveAndExit) { 1089 if (PrintSharedDictionary) { 1090 tty->print_cr("\nShared classes:\n"); 1091 SystemDictionary::print_shared(false); 1092 } 1093 if (_archive_loading_failed) { 1094 tty->print_cr("archive is invalid"); 1095 vm_exit(1); 1096 } else { 1097 tty->print_cr("archive is valid"); 1098 vm_exit(0); 1099 } 1100 } 1101 } 1102 1103 // JVM/TI RedefineClasses() support: 1104 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1105 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1106 1107 if (UseSharedSpaces) { 1108 // remap the shared readonly space to shared readwrite, private 1109 FileMapInfo* mapinfo = FileMapInfo::current_info(); 1110 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1111 return false; 1112 } 1113 } 1114 return true; 1115 } 1116 1117 int MetaspaceShared::count_class(const char* classlist_file) { 1118 if (classlist_file == NULL) { 1119 return 0; 1120 } | 405 406 tty->print_cr("%s", sep); 407 tty->print_cr(fmt_stats, "Total", 408 all_ro_count, all_ro_bytes, all_ro_perc, 409 all_rw_count, all_rw_bytes, all_rw_perc, 410 all_count, all_bytes, all_perc); 411 412 assert(all_ro_bytes == ro_all, "everything should have been counted"); 413 assert(all_rw_bytes == rw_all, "everything should have been counted"); 414 #undef fmt_stats 415 } 416 417 // Populate the shared space. 418 419 class VM_PopulateDumpSharedSpace: public VM_Operation { 420 private: 421 ClassLoaderData* _loader_data; 422 GrowableArray<Klass*> *_class_promote_order; 423 VirtualSpace _md_vs; 424 VirtualSpace _mc_vs; 425 CompactHashtableWriter* _string_cht; 426 GrowableArray<MemRegion> *_string_regions; 427 428 public: 429 VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data, 430 GrowableArray<Klass*> *class_promote_order) : 431 _loader_data(loader_data) { 432 433 // Split up and initialize the misc code and data spaces 434 ReservedSpace* shared_rs = MetaspaceShared::shared_rs(); 435 size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize; 436 ReservedSpace shared_ro_rw = shared_rs->first_part(metadata_size); 437 ReservedSpace misc_section = shared_rs->last_part(metadata_size); 438 439 // Now split into misc sections. 440 ReservedSpace md_rs = misc_section.first_part(SharedMiscDataSize); 441 ReservedSpace mc_rs = misc_section.last_part(SharedMiscDataSize); 442 _md_vs.initialize(md_rs, SharedMiscDataSize); 443 _mc_vs.initialize(mc_rs, SharedMiscCodeSize); 444 _class_promote_order = class_promote_order; 445 } 446 525 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 526 527 md_top += vtbl_list_size * sizeof(void*); 528 void* vtable = md_top; 529 530 // Reserve space for a new dummy vtable for klass objects in the 531 // heap. Generate self-patching vtable entries. 532 533 MetaspaceShared::generate_vtable_methods(vtbl_list, &vtable, 534 &md_top, md_end, 535 &mc_top, mc_end); 536 537 // Reorder the system dictionary. (Moving the symbols affects 538 // how the hash table indices are calculated.) 539 // Not doing this either. 540 541 SystemDictionary::reorder_dictionary(); 542 543 NOT_PRODUCT(SystemDictionary::verify();) 544 545 // Copy the the symbol table, string table, and the system dictionary to the shared 546 // space in usable form. Copy the hashtable 547 // buckets first [read-write], then copy the linked lists of entries 548 // [read-only]. 549 550 NOT_PRODUCT(SymbolTable::verify()); 551 handle_misc_data_space_failure(SymbolTable::copy_compact_table(&md_top, md_end)); 552 553 size_t ss_bytes = 0; 554 char* ss_low; 555 // The string space has maximum two regions. See FileMapInfo::write_string_regions() for details. 556 _string_regions = new GrowableArray<MemRegion>(2); 557 NOT_PRODUCT(StringTable::verify()); 558 handle_misc_data_space_failure(StringTable::copy_compact_table(&md_top, md_end, _string_regions, 559 &ss_bytes)); 560 ss_low = _string_regions->is_empty() ? NULL : (char*)_string_regions->first().start(); 561 562 SystemDictionary::reverse(); 563 SystemDictionary::copy_buckets(&md_top, md_end); 564 565 ClassLoader::verify(); 566 ClassLoader::copy_package_info_buckets(&md_top, md_end); 567 ClassLoader::verify(); 568 569 SystemDictionary::copy_table(&md_top, md_end); 570 ClassLoader::verify(); 571 ClassLoader::copy_package_info_table(&md_top, md_end); 572 ClassLoader::verify(); 573 574 // Write the other data to the output array. 575 WriteClosure wc(md_top, md_end); 576 MetaspaceShared::serialize(&wc); 577 md_top = wc.get_top(); 578 579 // Print shared spaces all the time 580 // To make fmt_space be a syntactic constant (for format warnings), use #define. 581 #define fmt_space "%s space: %9d [ %4.1f%% of total] out of %9d bytes [%4.1f%% used] at " INTPTR_FORMAT 582 Metaspace* ro_space = _loader_data->ro_metaspace(); 583 Metaspace* rw_space = _loader_data->rw_metaspace(); 584 585 // Allocated size of each space (may not be all occupied) 586 const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType); 587 const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType); 588 const size_t md_alloced = md_end-md_low; 589 const size_t mc_alloced = mc_end-mc_low; 590 const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced 591 + ss_bytes; 592 593 // Occupied size of each space. 594 const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType); 595 const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType); 596 const size_t md_bytes = size_t(md_top - md_low); 597 const size_t mc_bytes = size_t(mc_top - mc_low); 598 599 // Percent of total size 600 const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes + ss_bytes; 601 const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0; 602 const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0; 603 const double md_t_perc = md_bytes / double(total_bytes) * 100.0; 604 const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0; 605 const double ss_t_perc = ss_bytes / double(total_bytes) * 100.0; 606 607 // Percent of fullness of each space 608 const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0; 609 const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0; 610 const double md_u_perc = md_bytes / double(md_alloced) * 100.0; 611 const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0; 612 const double total_u_perc = total_bytes / double(total_alloced) * 100.0; 613 614 tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, ro_space->bottom()); 615 tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, rw_space->bottom()); 616 tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, md_low); 617 tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, mc_low); 618 tty->print_cr(fmt_space, "st", ss_bytes, ss_t_perc, ss_bytes, 100.0, ss_low); 619 tty->print_cr("total : %9d [100.0%% of total] out of %9d bytes [%4.1f%% used]", 620 total_bytes, total_alloced, total_u_perc); 621 622 // Update the vtable pointers in all of the Klass objects in the 623 // heap. They should point to newly generated vtable. 624 patch_klass_vtables(vtbl_list, vtable); 625 626 // dunno what this is for. 627 char* saved_vtbl = (char*)os::malloc(vtbl_list_size * sizeof(void*), mtClass); 628 memmove(saved_vtbl, vtbl_list, vtbl_list_size * sizeof(void*)); 629 memset(vtbl_list, 0, vtbl_list_size * sizeof(void*)); 630 631 // Create and write the archive file that maps the shared spaces. 632 633 FileMapInfo* mapinfo = new FileMapInfo(); 634 mapinfo->populate_header(MetaspaceShared::max_alignment()); 635 636 // Pass 1 - update file offsets in header. 637 mapinfo->write_header(); 638 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 639 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 640 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 641 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 642 SharedMiscDataSize, 643 false, false); 644 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 645 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 646 SharedMiscCodeSize, 647 true, true); 648 mapinfo->write_string_regions(_string_regions); 649 650 // Pass 2 - write data. 651 mapinfo->open_for_write(); 652 mapinfo->set_header_crc(mapinfo->compute_header_crc()); 653 mapinfo->write_header(); 654 mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true); 655 mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false); 656 mapinfo->write_region(MetaspaceShared::md, _md_vs.low(), 657 pointer_delta(md_top, _md_vs.low(), sizeof(char)), 658 SharedMiscDataSize, 659 false, false); 660 mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(), 661 pointer_delta(mc_top, _mc_vs.low(), sizeof(char)), 662 SharedMiscCodeSize, 663 true, true); 664 mapinfo->write_string_regions(_string_regions); 665 666 mapinfo->close(); 667 668 memmove(vtbl_list, saved_vtbl, vtbl_list_size * sizeof(void*)); 669 os::free(saved_vtbl); 670 671 if (PrintSharedSpaces) { 672 DumpAllocClosure dac; 673 dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO); 674 dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW); 675 676 dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes)); 677 } 678 #undef fmt_space 679 } 680 681 682 void MetaspaceShared::link_one_shared_class(Klass* obj, TRAPS) { 683 Klass* k = obj; 684 if (k->oop_is_instance()) { 685 InstanceKlass* ik = (InstanceKlass*) k; 942 943 void do_region(u_char* start, size_t size) { 944 assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment"); 945 assert(size % sizeof(intptr_t) == 0, "bad size"); 946 do_tag((int)size); 947 while (size > 0) { 948 *(intptr_t*)start = nextPtr(); 949 start += sizeof(intptr_t); 950 size -= sizeof(intptr_t); 951 } 952 } 953 954 bool reading() const { return true; } 955 }; 956 957 // Return true if given address is in the mapped shared space. 958 bool MetaspaceShared::is_in_shared_space(const void* p) { 959 return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p); 960 } 961 962 bool MetaspaceShared::is_string_region(int idx) { 963 return (idx >= MetaspaceShared::first_string && 964 idx < MetaspaceShared::first_string + MetaspaceShared::max_strings); 965 } 966 967 void MetaspaceShared::print_shared_spaces() { 968 if (UseSharedSpaces) { 969 FileMapInfo::current_info()->print_shared_spaces(); 970 } 971 } 972 973 974 // Map shared spaces at requested addresses and return if succeeded. 975 // Need to keep the bounds of the ro and rw space for the Metaspace::contains 976 // call, or is_in_shared_space. 977 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) { 978 size_t image_alignment = mapinfo->alignment(); 979 980 #ifndef _WINDOWS 981 // Map in the shared memory and then map the regions on top of it. 982 // On Windows, don't map the memory here because it will cause the 983 // mappings of the regions to fail. 984 ReservedSpace shared_rs = mapinfo->reserve_shared_memory(); 985 if (!shared_rs.is_reserved()) return false; 986 #endif 987 988 assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces"); 989 990 char* _ro_base = NULL; 991 char* _rw_base = NULL; 992 char* _md_base = NULL; 993 char* _mc_base = NULL; 994 995 // Map each shared region 996 if ((_ro_base = mapinfo->map_region(ro)) != NULL && 997 mapinfo->verify_region_checksum(ro) && 998 (_rw_base = mapinfo->map_region(rw)) != NULL && 999 mapinfo->verify_region_checksum(rw) && 1000 (_md_base = mapinfo->map_region(md)) != NULL && 1001 mapinfo->verify_region_checksum(md) && 1002 (_mc_base = mapinfo->map_region(mc)) != NULL && 1003 mapinfo->verify_region_checksum(mc) && 1004 mapinfo->map_string_regions() && 1005 mapinfo->verify_string_regions() && 1006 (image_alignment == (size_t)max_alignment()) && 1007 mapinfo->validate_classpath_entry_table()) { 1008 // Success (no need to do anything) 1009 return true; 1010 } else { 1011 // If there was a failure in mapping any of the spaces, unmap the ones 1012 // that succeeded 1013 if (_ro_base != NULL) mapinfo->unmap_region(ro); 1014 if (_rw_base != NULL) mapinfo->unmap_region(rw); 1015 if (_md_base != NULL) mapinfo->unmap_region(md); 1016 if (_mc_base != NULL) mapinfo->unmap_region(mc); 1017 mapinfo->unmap_string_regions(); 1018 #ifndef _WINDOWS 1019 // Release the entire mapped region 1020 shared_rs.release(); 1021 #endif 1022 // If -Xshare:on is specified, print out the error message and exit VM, 1023 // otherwise, set UseSharedSpaces to false and continue. 1024 if (RequireSharedSpaces || PrintSharedArchiveAndExit) { 1025 vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on."); 1026 } else { 1027 FLAG_SET_DEFAULT(UseSharedSpaces, false); 1028 } 1029 return false; 1030 } 1031 } 1032 1033 // Read the miscellaneous data from the shared file, and 1034 // serialize it out to its various destinations. 1035 1036 void MetaspaceShared::initialize_shared_spaces() { 1037 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1038 1039 char* buffer = mapinfo->header()->region_addr(md); 1040 1041 // Skip over (reserve space for) a list of addresses of C++ vtables 1042 // for Klass objects. They get filled in later. 1043 1044 void** vtbl_list = (void**)buffer; 1045 buffer += MetaspaceShared::vtbl_list_size * sizeof(void*); 1046 Universe::init_self_patching_vtbl_list(vtbl_list, vtbl_list_size); 1047 1048 // Skip over (reserve space for) dummy C++ vtables Klass objects. 1049 // They are used as is. 1050 1051 intptr_t vtable_size = *(intptr_t*)buffer; 1052 buffer += sizeof(intptr_t); 1053 buffer += vtable_size; 1054 1055 // Create the shared symbol table using the compact table at this spot in the 1056 // misc data space. (Todo: move this to read-only space. Currently 1057 // this is mapped copy-on-write but will never be written into). 1058 1059 buffer = (char*)SymbolTable::init_shared_table(buffer); 1060 SymbolTable::create_table(); 1061 1062 // Create the shared string table using the compact table 1063 buffer = (char*)StringTable::init_shared_table(mapinfo, buffer); 1064 1065 // Create the shared dictionary using the bucket array at this spot in 1066 // the misc data space. Since the shared dictionary table is never 1067 // modified, this region (of mapped pages) will be (effectively, if 1068 // not explicitly) read-only. 1069 1070 int sharedDictionaryLen = *(intptr_t*)buffer; 1071 buffer += sizeof(intptr_t); 1072 int number_of_entries = *(intptr_t*)buffer; 1073 buffer += sizeof(intptr_t); 1074 SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer, 1075 sharedDictionaryLen, 1076 number_of_entries); 1077 buffer += sharedDictionaryLen; 1078 1079 // Create the package info table using the bucket array at this spot in 1080 // the misc data space. Since the package info table is never 1081 // modified, this region (of mapped pages) will be (effectively, if 1082 // not explicitly) read-only. 1083 1084 int pkgInfoLen = *(intptr_t*)buffer; 1109 intptr_t* array = (intptr_t*)buffer; 1110 ReadClosure rc(&array); 1111 serialize(&rc); 1112 1113 // Close the mapinfo file 1114 mapinfo->close(); 1115 1116 if (PrintSharedArchiveAndExit) { 1117 if (PrintSharedDictionary) { 1118 tty->print_cr("\nShared classes:\n"); 1119 SystemDictionary::print_shared(false); 1120 } 1121 if (_archive_loading_failed) { 1122 tty->print_cr("archive is invalid"); 1123 vm_exit(1); 1124 } else { 1125 tty->print_cr("archive is valid"); 1126 vm_exit(0); 1127 } 1128 } 1129 } 1130 1131 void MetaspaceShared::fixup_shared_string_regions() { 1132 FileMapInfo *mapinfo = FileMapInfo::current_info(); 1133 mapinfo->fixup_string_regions(); 1134 } 1135 1136 // JVM/TI RedefineClasses() support: 1137 bool MetaspaceShared::remap_shared_readonly_as_readwrite() { 1138 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1139 1140 if (UseSharedSpaces) { 1141 // remap the shared readonly space to shared readwrite, private 1142 FileMapInfo* mapinfo = FileMapInfo::current_info(); 1143 if (!mapinfo->remap_shared_readonly_as_readwrite()) { 1144 return false; 1145 } 1146 } 1147 return true; 1148 } 1149 1150 int MetaspaceShared::count_class(const char* classlist_file) { 1151 if (classlist_file == NULL) { 1152 return 0; 1153 } |