Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f96cf87ed9dd4ec8b4f32fc5177b2cf35e0aff15
[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         boolean started;
52         /**
53          * Even if this attribute is public you must never access to it.
54          * It is used to compute the id of an MSG process.
55          */
56         private static long nextProcessId = 0;
57
58         /**
59          * Even if this attribute is public you must never access to it.
60          * It is compute automatically during the creation of the object. 
61          * The native functions use this identifier to synchronize the process.
62          */
63         private long id;
64
65         /** Time at which the process should be created  */
66         protected double startTime = 0;
67         /** Time at which the process should be killed.
68          * 
69          * Set at creation, and used internally by SimGrid
70          */
71         private double killTime = -1;
72
73         private String name = null;
74         
75         private int pid = -1;
76         private int ppid = -1;
77         private Host host = null;
78
79         /** The arguments of the method function of the process. */
80         private ArrayList<String> args = new ArrayList<>();
81
82         /**
83          * Constructs a new process from the name of a host and his name. The method
84          * function of the process doesn't have argument.
85          *
86          * @param hostname              Where to create the process.
87          * @param name                  The name of the process.
88          *
89          * @exception                   HostNotFoundException  if no host with this name exists.
90          *                      
91          *
92          */
93         public Process(String hostname, String name) throws HostNotFoundException {
94                 this(Host.getByName(hostname), name, null);
95         }
96         /**
97          * Constructs a new process from the name of a host and his name. The arguments
98          * of the method function of the process are specified by the parameter args.
99          *
100          * @param hostname              Where to create the process.
101          * @param name                  The name of the process.
102          * @param args                  The arguments of the main function of the process.
103          *
104          * @exception                   HostNotFoundException  if no host with this name exists.
105          *
106          */ 
107         public Process(String hostname, String name, String[] args) throws HostNotFoundException {
108                 this(Host.getByName(hostname), name, args);
109         }
110         /**
111          * Constructs a new process from a host and his name. The method function of the 
112          * process doesn't have argument.
113          *
114          * @param host                  Where to create the process.
115          * @param name                  The name of the process.
116          *
117          */
118         public Process(Host host, String name) {
119                 this(host, name, null);
120         }
121         /**
122          * Constructs a new process from a host and his name, the arguments of here method function are
123          * specified by the parameter args.
124          *
125          * @param host                  Where to create the process.
126          * @param name                  The name of the process.
127          * @param args                  The arguments of main method of the process.
128          */     
129         public Process(Host host, String name, String[]args) 
130         {
131                 if (host == null)
132                         throw new NullPointerException("Cannot create a process on the null host");
133                 if (name == null)
134                         throw new NullPointerException("Process name cannot be null");
135                 
136                 this.id = nextProcessId++;
137                 this.host = host;
138                 this.name = name;
139
140                 this.args = new ArrayList<>();
141                 if (null != args)
142                         this.args.addAll(Arrays.asList(args));
143         }
144         /**
145          * Constructs a new process from a host and his name, the arguments of here method function are
146          * specified by the parameter args.
147          *
148          * @param host                  Where to create the process.
149          * @param name                  The name of the process.
150          * @param args                  The arguments of main method of the process.
151          * @param startTime             Start time of the process
152          * @param killTime              Kill time of the process
153          *
154          */
155         public Process(Host host, String name, String[]args, double startTime, double killTime) {
156                 this(host, name, args);
157                 this.startTime = startTime;
158                 this.killTime = killTime;
159         }
160         /**
161          * The natively implemented method to create an MSG process.
162          * @param hostName    A valid (bound) host where create the process.
163          */
164         protected native void create(String hostName) throws HostNotFoundException;
165         /**
166          * This method kills all running process of the simulation.
167          *
168          * @param resetPID              Should we reset the PID numbers. A negative number means no reset
169          *                                              and a positive number will be used to set the PID of the next newly
170          *                                              created process.
171          *
172          * @return                              The function returns the PID of the next created process.
173          *                      
174          */ 
175         public static native int killAll(int resetPID);
176
177         /** Simply kills the receiving process.
178          *
179          * SimGrid sometimes have issues when you kill processes that are currently communicating and such. We are working on it to fix the issues.
180          */
181         public native void kill();
182         public static void kill(Process p) {
183                 p.kill();
184         }
185         
186         /** Suspends the process. See {@link #resume()} to resume it afterward */
187         public native void suspend();
188         /** Resume a process that was suspended by {@link #suspend()}. */
189         public native void resume();    
190         /** Tests if a process is suspended.
191          *
192          * @see #suspend()
193          * @see #resume()
194          */
195         public native boolean isSuspended();
196         
197         /** Yield the current process. All other processes that are ready at the same timestamp will be executed first */
198         public static native void yield();
199         
200         /**
201          * Specify whether the process should restart when its host restarts after a failure
202          * 
203          * A process naturally stops when its host stops. It starts again only if autoRestart is set to true.
204          * Otherwise, it just disappears when the host stops.
205          */
206         public native void setAutoRestart(boolean autoRestart);
207         /** Restarts the process from the beginning */
208         public native void restart();
209         /**
210          * Returns the name of the process
211          */
212         public String getName() {
213                 return this.name;
214         }
215         /**
216          * Returns the host of the process.
217          * @return                              The host instance of the process.
218          */ 
219         public Host getHost() {
220                 return this.host;
221         }
222         /**
223          * This static method gets a process from a PID.
224          *
225          * @param pid                   The process identifier of the process to get.
226          *
227          * @return                              The process with the specified PID.
228          */ 
229         public static native Process fromPID(int pid);
230         /**
231          * This method returns the PID of the process.
232          *
233          * @return                              The PID of the process.
234          *
235          */ 
236         public int getPID()  {
237                 return pid;
238         }
239         /**
240          * This method returns the PID of the parent of a process.
241          *
242          * @return                              The PID of the parent of the process.
243          *
244          */ 
245         public int getPPID()  {
246                 return ppid;
247         }
248         /**
249          * Returns the value of a given process property. 
250          */
251         public native String getProperty(String name);
252
253         /**
254          * Set the kill time of the process
255          * @param killTime the time when the process is killed
256          */
257         public native void setKillTime(double killTime);
258
259         /**
260          * This static method returns the currently running process.
261          *
262          * @return                              The current process.
263          *
264          */ 
265         public static native Process getCurrentProcess();
266         /**
267          * Migrates a process to another host.
268          *
269          * @param host                  The host where to migrate the process.
270          *
271          */
272         public native void migrate(Host host);  
273         /**
274          * Makes the current process sleep until millis milliseconds have elapsed.
275          * You should note that unlike "waitFor" which takes seconds, this method takes milliseconds.
276          * FIXME: Not optimal, maybe we should have two native functions.
277          * @param millis the length of time to sleep in milliseconds.
278          */
279         public static void sleep(long millis) throws HostFailureException  {
280                 sleep(millis,0);
281         }
282         /**
283          * Makes the current process sleep until millis milliseconds and nanos nanoseconds 
284          * have elapsed.
285          * Unlike {@link #waitFor(double)} which takes seconds, this method takes 
286          * milliseconds and nanoseconds.
287          * Overloads Thread.sleep.
288          * @param millis the length of time to sleep in milliseconds.
289          * @param nanos additional nanoseconds to sleep.
290          */
291         public static native void sleep(long millis, int nanos) throws HostFailureException;
292         /**
293          * Makes the current process sleep until time seconds have elapsed.
294          * @param seconds               The time the current process must sleep.
295          */ 
296         public native void waitFor(double seconds) throws HostFailureException;    
297         /**
298          * This method actually creates and run the process.
299          * It is a noop if the process is already launched.
300          * @throws HostNotFoundException
301          */
302         public final void start() throws HostNotFoundException {
303                 if (!started) {
304                         started = true;
305                         create(host.getName());
306                 }
307         }
308
309         /** This method runs the process. It calls the method function that you must overwrite. */
310         @Override
311         public void run() {
312
313                 String[] args = null;      /* do not fill it before the signal or this.args will be empty */
314
315                 try {
316                         args = new String[this.args.size()];
317                         if (!this.args.isEmpty()) {
318                                 this.args.toArray(args);
319                         }
320
321                         this.main(args);
322                 }
323                 catch(MsgException e) {
324                         e.printStackTrace();
325                         Msg.info("Unexpected behavior. Stopping now");
326                         System.exit(1);
327                 }
328                 catch(ProcessKilledError pk) {
329                         /* The process was killed before its end. With a kill() or something. */
330                 }       
331         }
332
333         /**
334          * The main function of the process (to implement).
335          *
336          * @param args
337          * @throws MsgException
338          */
339         public abstract void main(String[]args) throws MsgException;
340
341         /** Stops the execution of the current actor */
342         public void exit() {
343                 this.kill();
344         }
345         /**
346          * Class initializer, to initialize various JNI stuff
347          */
348         private static native void nativeInit();
349         static {
350                 org.simgrid.NativeLib.nativeInit();
351                 nativeInit();
352         }
353         /**
354          * This static method returns the current amount of processes running
355          *
356          * @return                      The count of the running processes
357          */ 
358         public static native int getCount();
359
360 }