Logo AND Algorithmique Numérique Distribuée

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