src/share/classes/sun/nio/ch/FileChannelImpl.java

Print this page
rev 5501 : imported patch io-trace

@@ -32,10 +32,11 @@
 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.security.action.GetPropertyAction;
 
 public class FileChannelImpl
     extends FileChannel
 {

@@ -54,41 +55,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 +137,11 @@
         if (!readable)
             throw new NonReadableChannelException();
         synchronized (positionLock) {
             int n = 0;
             int ti = -1;
+            Object traceContext = IoTrace.fileReadBegin(path);
             try {
                 begin();
                 ti = threads.add();
                 if (!isOpen())
                     return 0;

@@ -143,10 +149,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 +167,11 @@
         if (!readable)
             throw new NonReadableChannelException();
         synchronized (positionLock) {
             long n = 0;
             int ti = -1;
+            Object traceContext = IoTrace.fileReadBegin(path);
             try {
                 begin();
                 ti = threads.add();
                 if (!isOpen())
                     return 0;

@@ -171,10 +179,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 +193,11 @@
         if (!writable)
             throw new NonWritableChannelException();
         synchronized (positionLock) {
             int n = 0;
             int ti = -1;
+            Object traceContext = IoTrace.fileWriteBegin(path);
             try {
                 begin();
                 ti = threads.add();
                 if (!isOpen())
                     return 0;

@@ -196,10 +206,11 @@
                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
                 return IOStatus.normalize(n);
             } finally {
                 threads.remove(ti);
                 end(n > 0);
+                IoTrace.fileWriteEnd(traceContext, n > 0 ? n : 0);
                 assert IOStatus.check(n);
             }
         }
     }
 

@@ -212,10 +223,11 @@
         if (!writable)
             throw new NonWritableChannelException();
         synchronized (positionLock) {
             long n = 0;
             int ti = -1;
+            Object traceContext = IoTrace.fileWriteBegin(path);
             try {
                 begin();
                 ti = threads.add();
                 if (!isOpen())
                     return 0;

@@ -223,10 +235,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);
             }
         }
     }

@@ -663,10 +676,11 @@
         if (!readable)
             throw new NonReadableChannelException();
         ensureOpen();
         int n = 0;
         int ti = -1;
+        Object traceContext = IoTrace.fileReadBegin(path);
         try {
             begin();
             ti = threads.add();
             if (!isOpen())
                 return -1;

@@ -674,10 +688,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);
         }
     }
 

@@ -689,10 +704,11 @@
         if (!writable)
             throw new NonWritableChannelException();
         ensureOpen();
         int n = 0;
         int ti = -1;
+        Object traceContext = IoTrace.fileWriteBegin(path);
         try {
             begin();
             ti = threads.add();
             if (!isOpen())
                 return -1;

@@ -701,10 +717,11 @@
             } while ((n == IOStatus.INTERRUPTED) && isOpen());
             return IOStatus.normalize(n);
         } finally {
             threads.remove(ti);
             end(n > 0);
+            IoTrace.fileWriteEnd(traceContext, n > 0 ? n : 0);
             assert IOStatus.check(n);
         }
     }