src/share/vm/memory/filemap.cpp

Print this page




 428   }
 429   si->_base = base;
 430   si->_used = size;
 431   si->_capacity = capacity;
 432   si->_read_only = read_only;
 433   si->_allow_exec = allow_exec;
 434   write_bytes_aligned(base, (int)size);
 435 }
 436 
 437 
 438 // Dump bytes to file -- at the current file position.
 439 
 440 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 441   if (_file_open) {
 442     int n = ::write(_fd, buffer, nbytes);
 443     if (n != nbytes) {
 444       // It is dangerous to leave the corrupted shared archive file around,
 445       // close and remove the file. See bug 6372906.
 446       close();
 447       remove(_full_path);
 448       fail_stop("Unable to write to shared archive file.", NULL);
 449     }
 450   }
 451   _file_offset += nbytes;
 452 }
 453 
 454 
 455 // Align file position to an allocation unit boundary.
 456 
 457 void FileMapInfo::align_file_position() {
 458   long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
 459   if (new_file_offset != _file_offset) {
 460     _file_offset = new_file_offset;
 461     if (_file_open) {
 462       // Seek one byte back from the target and write a byte to insure
 463       // that the written file is the correct length.
 464       _file_offset -= 1;
 465       if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
 466         fail_stop("Unable to seek.", NULL);
 467       }
 468       char zero = 0;
 469       write_bytes(&zero, 1);
 470     }
 471   }
 472 }
 473 
 474 
 475 // Dump bytes to file -- at the current file position.
 476 
 477 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
 478   align_file_position();
 479   write_bytes(buffer, nbytes);
 480   align_file_position();
 481 }
 482 
 483 
 484 // Close the shared archive file.  This does NOT unmap mapped regions.
 485 
 486 void FileMapInfo::close() {


 517   }
 518   if (base != si->_base) {
 519     fail_continue("Unable to remap shared readonly space at required address.");
 520     return false;
 521   }
 522   si->_read_only = false;
 523   return true;
 524 }
 525 
 526 // Map the whole region at once, assumed to be allocated contiguously.
 527 ReservedSpace FileMapInfo::reserve_shared_memory() {
 528   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];
 529   char* requested_addr = si->_base;
 530 
 531   size_t size = FileMapInfo::shared_spaces_size();
 532 
 533   // Reserve the space first, then map otherwise map will go right over some
 534   // other reserved memory (like the code cache).
 535   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 536   if (!rs.is_reserved()) {
 537     fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr));
 538     return rs;
 539   }
 540   // the reserved virtual memory is for mapping class data sharing archive
 541   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 542 
 543   return rs;
 544 }
 545 
 546 // Memory map a region in the address space.
 547 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
 548 
 549 char* FileMapInfo::map_region(int i) {
 550   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 551   size_t used = si->_used;
 552   size_t alignment = os::vm_allocation_granularity();
 553   size_t size = align_size_up(used, alignment);
 554   char *requested_addr = si->_base;
 555 
 556   // map the contents of the CDS archive in this memory
 557   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 558                               requested_addr, size, si->_read_only,
 559                               si->_allow_exec);
 560   if (base == NULL || base != si->_base) {
 561     fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i]));
 562     return NULL;
 563   }
 564 #ifdef _WINDOWS
 565   // This call is Windows-only because the memory_type gets recorded for the other platforms
 566   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 567   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 568 #endif
 569   return base;
 570 }
 571 
 572 
 573 // Unmap a memory region in the address space.
 574 
 575 void FileMapInfo::unmap_region(int i) {
 576   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 577   size_t used = si->_used;
 578   size_t size = align_size_up(used, os::vm_allocation_granularity());
 579   if (!os::unmap_memory(si->_base, size)) {
 580     fail_stop("Unable to unmap shared space.");
 581   }
 582 }
 583 
 584 
 585 void FileMapInfo::assert_mark(bool check) {
 586   if (!check) {
 587     fail_stop("Mark mismatch while restoring from shared file.", NULL);
 588   }
 589 }
 590 
 591 
 592 FileMapInfo* FileMapInfo::_current_info = NULL;
 593 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL;
 594 int FileMapInfo::_classpath_entry_table_size = 0;
 595 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
 596 bool FileMapInfo::_validating_classpath_entry_table = false;
 597 
 598 // Open the shared archive file, read and validate the header
 599 // information (version, boot classpath, etc.).  If initialization
 600 // fails, shared spaces are disabled and the file is closed. [See
 601 // fail_continue.]
 602 //
 603 // Validation of the archive is done in two steps:
 604 //
 605 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
 606 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW
 607 //     region of the archive, which is not mapped yet.


 692     }
 693   }
 694 
 695   return false;
 696 }
 697 
 698 void FileMapInfo::print_shared_spaces() {
 699   gclog_or_tty->print_cr("Shared Spaces:");
 700   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 701     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 702     gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
 703                         shared_region_name[i],
 704                         si->_base, si->_base + si->_used);
 705   }
 706 }
 707 
 708 // Unmap mapped regions of shared space.
 709 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
 710   FileMapInfo *map_info = FileMapInfo::current_info();
 711   if (map_info) {
 712     map_info->fail_continue(msg);
 713     for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 714       if (map_info->_header->_space[i]._base != NULL) {
 715         map_info->unmap_region(i);
 716         map_info->_header->_space[i]._base = NULL;
 717       }
 718     }
 719   } else if (DumpSharedSpaces) {
 720     fail_stop(msg, NULL);
 721   }
 722 }


 428   }
 429   si->_base = base;
 430   si->_used = size;
 431   si->_capacity = capacity;
 432   si->_read_only = read_only;
 433   si->_allow_exec = allow_exec;
 434   write_bytes_aligned(base, (int)size);
 435 }
 436 
 437 
 438 // Dump bytes to file -- at the current file position.
 439 
 440 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 441   if (_file_open) {
 442     int n = ::write(_fd, buffer, nbytes);
 443     if (n != nbytes) {
 444       // It is dangerous to leave the corrupted shared archive file around,
 445       // close and remove the file. See bug 6372906.
 446       close();
 447       remove(_full_path);
 448       fail_stop("Unable to write to shared archive file.");
 449     }
 450   }
 451   _file_offset += nbytes;
 452 }
 453 
 454 
 455 // Align file position to an allocation unit boundary.
 456 
 457 void FileMapInfo::align_file_position() {
 458   long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
 459   if (new_file_offset != _file_offset) {
 460     _file_offset = new_file_offset;
 461     if (_file_open) {
 462       // Seek one byte back from the target and write a byte to insure
 463       // that the written file is the correct length.
 464       _file_offset -= 1;
 465       if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
 466         fail_stop("Unable to seek.");
 467       }
 468       char zero = 0;
 469       write_bytes(&zero, 1);
 470     }
 471   }
 472 }
 473 
 474 
 475 // Dump bytes to file -- at the current file position.
 476 
 477 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
 478   align_file_position();
 479   write_bytes(buffer, nbytes);
 480   align_file_position();
 481 }
 482 
 483 
 484 // Close the shared archive file.  This does NOT unmap mapped regions.
 485 
 486 void FileMapInfo::close() {


 517   }
 518   if (base != si->_base) {
 519     fail_continue("Unable to remap shared readonly space at required address.");
 520     return false;
 521   }
 522   si->_read_only = false;
 523   return true;
 524 }
 525 
 526 // Map the whole region at once, assumed to be allocated contiguously.
 527 ReservedSpace FileMapInfo::reserve_shared_memory() {
 528   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];
 529   char* requested_addr = si->_base;
 530 
 531   size_t size = FileMapInfo::shared_spaces_size();
 532 
 533   // Reserve the space first, then map otherwise map will go right over some
 534   // other reserved memory (like the code cache).
 535   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 536   if (!rs.is_reserved()) {
 537     fail_continue("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr);
 538     return rs;
 539   }
 540   // the reserved virtual memory is for mapping class data sharing archive
 541   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 542 
 543   return rs;
 544 }
 545 
 546 // Memory map a region in the address space.
 547 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
 548 
 549 char* FileMapInfo::map_region(int i) {
 550   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 551   size_t used = si->_used;
 552   size_t alignment = os::vm_allocation_granularity();
 553   size_t size = align_size_up(used, alignment);
 554   char *requested_addr = si->_base;
 555 
 556   // map the contents of the CDS archive in this memory
 557   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 558                               requested_addr, size, si->_read_only,
 559                               si->_allow_exec);
 560   if (base == NULL || base != si->_base) {
 561     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 562     return NULL;
 563   }
 564 #ifdef _WINDOWS
 565   // This call is Windows-only because the memory_type gets recorded for the other platforms
 566   // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
 567   MemTracker::record_virtual_memory_type((address)base, mtClassShared);
 568 #endif
 569   return base;
 570 }
 571 
 572 
 573 // Unmap a memory region in the address space.
 574 
 575 void FileMapInfo::unmap_region(int i) {
 576   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 577   size_t used = si->_used;
 578   size_t size = align_size_up(used, os::vm_allocation_granularity());
 579   if (!os::unmap_memory(si->_base, size)) {
 580     fail_stop("Unable to unmap shared space.");
 581   }
 582 }
 583 
 584 
 585 void FileMapInfo::assert_mark(bool check) {
 586   if (!check) {
 587     fail_stop("Mark mismatch while restoring from shared file.");
 588   }
 589 }
 590 
 591 
 592 FileMapInfo* FileMapInfo::_current_info = NULL;
 593 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL;
 594 int FileMapInfo::_classpath_entry_table_size = 0;
 595 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
 596 bool FileMapInfo::_validating_classpath_entry_table = false;
 597 
 598 // Open the shared archive file, read and validate the header
 599 // information (version, boot classpath, etc.).  If initialization
 600 // fails, shared spaces are disabled and the file is closed. [See
 601 // fail_continue.]
 602 //
 603 // Validation of the archive is done in two steps:
 604 //
 605 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
 606 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW
 607 //     region of the archive, which is not mapped yet.


 692     }
 693   }
 694 
 695   return false;
 696 }
 697 
 698 void FileMapInfo::print_shared_spaces() {
 699   gclog_or_tty->print_cr("Shared Spaces:");
 700   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 701     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 702     gclog_or_tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
 703                         shared_region_name[i],
 704                         si->_base, si->_base + si->_used);
 705   }
 706 }
 707 
 708 // Unmap mapped regions of shared space.
 709 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
 710   FileMapInfo *map_info = FileMapInfo::current_info();
 711   if (map_info) {
 712     map_info->fail_continue("%s", msg);
 713     for (int i = 0; i < MetaspaceShared::n_regions; i++) {
 714       if (map_info->_header->_space[i]._base != NULL) {
 715         map_info->unmap_region(i);
 716         map_info->_header->_space[i]._base = NULL;
 717       }
 718     }
 719   } else if (DumpSharedSpaces) {
 720     fail_stop("%s", msg);
 721   }
 722 }