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)
90 System.err.println(e.toString());
93 } catch(UnsatisfiedLinkError e){
94 System.err.println("Cannot load simgrid library : ");
96 System.err.println("Please check your LD_LIBRARY_PATH, "+
97 "or copy the library to the current directory");
103 void selectContextFactory(String name) throws NativeException;
105 /******************************************************************
106 * The natively implemented methods connected to the MSG Process *
107 ******************************************************************/
110 * The natively implemented method to create an MSG process.
112 * @param process The java process object to bind with the MSG native process.
113 * @param host A valid (binded) host where create the process.
115 * @exception simgrid.msg.JniException if JNI stuff goes wrong.
117 * @see Process constructors.
120 void processCreate(Process process, Host host) throws JniException;
123 * The natively implemented method to kill all the process of the simulation.
125 * @param resetPID Should we reset the PID numbers. A negative number means no reset
126 * and a positive number will be used to set the PID of the next newly
129 * @return The function returns the PID of the next created process.
131 final static native int processKillAll(int resetPID);
134 * The natively implemented method to suspend an MSG process.
136 * @param process The valid (binded with a native process) java process to suspend.
138 * @exception JniException if something goes wrong with JNI
139 * NativeException if the SimGrid native code failed.
141 * @see Process.pause()
143 final static native void processSuspend(Process process)
144 throws JniException, NativeException;
147 * The natively implemented method to kill a MSG process.
149 * @param process The valid (binded with a native process) java process to kill.
151 * @see Process.kill()
153 final static native void processKill(Process process)
157 * The natively implemented method to resume a suspended MSG process.
159 * @param process The valid (binded with a native process) java process to resume.
161 * @exception JniException if something goes wrong with JNI
162 * NativeException if the SimGrid native code failed.
164 * @see Process.restart()
166 final static native void processResume(Process process)
167 throws JniException, NativeException;
170 * The natively implemented method to test if MSG process is suspended.
172 * @param process The valid (binded with a native process) java process to test.
174 * @return If the process is suspended the method retuns true. Otherwise the
175 * method returns false.
177 * @exception JniException if something goes wrong with JNI
179 * @see Process.isSuspended()
181 final static native boolean processIsSuspended(Process process) throws JniException;
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 NativeException;
225 * The natively implemented method to get the PPID of a MSG process.
227 * @param process The valid (binded with a native process) java process to get the PID.
229 * @return The PPID of the specified process.
231 * @exception NativeException if the SimGrid native code failed.
233 * @see Process.getPPID()
235 final static native int processGetPPID(Process process) throws NativeException;
238 * The natively implemented method to get the current running process.
240 * @return The current process.
242 * @exception NativeException if the SimGrid native code failed.
244 * @see Process.currentProcess()
246 final static native Process processSelf() throws NativeException;
249 * The natively implemented method to get the current running process PID.
251 * @return The PID of the current process.
253 * @see Process.currentProcessPID()
255 final static native int processSelfPID();
258 * The natively implemented method to get the current running process PPID.
260 * @return The PPID of the current process.
262 * @see Process.currentProcessPPID()
264 final static native int processSelfPPID();
267 * The natively implemented method to migrate a process from his currnet host to a new host.
269 * @param process The (valid) process to migrate.
270 * @param host A (valid) host where move the process.
272 * @exception JniException if something goes wrong with JNI
273 * NativeException if the SimGrid native code failed.
275 * @see Process.migrate()
276 * @see Host.getByName()
278 final static native void processChangeHost(Process process,Host host)
279 throws JniException, NativeException;
282 * Process synchronization. The process wait the signal of the simulator to start.
284 * @exception JniException if something goes wrong with JNI
286 final static native void waitSignal(Process process) throws JniException;
289 * The natively implemented native to request the current process to sleep
290 * until time seconds have elapsed.
292 * @param seconds The time the current process must sleep.
294 * @exception NativeException if the SimGrid native code failed.
296 * @see Process.waitFor()
298 final static native void processWaitFor(double seconds) throws NativeException;
301 * The natively implemented native method to exit a process.
303 * @exception JniException if something goes wrong with JNI
305 * @see Process.exit()
307 final static native void processExit(Process process) throws JniException;
310 /******************************************************************
311 * The natively implemented methods connected to the MSG host *
312 ******************************************************************/
315 * The natively implemented method to get an host from his name.
317 * @param name The name of the host to get.
319 * @return The host having the specified name.
321 * @exception JniException if something goes wrong with JNI
322 * HostNotFoundException if there is no such host
323 * NativeException if the SimGrid native code failed.
325 * @see Host.getByName()
327 final static native Host hostGetByName(String name)
328 throws JniException, HostNotFoundException, NativeException;
331 * The natively implemented method to get the name of an MSG host.
333 * @param host The host (valid) to get the name.
335 * @return The name of the specified host.
337 * @exception JniException if something goes wrong with JNI
339 * @see Host.getName()
341 final static native String hostGetName(Host host) throws JniException;
344 * The natively implemented method to get the number of hosts of the simulation.
346 * @return The number of hosts of the simulation.
348 * @see Host.getNumber()
350 final static native int hostGetNumber();
353 * The natively implemented method to get the host of the current runing process.
355 * @return The host of the current running process.
357 * @exception JniException if something goes wrong with JNI
359 * @see Host.currentHost()
361 final static native Host hostSelf() throws JniException;
364 * The natively implemented method to get the speed of a MSG host.
366 * @param host The host to get the host.
368 * @return The speed of the specified host.
370 * @exception JniException if something goes wrong with JNI
372 * @see Host.getSpeed()
375 final static native double hostGetSpeed(Host host) throws JniException;
378 * The natively implemented native method to test if an host is avail.
380 * @param host The host to test.
382 * @return If the host is avail the method returns true.
383 * Otherwise the method returns false.
385 * @exception JniException if something goes wrong with JNI
387 * @see Host.isAvail()
389 final static native boolean hostIsAvail(Host host) throws JniException;
392 * The natively implemented native method to get all the hosts of the simulation.
394 * @exception JniException if something goes wrong with JNI
396 * @return A array which contains all the hosts of simulation.
399 final static native Host[] allHosts() throws JniException;
402 * The natively implemented native method to get the number of running tasks on a host.
404 * @param The host concerned by the operation.
406 * @return The number of running tasks.
408 * @exception JniException if something goes wrong with JNI
411 final static native int hostGetLoad(Host host) throws JniException;
413 /******************************************************************
414 * The natively implemented methods connected to the MSG task *
415 ******************************************************************/
418 * The natively implemented method to create a MSG task.
420 * @param name The name of th task.
421 * @param computeDuration A value of the processing amount (in flop) needed
422 * to process the task. If 0, then it cannot be executed
423 * with the execute() method. This value has to be >= 0.
424 * @param messageSize A value of amount of data (in bytes) needed to transfert
425 * this task. If 0, then it cannot be transfered this task.
426 * If 0, then it cannot be transfered with the get() and put()
427 * methods. This value has to be >= 0.
428 * @param task The java task object to bind with the native task to create.
430 * @exception JniException if something goes wrong with JNI
431 * NullPointerException if the specified name is null.
432 * IllegalArgumentException if compute duration <0 or message size <0
436 final static native void taskCreate(Task task,String name,double computeDuration, double messageSize)
437 throws JniException, NullPointerException, IllegalArgumentException;
440 * The natively implemented method to get the sender of a task.
442 * @param task The task (valid) to get the sender.
444 * @return The sender of the task.
446 * @exception InvalidTaskException is the specified task is not valid. A task
447 * is invalid if it is not binded with a native task.
449 * @see Task.getSender()
451 final static native Process taskGetSender(Task task) throws JniException;
454 * The natively implementd method to get the source of a task.
456 * @param task The task to get the source.
458 * @return The source of the task.
460 * @exception InvalidTaskException is the specified task is not valid. A task
461 * is invalid if it is not binded with a native task.
463 * @see Task.getSource()
465 final static native Host taskGetSource(Task task) throws JniException, NativeException;
468 * The natively implemented method to get the name of the task.
470 * @param task The task to get the name.
472 * @return The name of the specified task.
474 * @exception InvalidTaskException is the specified task is not valid. A task
475 * is invalid if it is not binded with a native task.
477 * @see Task.getName()
479 final static native String taskGetName(Task task) throws JniException;
482 * The natively implemented method to cancel a task.
484 * @param task The task to cancel.
486 * @exception InvalidTaskException if the specified task is not valid. A task
487 * is invalid if it is not binded with a native task.
488 * MsgException if the cancelation failed.
490 * @see Task.cancel().
492 final static native void taskCancel(Task task) throws JniException, NativeException;
495 * The natively implemented method to get the computing amount of the task.
497 * @param task The task to get the computing amount.
499 * @return The computing amount of the specified task.
501 * @exception InvalidTaskException if the specified task is not valid. A task
502 * is invalid if it is not binded with a native task.
503 * MsgException if the cancelation failed.
505 * @see Task.getComputeDuration()
507 final static native double taskGetComputeDuration(Task task) throws JniException;
510 * The natively implemented method to get the remaining computation
512 * @param task The task to get the remaining computation.
514 * @return The remaining computation of the specified task.
516 * @exception InvalidTaskException if the specified task is not valid. A task
517 * is invalid if it is not binded with a native task.
518 * MsgException if the cancelation failed.
520 * @see Task.getRemainingDuration()
522 final static native double taskGetRemainingDuration(Task task) throws JniException;
525 * The natively implemented method to set the priority of a task.
527 * @param task The task to set the prirotity
529 * @param priority The new priority of the specified task.
531 * @exception InvalidTaskException if the specified task is not valid. A task
532 * is invalid if it is not binded with a native task.
533 * MsgException if the cancelation failed.
535 * @see Task.setPriority()
537 final static native void taskSetPriority(Task task,double priority) throws JniException;
540 * The natively implemented method to destroy a MSG task.
542 * @param The task to destroy.
544 * @exception InvalidTaskException is the specified task is not valid. A task
545 * is invalid if it is not binded with a native task.
546 * MsgException if the destruction failed.
548 * @see Task.destroy()
550 final static native void taskDestroy(Task task) throws JniException, NativeException;
553 * The natively implemented method to execute a MSG task.
555 * @param task The task to execute.
557 * @exception InvalidTaskException is the specified task is not valid. A task
558 * is invalid if it is not binded with a native task.
559 * MsgException if the destruction failed.
561 * @see Task.execute()
563 final static native void taskExecute(Task task) throws JniException, NativeException;
567 /**************************************************************************
568 * The natively implemented methods connected to the MSG parallel task *
569 ***************************************************************************/
572 * The natively implemented method to create a MSG parallel task.
574 * @param name The name of the parallel task.
575 * @param hosts The list of hosts implied by the parallel task.
576 * @param computeDurations The total number of operations that have to be performed
578 * @param messageSizes An array of doubles
580 * @see ParallelTask.create()
582 final static native void parallelTaskCreate(ParallelTask parallelTask, String name,
583 Host[] hosts, double[] computeDurations, double[] messageSizes)
584 throws JniException, NullPointerException, IllegalArgumentException;
587 * The natively implemented method to get the sender of a parallel task.
589 * @param parallelTask The parallel task (valid) to get the sender.
591 * @return The sender of the parallel task.
593 * @see ParallelTask.getSender()
595 final static native Process parallelTaskGetSender(ParallelTask parallelTask) throws JniException;
598 * The natively implementd method to get the source of a parallel task.
600 * @param parallelTask The parallel task to get the source.
602 * @return The source of the parallel task.
604 * @see ParallelTask.getSource()
606 final static native Host parallelTaskGetSource(ParallelTask parallelTask) throws JniException;
609 * The natively implemented method to get the name of the parallel task.
611 * @param parallelTask The parallel task to get the name.
613 * @return The name of the specified parallel task.
615 * @see ParallelTask.getName()
617 final static native String parallelTaskGetName(ParallelTask parallelTask) throws JniException;
620 * The natively implemented method to cancel a parallel task.
622 * @param parallelTask The parallel task to cancel.
624 * @see ParallelTask.cancel().
626 final static native void parallelTaskCancel(ParallelTask parallelTask) throws JniException,NativeException;
629 * The natively implemented method to get the computing amount of the task.
631 * @param parallelTask The parallel task to get the computing amount.
633 * @return The computing amount of the specified parallel task.
635 * @see ParallelTask.getComputeDuration()
637 final static native double parallelTaskGetComputeDuration(ParallelTask parallelTask) throws JniException;
640 * The natively implemented method to get the remaining computation
642 * @param parallelTask The parallel task to get the remaining computation.
644 * @return The remaining computation of the specified parallel task.
646 * @see ParallelTask.getRemainingDuration()
648 final static native double parallelTaskGetRemainingDuration(ParallelTask parallelTask) throws JniException;
651 * The natively implemented method to set the priority of a parallel task.
653 * @param parallelTask The parallel task to set the prirotity
655 * @param priority The new priority of the specified parallel task.
657 * @see ParallelTask.setPriority()
659 final static native void parallelTaskSetPriority(ParallelTask parallelTask,double priority) throws JniException;
662 * The natively implemented method to destroy a MSG parallel task.
664 * @param parallelTask The parallel task to destroy.
666 * @see ParallelTask.destroy()
668 final static native void parallelTaskDestroy(ParallelTask parallelTask) throws JniException,NativeException;
671 * The natively implemented method to execute a MSG parallel task.
673 * @param parallelTask The parallel task to execute.
675 * @see ParallelTask.execute()
677 final static native void parallelTaskExecute(ParallelTask parallelTask) throws JniException, NativeException;
679 /******************************************************************
680 * The natively implemented methods connected to the MSG channel *
681 ******************************************************************/
684 * The natively implemented method to listen on the channel and wait for receiving a task.
686 * @param channel The channel to listen
688 * @return The task readed from the channel.
690 * @exception MsgException if the listening operation failed.
694 final static native Task channelGet(Channel channel) throws JniException,NativeException;
697 * The natively implemented method to listen on the channel and wait for receiving a task with a timeout.
699 * @param channel The channel to listen.
700 * @param timeout The timeout of the listening.
702 * @return The task readed from the channel.
704 * @exception MsgException if the listening operation failed.
706 * @see Channel.getWithTimeout()
709 final static native Task channelGetWithTimeout(Channel channel,double timeout) throws JniException,NativeException;
713 * The natively implemented method to listen on the channel of a specific host.
715 * @param channel The channel to listen.
717 * @param host The specific host.
719 * @return The task readed from the channel of the specific host.
721 * @exception InvalidHostException if the specified host is not valid.
722 * MsgException if the listening operation failed.
724 * @see Channel.getFromHost()
726 final static native Task channelGetFromHost(Channel channel,Host host) throws JniException,NativeException;
729 * The natively implemented method to test whether there is a pending communication on the channel.
731 * @param channel The channel concerned by the operation.
733 * @return The method returns true if there is a pending communication on the specified
734 * channel. Otherwise the method returns false.
736 * @see Channel.hasPendingCommunication()
738 final static native boolean channelHasPendingCommunication(Channel channel) throws JniException;
741 * The natively implemented method to test whether there is a pending communication on a
742 * channel, and who sent it.
744 * @param The channel concerned by the operation.
746 * @return The method returns -1 if there is no pending communication and
747 * the PID of the process who sent it otherwise.
749 * @see Channel.getCummunicatingProcess()
751 final static native int channelGetCommunicatingProcess(Channel channel) throws JniException;
754 * The natively implemented method to get the number of tasks waiting to be received on a
755 * channel and sent by a host.
757 * @param channel The channel concerned by the operation.
759 * @param host The sender of the tasks.
761 * @return The number of tasks waiting to be received on a channel
762 * and sent by the specified host.
764 * @exception InvalidHostException if the specified host is not valid.
766 * @see Channel.getHostWaiting()
768 final static native int channelGetHostWaitingTasks(Channel channel,Host host) throws JniException;
771 * The natively implemented method to put a task on the channel of an host.
773 * @param channel The channel where to put the task.
774 * @param task The task to put.
775 * @param host The host of the specified channel.
777 * @exception InvalidTaskException if the task is not valid.
778 * InvalidHostException if the host is not valid.
779 * MsgException if the operation failed.
783 final static native void channelPut(Channel channel,Task task,Host host) throws JniException,NativeException;
786 * The natively implemented method to put a task on a channel of an host (with a timeout
787 * on the waiting of the destination host) and waits for the end of the transmission.
789 * @param channel The channel where to put the task.
790 * @param task The task to put.
791 * @param host The host containing the channel.
792 * @param timeout The timeout of the transmission.
794 * @exception InvalidTaskException if the task is not valid.
795 * InvalidHostException if the host is not valid.
796 * MsgException if the operation failed.
798 * @see Channel.putWithTimeout()
800 final static native void channelPutWithTimeout(Channel channel,Task task,Host host,double timeout) throws JniException,NativeException;
803 * The natively implemented method to put a task on channel with a bounded transmition
806 * @param channel The channel where to put the task.
807 * @param task The task to put.
808 * @param host The host containing the channel.
809 * @param max_rate The bounded transmition rate.
811 * @exception InvalidTaskException if the task is not valid.
812 * InvalidHostException if the host is not valid.
813 * MsgException if the operation failed.
815 * @see Channel.putBounded()
817 final static native void channelPutBounded(Channel channel,Task task,Host host,double max_rate) throws JniException,NativeException;
820 * The natively implemented method to wait for at most timeout seconds for a task reception
821 * on channel. The PID is updated with the PID of the first process.
823 * @param channel The channel concerned by the operation.
824 * @param timeout The maximum time to wait for a task before
827 * @exception MsgException if the reception operation failed.
829 * @see Channel.wait()
831 final static native int channelWait(Channel channel, double timeout) throws JniException,NativeException;
834 * The natively implemented method to set the number of channel used by all the process
837 * @param channelNumber The channel number of the process.
839 * @see Channel.setNumber()
841 final static native void channelSetNumber(int channelNumber);
844 * The natively implemented method to get the number of channel of the process of the simulation.
846 * @return The number of channel per process.
848 * @see Channel.getNumber()
850 final static native int channelGetNumber();
852 /*********************************************************************************
853 * Additional native methods *
854 **********************************************************************************/
857 * The natively implemented method to get the simulation time.
859 * @param The simulation time.
861 public final static native double getClock();
863 public final static native void pajeOutput(String pajeFile);
866 public final static native void info(String s);
868 /*********************************************************************
869 * The natively implemented methods connected to the MSG simulation *
870 *********************************************************************/
873 * The natively implemented method to initialize a MSG simulation.
875 * @param args The arguments of the command line of the simulation.
879 public final static native void init(String[] args);
882 * Run the MSG simulation, and cleanup everything afterward.
884 * If you want to chain simulations in the same process, you
885 * should call again createEnvironment and deployApplication afterward.
887 * @see MSG_run, MSG_clean
889 public final static native void run() throws NativeException;
892 * The native implemented method to create the environment of the simulation.
894 * @param platformFile The XML file which contains the description of the environment of the simulation
897 public final static native void createEnvironment(String platformFile) throws NativeException;
901 * The method to deploy the simulation.
903 * @param appFile The XML file which contains the description of the application to deploy.
907 public static void deployApplication(String platformFile) {
909 Class c = Class.forName("com.sun.org.apache.xerces.internal.parsers.SAXParser");
910 XMLReader reader = (XMLReader)c.newInstance();
911 reader.setEntityResolver(new DTDResolver());
912 ApplicationHandler handler = new ApplicationHandler();
913 reader.setContentHandler(handler);
914 reader.setFeature("http://xml.org/sax/features/validation", false);
915 reader.parse(platformFile);
917 } catch(Exception e) {
918 /* FIXME: do not swallow exception ! */
919 System.out.println("Exception in Msg.launchApplication()");
920 System.out.println(e);
926 static public void main(String[]args) throws MsgException {
927 /* initialize the MSG simulation. Must be done before anything else (even logging). */
930 if(args.length < 2) {
932 Msg.info("Usage: Msg platform_file deployment_file");
936 /* specify the number of channel for the process of the simulation. */
937 Channel.setNumber(1);
939 /* Load the platform and deploy the application */
940 Msg.createEnvironment(args[0]);
941 Msg.deployApplication(args[1]);
943 /* Execute the simulation */