changeset 5765:0ccb6896143c

8006757: Refactor Socket and File I/O tracing Summary: Allow deferring evaluation/object allocations until we know events will be written, which may reduce the overhead of enabling these events. Reviewed-by: alanb Contributed-by: claes.redestad@oracle.com
author sla
date Wed, 13 Feb 2013 11:04:49 +0100
parents a724ea5862a7
children a049a8fe8723
files src/share/classes/java/net/SocketInputStream.java src/share/classes/java/net/SocketOutputStream.java src/share/classes/sun/misc/IoTrace.java src/share/classes/sun/nio/ch/SocketAdaptor.java src/share/classes/sun/nio/ch/SocketChannelImpl.java test/sun/misc/IoTrace/IoTraceAgent.java test/sun/misc/IoTrace/IoTraceBase.java test/sun/misc/IoTrace/IoTraceListener.java
diffstat 8 files changed, 81 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/net/SocketInputStream.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/src/share/classes/java/net/SocketInputStream.java	Wed Feb 13 11:04:49 2013 +0100
@@ -145,7 +145,7 @@
 
         boolean gotReset = false;
 
-        Object traceContext = IoTrace.socketReadBegin(impl.address, impl.port, timeout);
+        Object traceContext = IoTrace.socketReadBegin();
         // acquire file descriptor and do the read
         FileDescriptor fd = impl.acquireFD();
         try {
@@ -157,7 +157,8 @@
             gotReset = true;
         } finally {
             impl.releaseFD();
-            IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+            IoTrace.socketReadEnd(traceContext, impl.address, impl.port,
+                                  timeout, n > 0 ? n : 0);
         }
 
         /*
@@ -165,7 +166,7 @@
          * buffered on the socket
          */
         if (gotReset) {
-            traceContext = IoTrace.socketReadBegin(impl.address, impl.port, timeout);
+            traceContext = IoTrace.socketReadBegin();
             impl.setConnectionResetPending();
             impl.acquireFD();
             try {
@@ -176,7 +177,8 @@
             } catch (ConnectionResetException rstExc) {
             } finally {
                 impl.releaseFD();
-                IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+                IoTrace.socketReadEnd(traceContext, impl.address, impl.port,
+                                      timeout, n > 0 ? n : 0);
             }
         }
 
--- a/src/share/classes/java/net/SocketOutputStream.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/src/share/classes/java/net/SocketOutputStream.java	Wed Feb 13 11:04:49 2013 +0100
@@ -106,7 +106,7 @@
             throw new ArrayIndexOutOfBoundsException();
         }
 
-        Object traceContext = IoTrace.socketWriteBegin(impl.address, impl.port);
+        Object traceContext = IoTrace.socketWriteBegin();
         int bytesWritten = 0;
         FileDescriptor fd = impl.acquireFD();
         try {
@@ -124,7 +124,7 @@
             }
         } finally {
             impl.releaseFD();
-            IoTrace.socketWriteEnd(traceContext, bytesWritten);
+            IoTrace.socketWriteEnd(traceContext, impl.address, impl.port, bytesWritten);
         }
     }
 
--- a/src/share/classes/sun/misc/IoTrace.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/src/share/classes/sun/misc/IoTrace.java	Wed Feb 13 11:04:49 2013 +0100
@@ -69,17 +69,9 @@
     /**
      * Called before data is read from a socket.
      *
-     * @param address
-     *            the remote address the socket is bound to
-     * @param port
-     *            the remote port the socket is bound to
-     * @param timeout
-     *            the SO_TIMEOUT value of the socket (in milliseconds) or 0 if
-     *            there is no timeout set
      * @return a context object
      */
