Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
95b89eb36699eeaafbafe85740128e6c6d332de1
[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
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 "jmsg_application_handler.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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
35
36 static JavaVM *__java_vm = NULL;
37
38
39 JavaVM *get_java_VM(void)
40 {
41   return __java_vm;
42 }
43
44 JNIEnv *get_current_thread_env(void)
45 {
46   JNIEnv *env;
47
48   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
49
50   return env;
51 }
52
53 /*
54  * The MSG process connected functions implementation.                                 
55  */
56
57 JNIEXPORT void JNICALL
58 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
59                                          jobject jprocess_arg,
60                                          jobject jhostname)
61 {
62      
63    
64   jobject jprocess;             /* the global reference to the java process instance    */
65   jstring jname;                /* the name of the java process instance                */
66   const char *name;             /* the C name of the process                            */
67   const char *hostname;
68   m_process_t process;          /* the native process to create                         */
69   m_host_t host;                /* Where that process lives */
70    
71   hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
72
73   XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,host=%s)",
74          env, cls, jprocess_arg, hostname);
75    
76    
77   /* get the name of the java process */
78   jname = jprocess_get_name(jprocess_arg, env);
79   if (!jname) {
80     jxbt_throw_null(env,
81             xbt_strdup("Internal error: Process name cannot be NULL"));
82     return;
83   }
84
85   /* bind/retrieve the msg host */
86   host = MSG_get_host_by_name(hostname);
87
88   if (!(host)) {    /* not binded */
89     jxbt_throw_host_not_found(env, hostname);
90     return;
91   }
92
93   /* create a global java process instance */
94   jprocess = jprocess_new_global_ref(jprocess_arg, env);
95   if (!jprocess) {
96     jxbt_throw_jni(env, "Can't get a global ref to the java process");
97     return;
98   }
99
100   /* build the C name of the process */
101   name = (*env)->GetStringUTFChars(env, jname, 0);
102   name = xbt_strdup(name);
103   
104   /* Actually build the MSG process */
105   process = MSG_process_create_with_environment(name,
106                                                 (xbt_main_func_t) jprocess,
107                                                 /*data*/ NULL,
108                                                 host,
109                                                 /* kill_time */-1,
110                                                 /*argc, argv, properties*/
111                                                 0,NULL,NULL);
112      
113   MSG_process_set_data(process,&process);
114    
115   /* release our reference to the process name (variable name becomes invalid) */
116   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
117   //(*env)->ReleaseStringUTFChars(env, jname, name);
118   (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
119    
120   /* bind the java process instance to the native process */
121   jprocess_bind(jprocess, process, env);
122
123 }
124
125 JNIEXPORT void JNICALL
126 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
127                                           jobject jprocess)
128 {
129   m_process_t process = jprocess_to_native_process(jprocess, env);
130
131   if (!process) {
132     jxbt_throw_notbound(env, "process", jprocess);
133     return;
134   }
135
136   /* try to suspend the process */
137   MSG_error_t rv = MSG_process_suspend(process);
138
139   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
140                  bprintf("unexpected error , please report this bug"));
141
142 }
143
144 JNIEXPORT void JNICALL
145 Java_org_simgrid_msg_Process_simulatedSleep(JNIEnv * env, jobject jprocess,
146                                            jdouble jseconds) {
147           m_process_t process = jprocess_to_native_process(jprocess, env);
148
149           if (!process) {
150             jxbt_throw_notbound(env, "process", jprocess);
151             return;
152           }
153           MSG_error_t rv = MSG_process_sleep((double)jseconds);
154
155           jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
156                          bprintf("unexpected error , please report this bug"));
157 }
158
159
160 JNIEXPORT void JNICALL
161 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
162                                          jobject jprocess)
163 {
164   m_process_t process = jprocess_to_native_process(jprocess, env);
165
166   if (!process) {
167     jxbt_throw_notbound(env, "process", jprocess);
168     return;
169   }
170
171   /* try to resume the process */
172   MSG_error_t rv = MSG_process_resume(process);
173
174   jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
175                  bprintf("unexpected error , please report this bug"));
176 }
177
178 JNIEXPORT jboolean JNICALL
179 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
180                                               jobject jprocess)
181 {
182   m_process_t process = jprocess_to_native_process(jprocess, env);
183
184   if (!process) {
185     jxbt_throw_notbound(env, "process", jprocess);
186     return 0;
187   }
188
189   /* true is the process is suspended, false otherwise */
190   return (jboolean) MSG_process_is_suspended(process);
191 }
192
193 JNIEXPORT void JNICALL
194 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
195                                        jobject jprocess)
196 {
197   /* get the native instances from the java ones */
198   m_process_t process = jprocess_to_native_process(jprocess, env);
199
200   if (!process) {
201     jxbt_throw_notbound(env, "process", jprocess);
202     return;
203   }
204
205   /* kill the native process (this wrapper is call by the destructor of the java 
206    * process instance)
207    */
208   MSG_process_kill(process);
209 }
210
211 JNIEXPORT jobject JNICALL
212 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
213                                           jobject jprocess)
214 {
215   /* get the native instances from the java ones */
216   m_process_t process = jprocess_to_native_process(jprocess, env);
217   m_host_t host;
218
219   if (!process) {
220     jxbt_throw_notbound(env, "process", jprocess);
221     return NULL;
222   }
223
224   host = MSG_process_get_host(process);
225   jobject res = (jobject)MSG_host_get_data(host);
226
227   if (!res) {
228           XBT_INFO("Binding error for host %s ",MSG_host_get_name(host));
229           jxbt_throw_jni(env, bprintf("Binding error for host %s ",MSG_host_get_name(host)));
230           return NULL;
231   }
232
233   /* return the global reference to the java host instance */
234   return res;
235
236 }
237
238 JNIEXPORT jobject JNICALL
239 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
240                                           jint PID)
241 {
242   m_process_t process = MSG_process_from_PID(PID);
243
244   if (!process) {
245     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
246     return NULL;
247   }
248
249   if (!native_to_java_process(process)) {
250     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
251     return NULL;
252   }
253
254   return (jobject) (native_to_java_process(process));
255 }
256
257
258 JNIEXPORT jint JNICALL
259 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
260                                          jobject jprocess)
261 {
262   m_process_t process = jprocess_to_native_process(jprocess, env);
263
264   if (!process) {
265     jxbt_throw_notbound(env, "process", jprocess);
266     return 0;
267   }
268
269   return (jint) MSG_process_get_PID(process);
270 }
271
272
273 JNIEXPORT jint JNICALL
274 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
275                                           jobject jprocess)
276 {
277   m_process_t process = jprocess_to_native_process(jprocess, env);
278
279   if (!process) {
280     jxbt_throw_notbound(env, "process", jprocess);
281     return 0;
282   }
283
284   return (jint) MSG_process_get_PPID(process);
285 }
286
287 JNIEXPORT jobject JNICALL
288 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
289 {
290   m_process_t process = MSG_process_self();
291   jobject jprocess;
292
293   if (!process) {
294     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
295     return NULL;
296   }
297
298   jprocess = native_to_java_process(process);
299
300   if (!jprocess)
301     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
302
303   return jprocess;
304 }
305
306 JNIEXPORT void JNICALL
307 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
308                                              jobject jprocess, jobject jhost)
309 {
310   m_process_t process = jprocess_to_native_process(jprocess, env);
311
312   if (!process) {
313     jxbt_throw_notbound(env, "process", jprocess);
314     return;
315   }
316
317   m_host_t host = jhost_get_native(env, jhost);
318
319   if (!host) {
320     jxbt_throw_notbound(env, "host", jhost);
321     return;
322   }
323
324   /* try to change the host of the process */
325   MSG_error_t rv = MSG_process_migrate(process, host);
326   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
327                  bprintf("unexpected error , please report this bug"));
328
329 }
330
331 JNIEXPORT void JNICALL
332 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
333                                           jdouble seconds)
334 {
335   MSG_error_t rv = MSG_process_sleep((double) seconds);
336
337   jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
338                  bprintf("while process was waiting for %f seconds",
339                          (double) seconds));
340
341 }
342
343
344 /***************************************************************************************
345  * The MSG host connected functions implementation.                                    *
346  ***************************************************************************************/
347
348 JNIEXPORT jint JNICALL
349 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
350                                        jobject jhost)
351 {
352   m_host_t host = jhost_get_native(env, jhost);
353
354   if (!host) {
355     jxbt_throw_notbound(env, "host", jhost);
356     return -1;
357   }
358
359   return (jint) MSG_get_host_msgload(host);
360 }
361
362
363 /***************************************************************************************
364  * Unsortable functions                                                        *
365  ***************************************************************************************/
366
367 JNIEXPORT jdouble JNICALL
368 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
369 {
370   return (jdouble) MSG_get_clock();
371 }
372
373 JNIEXPORT void JNICALL
374 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
375 {
376   char **argv = NULL;
377   int index;
378   int argc = 0;
379   jstring jval;
380   const char *tmp;
381
382   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
383
384   if (jargs)
385     argc = (int) (*env)->GetArrayLength(env, jargs);
386
387   argc++;
388   argv = xbt_new(char *, argc + 1);
389   argv[0] = strdup("java");
390
391   for (index = 0; index < argc - 1; index++) {
392     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
393     tmp = (*env)->GetStringUTFChars(env, jval, 0);
394     argv[index + 1] = strdup(tmp);
395     (*env)->ReleaseStringUTFChars(env, jval, tmp);
396   }
397   argv[argc] = NULL;
398
399   MSG_global_init(&argc, argv);
400
401   for (index = 0; index < argc; index++)
402     free(argv[index]);
403
404   free(argv);
405
406   (*env)->GetJavaVM(env, &__java_vm);
407 }
408
409 JNIEXPORT void JNICALL
410     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
411 {
412   MSG_error_t rv;
413   int index;
414   xbt_dynar_t hosts;
415   jobject jhost;
416
417   /* Run everything */
418   XBT_INFO("Ready to run MSG_MAIN");
419   rv = MSG_main();
420   XBT_INFO("Done running MSG_MAIN");
421   jxbt_check_res("MSG_main()", rv, MSG_OK,
422                  bprintf
423                  ("unexpected error : MSG_main() failed .. please report this bug "));
424
425   XBT_INFO("MSG_main finished");
426
427   XBT_INFO("Clean java world");
428   /* Cleanup java hosts */
429   hosts = MSG_hosts_as_dynar();
430   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
431     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
432     if (jhost)
433       jhost_unref(env, jhost);
434
435   }
436   xbt_dynar_free(&hosts);
437   XBT_INFO("Clean native world");
438 }
439 JNIEXPORT void JNICALL
440     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
441 {
442   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
443   MSG_error_t rv = MSG_OK != MSG_clean();
444   jxbt_check_res("MSG_clean()", rv, MSG_OK,
445                  bprintf
446                  ("unexpected error : MSG_clean() failed .. please report this bug "));
447 }
448    
449 JNIEXPORT jint JNICALL
450 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
451                                           jint jresetPID)
452 {
453   return (jint) MSG_process_killall((int) jresetPID);
454 }
455
456 JNIEXPORT void JNICALL
457 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
458                                        jstring jplatformFile)
459 {
460
461   const char *platformFile =
462       (*env)->GetStringUTFChars(env, jplatformFile, 0);
463
464   MSG_create_environment(platformFile);
465
466   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
467 }
468
469 JNIEXPORT void JNICALL
470 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
471                                        jobject jprocess)
472 {
473
474   m_process_t process = jprocess_to_native_process(jprocess, env);
475
476   if (!process) {
477     jxbt_throw_notbound(env, "process", jprocess);
478     return;
479   }
480
481   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
482 }
483
484 JNIEXPORT void JNICALL
485 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
486 {
487   const char *s = (*env)->GetStringUTFChars(env, js, 0);
488   XBT_INFO("%s", s);
489   (*env)->ReleaseStringUTFChars(env, js, s);
490 }
491
492 JNIEXPORT jobjectArray JNICALL
493 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
494 {
495   int index;
496   jobjectArray jtable;
497   jobject jhost;
498   jstring jname;
499   m_host_t host;
500
501   xbt_dynar_t table =  MSG_hosts_as_dynar();
502   int count = xbt_dynar_length(table);
503
504   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
505
506   if (!cls) {
507     return NULL;
508   }
509
510   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
511
512   if (!jtable) {
513     jxbt_throw_jni(env, "Hosts table allocation failed");
514     return NULL;
515   }
516
517   for (index = 0; index < count; index++) {
518     host = xbt_dynar_get_as(table,index,m_host_t);
519     jhost = (jobject) (MSG_host_get_data(host));
520
521     if (!jhost) {
522       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
523
524       jhost =
525                 Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
526       /* FIXME: leak of jname ? */
527     }
528
529     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
530   }
531   xbt_dynar_free(&table);
532   return jtable;
533 }
534
535 JNIEXPORT void JNICALL
536 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
537                                        jstring jdeploymentFile)
538 {
539
540   const char *deploymentFile =
541       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
542
543   surf_parse_reset_callbacks();
544
545   surfxml_add_callback(STag_surfxml_process_cb_list,
546                        japplication_handler_on_begin_process);
547
548   surfxml_add_callback(ETag_surfxml_argument_cb_list,
549                        japplication_handler_on_process_arg);
550
551   surfxml_add_callback(STag_surfxml_prop_cb_list,
552                        japplication_handler_on_property);
553
554   surfxml_add_callback(ETag_surfxml_process_cb_list,
555                        japplication_handler_on_end_process);
556
557   surf_parse_open(deploymentFile);
558
559   japplication_handler_on_start_document();
560
561   if (surf_parse())
562     jxbt_throw_jni(env, "surf_parse() failed");
563
564   surf_parse_close();
565
566   japplication_handler_on_end_document();
567
568   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
569 }