Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add reduce scatter collectives from openmpi, and fix existing one
[simgrid.git] / src / bindings / java / jmsg.c
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007-2012. 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 <msg/msg.h>
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
12 #include <locale.h>
13
14 #include "smx_context_java.h"
15 #include "smx_context_cojava.h"
16
17 #include "jmsg_process.h"
18
19 #include "jmsg_host.h"
20 #include "jmsg_task.h"
21 #include "jxbt_utilities.h"
22
23 #include "jmsg.h"
24
25 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
26 #ifndef JNIEXPORT
27 #define JNIEXPORT
28 #endif
29 #ifndef JNICALL
30 #define JNICALL
31 #endif
32 /* end of eclipse-mandated pimple */
33
34 static int create_jprocess(int argc, char *argv[]);
35
36 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
37
38 JavaVM *__java_vm = NULL;
39
40 JavaVM *get_java_VM(void)
41 {
42   return __java_vm;
43 }
44
45 JNIEnv *get_current_thread_env(void)
46 {
47   JNIEnv *env;
48
49   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
50   return env;
51 }
52
53 void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
54   switch (status) {
55     case MSG_TIMEOUT:
56         jxbt_throw_time_out_failure(env,NULL);
57     break;
58     case MSG_TRANSFER_FAILURE:
59         jxbt_throw_transfer_failure(env,NULL);
60     break;
61     case MSG_HOST_FAILURE:
62         jxbt_throw_host_failure(env,NULL);
63     break;
64     default:
65         jxbt_throw_native(env,xbt_strdup("communication failed"));
66   }
67 }
68
69
70 /***************************************************************************************
71  * Unsortable functions                                                        *
72  ***************************************************************************************/
73
74 JNIEXPORT jdouble JNICALL
75 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
76 {
77   return (jdouble) MSG_get_clock();
78 }
79
80 JNIEXPORT void JNICALL
81 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
82 {
83   char **argv = NULL;
84   int index;
85   int argc = 0;
86   jstring jval;
87   const char *tmp;
88
89   XBT_LOG_CONNECT(jmsg);
90 #ifdef HAVE_TRACING
91   XBT_LOG_CONNECT(jtrace);
92 #endif
93
94   (*env)->GetJavaVM(env, &__java_vm);
95
96   if ((*env)->FindClass(env, "java/dyn/Coroutine")) {
97         XBT_INFO("Using Coroutines. Your simulation is on steroid.");
98         smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
99   }
100   else {
101         XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
102         smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
103   }
104   jthrowable exc = (*env)->ExceptionOccurred(env);
105   if (exc) {
106     (*env)->ExceptionClear(env);
107   }
108
109   setlocale(LC_NUMERIC,"C");
110
111   if (jargs)
112     argc = (int) (*env)->GetArrayLength(env, jargs);
113
114   argc++;
115   argv = xbt_new(char *, argc + 1);
116   argv[0] = strdup("java");
117
118   for (index = 0; index < argc - 1; index++) {
119     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
120     tmp = (*env)->GetStringUTFChars(env, jval, 0);
121     argv[index + 1] = strdup(tmp);
122     (*env)->ReleaseStringUTFChars(env, jval, tmp);
123   }
124   argv[argc] = NULL;
125
126   MSG_init(&argc, argv);
127
128   for (index = 0; index < argc; index++)
129     free(argv[index]);
130
131   free(argv);
132 }
133
134 JNIEXPORT void JNICALL
135     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
136 {
137   msg_error_t rv;
138   int index;
139   xbt_dynar_t hosts;
140   jobject jhost;
141
142   /* Run everything */
143   XBT_DEBUG("Ready to run MSG_MAIN");
144   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 "));
148
149   XBT_INFO("MSG_main finished; Cleaning up the simulation...");
150   /* Cleanup java hosts */
151   hosts = MSG_hosts_as_dynar();
152   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
153     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,msg_host_t));
154     if (jhost)
155       jhost_unref(env, jhost);
156
157   }
158   xbt_dynar_free(&hosts);
159 }
160
161 JNIEXPORT void JNICALL
162 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
163                                        jstring jplatformFile)
164 {
165
166   const char *platformFile =
167       (*env)->GetStringUTFChars(env, jplatformFile, 0);
168
169   MSG_create_environment(platformFile);
170
171   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
172 }
173 JNIEXPORT void JNICALL
174 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
175 {
176   const char *s = (*env)->GetStringUTFChars(env, js, 0);
177   XBT_DEBUG("%s", s);
178   (*env)->ReleaseStringUTFChars(env, js, s);
179 }
180 JNIEXPORT void JNICALL
181 Java_org_simgrid_msg_Msg_verb(JNIEnv * env, jclass cls, jstring js)
182 {
183   const char *s = (*env)->GetStringUTFChars(env, js, 0);
184   XBT_VERB("%s", s);
185   (*env)->ReleaseStringUTFChars(env, js, s);
186 }
187 JNIEXPORT void JNICALL
188 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
189 {
190   const char *s = (*env)->GetStringUTFChars(env, js, 0);
191   XBT_INFO("%s", s);
192   (*env)->ReleaseStringUTFChars(env, js, s);
193 }
194 JNIEXPORT void JNICALL
195 Java_org_simgrid_msg_Msg_warn(JNIEnv * env, jclass cls, jstring js)
196 {
197   const char *s = (*env)->GetStringUTFChars(env, js, 0);
198   XBT_WARN("%s", s);
199   (*env)->ReleaseStringUTFChars(env, js, s);
200 }
201 JNIEXPORT void JNICALL
202 Java_org_simgrid_msg_Msg_error(JNIEnv * env, jclass cls, jstring js)
203 {
204   const char *s = (*env)->GetStringUTFChars(env, js, 0);
205   XBT_ERROR("%s", s);
206   (*env)->ReleaseStringUTFChars(env, js, s);
207 }
208 JNIEXPORT void JNICALL
209 Java_org_simgrid_msg_Msg_critical(JNIEnv * env, jclass cls, jstring js)
210 {
211   const char *s = (*env)->GetStringUTFChars(env, js, 0);
212   XBT_CRITICAL("%s", s);
213   (*env)->ReleaseStringUTFChars(env, js, s);
214 }
215 JNIEXPORT void JNICALL
216 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
217                                        jstring jdeploymentFile)
218 {
219
220   const char *deploymentFile =
221       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
222
223   SIMIX_function_register_default(create_jprocess);
224   MSG_launch_application(deploymentFile);
225 }
226 /**
227  * Function called when there is the need to create the java Process object
228  * (when we are using deployement files).
229  * it HAS to be executed on the process context, else really bad things will happen.
230  */
231 static int create_jprocess(int argc, char *argv[]) {
232   JNIEnv *env = get_current_thread_env();
233   //Change the "." in class name for "/".
234   xbt_str_subst(argv[0],'.','/',0);
235   jclass class_Process = (*env)->FindClass(env, argv[0]);
236   xbt_str_subst(argv[0],'/','.',0);
237   //Retrieve the methodID for the constructor
238   xbt_assert((class_Process != NULL), "Class not found (%s).", argv[0]);
239   jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
240   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
241
242   //Retrieve the name of the process.
243   jstring jname = (*env)->NewStringUTF(env, argv[0]);
244   //Build the arguments
245   jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
246   (*env)->FindClass(env,"java/lang/String"),
247   (*env)->NewStringUTF(env,"")));
248   int i;
249   for (i = 1; i < argc; i++)
250       (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
251   //Retrieve the host for the process.
252   jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
253   jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
254   //creates the process
255   jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
256   xbt_assert((jprocess != NULL), "Process allocation failed.");
257   jprocess = (*env)->NewGlobalRef(env, jprocess);
258   //bind the process to the context
259   msg_process_t process = MSG_process_self();
260   smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
261   context->jprocess = jprocess;
262 /* sets the PID and the PPID of the process */
263 (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
264 (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
265   jprocess_bind(jprocess, process, env);
266
267   return 0;
268 }
269