Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
java process creation: this was already binded by java_main_jprocess()
[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   /* Retrieve the kill time from the process */
81   jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
82   MSG_process_set_kill_time(process, (double)jkill);
83
84   /* sets the PID and the PPID of the process */
85   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
86   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
87 }
88
89 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_daemonize(JNIEnv* env, jobject jprocess)
90 {
91   msg_process_t process = jprocess_to_native(jprocess, env);
92
93   if (not process) {
94     jxbt_throw_notbound(env, "process", jprocess);
95     return;
96   }
97
98   process->daemonize();
99 }
100
101 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv* env, jclass cls)
102 {
103   MSG_process_killall();
104 }
105
106 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
107 {
108   msg_process_t process = MSG_process_from_PID(pid);
109
110   if (not process) {
111     jxbt_throw_process_not_found(env, std::string("PID = ") + std::to_string(static_cast<int>(pid)));
112     return nullptr;
113   }
114
115   jobject jprocess = jprocess_from_native(process);
116
117   if (not jprocess) {
118     jxbt_throw_jni(env, "get process failed");
119     return nullptr;
120   }
121
122   return jprocess;
123 }
124
125 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_nativeGetPID(JNIEnv* env, jobject jprocess)
126 {
127   msg_process_t process = jprocess_to_native(jprocess, env);
128   return MSG_process_get_PID(process);
129 }
130
131 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
132   msg_process_t process = jprocess_to_native(jprocess, env);
133
134   if (not process) {
135     jxbt_throw_notbound(env, "process", jprocess);
136     return nullptr;
137   }
138   const char *name = env->GetStringUTFChars((jstring)jname, 0);
139
140   const char *property = MSG_process_get_property_value(process, name);
141   if (not property)
142     return nullptr;
143
144   jobject jproperty = env->NewStringUTF(property);
145
146   env->ReleaseStringUTFChars((jstring)jname, name);
147
148   return jproperty;
149 }
150
151 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
152 {
153   jobject jprocess = jprocess_from_native(MSG_process_self());
154   if (not jprocess)
155     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
156
157   return jprocess;
158 }
159
160 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
161 {
162   msg_process_t process = jprocess_to_native(jprocess, env);
163
164   if (not process) {
165     jxbt_throw_notbound(env, "process", jprocess);
166     return;
167   }
168
169   /* suspend the process */
170   process->suspend();
171 }
172
173 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
174 {
175   msg_process_t process = jprocess_to_native(jprocess, env);
176
177   if (not process) {
178     jxbt_throw_notbound(env, "process", jprocess);
179     return;
180   }
181
182   /* resume the process */
183   process->resume();
184 }
185
186 JNIEXPORT void
187 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
188
189   msg_process_t process = jprocess_to_native(jprocess, env);
190   if (not process) {
191     jxbt_throw_notbound(env, "process", jprocess);
192     return;
193   }
194
195   process->set_auto_restart(jauto_restart == JNI_TRUE);
196 }
197
198 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
199   msg_process_t process = jprocess_to_native(jprocess, env);
200
201   if (not process) {
202     jxbt_throw_notbound(env, "process", jprocess);
203     return;
204   }
205
206   process->restart();
207 }
208
209 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
210 {
211   msg_process_t process = jprocess_to_native(jprocess, env);
212
213   if (not process) {
214     jxbt_throw_notbound(env, "process", jprocess);
215     return 0;
216   }
217
218   /* true is the process is suspended, false otherwise */
219   return (jboolean)process->is_suspended();
220 }
221
222 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
223  {
224   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
225   msg_error_t rv;
226   try {
227     rv = MSG_process_sleep(time);
228   } catch (simgrid::kernel::context::Context::StopRequest const&) {
229     rv = MSG_HOST_FAILURE;
230   }
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 }