Logo AND Algorithmique Numérique Distribuée

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