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. */
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);
71 jxbt_throw_native(env,xbt_strdup("communication failed"));
76 /***************************************************************************************
77 * Unsortable functions *
78 ***************************************************************************************/
80 JNIEXPORT jdouble JNICALL
81 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
83 return (jdouble) MSG_get_clock();
86 static void __JAVA_host_priv_free(void *host)
90 static void __JAVA_storage_priv_free(void *storage)
94 JNIEXPORT void JNICALL
95 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
103 XBT_LOG_CONNECT(jmsg);
105 XBT_LOG_CONNECT(jtrace);
108 (*env)->GetJavaVM(env, &__java_vm);
110 if ((*env)->FindClass(env, "java/dyn/Coroutine"))
111 smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
113 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
114 jthrowable exc = (*env)->ExceptionOccurred(env);
116 (*env)->ExceptionClear(env);
119 setlocale(LC_NUMERIC,"C");
122 argc = (int) (*env)->GetArrayLength(env, jargs);
125 argv = xbt_new(char *, argc + 1);
126 argv[0] = strdup("java");
128 for (index = 0; index < argc - 1; index++) {
129 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
130 tmp = (*env)->GetStringUTFChars(env, jval, 0);
131 argv[index + 1] = strdup(tmp);
132 (*env)->ReleaseStringUTFChars(env, jval, tmp);
136 MSG_init(&argc, argv);
138 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
139 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
141 for (index = 0; index < argc; index++)
146 if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init)
147 XBT_INFO("Using Coroutines. Your simulation is on steroid.");
148 else if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
149 XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
151 xbt_die("Unknown context factory. Please report bug.");
154 JNIEXPORT void JNICALL
155 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
159 xbt_dynar_t hosts, storages;
160 jobject jhost, jstorage;
164 XBT_DEBUG("Ready to run MSG_MAIN");
166 XBT_DEBUG("Done running MSG_MAIN");
167 jxbt_check_res("MSG_main()", rv, MSG_OK,
168 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
170 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
171 /* Cleanup java hosts */
172 hosts = MSG_hosts_as_dynar();
173 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
174 jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
176 jhost_unref(env, jhost);
179 xbt_dynar_free(&hosts);
181 /* Cleanup java storages */
182 storages = MSG_storages_as_dynar();
183 if(!xbt_dynar_is_empty(storages)){
184 for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
185 jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
187 jstorage_unref(env, jstorage);
190 xbt_dynar_free(&storages);
194 JNIEXPORT void JNICALL
195 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
196 jstring jplatformFile)
199 const char *platformFile =
200 (*env)->GetStringUTFChars(env, jplatformFile, 0);
202 MSG_create_environment(platformFile);
204 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
207 JNIEXPORT jobject JNICALL
208 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
210 msg_as_t as = MSG_environment_get_routing_root();
211 jobject jas = jas_new_instance(env);
213 jxbt_throw_jni(env, "java As instantiation failed");
216 jas = jas_ref(env, jas);
218 jxbt_throw_jni(env, "new global ref allocation failed");
221 jas_bind(jas, as, env);
223 return (jobject) jas;
226 JNIEXPORT void JNICALL
227 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
229 const char *s = (*env)->GetStringUTFChars(env, js, 0);
231 (*env)->ReleaseStringUTFChars(env, js, s);
233 JNIEXPORT void JNICALL
234 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
236 const char *s = (*env)->GetStringUTFChars(env, js, 0);
238 (*env)->ReleaseStringUTFChars(env, js, s);
240 JNIEXPORT void JNICALL
241 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
243 const char *s = (*env)->GetStringUTFChars(env, js, 0);
245 (*env)->ReleaseStringUTFChars(env, js, s);
247 JNIEXPORT void JNICALL
248 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
250 const char *s = (*env)->GetStringUTFChars(env, js, 0);
252 (*env)->ReleaseStringUTFChars(env, js, s);
254 JNIEXPORT void JNICALL
255 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
257 const char *s = (*env)->GetStringUTFChars(env, js, 0);
259 (*env)->ReleaseStringUTFChars(env, js, s);
261 JNIEXPORT void JNICALL
262 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
264 const char *s = (*env)->GetStringUTFChars(env, js, 0);
265 XBT_CRITICAL("%s", s);
266 (*env)->ReleaseStringUTFChars(env, js, s);
268 JNIEXPORT void JNICALL
269 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
270 jstring jdeploymentFile)
273 const char *deploymentFile =
274 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
276 SIMIX_function_register_default(create_jprocess);
277 MSG_launch_application(deploymentFile);
280 * Function called when there is the need to create the java Process object
281 * (when we are using deployement files).
282 * it HAS to be executed on the process context, else really bad things will happen.
284 static int create_jprocess(int argc, char *argv[]) {
285 JNIEnv *env = get_current_thread_env();
286 //Change the "." in class name for "/".
287 xbt_str_subst(argv[0],'.','/',0);
288 jclass class_Process = (*env)->FindClass(env, argv[0]);
289 xbt_str_subst(argv[0],'/','.',0);
290 //Retrieve the methodID for the constructor
291 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]);
292 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
293 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
295 //Retrieve the name of the process.
296 jstring jname = (*env)->NewStringUTF(env, argv[0]);
297 //Build the arguments
298 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
299 (*env)->FindClass(env,"java/lang/String"),
300 (*env)->NewStringUTF(env,"")));
302 for (i = 1; i < argc; i++)
303 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
304 //Retrieve the host for the process.
305 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
306 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
307 //creates the process
308 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
309 xbt_assert((jprocess != NULL), "Process allocation failed.");
310 jprocess = (*env)->NewGlobalRef(env, jprocess);
311 //bind the process to the context
312 msg_process_t process = MSG_process_self();
313 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
314 context->jprocess = jprocess;
315 /* sets the PID and the PPID of the process */
316 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
317 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
318 jprocess_bind(jprocess, process, env);