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

Print this page
rev 6099 : [mq]: io-trace

@@ -31,11 +31,12 @@
 import java.nio.ByteBuffer;
 import java.nio.channels.*;
 import java.nio.channels.spi.*;
 import java.util.*;
 import sun.net.NetHooks;
-
+import sun.misc.IoTrace;
+import sun.misc.IoTraceContext;
 
 /**
  * An implementation of SocketChannels
  */
 

@@ -78,12 +79,12 @@
     private static final int ST_KILLPENDING = 3;
     private static final int ST_KILLED = 4;
     private int state = ST_UNINITIALIZED;
 
     // Binding
-    private SocketAddress localAddress;
-    private SocketAddress remoteAddress;
+    private InetSocketAddress localAddress;
+    private InetSocketAddress remoteAddress;
 
     // Input/Output open
     private boolean isInputOpen = true;
     private boolean isOutputOpen = true;
     private boolean readyToConnect = false;

@@ -276,10 +277,15 @@
             throw new NullPointerException();
 
         synchronized (readLock) {
             if (!ensureReadOpen())
                 return -1;
+            IoTraceContext traceContext = null;
+            if (isBlocking()) {
+                traceContext = IoTrace.socketReadBegin(remoteAddress.getAddress(),
+                                                      remoteAddress.getPort(), 0);
+            }
             int n = 0;
             try {
 
                 // Set up the interruption machinery; see
                 // AbstractInterruptibleChannel for details

@@ -365,10 +371,15 @@
                     return IOStatus.normalize(n);
                 }
 
             } finally {
                 readerCleanup();        // Clear reader thread
+
+                if (isBlocking()) {
+                    IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+                }
+
                 // The end method, which is defined in our superclass
                 // AbstractInterruptibleChannel, resets the interruption
                 // machinery.  If its argument is true then it returns
                 // normally; otherwise it checks the interrupt and open state
                 // of this channel and throws an appropriate exception if

@@ -405,10 +416,15 @@
             throw new IndexOutOfBoundsException();
         synchronized (readLock) {
             if (!ensureReadOpen())
                 return -1;
             long n = 0;
+            IoTraceContext traceContext = null;
+            if (isBlocking()) {
+                traceContext = IoTrace.socketReadBegin(remoteAddress.getAddress(),
+                                                      remoteAddress.getPort(), 0);
+            }
             try {
                 begin();
                 synchronized (stateLock) {
                     if (!isOpen())
                         return 0;

@@ -421,10 +437,13 @@
                         continue;
                     return IOStatus.normalize(n);
                 }
             } finally {
                 readerCleanup();
+                if (isBlocking()) {
+                    IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+                }
                 end(n > 0 || (n == IOStatus.UNAVAILABLE));
                 synchronized (stateLock) {
                     if ((n <= 0) && (!isInputOpen))
                         return IOStatus.EOF;
                 }

@@ -437,10 +456,14 @@
         if (buf == null)
             throw new NullPointerException();
         synchronized (writeLock) {
             ensureWriteOpen();
             int n = 0;
+            IoTraceContext traceContext =
+                IoTrace.socketWriteBegin(remoteAddress.getAddress(),
+                                         remoteAddress.getPort());
+
             try {
                 begin();
                 synchronized (stateLock) {
                     if (!isOpen())
                         return 0;

@@ -452,10 +475,11 @@
                         continue;
                     return IOStatus.normalize(n);
                 }
             } finally {
                 writerCleanup();
+                IoTrace.socketWriteEnd(traceContext, n > 0 ? n : 0);
                 end(n > 0 || (n == IOStatus.UNAVAILABLE));
                 synchronized (stateLock) {
                     if ((n <= 0) && (!isOutputOpen))
                         throw new AsynchronousCloseException();
                 }

@@ -470,10 +494,13 @@
         if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
             throw new IndexOutOfBoundsException();
         synchronized (writeLock) {
             ensureWriteOpen();
             long n = 0;
+            IoTraceContext traceContext =
+                IoTrace.socketWriteBegin(remoteAddress.getAddress(),
+                                         remoteAddress.getPort());
             try {
                 begin();
                 synchronized (stateLock) {
                     if (!isOpen())
                         return 0;

@@ -485,10 +512,11 @@
                         continue;
                     return IOStatus.normalize(n);
                 }
             } finally {
                 writerCleanup();
+                IoTrace.socketWriteEnd(traceContext, n > 0 ? n : 0);
                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
                 synchronized (stateLock) {
                     if ((n <= 0) && (!isOutputOpen))
                         throw new AsynchronousCloseException();
                 }