You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2008/11/20 12:32:53 UTC

svn commit: r719214 - in /harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni: shared/OSNetworkSystem.c unix/OSNetworkSystemLinux.c windows/OSNetworkSystemWin32.c

Author: tellison
Date: Thu Nov 20 03:32:52 2008
New Revision: 719214

URL: http://svn.apache.org/viewvc?rev=719214&view=rev
Log:
Fix for HARMONY-4039 ([classlib][luni] Socket implementation is too slow)
Rewrote the availableStreamImpl to use IOCTL call rather than peek at the bytes in the socket buffer.  Avoids multiple OS calls and copying date unnecessarily, so our performance is now comparable with RI for this function.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSNetworkSystemWin32.c

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c?rev=719214&r1=719213&r2=719214&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c Thu Nov 20 03:32:52 2008
@@ -736,57 +736,6 @@
 
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
- * Method:    availableStreamImpl
- * Signature: (Ljava/io/FileDescriptor;)I
- */
-JNIEXPORT jint JNICALL
-Java_org_apache_harmony_luni_platform_OSNetworkSystem_availableStreamImpl
-  (JNIEnv * env, jclass thisClz, jobject fileDescriptor)
-{
-#define MSGLEN 2048             /* This could be replaced by the default stack buffer size */
-  PORT_ACCESS_FROM_ENV(env);
-  hysocket_t hysocketP;
-  char message[MSGLEN];
-
-  I_32 result, flags = 0;
-
-  hysocketP = getJavaIoFileDescriptorContentsAsAPointer(env, fileDescriptor);
-  if (!hysock_socketIsValid(hysocketP)) {
-    throwJavaNetSocketException(env, HYPORT_ERROR_SOCKET_BADSOCKET);
-    return (jint) 0;
-  }
-
-  do {
-    result = hysock_select_read(hysocketP, 0, 1, FALSE);
-
-    if (HYPORT_ERROR_SOCKET_TIMEOUT == result) {
-      return (jint) 0;          /* The read operation timed out, so answer 0 bytes available */
-    } else if (HYPORT_ERROR_SOCKET_INTERRUPTED == result) {
-      continue;
-    } else if (0 > result) {
-      throwJavaNetSocketException(env, result);
-      return (jint) 0;
-    }
-  } while (HYPORT_ERROR_SOCKET_INTERRUPTED == result);
-
-  result = hysock_setflag(HYSOCK_MSG_PEEK, &flags);     /* Create a 'peek' flag argument for the read operation */
-  if (0 > result) {
-    throwJavaNetSocketException(env, result);
-    return (jint) 0;
-  }
-
-  result = hysock_read(hysocketP, (U_8 *) message, MSGLEN, flags);
-
-  if (0 > result) {
-    throwJavaNetSocketException(env, result);
-    return (jint) 0;
-  } else {
-    return (jint) result;
-  }
-}
-
-/*
- * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    acceptSocketImpl
  * Signature: (Ljava/io/FileDescriptor;Ljava/net/SocketImpl;Ljava/io/FileDescriptor;I)V
  */

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c?rev=719214&r1=719213&r2=719214&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/unix/OSNetworkSystemLinux.c Thu Nov 20 03:32:52 2008
@@ -27,6 +27,8 @@
 #include <poll.h>
 #endif
 
+#include <sys/ioctl.h>
+
 /* We do not get these header files "for free" on zOS, so we will use the
   definitions for these structures defined in OSNetworkSystem.h */
 #if !defined(ZOS)
@@ -475,3 +477,35 @@
 	hymem_free_memory(localAddr);
 	return channel_object;
 }
+
+/*
+ * Utilizes the ioctl call to get the available bytes pending on a socket
+ * which is similar to, but different to the call on other platforms.
+ *
+ * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
+ * Method:    availableStreamImpl
+ * Signature: (Ljava/io/FileDescriptor;)I
+ */
+JNIEXPORT jint JNICALL
+Java_org_apache_harmony_luni_platform_OSNetworkSystem_availableStreamImpl
+  (JNIEnv * env, jclass thisClz, jobject fileDescriptor)
+{
+  PORT_ACCESS_FROM_ENV(env);
+  hysocket_t hysocketP;
+  U_32 nbytes = 0;
+  I_32 result;
+
+  hysocketP = getJavaIoFileDescriptorContentsAsAPointer(env, fileDescriptor);
+  if (!hysock_socketIsValid(hysocketP)) {
+    throwJavaNetSocketException(env, HYPORT_ERROR_SOCKET_BADSOCKET);
+    return (jint) 0;
+  }
+
+  result = ioctl(hysocketP->sock, FIONREAD, &nbytes);
+  if (result != 0) {
+    throwJavaNetSocketException(env, result);
+    return (jint) 0;
+  }
+
+  return (jint) nbytes;
+}

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSNetworkSystemWin32.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSNetworkSystemWin32.c?rev=719214&r1=719213&r2=719214&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSNetworkSystemWin32.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/windows/OSNetworkSystemWin32.c Thu Nov 20 03:32:52 2008
@@ -334,3 +334,42 @@
   //inheritedChannel is not supported on windows platform. 
   return NULL;
 }
+
+/*
+ * Utilizes the Winsock2 call to get the available bytes pending on a socket
+ * which is similar to, but different to the call on other platforms.
+ *
+ * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
+ * Method:    availableStreamImpl
+ * Signature: (Ljava/io/FileDescriptor;)I
+ */
+JNIEXPORT jint JNICALL
+Java_org_apache_harmony_luni_platform_OSNetworkSystem_availableStreamImpl
+  (JNIEnv * env, jclass thisClz, jobject fileDescriptor)
+{
+  PORT_ACCESS_FROM_ENV(env);
+  hysocket_t hysocketP;
+  OSSOCKET socket;
+  U_32 nbytes = 0;
+  I_32 result;
+
+  hysocketP = getJavaIoFileDescriptorContentsAsAPointer(env, fileDescriptor);
+  if (!hysock_socketIsValid(hysocketP)) {
+    throwJavaNetSocketException(env, HYPORT_ERROR_SOCKET_BADSOCKET);
+    return (jint) 0;
+  }
+
+  if ((hysocketP->flags & SOCKET_USE_IPV4_MASK) == SOCKET_USE_IPV4_MASK) {
+      socket = hysocketP->ipv4;
+  } else {
+      socket = hysocketP->ipv6;
+  }
+
+  result = ioctlsocket(socket, FIONREAD, &nbytes);
+  if (result != 0) {
+    throwJavaNetSocketException(env, result);
+    return (jint) 0;
+  }
+
+  return (jint) nbytes;
+}