--- old/src/os/aix/vm/os_aix.cpp 2016-05-24 12:11:15.824073500 -0700 +++ new/src/os/aix/vm/os_aix.cpp 2016-05-24 12:11:15.619529500 -0700 @@ -4875,3 +4875,8 @@ } return yes; } + +bool os::map_memory_to_file(char* base, size_t size, const char* backingFileDir) { + VMError::report_and_die("Allocating object heap with backing file is not supported for AIX"); + return false; +} \ No newline at end of file --- old/src/os/bsd/vm/os_bsd.cpp 2016-05-24 12:11:17.610927500 -0700 +++ new/src/os/bsd/vm/os_bsd.cpp 2016-05-24 12:11:17.410393900 -0700 @@ -4578,3 +4578,8 @@ } return yes; } + +bool os::map_memory_to_file(char* base, size_t size, const char* backingFileDir) { + VMError::report_and_die("Allocating object heap with backing file is not supported for BSD"); + return false; +} \ No newline at end of file --- old/src/os/linux/vm/os_linux.cpp 2016-05-24 12:11:19.370088500 -0700 +++ new/src/os/linux/vm/os_linux.cpp 2016-05-24 12:11:19.171560800 -0700 @@ -3466,6 +3466,72 @@ } +// Helper function to create a temp file in the given directory +int os::Linux::create_tmpfile(const char* dir, size_t size, bool exec) { + + static char name_template[] = "/jvmheap.XXXXXX"; + + char fullname[strlen(dir) + sizeof(name_template)]; + (void)strcpy(fullname, dir); + (void)strcat(fullname, name_template); + + sigset_t set, oldset; + sigfillset(&set); + + // block all signals while we do the file operation + (void)sigprocmask(SIG_BLOCK, &set, &oldset); + + // set the file creation mask + mode_t new_mask = exec ? (S_IRUSR | S_IWUSR | S_IXUSR) : (S_IRUSR | S_IWUSR); + mode_t prev_umask = umask(new_mask); + + // create a new file + int fd = mkstemp(fullname); + + // reset the file creation mask + umask(prev_umask); + + if (fd < 0) { + warning("Could not create file for heap"); + return -1; + } + + // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted + (void)unlink(fullname); + + // reset the signal mask + (void)sigprocmask(SIG_SETMASK, &oldset, NULL); + + // allocate space for the file + if ((errno = posix_fallocate(fd, 0, (off_t)size)) != 0) { + warning("Could not allocate sufficient disk space for heap"); + return -1; + } + + return fd; +} + +// Map the given address range to a temporary file created at the specified directory. +// The address range must already be reserved for guaranteed success. If it not reserved, their could be an error while mapping leading to JVM shutdown +bool os::map_memory_to_file(char* base, size_t size, const char* backingFileDir) { + + int fd = os::Linux::create_tmpfile(backingFileDir, size, false); + if (fd == -1) { + vm_exit_during_initialization(err_msg("Could not create temporary file in %s for object heap", backingFileDir)); + return false; + } + int prot = PROT_READ | PROT_WRITE; + char* addr = (char*)mmap(base, size, prot, MAP_SHARED | MAP_FIXED, fd, 0); + + if (addr == MAP_FAILED || addr != base) { + close(fd); + vm_exit_during_initialization(err_msg("Error in mapping object heap at the given filesystem dir %s", backingFileDir)); + return false; + } + + return true; +} + // Reserve memory using mmap(MAP_HUGETLB). // - bytes shall be a multiple of alignment. // - req_addr can be NULL. If not NULL, it must be a multiple of alignment. --- old/src/os/linux/vm/os_linux.hpp 2016-05-24 12:11:21.214042000 -0700 +++ new/src/os/linux/vm/os_linux.hpp 2016-05-24 12:11:21.021217700 -0700 @@ -249,6 +249,7 @@ static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; } static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; } static int sched_getcpu_syscall(void); + static int create_tmpfile(const char* dir, size_t size, bool exec); public: static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; } static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) { --- old/src/os/solaris/vm/os_solaris.cpp 2016-05-24 12:11:22.934474800 -0700 +++ new/src/os/solaris/vm/os_solaris.cpp 2016-05-24 12:11:22.734935100 -0700 @@ -5847,3 +5847,8 @@ } return yes; } + +bool os::map_memory_to_file(char* base, size_t size, const char* backingFileDir) { + VMError::report_and_die("Allocating object heap with backing file is not supported for Solaris"); + return false; +} \ No newline at end of file --- old/src/os/windows/vm/os_windows.cpp 2016-05-24 12:11:24.816433500 -0700 +++ new/src/os/windows/vm/os_windows.cpp 2016-05-24 12:11:24.615872700 -0700 @@ -5620,3 +5620,8 @@ os::os_exception_wrapper((java_call_t)call_wrapper_dummy, NULL, NULL, NULL, NULL); } + +bool os::map_memory_to_file(char* base, size_t size, const char* backingFileDir) { + VMError::report_and_die("Allocating object heap with backing file is not supported for Windows"); + return false; +} \ No newline at end of file --- old/src/share/vm/memory/universe.cpp 2016-05-24 12:11:26.676772800 -0700 +++ new/src/share/vm/memory/universe.cpp 2016-05-24 12:11:26.474258700 -0700 @@ -813,7 +813,7 @@ || use_large_pages, "Wrong alignment to use large pages"); // Now create the space. - ReservedHeapSpace total_rs(total_reserved, alignment, use_large_pages); + ReservedHeapSpace total_rs(total_reserved, alignment, use_large_pages, FSDirForHeap); if (total_rs.is_reserved()) { assert((total_reserved == total_rs.size()) && ((uintptr_t)total_rs.base() % alignment == 0), --- old/src/share/vm/memory/virtualspace.cpp 2016-05-24 12:11:28.432711000 -0700 +++ new/src/share/vm/memory/virtualspace.cpp 2016-05-24 12:11:28.239211000 -0700 @@ -120,7 +120,9 @@ // If OS doesn't support demand paging for large page memory, we need // to use reserve_memory_special() to reserve and pin the entire region. - bool special = large && !os::can_commit_large_page_memory(); + // If there is a backing file directory for this VirtualSpace then whether largepages are allocated is upto the filesystem the dir resides in. + // So we ignore the UseLargePages flag in this case. + bool special = (_backingFileDir == NULL) && (large && !os::can_commit_large_page_memory()); char* base = NULL; if (special) { @@ -190,6 +192,13 @@ _base = base; _size = size; _alignment = alignment; + + if (_backingFileDir != NULL) { + // At this point a virtual address range is reserved, now map this memory to a file + os::map_memory_to_file(base, size, _backingFileDir); + // mark this virtual space as _special because the physical memory is committed. + _special = true; + } } @@ -313,7 +322,9 @@ // If OS doesn't support demand paging for large page memory, we need // to use reserve_memory_special() to reserve and pin the entire region. - bool special = large && !os::can_commit_large_page_memory(); + // If there is a backing file directory for this VirtualSpace then whether largepages are allocated is upto the filesystem the dir resides in. + // So we ignore the UseLargePages flag in this case. + bool special = (_backingFileDir == NULL) && (large && !os::can_commit_large_page_memory()); char* base = NULL; log_trace(gc, heap, coops)("Trying to allocate at address " PTR_FORMAT @@ -366,6 +377,13 @@ if ((((size_t)base) & (alignment - 1)) != 0) { // Base not aligned, retry. release(); + return; + } + if (_backingFileDir != NULL) { + // At this point a virtual address range is reserved, now map this memory to a file + os::map_memory_to_file(base, size, _backingFileDir); + // mark this virtual space as _special because the physical memory is committed. + _special = true; } } @@ -556,12 +574,13 @@ } } -ReservedHeapSpace::ReservedHeapSpace(size_t size, size_t alignment, bool large) : ReservedSpace() { +ReservedHeapSpace::ReservedHeapSpace(size_t size, size_t alignment, bool large, const char* backingFSforHeap) : ReservedSpace() { if (size == 0) { return; } + _backingFileDir= backingFSforHeap; // Heap size should be aligned to alignment, too. guarantee(is_size_aligned(size, alignment), "set by caller"); --- old/src/share/vm/memory/virtualspace.hpp 2016-05-24 12:11:30.121068600 -0700 +++ new/src/share/vm/memory/virtualspace.hpp 2016-05-24 12:11:29.923778300 -0700 @@ -37,6 +37,7 @@ size_t _noaccess_prefix; size_t _alignment; bool _special; + const char* _backingFileDir = NULL; private: bool _executable; @@ -111,7 +112,7 @@ void establish_noaccess_prefix(); public: // Constructor. Tries to find a heap that is good for compressed oops. - ReservedHeapSpace(size_t size, size_t forced_base_alignment, bool large); + ReservedHeapSpace(size_t size, size_t forced_base_alignment, bool large, const char* backingFSforHeap=NULL); // Returns the base to be used for compression, i.e. so that null can be // encoded safely and implicit null checks can work. char *compressed_oop_base() { return _base - _noaccess_prefix; } --- old/src/share/vm/runtime/globals.hpp 2016-05-24 12:11:31.836690000 -0700 +++ new/src/share/vm/runtime/globals.hpp 2016-05-24 12:11:31.630112000 -0700 @@ -4129,7 +4129,11 @@ diagnostic(bool, CompilerDirectivesPrint, false, \ "Print compiler directives on installation.") \ diagnostic(int, CompilerDirectivesLimit, 50, \ - "Limit on number of compiler directives.") + "Limit on number of compiler directives.") \ + \ + product(ccstr, FSDirForHeap, NULL, \ + "Path to the directoy where a temporary file will be created \ + to use as the backing store for Java Heap ") /* --- old/src/share/vm/runtime/os.hpp 2016-05-24 12:11:33.666702000 -0700 +++ new/src/share/vm/runtime/os.hpp 2016-05-24 12:11:33.461155300 -0700 @@ -311,6 +311,10 @@ size_t alignment_hint, MEMFLAGS flags); static char* reserve_memory_aligned(size_t size, size_t alignment); static char* attempt_reserve_memory_at(size_t bytes, char* addr); + + // Map the given address range to a temporary file created at the specified directory. + // The address range must already be reserved for guaranteed success. If it not reserved, their could be an error while mapping leading to JVM shutdown + static bool map_memory_to_file(char* base, size_t size, const char* backing_filename); static void split_reserved_memory(char *base, size_t size, size_t split, bool realloc); static bool commit_memory(char* addr, size_t bytes, bool executable);