< prev index next >

src/share/vm/memory/filemap.cpp

Print this page




 334   }
 335   _file_offset = (long)n;
 336 
 337   size_t info_size = _header->_paths_misc_info_size;
 338   _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
 339   if (_paths_misc_info == NULL) {
 340     fail_continue("Unable to read the file header.");
 341     return false;
 342   }
 343   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
 344   if (n != info_size) {
 345     fail_continue("Unable to read the shared path info header.");
 346     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 347     _paths_misc_info = NULL;
 348     return false;
 349   }
 350 
 351   size_t len = lseek(fd, 0, SEEK_END);
 352   struct FileMapInfo::FileMapHeader::space_info* si =
 353     &_header->_space[MetaspaceShared::last_valid_region];
 354   if (si->_file_offset >= len || len - si->_file_offset < si->_used) {

 355     fail_continue("The shared archive file has been truncated.");
 356     return false;
 357   }
 358 
 359   _file_offset += (long)n;
 360   return true;
 361 }
 362 
 363 
 364 // Read the FileMapInfo information from the file.
 365 bool FileMapInfo::open_for_read() {
 366   _full_path = Arguments::GetSharedArchivePath();
 367   int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
 368   if (fd < 0) {
 369     if (errno == ENOENT) {
 370       // Not locating the shared archive is ok.
 371       fail_continue("Specified shared archive not found.");
 372     } else {
 373       fail_continue("Failed to open shared archive file (%s).",
 374                     os::strerror(errno));


 426   write_bytes(addr, (int)sz); // skip the C++ vtable
 427   write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
 428   align_file_position();
 429 }
 430 
 431 
 432 // Dump region to file.
 433 
 434 void FileMapInfo::write_region(int region, char* base, size_t size,
 435                                bool read_only, bool allow_exec) {
 436   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
 437 
 438   if (_file_open) {
 439     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 440     log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
 441                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
 442                   region, size, p2i(base), _file_offset);
 443   } else {
 444     si->_file_offset = _file_offset;
 445   }
 446   if (MetaspaceShared::is_string_region(region)) {
 447     assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
 448     if (base != NULL) {
 449       si->_addr._offset = (intx)oopDesc::encode_heap_oop_not_null((oop)base);
 450     } else {
 451       si->_addr._offset = 0;
 452     }
 453   } else {
 454     si->_addr._base = base;
 455   }
 456   si->_used = size;
 457   si->_read_only = read_only;
 458   si->_allow_exec = allow_exec;
 459   si->_crc = ClassLoader::crc32(0, base, (jint)size);
 460   write_bytes_aligned(base, (int)size);
 461 }
 462 
 463 // Write the string space. The string space contains one or multiple GC(G1) regions.
 464 // When the total string space size is smaller than one GC region of the dump time,
 465 // only one string region is used for shared strings.
 466 //
 467 // If the total string space size is bigger than one GC region, there would be more
 468 // than one GC regions allocated for shared strings. The first/bottom GC region might
 469 // be a partial GC region with the empty portion at the higher address within that region.
 470 // The non-empty portion of the first region is written into the archive as one string
 471 // region. The rest are consecutive full GC regions if they exist, which can be written
 472 // out in one chunk as another string region.
 473 //
 474 // Here's the mapping from (GrowableArray<MemRegion> *regions) -> (metaspace string regions).
 475 //   + We have 1 or more heap regions: r0, r1, r2 ..... rn
 476 //   + We have 2 metaspace string regions: s0 and s1
 477 //
 478 // If there's a single heap region (r0), then s0 == r0, and s1 is empty.
 479 // Otherwise:
 480 //
 481 // "X" represented space that's occupied by heap objects.
 482 // "_" represented unused spaced in the heap region.
 483 //
 484 //
 485 //    |r0        | r1  | r2 | ...... | rn |
 486 //    |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
 487 //    |<-s0->|   |<- s1 ----------------->|
 488 //            ^^^
 489 //             |
 490 //             +-- unmapped space
 491 void FileMapInfo::write_string_regions(GrowableArray<MemRegion> *regions,
 492                                        char** st0_start, char** st0_top, char** st0_end,
 493                                        char** st1_start, char** st1_top, char** st1_end) {
 494   *st0_start = *st0_top = *st0_end = NULL;
 495   *st1_start = *st1_top = *st1_end = NULL;
 496 
 497   assert(MetaspaceShared::max_strings == 2, "this loop doesn't work for any other value");
 498   for (int i = MetaspaceShared::first_string;
 499            i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {



 500     char* start = NULL;
 501     size_t size = 0;
 502     int len = regions->length();
 503     if (len > 0) {
 504       if (i == MetaspaceShared::first_string) {
 505         MemRegion first = regions->first();
 506         start = (char*)first.start();
 507         size = first.byte_size();
 508         *st0_start = start;
 509         *st0_top = start + size;
 510         if (len > 1) {
 511           *st0_end = (char*)regions->at(1).start();
 512         } else {
 513           *st0_end = start + size;
 514         }
 515       } else {
 516         assert(i == MetaspaceShared::first_string + 1, "must be");
 517         if (len > 1) {
 518           start = (char*)regions->at(1).start();
 519           size = (char*)regions->at(len - 1).end() - start;
 520           *st1_start = start;
 521           *st1_top = start + size;
 522           *st1_end = start + size;
 523         }
 524       }
 525     }
 526     log_info(cds)("String region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
 527                   i, p2i(start), p2i(start + size), size);
 528     write_region(i, start, size, false, false);
 529   }
 530 }
 531 
 532 
 533 // Dump bytes to file -- at the current file position.
 534 
 535 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 536   if (_file_open) {
 537     int n = ::write(_fd, buffer, nbytes);
 538     if (n != nbytes) {
 539       // It is dangerous to leave the corrupted shared archive file around,
 540       // close and remove the file. See bug 6372906.
 541       close();
 542       remove(_full_path);
 543       fail_stop("Unable to write to shared archive file.");
 544     }
 545   }
 546   _file_offset += nbytes;
 547 }
 548 
 549 
 550 // Align file position to an allocation unit boundary.
 551 
 552 void FileMapInfo::align_file_position() {


 624 // Map the whole region at once, assumed to be allocated contiguously.
 625 ReservedSpace FileMapInfo::reserve_shared_memory() {
 626   char* requested_addr = _header->region_addr(0);
 627   size_t size = FileMapInfo::core_spaces_size();
 628 
 629   // Reserve the space first, then map otherwise map will go right over some
 630   // other reserved memory (like the code cache).
 631   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 632   if (!rs.is_reserved()) {
 633     fail_continue("Unable to reserve shared space at required address "
 634                   INTPTR_FORMAT, p2i(requested_addr));
 635     return rs;
 636   }
 637   // the reserved virtual memory is for mapping class data sharing archive
 638   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 639 
 640   return rs;
 641 }
 642 
 643 // Memory map a region in the address space.
 644 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode",
 645                                             "String1", "String2", "OptionalData" };
 646 
 647 char* FileMapInfo::map_region(int i) {
 648   assert(!MetaspaceShared::is_string_region(i), "sanity");
 649   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 650   size_t used = si->_used;
 651   size_t alignment = os::vm_allocation_granularity();
 652   size_t size = align_up(used, alignment);
 653   char *requested_addr = _header->region_addr(i);
 654 
 655   // If a tool agent is in use (debugging enabled), we must map the address space RW
 656   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
 657     si->_read_only = false;
 658   }
 659 
 660   // map the contents of the CDS archive in this memory
 661   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 662                               requested_addr, size, si->_read_only,
 663                               si->_allow_exec);
 664   if (base == NULL || base != requested_addr) {
 665     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 666     return NULL;
 667   }
 668 #ifdef _WINDOWS
 669   // This call is Windows-only because the memory_type gets recorded for the other platforms
 670   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 671   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 672 #endif
 673 
 674   return base;
 675 }
 676 
 677 static MemRegion *string_ranges = NULL;
 678 static int num_ranges = 0;
 679 bool FileMapInfo::map_string_regions() {
 680 #if INCLUDE_ALL_GCS
 681   if (UseG1GC && UseCompressedOops && UseCompressedClassPointers) {

















 682     // Check that all the narrow oop and klass encodings match the archive
 683     if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
 684         narrow_oop_shift() != Universe::narrow_oop_shift() ||
 685         narrow_klass_base() != Universe::narrow_klass_base() ||
 686         narrow_klass_shift() != Universe::narrow_klass_shift()) {
 687       if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
 688         log_info(cds)("Shared string data from the CDS archive is being ignored. "
 689                       "The current CompressedOops/CompressedClassPointers encoding differs from "
 690                       "that archived due to heap size change. The archive was dumped using max heap "
 691                       "size " UINTX_FORMAT "M.", max_heap_size()/M);
 692       }
 693     } else {
 694       string_ranges = new MemRegion[MetaspaceShared::max_strings];




































 695       struct FileMapInfo::FileMapHeader::space_info* si;

 696 
 697       for (int i = MetaspaceShared::first_string;
 698                i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {
 699         si = &_header->_space[i];
 700         size_t used = si->_used;
 701         if (used > 0) {
 702           size_t size = used;
 703           char* requested_addr = (char*)((void*)oopDesc::decode_heap_oop_not_null(
 704                                                  (narrowOop)si->_addr._offset));
 705           string_ranges[num_ranges] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
 706           num_ranges ++;
 707         }
 708       }
 709 
 710       if (num_ranges == 0) {
 711         StringTable::ignore_shared_strings(true);
 712         return true; // no shared string data
 713       }
 714 
 715       // Check that ranges are within the java heap
 716       if (!G1CollectedHeap::heap()->check_archive_addresses(string_ranges, num_ranges)) {
 717         fail_continue("Unable to allocate shared string space: range is not "
 718                       "within java heap.");


 719         return false;
 720       }
 721 
 722       // allocate from java heap
 723       if (!G1CollectedHeap::heap()->alloc_archive_regions(string_ranges, num_ranges)) {
 724         fail_continue("Unable to allocate shared string space: range is "
 725                       "already in use.");



 726         return false;
 727       }
 728 
 729       // Map the string data. No need to call MemTracker::record_virtual_memory_type()
 730       // for mapped string regions as they are part of the reserved java heap, which
 731       // is already recorded.
 732       for (int i = 0; i < num_ranges; i++) {
 733         si = &_header->_space[MetaspaceShared::first_string + i];
 734         char* addr = (char*)string_ranges[i].start();
 735         char* base = os::map_memory(_fd, _full_path, si->_file_offset,
 736                                     addr, string_ranges[i].byte_size(), si->_read_only,
 737                                     si->_allow_exec);
 738         if (base == NULL || base != addr) {
 739           // dealloc the string regions from java heap
 740           dealloc_string_regions();
 741           fail_continue("Unable to map shared string space at required address.");
 742           return false;
 743         }
 744       }
 745 
 746       if (!verify_string_regions()) {
 747         // dealloc the string regions from java heap
 748         dealloc_string_regions();
 749         fail_continue("Shared string regions are corrupt");
 750         return false;
 751       }
 752 
 753       // the shared string data is mapped successfully
 754       return true;
 755     }
 756   } else {
 757     if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
 758       log_info(cds)("Shared string data from the CDS archive is being ignored. UseG1GC, "
 759                     "UseCompressedOops and UseCompressedClassPointers are required.");


 760     }

 761   }
 762 
 763   // if we get here, the shared string data is not mapped
 764   assert(string_ranges == NULL && num_ranges == 0, "sanity");
 765   StringTable::ignore_shared_strings(true);
 766 #endif
 767   return true;
 768 }
 769 
 770 bool FileMapInfo::verify_string_regions() {
 771   for (int i = MetaspaceShared::first_string;
 772            i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) {
 773     if (!verify_region_checksum(i)) {
 774       return false;
 775     }
 776   }
 777   return true;
 778 }
 779 
 780 void FileMapInfo::fixup_string_regions() {
 781 #if INCLUDE_ALL_GCS
 782   // If any string regions were found, call the fill routine to make them parseable.
 783   // Note that string_ranges may be non-NULL even if no ranges were found.
 784   if (num_ranges != 0) {
 785     assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
 786     G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_ranges);















 787   }
 788 #endif
 789 }

 790 
 791 bool FileMapInfo::verify_region_checksum(int i) {
 792   if (!VerifySharedSpaces) {
 793     return true;
 794   }
 795 
 796   size_t sz = _header->_space[i]._used;
 797 
 798   if (sz == 0) {
 799     return true; // no data
 800   }
 801   if (MetaspaceShared::is_string_region(i) && StringTable::shared_string_ignored()) {
 802     return true; // shared string data are not mapped



 803   }
 804   const char* buf = _header->region_addr(i);
 805   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 806   if (crc != _header->_space[i]._crc) {
 807     fail_continue("Checksum verification failed.");
 808     return false;
 809   }
 810   return true;
 811 }
 812 
 813 // Unmap a memory region in the address space.
 814 
 815 void FileMapInfo::unmap_region(int i) {
 816   assert(!MetaspaceShared::is_string_region(i), "sanity");
 817   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 818   size_t used = si->_used;
 819   size_t size = align_up(used, os::vm_allocation_granularity());
 820 
 821   if (used == 0) {
 822     return;
 823   }
 824 
 825   char* addr = _header->region_addr(i);
 826   if (!os::unmap_memory(addr, size)) {
 827     fail_stop("Unable to unmap shared space.");
 828   }
 829 }
 830 
 831 // dealloc the archived string region from java heap
 832 void FileMapInfo::dealloc_string_regions() {
 833 #if INCLUDE_ALL_GCS
 834   if (num_ranges > 0) {
 835     assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
 836     G1CollectedHeap::heap()->dealloc_archive_regions(string_ranges, num_ranges);
 837   }
 838 #endif
 839 }
 840 
 841 void FileMapInfo::assert_mark(bool check) {
 842   if (!check) {
 843     fail_stop("Mark mismatch while restoring from shared file.");
 844   }
 845 }
 846 
 847 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
 848   it->push(&_classpath_entry_table);
 849   for (int i=0; i<_classpath_entry_table_size; i++) {
 850     shared_classpath(i)->metaspace_pointers_do(it);
 851   }
 852 }
 853 
 854 
 855 FileMapInfo* FileMapInfo::_current_info = NULL;
 856 Array<u8>* FileMapInfo::_classpath_entry_table = NULL;
 857 int FileMapInfo::_classpath_entry_table_size = 0;
 858 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
 859 bool FileMapInfo::_validating_classpath_entry_table = false;
 860 


 866 // Validation of the archive is done in two steps:
 867 //
 868 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
 869 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW
 870 //     region of the archive, which is not mapped yet.
 871 bool FileMapInfo::initialize() {
 872   assert(UseSharedSpaces, "UseSharedSpaces expected.");
 873 
 874   if (!open_for_read()) {
 875     return false;
 876   }
 877 
 878   init_from_file(_fd);
 879   if (!validate_header()) {
 880     return false;
 881   }
 882   return true;
 883 }
 884 
 885 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
 886   if (MetaspaceShared::is_string_region(idx)) {
 887     return (char*)((void*)oopDesc::decode_heap_oop_not_null(
 888               (narrowOop)_space[idx]._addr._offset));
 889   } else {
 890     return _space[idx]._addr._base;
 891   }
 892 }
 893 
 894 int FileMapInfo::FileMapHeader::compute_crc() {
 895   char* header = data();
 896   // start computing from the field after _crc
 897   char* buf = (char*)&_crc + sizeof(int);
 898   size_t sz = data_size() - (buf - header);
 899   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 900   return crc;
 901 }
 902 
 903 bool FileMapInfo::FileMapHeader::validate() {
 904   if (VerifySharedSpaces && compute_crc() != _crc) {
 905     fail_continue("Header checksum verification failed.");
 906     return false;
 907   }
 908 


 948 bool FileMapInfo::validate_header() {
 949   bool status = _header->validate();
 950 
 951   if (status) {
 952     if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) {
 953       if (!PrintSharedArchiveAndExit) {
 954         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
 955         status = false;
 956       }
 957     }
 958   }
 959 
 960   if (_paths_misc_info != NULL) {
 961     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 962     _paths_misc_info = NULL;
 963   }
 964   return status;
 965 }
 966 
 967 // The following method is provided to see whether a given pointer
 968 // falls in the mapped shared space.
 969 // Param:
 970 // p, The given pointer
 971 // Return:
 972 // True if the p is within the mapped shared space, otherwise, false.
 973 bool FileMapInfo::is_in_shared_space(const void* p) {
 974   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 975     char *base;
 976     if (MetaspaceShared::is_string_region(i) && _header->_space[i]._used == 0) {
 977       continue;
 978     }
 979     base = _header->region_addr(i);
 980     if (p >= base && p < base + _header->_space[i]._used) {
 981       return true;
 982     }
 983   }
 984 
 985   return false;
 986 }
 987 
 988 // Check if a given address is within one of the shared regions ( ro, rw, mc or md)
 989 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
 990   assert(idx == MetaspaceShared::ro ||
 991          idx == MetaspaceShared::rw ||
 992          idx == MetaspaceShared::mc ||
 993          idx == MetaspaceShared::md, "invalid region index");
 994   char* base = _header->region_addr(idx);
 995   if (p >= base && p < base + _header->_space[idx]._used) {
 996     return true;
 997   }
 998   return false;
 999 }
