Logo AND Algorithmique Numérique Distribuée

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