src/hotspot/share/memory/filemap.cpp
changeset 51491 187c84a5efe1
parent 51480 2e928420389d
child 51720 b7bfd64e43a6
equal deleted inserted replaced
51490:25048be67f4a 51491:187c84a5efe1
    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 
   186   _narrow_klass_base = Universe::narrow_klass_base();
   190   _narrow_klass_base = Universe::narrow_klass_base();
   187   _narrow_klass_shift = Universe::narrow_klass_shift();
   191   _narrow_klass_shift = Universe::narrow_klass_shift();
   188   _shared_path_table_size = mapinfo->_shared_path_table_size;
   192   _shared_path_table_size = mapinfo->_shared_path_table_size;
   189   _shared_path_table = mapinfo->_shared_path_table;
   193   _shared_path_table = mapinfo->_shared_path_table;
   190   _shared_path_entry_size = mapinfo->_shared_path_entry_size;
   194   _shared_path_entry_size = mapinfo->_shared_path_entry_size;
       
   195   if (MetaspaceShared::is_heap_object_archiving_allowed()) {
       
   196     _heap_reserved = Universe::heap()->reserved_region();
       
   197   }
   191 
   198 
   192   // The following fields are for sanity checks for whether this archive
   199   // The following fields are for sanity checks for whether this archive
   193   // will function correctly with this JVM and the bootclasspath it's
   200   // will function correctly with this JVM and the bootclasspath it's
   194   // invoked with.
   201   // invoked with.
   195 
   202 
   651 //
   658 //
   652 // "X" represented space that's occupied by heap objects.
   659 // "X" represented space that's occupied by heap objects.
   653 // "_" represented unused spaced in the heap region.
   660 // "_" represented unused spaced in the heap region.
   654 //
   661 //
   655 //
   662 //
   656 //    |ah0       | ah1 | ah2| ...... | ahn |
   663 //    |ah0       | ah1 | ah2| ...... | ahn|
   657 //    |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
   664 //    |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
   658 //    |<-r0->|   |<- r1 ----------------->|
   665 //    |<-r0->|   |<- r1 ----------------->|
   659 //            ^^^
   666 //            ^^^
   660 //             |
   667 //             |
   661 //             +-- gap
   668 //             +-- gap
   662 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
   669 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
       
   670                                                GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
   663                                                int first_region_id, int max_num_regions) {
   671                                                int first_region_id, int max_num_regions) {
   664   assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
   672   assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
   665 
   673 
   666   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
   674   int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
   667   if(arr_len > max_num_regions) {
   675   if(arr_len > max_num_regions) {
   682     }
   690     }
   683 
   691 
   684     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
   692     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
   685                   i, p2i(start), p2i(start + size), size);
   693                   i, p2i(start), p2i(start + size), size);
   686     write_region(i, start, size, false, false);
   694     write_region(i, start, size, false, false);
       
   695     if (size > 0) {
       
   696       space_at(i)->_oopmap = oopmaps->at(arr_idx)._oopmap;
       
   697       space_at(i)->_oopmap_size_in_bits = oopmaps->at(arr_idx)._oopmap_size_in_bits;
       
   698     }
   687   }
   699   }
   688   return total_size;
   700   return total_size;
   689 }
   701 }
   690 
   702 
   691 // Dump bytes to file -- at the current file position.
   703 // Dump bytes to file -- at the current file position.
   760   size_t used = si->_used;
   772   size_t used = si->_used;
   761   size_t size = align_up(used, os::vm_allocation_granularity());
   773   size_t size = align_up(used, os::vm_allocation_granularity());
   762   if (!open_for_read()) {
   774   if (!open_for_read()) {
   763     return false;
   775     return false;
   764   }
   776   }
   765   char *addr = _header->region_addr(idx);
   777   char *addr = region_addr(idx);
   766   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
   778   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
   767                                 addr, size, false /* !read_only */,
   779                                 addr, size, false /* !read_only */,
   768                                 si->_allow_exec);
   780                                 si->_allow_exec);
   769   close();
   781   close();
   770   if (base == NULL) {
   782   if (base == NULL) {
   779   return true;
   791   return true;
   780 }
   792 }
   781 
   793 
   782 // Map the whole region at once, assumed to be allocated contiguously.
   794 // Map the whole region at once, assumed to be allocated contiguously.
   783 ReservedSpace FileMapInfo::reserve_shared_memory() {
   795 ReservedSpace FileMapInfo::reserve_shared_memory() {
   784   char* requested_addr = _header->region_addr(0);
   796   char* requested_addr = region_addr(0);
   785   size_t size = FileMapInfo::core_spaces_size();
   797   size_t size = FileMapInfo::core_spaces_size();
   786 
   798 
   787   // Reserve the space first, then map otherwise map will go right over some
   799   // Reserve the space first, then map otherwise map will go right over some
   788   // other reserved memory (like the code cache).
   800   // other reserved memory (like the code cache).
   789   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
   801   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
   806   assert(!MetaspaceShared::is_heap_region(i), "sanity");
   818   assert(!MetaspaceShared::is_heap_region(i), "sanity");
   807   CDSFileMapRegion* si = space_at(i);
   819   CDSFileMapRegion* si = space_at(i);
   808   size_t used = si->_used;
   820   size_t used = si->_used;
   809   size_t alignment = os::vm_allocation_granularity();
   821   size_t alignment = os::vm_allocation_granularity();
   810   size_t size = align_up(used, alignment);
   822   size_t size = align_up(used, alignment);
   811   char *requested_addr = _header->region_addr(i);
   823   char *requested_addr = region_addr(i);
   812 
   824 
   813   // If a tool agent is in use (debugging enabled), we must map the address space RW
   825   // If a tool agent is in use (debugging enabled), we must map the address space RW
   814   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
   826   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
   815     si->_read_only = false;
   827     si->_read_only = false;
   816   }
   828   }
   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
   965                                 addr, regions[i].byte_size(), si->_read_only,
  1067                                 addr, regions[i].byte_size(), si->_read_only,
   966                                 si->_allow_exec);
  1068                                 si->_allow_exec);
   967     if (base == NULL || base != addr) {
  1069     if (base == NULL || base != addr) {
   968       // dealloc the regions from java heap
  1070       // dealloc the regions from java heap
   969       dealloc_archive_heap_regions(regions, region_num);
  1071       dealloc_archive_heap_regions(regions, region_num);
   970       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap.");
  1072       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
       
  1073                     INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
       
  1074                     p2i(addr), regions[i].byte_size());
   971       return false;
  1075       return false;
   972     }
  1076     }
   973   }
  1077   }
   974 
  1078 
   975   if (!verify_mapped_heap_regions(first, region_num)) {
  1079   if (!verify_mapped_heap_regions(first, region_num)) {
   993     }
  1097     }
   994   }
  1098   }
   995   return true;
  1099   return true;
   996 }
  1100 }
   997 
  1101 
       
  1102 void FileMapInfo::patch_archived_heap_embedded_pointers() {
       
  1103   if (!_heap_pointers_need_patching) {
       
  1104     return;
       
  1105   }
       
  1106 
       
  1107   patch_archived_heap_embedded_pointers(string_ranges,
       
  1108                                         num_string_ranges,
       
  1109                                         MetaspaceShared::first_string);
       
  1110 
       
  1111   patch_archived_heap_embedded_pointers(open_archive_heap_ranges,
       
  1112                                         num_open_archive_heap_ranges,
       
  1113                                         MetaspaceShared::first_open_archive_heap_region);
       
  1114 }
       
  1115 
       
  1116 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
       
  1117                                                         int first_region_idx) {
       
  1118   for (int i=0; i<num_ranges; i++) {
       
  1119     CDSFileMapRegion* si = space_at(i + first_region_idx);
       
  1120     HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->_oopmap,
       
  1121                                                       si->_oopmap_size_in_bits);
       
  1122   }
       
  1123 }
       
  1124 
       
  1125 // This internally allocates objects using SystemDictionary::Object_klass(), so it
       
  1126 // must be called after the well-known classes are resolved.
   998 void FileMapInfo::fixup_mapped_heap_regions() {
  1127 void FileMapInfo::fixup_mapped_heap_regions() {
   999   // If any string regions were found, call the fill routine to make them parseable.
  1128   // If any string regions were found, call the fill routine to make them parseable.
  1000   // Note that string_ranges may be non-NULL even if no ranges were found.
  1129   // Note that string_ranges may be non-NULL even if no ranges were found.
  1001   if (num_string_ranges != 0) {
  1130   if (num_string_ranges != 0) {
  1002     assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
  1131     assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
  1034        !StringTable::shared_string_mapped()) ||
  1163        !StringTable::shared_string_mapped()) ||
  1035       (MetaspaceShared::is_open_archive_heap_region(i) &&
  1164       (MetaspaceShared::is_open_archive_heap_region(i) &&
  1036        !MetaspaceShared::open_archive_heap_region_mapped())) {
  1165        !MetaspaceShared::open_archive_heap_region_mapped())) {
  1037     return true; // archived heap data is not mapped
  1166     return true; // archived heap data is not mapped
  1038   }
  1167   }
  1039   const char* buf = _header->region_addr(i);
  1168   const char* buf = region_addr(i);
  1040   int crc = ClassLoader::crc32(0, buf, (jint)sz);
  1169   int crc = ClassLoader::crc32(0, buf, (jint)sz);
  1041   if (crc != space_at(i)->_crc) {
  1170   if (crc != space_at(i)->_crc) {
  1042     fail_continue("Checksum verification failed.");
  1171     fail_continue("Checksum verification failed.");
  1043     return false;
  1172     return false;
  1044   }
  1173   }
  1055 
  1184 
  1056   if (used == 0) {
  1185   if (used == 0) {
  1057     return;
  1186     return;
  1058   }
  1187   }
  1059 
  1188 
  1060   char* addr = _header->region_addr(i);
  1189   char* addr = region_addr(i);
  1061   if (!os::unmap_memory(addr, size)) {
  1190   if (!os::unmap_memory(addr, size)) {
  1062     fail_stop("Unable to unmap shared space.");
  1191     fail_stop("Unable to unmap shared space.");
  1063   }
  1192   }
  1064 }
  1193 }
  1065 
  1194 
  1076   }
  1205   }
  1077 }
  1206 }
  1078 
  1207 
  1079 
  1208 
  1080 FileMapInfo* FileMapInfo::_current_info = NULL;
  1209 FileMapInfo* FileMapInfo::_current_info = NULL;
       
  1210 bool FileMapInfo::_heap_pointers_need_patching = false;
  1081 Array<u8>* FileMapInfo::_shared_path_table = NULL;
  1211 Array<u8>* FileMapInfo::_shared_path_table = NULL;
  1082 int FileMapInfo::_shared_path_table_size = 0;
  1212 int FileMapInfo::_shared_path_table_size = 0;
  1083 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
  1213 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
  1084 bool FileMapInfo::_validating_shared_path_table = false;
  1214 bool FileMapInfo::_validating_shared_path_table = false;
  1085 
  1215 
  1105     return false;
  1235     return false;
  1106   }
  1236   }
  1107   return true;
  1237   return true;
  1108 }
  1238 }
  1109 
  1239 
  1110 char* FileMapHeader::region_addr(int idx) {
  1240 char* FileMapInfo::region_addr(int idx) {
       
  1241   CDSFileMapRegion* si = space_at(idx);
  1111   if (MetaspaceShared::is_heap_region(idx)) {
  1242   if (MetaspaceShared::is_heap_region(idx)) {
  1112     return _space[idx]._used > 0 ?
  1243     assert(DumpSharedSpaces, "The following doesn't work at runtime");
  1113              (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
  1244     return si->_used > 0 ?
       
  1245           (char*)start_address_with_current_oop_encoding_mode(si) : NULL;
  1114   } else {
  1246   } else {
  1115     return _space[idx]._addr._base;
  1247     return si->_addr._base;
  1116   }
  1248   }
  1117 }
  1249 }
  1118 
  1250 
  1119 int FileMapHeader::compute_crc() {
  1251 int FileMapHeader::compute_crc() {
  1120   char* start = (char*)this;
  1252   char* start = (char*)this;
  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,