1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007-2015. 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. */
9 #include <simgrid/msg.h>
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
14 #include "smx_context_java.h"
16 #include "jmsg_process.h"
20 #include "jmsg_host.h"
21 #include "jmsg_storage.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 */
37 int JAVA_STORAGE_LEVEL;
39 static int create_jprocess(int argc, char *argv[]);
41 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
43 JavaVM *__java_vm = NULL;
45 JavaVM *get_java_VM(void)
50 JNIEnv *get_current_thread_env(void)
54 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
58 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
61 jxbt_throw_time_out_failure(env,NULL);
63 case MSG_TRANSFER_FAILURE:
64 jxbt_throw_transfer_failure(env,NULL);
66 case MSG_HOST_FAILURE:
67 jxbt_throw_host_failure(env,NULL);
69 case MSG_TASK_CANCELED:
70 jxbt_throw_task_cancelled(env,NULL);
73 jxbt_throw_native(env,xbt_strdup("undefined message failed (please see jmsg_throw_status function in jmsg.c)"));
78 /***************************************************************************************
79 * Unsortable functions *
80 ***************************************************************************************/
82 JNIEXPORT jdouble JNICALL
83 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
85 return (jdouble) MSG_get_clock();
88 static void __JAVA_host_priv_free(void *host)
92 static void __JAVA_storage_priv_free(void *storage)
96 JNIEXPORT void JNICALL
97 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
105 XBT_LOG_CONNECT(jmsg);
106 XBT_LOG_CONNECT(jtrace);
108 (*env)->GetJavaVM(env, &__java_vm);
110 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
111 jthrowable exc = (*env)->ExceptionOccurred(env);
113 (*env)->ExceptionClear(env);
116 setlocale(LC_NUMERIC,"C");
119 argc = (int) (*env)->GetArrayLength(env, jargs);
122 argv = xbt_new(char *, argc + 1);
123 argv[0] = xbt_strdup("java");
125 for (index = 0; index < argc - 1; index++) {
126 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
127 tmp = (*env)->GetStringUTFChars(env, jval, 0);
128 argv[index + 1] = xbt_strdup(tmp);
129 (*env)->ReleaseStringUTFChars(env, jval, tmp);
133 MSG_init(&argc, argv);
135 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
136 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
138 for (index = 0; index < argc; index++)
143 if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
144 XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
146 xbt_die("Unknown context factory. Please report bug.");
149 JNIEXPORT void JNICALL
150 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
154 xbt_dynar_t hosts, storages;
155 jobject jhost, jstorage;
159 XBT_DEBUG("Ready to run MSG_MAIN");
161 XBT_DEBUG("Done running MSG_MAIN");
162 jxbt_check_res("MSG_main()", rv, MSG_OK,
163 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
165 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
166 /* Cleanup java hosts */
167 hosts = MSG_hosts_as_dynar();
168 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
169 jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
171 jhost_unref(env, jhost);
174 xbt_dynar_free(&hosts);
176 /* Cleanup java storages */
177 storages = MSG_storages_as_dynar();
178 if(!xbt_dynar_is_empty(storages)){
179 for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
180 jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
182 jstorage_unref(env, jstorage);
185 xbt_dynar_free(&storages);
189 JNIEXPORT void JNICALL
190 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
191 jstring jplatformFile)
194 const char *platformFile =
195 (*env)->GetStringUTFChars(env, jplatformFile, 0);
197 MSG_create_environment(platformFile);
199 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
202 JNIEXPORT jobject JNICALL
203 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
205 msg_as_t as = MSG_environment_get_routing_root();
206 jobject jas = jas_new_instance(env);
208 jxbt_throw_jni(env, "java As instantiation failed");
211 jas = jas_ref(env, jas);
213 jxbt_throw_jni(env, "new global ref allocation failed");
216 jas_bind(jas, as, env);
218 return (jobject) jas;
221 JNIEXPORT void JNICALL
222 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
224 const char *s = (*env)->GetStringUTFChars(env, js, 0);
226 (*env)->ReleaseStringUTFChars(env, js, s);
228 JNIEXPORT void JNICALL
229 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
231 const char *s = (*env)->GetStringUTFChars(env, js, 0);
233 (*env)->ReleaseStringUTFChars(env, js, s);
235 JNIEXPORT void JNICALL
236 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
238 const char *s = (*env)->GetStringUTFChars(env, js, 0);
240 (*env)->ReleaseStringUTFChars(env, js, s);
242 JNIEXPORT void JNICALL
243 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
245 const char *s = (*env)->GetStringUTFChars(env, js, 0);
247 (*env)->ReleaseStringUTFChars(env, js, s);
249 JNIEXPORT void JNICALL
250 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
252 const char *s = (*env)->GetStringUTFChars(env, js, 0);
254 (*env)->ReleaseStringUTFChars(env, js, s);
256 JNIEXPORT void JNICALL
257 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
259 const char *s = (*env)->GetStringUTFChars(env, js, 0);
260 XBT_CRITICAL("%s", s);
261 (*env)->ReleaseStringUTFChars(env, js, s);
263 JNIEXPORT void JNICALL
264 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
265 jstring jdeploymentFile)
268 const char *deploymentFile =
269 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
271 SIMIX_function_register_default(create_jprocess);
272 MSG_launch_application(deploymentFile);
275 * Function called when there is the need to create the java Process object
276 * (when we are using deployement files).
277 * it HAS to be executed on the process context, else really bad things will happen.
279 static int create_jprocess(int argc, char *argv[]) {
280 JNIEnv *env = get_current_thread_env();
281 //Change the "." in class name for "/".
282 xbt_str_subst(argv[0],'.','/',0);
283 jclass class_Process = (*env)->FindClass(env, argv[0]);
284 xbt_str_subst(argv[0],'/','.',0);
285 //Retrieve the methodID for the constructor
286 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]);
287 jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
288 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
290 //Retrieve the name of the process.
291 jstring jname = (*env)->NewStringUTF(env, argv[0]);
292 //Build the arguments
293 jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
294 (*env)->FindClass(env,"java/lang/String"),
295 (*env)->NewStringUTF(env,"")));
297 for (i = 1; i < argc; i++)
298 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
299 //Retrieve the host for the process.
300 jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
301 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
302 //creates the process
303 jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
304 xbt_assert((jprocess != NULL), "Process allocation failed.");
305 jprocess = (*env)->NewGlobalRef(env, jprocess);
306 //bind the process to the context
307 msg_process_t process = MSG_process_self();
308 smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
309 context->jprocess = jprocess;
310 /* sets the PID and the PPID of the process */
311 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
312 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
313 jprocess_bind(jprocess, process, env);