Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove deprecated restart function
[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          * Resumes a suspended process by resuming the task on which it was
244          * waiting for the completion.
245          */
246         public native void resume();    
247         /**
248          * Tests if a process is suspended.
249          *
250          * @return                              The method returns true if the process is suspended.
251          *                                              Otherwise the method returns false.
252          */ 
253         public native boolean isSuspended();
254         /**
255          * Returns the name of the process
256          */
257         public String msgName() {
258                 return this.name;
259         }
260         /**
261          * Returns the host of the process.
262          * @return                              The host instance of the process.
263          */ 
264         public Host getHost() {
265                 return this.host;
266         }
267         /**
268          * This static method gets a process from a PID.
269          *
270          * @param PID                   The process identifier of the process to get.
271          *
272          * @return                              The process with the specified PID.
273          *
274          * @exception                   NativeException on error in the native SimGrid code
275          */ 
276         public static native Process fromPID(int PID) throws NativeException;
277         /**
278          * This method returns the PID of the process.
279          *
280          * @return                              The PID of the process.
281          *
282          */ 
283         public int getPID()  {
284                 return pid;
285         }
286         /**
287          * This method returns the PID of the parent of a process.
288          *
289          * @return                              The PID of the parent of the process.
290          *
291          */ 
292         public int getPPID()  {
293                 return ppid;
294         }
295         /**
296          * Set the kill time of the process
297          * @param killTime the time when the process is killed
298          */
299         public native void setKillTime(double killTime);
300         
301         /**
302          * This static method returns the currently running process.
303          *
304          * @return                              The current process.
305          *
306          */ 
307         public static native Process currentProcess();
308         /**
309          * Migrates a process to another host.
310          *
311          * @param process               The process to migrate.
312          * @param host                  The host where to migrate the process.
313          *
314          */
315         public native void migrate(Host host);  
316         /**
317          * Makes the current process sleep until millis millisecondes have elapsed.
318          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
319          * FIXME: Not optimal, maybe we should have two native functions.
320          * @param millis the length of time to sleep in milliseconds.
321          */
322         public static void sleep(long millis) throws HostFailureException  {
323                 sleep(millis,0);
324         }
325         /**
326          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
327          * have elapsed.
328          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds and nanoseconds.
329          * Overloads Thread.sleep.
330          * @param millis the length of time to sleep in milliseconds.
331          * @param nanos additionnal nanoseconds to sleep.
332          */
333         public native static void sleep(long millis, int nanos) throws HostFailureException;
334         /**
335          * Makes the current process sleep until time seconds have elapsed.
336          * @param seconds               The time the current process must sleep.
337          */ 
338         public native void waitFor(double seconds) throws HostFailureException;    
339         /**
340      *
341      */
342     public void showArgs() {
343                 Msg.info("[" + this.name + "/" + this.getHost().getName() + "] argc=" +
344                                 this.args.size());
345                 for (int i = 0; i < this.args.size(); i++)
346                         Msg.info("[" + this.msgName() + "/" + this.getHost().getName() +
347                                         "] args[" + i + "]=" + (String) (this.args.get(i)));
348         }
349     /**
350      * This method actually creates and run the process.
351      * It is a noop if the process is already launched.
352      * @throws HostNotFoundException
353      */
354     public final void start() throws HostNotFoundException {
355         if (!started) {
356                 started = true;
357                 create(host.getName());
358         }
359     }
360     
361         /**
362          * This method runs the process. Il calls the method function that you must overwrite.
363          */
364         public void run() {
365
366                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
367                 //waitSignal(); /* wait for other people to fill the process in */
368
369                 try {
370                         args = new String[this.args.size()];
371                         if (this.args.size() > 0) {
372                                 this.args.toArray(args);
373                         }
374
375                         this.main(args);
376                 } catch(MsgException e) {
377                         e.printStackTrace();
378                         Msg.info("Unexpected behavior. Stopping now");
379                         System.exit(1);
380                 }
381                  catch(ProcessKilledError pk) {
382                          
383                  }      
384                 exit();
385         }
386
387         /**
388          * The main function of the process (to implement).
389      *
390      * @param args
391      * @throws MsgException
392      */
393         public abstract void main(String[]args) throws MsgException;
394
395         public native void exit();    
396         /**
397          * Class initializer, to initialize various JNI stuff
398          */
399         public static native void nativeInit();
400         static {
401                 nativeInit();
402         }
403 }