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 static jfieldID jprocess_field_Process_bind;
18 static jfieldID jprocess_field_Process_name;
19 static jfieldID jprocess_field_Process_host;
20 static jfieldID jprocess_field_Process_id;
21 static jfieldID jprocess_field_Process_pid;
22 static jfieldID jprocess_field_Process_ppid;
24 jobject native_to_java_process(m_process_t process)
26 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
29 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
31 return (*env)->NewGlobalRef(env, jprocess);
34 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
36 (*env)->DeleteGlobalRef(env, jprocess);
39 void jprocess_join(jobject jprocess, JNIEnv * env)
41 m_process_t process = jprocess_to_native_process(jprocess,env);
42 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
43 xbt_os_thread_join(context->thread,NULL);
46 m_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
48 return (m_process_t) (long) (*env)->GetLongField(env, jprocess, jprocess_field_Process_bind);
51 void jprocess_bind(jobject jprocess, m_process_t process, JNIEnv * env)
53 (*env)->SetLongField(env, jprocess, jprocess_field_Process_bind, (jlong) (long) (process));
56 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
58 return (*env)->GetLongField(env, jprocess, jprocess_field_Process_id);
61 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
63 jstring jname = (jstring) (*env)->GetObjectField(env, jprocess, jprocess_field_Process_name);
64 return (*env)->NewGlobalRef(env, jname);
68 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
70 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
75 return (*env)->GetLongField(env, jprocess, id) ? JNI_TRUE : JNI_FALSE;
77 JNIEXPORT void JNICALL
78 Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
79 jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
81 jprocess_field_Process_name = jxbt_get_jfield(env, jprocess_class_Process, "name", "Ljava/lang/String;");
82 jprocess_field_Process_bind = jxbt_get_jfield(env, jprocess_class_Process, "bind", "J");
83 jprocess_field_Process_id = jxbt_get_jfield(env, jprocess_class_Process, "id", "J");
84 jprocess_field_Process_pid = jxbt_get_jfield(env, jprocess_class_Process, "pid", "I");
85 jprocess_field_Process_ppid = jxbt_get_jfield(env, jprocess_class_Process, "ppid", "I");
86 jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
88 if (!jprocess_class_Process || !jprocess_field_Process_id || !jprocess_field_Process_name || !jprocess_field_Process_pid ||
89 !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
90 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
93 JNIEXPORT void JNICALL
94 Java_org_simgrid_msg_Process_create(JNIEnv * env,
100 jobject jprocess; /* the global reference to the java process instance */
101 jstring jname; /* the name of the java process instance */
102 const char *name; /* the C name of the process */
103 const char *hostname;
104 m_process_t process; /* the native process to create */
105 m_host_t host; /* Where that process lives */
107 hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
109 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,jproc=%p,host=%s)",
110 env, jprocess_arg, hostname);
113 /* get the name of the java process */
114 jname = jprocess_get_name(jprocess_arg, env);
117 xbt_strdup("Internal error: Process name cannot be NULL"));
121 /* bind/retrieve the msg host */
122 host = MSG_get_host_by_name(hostname);
124 if (!(host)) { /* not binded */
125 jxbt_throw_host_not_found(env, hostname);
129 /* create a global java process instance */
130 jprocess = jprocess_new_global_ref(jprocess_arg, env);
132 jxbt_throw_jni(env, "Can't get a global ref to the java process");
136 /* build the C name of the process */
137 name = (*env)->GetStringUTFChars(env, jname, 0);
138 name = xbt_strdup(name);
140 /* Actually build the MSG process */
141 process = MSG_process_create_with_environment(name,
142 (xbt_main_func_t) jprocess,
146 /*argc, argv, properties*/
149 MSG_process_set_data(process,&process);
150 /* bind the java process instance to the native process */
151 jprocess_bind(jprocess, process, env);
153 /* release our reference to the process name (variable name becomes invalid) */
154 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
155 //(*env)->ReleaseStringUTFChars(env, jname, name);
156 (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
158 /* sets the PID and the PPID of the process */
159 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
160 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
161 /* sets the Host of the process */
162 jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname);
164 (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
167 JNIEXPORT jint JNICALL
168 Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls,
171 return (jint) MSG_process_killall((int) jresetPID);
174 JNIEXPORT jobject JNICALL
175 Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls,
178 m_process_t process = MSG_process_from_PID(PID);
181 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
185 jobject jprocess = native_to_java_process(process);
188 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
195 JNIEXPORT jobject JNICALL
196 Java_org_simgrid_msg_Process_currentProcess(JNIEnv * env, jclass cls)
198 m_process_t process = MSG_process_self();
202 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
206 jprocess = native_to_java_process(process);
209 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
214 JNIEXPORT void JNICALL
215 Java_org_simgrid_msg_Process_pause(JNIEnv * env,
218 m_process_t process = jprocess_to_native_process(jprocess, env);
221 jxbt_throw_notbound(env, "process", jprocess);
225 /* try to suspend the process */
226 MSG_error_t rv = MSG_process_suspend(process);
228 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
229 bprintf("unexpected error , please report this bug"));
232 JNIEXPORT void JNICALL
233 Java_org_simgrid_msg_Process_restart(JNIEnv * env,
236 m_process_t process = jprocess_to_native_process(jprocess, env);
239 jxbt_throw_notbound(env, "process", jprocess);
243 /* try to resume the process */
244 MSG_error_t rv = MSG_process_resume(process);
246 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
247 bprintf("unexpected error , please report this bug"));
249 JNIEXPORT jboolean JNICALL
250 Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env,
253 m_process_t process = jprocess_to_native_process(jprocess, env);
256 jxbt_throw_notbound(env, "process", jprocess);
260 /* true is the process is suspended, false otherwise */
261 return (jboolean) MSG_process_is_suspended(process);
264 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep
265 (JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) {
267 double time = jmillis / 1000 + jnanos / 1000;
269 MSG_error_t rv = MSG_process_sleep(time);
270 jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
271 bprintf("unexpected error , please report this bug"));
273 JNIEXPORT void JNICALL
274 Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
277 m_process_t process = jprocess_to_native_process(jprocess, env);
280 jxbt_throw_notbound(env, "process", jprocess);
283 MSG_error_t rv = MSG_process_sleep((double)jseconds);
285 // smx_ctx_java_stop(smx_ctx_java_self());
289 JNIEXPORT void JNICALL
290 Java_org_simgrid_msg_Process_kill(JNIEnv * env,
293 /* get the native instances from the java ones */
294 m_process_t process = jprocess_to_native_process(jprocess, env);
296 jxbt_throw_notbound(env, "process", jprocess);
299 /* Sets the "killed" flag to kill the process on the next unschedule */
300 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
303 MSG_process_kill(process);
305 JNIEXPORT void JNICALL
306 Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
307 jobject jprocess, jobject jhost)
309 m_process_t process = jprocess_to_native_process(jprocess, env);
312 jxbt_throw_notbound(env, "process", jprocess);
316 m_host_t host = jhost_get_native(env, jhost);
319 jxbt_throw_notbound(env, "host", jhost);
323 /* try to change the host of the process */
324 MSG_error_t rv = MSG_process_migrate(process, host);
325 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
326 bprintf("unexpected error , please report this bug"));
327 /* change the host java side */
328 (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);