Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
java: kill obscure NativeException
[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 host->cname();
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   xbt_assert(class_Host && jhost_field_Host_name && jhost_method_Host_constructor && jhost_field_Host_bind,
70              "Native initialization of msg/Host failed. Please report that bug");
71 }
72
73 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, jstring jname) {
74
75   /* get the C string from the java string */
76   if (jname == nullptr) {
77     jxbt_throw_null(env,bprintf("No host can have a null name"));
78     return nullptr;
79   }
80   const char *name = env->GetStringUTFChars(jname, 0);
81   /* get the host by name       (the hosts are created during the grid resolution) */
82   msg_host_t host = MSG_host_by_name(name);
83
84   if (!host) {                  /* invalid name */
85     jxbt_throw_host_not_found(env, name);
86     env->ReleaseStringUTFChars(jname, name);
87     return nullptr;
88   }
89   env->ReleaseStringUTFChars(jname, name);
90
91   if (!host->extension(JAVA_HOST_LEVEL)) {       /* native host not associated yet with java host */
92     /* Instantiate a new java host */
93     jobject jhost = jhost_new_instance(env);
94
95     if (!jhost) {
96       jxbt_throw_jni(env, "java host instantiation failed");
97       return nullptr;
98     }
99
100     /* get a global reference to the newly created host */
101     jhost = jhost_ref(env, jhost);
102
103     if (!jhost) {
104       jxbt_throw_jni(env, "new global ref allocation failed");
105       return nullptr;
106     }
107     /* Sets the java host name */
108     env->SetObjectField(jhost, jhost_field_Host_name, jname);
109     /* bind the java host and the native host */
110     jhost_bind(jhost, host, env);
111
112     /* the native host data field is set with the global reference to the java host returned by this function */
113     host->extension_set(JAVA_HOST_LEVEL, (void *)jhost);
114   }
115
116   /* return the global reference to the java host instance */
117   return (jobject) host->extension(JAVA_HOST_LEVEL);
118 }
119
120 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
121   jobject jhost;
122
123   msg_host_t host = MSG_host_self();
124
125   if (!host->extension(JAVA_HOST_LEVEL)) {
126     /* the native host not yet associated with the java host instance */
127
128     /* instanciate a new java host instance */
129     jhost = jhost_new_instance(env);
130
131     if (!jhost) {
132       jxbt_throw_jni(env, "java host instantiation failed");
133       return nullptr;
134     }
135
136     /* get a global reference to the newly created host */
137     jhost = jhost_ref(env, jhost);
138
139     if (!jhost) {
140       jxbt_throw_jni(env, "global ref allocation failed");
141       return nullptr;
142     }
143     /* Sets the host name */
144     jobject jname = env->NewStringUTF(host->cname());
145     env->SetObjectField(jhost, jhost_field_Host_name, jname);
146     /* Bind & store it */
147     jhost_bind(jhost, host, env);
148     host->extension_set(JAVA_HOST_LEVEL, (void *) jhost);
149   } else {
150     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
151   }
152
153   return jhost;
154 }
155
156 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) {
157   msg_host_t host = jhost_get_native(env, jhost);
158   MSG_host_on(host);
159 }
160
161 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
162   msg_host_t host = jhost_get_native(env, jhost);
163   MSG_host_off(host); 
164 }
165
166 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
167   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
168   int nb_host = xbt_dynar_length(hosts);
169   xbt_dynar_free(&hosts);
170   return (jint) nb_host;
171 }
172
173 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env, jobject jhost) {
174   msg_host_t host = jhost_get_native(env, jhost);
175
176   if (!host) {
177     jxbt_throw_notbound(env, "host", jhost);
178     return -1;
179   }
180
181   return (jdouble) MSG_host_get_speed(host);
182 }
183
184 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env, jobject jhost) {
185   msg_host_t host = jhost_get_native(env, jhost);
186
187   if (!host) {
188     jxbt_throw_notbound(env, "host", jhost);
189     return -1;
190   }
191
192   return (jdouble) MSG_host_get_core_number(host);
193 }
194
195 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
196   msg_host_t host = jhost_get_native(env, jhost);
197
198   if (!host) {
199     jxbt_throw_notbound(env, "host", jhost);
200     return nullptr;
201   }
202   const char *name = env->GetStringUTFChars((jstring) jname, 0);
203
204   const char *property = MSG_host_get_property_value(host, name);
205   if (!property) {
206     return nullptr;
207   }
208
209   jobject jproperty = env->NewStringUTF(property);
210
211   env->ReleaseStringUTFChars((jstring) jname, name);
212
213   return jproperty;
214 }
215
216 JNIEXPORT void JNICALL
217 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
218   msg_host_t host = jhost_get_native(env, jhost);
219
220   if (!host) {
221     jxbt_throw_notbound(env, "host", jhost);
222     return;
223   }
224   const char *name = env->GetStringUTFChars((jstring) jname, 0);
225   const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0);
226   char *value = xbt_strdup(value_java);
227
228   MSG_host_set_property_value(host, name, value);
229
230   env->ReleaseStringUTFChars((jstring) jvalue, value_java);
231   env->ReleaseStringUTFChars((jstring) jname, name);
232 }
233
234 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost)
235 {
236   msg_host_t host = jhost_get_native(env, jhost);
237
238   if (!host) {
239     jxbt_throw_notbound(env, "host", jhost);
240     return 0;
241   }
242
243   return (jboolean) MSG_host_is_on(host);
244 }
245
246 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost)
247 {
248   msg_host_t host = jhost_get_native(env, jhost);
249   jobject jstorage;
250   jstring jname;
251
252   if (!host) {
253     jxbt_throw_notbound(env, "host", jhost);
254     return 0;
255   }
256
257   int index = 0;
258   jobjectArray jtable;
259   xbt_dict_t dict =  MSG_host_get_mounted_storage_list(host);
260   int count = xbt_dict_length(dict);
261   jclass cls = env->FindClass("org/simgrid/msg/Storage");
262
263   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
264
265   if (!jtable) {
266    jxbt_throw_jni(env, "Storages table allocation failed");
267    return nullptr;
268   }
269
270   xbt_dict_cursor_t cursor=nullptr;
271   const char* mount_name;
272   const char* storage_name;
273
274   xbt_dict_foreach(dict,cursor,mount_name,storage_name) {
275     jname = env->NewStringUTF(storage_name);
276     jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname);
277     env->SetObjectArrayElement(jtable, index, jstorage);
278     index++;
279   }
280   xbt_dict_free(&dict);
281   return jtable;
282 }
283
284 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost)
285 {
286   msg_host_t host = jhost_get_native(env, jhost);
287
288   if (!host) {
289     jxbt_throw_notbound(env, "host", jhost);
290     return 0;
291   }
292   jobjectArray jtable;
293
294   xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host);
295   int count = xbt_dynar_length(dyn);
296   jclass cls = jxbt_get_class(env, "java/lang/String");
297   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
298   int index;
299   char *storage_name;
300   jstring jstorage_name;
301   for (index = 0; index < count; index++) {
302     storage_name = xbt_dynar_get_as(dyn,index,char*);
303     jstorage_name = env->NewStringUTF(storage_name);
304     env->SetObjectArrayElement(jtable, index, jstorage_name);
305   }
306
307   return jtable;
308 }
309
310 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost)
311 {
312   msg_host_t host = jhost_get_native(env, jhost);
313
314   if (!host) {
315     jxbt_throw_notbound(env, "host", jhost);
316     return 0;
317   }
318   return (jobjectArray)MSG_host_get_storage_content(host);
319 }
320
321 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
322 {
323   int index;
324   jobjectArray jtable;
325   jobject jhost;
326   jstring jname;
327   msg_host_t host;
328
329   xbt_dynar_t table =  MSG_hosts_as_dynar();
330   int count = xbt_dynar_length(table);
331
332   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
333
334   if (!cls) {
335     return nullptr;
336   }
337
338   jtable = env->NewObjectArray((jsize) count, cls, nullptr);
339
340   if (!jtable) {
341     jxbt_throw_jni(env, "Hosts table allocation failed");
342     return nullptr;
343   }
344
345   for (index = 0; index < count; index++) {
346     host = xbt_dynar_get_as(table,index,msg_host_t);
347     jhost = (jobject) host->extension(JAVA_HOST_LEVEL);
348
349     if (!jhost) {
350       jname = env->NewStringUTF(host->cname());
351
352       jhost = Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
353       /* FIXME: leak of jname ? */
354     }
355
356     env->SetObjectArrayElement(jtable, index, jhost);
357   }
358   xbt_dynar_free(&table);
359   return jtable;
360 }
361
362 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname)
363 {
364   const char *name = env->GetStringUTFChars((jstring) jname, 0);
365   MSG_mailbox_set_async(name);
366   env->ReleaseStringUTFChars((jstring) jname, name);
367 }
368
369 #include "simgrid/plugins/energy.h"
370 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost)
371 {
372   msg_host_t host = jhost_get_native(env, jhost);
373
374   if (!host) {
375     jxbt_throw_notbound(env, "host", jhost);
376     return 0;
377   }
378
379   return MSG_host_get_consumed_energy(host);
380 }
381
382 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setPstate(JNIEnv* env, jobject jhost, jint pstate)
383 {
384   msg_host_t host = jhost_get_native(env, jhost);
385   MSG_host_set_pstate(host, pstate);
386 }
387 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getPstate(JNIEnv* env, jobject jhost)
388 {
389   msg_host_t host = jhost_get_native(env, jhost);
390   return MSG_host_get_pstate(host);
391 }
392 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getPstatesCount(JNIEnv* env, jobject jhost)
393 {
394   msg_host_t host = jhost_get_native(env, jhost);
395   return MSG_host_get_nb_pstates(host);
396 }
397 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCurrentPowerPeak(JNIEnv* env, jobject jhost)
398 {
399   msg_host_t host = jhost_get_native(env, jhost);
400   return MSG_host_get_current_power_peak(host);
401 }
402 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getPowerPeakAt(JNIEnv* env, jobject jhost, jint pstate)
403 {
404   msg_host_t host = jhost_get_native(env, jhost);
405   return MSG_host_get_power_peak_at(host, pstate);
406 }