1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007-2012. 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 */
36 static int create_jprocess(int argc, char *argv[]);
38 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
40 JavaVM *__java_vm = NULL;
42 JavaVM *get_java_VM(void)
47 JNIEnv *get_current_thread_env(void)
51 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
55 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
58 jxbt_throw_time_out_failure(env,NULL);
60 case MSG_TRANSFER_FAILURE:
61 jxbt_throw_transfer_failure(env,NULL);
63 case MSG_HOST_FAILURE:
64 jxbt_throw_host_failure(env,NULL);
67 jxbt_throw_native(env,xbt_strdup("communication failed"));
72 /***************************************************************************************
73 * Unsortable functions *
74 ***************************************************************************************/
76 JNIEXPORT jdouble JNICALL
77 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
79 return (jdouble) MSG_get_clock();
82 JNIEXPORT void JNICALL
83 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
91 XBT_LOG_CONNECT(jmsg);
93 XBT_LOG_CONNECT(jtrace);
96 (*env)->GetJavaVM(env, &__java_vm);
98 if ((*env)->FindClass(env, "java/dyn/Coroutine")) {
99 XBT_INFO("Using Coroutines. Your simulation is on steroid.");
100 smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
103 XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
104 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
106 jthrowable exc = (*env)->ExceptionOccurred(env);
108 (*env)->ExceptionClear(env);
111 setlocale(LC_NUMERIC,"C");
114 argc = (int) (*env)->GetArrayLength(env, jargs);
117 argv = xbt_new(char *, argc + 1);
118 argv[0] = strdup("java");
120 for (index = 0; index < argc - 1; index++) {
121 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
122 tmp = (*env)->GetStringUTFChars(env, jval, 0);
123 argv[index + 1] = strdup(tmp);
124 (*env)->ReleaseStringUTFChars(env, jval, tmp);
128 MSG_init(&argc, argv);
130 for (index = 0; index < argc; index++)
136 JNIEXPORT void JNICALL
137 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
145 XBT_DEBUG("Ready to run MSG_MAIN");
147 XBT_DEBUG("Done running MSG_MAIN");
148 jxbt_check_res("MSG_main()", rv, MSG_OK,
149 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
151 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
152 /* Cleanup java hosts */
153 hosts = MSG_hosts_as_dynar();
154 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
155 jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,msg_host_t));
157 jhost_unref(env, jhost);
160 xbt_dynar_free(&hosts);
163 JNIEXPORT void JNICALL
164 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
165 jstring jplatformFile)
168 const char *platformFile =
169 (*env)->GetStringUTFChars(env, jplatformFile, 0);
171 MSG_create_environment(platformFile);
173 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
176 JNIEXPORT jobject JNICALL
177 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
179 msg_as_t as = MSG_environment_get_routing_root();
180 jobject jas = jas_new_instance(env);
182 jxbt_throw_jni(env, "java As instantiation failed");
185 jas = jas_ref(env, jas);
187 jxbt_throw_jni(env, "new global ref allocation failed");
190 jas_bind(jas, as, env);
192 return (jobject) jas;
195 JNIEXPORT void JNICALL
196 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
198 const char *s = (*env)->GetStringUTFChars(env, js, 0);
200 (*env)->ReleaseStringUTFChars(env, js, s);
202 JNIEXPORT void JNICALL
203 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
205 const char *s = (*env)->GetStringUTFChars(env, js, 0);
207 (*env)->ReleaseStringUTFChars(env, js, s);
209 JNIEXPORT void JNICALL
210 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
212 const char *s = (*env)->GetStringUTFChars(env, js, 0);
214 (*env)->ReleaseStringUTFChars(env, js, s);
216 JNIEXPORT void JNICALL
217 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
219 const char *s = (*env)->GetStringUTFChars(env, js, 0);
221 (*env)->ReleaseStringUTFChars(env, js, s);
223 JNIEXPORT void JNICALL
224 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
226 const char *s = (*env)->GetStringUTFChars(env, js, 0);
228 (*env)->ReleaseStringUTFChars(env, js, s);
230 JNIEXPORT void JNICALL
231 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
233 const char *s = (*env)->GetStringUTFChars(env, js, 0);
234 XBT_CRITICAL("%s", s);
235 (*env)->ReleaseStringUTFChars(env, js, s);
237 JNIEXPORT void JNICALL
238 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
239 jstring jdeploymentFile)
242 const char *deploymentFile =
243 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
245 SIMIX_function_register_default(create_jprocess);
246 MSG_launch_application(deploymentFile);
249 * Function called when there is the need to create the java Process object
250 * (when we are using deployement files).
251 * it HAS to be executed on the process context, else really bad things will happen.
253 static int create_jprocess(int argc, char *argv[]) {
254 JNIEnv *env = get_current_thread_env();
255 //Change the "." in class name for "/".
256 xbt_str_subst(argv[0],'.','/',0);
257 jclass class_Process = (*env)->FindClass(env, argv[0]);
258 xbt_str_subst(argv[0],'/','.',0);
259 //Retrieve the methodID for the constructor
260 xbt_assert((class_Process != NULL), "Class not found (%s).", argv[0]);
261 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
262 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
264 //Retrieve the name of the process.
265 jstring jname = (*env)->NewStringUTF(env, argv[0]);
266 //Build the arguments
267 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
268 (*env)->FindClass(env,"java/lang/String"),
269 (*env)->NewStringUTF(env,"")));
271 for (i = 1; i < argc; i++)
272 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
273 //Retrieve the host for the process.
274 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
275 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
276 //creates the process
277 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
278 xbt_assert((jprocess != NULL), "Process allocation failed.");
279 jprocess = (*env)->NewGlobalRef(env, jprocess);
280 //bind the process to the context
281 msg_process_t process = MSG_process_self();
282 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
283 context->jprocess = jprocess;
284 /* sets the PID and the PPID of the process */
285 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
286 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
287 jprocess_bind(jprocess, process, env);