classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FileChannel fd experiment


From: Mark Wielaard
Subject: [cp-patches] FileChannel fd experiment
Date: Sun, 20 Nov 2005 18:56:23 +0100

Hi list,

Inspired by the recent gcc merge, the study on JNI warnings posted by
Christian and the proposed VM interface for the net sockets I
experimented a bit with the FileChannelImpl and rewrite it to not fetch
the fd from an instance field, but just pass it to the jni method
directly. Patch attached.

This didn't give a real speedup with jamvm. Interesting enough a quick
test with around 1000 small files reading/writing was a little quicker
for the buffered case, but didn't show any change for the unbuffered
case (where I had expected it).

This patch isn't really meant as a real proposal. I will look into the
vm interface for this class hopefully later when I go over the
libgcj/classpath divergence again. But I thought I post it anyway so
people could have a look/do some experiments themselves.

Cheers,

Mark
Index: gnu/java/nio/channels/FileChannelImpl.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/java/nio/channels/FileChannelImpl.java,v
retrieving revision 1.19
diff -u -r1.19 FileChannelImpl.java
--- gnu/java/nio/channels/FileChannelImpl.java  11 Sep 2005 19:29:24 -0000      
1.19
+++ gnu/java/nio/channels/FileChannelImpl.java  20 Nov 2005 17:49:42 -0000
@@ -77,8 +77,6 @@
   public static FileChannelImpl out;
   public static FileChannelImpl err;
 
-  private static native void init();
-
   static
   {
     if (Configuration.INIT_LOAD_LIBRARY)
@@ -86,8 +84,6 @@
         System.loadLibrary("javanio");
       }
     
-    init();
-
     in  = new FileChannelImpl(0, READ);
     out = new FileChannelImpl(1, WRITE);
     err = new FileChannelImpl(2, WRITE);
@@ -159,16 +155,36 @@
 
   private native int open (String path, int mode) throws FileNotFoundException;
 
-  public native int available () throws IOException;
-  private native long implPosition () throws IOException;
-  private native void seek (long newPosition) throws IOException;
-  private native void implTruncate (long size) throws IOException;
+  public int available() throws IOException
+  {
+    return channelAvailable(fd);
+  }
+  private native int channelAvailable(int fd);
+
+  private native long channelPosition(int fd) throws IOException;
+
+  private native void channelSeek(int fd, long newPosition) throws IOException;
+
+  private native void channelTruncate(int fd, long size) throws IOException;
   
-  public native void unlock (long pos, long len) throws IOException;
+  public void unlock(long pos, long len) throws IOException
+  {
+    channelUnlock(fd, pos, len);
+  }
+  private native void channelUnlock(int fd, long pos, long len)
+    throws IOException;
 
-  public native long size () throws IOException;
+  public long size() throws IOException
+  {
+    return channelSize(fd);
+  }
+  private native long channelSize(int fd);
     
-  protected native void implCloseChannel() throws IOException;
+  protected void implCloseChannel() throws IOException
+  {
+    channelClose(fd);
+  }
+  private native void channelClose(int fd) throws IOException;
 
   /**
    * Makes sure the Channel is properly closed.
@@ -197,7 +213,7 @@
   {
     if (position < 0)
       throw new IllegalArgumentException ("position: " + position);
-    long oldPosition = implPosition ();
+    long oldPosition = channelPosition(fd);
     position (position);
     int result = read(dst);
     position (oldPosition);
@@ -205,10 +221,18 @@
     return result;
   }
 
-  public native int read ()
-    throws IOException;
+  public int read () throws IOException
+  {
+    return channelReadByte(fd);
+  }
+  private native int channelReadByte(int fd) throws IOException;
 
-  public native int read (byte[] buffer, int offset, int length)
+  public int read(byte[] buffer, int offset, int length) throws IOException
+  {
+    return channelReadBytes(fd, buffer, offset, length);
+  }
+  private native int channelReadBytes(int fd, byte[] buffer,
+                                     int offset, int length)
     throws IOException;
 
   public long read (ByteBuffer[] dsts, int offset, int length)
@@ -258,18 +282,27 @@
     int result;
     long oldPosition;
 
-    oldPosition = implPosition ();
-    seek (position);
+    oldPosition = channelPosition(fd);
+    channelSeek(fd, position);
     result = write(src);
-    seek (oldPosition);
+    channelSeek(fd, oldPosition);
     
     return result;
   }
 
-  public native void write (byte[] buffer, int offset, int length)
+  public void write (byte[] buffer, int offset, int length) throws IOException
+  {
+    channelWriteBytes(fd, buffer, offset, length);
+  }
+  private native void channelWriteBytes(int fd, byte[] buffer,
+                                       int offset, int length)
     throws IOException;
   
-  public native void write (int b) throws IOException;
+  public void write (int b) throws IOException
+  {
+    channelWriteByte(fd, b);
+  }
+  private native void channelWriteByte(int fd, int b) throws IOException;
 
   public long write(ByteBuffer[] srcs, int offset, int length)
     throws IOException
@@ -284,7 +317,14 @@
     return result;
   }
                                   
-  public native MappedByteBuffer mapImpl (char mode, long position, int size)
+  public MappedByteBuffer mapImpl(char mode, long position, int size)
+    throws IOException
+  {
+    return channelMap(fd, mode, position, size);
+  }
+
+  private native MappedByteBuffer channelMap(int fd, char mode,
+                                            long position, int size)
     throws IOException;
 
   public MappedByteBuffer map (FileChannel.MapMode mode,
@@ -321,10 +361,9 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
 
-    force ();
+    channelForce(fd, metaData);
   }
-
-  private native void force ();
+  private native void channelForce(int fd, boolean metaData);
 
   // like transferTo, but with a count of less than 2Gbytes
   private int smallTransferTo (long position, int count, 
@@ -472,7 +511,7 @@
     try
       {
        begin();
-       boolean lockable = lock(position, size, shared, false);
+       boolean lockable = channelLock(fd, position, size, shared, false);
        completed = true;
        return (lockable
                ? new FileLockImpl(this, position, size, shared)
@@ -489,8 +528,9 @@
    * If wait as specified, block until we can get it.
    * Otherwise return false.
    */