-    public static Object socketReadBegin(InetAddress address, int port,
-            int timeout) {
+    public static Object socketReadBegin() {
         return null;
     }
 
@@ -88,23 +80,27 @@
      *
      * @param context
      *            the context returned by the previous call to socketReadBegin()
+     * @param address
+     *            the remote address the socket is bound to
+     * @param port
+     *            the remote port the socket is bound to
+     * @param timeout
+     *            the SO_TIMEOUT value of the socket (in milliseconds) or 0 if
+     *            there is no timeout set
      * @param bytesRead
      *            the number of bytes read from the socket, 0 if there was an
      *            error reading from the socket
      */
-    public static void socketReadEnd(Object context, long bytesRead) {
+    public static void socketReadEnd(Object context, InetAddress address, int port,
+                                     int timeout, long bytesRead) {
     }
 
     /**
      * Called before data is written to a socket.
      *
-     * @param address
-     *            the remote address the socket is bound to
-     * @param port
-     *            the remote port the socket is bound to
      * @return a context object
      */
-    public static Object socketWriteBegin(InetAddress address, int port) {
+    public static Object socketWriteBegin() {
         return null;
     }
 
@@ -114,11 +110,16 @@
      * @param context
      *            the context returned by the previous call to
      *            socketWriteBegin()
+     * @param address
+     *            the remote address the socket is bound to
+     * @param port
+     *            the remote port the socket is bound to
      * @param bytesWritten
      *            the number of bytes written to the socket, 0 if there was an
      *            error writing to the socket
      */
-    public static void socketWriteEnd(Object context, long bytesWritten) {
+    public static void socketWriteEnd(Object context, InetAddress address, int port,
+                                      long bytesWritten) {
     }
 
     /**
--- a/src/share/classes/sun/nio/ch/SocketAdaptor.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/src/share/classes/sun/nio/ch/SocketAdaptor.java	Wed Feb 13 11:04:49 2013 +0100
@@ -207,7 +207,7 @@
                 Selector sel = null;
                 sc.configureBlocking(false);
                 int n = 0;
-                Object traceContext = IoTrace.socketReadBegin(getInetAddress(), getPort(), timeout);
+                Object traceContext = IoTrace.socketReadBegin();
                 try {
                     if ((n = sc.read(bb)) != 0)
                         return n;
@@ -229,7 +229,8 @@
                             throw new SocketTimeoutException();
                     }
                 } finally {
-                    IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+                    IoTrace.socketReadEnd(traceContext, getInetAddress(),
+                                          getPort(), timeout, n > 0 ? n : 0);
                     if (sk != null)
                         sk.cancel();
                     if (sc.isOpen())
--- a/src/share/classes/sun/nio/ch/SocketChannelImpl.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/src/share/classes/sun/nio/ch/SocketChannelImpl.java	Wed Feb 13 11:04:49 2013 +0100
@@ -280,8 +280,7 @@
                 return -1;
             Object traceContext = null;
             if (isBlocking()) {
-                traceContext = IoTrace.socketReadBegin(remoteAddress.getAddress(),
-                                                      remoteAddress.getPort(), 0);
+                traceContext = IoTrace.socketReadBegin();
             }
             int n = 0;
             try {
@@ -374,7 +373,8 @@
                 readerCleanup();        // Clear reader thread
 
                 if (isBlocking()) {
-                    IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+                    IoTrace.socketReadEnd(traceContext, remoteAddress.getAddress(),
+                                          remoteAddress.getPort(), 0, n > 0 ? n : 0);
                 }
 
                 // The end method, which is defined in our superclass
@@ -419,8 +419,7 @@
             long n = 0;
             Object traceContext = null;
             if (isBlocking()) {
-                traceContext = IoTrace.socketReadBegin(remoteAddress.getAddress(),
-                                                      remoteAddress.getPort(), 0);
+                traceContext = IoTrace.socketReadBegin();
             }
             try {
                 begin();
@@ -439,7 +438,8 @@
             } finally {
                 readerCleanup();
                 if (isBlocking()) {
-                    IoTrace.socketReadEnd(traceContext, n > 0 ? n : 0);
+                    IoTrace.socketReadEnd(traceContext, remoteAddress.getAddress(),
+                                          remoteAddress.getPort(), 0, n > 0 ? n : 0);
                 }
                 end(n > 0 || (n == IOStatus.UNAVAILABLE));
                 synchronized (stateLock) {
@@ -458,8 +458,7 @@
             ensureWriteOpen();
             int n = 0;
             Object traceContext =
-                IoTrace.socketWriteBegin(remoteAddress.getAddress(),
-                                         remoteAddress.getPort());
+                IoTrace.socketWriteBegin();
 
             try {
                 begin();
@@ -476,7 +475,8 @@
                 }
             } finally {
                 writerCleanup();
-                IoTrace.socketWriteEnd(traceContext, n > 0 ? n : 0);
+                IoTrace.socketWriteEnd(traceContext, remoteAddress.getAddress(),
+                                       remoteAddress.getPort(), n > 0 ? n : 0);
                 end(n > 0 || (n == IOStatus.UNAVAILABLE));
                 synchronized (stateLock) {
                     if ((n <= 0) && (!isOutputOpen))
@@ -496,8 +496,7 @@
             ensureWriteOpen();
             long n = 0;
             Object traceContext =
-                IoTrace.socketWriteBegin(remoteAddress.getAddress(),
-                                         remoteAddress.getPort());
+                IoTrace.socketWriteBegin();
             try {
                 begin();
                 synchronized (stateLock) {
@@ -513,7 +512,8 @@
                 }
             } finally {
                 writerCleanup();
-                IoTrace.socketWriteEnd(traceContext, n > 0 ? n : 0);
+                IoTrace.socketWriteEnd(traceContext, remoteAddress.getAddress(),
+                                       remoteAddress.getPort(), n > 0 ? n : 0);
                 end((n > 0) || (n == IOStatus.UNAVAILABLE));
                 synchronized (stateLock) {
                     if ((n <= 0) && (!isOutputOpen))
--- a/test/sun/misc/IoTrace/IoTraceAgent.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/test/sun/misc/IoTrace/IoTraceAgent.java	Wed Feb 13 11:04:49 2013 +0100
@@ -52,34 +52,35 @@
         listener = l;
     }
 
-    public static Object socketReadBegin(InetAddress address, int port,
-            int timeout) {
+    public static Object socketReadBegin() {
         IoTraceListener l = listener;
         if (l != null) {
-            return l.socketReadBegin(address, port, timeout);
+            return l.socketReadBegin();
         }
         return null;
     }
 
-    public static void socketReadEnd(Object context, long bytesRead) {
+    public static void socketReadEnd(Object context, InetAddress address, int port,
+                                     int timeout, long bytesRead) {
         IoTraceListener l = listener;
         if (l != null) {
-            l.socketReadEnd(context, bytesRead);
+            l.socketReadEnd(context, address, port, timeout, bytesRead);
         }
     }
 
-    public static Object socketWriteBegin(InetAddress address, int port) {
+    public static Object socketWriteBegin() {
         IoTraceListener l = listener;
         if (l != null) {
-            return l.socketWriteBegin(address, port);
+            return l.socketWriteBegin();
         }
         return null;
     }
 
-    public static void socketWriteEnd(Object context, long bytesWritten) {
+    public static void socketWriteEnd(Object context, InetAddress address, int port,
+                                      long bytesWritten) {
         IoTraceListener l = listener;
         if (l != null) {
-            l.socketWriteEnd(context, bytesWritten);
+            l.socketWriteEnd(context, address, port, bytesWritten);
         }
     }
 
@@ -133,7 +134,7 @@
         // 1: iload_1
         // 2: iload_2
         // 3: invokestatic #16 // Method
-        // IoTraceAgent.socketReadBegin:(Ljava/net/InetAddress;II)Ljava/lang/Object;
+        // IoTraceAgent.socketReadBegin:(II)Ljava/lang/Object;
         // 6: areturn
 
         for (Method om : IoTrace.class.getDeclaredMethods()) {
--- a/test/sun/misc/IoTrace/IoTraceBase.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/test/sun/misc/IoTrace/IoTraceBase.java	Wed Feb 13 11:04:49 2013 +0100
@@ -73,30 +73,31 @@
     }
 
     @Override
-    public Object socketReadBegin(InetAddress address, int port,
-            int timeout) {
-        this.address = address;
-        this.port = port;
-        this.timeout = timeout;
+    public Object socketReadBegin() {
         return my_context;
     }
 
     @Override
-    public void socketReadEnd(Object context, long bytesRead) {
+    public void socketReadEnd(Object context, InetAddress address, int port,
+                              int timeout, long bytesRead) {
         this.context = context;
+        this.address = address;
+        this.port = port;
+        this.timeout = timeout;
         this.bytesRead = bytesRead;
     }
 
     @Override
-    public Object socketWriteBegin(InetAddress address, int port) {
-        this.address = address;
-        this.port = port;
+    public Object socketWriteBegin() {
         return my_context;
     }
 
     @Override
-    public void socketWriteEnd(Object context, long bytesWritten) {
+    public void socketWriteEnd(Object context, InetAddress address, int port,
+                               long bytesWritten) {
         this.context = context;
+        this.address = address;
+        this.port = port;
         this.bytesWritten = bytesWritten;
     }
 
@@ -150,4 +151,4 @@
             throw new Exception("Expected " + t + " timeout, got: " + timeout);
         }
     }
-}
\ No newline at end of file
+}
--- a/test/sun/misc/IoTrace/IoTraceListener.java	Tue Feb 12 14:41:18 2013 -0800
+++ b/test/sun/misc/IoTrace/IoTraceListener.java	Wed Feb 13 11:04:49 2013 +0100
@@ -50,6 +50,15 @@
     /**
      * Called before data is read from a socket.
      *
+     * @return a context object
+     */
+    public Object socketReadBegin();
+
+    /**
+     * Called after data is read from the socket.
+     *
+     * @param context
+     *            the context returned by the previous call to socketReadBegin()
      * @param address
      *            the remote address the socket is bound to
      * @param port
@@ -57,31 +66,19 @@
      * @param timeout
      *            the SO_TIMEOUT value of the socket (in milliseconds) or 0 if
      *            there is no timeout set
-     * @return a context object
-     */
-    public Object socketReadBegin(InetAddress address, int port, int timeout);
-
-    /**
-     * Called after data is read from the socket.
-     *
-     * @param context
-     *            the context returned by the previous call to socketReadBegin()
      * @param bytesRead
      *            the number of bytes read from the socket, 0 if there was an
      *            error reading from the socket
      */
-    public void socketReadEnd(Object context, long bytesRead);
+    public void socketReadEnd(Object context, InetAddress address, int port,
+                              int timeout, long bytesRead);
 
     /**
      * Called before data is written to a socket.
      *
-     * @param address
-     *            the remote address the socket is bound to
-     * @param port
-     *            the remote port the socket is bound to
      * @return a context object
      */
-    public Object socketWriteBegin(InetAddress address, int port);
+    public Object socketWriteBegin();
 
     /**
      * Called after data is written to a socket.
@@ -89,11 +86,16 @@
      * @param context
      *            the context returned by the previous call to
      *            socketWriteBegin()
+     * @param address
+     *            the remote address the socket is bound to
+     * @param port
+     *            the remote port the socket is bound to
      * @param bytesWritten
      *            the number of bytes written to the socket, 0 if there was an
      *            error writing to the socket
      */
-    public void socketWriteEnd(Object context, long bytesWritten);
+    public void socketWriteEnd(Object context, InetAddress address, int port,
+                               long bytesWritten);
 
     /**
      * Called before data is read from a file.