Logo AND Algorithmique Numérique Distribuée

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