Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a cache on Host.name. Add a cache on some jfieldID/jmethodID on Host/Process
authorSamuel Lepetit <samuel.lepetit@inria.fr>
Wed, 9 May 2012 13:39:47 +0000 (15:39 +0200)
committerSamuel Lepetit <samuel.lepetit@inria.fr>
Wed, 9 May 2012 13:39:47 +0000 (15:39 +0200)
org/simgrid/msg/Host.java
src/jmsg_host.c
src/jmsg_host.h
src/jmsg_process.c
src/jmsg_synchro.c
src/jmsg_task.c

index 11e6bda..905596d 100644 (file)
@@ -50,7 +50,10 @@ public class Host {
         * @see                         Host.getByName().
         */ 
        public long bind;
-
+       /**
+        * Host name
+        */
+       private String name;
 
        /**
         * User data.
@@ -101,11 +104,12 @@ public class Host {
 
        /**
         * This method returns the name of a host.
-        * FIXME: Cache it.
         * @return                      The name of the host.
         *
         */ 
-       public native String getName();
+       public String getName() {
+               return name;
+       }
        /**
         * Sets the data of the host.
      * @param data
@@ -153,5 +157,11 @@ public class Host {
      */
        public native boolean isAvail();
        
-       
+       /**
+        * Class initializer, to initialize various JNI stuff
+        */
+       public static native void nativeInit();
+       static {
+               nativeInit();
+       }       
 } 
index 9707deb..fb3cdec 100644 (file)
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
-jobject jhost_new_instance(JNIEnv * env) {
+static jmethodID jhost_method_Host_constructor;
+static jfieldID jhost_field_Host_bind;
+static jfieldID jhost_field_Host_name;
 
-  jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
-  jmethodID constructor = jxbt_get_jmethod(env, cls, "<init>", "()V");
 
-  if (!constructor)
-    return NULL;
-
-  return (*env)->NewObject(env, cls, constructor);
+jobject jhost_new_instance(JNIEnv * env) {
+  jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
+  return (*env)->NewObject(env, cls, jhost_method_Host_constructor);
 }
 
 jobject jhost_ref(JNIEnv * env, jobject jhost) {
@@ -33,21 +32,11 @@ void jhost_unref(JNIEnv * env, jobject jhost) {
 }
 
 void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env) {
-  jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
-
-  if (!id)
-    return;
-
-  (*env)->SetLongField(env, jhost, id, (jlong) (long) (host));
+  (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
 }
 
 m_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
-  jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
-
-  if (!id)
-    return NULL;
-
-  return (m_host_t) (long) (*env)->GetLongField(env, jhost, id);
+  return (m_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
 }
 
 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
@@ -56,18 +45,24 @@ const char *jhost_get_name(jobject jhost, JNIEnv * env) {
 }
 
 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
-  jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J");
-
-  if (!id)
-    return 0;
-
-  if ((*env)->GetLongField(env, jhost, id)) {
+  if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
     return JNI_TRUE;
   } else {
     return JNI_FALSE;
   }
 }
 
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
+       jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
+       jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
+       //FIXME: Don't use jxbt_get_sfield directly, it is slower.
+       jhost_field_Host_bind = jxbt_get_sfield(env,"org/simgrid/msg/Host", "bind", "J");
+       jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
+       if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
+       jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+       }
+}
 JNIEXPORT jobject JNICALL
 Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
                                          jstring jname) {
@@ -109,7 +104,8 @@ Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
       jxbt_throw_jni(env, "new global ref allocation failed");
       return NULL;
     }
-
+    /* Sets the java host name */
+    (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
     /* bind the java host and the native host */
     jhost_bind(jhost, host, env);
 
@@ -147,7 +143,10 @@ Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
       jxbt_throw_jni(env, "global ref allocation failed");
       return NULL;
     }
