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 JNIEXPORT void JNICALL
31 Java_org_simgrid_msg_Process_exit(JNIEnv *env, jobject jprocess) {
35 jobject native_to_java_process(msg_process_t process)
37 simgrid::kernel::context::JavaContext* context = (simgrid::kernel::context::JavaContext*) MSG_process_get_smx_ctx(process);
38 return context->jprocess;
41 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
43 return env->NewGlobalRef(jprocess);
46 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
48 env->DeleteGlobalRef(jprocess);
51 void jprocess_join(jobject jprocess, JNIEnv * env)
53 msg_process_t process = jprocess_to_native_process(jprocess,env);
54 simgrid::kernel::context::JavaContext* context = (simgrid::kernel::context::JavaContext*) MSG_process_get_smx_ctx(process);
55 xbt_os_thread_join(context->thread,nullptr);
58 msg_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
60 return (msg_process_t)(intptr_t)env->GetLongField(jprocess, jprocess_field_Process_bind);
63 void jprocess_bind(jobject jprocess, msg_process_t process, JNIEnv * env)
65 env->SetLongField(jprocess, jprocess_field_Process_bind, (intptr_t)process);
68 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
70 return (intptr_t)env->GetLongField(jprocess, jprocess_field_Process_id);
73 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
75 jstring jname = (jstring) env->GetObjectField(jprocess, jprocess_field_Process_name);
76 return (jstring) env->NewGlobalRef(jname);
79 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
81 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
86 return env->GetLongField(jprocess, id) ? JNI_TRUE : JNI_FALSE;
89 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
90 jclass jprocess_class_Process = env->FindClass("org/simgrid/msg/Process");
92 jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
93 jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
94 jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
95 jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
96 jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
97 jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
98 jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
99 if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name ||
100 !jprocess_field_Process_pid || !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
101 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
105 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv * env, jobject jprocess_arg, jobject jhostname)
107 jobject jprocess; /* the global reference to the java process instance */
108 jstring jname; /* the name of the java process instance */
109 const char *name; /* the C name of the process */
110 const char *hostname;
111 msg_process_t process; /* the native process to create */
112 msg_host_t host; /* Where that process lives */
114 hostname = env->GetStringUTFChars((jstring) jhostname, 0);
116 /* get the name of the java process */
117 jname = jprocess_get_name(jprocess_arg, env);
120 xbt_strdup("Internal error: Process name cannot be nullptr"));
124 /* bind/retrieve the msg host */
125 host = MSG_host_by_name(hostname);
127 if (!(host)) { /* not bound */
128 jxbt_throw_host_not_found(env, hostname);
132 /* create a global java process instance */
133 jprocess = jprocess_new_global_ref(jprocess_arg, env);
135 jxbt_throw_jni(env, "Can't get a global ref to the java process");
139 /* build the C name of the process */
140 name = env->GetStringUTFChars(jname, 0);
141 name = xbt_strdup(name);
143 /* Retrieve the kill time from the process */
144 jdouble jkill = env->GetDoubleField(jprocess, jprocess_field_Process_killTime);
145 /* Actually build the MSG process */
146 process = MSG_process_create_with_environment(name, [](int argc, char** argv) -> int {
147 smx_actor_t process = SIMIX_process_self();
148 // This is the jprocess passed as environment.
149 // It would be simplet if we could use a closure.
150 jobject jprocess = (jobject) MSG_process_get_data(process);
151 simgrid::kernel::context::java_main_jprocess(jprocess);
155 /*argc, argv, properties*/
156 0, nullptr, nullptr);
157 MSG_process_set_kill_time(process, (double)jkill);
158 /* bind the java process instance to the native process */
159 jprocess_bind(jprocess, process, env);
161 /* release our reference to the process name (variable name becomes invalid) */
162 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
163 //env->ReleaseStringUTFChars(jname, name);
164 env->ReleaseStringUTFChars((jstring) jhostname, hostname);
166 /* sets the PID and the PPID of the process */
167 env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
168 env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
169 /* sets the Host of the process */
170 jobject jhost = Java_org_simgrid_msg_Host_getByName(env,nullptr, (jstring)jhostname);
172 env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
175 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls, jint jresetPID)
177 return (jint) MSG_process_killall((int) jresetPID);
180 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint PID)
182 msg_process_t process = MSG_process_from_PID(PID);
185 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
189 jobject jprocess = native_to_java_process(process);
192 jxbt_throw_jni(env, "get process failed");
199 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getProperty(JNIEnv *env, jobject jprocess, jobject jname) {
200 msg_process_t process = jprocess_to_native_process(jprocess, env);
203 jxbt_throw_notbound(env, "process", jprocess);
206 const char *name = env->GetStringUTFChars((jstring)jname, 0);
208 const char *property = MSG_process_get_property_value(process, name);
213 jobject jproperty = env->NewStringUTF(property);
215 env->ReleaseStringUTFChars((jstring)jname, name);
220 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls)
222 msg_process_t process = MSG_process_self();
226 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
230 jprocess = native_to_java_process(process);
233 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
238 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_suspend(JNIEnv * env, jobject jprocess)
240 msg_process_t process = jprocess_to_native_process(jprocess, env);
243 jxbt_throw_notbound(env, "process", jprocess);
247 /* try to suspend the process */
248 msg_error_t rv = MSG_process_suspend(process);
250 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
253 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_resume(JNIEnv * env, jobject jprocess)
255 msg_process_t process = jprocess_to_native_process(jprocess, env);
258 jxbt_throw_notbound(env, "process", jprocess);
262 /* try to resume the process */
263 msg_error_t rv = MSG_process_resume(process);
265 jxbt_check_res("MSG_process_resume()", rv, MSG_OK, bprintf("unexpected error , please report this bug"));
268 JNICALL Java_org_simgrid_msg_Process_setAutoRestart (JNIEnv *env, jobject jprocess, jboolean jauto_restart) {
269 msg_process_t process = jprocess_to_native_process(jprocess, env);
272 int auto_restart = jauto_restart == JNI_TRUE ? 1 : 0;
275 jxbt_throw_notbound(env, "process", jprocess);
280 MSG_process_auto_restart_set(process,auto_restart);
287 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_restart (JNIEnv *env, jobject jprocess) {
288 msg_process_t process = jprocess_to_native_process(jprocess, env);
292 jxbt_throw_notbound(env, "process", jprocess);
297 MSG_process_restart(process);
304 JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env, jobject jprocess)
306 msg_process_t process = jprocess_to_native_process(jprocess, env);
309 jxbt_throw_notbound(env, "process", jprocess);
313 /* true is the process is suspended, false otherwise */
314 return (jboolean) MSG_process_is_suspended(process);
317 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep(JNIEnv *env, jclass cls, jlong jmillis, jint jnanos)
319 double time = ((double)jmillis) / 1000 + ((double)jnanos) / 1000000000;
321 rv = MSG_process_sleep(time);
323 XBT_DEBUG("Something during the MSG_process_sleep invocation was wrong, trigger a HostFailureException");
325 //jmsg_throw_status(env,rv);
327 // adsein, the code above as been replaced by the code below. Indeed, according to the documentation, a sleep can only
328 // trigger a host_failure exception. When the sleep crashes due to a host shutdown, the exception thrown by smx_context_java.c
329 // 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
330 // function msg_error_t MSG_process_sleep(double nb_sec)
332 jxbt_throw_host_failure(env,nullptr);
336 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess, jdouble jseconds)
339 rv = MSG_process_sleep((double)jseconds);
340 if (env->ExceptionOccurred())
343 XBT_DEBUG("Status NOK");
344 jmsg_throw_status(env,rv);
348 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_kill(JNIEnv * env, jobject jprocess)
350 /* get the native instances from the java ones */
351 msg_process_t process = jprocess_to_native_process(jprocess, env);
353 jxbt_throw_notbound(env, "process", jprocess);
357 MSG_process_kill(process);
360 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_migrate(JNIEnv * env, jobject jprocess, jobject jhost)
362 msg_process_t process = jprocess_to_native_process(jprocess, env);
365 jxbt_throw_notbound(env, "process", jprocess);
369 msg_host_t host = jhost_get_native(env, jhost);
372 jxbt_throw_notbound(env, "host", jhost);
376 /* try to change the host of the process */
377 msg_error_t rv = MSG_process_migrate(process, host);
379 jmsg_throw_status(env,rv);
382 /* change the host java side */
383 env->SetObjectField(jprocess, jprocess_field_Process_host, jhost);
386 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_setKillTime (JNIEnv *env , jobject jprocess, jdouble jkilltime) {
387 msg_process_t process = jprocess_to_native_process(jprocess, env);
388 MSG_process_set_kill_time(process, (double)jkilltime);
391 JNIEXPORT jint JNICALL Java_org_simgrid_msg_Process_getCount(JNIEnv * env, jclass cls) {
392 return (jint) MSG_process_get_number();