Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix in Comm (GC didn't like it.). Add "debug" logging
[simgrid.git] / src / jmsg.c
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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
16 #include "jmsg_process.h"
17
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jxbt_utilities.h"
21
22 #include "jmsg.h"
23
24 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
25 #ifndef JNIEXPORT
26 #define JNIEXPORT
27 #endif
28 #ifndef JNICALL
29 #define JNICALL
30 #endif
31 /* end of eclipse-mandated pimple */
32
33 static int create_jprocess(int argc, char *argv[]);
34
35 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
36
37 static JavaVM *__java_vm = NULL;
38
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,bprintf("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   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
90
91   setlocale(LC_NUMERIC,"C");
92
93   if (jargs)
94     argc = (int) (*env)->GetArrayLength(env, jargs);
95
96   argc++;
97   argv = xbt_new(char *, argc + 1);
98   argv[0] = strdup("java");
99
100   for (index = 0; index < argc - 1; index++) {
101     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
102     tmp = (*env)->GetStringUTFChars(env, jval, 0);
103     argv[index + 1] = strdup(tmp);
104     (*env)->ReleaseStringUTFChars(env, jval, tmp);
105   }
106   argv[argc] = NULL;
107
108   MSG_global_init(&argc, argv);
109
110   for (index = 0; index < argc; index++)
111     free(argv[index]);
112
113   free(argv);
114
115   (*env)->GetJavaVM(env, &__java_vm);
116 }
117
118 JNIEXPORT void JNICALL
119     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
120 {
121   MSG_error_t rv;
122   int index;
123   xbt_dynar_t hosts;
124   jobject jhost;
125
126   /* Run everything */
127   XBT_INFO("Ready to run MSG_MAIN");
128   rv = MSG_main();
129   XBT_INFO("Done running MSG_MAIN");
130   jxbt_check_res("MSG_main()", rv, MSG_OK,
131                  bprintf
132                  ("unexpected error : MSG_main() failed .. please report this bug "));
133
134   XBT_INFO("MSG_main finished");
135
136   XBT_INFO("Clean java world");
137   /* Cleanup java hosts */
138   hosts = MSG_hosts_as_dynar();
139   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
140     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
141     if (jhost)
142       jhost_unref(env, jhost);
143
144   }
145   xbt_dynar_free(&hosts);
146   XBT_INFO("Clean native world");
147 }
148 JNIEXPORT void JNICALL
149     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
150 {
151   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
152   MSG_error_t rv = MSG_OK != MSG_clean();
153   jxbt_check_res("MSG_clean()", rv, MSG_OK,
154                  bprintf
155                  ("unexpected error : MSG_clean() failed .. please report this bug "));
156 }
157
158 JNIEXPORT void JNICALL
159 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
160                                        jstring jplatformFile)
161 {
162
163   const char *platformFile =
164       (*env)->GetStringUTFChars(env, jplatformFile, 0);
165
166   MSG_create_environment(platformFile);
167
168   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
169 }
170 JNIEXPORT void JNICALL
171 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
172 {
173   const char *s = (*env)->GetStringUTFChars(env, js, 0);
174   XBT_DEBUG("%s", s);
175   (*env)->ReleaseStringUTFChars(env, js, s);
176 }
177 JNIEXPORT void JNICALL
178 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
179 {
180   const char *s = (*env)->GetStringUTFChars(env, js, 0);
181   XBT_INFO("%s", s);
182   (*env)->ReleaseStringUTFChars(env, js, s);
183 }
184
185 JNIEXPORT void JNICALL
186 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
187                                        jstring jdeploymentFile)
188 {
189
190   const char *deploymentFile =
191       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
192
193   SIMIX_function_register_default(create_jprocess);
194   MSG_launch_application(deploymentFile);
195 }
196 /**
197  * Function called when there is the need to create the java Process object
198  * (when we are using deployement files).
199  * it HAS to be executed on the process context, else really bad things will happen.
200  */
201 static int create_jprocess(int argc, char *argv[]) {
202         JNIEnv *env = get_current_thread_env();
203         //Change the "." in class name for "/".
204         xbt_str_subst(argv[0],'.','/',0);
205         jclass class_Process = (*env)->FindClass(env, argv[0]);
206         xbt_str_subst(argv[0],'/','.',0);
207         //Retrieve the methodID for the constructor
208         xbt_assert((class_Process != NULL), "Class not found.");
209         jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
210         xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
211
212         //Retrieve the name of the process.
213         jstring jname = (*env)->NewStringUTF(env, argv[0]);
214         //Build the arguments
215         jobjectArray args = (jobjectArray)((*env)->NewObjectArray(env,argc - 1,
216         (*env)->FindClass(env,"java/lang/String"),
217         (*env)->NewStringUTF(env,"")));
218         int i;
219         for (i = 1; i < argc; i++)
220                 (*env)->SetObjectArrayElement(env,args,i - 1,(*env)->NewStringUTF(env, argv[i]));
221         //Retrieve the host for the process.
222         jstring jhostName = (*env)->NewStringUTF(env, MSG_host_get_name(MSG_host_self()));
223         jobject jhost = Java_org_simgrid_msg_Host_getByName(env, NULL, jhostName);
224         //creates the process
225         jobject jprocess = (*env)->NewObject(env, class_Process, constructor_Process, jhost, jname, args);
226         xbt_assert((jprocess != NULL), "Process allocation failed.");
227         jprocess = (*env)->NewGlobalRef(env, jprocess);
228         //bind the process to the context
229         m_process_t process = MSG_process_self();
230         smx_ctx_java_t context = (smx_ctx_java_t)MSG_process_get_smx_ctx(process);
231         context->jprocess = jprocess;
232   /* sets the PID and the PPID of the process */
233   (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process));
234   (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process));
235
236         jprocess_bind(jprocess, process, env);
237
238         return 0;
239 }
240