1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <simgrid/msg.h>
11 #include <simgrid/simix.h>
12 #include <simgrid/plugins/energy.h>
14 #include <simgrid/s4u/host.hpp>
16 #include <src/simix/smx_private.h>
18 #include "jmsg_process.h"
20 #include "jmsg_host.h"
21 #include "jmsg_storage.h"
22 #include "jmsg_task.h"
23 #include "jxbt_utilities.h"
26 #include "JavaContext.hpp"
30 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
37 /* end of eclipse-mandated pimple */
41 int JAVA_HOST_LEVEL = -1;
42 int JAVA_STORAGE_LEVEL = -1;
44 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
46 JavaVM *__java_vm = nullptr;
53 JNIEnv *get_current_thread_env()
55 using simgrid::kernel::context::JavaContext;
56 JavaContext* ctx = static_cast<JavaContext*>(xbt_os_thread_get_extra_data());
60 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
63 jxbt_throw_time_out_failure(env, "");
65 case MSG_TRANSFER_FAILURE:
66 jxbt_throw_transfer_failure(env, "");
68 case MSG_HOST_FAILURE:
69 jxbt_throw_host_failure(env, "");
71 case MSG_TASK_CANCELED:
72 jxbt_throw_task_cancelled(env, "");
75 xbt_die("undefined message failed (please see jmsg_throw_status function in jmsg.cpp)");
79 /***************************************************************************************
80 * Unsortable functions *
81 ***************************************************************************************/
83 JNIEXPORT jdouble JNICALL 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 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
98 char **argv = nullptr;
104 XBT_LOG_CONNECT(java);
105 XBT_LOG_CONNECT(jtrace);
107 env->GetJavaVM(&__java_vm);
109 simgrid::kernel::context::factory_initializer = &simgrid::kernel::context::java_factory;
110 jthrowable exc = env->ExceptionOccurred();
112 env->ExceptionClear();
115 setlocale(LC_NUMERIC,"C");
118 argc = (int) env->GetArrayLength(jargs);
121 argv = xbt_new(char *, argc + 1);
122 argv[0] = xbt_strdup("java");
124 for (index = 0; index < argc - 1; index++) {
125 jval = (jstring) env->GetObjectArrayElement(jargs, index);
126 tmp = env->GetStringUTFChars(jval, 0);
127 argv[index + 1] = xbt_strdup(tmp);
128 env->ReleaseStringUTFChars(jval, tmp);
130 argv[argc] = nullptr;
132 MSG_init(&argc, argv);
134 JAVA_HOST_LEVEL = simgrid::s4u::Host::extension_create(__JAVA_host_priv_free);
135 JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, __JAVA_storage_priv_free);
137 for (index = 0; index < argc; index++)
143 JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
146 XBT_DEBUG("Ready to run MSG_MAIN");
147 msg_error_t rv = MSG_main();
148 XBT_DEBUG("Done running MSG_MAIN");
149 jxbt_check_res("MSG_main()", rv, MSG_OK,
150 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
152 XBT_INFO("MSG_main finished; Cleaning up the simulation...");
153 /* Cleanup java hosts */
154 xbt_dynar_t hosts = MSG_hosts_as_dynar();
155 for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
156 msg_host_t msg_host = xbt_dynar_get_as(hosts,index,msg_host_t);
157 jobject jhost = (jobject) msg_host->extension(JAVA_HOST_LEVEL);
159 jhost_unref(env, jhost);
162 xbt_dynar_free(&hosts);
164 /* Cleanup java storages */
165 xbt_dynar_t storages = MSG_storages_as_dynar();
166 if(!xbt_dynar_is_empty(storages)){
167 for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) {
168 jobject jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
170 jstorage_unref(env, jstorage);
173 xbt_dynar_free(&storages);
176 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls, jstring jplatformFile)
178 const char *platformFile = env->GetStringUTFChars(jplatformFile, 0);
180 MSG_create_environment(platformFile);
182 env->ReleaseStringUTFChars(jplatformFile, platformFile);
185 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
187 msg_netzone_t as = MSG_environment_get_routing_root();
188 jobject jas = jnetzone_new_instance(env);
190 jxbt_throw_jni(env, "java As instantiation failed");
193 jas = jnetzone_ref(env, jas);
195 jxbt_throw_jni(env, "new global ref allocation failed");
198 jnetzone_bind(jas, as, env);
200 return (jobject) jas;
203 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
205 const char *s = env->GetStringUTFChars(js, 0);
207 env->ReleaseStringUTFChars(js, s);
210 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
212 const char *s = env->GetStringUTFChars(js, 0);
214 env->ReleaseStringUTFChars(js, s);
217 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
219 const char *s = env->GetStringUTFChars(js, 0);
221 env->ReleaseStringUTFChars(js, s);
224 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
226 const char *s = env->GetStringUTFChars(js, 0);
228 env->ReleaseStringUTFChars(js, s);
231 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
233 const char *s = env->GetStringUTFChars(js, 0);
235 env->ReleaseStringUTFChars(js, s);
238 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
240 const char *s = env->GetStringUTFChars(js, 0);
241 XBT_CRITICAL("%s", s);
242 env->ReleaseStringUTFChars(js, s);
245 static int java_main(int argc, char *argv[]);
247 JNIEXPORT void JNICALL
248 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls, jstring jdeploymentFile)
250 const char *deploymentFile = env->GetStringUTFChars(jdeploymentFile, 0);
252 SIMIX_function_register_default(java_main);
253 MSG_launch_application(deploymentFile);
256 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_energyInit() {
257 sg_host_energy_plugin_init();
262 /** Run a Java org.simgrid.msg.Process
264 * If needed, this waits for the process starting time.
265 * Then it calls the Process.run() method.
267 static void run_jprocess(JNIEnv *env, jobject jprocess)
269 // wait for the process's start time
270 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
271 jdouble startTime = env->GetDoubleField(jprocess, jprocess_field_Process_startTime);
272 if (startTime > MSG_get_clock())
273 MSG_process_sleep(startTime - MSG_get_clock());
274 //Execution of the "run" method.
275 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
276 xbt_assert((id != nullptr), "Method run() not found...");
277 env->CallVoidMethod(jprocess, id);
280 /** Create a Java org.simgrid.msg.Process with the arguments and run it */
281 static int java_main(int argc, char *argv[])
283 JNIEnv *env = get_current_thread_env();
284 simgrid::kernel::context::JavaContext* context = static_cast<simgrid::kernel::context::JavaContext*>(SIMIX_context_self());
286 //Change the "." in class name for "/".
287 xbt_str_subst(argv[0],'.','/',0);
288 jclass class_Process = env->FindClass(argv[0]);
289 xbt_str_subst(argv[0],'/','.',0);
290 //Retrieve the methodID for the constructor
291 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]);
292 jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
293 xbt_assert((constructor_Process != nullptr), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
295 //Retrieve the name of the process.
296 jstring jname = env->NewStringUTF(argv[0]);
297 //Build the arguments
298 jobjectArray args = static_cast<jobjectArray>(env->NewObjectArray(argc - 1, env->FindClass("java/lang/String"),
299 env->NewStringUTF("")));
300 for (int i = 1; i < argc; i++)
301 env->SetObjectArrayElement(args,i - 1, env->NewStringUTF(argv[i]));
302 //Retrieve the host for the process.
303 jstring jhostName = env->NewStringUTF(MSG_host_self()->cname());
304 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, nullptr, jhostName);
305 //creates the process
306 jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
307 xbt_assert((jprocess != nullptr), "Process allocation failed.");
308 jprocess = env->NewGlobalRef(jprocess);
309 //bind the process to the context
310 msg_process_t process = MSG_process_self();
312 context->jprocess = jprocess;
313 /* sets the PID and the PPID of the process */
314 env->SetIntField(jprocess, jprocess_field_Process_pid, static_cast<jint>(MSG_process_get_PID(process)));
315 env->SetIntField(jprocess, jprocess_field_Process_ppid, static_cast<jint>(MSG_process_get_PPID(process)));
316 jprocess_bind(jprocess, process, env);
318 run_jprocess(env, context->jprocess);
326 /** Run the Java org.simgrid.msg.Process */
327 void java_main_jprocess(jobject jprocess)
329 JNIEnv *env = get_current_thread_env();
330 simgrid::kernel::context::JavaContext* context = static_cast<simgrid::kernel::context::JavaContext*>(SIMIX_context_self());
331 context->jprocess = jprocess;
332 jprocess_bind(context->jprocess, MSG_process_self(), env);
334 run_jprocess(env, context->jprocess);