Logo AND Algorithmique Numérique Distribuée

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