aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Brian Burkhalter <bpb@openjdk.org> 2021-04-16 15:46:37 +0000
committerGravatar Brian Burkhalter <bpb@openjdk.org> 2021-04-16 15:46:37 +0000
commit1c3fd46d7da90d5a5b2a457f2a4ed77a12f8f3a5 (patch)
treee2622a243f1fa8e7e4047c438f51fe3d149084c6
parentcee4f1d9c49d148a0c3133b3c10434549cb4d939 (diff)
downloadjdk-1c3fd46d7da90d5a5b2a457f2a4ed77a12f8f3a5.tar.gz
jdk-1c3fd46d7da90d5a5b2a457f2a4ed77a12f8f3a5.zip
8265175: (fs) Files.copy(Path,Path,CopyOption...) should use sendfile on Linux
Reviewed-by: alanb
-rw-r--r--src/java.base/unix/native/libnio/fs/UnixCopyFile.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/java.base/unix/native/libnio/fs/UnixCopyFile.c b/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
index e464b20c929..ea3f2784105 100644
--- a/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
+++ b/src/java.base/unix/native/libnio/fs/UnixCopyFile.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,10 @@
#include <unistd.h>
#include <errno.h>
+#if defined(__linux__)
+#include <sys/sendfile.h>
+#endif
+
#include "sun_nio_fs_UnixCopyFile.h"
#define RESTARTABLE(_cmd, _result) do { \
@@ -47,15 +51,35 @@ static void throwUnixException(JNIEnv* env, int errnum) {
}
/**
- * Transfer all bytes from src to dst via user-space buffers
+ * Transfer all bytes from src to dst within the kernel if possible (Linux),
+ * otherwise via user-space buffers
*/
JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixCopyFile_transfer
(JNIEnv* env, jclass this, jint dst, jint src, jlong cancelAddress)
{
- char buf[8192];
volatile jint* cancel = (jint*)jlong_to_ptr(cancelAddress);
+#if defined(__linux__)
+ // Transfer within the kernel
+ const size_t count = 1048576; // 1 MB to give cancellation a chance
+ ssize_t bytes_sent;
+ do {
+ // sendfile() can transfer at most 0x7ffff000 bytes
+ RESTARTABLE(sendfile64(dst, src, NULL, count), bytes_sent);
+ if (bytes_sent == -1) {
+ throwUnixException(env, errno);
+ return;
+ }
+ if (cancel != NULL && *cancel != 0) {
+ throwUnixException(env, ECANCELED);
+ return;
+ }
+ } while (bytes_sent > 0);
+#else
+ // Transfer via user-space buffers
+ char buf[8192];
+
for (;;) {
ssize_t n, pos, len;
RESTARTABLE(read((int)src, &buf, sizeof(buf)), n);
@@ -82,4 +106,5 @@ Java_sun_nio_fs_UnixCopyFile_transfer
len -= n;
} while (len > 0);
}
+#endif
}