Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MSG aliasing instead of macro-ization to keep ABI safe
[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 "JavaContext.hpp"
13 #include "jmsg.hpp"
14 #include "jmsg_host.h"
15 #include "jxbt_utilities.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
18
19 extern "C" {
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 void JNICALL Java_org_simgrid_msg_Process_daemonize(JNIEnv* env, jobject jprocess)
95 {
96   msg_process_t process = jprocess_to_native(jprocess, env);
97
98   if (not process) {
99     jxbt_throw_notbound(env, "process", jprocess);
100     return;
101   }
102
103   process->daemonize();
104 }
105
106 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv* env, jclass cls)
107 {
108   return (jint)MSG_process_killall();
109 }
110
111 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
112 {
113   msg_process_t process = MSG_process_from_PID(pid);
114
115   if (not process) {
116     jxbt_throw_process_not_found(env, std::string("PID = ") + std::to_string(static_cast<int>(pid)));
117     return nullptr;
118   }
119
120   jobject jprocess = jprocess_from_native(process);
121
122   if (not jprocess) {
123     jxbt_throw_jni(env, "get process failed");
124     return nullptr;
125   }
126
127   return jprocess;
128 }
129
130 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_nativeGetPID(JNIEnv* env, jobject jprocess)
131 {
132   msg_process_t process = jprocess_to_native(jprocess, env);
133   return MSG_process_get_PID(process);
134 }
135
136 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
137   msg_process_t process = jprocess_to_native(jprocess, env);
138
139   if (not process) {
140     jxbt_throw_notbound(env, "process", jprocess);
141     return nullptr;
142   }
143   const char *name = env->GetStringUTFChars((jstring)jname, 0);
144
145   const char *property = MSG_process_get_property_value(process, name);
146   if (not property)
147     return nullptr;
148
149   jobject jproperty = env->NewStringUTF(property);
150
151   env->ReleaseStringUTFChars((jstring)jname, name);
152
153   return jproperty;
154 }
155
156 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
157 {
158   jobject jprocess = jprocess_from_native(MSG_process_self());
159   if (not jprocess)
160     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
161
162   return jprocess;
163 }
164
165 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
166 {
167   msg_process_t process = jprocess_to_native(jprocess, env);
168
169   if (not process) {
170     jxbt_throw_notbound(env, "process", jprocess);
171     return;
172   }
173
174   /* suspend the process */
175   process->suspend();
176 }
177
178 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
179 {
180   msg_process_t process = jprocess_to_native(jprocess, env);
181
182   if (not process) {
183     jxbt_throw_notbound(env, "process", jprocess);
184     return;
185   }
186
187   /* resume the process */
188   process->resume();
189 }
190
191 JNIEXPORT void
192 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
193
194   msg_process_t process = jprocess_to_native(jprocess, env);
195   if (not process) {
196     jxbt_throw_notbound(env, "process", jprocess);
197     return;
198   }
199
200   process->setAutoRestart(jauto_restart == JNI_TRUE);
201 }
202
203 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
204   msg_process_t process = jprocess_to_native(jprocess, env);
205
206   if (not process) {
207     jxbt_throw_notbound(env, "process", jprocess);
208     return;
209   }
210
211   process->restart();
212 }
213
214 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
215 {
216   msg_process_t process = jprocess_to_native(jprocess, env);
217
218   if (not process) {
219     jxbt_throw_notbound(env, "process", jprocess);
220     return 0;
221   }
222
223   /* true is the process is suspended, false otherwise */
224   return (jboolean)process->isSuspended();
225 }
226
227 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
228  {
229   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
230   msg_error_t rv;
231   rv = MSG_process_sleep(time);
232   if (rv != MSG_OK) {
233     XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
234
235     jxbt_throw_host_failure(env, "");
236   }
237 }
238
239 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
240 {
241   msg_error_t rv;
242   rv = MSG_process_sleep((double)jseconds);
243   if (env->ExceptionOccurred())
244     return;
245   if (rv != MSG_OK) {
246     XBT_DEBUG("Status NOK");
247     jmsg_throw_status(env,rv);
248   }
249 }
250
251 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
252 {
253   /* get the native instances from the java ones */
254   msg_process_t process = jprocess_to_native(jprocess, env);
255   if (not process) {
256     jxbt_throw_notbound(env, "process", jprocess);
257     return;
258   }
259   try {
260     MSG_process_kill(process);
261   } catch (xbt_ex& ex) {
262     XBT_VERB("Process %s just committed a suicide", MSG_process_get_name(process));
263     xbt_assert(process == MSG_process_self(),
264                "Killing a process should not raise an exception if it's not a suicide. Please report that bug.");
265   }
266 }
267
268 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
269 {
270   msg_process_t process = jprocess_to_native(jprocess, env);
271
272   if (not process) {
273     jxbt_throw_notbound(env, "process", jprocess);
274     return;
275   }
276
277   msg_host_t host = jhost_get_native(env, jhost);
278
279   if (not host) {
280     jxbt_throw_notbound(env, "host", jhost);
281     return;
282   }
283
284   /* try to change the host of the process */
285   msg_error_t rv = MSG_process_migrate(process, host);
286   if (rv != MSG_OK) {
287     jmsg_throw_status(env,rv);
288     return;
289   }
290   /* change the host java side */
291   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
292 }
293
294 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
295 {
296   MSG_process_yield();
297 }
298
299 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
300   msg_process_t process = jprocess_to_native(jprocess, env);
301   MSG_process_set_kill_time(process, (double)jkilltime);
302 }
303
304 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
305   return (jint) MSG_process_get_number();
306 }
307 }