Logo AND Algorithmique Numérique Distribuée

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