1 /* Functions related to the java process instances. */
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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. */
8 #include "jmsg_process.h"
11 #include "jmsg_host.h"
12 #include "jxbt_utilities.h"
13 #include "smx_context_java.h"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
17 jobject native_to_java_process(m_process_t process)
19 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
22 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
24 return (*env)->NewGlobalRef(env, jprocess);
27 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
29 (*env)->DeleteGlobalRef(env, jprocess);
32 void jprocess_join(jobject jprocess, JNIEnv * env)
34 m_process_t process = jprocess_to_native_process(jprocess,env);
35 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
36 xbt_os_thread_join(context->thread,NULL);
39 m_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
41 return (m_process_t) (long) (*env)->GetLongField(env, jprocess, jprocess_field_Process_bind);
44 void jprocess_bind(jobject jprocess, m_process_t process, JNIEnv * env)
46 (*env)->SetLongField(env, jprocess, jprocess_field_Process_bind, (jlong) (long) (process));
49 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
51 return (*env)->GetLongField(env, jprocess, jprocess_field_Process_id);
54 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
56 jstring jname = (jstring) (*env)->GetObjectField(env, jprocess, jprocess_field_Process_name);
57 return (*env)->NewGlobalRef(env, jname);
61 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
63 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
68 return (*env)->GetLongField(env, jprocess, id) ? JNI_TRUE : JNI_FALSE;
70 JNIEXPORT void JNICALL
71 Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
72 jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
74 jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
75 jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
76 jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
77 jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
78 jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
79 jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
80 jprocess_field_Process_killTime = jxbt_get_jfield(env, jprocess_class_Process, "killTime", "D");
81 if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name || !jprocess_field_Process_pid ||
82 !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
83 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
86 JNIEXPORT void JNICALL
87 Java_org_simgrid_msg_Process_create(JNIEnv * env,
93 jobject jprocess; /* the global reference to the java process instance */
94 jstring jname; /* the name of the java process instance */
95 const char *name; /* the C name of the process */
97 m_process_t process; /* the native process to create */
98 m_host_t host; /* Where that process lives */
100 hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
102 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,jproc=%p,host=%s)",
103 env, jprocess_arg, hostname);
106 /* get the name of the java process */
107 jname = jprocess_get_name(jprocess_arg, env);
110 xbt_strdup("Internal error: Process name cannot be NULL"));
114 /* bind/retrieve the msg host */
115 host = MSG_get_host_by_name(hostname);
117 if (!(host)) { /* not binded */
118 jxbt_throw_host_not_found(env, hostname);
122 /* create a global java process instance */
123 jprocess = jprocess_new_global_ref(jprocess_arg, env);
125 jxbt_throw_jni(env, "Can't get a global ref to the java process");
129 /* build the C name of the process */
130 name = (*env)->GetStringUTFChars(env, jname, 0);
131 name = xbt_strdup(name);
133 /* Retrieve the kill time from the process */
134 jdouble jkill = (*env)->GetDoubleField(env, jprocess, jprocess_field_Process_killTime);
135 /* Actually build the MSG process */
136 process = MSG_process_create_with_environment(name,
137 (xbt_main_func_t) jprocess,
140 (double)jkill, /* kill time */
141 /*argc, argv, properties*/
144 MSG_process_set_data(process,&process);
145 /* bind the java process instance to the native process */
146 jprocess_bind(jprocess, process, env);
148 /* release our reference to the process name (variable name becomes invalid) */
149 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
150 //(*env)->ReleaseStringUTFChars(env, jname, name);
151 (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
153 /* sets the PID and the PPID of the process */
154 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
155 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
156 /* sets the Host of the process */
157 jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname);
159 (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
162 JNIEXPORT jint JNICALL
163 Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls,
166 return (jint) MSG_process_killall((int) jresetPID);
169 JNIEXPORT jobject JNICALL
170 Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls,
173 m_process_t process = MSG_process_from_PID(PID);
176 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
180 jobject jprocess = native_to_java_process(process);
183 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
190 JNIEXPORT jobject JNICALL
191 Java_org_simgrid_msg_Process_currentProcess(JNIEnv * env, jclass cls)
193 m_process_t process = MSG_process_self();
197 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
201 jprocess = native_to_java_process(process);
204 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
209 JNIEXPORT void JNICALL
210 Java_org_simgrid_msg_Process_pause(JNIEnv * env,
213 m_process_t process = jprocess_to_native_process(jprocess, env);
216 jxbt_throw_notbound(env, "process", jprocess);
220 /* try to suspend the process */
221 MSG_error_t rv = MSG_process_suspend(process);
223 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
224 bprintf("unexpected error , please report this bug"));
227 JNIEXPORT void JNICALL
228 Java_org_simgrid_msg_Process_restart(JNIEnv * env,
231 m_process_t process = jprocess_to_native_process(jprocess, env);
234 jxbt_throw_notbound(env, "process", jprocess);
238 /* try to resume the process */
239 MSG_error_t rv = MSG_process_resume(process);
241 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
242 bprintf("unexpected error , please report this bug"));
244 JNIEXPORT jboolean JNICALL
245 Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env,
248 m_process_t process = jprocess_to_native_process(jprocess, env);
251 jxbt_throw_notbound(env, "process", jprocess);
255 /* true is the process is suspended, false otherwise */
256 return (jboolean) MSG_process_is_suspended(process);
259 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep
260 (JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) {
262 double time = jmillis / 1000 + jnanos / 1000;
265 rv = MSG_process_sleep(time);
271 jmsg_throw_status(env,rv);
274 JNIEXPORT void JNICALL
275 Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
278 m_process_t process = jprocess_to_native_process(jprocess, env);
281 jxbt_throw_notbound(env, "process", jprocess);
286 rv = MSG_process_sleep((double)jseconds);
292 XBT_INFO("Status NOK");
293 jmsg_throw_status(env,rv);
297 JNIEXPORT void JNICALL
298 Java_org_simgrid_msg_Process_kill(JNIEnv * env,
301 /* get the native instances from the java ones */
302 m_process_t process = jprocess_to_native_process(jprocess, env);
304 jxbt_throw_notbound(env, "process", jprocess);
308 MSG_process_kill(process);
310 JNIEXPORT void JNICALL
311 Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
312 jobject jprocess, jobject jhost)
314 m_process_t process = jprocess_to_native_process(jprocess, env);
317 jxbt_throw_notbound(env, "process", jprocess);
321 m_host_t host = jhost_get_native(env, jhost);
324 jxbt_throw_notbound(env, "host", jhost);
328 /* try to change the host of the process */
329 MSG_error_t rv = MSG_process_migrate(process, host);
330 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
331 bprintf("unexpected error , please report this bug"));
332 /* change the host java side */
333 (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);