Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
doxygen: kill group m_process_management
[simgrid.git] / src / bindings / java / jmsg.cpp
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include <algorithm>
9 #include <clocale>
10 #include <string>
11
12 #include "simgrid/Exception.hpp"
13 #include "simgrid/msg.h"
14 #include "simgrid/plugins/energy.h"
15 #include "simgrid/plugins/file_system.h"
16 #include "simgrid/plugins/live_migration.h"
17 #include "simgrid/simix.h"
18
19 #include "simgrid/s4u/Host.hpp"
20
21 #include "src/simix/smx_private.hpp"
22
23 #include "jmsg.hpp"
24 #include "jmsg_as.hpp"
25 #include "jmsg_host.h"
26 #include "jmsg_process.h"
27 #include "jmsg_storage.h"
28 #include "jmsg_task.h"
29 #include "jxbt_utilities.hpp"
30
31 #include "JavaContext.hpp"
32
33
34 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
35 #ifndef JNIEXPORT
36 #define JNIEXPORT
37 #endif
38 #ifndef JNICALL
39 #define JNICALL
40 #endif
41 /* end of eclipse-mandated pimple */
42
43 int JAVA_HOST_LEVEL = -1;
44
45 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java);
46
47 JavaVM *__java_vm = nullptr;
48
49 JNIEnv *get_current_thread_env()
50 {
51   using simgrid::kernel::context::JavaContext;
52   JavaContext* ctx = static_cast<JavaContext*>(xbt_os_thread_get_extra_data());
53   return ctx->jenv_;
54 }
55
56 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
57   switch (status) {
58     case MSG_TIMEOUT:
59       jxbt_throw_time_out_failure(env, "");
60       break;
61     case MSG_TRANSFER_FAILURE:
62       jxbt_throw_transfer_failure(env, "");
63       break;
64     case MSG_HOST_FAILURE:
65       jxbt_throw_host_failure(env, "");
66       break;
67     case MSG_TASK_CANCELED:
68       jxbt_throw_task_cancelled(env, "");
69       break;
70     default:
71       xbt_die("undefined message failed (please see jmsg_throw_status function in jmsg.cpp)");
72   }
73 }
74
75 /***************************************************************************************
76  * Unsortable functions                                                        *
77  ***************************************************************************************/
78
79 JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
80 {
81   return (jdouble) MSG_get_clock();
82 }
83
84 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
85 {
86   int argc = 0;
87
88   env->GetJavaVM(&__java_vm);
89
90   simgrid::kernel::context::factory_initializer = &simgrid::kernel::context::java_factory;
91   jthrowable exc = env->ExceptionOccurred();
92   if (exc) {
93     env->ExceptionClear();
94   }
95
96   setlocale(LC_NUMERIC,"C");
97
98   if (jargs)
99     argc = static_cast<int>(env->GetArrayLength(jargs));
100
101   argc++;
102   char** argv = new char*[argc + 1];
103   argv[0] = xbt_strdup("java");
104
105   for (int index = 0; index < argc - 1; index++) {
106     jstring jval    = (jstring)env->GetObjectArrayElement(jargs, index);
107     const char* tmp = env->GetStringUTFChars(jval, 0);
108     argv[index + 1] = xbt_strdup(tmp);
109     env->ReleaseStringUTFChars(jval, tmp);
110   }
111   argv[argc] = nullptr;
112
113   MSG_init(&argc, argv);
114   sg_vm_live_migration_plugin_init();
115
116   JAVA_HOST_LEVEL = simgrid::s4u::Host::extension_create(nullptr);
117
118   for (int index = 0; index < argc - 1; index++) {
119     env->SetObjectArrayElement(jargs, index, (jstring)env->NewStringUTF(argv[index + 1]));
120     free(argv[index]);
121   }
122   free(argv[argc]);
123   delete[] argv;
124 }
125
126 JNIEXPORT void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
127 {
128   /* Run everything */
129   XBT_DEBUG("Ready to run MSG_MAIN");
130   msg_error_t rv = MSG_main();
131   XBT_DEBUG("Done running MSG_MAIN");
132   jxbt_check_res("MSG_main()", rv, MSG_OK,
133                  xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
134
135   XBT_INFO("MSG_main finished; Terminating the simulation...");
136   /* Cleanup java hosts */
137   xbt_dynar_t hosts = MSG_hosts_as_dynar();
138   for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
139     msg_host_t msg_host = xbt_dynar_get_as(hosts,index,msg_host_t);
140     jobject jhost = (jobject) msg_host->extension(JAVA_HOST_LEVEL);
141     if (jhost)
142       jhost_unref(env, jhost);
143   }
144   xbt_dynar_free(&hosts);
145
146   /* Cleanup java storages */
147   for (auto const& elm : java_storage_map)
148     jstorage_unref(env, elm.second);
149
150   /* FIXME: don't be of such an EXTREM BRUTALITY to stop the jvm. Sorry I don't get it working otherwise.
151    * See the comment in ActorImpl.cpp::SIMIX_process_kill() */
152   exit(0);
153 }
154
155 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls, jstring jplatformFile)
156 {
157   const char *platformFile = env->GetStringUTFChars(jplatformFile, 0);
158
159   MSG_create_environment(platformFile);
160
161   env->ReleaseStringUTFChars(jplatformFile, platformFile);
162 }
163
164 JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
165 {
166   msg_netzone_t as = MSG_zone_get_root();
167   jobject jas      = jnetzone_new_instance(env);
168   if (not jas) {
169     jxbt_throw_jni(env, "java As instantiation failed");
170     return nullptr;
171   }
172   jas = jnetzone_ref(env, jas);
173   if (not jas) {
174     jxbt_throw_jni(env, "new global ref allocation failed");
175     return nullptr;
176   }
177   jnetzone_bind(jas, as, env);
178
179   return (jobject) jas;
180 }
181
182 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
183 {
184   const char *s = env->GetStringUTFChars(js, 0);
185   XBT_DEBUG("%s", s);
186   env->ReleaseStringUTFChars(js, s);
187 }
188
189 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
190 {
191   const char *s = env->GetStringUTFChars(js, 0);
192   XBT_VERB("%s", s);
193   env->ReleaseStringUTFChars(js, s);
194 }
195
196 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
197 {
198   const char *s = env->GetStringUTFChars(js, 0);
199   XBT_INFO("%s", s);
200   env->ReleaseStringUTFChars(js, s);
201 }
202
203 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
204 {
205   const char *s = env->GetStringUTFChars(js, 0);
206   XBT_WARN("%s", s);
207   env->ReleaseStringUTFChars(js, s);
208 }
209
210 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
211 {
212   const char *s = env->GetStringUTFChars(js, 0);
213   XBT_ERROR("%s", s);
214   env->ReleaseStringUTFChars(js, s);
215 }
216
217 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
218 {
219   const char *s = env->GetStringUTFChars(js, 0);
220   XBT_CRITICAL("%s", s);
221   env->ReleaseStringUTFChars(js, s);
222 }
223
224 static int java_main(int argc, char *argv[]);
225
226 JNIEXPORT void JNICALL
227 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls, jstring jdeploymentFile)
228 {
229   const char *deploymentFile = env->GetStringUTFChars(jdeploymentFile, 0);
230
231   SIMIX_function_register_default(java_main);
232   MSG_launch_application(deploymentFile);
233 }
234
235 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_energyInit() {
236   sg_host_energy_plugin_init();
237 }
238
239 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_fileSystemInit()
240 {
241   sg_storage_file_system_init();
242 }
243
244 /** Run a Java org.simgrid.msg.Process
245  *
246  *  If needed, this waits for the process starting time.
247  *  Then it calls the Process.run() method.
248  */
249 static void run_jprocess(JNIEnv *env, jobject jprocess)
250 {
251   // wait for the process's start time
252   jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
253   jdouble startTime = env->GetDoubleField(jprocess, jprocess_field_Process_startTime);
254   if (startTime > MSG_get_clock())
255     MSG_process_sleep(startTime - MSG_get_clock());
256
257   //Execution of the "run" method.
258   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
259   xbt_assert((id != nullptr), "Method Process.run() not found...");
260
261   env->CallVoidMethod(jprocess, id);
262 }
263
264 /** Create a Java org.simgrid.msg.Process with the arguments and run it */
265 static int java_main(int argc, char *argv[])
266 {
267   JNIEnv *env = get_current_thread_env();
268   simgrid::kernel::context::JavaContext* context = static_cast<simgrid::kernel::context::JavaContext*>(SIMIX_context_self());
269
270   //Change the "." in class name for "/".
271   std::string arg0 = argv[0];
272   std::replace(begin(arg0), end(arg0), '.', '/');
273   jclass class_Process = env->FindClass(arg0.c_str());
274   //Retrieve the methodID for the constructor
275   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]);
276   jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
277   xbt_assert((constructor_Process != nullptr), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
278
279   //Retrieve the name of the process.
280   jstring jname = env->NewStringUTF(argv[0]);
281   //Build the arguments
282   jobjectArray args = static_cast<jobjectArray>(env->NewObjectArray(argc - 1, env->FindClass("java/lang/String"),
283                                                                     env->NewStringUTF("")));
284   for (int i = 1; i < argc; i++)
285       env->SetObjectArrayElement(args,i - 1, env->NewStringUTF(argv[i]));
286   //Retrieve the host for the process.
287   jstring jhostName = env->NewStringUTF(MSG_host_self()->get_cname());
288   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, nullptr, jhostName);
289   //creates the process
290   jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
291   xbt_assert((jprocess != nullptr), "Process allocation failed.");
292   jprocess = env->NewGlobalRef(jprocess);
293   //bind the process to the context
294   msg_process_t process = MSG_process_self();
295
296   context->jprocess_ = jprocess;
297   /* sets the PID and the PPID of the process */
298   env->SetIntField(jprocess, jprocess_field_Process_pid, static_cast<jint>(MSG_process_get_PID(process)));
299   env->SetIntField(jprocess, jprocess_field_Process_ppid, static_cast<jint>(MSG_process_get_PPID(process)));
300   jprocess_bind(jprocess, process, env);
301
302   run_jprocess(env, context->jprocess_);
303   return 0;
304 }
305
306 namespace simgrid {
307 namespace kernel {
308 namespace context {
309
310 /** Run the Java org.simgrid.msg.Process */
311 void java_main_jprocess(jobject jprocess)
312 {
313   JNIEnv *env = get_current_thread_env();
314   simgrid::kernel::context::JavaContext* context = static_cast<simgrid::kernel::context::JavaContext*>(SIMIX_context_self());
315   context->jprocess_                             = jprocess;
316   jprocess_bind(context->jprocess_, MSG_process_self(), env);
317
318   run_jprocess(env, context->jprocess_);
319 }
320 }}}