Logo AND Algorithmique Numérique Distribuée

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