X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/347996b4a10c4e8579080692afa60e0afb88b60a..4cedf6f3e1b53c5de2a19a293418a04c5d4d777f:/src/bindings/java/jmsg_host.cpp diff --git a/src/bindings/java/jmsg_host.cpp b/src/bindings/java/jmsg_host.cpp index dba26b2996..7076f7a543 100644 --- a/src/bindings/java/jmsg_host.cpp +++ b/src/bindings/java/jmsg_host.cpp @@ -1,20 +1,20 @@ /* Functions related to the java host instances. */ -/* Copyright (c) 2007-2015. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "xbt/str.h" -#include "simgrid/msg.h" -#include "jmsg.h" +#include "simgrid/plugins/energy.h" +#include "simgrid/s4u/Host.hpp" +#include "simgrid/s4u/Storage.hpp" + +#include "jmsg.hpp" #include "jmsg_host.h" -#include "jxbt_utilities.h" #include "jmsg_storage.h" -#include +#include "jxbt_utilities.hpp" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); static jmethodID jhost_method_Host_constructor; static jfieldID jhost_field_Host_bind; @@ -41,76 +41,55 @@ msg_host_t jhost_get_native(JNIEnv * env, jobject jhost) { return (msg_host_t) (uintptr_t) env->GetLongField(jhost, jhost_field_Host_bind); } -const char *jhost_get_name(jobject jhost, JNIEnv * env) { - msg_host_t host = jhost_get_native(env, jhost); - return MSG_host_get_name(host); -} - -jboolean jhost_is_valid(jobject jhost, JNIEnv * env) { - if (env->GetLongField(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) { +JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) { jclass class_Host = env->FindClass("org/simgrid/msg/Host"); jhost_method_Host_constructor = env->GetMethodID(class_Host, "", "()V"); jhost_field_Host_bind = jxbt_get_jfield(env,class_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.")); - } + xbt_assert(class_Host && jhost_field_Host_name && jhost_method_Host_constructor && jhost_field_Host_bind, + "Native initialization of msg/Host failed. Please report that bug"); } -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, - jstring jname) { - msg_host_t host; /* native host */ - jobject jhost; /* global reference to the java host instance returned */ + +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, jstring jname) { /* get the C string from the java string */ - if (jname == NULL) { - jxbt_throw_null(env,bprintf("No host can have a null name")); - return NULL; + if (jname == nullptr) { + jxbt_throw_null(env, "No host can have a null name"); + return nullptr; } const char *name = env->GetStringUTFChars(jname, 0); /* get the host by name (the hosts are created during the grid resolution) */ - host = MSG_host_by_name(name); + msg_host_t host = MSG_host_by_name(name); - if (!host) { /* invalid name */ + if (not host) { /* invalid name */ jxbt_throw_host_not_found(env, name); env->ReleaseStringUTFChars(jname, name); - return NULL; + return nullptr; } env->ReleaseStringUTFChars(jname, name); - if (!host->extension(JAVA_HOST_LEVEL)) { /* native host not associated yet with java host */ - + if (not host->extension(JAVA_HOST_LEVEL)) { /* native host not associated yet with java host */ /* Instantiate a new java host */ - jhost = jhost_new_instance(env); + jobject jhost = jhost_new_instance(env); - if (!jhost) { + if (not jhost) { jxbt_throw_jni(env, "java host instantiation failed"); - return NULL; + return nullptr; } /* get a global reference to the newly created host */ jhost = jhost_ref(env, jhost); - if (!jhost) { + if (not jhost) { jxbt_throw_jni(env, "new global ref allocation failed"); - return NULL; + return nullptr; } /* Sets the java host name */ env->SetObjectField(jhost, jhost_field_Host_name, jname); /* 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 - */ + /* the native host data field is set with the global reference to the java host returned by this function */ host->extension_set(JAVA_HOST_LEVEL, (void *)jhost); } @@ -118,33 +97,31 @@ Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, return (jobject) host->extension(JAVA_HOST_LEVEL); } -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { jobject jhost; msg_host_t host = MSG_host_self(); - if (!host->extension(JAVA_HOST_LEVEL)) { + if (not host->extension(JAVA_HOST_LEVEL)) { /* the native host not yet associated with the java host instance */ /* instanciate a new java host instance */ jhost = jhost_new_instance(env); - if (!jhost) { + if (not jhost) { jxbt_throw_jni(env, "java host instantiation failed"); - return NULL; + return nullptr; } /* get a global reference to the newly created host */ jhost = jhost_ref(env, jhost); - if (!jhost) { + if (not jhost) { jxbt_throw_jni(env, "global ref allocation failed"); - return NULL; + return nullptr; } /* Sets the host name */ - const char *name = MSG_host_get_name(host); - jobject jname = env->NewStringUTF(name); + jobject jname = env->NewStringUTF(host->get_cname()); env->SetObjectField(jhost, jhost_field_Host_name, jname); /* Bind & store it */ jhost_bind(jhost, host, env); @@ -156,45 +133,38 @@ Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { return jhost; } -JNIEXPORT void JNICALL -Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) { +JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) { msg_host_t host = jhost_get_native(env, jhost); MSG_host_on(host); } -JNIEXPORT void JNICALL -Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) { +JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) { msg_host_t host = jhost_get_native(env, jhost); - MSG_host_off(host); + MSG_host_off(host); } -JNIEXPORT jint JNICALL -Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) { +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) { +JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env, jobject jhost) { msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return -1; } - return (jdouble) MSG_get_host_speed(host); + return (jdouble) MSG_host_get_speed(host); } -JNIEXPORT jdouble JNICALL -Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env, - jobject jhost) { +JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env, jobject jhost) { msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return -1; } @@ -202,19 +172,18 @@ Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env, return (jdouble) MSG_host_get_core_number(host); } -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) { +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) { msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); - return NULL; + return nullptr; } const char *name = env->GetStringUTFChars((jstring) jname, 0); const char *property = MSG_host_get_property_value(host, name); - if (!property) { - return NULL; + if (not property) { + return nullptr; } jobject jproperty = env->NewStringUTF(property); @@ -228,7 +197,7 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) { msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return; } @@ -236,17 +205,17 @@ Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0); char *value = xbt_strdup(value_java); - MSG_host_set_property_value(host, name, value, xbt_free_f); + MSG_host_set_property_value(host, name, value); env->ReleaseStringUTFChars((jstring) jvalue, value_java); env->ReleaseStringUTFChars((jstring) jname, name); - } -JNIEXPORT jboolean JNICALL -Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost) { + +JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost) +{ msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return 0; } @@ -254,117 +223,98 @@ Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost) { return (jboolean) MSG_host_is_on(host); } -JNIEXPORT jobjectArray JNICALL -Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost){ - +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost) +{ msg_host_t host = jhost_get_native(env, jhost); jobject jstorage; jstring jname; - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return 0; } int index = 0; jobjectArray jtable; - xbt_dict_t dict = MSG_host_get_mounted_storage_list(host); - int count = xbt_dict_length(dict); + std::unordered_map mounted_storages = host->getMountedStorages(); + int count = mounted_storages.size(); jclass cls = env->FindClass("org/simgrid/msg/Storage"); - jtable = env->NewObjectArray((jsize) count, cls, NULL); + jtable = env->NewObjectArray((jsize) count, cls, nullptr); - if (!jtable) { - jxbt_throw_jni(env, "Storages table allocation failed"); - return NULL; + if (not jtable) { + jxbt_throw_jni(env, "Storages table allocation failed"); + return nullptr; } - xbt_dict_cursor_t cursor=NULL; - const char *mount_name, *storage_name; - - xbt_dict_foreach(dict,cursor,mount_name,storage_name) { - jname = env->NewStringUTF(storage_name); + for (auto const& elm : mounted_storages) { + jname = env->NewStringUTF(elm.second->get_cname()); jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname); env->SetObjectArrayElement(jtable, index, jstorage); index++; } - xbt_dict_free(&dict); return jtable; } -JNIEXPORT jobjectArray JNICALL -Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost){ +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost) +{ msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return 0; } jobjectArray jtable; - xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host); - int count = xbt_dynar_length(dyn); - jclass cls = jxbt_get_class(env, "java/lang/String"); - jtable = env->NewObjectArray((jsize) count, cls, NULL); - int index; - char *storage_name; + xbt_dynar_t dyn = sg_host_get_attached_storage_list(host); + jclass cls = jxbt_get_class(env, "java/lang/String"); + jtable = env->NewObjectArray(static_cast(xbt_dynar_length(dyn)), cls, nullptr); + unsigned int index; + const char* storage_name; jstring jstorage_name; - for (index = 0; index < count; index++) { - storage_name = xbt_dynar_get_as(dyn,index,char*); + xbt_dynar_foreach (dyn, index, storage_name) { jstorage_name = env->NewStringUTF(storage_name); env->SetObjectArrayElement(jtable, index, jstorage_name); } - + xbt_dynar_free_container(&dyn); return jtable; } -JNIEXPORT jobjectArray JNICALL -Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost){ +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost) +{ msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return 0; } return (jobjectArray)MSG_host_get_storage_content(host); } - -JNIEXPORT jobjectArray JNICALL -Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg) +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg) { - int index; - jobjectArray jtable; - jobject jhost; - jstring jname; - msg_host_t host; xbt_dynar_t table = MSG_hosts_as_dynar(); int count = xbt_dynar_length(table); jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host"); + if (not cls) + return nullptr; - if (!cls) { - return NULL; - } - - jtable = env->NewObjectArray((jsize) count, cls, NULL); + jobjectArray jtable = env->NewObjectArray((jsize)count, cls, nullptr); - if (!jtable) { + if (not jtable) { jxbt_throw_jni(env, "Hosts table allocation failed"); - return NULL; + return nullptr; } - for (index = 0; index < count; index++) { - host = xbt_dynar_get_as(table,index,msg_host_t); - jhost = (jobject) host->extension(JAVA_HOST_LEVEL); + for (int index = 0; index < count; index++) { + msg_host_t host = xbt_dynar_get_as(table, index, msg_host_t); + jobject jhost = static_cast(host->extension(JAVA_HOST_LEVEL)); - if (!jhost) { - jname = env->NewStringUTF(MSG_host_get_name(host)); - - jhost = - Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname); - /* FIXME: leak of jname ? */ + if (not jhost) { + jstring jname = env->NewStringUTF(host->get_cname()); + jhost = Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname); } env->SetObjectArrayElement(jtable, index, jhost); @@ -373,25 +323,52 @@ Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg) return jtable; } -JNIEXPORT void JNICALL -Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname){ - +JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname) +{ const char *name = env->GetStringUTFChars((jstring) jname, 0); - MSG_mailbox_set_async(name); + sg_mailbox_set_receiver(name); env->ReleaseStringUTFChars((jstring) jname, name); +} +JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_updateAllEnergyConsumptions(JNIEnv* env, jclass cls) +{ + sg_host_energy_update_all(); } -#include "simgrid/plugins/energy.h" -JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost) { +JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost) +{ msg_host_t host = jhost_get_native(env, jhost); - if (!host) { + if (not host) { jxbt_throw_notbound(env, "host", jhost); return 0; } return MSG_host_get_consumed_energy(host); - } +JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setPstate(JNIEnv* env, jobject jhost, jint pstate) +{ + msg_host_t host = jhost_get_native(env, jhost); + MSG_host_set_pstate(host, pstate); +} +JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getPstate(JNIEnv* env, jobject jhost) +{ + msg_host_t host = jhost_get_native(env, jhost); + return MSG_host_get_pstate(host); +} +JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getPstatesCount(JNIEnv* env, jobject jhost) +{ + msg_host_t host = jhost_get_native(env, jhost); + return MSG_host_get_nb_pstates(host); +} +JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCurrentPowerPeak(JNIEnv* env, jobject jhost) +{ + msg_host_t host = jhost_get_native(env, jhost); + return MSG_host_get_speed(host); +} +JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getPowerPeakAt(JNIEnv* env, jobject jhost, jint pstate) +{ + msg_host_t host = jhost_get_native(env, jhost); + return MSG_host_get_power_peak_at(host, pstate); +}