Skip to content

Commit 2a02f65

Browse files
committed
Backport 8c6d12250b524c0f4ee25dbbc6fe959581b7617b
1 parent 3a21a27 commit 2a02f65

File tree

2 files changed

+84
-40
lines changed

2 files changed

+84
-40
lines changed

test/jdk/java/nio/channels/FileChannel/directio/DirectIOTest.java

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
* @summary Test for ExtendedOpenOption.DIRECT flag
2828
* @requires (os.family == "linux" | os.family == "aix")
2929
* @library /test/lib
30+
* @modules java.base/sun.nio.ch:+open java.base/java.io:+open
3031
* @build jdk.test.lib.Platform
3132
* @run main/native DirectIOTest
3233
*/
3334

3435
import java.io.*;
36+
import java.lang.reflect.Field;
3537
import java.nio.ByteBuffer;
3638
import java.nio.CharBuffer;
3739
import java.nio.channels.*;
@@ -47,10 +49,25 @@
4749
public class DirectIOTest {
4850

4951
private static final int BASE_SIZE = 4096;
52+
private static final int TRIES = 3;
53+
54+
public static int getFD(FileChannel channel) throws Exception {
55+
Field fFdFd = channel.getClass().getDeclaredField("fd");
56+
fFdFd.setAccessible(true);
57+
FileDescriptor fd = (FileDescriptor) fFdFd.get(channel);
58+
59+
Field fFd = FileDescriptor.class.getDeclaredField("fd");
60+
fFd.setAccessible(true);
61+
return fFd.getInt(fd);
62+
}
63+
64+
private static void testWrite(Path p, long blockSize) throws Exception {
65+
try (FileChannel fc = FileChannel.open(p,
66+
StandardOpenOption.READ,
67+
StandardOpenOption.WRITE,
68+
ExtendedOpenOption.DIRECT)) {
69+
int fd = getFD(fc);
5070

51-
private static int testWrite(Path p, long blockSize) throws Exception {
52-
try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE,
53-
ExtendedOpenOption.DIRECT)) {
5471
int bs = (int)blockSize;
5572
int size = Math.max(BASE_SIZE, bs);
5673
int alignment = bs;
@@ -60,22 +77,55 @@ private static int testWrite(Path p, long blockSize) throws Exception {
6077
for (int j = 0; j < size; j++) {
6178
src.put((byte)0);
6279
}
63-
src.flip();
64-
fc.write(src);
65-
return size;
80+
81+
// If there is AV or other FS tracing software, it may cache the file
82+
// contents on first access, even though we have asked for DIRECT here.
83+
// Do several attempts to make test more resilient.
84+
85+
for (int t = 0; t < TRIES; t++) {
86+
flushFileCache(size, fd);
87+
src.flip();
88+
fc.position(0);
89+
fc.write(src);
90+
if (!isFileInCache(size, fd)) {
91+
return;
92+
}
93+
}
94+
95+
throw new RuntimeException("DirectIO is not working properly with " +
96+
"write. File still exists in cache!");
6697
}
6798
}
6899

69-
private static int testRead(Path p, long blockSize) throws Exception {
70-
try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) {
100+
private static void testRead(Path p, long blockSize) throws Exception {
101+
try (FileChannel fc = FileChannel.open(p,
102+
StandardOpenOption.READ,
103+
ExtendedOpenOption.DIRECT)) {
104+
int fd = getFD(fc);
105+
71106
int bs = (int)blockSize;
72107
int size = Math.max(BASE_SIZE, bs);
73108
int alignment = bs;
74109
ByteBuffer dest = ByteBuffer.allocateDirect(size + alignment - 1)
75110
.alignedSlice(alignment);
76111
assert dest.capacity() != 0;
77-
fc.read(dest);
78-
return size;
112+
113+
// If there is AV or other FS tracing software, it may cache the file
114+
// contents on first access, even though we have asked for DIRECT here.
115+
// Do several attempts to make test more resilient.
116+
117+
for (int t = 0; t < TRIES; t++) {
118+
flushFileCache(size, fd);
119+
dest.clear();
120+
fc.position(0);
121+
fc.read(dest);
122+
if (!isFileInCache(size, fd)) {
123+
return;
124+
}
125+
}
126+
127+
throw new RuntimeException("DirectIO is not working properly with " +
128+
"read. File still exists in cache!");
79129
}
80130
}
81131

@@ -84,12 +134,8 @@ public static Path createTempFile() throws IOException {
84134
Paths.get(System.getProperty("test.dir", ".")), "test", null);
85135
}
86136

87-
private static boolean isFileInCache(int size, Path p) {
88-
String path = p.toString();
89-
return isFileInCache0(size, path);
90-
}
91-
92-
private static native boolean isFileInCache0(int size, String path);
137+
private static native boolean flushFileCache(int size, int fd);
138+
private static native boolean isFileInCache(int size, int fd);
93139

94140
public static void main(String[] args) throws Exception {
95141
Path p = createTempFile();
@@ -98,16 +144,8 @@ public static void main(String[] args) throws Exception {
98144
System.loadLibrary("DirectIO");
99145

100146
try {
101-
int size = testWrite(p, blockSize);
102-
if (isFileInCache(size, p)) {
103-
throw new RuntimeException("DirectIO is not working properly with "
104-
+ "write. File still exists in cache!");
105-
}
106-
size = testRead(p, blockSize);
107-
if (isFileInCache(size, p)) {
108-
throw new RuntimeException("DirectIO is not working properly with "
109-
+ "read. File still exists in cache!");
110-
}
147+
testWrite(p, blockSize);
148+
testRead(p, blockSize);
111149
} finally {
112150
Files.delete(p);
113151
}

test/jdk/java/nio/channels/FileChannel/directio/libDirectIO.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,27 @@ static void ThrowException(JNIEnv *env, const char *name, const char *msg) {
4545

4646
/*
4747
* Class: DirectIO
48-
* Method: isFileInCache0
49-
* Signature: (ILjava/lang/String;)Z
48+
* Method: flushFileCache
49+
* Signature: (II;)V
5050
*/
51-
JNIEXPORT jboolean Java_DirectIOTest_isFileInCache0(JNIEnv *env,
51+
JNIEXPORT void Java_DirectIOTest_flushFileCache(JNIEnv *env,
5252
jclass cls,
5353
jint file_size,
54-
jstring file_path) {
54+
jint fd) {
55+
#ifdef __linux__
56+
posix_fadvise(fd, 0, file_size, POSIX_FADV_DONTNEED);
57+
#endif
58+
}
59+
60+
/*
61+
* Class: DirectIO
62+
* Method: isFileInCache
63+
* Signature: (II;)Z
64+
*/
65+
JNIEXPORT jboolean Java_DirectIOTest_isFileInCache(JNIEnv *env,
66+
jclass cls,
67+
jint file_size,
68+
jint fd) {
5569
void *f_mmap;
5670
#ifdef __linux__
5771
unsigned char *f_seg;
@@ -69,17 +83,10 @@ JNIEXPORT jboolean Java_DirectIOTest_isFileInCache0(JNIEnv *env,
6983
size_t index = (file_size + page_size - 1) /page_size;
7084
jboolean result = JNI_FALSE;
7185

72-
const char* path = (*env)->GetStringUTFChars(env, file_path, JNI_FALSE);
73-
74-
int fd = open(path, O_RDWR);
75-
76-
(*env)->ReleaseStringUTFChars(env, file_path, path);
77-
7886
f_mmap = mmap(0, file_size, PROT_NONE, MAP_SHARED, fd, 0);
7987
if (f_mmap == MAP_FAILED) {
80-
close(fd);
8188
ThrowException(env, "java/io/IOException",
82-
"test of whether file exists in cache failed");
89+
"test of whether file exists in cache failed: mmap failed");
8390
}
8491
f_seg = malloc(index);
8592
if (f_seg != NULL) {
@@ -95,9 +102,8 @@ JNIEXPORT jboolean Java_DirectIOTest_isFileInCache0(JNIEnv *env,
95102
free(f_seg);
96103
} else {
97104
ThrowException(env, "java/io/IOException",
98-
"test of whether file exists in cache failed");
105+
"test of whether file exists in cache failed: malloc failed");
99106
}
100-
close(fd);
101107
munmap(f_mmap, file_size);
102108
return result;
103109
}

0 commit comments

Comments
 (0)