Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3a6ba91c3b9d11fc63df85741d057bf8d753c9b4
[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 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
55   jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
56   xbt_assert(jprocess_class_Process, "Native initialization of msg/Process failed. Please report that bug");
57
58   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
59   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
60   jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
61   jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
62   jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
63   jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
64   xbt_assert(jprocess_field_Process_name && jprocess_field_Process_pid && jprocess_field_Process_ppid &&
65                  jprocess_field_Process_host,
66              "Native initialization of msg/Process failed. Please report that bug");
67 }
68
69 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv* env, jobject jprocess_arg, jobject jhost)
70 {
71   /* create a global java process instance */
72   jobject jprocess = jprocess_ref(jprocess_arg, env);
73
74   /* Actually build the MSG process */
75   jstring jname         = (jstring)env->GetObjectField(jprocess, jprocess_field_Process_name);
76   const char* name      = env->GetStringUTFChars(jname, 0);
77   msg_process_t process =
78       MSG_process_create_from_stdfunc(name, [jprocess]() { simgrid::kernel::context::java_main_jprocess(jprocess); },
79                                       /*data*/ nullptr, jhost_get_native(env, jhost), /* properties*/ nullptr);
80   env->ReleaseStringUTFChars(jname, name);
81
82   /* bind the java process instance to the native process */
83   jprocess_bind(jprocess, process, env);
84
85   /* Retrieve the kill time from the process */
86   jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
87   MSG_process_set_kill_time(process, (double)jkill);
88
89   /* sets the PID and the PPID of the process */
90   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
91   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
92 }
93
94 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls, jint jresetPID)
95 {
96   return (jint) MSG_process_killall((int) jresetPID);
97 }
98
99 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
100 {
101   msg_process_t process = MSG_process_from_PID(pid);
102
103   if (not process) {
104     jxbt_throw_process_not_found(env, bprintf("PID = %d",static_cast<int>(pid)));
105     return nullptr;
106   }
107
108   jobject jprocess = jprocess_from_native(process);
109
110   if (not jprocess) {
111     jxbt_throw_jni(env, "get process failed");
112     return nullptr;
113   }
114
115   return jprocess;
116 }
117
118 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
119   msg_process_t process = jprocess_to_native(jprocess, env);
120
121   if (not process) {
122     jxbt_throw_notbound(env, "process", jprocess);
123     return nullptr;
124   }
125   const char *name = env->GetStringUTFChars((jstring)jname, 0);
126
127   const char *property = MSG_process_get_property_value(process, name);
128   if (not property)
129     return nullptr;
130
131   jobject jproperty = env->NewStringUTF(property);
132
133   env->ReleaseStringUTFChars((jstring)jname, name);
134
135   return jproperty;
136 }
137
138 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
139 {
140   jobject jprocess = jprocess_from_native(MSG_process_self());
141   if (not jprocess)
142     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
143
144   return jprocess;
145 }
146
147 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
148 {
149   msg_process_t process = jprocess_to_native(jprocess, env);
150
151   if (not process) {
152     jxbt_throw_notbound(env, "process", jprocess);
153     return;
154   }
155
156   /* try to suspend the process */
157   msg_error_t rv = MSG_process_suspend(process);
158
159   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
160 }
161
162 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
163 {
164   msg_process_t process = jprocess_to_native(jprocess, env);
165
166   if (not process) {
167     jxbt_throw_notbound(env, "process", jprocess);
168     return;
169   }
170
171   /* try to resume the process */
172   msg_error_t res = MSG_process_resume(process);
173   jxbt_check_res("MSG_process_resume()", res, MSG_OK, bprintf("unexpected error , please report this bug"));
174 }
175
176 JNIEXPORT void
177 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
178
179   msg_process_t process = jprocess_to_native(jprocess, env);
180   if (not process) {
181     jxbt_throw_notbound(env, "process", jprocess);
182     return;
183   }
184
185   MSG_process_auto_restart_set(process, (jauto_restart == JNI_TRUE));
186 }
187
188 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
189   msg_process_t process = jprocess_to_native(jprocess, env);
190   xbt_ex_t e;
191
192   if (not process) {
193     jxbt_throw_notbound(env, "process", jprocess);
194     return;
195   }
196
197   MSG_process_restart(process);
198 }
199
200 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
201 {
202   msg_process_t process = jprocess_to_native(jprocess, env);
203
204   if (not process) {
205     jxbt_throw_notbound(env, "process", jprocess);
206     return 0;
207   }
208
209   /* true is the process is suspended, false otherwise */
210   return (jboolean) MSG_process_is_suspended(process);
211 }
212
213 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
214  {
215   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
216   msg_error_t rv;
217   rv = MSG_process_sleep(time);
218   if (rv != MSG_OK) {
219     XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
220
221     //jmsg_throw_status(env,rv);
222
223     // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
224     // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
225     // 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
226     // function  msg_error_t MSG_process_sleep(double nb_sec)
227
228     jxbt_throw_host_failure(env, "");
229   }
230 }
231
232 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
233 {
234   msg_error_t rv;
235   rv = MSG_process_sleep((double)jseconds);
236   if (env->ExceptionOccurred())
237     return;
238   if (rv != MSG_OK) {
239     XBT_DEBUG("Status NOK");
240     jmsg_throw_status(env,rv);
241   }
242 }
243
244 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
245 {
246   /* get the native instances from the java ones */
247   msg_process_t process = jprocess_to_native(jprocess, env);
248   if (not process) {
249     jxbt_throw_notbound(env, "process", jprocess);
250     return;
251   }
252   try {
253     MSG_process_kill(process);
254   } catch (xbt_ex& ex) {
255     XBT_VERB("Process %s just committed a suicide", MSG_process_get_name(process));
256     xbt_assert(process == MSG_process_self(),
257                "Killing a process should not raise an exception if it's not a suicide. Please report that bug.");
258   }
259 }
260
261 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
262 {
263   msg_process_t process = jprocess_to_native(jprocess, env);
264
265   if (not process) {
266     jxbt_throw_notbound(env, "process", jprocess);
267     return;
268   }
269
270   msg_host_t host = jhost_get_native(env, jhost);
271
272   if (not host) {
273     jxbt_throw_notbound(env, "host", jhost);
274     return;
275   }
276
277   /* try to change the host of the process */
278   msg_error_t rv = MSG_process_migrate(process, host);
279   if (rv != MSG_OK) {
280     jmsg_throw_status(env,rv);
281     return;
282   }
283   /* change the host java side */
284   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
285 }
286
287 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
288 {
289   MSG_process_yield();
290 }
291
292 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
293   msg_process_t process = jprocess_to_native(jprocess, env);
294   MSG_process_set_kill_time(process, (double)jkilltime);
295 }
296
297 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
298   return (jint) MSG_process_get_number();
299 }
300
301 SG_END_DECL()