1000 
1001 void FileMapInfo::print_shared_spaces() {
1002   tty->print_cr("Shared Spaces:");
1003   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
1004     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
1005     char *base = _header->region_addr(i);
1006     tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
1007                         shared_region_name[i],
1008                         p2i(base), p2i(base + si->_used));
1009   }
1010 }
1011 
1012 // Unmap mapped regions of shared space.
1013 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1014   FileMapInfo *map_info = FileMapInfo::current_info();
1015   if (map_info) {
1016     map_info->fail_continue("%s", msg);
1017     for (int i = 0; i < MetaspaceShared::num_non_strings; i++) {
1018       char *addr = map_info->_header->region_addr(i);
1019       if (addr != NULL && !MetaspaceShared::is_string_region(i)) {
1020         map_info->unmap_region(i);
1021         map_info->_header->_space[i]._addr._base = NULL;
1022       }
1023     }
1024     // Dealloc the string regions only without unmapping. The string regions are part
1025     // of the java heap. Unmapping of the heap regions are managed by GC.
1026     map_info->dealloc_string_regions();


1027   } else if (DumpSharedSpaces) {
1028     fail_stop("%s", msg);
1029   }
1030 }


 334   }
 335   _file_offset = (long)n;
 336 
 337   size_t info_size = _header->_paths_misc_info_size;
 338   _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
 339   if (_paths_misc_info == NULL) {
 340     fail_continue("Unable to read the file header.");
 341     return false;
 342   }
 343   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
 344   if (n != info_size) {
 345     fail_continue("Unable to read the shared path info header.");
 346     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 347     _paths_misc_info = NULL;
 348     return false;
 349   }
 350 
 351   size_t len = lseek(fd, 0, SEEK_END);
 352   struct FileMapInfo::FileMapHeader::space_info* si =
 353     &_header->_space[MetaspaceShared::last_valid_region];
 354   // The last space might be empty
 355   if (si->_file_offset > len || len - si->_file_offset < si->_used) {
 356     fail_continue("The shared archive file has been truncated.");
 357     return false;
 358   }
 359 
 360   _file_offset += (long)n;
 361   return true;
 362 }
 363 
 364 
 365 // Read the FileMapInfo information from the file.
 366 bool FileMapInfo::open_for_read() {
 367   _full_path = Arguments::GetSharedArchivePath();
 368   int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
 369   if (fd < 0) {
 370     if (errno == ENOENT) {
 371       // Not locating the shared archive is ok.
 372       fail_continue("Specified shared archive not found.");
 373     } else {
 374       fail_continue("Failed to open shared archive file (%s).",
 375                     os::strerror(errno));


 427   write_bytes(addr, (int)sz); // skip the C++ vtable
 428   write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
 429   align_file_position();
 430 }
 431 
 432 
 433 // Dump region to file.
 434 
 435 void FileMapInfo::write_region(int region, char* base, size_t size,
 436                                bool read_only, bool allow_exec) {
 437   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
 438 
 439   if (_file_open) {
 440     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 441     log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
 442                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
 443                   region, size, p2i(base), _file_offset);
 444   } else {
 445     si->_file_offset = _file_offset;
 446   }
 447   if (MetaspaceShared::is_heap_region(region)) {
 448     assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
 449     if (base != NULL) {
 450       si->_addr._offset = (intx)oopDesc::encode_heap_oop_not_null((oop)base);
 451     } else {
 452       si->_addr._offset = 0;
 453     }
 454   } else {
 455     si->_addr._base = base;
 456   }
 457   si->_used = size;
 458   si->_read_only = read_only;
 459   si->_allow_exec = allow_exec;
 460   si->_crc = ClassLoader::crc32(0, base, (jint)size);
 461   write_bytes_aligned(base, (int)size);
 462 }
 463 
 464 // Write the current archive heap region, which contains one or multiple GC(G1) regions.
 465 // When current archive heap region size is smaller than one GC region of the dump time,
 466 // all archive heap data is written out as one region.
 467 //
 468 // If the current archive heap region size is bigger than one GC region, there would be more
 469 // than one GC regions allocated for archive heap data. The first/bottom GC region might
 470 // be a partial GC region with the empty portion at the higher address within that region.
 471 // The non-empty portion of the first region is written into the archive as one archive
 472 // region. The rest are consecutive full GC regions if they exist, which can be written
 473 // out in one chunk as another archive region.
 474 //
 475 // Here's the mapping from (archive heap regions) -> (GrowableArray<MemRegion> *regions).
 476 //   + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
 477 //   + We have 2 consolidate heap memory regions: r0 and r1
 478 //
 479 // If there's a single archive heap region (ah0), then r0 == ah0, and r1 is empty.
 480 // Otherwise:
 481 //
 482 // "X" represented space that's occupied by heap objects.
 483 // "_" represented unused spaced in the heap region.
 484 //
 485 //
 486 //    |ah0       | ah1 | ah2| ...... | ahn |
 487 //    |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
 488 //    |<-r0->|   |<- r1 ----------------->|
 489 //            ^^^
 490 //             |
 491 //             +-- gap 
 492 void FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *regions,
 493                                              int first_region, int max_num_regions,
 494                                              char** r0_start, char** r0_top,
 495                                              char** r1_start, char** r1_top) {
 496   int arr_len = regions == NULL ? 0 : regions->length();
 497   assert(max_num_regions <= 2, "this loop doesn't work for any other value");
 498   assert(arr_len <= max_num_regions, "number of memory regions exceeds maximum");
 499 
 500   *r0_start = *r0_top = NULL;
 501   *r1_start = *r1_top = NULL;
 502 
 503   for (int i = first_region, arr_idx = 0; i < first_region + max_num_regions; i++, arr_idx++) {
 504     char* start = NULL;
 505     size_t size = 0;
 506     if (arr_idx < arr_len) {
 507       start = (char*)regions->at(arr_idx).start();
 508       size = regions->at(arr_idx).byte_size();
 509     }
 510     if (i == first_region) {
 511       *r0_start = start;
 512       *r0_top = start + size;






 513     } else {
 514       assert(i == first_region + 1, "must be");
 515       *r1_start = start;
 516       *r1_top = start + size;




 517     }
 518 
 519     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",

 520                   i, p2i(start), p2i(start + size), size);
 521     write_region(i, start, size, false, false);
 522   }
 523 }
 524 

 525 // Dump bytes to file -- at the current file position.
 526 
 527 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 528   if (_file_open) {
 529     int n = ::write(_fd, buffer, nbytes);
 530     if (n != nbytes) {
 531       // It is dangerous to leave the corrupted shared archive file around,
 532       // close and remove the file. See bug 6372906.
 533       close();
 534       remove(_full_path);
 535       fail_stop("Unable to write to shared archive file.");
 536     }
 537   }
 538   _file_offset += nbytes;
 539 }
 540 
 541 
 542 // Align file position to an allocation unit boundary.
 543 
 544 void FileMapInfo::align_file_position() {


 616 // Map the whole region at once, assumed to be allocated contiguously.
 617 ReservedSpace FileMapInfo::reserve_shared_memory() {
 618   char* requested_addr = _header->region_addr(0);
 619   size_t size = FileMapInfo::core_spaces_size();
 620 
 621   // Reserve the space first, then map otherwise map will go right over some
 622   // other reserved memory (like the code cache).
 623   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 624   if (!rs.is_reserved()) {
 625     fail_continue("Unable to reserve shared space at required address "
 626                   INTPTR_FORMAT, p2i(requested_addr));
 627     return rs;
 628   }
 629   // the reserved virtual memory is for mapping class data sharing archive
 630   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 631 
 632   return rs;
 633 }
 634 
 635 // Memory map a region in the address space.
 636 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
 637                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
 638 
 639 char* FileMapInfo::map_region(int i) {
 640   assert(!MetaspaceShared::is_heap_region(i), "sanity");
 641   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 642   size_t used = si->_used;
 643   size_t alignment = os::vm_allocation_granularity();
 644   size_t size = align_up(used, alignment);
 645   char *requested_addr = _header->region_addr(i);
 646 
 647   // If a tool agent is in use (debugging enabled), we must map the address space RW
 648   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
 649     si->_read_only = false;
 650   }
 651 
 652   // map the contents of the CDS archive in this memory
 653   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 654                               requested_addr, size, si->_read_only,
 655                               si->_allow_exec);
 656   if (base == NULL || base != requested_addr) {
 657     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 658     return NULL;
 659   }
 660 #ifdef _WINDOWS
 661   // This call is Windows-only because the memory_type gets recorded for the other platforms
 662   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 663   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 664 #endif
 665 
 666   return base;
 667 }
 668 
 669 static MemRegion *string_ranges = NULL;
 670 static MemRegion *open_archive_heap_ranges = NULL;
 671 static int num_string_ranges = 0;
 672 static int num_open_archive_heap_ranges = 0;
 673 
 674 #if INCLUDE_CDS_JAVA_HEAP
 675 //
 676 // Map the string regions and open archive heap regions to the runtime java
 677 // heap.
 678 //
 679 // The archive string regions are mapped near the runtime java heap top. The
 680 // mapped string regions contain no out-going references to any other java heap
 681 // regions. GC does not write into the string regions.
 682 //
 683 // The open archive heap regions are mapped below the string regions in
 684 // the runtime java heap. The mapped open archive heap regions may contain
 685 // out-going references to the string regions or other java heap regions.
 686 // Objects inside the open archive heap regions may be marked by GC and
 687 // the references are updated by GC.
 688 //
 689 void FileMapInfo::map_heap_regions() {
 690   if (MetaspaceShared::allow_archive_heap_object()) {
 691     // Check that all the narrow oop and klass encodings match the archive
 692     if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
 693         narrow_oop_shift() != Universe::narrow_oop_shift() ||
 694         narrow_klass_base() != Universe::narrow_klass_base() ||
 695         narrow_klass_shift() != Universe::narrow_klass_shift()) {
 696       if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
 697         log_info(cds)("Cached heap data from the CDS archive is being ignored. "
 698                       "The current CompressedOops/CompressedClassPointers encoding differs from "
 699                       "that archived due to heap size change. The archive was dumped using max heap "
 700                       "size " UINTX_FORMAT "M.", max_heap_size()/M);
 701       }
 702     } else {
 703       // First, map string regions as closed archive heap regions.
 704       // GC does not write into the regions.
 705       if (map_heap_data(&string_ranges,
 706                          MetaspaceShared::first_string,
 707                          MetaspaceShared::max_strings,
 708                          &num_string_ranges)) {
 709         StringTable::set_shared_string_mapped();
 710 
 711         // Now, map open_archive heap regions, GC can write into the regions.
 712         if (map_heap_data(&open_archive_heap_ranges,
 713                           MetaspaceShared::first_open_archive_heap_region,
 714                           MetaspaceShared::max_open_archive_heap_region,
 715                           &num_open_archive_heap_ranges,
 716                           true /* open */)) {
 717           MetaspaceShared::set_open_archive_heap_region_mapped();
 718         }
 719       }
 720     }
 721   } else {
 722     if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
 723       log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
 724                     "UseCompressedOops and UseCompressedClassPointers are required.");
 725     }
 726   }
 727 
 728   if (!StringTable::shared_string_mapped()) {
 729     assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
 730   }
 731 
 732   if (!MetaspaceShared::open_archive_heap_region_mapped()) {
 733     assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
 734   }
 735 }
 736 
 737 bool FileMapInfo::map_heap_data(MemRegion **mem_regions, int first,
 738                                 int max, int* num, bool is_open_archive) {
 739   MemRegion * regions = new MemRegion[max];
 740   struct FileMapInfo::FileMapHeader::space_info* si;
 741   int region_num = 0;
 742 
 743   for (int i = first;
 744            i < first + max; i++) {
 745     si = &_header->_space[i];
 746     size_t used = si->_used;
 747     if (used > 0) {
 748       size_t size = used;
 749       char* requested_addr = (char*)((void*)oopDesc::decode_heap_oop_not_null(
 750                                             (narrowOop)si->_addr._offset));
 751       regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
 752       region_num ++;
 753     }
 754   }
 755 
 756   if (region_num == 0) {
 757     return false; // no archived java heap data

 758   }
 759 
 760   // Check that ranges are within the java heap
 761   if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
 762     if (log_is_enabled(Info, cds)) {
 763       log_info(cds)("UseSharedSpaces: Unable to allocate region, "
 764                     "range is not within java heap.");
 765     }
 766     return false;
 767   }
 768 
 769   // allocate from java heap
 770   if (!G1CollectedHeap::heap()->alloc_archive_regions(
 771              regions, region_num, is_open_archive)) {
 772     if (log_is_enabled(Info, cds)) {
 773       log_info(cds)("UseSharedSpaces: Unable to allocate region, "
 774                     "java heap range is already in use.");
 775     }
 776     return false;
 777   }
 778 
 779   // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
 780   // for mapped regions as they are part of the reserved java heap, which is
 781   // already recorded.
 782   for (int i = 0; i < region_num; i++) {
 783     si = &_header->_space[first + i];
 784     char* addr = (char*)regions[i].start();
 785     char* base = os::map_memory(_fd, _full_path, si->_file_offset,
 786                                 addr, regions[i].byte_size(), si->_read_only,
 787                                 si->_allow_exec);
 788     if (base == NULL || base != addr) {
 789       // dealloc the regions from java heap
 790       dealloc_archive_heap_regions(regions, region_num);
 791       if (log_is_enabled(Info, cds)) {
 792         log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap.");

 793       }





 794       return false;
 795     }



 796   }
 797 
 798   if (!verify_mapped_heap_regions(first, region_num)) {
 799     // dealloc the regions from java heap
 800     dealloc_archive_heap_regions(regions, region_num);
 801     if (log_is_enabled(Info, cds)) {
 802       log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
 803     }
 804     return false;
 805   }
 806 
 807   // the shared heap data is mapped successfully
 808   *mem_regions = regions;
 809   *num = region_num;

 810   return true;
 811 }
 812 
 813 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
 814   for (int i = first;
 815            i <= first + num; i++) {
 816     if (!verify_region_checksum(i)) {
 817       return false;
 818     }
 819   }
 820   return true;
 821 }
 822 
 823 void FileMapInfo::fixup_mapped_heap_regions() {

 824   // If any string regions were found, call the fill routine to make them parseable.
 825   // Note that string_ranges may be non-NULL even if no ranges were found.
 826   if (num_string_ranges != 0) {
 827     assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
 828     G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_string_ranges);
 829   }
 830 
 831   // do the same for mapped open archive heap regions
 832   if (num_open_archive_heap_ranges != 0) {
 833     assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
 834     G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
 835                                                   num_open_archive_heap_ranges);
 836   }
 837 }
 838 
 839 // dealloc the archive regions from java heap
 840 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num) {
 841   if (num > 0) {
 842     assert(regions != NULL, "Null archive ranges array with non-zero count");
 843     G1CollectedHeap::heap()->dealloc_archive_regions(regions, num);
 844   }

 845 }
 846 #endif // INCLUDE_CDS_JAVA_HEAP
 847 
 848 bool FileMapInfo::verify_region_checksum(int i) {
 849   if (!VerifySharedSpaces) {
 850     return true;
 851   }
 852 
 853   size_t sz = _header->_space[i]._used;
 854 
 855   if (sz == 0) {
 856     return true; // no data
 857   }
 858   if ((MetaspaceShared::is_string_region(i) &&
 859        !StringTable::shared_string_mapped()) ||
 860       (MetaspaceShared::is_open_archive_heap_region(i) &&
 861        !MetaspaceShared::open_archive_heap_region_mapped())) {
 862     return true; // archived heap data is not mapped
 863   }
 864   const char* buf = _header->region_addr(i);
 865   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 866   if (crc != _header->_space[i]._crc) {
 867     fail_continue("Checksum verification failed.");
 868     return false;
 869   }
 870   return true;
 871 }
 872 
 873 // Unmap a memory region in the address space.
 874 
 875 void FileMapInfo::unmap_region(int i) {
 876   assert(!MetaspaceShared::is_heap_region(i), "sanity");
 877   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 878   size_t used = si->_used;
 879   size_t size = align_up(used, os::vm_allocation_granularity());
 880 
 881   if (used == 0) {
 882     return;
 883   }
 884 
 885   char* addr = _header->region_addr(i);
 886   if (!os::unmap_memory(addr, size)) {
 887     fail_stop("Unable to unmap shared space.");
 888   }
 889 }
 890 










 891 void FileMapInfo::assert_mark(bool check) {
 892   if (!check) {
 893     fail_stop("Mark mismatch while restoring from shared file.");
 894   }
 895 }
 896 
 897 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
 898   it->push(&_classpath_entry_table);
 899   for (int i=0; i<_classpath_entry_table_size; i++) {
 900     shared_classpath(i)->metaspace_pointers_do(it);
 901   }
 902 }
 903 
 904 
 905 FileMapInfo* FileMapInfo::_current_info = NULL;
 906 Array<u8>* FileMapInfo::_classpath_entry_table = NULL;
 907 int FileMapInfo::_classpath_entry_table_size = 0;
 908 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
 909 bool FileMapInfo::_validating_classpath_entry_table = false;
 910 


 916 // Validation of the archive is done in two steps:
 917 //
 918 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
 919 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW
 920 //     region of the archive, which is not mapped yet.
 921 bool FileMapInfo::initialize() {
 922   assert(UseSharedSpaces, "UseSharedSpaces expected.");
 923 
 924   if (!open_for_read()) {
 925     return false;
 926   }
 927 
 928   init_from_file(_fd);
 929   if (!validate_header()) {
 930     return false;
 931   }
 932   return true;
 933 }
 934 
 935 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
 936   if (MetaspaceShared::is_heap_region(idx)) {
 937     return _space[idx]._used > 0 ?
 938              (char*)((void*)oopDesc::decode_heap_oop_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
 939   } else {
 940     return _space[idx]._addr._base;
 941   }
 942 }
 943 
 944 int FileMapInfo::FileMapHeader::compute_crc() {
 945   char* header = data();
 946   // start computing from the field after _crc
 947   char* buf = (char*)&_crc + sizeof(int);
 948   size_t sz = data_size() - (buf - header);
 949   int crc = ClassLoader::crc32(0, buf, (jint)sz);
 950   return crc;
 951 }
 952 
 953 bool FileMapInfo::FileMapHeader::validate() {
 954   if (VerifySharedSpaces && compute_crc() != _crc) {
 955     fail_continue("Header checksum verification failed.");
 956     return false;
 957   }
 958 


 998 bool FileMapInfo::validate_header() {
 999   bool status = _header->validate();
1000 
1001   if (status) {
1002     if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) {
1003       if (!PrintSharedArchiveAndExit) {
1004         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
1005         status = false;
1006       }
1007     }
1008   }
1009 
1010   if (_paths_misc_info != NULL) {
1011     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
1012     _paths_misc_info = NULL;
1013   }
1014   return status;
1015 }
1016 
1017 // The following method is provided to see whether a given pointer
1018 // falls in the mapped shared metadata space.
1019 // Param:
1020 // p, The given pointer
1021 // Return:
1022 // True if the p is within the mapped shared space, otherwise, false.
1023 bool FileMapInfo::is_in_shared_space(const void* p) {
1024   for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
1025     char *base;
1026     if (_header->_space[i]._used == 0) {
1027       continue;
1028     }
1029     base = _header->region_addr(i);
1030     if (p >= base && p < base + _header->_space[i]._used) {
1031       return true;
1032     }
1033   }
1034 
1035   return false;
1036 }
1037 
1038 // Check if a given address is within one of the shared regions
1039 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1040   assert(idx == MetaspaceShared::ro ||
1041          idx == MetaspaceShared::rw ||
1042          idx == MetaspaceShared::mc ||
1043          idx == MetaspaceShared::md, "invalid region index");
1044   char* base = _header->region_addr(idx);
1045   if (p >= base && p < base + _header->_space[idx]._used) {
1046     return true;
1047   }
1048   return false;
1049 }
1050 
1051 void FileMapInfo::print_shared_spaces() {
1052   tty->print_cr("Shared Spaces:");
1053   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
1054     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
1055     char *base = _header->region_addr(i);
1056     tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
1057                         shared_region_name[i],
1058                         p2i(base), p2i(base + si->_used));
1059   }
1060 }
1061 
1062 // Unmap mapped regions of shared space.
1063 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1064   FileMapInfo *map_info = FileMapInfo::current_info();
1065   if (map_info) {
1066     map_info->fail_continue("%s", msg);
1067     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
1068       char *addr = map_info->_header->region_addr(i);
1069       if (addr != NULL && !MetaspaceShared::is_heap_region(i)) {
1070         map_info->unmap_region(i);
1071         map_info->_header->_space[i]._addr._base = NULL;
1072       }
1073     }
1074     // Dealloc the archive heap regions only without unmapping. The regions are part
1075     // of the java heap. Unmapping of the heap regions are managed by GC.
1076     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1077                                            num_open_archive_heap_ranges);
1078     map_info->dealloc_archive_heap_regions(string_ranges, num_string_ranges);
1079   } else if (DumpSharedSpaces) {
1080     fail_stop("%s", msg);
1081   }
1082 }
< prev index next >