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.
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
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
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);
|
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 out the given archive heap memory regions. GC code combines multiple
465 // consequetive archive GC regions into one MemRegion whenever possible and
466 // produces the 'heap_mem' array.
467 //
468 // If the archive heap memory size is smaller than a single dump time GC region
469 // size, there is only one MemRegion in the array.
470 //
471 // If the archive heap memory size is bigger than one dump time GC region size,
472 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
473 // the first/bottom archive GC region is a partial GC region(with the empty
474 // portion at the higher address within the region), one MemRegion is used for
475 // the bottom partial archive GC region. The rest of the consqusective archive
476 // GC regions are combined into another MemRegion.
477 //
478 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
479 // + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
480 // + We have 1 or 2 consolidated heap memory regions: r0 and r1
481 //
482 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
483 // Otherwise:
484 //
485 // "X" represented space that's occupied by heap objects.
486 // "_" represented unused spaced in the heap region.
487 //
488 //
489 // |ah0 | ah1 | ah2| ...... | ahn |
490 // |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
491 // |<-r0->| |<- r1 ----------------->|
492 // ^^^
493 // |
494 // +-- gap
495 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
496 int first_region_id, int max_num_regions) {
497 assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
498
499 int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
500 if(arr_len > max_num_regions) {
501 fail_stop("Unable to write archive heap memory regions: "
502 "number of memory regions exceeds maximum due to fragmentation");
503 }
504
505 size_t total_size = 0;
506 for (int i = first_region_id, arr_idx = 0;
507 i < first_region_id + max_num_regions;
508 i++, arr_idx++) {
509 char* start = NULL;
510 size_t size = 0;
511 if (arr_idx < arr_len) {
512 start = (char*)heap_mem->at(arr_idx).start();
513 size = heap_mem->at(arr_idx).byte_size();
514 total_size += size;
515 }
516
517 log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
518 i, p2i(start), p2i(start + size), size);
519 write_region(i, start, size, false, false);
520 }
521 return total_size;
522 }
523
524 // Dump bytes to file -- at the current file position.
525
526 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
527 if (_file_open) {
528 int n = ::write(_fd, buffer, nbytes);
529 if (n != nbytes) {
530 // It is dangerous to leave the corrupted shared archive file around,
531 // close and remove the file. See bug 6372906.
532 close();
533 remove(_full_path);
534 fail_stop("Unable to write to shared archive file.");
535 }
536 }
537 _file_offset += nbytes;
538 }
539
540
541 // Align file position to an allocation unit boundary.
669 static MemRegion *open_archive_heap_ranges = NULL;
670 static int num_string_ranges = 0;
671 static int num_open_archive_heap_ranges = 0;
672
673 #if INCLUDE_CDS_JAVA_HEAP
674 //
675 // Map the string regions and open archive heap regions to the runtime java
676 // heap.
677 //
678 // The archive string regions are mapped near the runtime java heap top. The
679 // mapped string regions contain no out-going references to any other java heap
680 // regions. GC does not write into the string regions.
681 //
682 // The open archive heap regions are mapped below the string regions in
683 // the runtime java heap. The mapped open archive heap regions may contain
684 // out-going references to the string regions or other java heap regions.
685 // Objects inside the open archive heap regions may be marked by GC and
686 // the references are updated by GC.
687 //
688 void FileMapInfo::map_heap_regions() {
689 if (MetaspaceShared::is_heap_object_archiving_allowed()) {
690 // Check that all the narrow oop and klass encodings match the archive
691 if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
692 narrow_oop_shift() != Universe::narrow_oop_shift() ||
693 narrow_klass_base() != Universe::narrow_klass_base() ||
694 narrow_klass_shift() != Universe::narrow_klass_shift()) {
695 if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
696 log_info(cds)("Cached heap data from the CDS archive is being ignored. "
697 "The current CompressedOops/CompressedClassPointers encoding differs from "
698 "that archived due to heap size change. The archive was dumped using max heap "
699 "size " UINTX_FORMAT "M.", max_heap_size()/M);
700 }
701 } else {
702 // First, map string regions as closed archive heap regions.
703 // GC does not write into the regions.
704 if (map_heap_data(&string_ranges,
705 MetaspaceShared::first_string,
706 MetaspaceShared::max_strings,
707 &num_string_ranges)) {
708 StringTable::set_shared_string_mapped();
709
716 MetaspaceShared::set_open_archive_heap_region_mapped();
717 }
718 }
719 }
720 } else {
721 if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
722 log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
723 "UseCompressedOops and UseCompressedClassPointers are required.");
724 }
725 }
726
727 if (!StringTable::shared_string_mapped()) {
728 assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
729 }
730
731 if (!MetaspaceShared::open_archive_heap_region_mapped()) {
732 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
733 }
734 }
735
736 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
737 int max, int* num, bool is_open_archive) {
738 MemRegion * regions = new MemRegion[max];
739 struct FileMapInfo::FileMapHeader::space_info* si;
740 int region_num = 0;
741
742 for (int i = first;
743 i < first + max; i++) {
744 si = &_header->_space[i];
745 size_t used = si->_used;
746 if (used > 0) {
747 size_t size = used;
748 char* requested_addr = (char*)((void*)oopDesc::decode_heap_oop_not_null(
749 (narrowOop)si->_addr._offset));
750 regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
751 region_num ++;
752 }
753 }
754
755 if (region_num == 0) {
756 return false; // no archived java heap data
787 if (base == NULL || base != addr) {
788 // dealloc the regions from java heap
789 dealloc_archive_heap_regions(regions, region_num);
790 if (log_is_enabled(Info, cds)) {
791 log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap.");
792 }
793 return false;
794 }
795 }
796
797 if (!verify_mapped_heap_regions(first, region_num)) {
798 // dealloc the regions from java heap
799 dealloc_archive_heap_regions(regions, region_num);
800 if (log_is_enabled(Info, cds)) {
801 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
802 }
803 return false;
804 }
805
806 // the shared heap data is mapped successfully
807 *heap_mem = regions;
808 *num = region_num;
809 return true;
810 }
811
812 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
813 for (int i = first;
814 i <= first + num; i++) {
815 if (!verify_region_checksum(i)) {
816 return false;
817 }
818 }
819 return true;
820 }
821
822 void FileMapInfo::fixup_mapped_heap_regions() {
823 // If any string regions were found, call the fill routine to make them parseable.
824 // Note that string_ranges may be non-NULL even if no ranges were found.
825 if (num_string_ranges != 0) {
826 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
827 G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_string_ranges);
|