Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
jprocess: more cruft removed
[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 "jmsg.h"
13 #include "jmsg_host.h"
14 #include "jxbt_utilities.h"
15 #include "JavaContext.hpp"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
18
19 SG_BEGIN_DECL()
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 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
55 {
56   jstring jname = (jstring) env->GetObjectField(jprocess, jprocess_field_Process_name);
57   return (jstring) env->NewGlobalRef(jname);
58 }
59
60 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
61   jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
62   xbt_assert(jprocess_class_Process, "Native initialization of msg/Process failed. Please report that bug");
63
64   jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
65   jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
66   jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
67   jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
68   jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
69   jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
70   xbt_assert(jprocess_field_Process_name && jprocess_field_Process_pid && jprocess_field_Process_ppid &&
71                  jprocess_field_Process_host,
72              "Native initialization of msg/Process failed. Please report that bug");
73 }
74
75 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv* env, jobject jprocess_arg, jobject jhost)
76 {
77   /* create a global java process instance */
78   jobject jprocess = jprocess_ref(jprocess_arg, env);
79
80   /* Actually build the MSG process */
81   jstring jname         = jprocess_get_name(jprocess_arg, env);
82   const char* name      = env->GetStringUTFChars(jname, 0);
83   msg_process_t process = MSG_process_create_from_stdfunc(
84       name, [jprocess]() -> void { simgrid::kernel::context::java_main_jprocess(jprocess); },
85       /*data*/ nullptr, jhost_get_native(env, jhost), /* properties*/ nullptr);
86
87   env->ReleaseStringUTFChars(jname, name);
88   /* bind the java process instance to the native process */
89   jprocess_bind(jprocess, process, env);
90
91   /* Retrieve the kill time from the process */
92   jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
93   MSG_process_set_kill_time(process, (double)jkill);
94
95   /* sets the PID and the PPID of the process */
96   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
97   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
98 }
99
100 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls, jint jresetPID)
101 {
102   return (jint) MSG_process_killall((int) jresetPID);
103 }
104
105 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
106 {
107   msg_process_t process = MSG_process_from_PID(pid);
108
109   if (!process) {
110     jxbt_throw_process_not_found(env, bprintf("PID = %d",static_cast<int>(pid)));
111     return nullptr;
112   }
113
114   jobject jprocess = jprocess_from_native(process);
115
116   if (!jprocess) {
117     jxbt_throw_jni(env, "get process failed");
118     return nullptr;
119   }
120
121   return jprocess;
122 }
123
124 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
125   msg_process_t process = jprocess_to_native(jprocess, env);
126
127   if (!process) {
128     jxbt_throw_notbound(env, "process", jprocess);
129     return nullptr;
130   }
131   const char *name = env->GetStringUTFChars((jstring)jname, 0);
132
133   const char *property = MSG_process_get_property_value(process, name);
134   if (!property) {
135     return nullptr;
136   }
137
138   jobject jproperty = env->NewStringUTF(property);
139
140   env->ReleaseStringUTFChars((jstring)jname, name);
141
142   return jproperty;
143 }
144
145 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
146 {
147   msg_process_t process = MSG_process_self();
148   jobject jprocess;
149
150   if (!process) {
151     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
152     return nullptr;
153   }
154
155   jprocess = jprocess_from_native(process);
156
157   if (!jprocess)
158     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
159
160   return jprocess;
161 }
162
163 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
164 {
165   msg_process_t process = jprocess_to_native(jprocess, env);
166
167   if (!process) {
168     jxbt_throw_notbound(env, "process", jprocess);
169     return;
170   }
171
172   /* try to suspend the process */
173   msg_error_t rv = MSG_process_suspend(process);
174
175   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
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 (!process) {
183     jxbt_throw_notbound(env, "process", jprocess);
184     return;
185   }
186
187   /* try to resume the process */
188   msg_error_t rv = MSG_process_resume(process);
189
190   jxbt_check_res("MSG_process_resume()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
191 }
192 JNIEXPORT void
193 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
194   msg_process_t process = jprocess_to_native(jprocess, env);
195   xbt_ex_t e;
196
197   int auto_restart = jauto_restart == JNI_TRUE ? 1 : 0;
198
199   if (!process) {
200     jxbt_throw_notbound(env, "process", jprocess);
201     return;
202   }
203
204   try {
205     MSG_process_auto_restart_set(process,auto_restart);
206   }
207   catch (xbt_ex& e) {
208     // Nothing to do
209   }
210 }
211
212 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
213   msg_process_t process = jprocess_to_native(jprocess, env);
214   xbt_ex_t e;
215
216   if (!process) {
217     jxbt_throw_notbound(env, "process", jprocess);
218     return;
219   }
220
221   try {
222     MSG_process_restart(process);
223   }
224   catch (xbt_ex& e) {
225     // Nothing to do
226   }
227
228 }
229 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
230 {
231   msg_process_t process = jprocess_to_native(jprocess, env);
232
233   if (!process) {
234     jxbt_throw_notbound(env, "process", jprocess);
235     return 0;
236   }
237
238   /* true is the process is suspended, false otherwise */
239   return (jboolean) MSG_process_is_suspended(process);
240 }
241
242 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
243  {
244   double time =  ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
245   msg_error_t rv;
246   rv = MSG_process_sleep(time);
247   if (rv != MSG_OK) {
248     XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
249
250     //jmsg_throw_status(env,rv);
251
252     // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
253     // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
254     // is a cancelled_error, see bindings/java/smx_context_java.c, function void smx_ctx_java_stop(smx_context_t context) and src/msg/msg_gos.c
255     // function  msg_error_t MSG_process_sleep(double nb_sec)
256
257     jxbt_throw_host_failure(env, "");
258   }
259 }
260
261 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
262 {
263   msg_error_t rv;
264   rv = MSG_process_sleep((double)jseconds);
265   if (env->ExceptionOccurred())
266     return;
267   if (rv != MSG_OK) {
268     XBT_DEBUG("Status NOK");
269     jmsg_throw_status(env,rv);
270   }
271 }
272
273 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
274 {
275   /* get the native instances from the java ones */
276   msg_process_t process = jprocess_to_native(jprocess, env);
277   if (!process) {
278     jxbt_throw_notbound(env, "process", jprocess);
279     return;
280   }
281
282   try {
283     MSG_process_kill(process);
284   } catch (xbt_ex& ex) {
285     XBT_VERB("This process just killed itself.");
286   }
287 }
288
289 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
290 {
291   msg_process_t process = jprocess_to_native(jprocess, env);
292
293   if (!process) {
294     jxbt_throw_notbound(env, "process", jprocess);
295     return;
296   }
297
298   msg_host_t host = jhost_get_native(env, jhost);
299
300   if (!host) {
301     jxbt_throw_notbound(env, "host", jhost);
302     return;
303   }
304
305   /* try to change the host of the process */
306   msg_error_t rv = MSG_process_migrate(process, host);
307   if (rv != MSG_OK) {
308     jmsg_throw_status(env,rv);
309     return;
310   }
311   /* change the host java side */
312   env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
313 }
314
315 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
316 {
317   MSG_process_yield();
318 }
319
320 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
321   msg_process_t process = jprocess_to_native(jprocess, env);
322   MSG_process_set_kill_time(process, (double)jkilltime);
323 }
324
325 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
326   return (jint) MSG_process_get_number();
327 }
328
329 SG_END_DECL()