1 /* Java Wrappers to the MSG API. */
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. */
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
14 #include "smx_context_java.h"
16 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
25 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
32 /* end of eclipse-mandated pimple */
34 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
36 static JavaVM *__java_vm = NULL;
39 JavaVM *get_java_VM(void)
44 JNIEnv *get_current_thread_env(void)
48 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
54 * The MSG process connected functions implementation.
57 JNIEXPORT void JNICALL
58 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
64 jobject jprocess; /* the global reference to the java process instance */
65 jstring jname; /* the name of the java process instance */
66 const char *name; /* the C name of the process */
68 m_process_t process; /* the native process to create */
69 m_host_t host; /* Where that process lives */
71 hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
73 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,host=%s)",
74 env, cls, jprocess_arg, hostname);
77 /* get the name of the java process */
78 jname = jprocess_get_name(jprocess_arg, env);
81 xbt_strdup("Internal error: Process name cannot be NULL"));
85 /* bind/retrieve the msg host */
86 host = MSG_get_host_by_name(hostname);
88 if (!(host)) { /* not binded */
89 jxbt_throw_host_not_found(env, hostname);
93 /* create a global java process instance */
94 jprocess = jprocess_new_global_ref(jprocess_arg, env);
96 jxbt_throw_jni(env, "Can't get a global ref to the java process");
100 /* build the C name of the process */
101 name = (*env)->GetStringUTFChars(env, jname, 0);
102 name = xbt_strdup(name);
104 /* Actually build the MSG process */
105 process = MSG_process_create_with_environment(name,
106 (xbt_main_func_t) jprocess,
110 /*argc, argv, properties*/
113 MSG_process_set_data(process,&process);
115 /* release our reference to the process name (variable name becomes invalid) */
116 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
117 //(*env)->ReleaseStringUTFChars(env, jname, name);
118 (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
120 /* bind the java process instance to the native process */
121 jprocess_bind(jprocess, process, env);
125 JNIEXPORT void JNICALL
126 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
129 m_process_t process = jprocess_to_native_process(jprocess, env);
132 jxbt_throw_notbound(env, "process", jprocess);
136 /* try to suspend the process */
137 MSG_error_t rv = MSG_process_suspend(process);
139 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
140 bprintf("unexpected error , please report this bug"));
144 JNIEXPORT void JNICALL
145 Java_org_simgrid_msg_Process_simulatedSleep(JNIEnv * env, jobject jprocess,
147 m_process_t process = jprocess_to_native_process(jprocess, env);
150 jxbt_throw_notbound(env, "process", jprocess);
153 MSG_error_t rv = MSG_process_sleep((double)jseconds);
155 jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
156 bprintf("unexpected error , please report this bug"));
160 JNIEXPORT void JNICALL
161 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
164 m_process_t process = jprocess_to_native_process(jprocess, env);
167 jxbt_throw_notbound(env, "process", jprocess);
171 /* try to resume the process */
172 MSG_error_t rv = MSG_process_resume(process);
174 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
175 bprintf("unexpected error , please report this bug"));
178 JNIEXPORT jboolean JNICALL
179 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
182 m_process_t process = jprocess_to_native_process(jprocess, env);
185 jxbt_throw_notbound(env, "process", jprocess);
189 /* true is the process is suspended, false otherwise */
190 return (jboolean) MSG_process_is_suspended(process);
193 JNIEXPORT void JNICALL
194 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
197 /* get the native instances from the java ones */
198 m_process_t process = jprocess_to_native_process(jprocess, env);
201 jxbt_throw_notbound(env, "process", jprocess);
205 /* kill the native process (this wrapper is call by the destructor of the java
208 MSG_process_kill(process);
211 JNIEXPORT jobject JNICALL
212 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
215 /* get the native instances from the java ones */
216 m_process_t process = jprocess_to_native_process(jprocess, env);
220 jxbt_throw_notbound(env, "process", jprocess);
224 host = MSG_process_get_host(process);
225 jobject res = (jobject)MSG_host_get_data(host);
228 XBT_INFO("Binding error for host %s ",MSG_host_get_name(host));
229 jxbt_throw_jni(env, bprintf("Binding error for host %s ",MSG_host_get_name(host)));
233 /* return the global reference to the java host instance */
238 JNIEXPORT jobject JNICALL
239 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
242 m_process_t process = MSG_process_from_PID(PID);
245 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
249 if (!native_to_java_process(process)) {
250 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
254 return (jobject) (native_to_java_process(process));
258 JNIEXPORT jint JNICALL
259 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
262 m_process_t process = jprocess_to_native_process(jprocess, env);
265 jxbt_throw_notbound(env, "process", jprocess);
269 return (jint) MSG_process_get_PID(process);
273 JNIEXPORT jint JNICALL
274 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
277 m_process_t process = jprocess_to_native_process(jprocess, env);
280 jxbt_throw_notbound(env, "process", jprocess);
284 return (jint) MSG_process_get_PPID(process);
287 JNIEXPORT jobject JNICALL
288 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
290 m_process_t process = MSG_process_self();
294 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
298 jprocess = native_to_java_process(process);
301 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
306 JNIEXPORT void JNICALL
307 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
308 jobject jprocess, jobject jhost)
310 m_process_t process = jprocess_to_native_process(jprocess, env);
313 jxbt_throw_notbound(env, "process", jprocess);
317 m_host_t host = jhost_get_native(env, jhost);
320 jxbt_throw_notbound(env, "host", jhost);
324 /* try to change the host of the process */
325 MSG_error_t rv = MSG_process_migrate(process, host);
326 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
327 bprintf("unexpected error , please report this bug"));
331 JNIEXPORT void JNICALL
332 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
335 MSG_error_t rv = MSG_process_sleep((double) seconds);
337 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
338 bprintf("while process was waiting for %f seconds",
344 /***************************************************************************************
345 * The MSG host connected functions implementation. *
346 ***************************************************************************************/
348 JNIEXPORT jint JNICALL
349 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
352 m_host_t host = jhost_get_native(env, jhost);
355 jxbt_throw_notbound(env, "host", jhost);
359 return (jint) MSG_get_host_msgload(host);
363 /***************************************************************************************
364 * Unsortable functions *
365 ***************************************************************************************/
367 JNIEXPORT jdouble JNICALL
368 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
370 return (jdouble) MSG_get_clock();
373 JNIEXPORT void JNICALL
374 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
382 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
385 argc = (int) (*env)->GetArrayLength(env, jargs);
388 argv = xbt_new(char *, argc + 1);
389 argv[0] = strdup("java");
391 for (index = 0; index < argc - 1; index++) {
392 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
393 tmp = (*env)->GetStringUTFChars(env, jval, 0);
394 argv[index + 1] = strdup(tmp);
395 (*env)->ReleaseStringUTFChars(env, jval, tmp);
399 MSG_global_init(&argc, argv);
401 for (index = 0; index < argc; index++)
406 (*env)->GetJavaVM(env, &__java_vm);
409 JNIEXPORT void JNICALL
410 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
418 XBT_INFO("Ready to run MSG_MAIN");
420 XBT_INFO("Done running MSG_MAIN");
421 jxbt_check_res("MSG_main()", rv, MSG_OK,
423 ("unexpected error : MSG_main() failed .. please report this bug "));
425 XBT_INFO("MSG_main finished");
427 XBT_INFO("Clean java world");
428 /* Cleanup java hosts */
429 hosts = MSG_hosts_as_dynar();
430 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
431 jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
433 jhost_unref(env, jhost);
436 xbt_dynar_free(&hosts);
437 XBT_INFO("Clean native world");
439 JNIEXPORT void JNICALL
440 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
442 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
443 MSG_error_t rv = MSG_OK != MSG_clean();
444 jxbt_check_res("MSG_clean()", rv, MSG_OK,
446 ("unexpected error : MSG_clean() failed .. please report this bug "));
449 JNIEXPORT jint JNICALL
450 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
453 return (jint) MSG_process_killall((int) jresetPID);
456 JNIEXPORT void JNICALL
457 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
458 jstring jplatformFile)
461 const char *platformFile =
462 (*env)->GetStringUTFChars(env, jplatformFile, 0);
464 MSG_create_environment(platformFile);
466 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
469 JNIEXPORT void JNICALL
470 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
474 m_process_t process = jprocess_to_native_process(jprocess, env);
477 jxbt_throw_notbound(env, "process", jprocess);
481 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
484 JNIEXPORT void JNICALL
485 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
487 const char *s = (*env)->GetStringUTFChars(env, js, 0);
489 (*env)->ReleaseStringUTFChars(env, js, s);
492 JNIEXPORT jobjectArray JNICALL
493 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
501 xbt_dynar_t table = MSG_hosts_as_dynar();
502 int count = xbt_dynar_length(table);
504 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
510 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
513 jxbt_throw_jni(env, "Hosts table allocation failed");
517 for (index = 0; index < count; index++) {
518 host = xbt_dynar_get_as(table,index,m_host_t);
519 jhost = (jobject) (MSG_host_get_data(host));
522 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
525 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
526 /* FIXME: leak of jname ? */
529 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
531 xbt_dynar_free(&table);
535 JNIEXPORT void JNICALL
536 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
537 jstring jdeploymentFile)
540 const char *deploymentFile =
541 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
543 surf_parse_reset_callbacks();
545 surfxml_add_callback(STag_surfxml_process_cb_list,
546 japplication_handler_on_begin_process);
548 surfxml_add_callback(ETag_surfxml_argument_cb_list,
549 japplication_handler_on_process_arg);
551 surfxml_add_callback(STag_surfxml_prop_cb_list,
552 japplication_handler_on_property);
554 surfxml_add_callback(ETag_surfxml_process_cb_list,
555 japplication_handler_on_end_process);
557 surf_parse_open(deploymentFile);
559 japplication_handler_on_start_document();
562 jxbt_throw_jni(env, "surf_parse() failed");
566 japplication_handler_on_end_document();
568 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);