Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
codefactor: malformed whitespace issues
[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
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       static_cast<simgrid::kernel::context::JavaContext*>(process->get_impl()->context_.get());
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   simgrid::simix::ActorCode function = [jprocess]() { simgrid::kernel::context::java_main_jprocess(jprocess); };
76   smx_actor_t self                   = SIMIX_process_self();
77   sg_host_t host                     = jhost_get_native(env, jhost);
78   smx_actor_t actor                  = simgrid::kernel::actor::simcall([name, function, host, self] {
79     return simgrid::kernel::actor::ActorImpl::create(std::move(name), std::move(function), nullptr, host, nullptr, self)
80         .get();
81   });
82   MSG_process_yield();
83
84   env->ReleaseStringUTFChars(jname, name);
85
86   /* Retrieve the kill time from the process */
87   actor->ciface()->set_kill_time((double)env->GetDoubleField(jprocess, jprocess_field_Process_killTime));
88
89   /* sets the PID and the PPID of the process */
90   env->SetIntField(jprocess, jprocess_field_Process_pid, (jint)actor->ciface()->get_pid());
91   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint)actor->ciface()->get_ppid());
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 void JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv* env, jclass cls)
107 {
108   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 JNICALL Java_org_simgrid_msg_Process_setAutoRestart(JNIEnv* env, jobject jprocess,
192                                                                    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->set_auto_restart(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->is_suspended();
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 = MSG_OK;
231   if (not simgrid::ForcefulKillException::try_n_catch([&time]() { simgrid::s4u::this_actor::sleep_for(time); })) {
232     rv = MSG_HOST_FAILURE;
233   }
234   if (rv != MSG_OK) {
235     jmsg_throw_status(env, rv);
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 = MSG_OK;
242   if (not simgrid::ForcefulKillException::try_n_catch(
243           [&jseconds]() { simgrid::s4u::this_actor::sleep_for((double)jseconds); })) {
244     rv = MSG_HOST_FAILURE;
245     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
246   }
247   if (env->ExceptionOccurred())
248     return;
249   if (rv != MSG_OK) {
250     XBT_DEBUG("Status NOK");
251     jmsg_throw_status(env,rv);
252   }
253 }
254
255 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
256 {
257   /* get the native instances from the java ones */
258   msg_process_t process = jprocess_to_native(jprocess, env);
259   if (not process) {
260     jxbt_throw_notbound(env, "process", jprocess);
261     return;
262   }
263   if (not simgrid::ForcefulKillException::try_n_catch([&process]() { MSG_process_kill(process); })) {
264     jxbt_throw_by_name(env, "org/simgrid/msg/ProcessKilledError", "Process killed");
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   /* change the host of the process */
285   process->set_host(host);
286
287   /* change the host java side */
288   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
289 }
290
291 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
292 {
293   MSG_process_yield();
294 }
295
296 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
297   msg_process_t process = jprocess_to_native(jprocess, env);
298   MSG_process_set_kill_time(process, (double)jkilltime);
299 }
300
301 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
302   return (jint) MSG_process_get_number();
303 }