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 "simgrid/plugins/energy.h"
13 #include <src/simix/smx_private.h>
15 #include "jmsg_process.h"
17 #include "jmsg_host.h"
18 #include "jmsg_storage.h"
19 #include "jmsg_task.h"
20 #include "jxbt_utilities.h"
23 #include "JavaContext.hpp"
25 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
32 /* end of eclipse-mandated pimple */
36 int JAVA_HOST_LEVEL = -1;
37 int JAVA_STORAGE_LEVEL = -1;
39 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
41 JavaVM *__java_vm = nullptr;
48 JNIEnv *get_current_thread_env()
52 __java_vm->AttachCurrentThread((void **) &env, nullptr);
56 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
59 jxbt_throw_time_out_failure(env,nullptr);
61 case MSG_TRANSFER_FAILURE:
62 jxbt_throw_transfer_failure(env,nullptr);
64 case MSG_HOST_FAILURE:
65 jxbt_throw_host_failure(env,nullptr);
67 case MSG_TASK_CANCELED:
68 jxbt_throw_task_cancelled(env,nullptr);
71 jxbt_throw_native(env,xbt_strdup("undefined message failed "
72 "(please see jmsg_throw_status function in jmsg.cpp)"));
76 /***************************************************************************************
77 * Unsortable functions *
78 ***************************************************************************************/
80 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
82 return (jdouble) MSG_get_clock();
85 static void __JAVA_host_priv_free(void *host)
89 static void __JAVA_storage_priv_free(void *storage)
93 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
95 char **argv = nullptr;
101 XBT_LOG_CONNECT(jmsg);
102 XBT_LOG_CONNECT(jtrace);
104 env->GetJavaVM(&__java_vm);
106 simgrid::simix::factory_initializer = simgrid::java::java_factory;
107 jthrowable exc = env->ExceptionOccurred();
109 env->ExceptionClear();
112 setlocale(LC_NUMERIC,"C");
115 argc = (int) env->GetArrayLength(jargs);
118 argv = xbt_new(char *, argc + 1);
119 argv[0] = xbt_strdup("java");
121 for (index = 0; index < argc - 1; index++) {
122 jval = (jstring) env->GetObjectArrayElement(jargs, index);
123 tmp = env->GetStringUTFChars(jval, 0);
124 argv[index + 1] = xbt_strdup(tmp);
125 env->ReleaseStringUTFChars(jval, tmp);
127 argv[argc] = nullptr;
129 MSG_init(&argc, argv);
131 JAVA_HOST_LEVEL = simgrid::s4u::Host::extension_create(__JAVA_host_priv_free);
132 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, __JAVA_storage_priv_free);
134 for (index = 0; index < argc; index++)
140 JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
143 XBT_DEBUG("Ready to run MSG_MAIN");
144 msg_error_t rv = MSG_main();
145 XBT_DEBUG("Done running MSG_MAIN");
146 jxbt_check_res("MSG_main()", rv, MSG_OK,
147 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
149 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
150 /* Cleanup java hosts */
151 xbt_dynar_t hosts = MSG_hosts_as_dynar();
152 for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
153 msg_host_t msg_host = xbt_dynar_get_as(hosts,index,msg_host_t);
154 jobject jhost = (jobject) msg_host->extension(JAVA_HOST_LEVEL);
156 jhost_unref(env, jhost);
159 xbt_dynar_free(&hosts);
161 /* Cleanup java storages */
162 xbt_dynar_t storages = MSG_storages_as_dynar();
163 if(!xbt_dynar_is_empty(storages)){
164 for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) {
165 jobject jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
167 jstorage_unref(env, jstorage);
170 xbt_dynar_free(&storages);
173 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls, jstring jplatformFile)
175 const char *platformFile = env->GetStringUTFChars(jplatformFile, 0);
177 MSG_create_environment(platformFile);
179 env->ReleaseStringUTFChars(jplatformFile, platformFile);
182 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
184 msg_as_t as = MSG_environment_get_routing_root();
185 jobject jas = jas_new_instance(env);
187 jxbt_throw_jni(env, "java As instantiation failed");
190 jas = jas_ref(env, jas);
192 jxbt_throw_jni(env, "new global ref allocation failed");
195 jas_bind(jas, as, env);
197 return (jobject) jas;
200 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
202 const char *s = env->GetStringUTFChars(js, 0);
204 env->ReleaseStringUTFChars(js, s);
207 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
209 const char *s = env->GetStringUTFChars(js, 0);
211 env->ReleaseStringUTFChars(js, s);
214 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
216 const char *s = env->GetStringUTFChars(js, 0);
218 env->ReleaseStringUTFChars(js, s);
221 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
223 const char *s = env->GetStringUTFChars(js, 0);
225 env->ReleaseStringUTFChars(js, s);
228 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
230 const char *s = env->GetStringUTFChars(js, 0);
232 env->ReleaseStringUTFChars(js, s);
235 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
237 const char *s = env->GetStringUTFChars(js, 0);
238 XBT_CRITICAL("%s", s);
239 env->ReleaseStringUTFChars(js, s);
242 static int java_main(int argc, char *argv[]);
244 JNIEXPORT void JNICALL
245 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls, jstring jdeploymentFile)
247 const char *deploymentFile = env->GetStringUTFChars(jdeploymentFile, 0);
249 SIMIX_function_register_default(java_main);
250 MSG_launch_application(deploymentFile);
255 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_energyInit() {
256 sg_energy_plugin_init();
259 /** Run a Java org.simgrid.msg.Process
261 * If needed, this waits for the process starting time.
262 * Then it calls the Process.run() method.
264 static void run_jprocess(JNIEnv *env, jobject jprocess)
266 xbt_assert(jprocess != nullptr, "Process not created...");
267 //wait for the process to be able to begin
269 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
270 jdouble startTime = env->GetDoubleField(jprocess, jprocess_field_Process_startTime);
271 if (startTime > MSG_get_clock())
272 MSG_process_sleep(startTime - MSG_get_clock());
273 //Execution of the "run" method.
274 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
275 xbt_assert( (id != nullptr), "Method not found...");
276 env->CallVoidMethod(jprocess, id);
279 /** Create a Java org.simgrid.msg.Process with the arguments and run it */
280 static int java_main(int argc, char *argv[])
282 JNIEnv *env = get_current_thread_env();
283 simgrid::java::JavaContext* context = static_cast<simgrid::java::JavaContext*>(SIMIX_context_self());
285 //Change the "." in class name for "/".
286 xbt_str_subst(argv[0],'.','/',0);
287 jclass class_Process = env->FindClass(argv[0]);
288 xbt_str_subst(argv[0],'/','.',0);
289 //Retrieve the methodID for the constructor
290 xbt_assert((class_Process != nullptr), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]);
291 jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
292 xbt_assert((constructor_Process != nullptr), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
294 //Retrieve the name of the process.
295 jstring jname = env->NewStringUTF(argv[0]);
296 //Build the arguments
297 jobjectArray args = static_cast<jobjectArray>(env->NewObjectArray(argc - 1, env->FindClass("java/lang/String"),
298 env->NewStringUTF("")));
299 for (int i = 1; i < argc; i++)
300 env->SetObjectArrayElement(args,i - 1, env->NewStringUTF(argv[i]));
301 //Retrieve the host for the process.
302 jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
303 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, nullptr, jhostName);
304 //creates the process
305 jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
306 xbt_assert((jprocess != nullptr), "Process allocation failed.");
307 jprocess = env->NewGlobalRef(jprocess);
308 //bind the process to the context
309 msg_process_t process = MSG_process_self();
311 context->jprocess = jprocess;
312 /* sets the PID and the PPID of the process */
313 env->SetIntField(jprocess, jprocess_field_Process_pid, static_cast<jint>(MSG_process_get_PID(process)));
314 env->SetIntField(jprocess, jprocess_field_Process_ppid, static_cast<jint>(MSG_process_get_PPID(process)));
315 jprocess_bind(jprocess, process, env);
317 run_jprocess(env, context->jprocess);
324 /** Run the Java org.simgrid.msg.Process */
325 void java_main_jprocess(jobject jprocess)
327 JNIEnv *env = get_current_thread_env();
328 simgrid::java::JavaContext* context = static_cast<simgrid::java::JavaContext*>(SIMIX_context_self());
329 context->jprocess = jprocess;
330 smx_process_t process = SIMIX_process_self();
331 jprocess_bind(context->jprocess, process, env);
333 // Adrien, ugly path, just to bypass creation of context at low levels (i.e such as for the VM migration for instance)
334 if (context->jprocess == nullptr)
337 run_jprocess(env, context->jprocess);