< prev index next >

src/hotspot/os/linux/gc/z/zPhysicalMemoryBacking_linux.cpp

Print this page




 121 
 122   // Create backing file
 123   _fd = create_fd(ZFILENAME_HEAP);
 124   if (_fd == -1) {
 125     return;
 126   }
 127 
 128   // Get filesystem statistics
 129   struct statfs buf;
 130   if (fstatfs(_fd, &buf) == -1) {
 131     ZErrno err;
 132     log_error(gc)("Failed to determine filesystem type for backing file (%s)", err.to_string());
 133     return;
 134   }
 135 
 136   _filesystem = buf.f_type;
 137   _block_size = buf.f_bsize;
 138   _available = buf.f_bavail * _block_size;
 139 
 140   // Make sure we're on a supported filesystem
 141   if (!is_tmpfs() && !is_hugetlbfs()) {
 142     log_error(gc)("Backing file must be located on a %s or a %s filesystem",
 143                   ZFILESYSTEM_TMPFS, ZFILESYSTEM_HUGETLBFS);
 144     return;
 145   }
 146 
 147   // Make sure the filesystem type matches requested large page type
 148   if (ZLargePages::is_transparent() && !is_tmpfs()) {
 149     log_error(gc)("-XX:+UseTransparentHugePages can only be enable when using a %s filesystem",
 150                   ZFILESYSTEM_TMPFS);
 151     return;
 152   }
 153 
 154   if (ZLargePages::is_transparent() && !tmpfs_supports_transparent_huge_pages()) {
 155     log_error(gc)("-XX:+UseTransparentHugePages on a %s filesystem not supported by kernel",
 156                   ZFILESYSTEM_TMPFS);
 157     return;
 158   }
 159 
 160   if (ZLargePages::is_explicit() && !is_hugetlbfs()) {
 161     log_error(gc)("-XX:+UseLargePages (without -XX:+UseTransparentHugePages) can only be enabled "
 162                   "when using a %s filesystem", ZFILESYSTEM_HUGETLBFS);
 163     return;
 164   }
 165 
 166   if (!ZLargePages::is_explicit() && is_hugetlbfs()) {
 167     log_error(gc)("-XX:+UseLargePages must be enabled when using a %s filesystem",
 168                   ZFILESYSTEM_HUGETLBFS);
 169     return;
 170   }
 171 
 172   const size_t expected_block_size = is_tmpfs() ? os::vm_page_size() : os::large_page_size();
 173   if (expected_block_size != _block_size) {
 174     log_error(gc)("%s filesystem has unexpected block size " SIZE_FORMAT " (expected " SIZE_FORMAT ")",
 175                   is_tmpfs() ? ZFILESYSTEM_TMPFS : ZFILESYSTEM_HUGETLBFS, _block_size, expected_block_size);
 176     return;
 177   }
 178 
 179   // Successfully initialized
 180   _initialized = true;
 181 }
 182 
 183 int ZPhysicalMemoryBacking::create_mem_fd(const char* name) const {
 184   // Create file name
 185   char filename[PATH_MAX];
 186   snprintf(filename, sizeof(filename), "%s%s", name, ZLargePages::is_explicit() ? ".hugetlb" : "");
 187 
 188   // Create file
 189   const int extra_flags = ZLargePages::is_explicit() ? MFD_HUGETLB : 0;
 190   const int fd = ZSyscall::memfd_create(filename, MFD_CLOEXEC | extra_flags);
 191   if (fd == -1) {
 192     ZErrno err;
 193     log_debug(gc, init)("Failed to create memfd file (%s)",
 194                         ((ZLargePages::is_explicit() && err == EINVAL) ? "Hugepages not supported" : err.to_string()));
 195     return -1;
 196   }
 197 
 198   log_info(gc, init)("Heap backed by file: /memfd:%s", filename);
 199 
 200   return fd;
 201 }
 202 
 203 int ZPhysicalMemoryBacking::create_file_fd(const char* name) const {





 204   const char* const filesystem = ZLargePages::is_explicit()
 205                                  ? ZFILESYSTEM_HUGETLBFS
 206                                  : ZFILESYSTEM_TMPFS;
 207   const char** const preferred_mountpoints = ZLargePages::is_explicit()
 208                                              ? z_preferred_hugetlbfs_mountpoints
 209                                              : z_preferred_tmpfs_mountpoints;
 210 
 211   // Find mountpoint
 212   ZMountPoint mountpoint(filesystem, preferred_mountpoints);
 213   if (mountpoint.get() == NULL) {
 214     log_error(gc)("Use -XX:AllocateHeapAt to specify the path to a %s filesystem", filesystem);
 215     return -1;
 216   }
 217 
 218   // Try to create an anonymous file using the O_TMPFILE flag. Note that this
 219   // flag requires kernel >= 3.11. If this fails we fall back to open/unlink.
 220   const int fd_anon = os::open(mountpoint.get(), O_TMPFILE|O_EXCL|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR);
 221   if (fd_anon == -1) {
 222     ZErrno err;
 223     log_debug(gc, init)("Failed to create anonymous file in %s (%s)", mountpoint.get(),




 121 
 122   // Create backing file
 123   _fd = create_fd(ZFILENAME_HEAP);
 124   if (_fd == -1) {
 125     return;
 126   }
 127 
 128   // Get filesystem statistics
 129   struct statfs buf;
 130   if (fstatfs(_fd, &buf) == -1) {
 131     ZErrno err;
 132     log_error(gc)("Failed to determine filesystem type for backing file (%s)", err.to_string());
 133     return;
 134   }
 135 
 136   _filesystem = buf.f_type;
 137   _block_size = buf.f_bsize;
 138   _available = buf.f_bavail * _block_size;
 139 
 140   // Make sure we're on a supported filesystem
 141   if (!ZAllowHeapOnFileSystem && !is_tmpfs() && !is_hugetlbfs()) {
 142     log_error(gc)("Backing file must be located on a %s or a %s filesystem",
 143                   ZFILESYSTEM_TMPFS, ZFILESYSTEM_HUGETLBFS);
 144     return;
 145   }
 146 
 147   // Make sure the filesystem type matches requested large page type
 148   if (!ZAllowHeapOnFileSystem && ZLargePages::is_transparent() && !is_tmpfs()) {
 149     log_error(gc)("-XX:+UseTransparentHugePages can only be enable when using a %s filesystem",
 150                   ZFILESYSTEM_TMPFS);
 151     return;
 152   }
 153 
 154   if (ZLargePages::is_transparent() && !tmpfs_supports_transparent_huge_pages()) {
 155     log_error(gc)("-XX:+UseTransparentHugePages on a %s filesystem not supported by kernel",
 156                   ZFILESYSTEM_TMPFS);
 157     return;
 158   }
 159 
 160   if (ZLargePages::is_explicit() && !is_hugetlbfs()) {
 161     log_error(gc)("-XX:+UseLargePages (without -XX:+UseTransparentHugePages) can only be enabled "
 162                   "when using a %s filesystem", ZFILESYSTEM_HUGETLBFS);
 163     return;
 164   }
 165 
 166   if (!ZLargePages::is_explicit() && is_hugetlbfs()) {
 167     log_error(gc)("-XX:+UseLargePages must be enabled when using a %s filesystem",
 168                   ZFILESYSTEM_HUGETLBFS);
 169     return;
 170   }
 171 
 172   const size_t expected_block_size = is_tmpfs() ? os::vm_page_size() : os::large_page_size();
 173   if (!ZAllowHeapOnFileSystem && (expected_block_size != _block_size)) {
 174     log_error(gc)("%s filesystem has unexpected block size " SIZE_FORMAT " (expected " SIZE_FORMAT ")",
 175                   is_tmpfs() ? ZFILESYSTEM_TMPFS : ZFILESYSTEM_HUGETLBFS, _block_size, expected_block_size);
 176     return;
 177   }
 178 
 179   // Successfully initialized
 180   _initialized = true;
 181 }
 182 
 183 int ZPhysicalMemoryBacking::create_mem_fd(const char* name) const {
 184   // Create file name
 185   char filename[PATH_MAX];
 186   snprintf(filename, sizeof(filename), "%s%s", name, ZLargePages::is_explicit() ? ".hugetlb" : "");
 187 
 188   // Create file
 189   const int extra_flags = ZLargePages::is_explicit() ? MFD_HUGETLB : 0;
 190   const int fd = ZSyscall::memfd_create(filename, MFD_CLOEXEC | extra_flags);
 191   if (fd == -1) {
 192     ZErrno err;
 193     log_debug(gc, init)("Failed to create memfd file (%s)",
 194                         ((ZLargePages::is_explicit() && err == EINVAL) ? "Hugepages not supported" : err.to_string()));
 195     return -1;
 196   }
 197 
 198   log_info(gc, init)("Heap backed by file: /memfd:%s", filename);
 199 
 200   return fd;
 201 }
 202 
 203 int ZPhysicalMemoryBacking::create_file_fd(const char* name) const {
 204   if (ZAllowHeapOnFileSystem && (AllocateHeapAt == NULL)) {
 205     log_error(gc)("-XX:AllocateHeapAt is needed when ZAllowHeapOnFileSystem is specified");
 206     return -1;
 207   }
 208 
 209   const char* const filesystem = ZLargePages::is_explicit()
 210                                  ? ZFILESYSTEM_HUGETLBFS
 211                                  : ZFILESYSTEM_TMPFS;
 212   const char** const preferred_mountpoints = ZLargePages::is_explicit()
 213                                              ? z_preferred_hugetlbfs_mountpoints
 214                                              : z_preferred_tmpfs_mountpoints;
 215 
 216   // Find mountpoint
 217   ZMountPoint mountpoint(filesystem, preferred_mountpoints);
 218   if (mountpoint.get() == NULL) {
 219     log_error(gc)("Use -XX:AllocateHeapAt to specify the path to a %s filesystem", filesystem);
 220     return -1;
 221   }
 222 
 223   // Try to create an anonymous file using the O_TMPFILE flag. Note that this
 224   // flag requires kernel >= 3.11. If this fails we fall back to open/unlink.
 225   const int fd_anon = os::open(mountpoint.get(), O_TMPFILE|O_EXCL|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR);
 226   if (fd_anon == -1) {
 227     ZErrno err;
 228     log_debug(gc, init)("Failed to create anonymous file in %s (%s)", mountpoint.get(),


< prev index next >