src/share/classes/sun/nio/ch/FileChannelImpl.java
Print this page
rev 6099 : [mq]: io-trace
@@ -32,10 +32,12 @@
import java.nio.channels.*;
import java.util.ArrayList;
import java.util.List;
import java.security.AccessController;
import sun.misc.Cleaner;
+import sun.misc.IoTrace;
+import sun.misc.IoTraceContext;
import sun.security.action.GetPropertyAction;
public class FileChannelImpl
extends FileChannel
{
@@ -54,41 +56,45 @@
private final boolean append;
// Required to prevent finalization of creating stream (immutable)
private final Object parent;
+ // The path of the referenced file (null if the parent stream is created with a file descriptor)
+ private final String path;
+
// Thread-safe set of IDs of native threads, for signalling
private final NativeThreadSet threads = new NativeThreadSet(2);
// Lock for operations involving position and size
private final Object positionLock = new Object();
- private FileChannelImpl(FileDescriptor fd, boolean readable,
+ private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
boolean writable, boolean append, Object parent)
{
this.fd = fd;
this.readable = readable;
this.writable = writable;
this.append = append;
this.parent = parent;
+ this.path = path;
this.nd = new FileDispatcherImpl(append);
}
// Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
- public static FileChannel open(FileDescriptor fd,
+ public static FileChannel open(FileDescriptor fd, String path,
boolean readable, boolean writable,
Object parent)
{
- return new FileChannelImpl(fd, readable, writable, false, parent);
+ return new FileChannelImpl(fd, path, readable, writable, false, parent);
}
// Used by FileOutputStream.getChannel
- public static FileChannel open(FileDescriptor fd,
+ public static FileChannel open(FileDescriptor fd, String path,
boolean readable, boolean writable,
boolean append, Object parent)
{
- return new FileChannelImpl(fd, readable, writable, append, parent);
+ return new FileChannelImpl(fd, path, readable, writable, append, parent);
}
private void ensureOpen() throws IOException {
if (!isOpen())
throw new ClosedChannelException();
@@ -132,10 +138,11 @@
if (!readable)
throw new NonReadableChannelException();
synchronized (positionLock) {
int n = 0;
int ti = -1;
+ IoTraceContext traceContext = IoTrace.fileReadBegin(path);
try {
begin();
ti = threads.add();
if (!isOpen())
return 0;
@@ -143,10 +150,11 @@
n = IOUtil.read(fd, dst, -1, nd, positionLock);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
+ IoTrace.fileReadEnd(traceContext, n > 0 ? n : 0);
end(n > 0);
assert IOStatus.check(n);
}
}
}
@@ -160,10 +168,11 @@
if (!readable)
throw new NonReadableChannelException();
synchronized (positionLock) {
long n = 0;
int ti = -1;
+ IoTraceContext traceContext = IoTrace.fileReadBegin(path);
try {
begin();
ti = threads.add();
if (!isOpen())
return 0;
@@ -171,10 +180,11 @@
n = IOUtil.read(fd, dsts, offset, length, nd);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
+ IoTrace.fileReadEnd(traceContext, n > 0 ? n : 0);
end(n > 0);
assert IOStatus.check(n);
}
}
}
@@ -184,10 +194,11 @@
if (!writable)
throw new NonWritableChannelException();
synchronized (positionLock) {
int n = 0;
int ti = -1;
+ IoTraceContext traceContext = IoTrace.fileWriteBegin(path);
try {
begin();
ti = threads.add();
if (!isOpen())
return 0;
@@ -196,10 +207,11 @@
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
end(n > 0);
+ IoTrace.fileWriteEnd(traceContext, IOStatus.normalize(n));
assert IOStatus.check(n);
}
}
}
@@ -212,10 +224,11 @@
if (!writable)
throw new NonWritableChannelException();
synchronized (positionLock) {
long n = 0;
int ti = -1;
+ IoTraceContext traceContext = IoTrace.fileWriteBegin(path);
try {
begin();
ti = threads.add();
if (!isOpen())
return 0;
@@ -223,10 +236,11 @@
n = IOUtil.write(fd, srcs, offset, length, nd);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
+ IoTrace.fileWriteEnd(traceContext, n > 0 ? n : 0);
end(n > 0);
assert IOStatus.check(n);
}
}
}
@@ -671,10 +685,11 @@
if (!readable)
throw new NonReadableChannelException();
ensureOpen();
int n = 0;
int ti = -1;
+ IoTraceContext traceContext = IoTrace.fileReadBegin(path);
try {
begin();
ti = threads.add();
if (!isOpen())
return -1;
@@ -682,10 +697,11 @@
n = IOUtil.read(fd, dst, position, nd, positionLock);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
+ IoTrace.fileReadEnd(traceContext, n > 0 ? n : 0);
end(n > 0);
assert IOStatus.check(n);
}
}
@@ -697,10 +713,11 @@
if (!writable)
throw new NonWritableChannelException();
ensureOpen();
int n = 0;
int ti = -1;
+ IoTraceContext traceContext = IoTrace.fileWriteBegin(path);
try {
begin();
ti = threads.add();
if (!isOpen())
return -1;
@@ -709,10 +726,11 @@
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
end(n > 0);
+ IoTrace.fileWriteEnd(traceContext, IOStatus.normalize(n));
assert IOStatus.check(n);
}
}