Logo AND Algorithmique Numérique Distribuée

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