Logo AND Algorithmique Numérique Distribuée

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