Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move all the remaining stuff for Task from MsgNative to Task
[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 jobject JNICALL
349 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
350                                          jstring jname)
351 {
352   m_host_t host;                /* native host                                          */
353   jobject jhost;                /* global reference to the java host instance returned  */
354
355   /* get the C string from the java string */
356   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
357   XBT_DEBUG("Looking for host '%s'",name);
358   /* get the host by name       (the hosts are created during the grid resolution) */
359   host = MSG_get_host_by_name(name);
360   XBT_DEBUG("MSG gave %p as native host", host);
361
362   if (!host) {                  /* invalid name */
363     jxbt_throw_host_not_found(env, name);
364     (*env)->ReleaseStringUTFChars(env, jname, name);
365     return NULL;
366   }
367   (*env)->ReleaseStringUTFChars(env, jname, name);
368
369   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
370
371     /* Instantiate a new java host */
372     jhost = jhost_new_instance(env);
373
374     if (!jhost) {
375       jxbt_throw_jni(env, "java host instantiation failed");
376       return NULL;
377     }
378
379     /* get a global reference to the newly created host */
380     jhost = jhost_ref(env, jhost);
381
382     if (!jhost) {
383       jxbt_throw_jni(env, "new global ref allocation failed");
384       return NULL;
385     }
386
387     /* bind the java host and the native host */
388     jhost_bind(jhost, host, env);
389
390     /* the native host data field is set with the global reference to the 
391      * java host returned by this function 
392      */
393     MSG_host_set_data(host, (void *) jhost);
394   }
395
396   /* return the global reference to the java host instance */
397   return (jobject) MSG_host_get_data(host);
398 }
399
400 JNIEXPORT jstring JNICALL
401 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
402                                        jobject jhost)
403 {
404   m_host_t host = jhost_get_native(env, jhost);
405   const char* name;
406
407   if (!host) {
408     jxbt_throw_notbound(env, "host", jhost);
409     return NULL;
410   }
411
412   name = MSG_host_get_name(host);
413   if (!name)
414           xbt_die("This host has no name...");
415
416   return (*env)->NewStringUTF(env, name);
417 }
418
419 JNIEXPORT jint JNICALL
420 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
421 {
422   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
423   int nb_host = xbt_dynar_length(hosts);
424   xbt_dynar_free(&hosts);
425   return (jint) nb_host;
426 }
427
428 JNIEXPORT jobject JNICALL
429 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
430 {
431   jobject jhost;
432
433   m_host_t host = MSG_host_self();
434
435   if (!MSG_host_get_data(host)) {
436     /* the native host not yet associated with the java host instance */
437
438     /* instanciate a new java host instance */
439     jhost = jhost_new_instance(env);
440
441     if (!jhost) {
442       jxbt_throw_jni(env, "java host instantiation failed");
443       return NULL;
444     }
445
446     /* get a global reference to the newly created host */
447     jhost = jhost_ref(env, jhost);
448
449     if (!jhost) {
450       jxbt_throw_jni(env, "global ref allocation failed");
451       return NULL;
452     }
453
454     /* Bind & store it */
455     jhost_bind(jhost, host, env);
456     MSG_host_set_data(host, (void *) jhost);
457   } else {
458     jhost = (jobject) MSG_host_get_data(host);
459   }
460
461   return jhost;
462 }
463
464 JNIEXPORT jdouble JNICALL
465 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
466                                         jobject jhost)
467 {
468   m_host_t host = jhost_get_native(env, jhost);
469
470   if (!host) {
471     jxbt_throw_notbound(env, "host", jhost);
472     return -1;
473   }
474
475   return (jdouble) MSG_get_host_speed(host);
476 }
477
478 JNIEXPORT jint JNICALL
479 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
480                                        jobject jhost)
481 {
482   m_host_t host = jhost_get_native(env, jhost);
483
484   if (!host) {
485     jxbt_throw_notbound(env, "host", jhost);
486     return -1;
487   }
488
489   return (jint) MSG_get_host_msgload(host);
490 }
491
492
493 JNIEXPORT jboolean JNICALL
494 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
495                                        jobject jhost)
496 {
497   m_host_t host = jhost_get_native(env, jhost);
498
499   if (!host) {
500     jxbt_throw_notbound(env, "host", jhost);
501     return 0;
502   }
503
504   return (jboolean) MSG_host_is_avail(host);
505 }
506
507 /***************************************************************************************
508  * Unsortable functions                                                        *
509  ***************************************************************************************/
510
511 JNIEXPORT jdouble JNICALL
512 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
513 {
514   return (jdouble) MSG_get_clock();
515 }
516
517 JNIEXPORT void JNICALL
518 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
519 {
520   char **argv = NULL;
521   int index;
522   int argc = 0;
523   jstring jval;
524   const char *tmp;
525
526   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
527
528   if (jargs)
529     argc = (int) (*env)->GetArrayLength(env, jargs);
530
531   argc++;
532   argv = xbt_new(char *, argc + 1);
533   argv[0] = strdup("java");
534
535   for (index = 0; index < argc - 1; index++) {
536     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
537     tmp = (*env)->GetStringUTFChars(env, jval, 0);
538     argv[index + 1] = strdup(tmp);
539     (*env)->ReleaseStringUTFChars(env, jval, tmp);
540   }
541   argv[argc] = NULL;
542
543   MSG_global_init(&argc, argv);
544
545   for (index = 0; index < argc; index++)
546     free(argv[index]);
547
548   free(argv);
549
550   (*env)->GetJavaVM(env, &__java_vm);
551 }
552
553 JNIEXPORT void JNICALL
554     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
555 {
556   MSG_error_t rv;
557   int index;
558   xbt_dynar_t hosts;
559   jobject jhost;
560
561   /* Run everything */
562   XBT_INFO("Ready to run MSG_MAIN");
563   rv = MSG_main();
564   XBT_INFO("Done running MSG_MAIN");
565   jxbt_check_res("MSG_main()", rv, MSG_OK,
566                  bprintf
567                  ("unexpected error : MSG_main() failed .. please report this bug "));
568
569   XBT_INFO("MSG_main finished");
570
571   XBT_INFO("Clean java world");
572   /* Cleanup java hosts */
573   hosts = MSG_hosts_as_dynar();
574   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
575     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
576     if (jhost)
577       jhost_unref(env, jhost);
578
579   }
580   xbt_dynar_free(&hosts);
581   XBT_INFO("Clean native world");
582 }
583 JNIEXPORT void JNICALL
584     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
585 {
586   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
587   MSG_error_t rv = MSG_OK != MSG_clean();
588   jxbt_check_res("MSG_clean()", rv, MSG_OK,
589                  bprintf
590                  ("unexpected error : MSG_clean() failed .. please report this bug "));
591 }
592    
593 JNIEXPORT jint JNICALL
594 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
595                                           jint jresetPID)
596 {
597   return (jint) MSG_process_killall((int) jresetPID);
598 }
599
600 JNIEXPORT void JNICALL
601 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
602                                        jstring jplatformFile)
603 {
604
605   const char *platformFile =
606       (*env)->GetStringUTFChars(env, jplatformFile, 0);
607
608   MSG_create_environment(platformFile);
609
610   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
611 }
612
613 JNIEXPORT void JNICALL
614 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
615                                        jobject jprocess)
616 {
617
618   m_process_t process = jprocess_to_native_process(jprocess, env);
619
620   if (!process) {
621     jxbt_throw_notbound(env, "process", jprocess);
622     return;
623   }
624
625   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
626 }
627
628 JNIEXPORT void JNICALL
629 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
630 {
631   const char *s = (*env)->GetStringUTFChars(env, js, 0);
632   XBT_INFO("%s", s);
633   (*env)->ReleaseStringUTFChars(env, js, s);
634 }
635
636 JNIEXPORT jobjectArray JNICALL
637 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
638 {
639   int index;
640   jobjectArray jtable;
641   jobject jhost;
642   jstring jname;
643   m_host_t host;
644
645   xbt_dynar_t table =  MSG_hosts_as_dynar();
646   int count = xbt_dynar_length(table);
647
648   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
649
650   if (!cls) {
651     return NULL;
652   }
653
654   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
655
656   if (!jtable) {
657     jxbt_throw_jni(env, "Hosts table allocation failed");
658     return NULL;
659   }
660
661   for (index = 0; index < count; index++) {
662     host = xbt_dynar_get_as(table,index,m_host_t);
663     jhost = (jobject) (MSG_host_get_data(host));
664
665     if (!jhost) {
666       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
667
668       jhost =
669           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
670       /* FIXME: leak of jname ? */
671     }
672
673     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
674   }
675   xbt_dynar_free(&table);
676   return jtable;
677 }
678
679 JNIEXPORT void JNICALL
680 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
681                                        jstring jdeploymentFile)
682 {
683
684   const char *deploymentFile =
685       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
686
687   surf_parse_reset_callbacks();
688
689   surfxml_add_callback(STag_surfxml_process_cb_list,
690                        japplication_handler_on_begin_process);
691
692   surfxml_add_callback(ETag_surfxml_argument_cb_list,
693                        japplication_handler_on_process_arg);
694
695   surfxml_add_callback(STag_surfxml_prop_cb_list,
696                        japplication_handler_on_property);
697
698   surfxml_add_callback(ETag_surfxml_process_cb_list,
699                        japplication_handler_on_end_process);
700
701   surf_parse_open(deploymentFile);
702
703   japplication_handler_on_start_document();
704
705   if (surf_parse())
706     jxbt_throw_jni(env, "surf_parse() failed");
707
708   surf_parse_close();
709
710   japplication_handler_on_end_document();
711
712   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
713 }