Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Lowercase PID in function name (2/2 - public part).
[simgrid.git] / src / bindings / java / jmsg_host.cpp
1 /* Functions related to the java host instances.                            */
2
3 /* Copyright (c) 2007-2021. 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
13 #include "JavaContext.hpp"
14 #include "jmsg.hpp"
15 #include "jmsg_host.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, nullptr);
62   /* get the host by name       (the hosts are created during the grid resolution) */
63   sg_host_t host = sg_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, 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   sg_host_t host = sg_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     /* instantiate 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, 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   sg_host_t host = jhost_get_native(env, jhost);
139   sg_host_turn_on(host);
140 }
141
142 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
143   sg_host_t host = jhost_get_native(env, jhost);
144   if (not simgrid::ForcefulKillException::try_n_catch([host]() { sg_host_turn_off(host); }))
145     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Host turned off");
146 }
147
148 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
149   return (jint)sg_host_count();
150 }
151
152 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env, jobject jhost) {
153   const_sg_host_t host = jhost_get_native(env, jhost);
154
155   if (not host) {
156     jxbt_throw_notbound(env, "host", jhost);
157     return -1;
158   }
159
160   return (jdouble)sg_host_get_speed(host);
161 }
162
163 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCoreNumber(JNIEnv * env, jobject jhost) {
164   const_sg_host_t host = jhost_get_native(env, jhost);
165
166   if (not host) {
167     jxbt_throw_notbound(env, "host", jhost);
168     return -1;
169   }
170
171   return (jdouble)sg_host_core_count(host);
172 }
173
174 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getProperty(JNIEnv *env, jobject jhost, jobject jname) {
175   const_sg_host_t host = jhost_get_native(env, jhost);
176
177   if (not host) {
178     jxbt_throw_notbound(env, "host", jhost);
179     return nullptr;
180   }
181   const char* name = env->GetStringUTFChars((jstring)jname, nullptr);
182
183   const char* property = sg_host_get_property_value(host, name);
184   if (not property) {
185     return nullptr;
186   }
187
188   jobject jproperty = env->NewStringUTF(property);
189
190   env->ReleaseStringUTFChars((jstring) jname, name);
191
192   return jproperty;
193 }
194
195 JNIEXPORT void JNICALL
196 Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, jobject jvalue) {
197   sg_host_t host = jhost_get_native(env, jhost);
198
199   if (not host) {
200     jxbt_throw_notbound(env, "host", jhost);
201     return;
202   }
203   const char* name       = env->GetStringUTFChars((jstring)jname, nullptr);
204   const char* value_java = env->GetStringUTFChars((jstring)jvalue, nullptr);
205   const char* value      = xbt_strdup(value_java);
206
207   sg_host_set_property_value(host, name, value);
208
209   env->ReleaseStringUTFChars((jstring) jvalue, value_java);
210   env->ReleaseStringUTFChars((jstring) jname, name);
211 }
212
213 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Host_isOn(JNIEnv * env, jobject jhost)
214 {
215   const_sg_host_t host = jhost_get_native(env, jhost);
216
217   if (not host) {
218     jxbt_throw_notbound(env, "host", jhost);
219     return 0;
220   }
221
222   return (jboolean)sg_host_is_on(host);
223 }
224
225 JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
226 {
227   sg_host_t* table = sg_host_list();
228   int count        = sg_host_count();
229
230   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
231   if (not cls)
232     return nullptr;
233
234   jobjectArray jtable = env->NewObjectArray((jsize)count, cls, nullptr);
235
236   if (not jtable) {
237     jxbt_throw_jni(env, "Hosts table allocation failed");
238     return nullptr;
239   }
240
241   for (int index = 0; index < count; index++) {
242     auto jhost = static_cast<jobject>(table[index]->extension(JAVA_HOST_LEVEL));
243
244     if (not jhost) {
245       jstring jname = env->NewStringUTF(table[index]->get_cname());
246       jhost         = Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
247     }
248
249     env->SetObjectArrayElement(jtable, index, jhost);
250   }
251   xbt_free(table);
252   return jtable;
253 }
254
255 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname)
256 {
257   const char* name = env->GetStringUTFChars((jstring)jname, nullptr);
258   sg_mailbox_set_receiver(name);
259   env->ReleaseStringUTFChars((jstring) jname, name);
260 }
261
262 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_updateAllEnergyConsumptions(JNIEnv* env, jclass cls)
263 {
264   sg_host_energy_update_all();
265 }
266
267 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost)
268 {
269   const_sg_host_t host = jhost_get_native(env, jhost);
270
271   if (not host) {
272     jxbt_throw_notbound(env, "host", jhost);
273     return 0;
274   }
275
276   return sg_host_get_consumed_energy(host);
277 }
278
279 JNIEXPORT void JNICALL Java_org_simgrid_msg_Host_setPstate(JNIEnv* env, jobject jhost, jint pstate)
280 {
281   sg_host_t host = jhost_get_native(env, jhost);
282   sg_host_set_pstate(host, pstate);
283 }
284 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getPstate(JNIEnv* env, jobject jhost)
285 {
286   const_sg_host_t host = jhost_get_native(env, jhost);
287   return sg_host_get_pstate(host);
288 }
289 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Host_getPstatesCount(JNIEnv* env, jobject jhost)
290 {
291   const_sg_host_t host = jhost_get_native(env, jhost);
292   return sg_host_get_nb_pstates(host);
293 }
294 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCurrentPowerPeak(JNIEnv* env, jobject jhost)
295 {
296   const_sg_host_t host = jhost_get_native(env, jhost);
297   return sg_host_get_speed(host);
298 }
299 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getPowerPeakAt(JNIEnv* env, jobject jhost, jint pstate)
300 {
301   const_sg_host_t host = jhost_get_native(env, jhost);
302   return sg_host_get_pstate_speed(host, pstate);
303 }
304
305 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getLoad(JNIEnv* env, jobject jhost)
306 {
307   const_sg_host_t host = jhost_get_native(env, jhost);
308   return sg_host_get_load(host);
309 }
310
311 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getCurrentLoad (JNIEnv *env, jobject jhost)
312 {
313   const_sg_host_t host = jhost_get_native(env, jhost);
314
315   if (not host) {
316     jxbt_throw_notbound(env, "host", jhost);
317     return 0;
318   }
319
320   return sg_host_get_current_load(host);
321 }
322
323 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getComputedFlops (JNIEnv *env, jobject jhost)
324 {
325   const_sg_host_t host = jhost_get_native(env, jhost);
326
327   if (not host) {
328     jxbt_throw_notbound(env, "host", jhost);
329     return 0;
330   }
331
332   return sg_host_get_computed_flops(host);
333 }
334
335 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getAvgLoad (JNIEnv *env, jobject jhost)
336 {
337   const_sg_host_t host = jhost_get_native(env, jhost);
338
339   if (not host) {
340     jxbt_throw_notbound(env, "host", jhost);
341     return 0;
342   }
343
344   return sg_host_get_avg_load(host);
345 }