Logo AND Algorithmique Numérique Distribuée

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