1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
12 #include "simgrid/Exception.hpp"
13 #include "simgrid/plugins/energy.h"
14 #include "simgrid/plugins/file_system.h"
15 #include "simgrid/plugins/live_migration.h"
16 #include "simgrid/plugins/load.h"
18 #include "simgrid/s4u/Actor.hpp"
19 #include "simgrid/s4u/Host.hpp"
22 #include "jmsg_as.hpp"
23 #include "jmsg_host.h"
24 #include "jmsg_process.h"
25 #include "jmsg_task.h"
26 #include "jxbt_utilities.hpp"
28 #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 */
39 int JAVA_HOST_LEVEL = -1;
41 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
43 JavaVM *__java_vm = nullptr;
45 JNIEnv *get_current_thread_env()
47 using simgrid::kernel::context::JavaContext;
48 const JavaContext* ctx = static_cast<JavaContext*>(simgrid::kernel::context::Context::self());
55 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
58 jxbt_throw_time_out_failure(env, "");
60 case MSG_TRANSFER_FAILURE:
61 jxbt_throw_transfer_failure(env, "");
63 case MSG_HOST_FAILURE:
64 jxbt_throw_host_failure(env, "");
66 case MSG_TASK_CANCELED:
67 jxbt_throw_task_cancelled(env, "");
70 xbt_die("undefined message failed (please see jmsg_throw_status function in jmsg.cpp)");
74 /***************************************************************************************
75 * Unsortable functions *
76 ***************************************************************************************/
78 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Msg_getClock(JNIEnv*, jclass)
80 return (jdouble)simgrid_get_clock();
83 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_init(JNIEnv* env, jclass, jobjectArray jargs)
85 env->GetJavaVM(&__java_vm);
87 simgrid::kernel::context::factory_initializer = &simgrid::kernel::context::java_factory;
88 const _jthrowable* exc = env->ExceptionOccurred();
90 env->ExceptionClear();
93 setlocale(LC_NUMERIC,"C");
97 argc += static_cast<int>(env->GetArrayLength(jargs));
100 // Need a static storage because the XBT layer saves the arguments in xbt::binary_name and xbt::cmdline.
101 static std::vector<std::string> args;
104 args.emplace_back("java");
105 for (int index = 1; index < argc; index++) {
106 auto jval = (jstring)env->GetObjectArrayElement(jargs, index - 1);
107 jstring_wrapper tmp(env, jval);
108 args.emplace_back(tmp.value);
111 std::unique_ptr<char* []> argv(new char*[argc + 1]);
112 std::transform(begin(args), end(args), argv.get(), [](std::string& s) { return &s.front(); });
113 argv[argc] = nullptr;
116 MSG_init(&argc2, argv.get());
117 xbt_assert(argc2 <= argc);
119 for (int index = 1; index < argc2; index++)
120 env->SetObjectArrayElement(jargs, index - 1, (jstring)env->NewStringUTF(argv[index]));
122 sg_vm_live_migration_plugin_init();
123 JAVA_HOST_LEVEL = simgrid::s4u::Host::extension_create(nullptr);
126 JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv* env, jclass)
129 XBT_DEBUG("Ready to run");
131 XBT_DEBUG("Done running");
132 XBT_INFO("Terminating the simulation...");
133 /* Cleanup java hosts */
134 sg_host_t* hosts = sg_host_list();
135 size_t host_count = sg_host_count();
136 for (size_t index = 0; index < host_count - 1; index++) {
137 auto jhost = (jobject)hosts[index]->extension(JAVA_HOST_LEVEL);
139 jhost_unref(env, jhost);
143 /* Display the status of remaining threads. None should survive, but who knows */
144 jclass clsProcess = jxbt_get_class(env, "org/simgrid/msg/Process");
145 jmethodID idDebug = jxbt_get_static_jmethod(env, clsProcess, "debugAllThreads", "()V");
146 xbt_assert(idDebug != nullptr, "Method Process.debugAllThreads() not found...");
147 env->CallStaticVoidMethod(clsProcess, idDebug);
150 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass, jstring jplatformFile)
152 jstring_wrapper platformFile(env, jplatformFile);
153 simgrid_load_platform(platformFile);
156 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv* env, jclass)
158 sg_netzone_t as = sg_zone_get_root();
159 jobject jas = jnetzone_new_instance(env);
161 jxbt_throw_jni(env, "java As instantiation failed");
164 jas = jnetzone_ref(env, jas);
166 jxbt_throw_jni(env, "new global ref allocation failed");
169 jnetzone_bind(jas, as, env);
171 return (jobject) jas;
174 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_debug(JNIEnv* env, jclass, jstring js)
176 jstring_wrapper s(env, js);
177 XBT_DEBUG("%s", s.value);
180 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_verb(JNIEnv* env, jclass, jstring js)
182 jstring_wrapper s(env, js);
183 XBT_VERB("%s", s.value);
186 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_info(JNIEnv* env, jclass, jstring js)
188 jstring_wrapper s(env, js);
189 XBT_INFO("%s", s.value);
192 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_warn(JNIEnv* env, jclass, jstring js)
194 jstring_wrapper s(env, js);
195 XBT_WARN("%s", s.value);
198 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_error(JNIEnv* env, jclass, jstring js)
200 jstring_wrapper s(env, js);
201 XBT_ERROR("%s", s.value);
204 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_critical(JNIEnv* env, jclass, jstring js)
206 jstring_wrapper s(env, js);
207 XBT_CRITICAL("%s", s.value);
210 static void java_main(int argc, char* argv[]);
212 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_deployApplication(JNIEnv* env, jclass, jstring jdeploymentFile)
214 jstring_wrapper deploymentFile(env, jdeploymentFile);
216 simgrid_register_default(java_main);
217 simgrid_load_deployment(deploymentFile);
220 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_energyInit() {
221 sg_host_energy_plugin_init();
224 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_loadInit() {
225 sg_host_load_plugin_init();
227 /** Run a Java org.simgrid.msg.Process
229 * If needed, this waits for the process starting time.
230 * Then it calls the Process.run() method.
232 static void run_jprocess(JNIEnv *env, jobject jprocess)
234 // wait for the process's start time
235 jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
236 jdouble startTime = env->GetDoubleField(jprocess, jprocess_field_Process_startTime);
237 if (startTime > simgrid_get_clock())
238 simgrid::s4u::this_actor::sleep_for(startTime - simgrid_get_clock());
240 //Execution of the "run" method.
241 jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
242 xbt_assert((id != nullptr), "Method Process.run() not found...");
244 env->CallVoidMethod(jprocess, id);
245 if (env->ExceptionOccurred()) {
246 XBT_DEBUG("Something went wrong in this Java actor, forget about it.");
247 env->ExceptionClear();
248 xbt_assert(__java_vm->DetachCurrentThread() == JNI_OK, "Cannot detach failing thread");
249 simgrid::ForcefulKillException::do_throw();
253 /** Create a Java org.simgrid.msg.Process with the arguments and run it */
254 static void java_main(int argc, char* argv[])
256 JNIEnv *env = get_current_thread_env();
257 auto* context = static_cast<simgrid::kernel::context::JavaContext*>(simgrid::kernel::context::Context::self());
259 //Change the "." in class name for "/".
260 std::string arg0 = argv[0];
261 std::replace(begin(arg0), end(arg0), '.', '/');
262 jclass class_Process = env->FindClass(arg0.c_str());
263 //Retrieve the methodID for the constructor
264 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]);
265 jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
266 xbt_assert((constructor_Process != nullptr), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
268 //Retrieve the name of the process.
269 jstring jname = env->NewStringUTF(argv[0]);
270 //Build the arguments
271 auto args = static_cast<jobjectArray>(
272 env->NewObjectArray(argc - 1, env->FindClass("java/lang/String"), env->NewStringUTF("")));
273 for (int i = 1; i < argc; i++)
274 env->SetObjectArrayElement(args,i - 1, env->NewStringUTF(argv[i]));
275 //Retrieve the host for the process.
276 jstring jhostName = env->NewStringUTF(simgrid::s4u::Host::current()->get_cname());
277 jobject jhost = Java_org_simgrid_msg_Host_getByName(env, nullptr, jhostName);
278 //creates the process
279 jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
280 xbt_assert((jprocess != nullptr), "Process allocation failed.");
281 jprocess = env->NewGlobalRef(jprocess);
282 //bind the process to the context
283 const_sg_actor_t actor = sg_actor_self();
285 context->jprocess_ = jprocess;
286 /* sets the PID and the PPID of the process */
287 env->SetIntField(jprocess, jprocess_field_Process_pid, static_cast<jint>(actor->get_pid()));
288 env->SetIntField(jprocess, jprocess_field_Process_ppid, static_cast<jint>(actor->get_ppid()));
289 jprocess_bind(jprocess, actor, env);
291 run_jprocess(env, context->jprocess_);
298 /** Run the Java org.simgrid.msg.Process */
299 void java_main_jprocess(jobject jprocess)
301 JNIEnv *env = get_current_thread_env();
302 auto* context = static_cast<JavaContext*>(Context::self());
303 context->jprocess_ = jprocess;
304 jprocess_bind(context->jprocess_, sg_actor_self(), env);
306 run_jprocess(env, context->jprocess_);
308 } // namespace context
309 } // namespace kernel
310 } // namespace simgrid