2 * simgrid.msg.Msg.java 1.00 07/05/01
4 * Copyright 2006,2007 Martin Quinson, Malek Cherier
7 * This program is free software; you can redistribute
8 * it and/or modify it under the terms of the license
9 *(GNU LGPL) which comes with this package.
16 * MSG was the first distributed programming environment
17 * provided within SimGrid. While almost realistic, it
18 * remains quite simple.
19 * This class contains all the declaration of the natives methods
21 * All of these methods are statics. You can't directly use these methods
22 * but it is recommanded to use the classes Process, Host, Task and ParallelTask
23 * of the package to build your application.
25 * @author Abdelmalek Cherier
26 * @author Martin Quinson
27 * @version 1.00, 07/05/01
35 public final class Msg {
37 * The natively implemented method to get the last error code of the simulation.
39 * @return The last error code of the simulation.
41 final static native int getErrCode();
44 * Errors returned by the method Msg.getErrCode().
47 /* Everything is right. Keep on goin the way !
50 public static final int SUCCESS = 0;
52 /* Something must be not perfectly clean. But I
53 * may be paranoid freak... !
55 public static final int WARNING = 1;
57 /* There has been a problem during you task treansfer.
58 * Either the network is down or the remote host has
61 public static final int TRANSFERT_FAILURE = 2;
64 * System shutdown. The host on which you are running
65 * has just been rebooted. Free your datastructures and
68 public static final int HOST_FAILURE = 3;
71 * Cancelled task. This task has been cancelled by somebody !
73 public static final int TASK_CANCELLLED = 4;
76 * You've done something wrong. You'd better look at it...
78 public static final int FATAL_ERROR = 5;
81 * Staticaly load the SIMGRID4JAVA library
82 * which contains all native functions used in here
86 System.loadLibrary("simgrid");
87 Msg.selectContextFactory("jcontext_factory");
88 } catch(NativeException e) {
89 System.err.println(e.toString());
92 } catch(UnsatisfiedLinkError e) {
93 System.err.println("Cannot load simgrid library : ");
95 System.err.println("Please check your LD_LIBRARY_PATH, " +
96 "or copy the library to the current directory");
102 void selectContextFactory(String name) throws NativeException;
104 /******************************************************************
105 * The natively implemented methods connected to the MSG Process *
106 ******************************************************************/
109 * The natively implemented method to create an MSG process.
111 * @param process The java process object to bind with the MSG native process.
112 * @param host A valid (binded) host where create the process.
114 * @exception simgrid.msg.JniException if JNI stuff goes wrong.
116 * @see Process constructors.
119 void processCreate(Process process, Host host) throws JniException;
122 * The natively implemented method to kill all the process of the simulation.
124 * @param resetPID Should we reset the PID numbers. A negative number means no reset
125 * and a positive number will be used to set the PID of the next newly
128 * @return The function returns the PID of the next created process.
130 final static native int processKillAll(int resetPID);
133 * The natively implemented method to suspend an MSG process.
135 * @param process The valid (binded with a native process) java process to suspend.
137 * @exception JniException if something goes wrong with JNI
138 * NativeException if the SimGrid native code failed.
140 * @see Process.pause()
142 final static native void processSuspend(Process process)
143 throws JniException, NativeException;
146 * The natively implemented method to kill a MSG process.
148 * @param process The valid (binded with a native process) java process to kill.
150 * @see Process.kill()
152 final static native void processKill(Process process)
156 * The natively implemented method to resume a suspended MSG process.
158 * @param process The valid (binded with a native process) java process to resume.
160 * @exception JniException if something goes wrong with JNI
161 * NativeException if the SimGrid native code failed.
163 * @see Process.restart()
165 final static native void processResume(Process process)
166 throws JniException, NativeException;
169 * The natively implemented method to test if MSG process is suspended.
171 * @param process The valid (binded with a native process) java process to test.
173 * @return If the process is suspended the method retuns true. Otherwise the
174 * method returns false.
176 * @exception JniException if something goes wrong with JNI
178 * @see Process.isSuspended()
180 final static native boolean processIsSuspended(Process process) throws
184 * The natively implemented method to get the host of a MSG process.
186 * @param process The valid (binded with a native process) java process to get the host.
188 * @return The method returns the host where the process is running.
190 * @exception JniException if something goes wrong with JNI
191 * NativeException if the SimGrid native code failed.
193 * @see Process.getHost()
195 final static native Host processGetHost(Process process)
196 throws JniException, NativeException;
199 * The natively implemented method to get a MSG process from his PID.
201 * @param PID The PID of the process to get.
203 * @return The process with the specified PID.
205 * @exception NativeException if the SimGrid native code failed.
207 * @see Process.getFromPID()
209 final static native Process processFromPID(int PID) throws NativeException;
212 * The natively implemented method to get the PID of a MSG process.
214 * @param process The valid (binded with a native process) java process to get the PID.
216 * @return The PID of the specified process.
218 * @exception NativeException if the SimGrid native code failed.
220 * @see Process.getPID()
222 final static native int processGetPID(Process process) throws
226 * The natively implemented method to get the PPID of a MSG process.
228 * @param process The valid (binded with a native process) java process to get the PID.
230 * @return The PPID of the specified process.
232 * @exception NativeException if the SimGrid native code failed.
234 * @see Process.getPPID()
236 final static native int processGetPPID(Process process) throws
240 * The natively implemented method to get the current running process.
242 * @return The current process.
244 * @exception NativeException if the SimGrid native code failed.
246 * @see Process.currentProcess()
248 final static native Process processSelf() throws NativeException;
251 * The natively implemented method to get the current running process PID.
253 * @return The PID of the current process.
255 * @see Process.currentProcessPID()
257 final static native int processSelfPID();
260 * The natively implemented method to get the current running process PPID.
262 * @return The PPID of the current process.
264 * @see Process.currentProcessPPID()
266 final static native int processSelfPPID();
269 * The natively implemented method to migrate a process from his currnet host to a new host.
271 * @param process The (valid) process to migrate.
272 * @param host A (valid) host where move the process.
274 * @exception JniException if something goes wrong with JNI
275 * NativeException if the SimGrid native code failed.
277 * @see Process.migrate()
278 * @see Host.getByName()
280 final static native void processChangeHost(Process process, Host host)
281 throws JniException, NativeException;
284 * Process synchronization. The process wait the signal of the simulator to start.
286 * @exception JniException if something goes wrong with JNI
288 final static native void waitSignal(Process process) throws JniException;
291 * The natively implemented native to request the current process to sleep
292 * until time seconds have elapsed.
294 * @param seconds The time the current process must sleep.
296 * @exception NativeException if the SimGrid native code failed.
298 * @see Process.waitFor()
300 final static native void processWaitFor(double seconds) throws
304 * The natively implemented native method to exit a process.
306 * @exception JniException if something goes wrong with JNI
308 * @see Process.exit()
310 final static native void processExit(Process process) throws JniException;
313 /******************************************************************
314 * The natively implemented methods connected to the MSG host *
315 ******************************************************************/
318 * The natively implemented method to get an host from his name.
320 * @param name The name of the host to get.
322 * @return The host having the specified name.
324 * @exception JniException if something goes wrong with JNI
325 * HostNotFoundException if there is no such host
326 * NativeException if the SimGrid native code failed.
328 * @see Host.getByName()
330 final static native Host hostGetByName(String name)
331 throws JniException, HostNotFoundException, NativeException;
334 * The natively implemented method to get the name of an MSG host.
336 * @param host The host (valid) to get the name.
338 * @return The name of the specified host.
340 * @exception JniException if something goes wrong with JNI
342 * @see Host.getName()
344 final static native String hostGetName(Host host) throws JniException;
347 * The natively implemented method to get the number of hosts of the simulation.
349 * @return The number of hosts of the simulation.
351 * @see Host.getNumber()
353 final static native int hostGetNumber();
356 * The natively implemented method to get the host of the current runing process.
358 * @return The host of the current running process.
360 * @exception JniException if something goes wrong with JNI
362 * @see Host.currentHost()
364 final static native Host hostSelf() throws JniException;
367 * The natively implemented method to get the speed of a MSG host.
369 * @param host The host to get the host.
371 * @return The speed of the specified host.
373 * @exception JniException if something goes wrong with JNI
375 * @see Host.getSpeed()
378 final static native double hostGetSpeed(Host host) throws JniException;
381 * The natively implemented native method to test if an host is avail.
383 * @param host The host to test.
385 * @return If the host is avail the method returns true.
386 * Otherwise the method returns false.
388 * @exception JniException if something goes wrong with JNI
390 * @see Host.isAvail()
392 final static native boolean hostIsAvail(Host host) throws JniException;
395 * The natively implemented native method to get all the hosts of the simulation.
397 * @exception JniException if something goes wrong with JNI
399 * @return A array which contains all the hosts of simulation.
402 final static native Host[] allHosts() throws JniException;
405 * The natively implemented native method to get the number of running tasks on a host.
407 * @param The host concerned by the operation.
409 * @return The number of running tasks.
411 * @exception JniException if something goes wrong with JNI
414 final static native int hostGetLoad(Host host) throws JniException;
416 /******************************************************************
417 * The natively implemented methods connected to the MSG task *
418 ******************************************************************/
421 * The natively implemented method to create a MSG task.
423 * @param name The name of th task.
424 * @param computeDuration A value of the processing amount (in flop) needed
425 * to process the task. If 0, then it cannot be executed
426 * with the execute() method. This value has to be >= 0.
427 * @param messageSize A value of amount of data (in bytes) needed to transfert
428 * this task. If 0, then it cannot be transfered this task.
429 * If 0, then it cannot be transfered with the get() and put()
430 * methods. This value has to be >= 0.
431 * @param task The java task object to bind with the native task to create.
433 * @exception JniException if something goes wrong with JNI
434 * NullPointerException if the specified name is null.
435 * IllegalArgumentException if compute duration <0 or message size <0
439 final static native void taskCreate(Task task, String name,
440 double computeDuration,
442 throws JniException, NullPointerException, IllegalArgumentException;
445 * The natively implemented method to get the sender of a task.
447 * @param task The task (valid) to get the sender.
449 * @return The sender of the task.
451 * @exception InvalidTaskException is the specified task is not valid. A task
452 * is invalid if it is not binded with a native task.
454 * @see Task.getSender()
456 final static native Process taskGetSender(Task task) throws JniException;
459 * The natively implementd method to get the source of a task.
461 * @param task The task to get the source.
463 * @return The source of the task.
465 * @exception InvalidTaskException is the specified task is not valid. A task
466 * is invalid if it is not binded with a native task.
468 * @see Task.getSource()
470 final static native Host taskGetSource(Task task) throws JniException,
474 * The natively implemented method to get the name of the task.
476 * @param task The task to get the name.
478 * @return The name of the specified task.
480 * @exception InvalidTaskException is the specified task is not valid. A task
481 * is invalid if it is not binded with a native task.
483 * @see Task.getName()
485 final static native String taskGetName(Task task) throws JniException;
488 * The natively implemented method to cancel a task.
490 * @param task The task to cancel.
492 * @exception InvalidTaskException if the specified task is not valid. A task
493 * is invalid if it is not binded with a native task.
494 * MsgException if the cancelation failed.
496 * @see Task.cancel().
498 final static native void taskCancel(Task task) throws JniException,
502 * The natively implemented method to get the computing amount of the task.
504 * @param task The task to get the computing amount.
506 * @return The computing amount of the specified task.
508 * @exception InvalidTaskException if the specified task is not valid. A task
509 * is invalid if it is not binded with a native task.
510 * MsgException if the cancelation failed.
512 * @see Task.getComputeDuration()
514 final static native double taskGetComputeDuration(Task task) throws
518 * The natively implemented method to get the remaining computation
520 * @param task The task to get the remaining computation.
522 * @return The remaining computation of the specified task.
524 * @exception InvalidTaskException if the specified task is not valid. A task
525 * is invalid if it is not binded with a native task.
526 * MsgException if the cancelation failed.
528 * @see Task.getRemainingDuration()
530 final static native double taskGetRemainingDuration(Task task) throws
534 * The natively implemented method to set the priority of a task.
536 * @param task The task to set the prirotity
538 * @param priority The new priority of the specified task.
540 * @exception InvalidTaskException if the specified task is not valid. A task
541 * is invalid if it is not binded with a native task.
542 * MsgException if the cancelation failed.
544 * @see Task.setPriority()
546 final static native void taskSetPriority(Task task,
547 double priority) throws
551 * The natively implemented method to destroy a MSG task.
553 * @param The task to destroy.
555 * @exception InvalidTaskException is the specified task is not valid. A task
556 * is invalid if it is not binded with a native task.
557 * MsgException if the destruction failed.
559 * @see Task.destroy()
561 final static native void taskDestroy(Task task) throws JniException,
565 * The natively implemented method to execute a MSG task.
567 * @param task The task to execute.
569 * @exception InvalidTaskException is the specified task is not valid. A task
570 * is invalid if it is not binded with a native task.
571 * MsgException if the destruction failed.
573 * @see Task.execute()
575 final static native void taskExecute(Task task) throws JniException,
580 /**************************************************************************
581 * The natively implemented methods connected to the MSG parallel task *
582 ***************************************************************************/
585 * The natively implemented method to create a MSG parallel task.
587 * @param name The name of the parallel task.
588 * @param hosts The list of hosts implied by the parallel task.
589 * @param computeDurations The total number of operations that have to be performed
591 * @param messageSizes An array of doubles
593 * @see ParallelTask.create()
595 final static native void parallelTaskCreate(ParallelTask parallelTask,
596 String name, Host[]hosts,
597 double[]computeDurations,
598 double[]messageSizes)
599 throws JniException, NullPointerException, IllegalArgumentException;
602 * The natively implemented method to get the sender of a parallel task.
604 * @param parallelTask The parallel task (valid) to get the sender.
606 * @return The sender of the parallel task.
608 * @see ParallelTask.getSender()
610 final static native Process parallelTaskGetSender(ParallelTask parallelTask)
614 * The natively implementd method to get the source of a parallel task.
616 * @param parallelTask The parallel task to get the source.
618 * @return The source of the parallel task.
620 * @see ParallelTask.getSource()
622 final static native Host parallelTaskGetSource(ParallelTask parallelTask)
626 * The natively implemented method to get the name of the parallel task.
628 * @param parallelTask The parallel task to get the name.
630 * @return The name of the specified parallel task.
632 * @see ParallelTask.getName()
634 final static native String parallelTaskGetName(ParallelTask parallelTask)
638 * The natively implemented method to cancel a parallel task.
640 * @param parallelTask The parallel task to cancel.
642 * @see ParallelTask.cancel().
644 final static native void parallelTaskCancel(ParallelTask parallelTask)
645 throws JniException, NativeException;
648 * The natively implemented method to get the computing amount of the task.
650 * @param parallelTask The parallel task to get the computing amount.
652 * @return The computing amount of the specified parallel task.
654 * @see ParallelTask.getComputeDuration()
656 final static native double parallelTaskGetComputeDuration(ParallelTask
661 * The natively implemented method to get the remaining computation
663 * @param parallelTask The parallel task to get the remaining computation.
665 * @return The remaining computation of the specified parallel task.
667 * @see ParallelTask.getRemainingDuration()
669 final static native double parallelTaskGetRemainingDuration(ParallelTask
674 * The natively implemented method to set the priority of a parallel task.
676 * @param parallelTask The parallel task to set the prirotity
678 * @param priority The new priority of the specified parallel task.
680 * @see ParallelTask.setPriority()
682 final static native void parallelTaskSetPriority(ParallelTask parallelTask,
683 double priority) throws
687 * The natively implemented method to destroy a MSG parallel task.
689 * @param parallelTask The parallel task to destroy.
691 * @see ParallelTask.destroy()
693 final static native void parallelTaskDestroy(ParallelTask parallelTask)
694 throws JniException, NativeException;
697 * The natively implemented method to execute a MSG parallel task.
699 * @param parallelTask The parallel task to execute.
701 * @see ParallelTask.execute()
703 final static native void parallelTaskExecute(ParallelTask parallelTask)
704 throws JniException, NativeException;
706 /******************************************************************
707 * The natively implemented methods connected to the MSG channel *
708 ******************************************************************/
711 * The natively implemented method to listen on the channel and wait for receiving a task.
713 * @param channel The channel to listen
715 * @return The task readed from the channel.
717 * @exception MsgException if the listening operation failed.
721 final static native Task channelGet(Channel channel) throws JniException,
725 * The natively implemented method to listen on the channel and wait for receiving a task with a timeout.
727 * @param channel The channel to listen.
728 * @param timeout The timeout of the listening.
730 * @return The task readed from the channel.
732 * @exception MsgException if the listening operation failed.
734 * @see Channel.getWithTimeout()
737 final static native Task channelGetWithTimeout(Channel channel,
738 double timeout) throws
739 JniException, NativeException;
743 * The natively implemented method to listen on the channel of a specific host.
745 * @param channel The channel to listen.
747 * @param host The specific host.
749 * @return The task readed from the channel of the specific host.
751 * @exception InvalidHostException if the specified host is not valid.
752 * MsgException if the listening operation failed.
754 * @see Channel.getFromHost()
756 final static native Task channelGetFromHost(Channel channel,
757 Host host) throws JniException,
761 * The natively implemented method to test whether there is a pending communication on the channel.
763 * @param channel The channel concerned by the operation.
765 * @return The method returns true if there is a pending communication on the specified
766 * channel. Otherwise the method returns false.
768 * @see Channel.hasPendingCommunication()
770 final static native boolean channelHasPendingCommunication(Channel channel)
774 * The natively implemented method to test whether there is a pending communication on a
775 * channel, and who sent it.
777 * @param The channel concerned by the operation.
779 * @return The method returns -1 if there is no pending communication and
780 * the PID of the process who sent it otherwise.
782 * @see Channel.getCummunicatingProcess()
784 final static native int channelGetCommunicatingProcess(Channel channel)
788 * The natively implemented method to get the number of tasks waiting to be received on a
789 * channel and sent by a host.
791 * @param channel The channel concerned by the operation.
793 * @param host The sender of the tasks.
795 * @return The number of tasks waiting to be received on a channel
796 * and sent by the specified host.
798 * @exception InvalidHostException if the specified host is not valid.
800 * @see Channel.getHostWaiting()
802 final static native int channelGetHostWaitingTasks(Channel channel,
807 * The natively implemented method to put a task on the channel of an host.
809 * @param channel The channel where to put the task.
810 * @param task The task to put.
811 * @param host The host of the specified channel.
813 * @exception InvalidTaskException if the task is not valid.
814 * InvalidHostException if the host is not valid.
815 * MsgException if the operation failed.
819 final static native void channelPut(Channel channel, Task task,
820 Host host) throws JniException,
824 * The natively implemented method to put a task on a channel of an host (with a timeout
825 * on the waiting of the destination host) and waits for the end of the transmission.
827 * @param channel The channel where to put the task.
828 * @param task The task to put.
829 * @param host The host containing the channel.
830 * @param timeout The timeout of the transmission.
832 * @exception InvalidTaskException if the task is not valid.
833 * InvalidHostException if the host is not valid.
834 * MsgException if the operation failed.
836 * @see Channel.putWithTimeout()
838 final static native void channelPutWithTimeout(Channel channel, Task task,
840 double timeout) throws
841 JniException, NativeException;
844 * The natively implemented method to put a task on channel with a bounded transmition
847 * @param channel The channel where to put the task.
848 * @param task The task to put.
849 * @param host The host containing the channel.
850 * @param max_rate The bounded transmition rate.
852 * @exception InvalidTaskException if the task is not valid.
853 * InvalidHostException if the host is not valid.
854 * MsgException if the operation failed.
856 * @see Channel.putBounded()
858 final static native void channelPutBounded(Channel channel, Task task,
860 double max_rate) throws
861 JniException, NativeException;
864 * The natively implemented method to wait for at most timeout seconds for a task reception
865 * on channel. The PID is updated with the PID of the first process.
867 * @param channel The channel concerned by the operation.
868 * @param timeout The maximum time to wait for a task before
871 * @exception MsgException if the reception operation failed.
873 * @see Channel.wait()
875 final static native int channelWait(Channel channel,
876 double timeout) throws JniException,
880 * The natively implemented method to set the number of channel used by all the process
883 * @param channelNumber The channel number of the process.
885 * @see Channel.setNumber()
887 final static native void channelSetNumber(int channelNumber);
890 * The natively implemented method to get the number of channel of the process of the simulation.
892 * @return The number of channel per process.
894 * @see Channel.getNumber()
896 final static native int channelGetNumber();
898 /*********************************************************************************
899 * Additional native methods *
900 **********************************************************************************/
903 * The natively implemented method to get the simulation time.
905 * @param The simulation time.
907 public final static native double getClock();
909 public final static native void pajeOutput(String pajeFile);
912 public final static native void info(String s);
914 /*********************************************************************
915 * The natively implemented methods connected to the MSG simulation *
916 *********************************************************************/
919 * The natively implemented method to initialize a MSG simulation.
921 * @param args The arguments of the command line of the simulation.
925 public final static native void init(String[]args);
928 * Run the MSG simulation, and cleanup everything afterward.
930 * If you want to chain simulations in the same process, you
931 * should call again createEnvironment and deployApplication afterward.
933 * @see MSG_run, MSG_clean
935 public final static native void run() throws NativeException;
938 * The native implemented method to create the environment of the simulation.
940 * @param platformFile The XML file which contains the description of the environment of the simulation
943 public final static native void createEnvironment(String platformFile)
944 throws NativeException;
948 * The method to deploy the simulation.
950 * @param appFile The XML file which contains the description of the application to deploy.
954 public static void deployApplication(String platformFile) {
957 Class.forName("com.sun.org.apache.xerces.internal.parsers.SAXParser");
958 XMLReader reader = (XMLReader) c.newInstance();
960 reader.setEntityResolver(new DTDResolver());
961 ApplicationHandler handler = new ApplicationHandler();
963 reader.setContentHandler(handler);
964 reader.setFeature("http://xml.org/sax/features/validation", false);
965 reader.parse(platformFile);
967 } catch(Exception e) {
968 /* FIXME: do not swallow exception ! */
969 System.out.println("Exception in Msg.launchApplication()");
970 System.out.println(e);
976 static public void main(String[]args) throws MsgException {
977 /* initialize the MSG simulation. Must be done before anything else (even logging). */
980 if (args.length < 2) {
982 Msg.info("Usage: Msg platform_file deployment_file");
986 /* specify the number of channel for the process of the simulation. */
987 Channel.setNumber(1);
989 /* Load the platform and deploy the application */
990 Msg.createEnvironment(args[0]);
991 Msg.deployApplication(args[1]);
993 /* Execute the simulation */