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("simgrid4java");
87 } catch(UnsatisfiedLinkError e){
88 System.err.println("Cannot load simgrid4java library : ");
90 System.err.println("Please check your LD_LIBRARY_PATH, "+
91 "or copy the library to the current directory");
96 /******************************************************************
97 * The natively implemented methods connected to the MSG Process *
98 ******************************************************************/
101 * The natively implemented method to create an MSG process.
103 * @param process The java process object to bind with the MSG native process.
104 * @param host A valid (binded) host where create the process.
106 * @exception simgrid.msg.JniException if JNI stuff goes wrong.
108 * @see Process constructors.
111 void processCreate(Process process, Host host) throws JniException;
114 * The natively implemented method to kill all the process of the simulation.
116 * @param resetPID Should we reset the PID numbers. A negative number means no reset
117 * and a positive number will be used to set the PID of the next newly
120 * @return The function returns the PID of the next created process.
122 final static native int processKillAll(int resetPID);
125 * The natively implemented method to suspend an MSG process.
127 * @param process The valid (binded with a native process) java process to suspend.
129 * @exception JniException if something goes wrong with JNI
130 * NativeException if the SimGrid native code failed.
132 * @see Process.pause()
134 final static native void processSuspend(Process process)
135 throws JniException, NativeException;
138 * The natively implemented method to kill a MSG process.
140 * @param process The valid (binded with a native process) java process to kill.
142 * @see Process.kill()
144 final static native void processKill(Process process)
148 * The natively implemented method to resume a suspended MSG process.
150 * @param process The valid (binded with a native process) java process to resume.
152 * @exception JniException if something goes wrong with JNI
153 * NativeException if the SimGrid native code failed.
155 * @see Process.restart()
157 final static native void processResume(Process process)
158 throws JniException, NativeException;
161 * The natively implemented method to test if MSG process is suspended.
163 * @param process The valid (binded with a native process) java process to test.
165 * @return If the process is suspended the method retuns true. Otherwise the
166 * method returns false.
168 * @exception JniException if something goes wrong with JNI
170 * @see Process.isSuspended()
172 final static native boolean processIsSuspended(Process process) throws JniException;
175 * The natively implemented method to get the host of a MSG process.
177 * @param process The valid (binded with a native process) java process to get the host.
179 * @return The method returns the host where the process is running.
181 * @exception JniException if something goes wrong with JNI
182 * NativeException if the SimGrid native code failed.
184 * @see Process.getHost()
186 final static native Host processGetHost(Process process)
187 throws JniException, NativeException;
190 * The natively implemented method to get a MSG process from his PID.
192 * @param PID The PID of the process to get.
194 * @return The process with the specified PID.
196 * @exception NativeException if the SimGrid native code failed.
198 * @see Process.getFromPID()
200 final static native Process processFromPID(int PID) throws NativeException;
203 * The natively implemented method to get the PID of a MSG process.
205 * @param process The valid (binded with a native process) java process to get the PID.
207 * @return The PID of the specified process.
209 * @exception NativeException if the SimGrid native code failed.
211 * @see Process.getPID()
213 final static native int processGetPID(Process process) throws NativeException;
216 * The natively implemented method to get the PPID of a MSG process.
218 * @param process The valid (binded with a native process) java process to get the PID.
220 * @return The PPID of the specified process.
222 * @exception NativeException if the SimGrid native code failed.
224 * @see Process.getPPID()
226 final static native int processGetPPID(Process process) throws NativeException;
229 * The natively implemented method to get the current running process.
231 * @return The current process.
233 * @exception NativeException if the SimGrid native code failed.
235 * @see Process.currentProcess()
237 final static native Process processSelf() throws NativeException;
240 * The natively implemented method to get the current running process PID.
242 * @return The PID of the current process.
244 * @see Process.currentProcessPID()
246 final static native int processSelfPID();
249 * The natively implemented method to get the current running process PPID.
251 * @return The PPID of the current process.
253 * @see Process.currentProcessPPID()
255 final static native int processSelfPPID();
258 * The natively implemented method to migrate a process from his currnet host to a new host.
260 * @param process The (valid) process to migrate.
261 * @param host A (valid) host where move the process.
263 * @exception JniException if something goes wrong with JNI
264 * NativeException if the SimGrid native code failed.
266 * @see Process.migrate()
267 * @see Host.getByName()
269 final static native void processChangeHost(Process process,Host host)
270 throws JniException, NativeException;
273 * Process synchronization. The process wait the signal of the simulator to start.
275 * @exception JniException if something goes wrong with JNI
277 final static native void waitSignal(Process process) throws JniException;
280 * The natively implemented native to request the current process to sleep
281 * until time seconds have elapsed.
283 * @param seconds The time the current process must sleep.
285 * @exception NativeException if the SimGrid native code failed.
287 * @see Process.waitFor()
289 final static native void processWaitFor(double seconds) throws NativeException;
292 * The natively implemented native method to exit a process.
294 * @exception JniException if something goes wrong with JNI
296 * @see Process.exit()
298 final static native void processExit(Process process) throws JniException;
301 /******************************************************************
302 * The natively implemented methods connected to the MSG host *
303 ******************************************************************/
306 * The natively implemented method to get an host from his name.
308 * @param name The name of the host to get.
310 * @return The host having the specified name.
312 * @exception JniException if something goes wrong with JNI
313 * HostNotFoundException if there is no such host
314 * NativeException if the SimGrid native code failed.
316 * @see Host.getByName()
318 final static native Host hostGetByName(String name)
319 throws JniException, HostNotFoundException, NativeException;
322 * The natively implemented method to get the name of an MSG host.
324 * @param host The host (valid) to get the name.
326 * @return The name of the specified host.
328 * @exception JniException if something goes wrong with JNI
330 * @see Host.getName()
332 final static native String hostGetName(Host host) throws JniException;
335 * The natively implemented method to get the number of hosts of the simulation.
337 * @return The number of hosts of the simulation.
339 * @see Host.getNumber()
341 final static native int hostGetNumber();
344 * The natively implemented method to get the host of the current runing process.
346 * @return The host of the current running process.
348 * @exception JniException if something goes wrong with JNI
350 * @see Host.currentHost()
352 final static native Host hostSelf() throws JniException;
355 * The natively implemented method to get the speed of a MSG host.
357 * @param host The host to get the host.
359 * @return The speed of the specified host.
361 * @exception JniException if something goes wrong with JNI
363 * @see Host.getSpeed()
366 final static native double hostGetSpeed(Host host) throws JniException;
369 * The natively implemented native method to test if an host is avail.
371 * @param host The host to test.
373 * @return If the host is avail the method returns true.
374 * Otherwise the method returns false.
376 * @exception JniException if something goes wrong with JNI
378 * @see Host.isAvail()
380 final static native boolean hostIsAvail(Host host) throws JniException;
383 * The natively implemented native method to get all the hosts of the simulation.
385 * @exception JniException if something goes wrong with JNI
387 * @return A array which contains all the hosts of simulation.
390 final static native Host[] allHosts() throws JniException;
393 * The natively implemented native method to get the number of running tasks on a host.
395 * @param The host concerned by the operation.
397 * @return The number of running tasks.
399 * @exception JniException if something goes wrong with JNI
402 final static native int hostGetLoad(Host host) throws JniException;
404 /******************************************************************
405 * The natively implemented methods connected to the MSG task *
406 ******************************************************************/
409 * The natively implemented method to create a MSG task.
411 * @param name The name of th task.
412 * @param computeDuration A value of the processing amount (in flop) needed
413 * to process the task. If 0, then it cannot be executed
414 * with the execute() method. This value has to be >= 0.
415 * @param messageSize A value of amount of data (in bytes) needed to transfert
416 * this task. If 0, then it cannot be transfered this task.
417 * If 0, then it cannot be transfered with the get() and put()
418 * methods. This value has to be >= 0.
419 * @param task The java task object to bind with the native task to create.
421 * @exception JniException if something goes wrong with JNI
422 * NullPointerException if the specified name is null.
423 * IllegalArgumentException if compute duration <0 or message size <0
427 final static native void taskCreate(Task task,String name,double computeDuration, double messageSize)
428 throws JniException, NullPointerException, IllegalArgumentException;
431 * The natively implemented method to get the sender of a task.
433 * @param task The task (valid) to get the sender.
435 * @return The sender of the task.
437 * @exception InvalidTaskException is the specified task is not valid. A task
438 * is invalid if it is not binded with a native task.
440 * @see Task.getSender()
442 final static native Process taskGetSender(Task task) throws JniException;
445 * The natively implementd method to get the source of a task.
447 * @param task The task to get the source.
449 * @return The source 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.getSource()
456 final static native Host taskGetSource(Task task) throws JniException, NativeException;
459 * The natively implemented method to get the name of the task.
461 * @param task The task to get the name.
463 * @return The name of the specified 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.getName()
470 final static native String taskGetName(Task task) throws JniException;
473 * The natively implemented method to cancel a task.
475 * @param task The task to cancel.
477 * @exception InvalidTaskException if the specified task is not valid. A task
478 * is invalid if it is not binded with a native task.
479 * MsgException if the cancelation failed.
481 * @see Task.cancel().
483 final static native void taskCancel(Task task) throws JniException, NativeException;
486 * The natively implemented method to get the computing amount of the task.
488 * @param task The task to get the computing amount.
490 * @return The computing amount of the specified task.
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.getComputeDuration()
498 final static native double taskGetComputeDuration(Task task) throws JniException;
501 * The natively implemented method to get the remaining computation
503 * @param task The task to get the remaining computation.
505 * @return The remaining computation of the specified task.
507 * @exception InvalidTaskException if the specified task is not valid. A task
508 * is invalid if it is not binded with a native task.
509 * MsgException if the cancelation failed.
511 * @see Task.getRemainingDuration()
513 final static native double taskGetRemainingDuration(Task task) throws JniException;
516 * The natively implemented method to set the priority of a task.
518 * @param task The task to set the prirotity
520 * @param priority The new priority of the specified task.
522 * @exception InvalidTaskException if the specified task is not valid. A task
523 * is invalid if it is not binded with a native task.
524 * MsgException if the cancelation failed.
526 * @see Task.setPriority()
528 final static native void taskSetPriority(Task task,double priority) throws JniException;
531 * The natively implemented method to destroy a MSG task.
533 * @param The task to destroy.
535 * @exception InvalidTaskException is the specified task is not valid. A task
536 * is invalid if it is not binded with a native task.
537 * MsgException if the destruction failed.
539 * @see Task.destroy()
541 final static native void taskDestroy(Task task) throws JniException, NativeException;
544 * The natively implemented method to execute a MSG task.
546 * @param task The task to execute.
548 * @exception InvalidTaskException is the specified task is not valid. A task
549 * is invalid if it is not binded with a native task.
550 * MsgException if the destruction failed.
552 * @see Task.execute()
554 final static native void taskExecute(Task task) throws JniException, NativeException;
558 /**************************************************************************
559 * The natively implemented methods connected to the MSG parallel task *
560 ***************************************************************************/
563 * The natively implemented method to create a MSG parallel task.
565 * @param name The name of the parallel task.
566 * @param hosts The list of hosts implied by the parallel task.
567 * @param computeDurations The total number of operations that have to be performed
569 * @param messageSizes An array of doubles
571 * @see ParallelTask.create()
573 final static native void parallelTaskCreate(ParallelTask parallelTask, String name,
574 Host[] hosts, double[] computeDurations, double[] messageSizes)
575 throws JniException, NullPointerException, IllegalArgumentException;
578 * The natively implemented method to get the sender of a parallel task.
580 * @param parallelTask The parallel task (valid) to get the sender.
582 * @return The sender of the parallel task.
584 * @see ParallelTask.getSender()
586 final static native Process parallelTaskGetSender(ParallelTask parallelTask) throws JniException;
589 * The natively implementd method to get the source of a parallel task.
591 * @param parallelTask The parallel task to get the source.
593 * @return The source of the parallel task.
595 * @see ParallelTask.getSource()
597 final static native Host parallelTaskGetSource(ParallelTask parallelTask) throws JniException;
600 * The natively implemented method to get the name of the parallel task.
602 * @param parallelTask The parallel task to get the name.
604 * @return The name of the specified parallel task.
606 * @see ParallelTask.getName()
608 final static native String parallelTaskGetName(ParallelTask parallelTask) throws JniException;
611 * The natively implemented method to cancel a parallel task.
613 * @param parallelTask The parallel task to cancel.
615 * @see ParallelTask.cancel().
617 final static native void parallelTaskCancel(ParallelTask parallelTask) throws JniException,NativeException;
620 * The natively implemented method to get the computing amount of the task.
622 * @param parallelTask The parallel task to get the computing amount.
624 * @return The computing amount of the specified parallel task.
626 * @see ParallelTask.getComputeDuration()
628 final static native double parallelTaskGetComputeDuration(ParallelTask parallelTask) throws JniException;
631 * The natively implemented method to get the remaining computation
633 * @param parallelTask The parallel task to get the remaining computation.
635 * @return The remaining computation of the specified parallel task.
637 * @see ParallelTask.getRemainingDuration()
639 final static native double parallelTaskGetRemainingDuration(ParallelTask parallelTask) throws JniException;
642 * The natively implemented method to set the priority of a parallel task.
644 * @param parallelTask The parallel task to set the prirotity
646 * @param priority The new priority of the specified parallel task.
648 * @see ParallelTask.setPriority()
650 final static native void parallelTaskSetPriority(ParallelTask parallelTask,double priority) throws JniException;
653 * The natively implemented method to destroy a MSG parallel task.
655 * @param parallelTask The parallel task to destroy.
657 * @see ParallelTask.destroy()
659 final static native void parallelTaskDestroy(ParallelTask parallelTask) throws JniException,NativeException;
662 * The natively implemented method to execute a MSG parallel task.
664 * @param parallelTask The parallel task to execute.
666 * @see ParallelTask.execute()
668 final static native void parallelTaskExecute(ParallelTask parallelTask) throws JniException, NativeException;
670 /******************************************************************
671 * The natively implemented methods connected to the MSG channel *
672 ******************************************************************/
675 * The natively implemented method to listen on the channel and wait for receiving a task.
677 * @param channel The channel to listen
679 * @return The task readed from the channel.
681 * @exception MsgException if the listening operation failed.
685 final static native Task channelGet(Channel channel) throws JniException,NativeException;
688 * The natively implemented method to listen on the channel and wait for receiving a task with a timeout.
690 * @param channel The channel to listen.
691 * @param timeout The timeout of the listening.
693 * @return The task readed from the channel.
695 * @exception MsgException if the listening operation failed.
697 * @see Channel.getWithTimeout()
700 final static native Task channelGetWithTimeout(Channel channel,double timeout) throws JniException,NativeException;
704 * The natively implemented method to listen on the channel of a specific host.
706 * @param channel The channel to listen.
708 * @param host The specific host.
710 * @return The task readed from the channel of the specific host.
712 * @exception InvalidHostException if the specified host is not valid.
713 * MsgException if the listening operation failed.
715 * @see Channel.getFromHost()
717 final static native Task channelGetFromHost(Channel channel,Host host) throws JniException,NativeException;
720 * The natively implemented method to test whether there is a pending communication on the channel.
722 * @param channel The channel concerned by the operation.
724 * @return The method returns true if there is a pending communication on the specified
725 * channel. Otherwise the method returns false.
727 * @see Channel.hasPendingCommunication()
729 final static native boolean channelHasPendingCommunication(Channel channel) throws JniException;
732 * The natively implemented method to test whether there is a pending communication on a
733 * channel, and who sent it.
735 * @param The channel concerned by the operation.
737 * @return The method returns -1 if there is no pending communication and
738 * the PID of the process who sent it otherwise.
740 * @see Channel.getCummunicatingProcess()
742 final static native int channelGetCommunicatingProcess(Channel channel) throws JniException;
745 * The natively implemented method to get the number of tasks waiting to be received on a
746 * channel and sent by a host.
748 * @param channel The channel concerned by the operation.
750 * @param host The sender of the tasks.
752 * @return The number of tasks waiting to be received on a channel
753 * and sent by the specified host.
755 * @exception InvalidHostException if the specified host is not valid.
757 * @see Channel.getHostWaiting()
759 final static native int channelGetHostWaitingTasks(Channel channel,Host host) throws JniException;
762 * The natively implemented method to put a task on the channel of an host.
764 * @param channel The channel where to put the task.
765 * @param task The task to put.
766 * @param host The host of the specified channel.
768 * @exception InvalidTaskException if the task is not valid.
769 * InvalidHostException if the host is not valid.
770 * MsgException if the operation failed.
774 final static native void channelPut(Channel channel,Task task,Host host) throws JniException,NativeException;
777 * The natively implemented method to put a task on a channel of an host (with a timeout
778 * on the waiting of the destination host) and waits for the end of the transmission.
780 * @param channel The channel where to put the task.
781 * @param task The task to put.
782 * @param host The host containing the channel.
783 * @param timeout The timeout of the transmission.
785 * @exception InvalidTaskException if the task is not valid.
786 * InvalidHostException if the host is not valid.
787 * MsgException if the operation failed.
789 * @see Channel.putWithTimeout()
791 final static native void channelPutWithTimeout(Channel channel,Task task,Host host,double timeout) throws JniException,NativeException;
794 * The natively implemented method to put a task on channel with a bounded transmition
797 * @param channel The channel where to put the task.
798 * @param task The task to put.
799 * @param host The host containing the channel.
800 * @param max_rate The bounded transmition rate.
802 * @exception InvalidTaskException if the task is not valid.
803 * InvalidHostException if the host is not valid.
804 * MsgException if the operation failed.
806 * @see Channel.putBounded()
808 final static native void channelPutBounded(Channel channel,Task task,Host host,double max_rate) throws JniException,NativeException;
811 * The natively implemented method to wait for at most timeout seconds for a task reception
812 * on channel. The PID is updated with the PID of the first process.
814 * @param channel The channel concerned by the operation.
815 * @param timeout The maximum time to wait for a task before
818 * @exception MsgException if the reception operation failed.
820 * @see Channel.wait()
822 final static native int channelWait(Channel channel, double timeout) throws JniException,NativeException;
825 * The natively implemented method to set the number of channel used by all the process
828 * @param channelNumber The channel number of the process.
830 * @see Channel.setNumber()
832 final static native void channelSetNumber(int channelNumber);
835 * The natively implemented method to get the number of channel of the process of the simulation.
837 * @return The number of channel per process.
839 * @see Channel.getNumber()
841 final static native int channelGetNumber();
843 /*********************************************************************************
844 * Additional native methods *
845 **********************************************************************************/
848 * The natively implemented method to get the simulation time.
850 * @param The simulation time.
852 public final static native double getClock();
854 public final static native void pajeOutput(String pajeFile);
857 public final static native void info(String s);
859 /*********************************************************************
860 * The natively implemented methods connected to the MSG simulation *
861 *********************************************************************/
864 * The natively implemented method to initialize a MSG simulation.
866 * @param args The arguments of the command line of the simulation.
870 public final static native void init(String[] args);
873 * Run the MSG simulation, and cleanup everything afterward.
875 * If you want to chain simulations in the same process, you
876 * should call again createEnvironment and deployApplication afterward.
878 * @see MSG_run, MSG_clean
880 public final static native void run() throws NativeException;
883 * The native implemented method to create the environment of the simulation.
885 * @param platformFile The XML file which contains the description of the environment of the simulation
888 public final static native void createEnvironment(String platformFile) throws NativeException;
892 * The method to deploy the simulation.
894 * @param appFile The XML file which contains the description of the application to deploy.
898 public static void deployApplication(String platformFile) {
900 Class c = Class.forName("com.sun.org.apache.xerces.internal.parsers.SAXParser");
901 XMLReader reader = (XMLReader)c.newInstance();
902 reader.setEntityResolver(new DTDResolver());
903 ApplicationHandler handler = new ApplicationHandler();
904 reader.setContentHandler(handler);
905 reader.setFeature("http://xml.org/sax/features/validation", false);
906 reader.parse(platformFile);
908 } catch(Exception e) {
909 /* FIXME: do not swallow exception ! */
910 System.out.println("Exception in Msg.launchApplication()");
911 System.out.println(e);
917 static public void main(String[]args) throws MsgException {
918 /* initialize the MSG simulation. Must be done before anything else (even logging). */
921 if(args.length < 2) {
923 Msg.info("Usage: Msg platform_file deployment_file");
927 /* specify the number of channel for the process of the simulation. */
928 Channel.setNumber(1);
930 /* Load the platform and deploy the application */
931 Msg.createEnvironment(args[0]);
932 Msg.deployApplication(args[1]);
934 /* Execute the simulation */