Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7580858ef98763dda7ee62f09e18f222ac78fb6a
[simgrid.git] / src / bindings / java / jmsg_process.cpp
1 /* Functions related to the java process instances.                         */
2
3 /* Copyright (c) 2007-2018. 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 "jmsg_process.h"
9
10 #include "JavaContext.hpp"
11 #include "jmsg.hpp"
12 #include "jmsg_host.h"
13 #include "jxbt_utilities.hpp"
14 #include "simgrid/Exception.hpp"
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
17
18 jfieldID jprocess_field_Process_bind;
19 jfieldID jprocess_field_Process_host;
20 jfieldID jprocess_field_Process_killTime;
21 jfieldID jprocess_field_Process_name;
22 jfieldID jprocess_field_Process_pid;
23 jfieldID jprocess_field_Process_ppid;
24
25 jobject jprocess_from_native(msg_process_t process)
26 {
27   simgrid::kernel::context::JavaContext* context =
28       (simgrid::kernel::context::JavaContext*)process->get_impl()->context_;
29   return context->jprocess_;
30 }
31
32 jobject jprocess_ref(jobject jprocess, JNIEnv* env)
33 {
34   return env->NewGlobalRef(jprocess);
35 }
36
37 void jprocess_unref(jobject jprocess, JNIEnv* env)
38 {
39   env->DeleteGlobalRef(jprocess);
40 }
41
42 msg_process_t jprocess_to_native(jobject jprocess, JNIEnv* env)
43 {
44   return (msg_process_t)(intptr_t)env->GetLongField(jprocess, jprocess_field_Process_bind);
45 }
46
47 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
48 {
49   env->SetLongField(jprocess, jprocess_field_Process_bind, (intptr_t)process);
50 }
51
52 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
53   jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
54   xbt_assert(jprocess_class_Process, "Native initialization of msg/Process failed. Please report that bug");
55
56   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
57   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
58   jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
59   jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
60   jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
61   jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
62   xbt_assert(jprocess_field_Process_name && jprocess_field_Process_pid && jprocess_field_Process_ppid &&
63                  jprocess_field_Process_host,
64              "Native initialization of msg/Process failed. Please report that bug");
65 }
66
67 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv* env, jobject jprocess_arg, jobject jhost)
68 {
69   /* create a global java process instance */
70   jobject jprocess = jprocess_ref(jprocess_arg, env);
71
72   /* Actually build the MSG process */
73   jstring jname         = (jstring)env->GetObjectField(jprocess, jprocess_field_Process_name);
74   const char* name      = env->GetStringUTFChars(jname, 0);
75   msg_process_t process =
76       MSG_process_create_from_stdfunc(name, [jprocess]() { simgrid::kernel::context::java_main_jprocess(jprocess); },
77                                       /*data*/ nullptr, jhost_get_native(env, jhost), /* properties*/ nullptr);
78   env->ReleaseStringUTFChars(jname, name);
79
80   /* bind the java process instance to the native process */
81   jprocess_bind(jprocess, process, env);
82
83   /* Retrieve the kill time from the process */
84   jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
85   MSG_process_set_kill_time(process, (double)jkill);
86
87   /* sets the PID and the PPID of the process */
88   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
89   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
90 }
91
92 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_daemonize(JNIEnv* env, jobject jprocess)
93 {
94   msg_process_t process = jprocess_to_native(jprocess, env);
95
96   if (not process) {
97     jxbt_throw_notbound(env, "process", jprocess);
98     return;
99   }
100
101   process->daemonize();
102 }
103
104 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv* env, jclass cls)
105 {
106   MSG_process_killall();
107 }
108
109 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
110 {
111   msg_process_t process = MSG_process_from_PID(pid);
112
113   if (not process) {
114     jxbt_throw_process_not_found(env, std::string("PID = ") + std::to_string(static_cast<int>(pid)));
115     return nullptr;
116   }
117
118   jobject jprocess = jprocess_from_native(process);
119
120   if (not jprocess) {
121     jxbt_throw_jni(env, "get process failed");
122     return nullptr;
123   }
124
125   return jprocess;
126 }
127
128 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_nativeGetPID(JNIEnv* env, jobject jprocess)
129 {
130   msg_process_t process = jprocess_to_native(jprocess, env);
131   return MSG_process_get_PID(process);
132 }
133
134 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
135   msg_process_t process = jprocess_to_native(jprocess, env);
136
137   if (not process) {
138     jxbt_throw_notbound(env, "process", jprocess);
139     return nullptr;
140   }
141   const char *name = env->GetStringUTFChars((jstring)jname, 0);
142
143   const char *property = MSG_process_get_property_value(process, name);
144   if (not property)
145     return nullptr;
146
147   jobject jproperty = env->NewStringUTF(property);
148
149   env->ReleaseStringUTFChars((jstring)jname, name);
150
151   return jproperty;
152 }
153
154 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
155 {
156   jobject jprocess = jprocess_from_native(MSG_process_self());
157   if (not jprocess)
158     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
159
160   return jprocess;
161 }
162
163 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
164 {
165   msg_process_t process = jprocess_to_native(jprocess, env);
166
167   if (not process) {
168     jxbt_throw_notbound(env, "process", jprocess);
169     return;
170   }
171
172   /* suspend the process */
173   process->suspend();
174 }
175
176 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
177 {
178   msg_process_t process = jprocess_to_native(jprocess, env);
179
180   if (not process) {
181     jxbt_throw_notbound(env, "process", jprocess);
182     return;
183   }
184
185   /* resume the process */
186   process->resume();
187 }
188
189 JNIEXPORT void
190 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
191
192   msg_process_t process = jprocess_to_native(jprocess, env);
193   if (not process) {
194     jxbt_throw_notbound(env, "process", jprocess);
195     return;
196   }
197
198   process->set_auto_restart(jauto_restart == JNI_TRUE);
199 }
200
201 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
202   msg_process_t process = jprocess_to_native(jprocess, env);
203
204   if (not process) {
205     jxbt_throw_notbound(env, "process", jprocess);
206     return;
207   }
208
209   process->restart();
210 }
211
212 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
213 {
214   msg_process_t process = jprocess_to_native(jprocess, env);
215
216   if (not process) {
217     jxbt_throw_notbound(env, "process", jprocess);
218     return 0;
219   }
220
221   /* true is the process is suspended, false otherwise */
222   return (jboolean)process->is_suspended();
223 }
224
225 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
226  {
227   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
228   msg_error_t rv;
229   try {
230     rv = MSG_process_sleep(time);
231   } catch (simgrid::kernel::context::Context::StopRequest const&) {
232     rv = MSG_HOST_FAILURE;
233   }
234   if (rv != MSG_OK) {
235     XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
236
237     jxbt_throw_host_failure(env, "");
238   }
239 }
240
241 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
242 {
243   msg_error_t rv;
244   rv = MSG_process_sleep((double)jseconds);
245   if (env->ExceptionOccurred())
246     return;
247   if (rv != MSG_OK) {
248     XBT_DEBUG("Status NOK");
249     jmsg_throw_status(env,rv);
250   }
251 }
252
253 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
254 {
255   /* get the native instances from the java ones */
256   msg_process_t process = jprocess_to_native(jprocess, env);
257   if (not process) {
258     jxbt_throw_notbound(env, "process", jprocess);
259     return;
260   }
261   try {
262     MSG_process_kill(process);
263   } catch (xbt_ex& ex) {
264     XBT_VERB("Process %s just committed a suicide", MSG_process_get_name(process));
265     xbt_assert(process == MSG_process_self(),
266                "Killing a process should not raise an exception if it's not a suicide. Please report that bug.");
267   }
268 }
269
270 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
271 {
272   msg_process_t process = jprocess_to_native(jprocess, env);
273
274   if (not process) {
275     jxbt_throw_notbound(env, "process", jprocess);
276     return;
277   }
278
279   msg_host_t host = jhost_get_native(env, jhost);
280
281   if (not host) {
282     jxbt_throw_notbound(env, "host", jhost);
283     return;
284   }
285
286   /* change the host of the process */
287   process->migrate(host);
288
289   /* change the host java side */
290   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
291 }
292
293 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
294 {
295   MSG_process_yield();
296 }
297
298 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
299   msg_process_t process = jprocess_to_native(jprocess, env);
300   MSG_process_set_kill_time(process, (double)jkilltime);
301 }
302
303 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
304   return (jint) MSG_process_get_number();
305 }