-  private native boolean lock(long position, long size,
-                             boolean shared, boolean wait) throws IOException;
+  private native boolean channelLock(int fd, long position, long size,
+                                    boolean shared, boolean wait)
+    throws IOException;
   
   public FileLock lock (long position, long size, boolean shared)
     throws IOException
@@ -500,7 +540,7 @@
     boolean completed = false;
     try
       {
-       boolean lockable = lock(position, size, shared, true);
+       boolean lockable = channelLock(fd, position, size, shared, true);
        completed = true;
        return (lockable
                ? new FileLockImpl(this, position, size, shared)
@@ -518,7 +558,7 @@
     if (!isOpen ())
       throw new ClosedChannelException ();
 
-    return implPosition ();
+    return channelPosition(fd);
   }
   
   public FileChannel position (long newPosition)
@@ -532,7 +572,7 @@
 
     // FIXME note semantics if seeking beyond eof.
     // We should seek lazily - only on a write.
-    seek (newPosition);
+    channelSeek(fd, newPosition);
     return this;
   }
   
@@ -549,7 +589,7 @@
        throw new NonWritableChannelException ();
 
     if (size < size ())
-      implTruncate (size);
+      channelTruncate(fd, size);
 
     return this;
   }
Index: native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c,v
retrieving revision 1.27
diff -u -r1.27 gnu_java_nio_channels_FileChannelImpl.c
--- native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c 3 Aug 2005 
13:12:59 -0000       1.27
+++ native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c 20 Nov 2005 
17:49:42 -0000
@@ -99,45 +99,6 @@
 #define ALIGN_DOWN(p,s) ((p) - ((p) % (s)))
 #define ALIGN_UP(p,s) ((p) + ((s) - ((p) % (s))))
 
-/* cached fieldID of gnu.java.nio.channels.FileChannelImpl.fd */
-static jfieldID native_fd_fieldID;
-
-static jint
-get_native_fd (JNIEnv * env, jobject obj)
-{
-  return (*env)->GetIntField (env, obj, native_fd_fieldID);
-}
-
-/*
- * Library initialization routine.  Called as part of java.io.FileDescriptor
- * static initialization.
- */
-JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_init (JNIEnv * env,
-                                               jclass clazz
-                                               __attribute__ ((__unused__)))
-{
-  jclass clazz_fc;
-  jfieldID field;
-
-  /* Initialize native_fd_fieldID so we only compute it once! */
-  clazz_fc = (*env)->FindClass (env, "gnu/java/nio/channels/FileChannelImpl");
-  if (!clazz_fc)
-    {
-      JCL_ThrowException (env, IO_EXCEPTION, "Internal error");
-      return;
-    }
-
-  field = (*env)->GetFieldID (env, clazz_fc, "fd", "I");
-  if (!field)
-    {
-      JCL_ThrowException (env, IO_EXCEPTION, "Internal error");
-      return;
-    }
-
-  native_fd_fieldID = field;
-}
-
 /*
  * Open the specified file and return a native file descriptor
  */
