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>
13 #include <src/simix/smx_private.h>
15 #include "jmsg_process.h"
19 #include "jmsg_host.h"
20 #include "jmsg_storage.h"
21 #include "jmsg_task.h"
22 #include "jxbt_utilities.h"
26 #include "JavaContext.hpp"
28 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
35 /* end of eclipse-mandated pimple */
40 int JAVA_STORAGE_LEVEL;
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((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);
107 XBT_LOG_CONNECT(jtrace);
109 env->GetJavaVM(&__java_vm);
111 simgrid::simix::factory_initializer = simgrid::java::java_factory;
112 jthrowable exc = env->ExceptionOccurred();
114 env->ExceptionClear();
117 setlocale(LC_NUMERIC,"C");
120 argc = (int) env->GetArrayLength(jargs);
123 argv = xbt_new(char *, argc + 1);
124 argv[0] = xbt_strdup("java");
126 for (index = 0; index < argc - 1; index++) {
127 jval = (jstring) env->GetObjectArrayElement(jargs, index);
128 tmp = env->GetStringUTFChars(jval, 0);
129 argv[index + 1] = xbt_strdup(tmp);
130 env->ReleaseStringUTFChars(jval, tmp);
134 MSG_init(&argc, argv);
136 JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
137 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
139 for (index = 0; index < argc; index++)
145 JNIEXPORT void JNICALL
146 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
149 XBT_DEBUG("Ready to run MSG_MAIN");
150 msg_error_t rv = MSG_main();
151 XBT_DEBUG("Done running MSG_MAIN");
152 jxbt_check_res("MSG_main()", rv, MSG_OK,
153 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
155 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
156 /* Cleanup java hosts */
157 xbt_dynar_t hosts = MSG_hosts_as_dynar();
158 for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
159 jobject jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
161 jhost_unref(env, jhost);
164 xbt_dynar_free(&hosts);
166 /* Cleanup java storages */
167 xbt_dynar_t storages = MSG_storages_as_dynar();
168 if(!xbt_dynar_is_empty(storages)){
169 for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) {
170 jobject jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
172 jstorage_unref(env, jstorage);
175 xbt_dynar_free(&storages);
179 JNIEXPORT void JNICALL
180 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
181 jstring jplatformFile)
184 const char *platformFile =
185 env->GetStringUTFChars(jplatformFile, 0);
187 MSG_create_environment(platformFile);
189 env->ReleaseStringUTFChars(jplatformFile, platformFile);
192 JNIEXPORT jobject JNICALL
193 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
195 msg_as_t as = MSG_environment_get_routing_root();
196 jobject jas = jas_new_instance(env);
198 jxbt_throw_jni(env, "java As instantiation failed");
201 jas = jas_ref(env, jas);
203 jxbt_throw_jni(env, "new global ref allocation failed");
206 jas_bind(jas, as, env);
208 return (jobject) jas;
211 JNIEXPORT void JNICALL
212 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
214 const char *s = env->GetStringUTFChars(js, 0);
216 env->ReleaseStringUTFChars(js, s);
218 JNIEXPORT void JNICALL
219 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
221 const char *s = env->GetStringUTFChars(js, 0);
223 env->ReleaseStringUTFChars(js, s);
225 JNIEXPORT void JNICALL
226 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
228 const char *s = env->GetStringUTFChars(js, 0);
230 env->ReleaseStringUTFChars(js, s);
232 JNIEXPORT void JNICALL
233 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
235 const char *s = env->GetStringUTFChars(js, 0);
237 env->ReleaseStringUTFChars(js, s);
239 JNIEXPORT void JNICALL
240 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
242 const char *s = env->GetStringUTFChars(js, 0);
244 env->ReleaseStringUTFChars(js, s);
246 JNIEXPORT void JNICALL
247 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
249 const char *s = env->GetStringUTFChars(js, 0);
250 XBT_CRITICAL("%s", s);
251 env->ReleaseStringUTFChars(js, s);
253 JNIEXPORT void JNICALL
254 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
255 jstring jdeploymentFile)
258 const char *deploymentFile =
259 env->GetStringUTFChars(jdeploymentFile, 0);
261 SIMIX_function_register_default(simgrid::java::java_main);
262 MSG_launch_application(deploymentFile);
265 * Function called when there is the need to create the java Process object
266 * (when we are using deployement files).
267 * it HAS to be executed on the process context, else really bad things will happen.
269 static int create_jprocess(int argc, char *argv[]) {
270 JNIEnv *env = get_current_thread_env();
271 //Change the "." in class name for "/".
272 xbt_str_subst(argv[0],'.','/',0);
273 jclass class_Process = env->FindClass(argv[0]);
274 xbt_str_subst(argv[0],'/','.',0);
275 //Retrieve the methodID for the constructor
276 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]);
277 jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
278 xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
280 //Retrieve the name of the process.
281 jstring jname = env->NewStringUTF(argv[0]);
282 //Build the arguments
283 jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
284 env->FindClass("java/lang/String"),
285 env->NewStringUTF(""));
287 for (i = 1; i < argc; i++)
288 env->SetObjectArrayElement(args,i - 1,
289 env->NewStringUTF(argv[i]));
290 //Retrieve the host for the process.
291 jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
292 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
293 //creates the process
294 jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
295 xbt_assert((jprocess != NULL), "Process allocation failed.");
296 jprocess = env->NewGlobalRef(jprocess);
297 //bind the process to the context
298 msg_process_t process = MSG_process_self();
299 simgrid::java::JavaContext* context =
300 (simgrid::java::JavaContext*) MSG_process_get_smx_ctx(process);
301 context->jprocess = jprocess;
302 /* sets the PID and the PPID of the process */
303 env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
304 env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
305 jprocess_bind(jprocess, process, env);
317 int java_main(int argc, char *argv[])
319 JNIEnv *env = get_current_thread_env();
320 simgrid::java::JavaContext* context =
321 (simgrid::java::JavaContext*) SIMIX_context_self();
324 create_jprocess(argc, argv);
326 smx_process_t process = SIMIX_process_self();
327 context->jprocess = (jobject) MSG_process_get_data(process);
328 jprocess_bind(context->jprocess, process, env);
331 // Adrien, ugly path, just to bypass creation of context at low levels
332 // (i.e such as for the VM migration for instance)
333 if (context->jprocess != nullptr){
334 xbt_assert((context->jprocess != nullptr), "Process not created...");
335 //wait for the process to be able to begin
337 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
338 jdouble startTime = env->GetDoubleField(context->jprocess, jprocess_field_Process_startTime);
339 if (startTime > MSG_get_clock()) {
340 MSG_process_sleep(startTime - MSG_get_clock());
342 //Execution of the "run" method.
343 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
344 xbt_assert( (id != nullptr), "Method not found...");
345 env->CallVoidMethod(context->jprocess, id);