Logo AND Algorithmique Numérique Distribuée

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