1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007-2014. 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. */
9 #include <simgrid/msg.h>
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
14 #include "smx_context_java.h"
15 #include "smx_context_cojava.h"
17 #include "jmsg_process.h"
21 #include "jmsg_host.h"
22 #include "jmsg_storage.h"
23 #include "jmsg_task.h"
24 #include "jxbt_utilities.h"
28 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
35 /* end of eclipse-mandated pimple */
38 int JAVA_STORAGE_LEVEL;
40 static int create_jprocess(int argc, char *argv[]);
42 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
44 JavaVM *__java_vm = NULL;
46 JavaVM *get_java_VM(void)
51 JNIEnv *get_current_thread_env(void)
55 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
59 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
62 jxbt_throw_time_out_failure(env,NULL);
64 case MSG_TRANSFER_FAILURE:
65 jxbt_throw_transfer_failure(env,NULL);
67 case MSG_HOST_FAILURE:
68 jxbt_throw_host_failure(env,NULL);
70 case MSG_TASK_CANCELED:
71 jxbt_throw_task_cancelled(env,NULL);
74 jxbt_throw_native(env,xbt_strdup("undefined message failed (please see jmsg_throw_status function in jmsg.c)"));
79 /***************************************************************************************
80 * Unsortable functions *
81 ***************************************************************************************/
83 JNIEXPORT jdouble JNICALL
84 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
86 return (jdouble) MSG_get_clock();
89 static void __JAVA_host_priv_free(void *host)
93 static void __JAVA_storage_priv_free(void *storage)
97 JNIEXPORT void JNICALL
98 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
106 XBT_LOG_CONNECT(jmsg);
107 XBT_LOG_CONNECT(jtrace);
109 (*env)->GetJavaVM(env, &__java_vm);
111 if ((*env)->FindClass(env, "java/dyn/Coroutine"))
112 smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
114 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
115 jthrowable exc = (*env)->ExceptionOccurred(env);
117 (*env)->ExceptionClear(env);
120 setlocale(LC_NUMERIC,"C");
123 argc = (int) (*env)->GetArrayLength(env, jargs);
126 argv = xbt_new(char *, argc + 1);
127 argv[0] = strdup("java");
129 for (index = 0; index < argc - 1; index++) {
130 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
131 tmp = (*env)->GetStringUTFChars(env, jval, 0);
132 argv[index + 1] = strdup(tmp);
133 (*env)->ReleaseStringUTFChars(env, jval, tmp);
137 MSG_init(&argc, argv);
139 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
140 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
142 for (index = 0; index < argc; index++)
147 if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init)
148 XBT_INFO("Using Coroutines. Your simulation is on steroid.");
149 else if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
150 XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
152 xbt_die("Unknown context factory. Please report bug.");
155 JNIEXPORT void JNICALL
156 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
160 xbt_dynar_t hosts, storages;
161 jobject jhost, jstorage;
165 XBT_DEBUG("Ready to run MSG_MAIN");
167 XBT_DEBUG("Done running MSG_MAIN");
168 jxbt_check_res("MSG_main()", rv, MSG_OK,
169 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
171 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
172 /* Cleanup java hosts */
173 hosts = MSG_hosts_as_dynar();
174 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
175 jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
177 jhost_unref(env, jhost);
180 xbt_dynar_free(&hosts);
182 /* Cleanup java storages */
183 storages = MSG_storages_as_dynar();
184 if(!xbt_dynar_is_empty(storages)){
185 for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
186 jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
188 jstorage_unref(env, jstorage);
191 xbt_dynar_free(&storages);
195 JNIEXPORT void JNICALL
196 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
197 jstring jplatformFile)
200 const char *platformFile =
201 (*env)->GetStringUTFChars(env, jplatformFile, 0);
203 MSG_create_environment(platformFile);
205 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
208 JNIEXPORT jobject JNICALL
209 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
211 msg_as_t as = MSG_environment_get_routing_root();
212 jobject jas = jas_new_instance(env);
214 jxbt_throw_jni(env, "java As instantiation failed");
217 jas = jas_ref(env, jas);
219 jxbt_throw_jni(env, "new global ref allocation failed");
222 jas_bind(jas, as, env);
224 return (jobject) jas;
227 JNIEXPORT void JNICALL
228 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
230 const char *s = (*env)->GetStringUTFChars(env, js, 0);
232 (*env)->ReleaseStringUTFChars(env, js, s);
234 JNIEXPORT void JNICALL
235 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
237 const char *s = (*env)->GetStringUTFChars(env, js, 0);
239 (*env)->ReleaseStringUTFChars(env, js, s);
241 JNIEXPORT void JNICALL
242 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
244 const char *s = (*env)->GetStringUTFChars(env, js, 0);
246 (*env)->ReleaseStringUTFChars(env, js, s);
248 JNIEXPORT void JNICALL
249 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
251 const char *s = (*env)->GetStringUTFChars(env, js, 0);
253 (*env)->ReleaseStringUTFChars(env, js, s);
255 JNIEXPORT void JNICALL
256 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
258 const char *s = (*env)->GetStringUTFChars(env, js, 0);
260 (*env)->ReleaseStringUTFChars(env, js, s);
262 JNIEXPORT void JNICALL
263 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
265 const char *s = (*env)->GetStringUTFChars(env, js, 0);
266 XBT_CRITICAL("%s", s);
267 (*env)->ReleaseStringUTFChars(env, js, s);
269 JNIEXPORT void JNICALL
270 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
271 jstring jdeploymentFile)
274 const char *deploymentFile =
275 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
277 SIMIX_function_register_default(create_jprocess);
278 MSG_launch_application(deploymentFile);
281 * Function called when there is the need to create the java Process object
282 * (when we are using deployement files).
283 * it HAS to be executed on the process context, else really bad things will happen.
285 static int create_jprocess(int argc, char *argv[]) {
286 JNIEnv *env = get_current_thread_env();
287 //Change the "." in class name for "/".
288 xbt_str_subst(argv[0],'.','/',0);
289 jclass class_Process = (*env)->FindClass(env, argv[0]);
290 xbt_str_subst(argv[0],'/','.',0);
291 //Retrieve the methodID for the constructor
292 xbt_assert((class_Process != NULL), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]);
293 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
294 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
296 //Retrieve the name of the process.
297 jstring jname = (*env)->NewStringUTF(env, argv[0]);
298 //Build the arguments
299 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
300 (*env)->FindClass(env,"java/lang/String"),
301 (*env)->NewStringUTF(env,"")));
303 for (i = 1; i < argc; i++)
304 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
305 //Retrieve the host for the process.
306 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
307 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
308 //creates the process
309 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
310 xbt_assert((jprocess != NULL), "Process allocation failed.");
311 jprocess = (*env)->NewGlobalRef(env, jprocess);
312 //bind the process to the context
313 msg_process_t process = MSG_process_self();
314 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
315 context->jprocess = jprocess;
316 /* sets the PID and the PPID of the process */
317 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
318 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
319 jprocess_bind(jprocess, process, env);