Logo AND Algorithmique Numérique Distribuée

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