33 #include "classfile/altHashing.hpp" |
33 #include "classfile/altHashing.hpp" |
34 #include "logging/log.hpp" |
34 #include "logging/log.hpp" |
35 #include "logging/logStream.hpp" |
35 #include "logging/logStream.hpp" |
36 #include "logging/logMessage.hpp" |
36 #include "logging/logMessage.hpp" |
37 #include "memory/filemap.hpp" |
37 #include "memory/filemap.hpp" |
|
38 #include "memory/heapShared.inline.hpp" |
|
39 #include "memory/iterator.inline.hpp" |
38 #include "memory/metadataFactory.hpp" |
40 #include "memory/metadataFactory.hpp" |
39 #include "memory/metaspaceClosure.hpp" |
41 #include "memory/metaspaceClosure.hpp" |
40 #include "memory/metaspaceShared.hpp" |
42 #include "memory/metaspaceShared.hpp" |
41 #include "memory/oopFactory.hpp" |
43 #include "memory/oopFactory.hpp" |
42 #include "oops/compressedOops.inline.hpp" |
44 #include "oops/compressedOops.inline.hpp" |
43 #include "oops/objArrayOop.hpp" |
45 #include "oops/objArrayOop.hpp" |
|
46 #include "oops/oop.inline.hpp" |
44 #include "prims/jvmtiExport.hpp" |
47 #include "prims/jvmtiExport.hpp" |
45 #include "runtime/arguments.hpp" |
48 #include "runtime/arguments.hpp" |
46 #include "runtime/java.hpp" |
49 #include "runtime/java.hpp" |
47 #include "runtime/os.inline.hpp" |
50 #include "runtime/os.inline.hpp" |
48 #include "runtime/vm_version.hpp" |
51 #include "runtime/vm_version.hpp" |
49 #include "services/memTracker.hpp" |
52 #include "services/memTracker.hpp" |
50 #include "utilities/align.hpp" |
53 #include "utilities/align.hpp" |
51 #include "utilities/defaultStream.hpp" |
54 #include "utilities/defaultStream.hpp" |
52 #if INCLUDE_G1GC |
55 #if INCLUDE_G1GC |
53 #include "gc/g1/g1CollectedHeap.hpp" |
56 #include "gc/g1/g1CollectedHeap.hpp" |
|
57 #include "gc/g1/heapRegion.hpp" |
54 #endif |
58 #endif |
55 |
59 |
56 # include <sys/stat.h> |
60 # include <sys/stat.h> |
57 # include <errno.h> |
61 # include <errno.h> |
58 |
62 |
836 |
848 |
837 *top_ret = base + size; |
849 *top_ret = base + size; |
838 return base; |
850 return base; |
839 } |
851 } |
840 |
852 |
|
853 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) { |
|
854 if (with_current_oop_encoding_mode) { |
|
855 return (address)CompressedOops::decode_not_null(offset_of_space(spc)); |
|
856 } else { |
|
857 return (address)HeapShared::decode_with_archived_oop_encoding_mode(offset_of_space(spc)); |
|
858 } |
|
859 } |
|
860 |
841 static MemRegion *string_ranges = NULL; |
861 static MemRegion *string_ranges = NULL; |
842 static MemRegion *open_archive_heap_ranges = NULL; |
862 static MemRegion *open_archive_heap_ranges = NULL; |
843 static int num_string_ranges = 0; |
863 static int num_string_ranges = 0; |
844 static int num_open_archive_heap_ranges = 0; |
864 static int num_open_archive_heap_ranges = 0; |
845 |
865 |
846 #if INCLUDE_CDS_JAVA_HEAP |
866 #if INCLUDE_CDS_JAVA_HEAP |
|
867 bool FileMapInfo::has_heap_regions() { |
|
868 return (_header->_space[MetaspaceShared::first_string]._used > 0); |
|
869 } |
|
870 |
|
871 // Returns the address range of the archived heap regions computed using the |
|
872 // current oop encoding mode. This range may be different than the one seen at |
|
873 // dump time due to encoding mode differences. The result is used in determining |
|
874 // if/how these regions should be relocated at run time. |
|
875 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() { |
|
876 address start = (address) max_uintx; |
|
877 address end = NULL; |
|
878 |
|
879 for (int i = MetaspaceShared::first_string; i <= MetaspaceShared::last_valid_region; i++) { |
|
880 CDSFileMapRegion* si = space_at(i); |
|
881 size_t size = si->_used; |
|
882 if (size > 0) { |
|
883 address s = start_address_with_current_oop_encoding_mode(si); |
|
884 address e = s + size; |
|
885 if (start > s) { |
|
886 start = s; |
|
887 } |
|
888 if (end < e) { |
|
889 end = e; |
|
890 } |
|
891 } |
|
892 } |
|
893 assert(end != NULL, "must have at least one used heap region"); |
|
894 return MemRegion((HeapWord*)start, (HeapWord*)end); |
|
895 } |
|
896 |
847 // |
897 // |
848 // Map the shared string objects and open archive heap objects to the runtime |
898 // Map the shared string objects and open archive heap objects to the runtime |
849 // java heap. |
899 // java heap. |
850 // |
900 // |
851 // The shared strings are mapped near the runtime java heap top. The |
901 // The shared strings are mapped close to the end of the java heap top in |
852 // mapped strings contain no out-going references to any other java heap |
902 // closed archive regions. The mapped strings contain no out-going references |
853 // regions. GC does not write into the mapped shared strings. |
903 // to any other java heap regions. GC does not write into the mapped shared strings. |
854 // |
904 // |
855 // The open archive heap objects are mapped below the shared strings in |
905 // The open archive heap objects are mapped below the shared strings in |
856 // the runtime java heap. The mapped open archive heap data only contain |
906 // the runtime java heap. The mapped open archive heap data only contain |
857 // references to the shared strings and open archive objects initially. |
907 // references to the shared strings and open archive objects initially. |
858 // During runtime execution, out-going references to any other java heap |
908 // During runtime execution, out-going references to any other java heap |
859 // regions may be added. GC may mark and update references in the mapped |
909 // regions may be added. GC may mark and update references in the mapped |
860 // open archive objects. |
910 // open archive objects. |
|
911 void FileMapInfo::map_heap_regions_impl() { |
|
912 if (!MetaspaceShared::is_heap_object_archiving_allowed()) { |
|
913 log_info(cds)("CDS heap data is being ignored. UseG1GC, " |
|
914 "UseCompressedOops and UseCompressedClassPointers are required."); |
|
915 return; |
|
916 } |
|
917 |
|
918 MemRegion heap_reserved = Universe::heap()->reserved_region(); |
|
919 |
|
920 log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:", |
|
921 max_heap_size()/M); |
|
922 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", |
|
923 p2i(narrow_klass_base()), narrow_klass_shift()); |
|
924 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", |
|
925 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift()); |
|
926 |
|
927 log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT, |
|
928 heap_reserved.byte_size()/M, HeapRegion::GrainBytes); |
|
929 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", |
|
930 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift()); |
|
931 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", |
|
932 Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()), Universe::narrow_oop_shift()); |
|
933 |
|
934 if (narrow_klass_base() != Universe::narrow_klass_base() || |
|
935 narrow_klass_shift() != Universe::narrow_klass_shift()) { |
|
936 log_info(cds)("CDS heap data cannot be used because the archive was created with an incompatible narrow klass encoding mode."); |
|
937 return; |
|
938 } |
|
939 |
|
940 if (narrow_oop_mode() != Universe::narrow_oop_mode() || |
|
941 narrow_oop_base() != Universe::narrow_oop_base() || |
|
942 narrow_oop_shift() != Universe::narrow_oop_shift()) { |
|
943 log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode."); |
|
944 _heap_pointers_need_patching = true; |
|
945 } else { |
|
946 MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode(); |
|
947 if (!heap_reserved.contains(range)) { |
|
948 log_info(cds)("CDS heap data need to be relocated because"); |
|
949 log_info(cds)("the desired range " PTR_FORMAT " - " PTR_FORMAT, p2i(range.start()), p2i(range.end())); |
|
950 log_info(cds)("is outside of the heap " PTR_FORMAT " - " PTR_FORMAT, p2i(heap_reserved.start()), p2i(heap_reserved.end())); |
|
951 _heap_pointers_need_patching = true; |
|
952 } |
|
953 } |
|
954 |
|
955 ptrdiff_t delta = 0; |
|
956 if (_heap_pointers_need_patching) { |
|
957 // dumptime heap end ------------v |
|
958 // [ |archived heap regions| ] runtime heap end ------v |
|
959 // [ |archived heap regions| ] |
|
960 // |<-----delta-------------------->| |
|
961 // |
|
962 // At dump time, the archived heap regions were near the top of the heap. |
|
963 // At run time, they may not be inside the heap, so we move them so |
|
964 // that they are now near the top of the runtime time. This can be done by |
|
965 // the simple math of adding the delta as shown above. |
|
966 address dumptime_heap_end = (address)_header->_heap_reserved.end(); |
|
967 address runtime_heap_end = (address)heap_reserved.end(); |
|
968 delta = runtime_heap_end - dumptime_heap_end; |
|
969 } |
|
970 |
|
971 log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta); |
|
972 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift()); |
|
973 |
|
974 CDSFileMapRegion* si = space_at(MetaspaceShared::first_string); |
|
975 address relocated_strings_bottom = start_address_with_archived_oop_encoding_mode(si); |
|
976 if (!is_aligned(relocated_strings_bottom + delta, HeapRegion::GrainBytes)) { |
|
977 // Align the bottom of the string regions at G1 region boundary. This will avoid |
|
978 // the situation where the highest open region and the lowest string region sharing |
|
979 // the same G1 region. Otherwise we will fail to map the open regions. |
|
980 size_t align = size_t(relocated_strings_bottom) % HeapRegion::GrainBytes; |
|
981 delta -= align; |
|
982 assert(is_aligned(relocated_strings_bottom + delta, HeapRegion::GrainBytes), "must be"); |
|
983 |
|
984 log_info(cds)("CDS heap data need to be relocated lower by a further " SIZE_FORMAT |
|
985 " bytes to be aligned with HeapRegion::GrainBytes", align); |
|
986 |
|
987 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift()); |
|
988 _heap_pointers_need_patching = true; |
|
989 } |
|
990 |
|
991 // First, map string regions as closed archive heap regions. |
|
992 // GC does not write into the regions. |
|
993 if (map_heap_data(&string_ranges, |
|
994 MetaspaceShared::first_string, |
|
995 MetaspaceShared::max_strings, |
|
996 &num_string_ranges)) { |
|
997 StringTable::set_shared_string_mapped(); |
|
998 |
|
999 // Now, map open_archive heap regions, GC can write into the regions. |
|
1000 if (map_heap_data(&open_archive_heap_ranges, |
|
1001 MetaspaceShared::first_open_archive_heap_region, |
|
1002 MetaspaceShared::max_open_archive_heap_region, |
|
1003 &num_open_archive_heap_ranges, |
|
1004 true /* open */)) { |
|
1005 MetaspaceShared::set_open_archive_heap_region_mapped(); |
|
1006 } |
|
1007 } |
|
1008 } |
|
1009 |
861 void FileMapInfo::map_heap_regions() { |
1010 void FileMapInfo::map_heap_regions() { |
862 if (MetaspaceShared::is_heap_object_archiving_allowed()) { |
1011 if (has_heap_regions()) { |
863 log_info(cds)("Archived narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", |
1012 map_heap_regions_impl(); |
864 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift()); |
|
865 log_info(cds)("Archived narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", |
|
866 p2i(narrow_klass_base()), narrow_klass_shift()); |
|
867 |
|
868 // Check that all the narrow oop and klass encodings match the archive |
|
869 if (narrow_oop_mode() != Universe::narrow_oop_mode() || |
|
870 narrow_oop_base() != Universe::narrow_oop_base() || |
|
871 narrow_oop_shift() != Universe::narrow_oop_shift() || |
|
872 narrow_klass_base() != Universe::narrow_klass_base() || |
|
873 narrow_klass_shift() != Universe::narrow_klass_shift()) { |
|
874 if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) { |
|
875 log_info(cds)("Cached heap data from the CDS archive is being ignored. " |
|
876 "The current CompressedOops/CompressedClassPointers encoding differs from " |
|
877 "that archived due to heap size change. The archive was dumped using max heap " |
|
878 "size " UINTX_FORMAT "M.", max_heap_size()/M); |
|
879 log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d", |
|
880 Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()), |
|
881 Universe::narrow_oop_shift()); |
|
882 log_info(cds)("Current narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d", |
|
883 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift()); |
|
884 } |
|
885 } else { |
|
886 // First, map string regions as closed archive heap regions. |
|
887 // GC does not write into the regions. |
|
888 if (map_heap_data(&string_ranges, |
|
889 MetaspaceShared::first_string, |
|
890 MetaspaceShared::max_strings, |
|
891 &num_string_ranges)) { |
|
892 StringTable::set_shared_string_mapped(); |
|
893 |
|
894 // Now, map open_archive heap regions, GC can write into the regions. |
|
895 if (map_heap_data(&open_archive_heap_ranges, |
|
896 MetaspaceShared::first_open_archive_heap_region, |
|
897 MetaspaceShared::max_open_archive_heap_region, |
|
898 &num_open_archive_heap_ranges, |
|
899 true /* open */)) { |
|
900 MetaspaceShared::set_open_archive_heap_region_mapped(); |
|
901 } |
|
902 } |
|
903 } |
|
904 } else { |
|
905 if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) { |
|
906 log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, " |
|
907 "UseCompressedOops and UseCompressedClassPointers are required."); |
|
908 } |
|
909 } |
1013 } |
910 |
1014 |
911 if (!StringTable::shared_string_mapped()) { |
1015 if (!StringTable::shared_string_mapped()) { |
912 assert(string_ranges == NULL && num_string_ranges == 0, "sanity"); |
1016 assert(string_ranges == NULL && num_string_ranges == 0, "sanity"); |
913 } |
1017 } |
924 int region_num = 0; |
1028 int region_num = 0; |
925 |
1029 |
926 for (int i = first; |
1030 for (int i = first; |
927 i < first + max; i++) { |
1031 i < first + max; i++) { |
928 si = space_at(i); |
1032 si = space_at(i); |
929 size_t used = si->_used; |
1033 size_t size = si->_used; |
930 if (used > 0) { |
1034 if (size > 0) { |
931 size_t size = used; |
1035 HeapWord* start = (HeapWord*)start_address_with_archived_oop_encoding_mode(si); |
932 char* requested_addr = (char*)((void*)CompressedOops::decode_not_null( |
1036 regions[region_num] = MemRegion(start, size / HeapWordSize); |
933 (narrowOop)si->_addr._offset)); |
|
934 regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize); |
|
935 region_num ++; |
1037 region_num ++; |
|
1038 log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes", |
|
1039 i, p2i(start), size); |
936 } |
1040 } |
937 } |
1041 } |
938 |
1042 |
939 if (region_num == 0) { |
1043 if (region_num == 0) { |
940 return false; // no archived java heap data |
1044 return false; // no archived java heap data |
941 } |
1045 } |
942 |
1046 |
943 // Check that ranges are within the java heap |
1047 // Check that ranges are within the java heap |
944 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) { |
1048 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) { |
945 log_info(cds)("UseSharedSpaces: Unable to allocate region, " |
1049 log_info(cds)("UseSharedSpaces: Unable to allocate region, range is not within java heap."); |
946 "range is not within java heap."); |
|
947 return false; |
1050 return false; |
948 } |
1051 } |
949 |
1052 |
950 // allocate from java heap |
1053 // allocate from java heap |
951 if (!G1CollectedHeap::heap()->alloc_archive_regions( |
1054 if (!G1CollectedHeap::heap()->alloc_archive_regions( |
952 regions, region_num, is_open_archive)) { |
1055 regions, region_num, is_open_archive)) { |
953 log_info(cds)("UseSharedSpaces: Unable to allocate region, " |
1056 log_info(cds)("UseSharedSpaces: Unable to allocate region, java heap range is already in use."); |
954 "java heap range is already in use."); |
|
955 return false; |
1057 return false; |
956 } |
1058 } |
957 |
1059 |
958 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type() |
1060 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type() |
959 // for mapped regions as they are part of the reserved java heap, which is |
1061 // for mapped regions as they are part of the reserved java heap, which is |
1214 bool FileMapInfo::is_in_shared_region(const void* p, int idx) { |
1346 bool FileMapInfo::is_in_shared_region(const void* p, int idx) { |
1215 assert(idx == MetaspaceShared::ro || |
1347 assert(idx == MetaspaceShared::ro || |
1216 idx == MetaspaceShared::rw || |
1348 idx == MetaspaceShared::rw || |
1217 idx == MetaspaceShared::mc || |
1349 idx == MetaspaceShared::mc || |
1218 idx == MetaspaceShared::md, "invalid region index"); |
1350 idx == MetaspaceShared::md, "invalid region index"); |
1219 char* base = _header->region_addr(idx); |
1351 char* base = region_addr(idx); |
1220 if (p >= base && p < base + space_at(idx)->_used) { |
1352 if (p >= base && p < base + space_at(idx)->_used) { |
1221 return true; |
1353 return true; |
1222 } |
1354 } |
1223 return false; |
1355 return false; |
1224 } |
|
1225 |
|
1226 void FileMapInfo::print_shared_spaces() { |
|
1227 tty->print_cr("Shared Spaces:"); |
|
1228 for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
|
1229 CDSFileMapRegion* si = space_at(i); |
|
1230 char *base = _header->region_addr(i); |
|
1231 tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, |
|
1232 shared_region_name[i], |
|
1233 p2i(base), p2i(base + si->_used)); |
|
1234 } |
|
1235 } |
1356 } |
1236 |
1357 |
1237 // Unmap mapped regions of shared space. |
1358 // Unmap mapped regions of shared space. |
1238 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { |
1359 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { |
1239 FileMapInfo *map_info = FileMapInfo::current_info(); |
1360 FileMapInfo *map_info = FileMapInfo::current_info(); |
1240 if (map_info) { |
1361 if (map_info) { |
1241 map_info->fail_continue("%s", msg); |
1362 map_info->fail_continue("%s", msg); |
1242 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) { |
1363 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) { |
1243 char *addr = map_info->_header->region_addr(i); |
1364 if (!MetaspaceShared::is_heap_region(i)) { |
1244 if (addr != NULL && !MetaspaceShared::is_heap_region(i)) { |
1365 char *addr = map_info->region_addr(i); |
1245 map_info->unmap_region(i); |
1366 if (addr != NULL) { |
1246 map_info->space_at(i)->_addr._base = NULL; |
1367 map_info->unmap_region(i); |
|
1368 map_info->space_at(i)->_addr._base = NULL; |
|
1369 } |
1247 } |
1370 } |
1248 } |
1371 } |
1249 // Dealloc the archive heap regions only without unmapping. The regions are part |
1372 // Dealloc the archive heap regions only without unmapping. The regions are part |
1250 // of the java heap. Unmapping of the heap regions are managed by GC. |
1373 // of the java heap. Unmapping of the heap regions are managed by GC. |
1251 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges, |
1374 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges, |