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 for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
184 jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
186 jstorage_unref(env, jstorage);
189 xbt_dynar_free(&storages);
193 JNIEXPORT void JNICALL
194 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
195 jstring jplatformFile)
198 const char *platformFile =
199 (*env)->GetStringUTFChars(env, jplatformFile, 0);
201 MSG_create_environment(platformFile);
203 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
206 JNIEXPORT jobject JNICALL
207 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
209 msg_as_t as = MSG_environment_get_routing_root();
210 jobject jas = jas_new_instance(env);
212 jxbt_throw_jni(env, "java As instantiation failed");
215 jas = jas_ref(env, jas);
217 jxbt_throw_jni(env, "new global ref allocation failed");
220 jas_bind(jas, as, env);
222 return (jobject) jas;
225 JNIEXPORT void JNICALL
226 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
228 const char *s = (*env)->GetStringUTFChars(env, js, 0);
230 (*env)->ReleaseStringUTFChars(env, js, s);
232 JNIEXPORT void JNICALL
233 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
235 const char *s = (*env)->GetStringUTFChars(env, js, 0);
237 (*env)->ReleaseStringUTFChars(env, js, s);
239 JNIEXPORT void JNICALL
240 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
242 const char *s = (*env)->GetStringUTFChars(env, js, 0);
244 (*env)->ReleaseStringUTFChars(env, js, s);
246 JNIEXPORT void JNICALL
247 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
249 const char *s = (*env)->GetStringUTFChars(env, js, 0);
251 (*env)->ReleaseStringUTFChars(env, js, s);
253 JNIEXPORT void JNICALL
254 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
256 const char *s = (*env)->GetStringUTFChars(env, js, 0);
258 (*env)->ReleaseStringUTFChars(env, js, s);
260 JNIEXPORT void JNICALL
261 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
263 const char *s = (*env)->GetStringUTFChars(env, js, 0);
264 XBT_CRITICAL("%s", s);
265 (*env)->ReleaseStringUTFChars(env, js, s);
267 JNIEXPORT void JNICALL
268 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
269 jstring jdeploymentFile)
272 const char *deploymentFile =
273 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
275 SIMIX_function_register_default(create_jprocess);
276 MSG_launch_application(deploymentFile);
279 * Function called when there is the need to create the java Process object
280 * (when we are using deployement files).
281 * it HAS to be executed on the process context, else really bad things will happen.
283 static int create_jprocess(int argc, char *argv[]) {
284 JNIEnv *env = get_current_thread_env();
285 //Change the "." in class name for "/".
286 xbt_str_subst(argv[0],'.','/',0);
287 jclass class_Process = (*env)->FindClass(env, argv[0]);
288 xbt_str_subst(argv[0],'/','.',0);
289 //Retrieve the methodID for the constructor
290 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]);
291 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
292 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
294 //Retrieve the name of the process.
295 jstring jname = (*env)->NewStringUTF(env, argv[0]);
296 //Build the arguments
297 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
298 (*env)->FindClass(env,"java/lang/String"),
299 (*env)->NewStringUTF(env,"")));
301 for (i = 1; i < argc; i++)
302 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
303 //Retrieve the host for the process.
304 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
305 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
306 //creates the process
307 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
308 xbt_assert((jprocess != NULL), "Process allocation failed.");
309 jprocess = (*env)->NewGlobalRef(env, jprocess);
310 //bind the process to the context
311 msg_process_t process = MSG_process_self();
312 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
313 context->jprocess = jprocess;
314 /* sets the PID and the PPID of the process */
315 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
316 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
317 jprocess_bind(jprocess, process, env);