Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sed -i -e 's/\t/ /g' [sources] Please people, stop using tabs
[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 =
36     (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
37   return context->jprocess;
38 }
39
40 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
41 {
42   return env->NewGlobalRef(jprocess);
43 }
44
45 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
46 {
47   env->DeleteGlobalRef(jprocess);
48 }
49
50 void jprocess_join(jobject jprocess, JNIEnv * env)
51 {
52   msg_process_t process = jprocess_to_native_process(jprocess,env);
53   simgrid::java::JavaContext* context =
54     (simgrid::java::JavaContext*) 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(jprocess,
62                                                   jprocess_field_Process_bind);
63 }
64
65 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
66 {
67   env->SetLongField(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(jprocess, jprocess_field_Process_id);
75 }
76
77 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
78 {
79   jstring jname = (jstring) env->GetObjectField(jprocess, jprocess_field_Process_name);
80   return (jstring) env->NewGlobalRef(jname);
81 }
82
83 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
84 {
85   jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
86
87   if (!id)
88     return JNI_FALSE;
89
90   return env->GetLongField(jprocess, id) ? JNI_TRUE : JNI_FALSE;
91 }
92 JNIEXPORT void JNICALL
93 Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
94   jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
95
96   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
97   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
98   jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
99   jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
100   jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
101   jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
102   jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
103   if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name || !jprocess_field_Process_pid ||
104       !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
105     jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
106   }
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((jstring) 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_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(jname, 0);
150   name = xbt_strdup(name);
151
152   /* Retrieve the kill time from the process */
153   jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
154   /* Actually build the MSG process */
155   process = MSG_process_create_with_environment(name,
156             [](int argc, char** argv) -> int {
157               smx_process_t process = SIMIX_process_self();
158               // This is the jprocess passed as environment.
159               // It would be simplet if we could use a closure.
160               jobject jprocess = (jobject) MSG_process_get_data(process);
161               simgrid::java::java_main_jprocess(jprocess);
162               return 0;
163             }, jprocess,
164             host,
165             /*argc, argv, properties*/
166             0, NULL, NULL);
167   MSG_process_set_kill_time(process, (double)jkill);
168   /* bind the java process instance to the native process */
169   jprocess_bind(jprocess, process, env);
170
171   /* release our reference to the process name (variable name becomes invalid) */
172   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
173   //env->ReleaseStringUTFChars(jname, name);
174   env->ReleaseStringUTFChars((jstring) jhostname, hostname);
175
176   /* sets the PID and the PPID of the process */
177   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
178   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
179   /* sets the Host of the process */
180   jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL, (jstring)jhostname);
181
182   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
183 }
184
185 JNIEXPORT jint JNICALL
186 Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls,
187                                      jint jresetPID)
188 {
189   return (jint) MSG_process_killall((int) jresetPID);
190 }
191
192 JNIEXPORT jobject JNICALL
193 Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls,
194                                      jint PID)
195 {
196   msg_process_t process = MSG_process_from_PID(PID);
197
198   if (!process) {
199     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
200     return NULL;
201   }
202
203   jobject jprocess = native_to_java_process(process);
204
205   if (!jprocess) {
206     jxbt_throw_jni(env, "get process failed");
207     return NULL;
208   }
209
210   return jprocess;
211 }
212 JNIEXPORT jobject JNICALL
213 Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
214   msg_process_t process = jprocess_to_native_process(jprocess, env);
215
216   if (!process) {
217     jxbt_throw_notbound(env, "process", jprocess);
218     return NULL;
219   }
220   const char *name = env->GetStringUTFChars((jstring)jname, 0);
221
222   const char *property = MSG_process_get_property_value(process, name);
223   if (!property) {
224     return NULL;
225   }
226
227   jobject jproperty = env->NewStringUTF(property);
228
229   env->ReleaseStringUTFChars((jstring)jname, name);
230
231   return jproperty;
232 }
233 JNIEXPORT jobject JNICALL
234 Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
235 {
236   msg_process_t process = MSG_process_self();
237   jobject jprocess;
238
239   if (!process) {
240     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
241     return NULL;
242   }
243
244   jprocess = native_to_java_process(process);
245
246   if (!jprocess)
247     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
248
249   return jprocess;
250 }
251
252 JNIEXPORT void JNICALL
253 Java_org_simgrid_msg_Process_suspend(JNIEnv * env,
254                                    jobject jprocess)
255 {
256   msg_process_t process = jprocess_to_native_process(jprocess, env);
257
258   if (!process) {
259     jxbt_throw_notbound(env, "process", jprocess);
260     return;
261   }
262
263   /* try to suspend the process */
264   msg_error_t rv = MSG_process_suspend(process);
265
266   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
267                  bprintf("unexpected error , please report this bug"));
268
269 }
270
271 JNIEXPORT void JNICALL
272 Java_org_simgrid_msg_Process_resume(JNIEnv * env,
273                                      jobject jprocess)
274 {
275   msg_process_t process = jprocess_to_native_process(jprocess, env);
276
277   if (!process) {
278     jxbt_throw_notbound(env, "process", jprocess);
279     return;
280   }
281
282   /* try to resume the process */
283   msg_error_t rv = MSG_process_resume(process);
284
285   jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
286                  bprintf("unexpected error , please report this bug"));
287 }
288 JNIEXPORT void JNICALL
289 Java_org_simgrid_msg_Process_setAutoRestart
290     (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
291   msg_process_t process = jprocess_to_native_process(jprocess, env);
292   xbt_ex_t e;
293
294   int auto_restart = jauto_restart == JNI_TRUE ? 1 : 0;
295
296   if (!process) {
297     jxbt_throw_notbound(env, "process", jprocess);
298     return;
299   }
300
301   TRY {
302     MSG_process_auto_restart_set(process,auto_restart);
303   }
304   CATCH (e) {
305     xbt_ex_free(e);
306   }
307 }
308 JNIEXPORT void JNICALL
309 Java_org_simgrid_msg_Process_restart
310     (JNIEnv *env, jobject jprocess) {
311   msg_process_t process = jprocess_to_native_process(jprocess, env);
312   xbt_ex_t e;
313
314   if (!process) {
315     jxbt_throw_notbound(env, "process", jprocess);
316     return;
317   }
318
319   TRY {
320     MSG_process_restart(process);
321   }
322   CATCH (e) {
323     xbt_ex_free(e);
324   }
325
326 }
327 JNIEXPORT jboolean JNICALL
328 Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env,
329                                          jobject jprocess)
330 {
331   msg_process_t process = jprocess_to_native_process(jprocess, env);
332
333   if (!process) {
334     jxbt_throw_notbound(env, "process", jprocess);
335     return 0;
336   }
337
338   /* true is the process is suspended, false otherwise */
339   return (jboolean) MSG_process_is_suspended(process);
340 }
341
342 JNIEXPORT void JNICALL
343 Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
344  {
345   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
346   msg_error_t rv;
347   rv = MSG_process_sleep(time);
348   if (rv != MSG_OK) {
349     XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
350
351     //jmsg_throw_status(env,rv);
352
353     // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
354     // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
355     // 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
356     // function  msg_error_t MSG_process_sleep(double nb_sec)
357
358     jxbt_throw_host_failure(env,NULL);
359   }
360 }
361 JNIEXPORT void JNICALL
362 Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
363                                      jdouble jseconds)
364 {
365   msg_error_t rv;
366   rv = MSG_process_sleep((double)jseconds);
367   if (env->ExceptionOccurred())
368     return;
369   if (rv != MSG_OK) {
370     XBT_DEBUG("Status NOK");
371     jmsg_throw_status(env,rv);
372   }
373 }
374
375 JNIEXPORT void JNICALL
376 Java_org_simgrid_msg_Process_kill(JNIEnv * env,
377                                   jobject jprocess)
378 {
379   /* get the native instances from the java ones */
380   msg_process_t process = jprocess_to_native_process(jprocess, env);
381   if (!process) {
382     jxbt_throw_notbound(env, "process", jprocess);
383     return;
384   }
385
386   MSG_process_kill(process);
387 }
388 JNIEXPORT void JNICALL
389 Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
390                                      jobject jprocess, jobject jhost)
391 {
392   msg_process_t process = jprocess_to_native_process(jprocess, env);
393
394   if (!process) {
395     jxbt_throw_notbound(env, "process", jprocess);
396     return;
397   }
398
399   msg_host_t host = jhost_get_native(env, jhost);
400
401   if (!host) {
402     jxbt_throw_notbound(env, "host", jhost);
403     return;
404   }
405
406   /* try to change the host of the process */
407   msg_error_t rv = MSG_process_migrate(process, host);
408   if (rv != MSG_OK) {
409     jmsg_throw_status(env,rv);
410     return;
411   }
412   /* change the host java side */
413   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
414 }
415 JNIEXPORT void JNICALL
416 Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
417   msg_process_t process = jprocess_to_native_process(jprocess, env);
418   MSG_process_set_kill_time(process, (double)jkilltime);
419 }
420
421 JNIEXPORT jint JNICALL
422 Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
423   return (jint) MSG_process_get_number();
424 }
425
426 }