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.
17 * MSG was the first distributed programming environment
18 * provided within SimGrid. While almost realistic, it
19 * remains quite simple.
20 * This class contains all the declaration of the natives methods
22 * All of these methods are statics. You can't directly use these methods
23 * but it is recommanded to use the classes Process, Host, Task and ParallelTask
24 * of the package to build your application.
26 * @author Abdelmalek Cherier
27 * @author Martin Quinson
28 * @version 1.00, 07/05/01
36 public final class Msg {
38 * The natively implemented method to get the last error code of the simulation.
40 * @return The last error code of the simulation.
42 final static native int getErrCode();
45 * Errors returned by the method Msg.getErrCode().
48 /* Everything is right. Keep on goin the way !
51 public static final int SUCCESS = 0;
53 /* Something must be not perfectly clean. But I
54 * may be paranoid freak... !
56 public static final int WARNING = 1;
58 /* There has been a problem during you task treansfer.
59 * Either the network is down or the remote host has
62 public static final int TRANSFERT_FAILURE = 2;
65 * System shutdown. The host on which you are running
66 * has just been rebooted. Free your datastructures and
69 public static final int HOST_FAILURE = 3;
72 * Cancelled task. This task has been cancelled by somebody !
74 public static final int TASK_CANCELLLED = 4;
77 * You've done something wrong. You'd better look at it...
79 public static final int FATAL_ERROR = 5;
82 * Staticaly load the SIMGRID4JAVA library
83 * which contains all native functions used in here
87 System.loadLibrary("simgrid4java");
88 } catch(UnsatisfiedLinkError e){
89 System.err.println("Cannot load simgrid4java library : ");
91 System.err.println("Please check your LD_LIBRARY_PATH, "+
92 "or copy the library to the current directory");
97 /******************************************************************
98 * The natively implemented methods connected to the MSG Process *
99 ******************************************************************/
102 * The natively implemented method to create an MSG process.
104 * @param process The java process object to bind with the MSG native process.
105 * @param host A valid (binded) host where create the process.
107 * @exception simgrid.msg.JniException if JNI stuff goes wrong.
109 * @see Process constructors.
112 void processCreate(Process process, Host host) throws JniException;
115 * The natively implemented method to kill all the process of the simulation.
117 * @param resetPID Should we reset the PID numbers. A negative number means no reset
118 * and a positive number will be used to set the PID of the next newly
121 * @return The function returns the PID of the next created process.
123 final static native int processKillAll(int resetPID);
126 * The natively implemented method to suspend an MSG process.
128 * @param process The valid (binded with a native process) java process to suspend.
130 * @exception JniException if something goes wrong with JNI
131 * NativeException if the SimGrid native code failed.
133 * @see Process.pause()
135 final static native void processSuspend(Process process)
136 throws JniException, NativeException;
139 * The natively implemented method to kill a MSG process.
141 * @param process The valid (binded with a native process) java process to kill.
143 * @see Process.kill()
145 final static native void processKill(Process process)
149 * The natively implemented method to resume a suspended MSG process.
151 * @param process The valid (binded with a native process) java process to resume.
153 * @exception JniException if something goes wrong with JNI
154 * NativeException if the SimGrid native code failed.
156 * @see Process.restart()
158 final static native void processResume(Process process)
159 throws JniException, NativeException;
162 * The natively implemented method to test if MSG process is suspended.
164 * @param process The valid (binded with a native process) java process to test.
166 * @return If the process is suspended the method retuns true. Otherwise the
167 * method returns false.
169 * @exception JniException if something goes wrong with JNI
171 * @see Process.isSuspended()
173 final static native boolean processIsSuspended(Process process) throws JniException;
176 * The natively implemented method to get the host of a MSG process.
178 * @param process The valid (binded with a native process) java process to get the host.
180 * @return The method returns the host where the process is running.
182 * @exception JniException if something goes wrong with JNI
183 * NativeException if the SimGrid native code failed.
185 * @see Process.getHost()
187 final static native Host processGetHost(Process process)
188 throws JniException, NativeException;
191 * The natively implemented method to get a MSG process from his PID.
193 * @param PID The PID of the process to get.
195 * @return The process with the specified PID.
197 * @exception NativeException if the SimGrid native code failed.
199 * @see Process.getFromPID()
201 final static native Process processFromPID(int PID) throws NativeException;
204 * The natively implemented method to get the PID of a MSG process.
206 * @param process The valid (binded with a native process) java process to get the PID.
208 * @return The PID of the specified process.
210 * @exception NativeException if the SimGrid native code failed.
212 * @see Process.getPID()
214 final static native int processGetPID(Process process) throws NativeException;
217 * The natively implemented method to get the PPID of a MSG process.
219 * @param process The valid (binded with a native process) java process to get the PID.
221 * @return The PPID of the specified process.
223 * @exception NativeException if the SimGrid native code failed.
225 * @see Process.getPPID()
227 final static native int processGetPPID(Process process) throws NativeException;
230 * The natively implemented method to get the current running process.
232 * @return The current process.
234 * @exception NativeException if the SimGrid native code failed.
236 * @see Process.currentProcess()
238 final static native Process processSelf() throws NativeException;
241 * The natively implemented method to get the current running process PID.
243 * @return The PID of the current process.
245 * @see Process.currentProcessPID()
247 final static native int processSelfPID();
250 * The natively implemented method to get the current running process PPID.
252 * @return The PPID of the current process.
254 * @see Process.currentProcessPPID()
256 final static native int processSelfPPID();
259 * The natively implemented method to migrate a process from his currnet host to a new host.
261 * @param process The (valid) process to migrate.
262 * @param host A (valid) host where move the process.
264 * @exception JniException if something goes wrong with JNI
265 * NativeException if the SimGrid native code failed.
267 * @see Process.migrate()
268 * @see Host.getByName()
270 final static native void processChangeHost(Process process,Host host)
271 throws JniException, NativeException;
274 * Process synchronization. The process wait the signal of the simulator to start.
276 * @exception JniException if something goes wrong with JNI
278 final static native void waitSignal(Process process) throws JniException;
281 * The natively implemented native to request the current process to sleep
282 * until time seconds have elapsed.
284 * @param seconds The time the current process must sleep.
286 * @exception NativeException if the SimGrid native code failed.
288 * @see Process.waitFor()
290 final static native void processWaitFor(double seconds) throws NativeException;
293 * The natively implemented native method to exit a process.
295 * @exception JniException if something goes wrong with JNI
297 * @see Process.exit()
299 final static native void processExit(Process process) throws JniException;
302 /******************************************************************
303 * The natively implemented methods connected to the MSG host *
304 ******************************************************************/
307 * The natively implemented method to get an host from his name.
309 * @param name The name of the host to get.
311 * @return The host having the specified name.
313 * @exception JniException if something goes wrong with JNI
314 * HostNotFoundException if there is no such host
315 * NativeException if the SimGrid native code failed.
317 * @see Host.getByName()
319 final static native Host hostGetByName(String name)
320 throws JniException, HostNotFoundException, NativeException;
323 * The natively implemented method to get the name of an MSG host.
325 * @param host The host (valid) to get the name.
327 * @return The name of the specified host.
329 * @exception JniException if something goes wrong with JNI
331 * @see Host.getName()
333 final static native String hostGetName(Host host) throws JniException;
336 * The natively implemented method to get the number of hosts of the simulation.
338 * @return The number of hosts of the simulation.
340 * @see Host.getNumber()
342 final static native int hostGetNumber();
345 * The natively implemented method to get the host of the current runing process.
347 * @return The host of the current running process.
349 * @exception JniException if something goes wrong with JNI
351 * @see Host.currentHost()
353 final static native Host hostSelf() throws JniException;
356 * The natively implemented method to get the speed of a MSG host.
358 * @param host The host to get the host.
360 * @return The speed of the specified host.
362 * @exception JniException if something goes wrong with JNI
364 * @see Host.getSpeed()
367 final static native double hostGetSpeed(Host host) throws JniException;
370 * The natively implemented native method to test if an host is avail.
372 * @param host The host to test.
374 * @return If the host is avail the method returns true.
375 * Otherwise the method returns false.
377 * @exception JniException if something goes wrong with JNI
379 * @see Host.isAvail()
381 final static native boolean hostIsAvail(Host host) throws JniException;
384 * The natively implemented native method to get all the hosts of the simulation.
386 * @exception JniException if something goes wrong with JNI
388 * @return A array which contains all the hosts of simulation.
391 final static native Host[] allHosts() throws JniException;
394 * The natively implemented native method to get the number of running tasks on a host.
396 * @param The host concerned by the operation.
398 * @return The number of running tasks.
400 * @exception JniException if something goes wrong with JNI
403 final static native int hostGetLoad(Host host) throws JniException;
405 /******************************************************************
406 * The natively implemented methods connected to the MSG task *
407 ******************************************************************/
410 * The natively implemented method to create a MSG task.
412 * @param name The name of th task.
413 * @param computeDuration A value of the processing amount (in flop) needed
414 * to process the task. If 0, then it cannot be executed
415 * with the execute() method. This value has to be >= 0.
416 * @param messageSize A value of amount of data (in bytes) needed to transfert
417 * this task. If 0, then it cannot be transfered this task.
418 * If 0, then it cannot be transfered with the get() and put()
419 * methods. This value has to be >= 0.
420 * @param task The java task object to bind with the native task to create.
422 * @exception JniException if something goes wrong with JNI
423 * NullPointerException if the specified name is null.
424 * IllegalArgumentException if compute duration <0 or message size <0
428 final static native void taskCreate(Task task,String name,double computeDuration, double messageSize)
429 throws JniException, NullPointerException, IllegalArgumentException;
432 * The natively implemented method to get the sender of a task.
434 * @param task The task (valid) to get the sender.
436 * @return The sender of the task.
438 * @exception InvalidTaskException is the specified task is not valid. A task
439 * is invalid if it is not binded with a native task.
441 * @see Task.getSender()
443 final static native Process taskGetSender(Task task) throws JniException;
446 * The natively implementd method to get the source of a task.
448 * @param task The task to get the source.
450 * @return The source of the task.
452 * @exception InvalidTaskException is the specified task is not valid. A task
453 * is invalid if it is not binded with a native task.
455 * @see Task.getSource()
457 final static native Host taskGetSource(Task task) throws JniException, NativeException;
460 * The natively implemented method to get the name of the task.
462 * @param task The task to get the name.
464 * @return The name of the specified task.
466 * @exception InvalidTaskException is the specified task is not valid. A task
467 * is invalid if it is not binded with a native task.
469 * @see Task.getName()
471 final static native String taskGetName(Task task) throws JniException;
474 * The natively implemented method to cancel a task.
476 * @param task The task to cancel.
478 * @exception InvalidTaskException if the specified task is not valid. A task
479 * is invalid if it is not binded with a native task.
480 * MsgException if the cancelation failed.
482 * @see Task.cancel().
484 final static native void taskCancel(Task task) throws JniException, NativeException;
487 * The natively implemented method to get the computing amount of the task.
489 * @param task The task to get the computing amount.
491 * @return The computing amount of the specified task.
493 * @exception InvalidTaskException if the specified task is not valid. A task
494 * is invalid if it is not binded with a native task.
495 * MsgException if the cancelation failed.
497 * @see Task.getComputeDuration()
499 final static native double taskGetComputeDuration(Task task) throws JniException;
502 * The natively implemented method to get the remaining computation
504 * @param task The task to get the remaining computation.
506 * @return The remaining computation 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.getRemainingDuration()
514 final static native double taskGetRemainingDuration(Task task) throws JniException;
517 * The natively implemented method to set the priority of a task.
519 * @param task The task to set the prirotity
521 * @param priority The new priority of the specified task.
523 * @exception InvalidTaskException if the specified task is not valid. A task
524 * is invalid if it is not binded with a native task.
525 * MsgException if the cancelation failed.
527 * @see Task.setPriority()
529 final static native void taskSetPriority(Task task,double priority) throws JniException;
532 * The natively implemented method to destroy a MSG task.
534 * @param The task to destroy.
536 * @exception InvalidTaskException is the specified task is not valid. A task
537 * is invalid if it is not binded with a native task.
538 * MsgException if the destruction failed.
540 * @see Task.destroy()
542 final static native void taskDestroy(Task task) throws JniException, NativeException;
545 * The natively implemented method to execute a MSG task.
547 * @param task The task to execute.
549 * @exception InvalidTaskException is the specified task is not valid. A task
550 * is invalid if it is not binded with a native task.
551 * MsgException if the destruction failed.
553 * @see Task.execute()
555 final static native void taskExecute(Task task) throws JniException, NativeException;
559 /**************************************************************************
560 * The natively implemented methods connected to the MSG parallel task *
561 ***************************************************************************/
564 * The natively implemented method to create a MSG parallel task.
566 * @param name The name of the parallel task.
567 * @param hosts The list of hosts implied by the parallel task.
568 * @param computeDurations The total number of operations that have to be performed
570 * @param messageSizes An array of doubles
572 * @see ParallelTask.create()
574 final static native void parallelTaskCreate(ParallelTask parallelTask, String name,
575 Host[] hosts, double[] computeDurations, double[] messageSizes)
576 throws JniException, NullPointerException, IllegalArgumentException;
579 * The natively implemented method to get the sender of a parallel task.
581 * @param parallelTask The parallel task (valid) to get the sender.
583 * @return The sender of the parallel task.
585 * @see ParallelTask.getSender()
587 final static native Process parallelTaskGetSender(ParallelTask parallelTask) throws JniException;
590 * The natively implementd method to get the source of a parallel task.
592 * @param parallelTask The parallel task to get the source.
594 * @return The source of the parallel task.
596 * @see ParallelTask.getSource()
598 final static native Host parallelTaskGetSource(ParallelTask parallelTask) throws JniException;
601 * The natively implemented method to get the name of the parallel task.
603 * @param parallelTask The parallel task to get the name.
605 * @return The name of the specified parallel task.
607 * @see ParallelTask.getName()
609 final static native String parallelTaskGetName(ParallelTask parallelTask) throws JniException;
612 * The natively implemented method to cancel a parallel task.
614 * @param parallelTask The parallel task to cancel.
616 * @see ParallelTask.cancel().
618 final static native void parallelTaskCancel(ParallelTask parallelTask) throws JniException,NativeException;
621 * The natively implemented method to get the computing amount of the task.
623 * @param parallelTask The parallel task to get the computing amount.
625 * @return The computing amount of the specified parallel task.
627 * @see ParallelTask.getComputeDuration()
629 final static native double parallelTaskGetComputeDuration(ParallelTask parallelTask) throws JniException;
632 * The natively implemented method to get the remaining computation
634 * @param parallelTask The parallel task to get the remaining computation.
636 * @return The remaining computation of the specified parallel task.
638 * @see ParallelTask.getRemainingDuration()
640 final static native double parallelTaskGetRemainingDuration(ParallelTask parallelTask) throws JniException;
643 * The natively implemented method to set the priority of a parallel task.
645 * @param parallelTask The parallel task to set the prirotity
647 * @param priority The new priority of the specified parallel task.
649 * @see ParallelTask.setPriority()
651 final static native void parallelTaskSetPriority(ParallelTask parallelTask,double priority) throws JniException;
654 * The natively implemented method to destroy a MSG parallel task.
656 * @param parallelTask The parallel task to destroy.
658 * @see ParallelTask.destroy()
660 final static native void parallelTaskDestroy(ParallelTask parallelTask) throws JniException,NativeException;
663 * The natively implemented method to execute a MSG parallel task.
665 * @param parallelTask The parallel task to execute.
667 * @see ParallelTask.execute()
669 final static native void parallelTaskExecute(ParallelTask parallelTask) throws JniException, NativeException;
671 /******************************************************************
672 * The natively implemented methods connected to the MSG channel *
673 ******************************************************************/
676 * The natively implemented method to listen on the channel and wait for receiving a task.
678 * @param channel The channel to listen
680 * @return The task readed from the channel.
682 * @exception MsgException if the listening operation failed.
686 final static native Task channelGet(Channel channel) throws JniException,NativeException;
689 * The natively implemented method to listen on the channel and wait for receiving a task with a timeout.
691 * @param channel The channel to listen.
692 * @param timeout The timeout of the listening.
694 * @return The task readed from the channel.
696 * @exception MsgException if the listening operation failed.
698 * @see Channel.getWithTimeout()
701 final static native Task channelGetWithTimeout(Channel channel,double timeout) throws JniException,NativeException;
705 * The natively implemented method to listen on the channel of a specific host.
707 * @param channel The channel to listen.
709 * @param host The specific host.
711 * @return The task readed from the channel of the specific host.
713 * @exception InvalidHostException if the specified host is not valid.
714 * MsgException if the listening operation failed.
716 * @see Channel.getFromHost()
718 final static native Task channelGetFromHost(Channel channel,Host host) throws JniException,NativeException;
721 * The natively implemented method to test whether there is a pending communication on the channel.
723 * @param channel The channel concerned by the operation.
725 * @return The method returns true if there is a pending communication on the specified
726 * channel. Otherwise the method returns false.
728 * @see Channel.hasPendingCommunication()
730 final static native boolean channelHasPendingCommunication(Channel channel) throws JniException;
733 * The natively implemented method to test whether there is a pending communication on a
734 * channel, and who sent it.
736 * @param The channel concerned by the operation.
738 * @return The method returns -1 if there is no pending communication and
739 * the PID of the process who sent it otherwise.
741 * @see Channel.getCummunicatingProcess()
743 final static native int channelGetCommunicatingProcess(Channel channel) throws JniException;
746 * The natively implemented method to get the number of tasks waiting to be received on a
747 * channel and sent by a host.
749 * @param channel The channel concerned by the operation.
751 * @param host The sender of the tasks.
753 * @return The number of tasks waiting to be received on a channel
754 * and sent by the specified host.
756 * @exception InvalidHostException if the specified host is not valid.
758 * @see Channel.getHostWaiting()
760 final static native int channelGetHostWaitingTasks(Channel channel,Host host) throws JniException;
763 * The natively implemented method to put a task on the channel of an host.
765 * @param channel The channel where to put the task.
766 * @param task The task to put.
767 * @param host The host of the specified channel.
769 * @exception InvalidTaskException if the task is not valid.
770 * InvalidHostException if the host is not valid.
771 * MsgException if the operation failed.
775 final static native void channelPut(Channel channel,Task task,Host host) throws JniException,NativeException;
778 * The natively implemented method to put a task on a channel of an host (with a timeout
779 * on the waiting of the destination host) and waits for the end of the transmission.
781 * @param channel The channel where to put the task.
782 * @param task The task to put.
783 * @param host The host containing the channel.
784 * @param timeout The timeout of the transmission.
786 * @exception InvalidTaskException if the task is not valid.
787 * InvalidHostException if the host is not valid.
788 * MsgException if the operation failed.
790 * @see Channel.putWithTimeout()
792 final static native void channelPutWithTimeout(Channel channel,Task task,Host host,double timeout) throws JniException,NativeException;
795 * The natively implemented method to put a task on channel with a bounded transmition
798 * @param channel The channel where to put the task.
799 * @param task The task to put.
800 * @param host The host containing the channel.
801 * @param max_rate The bounded transmition rate.
803 * @exception InvalidTaskException if the task is not valid.
804 * InvalidHostException if the host is not valid.
805 * MsgException if the operation failed.
807 * @see Channel.putBounded()
809 final static native void channelPutBounded(Channel channel,Task task,Host host,double max_rate) throws JniException,NativeException;
812 * The natively implemented method to wait for at most timeout seconds for a task reception
813 * on channel. The PID is updated with the PID of the first process.
815 * @param channel The channel concerned by the operation.
816 * @param timeout The maximum time to wait for a task before
819 * @exception MsgException if the reception operation failed.
821 * @see Channel.wait()
823 final static native int channelWait(Channel channel, double timeout) throws JniException,NativeException;
826 * The natively implemented method to set the number of channel used by all the process
829 * @param channelNumber The channel number of the process.
831 * @see Channel.setNumber()
833 final static native void channelSetNumber(int channelNumber);
836 * The natively implemented method to get the number of channel of the process of the simulation.
838 * @return The number of channel per process.
840 * @see Channel.getNumber()
842 final static native int channelGetNumber();
844 /*********************************************************************************
845 * Additional native methods *
846 **********************************************************************************/
849 * The natively implemented method to get the simulation time.
851 * @param The simulation time.
853 public final static native double getClock();
855 public final static native void pajeOutput(String pajeFile);
858 public final static native void info(String s);
860 /*********************************************************************
861 * The natively implemented methods connected to the MSG simulation *
862 *********************************************************************/
865 * The natively implemented method to initialize a MSG simulation.
867 * @param args The arguments of the command line of the simulation.
871 public final static native void init(String[] args);
874 * Run the MSG simulation, and cleanup everything afterward.
876 * If you want to chain simulations in the same process, you
877 * should call again createEnvironment and deployApplication afterward.
879 * @see MSG_run, MSG_clean
881 public final static native void run() throws NativeException;
884 * The native implemented method to create the environment of the simulation.
886 * @param platformFile The XML file which contains the description of the environment of the simulation
889 public final static native void createEnvironment(String platformFile) throws NativeException;
893 * The method to deploy the simulation.
895 * @param appFile The XML file which contains the description of the application to deploy.
899 public static void deployApplication(String platformFile) {
901 Class c = Class.forName("com.sun.org.apache.xerces.internal.parsers.SAXParser");
902 XMLReader reader = (XMLReader)c.newInstance();
903 reader.setEntityResolver(new DTDResolver());
904 ApplicationHandler handler = new ApplicationHandler();
905 reader.setContentHandler(handler);
906 reader.setFeature("http://xml.org/sax/features/validation", false);
907 reader.parse(platformFile);
909 } catch(Exception e) {
910 /* FIXME: do not swallow exception ! */
911 System.out.println("Exception in Msg.launchApplication()");
912 System.out.println(e);
918 static public void main(String[]args) throws MsgException {
919 /* initialize the MSG simulation. Must be done before anything else (even logging). */
922 if(args.length < 2) {
924 Msg.info("Usage: Msg platform_file deployment_file");
928 /* specify the number of channel for the process of the simulation. */
929 Channel.setNumber(1);
931 /* Load the platform and deploy the application */
932 Msg.createEnvironment(args[0]);
933 Msg.deployApplication(args[1]);
935 /* Execute the simulation */