Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add Process.setAutoRestart
[simgrid.git] / org / simgrid / msg / Process.java
1 /*
2  * Copyright 2006-2012 The SimGrid team
3  * All right reserved. 
4  *
5  * This program is free software; you can redistribute 
6  * it and/or modify it under the terms of the license 
7  * (GNU LGPL) which comes with this package.
8  */
9
10 package org.simgrid.msg;
11  
12 import java.util.Arrays;
13 import java.util.Hashtable;
14 import java.util.Vector;
15 import java.lang.Runnable;
16 import java.util.concurrent.Semaphore;
17
18 /**
19  * A process may be defined as a code, with some private data, executing 
20  * in a location (host). All the process used by your simulation must be
21  * declared in the deployment file (XML format).
22  * To create your own process you must inherit your own process from this 
23  * class and override the method "main()". For example if you want to use 
24  * a process named Slave proceed as it :
25  *
26  * (1) import the class Process of the package simgrid.msg
27  * import simgrid.msg.Process;
28  * 
29  * public class Slave extends simgrid.msg.Process {
30  *
31  *  (2) Override the method function
32  * 
33  *  \verbatim
34  *      public void main(String[] args) {
35  *              System.out.println("Hello MSG");
36  *      }
37  *  \endverbatim
38  * }
39  * The name of your process must be declared in the deployment file of your simulation.
40  * For the example, for the previous process Slave this file must contains a line :
41  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
42  * Slave. All the process of your simulation are automatically launched and managed by Msg.
43  * A process use tasks to simulate communications or computations with another process. 
44  * For more information see Task. For more information on host concept 
45  * see Host.
46  * 
47  */
48
49 public abstract class Process implements Runnable {
50         /**
51          * This attribute represents a bind between a java process object and
52          * a native process. Even if this attribute is public you must never
53          * access to it. It is set automatically during the build of the object.
54          */
55         private long bind;
56         /**
57          * Indicates if the process is started
58          */
59         boolean started;
60         /**
61          * Even if this attribute is public you must never access to it.
62          * It is used to compute the id of an MSG process.
63          */
64         public static long nextProcessId = 0;
65         
66         /**
67          * Even if this attribute is public you must never access to it.
68          * It is compute automatically during the creation of the object. 
69          * The native functions use this identifier to synchronize the process.
70          */
71         public long id;
72         
73         /**
74          * Start time of the process
75          */
76         public double startTime = 0;
77         /**
78          * Kill time of the process
79          */
80         public double killTime = -1;
81
82     public Hashtable<String,String> properties;
83
84         /**
85          * The name of the process.                                                     
86          */
87         protected String name;
88         /**
89           * The PID of the process
90           */
91         protected int pid = -1;
92         /**
93          * The PPID of the process 
94          */
95         protected int ppid = -1;
96         /**
97          * The host of the process
98          */
99         protected Host host = null;
100
101         /** The arguments of the method function of the process. */     
102         public Vector<String> args;
103
104         
105         /**
106          * Default constructor (used in ApplicationHandler to initialize it)
107          */
108         protected Process() {
109                 this.id = nextProcessId++;
110                 this.name = null;
111                 this.bind = 0;
112                 this.args = new Vector<String>();
113                 this.properties = null;
114         }
115
116
117         /**
118          * Constructs a new process from the name of a host and his name. The method
119          * function of the process doesn't have argument.
120          *
121          * @param hostname              The name of the host of the process to create.
122          * @param name                  The name of the process.
123          *
124          * @exception                   HostNotFoundException  if no host with this name exists.
125          *                      
126          *
127          */
128         public Process(String hostname, String name) throws HostNotFoundException {
129                 this(Host.getByName(hostname), name, null);
130         }
131         /**
132          * Constructs a new process from the name of a host and his name. The arguments
133          * of the method function of the process are specified by the parameter args.
134          *
135          * @param hostname              The name of the host of the process to create.
136          * @param name                  The name of the process.
137          * @param args                  The arguments of the main function of the process.
138          *
139          * @exception                   HostNotFoundException  if no host with this name exists.
140      *                      NativeException
141      * @throws NativeException
142          *
143          */ 
144         public Process(String hostname, String name, String args[]) throws HostNotFoundException, NativeException {
145                 this(Host.getByName(hostname), name, args);
146         }
147         /**
148          * Constructs a new process from a host and his name. The method function of the 
149          * process doesn't have argument.
150          *
151          * @param host                  The host of the process to create.
152          * @param name                  The name of the process.
153          *
154          */
155         public Process(Host host, String name) {
156                 this(host, name, null);
157         }
158         /**
159          * Constructs a new process from a host and his name, the arguments of here method function are
160          * specified by the parameter args.
161          *
162          * @param host                  The host of the process to create.
163          * @param name                  The name of the process.
164          * @param args                  The arguments of main method of the process.
165          */     
166         public Process(Host host, String name, String[]args) {
167                 this();
168                 this.host = host;
169                 if (name == null)
170                         throw new NullPointerException("Process name cannot be NULL");
171                 this.name = name;
172
173                 this.args = new Vector<String>();
174                 if (null != args)
175                         this.args.addAll(Arrays.asList(args));
176                 
177                 this.properties = new Hashtable<String,String>();
178         }       
179         /**
180          * Constructs a new process from a host and his name, the arguments of here method function are
181          * specified by the parameter args.
182          *
183          * @param host                  The host of the process to create.
184          * @param name                  The name of the process.
185          * @param args                  The arguments of main method of the process.
186          * @param startTime             Start time of the process
187          * @param killTime              Kill time of the process
188          *
189          */
190         public Process(Host host, String name, String[]args, double startTime, double killTime) {
191                 this();
192                 this.host = host;
193                 if (name == null)
194                         throw new NullPointerException("Process name cannot be NULL");
195                 this.name = name;
196
197                 this.args = new Vector<String>();
198                 if (null != args)
199                         this.args.addAll(Arrays.asList(args));
200                 
201                 this.properties = new Hashtable<String,String>();
202                 
203                 this.startTime = startTime;
204                 this.killTime = killTime;               
205         }
206         /**
207          * The natively implemented method to create an MSG process.
208          * @param host    A valid (binded) host where create the process.
209          */
210         protected native void create(String hostName) throws HostNotFoundException;
211         /**
212          * This method kills all running process of the simulation.
213          *
214          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
215          *                                              and a positive number will be used to set the PID of the next newly
216          *                                              created process.
217          *
218          * @return                              The function returns the PID of the next created process.
219          *                      
220          */ 
221         public static native int killAll(int resetPID);
222
223         /**
224          * This method kill a process.
225          *
226          */
227         public native void kill();
228         /**
229          * Suspends the process by suspending the task on which it was
230          * waiting for the completion.
231          */
232         public native void suspend();
233         /**
234          * Suspends the process by suspending the task on which it was
235          * waiting for the completion.
236          * DEPRECATED: use suspend instead.
237          */
238         @Deprecated
239         public void pause() {
240                 suspend();
241         }
242         /**
243          * Sets the "auto-restart" flag of the process.
244          */
245         public native void setAutoRestart(boolean autoRestart);
246         /**
247          * Restarts the process from the beginning
248          */
249         public native void restart();
250         /**
251          * Resumes a suspended process by resuming the task on which it was
252          * waiting for the completion.
253          */
254         public native void resume();    
255         /**
256          * Tests if a process is suspended.
257          *
258          * @return                              The method returns true if the process is suspended.
259          *                                              Otherwise the method returns false.
260          */ 
261         public native boolean isSuspended();
262         /**
263          * Returns the name of the process
264          */
265         public String msgName() {
266                 return this.name;
267         }
268         /**
269          * Returns the host of the process.
270          * @return                              The host instance of the process.
271          */ 
272         public Host getHost() {
273                 return this.host;
274         }
275         /**
276          * This static method gets a process from a PID.
277          *
278          * @param PID                   The process identifier of the process to get.
279          *
280          * @return                              The process with the specified PID.
281          *
282          * @exception                   NativeException on error in the native SimGrid code
283          */ 
284         public static native Process fromPID(int PID) throws NativeException;
285         /**
286          * This method returns the PID of the process.
287          *
288          * @return                              The PID of the process.
289          *
290          */ 
291         public int getPID()  {
292                 return pid;
293         }
294         /**
295          * This method returns the PID of the parent of a process.
296          *
297          * @return                              The PID of the parent of the process.
298          *
299          */ 
300         public int getPPID()  {
301                 return ppid;
302         }
303         /**
304          * Set the kill time of the process
305          * @param killTime the time when the process is killed
306          */
307         public native void setKillTime(double killTime);
308         
309         /**
310          * This static method returns the currently running process.
311          *
312          * @return                              The current process.
313          *
314          */ 
315         public static native Process currentProcess();
316         /**
317          * Migrates a process to another host.
318          *
319          * @param process               The process to migrate.
320          * @param host                  The host where to migrate the process.
321          *
322          */
323         public native void migrate(Host host);  
324         /**
325          * Makes the current process sleep until millis millisecondes have elapsed.
326          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
327          * FIXME: Not optimal, maybe we should have two native functions.
328          * @param millis the length of time to sleep in milliseconds.
329          */
330         public static void sleep(long millis) throws HostFailureException  {
331                 sleep(millis,0);
332         }
333         /**
334          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
335          * have elapsed.
336          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
337          * Overloads Thread.sleep.
338          * @param millis the length of time to sleep in milliseconds.
339          * @param nanos additionnal nanoseconds to sleep.
340          */
341         public native static void sleep(long millis, int nanos) throws HostFailureException;
342         /**
343          * Makes the current process sleep until time seconds have elapsed.
344          * @param seconds               The time the current process must sleep.
345          */ 
346         public native void waitFor(double seconds) throws HostFailureException;    
347         /**
348      *
349      */
350     public void showArgs() {
351                 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
352                                 this.args.size());
353                 for (int i = 0; i < this.args.size(); i++)
354                         Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
355                                         "] args[" + i + "]=" + (String) (this.args.get(i)));
356         }
357     /**
358      * This method actually creates and run the process.
359      * It is a noop if the process is already launched.
360      * @throws HostNotFoundException
361      */
362     public final void start() throws HostNotFoundException {
363         if (!started) {
364                 started = true;
365                 create(host.getName());
366         }
367     }
368     
369         /**
370          * This method runs the process. Il calls the method function that you must overwrite.
371          */
372         public void run() {
373
374                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
375                 //waitSignal(); /* wait for other people to fill the process in */
376
377                 try {
378                         args = new String[this.args.size()];
379                         if (this.args.size() > 0) {
380                                 this.args.toArray(args);
381                         }
382
383                         this.main(args);
384                 } catch(MsgException e) {
385                         e.printStackTrace();
386                         Msg.info("Unexpected behavior. Stopping now");
387                         System.exit(1);
388                 }
389                  catch(ProcessKilledError pk) {
390                          
391                  }      
392                 exit();
393         }
394
395         /**
396          * The main function of the process (to implement).
397      *
398      * @param args
399      * @throws MsgException
400      */
401         public abstract void main(String[]args) throws MsgException;
402
403         public native void exit();    
404         /**
405          * Class initializer, to initialize various JNI stuff
406          */
407         public static native void nativeInit();
408         static {
409                 nativeInit();
410         }
411 }