This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: java.io fix and speedup


Hi,

On Sat, 2003-01-04 at 06:43, Jeff Sturm wrote:
> On 4 Jan 2003, Mark Wielaard wrote:
> > If the mmap patch from Jeff is useable in
> > general then that would give us much bigger improvements.
> 
> Below is the patch I used (against 3.2 branch).  It only attempts to
> mmap() for read access.  I don't know whether this is a good idea, versus
> waiting for a possible java.nio implementation.
> 
> (I got the idea from Per Bothner, in a message I can't find now.)

Nice idea, thanks. Attached is the patch against current CVS for people
that are interested in playing with it. In my tests there was a small
speedup but nothing spectacular. The system time dropped, but user time
was up a bit. I think the recent addition of the position field made
most of the difference for me. But maybe for some other workloads this
does make a big difference. If someone has I/O intensive benchmarks this
might be an interesting patch to try out.

Cheers,

Mark
Index: configure
===================================================================
RCS file: /cvs/gcc/gcc/libjava/configure,v
retrieving revision 1.161
diff -u -r1.161 configure
--- configure	14 Jan 2003 13:51:13 -0000	1.161
+++ configure	18 Jan 2003 14:15:31 -0000
@@ -3572,7 +3572,7 @@
 fi
 done
 
-   for ac_func in access stat mkdir rename rmdir unlink realpath utime chmod
+   for ac_func in access stat mkdir rename rmdir unlink realpath utime chmod mmap
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
 echo "configure:3579: checking for $ac_func" >&5
Index: configure.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/configure.in,v
retrieving revision 1.144
diff -u -r1.144 configure.in
--- configure.in	14 Jan 2003 13:51:13 -0000	1.144
+++ configure.in	18 Jan 2003 14:15:32 -0000
@@ -504,7 +504,7 @@
 else
    AC_CHECK_FUNCS(strerror ioctl select fstat open fsync sleep opendir)
    AC_CHECK_FUNCS(gmtime_r localtime_r readdir_r getpwuid_r getcwd)
-   AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath utime chmod)
+   AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath utime chmod mmap)
    AC_CHECK_FUNCS(nl_langinfo setlocale)
    AC_CHECK_FUNCS(inet_aton inet_addr, break)
    AC_CHECK_FUNCS(inet_pton uname inet_ntoa)
Index: include/config.h.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/config.h.in,v
retrieving revision 1.45
diff -u -r1.45 config.h.in
--- include/config.h.in	21 Oct 2002 01:50:13 -0000	1.45
+++ include/config.h.in	18 Jan 2003 14:15:37 -0000
@@ -253,6 +253,9 @@
 /* Define if you have the mkdir function.  */
 #undef HAVE_MKDIR
 
+/* Define if you have the mmap function.  */
+#undef HAVE_MMAP
+
 /* Define if you have the nl_langinfo function.  */
 #undef HAVE_NL_LANGINFO
 
@@ -393,9 +396,6 @@
 
 /* Define if the compiler is configured for setjmp/longjmp exceptions. */
 #undef SJLJ_EXCEPTIONS
-
-/* Indicate that linker is not able to 8-byte align static data */
-#undef JV_LINKER_CANNOT_8BYTE_ALIGN_STATICS
 
 /* Required define if using POSIX threads */
 #undef _REENTRANT
Index: java/io/FileDescriptor.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileDescriptor.java,v
retrieving revision 1.11
diff -u -r1.11 FileDescriptor.java
--- java/io/FileDescriptor.java	4 Jan 2003 03:53:00 -0000	1.11
+++ java/io/FileDescriptor.java	18 Jan 2003 14:15:37 -0000
@@ -10,6 +10,8 @@
 
 package java.io;
 
