Logo AND Algorithmique Numérique Distribuée

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