Logo AND Algorithmique Numérique Distribuée

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