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_host;
18 static jfieldID jprocess_field_Process_pid;
19 static jfieldID jprocess_field_Process_ppid;
21 jobject native_to_java_process(m_process_t process)
23 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
26 jobject jprocess_new_global_ref(jobject jprocess, JNIEnv * env)
28 return (*env)->NewGlobalRef(env, jprocess);
31 void jprocess_delete_global_ref(jobject jprocess, JNIEnv * env)
33 (*env)->DeleteGlobalRef(env, jprocess);
36 void jprocess_join(jobject jprocess, JNIEnv * env)
38 m_process_t process = jprocess_to_native_process(jprocess,env);
39 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
40 xbt_os_thread_join(context->thread,NULL);
43 void jprocess_start(jobject jprocess, JNIEnv * env)
46 jxbt_get_smethod(env, "org/simgrid/msg/Process", "start", "()V");
51 XBT_DEBUG("jprocess_start(jproc=%p,env=%p)", jprocess, env);
52 (*env)->CallVoidMethod(env, jprocess, id);
53 XBT_DEBUG("jprocess started");
56 m_process_t jprocess_to_native_process(jobject jprocess, JNIEnv * env)
58 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
61 XBT_INFO("Can't find bind field in Process");
64 return (m_process_t) (long) (*env)->GetLongField(env, jprocess, id);
67 void jprocess_bind(jobject jprocess, m_process_t process, JNIEnv * env)
69 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
70 xbt_assert((id != NULL), "field bind not found in org/simgrid/msg/Process");
71 (*env)->SetLongField(env, jprocess, id, (jlong) (long) (process));
74 jlong jprocess_get_id(jobject jprocess, JNIEnv * env)
76 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "id", "J");
81 return (*env)->GetLongField(env, jprocess, id);
84 jstring jprocess_get_name(jobject jprocess, JNIEnv * env)
86 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "name",
87 "Ljava/lang/String;");
93 jname = (jstring) (*env)->GetObjectField(env, jprocess, id);
95 return (*env)->NewGlobalRef(env, jname);
99 jboolean jprocess_is_valid(jobject jprocess, JNIEnv * env)
101 jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Process", "bind", "J");
106 return (*env)->GetLongField(env, jprocess, id) ? JNI_TRUE : JNI_FALSE;
108 JNIEXPORT void JNICALL
109 Java_org_simgrid_msg_Process_nativeInit(JNIEnv *env, jclass cls) {
110 jclass jprocess_class_Process = (*env)->FindClass(env, "org/simgrid/msg/Process");
112 jprocess_field_Process_pid = jxbt_get_sfield(env, "org/simgrid/msg/Process", "pid", "I");
113 jprocess_field_Process_ppid = jxbt_get_sfield(env, "org/simgrid/msg/Process", "ppid", "I");
114 jprocess_field_Process_host = jxbt_get_jfield(env, jprocess_class_Process, "host", "Lorg/simgrid/msg/Host;");
116 if (!jprocess_class_Process || !jprocess_field_Process_pid ||
117 !jprocess_field_Process_ppid || !jprocess_field_Process_host) {
118 jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
121 JNIEXPORT void JNICALL
122 Java_org_simgrid_msg_Process_create(JNIEnv * env,
123 jobject jprocess_arg,
128 jobject jprocess; /* the global reference to the java process instance */
129 jstring jname; /* the name of the java process instance */
130 const char *name; /* the C name of the process */
131 const char *hostname;
132 m_process_t process; /* the native process to create */
133 m_host_t host; /* Where that process lives */
135 hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
137 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,jproc=%p,host=%s)",
138 env, jprocess_arg, hostname);
141 /* get the name of the java process */
142 jname = jprocess_get_name(jprocess_arg, env);
145 xbt_strdup("Internal error: Process name cannot be NULL"));
149 /* bind/retrieve the msg host */
150 host = MSG_get_host_by_name(hostname);
152 if (!(host)) { /* not binded */
153 jxbt_throw_host_not_found(env, hostname);
157 /* create a global java process instance */
158 jprocess = jprocess_new_global_ref(jprocess_arg, env);
160 jxbt_throw_jni(env, "Can't get a global ref to the java process");
164 /* build the C name of the process */
165 name = (*env)->GetStringUTFChars(env, jname, 0);
166 name = xbt_strdup(name);
168 /* Actually build the MSG process */
169 process = MSG_process_create_with_environment(name,
170 (xbt_main_func_t) jprocess,
174 /*argc, argv, properties*/
177 MSG_process_set_data(process,&process);
178 /* bind the java process instance to the native process */
179 jprocess_bind(jprocess, process, env);
181 /* release our reference to the process name (variable name becomes invalid) */
182 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
183 //(*env)->ReleaseStringUTFChars(env, jname, name);
184 (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
186 /* sets the PID and the PPID of the process */
187 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
188 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
189 /* sets the Host of the process */
190 jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname);
192 (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);
195 JNIEXPORT jint JNICALL
196 Java_org_simgrid_msg_Process_killAll(JNIEnv * env, jclass cls,
199 return (jint) MSG_process_killall((int) jresetPID);
202 JNIEXPORT jobject JNICALL
203 Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls,
206 m_process_t process = MSG_process_from_PID(PID);
209 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
213 jobject jprocess = native_to_java_process(process);
216 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
223 JNIEXPORT jobject JNICALL
224 Java_org_simgrid_msg_Process_currentProcess(JNIEnv * env, jclass cls)
226 m_process_t process = MSG_process_self();
230 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
234 jprocess = native_to_java_process(process);
237 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
242 JNIEXPORT void JNICALL
243 Java_org_simgrid_msg_Process_pause(JNIEnv * env,
246 m_process_t process = jprocess_to_native_process(jprocess, env);
249 jxbt_throw_notbound(env, "process", jprocess);
253 /* try to suspend the process */
254 MSG_error_t rv = MSG_process_suspend(process);
256 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
257 bprintf("unexpected error , please report this bug"));
260 JNIEXPORT void JNICALL
261 Java_org_simgrid_msg_Process_restart(JNIEnv * env,
264 m_process_t process = jprocess_to_native_process(jprocess, env);
267 jxbt_throw_notbound(env, "process", jprocess);
271 /* try to resume the process */
272 MSG_error_t rv = MSG_process_resume(process);
274 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
275 bprintf("unexpected error , please report this bug"));
277 JNIEXPORT jboolean JNICALL
278 Java_org_simgrid_msg_Process_isSuspended(JNIEnv * env,
281 m_process_t process = jprocess_to_native_process(jprocess, env);
284 jxbt_throw_notbound(env, "process", jprocess);
288 /* true is the process is suspended, false otherwise */
289 return (jboolean) MSG_process_is_suspended(process);
292 JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_sleep
293 (JNIEnv *env, jclass cls, jlong jmillis, jint jnanos) {
295 double time = jmillis / 1000 + jnanos / 1000;
297 MSG_error_t rv = MSG_process_sleep(time);
298 jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
299 bprintf("unexpected error , please report this bug"));
301 JNIEXPORT void JNICALL
302 Java_org_simgrid_msg_Process_waitFor(JNIEnv * env, jobject jprocess,
305 m_process_t process = jprocess_to_native_process(jprocess, env);
308 jxbt_throw_notbound(env, "process", jprocess);
311 MSG_error_t rv = MSG_process_sleep((double)jseconds);
313 // smx_ctx_java_stop(smx_ctx_java_self());
317 JNIEXPORT void JNICALL
318 Java_org_simgrid_msg_Process_kill(JNIEnv * env,
321 /* get the native instances from the java ones */
322 m_process_t process = jprocess_to_native_process(jprocess, env);
324 jxbt_throw_notbound(env, "process", jprocess);
327 /* Sets the "killed" flag to kill the process on the next unschedule */
328 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
331 MSG_process_kill(process);
333 JNIEXPORT void JNICALL
334 Java_org_simgrid_msg_Process_migrate(JNIEnv * env,
335 jobject jprocess, jobject jhost)
337 m_process_t process = jprocess_to_native_process(jprocess, env);
340 jxbt_throw_notbound(env, "process", jprocess);
344 m_host_t host = jhost_get_native(env, jhost);
347 jxbt_throw_notbound(env, "host", jhost);
351 /* try to change the host of the process */
352 MSG_error_t rv = MSG_process_migrate(process, host);
353 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
354 bprintf("unexpected error , please report this bug"));
355 /* change the host java side */
356 (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost);