Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[jmsg] deletion of Java_org_simgrid_msg_VM_get_pm: dead and dangerous code
[simgrid.git] / src / bindings / java / jmsg.cpp
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include <simgrid/msg.h>
10 #include <simgrid/simix.h>
11 #include <locale.h>
12 #include <src/simix/smx_private.h>
13
14 #include "jmsg_process.h"
15
16 #include "jmsg_as.h"
17
18 #include "jmsg_host.h"
19 #include "jmsg_storage.h"
20 #include "jmsg_task.h"
21 #include "jxbt_utilities.h"
22
23 #include "jmsg.h"
24
25 #include "JavaContext.hpp"
26
27 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
28 #ifndef JNIEXPORT
29 #define JNIEXPORT
30 #endif
31 #ifndef JNICALL
32 #define JNICALL
33 #endif
34 /* end of eclipse-mandated pimple */
35
36 SG_BEGIN_DECL()
37
38 int JAVA_HOST_LEVEL;
39 int JAVA_STORAGE_LEVEL;
40
41 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
42
43 JavaVM *__java_vm = NULL;
44
45 JavaVM *get_java_VM(void)
46 {
47   return __java_vm;
48 }
49
50 JNIEnv *get_current_thread_env(void)
51 {
52   JNIEnv *env;
53
54   __java_vm->AttachCurrentThread((void **) &env, NULL);
55   return env;
56 }
57
58 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
59   switch (status) {
60     case MSG_TIMEOUT:
61         jxbt_throw_time_out_failure(env,NULL);
62     break;
63     case MSG_TRANSFER_FAILURE:
64         jxbt_throw_transfer_failure(env,NULL);
65     break;
66     case MSG_HOST_FAILURE:
67         jxbt_throw_host_failure(env,NULL);
68     break;
69     case MSG_TASK_CANCELED:
70         jxbt_throw_task_cancelled(env,NULL);
71     break;
72     default:
73         jxbt_throw_native(env,xbt_strdup("undefined message failed "
74           "(please see jmsg_throw_status function in jmsg.cpp)"));
75   }
76 }
77
78
79 /***************************************************************************************
80  * Unsortable functions                                                        *
81  ***************************************************************************************/
82
83 JNIEXPORT jdouble JNICALL
84 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
85 {
86   return (jdouble) MSG_get_clock();
87 }
88
89 static void __JAVA_host_priv_free(void *host)
90 {
91 }
92
93 static void __JAVA_storage_priv_free(void *storage)
94 {
95 }
96
97 JNIEXPORT void JNICALL
98 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
99 {
100   char **argv = NULL;
101   int index;
102   int argc = 0;
103   jstring jval;
104   const char *tmp;
105
106   XBT_LOG_CONNECT(jmsg);
107   XBT_LOG_CONNECT(jtrace);
108
109   env->GetJavaVM(&__java_vm);
110
111   simgrid::simix::factory_initializer = simgrid::java::java_factory;
112   jthrowable exc = env->ExceptionOccurred();
113   if (exc) {
114     env->ExceptionClear();
115   }
116
117   setlocale(LC_NUMERIC,"C");
118
119   if (jargs)
120     argc = (int) env->GetArrayLength(jargs);
121
122   argc++;
123   argv = xbt_new(char *, argc + 1);
124   argv[0] = xbt_strdup("java");
125
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);
131   }
132   argv[argc] = NULL;
133
134   MSG_init(&argc, argv);
135
136   JAVA_HOST_LEVEL = simgrid::s4u::Host::extension_create(__JAVA_host_priv_free);
137   JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, __JAVA_storage_priv_free);
138
139   for (index = 0; index < argc; index++)
140     free(argv[index]);
141
142   free(argv);
143 }
144
145 JNIEXPORT void JNICALL
146     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
147 {
148   /* Run everything */
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 "));
154
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     msg_host_t msg_host = xbt_dynar_get_as(hosts,index,msg_host_t);
160     jobject jhost = (jobject) msg_host->extension(JAVA_HOST_LEVEL);
161     if (jhost)
162       jhost_unref(env, jhost);
163
164   }
165   xbt_dynar_free(&hosts);
166
167   /* Cleanup java storages */
168   xbt_dynar_t storages = MSG_storages_as_dynar();
169   if(!xbt_dynar_is_empty(storages)){
170     for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) {
171       jobject jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
172       if (jstorage)
173         jstorage_unref(env, jstorage);
174     }
175   }
176   xbt_dynar_free(&storages);
177
178 }
179
180 JNIEXPORT void JNICALL
181 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
182                                        jstring jplatformFile)
183 {
184
185   const char *platformFile =
186       env->GetStringUTFChars(jplatformFile, 0);
187
188   MSG_create_environment(platformFile);
189
190   env->ReleaseStringUTFChars(jplatformFile, platformFile);
191 }
192
193 JNIEXPORT jobject JNICALL
194 Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
195 {
196   msg_as_t as = MSG_environment_get_routing_root();
197   jobject jas = jas_new_instance(env);
198   if (!jas) {
199     jxbt_throw_jni(env, "java As instantiation failed");
200     return NULL;
201   }
202   jas = jas_ref(env, jas);
203   if (!jas) {
204     jxbt_throw_jni(env, "new global ref allocation failed");
205     return NULL;
206   }
207   jas_bind(jas, as, env);
208
209   return (jobject) jas;
210 }
211
212 JNIEXPORT void JNICALL
213 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
214 {
215   const char *s = env->GetStringUTFChars(js, 0);
216   XBT_DEBUG("%s", s);
217   env->ReleaseStringUTFChars(js, s);
218 }
219 JNIEXPORT void JNICALL
220 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
221 {
222   const char *s = env->GetStringUTFChars(js, 0);
223   XBT_VERB("%s", s);
224   env->ReleaseStringUTFChars(js, s);
225 }
226 JNIEXPORT void JNICALL
227 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
228 {
229   const char *s = env->GetStringUTFChars(js, 0);
230   XBT_INFO("%s", s);
231   env->ReleaseStringUTFChars(js, s);
232 }
233 JNIEXPORT void JNICALL
234 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
235 {
236   const char *s = env->GetStringUTFChars(js, 0);
237   XBT_WARN("%s", s);
238   env->ReleaseStringUTFChars(js, s);
239 }
240 JNIEXPORT void JNICALL
241 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
242 {
243   const char *s = env->GetStringUTFChars(js, 0);
244   XBT_ERROR("%s", s);
245   env->ReleaseStringUTFChars(js, s);
246 }
247 JNIEXPORT void JNICALL
248 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
249 {
250   const char *s = env->GetStringUTFChars(js, 0);
251   XBT_CRITICAL("%s", s);
252   env->ReleaseStringUTFChars(js, s);
253 }
254
255 static int java_main(int argc, char *argv[]);
256
257 JNIEXPORT void JNICALL
258 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
259                                        jstring jdeploymentFile)
260 {
261
262   const char *deploymentFile =
263       env->GetStringUTFChars(jdeploymentFile, 0);
264
265   SIMIX_function_register_default(java_main);
266   MSG_launch_application(deploymentFile);
267 }
268
269 SG_END_DECL()
270
271 /** Run a Java org.simgrid.msg.Process
272  *
273  *  If needed, this waits for the process starting time.
274  *  Then it calls the Process.run() method.
275  */
276 static void run_jprocess(JNIEnv *env, jobject jprocess)
277 {
278   xbt_assert(jprocess != nullptr, "Process not created...");
279   //wait for the process to be able to begin
280   //TODO: Cache it
281   jfieldID jprocess_field_Process_startTime = jxbt_get_sfield(env, "org/simgrid/msg/Process", "startTime", "D");
282   jdouble startTime = env->GetDoubleField(jprocess, jprocess_field_Process_startTime);
283   if (startTime > MSG_get_clock())
284     MSG_process_sleep(startTime - MSG_get_clock());
285   //Execution of the "run" method.
286   jmethodID id = jxbt_get_smethod(env, "org/simgrid/msg/Process", "run", "()V");
287   xbt_assert( (id != nullptr), "Method not found...");
288   env->CallVoidMethod(jprocess, id);
289 }
290
291 /** Create a Java org.simgrid.msg.Process with the arguments and run it */
292 static int java_main(int argc, char *argv[])
293 {
294   JNIEnv *env = get_current_thread_env();
295   simgrid::java::JavaContext* context =
296     (simgrid::java::JavaContext*) SIMIX_context_self();
297
298   //Change the "." in class name for "/".
299   xbt_str_subst(argv[0],'.','/',0);
300   jclass class_Process = env->FindClass(argv[0]);
301   xbt_str_subst(argv[0],'/','.',0);
302   //Retrieve the methodID for the constructor
303   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]);
304   jmethodID constructor_Process = env->GetMethodID(class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
305   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
306
307   //Retrieve the name of the process.
308   jstring jname = env->NewStringUTF(argv[0]);
309   //Build the arguments
310   jobjectArray args = (jobjectArray)env->NewObjectArray(argc - 1,
311     env->FindClass("java/lang/String"),
312     env->NewStringUTF(""));
313   int i;
314   for (i = 1; i < argc; i++)
315       env->SetObjectArrayElement(args,i - 1,
316         env->NewStringUTF(argv[i]));
317   //Retrieve the host for the process.
318   jstring jhostName = env->NewStringUTF(MSG_host_get_name(MSG_host_self()));
319   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
320   //creates the process
321   jobject jprocess = env->NewObject(class_Process, constructor_Process, jhost, jname, args);
322   xbt_assert((jprocess != NULL), "Process allocation failed.");
323   jprocess = env->NewGlobalRef(jprocess);
324   //bind the process to the context
325   msg_process_t process = MSG_process_self();
326
327   context->jprocess = jprocess;
328   /* sets the PID and the PPID of the process */
329   env->SetIntField(jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
330   env->SetIntField(jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
331   jprocess_bind(jprocess, process, env);
332
333   run_jprocess(env, context->jprocess);
334   return 0;
335 }
336
337 namespace simgrid {
338 namespace java {
339
340 /** Run the Java org.simgrid.msg.Process */
341 void java_main_jprocess(jobject jprocess)
342 {
343   JNIEnv *env = get_current_thread_env();
344   simgrid::java::JavaContext* context =
345     (simgrid::java::JavaContext*) SIMIX_context_self();
346   context->jprocess = jprocess;
347   smx_process_t process = SIMIX_process_self();
348   jprocess_bind(context->jprocess, process, env);
349
350   // Adrien, ugly path, just to bypass creation of context at low levels
351   // (i.e such as for the VM migration for instance)
352   if (context->jprocess == nullptr)
353     return;
354   else
355     run_jprocess(env, context->jprocess);
356 }
357
358 }
359 }
360
361
362
363 #include "simgrid/plugins/energy.h"
364 JNIEXPORT void JNICALL
365 Java_org_simgrid_msg_Msg_energyInit(void) {
366   sg_energy_plugin_init();
367 }