@@ -228,14 +189,13 @@
  * Exception on error
  */
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_implCloseChannel (JNIEnv * env,
-                                                            jobject obj)
+Java_gnu_java_nio_channels_FileChannelImpl_channelClose (JNIEnv * env,
+                                                        jobject obj
+                                                        __attribute__
+                                                        ((__unused__)),
+                                                        int native_fd)
 {
-  int native_fd;
   int result;
-
-  native_fd = get_native_fd (env, obj);
-
   do
     {
       TARGET_NATIVE_FILE_CLOSE (native_fd, result);
@@ -256,15 +216,14 @@
  * Exception on error
  */
 JNIEXPORT jint JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_available (JNIEnv * env,
-                                                     jobject obj)
+Java_gnu_java_nio_channels_FileChannelImpl_channelAvailable (JNIEnv * env,
+                                                            jobject obj
+                                                            __attribute__
+                                                            ((__unused__)),
+                                                            int native_fd)
 {
-  int native_fd;
   jlong bytes_available;
   int result;
-
-  native_fd = get_native_fd (env, obj);
-
   do
     {
       TARGET_NATIVE_FILE_AVAILABLE (native_fd, bytes_available, result);
@@ -284,14 +243,15 @@
 }
 
 JNIEXPORT jlong JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_size (JNIEnv * env, jobject obj)
+Java_gnu_java_nio_channels_FileChannelImpl_channelSize (JNIEnv * env,
+                                                       jobject obj
+                                                        __attribute__
+                                                        ((__unused__)),
+                                                       int native_fd)
 {
-  int native_fd;
   jlong file_size;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
   TARGET_NATIVE_FILE_SIZE (native_fd, file_size, result);
   if (result != TARGET_NATIVE_OK)
     {
@@ -308,15 +268,15 @@
  * Exception on error
  */
 JNIEXPORT jlong JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_implPosition (JNIEnv * env,
-                                                        jobject obj)
+Java_gnu_java_nio_channels_FileChannelImpl_channelPosition (JNIEnv * env,
+                                                           jobject obj
+                                                           __attribute__
+                                                           ((__unused__)),
+                                                           int native_fd)
 {
-  int native_fd;
   jlong current_offset;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
   TARGET_NATIVE_FILE_TELL (native_fd, current_offset, result);
   if (result != TARGET_NATIVE_OK)
     {
@@ -333,15 +293,16 @@
  * Exception on error
  */
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_seek (JNIEnv * env, jobject obj,
-                                                jlong offset)
+Java_gnu_java_nio_channels_FileChannelImpl_channelSeek (JNIEnv * env,
+                                                       jobject obj
+                                                        __attribute__
+                                                        ((__unused__)),
+                                                       int native_fd,
+                                                       jlong offset)
 {
-  int native_fd;
   jlong new_offset;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
 #if 0
   /* Should there be such an exception? All native layer macros should
      be accepting 64bit-values if needed. It some target is not able
@@ -378,19 +339,19 @@
  * Exception on error
  */
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_implTruncate (JNIEnv * env,
-                                                        jobject obj,
-                                                        jlong len)
+Java_gnu_java_nio_channels_FileChannelImpl_channelTruncate (JNIEnv * env,
+                                                           jobject obj
+                                                           __attribute__
+                                                           ((__unused__)),
+                                                           int native_fd,
+                                                           jlong len)
 {
-  int native_fd;
   jlong file_size;
   int bytes_written;
   jlong save_offset, new_offset;
   char data;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
 #if 0
   /* Should there be such an exception? All native layer macros should
      be accepting 64bit-values if needed. It some target is not able
@@ -507,8 +468,14 @@
 }
 
 JNIEXPORT jobject JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_mapImpl (JNIEnv *env, jobject obj,
-                                                   jchar mode, jlong position, 
jint size)
+Java_gnu_java_nio_channels_FileChannelImpl_channelMap (JNIEnv *env,
+                                                      jobject obj
+                                                      __attribute__
+                                                      ((__unused__)),
+                                                      int fd,
+                                                      jchar mode,
+                                                      jlong position,
+                                                      jint size)
 {
 #ifdef HAVE_MMAP
   jclass MappedByteBufferImpl_class;
@@ -517,7 +484,6 @@
   volatile jobject buffer;
   long pagesize;
   int prot, flags;
-  int fd;
   void *p;
   void *address;
 
@@ -542,7 +508,6 @@
   if (mode == '+')
     prot |= PROT_WRITE;
   flags = (mode == 'c' ? MAP_PRIVATE : MAP_SHARED);
-  fd = get_native_fd (env, obj);
   p = mmap (NULL, (size_t) ALIGN_UP (size, pagesize), prot, flags,
            fd, ALIGN_DOWN (position, pagesize));
   if (p == MAP_FAILED)
@@ -599,15 +564,16 @@
  * Return byte read or -1 on eof, exception on error
  */
 JNIEXPORT jint JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_read__ (JNIEnv * env, jobject obj)
+Java_gnu_java_nio_channels_FileChannelImpl_channelReadByte (JNIEnv * env,
+                                                           jobject obj
+                                                           __attribute__
+                                                           ((__unused__)),
+                                                           int native_fd)
 {
-  int native_fd;
   char data;
   ssize_t bytes_read;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
   bytes_read = 0;
   do
     {
@@ -635,20 +601,20 @@
  * Return number of bytes read or -1 on eof, exception on error
  */
 JNIEXPORT jint JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_read___3BII (JNIEnv * env,
-                                                       jobject obj,
-                                                       jbyteArray buffer,
-                                                       jint offset,
-                                                       jint length)
+Java_gnu_java_nio_channels_FileChannelImpl_channelReadBytes (JNIEnv * env,
+                                                            jobject obj
+                                                            __attribute__
+                                                            ((__unused__)),
+                                                            int native_fd,
+                                                            jbyteArray buffer,
+                                                            jint offset,
+                                                            jint length)
 {
-  int native_fd;
   jbyte *bufptr;
   ssize_t bytes_read;
   ssize_t n;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
   /* Must return 0 if an attempt is made to read 0 bytes. */
   if (length == 0)
     return 0;
@@ -709,15 +675,17 @@
  * Return status code, exception on error
  */
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_write__I (JNIEnv * env,
-                                                    jobject obj, jint b)
+Java_gnu_java_nio_channels_FileChannelImpl_channelWriteByte (JNIEnv * env,
+                                                            jobject obj
+                                                            __attribute__
+                                                            ((__unused__)),
+                                                            int native_fd,
+                                                            jint b)
 {
-  int native_fd;
   char native_data;
   ssize_t bytes_written;
   int result;
 
-  native_fd = get_native_fd (env, obj);
   native_data = (char) (CONVERT_JINT_TO_INT (b) & 0xFF);
 
   do
@@ -740,12 +708,17 @@
  * Copies all parts of a file to disk.
  */
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_force (JNIEnv * env,
-                                                 jobject obj)
+Java_gnu_java_nio_channels_FileChannelImpl_channelForce (JNIEnv * env,
+                                                        jobject obj
+                                                        __attribute__
+                                                        ((__unused__)),
+                                                        int native_fd,
+                                                        jboolean meta_data
+                                                        __attribute__
+                                                        ((__unused__)))
 {
-  int native_fd;
   int result;
-  native_fd = get_native_fd (env, obj);
+  /* FIXME - Use meta_data argument when possible. */
   TARGET_NATIVE_FILE_FSYNC (native_fd, result);
   if (result != TARGET_NATIVE_OK)
     JCL_ThrowException (env, IO_EXCEPTION,
@@ -757,20 +730,20 @@
  * Return status code, exception on error
  */
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_write___3BII (JNIEnv * env,
-                                                        jobject obj,
-                                                        jbyteArray buffer,
-                                                        jint offset,
-                                                        jint length)
+Java_gnu_java_nio_channels_FileChannelImpl_channelWriteBytes(JNIEnv * env,
+                                                            jobject obj
+                                                            __attribute__
+                                                            ((__unused__)),
+                                                            int native_fd,
+                                                            jbyteArray buffer,
+                                                            jint offset,
+                                                            jint length)
 {
-  int native_fd;
   jbyte *bufptr;
   ssize_t bytes_written;
   ssize_t n;
   int result;
 
-  native_fd = get_native_fd (env, obj);
-
   /* Just return if an attempt is made to write 0 bytes. */
   if (length == 0)
     return;
@@ -804,12 +777,17 @@
 }
 
 JNIEXPORT jboolean JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_lock (JNIEnv *env, jobject obj,
-                                                 jlong position, jlong size,
-                                                 jboolean shared, jboolean 
wait)
+Java_gnu_java_nio_channels_FileChannelImpl_channelLock (JNIEnv *env,
+                                                       jobject obj
+                                                       __attribute__
+                                                       ((__unused__)),
+                                                       int fd,
+                                                       jlong position,
+                                                       jlong size,
+                                                       jboolean shared,
+                                                       jboolean wait)
 {
 #ifdef HAVE_FCNTL
-  int fd = get_native_fd (env, obj);
   int cmd = wait ? F_SETLKW : F_SETLK;
   struct flock flock;
   int ret;
@@ -851,13 +829,15 @@
 }
 
 JNIEXPORT void JNICALL
-Java_gnu_java_nio_channels_FileChannelImpl_unlock (JNIEnv *env,
-                                                   jobject obj,
-                                                   jlong position,
-                                                   jlong length)
+Java_gnu_java_nio_channels_FileChannelImpl_channelUnlock (JNIEnv *env,
+                                                         jobject obj
+                                                         __attribute__
+                                                         ((__unused__)),
+                                                         int fd,
+                                                         jlong position,
+                                                         jlong length)
 {
 #ifdef HAVE_FCNTL
-  int fd = get_native_fd (env, obj);
   struct flock flock;
   int ret;
 

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]