Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4e9247b0c4fda33e77dcb1fb909954a3a2b2ada1
[simgrid.git] / src / bindings / java / org / simgrid / msg / Process.java
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 package org.simgrid.msg;
8
9 import java.util.Arrays;
10 import java.util.ArrayList;
11
12 /**
13  * A process may be defined as a code, with some private data, executing 
14  * in a location (host). All the process used by your simulation must be
15  * declared in the deployment file (XML format).
16  * To create your own process you must inherit your own process from this 
17  * class and override the method "main()". For example if you want to use 
18  * a process named Slave proceed as it :
19  *
20  * (1) import the class Process of the package simgrid.msg
21  * import simgrid.msg.Process;
22  * 
23  * public class Slave extends simgrid.msg.Process {
24  *
25  *  (2) Override the method function
26  * 
27  *  \verbatim
28  *      public void main(String[] args) {
29  *              System.out.println("Hello MSG");
30  *      }
31  *  \endverbatim
32  * }
33  * The name of your process must be declared in the deployment file of your simulation.
34  * For the example, for the previous process Slave this file must contains a line :
35  * <process host="Maxims" function="Slave"/>, where Maxims is the host of the process
36  * Slave. All the process of your simulation are automatically launched and managed by Msg.
37  * A process use tasks to simulate communications or computations with another process. 
38  * For more information see Task. For more information on host concept 
39  * see Host.
40  * 
41  */
42
43 public abstract class Process implements Runnable {
44         /**
45          * This attribute represents a bind between a java process object and
46          * a native process. Even if this attribute is public you must never
47          * access to it. It is set automatically during the build of the object.
48          */
49         private long bind = 0;
50         /** Indicates if the process is started */
51
52         /** Time at which the process should be created  */
53         protected double startTime = 0;
54         /** Time at which the process should be killed.
55          * 
56          * Set at creation, and used internally by SimGrid
57          */
58         private double killTime = -1;
59
60         private String name = null;
61         
62         private int pid = -1;
63         private int ppid = -1;
64         private Host host = null;
65
66         /** The arguments of the method function of the process. */
67         private ArrayList<String> args = new ArrayList<>();
68
69         /**
70          * Constructs a new process from the name of a host and his name. The method
71          * function of the process doesn't have argument.
72          *
73          * @param hostname              Where to create the process.
74          * @param name                  The name of the process.
75          *
76          * @exception                   HostNotFoundException  if no host with this name exists.
77          *                      
78          *
79          */
80         public Process(String hostname, String name) throws HostNotFoundException {
81                 this(Host.getByName(hostname), name, null);
82         }
83         /**
84          * Constructs a new process from the name of a host and his name. The arguments
85          * of the method function of the process are specified by the parameter args.
86          *
87          * @param hostname              Where to create the process.
88          * @param name                  The name of the process.
89          * @param args                  The arguments of the main function of the process.
90          *
91          * @exception                   HostNotFoundException  if no host with this name exists.
92          *
93          */ 
94         public Process(String hostname, String name, String[] args) throws HostNotFoundException {
95                 this(Host.getByName(hostname), name, args);
96         }
97         /**
98          * Constructs a new process from a host and his name. The method function of the 
99          * process doesn't have argument.
100          *
101          * @param host                  Where to create the process.
102          * @param name                  The name of the process.
103          *
104          */
105         public Process(Host host, String name) {
106                 this(host, name, null);
107         }
108         /**
109          * Constructs a new process from a host and his name, the arguments of here method function are
110          * specified by the parameter args.
111          *
112          * @param host                  Where to create the process.
113          * @param name                  The name of the process.
114          * @param args                  The arguments of main method of the process.
115          */     
116         public Process(Host host, String name, String[]args) 
117         {
118                 if (host == null)
119                         throw new NullPointerException("Cannot create a process on the null host");
120                 if (name == null)
121                         throw new NullPointerException("Process name cannot be null");
122                 
123                 this.host = host;
124                 this.name = name;
125
126                 this.args = new ArrayList<>();
127                 if (null != args)
128                         this.args.addAll(Arrays.asList(args));
129         }
130         /**
131          * Constructs a new process from a host and his name, the arguments of here method function are
132          * specified by the parameter args.
133          *
134          * @param host                  Where to create the process.
135          * @param name                  The name of the process.
136          * @param args                  The arguments of main method of the process.
137          * @param startTime             Start time of the process
138          * @param killTime              Kill time of the process
139          *
140          */
141         public Process(Host host, String name, String[]args, double startTime, double killTime) {
142                 this(host, name, args);
143                 this.startTime = startTime;
144                 this.killTime = killTime;
145         }
146         /**
147          * The native method to create an MSG process.
148          * @param host    where to create the process.
149          */
150         protected native void create(Host host);
151         
152         /**
153          * This method kills all running process of the simulation.
154          *
155          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
156          *                                              and a positive number will be used to set the PID of the next newly
157          *                                              created process.
158          *
159          * @return                              The function returns the PID of the next created process.
160          *                      
161          */ 
162         public static native int killAll(int resetPID);
163
164         /** Simply kills the receiving process.
165          *
166          * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
167          */
168         public native void kill();
169         public static void kill(Process p) {
170                 p.kill();
171         }
172         
173         /** Suspends the process. See {@link #resume()} to resume it afterward */
174         public native void suspend();
175         /** Resume a process that was suspended by {@link #suspend()}. */
176         public native void resume();    
177         /** Tests if a process is suspended.
178          *
179          * @see #suspend()
180          * @see #resume()
181          */
182         public native boolean isSuspended();
183         
184         /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
185         public static native void yield();
186         
187         /**
188          * Specify whether the process should restart when its host restarts after a failure
189          * 
190          * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
191          * Otherwise, it just disappears when the host stops.
192          */
193         public native void setAutoRestart(boolean autoRestart);
194         /** Restarts the process from the beginning */
195         public native void restart();
196         /**
197          * Returns the name of the process
198          */
199         public String getName() {
200                 return this.name;
201         }
202         /**
203          * Returns the host of the process.
204          * @return                              The host instance of the process.
205          */ 
206         public Host getHost() {
207                 return this.host;
208         }
209         /**
210          * This static method gets a process from a PID.
211          *
212          * @param pid                   The process identifier of the process to get.
213          *
214          * @return                              The process with the specified PID.
215          */ 
216         public static native Process fromPID(int pid);
217         /**
218          * This method returns the PID of the process.
219          *
220          * @return                              The PID of the process.
221          *
222          */ 
223         public int getPID()  {
224                 return pid;
225         }
226         /**
227          * This method returns the PID of the parent of a process.
228          *
229          * @return                              The PID of the parent of the process.
230          *
231          */ 
232         public int getPPID()  {
233                 return ppid;
234         }
235         /**
236          * Returns the value of a given process property. 
237          */
238         public native String getProperty(String name);
239
240         /**
241          * Set the kill time of the process
242          * @param killTime the time when the process is killed
243          */
244         public native void setKillTime(double killTime);
245
246         /**
247          * This static method returns the currently running process.
248          *
249          * @return                              The current process.
250          *
251          */ 
252         public static native Process getCurrentProcess();
253         /**
254          * Migrates a process to another host.
255          *
256          * @param host                  The host where to migrate the process.
257          *
258          */
259         public native void migrate(Host host);  
260         /**
261          * Makes the current process sleep until millis milliseconds have elapsed.
262          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
263          * FIXME: Not optimal, maybe we should have two native functions.
264          * @param millis the length of time to sleep in milliseconds.
265          */
266         public static void sleep(long millis) throws HostFailureException  {
267                 sleep(millis,0);
268         }
269         /**
270          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
271          * have elapsed.
272          * Unlike {@link #waitFor(double)} which takes seconds, this method takes 
273          * milliseconds and nanoseconds.
274          * Overloads Thread.sleep.
275          * @param millis the length of time to sleep in milliseconds.
276          * @param nanos additional nanoseconds to sleep.
277          */
278         public static native void sleep(long millis, int nanos) throws HostFailureException;
279         /**
280          * Makes the current process sleep until time seconds have elapsed.
281          * @param seconds               The time the current process must sleep.
282          */ 
283         public native void waitFor(double seconds) throws HostFailureException;    
284         /**
285          * This method actually creates and run the process.
286          * It is a noop if the process is already launched.
287          * @throws HostNotFoundException
288          */
289         public final void start() throws HostNotFoundException {
290                 if (bind == 0)
291                          create(host);
292         }
293
294         /** This method runs the process. It calls the method function that you must overwrite. */
295         @Override
296         public void run() {
297
298                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
299
300                 try {
301                         args = new String[this.args.size()];
302                         if (!this.args.isEmpty()) {
303                                 this.args.toArray(args);
304                         }
305
306                         this.main(args);
307                 }
308                 catch(MsgException e) {
309                         e.printStackTrace();
310                         Msg.info("Unexpected behavior. Stopping now");
311                         System.exit(1);
312                 }
313                 catch(ProcessKilledError pk) {
314                         /* The process was killed before its end. With a kill() or something. */
315                 }       
316         }
317
318         /**
319          * The main function of the process (to implement).
320          *
321          * @param args
322          * @throws MsgException
323          */
324         public abstract void main(String[]args) throws MsgException;
325
326         /** Stops the execution of the current actor */
327         public void exit() {
328                 this.kill();
329         }
330         /**
331          * Class initializer, to initialize various JNI stuff
332          */
333         private static native void nativeInit();
334         static {
335                 org.simgrid.NativeLib.nativeInit();
336                 nativeInit();
337         }
338         /**
339          * This static method returns the current amount of processes running
340          *
341          * @return                      The count of the running processes
342          */ 
343         public static native int getCount();
344
345 }