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_task.h"
23 #include "jxbt_utilities.h"
27 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
34 /* end of eclipse-mandated pimple */
38 static int create_jprocess(int argc, char *argv[]);
40 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
42 JavaVM *__java_vm = NULL;
44 JavaVM *get_java_VM(void)
49 JNIEnv *get_current_thread_env(void)
53 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
57 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
60 jxbt_throw_time_out_failure(env,NULL);
62 case MSG_TRANSFER_FAILURE:
63 jxbt_throw_transfer_failure(env,NULL);
65 case MSG_HOST_FAILURE:
66 jxbt_throw_host_failure(env,NULL);
69 jxbt_throw_native(env,xbt_strdup("communication failed"));
74 /***************************************************************************************
75 * Unsortable functions *
76 ***************************************************************************************/
78 JNIEXPORT jdouble JNICALL
79 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
81 return (jdouble) MSG_get_clock();
84 static void __JAVA_host_priv_free(void *host)
88 JNIEXPORT void JNICALL
89 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
97 XBT_LOG_CONNECT(jmsg);
99 XBT_LOG_CONNECT(jtrace);
102 (*env)->GetJavaVM(env, &__java_vm);
104 if ((*env)->FindClass(env, "java/dyn/Coroutine"))
105 smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
107 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
108 jthrowable exc = (*env)->ExceptionOccurred(env);
110 (*env)->ExceptionClear(env);
113 setlocale(LC_NUMERIC,"C");
116 argc = (int) (*env)->GetArrayLength(env, jargs);
119 argv = xbt_new(char *, argc + 1);
120 argv[0] = strdup("java");
122 for (index = 0; index < argc - 1; index++) {
123 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
124 tmp = (*env)->GetStringUTFChars(env, jval, 0);
125 argv[index + 1] = strdup(tmp);
126 (*env)->ReleaseStringUTFChars(env, jval, tmp);
130 MSG_init(&argc, argv);
132 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
134 for (index = 0; index < argc; index++)
139 if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init)
140 XBT_INFO("Using Coroutines. Your simulation is on steroid.");
141 else if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
142 XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
144 xbt_die("Unknown context factory. Please report bug.");
147 JNIEXPORT void JNICALL
148 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
156 XBT_DEBUG("Ready to run MSG_MAIN");
158 XBT_DEBUG("Done running MSG_MAIN");
159 jxbt_check_res("MSG_main()", rv, MSG_OK,
160 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
162 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
163 /* Cleanup java hosts */
164 hosts = MSG_hosts_as_dynar();
165 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
166 jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
168 jhost_unref(env, jhost);
171 xbt_dynar_free(&hosts);
174 JNIEXPORT void JNICALL
175 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
176 jstring jplatformFile)
179 const char *platformFile =
180 (*env)->GetStringUTFChars(env, jplatformFile, 0);
182 MSG_create_environment(platformFile);
184 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
187 JNIEXPORT jobject JNICALL
188 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
190 msg_as_t as = MSG_environment_get_routing_root();
191 jobject jas = jas_new_instance(env);
193 jxbt_throw_jni(env, "java As instantiation failed");
196 jas = jas_ref(env, jas);
198 jxbt_throw_jni(env, "new global ref allocation failed");
201 jas_bind(jas, as, env);
203 return (jobject) jas;
206 JNIEXPORT void JNICALL
207 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
209 const char *s = (*env)->GetStringUTFChars(env, js, 0);
211 (*env)->ReleaseStringUTFChars(env, js, s);
213 JNIEXPORT void JNICALL
214 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
216 const char *s = (*env)->GetStringUTFChars(env, js, 0);
218 (*env)->ReleaseStringUTFChars(env, js, s);
220 JNIEXPORT void JNICALL
221 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
223 const char *s = (*env)->GetStringUTFChars(env, js, 0);
225 (*env)->ReleaseStringUTFChars(env, js, s);
227 JNIEXPORT void JNICALL
228 Java_org_simgrid_msg_Msg_warn(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_error(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_critical(JNIEnv * env, jclass cls, jstring js)
244 const char *s = (*env)->GetStringUTFChars(env, js, 0);
245 XBT_CRITICAL("%s", s);
246 (*env)->ReleaseStringUTFChars(env, js, s);
248 JNIEXPORT void JNICALL
249 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
250 jstring jdeploymentFile)
253 const char *deploymentFile =
254 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
256 SIMIX_function_register_default(create_jprocess);
257 MSG_launch_application(deploymentFile);
260 * Function called when there is the need to create the java Process object
261 * (when we are using deployement files).
262 * it HAS to be executed on the process context, else really bad things will happen.
264 static int create_jprocess(int argc, char *argv[]) {
265 JNIEnv *env = get_current_thread_env();
266 //Change the "." in class name for "/".
267 xbt_str_subst(argv[0],'.','/',0);
268 jclass class_Process = (*env)->FindClass(env, argv[0]);
269 xbt_str_subst(argv[0],'/','.',0);
270 //Retrieve the methodID for the constructor
271 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]);
272 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
273 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
275 //Retrieve the name of the process.
276 jstring jname = (*env)->NewStringUTF(env, argv[0]);
277 //Build the arguments
278 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
279 (*env)->FindClass(env,"java/lang/String"),
280 (*env)->NewStringUTF(env,"")));
282 for (i = 1; i < argc; i++)
283 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
284 //Retrieve the host for the process.
285 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
286 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
287 //creates the process
288 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
289 xbt_assert((jprocess != NULL), "Process allocation failed.");
290 jprocess = (*env)->NewGlobalRef(env, jprocess);
291 //bind the process to the context
292 msg_process_t process = MSG_process_self();
293 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
294 context->jprocess = jprocess;
295 /* sets the PID and the PPID of the process */
296 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
297 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
298 jprocess_bind(jprocess, process, env);