1 /* Functions related to the java process instances. */
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "jmsg_process.h"
14 #include "jmsg_host.h"
15 #include "jxbt_utilities.h"
16 #include "JavaContext.hpp"
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
22 jfieldID jprocess_field_Process_bind;
23 jfieldID jprocess_field_Process_host;
24 jfieldID jprocess_field_Process_killTime;
25 jfieldID jprocess_field_Process_id;
26 jfieldID jprocess_field_Process_name;
27 jfieldID jprocess_field_Process_pid;
28 jfieldID jprocess_field_Process_ppid;
30 jobject native_to_java_process(msg_process_t process)
32 simgrid::kernel::context::JavaContext* context = (simgrid::kernel::context::JavaContext*) MSG_process_get_smx_ctx(process);
33 return context->jprocess;
36 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
38 return env->NewGlobalRef(jprocess);
41 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
43 env->DeleteGlobalRef(jprocess);
46 void jprocess_join(jobject jprocess, JNIEnv * env)
48 msg_process_t process = jprocess_to_native_process(jprocess,env);
49 simgrid::kernel::context::JavaContext* context = (simgrid::kernel::context::JavaContext*) MSG_process_get_smx_ctx(process);
50 xbt_os_thread_join(context->thread,nullptr);
53 msg_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
55 return (msg_process_t)(intptr_t)env->GetLongField(jprocess, jprocess_field_Process_bind);
58 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
60 env->SetLongField(jprocess, jprocess_field_Process_bind, (intptr_t)process);
63 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
65 return (intptr_t)env->GetLongField(jprocess, jprocess_field_Process_id);
68 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
70 jstring jname = (jstring) env->GetObjectField(jprocess, jprocess_field_Process_name);
71 return (jstring) env->NewGlobalRef(jname);
74 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
76 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
81 return env->GetLongField(jprocess, id) ? JNI_TRUE : JNI_FALSE;
84 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
85 jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
86 xbt_assert(jprocess_class_Process, "Native initialization of msg/Process failed. Please report that bug");
88 jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
89 jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
90 jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
91 jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
92 jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
93 jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
94 jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
95 xbt_assert(jprocess_field_Process_id && jprocess_field_Process_name && jprocess_field_Process_pid &&
96 jprocess_field_Process_ppid && jprocess_field_Process_host,
97 "Native initialization of msg/Process failed. Please report that bug");
100 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv * env, jobject jprocess_arg, jobject jhostname)
102 jobject jprocess; /* the global reference to the java process instance */
103 jstring jname; /* the name of the java process instance */
104 const char *name; /* the C name of the process */
105 const char *hostname;
106 msg_process_t process; /* the native process to create */
107 msg_host_t host; /* Where that process lives */
109 hostname = env->GetStringUTFChars((jstring) jhostname, 0);
111 /* get the name of the java process */
112 jname = jprocess_get_name(jprocess_arg, env);
115 xbt_strdup("Internal error: Process name cannot be nullptr"));
119 /* bind/retrieve the msg host */
120 host = MSG_host_by_name(hostname);
122 if (!(host)) { /* not bound */
123 jxbt_throw_host_not_found(env, hostname);
127 /* create a global java process instance */
128 jprocess = jprocess_new_global_ref(jprocess_arg, env);
130 jxbt_throw_jni(env, "Can't get a global ref to the java process");
134 /* build the C name of the process */
135 name = env->GetStringUTFChars(jname, 0);
136 name = xbt_strdup(name);
138 /* Retrieve the kill time from the process */
139 jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
140 /* Actually build the MSG process */
141 process = MSG_process_create_with_environment(name, [](int argc, char** argv) -> int {
142 smx_actor_t process = SIMIX_process_self();
143 // This is the jprocess passed as environment.
144 // It would be simplet if we could use a closure.
145 jobject jprocess = (jobject) MSG_process_get_data(process);
146 simgrid::kernel::context::java_main_jprocess(jprocess);
150 /*argc, argv, properties*/
151 0, nullptr, nullptr);
152 MSG_process_set_kill_time(process, (double)jkill);
153 /* bind the java process instance to the native process */
154 jprocess_bind(jprocess, process, env);
156 /* release our reference to the process name (variable name becomes invalid) */
157 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
158 //env->ReleaseStringUTFChars(jname, name);
159 env->ReleaseStringUTFChars((jstring) jhostname, hostname);
161 /* sets the PID and the PPID of the process */
162 env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
163 env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
164 /* sets the Host of the process */
165 jobject jhost = Java_org_simgrid_msg_Host_getByName(env,nullptr, (jstring)jhostname);
167 env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
170 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls, jint jresetPID)
172 return (jint) MSG_process_killall((int) jresetPID);
175 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint pid)
177 msg_process_t process = MSG_process_from_PID(pid);
180 jxbt_throw_process_not_found(env, bprintf("PID = %d",static_cast<int>(pid)));
184 jobject jprocess = native_to_java_process(process);
187 jxbt_throw_jni(env, "get process failed");
194 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
195 msg_process_t process = jprocess_to_native_process(jprocess, env);
198 jxbt_throw_notbound(env, "process", jprocess);
201 const char *name = env->GetStringUTFChars((jstring)jname, 0);
203 const char *property = MSG_process_get_property_value(process, name);
208 jobject jproperty = env->NewStringUTF(property);
210 env->ReleaseStringUTFChars((jstring)jname, name);
215 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
217 msg_process_t process = MSG_process_self();
221 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
225 jprocess = native_to_java_process(process);
228 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
233 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
235 msg_process_t process = jprocess_to_native_process(jprocess, env);
238 jxbt_throw_notbound(env, "process", jprocess);
242 /* try to suspend the process */
243 msg_error_t rv = MSG_process_suspend(process);
245 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
248 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
250 msg_process_t process = jprocess_to_native_process(jprocess, env);
253 jxbt_throw_notbound(env, "process", jprocess);
257 /* try to resume the process */
258 msg_error_t rv = MSG_process_resume(process);
260 jxbt_check_res("MSG_process_resume()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
263 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
264 msg_process_t process = jprocess_to_native_process(jprocess, env);
267 int auto_restart = jauto_restart == JNI_TRUE ? 1 : 0;
270 jxbt_throw_notbound(env, "process", jprocess);
275 MSG_process_auto_restart_set(process,auto_restart);
282 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
283 msg_process_t process = jprocess_to_native_process(jprocess, env);
287 jxbt_throw_notbound(env, "process", jprocess);
292 MSG_process_restart(process);
299 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
301 msg_process_t process = jprocess_to_native_process(jprocess, env);
304 jxbt_throw_notbound(env, "process", jprocess);
308 /* true is the process is suspended, false otherwise */
309 return (jboolean) MSG_process_is_suspended(process);
312 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
314 double time = ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
316 rv = MSG_process_sleep(time);
318 XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
320 //jmsg_throw_status(env,rv);
322 // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
323 // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
324 // 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
325 // function msg_error_t MSG_process_sleep(double nb_sec)
327 jxbt_throw_host_failure(env,nullptr);
331 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
334 rv = MSG_process_sleep((double)jseconds);
335 if (env->ExceptionOccurred())
338 XBT_DEBUG("Status NOK");
339 jmsg_throw_status(env,rv);
343 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
345 /* get the native instances from the java ones */
346 msg_process_t process = jprocess_to_native_process(jprocess, env);
348 jxbt_throw_notbound(env, "process", jprocess);
353 MSG_process_kill(process);
354 } catch (xbt_ex& ex) {
355 XBT_VERB("This process just killed itself.");
359 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
361 msg_process_t process = jprocess_to_native_process(jprocess, env);
364 jxbt_throw_notbound(env, "process", jprocess);
368 msg_host_t host = jhost_get_native(env, jhost);
371 jxbt_throw_notbound(env, "host", jhost);
375 /* try to change the host of the process */
376 msg_error_t rv = MSG_process_migrate(process, host);
378 jmsg_throw_status(env,rv);
381 /* change the host java side */
382 env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
385 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_yield(JNIEnv* env, jclass cls)
390 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
391 msg_process_t process = jprocess_to_native_process(jprocess, env);
392 MSG_process_set_kill_time(process, (double)jkilltime);
395 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
396 return (jint) MSG_process_get_number();