Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9e16cc0218c34947ffa1de72040736e03227d224
[simgrid.git] / src / bindings / java / jmsg_host.cpp
1 /* Functions related to the java host instances.                            */
2
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <xbt/dict.h>
10 #include <xbt/dynar.h>
11 #include <xbt/log.h>
12 #include <xbt/str.h>
13
14 #include <surf/surf_routing.h>
15
16 #include <simgrid/s4u/host.hpp>
17
18 #include "simgrid/msg.h"
19 #include "jmsg.h"
20 #include "jmsg_host.h"
21 #include "jxbt_utilities.h"
22 #include "jmsg_storage.h"
23
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
25
26 static jmethodID jhost_method_Host_constructor;
27 static jfieldID jhost_field_Host_bind;
28 static jfieldID jhost_field_Host_name;
29
30 jobject jhost_new_instance(JNIEnv * env) {
31   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
32   return env->NewObject(cls, jhost_method_Host_constructor);
33 }
34
35 jobject jhost_ref(JNIEnv * env, jobject jhost) {
36   return env->NewGlobalRef(jhost);
37 }
38
39 void jhost_unref(JNIEnv * env, jobject jhost) {
40   env->DeleteGlobalRef(jhost);
41 }
42
43 void jhost_bind(jobject jhost, msg_host_t host, JNIEnv * env) {
44   env->SetLongField(jhost, jhost_field_Host_bind, (jlong) (uintptr_t) (host));
45 }
46
47 msg_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
48   return (msg_host_t) (uintptr_t) env->GetLongField(jhost, jhost_field_Host_bind);
49 }
50
51 const char *jhost_get_name(jobject jhost, JNIEnv * env) {
52   msg_host_t host = jhost_get_native(env, jhost);
53   return MSG_host_get_name(host);
54 }
55
56 jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
57   if (env->GetLongField(jhost, jhost_field_Host_bind)) {
58     return JNI_TRUE;
59   } else {
60     return JNI_FALSE;
61   }
62 }
63
64 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
65   jclass class_Host = env->FindClass("org/simgrid/msg/Host");
66   jhost_method_Host_constructor = env->GetMethodID(class_Host, "<init>", "()V");
67   jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
68   jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
69   if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
70     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
71   }
72 }
73
74 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, jstring jname) {
75   msg_host_t host;                /* native host                                          */
76   jobject jhost;                /* global reference to the java host instance returned  */
77
78   /* get the C string from the java string */
79   if (jname == nullptr) {
80     jxbt_throw_null(env,bprintf("No host can have a null name"));
81     return nullptr;
82   }
83   const char *name = env->GetStringUTFChars(jname, 0);
84   /* get the host by name       (the hosts are created during the grid resolution) */
85   host = MSG_host_by_name(name);
86
87   if (!host) {                  /* invalid name */
88     jxbt_throw_host_not_found(env, name);
89     env->ReleaseStringUTFChars(jname, name);
90     return nullptr;
91   }
92   env->ReleaseStringUTFChars(jname, name);
93
94   if (!host->extension(JAVA_HOST_LEVEL)) {       /* native host not associated yet with java host */
95     /* Instantiate a new java host */
96     jhost = jhost_new_instance(env);
97
98     if (!jhost) {
99       jxbt_throw_jni(env, "java host instantiation failed");
100       return nullptr;
101     }
102
103     /* get a global reference to the newly created host */
104     jhost = jhost_ref(env, jhost);
105
106     if (!jhost) {
107       jxbt_throw_jni(env, "new global ref allocation failed");
108       return nullptr;
109     }
110     /* Sets the java host name */
111     env->SetObjectField(jhost, jhost_field_Host_name, jname);
112     /* bind the java host and the native host */
113     jhost_bind(jhost, host, env);
114
115     /* the native host data field is set with the global reference to the java host returned by this function */
116     host->extension_set(JAVA_HOST_LEVEL, (void *)jhost);
117   }
118
119   /* return the global reference to the java host instance */
120   return (jobject) host->extension(JAVA_HOST_LEVEL);
121 }
122
123 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
124   jobject jhost;
125
126   msg_host_t host = MSG_host_self();
127
128   if (!host->extension(JAVA_HOST_LEVEL)) {
129     /* the native host not yet associated with the java host instance */
130
131     /* instanciate a new java host instance */
132     jhost = jhost_new_instance(env);
133
134     if (!jhost) {
135       jxbt_throw_jni(env, "java host instantiation failed");
136       return nullptr;
137     }
138
139     /* get a global reference to the newly created host */
140     jhost = jhost_ref(env, jhost);
141
142     if (!jhost) {
143       jxbt_throw_jni(env, "global ref allocation failed");
144       return nullptr;
145     }
146     /* Sets the host name */
147     const char *name = MSG_host_get_name(host);
148     jobject jname = env->NewStringUTF(name);
149     env->SetObjectField(jhost, jhost_field_Host_name, jname);
150     /* Bind & store it */
151     jhost_bind(jhost, host, env);
152     host->extension_set(JAVA_HOST_LEVEL, (void *) jhost);
153   } else {
154     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
155   }
156
157   return jhost;
158 }
159
160 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) {
161   msg_host_t host = jhost_get_native(env, jhost);
162   MSG_host_on(host);
163 }
164
165 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
166   msg_host_t host = jhost_get_native(env, jhost);
167   MSG_host_off(host); 
168 }
169
170 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
171   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
172   int nb_host = xbt_dynar_length(hosts);
173   xbt_dynar_free(&hosts);
174   return (jint) nb_host;
175 }
176
177 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env, jobject jhost) {
178   msg_host_t host = jhost_get_native(env, jhost);
179
180   if (!host) {
181     jxbt_throw_notbound(env, "host", jhost);
182     return -1;
183   }
184
185   return (jdouble) MSG_host_get_speed(host);
186 }
187
188 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env, jobject jhost) {
189   msg_host_t host = jhost_get_native(env, jhost);
190
191   if (!host) {
192     jxbt_throw_notbound(env, "host", jhost);
193     return -1;
194   }
195
196   return (jdouble) MSG_host_get_core_number(host);
197 }
198
199 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
200   msg_host_t host = jhost_get_native(env, jhost);
201
202   if (!host) {
203     jxbt_throw_notbound(env, "host", jhost);
204     return nullptr;
205   }
206   const char *name = env->GetStringUTFChars((jstring) jname, 0);
207
208   const char *property = MSG_host_get_property_value(host, name);
209   if (!property) {
210     return nullptr;
211   }
212
213   jobject jproperty = env->NewStringUTF(property);
214
215   env->ReleaseStringUTFChars((jstring) jname, name);
216
217   return jproperty;
218 }
219
220 JNIEXPORT void JNICALL
221 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
222   msg_host_t host = jhost_get_native(env, jhost);
223
224   if (!host) {
225     jxbt_throw_notbound(env, "host", jhost);
226     return;
227   }
228   const char *name = env->GetStringUTFChars((jstring) jname, 0);
229   const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
230   char *value = xbt_strdup(value_java);
231
232   MSG_host_set_property_value(host, name, value, xbt_free_f);
233
234   env->ReleaseStringUTFChars((jstring) jvalue, value_java);
235   env->ReleaseStringUTFChars((jstring) jname, name);
236 }
237
238 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost)
239 {
240   msg_host_t host = jhost_get_native(env, jhost);
241
242   if (!host) {
243     jxbt_throw_notbound(env, "host", jhost);
244     return 0;
245   }
246
247   return (jboolean) MSG_host_is_on(host);
248 }
249
250 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost)
251 {
252   msg_host_t host = jhost_get_native(env, jhost);
253   jobject jstorage;
254   jstring jname;
255
256   if (!host) {
257     jxbt_throw_notbound(env, "host", jhost);
258     return 0;
259   }
260
261   int index = 0;
262   jobjectArray jtable;
263   xbt_dict_t dict =  MSG_host_get_mounted_storage_list(host);
264   int count = xbt_dict_length(dict);
265   jclass cls = env->FindClass("org/simgrid/msg/Storage");
266
267   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
268
269   if (!jtable) {
270    jxbt_throw_jni(env, "Storages table allocation failed");
271    return nullptr;
272   }
273
274   xbt_dict_cursor_t cursor=nullptr;
275   const char* mount_name;
276   const char* storage_name;
277
278   xbt_dict_foreach(dict,cursor,mount_name,storage_name) {
279     jname = env->NewStringUTF(storage_name);
280     jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname);
281     env->SetObjectArrayElement(jtable, index, jstorage);
282     index++;
283   }
284   xbt_dict_free(&dict);
285   return jtable;
286 }
287
288 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost)
289 {
290   msg_host_t host = jhost_get_native(env, jhost);
291
292   if (!host) {
293     jxbt_throw_notbound(env, "host", jhost);
294     return 0;
295   }
296   jobjectArray jtable;
297
298   xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host);
299   int count = xbt_dynar_length(dyn);
300   jclass cls = jxbt_get_class(env, "java/lang/String");
301   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
302   int index;
303   char *storage_name;
304   jstring jstorage_name;
305   for (index = 0; index < count; index++) {
306     storage_name = xbt_dynar_get_as(dyn,index,char*);
307     jstorage_name = env->NewStringUTF(storage_name);
308     env->SetObjectArrayElement(jtable, index, jstorage_name);
309   }
310
311   return jtable;
312 }
313
314 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost)
315 {
316   msg_host_t host = jhost_get_native(env, jhost);
317
318   if (!host) {
319     jxbt_throw_notbound(env, "host", jhost);
320     return 0;
321   }
322   return (jobjectArray)MSG_host_get_storage_content(host);
323 }
324
325 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
326 {
327   int index;
328   jobjectArray jtable;
329   jobject jhost;
330   jstring jname;
331   msg_host_t host;
332
333   xbt_dynar_t table =  MSG_hosts_as_dynar();
334   int count = xbt_dynar_length(table);
335
336   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
337
338   if (!cls) {
339     return nullptr;
340   }
341
342   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
343
344   if (!jtable) {
345     jxbt_throw_jni(env, "Hosts table allocation failed");
346     return nullptr;
347   }
348
349   for (index = 0; index < count; index++) {
350     host = xbt_dynar_get_as(table,index,msg_host_t);
351     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
352
353     if (!jhost) {
354       jname = env->NewStringUTF(MSG_host_get_name(host));
355
356       jhost = Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
357       /* FIXME: leak of jname ? */
358     }
359
360     env->SetObjectArrayElement(jtable, index, jhost);
361   }
362   xbt_dynar_free(&table);
363   return jtable;
364 }
365
366 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname)
367 {
368   const char *name = env->GetStringUTFChars((jstring) jname, 0);
369   MSG_mailbox_set_async(name);
370   env->ReleaseStringUTFChars((jstring) jname, name);
371 }
372
373 #include "simgrid/plugins/energy.h"
374 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost)
375 {
376   msg_host_t host = jhost_get_native(env, jhost);
377
378   if (!host) {
379     jxbt_throw_notbound(env, "host", jhost);
380     return 0;
381   }
382
383   return MSG_host_get_consumed_energy(host);
384 }