Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / src / bindings / java / jmsg_process.cpp
1 /* Functions related to the java process 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/ex.hpp>
10
11 #include "jmsg_process.h"
12
13 #include "jmsg.h"
14 #include "jmsg_host.h"
15 #include "jxbt_utilities.h"
16 #include "JavaContext.hpp"
17
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
19
20 extern "C" {
21
22 jfieldID jprocess_field_Process_bind;
23 jfieldID jprocess_field_Process_host;
24 jfieldID jprocess_field_Process_killTime;
25 jfieldID jprocess_field_Process_id;
26 jfieldID jprocess_field_Process_name;
27 jfieldID jprocess_field_Process_pid;
28 jfieldID jprocess_field_Process_ppid;
29
30 JNIEXPORT void JNICALL
31 Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject jprocess) {
32
33 }
34
35 jobject native_to_java_process(msg_process_t process)
36 {
37   simgrid::kernel::context::JavaContext* context = (simgrid::kernel::context::JavaContext*) MSG_process_get_smx_ctx(process);
38   return context->jprocess;
39 }
40
41 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
42 {
43   return env->NewGlobalRef(jprocess);
44 }
45
46 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
47 {
48   env->DeleteGlobalRef(jprocess);
49 }
50
51 void jprocess_join(jobject jprocess, JNIEnv * env)
52 {
53   msg_process_t process = jprocess_to_native_process(jprocess,env);
54   simgrid::kernel::context::JavaContext* context = (simgrid::kernel::context::JavaContext*) MSG_process_get_smx_ctx(process);
55   xbt_os_thread_join(context->thread,nullptr);
56 }
57
58 msg_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
59 {
60   return (msg_process_t)(intptr_t)env->GetLongField(jprocess, jprocess_field_Process_bind);
61 }
62
63 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
64 {
65   env->SetLongField(jprocess, jprocess_field_Process_bind, (intptr_t)process);
66 }
67
68 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
69 {
70   return (intptr_t)env->GetLongField(jprocess, jprocess_field_Process_id);
71 }
72
73 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
74 {
75   jstring jname = (jstring) env->GetObjectField(jprocess, jprocess_field_Process_name);
76   return (jstring) env->NewGlobalRef(jname);
77 }
78
79 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
80 {
81   jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
82
83   if (!id)
84     return JNI_FALSE;
85
86   return env->GetLongField(jprocess, id) ? JNI_TRUE : JNI_FALSE;
87 }
88
89 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
90   jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
91
92   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
93   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
94   jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
95   jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
96   jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
97   jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
98   jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
99   if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name ||
100       !jprocess_field_Process_pid || !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
101     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
102   }
103 }
104
105 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv * env, jobject jprocess_arg, jobject jhostname)
106 {
107   jobject jprocess;             /* the global reference to the java process instance    */
108   jstring jname;                /* the name of the java process instance                */
109   const char *name;             /* the C name of the process                            */
110   const char *hostname;
111   msg_process_t process;          /* the native process to create                         */
112   msg_host_t host;                /* Where that process lives */
113
114   hostname = env->GetStringUTFChars((jstring) jhostname, 0);
115
116   /* get the name of the java process */
117   jname = jprocess_get_name(jprocess_arg, env);
118   if (!jname) {
119     jxbt_throw_null(env,
120             xbt_strdup("Internal error: Process name cannot be nullptr"));
121     return;
122   }
123
124   /* bind/retrieve the msg host */
125   host = MSG_host_by_name(hostname);
126
127   if (!(host)) {    /* not bound */
128     jxbt_throw_host_not_found(env, hostname);
129     return;
130   }
131
132   /* create a global java process instance */
133   jprocess = jprocess_new_global_ref(jprocess_arg, env);
134   if (!jprocess) {
135     jxbt_throw_jni(env, "Can't get a global ref to the java process");
136     return;
137   }
138
139   /* build the C name of the process */
140   name = env->GetStringUTFChars(jname, 0);
141   name = xbt_strdup(name);
142
143   /* Retrieve the kill time from the process */
144   jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
145   /* Actually build the MSG process */
146   process = MSG_process_create_with_environment(name, [](int argc, char** argv) -> int {
147               smx_actor_t process = SIMIX_process_self();
148               // This is the jprocess passed as environment.
149               // It would be simplet if we could use a closure.
150               jobject jprocess = (jobject) MSG_process_get_data(process);
151               simgrid::kernel::context::java_main_jprocess(jprocess);
152               return 0;
153             }, jprocess,
154             host,
155             /*argc, argv, properties*/
156             0, nullptr, nullptr);
157   MSG_process_set_kill_time(process, (double)jkill);
158   /* bind the java process instance to the native process */
159   jprocess_bind(jprocess, process, env);
160
161   /* release our reference to the process name (variable name becomes invalid) */
162   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
163   //env->ReleaseStringUTFChars(jname, name);
164   env->ReleaseStringUTFChars((jstring) jhostname, hostname);
165
166   /* sets the PID and the PPID of the process */
167   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
168   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
169   /* sets the Host of the process */
170   jobject jhost = Java_org_simgrid_msg_Host_getByName(env,nullptr, (jstring)jhostname);
171
172   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
173 }
174
175 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls, jint jresetPID)
176 {
177   return (jint) MSG_process_killall((int) jresetPID);
178 }
179
180 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint PID)
181 {
182   msg_process_t process = MSG_process_from_PID(PID);
183
184   if (!process) {
185     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
186     return nullptr;
187   }
188
189   jobject jprocess = native_to_java_process(process);
190
191   if (!jprocess) {
192     jxbt_throw_jni(env, "get process failed");
193     return nullptr;
194   }
195
196   return jprocess;
197 }
198
199 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
200   msg_process_t process = jprocess_to_native_process(jprocess, env);
201
202   if (!process) {
203     jxbt_throw_notbound(env, "process", jprocess);
204     return nullptr;
205   }
206   const char *name = env->GetStringUTFChars((jstring)jname, 0);
207
208   const char *property = MSG_process_get_property_value(process, name);
209   if (!property) {
210     return nullptr;
211   }
212
213   jobject jproperty = env->NewStringUTF(property);
214
215   env->ReleaseStringUTFChars((jstring)jname, name);
216
217   return jproperty;
218 }
219
220 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
221 {
222   msg_process_t process = MSG_process_self();
223   jobject jprocess;
224
225   if (!process) {
226     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
227     return nullptr;
228   }
229
230   jprocess = native_to_java_process(process);
231
232   if (!jprocess)
233     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
234
235   return jprocess;
236 }
237
238 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
239 {
240   msg_process_t process = jprocess_to_native_process(jprocess, env);
241
242   if (!process) {
243     jxbt_throw_notbound(env, "process", jprocess);
244     return;
245   }
246
247   /* try to suspend the process */
248   msg_error_t rv = MSG_process_suspend(process);
249
250   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
251 }
252
253 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
254 {
255   msg_process_t process = jprocess_to_native_process(jprocess, env);
256
257   if (!process) {
258     jxbt_throw_notbound(env, "process", jprocess);
259     return;
260   }
261
262   /* try to resume the process */
263   msg_error_t rv = MSG_process_resume(process);
264
265   jxbt_check_res("MSG_process_resume()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
266 }
267 JNIEXPORT void
268 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
269   msg_process_t process = jprocess_to_native_process(jprocess, env);
270   xbt_ex_t e;
271
272   int auto_restart = jauto_restart == JNI_TRUE ? 1 : 0;
273
274   if (!process) {
275     jxbt_throw_notbound(env, "process", jprocess);
276     return;
277   }
278
279   try {
280     MSG_process_auto_restart_set(process,auto_restart);
281   }
282   catch (xbt_ex& e) {
283     // Nothing to do
284   }
285 }
286
287 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
288   msg_process_t process = jprocess_to_native_process(jprocess, env);
289   xbt_ex_t e;
290
291   if (!process) {
292     jxbt_throw_notbound(env, "process", jprocess);
293     return;
294   }
295
296   try {
297     MSG_process_restart(process);
298   }
299   catch (xbt_ex& e) {
300     // Nothing to do
301   }
302
303 }
304 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
305 {
306   msg_process_t process = jprocess_to_native_process(jprocess, env);
307
308   if (!process) {
309     jxbt_throw_notbound(env, "process", jprocess);
310     return 0;
311   }
312
313   /* true is the process is suspended, false otherwise */
314   return (jboolean) MSG_process_is_suspended(process);
315 }
316
317 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
318  {
319   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
320   msg_error_t rv;
321   rv = MSG_process_sleep(time);
322   if (rv != MSG_OK) {
323     XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
324
325     //jmsg_throw_status(env,rv);
326
327     // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
328     // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
329     // is a cancelled_error, see bindings/java/smx_context_java.c, function void smx_ctx_java_stop(smx_context_t context) and src/msg/msg_gos.c
330     // function  msg_error_t MSG_process_sleep(double nb_sec)
331
332     jxbt_throw_host_failure(env,nullptr);
333   }
334 }
335
336 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
337 {
338   msg_error_t rv;
339   rv = MSG_process_sleep((double)jseconds);
340   if (env->ExceptionOccurred())
341     return;
342   if (rv != MSG_OK) {
343     XBT_DEBUG("Status NOK");
344     jmsg_throw_status(env,rv);
345   }
346 }
347
348 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
349 {
350   /* get the native instances from the java ones */
351   msg_process_t process = jprocess_to_native_process(jprocess, env);
352   if (!process) {
353     jxbt_throw_notbound(env, "process", jprocess);
354     return;
355   }
356
357   MSG_process_kill(process);
358 }
359
360 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
361 {
362   msg_process_t process = jprocess_to_native_process(jprocess, env);
363
364   if (!process) {
365     jxbt_throw_notbound(env, "process", jprocess);
366     return;
367   }
368
369   msg_host_t host = jhost_get_native(env, jhost);
370
371   if (!host) {
372     jxbt_throw_notbound(env, "host", jhost);
373     return;
374   }
375
376   /* try to change the host of the process */
377   msg_error_t rv = MSG_process_migrate(process, host);
378   if (rv != MSG_OK) {
379     jmsg_throw_status(env,rv);
380     return;
381   }
382   /* change the host java side */
383   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
384 }
385
386 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
387   msg_process_t process = jprocess_to_native_process(jprocess, env);
388   MSG_process_set_kill_time(process, (double)jkilltime);
389 }
390
391 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
392   return (jint) MSG_process_get_number();
393 }
394
395 } // extern C