6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include "precompiled.hpp"
25 #include "gc/z/zArray.inline.hpp"
26 #include "gc/z/zBackingPath_linux.hpp"
27 #include "gc/z/zErrno.hpp"
28 #include "gc/z/zGlobals.hpp"
29 #include "gc/z/zLargePages.inline.hpp"
30 #include "gc/z/zPhysicalMemoryBacking_linux.hpp"
31 #include "gc/z/zSyscall_linux.hpp"
32 #include "logging/log.hpp"
33 #include "runtime/init.hpp"
34 #include "runtime/os.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/debug.hpp"
37
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42 #include <sys/statfs.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45
46 //
47 // Support for building on older Linux systems
48 //
49
190 ZErrno err;
191 log_debug(gc, init)("Failed to create memfd file (%s)",
192 ((ZLargePages::is_explicit() && err == EINVAL) ? "Hugepages not supported" : err.to_string()));
193 return -1;
194 }
195
196 log_info(gc, init)("Heap backed by file: /memfd:%s", filename);
197
198 return fd;
199 }
200
201 int ZPhysicalMemoryBacking::create_file_fd(const char* name) const {
202 const char* const filesystem = ZLargePages::is_explicit()
203 ? ZFILESYSTEM_HUGETLBFS
204 : ZFILESYSTEM_TMPFS;
205 const char** const preferred_mountpoints = ZLargePages::is_explicit()
206 ? z_preferred_hugetlbfs_mountpoints
207 : z_preferred_tmpfs_mountpoints;
208
209 // Find mountpoint
210 ZBackingPath path(filesystem, preferred_mountpoints);
211 if (path.get() == NULL) {
212 log_error(gc)("Use -XX:ZPath to specify the path to a %s filesystem", filesystem);
213 return -1;
214 }
215
216 // Try to create an anonymous file using the O_TMPFILE flag. Note that this
217 // flag requires kernel >= 3.11. If this fails we fall back to open/unlink.
218 const int fd_anon = os::open(path.get(), O_TMPFILE|O_EXCL|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR);
219 if (fd_anon == -1) {
220 ZErrno err;
221 log_debug(gc, init)("Failed to create anonymous file in %s (%s)", path.get(),
222 (err == EINVAL ? "Not supported" : err.to_string()));
223 } else {
224 // Get inode number for anonymous file
225 struct stat stat_buf;
226 if (fstat(fd_anon, &stat_buf) == -1) {
227 ZErrno err;
228 log_error(gc)("Failed to determine inode number for anonymous file (%s)", err.to_string());
229 return -1;
230 }
231
232 log_info(gc, init)("Heap backed by file: %s/#" UINT64_FORMAT, path.get(), (uint64_t)stat_buf.st_ino);
233
234 return fd_anon;
235 }
236
237 log_debug(gc, init)("Falling back to open/unlink");
238
239 // Create file name
240 char filename[PATH_MAX];
241 snprintf(filename, sizeof(filename), "%s/%s.%d", path.get(), name, os::current_process_id());
242
243 // Create file
244 const int fd = os::open(filename, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR);
245 if (fd == -1) {
246 ZErrno err;
247 log_error(gc)("Failed to create file %s (%s)", filename, err.to_string());
248 return -1;
249 }
250
251 // Unlink file
252 if (unlink(filename) == -1) {
253 ZErrno err;
254 log_error(gc)("Failed to unlink file %s (%s)", filename, err.to_string());
255 return -1;
256 }
257
258 log_info(gc, init)("Heap backed by file: %s", filename);
259
260 return fd;
261 }
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include "precompiled.hpp"
25 #include "gc/z/zArray.inline.hpp"
26 #include "gc/z/zErrno.hpp"
27 #include "gc/z/zGlobals.hpp"
28 #include "gc/z/zLargePages.inline.hpp"
29 #include "gc/z/zMountPoint_linux.hpp"
30 #include "gc/z/zPhysicalMemoryBacking_linux.hpp"
31 #include "gc/z/zSyscall_linux.hpp"
32 #include "logging/log.hpp"
33 #include "runtime/init.hpp"
34 #include "runtime/os.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/debug.hpp"
37
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42 #include <sys/statfs.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45
46 //
47 // Support for building on older Linux systems
48 //
49
190 ZErrno err;
191 log_debug(gc, init)("Failed to create memfd file (%s)",
192 ((ZLargePages::is_explicit() && err == EINVAL) ? "Hugepages not supported" : err.to_string()));
193 return -1;
194 }
195
196 log_info(gc, init)("Heap backed by file: /memfd:%s", filename);
197
198 return fd;
199 }
200
201 int ZPhysicalMemoryBacking::create_file_fd(const char* name) const {
202 const char* const filesystem = ZLargePages::is_explicit()
203 ? ZFILESYSTEM_HUGETLBFS
204 : ZFILESYSTEM_TMPFS;
205 const char** const preferred_mountpoints = ZLargePages::is_explicit()
206 ? z_preferred_hugetlbfs_mountpoints
207 : z_preferred_tmpfs_mountpoints;
208
209 // Find mountpoint
210 ZMountPoint mountpoint(filesystem, preferred_mountpoints);
211 if (mountpoint.get() == NULL) {
212 log_error(gc)("Use -XX:ZPath to specify the path to a %s filesystem", filesystem);
213 return -1;
214 }
215
216 // Try to create an anonymous file using the O_TMPFILE flag. Note that this
217 // flag requires kernel >= 3.11. If this fails we fall back to open/unlink.
218 const int fd_anon = os::open(mountpoint.get(), O_TMPFILE|O_EXCL|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR);
219 if (fd_anon == -1) {
220 ZErrno err;
221 log_debug(gc, init)("Failed to create anonymous file in %s (%s)", mountpoint.get(),
222 (err == EINVAL ? "Not supported" : err.to_string()));
223 } else {
224 // Get inode number for anonymous file
225 struct stat stat_buf;
226 if (fstat(fd_anon, &stat_buf) == -1) {
227 ZErrno err;
228 log_error(gc)("Failed to determine inode number for anonymous file (%s)", err.to_string());
229 return -1;
230 }
231
232 log_info(gc, init)("Heap backed by file: %s/#" UINT64_FORMAT, mountpoint.get(), (uint64_t)stat_buf.st_ino);
233
234 return fd_anon;
235 }
236
237 log_debug(gc, init)("Falling back to open/unlink");
238
239 // Create file name
240 char filename[PATH_MAX];
241 snprintf(filename, sizeof(filename), "%s/%s.%d", mountpoint.get(), name, os::current_process_id());
242
243 // Create file
244 const int fd = os::open(filename, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR);
245 if (fd == -1) {
246 ZErrno err;
247 log_error(gc)("Failed to create file %s (%s)", filename, err.to_string());
248 return -1;
249 }
250
251 // Unlink file
252 if (unlink(filename) == -1) {
253 ZErrno err;
254 log_error(gc)("Failed to unlink file %s (%s)", filename, err.to_string());
255 return -1;
256 }
257
258 log_info(gc, init)("Heap backed by file: %s", filename);
259
260 return fd;
261 }
|