157 assert(_current_info == NULL, "must be singleton"); // not thread safe |
157 assert(_current_info == NULL, "must be singleton"); // not thread safe |
158 _current_info = this; |
158 _current_info = this; |
159 memset((void*)this, 0, sizeof(FileMapInfo)); |
159 memset((void*)this, 0, sizeof(FileMapInfo)); |
160 _file_offset = 0; |
160 _file_offset = 0; |
161 _file_open = false; |
161 _file_open = false; |
162 _header = new FileMapHeader(); |
162 _header = (FileMapHeader*)os::malloc(sizeof(FileMapHeader), mtInternal); |
163 _header->_version = _invalid_version; |
163 _header->_version = INVALID_CDS_ARCHIVE_VERSION; |
164 _header->_has_platform_or_app_classes = true; |
164 _header->_has_platform_or_app_classes = true; |
165 } |
165 } |
166 |
166 |
167 FileMapInfo::~FileMapInfo() { |
167 FileMapInfo::~FileMapInfo() { |
168 assert(_current_info == this, "must be singleton"); // not thread safe |
168 assert(_current_info == this, "must be singleton"); // not thread safe |
171 |
171 |
172 void FileMapInfo::populate_header(size_t alignment) { |
172 void FileMapInfo::populate_header(size_t alignment) { |
173 _header->populate(this, alignment); |
173 _header->populate(this, alignment); |
174 } |
174 } |
175 |
175 |
176 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { |
176 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { |
177 _magic = 0xf00baba2; |
177 _magic = CDS_ARCHIVE_MAGIC; |
178 _version = _current_version; |
178 _version = CURRENT_CDS_ARCHIVE_VERSION; |
179 _alignment = alignment; |
179 _alignment = alignment; |
180 _obj_alignment = ObjectAlignmentInBytes; |
180 _obj_alignment = ObjectAlignmentInBytes; |
181 _compact_strings = CompactStrings; |
181 _compact_strings = CompactStrings; |
182 _narrow_oop_mode = Universe::narrow_oop_mode(); |
182 _narrow_oop_mode = Universe::narrow_oop_mode(); |
183 _narrow_oop_base = Universe::narrow_oop_base(); |
183 _narrow_oop_base = Universe::narrow_oop_base(); |
491 } |
491 } |
492 |
492 |
493 // Read the FileMapInfo information from the file. |
493 // Read the FileMapInfo information from the file. |
494 |
494 |
495 bool FileMapInfo::init_from_file(int fd) { |
495 bool FileMapInfo::init_from_file(int fd) { |
496 size_t sz = _header->data_size(); |
496 size_t sz = sizeof(FileMapHeader); |
497 char* addr = _header->data(); |
497 size_t n = os::read(fd, _header, (unsigned int)sz); |
498 size_t n = os::read(fd, addr, (unsigned int)sz); |
|
499 if (n != sz) { |
498 if (n != sz) { |
500 fail_continue("Unable to read the file header."); |
499 fail_continue("Unable to read the file header."); |
501 return false; |
500 return false; |
502 } |
501 } |
503 if (_header->_version != current_version()) { |
502 if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) { |
504 fail_continue("The shared archive file has the wrong version."); |
503 fail_continue("The shared archive file has the wrong version."); |
505 return false; |
504 return false; |
506 } |
505 } |
507 _file_offset = (long)n; |
506 _file_offset = (long)n; |
508 |
507 |
519 _paths_misc_info = NULL; |
518 _paths_misc_info = NULL; |
520 return false; |
519 return false; |
521 } |
520 } |
522 |
521 |
523 size_t len = lseek(fd, 0, SEEK_END); |
522 size_t len = lseek(fd, 0, SEEK_END); |
524 struct FileMapInfo::FileMapHeader::space_info* si = |
523 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region); |
525 &_header->_space[MetaspaceShared::last_valid_region]; |
|
526 // The last space might be empty |
524 // The last space might be empty |
527 if (si->_file_offset > len || len - si->_file_offset < si->_used) { |
525 if (si->_file_offset > len || len - si->_file_offset < si->_used) { |
528 fail_continue("The shared archive file has been truncated."); |
526 fail_continue("The shared archive file has been truncated."); |
529 return false; |
527 return false; |
530 } |
528 } |
589 int info_size = ClassLoader::get_shared_paths_misc_info_size(); |
587 int info_size = ClassLoader::get_shared_paths_misc_info_size(); |
590 |
588 |
591 _header->_paths_misc_info_size = info_size; |
589 _header->_paths_misc_info_size = info_size; |
592 |
590 |
593 align_file_position(); |
591 align_file_position(); |
594 size_t sz = _header->data_size(); |
592 write_bytes(_header, sizeof(FileMapHeader)); |
595 char* addr = _header->data(); |
593 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size); |
596 write_bytes(addr, (int)sz); // skip the C++ vtable |
|
597 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); |
|
598 align_file_position(); |
594 align_file_position(); |
599 } |
595 } |
600 |
596 |
601 |
597 |
602 // Dump region to file. |
598 // Dump region to file. |
603 |
599 |
604 void FileMapInfo::write_region(int region, char* base, size_t size, |
600 void FileMapInfo::write_region(int region, char* base, size_t size, |
605 bool read_only, bool allow_exec) { |
601 bool read_only, bool allow_exec) { |
606 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; |
602 CDSFileMapRegion* si = space_at(region); |
607 |
603 |
608 if (_file_open) { |
604 if (_file_open) { |
609 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); |
605 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); |
610 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08) |
606 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08) |
611 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08), |
607 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08), |
626 si->_used = size; |
622 si->_used = size; |
627 si->_read_only = read_only; |
623 si->_read_only = read_only; |
628 si->_allow_exec = allow_exec; |
624 si->_allow_exec = allow_exec; |
629 si->_crc = ClassLoader::crc32(0, base, (jint)size); |
625 si->_crc = ClassLoader::crc32(0, base, (jint)size); |
630 if (base != NULL) { |
626 if (base != NULL) { |
631 write_bytes_aligned(base, (int)size); |
627 write_bytes_aligned(base, size); |
632 } |
628 } |
633 } |
629 } |
634 |
630 |
635 // Write out the given archive heap memory regions. GC code combines multiple |
631 // Write out the given archive heap memory regions. GC code combines multiple |
636 // consecutive archive GC regions into one MemRegion whenever possible and |
632 // consecutive archive GC regions into one MemRegion whenever possible and |
692 return total_size; |
688 return total_size; |
693 } |
689 } |
694 |
690 |
695 // Dump bytes to file -- at the current file position. |
691 // Dump bytes to file -- at the current file position. |
696 |
692 |
697 void FileMapInfo::write_bytes(const void* buffer, int nbytes) { |
693 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) { |
698 if (_file_open) { |
694 if (_file_open) { |
699 int n = ::write(_fd, buffer, nbytes); |
695 size_t n = os::write(_fd, buffer, (unsigned int)nbytes); |
700 if (n != nbytes) { |
696 if (n != nbytes) { |
701 // It is dangerous to leave the corrupted shared archive file around, |
697 // It is dangerous to leave the corrupted shared archive file around, |
702 // close and remove the file. See bug 6372906. |
698 // close and remove the file. See bug 6372906. |
703 close(); |
699 close(); |
704 remove(_full_path); |
700 remove(_full_path); |
730 } |
726 } |
731 |
727 |
732 |
728 |
733 // Dump bytes to file -- at the current file position. |
729 // Dump bytes to file -- at the current file position. |
734 |
730 |
735 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { |
731 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) { |
736 align_file_position(); |
732 align_file_position(); |
737 write_bytes(buffer, nbytes); |
733 write_bytes(buffer, nbytes); |
738 align_file_position(); |
734 align_file_position(); |
739 } |
735 } |
740 |
736 |
754 |
750 |
755 // JVM/TI RedefineClasses() support: |
751 // JVM/TI RedefineClasses() support: |
756 // Remap the shared readonly space to shared readwrite, private. |
752 // Remap the shared readonly space to shared readwrite, private. |
757 bool FileMapInfo::remap_shared_readonly_as_readwrite() { |
753 bool FileMapInfo::remap_shared_readonly_as_readwrite() { |
758 int idx = MetaspaceShared::ro; |
754 int idx = MetaspaceShared::ro; |
759 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx]; |
755 CDSFileMapRegion* si = space_at(idx); |
760 if (!si->_read_only) { |
756 if (!si->_read_only) { |
761 // the space is already readwrite so we are done |
757 // the space is already readwrite so we are done |
762 return true; |
758 return true; |
763 } |
759 } |
764 size_t used = si->_used; |
760 size_t used = si->_used; |
806 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData", |
802 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData", |
807 "String1", "String2", "OpenArchive1", "OpenArchive2" }; |
803 "String1", "String2", "OpenArchive1", "OpenArchive2" }; |
808 |
804 |
809 char* FileMapInfo::map_region(int i, char** top_ret) { |
805 char* FileMapInfo::map_region(int i, char** top_ret) { |
810 assert(!MetaspaceShared::is_heap_region(i), "sanity"); |
806 assert(!MetaspaceShared::is_heap_region(i), "sanity"); |
811 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
807 CDSFileMapRegion* si = space_at(i); |
812 size_t used = si->_used; |
808 size_t used = si->_used; |
813 size_t alignment = os::vm_allocation_granularity(); |
809 size_t alignment = os::vm_allocation_granularity(); |
814 size_t size = align_up(used, alignment); |
810 size_t size = align_up(used, alignment); |
815 char *requested_addr = _header->region_addr(i); |
811 char *requested_addr = _header->region_addr(i); |
816 |
812 |
873 if (narrow_oop_mode() != Universe::narrow_oop_mode() || |
869 if (narrow_oop_mode() != Universe::narrow_oop_mode() || |
874 narrow_oop_base() != Universe::narrow_oop_base() || |
870 narrow_oop_base() != Universe::narrow_oop_base() || |
875 narrow_oop_shift() != Universe::narrow_oop_shift() || |
871 narrow_oop_shift() != Universe::narrow_oop_shift() || |
876 narrow_klass_base() != Universe::narrow_klass_base() || |
872 narrow_klass_base() != Universe::narrow_klass_base() || |
877 narrow_klass_shift() != Universe::narrow_klass_shift()) { |
873 narrow_klass_shift() != Universe::narrow_klass_shift()) { |
878 if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) { |
874 if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) { |
879 log_info(cds)("Cached heap data from the CDS archive is being ignored. " |
875 log_info(cds)("Cached heap data from the CDS archive is being ignored. " |
880 "The current CompressedOops/CompressedClassPointers encoding differs from " |
876 "The current CompressedOops/CompressedClassPointers encoding differs from " |
881 "that archived due to heap size change. The archive was dumped using max heap " |
877 "that archived due to heap size change. The archive was dumped using max heap " |
882 "size " UINTX_FORMAT "M.", max_heap_size()/M); |
878 "size " UINTX_FORMAT "M.", max_heap_size()/M); |
883 log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", |
879 log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", |
904 MetaspaceShared::set_open_archive_heap_region_mapped(); |
900 MetaspaceShared::set_open_archive_heap_region_mapped(); |
905 } |
901 } |
906 } |
902 } |
907 } |
903 } |
908 } else { |
904 } else { |
909 if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) { |
905 if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) { |
910 log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, " |
906 log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, " |
911 "UseCompressedOops and UseCompressedClassPointers are required."); |
907 "UseCompressedOops and UseCompressedClassPointers are required."); |
912 } |
908 } |
913 } |
909 } |
914 |
910 |
922 } |
918 } |
923 |
919 |
924 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first, |
920 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first, |
925 int max, int* num, bool is_open_archive) { |
921 int max, int* num, bool is_open_archive) { |
926 MemRegion * regions = new MemRegion[max]; |
922 MemRegion * regions = new MemRegion[max]; |
927 struct FileMapInfo::FileMapHeader::space_info* si; |
923 CDSFileMapRegion* si; |
928 int region_num = 0; |
924 int region_num = 0; |
929 |
925 |
930 for (int i = first; |
926 for (int i = first; |
931 i < first + max; i++) { |
927 i < first + max; i++) { |
932 si = &_header->_space[i]; |
928 si = space_at(i); |
933 size_t used = si->_used; |
929 size_t used = si->_used; |
934 if (used > 0) { |
930 if (used > 0) { |
935 size_t size = used; |
931 size_t size = used; |
936 char* requested_addr = (char*)((void*)CompressedOops::decode_not_null( |
932 char* requested_addr = (char*)((void*)CompressedOops::decode_not_null( |
937 (narrowOop)si->_addr._offset)); |
933 (narrowOop)si->_addr._offset)); |
961 |
957 |
962 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type() |
958 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type() |
963 // for mapped regions as they are part of the reserved java heap, which is |
959 // for mapped regions as they are part of the reserved java heap, which is |
964 // already recorded. |
960 // already recorded. |
965 for (int i = 0; i < region_num; i++) { |
961 for (int i = 0; i < region_num; i++) { |
966 si = &_header->_space[first + i]; |
962 si = space_at(first + i); |
967 char* addr = (char*)regions[i].start(); |
963 char* addr = (char*)regions[i].start(); |
968 char* base = os::map_memory(_fd, _full_path, si->_file_offset, |
964 char* base = os::map_memory(_fd, _full_path, si->_file_offset, |
969 addr, regions[i].byte_size(), si->_read_only, |
965 addr, regions[i].byte_size(), si->_read_only, |
970 si->_allow_exec); |
966 si->_allow_exec); |
971 if (base == NULL || base != addr) { |
967 if (base == NULL || base != addr) { |
1040 !MetaspaceShared::open_archive_heap_region_mapped())) { |
1036 !MetaspaceShared::open_archive_heap_region_mapped())) { |
1041 return true; // archived heap data is not mapped |
1037 return true; // archived heap data is not mapped |
1042 } |
1038 } |
1043 const char* buf = _header->region_addr(i); |
1039 const char* buf = _header->region_addr(i); |
1044 int crc = ClassLoader::crc32(0, buf, (jint)sz); |
1040 int crc = ClassLoader::crc32(0, buf, (jint)sz); |
1045 if (crc != _header->_space[i]._crc) { |
1041 if (crc != space_at(i)->_crc) { |
1046 fail_continue("Checksum verification failed."); |
1042 fail_continue("Checksum verification failed."); |
1047 return false; |
1043 return false; |
1048 } |
1044 } |
1049 return true; |
1045 return true; |
1050 } |
1046 } |
1051 |
1047 |
1052 // Unmap a memory region in the address space. |
1048 // Unmap a memory region in the address space. |
1053 |
1049 |
1054 void FileMapInfo::unmap_region(int i) { |
1050 void FileMapInfo::unmap_region(int i) { |
1055 assert(!MetaspaceShared::is_heap_region(i), "sanity"); |
1051 assert(!MetaspaceShared::is_heap_region(i), "sanity"); |
1056 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
1052 CDSFileMapRegion* si = space_at(i); |
1057 size_t used = si->_used; |
1053 size_t used = si->_used; |
1058 size_t size = align_up(used, os::vm_allocation_granularity()); |
1054 size_t size = align_up(used, os::vm_allocation_granularity()); |
1059 |
1055 |
1060 if (used == 0) { |
1056 if (used == 0) { |
1061 return; |
1057 return; |
1109 return false; |
1105 return false; |
1110 } |
1106 } |
1111 return true; |
1107 return true; |
1112 } |
1108 } |
1113 |
1109 |
1114 char* FileMapInfo::FileMapHeader::region_addr(int idx) { |
1110 char* FileMapHeader::region_addr(int idx) { |
1115 if (MetaspaceShared::is_heap_region(idx)) { |
1111 if (MetaspaceShared::is_heap_region(idx)) { |
1116 return _space[idx]._used > 0 ? |
1112 return _space[idx]._used > 0 ? |
1117 (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL; |
1113 (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL; |
1118 } else { |
1114 } else { |
1119 return _space[idx]._addr._base; |
1115 return _space[idx]._addr._base; |
1120 } |
1116 } |
1121 } |
1117 } |
1122 |
1118 |
1123 int FileMapInfo::FileMapHeader::compute_crc() { |
1119 int FileMapHeader::compute_crc() { |
1124 char* header = data(); |
1120 char* start = (char*)this; |
1125 // start computing from the field after _crc |
1121 // start computing from the field after _crc |
1126 char* buf = (char*)&_crc + sizeof(int); |
1122 char* buf = (char*)&_crc + sizeof(_crc); |
1127 size_t sz = data_size() - (buf - header); |
1123 size_t sz = sizeof(FileMapHeader) - (buf - start); |
1128 int crc = ClassLoader::crc32(0, buf, (jint)sz); |
1124 int crc = ClassLoader::crc32(0, buf, (jint)sz); |
1129 return crc; |
1125 return crc; |
1130 } |
1126 } |
1131 |
1127 |
1132 // This function should only be called during run time with UseSharedSpaces enabled. |
1128 // This function should only be called during run time with UseSharedSpaces enabled. |
1133 bool FileMapInfo::FileMapHeader::validate() { |
1129 bool FileMapHeader::validate() { |
1134 if (VerifySharedSpaces && compute_crc() != _crc) { |
1130 if (VerifySharedSpaces && compute_crc() != _crc) { |
1135 fail_continue("Header checksum verification failed."); |
1131 FileMapInfo::fail_continue("Header checksum verification failed."); |
1136 return false; |
1132 return false; |
1137 } |
1133 } |
1138 |
1134 |
1139 if (!Arguments::has_jimage()) { |
1135 if (!Arguments::has_jimage()) { |
1140 FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build."); |
1136 FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build."); |
1141 return false; |
1137 return false; |
1142 } |
1138 } |
1143 |
1139 |
1144 if (_version != current_version()) { |
1140 if (_version != CURRENT_CDS_ARCHIVE_VERSION) { |
1145 FileMapInfo::fail_continue("The shared archive file is the wrong version."); |
1141 FileMapInfo::fail_continue("The shared archive file is the wrong version."); |
1146 return false; |
1142 return false; |
1147 } |
1143 } |
1148 if (_magic != (int)0xf00baba2) { |
1144 if (_magic != CDS_ARCHIVE_MAGIC) { |
1149 FileMapInfo::fail_continue("The shared archive file has a bad magic number."); |
1145 FileMapInfo::fail_continue("The shared archive file has a bad magic number."); |
1150 return false; |
1146 return false; |
1151 } |
1147 } |
1152 char header_version[JVM_IDENT_MAX]; |
1148 char header_version[JVM_IDENT_MAX]; |
1153 get_header_version(header_version); |
1149 get_header_version(header_version); |
1219 assert(idx == MetaspaceShared::ro || |
1215 assert(idx == MetaspaceShared::ro || |
1220 idx == MetaspaceShared::rw || |
1216 idx == MetaspaceShared::rw || |
1221 idx == MetaspaceShared::mc || |
1217 idx == MetaspaceShared::mc || |
1222 idx == MetaspaceShared::md, "invalid region index"); |
1218 idx == MetaspaceShared::md, "invalid region index"); |
1223 char* base = _header->region_addr(idx); |
1219 char* base = _header->region_addr(idx); |
1224 if (p >= base && p < base + _header->_space[idx]._used) { |
1220 if (p >= base && p < base + space_at(idx)->_used) { |
1225 return true; |
1221 return true; |
1226 } |
1222 } |
1227 return false; |
1223 return false; |
1228 } |
1224 } |
1229 |
1225 |
1230 void FileMapInfo::print_shared_spaces() { |
1226 void FileMapInfo::print_shared_spaces() { |
1231 tty->print_cr("Shared Spaces:"); |
1227 tty->print_cr("Shared Spaces:"); |
1232 for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
1228 for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
1233 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
1229 CDSFileMapRegion* si = space_at(i); |
1234 char *base = _header->region_addr(i); |
1230 char *base = _header->region_addr(i); |
1235 tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, |
1231 tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, |
1236 shared_region_name[i], |
1232 shared_region_name[i], |
1237 p2i(base), p2i(base + si->_used)); |
1233 p2i(base), p2i(base + si->_used)); |
1238 } |
1234 } |
1245 map_info->fail_continue("%s", msg); |
1241 map_info->fail_continue("%s", msg); |
1246 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) { |
1242 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) { |
1247 char *addr = map_info->_header->region_addr(i); |
1243 char *addr = map_info->_header->region_addr(i); |
1248 if (addr != NULL && !MetaspaceShared::is_heap_region(i)) { |
1244 if (addr != NULL && !MetaspaceShared::is_heap_region(i)) { |
1249 map_info->unmap_region(i); |
1245 map_info->unmap_region(i); |
1250 map_info->_header->_space[i]._addr._base = NULL; |
1246 map_info->space_at(i)->_addr._base = NULL; |
1251 } |
1247 } |
1252 } |
1248 } |
1253 // Dealloc the archive heap regions only without unmapping. The regions are part |
1249 // Dealloc the archive heap regions only without unmapping. The regions are part |
1254 // of the java heap. Unmapping of the heap regions are managed by GC. |
1250 // of the java heap. Unmapping of the heap regions are managed by GC. |
1255 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges, |
1251 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges, |