Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0cfc8057ddd5f296362cb6161d72a3cd2b3d92a1
[simgrid.git] / src / bindings / java / jmsg_process.cpp
1 /* Functions related to the java process instances.                         */
2
3 /* Copyright (c) 2007-2019. 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 #include "src/simix/ActorImpl.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   simgrid::simix::ActorCode function = [jprocess]() { simgrid::kernel::context::java_main_jprocess(jprocess); };
77   smx_actor_t self                   = SIMIX_process_self();
78   sg_host_t host                     = jhost_get_native(env, jhost);
79   smx_actor_t actor                  = simgrid::simix::simcall([name, function, host, self] {
80     return simgrid::kernel::actor::ActorImpl::create(std::move(name), std::move(function), nullptr, host, nullptr, self)
81         .get();
82   });
83   MSG_process_yield();
84
85   env->ReleaseStringUTFChars(jname, name);
86
87   /* Retrieve the kill time from the process */
88   actor->ciface()->set_kill_time((double)env->GetDoubleField(jprocess, jprocess_field_Process_killTime));
89
90   /* sets the PID and the PPID of the process */
91   env->SetIntField(jprocess, jprocess_field_Process_pid, (jint)actor->ciface()->get_pid());
92   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint)actor->ciface()->get_ppid());
93 }
94
95 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_daemonize(JNIEnv* env, jobject jprocess)
96 {
97   msg_process_t process = jprocess_to_native(jprocess, env);
98
99   if (not process) {
100     jxbt_throw_notbound(env, "process", jprocess);
101     return;
102   }
103
104   process->daemonize();
105 }
106
107 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv* env, jclass cls)
108 {
109   MSG_process_killall();
110 }
111
112 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
113 {
114   msg_process_t process = MSG_process_from_PID(pid);
115
116   if (not process) {
117     jxbt_throw_process_not_found(env, std::string("PID = ") + std::to_string(static_cast<int>(pid)));
118     return nullptr;
119   }
120
121   jobject jprocess = jprocess_from_native(process);
122
123   if (not jprocess) {
124     jxbt_throw_jni(env, "get process failed");
125     return nullptr;
126   }
127
128   return jprocess;
129 }
130
131 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_nativeGetPID(JNIEnv* env, jobject jprocess)
132 {
133   msg_process_t process = jprocess_to_native(jprocess, env);
134   return MSG_process_get_PID(process);
135 }
136
137 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
138   msg_process_t process = jprocess_to_native(jprocess, env);
139
140   if (not process) {
141     jxbt_throw_notbound(env, "process", jprocess);
142     return nullptr;
143   }
144   const char *name = env->GetStringUTFChars((jstring)jname, 0);
145
146   const char *property = MSG_process_get_property_value(process, name);
147   if (not property)
148     return nullptr;
149
150   jobject jproperty = env->NewStringUTF(property);
151
152   env->ReleaseStringUTFChars((jstring)jname, name);
153
154   return jproperty;
155 }
156
157 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
158 {
159   jobject jprocess = jprocess_from_native(MSG_process_self());
160   if (not jprocess)
161     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
162
163   return jprocess;
164 }
165
166 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
167 {
168   msg_process_t process = jprocess_to_native(jprocess, env);
169
170   if (not process) {
171     jxbt_throw_notbound(env, "process", jprocess);
172     return;
173   }
174
175   /* suspend the process */
176   process->suspend();
177 }
178
179 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
180 {
181   msg_process_t process = jprocess_to_native(jprocess, env);
182
183   if (not process) {
184     jxbt_throw_notbound(env, "process", jprocess);
185     return;
186   }
187
188   /* resume the process */
189   process->resume();
190 }
191
192 JNIEXPORT void
193 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
194
195   msg_process_t process = jprocess_to_native(jprocess, env);
196   if (not process) {
197     jxbt_throw_notbound(env, "process", jprocess);
198     return;
199   }
200
201   process->set_auto_restart(jauto_restart == JNI_TRUE);
202 }
203
204 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
205   msg_process_t process = jprocess_to_native(jprocess, env);
206
207   if (not process) {
208     jxbt_throw_notbound(env, "process", jprocess);
209     return;
210   }
211
212   process->restart();
213 }
214
215 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
216 {
217   msg_process_t process = jprocess_to_native(jprocess, env);
218
219   if (not process) {
220     jxbt_throw_notbound(env, "process", jprocess);
221     return 0;
222   }
223
224   /* true is the process is suspended, false otherwise */
225   return (jboolean)process->is_suspended();
226 }
227
228 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
229  {
230   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
231   msg_error_t rv = MSG_OK;
232   if (not simgrid::ForcefulKillException::try_n_catch([&time]() { simgrid::s4u::this_actor::sleep_for(time); })) {
233     rv = MSG_HOST_FAILURE;
234   }
235   if (rv != MSG_OK) {
236     jmsg_throw_status(env, rv);
237   }
238 }
239
240 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
241 {
242   msg_error_t rv = MSG_OK;
243   if (not simgrid::ForcefulKillException::try_n_catch(
244           [&jseconds]() { simgrid::s4u::this_actor::sleep_for((double)jseconds); })) {
245     rv = MSG_HOST_FAILURE;
246     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
247   }
248   if (env->ExceptionOccurred())
249     return;
250   if (rv != MSG_OK) {
251     XBT_DEBUG("Status NOK");
252     jmsg_throw_status(env,rv);
253   }
254 }
255
256 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
257 {
258   /* get the native instances from the java ones */
259   msg_process_t process = jprocess_to_native(jprocess, env);
260   if (not process) {
261     jxbt_throw_notbound(env, "process", jprocess);
262     return;
263   }
264   if (not simgrid::ForcefulKillException::try_n_catch([&process]() { MSG_process_kill(process); })) {
265     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
266   }
267 }
268
269 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
270 {
271   msg_process_t process = jprocess_to_native(jprocess, env);
272
273   if (not process) {
274     jxbt_throw_notbound(env, "process", jprocess);
275     return;
276   }
277
278   msg_host_t host = jhost_get_native(env, jhost);
279
280   if (not host) {
281     jxbt_throw_notbound(env, "host", jhost);
282     return;
283   }
284
285   /* change the host of the process */
286   process->migrate(host);
287
288   /* change the host java side */
289   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
290 }
291
292 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
293 {
294   MSG_process_yield();
295 }
296
297 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
298   msg_process_t process = jprocess_to_native(jprocess, env);
299   MSG_process_set_kill_time(process, (double)jkilltime);
300 }
301
302 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
303   return (jint) MSG_process_get_number();
304 }