Logo AND Algorithmique Numérique Distribuée

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