Logo AND Algorithmique Numérique Distribuée

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