Logo AND Algorithmique Numérique Distribuée

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