+import gnu.gcj.RawData;
+
 /**
  * @author Tom Tromey <tromey@cygnus.com>
  * @date September 24, 1998 
@@ -103,4 +105,7 @@
   private int fd = -1;
 
   private long position = 0;
+
+  private gnu.gcj.RawData maddr;
+  private long mlen;
 }
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.24
diff -u -r1.24 natFileDescriptorPosix.cc
--- java/io/natFileDescriptorPosix.cc	4 Jan 2003 03:53:00 -0000	1.24
+++ java/io/natFileDescriptorPosix.cc	18 Jan 2003 14:15:38 -0000
@@ -28,6 +28,10 @@
 #include <sys/filio.h>
 #endif
 
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+#endif
+
 #include <gcj/cni.h>
 #include <jvm.h>
 #include <java/io/FileDescriptor.h>
@@ -124,6 +128,31 @@
 
   _Jv_platform_close_on_exec (fd);
 
+#ifdef HAVE_MMAP
+  maddr = NULL;
+  mlen = 0;
+  position = 0;
+#  ifdef O_BINARY
+  if (flags == O_RDONLY | O_BINARY)
+#  else
+  if (flags == O_RDONLY)
+#  endif
+    {
+      struct stat sb;
+      if (::fstat (fd, &sb) != -1)
+	{
+	  mlen = sb.st_size;
+	  maddr = (gnu::gcj::RawData *) ::mmap (NULL, mlen, PROT_READ,
+						MAP_SHARED, fd, 0);
+	  if (maddr == MAP_FAILED)
+	    {
+	      mlen = 0;
+	      maddr = NULL;
+	    }
+	}
+    }
+#endif
+
   return fd;
 }
 
@@ -187,6 +216,10 @@
 {
   jint save = fd;
   fd = -1;
+#ifdef HAVE_MMAP
+  if (maddr)
+    ::munmap (maddr, mlen);
+#endif
   if (::close (save))
     throw new IOException (JvNewStringLatin1 (strerror (errno)));
 }
@@ -229,11 +262,19 @@
 jint
 java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
 {
+  off_t r;
   JvAssert (whence == SET || whence == CUR);
 
   if (eof_trunc)
     {
-      jlong len = length ();
+      jlong len;
+#ifdef HAVE_MMAP
+      if (maddr)
+	len = mlen;
+      else
+#endif
+        len = length ();
+
       if (whence == SET)
 	{
 	  if (pos > len)
@@ -250,9 +291,23 @@
 	}
     }
 
-  off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
-  if (r == -1)
-    throw new IOException (JvNewStringLatin1 (strerror (errno)));
+#ifdef HAVE_MMAP
+  if (maddr)
+    {
+      if (whence == SET)
+	r = pos;
+      else
+	r = position + pos;
+    }
+  else
+    {
+#endif
+      r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
+      if (r == -1)
+	throw new IOException (JvNewStringLatin1 (strerror (errno)));
+#ifdef HAVE_MMAP
+    }
+#endif
   position = r;
   return r;
 }
@@ -260,10 +315,21 @@
 jlong
 java::io::FileDescriptor::length (void)
 {
-  struct stat sb;
-  if (::fstat (fd, &sb))
-    throw new IOException (JvNewStringLatin1 (strerror (errno)));
-  return sb.st_size;
+#ifdef HAVE_MMAP
+  if (maddr)
+    {
+      return mlen;
+    }
+  else
+    {
+#endif
+      struct stat sb;
+      if (::fstat (fd, &sb))
+      throw new IOException (JvNewStringLatin1 (strerror (errno)));
+      return sb.st_size;
+#ifdef HAVE_MMAP
+    }
+#endif
 }
 
 jlong
@@ -276,7 +342,12 @@
 java::io::FileDescriptor::read (void)
 {
   jbyte b;
-  int r = ::read (fd, &b, 1);
+  int r;
+#ifdef HAVE_MMAP
+  if (maddr)
+    return position < mlen ? ((unsigned char *) maddr)[position++] : -1;
+#endif
+  r = ::read (fd, &b, 1);
   if (r == 0)
     return -1;
   if (r == -1)
@@ -308,6 +379,21 @@
     return 0;
 
   jbyte *bytes = elements (buffer) + offset;
+#ifdef HAVE_MMAP
+  if (maddr)
+    {
+      if (position < mlen)
+	{
+	  if (count > mlen - position)
+	    count = mlen - position;
+	  memcpy (bytes, ((char *) maddr) + position, count);
+	  position += count;
+	}
+      else
+	count = -1;
+      return count;
+    }
+#endif
   int r = ::read (fd, bytes, count);
   if (r == 0)
     return -1;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]