8294437: java/nio/channels/FileChannel tests slow on Windows

Reviewed-by: alanb, bpb
This commit is contained in:
Daniel Jeliński 2022-10-03 17:42:56 +00:00
parent c6e3daa5fa
commit a4f2078bd6
6 changed files with 348 additions and 273 deletions

View File

@ -0,0 +1,44 @@
/*
* 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.
*/
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;
/**
* Common library for various test file utility functions.
*/
public final class FileChannelUtils {
public static Path createSparseTempFile(String prefix, String suffix) throws IOException {
Path file = Files.createTempFile(prefix, suffix);
Files.delete(file); // need CREATE_NEW to make the file sparse
FileChannel fc = FileChannel.open(file, CREATE_NEW, SPARSE, WRITE);
fc.close();
return file;
}
}

View File

@ -21,14 +21,18 @@
* questions.
*/
import jdk.test.lib.RandomFactory;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.MemorySession;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static java.nio.file.StandardOpenOption.*;
/*
@ -38,42 +42,46 @@ import static java.nio.file.StandardOpenOption.*;
* @summary Ensure that memory mapping beyond 32-bit range does not cause an
* EXCEPTION_ACCESS_VIOLATION.
* @requires vm.bits == 64
* @run main/othervm/timeout=240 LargeMapTest
* @library /test/lib
* @build jdk.test.lib.RandomFactory FileChannelUtils
* @run main/othervm LargeMapTest
* @key randomness
*/
public class LargeMapTest {
private static final String FILE = "test.dat";
private static final long LENGTH = 8000000000L;
private static final long OFFSET = 3704800000L;
private static final int BUFSIZ = 100000;
private static final long LENGTH = (1L << 32) + 512;
private static final int EXTRA = 1024;
private static final long BASE = LENGTH - EXTRA;
private static final Random GEN = RandomFactory.getRandom();
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("sun.arch.data.model"));
System.out.println(System.getProperty("os.arch"));
System.out.println(System.getProperty("java.version"));
Path p = Path.of(FILE);
System.out.println(" Writing large file...");
long t0 = System.nanoTime();
Path p = FileChannelUtils.createSparseTempFile("test", ".dat");
p.toFile().deleteOnExit();
try (FileChannel fc = FileChannel.open(p, CREATE, WRITE)) {
fc.position(LENGTH - 1);
fc.write(ByteBuffer.wrap(new byte[] {27}));
ByteBuffer bb;
try (FileChannel fc = FileChannel.open(p, WRITE)) {
fc.position(BASE);
byte[] b = new byte[EXTRA];
GEN.nextBytes(b);
bb = ByteBuffer.wrap(b);
fc.write(bb);
long t1 = System.nanoTime();
System.out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
}
bb.rewind();
long offset = OFFSET;
ByteBuffer bb = ByteBuffer.allocateDirect(BUFSIZ);
try (FileChannel fc = FileChannel.open(p, READ, WRITE);) {
MemorySegment mbb = MemorySegment.ofBuffer(bb);
try (FileChannel fc = FileChannel.open(p, READ, WRITE)) {
MemorySegment mappedMemorySegment =
fc.map(FileChannel.MapMode.READ_WRITE, 0, p.toFile().length(),
MemorySession.openImplicit());
final int interval = BUFSIZ*1000;
while (offset < LENGTH) {
if (offset % interval == 0)
System.out.println("offset: " + offset);
MemorySegment target = mappedMemorySegment.asSlice(offset, BUFSIZ);
offset = offset + BUFSIZ;
target.copyFrom(mbb);
MemorySegment target = mappedMemorySegment.asSlice(BASE, EXTRA);
if (!target.asByteBuffer().equals(bb)) {
throw new RuntimeException("Expected buffers to be equal");
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -36,6 +36,7 @@ import java.nio.file.Files;
import static java.nio.file.StandardOpenOption.*;
import static java.nio.charset.StandardCharsets.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
@ -56,21 +57,42 @@ public class MapTest {
public static void main(String[] args) throws Exception {
blah = File.createTempFile("blah", null);
blah.deleteOnExit();
long t0 = System.nanoTime();
initTestFile(blah);
long t1 = System.nanoTime();
out.printf("Test file %s initialized in %d ns (%d ms) %n",
blah, t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
try {
out.println("Test file " + blah + " initialized");
testZero();
out.println("Zero size: OK");
t1 = System.nanoTime();
out.printf("Zero size: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testRead();
out.println("Read: OK");
t1 = System.nanoTime();
out.printf("Read: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testWrite();
out.println("Write: OK");
t1 = System.nanoTime();
out.printf("Write: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testHighOffset();
out.println("High offset: OK");
t1 = System.nanoTime();
out.printf("High offset: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testForce();
out.println("Force: OK");
t1 = System.nanoTime();
out.printf("Force: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testExceptions();
out.println("Exceptions: OK");
t1 = System.nanoTime();
out.printf("Exceptions: done in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
} finally {
blah.delete();
}
@ -195,7 +217,6 @@ public class MapTest {
* the data exercising various valid and invalid writeback ranges.
*/
private static void testForce() throws Exception {
for (int x=0; x<50; x++) {
try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {
FileChannel fc = raf.getChannel();
final int BLOCK_SIZE = 64;
@ -235,7 +256,6 @@ public class MapTest {
}
}
}
}
/**
* Test exceptions specified by map method

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
@ -26,51 +26,67 @@
* @bug 8271308
* @summary Verify that transferTo() copies more than Integer.MAX_VALUE bytes
* @library .. /test/lib
* @build jdk.test.lib.Platform
* @build jdk.test.lib.Platform jdk.test.lib.RandomFactory FileChannelUtils
* @run main/othervm/timeout=300 Transfer2GPlus
* @key randomness
*/
import java.io.File;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import jdk.test.lib.Platform;
import jdk.test.lib.RandomFactory;
import static java.nio.file.StandardOpenOption.*;
public class Transfer2GPlus {
private static final long BASE = (long)Integer.MAX_VALUE;
private static final int EXTRA = 1024;
private static final long LENGTH = BASE + EXTRA;
private static final Random GEN = RandomFactory.getRandom();
public static void main(String[] args) throws IOException {
Path src = Files.createTempFile("src", ".dat");
Path src = FileChannelUtils.createSparseTempFile("src", ".dat");
src.toFile().deleteOnExit();
long t0 = System.nanoTime();
byte[] b = createSrcFile(src);
long t1 = System.nanoTime();
System.out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testToFileChannel(src, b);
t1 = System.nanoTime();
System.out.printf(" Copied to file channel in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
t0 = t1;
testToWritableByteChannel(src, b);
t1 = System.nanoTime();
System.out.printf(" Copied to byte channel in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
}
// Create a file of size LENGTH with EXTRA random bytes at offset BASE.
private static byte[] createSrcFile(Path src)
throws IOException {
RandomAccessFile raf = new RandomAccessFile(src.toString(), "rw");
raf.setLength(LENGTH);
raf.seek(BASE);
Random r = new Random(System.nanoTime());
try (FileChannel fc = FileChannel.open(src, WRITE)) {
fc.position(BASE);
byte[] b = new byte[EXTRA];
r.nextBytes(b);
raf.write(b);
GEN.nextBytes(b);
fc.write(ByteBuffer.wrap(b));
return b;
}
}
// Exercises transferToDirectly() on Linux and transferToTrustedChannel()
// on macOS and Windows.
@ -79,8 +95,7 @@ public class Transfer2GPlus {
Path dst = Files.createTempFile("dst", ".dat");
dst.toFile().deleteOnExit();
try (FileChannel srcCh = FileChannel.open(src)) {
try (FileChannel dstCh = FileChannel.open(dst,
StandardOpenOption.READ, StandardOpenOption.WRITE)) {
try (FileChannel dstCh = FileChannel.open(dst, READ, WRITE)) {
long total = 0L;
if ((total = srcCh.transferTo(0, LENGTH, dstCh)) < LENGTH) {
if (!Platform.isLinux())
@ -115,14 +130,12 @@ public class Transfer2GPlus {
// Exercises transferToArbitraryChannel() on all platforms.
private static void testToWritableByteChannel(Path src, byte[] expected)
throws IOException {
File file = File.createTempFile("dst", ".dat");
file.deleteOnExit();
try (FileChannel srcCh = FileChannel.open(src)) {
// The FileOutputStream is wrapped so that newChannel() does not
// return a FileChannelImpl and so make a faster path be taken.
try (DataOutputStream stream =
new DataOutputStream(new FileOutputStream(file))) {
try (WritableByteChannel wbc = Channels.newChannel(stream)) {
// transfer src to channel that is not FileChannelImpl
try (FileChannel srcCh = FileChannel.open(src);
ByteArrayOutputStream baos = new ByteArrayOutputStream(EXTRA);
OutputStream os = new SkipBytesStream(baos, BASE);
WritableByteChannel wbc = Channels.newChannel(os)){
long n;
if ((n = srcCh.transferTo(0, LENGTH, wbc)) < LENGTH)
throw new RuntimeException("Too few bytes transferred: " +
@ -130,14 +143,46 @@ public class Transfer2GPlus {
System.out.println("Transferred " + n + " bytes");
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(BASE);
byte[] b = new byte[EXTRA];
raf.read(b);
byte[] b = baos.toByteArray();
if (!Arrays.equals(b, expected))
throw new RuntimeException("Unexpected values");
}
}
/**
* Stream that discards the first bytesToSkip bytes, then passes through
*/
static class SkipBytesStream extends FilterOutputStream {
private long bytesToSkip;
public SkipBytesStream(OutputStream out, long bytesToSkip) {
super(out);
this.bytesToSkip = bytesToSkip;
}
@Override
public void write(int b) throws IOException {
if (bytesToSkip > 0) {
bytesToSkip--;
} else {
super.write(b);
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
// check copied from FilterOutputStream
if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
throw new IndexOutOfBoundsException();
if (bytesToSkip >= len) {
bytesToSkip -= len;
} else {
int skip = (int)bytesToSkip;
bytesToSkip = 0;
super.write(b, off + skip, len - skip);
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
@ -24,25 +24,24 @@
/* @test
* @bug 4638365
* @summary Test FileChannel.transferFrom and transferTo for 4GB files
* @build FileChannelUtils
* @run testng/timeout=300 Transfer4GBFile
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;
import java.nio.file.FileAlreadyExistsException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import static java.nio.file.StandardOpenOption.*;
public class Transfer4GBFile {
private static PrintStream err = System.err;
@ -51,30 +50,24 @@ public class Transfer4GBFile {
// Test transferTo with large file
@Test
public void xferTest04() throws Exception { // for bug 4638365
File source = File.createTempFile("blah", null);
source.deleteOnExit();
Path source = FileChannelUtils.createSparseTempFile("blah", null);
source.toFile().deleteOnExit();
long testSize = ((long)Integer.MAX_VALUE) * 2;
initTestFile(source, 10);
RandomAccessFile raf = new RandomAccessFile(source, "rw");
FileChannel fc = raf.getChannel();
out.println(" Writing large file...");
long t0 = System.nanoTime();
try (FileChannel fc = FileChannel.open(source, READ, WRITE)) {
fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
long t1 = System.nanoTime();
out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
}
fc.close();
raf.close();
Path sink = Files.createTempFile("sink", null);
sink.toFile().deleteOnExit();
File sink = File.createTempFile("sink", null);
sink.deleteOnExit();
FileInputStream fis = new FileInputStream(source);
FileChannel sourceChannel = fis.getChannel();
raf = new RandomAccessFile(sink, "rw");
FileChannel sinkChannel = raf.getChannel();
try (FileChannel sourceChannel = FileChannel.open(source, READ);
FileChannel sinkChannel = FileChannel.open(sink, WRITE)) {
long bytesWritten = sourceChannel.transferTo(testSize - 40, 10,
sinkChannel);
@ -82,41 +75,25 @@ public class Transfer4GBFile {
throw new RuntimeException("Transfer test 4 failed " +
bytesWritten);
}
sourceChannel.close();
sinkChannel.close();
}
source.delete();
sink.delete();
Files.delete(source);
Files.delete(sink);
}
// Test transferFrom with large file
@Test
public void xferTest05() throws Exception { // for bug 4638365
// Create a source file & large sink file for the test
File source = File.createTempFile("blech", null);
source.deleteOnExit();
Path source = Files.createTempFile("blech", null);
source.toFile().deleteOnExit();
initTestFile(source, 100);
// Create the sink file as a sparse file if possible
File sink = null;
FileChannel fc = null;
while (fc == null) {
sink = File.createTempFile("sink", null);
// re-create as a sparse file
sink.delete();
try {
fc = FileChannel.open(sink.toPath(),
StandardOpenOption.CREATE_NEW,
StandardOpenOption.WRITE,
StandardOpenOption.SPARSE);
} catch (FileAlreadyExistsException ignore) {
// someone else got it
}
}
sink.deleteOnExit();
Path sink = FileChannelUtils.createSparseTempFile("sink", null);
sink.toFile().deleteOnExit();
long testSize = ((long)Integer.MAX_VALUE) * 2;
try {
try (FileChannel fc = FileChannel.open(sink, WRITE)){
out.println(" Writing large file...");
long t0 = System.nanoTime();
fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
@ -128,46 +105,33 @@ public class Transfer4GBFile {
// Can't set up the test, abort it
err.println("xferTest05 was aborted.");
return;
} finally {
fc.close();
}
// Get new channels for the source and sink and attempt transfer
FileChannel sourceChannel = new FileInputStream(source).getChannel();
try {
FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
try {
try (FileChannel sourceChannel = FileChannel.open(source, READ);
FileChannel sinkChannel = FileChannel.open(sink, WRITE)) {
long bytesWritten = sinkChannel.transferFrom(sourceChannel,
testSize - 40, 10);
if (bytesWritten != 10) {
throw new RuntimeException("Transfer test 5 failed " +
bytesWritten);
}
} finally {
sinkChannel.close();
}
} finally {
sourceChannel.close();
}
source.delete();
sink.delete();
Files.delete(source);
Files.delete(sink);
}
/**
* Creates file blah of specified size in bytes.
*/
private static void initTestFile(File blah, long size) throws Exception {
if (blah.exists())
blah.delete();
FileOutputStream fos = new FileOutputStream(blah);
BufferedWriter awriter
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
private static void initTestFile(Path blah, long size) throws Exception {
try (BufferedWriter awriter = Files.newBufferedWriter(blah,
StandardCharsets.ISO_8859_1)) {
for (int i = 0; i < size; i++) {
awriter.write("e");
}
awriter.flush();
awriter.close();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 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
@ -24,23 +24,26 @@
/* @test
* @bug 6253145
* @summary Test FileChannel.transferTo with file positions up to 8GB
* @build FileChannelUtils
* @run testng/timeout=300 TransferTo6GBFile
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import static java.nio.file.StandardOpenOption.*;
public class TransferTo6GBFile {
private static PrintStream err = System.err;
@ -52,27 +55,18 @@ public class TransferTo6GBFile {
final long G = 1024L * 1024L * 1024L;
// Create 6GB file
File file = File.createTempFile("source", null);
file.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
Path file = FileChannelUtils.createSparseTempFile("source", null);
file.toFile().deleteOnExit();
out.println(" Writing large file...");
long t0 = System.nanoTime();
try {
try (FileChannel fc = FileChannel.open(file, READ, WRITE)) {
fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
long t1 = System.nanoTime();
out.printf(" Wrote large file in %d ns (%d ms) %n",
t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
} catch (IOException x) {
err.println(" Unable to create test file:" + x);
fc.close();
return;
}
// Setup looback connection and echo server
// Setup loopback connection and echo server
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
@ -148,10 +142,10 @@ public class TransferTo6GBFile {
} finally {
source.close();
ssc.close();
fc.close();
file.delete();
}
}
Files.delete(file);
}
/**
* Simple in-process server to echo bytes read by a given socket channel