From 83803fce1bb1e3657b8e5697df09d73b551bab05 Mon Sep 17 00:00:00 2001 From: Samuel Lepetit Date: Fri, 4 May 2012 19:36:10 +0200 Subject: [PATCH] Move functions from MsgNative to Host, and from jmsg to jmsg_host --- org/simgrid/msg/Host.java | 38 +++---- org/simgrid/msg/MsgNative.java | 75 -------------- org/simgrid/msg/Task.java | 4 +- src/jmsg.c | 146 +-------------------------- src/jmsg.h | 49 ---------- src/jmsg_host.c | 174 ++++++++++++++++++++++++++++++--- src/jmsg_host.h | 55 +++++++++++ 7 files changed, 229 insertions(+), 312 deletions(-) diff --git a/org/simgrid/msg/Host.java b/org/simgrid/msg/Host.java index 186a0ab00c..8e09849435 100644 --- a/org/simgrid/msg/Host.java +++ b/org/simgrid/msg/Host.java @@ -74,31 +74,22 @@ public class Host { * @exception HostNotFoundException if the name of the host is not valid. * NativeException if the native version of this method failed. */ - public static Host getByName(String name) - throws HostNotFoundException { - if (name==null) - throw new NullPointerException("No host can have a null name"); - return MsgNative.hostGetByName(name); - } - + public native static Host getByName(String name) + throws HostNotFoundException, NullPointerException; /** * This static method returns the count of the installed hosts. * * @return The count of the installed hosts. - * + * FIXME: Not implemented... ? */ - public static int getCount() { - return MsgNative.hostGetCount(); - } + public native static int getCount(); /** * This static method return an instance to the host of the current process. * * @return The host on which the current process is executed. */ - public static Host currentHost() { - return MsgNative.hostSelf(); - } + public native static Host currentHost(); /** * This static method returns all of the hosts of the installed platform. @@ -116,10 +107,7 @@ public class Host { * @return The name of the host. * */ - public String getName() { - return MsgNative.hostGetName(this); - } - + public native String getName(); /** * Sets the data of the host. * @@ -153,9 +141,7 @@ public class Host { * * @return The number of tasks currently running on a host. */ - public int getLoad() { - return MsgNative.hostGetLoad(this); - } + public native int getLoad(); /** * This method returns the speed of the processor of a host, @@ -164,14 +150,12 @@ public class Host { * @return The speed of the processor of the host in flops. * */ - public double getSpeed() { - return MsgNative.hostGetSpeed(this); - } + public native double getSpeed(); /** This method tests if a host is avail. * @return */ - public boolean isAvail() { - return MsgNative.hostIsAvail(this); - } + public native boolean isAvail(); + + } diff --git a/org/simgrid/msg/MsgNative.java b/org/simgrid/msg/MsgNative.java index 088442bb6e..f6c28a924a 100644 --- a/org/simgrid/msg/MsgNative.java +++ b/org/simgrid/msg/MsgNative.java @@ -176,73 +176,6 @@ final class MsgNative { * The natively implemented methods connected to the MSG host * ******************************************************************/ - /** - * The natively implemented method to get an host from his name. - * - * @param name The name of the host to get. - * - * @return The host having the specified name. - * - * @exception HostNotFoundException if there is no such host - * - * - * @see Host.getByName() - */ - final static native Host hostGetByName(String name) throws HostNotFoundException; - - /** - * The natively implemented method to get the name of an MSG host. - * - * @param host The host (valid) to get the name. - * - * @return The name of the specified host. - * - * @see Host.getName() - */ - final static native String hostGetName(Host host); - - /** - * The natively implemented method to get the number of hosts of the simulation. - * - * @return The number of hosts of the simulation. - * - * @see Host.getNumber() - */ - final static native int hostGetCount(); - - /** - * The natively implemented method to get the host of the current runing process. - * - * @return The host of the current running process. - * - * @see Host.currentHost() - */ - final static native Host hostSelf(); - - /** - * The natively implemented method to get the speed of a MSG host. - * - * @param host The host to get the host. - * - * @return The speed of the specified host. - * - * @see Host.getSpeed() - */ - - final static native double hostGetSpeed(Host host); - - /** - * The natively implemented native method to test if an host is avail. - * - * @param host The host to test. - * - * @return If the host is avail the method returns true. - * Otherwise the method returns false. - * - * @see Host.isAvail() - */ - final static native boolean hostIsAvail(Host host); - /** * The natively implemented native method to get all the hosts of the simulation. * @@ -251,12 +184,4 @@ final class MsgNative { final static native Host[] allHosts(); - /** - * The natively implemented native method to get the number of running tasks on a host. - * - * @param The host concerned by the operation. - * - * @return The number of running tasks. - */ - final static native int hostGetLoad(Host host); } diff --git a/org/simgrid/msg/Task.java b/org/simgrid/msg/Task.java index 93b7863d6c..5878ad9e23 100644 --- a/org/simgrid/msg/Task.java +++ b/org/simgrid/msg/Task.java @@ -172,7 +172,7 @@ public class Task { * @throws HostFailureException * @throws TransferFailureException */ - public void send(String mailbox) throws NativeException, TransferFailureException, HostFailureException, TimeoutException { + public void send(String mailbox) throws TransferFailureException, HostFailureException, TimeoutException { send(mailbox, -1); } @@ -186,7 +186,7 @@ public class Task { * @throws HostFailureException * @throws TransferFailureException */ - public native void send(String mailbox, double timeout) throws NativeException, TransferFailureException, HostFailureException, TimeoutException; + public native void send(String mailbox, double timeout) throws TransferFailureException, HostFailureException, TimeoutException; /** * Sends the task on the mailbox identified by the specified alias (capping the sending rate to \a maxrate) * diff --git a/src/jmsg.c b/src/jmsg.c index e876df3729..95b89eb366 100644 --- a/src/jmsg.c +++ b/src/jmsg.c @@ -345,136 +345,6 @@ Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls, * The MSG host connected functions implementation. * ***************************************************************************************/ -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls, - jstring jname) -{ - m_host_t host; /* native host */ - jobject jhost; /* global reference to the java host instance returned */ - - /* get the C string from the java string */ - const char *name = (*env)->GetStringUTFChars(env, jname, 0); - XBT_DEBUG("Looking for host '%s'",name); - /* get the host by name (the hosts are created during the grid resolution) */ - host = MSG_get_host_by_name(name); - XBT_DEBUG("MSG gave %p as native host", host); - - if (!host) { /* invalid name */ - jxbt_throw_host_not_found(env, name); - (*env)->ReleaseStringUTFChars(env, jname, name); - return NULL; - } - (*env)->ReleaseStringUTFChars(env, jname, name); - - if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */ - - /* Instantiate a new java host */ - jhost = jhost_new_instance(env); - - if (!jhost) { - jxbt_throw_jni(env, "java host instantiation failed"); - return NULL; - } - - /* get a global reference to the newly created host */ - jhost = jhost_ref(env, jhost); - - if (!jhost) { - jxbt_throw_jni(env, "new global ref allocation failed"); - return NULL; - } - - /* bind the java host and the native host */ - jhost_bind(jhost, host, env); - - /* the native host data field is set with the global reference to the - * java host returned by this function - */ - MSG_host_set_data(host, (void *) jhost); - } - - /* return the global reference to the java host instance */ - return (jobject) MSG_host_get_data(host); -} - -JNIEXPORT jstring JNICALL -Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls, - jobject jhost) -{ - m_host_t host = jhost_get_native(env, jhost); - const char* name; - - if (!host) { - jxbt_throw_notbound(env, "host", jhost); - return NULL; - } - - name = MSG_host_get_name(host); - if (!name) - xbt_die("This host has no name..."); - - return (*env)->NewStringUTF(env, name); -} - -JNIEXPORT jint JNICALL -Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls) -{ - xbt_dynar_t hosts = MSG_hosts_as_dynar(); - int nb_host = xbt_dynar_length(hosts); - xbt_dynar_free(&hosts); - return (jint) nb_host; -} - -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls) -{ - jobject jhost; - - m_host_t host = MSG_host_self(); - - if (!MSG_host_get_data(host)) { - /* the native host not yet associated with the java host instance */ - - /* instanciate a new java host instance */ - jhost = jhost_new_instance(env); - - if (!jhost) { - jxbt_throw_jni(env, "java host instantiation failed"); - return NULL; - } - - /* get a global reference to the newly created host */ - jhost = jhost_ref(env, jhost); - - if (!jhost) { - jxbt_throw_jni(env, "global ref allocation failed"); - return NULL; - } - - /* Bind & store it */ - jhost_bind(jhost, host, env); - MSG_host_set_data(host, (void *) jhost); - } else { - jhost = (jobject) MSG_host_get_data(host); - } - - return jhost; -} - -JNIEXPORT jdouble JNICALL -Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls, - jobject jhost) -{ - m_host_t host = jhost_get_native(env, jhost); - - if (!host) { - jxbt_throw_notbound(env, "host", jhost); - return -1; - } - - return (jdouble) MSG_get_host_speed(host); -} - JNIEXPORT jint JNICALL Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls, jobject jhost) @@ -490,20 +360,6 @@ Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls, } -JNIEXPORT jboolean JNICALL -Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls, - jobject jhost) -{ - m_host_t host = jhost_get_native(env, jhost); - - if (!host) { - jxbt_throw_notbound(env, "host", jhost); - return 0; - } - - return (jboolean) MSG_host_is_avail(host); -} - /*************************************************************************************** * Unsortable functions * ***************************************************************************************/ @@ -666,7 +522,7 @@ Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg) jname = (*env)->NewStringUTF(env, MSG_host_get_name(host)); jhost = - Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname); + Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname); /* FIXME: leak of jname ? */ } diff --git a/src/jmsg.h b/src/jmsg.h index eb800fd7cb..25aaf8211e 100644 --- a/src/jmsg.h +++ b/src/jmsg.h @@ -137,58 +137,10 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_MsgNative_processMigrate JNIEXPORT void JNICALL Java_org_simgrid_msg_MsgNative_processWaitFor (JNIEnv *, jclass, jdouble); -/* - * Class simgrid_msg_Msg - * Method hostGetByName - * Signature (Ljava/lang/String;)Lsimgrid/msg/Host; - */ -JNIEXPORT jobject JNICALL Java_org_simgrid_msg_MsgNative_hostGetByName - (JNIEnv *, jclass, jstring); - -/* - * Class simgrid_msg_Msg - * Method hostGetName - * Signature (Lsimgrid/msg/Host;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_simgrid_msg_MsgNative_hostGetName - (JNIEnv *, jclass, jobject); - -/* - * Class simgrid_msg_Msg - * Method hostGetNumber - * Signature ()I - */ -JNIEXPORT jint JNICALL Java_org_simgrid_msg_MsgNative_hostGetNumber - (JNIEnv *, jclass); - -/* - * Class simgrid_msg_Msg - * Method hostSelf - * Signature ()Lsimgrid/msg/Host; - */ -JNIEXPORT jobject JNICALL Java_org_simgrid_msg_MsgNative_hostSelf - (JNIEnv *, jclass); - -/* - * Class simgrid_msg_Msg - * Method hostGetSpeed - * Signature (Lsimgrid/msg/Host;)D - */ -JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_MsgNative_hostGetSpeed - (JNIEnv *, jclass, jobject); - JNIEXPORT jint JNICALL Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls, jobject jhost); -/* - * Class simgrid_msg_Msg - * Method hostIsAvail - * Signature (Lsimgrid/msg/Host;)Z - */ -JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_MsgNative_hostIsAvail - (JNIEnv *, jclass, jobject); - /* * Class simgrid_msg_Msg * Method getClock @@ -229,7 +181,6 @@ Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv *, jclass); JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls, jstring jplatformFile); - JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls, jstring jdeploymentFile); diff --git a/src/jmsg_host.c b/src/jmsg_host.c index 7185b2041a..b124580b17 100644 --- a/src/jmsg_host.c +++ b/src/jmsg_host.c @@ -11,8 +11,9 @@ #include "jmsg_host.h" #include "jxbt_utilities.h" -jobject jhost_new_instance(JNIEnv * env) -{ +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); + +jobject jhost_new_instance(JNIEnv * env) { jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host"); jmethodID constructor = jxbt_get_jmethod(env, cls, "", "()V"); @@ -23,18 +24,15 @@ jobject jhost_new_instance(JNIEnv * env) return (*env)->NewObject(env, cls, constructor); } -jobject jhost_ref(JNIEnv * env, jobject jhost) -{ +jobject jhost_ref(JNIEnv * env, jobject jhost) { return (*env)->NewGlobalRef(env, jhost); } -void jhost_unref(JNIEnv * env, jobject jhost) -{ +void jhost_unref(JNIEnv * env, jobject jhost) { (*env)->DeleteGlobalRef(env, jhost); } -void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env) -{ +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) @@ -43,8 +41,7 @@ void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env) (*env)->SetLongField(env, jhost, id, (jlong) (long) (host)); } -m_host_t jhost_get_native(JNIEnv * env, jobject jhost) -{ +m_host_t jhost_get_native(JNIEnv * env, jobject jhost) { jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Host", "bind", "J"); if (!id) @@ -53,14 +50,12 @@ m_host_t jhost_get_native(JNIEnv * env, jobject jhost) return (m_host_t) (long) (*env)->GetLongField(env, jhost, id); } -const char *jhost_get_name(jobject jhost, JNIEnv * env) -{ +const char *jhost_get_name(jobject jhost, JNIEnv * env) { m_host_t host = jhost_get_native(env, jhost); return MSG_host_get_name(host); } -jboolean jhost_is_valid(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) @@ -72,3 +67,154 @@ jboolean jhost_is_valid(jobject jhost, JNIEnv * env) return JNI_FALSE; } } + +JNIEXPORT jobject JNICALL +Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, + jstring jname) { + m_host_t host; /* native host */ + jobject jhost; /* global reference to the java host instance returned */ + + /* get the C string from the java string */ + const char *name = (*env)->GetStringUTFChars(env, jname, 0); + if (name == NULL) { + jxbt_throw_null(env,bprintf("No host can have a null name")); + return NULL; + } + XBT_DEBUG("Looking for host '%s'",name); + /* get the host by name (the hosts are created during the grid resolution) */ + host = MSG_get_host_by_name(name); + XBT_DEBUG("MSG gave %p as native host", host); + + if (!host) { /* invalid name */ + jxbt_throw_host_not_found(env, name); + (*env)->ReleaseStringUTFChars(env, jname, name); + return NULL; + } + (*env)->ReleaseStringUTFChars(env, jname, name); + + if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */ + + /* Instantiate a new java host */ + jhost = jhost_new_instance(env); + + if (!jhost) { + jxbt_throw_jni(env, "java host instantiation failed"); + return NULL; + } + + /* get a global reference to the newly created host */ + jhost = jhost_ref(env, jhost); + + if (!jhost) { + jxbt_throw_jni(env, "new global ref allocation failed"); + return NULL; + } + + /* bind the java host and the native host */ + jhost_bind(jhost, host, env); + + /* the native host data field is set with the global reference to the + * java host returned by this function + */ + MSG_host_set_data(host, (void *) jhost); + } + + /* return the global reference to the java host instance */ + return (jobject) MSG_host_get_data(host); +} + +JNIEXPORT jobject JNICALL +Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { + jobject jhost; + + m_host_t host = MSG_host_self(); + + if (!MSG_host_get_data(host)) { + /* the native host not yet associated with the java host instance */ + + /* instanciate a new java host instance */ + jhost = jhost_new_instance(env); + + if (!jhost) { + jxbt_throw_jni(env, "java host instantiation failed"); + return NULL; + } + + /* get a global reference to the newly created host */ + jhost = jhost_ref(env, jhost); + + if (!jhost) { + jxbt_throw_jni(env, "global ref allocation failed"); + return NULL; + } + + /* Bind & store it */ + jhost_bind(jhost, host, env); + MSG_host_set_data(host, (void *) jhost); + } else { + jhost = (jobject) MSG_host_get_data(host); + } + + return jhost; +} + +JNIEXPORT jstring JNICALL +Java_org_simgrid_msg_Host_getName(JNIEnv * env, + jobject jhost) { + m_host_t host = jhost_get_native(env, jhost); + const char* name; + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return NULL; + } + + name = MSG_host_get_name(host); + if (!name) + xbt_die("This host has no name..."); + + return (*env)->NewStringUTF(env, name); +} + +JNIEXPORT jint JNICALL +Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) { + xbt_dynar_t hosts = MSG_hosts_as_dynar(); + int nb_host = xbt_dynar_length(hosts); + xbt_dynar_free(&hosts); + return (jint) nb_host; +} + +JNIEXPORT jdouble JNICALL +Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env, + jobject jhost) { + m_host_t host = jhost_get_native(env, jhost); + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return -1; + } + + return (jdouble) MSG_get_host_speed(host); +} +JNIEXPORT jint JNICALL +Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) { + m_host_t host = jhost_get_native(env, jhost); + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return -1; + } + + return (jint) MSG_get_host_msgload(host); +} +JNIEXPORT jboolean JNICALL +Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) { + m_host_t host = jhost_get_native(env, jhost); + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return 0; + } + + return (jboolean) MSG_host_is_avail(host); +} diff --git a/src/jmsg_host.h b/src/jmsg_host.h index 4434b16f6b..d4ddd1d910 100644 --- a/src/jmsg_host.h +++ b/src/jmsg_host.h @@ -97,4 +97,59 @@ const char *jhost_get_name(jobject jhost, JNIEnv * env); */ jboolean jhost_is_valid(jobject jhost, JNIEnv * env); +/* + * Class simgrid_msg_Host + * Method getByName + * Signature (Ljava/lang/String;)Lsimgrid/msg/Host; + */ +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getByName + (JNIEnv *, jclass, jstring); + +/* + * Class simgrid_msg_Host + * Method currentHost + * Signature ()Lsimgrid/msg/Host; + */ +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_currentHost + (JNIEnv *, jclass); + +/* + * Class simgrid_msg_Host + * Method getName + * Signature ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_simgrid_msg_Host_getName + (JNIEnv *, jobject); +/* + * Class simgrid_msg_Host + * Method getCount + * Signature ()I + */ +JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getCount + (JNIEnv *, jclass); + +/* + * Class simgrid_msg_Host + * Method getSpeed + * Signature ()D + */ +JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getSpeed + (JNIEnv *, jobject); +/* + * Class simgrid_msg_Host + * Method getLoad + * Signature ()I + */ +JNIEXPORT jint JNICALL +Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, + jobject jhost); + +/* + * Class simgrid_msg_Msg + * Method isAvail + * Signature ()Z + */ +JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Host_isAvail + (JNIEnv *, jobject); + #endif /*!MSG_JHOST_H */ -- 2.20.1