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);
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);
108 XBT_LOG_CONNECT(jtrace);
111 (*env)->GetJavaVM(env, &__java_vm);
113 if ((*env)->FindClass(env, "java/dyn/Coroutine"))
114 smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
116 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
117 jthrowable exc = (*env)->ExceptionOccurred(env);
119 (*env)->ExceptionClear(env);
122 setlocale(LC_NUMERIC,"C");
125 argc = (int) (*env)->GetArrayLength(env, jargs);
128 argv = xbt_new(char *, argc + 1);
129 argv[0] = strdup("java");
131 for (index = 0; index < argc - 1; index++) {
132 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
133 tmp = (*env)->GetStringUTFChars(env, jval, 0);
134 argv[index + 1] = strdup(tmp);
135 (*env)->ReleaseStringUTFChars(env, jval, tmp);
139 MSG_init(&argc, argv);
141 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
142 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
144 for (index = 0; index < argc; index++)
149 if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init)
150 XBT_INFO("Using Coroutines. Your simulation is on steroid.");
151 else if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
152 XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
154 xbt_die("Unknown context factory. Please report bug.");
157 JNIEXPORT void JNICALL
158 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
162 xbt_dynar_t hosts, storages;
163 jobject jhost, jstorage;
167 XBT_DEBUG("Ready to run MSG_MAIN");
169 XBT_DEBUG("Done running MSG_MAIN");
170 jxbt_check_res("MSG_main()", rv, MSG_OK,
171 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
173 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
174 /* Cleanup java hosts */
175 hosts = MSG_hosts_as_dynar();
176 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
177 jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
179 jhost_unref(env, jhost);
182 xbt_dynar_free(&hosts);
184 /* Cleanup java storages */
185 storages = MSG_storages_as_dynar();
186 if(!xbt_dynar_is_empty(storages)){
187 for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
188 jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
190 jstorage_unref(env, jstorage);
193 xbt_dynar_free(&storages);
197 JNIEXPORT void JNICALL
198 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
199 jstring jplatformFile)
202 const char *platformFile =
203 (*env)->GetStringUTFChars(env, jplatformFile, 0);
205 MSG_create_environment(platformFile);
207 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
210 JNIEXPORT jobject JNICALL
211 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
213 msg_as_t as = MSG_environment_get_routing_root();
214 jobject jas = jas_new_instance(env);
216 jxbt_throw_jni(env, "java As instantiation failed");
219 jas = jas_ref(env, jas);
221 jxbt_throw_jni(env, "new global ref allocation failed");
224 jas_bind(jas, as, env);
226 return (jobject) jas;
229 JNIEXPORT void JNICALL
230 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
232 const char *s = (*env)->GetStringUTFChars(env, js, 0);
234 (*env)->ReleaseStringUTFChars(env, js, s);
236 JNIEXPORT void JNICALL
237 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
239 const char *s = (*env)->GetStringUTFChars(env, js, 0);
241 (*env)->ReleaseStringUTFChars(env, js, s);
243 JNIEXPORT void JNICALL
244 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
246 const char *s = (*env)->GetStringUTFChars(env, js, 0);
248 (*env)->ReleaseStringUTFChars(env, js, s);
250 JNIEXPORT void JNICALL
251 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
253 const char *s = (*env)->GetStringUTFChars(env, js, 0);
255 (*env)->ReleaseStringUTFChars(env, js, s);
257 JNIEXPORT void JNICALL
258 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
260 const char *s = (*env)->GetStringUTFChars(env, js, 0);
262 (*env)->ReleaseStringUTFChars(env, js, s);
264 JNIEXPORT void JNICALL
265 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
267 const char *s = (*env)->GetStringUTFChars(env, js, 0);
268 XBT_CRITICAL("%s", s);
269 (*env)->ReleaseStringUTFChars(env, js, s);
271 JNIEXPORT void JNICALL
272 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
273 jstring jdeploymentFile)
276 const char *deploymentFile =
277 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
279 SIMIX_function_register_default(create_jprocess);
280 MSG_launch_application(deploymentFile);
283 * Function called when there is the need to create the java Process object
284 * (when we are using deployement files).
285 * it HAS to be executed on the process context, else really bad things will happen.
287 static int create_jprocess(int argc, char *argv[]) {
288 JNIEnv *env = get_current_thread_env();
289 //Change the "." in class name for "/".
290 xbt_str_subst(argv[0],'.','/',0);
291 jclass class_Process = (*env)->FindClass(env, argv[0]);
292 xbt_str_subst(argv[0],'/','.',0);
293 //Retrieve the methodID for the constructor
294 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]);
295 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
296 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
298 //Retrieve the name of the process.
299 jstring jname = (*env)->NewStringUTF(env, argv[0]);
300 //Build the arguments
301 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
302 (*env)->FindClass(env,"java/lang/String"),
303 (*env)->NewStringUTF(env,"")));
305 for (i = 1; i < argc; i++)
306 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
307 //Retrieve the host for the process.
308 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
309 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
310 //creates the process
311 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
312 xbt_assert((jprocess != NULL), "Process allocation failed.");
313 jprocess = (*env)->NewGlobalRef(env, jprocess);
314 //bind the process to the context
315 msg_process_t process = MSG_process_self();
316 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
317 context->jprocess = jprocess;
318 /* sets the PID and the PPID of the process */
319 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
320 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
321 jprocess_bind(jprocess, process, env);