-
+    /* Sets the host name */
+    const char *name = MSG_host_get_name(host);
+    jobject jname = (*env)->NewStringUTF(env,name);
+    (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
     /* Bind & store it */
     jhost_bind(jhost, host, env);
     MSG_host_set_data(host, (void *) jhost);
index aa0b5b0..374d8ef 100644 (file)
@@ -96,6 +96,13 @@ const char *jhost_get_name(jobject jhost, JNIEnv * env);
  *                                             Otherwise the function returns false.
  */
 jboolean jhost_is_valid(jobject jhost, JNIEnv * env);
+/*
+ * Class               org_simgrid_msg_Host
+ * Method              nativeInit
+ * Signature   ();
+ */
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls);
 
 /*
  * Class               org_simgrid_msg_Host
index f0fa160..9305f82 100644 (file)
@@ -15,9 +15,9 @@
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
+static jfieldID jprocess_field_Process_host;
 static jfieldID jprocess_field_Process_pid;
 static jfieldID jprocess_field_Process_ppid;
-static jfieldID jprocess_field_Process_host;
 
 jobject native_to_java_process(m_process_t process)
 {
@@ -232,6 +232,11 @@ Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
        jprocess_field_Process_pid = jxbt_get_sfield(env, "org/simgrid/msg/Process", "pid", "I");
        jprocess_field_Process_ppid = jxbt_get_sfield(env, "org/simgrid/msg/Process", "ppid", "I");
        jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
+
+       if (!jprocess_class_Process || !jprocess_field_Process_pid ||
+                       !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
+       jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+       }
 }
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Process_create(JNIEnv * env,
index f2f06df..d7399ca 100644 (file)
@@ -8,7 +8,6 @@
 #include "jmsg_synchro.h"
 #include "jxbt_utilities.h"
 
-
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Mutex_init(JNIEnv * env, jobject obj) {
        xbt_mutex_t mutex = xbt_mutex_init();
index f91185b..bd1ea49 100644 (file)
@@ -18,7 +18,7 @@
 #include <msg/msg.h>
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
-static jmethodID jtask_field_Comm_constructor;
+static jmethodID jtask_method_Comm_constructor;
 
 static jfieldID jtask_field_Task_bind;
 static jfieldID jtask_field_Comm_bind;
@@ -44,14 +44,14 @@ JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Task_nativeInit(JNIEnv *env, jclass cls) {
        jclass jtask_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
 
-       jtask_field_Comm_constructor = (*env)->GetMethodID(env, jtask_class_Comm, "<init>", "()V");
+       jtask_method_Comm_constructor = (*env)->GetMethodID(env, jtask_class_Comm, "<init>", "()V");
        //FIXME: Don't use jxbt_get_sfield directly, it is slower.
        jtask_field_Task_bind = jxbt_get_sfield(env, "org/simgrid/msg/Task", "bind", "J");
        jtask_field_Comm_bind = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
        jtask_field_Comm_taskBind = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "taskBind", "J");
        jtask_field_Comm_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
        if (!jtask_field_Task_bind || !jtask_field_Comm_bind || !jtask_field_Comm_taskBind ||
-                 !jtask_field_Comm_receiving || !jtask_field_Comm_constructor) {
+                 !jtask_field_Comm_receiving || !jtask_method_Comm_constructor) {
                        jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
                  }
 }
@@ -446,7 +446,7 @@ Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox) {
                return NULL;
        }
 
-       jobject jcomm = (*env)->NewObject(env, comm_class, jtask_field_Comm_constructor);
+       jobject jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor);
        if (!jcomm) {
                jxbt_throw_native(env,bprintf("Can't create a Comm object."));
                return NULL;
@@ -480,7 +480,7 @@ Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject jtask, jstring jmailbox) {
 
        if (!comm_class) return NULL;
 
-       jcomm = (*env)->NewObject(env, comm_class, jtask_field_Comm_constructor);
+       jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor);
        mailbox = (*env)->GetStringUTFChars(env, jmailbox, 0);
 
        task = jtask_to_native_task(jtask, env);