Skip to content

Commit

Permalink
8292562: (fc) Use copy_file_range in FileChannel::transferTo on Linux
Browse files Browse the repository at this point in the history
Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed Aug 26, 2022
1 parent 3844685 commit c74b6d4
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,11 @@ private long transferToDirectlyInternal(long position, int icount,
ti = threads.add();
if (!isOpen())
return -1;
boolean append = fdAccess.getAppend(targetFD);
do {
long comp = Blocker.begin();
try {
n = transferTo0(fd, position, icount, targetFD);
n = transferTo0(fd, position, icount, targetFD, append);
} finally {
Blocker.end(comp);
}
Expand Down Expand Up @@ -801,7 +802,8 @@ private long transferFromDirectlyInternal(FileDescriptor srcFD,
do {
long comp = Blocker.begin();
try {
n = transferFrom0(srcFD, fd, position, count);
boolean append = fdAccess.getAppend(fd);
n = transferFrom0(srcFD, fd, position, count, append);
} finally {
Blocker.end(comp);
}
Expand Down Expand Up @@ -1573,11 +1575,13 @@ private native long map0(FileDescriptor fd, int prot, long position,
// Transfers from src to dst, or returns IOStatus.UNSUPPORTED (-4) or
// IOStatus.UNSUPPORTED_CASE (-6) if the kernel does not support it
private static native long transferTo0(FileDescriptor src, long position,
long count, FileDescriptor dst);
long count, FileDescriptor dst,
boolean append);

private static native long transferFrom0(FileDescriptor src,
FileDescriptor dst,
long position, long count);
long position, long count,
boolean append);

// Retrieves the maximum size of a transfer
private static native int maxDirectTransferSize0();
Expand Down
39 changes: 35 additions & 4 deletions src/java.base/unix/native/libnio/ch/FileChannelImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,40 @@ JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
jobject srcFDO,
jlong position, jlong count,
jobject dstFDO)
jobject dstFDO, jboolean append)
{
jint srcFD = fdval(env, srcFDO);
jint dstFD = fdval(env, dstFDO);

#if defined(__linux__)
// copy_file_range fails with EBADF when appending, and sendfile
// fails with EINVAL
if (append == JNI_TRUE)
return IOS_UNSUPPORTED_CASE;

off64_t offset = (off64_t)position;
jlong n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
jlong n;
if (my_copy_file_range_func != NULL) {
size_t len = (size_t)count;
n = my_copy_file_range_func(srcFD, &offset, dstFD, NULL, len, 0);
if (n < 0) {
switch (errno) {
case EINTR:
return IOS_INTERRUPTED;
case EINVAL:
case EXDEV:
// ignore and try sendfile()
break;
default:
JNU_ThrowIOExceptionWithLastError(env, "Copy failed");
return IOS_THROWN;
}
}
if (n >= 0)
return n;
}

n = sendfile64(dstFD, srcFD, &offset, (size_t)count);
if (n < 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
Expand Down Expand Up @@ -262,17 +288,22 @@ Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferFrom0(JNIEnv *env, jobject this,
jobject srcFDO, jobject dstFDO,
jlong position, jlong count)
jlong position, jlong count,
jboolean append)
{
#if defined(__linux__)
if (my_copy_file_range_func == NULL)
return IOS_UNSUPPORTED;
// copy_file_range fails with EBADF when appending
if (append == JNI_TRUE)
return IOS_UNSUPPORTED_CASE;

jint srcFD = fdval(env, srcFDO);
jint dstFD = fdval(env, dstFDO);

off64_t offset = (off64_t)position;
jlong n = my_copy_file_range_func(srcFD, NULL, dstFD, &offset, count, 0);
size_t len = (size_t)count;
jlong n = my_copy_file_range_func(srcFD, NULL, dstFD, &offset, len, 0);
if (n < 0) {
if (errno == EAGAIN)
return IOS_UNAVAILABLE;
Expand Down
5 changes: 3 additions & 2 deletions src/java.base/windows/native/libnio/ch/FileChannelImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
jobject srcFD,
jlong position, jlong count,
jobject dstFD)
jobject dstFD, jboolean append)
{
const int PACKET_SIZE = 524288;

Expand Down Expand Up @@ -191,7 +191,8 @@ Java_sun_nio_ch_FileChannelImpl_transferTo0(JNIEnv *env, jobject this,
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_FileChannelImpl_transferFrom0(JNIEnv *env, jobject this,
jobject srcFDO, jobject dstFDO,
jlong position, jlong count)
jlong position, jlong count,
jboolean append)
{
return IOS_UNSUPPORTED;
}
Expand Down
105 changes: 105 additions & 0 deletions test/jdk/java/nio/channels/FileChannel/TransferToAppending.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2022, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/* @test
* @bug 8292562
* @summary Test transferTo and transferFrom when target is appending
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @run main TransferToAppending
* @key randomness
*/

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.channels.FileChannel;
import java.util.Random;
import jdk.test.lib.RandomFactory;

import static java.nio.file.StandardOpenOption.*;

public class TransferToAppending {
private static final int MIN_SIZE = 128;
private static final int MAX_SIZE = 32768;
private static final Random RND = RandomFactory.getRandom();

public static void main(String... args) throws IOException {
// Create files in size range [MIN_SIZE,MAX_SIZE)
// filled with random bytes
Path source = createFile("src");
Path target = createFile("tgt");

try (FileChannel src = FileChannel.open(source, READ, WRITE);
FileChannel tgt = FileChannel.open(target, WRITE, APPEND);) {
// Set source range to a subset of the source
long size = Files.size(source);
long position = RND.nextInt((int)size);
long count = RND.nextInt((int)(size - position));
long tgtSize = Files.size(target);

// Transfer subrange to target
long nbytes = src.transferTo(position, count, tgt);

long expectedSize = tgtSize + nbytes;

if (Files.size(target) != expectedSize) {
String msg = String.format("Bad size: expected %d, actual %d%n",
expectedSize, Files.size(target));
throw new RuntimeException(msg);
}

tgt.close();

// Load subrange of source
ByteBuffer bufSrc = ByteBuffer.allocate((int)nbytes);
src.read(bufSrc, position);

try (FileChannel res = FileChannel.open(target, READ, WRITE)) {
// Load appended range of target
ByteBuffer bufTgt = ByteBuffer.allocate((int)nbytes);
res.read(bufTgt, tgtSize);

// Subranges of values should be equal
if (bufSrc.mismatch(bufTgt) != -1) {
throw new RuntimeException("Range of values unequal");
}
}
} finally {
Files.delete(source);
Files.delete(target);
}
}

private static Path createFile(String name) throws IOException {
Path path = Files.createTempFile(name, ".dat");
try (FileChannel fc = FileChannel.open(path, CREATE, READ, WRITE)) {
int size = Math.max(RND.nextInt(MAX_SIZE), 128);
byte[] b = new byte[size];
RND.nextBytes(b);
fc.write(ByteBuffer.wrap(b));
}
return path;
}
}

1 comment on commit c74b6d4

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.